From 3e3e091cdb403a54013dde1cc8a103fae6b0147a Mon Sep 17 00:00:00 2001 From: default Date: Wed, 23 Nov 2022 13:32:23 +0100 Subject: [PATCH] New function db_upgrade(). --- data.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++------- snac.h | 1 + utils.c | 8 +++++++- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/data.c b/data.c index 21cf41e..bb87885 100644 --- a/data.c +++ b/data.c @@ -13,6 +13,10 @@ #include #include +double db_layout = 2.0; + + +int db_upgrade(d_char **error); int srv_open(char *basedir) /* opens a server */ @@ -47,13 +51,10 @@ int srv_open(char *basedir) char *host; char *prefix; char *dbglvl; - char *layout; - double f = 0.0; host = xs_dict_get(srv_config, "host"); prefix = xs_dict_get(srv_config, "prefix"); dbglvl = xs_dict_get(srv_config, "dbglevel"); - layout = xs_dict_get(srv_config, "layout"); if (host == NULL || prefix == NULL) error = xs_str_new("ERROR: cannot get server data"); @@ -67,10 +68,7 @@ int srv_open(char *basedir) error = xs_fmt("DEBUG level set to %d from environment", dbglevel); } - if (!layout || (f = xs_number_get(layout)) != 2.0) - error = xs_fmt("ERROR: unsupported old disk layout %f\n", f); - else - ret = 1; + ret = db_upgrade(&error); } } @@ -1238,3 +1236,55 @@ void purge_all(void) } } } + + +int db_upgrade(d_char **error) +{ + int ret = 1; + int changed = 0; + double f = 0.0; + + do { + char *layout = xs_dict_get(srv_config, "layout"); + + f = xs_number_get(layout); + + if (f < 2.0) { + *error = xs_fmt("ERROR: unsupported old disk layout %lf\n", f); + ret = 0; + break; + } +/* else + if (f < 2.1) { + srv_log(xs_dup("upgrading db layout to version 2.1")); + + xs *dir = xs_fmt("%s/object", srv_basedir); + mkdir(dir, 0755); + + xs *nv = xs_number_new(2.1); + srv_config = xs_dict_set(srv_config, "layout", nv); + changed++; + }*/ + } while (f < db_layout); + + if (f > db_layout) { + *error = xs_fmt("ERROR: unknown future version %lf\n", f); + ret = 0; + } + + if (changed) { + /* upgrade the configuration file */ + xs *fn = xs_fmt("%s/server.json", srv_basedir); + FILE *f; + + if ((f = fopen(fn, "w")) != NULL) { + xs *j = xs_json_dumps_pp(srv_config, 4); + fwrite(j, strlen(j), 1, f); + fclose(f); + } + else + ret = 0; + } + + return ret; +} diff --git a/snac.h b/snac.h index 390d4c6..4221de5 100644 --- a/snac.h +++ b/snac.h @@ -5,6 +5,7 @@ #define USER_AGENT "snac/" VERSION +extern double db_layout; extern d_char *srv_basedir; extern d_char *srv_config; extern d_char *srv_baseurl; diff --git a/utils.c b/utils.c index 582a2f6..58c2246 100644 --- a/utils.c +++ b/utils.c @@ -18,7 +18,7 @@ const char *default_srv_config = "{" "\"prefix\": \"\"," "\"address\": \"127.0.0.1\"," "\"port\": 8001," - "\"layout\": 2," + "\"layout\": 0.0," "\"dbglevel\": 0," "\"queue_retry_minutes\": 2," "\"queue_retry_max\": 10," @@ -103,6 +103,9 @@ int initdb(const char *basedir) srv_config = xs_json_loads(default_srv_config); + xs *layout = xs_number_new(db_layout); + srv_config = xs_dict_set(srv_config, "layout", layout); + printf("Network address [%s]:\n", xs_dict_get(srv_config, "address")); { xs *i = xs_strip(xs_readline(stdin)); @@ -148,6 +151,9 @@ int initdb(const char *basedir) xs *udir = xs_fmt("%s/user", srv_basedir); mkdir(udir, 0755); + xs *odir = xs_fmt("%s/object", srv_basedir); + mkdir(odir, 0755); + xs *gfn = xs_fmt("%s/greeting.html", srv_basedir); if ((f = fopen(gfn, "w")) == NULL) { printf("ERROR: cannot create '%s'\n", gfn);