diff options
| author | 2022-11-24 08:00:35 +0100 | |
|---|---|---|
| committer | 2022-11-24 08:00:35 +0100 | |
| commit | 9d7b35940f2a09b2ef1a8e1e705821b362ebf7c9 (patch) | |
| tree | d91731b9abf6c983d638831a72cc234cbed4aff1 | |
| parent | Don't search explicitly for Person in actor_get(). (diff) | |
| download | snac2-9d7b35940f2a09b2ef1a8e1e705821b362ebf7c9.tar.gz snac2-9d7b35940f2a09b2ef1a8e1e705821b362ebf7c9.tar.xz snac2-9d7b35940f2a09b2ef1a8e1e705821b362ebf7c9.zip | |
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) | |||
| 191 | } | 191 | } |
| 192 | 192 | ||
| 193 | 193 | ||
| 194 | /** object database 2.1+ **/ | 194 | /** database 2.1+ **/ |
| 195 | 195 | ||
| 196 | d_char *_object_fn_by_md5(const char *md5) | 196 | int index_add(const char *fn, const char *md5) |
| 197 | { | 197 | /* adds an md5 to an index */ |
| 198 | xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]); | ||
| 199 | |||
| 200 | mkdir(bfn, 0755); | ||
| 201 | |||
| 202 | return xs_fmt("%s/%s.json", bfn, md5); | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | d_char *_object_fn(const char *id) | ||
| 207 | { | ||
| 208 | xs *md5 = xs_md5_hex(id, strlen(id)); | ||
| 209 | |||
| 210 | return _object_fn_by_md5(md5); | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | int object_get_by_md5(const char *md5, d_char **obj, const char *type) | ||
| 215 | /* returns a loaded object, optionally of the requested type */ | ||
| 216 | { | ||
| 217 | int status = 404; | ||
| 218 | xs *fn = _object_fn_by_md5(md5); | ||
| 219 | FILE *f; | ||
| 220 | |||
| 221 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 222 | flock(fileno(f), LOCK_SH); | ||
| 223 | |||
| 224 | xs *j = xs_readall(f); | ||
| 225 | fclose(f); | ||
| 226 | |||
| 227 | *obj = xs_json_loads(j); | ||
| 228 | |||
| 229 | if (*obj) { | ||
| 230 | status = 200; | ||
| 231 | |||
| 232 | /* specific type requested? */ | ||
| 233 | if (!xs_is_null(type)) { | ||
| 234 | char *v = xs_dict_get(*obj, "type"); | ||
| 235 | |||
| 236 | if (xs_is_null(v) || strcmp(v, type) != 0) { | ||
| 237 | status = 404; | ||
| 238 | *obj = xs_free(*obj); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } | ||
| 243 | else | ||
| 244 | *obj = NULL; | ||
| 245 | |||
| 246 | return status; | ||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | int object_get(const char *id, d_char **obj, const char *type) | ||
| 251 | /* returns a loaded object, optionally of the requested type */ | ||
| 252 | { | ||
| 253 | xs *md5 = xs_md5_hex(id, strlen(id)); | ||
| 254 | |||
| 255 | return object_get_by_md5(md5, obj, type); | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | int object_add(const char *id, d_char *obj) | ||
| 260 | /* stores an object */ | ||
| 261 | { | ||
| 262 | int status = 201; /* Created */ | ||
| 263 | xs *fn = _object_fn(id); | ||
| 264 | FILE *f; | ||
| 265 | |||
| 266 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 267 | flock(fileno(f), LOCK_EX); | ||
| 268 | |||
| 269 | xs *j = xs_json_dumps_pp(obj, 4); | ||
| 270 | |||
| 271 | fwrite(j, strlen(j), 1, f); | ||
| 272 | fclose(f); | ||
| 273 | } | ||
| 274 | else | ||
| 275 | status = 500; | ||
| 276 | |||
| 277 | srv_debug(2, xs_fmt("object_add %s %s %d", id, fn, status)); | ||
| 278 | |||
| 279 | return status; | ||
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | int object_del(const char *id) | ||
| 284 | /* deletes an object */ | ||
| 285 | { | ||
| 286 | int status = 404; | ||
| 287 | xs *fn = _object_fn(id); | ||
| 288 | |||
| 289 | if (fn != NULL && unlink(fn) != -1) | ||
| 290 | status = 200; | ||
| 291 | |||
| 292 | srv_debug(2, xs_fmt("object_del %s %d", id, status)); | ||
| 293 | |||
| 294 | return status; | ||
| 295 | } | ||
| 296 | |||
| 297 | |||
| 298 | int listfile_add_md5(const char *fn, const char *md5) | ||
| 299 | /* adds an md5 to a list */ | ||
| 300 | { | 198 | { |
| 301 | int status = 200; | 199 | int status = 200; |
| 302 | FILE *f; | 200 | FILE *f; |
| @@ -314,8 +212,8 @@ int listfile_add_md5(const char *fn, const char *md5) | |||
| 314 | } | 212 | } |
| 315 | 213 | ||
| 316 | 214 | ||
| 317 | int listfile_del_md5(const char *fn, const char *md5) | 215 | int index_del(const char *fn, const char *md5) |
| 318 | /* deletes an md5 from a list */ | 216 | /* deletes an md5 from an index */ |
| 319 | { | 217 | { |
| 320 | int status = 404; | 218 | int status = 404; |
| 321 | FILE *i, *o; | 219 | FILE *i, *o; |
| @@ -352,8 +250,8 @@ int listfile_del_md5(const char *fn, const char *md5) | |||
| 352 | } | 250 | } |
| 353 | 251 | ||
| 354 | 252 | ||
| 355 | d_char *listfile_get_n(const char *fn, int max) | 253 | d_char *index_list(const char *fn, int max) |
| 356 | /* returns a list */ | 254 | /* returns an index as a list */ |
| 357 | { | 255 | { |
| 358 | d_char *list = NULL; | 256 | d_char *list = NULL; |
| 359 | FILE *f; | 257 | FILE *f; |
| @@ -378,8 +276,8 @@ d_char *listfile_get_n(const char *fn, int max) | |||
| 378 | } | 276 | } |
| 379 | 277 | ||
| 380 | 278 | ||
| 381 | d_char *listfile_get_inv_n(const char *fn, int max) | 279 | d_char *index_list_desc(const char *fn, int max) |
| 382 | /* returns a list, inversely */ | 280 | /* returns an index as a list, in reverse order */ |
| 383 | { | 281 | { |
| 384 | d_char *list = NULL; | 282 | d_char *list = NULL; |
| 385 | FILE *f; | 283 | FILE *f; |
| @@ -411,6 +309,108 @@ d_char *listfile_get_inv_n(const char *fn, int max) | |||
| 411 | } | 309 | } |
| 412 | 310 | ||
| 413 | 311 | ||
| 312 | d_char *_object_fn_by_md5(const char *md5) | ||
| 313 | { | ||
| 314 | xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]); | ||
| 315 | |||
| 316 | mkdir(bfn, 0755); | ||
| 317 | |||
| 318 | return xs_fmt("%s/%s.json", bfn, md5); | ||
| 319 | } | ||
| 320 | |||
| 321 | |||
| 322 | d_char *_object_fn(const char *id) | ||
| 323 | { | ||
| 324 | xs *md5 = xs_md5_hex(id, strlen(id)); | ||
| 325 | |||
| 326 | return _object_fn_by_md5(md5); | ||
| 327 | } | ||
| 328 | |||
| 329 | |||
| 330 | int object_get_by_md5(const char *md5, d_char **obj, const char *type) | ||
| 331 | /* returns a loaded object, optionally of the requested type */ | ||
| 332 | { | ||
| 333 | int status = 404; | ||
| 334 | xs *fn = _object_fn_by_md5(md5); | ||
| 335 | FILE *f; | ||
| 336 | |||
| 337 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 338 | flock(fileno(f), LOCK_SH); | ||
| 339 | |||
| 340 | xs *j = xs_readall(f); | ||
| 341 | fclose(f); | ||
| 342 | |||
| 343 | *obj = xs_json_loads(j); | ||
| 344 | |||
| 345 | if (*obj) { | ||
| 346 | status = 200; | ||
| 347 | |||
| 348 | /* specific type requested? */ | ||
| 349 | if (!xs_is_null(type)) { | ||
| 350 | char *v = xs_dict_get(*obj, "type"); | ||
| 351 | |||
| 352 | if (xs_is_null(v) || strcmp(v, type) != 0) { | ||
| 353 | status = 404; | ||
| 354 | *obj = xs_free(*obj); | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | } | ||
| 359 | else | ||
| 360 | *obj = NULL; | ||
| 361 | |||
| 362 | return status; | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | int object_get(const char *id, d_char **obj, const char *type) | ||
| 367 | /* returns a loaded object, optionally of the requested type */ | ||
| 368 | { | ||
| 369 | xs *md5 = xs_md5_hex(id, strlen(id)); | ||
| 370 | |||
| 371 | return object_get_by_md5(md5, obj, type); | ||
| 372 | } | ||
| 373 | |||
| 374 | |||
| 375 | int object_add(const char *id, d_char *obj) | ||
| 376 | /* stores an object */ | ||
| 377 | { | ||
| 378 | int status = 201; /* Created */ | ||
| 379 | xs *fn = _object_fn(id); | ||
| 380 | FILE *f; | ||
| 381 | |||
| 382 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 383 | flock(fileno(f), LOCK_EX); | ||
| 384 | |||
| 385 | xs *j = xs_json_dumps_pp(obj, 4); | ||
| 386 | |||
| 387 | fwrite(j, strlen(j), 1, f); | ||
| 388 | fclose(f); | ||
| 389 | } | ||
| 390 | else | ||
| 391 | status = 500; | ||
| 392 | |||
| 393 | srv_debug(2, xs_fmt("object_add %s %s %d", id, fn, status)); | ||
| 394 | |||
| 395 | return status; | ||
| 396 | } | ||
| 397 | |||
| 398 | |||
| 399 | int object_del(const char *id) | ||
| 400 | /* deletes an object */ | ||
| 401 | { | ||
| 402 | int status = 404; | ||
| 403 | xs *fn = _object_fn(id); | ||
| 404 | |||
| 405 | if (fn != NULL && unlink(fn) != -1) | ||
| 406 | status = 200; | ||
| 407 | |||
| 408 | srv_debug(2, xs_fmt("object_del %s %d", id, status)); | ||
| 409 | |||
| 410 | return status; | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | d_char *_follower_fn(snac *snac, char *actor) | 414 | d_char *_follower_fn(snac *snac, char *actor) |
| 415 | { | 415 | { |
| 416 | xs *md5 = xs_md5_hex(actor, strlen(actor)); | 416 | xs *md5 = xs_md5_hex(actor, strlen(actor)); |
| @@ -52,10 +52,10 @@ void srv_archive(char *direction, char *req, char *payload, int p_size, | |||
| 52 | 52 | ||
| 53 | double mtime(char *fn); | 53 | double mtime(char *fn); |
| 54 | 54 | ||
| 55 | int listfile_add_md5(const char *fn, const char *md5); | 55 | int index_add(const char *fn, const char *md5); |
| 56 | int listfile_del_md5(const char *fn, const char *md5); | 56 | int index_del(const char *fn, const char *md5); |
| 57 | d_char *listfile_get_n(const char *fn, int max); | 57 | d_char *index_list(const char *fn, int max); |
| 58 | d_char *listfile_get_inv_n(const char *fn, int max); | 58 | d_char *index_list_desc(const char *fn, int max); |
| 59 | 59 | ||
| 60 | int follower_add(snac *snac, char *actor, char *msg); | 60 | int follower_add(snac *snac, char *actor, char *msg); |
| 61 | int follower_del(snac *snac, char *actor); | 61 | int follower_del(snac *snac, char *actor); |