From 947296a00d17ef115f4097aa86a8c6f1bfc8bf58 Mon Sep 17 00:00:00 2001
From: David Tardon <dtardon@redhat.com>
Date: Wed, 22 Mar 2023 12:36:54 +0100
Subject: [PATCH] journal-vacuum: count size of all journal files

Currently, active journal files are excluded, which means that vacuuming
may not remove anything even if *MaxUse= has been exceeded.

(cherry picked from commit 9ea46af4f2368b41d57705bac09774778126507f)

Resolves: #2180380
---
 src/journal/journal-vacuum.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/journal/journal-vacuum.c b/src/journal/journal-vacuum.c
index 8d3ae71440..dd21f40966 100644
--- a/src/journal/journal-vacuum.c
+++ b/src/journal/journal-vacuum.c
@@ -179,6 +179,8 @@ int journal_directory_vacuum(
                 if (!S_ISREG(st.st_mode))
                         continue;
 
+                size = 512UL * (uint64_t) st.st_blocks;
+
                 q = strlen(de->d_name);
 
                 if (endswith(de->d_name, ".journal")) {
@@ -188,6 +190,7 @@ int journal_directory_vacuum(
 
                         if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8) {
                                 n_active_files++;
+                                sum += size;
                                 continue;
                         }
 
@@ -195,6 +198,7 @@ int journal_directory_vacuum(
                             de->d_name[q-8-16-1-16-1] != '-' ||
                             de->d_name[q-8-16-1-16-1-32-1] != '@') {
                                 n_active_files++;
+                                sum += size;
                                 continue;
                         }
 
@@ -207,11 +211,13 @@ int journal_directory_vacuum(
                         de->d_name[q-8-16-1-16-1] = 0;
                         if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
                                 n_active_files++;
+                                sum += size;
                                 continue;
                         }
 
                         if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
                                 n_active_files++;
+                                sum += size;
                                 continue;
                         }
 
@@ -224,12 +230,14 @@ int journal_directory_vacuum(
 
                         if (q < 1 + 16 + 1 + 16 + 8 + 1) {
                                 n_active_files++;
+                                sum += size;
                                 continue;
                         }
 
                         if (de->d_name[q-1-8-16-1] != '-' ||
                             de->d_name[q-1-8-16-1-16-1] != '@') {
                                 n_active_files++;
+                                sum += size;
                                 continue;
                         }
 
@@ -241,6 +249,7 @@ int journal_directory_vacuum(
 
                         if (sscanf(de->d_name + q-1-8-16-1-16, "%16llx-%16llx.journal~", &realtime, &tmp) != 2) {
                                 n_active_files++;
+                                sum += size;
                                 continue;
                         }
 
@@ -251,8 +260,6 @@ int journal_directory_vacuum(
                         continue;
                 }
 
-                size = 512UL * (uint64_t) st.st_blocks;
-
                 r = journal_file_empty(dirfd(d), p);
                 if (r < 0) {
                         log_debug_errno(r, "Failed check if %s is empty, ignoring: %m", p);
