]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: add API for fsync()ing parent dir of path
authorLennart Poettering <lennart@poettering.net>
Mon, 1 Feb 2021 16:12:12 +0000 (17:12 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 8 Jul 2021 08:22:34 +0000 (10:22 +0200)
src/basic/fs-util.c
src/basic/fs-util.h

index 127f709cd9caf65079a87a818ff34c4da04eabe8..62c7954c843617ab05e7224adb9a58e5000fdb65 100644 (file)
@@ -1484,6 +1484,30 @@ int fsync_path_at(int at_fd, const char *path) {
         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 syncfs_path(int atfd, const char *path) {
         _cleanup_close_ int fd = -1;
 
index 7f15b558ca8b587095718726c611e16ac8cad685..c4ab98dbb4ec2545a1093f587baf5d842c75108e 100644 (file)
@@ -140,6 +140,7 @@ 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 syncfs_path(int atfd, const char *path);