1 Return-Path: <novalazy@gmail.com>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 87046431FAF
\r
6 for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:30 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5
\r
12 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
\r
13 FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id swS5-moHRvv5 for <notmuch@notmuchmail.org>;
\r
17 Sat, 24 Nov 2012 17:18:30 -0800 (PST)
\r
18 Received: from mail-pb0-f53.google.com (mail-pb0-f53.google.com
\r
19 [209.85.160.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id E4642431FAE
\r
22 for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:29 -0800 (PST)
\r
23 Received: by mail-pb0-f53.google.com with SMTP id jt11so7944513pbb.26
\r
24 for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:29 -0800 (PST)
\r
25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
\r
26 h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;
\r
27 bh=PuYwcem/vc/qBiHL8StfxxN4P+B9e7JuX2ucBDMO9aQ=;
\r
28 b=oIHcDxe48tuwg1uxRPzWpa4JwzltJCICIS44hQL76wjVYUQV2GYte88YsDbYqOOQZN
\r
29 EbF4E90zigi2uTkCY21skxxUdfpM5ilpzqk89Wfx0bW0/BVA/IEVIccK1eWtjb7OqNzv
\r
30 e7kRLrLENmB5rNim4C55lTMPZO16G51LI4rq1byylXiIyeuZYaFjE6x6jp6vHpGY/9k9
\r
31 P1NkkjKhX2IH/5uPK8qPN9+Wcvazb3aeemIEA9/gOdytfDLMi5K6UGOvwt/YNZDTyBfd
\r
32 b62//U6i1NuB33o4cQYmogUol7x4lJkm1szIrMhH3r7xqibghVyu6Ise/+KXt9NZfnxD
\r
34 Received: by 10.66.77.39 with SMTP id p7mr22254729paw.8.1353806309673;
\r
35 Sat, 24 Nov 2012 17:18:29 -0800 (PST)
\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.
\r
38 by mx.google.com with ESMTPS id t1sm6173552paw.11.2012.11.24.17.18.27
\r
39 (version=TLSv1/SSLv3 cipher=OTHER);
\r
40 Sat, 24 Nov 2012 17:18:28 -0800 (PST)
\r
41 From: Peter Wang <novalazy@gmail.com>
\r
42 To: notmuch@notmuchmail.org
\r
43 Subject: [PATCH v2 15/20] insert: fsync new directory after rename
\r
44 Date: Sun, 25 Nov 2012 12:16:41 +1100
\r
45 Message-Id: <1353806206-29133-16-git-send-email-novalazy@gmail.com>
\r
46 X-Mailer: git-send-email 1.7.12.1
\r
47 In-Reply-To: <1353806206-29133-1-git-send-email-novalazy@gmail.com>
\r
48 References: <1353806206-29133-1-git-send-email-novalazy@gmail.com>
\r
49 X-BeenThere: notmuch@notmuchmail.org
\r
50 X-Mailman-Version: 2.1.13
\r
52 List-Id: "Use and development of the notmuch mail system."
\r
53 <notmuch.notmuchmail.org>
\r
54 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
55 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
56 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
57 List-Post: <mailto:notmuch@notmuchmail.org>
\r
58 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
59 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
60 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
61 X-List-Received-Date: Sun, 25 Nov 2012 01:18:30 -0000
\r
63 After moving the file from the 'tmp' to the 'new' directory,
\r
64 fsync on the 'new' directory for durability.
\r
66 notmuch-insert.c | 39 ++++++++++++++++++++++++++++++++-------
\r
67 1 file changed, 32 insertions(+), 7 deletions(-)
\r
69 diff --git a/notmuch-insert.c b/notmuch-insert.c
\r
70 index f09c579..831b322 100644
\r
71 --- a/notmuch-insert.c
\r
72 +++ b/notmuch-insert.c
\r
73 @@ -143,10 +143,10 @@ maildir_create_folder (void *ctx, const char *dir)
\r
74 /* Open a unique file in the Maildir 'tmp' directory.
\r
75 * Returns the file descriptor on success, or -1 on failure.
\r
76 * On success, file paths into the 'tmp' and 'new' directories are returned
\r
77 - * via tmppath and newpath. */
\r
78 + * via tmppath and newpath, and the path of the 'new' directory in newdir. */
\r
80 maildir_open_tmp_file (void *ctx, const char *dir,
\r
81 - char **tmppath, char **newpath)
\r
82 + char **tmppath, char **newpath, char **newdir)
\r
86 @@ -183,8 +183,9 @@ maildir_open_tmp_file (void *ctx, const char *dir,
\r
90 + *newdir = talloc_asprintf (ctx, "%s/new", dir);
\r
91 *newpath = talloc_asprintf (ctx, "%s/new/%s", dir, filename);
\r
93 + if (! *newdir || ! *newpath) {
\r
94 fprintf (stderr, "Out of memory\n");
\r
97 @@ -204,14 +205,31 @@ maildir_open_tmp_file (void *ctx, const char *dir,
\r
98 * http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_delivery
\r
100 static notmuch_bool_t
\r
101 -maildir_move_tmp_to_new (const char *tmppath, const char *newpath)
\r
102 +maildir_move_tmp_to_new (const char *tmppath, const char *newpath,
\r
103 + const char *newdir)
\r
105 + notmuch_bool_t ret;
\r
108 if (rename (tmppath, newpath) != 0) {
\r
109 fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));
\r
114 + /* Sync the 'new' directory after rename for durability. */
\r
116 + fd = open (newdir, O_RDONLY);
\r
118 + fprintf (stderr, "Error: open() dir failed: %s\n", strerror (errno));
\r
121 + if (ret && fsync (fd) != 0) {
\r
122 + fprintf (stderr, "Error: fsync() dir failed: %s\n", strerror (errno));
\r
130 /* Copy the contents of fdin into fdout. */
\r
131 @@ -307,6 +325,12 @@ add_file_to_database (notmuch_database_t *notmuch, const char *path,
\r
133 notmuch_message_thaw (message);
\r
135 + /* notmuch_message_tags_to_maildir_flags may rename the message file
\r
136 + * once more, and does so without fsyncing the directory afterwards.
\r
137 + * rename() is atomic so after a crash the file should appear under
\r
138 + * the old or new name. notmuch new should be able to rename the file
\r
139 + * again if required, so another fsync is not required, I think.
\r
141 notmuch_message_tags_to_maildir_flags (message);
\r
143 notmuch_message_destroy (message);
\r
144 @@ -321,10 +345,11 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,
\r
150 notmuch_bool_t ret;
\r
152 - fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath);
\r
153 + fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir);
\r
157 @@ -335,7 +360,7 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,
\r
161 - ret = maildir_move_tmp_to_new (tmppath, newpath);
\r
162 + ret = maildir_move_tmp_to_new (tmppath, newpath, newdir);
\r