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 C6BAD429E49
\r
6 for <notmuch@notmuchmail.org>; Sun, 28 Dec 2014 09:38:11 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]
\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 W1fNrezklmQx for <notmuch@notmuchmail.org>;
\r
16 Sun, 28 Dec 2014 09:38:10 -0800 (PST)
\r
17 Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])
\r
18 (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))
\r
19 (No client certificate requested)
\r
20 by olra.theworths.org (Postfix) with ESMTPS id 5ECA3429E3E
\r
21 for <notmuch@notmuchmail.org>; Sun, 28 Dec 2014 09:38:09 -0800 (PST)
\r
22 Received: from remotemail by yantan.tethera.net with local (Exim 4.80)
\r
23 (envelope-from <bremner@tesseract.cs.unb.ca>)
\r
24 id 1Y5Hn6-0002gB-Tr; Sun, 28 Dec 2014 13:38:08 -0400
\r
25 Received: (nullmailer pid 11494 invoked by uid 1000); Sun, 28 Dec 2014
\r
27 From: David Bremner <david@tethera.net>
\r
28 To: notmuch@notmuchmail.org
\r
29 Subject: [Patch v3 1/6] lib: add "verbose" versions of
\r
30 notmuch_database_{open,create}
\r
31 Date: Sun, 28 Dec 2014 18:33:45 +0100
\r
32 Message-Id: <1419788030-10567-2-git-send-email-david@tethera.net>
\r
33 X-Mailer: git-send-email 2.1.3
\r
34 In-Reply-To: <1419788030-10567-1-git-send-email-david@tethera.net>
\r
35 References: <1419788030-10567-1-git-send-email-david@tethera.net>
\r
36 X-BeenThere: notmuch@notmuchmail.org
\r
37 X-Mailman-Version: 2.1.13
\r
39 List-Id: "Use and development of the notmuch mail system."
\r
40 <notmuch.notmuchmail.org>
\r
41 List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
47 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
48 X-List-Received-Date: Sun, 28 Dec 2014 17:38:12 -0000
\r
50 This has the the non-trivial side effect silencing all output to
\r
51 stderror from these two routines.
\r
53 The stdargs based infrastucture will be used in following commits for
\r
54 a more general logging mechanism.
\r
56 The changes to notmuch-{new,search} and test/symbol-test are just to
\r
57 make the test suite pass. The fact that no other changes are needed is
\r
58 probably a sign of weakness in the test suite.
\r
60 lib/database.cc | 88 ++++++++++++++++++++++++++++++++++++++++++++---------
\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, 111 insertions(+), 20 deletions(-)
\r
67 diff --git a/lib/database.cc b/lib/database.cc
\r
68 index 3601f9d..4f4e871 100644
\r
69 --- a/lib/database.cc
\r
70 +++ b/lib/database.cc
\r
71 @@ -343,6 +343,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 @@ -602,28 +631,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 @@ -634,15 +672,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 @@ -660,6 +698,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 @@ -760,37 +800,47 @@ notmuch_database_open (const char *path,
\r
178 notmuch_database_mode_t mode,
\r
179 notmuch_database_t **database)
\r
181 + return notmuch_database_open_verbose(path, mode, database, NULL);
\r
185 +notmuch_database_open_verbose (const char *path,
\r
186 + notmuch_database_mode_t mode,
\r
187 + notmuch_database_t **database,
\r
188 + char **status_string)
\r
190 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
\r
191 void *local = talloc_new (NULL);
\r
192 notmuch_database_t *notmuch = NULL;
\r
193 char *notmuch_path, *xapian_path, *incompat_features;
\r
194 + char *message = NULL;
\r
197 unsigned int i, version;
\r
198 static int initialized = 0;
\r
200 if (path == NULL) {
\r
201 - fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");
\r
202 + log_to_string (&message, "Error: Cannot open a database for a NULL path.\n");
\r
203 status = NOTMUCH_STATUS_NULL_POINTER;
\r
207 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
\r
208 - fprintf (stderr, "Out of memory\n");
\r
209 + log_to_string (&message, "Out of memory\n");
\r
210 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
214 err = stat (notmuch_path, &st);
\r
216 - fprintf (stderr, "Error opening database at %s: %s\n",
\r
217 + log_to_string (&message, "Error opening database at %s: %s\n",
\r
218 notmuch_path, strerror (errno));
\r
219 status = NOTMUCH_STATUS_FILE_ERROR;
\r
223 if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
\r
224 - fprintf (stderr, "Out of memory\n");
\r
225 + log_to_string (&message, "Out of memory\n");
\r
226 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
229 @@ -830,7 +880,7 @@ notmuch_database_open (const char *path,
\r
230 * means a dramatically incompatible change. */
\r
231 version = notmuch_database_get_version (notmuch);
\r
232 if (version > NOTMUCH_DATABASE_VERSION) {
\r
234 + log_to_string (&message,
\r
235 "Error: Notmuch database at %s\n"
\r
236 " has a newer database format version (%u) than supported by this\n"
\r
237 " version of notmuch (%u).\n",
\r
238 @@ -849,7 +899,7 @@ notmuch_database_open (const char *path,
\r
239 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
\r
240 &incompat_features);
\r
241 if (incompat_features) {
\r
243 + log_to_string (&message,
\r
244 "Error: Notmuch database at %s\n"
\r
245 " requires features (%s)\n"
\r
246 " not supported by this version of notmuch.\n",
\r
247 @@ -899,7 +949,7 @@ notmuch_database_open (const char *path,
\r
248 notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);
\r
250 } catch (const Xapian::Error &error) {
\r
251 - fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
\r
252 + log_to_string (&message, "A Xapian exception occurred opening database: %s\n",
\r
253 error.get_msg().c_str());
\r
254 notmuch_database_destroy (notmuch);
\r
256 @@ -909,6 +959,9 @@ notmuch_database_open (const char *path,
\r
258 talloc_free (local);
\r
260 + if (status_string && message)
\r
261 + *status_string = strdup (message);
\r
264 *database = notmuch;
\r
266 @@ -1032,13 +1085,18 @@ notmuch_database_compact (const char *path,
\r
267 notmuch_database_t *notmuch = NULL;
\r
268 struct stat statbuf;
\r
269 notmuch_bool_t keep_backup;
\r
270 + char *message = NULL;
\r
272 local = talloc_new (NULL);
\r
274 return NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
276 - ret = notmuch_database_open (path, NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much);
\r
277 + ret = notmuch_database_open_verbose (path,
\r
278 + NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
282 + if (status_cb) status_cb (message, closure);
\r
286 diff --git a/lib/notmuch.h b/lib/notmuch.h
\r
287 index 220839b..0dfac8f 100644
\r
288 --- a/lib/notmuch.h
\r
289 +++ b/lib/notmuch.h
\r
290 @@ -230,6 +230,16 @@ notmuch_status_t
\r
291 notmuch_database_create (const char *path, notmuch_database_t **database);
\r
294 + * Like notmuch_database_create, except optionally return an error
\r
295 + * message. This message is allocated by malloc and should be freed by
\r
299 +notmuch_database_create_verbose (const char *path,
\r
300 + notmuch_database_t **database,
\r
301 + char **error_message);
\r
304 * Database open mode for notmuch_database_open.
\r
307 @@ -279,6 +289,17 @@ notmuch_status_t
\r
308 notmuch_database_open (const char *path,
\r
309 notmuch_database_mode_t mode,
\r
310 notmuch_database_t **database);
\r
312 + * Like notmuch_database_open, except optionally return an error
\r
313 + * message. This message is allocated by malloc and should be freed by
\r
318 +notmuch_database_open_verbose (const char *path,
\r
319 + notmuch_database_mode_t mode,
\r
320 + notmuch_database_t **database,
\r
321 + char **error_message);
\r
324 * Commit changes and close the given notmuch database.
\r
325 diff --git a/notmuch-new.c b/notmuch-new.c
\r
326 index ddf42c1..93b70bf 100644
\r
327 --- a/notmuch-new.c
\r
328 +++ b/notmuch-new.c
\r
329 @@ -985,9 +985,13 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
\r
330 return EXIT_FAILURE;
\r
331 add_files_state.total_files = count;
\r
333 - if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
335 + char *status_string = NULL;
\r
336 + if (notmuch_database_open_verbose (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
337 + ¬much, &status_string)) {
\r
338 + if (status_string) fputs (status_string, stderr);
\r
340 return EXIT_FAILURE;
\r
343 if (notmuch_database_needs_upgrade (notmuch)) {
\r
344 time_t now = time (NULL);
\r
345 diff --git a/notmuch-search.c b/notmuch-search.c
\r
346 index 14b9f01..250e567 100644
\r
347 --- a/notmuch-search.c
\r
348 +++ b/notmuch-search.c
\r
349 @@ -545,6 +545,7 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar
\r
353 + char *status_string = NULL;
\r
355 switch (ctx->format_sel) {
\r
356 case NOTMUCH_FORMAT_TEXT:
\r
357 @@ -570,9 +571,12 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar
\r
359 notmuch_exit_if_unsupported_format ();
\r
361 - if (notmuch_database_open (notmuch_config_get_database_path (config),
\r
362 - NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))
\r
363 + if (notmuch_database_open_verbose (
\r
364 + notmuch_config_get_database_path (config),
\r
365 + NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
\r
366 + if (status_string) fputs (status_string, stderr);
\r
367 return EXIT_FAILURE;
\r
370 query_str = query_string_from_args (ctx->notmuch, argc, argv);
\r
371 if (query_str == NULL) {
\r
372 diff --git a/test/symbol-test.cc b/test/symbol-test.cc
\r
373 index 3e96c03..9f8eea7 100644
\r
374 --- a/test/symbol-test.cc
\r
375 +++ b/test/symbol-test.cc
\r
379 notmuch_database_t *notmuch;
\r
380 - notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much);
\r
381 + char *message = NULL;
\r
383 + if (notmuch_database_open_verbose ("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much, &message))
\r
384 + if (message) fputs (message, stderr);
\r
388 (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);
\r