[PATCH v5 0/6] Use Xapian query syntax for batch-tag dump/restore
authorAustin Clements <amdragon@MIT.EDU>
Sun, 6 Jan 2013 20:22:36 +0000 (15:22 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:57 +0000 (09:52 -0800)
50/f3f36b3482898b3525b52b7042528f933e22e3 [new file with mode: 0644]

diff --git a/50/f3f36b3482898b3525b52b7042528f933e22e3 b/50/f3f36b3482898b3525b52b7042528f933e22e3
new file mode 100644 (file)
index 0000000..5c12b92
--- /dev/null
@@ -0,0 +1,368 @@
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id C68EE431FAF\r
+       for <notmuch@notmuchmail.org>; Sun,  6 Jan 2013 12:23:13 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id ktdQvEuy5Zas for <notmuch@notmuchmail.org>;\r
+       Sun,  6 Jan 2013 12:23:12 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-6.mit.edu (DMZ-MAILSEC-SCANNER-6.MIT.EDU\r
+       [18.7.68.35])\r
+       by olra.theworths.org (Postfix) with ESMTP id E6FAE431FAE\r
+       for <notmuch@notmuchmail.org>; Sun,  6 Jan 2013 12:23:11 -0800 (PST)\r
+X-AuditID: 12074423-b7ef96d000000725-20-50e9dd2ed548\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id A6.D1.01829.E2DD9E05; Sun,  6 Jan 2013 15:23:10 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id r06KN9tj012714; \r
+       Sun, 6 Jan 2013 15:23:09 -0500\r
+Received: from drake.dyndns.org (a069.catapulsion.net [70.36.81.69])\r
+       (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id r06KMqP7020340\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Sun, 6 Jan 2013 15:23:02 -0500 (EST)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1Trwk5-0007Y6-EE; Sun, 06 Jan 2013 15:22:49 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5 0/6] Use Xapian query syntax for batch-tag dump/restore\r
+Date: Sun,  6 Jan 2013 15:22:36 -0500\r
+Message-Id: <1357503762-28759-1-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10.4\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFjrEIsWRmVeSWpSXmKPExsUixG6nrqt392WAwdmnghY3WrsZLZqmO1us\r
+       nstjcf3mTGaLNyvnsTqweuycdZfd4/DXhSwet+6/Zvd4tuoWs8eWQ++ZA1ijuGxSUnMyy1KL\r
+       9O0SuDKmLHnFWLA5oGLC4nlMDYz7bLsYOTkkBEwkFt2/wAZhi0lcuLceyObiEBLYxyixceMr\r
+       VghnPaPEq1/PoDL7mSQmTFjOAuHMZZR48mozC0g/m4CGxLb9yxlBbBEBaYmdd2ezgtjMAnES\r
+       Ky8tZgexhQW8JKb0TAXbxyKgKtHyfDIziM0r4CBx/EMX1B2KEt3PJrBNYORdwMiwilE2JbdK\r
+       NzcxM6c4NVm3ODkxLy+1SNdMLzezRC81pXQTIzioXJR3MP45qHSIUYCDUYmH98LOFwFCrIll\r
+       xZW5hxglOZiURHl3X3wZIMSXlJ9SmZFYnBFfVJqTWnyIUYKDWUmEd98xoBxvSmJlVWpRPkxK\r
+       moNFSZz3WspNfyGB9MSS1OzU1ILUIpisDAeHkgSv5B2gRsGi1PTUirTMnBKENBMHJ8hwHqDh\r
+       L2+DDC8uSMwtzkyHyJ9iVJQS55UBaRYASWSU5sH1wqL+FaM40CvCvAYgVTzAhAHX/QpoMBPQ\r
+       4NTHz0EGlyQipKQaGB07rOXapmw7f/pHUNaS4utrqnfPtFn4oFNO5vzLa0bFleueiTHszt0a\r
+       v3/jxpi1X6/wXLHj2nCPQz3zzDoD811cnj8WnOIySkwvyDLleMwYuEgn0feNuJXIOr0LyQVB\r
+       ZvvdZ2du8Py8f7Vuy44UL/livkb3EK+jzcyRUdeWiRY7ZOWHRC3mUGIpzkg01GIuKk4EAGpD\r
+       +tDVAgAA\r
+Cc: tomi.ollila@iki.fi\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 06 Jan 2013 20:23:13 -0000\r
+\r
+This obsoletes\r
+\r
+  id:1356936162-2589-1-git-send-email-amdragon@mit.edu\r
+\r
+v5 should address all of the comments on v4 except those I\r
+specifically replied to (via the ML or IRC).  It also adds a new patch\r
+at the beginning that makes missing message IDs non-fatal in restore,\r
+like they were in 0.14.  This patch can be pushed separately; it's in\r
+this series because later tests rely on it.\r
+\r
+The diff from v4 follows.\r
+\r
+diff --git a/notmuch-dump.c b/notmuch-dump.c\r
+index bf01a39..a3244e0 100644\r
+--- a/notmuch-dump.c\r
++++ b/notmuch-dump.c\r
+@@ -103,6 +103,18 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
+       message = notmuch_messages_get (messages);\r
+       message_id = notmuch_message_get_message_id (message);\r
\r
++      if (output_format == DUMP_FORMAT_BATCH_TAG &&\r
++          strchr (message_id, '\n')) {\r
++          /* This will produce a line break in the output, which\r
++           * would be difficult to handle in tools.  However, it's\r
++           * also impossible to produce an email containing a line\r
++           * break in a message ID because of unfolding, so we can\r
++           * safely disallow it. */\r
++          fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);\r
++          notmuch_message_destroy (message);\r
++          continue;\r
++      }\r
++\r
+       if (output_format == DUMP_FORMAT_SUP) {\r
+           fprintf (output, "%s (", message_id);\r
+       }\r
+@@ -133,19 +145,10 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
+       if (output_format == DUMP_FORMAT_SUP) {\r
+           fputs (")\n", output);\r
+       } else {\r
+-          if (strchr (message_id, '\n')) {\r
+-              /* This will produce a line break in the output, which\r
+-               * would be difficult to handle in tools.  However,\r
+-               * it's also impossible to produce an email containing\r
+-               * a line break in a message ID because of unfolding,\r
+-               * so we can safely disallow it. */\r
+-              fprintf (stderr, "Error: cannot dump message id containing line break: %s\n", message_id);\r
+-              return 1;\r
+-          }\r
+           if (make_boolean_term (notmuch, "id", message_id,\r
+                                  &buffer, &buffer_size)) {\r
+-                  fprintf (stderr, "Error: failed to quote message id %s\n",\r
+-                           message_id);\r
++                  fprintf (stderr, "Error quoting message id %s: %s\n",\r
++                           message_id, strerror (errno));\r
+                   return 1;\r
+           }\r
+           fprintf (output, " -- %s\n", buffer);\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index 77a4c27..81d4d98 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -26,7 +26,8 @@\r
+ static regex_t regex;\r
\r
+ /* Non-zero return indicates an error in retrieving the message,\r
+- * or in applying the tags.\r
++ * or in applying the tags.  Missing messages are reported, but not\r
++ * considered errors.\r
+  */\r
+ static int\r
+ tag_message (unused (void *ctx),\r
+@@ -40,13 +41,17 @@ tag_message (unused (void *ctx),\r
+     int ret = 0;\r
\r
+     status = notmuch_database_find_message (notmuch, message_id, &message);\r
+-    if (status || message == NULL) {\r
+-      fprintf (stderr, "Warning: cannot apply tags to %smessage: %s\n",\r
+-               message ? "" : "missing ", message_id);\r
+-      if (status)\r
+-          fprintf (stderr, "%s\n", notmuch_status_to_string (status));\r
++    if (status) {\r
++      fprintf (stderr, "Error applying tags to message %s: %s\n",\r
++               message_id, notmuch_status_to_string (status));\r
+       return 1;\r
+     }\r
++    if (message == NULL) {\r
++      fprintf (stderr, "Warning: cannot apply tags to missing message: %s\n",\r
++               message_id);\r
++      /* We consider this a non-fatal error. */\r
++      return 0;\r
++    }\r
\r
+     /* In order to detect missing messages, this check/optimization is\r
+      * intentionally done *after* first finding the message. */\r
+@@ -222,12 +227,17 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+           if (ret == 0) {\r
+               ret = parse_boolean_term (line_ctx, query_string,\r
+                                         &prefix, &term);\r
+-              if (ret) {\r
+-                  fprintf (stderr, "Warning: cannot parse query: %s\n",\r
+-                           query_string);\r
++              if (ret && errno == EINVAL) {\r
++                  fprintf (stderr, "Warning: cannot parse query: %s (skipping)\n", query_string);\r
+                   continue;\r
++              } else if (ret) {\r
++                  /* This is more fatal (e.g., out of memory) */\r
++                  fprintf (stderr, "Error parsing query: %s\n",\r
++                           strerror (errno));\r
++                  ret = 1;\r
++                  break;\r
+               } else if (strcmp ("id", prefix) != 0) {\r
+-                  fprintf (stderr, "Warning: not an id query: %s\n", query_string);\r
++                  fprintf (stderr, "Warning: not an id query: %s (skipping)\n", query_string);\r
+                   continue;\r
+               }\r
+               query_string = term;\r
+diff --git a/test/dump-restore b/test/dump-restore\r
+index f9ae5b3..f076c12 100755\r
+--- a/test/dump-restore\r
++++ b/test/dump-restore\r
+@@ -202,18 +202,32 @@ a\r
+ + +e -- id:20091117232137.GA7669@griffis1.net\r
+ # valid id, but warning about missing message\r
+ +e id:missing_message_id\r
++# exercise parser\r
+++e -- id:some)stuff\r
+++e -- id:some stuff\r
+++e -- id:some"stuff\r
+++e -- id:"a_message_id_with""_a_quote"\r
+++e -- id:"a message id with spaces"\r
+++e --  id:an_id_with_leading_and_trailing_ws \\r
++\r
+ EOF\r
\r
+ cat <<EOF > EXPECTED\r
+-Warning: cannot parse query: a\r
++Warning: cannot parse query: a (skipping)\r
+ Warning: no query string [+0]\r
+ Warning: no query string [+a +b]\r
+ Warning: missing query string [+a +b ]\r
+ Warning: no query string after -- [+c +d --]\r
+ Warning: hex decoding of tag %zz failed [+%zz -- id:whatever]\r
+-Warning: cannot parse query: id:"\r
+-Warning: not an id query: tag:abc\r
++Warning: cannot parse query: id:" (skipping)\r
++Warning: not an id query: tag:abc (skipping)\r
+ Warning: cannot apply tags to missing message: missing_message_id\r
++Warning: cannot parse query: id:some)stuff (skipping)\r
++Warning: cannot parse query: id:some stuff (skipping)\r
++Warning: cannot apply tags to missing message: some"stuff\r
++Warning: cannot apply tags to missing message: a_message_id_with"_a_quote\r
++Warning: cannot apply tags to missing message: a message id with spaces\r
++Warning: cannot apply tags to missing message: an_id_with_leading_and_trailing_ws\r
+ EOF\r
\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+diff --git a/util/string-util.c b/util/string-util.c\r
+index 52c7781..aba9aa8 100644\r
+--- a/util/string-util.c\r
++++ b/util/string-util.c\r
+@@ -23,6 +23,7 @@\r
+ #include "talloc.h"\r
\r
+ #include <ctype.h>\r
++#include <errno.h>\r
\r
+ char *\r
+ strtok_len (char *s, const char *delim, size_t *len)\r
+@@ -36,6 +37,12 @@ strtok_len (char *s, const char *delim, size_t *len)\r
+     return *len ? s : NULL;\r
+ }\r
\r
++static int\r
++is_unquoted_terminator (unsigned char c)\r
++{\r
++    return c == 0 || c <= ' ' || c == ')';\r
++}\r
++\r
+ int\r
+ make_boolean_term (void *ctx, const char *prefix, const char *term,\r
+                  char **buf, size_t *len)\r
+@@ -49,7 +56,8 @@ make_boolean_term (void *ctx, const char *prefix, const char *term,\r
+      * containing a quote, even though it only matters at the\r
+      * beginning, and anything containing non-ASCII text. */\r
+     for (in = term; *in && !need_quoting; in++)\r
+-      if (*in <= ' ' || *in == ')' || *in == '"' || (unsigned char)*in > 127)\r
++      if (is_unquoted_terminator (*in) || *in == '"'\r
++          || (unsigned char)*in > 127)\r
+           need_quoting = 1;\r
\r
+     if (need_quoting)\r
+@@ -67,8 +75,10 @@ make_boolean_term (void *ctx, const char *prefix, const char *term,\r
+       *buf = talloc_realloc (ctx, *buf, char, *len);\r
+     }\r
\r
+-    if (! *buf)\r
+-      return 1;\r
++    if (! *buf) {\r
++      errno = ENOMEM;\r
++      return -1;\r
++    }\r
\r
+     out = *buf;\r
\r
+@@ -102,7 +112,7 @@ make_boolean_term (void *ctx, const char *prefix, const char *term,\r
+ static const char*\r
+ skip_space (const char *str)\r
+ {\r
+-    while (*str && isspace (*str))\r
++    while (*str && isspace ((unsigned char) *str))\r
+       ++str;\r
+     return str;\r
+ }\r
+@@ -111,6 +121,7 @@ int\r
+ parse_boolean_term (void *ctx, const char *str,\r
+                   char **prefix_out, char **term_out)\r
+ {\r
++    int err = EINVAL;\r
+     *prefix_out = *term_out = NULL;\r
\r
+     /* Parse prefix */\r
+@@ -119,12 +130,20 @@ parse_boolean_term (void *ctx, const char *str,\r
+     if (! pos)\r
+       goto FAIL;\r
+     *prefix_out = talloc_strndup (ctx, str, pos - str);\r
++    if (! *prefix_out) {\r
++      err = ENOMEM;\r
++      goto FAIL;\r
++    }\r
+     ++pos;\r
\r
+     /* Implement de-quoting compatible with make_boolean_term. */\r
+     if (*pos == '"') {\r
+       char *out = talloc_array (ctx, char, strlen (pos));\r
+       int closed = 0;\r
++      if (! out) {\r
++          err = ENOMEM;\r
++          goto FAIL;\r
++      }\r
+       *term_out = out;\r
+       /* Skip the opening quote, find the closing quote, and\r
+        * un-double doubled internal quotes. */\r
+@@ -148,18 +167,25 @@ parse_boolean_term (void *ctx, const char *str,\r
+     } else {\r
+       const char *start = pos;\r
+       /* Check for text after the boolean term. */\r
+-      while (*pos > ' ' && *pos != ')')\r
++      while (! is_unquoted_terminator (*pos))\r
+           ++pos;\r
+-      if (*skip_space (pos))\r
++      if (*skip_space (pos)) {\r
++          err = EINVAL;\r
+           goto FAIL;\r
++      }\r
+       /* No trailing text; dup the string so the caller can free\r
+        * it. */\r
+       *term_out = talloc_strndup (ctx, start, pos - start);\r
++      if (! *term_out) {\r
++          err = ENOMEM;\r
++          goto FAIL;\r
++      }\r
+     }\r
+     return 0;\r
\r
+  FAIL:\r
+     talloc_free (*prefix_out);\r
+     talloc_free (*term_out);\r
+-    return 1;\r
++    errno = err;\r
++    return -1;\r
+ }\r
+diff --git a/util/string-util.h b/util/string-util.h\r
+index 8b9fe50..0194607 100644\r
+--- a/util/string-util.h\r
++++ b/util/string-util.h\r
+@@ -28,7 +28,8 @@ char *strtok_len (char *s, const char *delim, size_t *len);\r
+  * can be parsed by parse_boolean_term.\r
+  *\r
+  * Output is into buf; it may be talloc_realloced.\r
+- * Return: 0 on success, non-zero on memory allocation failure.\r
++ * Return: 0 on success, -1 on error.  errno will be set to ENOMEM if\r
++ * there is an allocation failure.\r
+  */\r
+ int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term,\r
+                      char **buf, size_t *len);\r
+@@ -42,7 +43,8 @@ int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term,\r
+  * of the quoting styles supported by Xapian (and hence notmuch).\r
+  * *prefix_out and *term_out will be talloc'd with context ctx.\r
+  *\r
+- * Return: 0 on success, non-zero on parse error.\r
++ * Return: 0 on success, -1 on error.  errno will be set to EINVAL if\r
++ * there is a parse error or ENOMEM if there is an allocation failure.\r
+  */\r
+ int\r
+ parse_boolean_term (void *ctx, const char *str,\r
+\r
+\r