From: Lennart Poettering Date: Mon, 21 Mar 2022 17:13:06 +0000 (+0100) Subject: journal-file: port journal_file_open() to openat_report_new() X-Git-Tag: v251-rc1~82^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F22808%2Fhead;p=thirdparty%2Fsystemd.git journal-file: port journal_file_open() to openat_report_new() 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. --- diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index e7014196c8d..27875fb9e71 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -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) {