Re: [PATCH] Fix typo in Message.maildir_flags_to_tags
[notmuch-archives.git] / fa / 3a4318f276ab1accdaee00c4ea07da1e1069a1
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 arlo.cworth.org (Postfix) with ESMTP id F12036DE091C\r
6  for <notmuch@notmuchmail.org>; Wed, 25 May 2016 03:51:52 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.012\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.012 tagged_above=-999 required=5\r
12  tests=[AWL=-0.001, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
13  autolearn=disabled\r
14 Received: from arlo.cworth.org ([127.0.0.1])\r
15  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
16  with ESMTP id 1trr0uNZ4KOz for <notmuch@notmuchmail.org>;\r
17  Wed, 25 May 2016 03:51:45 -0700 (PDT)\r
18 Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
19  by arlo.cworth.org (Postfix) with ESMTPS id C69396DE015B\r
20  for <notmuch@notmuchmail.org>; Wed, 25 May 2016 03:51:36 -0700 (PDT)\r
21 Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
22  (envelope-from <bremner@tethera.net>)\r
23  id 1b5WPO-0004To-HT; Wed, 25 May 2016 06:51:26 -0400\r
24 Received: (nullmailer pid 23142 invoked by uid 1000);\r
25  Wed, 25 May 2016 10:51:31 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [Patch v6 4/7] CLI: optionally restore config data.\r
29 Date: Wed, 25 May 2016 07:51:23 -0300\r
30 Message-Id: <1464173486-23032-5-git-send-email-david@tethera.net>\r
31 X-Mailer: git-send-email 2.8.1\r
32 In-Reply-To: <1464173486-23032-1-git-send-email-david@tethera.net>\r
33 References: <1464173486-23032-1-git-send-email-david@tethera.net>\r
34 X-BeenThere: notmuch@notmuchmail.org\r
35 X-Mailman-Version: 2.1.20\r
36 Precedence: list\r
37 List-Id: "Use and development of the notmuch mail system."\r
38  <notmuch.notmuchmail.org>\r
39 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
40  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
42 List-Post: <mailto:notmuch@notmuchmail.org>\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
44 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
45  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
46 X-List-Received-Date: Wed, 25 May 2016 10:51:53 -0000\r
47 \r
48 The default to restore config data seems safest, especially since\r
49 currently we have no config data to mess up.\r
50 ---\r
51  doc/man1/notmuch-restore.rst | 18 +++++++++++++++\r
52  notmuch-restore.c            | 54 ++++++++++++++++++++++++++++++++++++++++++++\r
53  test/T590-libconfig.sh       | 11 +++++++++\r
54  3 files changed, 83 insertions(+)\r
55 \r
56 diff --git a/doc/man1/notmuch-restore.rst b/doc/man1/notmuch-restore.rst\r
57 index 362e262..87fa22e 100644\r
58 --- a/doc/man1/notmuch-restore.rst\r
59 +++ b/doc/man1/notmuch-restore.rst\r
60 @@ -50,6 +50,24 @@ Supported options for **restore** include\r
61              format, this heuristic, based the fact that batch-tag format\r
62              contains no parentheses, should be accurate.\r
63  \r
64 +    ``--include=(config|tags)``\r
65 +\r
66 +      Control what kind of metadata is restored.\r
67 +\r
68 +       **config**\r
69 +\r
70 +         Restore configuration data to the database. Each configuration line starts\r
71 +         with "#@ ", followed by a space seperated key-value pair.\r
72 +         Both key and value are hex encoded if needed.\r
73 +\r
74 +       **tags**\r
75 +\r
76 +         Output per-message metadata, namely tags. See *format* above\r
77 +         for more details.\r
78 +\r
79 +      The default is to restore both tags and configuration\r
80 +      information\r
81 +\r
82      ``--input=``\ <filename>\r
83          Read input from given file instead of stdin.\r
84  \r
85 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
86 index 9abc64f..8ad67b5 100644\r
87 --- a/notmuch-restore.c\r
88 +++ b/notmuch-restore.c\r
89 @@ -24,6 +24,39 @@\r
90  #include "string-util.h"\r
91  #include "zlib-extra.h"\r
92  \r
93 +static int\r
94 +process_config_line (notmuch_database_t *notmuch, const char* line)\r
95 +{\r
96 +    const char *key_p, *val_p;\r
97 +    char *key, *val;\r
98 +    size_t key_len,val_len;\r
99 +    const char *delim = " \t\n";\r
100 +    int ret = EXIT_FAILURE;\r
101 +\r
102 +    void *local = talloc_new(NULL);\r
103 +\r
104 +    key_p = strtok_len_c (line, delim, &key_len);\r
105 +    val_p = strtok_len_c (key_p+key_len, delim, &val_len);\r
106 +\r
107 +    key = talloc_strndup (local, key_p, key_len);\r
108 +    val = talloc_strndup (local, val_p, val_len);\r
109 +    if (hex_decode_inplace (key) != HEX_SUCCESS ||\r
110 +       hex_decode_inplace (val) != HEX_SUCCESS ) {\r
111 +       fprintf (stderr, "hex decoding failure on line %s\n", line);\r
112 +       goto DONE;\r
113 +    }\r
114 +\r
115 +    if (print_status_database ("notmuch restore", notmuch,\r
116 +                              notmuch_database_set_config (notmuch, key, val)))\r
117 +       goto DONE;\r
118 +\r
119 +    ret = EXIT_SUCCESS;\r
120 +\r
121 + DONE:\r
122 +    talloc_free (local);\r
123 +    return ret;\r
124 +}\r
125 +\r
126  static regex_t regex;\r
127  \r
128  /* Non-zero return indicates an error in retrieving the message,\r
129 @@ -137,6 +170,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
130  \r
131      int ret = 0;\r
132      int opt_index;\r
133 +    int include = 0;\r
134      int input_format = DUMP_FORMAT_AUTO;\r
135  \r
136      if (notmuch_database_open (notmuch_config_get_database_path (config),\r
137 @@ -152,6 +186,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
138                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
139                                   { "sup", DUMP_FORMAT_SUP },\r
140                                   { 0, 0 } } },\r
141 +       { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I',\r
142 +         (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },\r
143 +                                 { "tags", DUMP_INCLUDE_TAGS} } },\r
144 +\r
145         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
146         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },\r
147         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
148 @@ -167,6 +205,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
149      notmuch_process_shared_options (argv[0]);\r
150      notmuch_exit_if_unmatched_db_uuid (notmuch);\r
151  \r
152 +    if (include == 0) {\r
153 +       include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS;\r
154 +    }\r
155 +\r
156      name_for_error = input_file_name ? input_file_name : "stdin";\r
157  \r
158      if (! accumulate)\r
159 @@ -225,11 +267,23 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
160             ret = EXIT_FAILURE;\r
161             goto DONE;\r
162         }\r
163 +\r
164 +       if ((include & DUMP_INCLUDE_CONFIG) && line_len >= 2 && line[0] == '#' && line[1] == '@') {\r
165 +           ret = process_config_line(notmuch, line+2);\r
166 +           if (ret)\r
167 +               goto DONE;\r
168 +       }\r
169 +\r
170      } while ((line_len == 0) ||\r
171              (line[0] == '#') ||\r
172              /* the cast is safe because we checked about for line_len < 0 */\r
173              (strspn (line, " \t\n") == (unsigned)line_len));\r
174  \r
175 +    if (! (include & DUMP_INCLUDE_TAGS)) {\r
176 +       ret = EXIT_SUCCESS;\r
177 +       goto DONE;\r
178 +    }\r
179 +\r
180      char *p;\r
181      for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {\r
182         if (*p == '(')\r
183 diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
184 index 5ea5300..9c1e566 100755\r
185 --- a/test/T590-libconfig.sh\r
186 +++ b/test/T590-libconfig.sh\r
187 @@ -132,4 +132,15 @@ cat <<'EOF' >EXPECTED\r
188  EOF\r
189  test_expect_equal_file EXPECTED OUTPUT\r
190  \r
191 +test_begin_subtest "restore config"\r
192 +notmuch dump --include=config >EXPECTED\r
193 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
194 +{\r
195 +    RUN(notmuch_database_set_config (db, "testkey1", "mutatedvalue"));\r
196 +}\r
197 +EOF\r
198 +notmuch restore --include=config <EXPECTED\r
199 +notmuch dump --include=config >OUTPUT\r
200 +test_expect_equal_file EXPECTED OUTPUT\r
201 +\r
202  test_done\r
203 -- \r
204 2.8.1\r
205 \r