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 0ACA7431FD4 for ; Tue, 23 Feb 2010 18:28:19 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -1.577 X-Spam-Level: X-Spam-Status: No, score=-1.577 tagged_above=-999 required=5 tests=[AWL=1.022, BAYES_00=-2.599] autolearn=ham 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 rZEcWSLUrQ0x for ; Tue, 23 Feb 2010 18:28:17 -0800 (PST) Received: from dmz-mailsec-scanner-3.mit.edu (DMZ-MAILSEC-SCANNER-3.MIT.EDU [18.9.25.14]) by olra.theworths.org (Postfix) with ESMTP id 3B7AD431FAE for ; Tue, 23 Feb 2010 18:28:17 -0800 (PST) X-AuditID: 1209190e-b7ca7ae00000096e-91-4b848d9228a2 Received: from mailhub-auth-2.mit.edu (MAILHUB-AUTH-2.MIT.EDU [18.7.62.36]) by dmz-mailsec-scanner-3.mit.edu (Symantec Brightmail Gateway) with SMTP id B7.DF.02414.29D848B4; Tue, 23 Feb 2010 21:23:14 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id o1O2NE5o008874; Tue, 23 Feb 2010 21:23:14 -0500 Received: from localhost.localdomain (ET-NINETY-THREE.MIT.EDU [18.208.1.93]) (authenticated bits=0) (User authenticated as davidben@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id o1O2NTit021532 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 23 Feb 2010 21:23:34 -0500 (EST) From: David Benjamin To: notmuch@notmuchmail.org Date: Tue, 23 Feb 2010 21:23:01 -0500 Message-Id: <1266978183-19698-3-git-send-email-davidben@mit.edu> X-Mailer: git-send-email 1.7.0.18.g39b3 In-Reply-To: <1266978183-19698-1-git-send-email-davidben@mit.edu> References: <1266978183-19698-1-git-send-email-davidben@mit.edu> X-Brightmail-Tracker: AAAAAA== Subject: [notmuch] [PATCH 2/4] Pass separate notmuch_path when opening database 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: Wed, 24 Feb 2010 02:28:19 -0000 Avoid hard-coding $MAILSTORE/.notmuch in the database layer. Signed-off-by: David Benjamin --- lib/database-private.h | 1 + lib/database.cc | 28 ++++++++++++++-------------- lib/notmuch.h | 32 ++++++++++++++++++++------------ notmuch-client.h | 4 ++++ notmuch-config.c | 29 +++++++++++++++++++++++++++++ notmuch-count.c | 4 ++-- notmuch-dump.c | 4 ++-- notmuch-new.c | 3 ++- notmuch-reply.c | 4 ++-- notmuch-restore.c | 4 ++-- notmuch-search-tags.c | 4 ++-- notmuch-search.c | 4 ++-- notmuch-show.c | 4 ++-- notmuch-tag.c | 4 ++-- 14 files changed, 86 insertions(+), 43 deletions(-) diff --git a/lib/database-private.h b/lib/database-private.h index 41918d7..59ed117 100644 --- a/lib/database-private.h +++ b/lib/database-private.h @@ -38,6 +38,7 @@ struct _notmuch_database { notmuch_bool_t exception_reported; char *path; + char *notmuch_path; notmuch_bool_t needs_upgrade; notmuch_database_mode_t mode; diff --git a/lib/database.cc b/lib/database.cc index 1bb24ec..f54fbd1 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -437,14 +437,14 @@ parse_references (void *ctx, } notmuch_database_t * -notmuch_database_create (const char *path) +notmuch_database_create (const char *path, + const char *notmuch_path) { notmuch_database_t *notmuch = NULL; - char *notmuch_path = NULL; struct stat st; int err; - if (path == NULL) { + if (path == NULL || notmuch_path == NULL) { fprintf (stderr, "Error: Cannot create a database for a NULL path.\n"); goto DONE; } @@ -462,8 +462,6 @@ notmuch_database_create (const char *path) goto DONE; } - notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch"); - err = mkdir (notmuch_path, 0755); if (err) { @@ -473,13 +471,11 @@ notmuch_database_create (const char *path) } notmuch = notmuch_database_open (path, + notmuch_path, NOTMUCH_DATABASE_MODE_READ_WRITE); notmuch_database_upgrade (notmuch, NULL, NULL); DONE: - if (notmuch_path) - talloc_free (notmuch_path); - return notmuch; } @@ -496,17 +492,17 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch) notmuch_database_t * notmuch_database_open (const char *path, + const char *notmuch_path, notmuch_database_mode_t mode) { notmuch_database_t *notmuch = NULL; - char *notmuch_path = NULL, *xapian_path = NULL; + char *xapian_path = NULL; struct stat st; int err; unsigned int i, version; - if (asprintf (¬much_path, "%s/%s", path, ".notmuch") == -1) { - notmuch_path = NULL; - fprintf (stderr, "Out of memory\n"); + if (path == NULL || notmuch_path == NULL) { + fprintf (stderr, "Error: Cannot create a database for a NULL path.\n"); goto DONE; } @@ -611,8 +607,6 @@ notmuch_database_open (const char *path, } DONE: - if (notmuch_path) - free (notmuch_path); if (xapian_path) free (xapian_path); @@ -645,6 +639,12 @@ notmuch_database_get_path (notmuch_database_t *notmuch) return notmuch->path; } +const char * +notmuch_database_get_notmuch_path (notmuch_database_t *notmuch) +{ + return notmuch->notmuch_path; +} + unsigned int notmuch_database_get_version (notmuch_database_t *notmuch) { diff --git a/lib/notmuch.h b/lib/notmuch.h index d3e50a7..943c297 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -120,12 +120,12 @@ typedef struct _notmuch_tags notmuch_tags_t; typedef struct _notmuch_directory notmuch_directory_t; typedef struct _notmuch_filenames notmuch_filenames_t; -/* Create a new, empty notmuch database located at 'path'. +/* Create a new, empty notmuch database located at 'notmuch_path', + * indexing mail store 'path' * * The path should be a top-level directory to a collection of * plain-text email messages (one message per file). This call will - * create a new ".notmuch" directory within 'path' where notmuch will - * store its data. + * create directory 'notmuch_path' where notmuch will store its data. * * After a successful call to notmuch_database_create, the returned * database will be open so the caller should call @@ -140,7 +140,8 @@ typedef struct _notmuch_filenames notmuch_filenames_t; * an error message on stderr). */ notmuch_database_t * -notmuch_database_create (const char *path); +notmuch_database_create (const char *path, + const char *notmuch_path); typedef enum { NOTMUCH_DATABASE_MODE_READ_ONLY = 0, @@ -150,16 +151,15 @@ typedef enum { /* XXX: I think I'd like this to take an extra argument of * notmuch_status_t* for returning a status value on failure. */ -/* Open an existing notmuch database located at 'path'. +/* Open an existing notmuch database located at 'notmuch_path' with + * mail store rooted at 'path' * * The database should have been created at some time in the past, * (not necessarily by this process), by calling - * notmuch_database_create with 'path'. By default the database should be - * opened for reading only. In order to write to the database you need to - * pass the NOTMUCH_DATABASE_MODE_WRITABLE mode. - * - * An existing notmuch database can be identified by the presence of a - * directory named ".notmuch" below 'path'. + * notmuch_database_create with 'path' and 'notmuch_path'. By default + * the database should be opened for reading only. In order to write + * to the database you need to pass the NOTMUCH_DATABASE_MODE_WRITABLE + * mode. * * The caller should call notmuch_database_close when finished with * this database. @@ -169,6 +169,7 @@ typedef enum { */ notmuch_database_t * notmuch_database_open (const char *path, + const char *notmuch_path, notmuch_database_mode_t mode); /* Close the given notmuch database, freeing all associated @@ -176,13 +177,20 @@ notmuch_database_open (const char *path, void notmuch_database_close (notmuch_database_t *database); -/* Return the database path of the given database. +/* Return the mail store path of the given database. * * The return value is a string owned by notmuch so should not be * modified nor freed by the caller. */ const char * notmuch_database_get_path (notmuch_database_t *database); +/* Return the database path of the given database. + * + * The return value is a string owned by notmuch so should not be + * modified nor freed by the caller. */ +const char * +notmuch_database_get_notmuch_path (notmuch_database_t *database); + /* Return the database format version of the given database. */ unsigned int notmuch_database_get_version (notmuch_database_t *database); diff --git a/notmuch-client.h b/notmuch-client.h index c80b39c..1a676d2 100644 --- a/notmuch-client.h +++ b/notmuch-client.h @@ -148,6 +148,10 @@ void notmuch_config_set_database_path (notmuch_config_t *config, const char *database_path); +notmuch_database_t * +notmuch_config_open_database (notmuch_config_t *config, + notmuch_database_mode_t mode); + const char * notmuch_config_get_user_name (notmuch_config_t *config); diff --git a/notmuch-config.c b/notmuch-config.c index 95430db..58a28b1 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -353,6 +353,35 @@ notmuch_config_set_database_path (notmuch_config_t *config, config->database_path = NULL; } +notmuch_database_t * +notmuch_config_open_database (notmuch_config_t *config, + notmuch_database_mode_t mode) +{ + const char *path = NULL; + char *db_path = NULL; + notmuch_database_t *notmuch = NULL; + + path = notmuch_config_get_database_path (config); + if (path == NULL) { + fprintf (stderr, "Error: Cannot create a database for a NULL path.\n"); + goto DONE; + } + + if (asprintf (&db_path, "%s/%s", path, ".notmuch") == -1) { + db_path = NULL; + fprintf (stderr, "Out of memory\n"); + goto DONE; + } + + notmuch = notmuch_database_open (path, db_path, mode); + +DONE: + if (db_path) + free(db_path); + + return notmuch; +} + const char * notmuch_config_get_user_name (notmuch_config_t *config) { diff --git a/notmuch-count.c b/notmuch-count.c index 77aa433..8d077eb 100644 --- a/notmuch-count.c +++ b/notmuch-count.c @@ -80,8 +80,8 @@ notmuch_count_command (void *ctx, int argc, char *argv[]) if (config == NULL) return 1; - notmuch = notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_ONLY); + notmuch = notmuch_config_open_database (config, + NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) return 1; diff --git a/notmuch-dump.c b/notmuch-dump.c index ea326bb..a051fd4 100644 --- a/notmuch-dump.c +++ b/notmuch-dump.c @@ -35,8 +35,8 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[]) if (config == NULL) return 1; - notmuch = notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_ONLY); + notmuch = notmuch_config_open_database (config, + NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) return 1; diff --git a/notmuch-new.c b/notmuch-new.c index f25c71f..d24dab9 100644 --- a/notmuch-new.c +++ b/notmuch-new.c @@ -749,10 +749,11 @@ notmuch_new_command (void *ctx, int argc, char *argv[]) return 1; printf ("Found %d total files (that's not much mail).\n", count); - notmuch = notmuch_database_create (db_path); + notmuch = notmuch_database_create (db_path, dot_notmuch_path); add_files_state.total_files = count; } else { notmuch = notmuch_database_open (db_path, + dot_notmuch_path, NOTMUCH_DATABASE_MODE_READ_WRITE); if (notmuch == NULL) return 1; diff --git a/notmuch-reply.c b/notmuch-reply.c index 98f6442..10734bc 100644 --- a/notmuch-reply.c +++ b/notmuch-reply.c @@ -467,8 +467,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[]) return 1; } - notmuch = notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_ONLY); + notmuch = notmuch_config_open_database (config, + NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) return 1; diff --git a/notmuch-restore.c b/notmuch-restore.c index 53ce254..d495b4f 100644 --- a/notmuch-restore.c +++ b/notmuch-restore.c @@ -36,8 +36,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) if (config == NULL) return 1; - notmuch = notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_WRITE); + notmuch = notmuch_config_open_database (config, + NOTMUCH_DATABASE_MODE_READ_WRITE); if (notmuch == NULL) return 1; diff --git a/notmuch-search-tags.c b/notmuch-search-tags.c index 7a1305e..1ddbb94 100644 --- a/notmuch-search-tags.c +++ b/notmuch-search-tags.c @@ -51,8 +51,8 @@ notmuch_search_tags_command (void *ctx, int argc, char *argv[]) goto error; } - db = notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_ONLY); + db = notmuch_config_open_database (config, + NOTMUCH_DATABASE_MODE_READ_ONLY); if (db == NULL) { goto error; } diff --git a/notmuch-search.c b/notmuch-search.c index 25dd6eb..2b65e87 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -244,8 +244,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) if (config == NULL) return 1; - notmuch = notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_ONLY); + notmuch = notmuch_config_open_database (config, + NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) return 1; diff --git a/notmuch-show.c b/notmuch-show.c index 1a1d601..88124dc 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -446,8 +446,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[])) return 1; } - notmuch = notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_ONLY); + notmuch = notmuch_config_open_database (config, + NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) return 1; diff --git a/notmuch-tag.c b/notmuch-tag.c index 00588a1..1deff1a 100644 --- a/notmuch-tag.c +++ b/notmuch-tag.c @@ -96,8 +96,8 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[])) if (config == NULL) return 1; - notmuch = notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_WRITE); + notmuch = notmuch_config_open_database (config, + NOTMUCH_DATABASE_MODE_READ_WRITE); if (notmuch == NULL) return 1; -- 1.7.0.18.g39b3