summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
Diffstat (limited to 'data.c')
-rw-r--r--data.c43
1 files changed, 17 insertions, 26 deletions
diff --git a/data.c b/data.c
index 475cd4a..eda263d 100644
--- a/data.c
+++ b/data.c
@@ -545,18 +545,24 @@ xs_list *index_list_desc(const char *fn, int skip, int show)
545 545
546static xs_str *_object_fn_by_md5(const char *md5, const char *func) 546static 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 */ 548 xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
549 if (md5[0] == '-') 549 int ok = 1;
550 return NULL;
551 550
551 /* an object deleted from an index; fail but don't bark */
552 if (md5[0] == '-')
553 ok = 0;
554 else
552 if (!xs_is_hex(md5) || strlen(md5) != 32) { 555 if (!xs_is_hex(md5) || strlen(md5) != 32) {
553 srv_log(xs_fmt("_object_fn_by_md5() [from %s()]: bad md5 '%s'", func, md5)); 556 srv_log(xs_fmt("_object_fn_by_md5() [from %s()]: bad md5 '%s'", func, md5));
554 return NULL; 557 ok = 0;
555 } 558 }
556 559
557 xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]); 560 if (ok)
558 561 mkdirx(bfn);
559 mkdirx(bfn); 562 else {
563 xs_free(bfn);
564 bfn = xs_fmt("%s/object/invalid", srv_basedir);
565 }
560 566
561 return xs_fmt("%s/%s.json", bfn, md5); 567 return xs_fmt("%s/%s.json", bfn, md5);
562} 568}
@@ -573,7 +579,7 @@ int object_here_by_md5(const char *id)
573/* checks if an object is already downloaded */ 579/* checks if an object is already downloaded */
574{ 580{
575 xs *fn = _object_fn_by_md5(id, "object_here_by_md5"); 581 xs *fn = _object_fn_by_md5(id, "object_here_by_md5");
576 return fn && mtime(fn) > 0.0; 582 return mtime(fn) > 0.0;
577} 583}
578 584
579 585
@@ -589,16 +595,9 @@ int object_get_by_md5(const char *md5, xs_dict **obj)
589/* returns a stored object, optionally of the requested type */ 595/* returns a stored object, optionally of the requested type */
590{ 596{
591 int status = 404; 597 int status = 404;
592 xs *fn = NULL; 598 xs *fn = _object_fn_by_md5(md5, "object_get_my_md5");
593 FILE *f; 599 FILE *f;
594 600
595 /* objects deleted in indexes start with - */
596 if (md5[0] == '-')
597 return status;
598
599 if (xs_is_null((fn = _object_fn_by_md5(md5, "object_get_my_md5"))))
600 return 500;
601
602 if ((f = fopen(fn, "r")) != NULL) { 601 if ((f = fopen(fn, "r")) != NULL) {
603 flock(fileno(f), LOCK_SH); 602 flock(fileno(f), LOCK_SH);
604 603
@@ -702,9 +701,6 @@ int object_del_by_md5(const char *md5)
702 int status = 404; 701 int status = 404;
703 xs *fn = _object_fn_by_md5(md5, "object_del_by_md5"); 702 xs *fn = _object_fn_by_md5(md5, "object_del_by_md5");
704 703
705 if (xs_is_null(fn))
706 return 500;
707
708 if (unlink(fn) != -1) { 704 if (unlink(fn) != -1) {
709 status = 200; 705 status = 200;
710 706
@@ -742,7 +738,7 @@ int object_del_if_unref(const char *id)
742 int n_links; 738 int n_links;
743 int ret = 0; 739 int ret = 0;
744 740
745 if (fn && mtime_nl(fn, &n_links) > 0.0 && n_links < 2) 741 if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
746 ret = object_del(id); 742 ret = object_del(id);
747 743
748 return ret; 744 return ret;
@@ -752,7 +748,7 @@ int object_del_if_unref(const char *id)
752double object_ctime_by_md5(const char *md5) 748double object_ctime_by_md5(const char *md5)
753{ 749{
754 xs *fn = _object_fn_by_md5(md5, "object_ctime_by_md5"); 750 xs *fn = _object_fn_by_md5(md5, "object_ctime_by_md5");
755 return fn ? f_ctime(fn) : 0.0; 751 return f_ctime(fn);
756} 752}
757 753
758 754
@@ -813,8 +809,6 @@ int object_parent(const char *md5, char *buf, int size)
813/* returns the object parent, if any */ 809/* returns the object parent, if any */
814{ 810{
815 xs *fn = _object_fn_by_md5(md5, "object_parent"); 811 xs *fn = _object_fn_by_md5(md5, "object_parent");
816 if (xs_is_null(fn))
817 return 0;
818 812
819 fn = xs_replace_i(fn, ".json", "_p.idx"); 813 fn = xs_replace_i(fn, ".json", "_p.idx");
820 return index_first(fn, buf, size); 814 return index_first(fn, buf, size);
@@ -827,9 +821,6 @@ int object_admire(const char *id, const char *actor, int like)
827 int status = 200; 821 int status = 200;
828 xs *fn = _object_fn(id, "object_admire"); 822 xs *fn = _object_fn(id, "object_admire");
829 823
830 if (xs_is_null(fn))
831 return 500;
832
833 fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx"); 824 fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
834 825
835 if (!index_in(fn, actor)) { 826 if (!index_in(fn, actor)) {