]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: split out sync() family of calls from fs-util.[ch] into new c/h file
authorLennart Poettering <lennart@poettering.net>
Tue, 5 Oct 2021 12:03:26 +0000 (14:03 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 5 Oct 2021 13:47:32 +0000 (15:47 +0200)
No changes in code, just some splitting out.

22 files changed:
src/basic/fileio.c
src/basic/fs-util.c
src/basic/fs-util.h
src/basic/meson.build
src/basic/sync-util.c [new file with mode: 0644]
src/basic/sync-util.h [new file with mode: 0644]
src/boot/bless-boot.c
src/boot/bootctl.c
src/coredump/coredump.c
src/home/homed-manager.c
src/home/homework-luks.c
src/import/pull-job.c
src/libsystemd/sd-id128/id128-util.c
src/libsystemd/sd-journal/journal-file.c
src/partition/repart.c
src/random-seed/random-seed.c
src/shared/copy.c
src/shared/install-file.c
src/shared/machine-id-setup.c
src/shutdown/umount.c
src/sysusers/sysusers.c
src/test/test-fs-util.c

index 601e1a61e9d775baabae61b2237ed1c6df475d2d..a268a41addf2764ffa51d6706300b8a24a9a9074 100644 (file)
@@ -25,6 +25,7 @@
 #include "socket-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
+#include "sync-util.h"
 #include "tmpfile-util.h"
 
 /* The maximum size of the file we'll read in one go in read_full_file() (64M). */
@@ -1195,10 +1196,7 @@ int fflush_sync_and_check(FILE *f) {
         if (fd < 0)
                 return 0;
 
-        if (fsync(fd) < 0)
-                return -errno;
-
-        r = fsync_directory_of_file(fd);
+        r = fsync_full(fd);
         if (r < 0)
                 return r;
 
index 6c3ef972fef6ac0f9bbe2e2ce24cd167e99fa375..2412df197a4331267bd591fd037b6d4d9dffcd0a 100644 (file)
@@ -1349,172 +1349,6 @@ int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags) {
         return 0;
 }
 
-int fsync_directory_of_file(int fd) {
-        _cleanup_close_ int dfd = -1;
-        struct stat st;
-        int r;
-
-        assert(fd >= 0);
-
-        /* We only reasonably can do this for regular files and directories, or for O_PATH fds, hence check
-         * for the inode type first */
-        if (fstat(fd, &st) < 0)
-                return -errno;
-
-        if (S_ISDIR(st.st_mode)) {
-                dfd = openat(fd, "..", O_RDONLY|O_DIRECTORY|O_CLOEXEC, 0);
-                if (dfd < 0)
-                        return -errno;
-
-        } else if (!S_ISREG(st.st_mode)) { /* Regular files are OK regardless if O_PATH or not, for all other
-                                            * types check O_PATH flag */
-                int flags;
-
-                flags = fcntl(fd, F_GETFL);
-                if (flags < 0)
-                        return -errno;
-
-                if (!FLAGS_SET(flags, O_PATH)) /* If O_PATH this refers to the inode in the fs, in which case
-                                                * we can sensibly do what is requested. Otherwise this refers
-                                                * to a socket, fifo or device node, where the concept of a
-                                                * containing directory doesn't make too much sense. */
-                        return -ENOTTY;
-        }
-
-        if (dfd < 0) {
-                _cleanup_free_ char *path = NULL;
-
-                r = fd_get_path(fd, &path);
-                if (r < 0) {
-                        log_debug_errno(r, "Failed to query /proc/self/fd/%d%s: %m",
-                                        fd,
-                                        r == -ENOSYS ? ", ignoring" : "");
-
-                        if (r == -ENOSYS)
-                                /* If /proc is not available, we're most likely running in some
-                                 * chroot environment, and syncing the directory is not very
-                                 * important in that case. Let's just silently do nothing. */
-                                return 0;
-
-                        return r;
-                }
-
-                if (!path_is_absolute(path))
-                        return -EINVAL;
-
-                dfd = open_parent(path, O_CLOEXEC|O_NOFOLLOW, 0);
-                if (dfd < 0)
-                        return dfd;
-        }
-
-        if (fsync(dfd) < 0)
-                return -errno;
-
-        return 0;
-}
-
-int fsync_full(int fd) {
-        int r, q;
-
-        /* Sync both the file and the directory */
-
-        r = fsync(fd) < 0 ? -errno : 0;
-
-        q = fsync_directory_of_file(fd);
-        if (r < 0) /* Return earlier error */
-                return r;
-        if (q == -ENOTTY) /* Ignore if the 'fd' refers to a block device or so which doesn't really have a
-                           * parent dir */
-                return 0;
-        return q;
-}
-
-int fsync_path_at(int at_fd, const char *path) {
-        _cleanup_close_ int opened_fd = -1;
-        int fd;
-
-        if (isempty(path)) {
-                if (at_fd == AT_FDCWD) {
-                        opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
-                        if (opened_fd < 0)
-                                return -errno;
-
-                        fd = opened_fd;
-                } else
-                        fd = at_fd;
-        } else {
-                opened_fd = openat(at_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
-                if (opened_fd < 0)
-                        return -errno;
-
-                fd = opened_fd;
-        }
-
-        if (fsync(fd) < 0)
-                return -errno;
-
-        return 0;
-}
-
-int fsync_parent_at(int at_fd, const char *path) {
-        _cleanup_close_ int opened_fd = -1;
-
-        if (isempty(path)) {
-                if (at_fd != AT_FDCWD)
-                        return fsync_directory_of_file(at_fd);
-
-                opened_fd = open("..", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
-                if (opened_fd < 0)
-                        return -errno;
-
-                if (fsync(opened_fd) < 0)
-                        return -errno;
-
-                return 0;
-        }
-
-        opened_fd = openat(at_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
-        if (opened_fd < 0)
-                return -errno;
-
-        return fsync_directory_of_file(opened_fd);
-}
-
-int fsync_path_and_parent_at(int at_fd, const char *path) {
-        _cleanup_close_ int opened_fd = -1;
-
-        if (isempty(path)) {
-                if (at_fd != AT_FDCWD)
-                        return fsync_full(at_fd);
-
-                opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
-        } else
-                opened_fd = openat(at_fd, path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC);
-        if (opened_fd < 0)
-                return -errno;
-
-        return fsync_full(opened_fd);
-}
-
-int syncfs_path(int atfd, const char *path) {
-        _cleanup_close_ int fd = -1;
-
-        if (isempty(path)) {
-                if (atfd != AT_FDCWD)
-                        return syncfs(atfd) < 0 ? -errno : 0;
-
-                fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
-        } else
-                fd = openat(atfd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
-        if (fd < 0)
-                return -errno;
-
-        if (syncfs(fd) < 0)
-                return -errno;
-
-        return 0;
-}
-
 int open_parent(const char *path, int flags, mode_t mode) {
         _cleanup_free_ char *parent = NULL;
         int fd, r;
index d612cbe404a03419fea7e3c0c105c1e0b1855c1b..7120ecd99db7656ad78ec4375da54a9a3d1fe8e9 100644 (file)
@@ -137,14 +137,6 @@ typedef enum UnlinkDeallocateFlags {
 
 int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags);
 
-int fsync_directory_of_file(int fd);
-int fsync_full(int fd);
-int fsync_path_at(int at_fd, const char *path);
-int fsync_parent_at(int at_fd, const char *path);
-int fsync_path_and_parent_at(int at_fd, const char *path);
-
-int syncfs_path(int atfd, const char *path);
-
 int open_parent(const char *path, int flags, mode_t mode);
 
 int conservative_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
index 82f42df9292955cda63bff6489542810fc2f4d5f..7b42ac428c1bea6eccf1718721766367a62d9b73 100644 (file)
@@ -215,6 +215,8 @@ basic_sources = files('''
         strv.h
         strxcpyx.c
         strxcpyx.h
+        sync-util.c
+        sync-util.h
         sysctl-util.c
         sysctl-util.h
         syslog-util.c
diff --git a/src/basic/sync-util.c b/src/basic/sync-util.c
new file mode 100644 (file)
index 0000000..479ff73
--- /dev/null
@@ -0,0 +1,175 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <fcntl.h>
+#include <sys/stat.h>
+
+#include "fd-util.h"
+#include "fs-util.h"
+#include "path-util.h"
+#include "sync-util.h"
+
+int fsync_directory_of_file(int fd) {
+        _cleanup_close_ int dfd = -1;
+        struct stat st;
+        int r;
+
+        assert(fd >= 0);
+
+        /* We only reasonably can do this for regular files and directories, or for O_PATH fds, hence check
+         * for the inode type first */
+        if (fstat(fd, &st) < 0)
+                return -errno;
+
+        if (S_ISDIR(st.st_mode)) {
+                dfd = openat(fd, "..", O_RDONLY|O_DIRECTORY|O_CLOEXEC, 0);
+                if (dfd < 0)
+                        return -errno;
+
+        } else if (!S_ISREG(st.st_mode)) { /* Regular files are OK regardless if O_PATH or not, for all other
+                                            * types check O_PATH flag */
+                int flags;
+
+                flags = fcntl(fd, F_GETFL);
+                if (flags < 0)
+                        return -errno;
+
+                if (!FLAGS_SET(flags, O_PATH)) /* If O_PATH this refers to the inode in the fs, in which case
+                                                * we can sensibly do what is requested. Otherwise this refers
+                                                * to a socket, fifo or device node, where the concept of a
+                                                * containing directory doesn't make too much sense. */
+                        return -ENOTTY;
+        }
+
+        if (dfd < 0) {
+                _cleanup_free_ char *path = NULL;
+
+                r = fd_get_path(fd, &path);
+                if (r < 0) {
+                        log_debug_errno(r, "Failed to query /proc/self/fd/%d%s: %m",
+                                        fd,
+                                        r == -ENOSYS ? ", ignoring" : "");
+
+                        if (r == -ENOSYS)
+                                /* If /proc is not available, we're most likely running in some
+                                 * chroot environment, and syncing the directory is not very
+                                 * important in that case. Let's just silently do nothing. */
+                                return 0;
+
+                        return r;
+                }
+
+                if (!path_is_absolute(path))
+                        return -EINVAL;
+
+                dfd = open_parent(path, O_CLOEXEC|O_NOFOLLOW, 0);
+                if (dfd < 0)
+                        return dfd;
+        }
+
+        if (fsync(dfd) < 0)
+                return -errno;
+
+        return 0;
+}
+
+int fsync_full(int fd) {
+        int r, q;
+
+        /* Sync both the file and the directory */
+
+        r = fsync(fd) < 0 ? -errno : 0;
+
+        q = fsync_directory_of_file(fd);
+        if (r < 0) /* Return earlier error */
+                return r;
+        if (q == -ENOTTY) /* Ignore if the 'fd' refers to a block device or so which doesn't really have a
+                           * parent dir */
+                return 0;
+        return q;
+}
+
+int fsync_path_at(int at_fd, const char *path) {
+        _cleanup_close_ int opened_fd = -1;
+        int fd;
+
+        if (isempty(path)) {
+                if (at_fd == AT_FDCWD) {
+                        opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
+                        if (opened_fd < 0)
+                                return -errno;
+
+                        fd = opened_fd;
+                } else
+                        fd = at_fd;
+        } else {
+                opened_fd = openat(at_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
+                if (opened_fd < 0)
+                        return -errno;
+
+                fd = opened_fd;
+        }
+
+        if (fsync(fd) < 0)
+                return -errno;
+
+        return 0;
+}
+
+int fsync_parent_at(int at_fd, const char *path) {
+        _cleanup_close_ int opened_fd = -1;
+
+        if (isempty(path)) {
+                if (at_fd != AT_FDCWD)
+                        return fsync_directory_of_file(at_fd);
+
+                opened_fd = open("..", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
+                if (opened_fd < 0)
+                        return -errno;
+
+                if (fsync(opened_fd) < 0)
+                        return -errno;
+
+                return 0;
+        }
+
+        opened_fd = openat(at_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
+        if (opened_fd < 0)
+                return -errno;
+
+        return fsync_directory_of_file(opened_fd);
+}
+
+int fsync_path_and_parent_at(int at_fd, const char *path) {
+        _cleanup_close_ int opened_fd = -1;
+
+        if (isempty(path)) {
+                if (at_fd != AT_FDCWD)
+                        return fsync_full(at_fd);
+
+                opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
+        } else
+                opened_fd = openat(at_fd, path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC);
+        if (opened_fd < 0)
+                return -errno;
+
+        return fsync_full(opened_fd);
+}
+
+int syncfs_path(int at_fd, const char *path) {
+        _cleanup_close_ int fd = -1;
+
+        if (isempty(path)) {
+                if (at_fd != AT_FDCWD)
+                        return syncfs(at_fd) < 0 ? -errno : 0;
+
+                fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
+        } else
+                fd = openat(at_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
+        if (fd < 0)
+                return -errno;
+
+        if (syncfs(fd) < 0)
+                return -errno;
+
+        return 0;
+}
diff --git a/src/basic/sync-util.h b/src/basic/sync-util.h
new file mode 100644 (file)
index 0000000..e449440
--- /dev/null
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int fsync_directory_of_file(int fd);
+int fsync_full(int fd);
+
+int fsync_path_at(int at_fd, const char *path);
+int fsync_parent_at(int at_fd, const char *path);
+int fsync_path_and_parent_at(int at_fd, const char *path);
+
+int syncfs_path(int at_fd, const char *path);
index 7d5a52841ee107190ba4fd47d8d7fd98f0fd55bb..cb0688672783fafd658d3174cfae8abf6d9bf61d 100644 (file)
@@ -14,6 +14,7 @@
 #include "parse-util.h"
 #include "path-util.h"
 #include "pretty-print.h"
+#include "sync-util.h"
 #include "terminal-util.h"
 #include "util.h"
 #include "verbs.h"
index 27b0086406c1a4bd1654367accbcc007760d746e..91b36e91ba3af7e70f68079ceb9155bc17b304b5 100644 (file)
@@ -38,6 +38,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "terminal-util.h"
 #include "tmpfile-util.h"
 #include "umask-util.h"
@@ -569,13 +570,12 @@ static int copy_file_with_version_check(const char *from, const char *to, bool f
 
         (void) copy_times(fd_from, fd_to, 0);
 
-        if (fsync(fd_to) < 0) {
+        r = fsync_full(fd_to);
+        if (r < 0) {
                 (void) unlink_noerrno(t);
-                return log_error_errno(errno, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
+                return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
         }
 
-        (void) fsync_directory_of_file(fd_to);
-
         if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0) {
                 (void) unlink_noerrno(t);
                 return log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", t, to);
index 444b9ec374ffa1fed46fae51d5f71962ac848b58..d7eab3d36c7499b66138d9c8f12295f4439fdf72 100644 (file)
@@ -48,6 +48,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "tmpfile-util.h"
 #include "user-record.h"
 #include "user-util.h"
@@ -261,10 +262,9 @@ static int fix_permissions(
         (void) fix_acl(fd, uid);
         (void) fix_xattr(fd, context);
 
-        if (fsync(fd) < 0)
-                return log_error_errno(errno, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename));
-
-        (void) fsync_directory_of_file(fd);
+        r = fsync_full(fd);
+        if (r < 0)
+                return log_error_errno(r, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename));
 
         r = link_tmpfile(fd, filename, target);
         if (r < 0)
index 6549e6bd4ef24b46988729e74a34927b6b3ee38d..070fd97d69bfbfa1d093ed61b4e88531598057c3 100644 (file)
@@ -38,6 +38,7 @@
 #include "socket-util.h"
 #include "stat-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "tmpfile-util.h"
 #include "udev-util.h"
 #include "user-record-sign.h"
index 2321a31021205e7ed0c1a15a9dec9adcdae42b5b..8af9223d57ab680135b65383f24c6c5668599f89 100644 (file)
@@ -36,6 +36,7 @@
 #include "resize-fs.h"
 #include "stat-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "tmpfile-util.h"
 
 /* Round down to the nearest 4K size. Given that newer hardware generally prefers 4K sectors, let's align our
index 4e37dce33f5b764e9fab6ba0f6ea024d66109d63..34b116a8f87946e4f1e3aafe7387a6cc801e4793 100644 (file)
@@ -7,7 +7,6 @@
 #include "alloc-util.h"
 #include "fd-util.h"
 #include "format-util.h"
-#include "fs-util.h"
 #include "gcrypt-util.h"
 #include "hexdecoct.h"
 #include "import-util.h"
@@ -18,6 +17,7 @@
 #include "pull-job.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "xattr-util.h"
 
 void pull_job_close_disk_fd(PullJob *j) {
index 2074771a41bfdb209cc4ecbde36815a10367e1ea..19937110b05e1e6bdd3af0605bc284936d5b1d1f 100644 (file)
@@ -5,12 +5,12 @@
 #include <unistd.h>
 
 #include "fd-util.h"
-#include "fs-util.h"
 #include "hexdecoct.h"
 #include "id128-util.h"
 #include "io-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
+#include "sync-util.h"
 
 char *id128_to_uuid_string(sd_id128_t id, char s[static ID128_UUID_STRING_MAX]) {
         unsigned n, k = 0;
@@ -167,10 +167,7 @@ int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync) {
                 return r;
 
         if (do_sync) {
-                if (fsync(fd) < 0)
-                        return -errno;
-
-                r = fsync_directory_of_file(fd);
+                r = fsync_full(fd);
                 if (r < 0)
                         return r;
         }
index 9fecc84495b2eadf006e23762a3c1038bbd33948..d5ceba2f8fb1f361d08fbc059f80081130553784 100644 (file)
@@ -32,6 +32,7 @@
 #include "stat-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "xattr-util.h"
 
 #define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*sizeof(HashItem))
@@ -474,11 +475,9 @@ static int journal_file_refresh_header(JournalFile *f) {
 
         r = journal_file_set_online(f);
 
-        /* Sync the online state to disk */
-        (void) fsync(f->fd);
-
-        /* We likely just created a new file, also sync the directory this file is located in. */
-        (void) fsync_directory_of_file(f->fd);
+        /* Sync the online state to disk; likely just created a new file, also sync the directory this file
+         * is located in. */
+        (void) fsync_full(f->fd);
 
         return r;
 }
index 564ee4835bc7fffac91d8e5b2e79e5c5db0b54d1..3203b48d8c2ef301f625e8fcc60c1ac49397bf83 100644 (file)
@@ -61,6 +61,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "terminal-util.h"
 #include "tpm2-util.h"
 #include "user-util.h"
index e003ca60e3c39281a25a278254bc38569fa21df9..7724e0365e76c591c04bfc412388f2c3e6a86ba2 100644 (file)
@@ -25,6 +25,7 @@
 #include "parse-util.h"
 #include "random-util.h"
 #include "string-util.h"
+#include "sync-util.h"
 #include "util.h"
 #include "xattr-util.h"
 
index f27ce252ee4e5ef0bda547f1772b32e081c3df68..9053fce33378456945b905b962ca695156c4c45c 100644 (file)
@@ -29,6 +29,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "time-util.h"
 #include "tmpfile-util.h"
 #include "umask-util.h"
index 664f028ad1e443ae10cd8ec049d834ba0d2956f9..5187d71beca0fe28bbef2779c720acc130cba9dc 100644 (file)
@@ -10,6 +10,7 @@
 #include "install-file.h"
 #include "missing_syscall.h"
 #include "rm-rf.h"
+#include "sync-util.h"
 
 int fs_make_very_read_only(int fd) {
         struct stat st;
index d2f989272ddd12563dd5f99ff7ecf592824841e1..e483675a75c2186e487da51eaca4483acee1ff1a 100644 (file)
@@ -9,7 +9,6 @@
 
 #include "alloc-util.h"
 #include "fd-util.h"
-#include "fs-util.h"
 #include "id128-util.h"
 #include "io-util.h"
 #include "log.h"
@@ -23,6 +22,7 @@
 #include "process-util.h"
 #include "stat-util.h"
 #include "string-util.h"
+#include "sync-util.h"
 #include "umask-util.h"
 #include "util.h"
 #include "virt.h"
index c2a26242c02115f53f1c034600e8e5afffc5c506..09fc339ab1d486620b0a124cce704ce0511faaca 100644 (file)
@@ -23,7 +23,6 @@
 #include "device-util.h"
 #include "escape.h"
 #include "fd-util.h"
-#include "fs-util.h"
 #include "fstab-util.h"
 #include "libmount-util.h"
 #include "mount-setup.h"
@@ -34,6 +33,7 @@
 #include "signal-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "umount.h"
 #include "util.h"
 #include "virt.h"
index 7daebdbe554feaa74280729797d39ccfdee9790a..c55f7f8b7eebc742b3f220696cebe00707572e09 100644 (file)
@@ -29,6 +29,7 @@
 #include "specifier.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "tmpfile-util-label.h"
 #include "uid-range.h"
 #include "user-record.h"
index ac7ee5d3bf75fcf67403429ce064ff2d4c213077..f0cac447c3cfa9eac2499a2af8879e62e930a021 100644 (file)
@@ -16,6 +16,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sync-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
 #include "umask-util.h"