[Patch v5 08/11] CLI: optionally restore config data.
authorDavid Bremner <david@tethera.net>
Fri, 13 May 2016 10:38:10 +0000 (07:38 +2100)
committerW. Trevor King <wking@tremily.us>
Sat, 20 Aug 2016 23:21:49 +0000 (16:21 -0700)
74/efab1090025551f915304f694254a2fdae5829 [new file with mode: 0644]

diff --git a/74/efab1090025551f915304f694254a2fdae5829 b/74/efab1090025551f915304f694254a2fdae5829
new file mode 100644 (file)
index 0000000..056674b
--- /dev/null
@@ -0,0 +1,205 @@
+Return-Path: <bremner@tethera.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id A48FD6DE0930\r
+ for <notmuch@notmuchmail.org>; Fri, 13 May 2016 03:38:46 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.013\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.013 tagged_above=-999 required=5\r
+ tests=[AWL=-0.002, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id XoEn0ZNirPBx for <notmuch@notmuchmail.org>;\r
+ Fri, 13 May 2016 03:38:38 -0700 (PDT)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 509C46DE028C\r
+ for <notmuch@notmuchmail.org>; Fri, 13 May 2016 03:38:24 -0700 (PDT)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1b1AU5-0003Of-7Y; Fri, 13 May 2016 06:38:17 -0400\r
+Received: (nullmailer pid 7560 invoked by uid 1000);\r
+ Fri, 13 May 2016 10:38:17 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v5 08/11] CLI: optionally restore config data.\r
+Date: Fri, 13 May 2016 07:38:10 -0300\r
+Message-Id: <1463135893-7471-9-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.8.1\r
+In-Reply-To: <1463135893-7471-1-git-send-email-david@tethera.net>\r
+References: <1463135893-7471-1-git-send-email-david@tethera.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 13 May 2016 10:38:46 -0000\r
+\r
+The default to restore config data seems safest, especially since\r
+currently we have no config data to mess up.\r
+---\r
+ doc/man1/notmuch-restore.rst | 18 +++++++++++++++\r
+ notmuch-restore.c            | 54 ++++++++++++++++++++++++++++++++++++++++++++\r
+ test/T590-libconfig.sh       | 11 +++++++++\r
+ 3 files changed, 83 insertions(+)\r
+\r
+diff --git a/doc/man1/notmuch-restore.rst b/doc/man1/notmuch-restore.rst\r
+index 362e262..87fa22e 100644\r
+--- a/doc/man1/notmuch-restore.rst\r
++++ b/doc/man1/notmuch-restore.rst\r
+@@ -50,6 +50,24 @@ Supported options for **restore** include\r
+             format, this heuristic, based the fact that batch-tag format\r
+             contains no parentheses, should be accurate.\r
\r
++    ``--include=(config|tags)``\r
++\r
++      Control what kind of metadata is restored.\r
++\r
++      **config**\r
++\r
++        Restore configuration data to the database. Each configuration line starts\r
++        with "#@ ", followed by a space seperated key-value pair.\r
++        Both key and value are hex encoded if needed.\r
++\r
++      **tags**\r
++\r
++        Output per-message metadata, namely tags. See *format* above\r
++        for more details.\r
++\r
++      The default is to restore both tags and configuration\r
++      information\r
++\r
+     ``--input=``\ <filename>\r
+         Read input from given file instead of stdin.\r
\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index 9abc64f..1f37f5c 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -24,6 +24,39 @@\r
+ #include "string-util.h"\r
+ #include "zlib-extra.h"\r
\r
++static int\r
++process_config_line(notmuch_database_t *notmuch, const char* line)\r
++{\r
++    const char *key_p, *val_p;\r
++    char *key, *val;\r
++    size_t key_len,val_len;\r
++    const char *delim=" \t\n";\r
++    int ret = EXIT_FAILURE;\r
++\r
++    void *local = talloc_new(NULL);\r
++\r
++    key_p = strtok_len_c (line, delim, &key_len);\r
++    val_p = strtok_len_c (key_p + key_len, delim, &val_len);\r
++\r
++    key = talloc_strndup (local, key_p, key_len);\r
++    val = talloc_strndup (local, val_p, val_len);\r
++    if (hex_decode_inplace (key) != HEX_SUCCESS ||\r
++      hex_decode_inplace (val) != HEX_SUCCESS ) {\r
++      fprintf (stderr, "hex decoding failure on line %s\n", line);\r
++      goto DONE;\r
++    }\r
++\r
++    if (print_status_database ("notmuch restore", notmuch,\r
++                             notmuch_database_set_config (notmuch, key, val)))\r
++      goto DONE;\r
++\r
++    ret = EXIT_SUCCESS;\r
++\r
++ DONE:\r
++    talloc_free (local);\r
++    return ret;\r
++}\r
++\r
+ static regex_t regex;\r
\r
+ /* Non-zero return indicates an error in retrieving the message,\r
+@@ -137,6 +170,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
\r
+     int ret = 0;\r
+     int opt_index;\r
++    int include=0;\r
+     int input_format = DUMP_FORMAT_AUTO;\r
\r
+     if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+@@ -152,6 +186,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+                                 { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
+                                 { "sup", DUMP_FORMAT_SUP },\r
+                                 { 0, 0 } } },\r
++      { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I',\r
++        (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },\r
++                                { "tags", DUMP_INCLUDE_TAGS} } },\r
++\r
+       { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
+       { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },\r
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+@@ -167,6 +205,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+     notmuch_process_shared_options (argv[0]);\r
+     notmuch_exit_if_unmatched_db_uuid (notmuch);\r
\r
++    if (include == 0) {\r
++      include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS;\r
++    }\r
++\r
+     name_for_error = input_file_name ? input_file_name : "stdin";\r
\r
+     if (! accumulate)\r
+@@ -225,11 +267,23 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+           ret = EXIT_FAILURE;\r
+           goto DONE;\r
+       }\r
++\r
++      if ((include & DUMP_INCLUDE_CONFIG) && line_len >= 2 && line[0] == '#' && line[1] == '@') {\r
++          ret = process_config_line(notmuch, line+2);\r
++          if (ret)\r
++              goto DONE;\r
++      }\r
++\r
+     } while ((line_len == 0) ||\r
+            (line[0] == '#') ||\r
+            /* the cast is safe because we checked about for line_len < 0 */\r
+            (strspn (line, " \t\n") == (unsigned)line_len));\r
\r
++    if (! (include & DUMP_INCLUDE_TAGS)) {\r
++      ret = EXIT_SUCCESS;\r
++      goto DONE;\r
++    }\r
++\r
+     char *p;\r
+     for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {\r
+       if (*p == '(')\r
+diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
+index 5ea5300..9c1e566 100755\r
+--- a/test/T590-libconfig.sh\r
++++ b/test/T590-libconfig.sh\r
+@@ -132,4 +132,15 @@ cat <<'EOF' >EXPECTED\r
+ EOF\r
+ test_expect_equal_file EXPECTED OUTPUT\r
\r
++test_begin_subtest "restore config"\r
++notmuch dump --include=config >EXPECTED\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++    RUN(notmuch_database_set_config (db, "testkey1", "mutatedvalue"));\r
++}\r
++EOF\r
++notmuch restore --include=config <EXPECTED\r
++notmuch dump --include=config >OUTPUT\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
+ test_done\r
+-- \r
+2.8.1\r
+\r