[notmuch] [PATCH 2/4] Pass separate notmuch_path when opening database
authorDavid Benjamin <davidben@MIT.EDU>
Wed, 24 Feb 2010 02:23:01 +0000 (21:23 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:36:16 +0000 (09:36 -0800)
83/cbddd7d48a5492c1a5fb3a1a246ea6ffdf42ba [new file with mode: 0644]

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