summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
Diffstat (limited to 'data.c')
-rw-r--r--data.c95
1 files changed, 61 insertions, 34 deletions
diff --git a/data.c b/data.c
index edbc64f..e24bf16 100644
--- a/data.c
+++ b/data.c
@@ -303,6 +303,33 @@ int user_open_by_md5(snac *snac, const char *md5)
303 return 0; 303 return 0;
304} 304}
305 305
306int user_persist(snac *snac)
307/* store user */
308{
309 xs *fn = xs_fmt("%s/user.json", snac->basedir);
310 xs *bfn = xs_fmt("%s.bak", fn);
311 FILE *f;
312
313 rename(fn, bfn);
314
315 if ((f = fopen(fn, "w")) != NULL) {
316 xs_json_dump(snac->config, 4, f);
317 fclose(f);
318 }
319 else
320 rename(bfn, fn);
321
322 history_del(snac, "timeline.html_");
323
324 xs *a_msg = msg_actor(snac);
325 xs *u_msg = msg_update(snac, a_msg);
326
327 enqueue_message(snac, u_msg);
328 enqueue_verify_links(snac);
329
330 return 0;
331}
332
306 333
307double mtime_nl(const char *fn, int *n_link) 334double mtime_nl(const char *fn, int *n_link)
308/* returns the mtime and number of links of a file or directory, or 0.0 */ 335/* returns the mtime and number of links of a file or directory, or 0.0 */
@@ -355,12 +382,12 @@ int is_md5_hex(const char *md5)
355int index_add_md5(const char *fn, const char *md5) 382int index_add_md5(const char *fn, const char *md5)
356/* adds an md5 to an index */ 383/* adds an md5 to an index */
357{ 384{
358 int status = 201; /* Created */ 385 int status = HTTP_STATUS_CREATED;
359 FILE *f; 386 FILE *f;
360 387
361 if (!is_md5_hex(md5)) { 388 if (!is_md5_hex(md5)) {
362 srv_log(xs_fmt("index_add_md5: bad md5 %s %s", fn, md5)); 389 srv_log(xs_fmt("index_add_md5: bad md5 %s %s", fn, md5));
363 return 400; 390 return HTTP_STATUS_BAD_REQUEST;
364 } 391 }
365 392
366 pthread_mutex_lock(&data_mutex); 393 pthread_mutex_lock(&data_mutex);
@@ -375,7 +402,7 @@ int index_add_md5(const char *fn, const char *md5)
375 fclose(f); 402 fclose(f);
376 } 403 }
377 else 404 else
378 status = 500; 405 status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
379 406
380 pthread_mutex_unlock(&data_mutex); 407 pthread_mutex_unlock(&data_mutex);
381 408
@@ -394,7 +421,7 @@ int index_add(const char *fn, const char *id)
394int index_del_md5(const char *fn, const char *md5) 421int index_del_md5(const char *fn, const char *md5)
395/* deletes an md5 from an index */ 422/* deletes an md5 from an index */
396{ 423{
397 int status = 404; 424 int status = HTTP_STATUS_NOT_FOUND;
398 FILE *f; 425 FILE *f;
399 426
400 pthread_mutex_lock(&data_mutex); 427 pthread_mutex_lock(&data_mutex);
@@ -411,7 +438,7 @@ int index_del_md5(const char *fn, const char *md5)
411 [yes: this breaks index_len()] */ 438 [yes: this breaks index_len()] */
412 fseek(f, -33, SEEK_CUR); 439 fseek(f, -33, SEEK_CUR);
413 fwrite("-", 1, 1, f); 440 fwrite("-", 1, 1, f);
414 status = 200; 441 status = HTTP_STATUS_OK;
415 442
416 break; 443 break;
417 } 444 }
@@ -420,7 +447,7 @@ int index_del_md5(const char *fn, const char *md5)
420 fclose(f); 447 fclose(f);
421 } 448 }
422 else 449 else
423 status = 410; 450 status = HTTP_STATUS_GONE;
424 451
425 pthread_mutex_unlock(&data_mutex); 452 pthread_mutex_unlock(&data_mutex);
426 453
@@ -660,7 +687,7 @@ int object_here(const char *id)
660int object_get_by_md5(const char *md5, xs_dict **obj) 687int object_get_by_md5(const char *md5, xs_dict **obj)
661/* returns a stored object, optionally of the requested type */ 688/* returns a stored object, optionally of the requested type */
662{ 689{
663 int status = 404; 690 int status = HTTP_STATUS_NOT_FOUND;
664 xs *fn = _object_fn_by_md5(md5, "object_get_by_md5"); 691 xs *fn = _object_fn_by_md5(md5, "object_get_by_md5");
665 FILE *f; 692 FILE *f;
666 693
@@ -669,7 +696,7 @@ int object_get_by_md5(const char *md5, xs_dict **obj)
669 fclose(f); 696 fclose(f);
670 697
671 if (*obj) 698 if (*obj)
672 status = 200; 699 status = HTTP_STATUS_OK;
673 } 700 }
674 else 701 else
675 *obj = NULL; 702 *obj = NULL;
@@ -689,7 +716,7 @@ int object_get(const char *id, xs_dict **obj)
689int _object_add(const char *id, const xs_dict *obj, int ow) 716int _object_add(const char *id, const xs_dict *obj, int ow)
690/* stores an object */ 717/* stores an object */
691{ 718{
692 int status = 201; /* Created */ 719 int status = HTTP_STATUS_CREATED; /* Created */
693 xs *fn = _object_fn(id); 720 xs *fn = _object_fn(id);
694 FILE *f; 721 FILE *f;
695 722
@@ -697,10 +724,10 @@ int _object_add(const char *id, const xs_dict *obj, int ow)
697 if (!ow) { 724 if (!ow) {
698 /* object already here */ 725 /* object already here */
699 srv_debug(1, xs_fmt("object_add object already here %s", id)); 726 srv_debug(1, xs_fmt("object_add object already here %s", id));
700 return 204; /* No content */ 727 return HTTP_STATUS_NO_CONTENT;
701 } 728 }
702 else 729 else
703 status = 200; 730 status = HTTP_STATUS_OK;
704 } 731 }
705 732
706 if ((f = fopen(fn, "w")) != NULL) { 733 if ((f = fopen(fn, "w")) != NULL) {
@@ -736,7 +763,7 @@ int _object_add(const char *id, const xs_dict *obj, int ow)
736 } 763 }
737 else { 764 else {
738 srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno)); 765 srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
739 status = 500; 766 status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
740 } 767 }
741 768
742 srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status)); 769 srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
@@ -762,11 +789,11 @@ int object_add_ow(const char *id, const xs_dict *obj)
762int object_del_by_md5(const char *md5) 789int object_del_by_md5(const char *md5)
763/* deletes an object by its md5 */ 790/* deletes an object by its md5 */
764{ 791{
765 int status = 404; 792 int status = HTTP_STATUS_NOT_FOUND;
766 xs *fn = _object_fn_by_md5(md5, "object_del_by_md5"); 793 xs *fn = _object_fn_by_md5(md5, "object_del_by_md5");
767 794
768 if (unlink(fn) != -1) { 795 if (unlink(fn) != -1) {
769 status = 200; 796 status = HTTP_STATUS_OK;
770 797
771 /* also delete associated indexes */ 798 /* also delete associated indexes */
772 xs *spec = xs_dup(fn); 799 xs *spec = xs_dup(fn);
@@ -907,7 +934,7 @@ int object_parent(const char *md5, char *buf, int size)
907int object_admire(const char *id, const char *actor, int like) 934int object_admire(const char *id, const char *actor, int like)
908/* actor likes or announces this object */ 935/* actor likes or announces this object */
909{ 936{
910 int status = 200; 937 int status = HTTP_STATUS_OK;
911 xs *fn = _object_fn(id); 938 xs *fn = _object_fn(id);
912 939
913 fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx"); 940 fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
@@ -1007,7 +1034,7 @@ int follower_add(snac *snac, const char *actor)
1007 1034
1008 snac_debug(snac, 2, xs_fmt("follower_add %s", actor)); 1035 snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
1009 1036
1010 return ret == -1 ? 500 : 200; 1037 return ret == -1 ? HTTP_STATUS_INTERNAL_SERVER_ERROR : HTTP_STATUS_OK;
1011} 1038}
1012 1039
1013 1040
@@ -1018,7 +1045,7 @@ int follower_del(snac *snac, const char *actor)
1018 1045
1019 snac_debug(snac, 2, xs_fmt("follower_del %s", actor)); 1046 snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
1020 1047
1021 return ret == -1 ? 404 : 200; 1048 return ret == -1 ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK;
1022} 1049}
1023 1050
1024 1051
@@ -1109,7 +1136,7 @@ int timeline_here(snac *snac, const char *md5)
1109int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg) 1136int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
1110/* gets a message from the timeline */ 1137/* gets a message from the timeline */
1111{ 1138{
1112 int status = 404; 1139 int status = HTTP_STATUS_NOT_FOUND;
1113 FILE *f = NULL; 1140 FILE *f = NULL;
1114 1141
1115 xs *fn = timeline_fn_by_md5(snac, md5); 1142 xs *fn = timeline_fn_by_md5(snac, md5);
@@ -1119,7 +1146,7 @@ int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
1119 fclose(f); 1146 fclose(f);
1120 1147
1121 if (*msg != NULL) 1148 if (*msg != NULL)
1122 status = 200; 1149 status = HTTP_STATUS_OK;
1123 } 1150 }
1124 1151
1125 return status; 1152 return status;
@@ -1282,7 +1309,7 @@ xs_str *_following_fn(snac *snac, const char *actor)
1282int following_add(snac *snac, const char *actor, const xs_dict *msg) 1309int following_add(snac *snac, const char *actor, const xs_dict *msg)
1283/* adds to the following list */ 1310/* adds to the following list */
1284{ 1311{
1285 int ret = 201; /* created */ 1312 int ret = HTTP_STATUS_CREATED;
1286 xs *fn = _following_fn(snac, actor); 1313 xs *fn = _following_fn(snac, actor);
1287 FILE *f; 1314 FILE *f;
1288 xs *p_object = NULL; 1315 xs *p_object = NULL;
@@ -1295,7 +1322,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg)
1295 1322
1296 if (!xs_is_null(type) && strcmp(type, "Accept") == 0) { 1323 if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
1297 snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor)); 1324 snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor));
1298 return 200; 1325 return HTTP_STATUS_OK;
1299 } 1326 }
1300 } 1327 }
1301 1328
@@ -1311,7 +1338,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg)
1311 link(actor_fn, fn); 1338 link(actor_fn, fn);
1312 } 1339 }
1313 else 1340 else
1314 ret = 500; 1341 ret = HTTP_STATUS_INTERNAL_SERVER_ERROR;
1315 1342
1316 snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn)); 1343 snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
1317 1344
@@ -1332,7 +1359,7 @@ int following_del(snac *snac, const char *actor)
1332 fn = xs_replace_i(fn, ".json", "_a.json"); 1359 fn = xs_replace_i(fn, ".json", "_a.json");
1333 unlink(fn); 1360 unlink(fn);
1334 1361
1335 return 200; 1362 return HTTP_STATUS_OK;
1336} 1363}
1337 1364
1338 1365
@@ -1350,14 +1377,14 @@ int following_get(snac *snac, const char *actor, xs_dict **data)
1350{ 1377{
1351 xs *fn = _following_fn(snac, actor); 1378 xs *fn = _following_fn(snac, actor);
1352 FILE *f; 1379 FILE *f;
1353 int status = 200; 1380 int status = HTTP_STATUS_OK;
1354 1381
1355 if ((f = fopen(fn, "r")) != NULL) { 1382 if ((f = fopen(fn, "r")) != NULL) {
1356 *data = xs_json_load(f); 1383 *data = xs_json_load(f);
1357 fclose(f); 1384 fclose(f);
1358 } 1385 }
1359 else 1386 else
1360 status = 404; 1387 status = HTTP_STATUS_NOT_FOUND;
1361 1388
1362 return status; 1389 return status;
1363} 1390}
@@ -1576,7 +1603,7 @@ int actor_add(const char *actor, const xs_dict *msg)
1576int actor_get(const char *actor, xs_dict **data) 1603int actor_get(const char *actor, xs_dict **data)
1577/* returns an already downloaded actor */ 1604/* returns an already downloaded actor */
1578{ 1605{
1579 int status = 200; 1606 int status = HTTP_STATUS_OK;
1580 xs_dict *d = NULL; 1607 xs_dict *d = NULL;
1581 1608
1582 if (xs_startswith(actor, srv_baseurl)) { 1609 if (xs_startswith(actor, srv_baseurl)) {
@@ -1590,10 +1617,10 @@ int actor_get(const char *actor, xs_dict **data)
1590 *data = msg_actor(&user); 1617 *data = msg_actor(&user);
1591 1618
1592 user_free(&user); 1619 user_free(&user);
1593 return 200; 1620 return HTTP_STATUS_OK;
1594 } 1621 }
1595 else 1622 else
1596 return 404; 1623 return HTTP_STATUS_NOT_FOUND;
1597 } 1624 }
1598 1625
1599 /* read the object */ 1626 /* read the object */
@@ -1606,7 +1633,7 @@ int actor_get(const char *actor, xs_dict **data)
1606 if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) { 1633 if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) {
1607 srv_debug(1, xs_fmt("corrupted actor object %s", actor)); 1634 srv_debug(1, xs_fmt("corrupted actor object %s", actor));
1608 d = xs_free(d); 1635 d = xs_free(d);
1609 return 404; 1636 return HTTP_STATUS_NOT_FOUND;
1610 } 1637 }
1611 1638
1612 if (data) 1639 if (data)
@@ -1622,7 +1649,7 @@ int actor_get(const char *actor, xs_dict **data)
1622 1649
1623 if (mtime(fn) + max_time < (double) time(NULL)) { 1650 if (mtime(fn) + max_time < (double) time(NULL)) {
1624 /* actor data exists but also stinks */ 1651 /* actor data exists but also stinks */
1625 status = 205; /* "205: Reset Content" "110: Response Is Stale" */ 1652 status = HTTP_STATUS_RESET_CONTENT; /* "110: Response Is Stale" */
1626 } 1653 }
1627 1654
1628 return status; 1655 return status;
@@ -1634,7 +1661,7 @@ int actor_get_refresh(snac *user, const char *actor, xs_dict **data)
1634{ 1661{
1635 int status = actor_get(actor, data); 1662 int status = actor_get(actor, data);
1636 1663
1637 if (status == 205 && user && !xs_startswith(actor, srv_baseurl)) 1664 if (status == HTTP_STATUS_RESET_CONTENT && user && !xs_startswith(actor, srv_baseurl))
1638 enqueue_actor_refresh(user, actor, 0); 1665 enqueue_actor_refresh(user, actor, 0);
1639 1666
1640 return status; 1667 return status;
@@ -1953,7 +1980,7 @@ static int _load_raw_file(const char *fn, xs_val **data, int *size,
1953 const char *inm, xs_str **etag) 1980 const char *inm, xs_str **etag)
1954/* loads a cached file */ 1981/* loads a cached file */
1955{ 1982{
1956 int status = 404; 1983 int status = HTTP_STATUS_NOT_FOUND;
1957 1984
1958 if (fn) { 1985 if (fn) {
1959 double tm = mtime(fn); 1986 double tm = mtime(fn);
@@ -1965,7 +1992,7 @@ static int _load_raw_file(const char *fn, xs_val **data, int *size,
1965 /* if if-none-match is set, check if it's the same */ 1992 /* if if-none-match is set, check if it's the same */
1966 if (!xs_is_null(inm) && strcmp(e, inm) == 0) { 1993 if (!xs_is_null(inm) && strcmp(e, inm) == 0) {
1967 /* client has the newest version */ 1994 /* client has the newest version */
1968 status = 304; 1995 status = HTTP_STATUS_NOT_MODIFIED;
1969 } 1996 }
1970 else { 1997 else {
1971 /* newer or never downloaded; read the full file */ 1998 /* newer or never downloaded; read the full file */
@@ -1976,7 +2003,7 @@ static int _load_raw_file(const char *fn, xs_val **data, int *size,
1976 *data = xs_read(f, size); 2003 *data = xs_read(f, size);
1977 fclose(f); 2004 fclose(f);
1978 2005
1979 status = 200; 2006 status = HTTP_STATUS_OK;
1980 } 2007 }
1981 } 2008 }
1982 2009