commit d0e0827

default  ·  2023-04-28 06:58:33 +0000 UTC
parent 765f637
Incomplete mastoapi apps are purged.
1 files changed,  +35, -2
+35, -2
 1@@ -9,6 +9,7 @@
 2 #include "xs_json.h"
 3 #include "xs_io.h"
 4 #include "xs_time.h"
 5+#include "xs_glob.h"
 6 
 7 #include "snac.h"
 8 
 9@@ -59,13 +60,19 @@ int app_add(const char *id, const xs_dict *app)
10 }
11 
12 
13+xs_str *_app_fn(const char *id)
14+{
15+    return xs_fmt("%s/app/%s.json", srv_basedir, id);
16+}
17+
18+
19 xs_dict *app_get(const char *id)
20 /* gets an app */
21 {
22     if (!xs_is_hex(id))
23         return NULL;
24 
25-    xs *fn       = xs_fmt("%s/app/%s.json", srv_basedir, id);
26+    xs *fn       = _app_fn(id);
27     xs_dict *app = NULL;
28     FILE *f;
29 
30@@ -86,7 +93,7 @@ int app_del(const char *id)
31     if (!xs_is_hex(id))
32         return -1;
33 
34-    xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
35+    xs *fn = _app_fn(id);
36 
37     return unlink(fn);
38 }
39@@ -1963,6 +1970,32 @@ int mastoapi_put_handler(const xs_dict *req, const char *q_path,
40 
41 void mastoapi_purge(void)
42 {
43+    xs *spec   = xs_fmt("%s/app/" "*.json", srv_basedir);
44+    xs *files  = xs_glob(spec, 1, 0);
45+    xs_list *p = files;
46+    xs_str *v;
47+
48+    time_t mt = time(NULL) - 3600;
49+
50+    while (xs_list_iter(&p, &v)) {
51+        xs *cid = xs_replace(v, ".json", "");
52+        xs *fn  = _app_fn(cid);
53+
54+        if (mtime(fn) < mt) {
55+            /* get the app */
56+            xs *app = app_get(cid);
57+
58+            if (app) {
59+                /* old apps with no uid are incomplete cruft */
60+                const char *uid = xs_dict_get(app, "uid");
61+
62+                if (xs_is_null(uid) || *uid == '\0') {
63+                    unlink(fn);
64+                    srv_debug(2, xs_fmt("purged %s", fn));
65+                }
66+            }
67+        }
68+    }
69 }
70 
71