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 DAF12431FC2
\r
6 for <notmuch@notmuchmail.org>; Sat, 20 Jul 2013 17:08:03 -0700 (PDT)
\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 nwV+ObB15cWw for <notmuch@notmuchmail.org>;
\r
17 Sat, 20 Jul 2013 17:07:56 -0700 (PDT)
\r
18 Received: from mail-pb0-f51.google.com (mail-pb0-f51.google.com
\r
19 [209.85.160.51]) (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 27C48431FB6
\r
22 for <notmuch@notmuchmail.org>; Sat, 20 Jul 2013 17:07:56 -0700 (PDT)
\r
23 Received: by mail-pb0-f51.google.com with SMTP id um15so5763157pbc.10
\r
24 for <notmuch@notmuchmail.org>; Sat, 20 Jul 2013 17:07:55 -0700 (PDT)
\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;
\r
27 bh=xy4St4yA/Cfj6QR8U0e1DLl54f8kW6J3/5v0YQNjyT4=;
\r
28 b=KJ1WZ5UwwlQatlIrSjIuwlLBkFLanp5S6VWJBSnKidaZgNEXVsxD4LR+O3399QzCgw
\r
29 qUInfNNc4ZO2bQxdjH0PP0H2ridGypYkoR7U/rt84BAW+ZB0UT5bFtRQeckjJB7eRyri
\r
30 91mh4VZZ02XWhXeJesPDQqhFmgRufrcfh1cdEPkNBtwOrdkr5rex3bOX43CSgoC8kR04
\r
31 kFxL28gcN9Z13dRBFaF7Ztvqeen7uII1pB9FRkHz5NV1pl7p9AYw/YFGpE9FwA8FtmyQ
\r
32 NVxak5CnI6Mzm3zx1/wBqPt7vwPe7fHmh0MW18kRo8f6xzzTJHizXLNOA9hUdQfFOSNL
\r
34 X-Received: by 10.66.240.2 with SMTP id vw2mr25577980pac.137.1374365274354;
\r
35 Sat, 20 Jul 2013 17:07:54 -0700 (PDT)
\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.
\r
37 [220.233.42.215]) by mx.google.com with ESMTPSA id
\r
38 ht5sm27318443pbb.29.2013.07.20.17.07.50 for <multiple recipients>
\r
39 (version=TLSv1.2 cipher=RC4-SHA bits=128/128);
\r
40 Sat, 20 Jul 2013 17:07:53 -0700 (PDT)
\r
41 From: Peter Wang <novalazy@gmail.com>
\r
42 To: notmuch@notmuchmail.org
\r
43 Subject: [PATCH 1/3] cli: add insert --must-index option
\r
44 Date: Sun, 21 Jul 2013 10:07:32 +1000
\r
45 Message-Id: <1374365254-13227-1-git-send-email-novalazy@gmail.com>
\r
46 X-Mailer: git-send-email 1.7.12.1
\r
47 X-BeenThere: notmuch@notmuchmail.org
\r
48 X-Mailman-Version: 2.1.13
\r
50 List-Id: "Use and development of the notmuch mail system."
\r
51 <notmuch.notmuchmail.org>
\r
52 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
53 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
54 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
55 List-Post: <mailto:notmuch@notmuchmail.org>
\r
56 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
57 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
58 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
59 X-List-Received-Date: Sun, 21 Jul 2013 00:08:04 -0000
\r
61 This option causes notmuch insert to fail as a whole if the message
\r
62 failed to be added to the notmuch database. The new message file
\r
63 will be deleted from disk, and a distinct status code (2) returned.
\r
65 notmuch-insert.c | 76 ++++++++++++++++++++++++++++++++++----------------------
\r
66 1 file changed, 46 insertions(+), 30 deletions(-)
\r
68 diff --git a/notmuch-insert.c b/notmuch-insert.c
\r
69 index 2207b1e..505b647 100644
\r
70 --- a/notmuch-insert.c
\r
71 +++ b/notmuch-insert.c
\r
73 #include <sys/stat.h>
\r
76 +#define INSERT_EXIT_SUCCESS 0
\r
77 +#define INSERT_EXIT_FAIL_WRITE 1
\r
78 +#define INSERT_EXIT_FAIL_INDEX 2
\r
80 static volatile sig_atomic_t interrupted;
\r
83 @@ -293,12 +297,13 @@ copy_stdin (int fdin, int fdout)
\r
85 /* Add the specified message file to the notmuch database, applying tags.
\r
86 * The file is renamed to encode notmuch tags as maildir flags. */
\r
88 +static notmuch_status_t
\r
89 add_file_to_database (notmuch_database_t *notmuch, const char *path,
\r
90 tag_op_list_t *tag_ops)
\r
92 notmuch_message_t *message;
\r
93 notmuch_status_t status;
\r
94 + notmuch_status_t sync;
\r
96 status = notmuch_database_add_message (notmuch, path, &message);
\r
98 @@ -318,47 +323,52 @@ add_file_to_database (notmuch_database_t *notmuch, const char *path,
\r
99 case NOTMUCH_STATUS_LAST_STATUS:
\r
100 fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",
\r
101 path, notmuch_status_to_string (status));
\r
106 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
\r
107 /* Don't change tags of an existing message. */
\r
108 - status = notmuch_message_tags_to_maildir_flags (message);
\r
109 - if (status != NOTMUCH_STATUS_SUCCESS)
\r
110 + sync = notmuch_message_tags_to_maildir_flags (message);
\r
111 + if (sync != NOTMUCH_STATUS_SUCCESS)
\r
112 fprintf (stderr, "Error: failed to sync tags to maildir flags\n");
\r
114 tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC);
\r
117 notmuch_message_destroy (message);
\r
122 -static notmuch_bool_t
\r
124 insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,
\r
125 - const char *dir, tag_op_list_t *tag_ops)
\r
126 + const char *dir, tag_op_list_t *tag_ops,
\r
127 + notmuch_bool_t must_index)
\r
133 - char *cleanup_path;
\r
134 + notmuch_status_t status;
\r
136 fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir);
\r
139 + return INSERT_EXIT_FAIL_WRITE;
\r
141 - cleanup_path = tmppath;
\r
143 - if (! copy_stdin (fdin, fdout))
\r
145 + if (! copy_stdin (fdin, fdout)) {
\r
147 + unlink (tmppath);
\r
148 + return INSERT_EXIT_FAIL_WRITE;
\r
151 if (fsync (fdout) != 0) {
\r
152 fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));
\r
155 + unlink (tmppath);
\r
156 + return INSERT_EXIT_FAIL_WRITE;
\r
162 /* Atomically move the new message file from the Maildir 'tmp' directory
\r
163 * to the 'new' directory. We follow the Dovecot recommendation to
\r
164 @@ -367,25 +377,28 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,
\r
166 if (rename (tmppath, newpath) != 0) {
\r
167 fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));
\r
169 + unlink (tmppath);
\r
170 + return INSERT_EXIT_FAIL_WRITE;
\r
173 - cleanup_path = newpath;
\r
175 - if (! sync_dir (newdir))
\r
177 + if (! sync_dir (newdir)) {
\r
178 + unlink (newpath);
\r
179 + return INSERT_EXIT_FAIL_WRITE;
\r
182 - /* Even if adding the message to the notmuch database fails,
\r
183 - * the message is on disk and we consider the delivery completed. */
\r
184 - add_file_to_database (notmuch, newpath, tag_ops);
\r
185 + status = add_file_to_database (notmuch, newpath, tag_ops);
\r
188 + /* If must_index is TRUE, then indexing must succeed. Otherwise, we
\r
189 + * consider the delivery completed as long as the message is on disk. */
\r
190 + if (must_index &&
\r
191 + status != NOTMUCH_STATUS_SUCCESS &&
\r
192 + status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
\r
194 + unlink (newpath);
\r
195 + return INSERT_EXIT_FAIL_INDEX;
\r
201 - unlink (cleanup_path);
\r
203 + return INSERT_EXIT_SUCCESS;
\r
207 @@ -400,6 +413,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
\r
208 char *query_string = NULL;
\r
209 const char *folder = NULL;
\r
210 notmuch_bool_t create_folder = FALSE;
\r
211 + notmuch_bool_t must_index = FALSE;
\r
212 const char *maildir;
\r
215 @@ -408,6 +422,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
\r
216 notmuch_opt_desc_t options[] = {
\r
217 { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },
\r
218 { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },
\r
219 + { NOTMUCH_OPT_BOOLEAN, &must_index, "must-index", 0, 0 },
\r
220 { NOTMUCH_OPT_END, 0, 0, 0, 0 }
\r
223 @@ -471,9 +486,10 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
\r
224 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
\r
227 - ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops);
\r
228 + ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops,
\r
231 notmuch_database_destroy (notmuch);
\r
233 - return (ret) ? 0 : 1;
\r