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