diff options
| author | 2023-06-18 19:53:33 +0200 | |
|---|---|---|
| committer | 2023-06-18 19:53:33 +0200 | |
| commit | 551bf92b3b95dc070bbd44c68b7761059b884a52 (patch) | |
| tree | 51b67519c2af4f9abacd1b54d1b8b2317b1f79e4 /data.c | |
| parent | Renamed the HTTP Basic auth realm to something more informative. (diff) | |
| download | snac2-551bf92b3b95dc070bbd44c68b7761059b884a52.tar.gz snac2-551bf92b3b95dc070bbd44c68b7761059b884a52.tar.xz snac2-551bf92b3b95dc070bbd44c68b7761059b884a52.zip | |
Added the caller function name to _object_fn_by_md5() and _object_fn().
Diffstat (limited to 'data.c')
| -rw-r--r-- | data.c | 61 |
1 files changed, 34 insertions, 27 deletions
| @@ -543,10 +543,14 @@ xs_list *index_list_desc(const char *fn, int skip, int show) | |||
| 543 | 543 | ||
| 544 | /** objects **/ | 544 | /** objects **/ |
| 545 | 545 | ||
| 546 | static xs_str *_object_fn_by_md5(const char *md5) | 546 | static xs_str *_object_fn_by_md5(const char *md5, const char *func) |
| 547 | { | 547 | { |
| 548 | /* object deleted in an index; fail, but don't bark */ | ||
| 549 | if (md5[0] == '-') | ||
| 550 | return NULL; | ||
| 551 | |||
| 548 | if (!xs_is_hex(md5) || strlen(md5) != 32) { | 552 | if (!xs_is_hex(md5) || strlen(md5) != 32) { |
| 549 | srv_log(xs_fmt("_object_fn_by_md5(): bad md5 '%s'", md5)); | 553 | srv_log(xs_fmt("_object_fn_by_md5() [from %s()]: bad md5 '%s'", func, md5)); |
| 550 | return NULL; | 554 | return NULL; |
| 551 | } | 555 | } |
| 552 | 556 | ||
| @@ -558,17 +562,17 @@ static xs_str *_object_fn_by_md5(const char *md5) | |||
| 558 | } | 562 | } |
| 559 | 563 | ||
| 560 | 564 | ||
| 561 | xs_str *_object_fn(const char *id) | 565 | static xs_str *_object_fn(const char *id, const char *func) |
| 562 | { | 566 | { |
| 563 | xs *md5 = xs_md5_hex(id, strlen(id)); | 567 | xs *md5 = xs_md5_hex(id, strlen(id)); |
| 564 | return _object_fn_by_md5(md5); | 568 | return _object_fn_by_md5(md5, func); |
| 565 | } | 569 | } |
| 566 | 570 | ||
| 567 | 571 | ||
| 568 | int object_here_by_md5(const char *id) | 572 | int object_here_by_md5(const char *id) |
| 569 | /* checks if an object is already downloaded */ | 573 | /* checks if an object is already downloaded */ |
| 570 | { | 574 | { |
| 571 | xs *fn = _object_fn_by_md5(id); | 575 | xs *fn = _object_fn_by_md5(id, "object_here_by_md5"); |
| 572 | return fn && mtime(fn) > 0.0; | 576 | return fn && mtime(fn) > 0.0; |
| 573 | } | 577 | } |
| 574 | 578 | ||
| @@ -576,7 +580,7 @@ int object_here_by_md5(const char *id) | |||
| 576 | int object_here(const char *id) | 580 | int object_here(const char *id) |
| 577 | /* checks if an object is already downloaded */ | 581 | /* checks if an object is already downloaded */ |
| 578 | { | 582 | { |
| 579 | xs *fn = _object_fn(id); | 583 | xs *fn = _object_fn(id, "object_here"); |
| 580 | return mtime(fn) > 0.0; | 584 | return mtime(fn) > 0.0; |
| 581 | } | 585 | } |
| 582 | 586 | ||
| @@ -585,13 +589,15 @@ int object_get_by_md5(const char *md5, xs_dict **obj) | |||
| 585 | /* returns a stored object, optionally of the requested type */ | 589 | /* returns a stored object, optionally of the requested type */ |
| 586 | { | 590 | { |
| 587 | int status = 404; | 591 | int status = 404; |
| 588 | xs *fn = _object_fn_by_md5(md5); | 592 | xs *fn = NULL; |
| 589 | FILE *f; | 593 | FILE *f; |
| 590 | 594 | ||
| 591 | if (xs_is_null(fn)) { | 595 | /* objects deleted in indexes start with - */ |
| 592 | srv_log(xs_fmt("object_get_by_md5(): bad md5 '%s'", md5)); | 596 | if (md5[0] == '-') |
| 597 | return status; | ||
| 598 | |||
| 599 | if (xs_is_null((fn = _object_fn_by_md5(md5, "object_get_my_md5")))) | ||
| 593 | return 500; | 600 | return 500; |
| 594 | } | ||
| 595 | 601 | ||
| 596 | if ((f = fopen(fn, "r")) != NULL) { | 602 | if ((f = fopen(fn, "r")) != NULL) { |
| 597 | flock(fileno(f), LOCK_SH); | 603 | flock(fileno(f), LOCK_SH); |
| @@ -623,7 +629,7 @@ int _object_add(const char *id, const xs_dict *obj, int ow) | |||
| 623 | /* stores an object */ | 629 | /* stores an object */ |
| 624 | { | 630 | { |
| 625 | int status = 201; /* Created */ | 631 | int status = 201; /* Created */ |
| 626 | xs *fn = _object_fn(id); | 632 | xs *fn = _object_fn(id, "_object_add 1"); |
| 627 | FILE *f; | 633 | FILE *f; |
| 628 | 634 | ||
| 629 | if (!ow && mtime(fn) > 0.0) { | 635 | if (!ow && mtime(fn) > 0.0) { |
| @@ -645,7 +651,7 @@ int _object_add(const char *id, const xs_dict *obj, int ow) | |||
| 645 | 651 | ||
| 646 | if (!xs_is_null(in_reply_to) && *in_reply_to) { | 652 | if (!xs_is_null(in_reply_to) && *in_reply_to) { |
| 647 | /* update the children index of the parent */ | 653 | /* update the children index of the parent */ |
| 648 | xs *c_idx = _object_fn(in_reply_to); | 654 | xs *c_idx = _object_fn(in_reply_to, "_object_add 2"); |
| 649 | 655 | ||
| 650 | c_idx = xs_replace_i(c_idx, ".json", "_c.idx"); | 656 | c_idx = xs_replace_i(c_idx, ".json", "_c.idx"); |
| 651 | 657 | ||
| @@ -694,12 +700,10 @@ int object_del_by_md5(const char *md5) | |||
| 694 | /* deletes an object by its md5 */ | 700 | /* deletes an object by its md5 */ |
| 695 | { | 701 | { |
| 696 | int status = 404; | 702 | int status = 404; |
| 697 | xs *fn = _object_fn_by_md5(md5); | 703 | xs *fn = _object_fn_by_md5(md5, "object_del_by_md5"); |
| 698 | 704 | ||
| 699 | if (xs_is_null(fn)) { | 705 | if (xs_is_null(fn)) |
| 700 | srv_log(xs_fmt("object_del_by_md5(): bad md5 '%s'", md5)); | ||
| 701 | return 500; | 706 | return 500; |
| 702 | } | ||
| 703 | 707 | ||
| 704 | if (unlink(fn) != -1) { | 708 | if (unlink(fn) != -1) { |
| 705 | status = 200; | 709 | status = 200; |
| @@ -734,11 +738,11 @@ int object_del(const char *id) | |||
| 734 | int object_del_if_unref(const char *id) | 738 | int object_del_if_unref(const char *id) |
| 735 | /* deletes an object if its n_links < 2 */ | 739 | /* deletes an object if its n_links < 2 */ |
| 736 | { | 740 | { |
| 737 | xs *fn = _object_fn(id); | 741 | xs *fn = _object_fn(id, "object_del_if_unref"); |
| 738 | int n_links; | 742 | int n_links; |
| 739 | int ret = 0; | 743 | int ret = 0; |
| 740 | 744 | ||
| 741 | if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2) | 745 | if (fn && mtime_nl(fn, &n_links) > 0.0 && n_links < 2) |
| 742 | ret = object_del(id); | 746 | ret = object_del(id); |
| 743 | 747 | ||
| 744 | return ret; | 748 | return ret; |
| @@ -747,7 +751,7 @@ int object_del_if_unref(const char *id) | |||
| 747 | 751 | ||
| 748 | double object_ctime_by_md5(const char *md5) | 752 | double object_ctime_by_md5(const char *md5) |
| 749 | { | 753 | { |
| 750 | xs *fn = _object_fn_by_md5(md5); | 754 | xs *fn = _object_fn_by_md5(md5, "object_ctime_by_md5"); |
| 751 | return fn ? f_ctime(fn) : 0.0; | 755 | return fn ? f_ctime(fn) : 0.0; |
| 752 | } | 756 | } |
| 753 | 757 | ||
| @@ -762,7 +766,7 @@ double object_ctime(const char *id) | |||
| 762 | xs_str *_object_index_fn(const char *id, const char *idxsfx) | 766 | xs_str *_object_index_fn(const char *id, const char *idxsfx) |
| 763 | /* returns the filename of an object's index */ | 767 | /* returns the filename of an object's index */ |
| 764 | { | 768 | { |
| 765 | xs_str *fn = _object_fn(id); | 769 | xs_str *fn = _object_fn(id, "_object_index_fn"); |
| 766 | return xs_replace_i(fn, ".json", idxsfx); | 770 | return xs_replace_i(fn, ".json", idxsfx); |
| 767 | } | 771 | } |
| 768 | 772 | ||
| @@ -808,7 +812,7 @@ xs_list *object_announces(const char *id) | |||
| 808 | int object_parent(const char *md5, char *buf, int size) | 812 | int object_parent(const char *md5, char *buf, int size) |
| 809 | /* returns the object parent, if any */ | 813 | /* returns the object parent, if any */ |
| 810 | { | 814 | { |
| 811 | xs *fn = _object_fn_by_md5(md5); | 815 | xs *fn = _object_fn_by_md5(md5, "object_parent"); |
| 812 | if (xs_is_null(fn)) | 816 | if (xs_is_null(fn)) |
| 813 | return 0; | 817 | return 0; |
| 814 | 818 | ||
| @@ -821,7 +825,10 @@ int object_admire(const char *id, const char *actor, int like) | |||
| 821 | /* actor likes or announces this object */ | 825 | /* actor likes or announces this object */ |
| 822 | { | 826 | { |
| 823 | int status = 200; | 827 | int status = 200; |
| 824 | xs *fn = _object_fn(id); | 828 | xs *fn = _object_fn(id, "object_admire"); |
| 829 | |||
| 830 | if (xs_is_null(fn)) | ||
| 831 | return 500; | ||
| 825 | 832 | ||
| 826 | fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx"); | 833 | fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx"); |
| 827 | 834 | ||
| @@ -839,7 +846,7 @@ int object_unadmire(const char *id, const char *actor, int like) | |||
| 839 | /* actor no longer likes or announces this object */ | 846 | /* actor no longer likes or announces this object */ |
| 840 | { | 847 | { |
| 841 | int status; | 848 | int status; |
| 842 | xs *fn = _object_fn(id); | 849 | xs *fn = _object_fn(id, "object_unadmire"); |
| 843 | 850 | ||
| 844 | fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx"); | 851 | fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx"); |
| 845 | 852 | ||
| @@ -855,7 +862,7 @@ int object_unadmire(const char *id, const char *actor, int like) | |||
| 855 | int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del) | 862 | int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del) |
| 856 | /* adds or deletes from a user cache */ | 863 | /* adds or deletes from a user cache */ |
| 857 | { | 864 | { |
| 858 | xs *ofn = _object_fn(id); | 865 | xs *ofn = _object_fn(id, "_object_user_cache"); |
| 859 | xs *l = xs_split(ofn, "/"); | 866 | xs *l = xs_split(ofn, "/"); |
| 860 | xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1)); | 867 | xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1)); |
| 861 | xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir); | 868 | xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir); |
| @@ -1194,7 +1201,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg) | |||
| 1194 | fclose(f); | 1201 | fclose(f); |
| 1195 | 1202 | ||
| 1196 | /* get the filename of the actor object */ | 1203 | /* get the filename of the actor object */ |
| 1197 | xs *actor_fn = _object_fn(actor); | 1204 | xs *actor_fn = _object_fn(actor, "following_add"); |
| 1198 | 1205 | ||
| 1199 | /* increase its reference count */ | 1206 | /* increase its reference count */ |
| 1200 | fn = xs_replace_i(fn, ".json", "_a.json"); | 1207 | fn = xs_replace_i(fn, ".json", "_a.json"); |
| @@ -1292,7 +1299,7 @@ xs_list *following_list(snac *snac) | |||
| 1292 | 1299 | ||
| 1293 | if (mtime(v2) == 0.0) { | 1300 | if (mtime(v2) == 0.0) { |
| 1294 | /* no; add a link to it */ | 1301 | /* no; add a link to it */ |
| 1295 | xs *actor_fn = _object_fn(actor); | 1302 | xs *actor_fn = _object_fn(actor, "following_list"); |
| 1296 | link(actor_fn, v2); | 1303 | link(actor_fn, v2); |
| 1297 | } | 1304 | } |
| 1298 | } | 1305 | } |
| @@ -1443,7 +1450,7 @@ int actor_get(snac *snac1, const char *actor, xs_dict **data) | |||
| 1443 | else | 1450 | else |
| 1444 | d = xs_free(d); | 1451 | d = xs_free(d); |
| 1445 | 1452 | ||
| 1446 | xs *fn = _object_fn(actor); | 1453 | xs *fn = _object_fn(actor, "actor_get"); |
| 1447 | double max_time; | 1454 | double max_time; |
| 1448 | 1455 | ||
| 1449 | /* maximum time for the actor data to be considered stale */ | 1456 | /* maximum time for the actor data to be considered stale */ |