commit 9d7b359
default
·
2022-11-24 07:00:35 +0000 UTC
parent cde8650
Renamed listfile functions to index.
M
data.c
+111,
-111
1@@ -191,112 +191,10 @@ double mtime(char *fn)
2 }
3
4
5-/** object database 2.1+ **/
6+/** database 2.1+ **/
7
8-d_char *_object_fn_by_md5(const char *md5)
9-{
10- xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
11-
12- mkdir(bfn, 0755);
13-
14- return xs_fmt("%s/%s.json", bfn, md5);
15-}
16-
17-
18-d_char *_object_fn(const char *id)
19-{
20- xs *md5 = xs_md5_hex(id, strlen(id));
21-
22- return _object_fn_by_md5(md5);
23-}
24-
25-
26-int object_get_by_md5(const char *md5, d_char **obj, const char *type)
27-/* returns a loaded object, optionally of the requested type */
28-{
29- int status = 404;
30- xs *fn = _object_fn_by_md5(md5);
31- FILE *f;
32-
33- if ((f = fopen(fn, "r")) != NULL) {
34- flock(fileno(f), LOCK_SH);
35-
36- xs *j = xs_readall(f);
37- fclose(f);
38-
39- *obj = xs_json_loads(j);
40-
41- if (*obj) {
42- status = 200;
43-
44- /* specific type requested? */
45- if (!xs_is_null(type)) {
46- char *v = xs_dict_get(*obj, "type");
47-
48- if (xs_is_null(v) || strcmp(v, type) != 0) {
49- status = 404;
50- *obj = xs_free(*obj);
51- }
52- }
53- }
54- }
55- else
56- *obj = NULL;
57-
58- return status;
59-}
60-
61-
62-int object_get(const char *id, d_char **obj, const char *type)
63-/* returns a loaded object, optionally of the requested type */
64-{
65- xs *md5 = xs_md5_hex(id, strlen(id));
66-
67- return object_get_by_md5(md5, obj, type);
68-}
69-
70-
71-int object_add(const char *id, d_char *obj)
72-/* stores an object */
73-{
74- int status = 201; /* Created */
75- xs *fn = _object_fn(id);
76- FILE *f;
77-
78- if ((f = fopen(fn, "w")) != NULL) {
79- flock(fileno(f), LOCK_EX);
80-
81- xs *j = xs_json_dumps_pp(obj, 4);
82-
83- fwrite(j, strlen(j), 1, f);
84- fclose(f);
85- }
86- else
87- status = 500;
88-
89- srv_debug(2, xs_fmt("object_add %s %s %d", id, fn, status));
90-
91- return status;
92-}
93-
94-
95-int object_del(const char *id)
96-/* deletes an object */
97-{
98- int status = 404;
99- xs *fn = _object_fn(id);
100-
101- if (fn != NULL && unlink(fn) != -1)
102- status = 200;
103-
104- srv_debug(2, xs_fmt("object_del %s %d", id, status));
105-
106- return status;
107-}
108-
109-
110-int listfile_add_md5(const char *fn, const char *md5)
111-/* adds an md5 to a list */
112+int index_add(const char *fn, const char *md5)
113+/* adds an md5 to an index */
114 {
115 int status = 200;
116 FILE *f;
117@@ -314,8 +212,8 @@ int listfile_add_md5(const char *fn, const char *md5)
118 }
119
120
121-int listfile_del_md5(const char *fn, const char *md5)
122-/* deletes an md5 from a list */
123+int index_del(const char *fn, const char *md5)
124+/* deletes an md5 from an index */
125 {
126 int status = 404;
127 FILE *i, *o;
128@@ -352,8 +250,8 @@ int listfile_del_md5(const char *fn, const char *md5)
129 }
130
131
132-d_char *listfile_get_n(const char *fn, int max)
133-/* returns a list */
134+d_char *index_list(const char *fn, int max)
135+/* returns an index as a list */
136 {
137 d_char *list = NULL;
138 FILE *f;
139@@ -378,8 +276,8 @@ d_char *listfile_get_n(const char *fn, int max)
140 }
141
142
143-d_char *listfile_get_inv_n(const char *fn, int max)
144-/* returns a list, inversely */
145+d_char *index_list_desc(const char *fn, int max)
146+/* returns an index as a list, in reverse order */
147 {
148 d_char *list = NULL;
149 FILE *f;
150@@ -411,6 +309,108 @@ d_char *listfile_get_inv_n(const char *fn, int max)
151 }
152
153
154+d_char *_object_fn_by_md5(const char *md5)
155+{
156+ xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
157+
158+ mkdir(bfn, 0755);
159+
160+ return xs_fmt("%s/%s.json", bfn, md5);
161+}
162+
163+
164+d_char *_object_fn(const char *id)
165+{
166+ xs *md5 = xs_md5_hex(id, strlen(id));
167+
168+ return _object_fn_by_md5(md5);
169+}
170+
171+
172+int object_get_by_md5(const char *md5, d_char **obj, const char *type)
173+/* returns a loaded object, optionally of the requested type */
174+{
175+ int status = 404;
176+ xs *fn = _object_fn_by_md5(md5);
177+ FILE *f;
178+
179+ if ((f = fopen(fn, "r")) != NULL) {
180+ flock(fileno(f), LOCK_SH);
181+
182+ xs *j = xs_readall(f);
183+ fclose(f);
184+
185+ *obj = xs_json_loads(j);
186+
187+ if (*obj) {
188+ status = 200;
189+
190+ /* specific type requested? */
191+ if (!xs_is_null(type)) {
192+ char *v = xs_dict_get(*obj, "type");
193+
194+ if (xs_is_null(v) || strcmp(v, type) != 0) {
195+ status = 404;
196+ *obj = xs_free(*obj);
197+ }
198+ }
199+ }
200+ }
201+ else
202+ *obj = NULL;
203+
204+ return status;
205+}
206+
207+
208+int object_get(const char *id, d_char **obj, const char *type)
209+/* returns a loaded object, optionally of the requested type */
210+{
211+ xs *md5 = xs_md5_hex(id, strlen(id));
212+
213+ return object_get_by_md5(md5, obj, type);
214+}
215+
216+
217+int object_add(const char *id, d_char *obj)
218+/* stores an object */
219+{
220+ int status = 201; /* Created */
221+ xs *fn = _object_fn(id);
222+ FILE *f;
223+
224+ if ((f = fopen(fn, "w")) != NULL) {
225+ flock(fileno(f), LOCK_EX);
226+
227+ xs *j = xs_json_dumps_pp(obj, 4);
228+
229+ fwrite(j, strlen(j), 1, f);
230+ fclose(f);
231+ }
232+ else
233+ status = 500;
234+
235+ srv_debug(2, xs_fmt("object_add %s %s %d", id, fn, status));
236+
237+ return status;
238+}
239+
240+
241+int object_del(const char *id)
242+/* deletes an object */
243+{
244+ int status = 404;
245+ xs *fn = _object_fn(id);
246+
247+ if (fn != NULL && unlink(fn) != -1)
248+ status = 200;
249+
250+ srv_debug(2, xs_fmt("object_del %s %d", id, status));
251+
252+ return status;
253+}
254+
255+
256 d_char *_follower_fn(snac *snac, char *actor)
257 {
258 xs *md5 = xs_md5_hex(actor, strlen(actor));
M
snac.h
+4,
-4
1@@ -52,10 +52,10 @@ void srv_archive(char *direction, char *req, char *payload, int p_size,
2
3 double mtime(char *fn);
4
5-int listfile_add_md5(const char *fn, const char *md5);
6-int listfile_del_md5(const char *fn, const char *md5);
7-d_char *listfile_get_n(const char *fn, int max);
8-d_char *listfile_get_inv_n(const char *fn, int max);
9+int index_add(const char *fn, const char *md5);
10+int index_del(const char *fn, const char *md5);
11+d_char *index_list(const char *fn, int max);
12+d_char *index_list_desc(const char *fn, int max);
13
14 int follower_add(snac *snac, char *actor, char *msg);
15 int follower_del(snac *snac, char *actor);