diff options
author | default <nobody@localhost> | 2022-11-24 08:00:35 +0100 |
---|---|---|
committer | default <nobody@localhost> | 2022-11-24 08:00:35 +0100 |
commit | 9d7b35940f2a09b2ef1a8e1e705821b362ebf7c9 (patch) | |
tree | d91731b9abf6c983d638831a72cc234cbed4aff1 | |
parent | cde8650df756d0e1b265c529aa0a32cae1d617af (diff) |
Renamed listfile functions to index.
-rw-r--r-- | data.c | 222 | ||||
-rw-r--r-- | snac.h | 8 |
2 files changed, 115 insertions, 115 deletions
@@ -191,112 +191,10 @@ double mtime(char *fn) } -/** object database 2.1+ **/ +/** database 2.1+ **/ -d_char *_object_fn_by_md5(const char *md5) -{ - xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]); - - mkdir(bfn, 0755); - - return xs_fmt("%s/%s.json", bfn, md5); -} - - -d_char *_object_fn(const char *id) -{ - xs *md5 = xs_md5_hex(id, strlen(id)); - - return _object_fn_by_md5(md5); -} - - -int object_get_by_md5(const char *md5, d_char **obj, const char *type) -/* returns a loaded object, optionally of the requested type */ -{ - int status = 404; - xs *fn = _object_fn_by_md5(md5); - FILE *f; - - if ((f = fopen(fn, "r")) != NULL) { - flock(fileno(f), LOCK_SH); - - xs *j = xs_readall(f); - fclose(f); - - *obj = xs_json_loads(j); - - if (*obj) { - status = 200; - - /* specific type requested? */ - if (!xs_is_null(type)) { - char *v = xs_dict_get(*obj, "type"); - - if (xs_is_null(v) || strcmp(v, type) != 0) { - status = 404; - *obj = xs_free(*obj); - } - } - } - } - else - *obj = NULL; - - return status; -} - - -int object_get(const char *id, d_char **obj, const char *type) -/* returns a loaded object, optionally of the requested type */ -{ - xs *md5 = xs_md5_hex(id, strlen(id)); - - return object_get_by_md5(md5, obj, type); -} - - -int object_add(const char *id, d_char *obj) -/* stores an object */ -{ - int status = 201; /* Created */ - xs *fn = _object_fn(id); - FILE *f; - - if ((f = fopen(fn, "w")) != NULL) { - flock(fileno(f), LOCK_EX); - - xs *j = xs_json_dumps_pp(obj, 4); - - fwrite(j, strlen(j), 1, f); - fclose(f); - } - else - status = 500; - - srv_debug(2, xs_fmt("object_add %s %s %d", id, fn, status)); - - return status; -} - - -int object_del(const char *id) -/* deletes an object */ -{ - int status = 404; - xs *fn = _object_fn(id); - - if (fn != NULL && unlink(fn) != -1) - status = 200; - - srv_debug(2, xs_fmt("object_del %s %d", id, status)); - - return status; -} - - -int listfile_add_md5(const char *fn, const char *md5) -/* adds an md5 to a list */ +int index_add(const char *fn, const char *md5) +/* adds an md5 to an index */ { int status = 200; FILE *f; @@ -314,8 +212,8 @@ int listfile_add_md5(const char *fn, const char *md5) } -int listfile_del_md5(const char *fn, const char *md5) -/* deletes an md5 from a list */ +int index_del(const char *fn, const char *md5) +/* deletes an md5 from an index */ { int status = 404; FILE *i, *o; @@ -352,8 +250,8 @@ int listfile_del_md5(const char *fn, const char *md5) } -d_char *listfile_get_n(const char *fn, int max) -/* returns a list */ +d_char *index_list(const char *fn, int max) +/* returns an index as a list */ { d_char *list = NULL; FILE *f; @@ -378,8 +276,8 @@ d_char *listfile_get_n(const char *fn, int max) } -d_char *listfile_get_inv_n(const char *fn, int max) -/* returns a list, inversely */ +d_char *index_list_desc(const char *fn, int max) +/* returns an index as a list, in reverse order */ { d_char *list = NULL; FILE *f; @@ -411,6 +309,108 @@ d_char *listfile_get_inv_n(const char *fn, int max) } +d_char *_object_fn_by_md5(const char *md5) +{ + xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]); + + mkdir(bfn, 0755); + + return xs_fmt("%s/%s.json", bfn, md5); +} + + +d_char *_object_fn(const char *id) +{ + xs *md5 = xs_md5_hex(id, strlen(id)); + + return _object_fn_by_md5(md5); +} + + +int object_get_by_md5(const char *md5, d_char **obj, const char *type) +/* returns a loaded object, optionally of the requested type */ +{ + int status = 404; + xs *fn = _object_fn_by_md5(md5); + FILE *f; + + if ((f = fopen(fn, "r")) != NULL) { + flock(fileno(f), LOCK_SH); + + xs *j = xs_readall(f); + fclose(f); + + *obj = xs_json_loads(j); + + if (*obj) { + status = 200; + + /* specific type requested? */ + if (!xs_is_null(type)) { + char *v = xs_dict_get(*obj, "type"); + + if (xs_is_null(v) || strcmp(v, type) != 0) { + status = 404; + *obj = xs_free(*obj); + } + } + } + } + else + *obj = NULL; + + return status; +} + + +int object_get(const char *id, d_char **obj, const char *type) +/* returns a loaded object, optionally of the requested type */ +{ + xs *md5 = xs_md5_hex(id, strlen(id)); + + return object_get_by_md5(md5, obj, type); +} + + +int object_add(const char *id, d_char *obj) +/* stores an object */ +{ + int status = 201; /* Created */ + xs *fn = _object_fn(id); + FILE *f; + + if ((f = fopen(fn, "w")) != NULL) { + flock(fileno(f), LOCK_EX); + + xs *j = xs_json_dumps_pp(obj, 4); + + fwrite(j, strlen(j), 1, f); + fclose(f); + } + else + status = 500; + + srv_debug(2, xs_fmt("object_add %s %s %d", id, fn, status)); + + return status; +} + + +int object_del(const char *id) +/* deletes an object */ +{ + int status = 404; + xs *fn = _object_fn(id); + + if (fn != NULL && unlink(fn) != -1) + status = 200; + + srv_debug(2, xs_fmt("object_del %s %d", id, status)); + + return status; +} + + d_char *_follower_fn(snac *snac, char *actor) { xs *md5 = xs_md5_hex(actor, strlen(actor)); @@ -52,10 +52,10 @@ void srv_archive(char *direction, char *req, char *payload, int p_size, double mtime(char *fn); -int listfile_add_md5(const char *fn, const char *md5); -int listfile_del_md5(const char *fn, const char *md5); -d_char *listfile_get_n(const char *fn, int max); -d_char *listfile_get_inv_n(const char *fn, int max); +int index_add(const char *fn, const char *md5); +int index_del(const char *fn, const char *md5); +d_char *index_list(const char *fn, int max); +d_char *index_list_desc(const char *fn, int max); int follower_add(snac *snac, char *actor, char *msg); int follower_del(snac *snac, char *actor); |