Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 87046431FAF for ; Sat, 24 Nov 2012 17:18:30 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id swS5-moHRvv5 for ; Sat, 24 Nov 2012 17:18:30 -0800 (PST) Received: from mail-pb0-f53.google.com (mail-pb0-f53.google.com [209.85.160.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id E4642431FAE for ; Sat, 24 Nov 2012 17:18:29 -0800 (PST) Received: by mail-pb0-f53.google.com with SMTP id jt11so7944513pbb.26 for ; Sat, 24 Nov 2012 17:18:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=PuYwcem/vc/qBiHL8StfxxN4P+B9e7JuX2ucBDMO9aQ=; b=oIHcDxe48tuwg1uxRPzWpa4JwzltJCICIS44hQL76wjVYUQV2GYte88YsDbYqOOQZN EbF4E90zigi2uTkCY21skxxUdfpM5ilpzqk89Wfx0bW0/BVA/IEVIccK1eWtjb7OqNzv e7kRLrLENmB5rNim4C55lTMPZO16G51LI4rq1byylXiIyeuZYaFjE6x6jp6vHpGY/9k9 P1NkkjKhX2IH/5uPK8qPN9+Wcvazb3aeemIEA9/gOdytfDLMi5K6UGOvwt/YNZDTyBfd b62//U6i1NuB33o4cQYmogUol7x4lJkm1szIrMhH3r7xqibghVyu6Ise/+KXt9NZfnxD Ehpw== Received: by 10.66.77.39 with SMTP id p7mr22254729paw.8.1353806309673; Sat, 24 Nov 2012 17:18:29 -0800 (PST) Received: from localhost (215.42.233.220.static.exetel.com.au. [220.233.42.215]) by mx.google.com with ESMTPS id t1sm6173552paw.11.2012.11.24.17.18.27 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 24 Nov 2012 17:18:28 -0800 (PST) From: Peter Wang To: notmuch@notmuchmail.org Subject: [PATCH v2 15/20] insert: fsync new directory after rename Date: Sun, 25 Nov 2012 12:16:41 +1100 Message-Id: <1353806206-29133-16-git-send-email-novalazy@gmail.com> X-Mailer: git-send-email 1.7.12.1 In-Reply-To: <1353806206-29133-1-git-send-email-novalazy@gmail.com> References: <1353806206-29133-1-git-send-email-novalazy@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Nov 2012 01:18:30 -0000 After moving the file from the 'tmp' to the 'new' directory, fsync on the 'new' directory for durability. --- notmuch-insert.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index f09c579..831b322 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -143,10 +143,10 @@ 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 into the 'tmp' and 'new' directories are returned - * via tmppath and newpath. */ + * via tmppath and newpath, and the path of the 'new' directory 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]; @@ -183,8 +183,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); @@ -204,14 +205,31 @@ 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) { + notmuch_bool_t ret; + int fd; + 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. */ + ret = TRUE; + fd = open (newdir, 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; } /* Copy the contents of fdin into fdout. */ @@ -307,6 +325,12 @@ add_file_to_database (notmuch_database_t *notmuch, const char *path, notmuch_message_thaw (message); + /* notmuch_message_tags_to_maildir_flags may rename the message file + * once more, and does so without fsyncing the directory afterwards. + * rename() is atomic so after a crash the file should appear under + * the old or new name. notmuch new should be able to rename the file + * again if required, so another fsync is not required, I think. + */ notmuch_message_tags_to_maildir_flags (message); notmuch_message_destroy (message); @@ -321,10 +345,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; } @@ -335,7 +360,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