diff options
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | data.c | 67 | ||||
| -rw-r--r-- | upgrade.c | 78 |
3 files changed, 80 insertions, 68 deletions
| @@ -4,7 +4,8 @@ CFLAGS=-g -Wall | |||
| 4 | 4 | ||
| 5 | all: snac | 5 | all: snac |
| 6 | 6 | ||
| 7 | snac: snac.o main.o data.o http.o httpd.o webfinger.o activitypub.o html.o utils.o format.o | 7 | snac: snac.o main.o data.o http.o httpd.o webfinger.o \ |
| 8 | activitypub.o html.o utils.o format.o upgrade.o | ||
| 8 | $(CC) $(CFLAGS) -L/usr/local/lib *.o -lcurl -lcrypto -pthread -o $@ | 9 | $(CC) $(CFLAGS) -L/usr/local/lib *.o -lcurl -lcrypto -pthread -o $@ |
| 9 | 10 | ||
| 10 | .c.o: | 11 | .c.o: |
| @@ -1312,70 +1312,3 @@ void purge_all(void) | |||
| 1312 | } | 1312 | } |
| 1313 | } | 1313 | } |
| 1314 | } | 1314 | } |
| 1315 | |||
| 1316 | |||
| 1317 | int db_upgrade(d_char **error) | ||
| 1318 | { | ||
| 1319 | int ret = 1; | ||
| 1320 | int changed = 0; | ||
| 1321 | double f = 0.0; | ||
| 1322 | |||
| 1323 | for (;;) { | ||
| 1324 | char *layout = xs_dict_get(srv_config, "layout"); | ||
| 1325 | double nf; | ||
| 1326 | |||
| 1327 | f = nf = xs_number_get(layout); | ||
| 1328 | |||
| 1329 | if (!(f < db_layout)) | ||
| 1330 | break; | ||
| 1331 | |||
| 1332 | srv_log(xs_fmt("db_upgrade %1.1lf < %1.1lf", f, db_layout)); | ||
| 1333 | |||
| 1334 | if (f < 2.0) { | ||
| 1335 | *error = xs_fmt("ERROR: unsupported old disk layout %1.1lf\n", f); | ||
| 1336 | ret = 0; | ||
| 1337 | break; | ||
| 1338 | } | ||
| 1339 | else | ||
| 1340 | if (f < 2.1) { | ||
| 1341 | xs *dir = xs_fmt("%s/object", srv_basedir); | ||
| 1342 | mkdir(dir, 0755); | ||
| 1343 | |||
| 1344 | nf = 2.1; | ||
| 1345 | } | ||
| 1346 | |||
| 1347 | if (f < nf) { | ||
| 1348 | f = nf; | ||
| 1349 | xs *nv = xs_number_new(f); | ||
| 1350 | srv_config = xs_dict_set(srv_config, "layout", nv); | ||
| 1351 | |||
| 1352 | srv_log(xs_fmt("db_upgrade converted to version %1.1lf", f)); | ||
| 1353 | changed++; | ||
| 1354 | } | ||
| 1355 | else | ||
| 1356 | break; | ||
| 1357 | } | ||
| 1358 | |||
| 1359 | if (f > db_layout) { | ||
| 1360 | *error = xs_fmt("ERROR: unknown future version %lf\n", f); | ||
| 1361 | ret = 0; | ||
| 1362 | } | ||
| 1363 | |||
| 1364 | if (changed) { | ||
| 1365 | /* upgrade the configuration file */ | ||
| 1366 | xs *fn = xs_fmt("%s/server.json", srv_basedir); | ||
| 1367 | FILE *f; | ||
| 1368 | |||
| 1369 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 1370 | xs *j = xs_json_dumps_pp(srv_config, 4); | ||
| 1371 | fwrite(j, strlen(j), 1, f); | ||
| 1372 | fclose(f); | ||
| 1373 | |||
| 1374 | srv_log(xs_fmt("upgraded db %s after %d changes", fn, changed)); | ||
| 1375 | } | ||
| 1376 | else | ||
| 1377 | ret = 0; | ||
| 1378 | } | ||
| 1379 | |||
| 1380 | return ret; | ||
| 1381 | } | ||
diff --git a/upgrade.c b/upgrade.c new file mode 100644 index 0000000..e4c75bb --- /dev/null +++ b/upgrade.c | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | /* snac - A simple, minimalistic ActivityPub instance */ | ||
| 2 | /* copyright (c) 2022 grunfink - MIT license */ | ||
| 3 | |||
| 4 | #include "xs.h" | ||
| 5 | #include "xs_io.h" | ||
| 6 | #include "xs_json.h" | ||
| 7 | #include "xs_glob.h" | ||
| 8 | |||
| 9 | #include "snac.h" | ||
| 10 | |||
| 11 | #include <sys/stat.h> | ||
| 12 | |||
| 13 | |||
| 14 | int db_upgrade(d_char **error) | ||
| 15 | { | ||
| 16 | int ret = 1; | ||
| 17 | int changed = 0; | ||
| 18 | double f = 0.0; | ||
| 19 | |||
| 20 | for (;;) { | ||
| 21 | char *layout = xs_dict_get(srv_config, "layout"); | ||
| 22 | double nf; | ||
| 23 | |||
| 24 | f = nf = xs_number_get(layout); | ||
| 25 | |||
| 26 | if (!(f < db_layout)) | ||
| 27 | break; | ||
| 28 | |||
| 29 | srv_log(xs_fmt("db_upgrade %1.1lf < %1.1lf", f, db_layout)); | ||
| 30 | |||
| 31 | if (f < 2.0) { | ||
| 32 | *error = xs_fmt("ERROR: unsupported old disk layout %1.1lf\n", f); | ||
| 33 | ret = 0; | ||
| 34 | break; | ||
| 35 | } | ||
| 36 | else | ||
| 37 | if (f < 2.1) { | ||
| 38 | xs *dir = xs_fmt("%s/object", srv_basedir); | ||
| 39 | mkdir(dir, 0755); | ||
| 40 | |||
| 41 | nf = 2.1; | ||
| 42 | } | ||
| 43 | |||
| 44 | if (f < nf) { | ||
| 45 | f = nf; | ||
| 46 | xs *nv = xs_number_new(f); | ||
| 47 | srv_config = xs_dict_set(srv_config, "layout", nv); | ||
| 48 | |||
| 49 | srv_log(xs_fmt("db_upgrade converted to version %1.1lf", f)); | ||
| 50 | changed++; | ||
| 51 | } | ||
| 52 | else | ||
| 53 | break; | ||
| 54 | } | ||
| 55 | |||
| 56 | if (f > db_layout) { | ||
| 57 | *error = xs_fmt("ERROR: unknown future version %lf\n", f); | ||
| 58 | ret = 0; | ||
| 59 | } | ||
| 60 | |||
| 61 | if (changed) { | ||
| 62 | /* upgrade the configuration file */ | ||
| 63 | xs *fn = xs_fmt("%s/server.json", srv_basedir); | ||
| 64 | FILE *f; | ||
| 65 | |||
| 66 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 67 | xs *j = xs_json_dumps_pp(srv_config, 4); | ||
| 68 | fwrite(j, strlen(j), 1, f); | ||
| 69 | fclose(f); | ||
| 70 | |||
| 71 | srv_log(xs_fmt("upgraded db %s after %d changes", fn, changed)); | ||
| 72 | } | ||
| 73 | else | ||
| 74 | ret = 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | return ret; | ||
| 78 | } | ||