chiark / gitweb /
journal/vacuum: cleanup
[elogind.git] / src / journal / journal-vacuum.c
index 22c9cfcd526a02e78574e304ff20f8a95a0fcf1e..1ddb043e2cd13ccc0bbf4a70984455752382d1fe 100644 (file)
@@ -36,7 +36,7 @@
 #include "util.h"
 
 struct vacuum_info {
-        off_t usage;
+        uint64_t usage;
         char *filename;
 
         uint64_t realtime;
@@ -135,10 +135,11 @@ int journal_directory_vacuum(
                 usec_t max_retention_usec,
                 usec_t *oldest_usec) {
 
-        DIR *d;
+        _cleanup_closedir_ DIR *d = NULL;
         int r = 0;
         struct vacuum_info *list = NULL;
-        unsigned n_list = 0, n_allocated = 0, i;
+        unsigned n_list = 0, i;
+        size_t n_allocated = 0;
         uint64_t sum = 0;
         usec_t retention_limit = 0;
 
@@ -243,23 +244,12 @@ int journal_directory_vacuum(
 
                         have_seqnum = false;
                 } else
+                        /* We do not vacuum active files or unknown files! */
                         continue;
 
                 patch_realtime(directory, de->d_name, &st, &realtime);
 
-                if (n_list >= n_allocated) {
-                        struct vacuum_info *j;
-
-                        n_allocated = MAX(n_allocated * 2U, 8U);
-                        j = realloc(list, n_allocated * sizeof(struct vacuum_info));
-                        if (!j) {
-                                free(p);
-                                r = -ENOMEM;
-                                goto finish;
-                        }
-
-                        list = j;
-                }
+                GREEDY_REALLOC(list, n_allocated, n_list + 1);
 
                 list[n_list].filename = p;
                 list[n_list].usage = 512UL * (uint64_t) st.st_blocks;
@@ -291,7 +281,12 @@ int journal_directory_vacuum(
 
                 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
                         log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
-                        sum -= list[i].usage;
+
+                        if (list[i].usage < sum)
+                                sum -= list[i].usage;
+                        else
+                                sum = 0;
+
                 } else if (errno != ENOENT)
                         log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
         }
@@ -302,11 +297,7 @@ int journal_directory_vacuum(
 finish:
         for (i = 0; i < n_list; i++)
                 free(list[i].filename);
-
         free(list);
 
-        if (d)
-                closedir(d);
-
         return r;
 }