diff options
Diffstat (limited to 'mastoapi.c')
| -rw-r--r-- | mastoapi.c | 37 |
1 files changed, 35 insertions, 2 deletions
| @@ -9,6 +9,7 @@ | |||
| 9 | #include "xs_json.h" | 9 | #include "xs_json.h" |
| 10 | #include "xs_io.h" | 10 | #include "xs_io.h" |
| 11 | #include "xs_time.h" | 11 | #include "xs_time.h" |
| 12 | #include "xs_glob.h" | ||
| 12 | 13 | ||
| 13 | #include "snac.h" | 14 | #include "snac.h" |
| 14 | 15 | ||
| @@ -59,13 +60,19 @@ int app_add(const char *id, const xs_dict *app) | |||
| 59 | } | 60 | } |
| 60 | 61 | ||
| 61 | 62 | ||
| 63 | xs_str *_app_fn(const char *id) | ||
| 64 | { | ||
| 65 | return xs_fmt("%s/app/%s.json", srv_basedir, id); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 62 | xs_dict *app_get(const char *id) | 69 | xs_dict *app_get(const char *id) |
| 63 | /* gets an app */ | 70 | /* gets an app */ |
| 64 | { | 71 | { |
| 65 | if (!xs_is_hex(id)) | 72 | if (!xs_is_hex(id)) |
| 66 | return NULL; | 73 | return NULL; |
| 67 | 74 | ||
| 68 | xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id); | 75 | xs *fn = _app_fn(id); |
| 69 | xs_dict *app = NULL; | 76 | xs_dict *app = NULL; |
| 70 | FILE *f; | 77 | FILE *f; |
| 71 | 78 | ||
| @@ -86,7 +93,7 @@ int app_del(const char *id) | |||
| 86 | if (!xs_is_hex(id)) | 93 | if (!xs_is_hex(id)) |
| 87 | return -1; | 94 | return -1; |
| 88 | 95 | ||
| 89 | xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id); | 96 | xs *fn = _app_fn(id); |
| 90 | 97 | ||
| 91 | return unlink(fn); | 98 | return unlink(fn); |
| 92 | } | 99 | } |
| @@ -1963,6 +1970,32 @@ int mastoapi_put_handler(const xs_dict *req, const char *q_path, | |||
| 1963 | 1970 | ||
| 1964 | void mastoapi_purge(void) | 1971 | void mastoapi_purge(void) |
| 1965 | { | 1972 | { |
| 1973 | xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir); | ||
| 1974 | xs *files = xs_glob(spec, 1, 0); | ||
| 1975 | xs_list *p = files; | ||
| 1976 | xs_str *v; | ||
| 1977 | |||
| 1978 | time_t mt = time(NULL) - 3600; | ||
| 1979 | |||
| 1980 | while (xs_list_iter(&p, &v)) { | ||
| 1981 | xs *cid = xs_replace(v, ".json", ""); | ||
| 1982 | xs *fn = _app_fn(cid); | ||
| 1983 | |||
| 1984 | if (mtime(fn) < mt) { | ||
| 1985 | /* get the app */ | ||
| 1986 | xs *app = app_get(cid); | ||
| 1987 | |||
| 1988 | if (app) { | ||
| 1989 | /* old apps with no uid are incomplete cruft */ | ||
| 1990 | const char *uid = xs_dict_get(app, "uid"); | ||
| 1991 | |||
| 1992 | if (xs_is_null(uid) || *uid == '\0') { | ||
| 1993 | unlink(fn); | ||
| 1994 | srv_debug(2, xs_fmt("purged %s", fn)); | ||
| 1995 | } | ||
| 1996 | } | ||
| 1997 | } | ||
| 1998 | } | ||
| 1966 | } | 1999 | } |
| 1967 | 2000 | ||
| 1968 | 2001 | ||