Skip to content

Commit

Permalink
upload-queue: clean up the fail queue periodically
Browse files Browse the repository at this point in the history
Keep failed transactions around for a couple of weeks, just in
case there are some useful edits in there that a user would like
to go to the trouble of extracting, but clean them up thereafter
to avoid amassing gigs of useless changes.

Signed-off-by: Bob Copeland <[email protected]>
  • Loading branch information
Bob Copeland committed Jan 4, 2016
1 parent 8b3b0f6 commit 935e43f
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions upload-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
#include <fcntl.h>
#include <signal.h>

/* keep around failed updates for a couple of weeks */
#define FAIL_MAX_AGE 86400 * 14

static void make_upload_dir(const char *path)
{
_cleanup_free_ char *base_path = NULL;
Expand Down Expand Up @@ -92,6 +95,42 @@ static void upload_queue_write_entry(const char *entry, unsigned const char key[
config_write_encrypted_string(name, entry, key);
}

static void upload_queue_cleanup_failures()
{
_cleanup_free_ char *base_path = config_path("upload-fail");
DIR *dir = opendir(base_path);
struct dirent *entry;
char *p;
struct stat sbuf;
int ret;

if (!dir)
return;

while ((entry = readdir(dir))) {
_cleanup_free_ char *fn = NULL;

if (entry->d_type != DT_REG)
continue;
for (p = entry->d_name; *p; ++p) {
if (!isdigit(*p))
break;
}
if (*p)
continue;

xasprintf(&fn, "%s/%s", base_path, entry->d_name);
ret = stat(fn, &sbuf);
if (ret)
continue;

if ((time(NULL) - sbuf.st_mtime) > FAIL_MAX_AGE) {
unlink(fn);
}
}
closedir(dir);
}

static void upload_queue_drop(const char *name)
{
_cleanup_free_ char *newname = NULL;
Expand All @@ -112,6 +151,8 @@ static void upload_queue_drop(const char *name)
old_full = config_path(name);
new_full = config_path(newname);
rename(old_full, new_full);

upload_queue_cleanup_failures();
}

static char *upload_queue_next_entry(unsigned const char key[KDF_HASH_LEN], char **name, char **lock)
Expand Down

0 comments on commit 935e43f

Please sign in to comment.