diff options
Diffstat (limited to 'upgrade.c')
| -rw-r--r-- | upgrade.c | 78 |
1 files changed, 78 insertions, 0 deletions
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 | } | ||