Re: [PATCH v4 09/16] index encrypted parts when asked.
[notmuch-archives.git] / e1 / 8fd55ab4e6c071643678996d7b0b191b847453
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 252146DE0946\r
6  for <notmuch@notmuchmail.org>; Sat, 30 Apr 2016 18:25:22 -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.015\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.015 tagged_above=-999 required=5\r
12  tests=[AWL=-0.004, 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 yMh7Jw2z4hZo for <notmuch@notmuchmail.org>;\r
17  Sat, 30 Apr 2016 18:25:13 -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 36E9B6DE035F\r
20  for <notmuch@notmuchmail.org>; Sat, 30 Apr 2016 18:24:53 -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 1awg7s-0006cS-8y; Sat, 30 Apr 2016 21:24:48 -0400\r
24 Received: (nullmailer pid 29951 invoked by uid 1000);\r
25  Sun, 01 May 2016 01:24:43 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [Patch v3 06/11] lib: config list iterators\r
29 Date: Sat, 30 Apr 2016 22:24:34 -0300\r
30 Message-Id: <1462065879-29860-7-git-send-email-david@tethera.net>\r
31 X-Mailer: git-send-email 2.8.0.rc3\r
32 In-Reply-To: <1462065879-29860-1-git-send-email-david@tethera.net>\r
33 References: <1462065879-29860-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: Sun, 01 May 2016 01:25:22 -0000\r
47 \r
48 Since xapian provides the ability to restrict the iterator to a given\r
49 prefix, we expose this ability to the user. Otherwise we mimic the other\r
50 iterator interfances in notmuch (e.g. tags.c).\r
51 ---\r
52  lib/config.cc          | 104 +++++++++++++++++++++++++++++++++++++++++++++++++\r
53  lib/notmuch.h          |  44 +++++++++++++++++++++\r
54  test/T590-libconfig.sh |  60 ++++++++++++++++++++++++++++\r
55  3 files changed, 208 insertions(+)\r
56 \r
57 diff --git a/lib/config.cc b/lib/config.cc\r
58 index af00d6f..e581f32 100644\r
59 --- a/lib/config.cc\r
60 +++ b/lib/config.cc\r
61 @@ -24,6 +24,19 @@\r
62  \r
63  static const std::string CONFIG_PREFIX="C";\r
64  \r
65 +struct _notmuch_config_list {\r
66 +    notmuch_database_t *notmuch;\r
67 +    Xapian::TermIterator *iterator;\r
68 +    char *current_key;\r
69 +    char *current_val;\r
70 +};\r
71 +\r
72 +static int\r
73 +_notmuch_config_list_destroy (notmuch_config_list_t *list) {\r
74 +    delete list->iterator;\r
75 +    return 0;\r
76 +}\r
77 +\r
78  notmuch_status_t\r
79  notmuch_database_set_config (notmuch_database_t *notmuch,\r
80                              const char *key,\r
81 @@ -88,3 +101,94 @@ notmuch_database_get_config (notmuch_database_t *notmuch,\r
82  \r
83      return NOTMUCH_STATUS_SUCCESS;\r
84  }\r
85 +\r
86 +notmuch_status_t\r
87 +notmuch_database_get_config_list (notmuch_database_t *notmuch,\r
88 +                                const char *prefix,\r
89 +                                notmuch_config_list_t **out)\r
90 +{\r
91 +    notmuch_config_list_t *list = NULL;\r
92 +    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
93 +\r
94 +    list = talloc (notmuch, notmuch_config_list_t);\r
95 +    if (!list) {\r
96 +       status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
97 +       goto DONE;\r
98 +    }\r
99 +\r
100 +    talloc_set_destructor (list, _notmuch_config_list_destroy);\r
101 +    list->iterator = new Xapian::TermIterator;\r
102 +    list->notmuch = notmuch;\r
103 +    list->current_key = NULL;\r
104 +    list->current_val = NULL;\r
105 +\r
106 +    try {\r
107 +\r
108 +       *list->iterator = notmuch->xapian_db->metadata_keys_begin (CONFIG_PREFIX + (prefix ? prefix : ""));\r
109 +\r
110 +    } catch (const Xapian::Error &error) {\r
111 +       _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",\r
112 +                              error.get_msg().c_str());\r
113 +       notmuch->exception_reported = TRUE;\r
114 +       status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
115 +    }\r
116 +\r
117 +    *out = list;\r
118 +\r
119 + DONE:\r
120 +    if (status && list)\r
121 +       talloc_free (list);\r
122 +\r
123 +    return status;\r
124 +}\r
125 +\r
126 +notmuch_bool_t\r
127 +notmuch_config_list_valid (notmuch_config_list_t *metadata)\r
128 +{\r
129 +    if (*(metadata->iterator) == metadata->notmuch->xapian_db->metadata_keys_end())\r
130 +       return FALSE;\r
131 +\r
132 +    return TRUE;\r
133 +}\r
134 +\r
135 +const char *\r
136 +notmuch_config_list_key (notmuch_config_list_t *list)\r
137 +{\r
138 +    if (list->current_key)\r
139 +       talloc_free (list->current_key);\r
140 +\r
141 +    list->current_key = talloc_strdup (list, (**(list->iterator)).c_str () + CONFIG_PREFIX.length ());\r
142 +\r
143 +    return  list->current_key;\r
144 +}\r
145 +\r
146 +const char *\r
147 +notmuch_config_list_value (notmuch_config_list_t *list)\r
148 +{\r
149 +    std::string strval;\r
150 +    notmuch_status_t status;\r
151 +    const char *key = notmuch_config_list_key (list);\r
152 +\r
153 +    /* TODO: better error reporting?? */\r
154 +    status = _metadata_value (list->notmuch, key, strval);\r
155 +    if (status)\r
156 +       return NULL;\r
157 +\r
158 +    if (list->current_val)\r
159 +       talloc_free(list->current_val);\r
160 +\r
161 +    list->current_val = talloc_strdup(list, strval.c_str ());\r
162 +    return list->current_val;\r
163 +}\r
164 +\r
165 +void\r
166 +notmuch_config_list_move_to_next (notmuch_config_list_t *list)\r
167 +{\r
168 +    (*(list->iterator))++;\r
169 +}\r
170 +\r
171 +void\r
172 +notmuch_config_list_destroy (notmuch_config_list_t *list)\r
173 +{\r
174 +    talloc_free (list);\r
175 +}\r
176 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
177 index c827e02..bd977c3 100644\r
178 --- a/lib/notmuch.h\r
179 +++ b/lib/notmuch.h\r
180 @@ -206,6 +206,7 @@ typedef struct _notmuch_message notmuch_message_t;\r
181  typedef struct _notmuch_tags notmuch_tags_t;\r
182  typedef struct _notmuch_directory notmuch_directory_t;\r
183  typedef struct _notmuch_filenames notmuch_filenames_t;\r
184 +typedef struct _notmuch_config_list notmuch_config_list_t;\r
185  #endif /* __DOXYGEN__ */\r
186  \r
187  /**\r
188 @@ -1859,6 +1860,49 @@ notmuch_status_t\r
189  notmuch_database_get_config (notmuch_database_t *db, const char *key, char **value);\r
190  \r
191  /**\r
192 + * Create an iterator for all config items with keys matching a given prefix\r
193 + */\r
194 +notmuch_status_t\r
195 +notmuch_database_get_config_list (notmuch_database_t *db, const char *prefix, notmuch_config_list_t **out);\r
196 +\r
197 +/**\r
198 + * Is 'config_list' iterator valid (i.e. _key, _value, _move_to_next can be called).\r
199 + */\r
200 +notmuch_bool_t\r
201 +notmuch_config_list_valid (notmuch_config_list_t *config_list);\r
202 +\r
203 +/**\r
204 + * return key for current config pair\r
205 + *\r
206 + * return value is owned by the iterator, and will be destroyed by the\r
207 + * next call to notmuch_config_list_key or notmuch_config_list_destroy.\r
208 + */\r
209 +const char *\r
210 +notmuch_config_list_key (notmuch_config_list_t *config_list);\r
211 +\r
212 +/**\r
213 + * return 'value' for current config pair\r
214 + *\r
215 + * return value is owned by the iterator, and will be destroyed by the\r
216 + * next call to notmuch_config_list_value or notmuch config_list_destroy\r
217 + */\r
218 +const char *\r
219 +notmuch_config_list_value (notmuch_config_list_t *config_list);\r
220 +\r
221 +\r
222 +/**\r
223 + * move 'config_list' iterator to the next pair\r
224 + */\r
225 +void\r
226 +notmuch_config_list_move_to_next (notmuch_config_list_t *config_list);\r
227 +\r
228 +/**\r
229 + * free any resources held by 'config_list'\r
230 + */\r
231 +void\r
232 +notmuch_config_list_destroy (notmuch_config_list_t *config_list);\r
233 +\r
234 +/**\r
235   * interrogate the library for compile time features\r
236   */\r
237  notmuch_bool_t\r
238 diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
239 index 85e4497..8ca6883 100755\r
240 --- a/test/T590-libconfig.sh\r
241 +++ b/test/T590-libconfig.sh\r
242 @@ -55,4 +55,64 @@ testkey2 = testvalue2\r
243  EOF\r
244  test_expect_equal_file EXPECTED OUTPUT\r
245  \r
246 +\r
247 +test_begin_subtest "notmuch_database_get_config_list: empty list"\r
248 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
249 +{\r
250 +   notmuch_config_list_t *list;\r
251 +   RUN(notmuch_database_get_config_list (db, "nonexistent", &list));\r
252 +   printf("valid = %d\n", notmuch_config_list_valid (list));\r
253 +   notmuch_config_list_destroy (list);\r
254 +}\r
255 +EOF\r
256 +cat <<'EOF' >EXPECTED\r
257 +== stdout ==\r
258 +valid = 0\r
259 +== stderr ==\r
260 +EOF\r
261 +test_expect_equal_file EXPECTED OUTPUT\r
262 +\r
263 +\r
264 +test_begin_subtest "notmuch_database_get_config_list: all pairs"\r
265 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
266 +{\r
267 +   notmuch_config_list_t *list;\r
268 +   RUN(notmuch_database_set_config (db, "zzzafter", "afterval"));\r
269 +   RUN(notmuch_database_set_config (db, "aaabefore", "beforeval"));\r
270 +   RUN(notmuch_database_get_config_list (db, "", &list));\r
271 +   for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {\r
272 +      printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));\r
273 +   }\r
274 +   notmuch_config_list_destroy (list);\r
275 +}\r
276 +EOF\r
277 +cat <<'EOF' >EXPECTED\r
278 +== stdout ==\r
279 +aaabefore beforeval\r
280 +testkey1 testvalue1\r
281 +testkey2 testvalue2\r
282 +zzzafter afterval\r
283 +== stderr ==\r
284 +EOF\r
285 +test_expect_equal_file EXPECTED OUTPUT\r
286 +\r
287 +test_begin_subtest "notmuch_database_get_config_list: one prefix"\r
288 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
289 +{\r
290 +   notmuch_config_list_t *list;\r
291 +   RUN(notmuch_database_get_config_list (db, "testkey", &list));\r
292 +   for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {\r
293 +      printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));\r
294 +   }\r
295 +   notmuch_config_list_destroy (list);\r
296 +}\r
297 +EOF\r
298 +cat <<'EOF' >EXPECTED\r
299 +== stdout ==\r
300 +testkey1 testvalue1\r
301 +testkey2 testvalue2\r
302 +== stderr ==\r
303 +EOF\r
304 +test_expect_equal_file EXPECTED OUTPUT\r
305 +\r
306  test_done\r
307 -- \r
308 2.8.0.rc3\r
309 \r