[Patch v4 05/12] lib: provide config API
[notmuch-archives.git] / 52 / a9ad5e72d4d57be7bb8c2217485fc8c8f64576
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 B19B66DE0355\r
6  for <notmuch@notmuchmail.org>; Sat,  7 May 2016 17:06:49 -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.013\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.013 tagged_above=-999 required=5\r
12  tests=[AWL=-0.002, 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 7cS0fYWNCnmQ for <notmuch@notmuchmail.org>;\r
17  Sat,  7 May 2016 17:06: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 430066DE092B\r
20  for <notmuch@notmuchmail.org>; Sat,  7 May 2016 17:05:07 -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 1azCDV-00015T-0r; Sat, 07 May 2016 20:05:01 -0400\r
24 Received: (nullmailer pid 17191 invoked by uid 1000);\r
25  Sun, 08 May 2016 00:04:53 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [Patch v4 05/12] lib: provide config API\r
29 Date: Sat,  7 May 2016 21:04:42 -0300\r
30 Message-Id: <1462665889-17121-6-git-send-email-david@tethera.net>\r
31 X-Mailer: git-send-email 2.8.1\r
32 In-Reply-To: <1462665889-17121-1-git-send-email-david@tethera.net>\r
33 References: <1462665889-17121-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: Sun, 08 May 2016 00:06:49 -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          | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
61  lib/notmuch.h          | 20 +++++++++++\r
62  test/T590-libconfig.sh | 58 ++++++++++++++++++++++++++++++++\r
63  4 files changed, 170 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 36c3924..76b57cb 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..ce8fb31\r
82 --- /dev/null\r
83 +++ b/lib/config.cc\r
84 @@ -0,0 +1,91 @@\r
85 +/* config.cc - API for database metadata\r
86 + *\r
87 + * Copyright © 2016 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 +{\r
162 +    std::string strval;\r
163 +    notmuch_status_t status;\r
164 +\r
165 +    if (! value)\r
166 +       return NOTMUCH_STATUS_NULL_POINTER;\r
167 +\r
168 +    status = _metadata_value (notmuch, key, strval);\r
169 +    if (status)\r
170 +       return status;\r
171 +\r
172 +    *value = strdup (strval.c_str ());\r
173 +\r
174 +    return NOTMUCH_STATUS_SUCCESS;\r
175 +}\r
176 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
177 index 3a092ef..c827e02 100644\r
178 --- a/lib/notmuch.h\r
179 +++ b/lib/notmuch.h\r
180 @@ -1838,6 +1838,26 @@ notmuch_filenames_move_to_next (notmuch_filenames_t *filenames);\r
181  void\r
182  notmuch_filenames_destroy (notmuch_filenames_t *filenames);\r
183  \r
184 +\r
185 +/**\r
186 + * set config 'key' to 'value'\r
187 + *\r
188 + */\r
189 +notmuch_status_t\r
190 +notmuch_database_set_config (notmuch_database_t *db, const char *key, const char *value);\r
191 +\r
192 +/**\r
193 + * retrieve config item 'key', assign to  'value'\r
194 + *\r
195 + * keys which have not been previously set with n_d_set_config will\r
196 + * return an empty string.\r
197 + *\r
198 + * return value is allocated by malloc and should be freed by the\r
199 + * caller.\r
200 + */\r
201 +notmuch_status_t\r
202 +notmuch_database_get_config (notmuch_database_t *db, const char *key, char **value);\r
203 +\r
204  /**\r
205   * interrogate the library for compile time features\r
206   */\r
207 diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
208 new file mode 100755\r
209 index 0000000..85e4497\r
210 --- /dev/null\r
211 +++ b/test/T590-libconfig.sh\r
212 @@ -0,0 +1,58 @@\r
213 +#!/usr/bin/env bash\r
214 +test_description="library config API"\r
215 +\r
216 +. ./test-lib.sh || exit 1\r
217 +\r
218 +add_email_corpus\r
219 +\r
220 +cat <<EOF > c_head\r
221 +#include <stdio.h>\r
222 +#include <string.h>\r
223 +#include <stdlib.h>\r
224 +#include <notmuch.h>\r
225 +\r
226 +void run(int line, notmuch_status_t ret)\r
227 +{\r
228 +   if (ret) {\r
229 +       fprintf (stderr, "line %d: %s\n", line, ret);\r
230 +       exit (1);\r
231 +   }\r
232 +}\r
233 +\r
234 +#define RUN(v)  run(__LINE__, v);\r
235 +\r
236 +int main (int argc, char** argv)\r
237 +{\r
238 +   notmuch_database_t *db;\r
239 +   char *val;\r
240 +   notmuch_status_t stat;\r
241 +\r
242 +   RUN(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));\r
243 +\r
244 +EOF\r
245 +\r
246 +cat <<EOF > c_tail\r
247 +   RUN(notmuch_database_destroy(db));\r
248 +}\r
249 +EOF\r
250 +\r
251 +test_begin_subtest "notmuch_database_{set,get}_config"\r
252 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
253 +{\r
254 +   RUN(notmuch_database_set_config (db, "testkey1", "testvalue1"));\r
255 +   RUN(notmuch_database_set_config (db, "testkey2", "testvalue2"));\r
256 +   RUN(notmuch_database_get_config (db, "testkey1", &val));\r
257 +   printf("testkey1 = %s\n", val);\r
258 +   RUN(notmuch_database_get_config (db, "testkey2", &val));\r
259 +   printf("testkey2 = %s\n", val);\r
260 +}\r
261 +EOF\r
262 +cat <<'EOF' >EXPECTED\r
263 +== stdout ==\r
264 +testkey1 = testvalue1\r
265 +testkey2 = testvalue2\r
266 +== stderr ==\r
267 +EOF\r
268 +test_expect_equal_file EXPECTED OUTPUT\r
269 +\r
270 +test_done\r
271 -- \r
272 2.8.1\r
273 \r