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