1 Return-Path: <bremner@tesseract.cs.unb.ca>
\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 olra.theworths.org (Postfix) with ESMTP id C528A431E84
\r
6 for <notmuch@notmuchmail.org>; Sat, 14 Mar 2015 10:05:24 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=2.438 tagged_above=-999 required=5
\r
12 tests=[DNS_FROM_AHBL_RHSBL=2.438] autolearn=disabled
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id 6SjYlH5m0XF8 for <notmuch@notmuchmail.org>;
\r
16 Sat, 14 Mar 2015 10:05:23 -0700 (PDT)
\r
17 Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net
\r
19 (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id 51DC4431E64
\r
22 for <notmuch@notmuchmail.org>; Sat, 14 Mar 2015 10:05:23 -0700 (PDT)
\r
23 Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim
\r
24 4.80) (envelope-from <bremner@tesseract.cs.unb.ca>)
\r
25 id 1YWpUP-000293-Ou; Sat, 14 Mar 2015 17:04:41 +0000
\r
26 Received: (nullmailer pid 4987 invoked by uid 1000); Sat, 14 Mar 2015
\r
28 From: David Bremner <david@tethera.net>
\r
29 To: notmuch@notmuchmail.org
\r
30 Subject: [Patch v4 4/9] lib: add "verbose" versions of
\r
31 notmuch_database_{open,create}
\r
32 Date: Sat, 14 Mar 2015 18:02:29 +0100
\r
33 Message-Id: <1426352554-4383-5-git-send-email-david@tethera.net>
\r
34 X-Mailer: git-send-email 2.1.4
\r
35 In-Reply-To: <1426352554-4383-1-git-send-email-david@tethera.net>
\r
36 References: <1426352554-4383-1-git-send-email-david@tethera.net>
\r
37 X-BeenThere: notmuch@notmuchmail.org
\r
38 X-Mailman-Version: 2.1.13
\r
40 List-Id: "Use and development of the notmuch mail system."
\r
41 <notmuch.notmuchmail.org>
\r
42 List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
48 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
49 X-List-Received-Date: Sat, 14 Mar 2015 17:05:24 -0000
\r
51 The compatibility wrapper ensures that clients calling
\r
52 notmuch_database_open will receive consistent output for now.
\r
54 The stdargs based infrastucture will be used in following commits for
\r
55 a more general logging mechanism.
\r
57 The changes to notmuch-{new,search} and test/symbol-test are just to
\r
58 make the test suite pass.
\r
60 lib/database.cc | 96 ++++++++++++++++++++++++++++++++++++++++++++---------
\r
61 lib/notmuch.h | 21 ++++++++++++
\r
62 notmuch-new.c | 8 +++--
\r
63 notmuch-search.c | 8 +++--
\r
64 test/symbol-test.cc | 6 +++-
\r
65 5 files changed, 119 insertions(+), 20 deletions(-)
\r
67 diff --git a/lib/database.cc b/lib/database.cc
\r
68 index 3974e2e..b774edc 100644
\r
69 --- a/lib/database.cc
\r
70 +++ b/lib/database.cc
\r
71 @@ -349,6 +349,35 @@ notmuch_status_to_string (notmuch_status_t status)
\r
75 +vlog_to_string (void *ctx,
\r
76 + char **status_string,
\r
77 + const char *format,
\r
80 + if (!status_string)
\r
83 + if (*status_string)
\r
84 + talloc_free (*status_string);
\r
86 + *status_string = talloc_vasprintf (ctx, format, va_args);
\r
90 +log_to_string (char **str,
\r
91 + const char *format,
\r
96 + va_start (va_args, format);
\r
98 + vlog_to_string (NULL, str, format, va_args);
\r
100 + va_end (va_args);
\r
104 find_doc_ids_for_term (notmuch_database_t *notmuch,
\r
106 Xapian::PostingIterator *begin,
\r
107 @@ -608,28 +637,37 @@ parse_references (void *ctx,
\r
109 notmuch_database_create (const char *path, notmuch_database_t **database)
\r
111 + return notmuch_database_create_verbose (path, database, NULL);
\r
115 +notmuch_database_create_verbose (const char *path,
\r
116 + notmuch_database_t **database,
\r
117 + char **status_string)
\r
119 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
\r
120 notmuch_database_t *notmuch = NULL;
\r
121 char *notmuch_path = NULL;
\r
122 + char *message = NULL;
\r
126 if (path == NULL) {
\r
127 - fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
\r
128 + log_to_string (&message, "Error: Cannot create a database for a NULL path.\n");
\r
129 status = NOTMUCH_STATUS_NULL_POINTER;
\r
133 err = stat (path, &st);
\r
135 - fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
\r
136 + log_to_string (&message, "Error: Cannot create database at %s: %s.\n",
\r
137 path, strerror (errno));
\r
138 status = NOTMUCH_STATUS_FILE_ERROR;
\r
142 if (! S_ISDIR (st.st_mode)) {
\r
143 - fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",
\r
144 + log_to_string (&message, "Error: Cannot create database at %s: Not a directory.\n",
\r
146 status = NOTMUCH_STATUS_FILE_ERROR;
\r
148 @@ -640,15 +678,15 @@ notmuch_database_create (const char *path, notmuch_database_t **database)
\r
149 err = mkdir (notmuch_path, 0755);
\r
152 - fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
\r
153 + log_to_string (&message, "Error: Cannot create directory %s: %s.\n",
\r
154 notmuch_path, strerror (errno));
\r
155 status = NOTMUCH_STATUS_FILE_ERROR;
\r
159 - status = notmuch_database_open (path,
\r
160 - NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
162 + status = notmuch_database_open_verbose (path,
\r
163 + NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
164 + ¬much, &message);
\r
168 @@ -667,6 +705,8 @@ notmuch_database_create (const char *path, notmuch_database_t **database)
\r
170 talloc_free (notmuch_path);
\r
173 + *status_string = strdup(message);
\r
175 *database = notmuch;
\r
177 @@ -767,37 +807,55 @@ notmuch_database_open (const char *path,
\r
178 notmuch_database_mode_t mode,
\r
179 notmuch_database_t **database)
\r
181 + char *status_string = NULL;
\r
182 + notmuch_status_t status;
\r
184 + status = notmuch_database_open_verbose(path, mode, database,
\r
187 + if (status_string) fputs(status_string, stderr);
\r
193 +notmuch_database_open_verbose (const char *path,
\r
194 + notmuch_database_mode_t mode,
\r
195 + notmuch_database_t **database,
\r
196 + char **status_string)
\r
198 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
\r
199 void *local = talloc_new (NULL);
\r
200 notmuch_database_t *notmuch = NULL;
\r
201 char *notmuch_path, *xapian_path, *incompat_features;
\r
202 + char *message = NULL;
\r
205 unsigned int i, version;
\r
206 static int initialized = 0;
\r
208 if (path == NULL) {
\r
209 - fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");
\r
210 + log_to_string (&message, "Error: Cannot open a database for a NULL path.\n");
\r
211 status = NOTMUCH_STATUS_NULL_POINTER;
\r
215 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
\r
216 - fprintf (stderr, "Out of memory\n");
\r
217 + log_to_string (&message, "Out of memory\n");
\r
218 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
222 err = stat (notmuch_path, &st);
\r
224 - fprintf (stderr, "Error opening database at %s: %s\n",
\r
225 + log_to_string (&message, "Error opening database at %s: %s\n",
\r
226 notmuch_path, strerror (errno));
\r
227 status = NOTMUCH_STATUS_FILE_ERROR;
\r
231 if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
\r
232 - fprintf (stderr, "Out of memory\n");
\r
233 + log_to_string (&message, "Out of memory\n");
\r
234 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
237 @@ -837,7 +895,7 @@ notmuch_database_open (const char *path,
\r
238 * means a dramatically incompatible change. */
\r
239 version = notmuch_database_get_version (notmuch);
\r
240 if (version > NOTMUCH_DATABASE_VERSION) {
\r
242 + log_to_string (&message,
\r
243 "Error: Notmuch database at %s\n"
\r
244 " has a newer database format version (%u) than supported by this\n"
\r
245 " version of notmuch (%u).\n",
\r
246 @@ -856,7 +914,7 @@ notmuch_database_open (const char *path,
\r
247 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
\r
248 &incompat_features);
\r
249 if (incompat_features) {
\r
251 + log_to_string (&message,
\r
252 "Error: Notmuch database at %s\n"
\r
253 " requires features (%s)\n"
\r
254 " not supported by this version of notmuch.\n",
\r
255 @@ -906,7 +964,7 @@ notmuch_database_open (const char *path,
\r
256 notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);
\r
258 } catch (const Xapian::Error &error) {
\r
259 - fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
\r
260 + log_to_string (&message, "A Xapian exception occurred opening database: %s\n",
\r
261 error.get_msg().c_str());
\r
262 notmuch_database_destroy (notmuch);
\r
264 @@ -916,6 +974,9 @@ notmuch_database_open (const char *path,
\r
266 talloc_free (local);
\r
268 + if (status_string && message)
\r
269 + *status_string = strdup (message);
\r
272 *database = notmuch;
\r
274 @@ -1039,13 +1100,18 @@ notmuch_database_compact (const char *path,
\r
275 notmuch_database_t *notmuch = NULL;
\r
276 struct stat statbuf;
\r
277 notmuch_bool_t keep_backup;
\r
278 + char *message = NULL;
\r
280 local = talloc_new (NULL);
\r
282 return NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
284 - ret = notmuch_database_open (path, NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much);
\r
285 + ret = notmuch_database_open_verbose (path,
\r
286 + NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
290 + if (status_cb) status_cb (message, closure);
\r
294 diff --git a/lib/notmuch.h b/lib/notmuch.h
\r
295 index f066245..c671d82 100644
\r
296 --- a/lib/notmuch.h
\r
297 +++ b/lib/notmuch.h
\r
298 @@ -230,6 +230,16 @@ notmuch_status_t
\r
299 notmuch_database_create (const char *path, notmuch_database_t **database);
\r
302 + * Like notmuch_database_create, except optionally return an error
\r
303 + * message. This message is allocated by malloc and should be freed by
\r
307 +notmuch_database_create_verbose (const char *path,
\r
308 + notmuch_database_t **database,
\r
309 + char **error_message);
\r
312 * Database open mode for notmuch_database_open.
\r
315 @@ -279,6 +289,17 @@ notmuch_status_t
\r
316 notmuch_database_open (const char *path,
\r
317 notmuch_database_mode_t mode,
\r
318 notmuch_database_t **database);
\r
320 + * Like notmuch_database_open, except optionally return an error
\r
321 + * message. This message is allocated by malloc and should be freed by
\r
326 +notmuch_database_open_verbose (const char *path,
\r
327 + notmuch_database_mode_t mode,
\r
328 + notmuch_database_t **database,
\r
329 + char **error_message);
\r
332 * Commit changes and close the given notmuch database.
\r
333 diff --git a/notmuch-new.c b/notmuch-new.c
\r
334 index ddf42c1..93b70bf 100644
\r
335 --- a/notmuch-new.c
\r
336 +++ b/notmuch-new.c
\r
337 @@ -985,9 +985,13 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
\r
338 return EXIT_FAILURE;
\r
339 add_files_state.total_files = count;
\r
341 - if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
343 + char *status_string = NULL;
\r
344 + if (notmuch_database_open_verbose (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
345 + ¬much, &status_string)) {
\r
346 + if (status_string) fputs (status_string, stderr);
\r
348 return EXIT_FAILURE;
\r
351 if (notmuch_database_needs_upgrade (notmuch)) {
\r
352 time_t now = time (NULL);
\r
353 diff --git a/notmuch-search.c b/notmuch-search.c
\r
354 index a591d45..d012af3 100644
\r
355 --- a/notmuch-search.c
\r
356 +++ b/notmuch-search.c
\r
357 @@ -545,6 +545,7 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar
\r
361 + char *status_string = NULL;
\r
363 switch (ctx->format_sel) {
\r
364 case NOTMUCH_FORMAT_TEXT:
\r
365 @@ -570,9 +571,12 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar
\r
367 notmuch_exit_if_unsupported_format ();
\r
369 - if (notmuch_database_open (notmuch_config_get_database_path (config),
\r
370 - NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))
\r
371 + if (notmuch_database_open_verbose (
\r
372 + notmuch_config_get_database_path (config),
\r
373 + NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
\r
374 + if (status_string) fputs (status_string, stderr);
\r
375 return EXIT_FAILURE;
\r
378 query_str = query_string_from_args (ctx->notmuch, argc, argv);
\r
379 if (query_str == NULL) {
\r
380 diff --git a/test/symbol-test.cc b/test/symbol-test.cc
\r
381 index 3e96c03..9f8eea7 100644
\r
382 --- a/test/symbol-test.cc
\r
383 +++ b/test/symbol-test.cc
\r
387 notmuch_database_t *notmuch;
\r
388 - notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much);
\r
389 + char *message = NULL;
\r
391 + if (notmuch_database_open_verbose ("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much, &message))
\r
392 + if (message) fputs (message, stderr);
\r
396 (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);
\r