1 Return-Path: <jani@nikula.org>
\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 156DD431FC0
\r
6 for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:55:20 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"
\r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5
\r
13 tests=[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 Nhtc693K3gC1 for <notmuch@notmuchmail.org>;
\r
17 Mon, 22 Sep 2014 02:55:11 -0700 (PDT)
\r
18 Received: from mail-wg0-f49.google.com (mail-wg0-f49.google.com
\r
19 [74.125.82.49]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client
\r
20 certificate requested) by olra.theworths.org (Postfix) with ESMTPS id
\r
21 B8204431FDF for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:54:59 -0700
\r
23 Received: by mail-wg0-f49.google.com with SMTP id x12so2366252wgg.32
\r
24 for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:54:58 -0700 (PDT)
\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
\r
26 d=1e100.net; s=20130820;
\r
27 h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
\r
28 :references:in-reply-to:references;
\r
29 bh=3bVqcdDhIySrRRRi4bCUdGBrxsSnSBjKxj6MAQxsjS0=;
\r
30 b=RfSkh8JZ6gVTbELKbOVK/ffVm0TrloUC//BU1zimdPfLcVyBn7qAHeP8s8SCAd3kbX
\r
31 Odt6qh2vg2uPA1V27wdoFeU7AfB5MD3Eq0o765/W1nQKcfQCSPaoK+ppcxAiA0w+MuEI
\r
32 cMgfJjbKoX37VD0d6rJjEVubrN4gF/ZxRpk3KwqFD1Bsl81Zo58e/9HRAWmoe5vTT4Wx
\r
33 8vjUgYEMr/pKySTMMgZA5UIZEBLlIExXHe+DPjq8cA4IyrmZtxSEljEyqmG30fwbdEyj
\r
34 WEuIGmEIKrFbqeP5SqxpfQ9i/6JUqaqjC6TPVxmqOD2CEwpQkEfa8wTCL/dshbaRk1HB
\r
37 ALoCoQnRQVpFWPOgxEBHRuNvbtpG6I88MzoSvV3TYC/mwRUwhVr0OQoMzTaw701qJ6zv8Coh96K3
\r
38 X-Received: by 10.180.99.34 with SMTP id en2mr14438687wib.34.1411379698621;
\r
39 Mon, 22 Sep 2014 02:54:58 -0700 (PDT)
\r
40 Received: from localhost ([2001:4b98:dc0:43:216:3eff:fe1b:25f3])
\r
41 by mx.google.com with ESMTPSA id e5sm5748146wiw.2.2014.09.22.02.54.57
\r
42 for <multiple recipients>
\r
43 (version=TLSv1.1 cipher=RC4-SHA bits=128/128);
\r
44 Mon, 22 Sep 2014 02:54:58 -0700 (PDT)
\r
45 From: Jani Nikula <jani@nikula.org>
\r
46 To: notmuch@notmuchmail.org
\r
47 Subject: [PATCH 08/11] cli/insert: rehash file writing functions
\r
48 Date: Mon, 22 Sep 2014 11:54:59 +0200
\r
50 <db771a008661ab5205dc4749206bc7515950f9e3.1411379395.git.jani@nikula.org>
\r
51 X-Mailer: git-send-email 1.7.2.5
\r
52 In-Reply-To: <cover.1411379395.git.jani@nikula.org>
\r
53 References: <cover.1411379395.git.jani@nikula.org>
\r
54 In-Reply-To: <cover.1411379395.git.jani@nikula.org>
\r
55 References: <cover.1411379395.git.jani@nikula.org>
\r
56 X-BeenThere: notmuch@notmuchmail.org
\r
57 X-Mailman-Version: 2.1.13
\r
59 List-Id: "Use and development of the notmuch mail system."
\r
60 <notmuch.notmuchmail.org>
\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
62 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
64 List-Post: <mailto:notmuch@notmuchmail.org>
\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
67 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
68 X-List-Received-Date: Mon, 22 Sep 2014 09:55:20 -0000
\r
70 Make the function calls make more sense as independent building blocks
\r
71 of the big picture, with clear inputs and outputs. Split up
\r
72 write_message into two. Improve function documentation. Cleanup and
\r
73 clarify the error paths.
\r
75 notmuch-insert.c | 127 ++++++++++++++++++++++++++++++++----------------------
\r
76 1 files changed, 75 insertions(+), 52 deletions(-)
\r
78 diff --git a/notmuch-insert.c b/notmuch-insert.c
\r
79 index a1d564c..5ef6e66 100644
\r
80 --- a/notmuch-insert.c
\r
81 +++ b/notmuch-insert.c
\r
82 @@ -204,47 +204,37 @@ tempfilename (const void *ctx)
\r
86 -/* Open a unique file in the 'tmp' sub-directory of dir.
\r
87 - * Returns the file descriptor on success, or -1 on failure.
\r
88 - * On success, file paths for the message in the 'tmp' and 'new'
\r
89 - * directories are returned via tmppath and newpath,
\r
90 - * and the path of the 'new' directory itself in newdir. */
\r
92 + * Create a unique temporary file in maildir/tmp, return fd and full
\r
93 + * path to file in *path_out, or -1 on errors (in which case *path_out
\r
94 + * is not touched).
\r
97 -maildir_open_tmp_file (void *ctx, const char *dir,
\r
98 - char **tmppath, char **newpath, char **newdir)
\r
99 +maildir_mktemp (const void *ctx, const char *maildir, char **path_out)
\r
103 + char *filename, *path;
\r
107 filename = tempfilename (ctx);
\r
111 - *tmppath = talloc_asprintf (ctx, "%s/tmp/%s", dir, filename);
\r
112 - if (! *tmppath) {
\r
113 - fprintf (stderr, "Out of memory\n");
\r
114 + path = talloc_asprintf (ctx, "%s/tmp/%s", maildir, filename);
\r
116 + fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
\r
120 - fd = open (*tmppath, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);
\r
121 + fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);
\r
122 } while (fd == -1 && errno == EEXIST);
\r
125 - fprintf (stderr, "Error: opening %s: %s\n", *tmppath, strerror (errno));
\r
126 + fprintf (stderr, "Error: open '%s': %s\n", path, strerror (errno));
\r
130 - *newdir = talloc_asprintf (ctx, "%s/new", dir);
\r
131 - *newpath = talloc_asprintf (ctx, "%s/new/%s", dir, filename);
\r
132 - if (! *newdir || ! *newpath) {
\r
133 - fprintf (stderr, "Out of memory\n");
\r
135 - unlink (*tmppath);
\r
139 - talloc_free (filename);
\r
140 + *path_out = path;
\r
144 @@ -293,53 +283,85 @@ copy_fd (int fdout, int fdin)
\r
145 return (!interrupted && !empty);
\r
148 -static notmuch_bool_t
\r
149 -write_message (void *ctx, int fdin, const char *dir, char **newpath)
\r
151 + * Write fdin to a new temp file in maildir/tmp, return full path to
\r
152 + * the file, or NULL on errors.
\r
155 +maildir_write_tmp (const void *ctx, int fdin, const char *maildir)
\r
159 - char *cleanup_path;
\r
163 - fdout = maildir_open_tmp_file (ctx, dir, &tmppath, newpath, &newdir);
\r
164 + fdout = maildir_mktemp (ctx, maildir, &path);
\r
168 - cleanup_path = tmppath;
\r
171 if (! copy_fd (fdout, fdin))
\r
174 - if (fsync (fdout) != 0) {
\r
175 - fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));
\r
176 + if (fsync (fdout)) {
\r
177 + fprintf (stderr, "Error: fsync '%s': %s\n", path, strerror (errno));
\r
184 - /* Atomically move the new message file from the Maildir 'tmp' directory
\r
185 - * to the 'new' directory. We follow the Dovecot recommendation to
\r
186 - * simply use rename() instead of link() and unlink().
\r
187 - * See also: http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_delivery
\r
189 - if (rename (tmppath, *newpath) != 0) {
\r
190 - fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));
\r
202 + * Write fdin to a new file in maildir/new, using an intermediate temp
\r
203 + * file in maildir/tmp, return full path to the new file, or NULL on
\r
207 +maildir_write_new (const void *ctx, int fdin, const char *maildir)
\r
209 + char *cleanpath, *tmppath, *newpath, *newdir;
\r
211 + tmppath = maildir_write_tmp (ctx, fdin, maildir);
\r
214 + cleanpath = tmppath;
\r
216 + newpath = talloc_strdup (ctx, tmppath);
\r
218 + fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
\r
222 - cleanup_path = *newpath;
\r
223 + /* sanity checks needed? */
\r
224 + memcpy (newpath + strlen (maildir) + 1, "new", 3);
\r
226 + if (rename (tmppath, newpath)) {
\r
227 + fprintf (stderr, "Error: rename '%s' '%s': %s\n",
\r
228 + tmppath, newpath, strerror (errno));
\r
231 + cleanpath = newpath;
\r
233 + newdir = talloc_asprintf (ctx, "%s/%s", maildir, "new");
\r
235 + fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
\r
239 if (! sync_dir (newdir))
\r
246 + unlink (cleanpath);
\r
251 - unlink (cleanup_path);
\r
256 /* Add the specified message file to the notmuch database, applying tags.
\r
257 @@ -477,7 +499,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
\r
258 return EXIT_FAILURE;
\r
260 /* Write the message to the Maildir new directory. */
\r
261 - if (! write_message (config, STDIN_FILENO, maildir, &newpath)) {
\r
262 + newpath = maildir_write_new (config, STDIN_FILENO, maildir);
\r
264 notmuch_database_destroy (notmuch);
\r
265 return EXIT_FAILURE;
\r