]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-file: port journal_file_open() to openat_report_new() 22808/head
authorLennart Poettering <lennart@poettering.net>
Mon, 21 Mar 2022 17:13:06 +0000 (18:13 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Mar 2022 17:23:44 +0000 (18:23 +0100)
We so far had some magic logic in place that files we open for write
with size zero are freshly created. That of course is a bogus
assumption, in particular as this code deals with corrupted file systems
which oftentimes contain zero size inodes from left-over runs.

Let's fix this properly, and actually let the kernel tell us whether it
create the file or not.

src/libsystemd/sd-journal/journal-file.c

index e7014196c8dcdb768adf9be88b96b33a31a1e774..27875fb9e71cc4a1976f51b77e9d180afc276581 100644 (file)
@@ -3424,9 +3424,9 @@ int journal_file_open(
                  * or so, we likely fail quickly than block for long. For regular files O_NONBLOCK has no effect, hence
                  * it doesn't hurt in that case. */
 
-                f->fd = open(f->path, f->flags|O_CLOEXEC|O_NONBLOCK, f->mode);
+                f->fd = openat_report_new(AT_FDCWD, f->path, f->flags|O_CLOEXEC|O_NONBLOCK, f->mode, &newly_created);
                 if (f->fd < 0) {
-                        r = -errno;
+                        r = f->fd;
                         goto fail;
                 }
 
@@ -3436,6 +3436,19 @@ int journal_file_open(
                 r = fd_nonblock(f->fd, false);
                 if (r < 0)
                         goto fail;
+
+                if (!newly_created) {
+                        r = journal_file_fstat(f);
+                        if (r < 0)
+                                goto fail;
+                }
+        } else {
+                r = journal_file_fstat(f);
+                if (r < 0)
+                        goto fail;
+
+                /* If we just got the fd passed in, we don't really know if we created the file anew */
+                newly_created = f->last_stat.st_size == 0 && f->writable;
         }
 
         f->cache_fd = mmap_cache_add_fd(mmap_cache, f->fd, prot_from_flags(flags));
@@ -3444,12 +3457,7 @@ int journal_file_open(
                 goto fail;
         }
 
-        r = journal_file_fstat(f);
-        if (r < 0)
-                goto fail;
-
-        if (f->last_stat.st_size == 0 && f->writable) {
-
+        if (newly_created) {
                 (void) journal_file_warn_btrfs(f);
 
                 /* Let's attach the creation time to the journal file, so that the vacuuming code knows the age of this
@@ -3476,8 +3484,6 @@ int journal_file_open(
                 r = journal_file_fstat(f);
                 if (r < 0)
                         goto fail;
-
-                newly_created = true;
         }
 
         if (f->last_stat.st_size < (off_t) HEADER_SIZE_MIN) {