[ovs-dev] [PATCH 12/14] ovsdb: Factor out code to fsync a file's containing directory.

Ben Pfaff blp at nicira.com
Fri Feb 12 11:37:32 PST 2010


In an upcoming commit, another function wants to do the same thing, so
break it out into a helper function.
---
 lib/socket-util.c |   31 +++++++++++++++++++++++++++++++
 lib/socket-util.h |    2 ++
 ovsdb/log.c       |   13 ++-----------
 3 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/lib/socket-util.c b/lib/socket-util.c
index a8f805c..912c47e 100644
--- a/lib/socket-util.c
+++ b/lib/socket-util.c
@@ -557,3 +557,34 @@ write_fully(int fd, const void *p_, size_t size, size_t *bytes_written)
     }
     return 0;
 }
+
+/* Given file name 'file_name', fsyncs the directory in which it is contained.
+ * Returns 0 if successful, otherwise a positive errno value. */
+int
+fsync_parent_dir(const char *file_name)
+{
+    int error = 0;
+    char *dir;
+    int fd;
+
+    dir = dir_name(file_name);
+    fd = open(dir, O_RDONLY);
+    if (fd >= 0) {
+        if (fsync(fd)) {
+            if (errno == EINVAL || errno == EROFS) {
+                /* This directory does not support synchronization.  Not
+                 * really an error. */
+            } else {
+                error = errno;
+                VLOG_ERR("%s: fsync failed (%s)", dir, strerror(error));
+            }
+        }
+        close(fd);
+    } else {
+        error = errno;
+        VLOG_ERR("%s: open failed (%s)", dir, strerror(error));
+    }
+    free(dir);
+
+    return error;
+}
diff --git a/lib/socket-util.h b/lib/socket-util.h
index 11f21c2..e489926 100644
--- a/lib/socket-util.h
+++ b/lib/socket-util.h
@@ -42,4 +42,6 @@ int inet_open_passive(int style, const char *target, int default_port,
 int read_fully(int fd, void *, size_t, size_t *bytes_read);
 int write_fully(int fd, const void *, size_t, size_t *bytes_written);
 
+int fsync_parent_dir(const char *file_name);
+
 #endif /* socket-util.h */
diff --git a/ovsdb/log.c b/ovsdb/log.c
index 6d07aca..9c2767c 100644
--- a/ovsdb/log.c
+++ b/ovsdb/log.c
@@ -29,6 +29,7 @@
 #include "ovsdb.h"
 #include "ovsdb-error.h"
 #include "sha1.h"
+#include "socket-util.h"
 #include "transaction.h"
 #include "util.h"
 
@@ -106,17 +107,7 @@ ovsdb_log_open(const char *name, enum ovsdb_log_open_mode open_mode,
     if (!fstat(fd, &s) && s.st_size == 0) {
         /* It's (probably) a new file so fsync() its parent directory to ensure
          * that its directory entry is committed to disk. */
-        char *dir = dir_name(name);
-        int dirfd = open(dir, O_RDONLY);
-        if (dirfd >= 0) {
-            if (fsync(dirfd) && errno != EINVAL) {
-                VLOG_ERR("%s: fsync failed (%s)", dir, strerror(errno));
-            }
-            close(dirfd);
-        } else {
-            VLOG_ERR("%s: open failed (%s)", dir, strerror(errno));
-        }
-        free(dir);
+        fsync_parent_dir(name);
     }
 
     stream = fdopen(fd, open_mode == OVSDB_LOG_READ_ONLY ? "rb" : "w+b");
-- 
1.6.6.1





More information about the dev mailing list