[PATCH v3 15/20] insert: fsync parent directory after mkdir

Subject: [PATCH v3 15/20] insert: fsync parent directory after mkdir

Date: Sun, 20 Jan 2013 11:49:59 +1100

To: notmuch@notmuchmail.org

Cc:

From: Peter Wang


After creating a subdirectory, fsync on its parent directory for
durability.
---
 notmuch-insert.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/notmuch-insert.c b/notmuch-insert.c
index c4d5e75..8012eb4 100644
--- a/notmuch-insert.c
+++ b/notmuch-insert.c
@@ -82,9 +82,21 @@ sync_dir (const char *dir)
 static notmuch_bool_t
 make_directory (char *path, int mode)
 {
+    notmuch_bool_t ret;
+    char *slash;
+
     if (mkdir (path, mode) != 0)
 	return (errno == EEXIST);
-    return TRUE;
+
+    /* Sync the parent directory for durability. */
+    ret = TRUE;
+    slash = strrchr (path, '/');
+    if (slash) {
+	*slash = '\0';
+	ret = sync_dir (path);
+	*slash = '/';
+    }
+    return ret;
 }
 
 /* Make the given directory including its parent directories as necessary.
-- 
1.7.12.1


Thread: