From: David Bremner Date: Sat, 27 Dec 2014 22:05:32 +0000 (+0100) Subject: [PATCH v2 1/6] lib: add "verbose" versions of notmuch_database_{open,create} X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1de47a5851a3db7305837c1b37ba60daae19bccf;p=notmuch-archives.git [PATCH v2 1/6] lib: add "verbose" versions of notmuch_database_{open,create} --- diff --git a/ca/cb9ae4fa383bc15085d81abcd7f18f6a712bb9 b/ca/cb9ae4fa383bc15085d81abcd7f18f6a712bb9 new file mode 100644 index 000000000..c8c008ec9 --- /dev/null +++ b/ca/cb9ae4fa383bc15085d81abcd7f18f6a712bb9 @@ -0,0 +1,360 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 8179F429E25 + for ; Sat, 27 Dec 2014 14:06:11 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0 +X-Spam-Level: +X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none] + autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id oop9y-KI+yfa for ; + Sat, 27 Dec 2014 14:06:09 -0800 (PST) +Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155]) + (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 93CCF431FBC + for ; Sat, 27 Dec 2014 14:06:09 -0800 (PST) +Received: from remotemail by yantan.tethera.net with local (Exim 4.80) + (envelope-from ) + id 1Y4zUv-0000E8-9f; Sat, 27 Dec 2014 18:06:09 -0400 +Received: (nullmailer pid 1473 invoked by uid 1000); Sat, 27 Dec 2014 + 22:05:48 -0000 +From: David Bremner +To: notmuch@notmuchmail.org +Subject: [PATCH v2 1/6] lib: add "verbose" versions of + notmuch_database_{open,create} +Date: Sat, 27 Dec 2014 23:05:32 +0100 +Message-Id: <1419717937-1108-2-git-send-email-david@tethera.net> +X-Mailer: git-send-email 2.1.3 +In-Reply-To: <1419717937-1108-1-git-send-email-david@tethera.net> +References: <1419717937-1108-1-git-send-email-david@tethera.net> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sat, 27 Dec 2014 22:06:11 -0000 + +This has the the non-trivial side effect silencing all output to +stderror from these two routines. + +The stdargs based infrastucture will be used in following commits for +a more general logging mechanism. + +The changes to notmuch-{new,search} and test/symbol-test are just to +make the test suite pass. The fact that no other changes are needed is +probably a sign of weakness in the test suite. +--- + lib/database.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++-------- + lib/notmuch.h | 21 +++++++++++++++ + notmuch-new.c | 8 ++++-- + notmuch-search.c | 8 ++++-- + test/symbol-test.cc | 6 ++++- + 5 files changed, 102 insertions(+), 16 deletions(-) + +diff --git a/lib/database.cc b/lib/database.cc +index 3601f9d..06e6fc5 100644 +--- a/lib/database.cc ++++ b/lib/database.cc +@@ -343,6 +343,35 @@ notmuch_status_to_string (notmuch_status_t status) + } + + static void ++vlog_to_string (void *ctx, ++ char **status_string, ++ const char *format, ++ va_list va_args) ++{ ++ if (!status_string) ++ return; ++ ++ if (*status_string) ++ talloc_free (*status_string); ++ ++ *status_string = talloc_vasprintf (ctx, format, va_args); ++} ++ ++static void ++log_to_string (char **str, ++ const char *format, ++ ...) ++{ ++ va_list va_args; ++ ++ va_start (va_args, format); ++ ++ vlog_to_string (NULL, str, format, va_args); ++ ++ va_end (va_args); ++} ++ ++static void + find_doc_ids_for_term (notmuch_database_t *notmuch, + const char *term, + Xapian::PostingIterator *begin, +@@ -602,28 +631,37 @@ parse_references (void *ctx, + notmuch_status_t + notmuch_database_create (const char *path, notmuch_database_t **database) + { ++ return notmuch_database_create_verbose (path, database, NULL); ++} ++ ++notmuch_status_t ++notmuch_database_create_verbose (const char *path, ++ notmuch_database_t **database, ++ char **status_string) ++{ + notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; + notmuch_database_t *notmuch = NULL; + char *notmuch_path = NULL; ++ char *message = NULL; + struct stat st; + int err; + + if (path == NULL) { +- fprintf (stderr, "Error: Cannot create a database for a NULL path.\n"); ++ log_to_string (&message, "Error: Cannot create a database for a NULL path.\n"); + status = NOTMUCH_STATUS_NULL_POINTER; + goto DONE; + } + + err = stat (path, &st); + if (err) { +- fprintf (stderr, "Error: Cannot create database at %s: %s.\n", ++ log_to_string (&message, "Error: Cannot create database at %s: %s.\n", + path, strerror (errno)); + status = NOTMUCH_STATUS_FILE_ERROR; + goto DONE; + } + + if (! S_ISDIR (st.st_mode)) { +- fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n", ++ log_to_string (&message, "Error: Cannot create database at %s: Not a directory.\n", + path); + status = NOTMUCH_STATUS_FILE_ERROR; + goto DONE; +@@ -634,7 +672,7 @@ notmuch_database_create (const char *path, notmuch_database_t **database) + err = mkdir (notmuch_path, 0755); + + if (err) { +- fprintf (stderr, "Error: Cannot create directory %s: %s.\n", ++ log_to_string (&message, "Error: Cannot create directory %s: %s.\n", + notmuch_path, strerror (errno)); + status = NOTMUCH_STATUS_FILE_ERROR; + goto DONE; +@@ -660,6 +698,8 @@ notmuch_database_create (const char *path, notmuch_database_t **database) + if (notmuch_path) + talloc_free (notmuch_path); + ++ if (message) ++ *status_string = strdup(message); + if (database) + *database = notmuch; + else +@@ -760,37 +800,47 @@ notmuch_database_open (const char *path, + notmuch_database_mode_t mode, + notmuch_database_t **database) + { ++ return notmuch_database_open_verbose(path, mode, database, NULL); ++} ++ ++notmuch_status_t ++notmuch_database_open_verbose (const char *path, ++ notmuch_database_mode_t mode, ++ notmuch_database_t **database, ++ char **status_string) ++{ + notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; + void *local = talloc_new (NULL); + notmuch_database_t *notmuch = NULL; + char *notmuch_path, *xapian_path, *incompat_features; ++ char *message = NULL; + struct stat st; + int err; + unsigned int i, version; + static int initialized = 0; + + if (path == NULL) { +- fprintf (stderr, "Error: Cannot open a database for a NULL path.\n"); ++ log_to_string (&message, "Error: Cannot open a database for a NULL path.\n"); + status = NOTMUCH_STATUS_NULL_POINTER; + goto DONE; + } + + if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) { +- fprintf (stderr, "Out of memory\n"); ++ log_to_string (&message, "Out of memory\n"); + status = NOTMUCH_STATUS_OUT_OF_MEMORY; + goto DONE; + } + + err = stat (notmuch_path, &st); + if (err) { +- fprintf (stderr, "Error opening database at %s: %s\n", ++ log_to_string (&message, "Error opening database at %s: %s\n", + notmuch_path, strerror (errno)); + status = NOTMUCH_STATUS_FILE_ERROR; + goto DONE; + } + + if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) { +- fprintf (stderr, "Out of memory\n"); ++ log_to_string (&message, "Out of memory\n"); + status = NOTMUCH_STATUS_OUT_OF_MEMORY; + goto DONE; + } +@@ -830,7 +880,7 @@ notmuch_database_open (const char *path, + * means a dramatically incompatible change. */ + version = notmuch_database_get_version (notmuch); + if (version > NOTMUCH_DATABASE_VERSION) { +- fprintf (stderr, ++ log_to_string (&message, + "Error: Notmuch database at %s\n" + " has a newer database format version (%u) than supported by this\n" + " version of notmuch (%u).\n", +@@ -849,7 +899,7 @@ notmuch_database_open (const char *path, + version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r', + &incompat_features); + if (incompat_features) { +- fprintf (stderr, ++ log_to_string (&message, + "Error: Notmuch database at %s\n" + " requires features (%s)\n" + " not supported by this version of notmuch.\n", +@@ -899,7 +949,7 @@ notmuch_database_open (const char *path, + notmuch->query_parser->add_prefix (prefix->name, prefix->prefix); + } + } catch (const Xapian::Error &error) { +- fprintf (stderr, "A Xapian exception occurred opening database: %s\n", ++ log_to_string (&message, "A Xapian exception occurred opening database: %s\n", + error.get_msg().c_str()); + notmuch_database_destroy (notmuch); + notmuch = NULL; +@@ -909,6 +959,9 @@ notmuch_database_open (const char *path, + DONE: + talloc_free (local); + ++ if (status_string && message) ++ *status_string = strdup (message); ++ + if (database) + *database = notmuch; + else +diff --git a/lib/notmuch.h b/lib/notmuch.h +index 220839b..0dfac8f 100644 +--- a/lib/notmuch.h ++++ b/lib/notmuch.h +@@ -230,6 +230,16 @@ notmuch_status_t + notmuch_database_create (const char *path, notmuch_database_t **database); + + /** ++ * Like notmuch_database_create, except optionally return an error ++ * message. This message is allocated by malloc and should be freed by ++ * the caller. ++ */ ++notmuch_status_t ++notmuch_database_create_verbose (const char *path, ++ notmuch_database_t **database, ++ char **error_message); ++ ++/** + * Database open mode for notmuch_database_open. + */ + typedef enum { +@@ -279,6 +289,17 @@ notmuch_status_t + notmuch_database_open (const char *path, + notmuch_database_mode_t mode, + notmuch_database_t **database); ++/** ++ * Like notmuch_database_open, except optionally return an error ++ * message. This message is allocated by malloc and should be freed by ++ * the caller. ++ */ ++ ++notmuch_status_t ++notmuch_database_open_verbose (const char *path, ++ notmuch_database_mode_t mode, ++ notmuch_database_t **database, ++ char **error_message); + + /** + * Commit changes and close the given notmuch database. +diff --git a/notmuch-new.c b/notmuch-new.c +index ddf42c1..93b70bf 100644 +--- a/notmuch-new.c ++++ b/notmuch-new.c +@@ -985,9 +985,13 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]) + return EXIT_FAILURE; + add_files_state.total_files = count; + } else { +- if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE, +- ¬much)) ++ char *status_string = NULL; ++ if (notmuch_database_open_verbose (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE, ++ ¬much, &status_string)) { ++ if (status_string) fputs (status_string, stderr); ++ + return EXIT_FAILURE; ++ } + + if (notmuch_database_needs_upgrade (notmuch)) { + time_t now = time (NULL); +diff --git a/notmuch-search.c b/notmuch-search.c +index 14b9f01..250e567 100644 +--- a/notmuch-search.c ++++ b/notmuch-search.c +@@ -545,6 +545,7 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar + { + char *query_str; + unsigned int i; ++ char *status_string = NULL; + + switch (ctx->format_sel) { + case NOTMUCH_FORMAT_TEXT: +@@ -570,9 +571,12 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar + + notmuch_exit_if_unsupported_format (); + +- if (notmuch_database_open (notmuch_config_get_database_path (config), +- NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch)) ++ if (notmuch_database_open_verbose ( ++ notmuch_config_get_database_path (config), ++ NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) { ++ if (status_string) fputs (status_string, stderr); + return EXIT_FAILURE; ++ } + + query_str = query_string_from_args (ctx->notmuch, argc, argv); + if (query_str == NULL) { +diff --git a/test/symbol-test.cc b/test/symbol-test.cc +index 3e96c03..9f8eea7 100644 +--- a/test/symbol-test.cc ++++ b/test/symbol-test.cc +@@ -5,7 +5,11 @@ + + int main() { + notmuch_database_t *notmuch; +- notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much); ++ char *message = NULL; ++ ++ if (notmuch_database_open_verbose ("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much, &message)) ++ if (message) fputs (message, stderr); ++ + + try { + (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN); +-- +2.1.3 +