Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 71 / 916fa96299f21043bfda6ada114aa508fd1fd7
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 563916DE0355\r
6  for <notmuch@notmuchmail.org>; Tue, 22 Mar 2016 03:55: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.029\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.029 tagged_above=-999 required=5\r
12  tests=[AWL=-0.018, 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 XMtDtNBj4HpP for <notmuch@notmuchmail.org>;\r
17  Tue, 22 Mar 2016 03:55:41 -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 610686DE02AC\r
20  for <notmuch@notmuchmail.org>; Tue, 22 Mar 2016 03:55:21 -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 1aiJye-000152-Tp; Tue, 22 Mar 2016 06:55:56 -0400\r
24 Received: (nullmailer pid 14079 invoked by uid 1000);\r
25  Tue, 22 Mar 2016 10:55:11 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [PATCH 06/13] lib: provide config API\r
29 Date: Tue, 22 Mar 2016 07:54:47 -0300\r
30 Message-Id: <1458644094-13951-7-git-send-email-david@tethera.net>\r
31 X-Mailer: git-send-email 2.7.0\r
32 In-Reply-To: <1458644094-13951-1-git-send-email-david@tethera.net>\r
33 References: <1458644094-13951-1-git-send-email-david@tethera.net>\r
34 MIME-Version: 1.0\r
35 Content-Type: text/plain; charset=UTF-8\r
36 Content-Transfer-Encoding: 8bit\r
37 X-BeenThere: notmuch@notmuchmail.org\r
38 X-Mailman-Version: 2.1.20\r
39 Precedence: list\r
40 List-Id: "Use and development of the notmuch mail system."\r
41  <notmuch.notmuchmail.org>\r
42 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
43  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
44 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
45 List-Post: <mailto:notmuch@notmuchmail.org>\r
46 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
47 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
48  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
49 X-List-Received-Date: Tue, 22 Mar 2016 10:55:52 -0000\r
50 \r
51 This is a thin wrapper around the Xapian metadata API. The job of this\r
52 layer is to keep the config key value pairs from colliding with other\r
53 metadata by transparently prefixing the keys, along with the usual glue\r
54 to provide a C interface.\r
55 \r
56 The split of _get_config into two functions is to allow returning of the\r
57 return value with different memory ownership semantics.\r
58 ---\r
59  lib/Makefile.local     |  1 +\r
60  lib/config.cc          | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
61  lib/notmuch.h          | 20 +++++++++++\r
62  test/T590-libconfig.sh | 58 ++++++++++++++++++++++++++++++++\r
63  4 files changed, 169 insertions(+)\r
64  create mode 100644 lib/config.cc\r
65  create mode 100755 test/T590-libconfig.sh\r
66 \r
67 diff --git a/lib/Makefile.local b/lib/Makefile.local\r
68 index 4ad0158..96899bf 100644\r
69 --- a/lib/Makefile.local\r
70 +++ b/lib/Makefile.local\r
71 @@ -49,6 +49,7 @@ libnotmuch_cxx_srcs =         \\r
72         $(dir)/index.cc         \\r
73         $(dir)/message.cc       \\r
74         $(dir)/query.cc         \\r
75 +       $(dir)/config.cc        \\r
76         $(dir)/thread.cc\r
77  \r
78  libnotmuch_modules := $(libnotmuch_c_srcs:.c=.o) $(libnotmuch_cxx_srcs:.cc=.o)\r
79 diff --git a/lib/config.cc b/lib/config.cc\r
80 new file mode 100644\r
81 index 0000000..af00d6f\r
82 --- /dev/null\r
83 +++ b/lib/config.cc\r
84 @@ -0,0 +1,90 @@\r
85 +/* metadata.cc - API for database metadata\r
86 + *\r
87 + * Copyright © 2015 David Bremner\r
88 + *\r
89 + * This program is free software: you can redistribute it and/or modify\r
90 + * it under the terms of the GNU General Public License as published by\r
91 + * the Free Software Foundation, either version 3 of the License, or\r
92 + * (at your option) any later version.\r
93 + *\r
94 + * This program is distributed in the hope that it will be useful,\r
95 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
96 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
97 + * GNU General Public License for more details.\r
98 + *\r
99 + * You should have received a copy of the GNU General Public License\r
100 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
101 + *\r
102 + * Author: David Bremner <david@tethera.net>\r
103 + */\r
104 +\r
105 +#include "notmuch.h"\r
106 +#include "notmuch-private.h"\r
107 +#include "database-private.h"\r
108 +\r
109 +static const std::string CONFIG_PREFIX="C";\r
110 +\r
111 +notmuch_status_t\r
112 +notmuch_database_set_config (notmuch_database_t *notmuch,\r
113 +                            const char *key,\r
114 +                            const char *value)\r
115 +{\r
116 +    notmuch_status_t status;\r
117 +    Xapian::WritableDatabase *db;\r
118 +\r
119 +    status = _notmuch_database_ensure_writable (notmuch);\r
120 +    if (status)\r
121 +       return status;\r
122 +\r
123 +    try {\r
124 +       db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
125 +       db->set_metadata (CONFIG_PREFIX+key, value);\r
126 +    } catch (const Xapian::Error &error) {\r
127 +       status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
128 +       notmuch->exception_reported = TRUE;\r
129 +       if (! notmuch->exception_reported) {\r
130 +           _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",\r
131 +                                  error.get_msg().c_str());\r
132 +       }\r
133 +    }\r
134 +    return NOTMUCH_STATUS_SUCCESS;\r
135 +}\r
136 +\r
137 +static notmuch_status_t\r
138 +_metadata_value (notmuch_database_t *notmuch,\r
139 +                const char *key,\r
140 +                std::string &value)\r
141 +{\r
142 +    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
143 +\r
144 +    try {\r
145 +       value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX+key);\r
146 +    } catch (const Xapian::Error &error) {\r
147 +       status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
148 +       notmuch->exception_reported = TRUE;\r
149 +       if (! notmuch->exception_reported) {\r
150 +           _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",\r
151 +                                  error.get_msg().c_str());\r
152 +       }\r
153 +    }\r
154 +    return status;\r
155 +}\r
156 +\r
157 +notmuch_status_t\r
158 +notmuch_database_get_config (notmuch_database_t *notmuch,\r
159 +                            const char *key,\r
160 +                            char **value) {\r
161 +    std::string strval;\r
162 +    notmuch_status_t status;\r
163 +\r
164 +    if (!value)\r
165 +       return NOTMUCH_STATUS_NULL_POINTER;\r
166 +\r
167 +    status = _metadata_value (notmuch, key, strval);\r
168 +    if (status)\r
169 +       return status;\r
170 +\r
171 +    *value = strdup (strval.c_str ());\r
172 +\r
173 +    return NOTMUCH_STATUS_SUCCESS;\r
174 +}\r
175 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
176 index b29dd5f..e416fb4 100644\r
177 --- a/lib/notmuch.h\r
178 +++ b/lib/notmuch.h\r
179 @@ -1838,6 +1838,26 @@ notmuch_filenames_move_to_next (notmuch_filenames_t *filenames);\r
180  void\r
181  notmuch_filenames_destroy (notmuch_filenames_t *filenames);\r
182  \r
183 +\r
184 +/**\r
185 + * set config 'key' to 'value'\r
186 + *\r
187 + */\r
188 +notmuch_status_t\r
189 +notmuch_database_set_config (notmuch_database_t *db, const char *key, const char *value);\r
190 +\r
191 +/**\r
192 + * retrieve config item 'key', assign to  'value'\r
193 + *\r
194 + * keys which have not been previously set with n_d_set_config will\r
195 + * return an empty string.\r
196 + *\r
197 + * return value is allocated by malloc and should be freed by the\r
198 + * caller.\r
199 + */\r
200 +notmuch_status_t\r
201 +notmuch_database_get_config (notmuch_database_t *db, const char *key, char **value);\r
202 +\r
203  typedef enum {\r
204      NOTMUCH_OPTION_COMPACT = 1,\r
205      NOTMUCH_OPTION_FIELD_PROCESSOR = 2\r
206 diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
207 new file mode 100755\r
208 index 0000000..85e4497\r
209 --- /dev/null\r
210 +++ b/test/T590-libconfig.sh\r
211 @@ -0,0 +1,58 @@\r
212 +#!/usr/bin/env bash\r
213 +test_description="library config API"\r
214 +\r
215 +. ./test-lib.sh || exit 1\r
216 +\r
217 +add_email_corpus\r
218 +\r
219 +cat <<EOF > c_head\r
220 +#include <stdio.h>\r
221 +#include <string.h>\r
222 +#include <stdlib.h>\r
223 +#include <notmuch.h>\r
224 +\r
225 +void run(int line, notmuch_status_t ret)\r
226 +{\r
227 +   if (ret) {\r
228 +       fprintf (stderr, "line %d: %s\n", line, ret);\r
229 +       exit (1);\r
230 +   }\r
231 +}\r
232 +\r
233 +#define RUN(v)  run(__LINE__, v);\r
234 +\r
235 +int main (int argc, char** argv)\r
236 +{\r
237 +   notmuch_database_t *db;\r
238 +   char *val;\r
239 +   notmuch_status_t stat;\r
240 +\r
241 +   RUN(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));\r
242 +\r
243 +EOF\r
244 +\r
245 +cat <<EOF > c_tail\r
246 +   RUN(notmuch_database_destroy(db));\r
247 +}\r
248 +EOF\r
249 +\r
250 +test_begin_subtest "notmuch_database_{set,get}_config"\r
251 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
252 +{\r
253 +   RUN(notmuch_database_set_config (db, "testkey1", "testvalue1"));\r
254 +   RUN(notmuch_database_set_config (db, "testkey2", "testvalue2"));\r
255 +   RUN(notmuch_database_get_config (db, "testkey1", &val));\r
256 +   printf("testkey1 = %s\n", val);\r
257 +   RUN(notmuch_database_get_config (db, "testkey2", &val));\r
258 +   printf("testkey2 = %s\n", val);\r
259 +}\r
260 +EOF\r
261 +cat <<'EOF' >EXPECTED\r
262 +== stdout ==\r
263 +testkey1 = testvalue1\r
264 +testkey2 = testvalue2\r
265 +== stderr ==\r
266 +EOF\r
267 +test_expect_equal_file EXPECTED OUTPUT\r
268 +\r
269 +test_done\r
270 -- \r
271 2.7.0\r
272 \r