]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: move fsync_directory_of_file() into generic code
authorLennart Poettering <lennart@poettering.net>
Mon, 19 Feb 2018 17:23:38 +0000 (18:23 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 20 Feb 2018 14:39:31 +0000 (15:39 +0100)
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.

src/basic/fs-util.c
src/basic/fs-util.h
src/journal/journal-file.c
src/test/test-fs-util.c

index 47edcbb04f1556fd5619352095390c2fa8eef264..85c8070a1b2571c385f6abb4fcca7132caa4325e 100644 (file)
@@ -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;
+}
index acb83dfd83a030ab030643cc3484d6140cb3452e..82d7e765b3e1d68aa533b34ecd48abc3f23c3833 100644 (file)
@@ -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);
index 96e45e92f5c276cdd5a0c3d6d4a908f87cf2dfda..982012366037772bfa73af2465f751a04886274e 100644 (file)
@@ -33,6 +33,7 @@
 #include "chattr-util.h"
 #include "compress.h"
 #include "fd-util.h"
+#include "fs-util.h"
 #include "journal-authenticate.h"
 #include "journal-def.h"
 #include "journal-file.h"
@@ -454,39 +455,6 @@ static int journal_file_init_header(JournalFile *f, JournalFile *template) {
         return 0;
 }
 
-static int fsync_directory_of_file(int fd) {
-        _cleanup_free_ char *path = NULL, *dn = NULL;
-        _cleanup_close_ int dfd = -1;
-        struct stat st;
-        int r;
-
-        if (fstat(fd, &st) < 0)
-                return -errno;
-
-        if (!S_ISREG(st.st_mode))
-                return -EBADFD;
-
-        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;
-}
-
 static int journal_file_refresh_header(JournalFile *f) {
         sd_id128_t boot_id;
         int r;
index 184a2a52c2ca994a86ddd1969d56d6acffbb839b..ebcec4fcc5502f3fdb50bf698a65814b98a33fc1 100644 (file)
@@ -552,6 +552,15 @@ static void test_unlinkat_deallocate(void) {
         assert_se(st.st_nlink == 0);
 }
 
+static void test_fsync_directory_of_file(void) {
+        _cleanup_close_ int fd = -1;
+
+        fd = open_tmpfile_unlinkable(NULL, O_RDWR);
+        assert_se(fd >= 0);
+
+        assert_se(fsync_directory_of_file(fd) >= 0);
+}
+
 int main(int argc, char *argv[]) {
         test_unlink_noerrno();
         test_get_files_in_directory();
@@ -562,6 +571,7 @@ int main(int argc, char *argv[]) {
         test_access_fd();
         test_touch_file();
         test_unlinkat_deallocate();
+        test_fsync_directory_of_file();
 
         return 0;
 }