--- /dev/null
+Return-Path: <amdragon@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 E435A429E38\r
+ for <notmuch@notmuchmail.org>; Thu, 17 May 2012 21:13:57 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\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 5QKL3f7IKhu0 for <notmuch@notmuchmail.org>;\r
+ Thu, 17 May 2012 21:13:55 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-7.mit.edu (DMZ-MAILSEC-SCANNER-7.MIT.EDU\r
+ [18.7.68.36])\r
+ by olra.theworths.org (Postfix) with ESMTP id 367DB431FDB\r
+ for <notmuch@notmuchmail.org>; Thu, 17 May 2012 21:13:53 -0700 (PDT)\r
+X-AuditID: 12074424-b7fae6d000000906-f7-4fb5cc809d4b\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 64.51.02310.08CC5BF4; Fri, 18 May 2012 00:13:52 -0400 (EDT)\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 q4I4Dpb8021449; \r
+ Fri, 18 May 2012 00:13:51 -0400\r
+Received: from drake.mit.edu\r
+ (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+ [209.6.116.242]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q4I4Dn14026781\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Fri, 18 May 2012 00:13:51 -0400 (EDT)\r
+Received: from amthrax by drake.mit.edu with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1SVEZZ-0000yx-OQ; Fri, 18 May 2012 00:13:49 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 1/9] lib: Make directory document creation optional for\r
+ _notmuch_directory_create\r
+Date: Fri, 18 May 2012 00:13:34 -0400\r
+Message-Id: <1337314423-3702-2-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10\r
+In-Reply-To: <1337314423-3702-1-git-send-email-amdragon@mit.edu>\r
+References: <1337314423-3702-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrAIsWRmVeSWpSXmKPExsUixG6nottwZqu/wYSDohbXb85kdmD0eLbq\r
+ FnMAYxSXTUpqTmZZapG+XQJXxrQZy9kLbutV/Hx2hLWB8YRqFyMnh4SAicSrmR9YIGwxiQv3\r
+ 1rN1MXJxCAnsY5S4+f0GK4SzgVHiyZ0FYFVCAveZJOZMyYBIzGKU+HXkOyNIgk1AQ2Lb/uVg\r
+ toiAtMTOu7OBujk4mAXUJP50qYCYwgKJEktfxYBUsAioSsyc/JUVxOYVsJe40LyHFeIIeYmn\r
+ 9/vYQMo5BRwkph2PhthqL/HvQjPLBEb+BYwMqxhlU3KrdHMTM3OKU5N1i5MT8/JSi3TN9XIz\r
+ S/RSU0o3MYIDxkVlB2PzIaVDjAIcjEo8vALzt/oLsSaWFVfmHmKU5GBSEuX9fhIoxJeUn1KZ\r
+ kVicEV9UmpNafIhRgoNZSYRXthsox5uSWFmVWpQPk5LmYFES59XQeucnJJCeWJKanZpakFoE\r
+ k5Xh4FCS4N10GqhRsCg1PbUiLTOnBCHNxMEJMpwHaHgmSA1vcUFibnFmOkT+FKOilDhvGUhC\r
+ ACSRUZoH1wuL6FeM4kCvCPNOAKniASYDuO5XQIOZgAaX5W4CGVySiJCSamA8cOzpK9bQpLsM\r
+ 7h8/S30L3fPyinThIv3aU+ynq8znrA8VFNXXznEu2KjsMuE/g9tZ3Z26Coe6c2K3dy06G10a\r
+ OpXjVtG6/+r2Ajvufnsak9TnZ+nmlyYT1WjxzLat7Ztz9O+6bzub7P48PNIT3nfv7JNdIjkz\r
+ CwsOMG89/P+D6YuNbUV+Hy8qsRRnJBpqMRcVJwIAiYfN1cMCAAA=\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: Fri, 18 May 2012 04:13:58 -0000\r
+\r
+Previously this function would create directory documents if they\r
+didn't exist. As a result, it could only be used on writable\r
+databases. This adds an argument to make creation optional and to\r
+make this function work on read-only databases. We use a flag\r
+argument to avoid a bare boolean and to permit future expansion.\r
+\r
+Both callers have been updated, but currently retain the old behavior.\r
+We'll take advantage of the new argument in the following patches.\r
+---\r
+ lib/database.cc | 6 +++---\r
+ lib/directory.cc | 33 ++++++++++++++++++++++++++++-----\r
+ lib/notmuch-private.h | 8 ++++++++\r
+ 3 files changed, 39 insertions(+), 8 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index f8c4a7d..df996a9 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -956,7 +956,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ document.get_value (NOTMUCH_VALUE_TIMESTAMP));\r
+ \r
+ directory = _notmuch_directory_create (notmuch, term.c_str() + 10,\r
+- &status);\r
++ NOTMUCH_FIND_CREATE, &status);\r
+ notmuch_directory_set_mtime (directory, mtime);\r
+ notmuch_directory_destroy (directory);\r
+ }\r
+@@ -1210,7 +1210,7 @@ _notmuch_database_find_directory_id (notmuch_database_t *notmuch,\r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
+ \r
+- directory = _notmuch_directory_create (notmuch, path, &status);\r
++ directory = _notmuch_directory_create (notmuch, path, NOTMUCH_FIND_CREATE, &status);\r
+ if (status) {\r
+ *directory_id = -1;\r
+ return status;\r
+@@ -1320,7 +1320,7 @@ notmuch_database_get_directory (notmuch_database_t *notmuch,\r
+ return NOTMUCH_STATUS_READ_ONLY_DATABASE;\r
+ \r
+ try {\r
+- *directory = _notmuch_directory_create (notmuch, path, &status);\r
++ *directory = _notmuch_directory_create (notmuch, path, NOTMUCH_FIND_CREATE, &status);\r
+ } catch (const Xapian::Error &error) {\r
+ fprintf (stderr, "A Xapian exception occurred getting directory: %s.\n",\r
+ error.get_msg().c_str());\r
+diff --git a/lib/directory.cc b/lib/directory.cc\r
+index 70e1693..83bb19b 100644\r
+--- a/lib/directory.cc\r
++++ b/lib/directory.cc\r
+@@ -82,28 +82,41 @@ find_directory_document (notmuch_database_t *notmuch,\r
+ return NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
+ }\r
+ \r
++/* Find or create a directory document.\r
++ *\r
++ * 'path' should be a path relative to the path of 'database', or else\r
++ * should be an absolute path with initial components that match the\r
++ * path of 'database'.\r
++ *\r
++ * If (flags & NOTMUCH_FIND_CREATE), then the directory document will\r
++ * be created if it does not exist. Otherwise, if the directory\r
++ * document does not exist, *status_ret is set to\r
++ * NOTMUCH_STATUS_SUCCESS and this returns NULL.\r
++ */\r
+ notmuch_directory_t *\r
+ _notmuch_directory_create (notmuch_database_t *notmuch,\r
+ const char *path,\r
++ notmuch_find_flags_t flags,\r
+ notmuch_status_t *status_ret)\r
+ {\r
+ Xapian::WritableDatabase *db;\r
+ notmuch_directory_t *directory;\r
+ notmuch_private_status_t private_status;\r
+ const char *db_path;\r
++ notmuch_bool_t create = (flags & NOTMUCH_FIND_CREATE);\r
+ \r
+ *status_ret = NOTMUCH_STATUS_SUCCESS;\r
+ \r
+ path = _notmuch_database_relative_path (notmuch, path);\r
+ \r
+- if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)\r
++ if (create && notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)\r
+ INTERNAL_ERROR ("Failure to ensure database is writable");\r
+ \r
+- db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
+-\r
+ directory = talloc (notmuch, notmuch_directory_t);\r
+- if (unlikely (directory == NULL))\r
++ if (unlikely (directory == NULL)) {\r
++ *status_ret = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+ return NULL;\r
++ }\r
+ \r
+ directory->notmuch = notmuch;\r
+ \r
+@@ -122,6 +135,13 @@ _notmuch_directory_create (notmuch_database_t *notmuch,\r
+ directory->document_id = directory->doc.get_docid ();\r
+ \r
+ if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
++ if (!create) {\r
++ notmuch_directory_destroy (directory);\r
++ directory = NULL;\r
++ *status_ret = NOTMUCH_STATUS_SUCCESS;\r
++ goto DONE;\r
++ }\r
++\r
+ void *local = talloc_new (directory);\r
+ const char *parent, *basename;\r
+ Xapian::docid parent_id;\r
+@@ -145,6 +165,8 @@ _notmuch_directory_create (notmuch_database_t *notmuch,\r
+ directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,\r
+ Xapian::sortable_serialise (0));\r
+ \r
++ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
++\r
+ directory->document_id = _notmuch_database_generate_doc_id (notmuch);\r
+ db->replace_document (directory->document_id, directory->doc);\r
+ talloc_free (local);\r
+@@ -158,10 +180,11 @@ _notmuch_directory_create (notmuch_database_t *notmuch,\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+ notmuch_directory_destroy (directory);\r
++ directory = NULL;\r
+ *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+- return NULL;\r
+ }\r
+ \r
++ DONE:\r
+ if (db_path != path)\r
+ free ((char *) db_path);\r
+ \r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 3886e0c..538274f 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -146,6 +146,13 @@ typedef enum _notmuch_private_status {\r
+ : \\r
+ (notmuch_status_t) private_status)\r
+ \r
++/* Flags shared by various lookup functions. */\r
++typedef enum _notmuch_find_flags {\r
++ /* If set, create the necessary document (or documents) if they\r
++ * are missing. Requires a read/write database. */\r
++ NOTMUCH_FIND_CREATE = 1<<0,\r
++} notmuch_find_flags_t;\r
++\r
+ typedef struct _notmuch_doc_id_set notmuch_doc_id_set_t;\r
+ \r
+ typedef struct _notmuch_string_list notmuch_string_list_t;\r
+@@ -206,6 +213,7 @@ _notmuch_database_filename_to_direntry (void *ctx,\r
+ notmuch_directory_t *\r
+ _notmuch_directory_create (notmuch_database_t *notmuch,\r
+ const char *path,\r
++ notmuch_find_flags_t flags,\r
+ notmuch_status_t *status_ret);\r
+ \r
+ unsigned int\r
+-- \r
+1.7.10\r
+\r