New function db_upgrade().

This commit is contained in:
default 2022-11-23 13:32:23 +01:00
parent 20235dfaa2
commit 3e3e091cdb
3 changed files with 65 additions and 8 deletions

64
data.c
View File

@ -13,6 +13,10 @@
#include <glob.h>
#include <sys/stat.h>
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;
}

1
snac.h
View File

@ -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;

View File

@ -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);