summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-02-19 18:23:38 +0100
committerLennart Poettering <lennart@poettering.net>2018-02-20 15:39:31 +0100
commit11b29a96e98054ed69a2314ed71a286e852fa24e (patch)
tree625dc2f77e386ca4d1178e79fd2c0e8723cf4b08 /src/basic
parent3cc44114038621237a9d8ee4be3ff836138a55c1 (diff)
downloadsystemd-11b29a96e98054ed69a2314ed71a286e852fa24e.tar.gz
fs-util: move fsync_directory_of_file() into generic code
This function used by the journal code is pretty useful generically, let's move it to fs-util.c to make it useful for other code too.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/fs-util.c30
-rw-r--r--src/basic/fs-util.h2
2 files changed, 32 insertions, 0 deletions
diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c
index 47edcbb04f..85c8070a1b 100644
--- a/src/basic/fs-util.c
+++ b/src/basic/fs-util.c
@@ -967,3 +967,33 @@ int unlinkat_deallocate(int fd, const char *name, int flags) {
return 0;
}
+
+int fsync_directory_of_file(int fd) {
+ _cleanup_free_ char *path = NULL, *dn = NULL;
+ _cleanup_close_ int dfd = -1;
+ int r;
+
+ r = fd_verify_regular(fd);
+ if (r < 0)
+ return r;
+
+ r = fd_get_path(fd, &path);
+ if (r < 0)
+ return r;
+
+ if (!path_is_absolute(path))
+ return -EINVAL;
+
+ dn = dirname_malloc(path);
+ if (!dn)
+ return -ENOMEM;
+
+ dfd = open(dn, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
+ if (dfd < 0)
+ return -errno;
+
+ if (fsync(dfd) < 0)
+ return -errno;
+
+ return 0;
+}
diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h
index acb83dfd83..82d7e765b3 100644
--- a/src/basic/fs-util.h
+++ b/src/basic/fs-util.h
@@ -107,3 +107,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
int access_fd(int fd, int mode);
int unlinkat_deallocate(int fd, const char *name, int flags);
+
+int fsync_directory_of_file(int fd);