chiark / gitweb /
journal: add call to determine current journal file disk usage
[elogind.git] / src / journal / sd-journal.c
index 33686ed2b26529793f78b8f8d924bfff4abd9d25..b4d35eebafd7612f7b8cd128bc0bc996d810b624 100644 (file)
@@ -1118,7 +1118,7 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
                 return 0;
         }
 
-        r = journal_file_open(path, O_RDONLY, 0, NULL, NULL, &f);
+        r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
         free(path);
 
         if (r < 0) {
@@ -1439,6 +1439,15 @@ static sd_journal *journal_new(int flags, const char *path) {
                 return NULL;
         }
 
+        j->mmap = mmap_cache_new();
+        if (!j->mmap) {
+                hashmap_free(j->files);
+                hashmap_free(j->directories_by_path);
+                free(j->path);
+                free(j);
+                return NULL;
+        }
+
         return j;
 }
 
@@ -1527,6 +1536,9 @@ _public_ void sd_journal_close(sd_journal *j) {
 
         sd_journal_flush_matches(j);
 
+        if (j->mmap)
+                mmap_cache_unref(j->mmap);
+
         free(j->path);
         free(j);
 }
@@ -2050,6 +2062,29 @@ void journal_print_header(sd_journal *j) {
         }
 }
 
+_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
+        Iterator i;
+        JournalFile *f;
+        uint64_t sum = 0;
+
+        if (!j)
+                return -EINVAL;
+        if (!bytes)
+                return -EINVAL;
+
+        HASHMAP_FOREACH(f, j->files, i) {
+                struct stat st;
+
+                if (fstat(f->fd, &st) < 0)
+                        return -errno;
+
+                sum += (uint64_t) st.st_blocks * 512ULL;
+        }
+
+        *bytes = sum;
+        return 0;
+}
+
 /* _public_ int sd_journal_query_unique(sd_journal *j, const char *field) { */
 /*         if (!j) */
 /*                 return -EINVAL; */