diff options
| -rw-r--r-- | data.c | 47 | ||||
| -rw-r--r-- | snac.h | 2 |
2 files changed, 40 insertions, 9 deletions
| @@ -185,7 +185,7 @@ d_char *user_list(void) | |||
| 185 | } | 185 | } |
| 186 | 186 | ||
| 187 | 187 | ||
| 188 | double mtime(char *fn) | 188 | double mtime(const char *fn) |
| 189 | /* returns the mtime of a file or directory, or 0.0 */ | 189 | /* returns the mtime of a file or directory, or 0.0 */ |
| 190 | { | 190 | { |
| 191 | struct stat st; | 191 | struct stat st; |
| @@ -1477,22 +1477,51 @@ d_char *dequeue(snac *snac, char *fn) | |||
| 1477 | } | 1477 | } |
| 1478 | 1478 | ||
| 1479 | 1479 | ||
| 1480 | static void _purge_file(const char *fn, int days) | ||
| 1481 | { | ||
| 1482 | time_t mt = time(NULL) - days * 24 * 3600; | ||
| 1483 | |||
| 1484 | if (mtime(fn) < mt) { | ||
| 1485 | /* older than the minimum time: delete it */ | ||
| 1486 | unlink(fn); | ||
| 1487 | srv_debug(1, xs_fmt("purged %s", fn)); | ||
| 1488 | } | ||
| 1489 | } | ||
| 1490 | |||
| 1491 | |||
| 1480 | static void _purge_subdir(snac *snac, const char *subdir, int days) | 1492 | static void _purge_subdir(snac *snac, const char *subdir, int days) |
| 1481 | /* purges all files in subdir older than days */ | 1493 | /* purges all files in subdir older than days */ |
| 1482 | { | 1494 | { |
| 1483 | if (days) { | 1495 | if (days) { |
| 1484 | time_t mt = time(NULL) - days * 24 * 3600; | ||
| 1485 | xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir); | 1496 | xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir); |
| 1486 | xs *list = xs_glob(spec, 0, 0); | 1497 | xs *list = xs_glob(spec, 0, 0); |
| 1487 | char *p, *v; | 1498 | char *p, *v; |
| 1488 | 1499 | ||
| 1489 | p = list; | 1500 | p = list; |
| 1490 | while (xs_list_iter(&p, &v)) { | 1501 | while (xs_list_iter(&p, &v)) |
| 1491 | if (mtime(v) < mt) { | 1502 | _purge_file(v, days); |
| 1492 | /* older than the minimum time: delete it */ | 1503 | } |
| 1493 | unlink(v); | 1504 | } |
| 1494 | snac_debug(snac, 1, xs_fmt("purged %s", v)); | 1505 | |
| 1495 | } | 1506 | |
| 1507 | void purge_server(void) | ||
| 1508 | /* purge global server data */ | ||
| 1509 | { | ||
| 1510 | int tpd = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days")); | ||
| 1511 | // int lpd = xs_number_get(xs_dict_get(srv_config, "local_purge_days")); | ||
| 1512 | xs *spec = xs_fmt("%s/object/??", srv_basedir); | ||
| 1513 | xs *dirs = xs_glob(spec, 0, 0); | ||
| 1514 | char *p, *v; | ||
| 1515 | |||
| 1516 | p = dirs; | ||
| 1517 | while (xs_list_iter(&p, &v)) { | ||
| 1518 | xs *spec2 = xs_fmt("%s/" "*", v); | ||
| 1519 | xs *files = xs_glob(spec2, 0, 0); | ||
| 1520 | char *p2, *v2; | ||
| 1521 | |||
| 1522 | p2 = files; | ||
| 1523 | while (xs_list_iter(&p2, &v2)) { | ||
| 1524 | _purge_file(v2, tpd); | ||
| 1496 | } | 1525 | } |
| 1497 | } | 1526 | } |
| 1498 | } | 1527 | } |
| @@ -1515,6 +1544,8 @@ void purge_user(snac *snac) | |||
| 1515 | void purge_all(void) | 1544 | void purge_all(void) |
| 1516 | /* purge all users */ | 1545 | /* purge all users */ |
| 1517 | { | 1546 | { |
| 1547 | purge_server(); | ||
| 1548 | |||
| 1518 | snac snac; | 1549 | snac snac; |
| 1519 | xs *list = user_list(); | 1550 | xs *list = user_list(); |
| 1520 | char *p, *uid; | 1551 | char *p, *uid; |
| @@ -50,7 +50,7 @@ int check_password(char *uid, char *passwd, char *hash); | |||
| 50 | void srv_archive(char *direction, char *req, char *payload, int p_size, | 50 | void srv_archive(char *direction, char *req, char *payload, int p_size, |
| 51 | int status, char *headers, char *body, int b_size); | 51 | int status, char *headers, char *body, int b_size); |
| 52 | 52 | ||
| 53 | double mtime(char *fn); | 53 | double mtime(const char *fn); |
| 54 | 54 | ||
| 55 | int index_add(const char *fn, const char *md5); | 55 | int index_add(const char *fn, const char *md5); |
| 56 | int index_del(const char *fn, const char *md5); | 56 | int index_del(const char *fn, const char *md5); |