upgrade.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "xs_glob.h"
  7. #include "snac.h"
  8. #include <sys/stat.h>
  9. int snac_upgrade(xs_str **error)
  10. {
  11. int ret = 1;
  12. int changed = 0;
  13. double f = 0.0;
  14. for (;;) {
  15. const char *layout = xs_dict_get(srv_config, "layout");
  16. double nf;
  17. f = nf = xs_number_get(layout);
  18. if (!(f < disk_layout))
  19. break;
  20. srv_log(xs_fmt("disk layout upgrade needed (%1.1lf < %1.1lf)", f, disk_layout));
  21. if (f < 2.0) {
  22. *error = xs_fmt("ERROR: unsupported old disk layout %1.1lf\n", f);
  23. ret = 0;
  24. break;
  25. }
  26. else
  27. if (f < 2.1) {
  28. xs *dir = xs_fmt("%s/object", srv_basedir);
  29. mkdirx(dir);
  30. nf = 2.1;
  31. }
  32. else
  33. if (f < 2.2) {
  34. xs *users = user_list();
  35. char *p;
  36. const char *v;
  37. p = users;
  38. while (xs_list_iter(&p, &v)) {
  39. snac snac;
  40. if (user_open(&snac, v)) {
  41. xs *spec = xs_fmt("%s/actors/" "*.json", snac.basedir);
  42. xs *list = xs_glob(spec, 0, 0);
  43. char *g;
  44. const char *fn;
  45. g = list;
  46. while (xs_list_iter(&g, &fn)) {
  47. xs *l = xs_split(fn, "/");
  48. const char *b = xs_list_get(l, -1);
  49. xs *dir = xs_fmt("%s/object/%c%c", srv_basedir, b[0], b[1]);
  50. xs *nfn = xs_fmt("%s/%s", dir, b);
  51. mkdirx(dir);
  52. rename(fn, nfn);
  53. }
  54. xs *odir = xs_fmt("%s/actors", snac.basedir);
  55. rmdir(odir);
  56. user_free(&snac);
  57. }
  58. }
  59. nf = 2.2;
  60. }
  61. else
  62. if (f < 2.3) {
  63. xs *users = user_list();
  64. char *p;
  65. const char *v;
  66. p = users;
  67. while (xs_list_iter(&p, &v)) {
  68. snac snac;
  69. if (user_open(&snac, v)) {
  70. char *p;
  71. const char *v;
  72. xs *dir = xs_fmt("%s/hidden", snac.basedir);
  73. /* create the hidden directory */
  74. mkdirx(dir);
  75. /* rename all muted files incorrectly named .json */
  76. xs *spec = xs_fmt("%s/muted/" "*.json", snac.basedir);
  77. xs *fns = xs_glob(spec, 0, 0);
  78. p = fns;
  79. while (xs_list_iter(&p, &v)) {
  80. xs *nfn = xs_replace(v, ".json", "");
  81. rename(v, nfn);
  82. }
  83. user_free(&snac);
  84. }
  85. }
  86. nf = 2.3;
  87. }
  88. else
  89. if (f < 2.4) {
  90. xs *users = user_list();
  91. char *p;
  92. const char *v;
  93. p = users;
  94. while (xs_list_iter(&p, &v)) {
  95. snac snac;
  96. if (user_open(&snac, v)) {
  97. xs *dir = xs_fmt("%s/public", snac.basedir);
  98. mkdirx(dir);
  99. dir = xs_replace_i(dir, "public", "private");
  100. mkdirx(dir);
  101. user_free(&snac);
  102. }
  103. }
  104. nf = 2.4;
  105. }
  106. else
  107. if (f < 2.5) {
  108. /* upgrade followers */
  109. xs *users = user_list();
  110. char *p;
  111. const char *v;
  112. p = users;
  113. while (xs_list_iter(&p, &v)) {
  114. snac snac;
  115. if (user_open(&snac, v)) {
  116. xs *spec = xs_fmt("%s/followers/" "*.json", snac.basedir);
  117. xs *dir = xs_glob(spec, 0, 0);
  118. char *p;
  119. const char *v;
  120. p = dir;
  121. while (xs_list_iter(&p, &v)) {
  122. FILE *f;
  123. if ((f = fopen(v, "r")) != NULL) {
  124. xs *s = xs_readall(f);
  125. xs *o = xs_json_loads(s);
  126. fclose(f);
  127. const char *type = xs_dict_get(o, "type");
  128. if (!xs_is_null(type) && strcmp(type, "Follow") == 0) {
  129. unlink(v);
  130. const char *actor = xs_dict_get(o, "actor");
  131. if (!xs_is_null(actor))
  132. follower_add(&snac, actor);
  133. }
  134. }
  135. }
  136. user_free(&snac);
  137. }
  138. }
  139. nf = 2.5;
  140. }
  141. else
  142. if (f < 2.6) {
  143. /* upgrade local/ to public/ */
  144. xs *users = user_list();
  145. char *p;
  146. const char *v;
  147. p = users;
  148. while (xs_list_iter(&p, &v)) {
  149. snac snac;
  150. if (user_open(&snac, v)) {
  151. xs *spec = xs_fmt("%s/local/" "*.json", snac.basedir);
  152. xs *dir = xs_glob(spec, 0, 0);
  153. char *p;
  154. const char *v;
  155. p = dir;
  156. while (xs_list_iter(&p, &v)) {
  157. FILE *f;
  158. if ((f = fopen(v, "r")) != NULL) {
  159. xs *s = xs_readall(f);
  160. xs *o = xs_json_loads(s);
  161. fclose(f);
  162. xs *meta = xs_dup(xs_dict_get(o, "_snac"));
  163. o = xs_dict_del(o, "_snac");
  164. const char *id = xs_dict_get(o, "id");
  165. /* store object */
  166. object_add_ow(id, o);
  167. /* if it's from us, add to public */
  168. if (xs_startswith(id, snac.actor)) {
  169. const xs_list *p;
  170. const char *v;
  171. int c;
  172. object_user_cache_add(&snac, id, "public");
  173. p = xs_dict_get(meta, "announced_by");
  174. c = 0;
  175. while (xs_list_next(p, &v, &c))
  176. object_admire(id, v, 0);
  177. p = xs_dict_get(meta, "liked_by");
  178. c = 0;
  179. while (xs_list_next(p, &v, &c))
  180. object_admire(id, v, 1);
  181. }
  182. unlink(v);
  183. }
  184. }
  185. xs *od = xs_fmt("%s/local", snac.basedir);
  186. rmdir(od);
  187. user_free(&snac);
  188. }
  189. }
  190. nf = 2.6;
  191. }
  192. else
  193. if (f < 2.7) {
  194. /* upgrade timeline/ to private/ */
  195. xs *users = user_list();
  196. char *p;
  197. const char *v;
  198. p = users;
  199. while (xs_list_iter(&p, &v)) {
  200. snac snac;
  201. if (user_open(&snac, v)) {
  202. xs *spec = xs_fmt("%s/timeline/" "*.json", snac.basedir);
  203. xs *dir = xs_glob(spec, 0, 0);
  204. char *p;
  205. const char *v;
  206. p = dir;
  207. while (xs_list_iter(&p, &v)) {
  208. FILE *f;
  209. if ((f = fopen(v, "r")) != NULL) {
  210. xs *s = xs_readall(f);
  211. xs *o = xs_json_loads(s);
  212. fclose(f);
  213. xs *meta = xs_dup(xs_dict_get(o, "_snac"));
  214. o = xs_dict_del(o, "_snac");
  215. const char *id = xs_dict_get(o, "id");
  216. /* store object */
  217. object_add_ow(id, o);
  218. {
  219. const xs_list *p;
  220. const char *v;
  221. int c = 0;
  222. object_user_cache_add(&snac, id, "private");
  223. p = xs_dict_get(meta, "announced_by");
  224. c = 0;
  225. while (xs_list_next(p, &v, &c))
  226. object_admire(id, v, 0);
  227. p = xs_dict_get(meta, "liked_by");
  228. c = 0;
  229. while (xs_list_next(p, &v, &c))
  230. object_admire(id, v, 1);
  231. }
  232. unlink(v);
  233. }
  234. }
  235. xs *od = xs_fmt("%s/timeline", snac.basedir);
  236. rmdir(od);
  237. user_free(&snac);
  238. }
  239. }
  240. nf = 2.7;
  241. }
  242. if (f < nf) {
  243. f = nf;
  244. xs *nv = xs_number_new(f);
  245. srv_config = xs_dict_set(srv_config, "layout", nv);
  246. srv_log(xs_fmt("disk layout upgraded to version %1.1lf", f));
  247. changed++;
  248. }
  249. else
  250. break;
  251. }
  252. if (f > disk_layout) {
  253. *error = xs_fmt("ERROR: unknown future version %lf\n", f);
  254. ret = 0;
  255. }
  256. if (changed) {
  257. /* upgrade the configuration file */
  258. xs *fn = xs_fmt("%s/server.json", srv_basedir);
  259. FILE *f;
  260. if ((f = fopen(fn, "w")) != NULL) {
  261. xs_json_dump(srv_config, 4, f);
  262. fclose(f);
  263. srv_log(xs_fmt("disk layout upgraded %s after %d changes", fn, changed));
  264. }
  265. else
  266. ret = 0;
  267. }
  268. return ret;
  269. }