After moving the file from the 'tmp' to the 'new' directory, fsync on the 'new' directory for durability. --- notmuch-insert.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index 60855c1..c4d5e75 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -56,6 +56,28 @@ check_folder_name (const char *folder) } } +/* Call fsync() on a directory path. */ +static notmuch_bool_t +sync_dir (const char *dir) +{ + notmuch_bool_t ret; + int fd; + + ret = TRUE; + fd = open (dir, O_RDONLY); + if (fd == -1) { + fprintf (stderr, "Error: open() dir failed: %s\n", strerror (errno)); + ret = FALSE; + } + if (ret && fsync (fd) != 0) { + fprintf (stderr, "Error: fsync() dir failed: %s\n", strerror (errno)); + ret = FALSE; + } + if (fd != -1) + close (fd); + return ret; +} + /* Make the given directory, succeeding if it already exists. */ static notmuch_bool_t make_directory (char *path, int mode) @@ -140,10 +162,11 @@ maildir_create_folder (void *ctx, const char *dir) /* Open a unique file in the Maildir 'tmp' directory. * Returns the file descriptor on success, or -1 on failure. * On success, file paths for the message in the 'tmp' and 'new' - * directories are returned via tmppath and newpath. */ + * directories are returned via tmppath and newpath, + * and the path of the 'new' directory itself in newdir. */ static int maildir_open_tmp_file (void *ctx, const char *dir, - char **tmppath, char **newpath) + char **tmppath, char **newpath, char **newdir) { pid_t pid; char hostname[256]; @@ -180,8 +203,9 @@ maildir_open_tmp_file (void *ctx, const char *dir, return -1; } + *newdir = talloc_asprintf (ctx, "%s/new", dir); *newpath = talloc_asprintf (ctx, "%s/new/%s", dir, filename); - if (! *newpath) { + if (! *newdir || ! *newpath) { fprintf (stderr, "Out of memory\n"); close (fd); unlink (*tmppath); @@ -201,14 +225,16 @@ maildir_open_tmp_file (void *ctx, const char *dir, * http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_delivery */ static notmuch_bool_t -maildir_move_tmp_to_new (const char *tmppath, const char *newpath) +maildir_move_tmp_to_new (const char *tmppath, const char *newpath, + const char *newdir) { if (rename (tmppath, newpath) != 0) { fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno)); return FALSE; } - return TRUE; + /* Sync the 'new' directory after rename for durability. */ + return sync_dir (newdir); } /* Copy the contents of standard input (fdin) into fdout. */ @@ -297,10 +323,11 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin, { char *tmppath; char *newpath; + char *newdir; int fdout; notmuch_bool_t ret; - fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath); + fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir); if (fdout < 0) { return FALSE; } @@ -311,7 +338,7 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin, } close (fdout); if (ret) { - ret = maildir_move_tmp_to_new (tmppath, newpath); + ret = maildir_move_tmp_to_new (tmppath, newpath, newdir); } if (!ret) { unlink (tmppath); -- 1.7.12.1