1 Return-Path: <bremner@tethera.net>
\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 73164431FD5
\r
6 for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 13:20:50 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id ugtun4eivJZn for <notmuch@notmuchmail.org>;
\r
16 Sat, 24 Nov 2012 13:20:47 -0800 (PST)
\r
17 Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])
\r
18 (using TLSv1 with cipher AES256-SHA (256/256 bits))
\r
19 (No client certificate requested)
\r
20 by olra.theworths.org (Postfix) with ESMTPS id 43FF5431E62
\r
21 for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 13:20:34 -0800 (PST)
\r
22 Received: from fctnnbsc30w-156034089108.dhcp-dynamic.fibreop.nb.bellaliant.net
\r
23 ([156.34.89.108] helo=zancas.localnet)
\r
24 by tesseract.cs.unb.ca with esmtpsa
\r
25 (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)
\r
26 (envelope-from <bremner@tethera.net>)
\r
27 id 1TcN9N-0006ST-3y; Sat, 24 Nov 2012 17:20:33 -0400
\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)
\r
29 (envelope-from <bremner@tethera.net>)
\r
30 id 1TcN9H-0008DN-Lr; Sat, 24 Nov 2012 17:20:27 -0400
\r
31 From: david@tethera.net
\r
32 To: notmuch@notmuchmail.org
\r
33 Subject: [Patch v2 13/17] notmuch-restore: add support for input format
\r
35 Date: Sat, 24 Nov 2012 17:20:13 -0400
\r
36 Message-Id: <1353792017-31459-14-git-send-email-david@tethera.net>
\r
37 X-Mailer: git-send-email 1.7.10.4
\r
38 In-Reply-To: <1353792017-31459-1-git-send-email-david@tethera.net>
\r
39 References: <1353792017-31459-1-git-send-email-david@tethera.net>
\r
41 Cc: David Bremner <bremner@debian.org>
\r
42 X-BeenThere: notmuch@notmuchmail.org
\r
43 X-Mailman-Version: 2.1.13
\r
45 List-Id: "Use and development of the notmuch mail system."
\r
46 <notmuch.notmuchmail.org>
\r
47 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
48 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
49 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
50 List-Post: <mailto:notmuch@notmuchmail.org>
\r
51 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
52 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
53 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
54 X-List-Received-Date: Sat, 24 Nov 2012 21:20:50 -0000
\r
56 From: David Bremner <bremner@debian.org>
\r
58 This is the same as the batch input for notmuch tag, except by default
\r
59 it removes all tags before modifying a given message id and only "id:"
\r
62 notmuch-restore.c | 199 +++++++++++++++++++++++++++++++++--------------------
\r
63 1 file changed, 125 insertions(+), 74 deletions(-)
\r
65 diff --git a/notmuch-restore.c b/notmuch-restore.c
\r
66 index f03dcac..22fcd2d 100644
\r
67 --- a/notmuch-restore.c
\r
68 +++ b/notmuch-restore.c
\r
72 #include "notmuch-client.h"
\r
73 +#include "dump-restore-private.h"
\r
74 +#include "tag-util.h"
\r
75 +#include "string-util.h"
\r
77 +static volatile sig_atomic_t interrupted;
\r
78 +static regex_t regex;
\r
81 -tag_message (notmuch_database_t *notmuch, const char *message_id,
\r
82 - char *file_tags, notmuch_bool_t remove_all,
\r
83 - notmuch_bool_t synchronize_flags)
\r
84 +tag_message (unused (void *ctx),
\r
85 + notmuch_database_t *notmuch,
\r
86 + const char *message_id,
\r
87 + tag_op_list_t *tag_ops,
\r
88 + tag_op_flag_t flags)
\r
90 notmuch_status_t status;
\r
91 - notmuch_tags_t *db_tags;
\r
92 - char *db_tags_str;
\r
93 notmuch_message_t *message = NULL;
\r
98 status = notmuch_database_find_message (notmuch, message_id, &message);
\r
99 @@ -44,55 +48,63 @@ tag_message (notmuch_database_t *notmuch, const char *message_id,
\r
101 /* In order to detect missing messages, this check/optimization is
\r
102 * intentionally done *after* first finding the message. */
\r
103 - if (! remove_all && (file_tags == NULL || *file_tags == '\0'))
\r
106 - db_tags_str = NULL;
\r
107 - for (db_tags = notmuch_message_get_tags (message);
\r
108 - notmuch_tags_valid (db_tags);
\r
109 - notmuch_tags_move_to_next (db_tags)) {
\r
110 - tag = notmuch_tags_get (db_tags);
\r
113 - db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
\r
115 - db_tags_str = talloc_strdup (message, tag);
\r
117 + if ( (flags & TAG_FLAG_REMOVE_ALL) || (tag_op_list_size (tag_ops)))
\r
118 + tag_op_list_apply (message, tag_ops, flags);
\r
120 - if (((file_tags == NULL || *file_tags == '\0') &&
\r
121 - (db_tags_str == NULL || *db_tags_str == '\0')) ||
\r
122 - (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
\r
125 + notmuch_message_destroy (message);
\r
127 - notmuch_message_freeze (message);
\r
132 - notmuch_message_remove_all_tags (message);
\r
134 +parse_sup_line (void *ctx, char *line,
\r
135 + char **query_str, tag_op_list_t *tag_ops)
\r
138 - next = file_tags;
\r
140 - tag = strsep (&next, " ");
\r
141 - if (*tag == '\0')
\r
143 - status = notmuch_message_add_tag (message, tag);
\r
145 - fprintf (stderr, "Error applying tag %s to message %s:\n",
\r
146 - tag, message_id);
\r
147 - fprintf (stderr, "%s\n", notmuch_status_to_string (status));
\r
150 + regmatch_t match[3];
\r
154 + tag_op_list_reset (tag_ops);
\r
156 + chomp_newline (line);
\r
158 + /* Silently ignore blank lines */
\r
159 + if (line[0] == '\0') {
\r
163 + rerr = xregexec (®ex, line, 3, match, 0);
\r
164 + if (rerr == REG_NOMATCH) {
\r
165 + fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
\r
170 - notmuch_message_thaw (message);
\r
171 + *query_str = talloc_strndup (ctx, line + match[1].rm_so,
\r
172 + match[1].rm_eo - match[1].rm_so);
\r
173 + file_tags = talloc_strndup (ctx, line + match[2].rm_so,
\r
174 + match[2].rm_eo - match[2].rm_so);
\r
176 - if (synchronize_flags)
\r
177 - notmuch_message_tags_to_maildir_flags (message);
\r
178 + char *tok = file_tags;
\r
179 + size_t tok_len = 0;
\r
183 - notmuch_message_destroy (message);
\r
184 + tag_op_list_reset (tag_ops);
\r
186 + while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
\r
188 + if (*(tok + tok_len) != '\0') {
\r
189 + *(tok + tok_len) = '\0';
\r
193 + if (tag_op_list_append (ctx, tag_ops, tok, FALSE))
\r
203 @@ -100,16 +112,19 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
205 notmuch_config_t *config;
\r
206 notmuch_database_t *notmuch;
\r
207 - notmuch_bool_t synchronize_flags;
\r
208 notmuch_bool_t accumulate = FALSE;
\r
209 + tag_op_flag_t flags = 0;
\r
210 + tag_op_list_t *tag_ops;
\r
212 char *input_file_name = NULL;
\r
213 FILE *input = stdin;
\r
222 + int input_format = DUMP_FORMAT_AUTO;
\r
224 config = notmuch_config_open (ctx, NULL, NULL);
\r
225 if (config == NULL)
\r
226 @@ -119,9 +134,15 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
227 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
\r
230 - synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
\r
231 + if (notmuch_config_get_maildir_synchronize_flags (config))
\r
232 + flags |= TAG_FLAG_MAILDIR_SYNC;
\r
234 notmuch_opt_desc_t options[] = {
\r
235 + { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',
\r
236 + (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },
\r
237 + { "batch-tag", DUMP_FORMAT_BATCH_TAG },
\r
238 + { "sup", DUMP_FORMAT_SUP },
\r
240 { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
\r
241 { NOTMUCH_OPT_BOOLEAN, &accumulate, "accumulate", 'a', 0 },
\r
243 @@ -134,6 +155,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
247 + if (! accumulate)
\r
248 + flags |= TAG_FLAG_REMOVE_ALL;
\r
250 if (input_file_name) {
\r
251 input = fopen (input_file_name, "r");
\r
252 if (input == NULL) {
\r
253 @@ -154,35 +178,61 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
254 * non-space characters for the message-id, then one or more
\r
255 * spaces, then a list of space-separated tags as a sequence of
\r
256 * characters within literal '(' and ')'. */
\r
257 - if ( xregcomp (®ex,
\r
258 - "^([^ ]+) \\(([^)]*)\\)$",
\r
260 - INTERNAL_ERROR ("compile time constant regex failed.");
\r
263 - while ((line_len = getline (&line, &line_size, input)) != -1) {
\r
264 - regmatch_t match[3];
\r
265 - char *message_id, *file_tags;
\r
266 + line_len = getline (&line, &line_size, input);
\r
267 + if (line_len == 0)
\r
270 - chomp_newline (line);
\r
271 + for (p = line; *p; p++) {
\r
273 + input_format = DUMP_FORMAT_SUP;
\r
276 - rerr = xregexec (®ex, line, 3, match, 0);
\r
277 - if (rerr == REG_NOMATCH) {
\r
278 - fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
\r
281 + if (input_format == DUMP_FORMAT_AUTO)
\r
282 + input_format = DUMP_FORMAT_BATCH_TAG;
\r
284 + if (input_format == DUMP_FORMAT_SUP)
\r
285 + if ( xregcomp (®ex,
\r
286 + "^([^ ]+) \\(([^)]*)\\)$",
\r
288 + INTERNAL_ERROR ("compile time constant regex failed.");
\r
290 + tag_ops = tag_op_list_create (ctx);
\r
291 + if (tag_ops == NULL) {
\r
292 + fprintf (stderr, "Out of memory.\n");
\r
297 + char *query_string;
\r
299 + if (input_format == DUMP_FORMAT_SUP) {
\r
300 + ret = parse_sup_line (ctx, line, &query_string, tag_ops);
\r
302 + ret = parse_tag_line (ctx, line, TAG_FLAG_BE_GENEROUS,
\r
303 + &query_string, tag_ops);
\r
306 + if ( strncmp ("id:", query_string, 3) != 0) {
\r
307 + fprintf (stderr, "Unsupported query: %s\n", query_string);
\r
310 + /* delete id: from front of string; tag_message expects a
\r
311 + * raw message-id */
\r
312 + query_string = query_string + 3;
\r
316 - message_id = xstrndup (line + match[1].rm_so,
\r
317 - match[1].rm_eo - match[1].rm_so);
\r
318 - file_tags = xstrndup (line + match[2].rm_so,
\r
319 - match[2].rm_eo - match[2].rm_so);
\r
323 - tag_message (notmuch, message_id, file_tags, ! accumulate,
\r
324 - synchronize_flags);
\r
325 + if (ret < 0 || tag_message (ctx, notmuch, query_string,
\r
329 + } while ((line_len = getline (&line, &line_size, input)) != -1);
\r
331 - free (message_id);
\r
332 - free (file_tags);
\r
337 @@ -190,8 +240,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
340 notmuch_database_destroy (notmuch);
\r
342 if (input != stdin)
\r