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 16544431FC2
\r
6 for <notmuch@notmuchmail.org>; Sat, 8 Dec 2012 14:57:55 -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 yIU39Bk32DF9 for <notmuch@notmuchmail.org>;
\r
16 Sat, 8 Dec 2012 14:57:52 -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 1AA32429E41
\r
21 for <notmuch@notmuchmail.org>; Sat, 8 Dec 2012 14:57:43 -0800 (PST)
\r
22 Received: from fctnnbsc30w-142167090129.dhcp-dynamic.fibreop.nb.bellaliant.net
\r
23 ([142.167.90.129] 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 1ThTL3-00066h-NS; Sat, 08 Dec 2012 18:57:42 -0400
\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)
\r
29 (envelope-from <bremner@tethera.net>)
\r
30 id 1ThTKy-0000qh-8K; Sat, 08 Dec 2012 18:57:36 -0400
\r
31 From: david@tethera.net
\r
32 To: notmuch@notmuchmail.org
\r
33 Subject: [Patch v5 05/11] notmuch-restore: add support for input format
\r
35 Date: Sat, 8 Dec 2012 18:56:55 -0400
\r
36 Message-Id: <1355007421-3069-6-git-send-email-david@tethera.net>
\r
37 X-Mailer: git-send-email 1.7.10.4
\r
38 In-Reply-To: <1355007421-3069-1-git-send-email-david@tethera.net>
\r
39 References: <1355007421-3069-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, 08 Dec 2012 22:57:55 -0000
\r
56 From: David Bremner <bremner@debian.org>
\r
58 This can be enabled with the new --format=batch-tag command line
\r
59 option to "notmuch restore". The input must consist of lines of the
\r
62 +<tag>|-<tag> [...] [--] id:<msg-id>
\r
64 Each line is interpreted similarly to "notmuch tag" command line
\r
65 arguments. The delimiter is one or more spaces ' '. Any characters in
\r
66 <tag> and <search-terms> MAY be hex encoded with %NN where NN is the
\r
67 hexadecimal value of the character. Any ' ' and '%' characters in
\r
68 <tag> and <msg-id> MUST be hex encoded (using %20 and %25,
\r
69 respectively). Any characters that are not part of <tag> or
\r
70 <search-terms> MUST NOT be hex encoded.
\r
72 Leading and trailing space ' ' is ignored. Empty lines and lines
\r
73 beginning with '#' are ignored.
\r
75 Commit message mainly stolen from Jani's batch tagging commit, to
\r
78 notmuch-restore.c | 220 +++++++++++++++++++++++++++++++++--------------------
\r
79 1 file changed, 138 insertions(+), 82 deletions(-)
\r
81 diff --git a/notmuch-restore.c b/notmuch-restore.c
\r
82 index f03dcac..44bf88d 100644
\r
83 --- a/notmuch-restore.c
\r
84 +++ b/notmuch-restore.c
\r
88 #include "notmuch-client.h"
\r
89 +#include "dump-restore-private.h"
\r
90 +#include "tag-util.h"
\r
91 +#include "string-util.h"
\r
93 +static volatile sig_atomic_t interrupted;
\r
94 +static regex_t regex;
\r
97 -tag_message (notmuch_database_t *notmuch, const char *message_id,
\r
98 - char *file_tags, notmuch_bool_t remove_all,
\r
99 - notmuch_bool_t synchronize_flags)
\r
100 +tag_message (unused (void *ctx),
\r
101 + notmuch_database_t *notmuch,
\r
102 + const char *message_id,
\r
103 + tag_op_list_t *tag_ops,
\r
104 + tag_op_flag_t flags)
\r
106 notmuch_status_t status;
\r
107 - notmuch_tags_t *db_tags;
\r
108 - char *db_tags_str;
\r
109 notmuch_message_t *message = NULL;
\r
114 status = notmuch_database_find_message (notmuch, message_id, &message);
\r
115 @@ -44,55 +48,67 @@ tag_message (notmuch_database_t *notmuch, const char *message_id,
\r
117 /* In order to detect missing messages, this check/optimization is
\r
118 * intentionally done *after* first finding the message. */
\r
119 - if (! remove_all && (file_tags == NULL || *file_tags == '\0'))
\r
122 - db_tags_str = NULL;
\r
123 - for (db_tags = notmuch_message_get_tags (message);
\r
124 - notmuch_tags_valid (db_tags);
\r
125 - notmuch_tags_move_to_next (db_tags)) {
\r
126 - tag = notmuch_tags_get (db_tags);
\r
129 - db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
\r
131 - db_tags_str = talloc_strdup (message, tag);
\r
133 + if ((flags & TAG_FLAG_REMOVE_ALL) || tag_op_list_size (tag_ops))
\r
134 + tag_op_list_apply (message, tag_ops, flags);
\r
136 - if (((file_tags == NULL || *file_tags == '\0') &&
\r
137 - (db_tags_str == NULL || *db_tags_str == '\0')) ||
\r
138 - (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
\r
140 + notmuch_message_destroy (message);
\r
142 - notmuch_message_freeze (message);
\r
147 - notmuch_message_remove_all_tags (message);
\r
148 +/* Sup dump output is one line per message. We match a sequence of
\r
149 + * non-space characters for the message-id, then one or more
\r
150 + * spaces, then a list of space-separated tags as a sequence of
\r
151 + * characters within literal '(' and ')'. */
\r
153 - next = file_tags;
\r
155 - tag = strsep (&next, " ");
\r
156 - if (*tag == '\0')
\r
158 - status = notmuch_message_add_tag (message, tag);
\r
160 - fprintf (stderr, "Error applying tag %s to message %s:\n",
\r
161 - tag, message_id);
\r
162 - fprintf (stderr, "%s\n", notmuch_status_to_string (status));
\r
166 +parse_sup_line (void *ctx, char *line,
\r
167 + char **query_str, tag_op_list_t *tag_ops)
\r
170 + regmatch_t match[3];
\r
174 + tag_op_list_reset (tag_ops);
\r
176 + chomp_newline (line);
\r
178 + /* Silently ignore blank lines */
\r
179 + if (line[0] == '\0') {
\r
183 - notmuch_message_thaw (message);
\r
184 + rerr = xregexec (®ex, line, 3, match, 0);
\r
185 + if (rerr == REG_NOMATCH) {
\r
186 + fprintf (stderr, "Warning: Ignoring invalid sup format line: %s\n",
\r
191 - if (synchronize_flags)
\r
192 - notmuch_message_tags_to_maildir_flags (message);
\r
193 + *query_str = talloc_strndup (ctx, line + match[1].rm_so,
\r
194 + match[1].rm_eo - match[1].rm_so);
\r
195 + file_tags = talloc_strndup (ctx, line + match[2].rm_so,
\r
196 + match[2].rm_eo - match[2].rm_so);
\r
200 - notmuch_message_destroy (message);
\r
201 + char *tok = file_tags;
\r
202 + size_t tok_len = 0;
\r
204 + tag_op_list_reset (tag_ops);
\r
206 + while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
\r
208 + if (*(tok + tok_len) != '\0') {
\r
209 + *(tok + tok_len) = '\0';
\r
213 + if (tag_op_list_append (ctx, tag_ops, tok, FALSE))
\r
223 @@ -100,16 +116,19 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
225 notmuch_config_t *config;
\r
226 notmuch_database_t *notmuch;
\r
227 - notmuch_bool_t synchronize_flags;
\r
228 notmuch_bool_t accumulate = FALSE;
\r
229 + tag_op_flag_t flags = 0;
\r
230 + tag_op_list_t *tag_ops;
\r
232 char *input_file_name = NULL;
\r
233 FILE *input = stdin;
\r
242 + int input_format = DUMP_FORMAT_AUTO;
\r
244 config = notmuch_config_open (ctx, NULL, NULL);
\r
245 if (config == NULL)
\r
246 @@ -119,9 +138,15 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
247 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
\r
250 - synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
\r
251 + if (notmuch_config_get_maildir_synchronize_flags (config))
\r
252 + flags |= TAG_FLAG_MAILDIR_SYNC;
\r
254 notmuch_opt_desc_t options[] = {
\r
255 + { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',
\r
256 + (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },
\r
257 + { "batch-tag", DUMP_FORMAT_BATCH_TAG },
\r
258 + { "sup", DUMP_FORMAT_SUP },
\r
260 { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
\r
261 { NOTMUCH_OPT_BOOLEAN, &accumulate, "accumulate", 'a', 0 },
\r
263 @@ -134,6 +159,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
267 + if (! accumulate)
\r
268 + flags |= TAG_FLAG_REMOVE_ALL;
\r
270 if (input_file_name) {
\r
271 input = fopen (input_file_name, "r");
\r
272 if (input == NULL) {
\r
273 @@ -149,49 +177,77 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
279 - /* Dump output is one line per message. We match a sequence of
\r
280 - * non-space characters for the message-id, then one or more
\r
281 - * spaces, then a list of space-separated tags as a sequence of
\r
282 - * characters within literal '(' and ')'. */
\r
283 - if ( xregcomp (®ex,
\r
284 - "^([^ ]+) \\(([^)]*)\\)$",
\r
286 - INTERNAL_ERROR ("compile time constant regex failed.");
\r
288 - while ((line_len = getline (&line, &line_size, input)) != -1) {
\r
289 - regmatch_t match[3];
\r
290 - char *message_id, *file_tags;
\r
292 - chomp_newline (line);
\r
294 - rerr = xregexec (®ex, line, 3, match, 0);
\r
295 - if (rerr == REG_NOMATCH) {
\r
296 - fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
\r
299 + line_len = getline (&line, &line_size, input);
\r
300 + if (line_len == 0)
\r
303 + tag_ops = tag_op_list_create (ctx);
\r
304 + if (tag_ops == NULL) {
\r
305 + fprintf (stderr, "Out of memory.\n");
\r
309 + for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {
\r
311 + input_format = DUMP_FORMAT_SUP;
\r
314 + if (input_format == DUMP_FORMAT_AUTO)
\r
315 + input_format = DUMP_FORMAT_BATCH_TAG;
\r
317 + if (input_format == DUMP_FORMAT_SUP)
\r
318 + if ( xregcomp (®ex,
\r
319 + "^([^ ]+) \\(([^)]*)\\)$",
\r
321 + INTERNAL_ERROR ("compile time constant regex failed.");
\r
324 + char *query_string;
\r
326 + if (input_format == DUMP_FORMAT_SUP) {
\r
327 + ret = parse_sup_line (ctx, line, &query_string, tag_ops);
\r
329 + ret = parse_tag_line (ctx, line, TAG_FLAG_BE_GENEROUS,
\r
330 + &query_string, tag_ops);
\r
333 + if (strncmp ("id:", query_string, 3) != 0) {
\r
334 + fprintf (stderr, "Unsupported query: %s\n", query_string);
\r
337 + /* delete id: from front of string; tag_message
\r
338 + * expects a raw message-id.
\r
340 + * XXX: Note that query string id:foo and bar will be
\r
341 + * interpreted as a message id "foo and bar". This
\r
342 + * should eventually be fixed to give a better error
\r
345 + query_string = query_string + 3;
\r
349 - message_id = xstrndup (line + match[1].rm_so,
\r
350 - match[1].rm_eo - match[1].rm_so);
\r
351 - file_tags = xstrndup (line + match[2].rm_so,
\r
352 - match[2].rm_eo - match[2].rm_so);
\r
356 - tag_message (notmuch, message_id, file_tags, ! accumulate,
\r
357 - synchronize_flags);
\r
358 + if (ret < 0 || tag_message (ctx, notmuch, query_string,
\r
362 - free (message_id);
\r
363 - free (file_tags);
\r
365 + } while ((line_len = getline (&line, &line_size, input)) != -1);
\r
367 - regfree (®ex);
\r
368 + if (input_format == DUMP_FORMAT_SUP)
\r
369 + regfree (®ex);
\r
374 notmuch_database_destroy (notmuch);
\r
376 if (input != stdin)
\r