Atomically move the new message file from the Maildir 'tmp' directory to 'new'. --- notmuch-insert.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/notmuch-insert.c b/notmuch-insert.c index 38e815f..c0289d9 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -94,6 +94,24 @@ maildir_open_tmp_file (void *ctx, const char *dir, return fd; } +/* Atomically move the new message file from the Maildir 'tmp' directory + * to the 'new' directory. + * + * We follow the Dovecot recommendation to simply use rename() + * instead of link() and unlink(). See also: + * http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_delivery + */ +static notmuch_bool_t +maildir_move_tmp_to_new (const char *tmppath, const char *newpath) +{ + if (rename (tmppath, newpath) != 0) { + fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno)); + return FALSE; + } + + return TRUE; +} + /* Copy the contents of standard input (fdin) into fdout. */ static notmuch_bool_t copy_stdin (int fdin, int fdout) @@ -150,6 +168,9 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin, } ret = copy_stdin (fdin, fdout); close (fdout); + if (ret) { + ret = maildir_move_tmp_to_new (tmppath, newpath); + } if (!ret) { unlink (tmppath); } -- 1.7.12.1