journal: Update the JournalFile path when archiving
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 1 Dec 2021 08:34:13 +0000 (09:34 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 6 Dec 2021 21:17:40 +0000 (22:17 +0100)
When we archive a path, we rename the file to indicate this. However,
until now, we didn't actually update the path member of the corresponding
JournalFile instance. Let's make sure we also update this to avoid
misuse of the old path later on.

This change also requires we save the previous path in journal_file_rotate()
since we need to open a new file at the previous path.

src/journal/journald-file.c
src/journal/journald-server.c
src/libsystemd/sd-journal/journal-file.c
src/libsystemd/sd-journal/journal-file.h

index 5dba7915170d0eac763cd1ecdc71a8b1a111a521..fc46aa1963b9875769801cd803b5826d39b31100 100644 (file)
@@ -382,19 +382,20 @@ int journald_file_rotate(
                 bool seal,
                 Set *deferred_closes) {
 
+        _cleanup_free_ char *path = NULL;
         JournaldFile *new_file = NULL;
         int r;
 
         assert(f);
         assert(*f);
 
-        r = journal_file_archive((*f)->file);
+        r = journal_file_archive((*f)->file, &path);
         if (r < 0)
                 return r;
 
         r = journald_file_open(
                         -1,
-                        (*f)->file->path,
+                        path,
                         (*f)->file->flags,
                         (*f)->file->mode,
                         compress,
index 5e97cd41fd9e47305466c6bc9ca56948357f4dc4..a13bbeeee9fc012137ff98b82879801ce751c180 100644 (file)
@@ -602,7 +602,7 @@ static int vacuum_offline_user_journals(Server *s) {
 
                 TAKE_FD(fd); /* Donated to journald_file_open() */
 
-                r = journal_file_archive(f->file);
+                r = journal_file_archive(f->file, NULL);
                 if (r < 0)
                         log_debug_errno(r, "Failed to archive journal file '%s', ignoring: %m", full);
 
index 9755bf14b94274d5d8d86e76fea35e1c28a02eae..bc80a51524d8f0d4a769002094a86c345e866349 100644 (file)
@@ -3521,7 +3521,7 @@ fail:
         return r;
 }
 
-int journal_file_archive(JournalFile *f) {
+int journal_file_archive(JournalFile *f, char **ret_previous_path) {
         _cleanup_free_ char *p = NULL;
 
         assert(f);
@@ -3552,6 +3552,13 @@ int journal_file_archive(JournalFile *f) {
         /* Sync the rename to disk */
         (void) fsync_directory_of_file(f->fd);
 
+        if (ret_previous_path)
+                *ret_previous_path = f->path;
+        else
+                free(f->path);
+
+        f->path = TAKE_PTR(p);
+
         /* Set as archive so offlining commits w/state=STATE_ARCHIVED. Previously we would set old_file->header->state
          * to STATE_ARCHIVED directly here, but journal_file_set_offline() short-circuits when state != STATE_ONLINE,
          * which would result in the rotated journal never getting fsync() called before closing.  Now we simply queue
index 6916cc4ac3ae4b4d297ba0089110d4cf097b28b6..5bcf591337d23a3379e8f6205792bf92fc8d62cf 100644 (file)
@@ -232,7 +232,7 @@ int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint6
 void journal_file_dump(JournalFile *f);
 void journal_file_print_header(JournalFile *f);
 
-int journal_file_archive(JournalFile *f);
+int journal_file_archive(JournalFile *f, char **ret_previous_path);
 
 int journal_file_dispose(int dir_fd, const char *fname);