--- /dev/null
+Return-Path: <amthrax@drake.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 D95CE431FCB\r
+ for <notmuch@notmuchmail.org>; Sun, 13 May 2012 12:57:24 -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 ufcnFKicvW9i for <notmuch@notmuchmail.org>;\r
+ Sun, 13 May 2012 12:57:23 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-5.mit.edu (DMZ-MAILSEC-SCANNER-5.MIT.EDU\r
+ [18.7.68.34])\r
+ by olra.theworths.org (Postfix) with ESMTP id EF138431FC3\r
+ for <notmuch@notmuchmail.org>; Sun, 13 May 2012 12:57:22 -0700 (PDT)\r
+X-AuditID: 12074422-b7fd66d0000008f9-a7-4fb012227241\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ by dmz-mailsec-scanner-5.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 75.24.02297.22210BF4; Sun, 13 May 2012 15:57:22 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+ by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q4DJvKsJ023343; \r
+ Sun, 13 May 2012 15:57:20 -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 q4DJvF49022907\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Sun, 13 May 2012 15:57:16 -0400 (EDT)\r
+Received: from amthrax by drake.mit.edu with local (Exim 4.77)\r
+ (envelope-from <amthrax@drake.mit.edu>)\r
+ id 1STeup-0003en-L8; Sun, 13 May 2012 15:57:15 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 1/5] lib/cli: Make notmuch_database_get_directory return a\r
+ status code\r
+Date: Sun, 13 May 2012 15:57:05 -0400\r
+Message-Id: <1336939029-13969-2-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10\r
+In-Reply-To: <1336939029-13969-1-git-send-email-amdragon@mit.edu>\r
+References: <1336939029-13969-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFrrNIsWRmVeSWpSXmKPExsUixG6nrqsktMHfoPuTiMXs1h9MFjdauxkt\r
+ mqY7W1y/OZPZ4s3KeawOrB6Hvy5k8Zh4/jSbx637r9k9nq26xeyx5dB75gDWKC6blNSczLLU\r
+ In27BK6MxsNOBTcMK65ffMbUwNiq0cXIySEhYCKxoekHG4QtJnHh3nogm4tDSGAfo8SfyW9Z\r
+ IJwNjBILV05lgnDuM0lMvriBEcKZzygx89hBsH42AQ2JbfuXM4LYIgLSEjvvzmYFKWIW6GKU\r
+ +P2mmRkkISwQJrH8RwtYEYuAqkTvrf1gcV4BB4mTD9qZIQ6Rl3h6vw9sKKeAo8SyKwvB4kIg\r
+ NR//Mk9g5F/AyLCKUTYlt0o3NzEzpzg1Wbc4OTEvL7VI11QvN7NELzWldBMjOAxdlHYw/jyo\r
+ dIhRgINRiYd3geh6fyHWxLLiytxDjJIcTEqivGmCG/yF+JLyUyozEosz4otKc1KLDzFKcDAr\r
+ ifCufwhUzpuSWFmVWpQPk5LmYFES51XXeucnJJCeWJKanZpakFoEk5Xh4FCS4M0FGSpYlJqe\r
+ WpGWmVOCkGbi4AQZzgM0PAWkhre4IDG3ODMdIn+KUVFKHKJZACSRUZoH1wtLE68YxYFeEeZt\r
+ BKniAaYYuO5XQIOZgAbzC6wDGVySiJCSamCcpN9tbxvyYLLKsr4Vh8VOiaxz6p3JsePzZAcO\r
+ T9+1TPZ7Fmht+S+62+9Xy/s/V1lbnt1qmLukL9W5xmr2xuiXew7Wf3tXk8T5qu5nuP1URtav\r
+ CxOESme3lf65MUugZ+f5xX+0D4v2qT9NUWs5OOfkxaaKuHNyfe/np8sbP/Lh0FVsn12T+6VI\r
+ iaU4I9FQi7moOBEAKnU/+u4CAAA=\r
+Cc: tomi.ollila@iki.fi\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: Sun, 13 May 2012 19:57:25 -0000\r
+\r
+Previously, notmuch_database_get_directory had no way to indicate how\r
+it had failed. This changes its prototype to return a status code and\r
+set an out-argument to the retrieved directory, like similar functions\r
+in the library API. This does *not* change its currently broken\r
+behavior of creating directory objects when they don't exist, but it\r
+does document it and paves the way for fixing this. Also, it can now\r
+check for a read-only database and return\r
+NOTMUCH_STATUS_READ_ONLY_DATABASE instead of crashing.\r
+\r
+In the interest of atomicity, this also updates calls from the CLI so\r
+that notmuch still compiles.\r
+---\r
+ lib/database.cc | 22 ++++++++++++++++------\r
+ lib/notmuch.h | 24 +++++++++++++++++++++---\r
+ notmuch-new.c | 21 ++++++++++++++-------\r
+ 3 files changed, 51 insertions(+), 16 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 07f186e..f8c4a7d 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -955,8 +955,8 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ mtime = Xapian::sortable_unserialise (\r
+ document.get_value (NOTMUCH_VALUE_TIMESTAMP));\r
+ \r
+- directory = notmuch_database_get_directory (notmuch,\r
+- term.c_str() + 10);\r
++ directory = _notmuch_directory_create (notmuch, term.c_str() + 10,\r
++ &status);\r
+ notmuch_directory_set_mtime (directory, mtime);\r
+ notmuch_directory_destroy (directory);\r
+ }\r
+@@ -1304,20 +1304,30 @@ _notmuch_database_relative_path (notmuch_database_t *notmuch,\r
+ return relative;\r
+ }\r
+ \r
+-notmuch_directory_t *\r
++notmuch_status_t\r
+ notmuch_database_get_directory (notmuch_database_t *notmuch,\r
+- const char *path)\r
++ const char *path,\r
++ notmuch_directory_t **directory)\r
+ {\r
+ notmuch_status_t status;\r
+ \r
++ if (directory == NULL)\r
++ return NOTMUCH_STATUS_NULL_POINTER;\r
++ *directory = NULL;\r
++\r
++ /* XXX Handle read-only databases properly */\r
++ if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)\r
++ return NOTMUCH_STATUS_READ_ONLY_DATABASE;\r
++\r
+ try {\r
+- return _notmuch_directory_create (notmuch, path, &status);\r
++ *directory = _notmuch_directory_create (notmuch, path, &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
+ notmuch->exception_reported = TRUE;\r
+- return NULL;\r
++ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+ }\r
++ return status;\r
+ }\r
+ \r
+ /* Allocate a document ID that satisfies the following criteria:\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index e6e5cc2..d414198 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -300,11 +300,29 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch);\r
+ * (see notmuch_database_get_path), or else should be an absolute path\r
+ * with initial components that match the path of 'database'.\r
+ *\r
+- * Can return NULL if a Xapian exception occurs.\r
++ * Note: Currently this will create the directory object if it doesn't\r
++ * exist. This is not intentional behavior and is likely to change in\r
++ * the near future. To future-proof, when this returns\r
++ * NOTMUCH_STATUS_SUCCESS, callers should check that *directory is not\r
++ * NULL.\r
++ *\r
++ * Return value:\r
++ *\r
++ * NOTMUCH_STATUS_SUCCESS: Successfully retrieved directory.\r
++ *\r
++ * NOTMUCH_STATUS_NULL_POINTER: The given 'directory' argument is NULL.\r
++ *\r
++ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;\r
++ * directory not retrieved.\r
++ *\r
++ * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only\r
++ * mode so the directory cannot be created (this case will be\r
++ * removed in the future).\r
+ */\r
+-notmuch_directory_t *\r
++notmuch_status_t\r
+ notmuch_database_get_directory (notmuch_database_t *database,\r
+- const char *path);\r
++ const char *path,\r
++ notmuch_directory_t **directory);\r
+ \r
+ /* Add a new message to the given notmuch database or associate an\r
+ * additional filename with an existing message.\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index cb720cc..a3a8bec 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -250,7 +250,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;\r
+ notmuch_message_t *message = NULL;\r
+ struct dirent **fs_entries = NULL;\r
+- int i, num_fs_entries;\r
++ int i, num_fs_entries = 0;\r
+ notmuch_directory_t *directory;\r
+ notmuch_filenames_t *db_files = NULL;\r
+ notmuch_filenames_t *db_subdirs = NULL;\r
+@@ -274,8 +274,12 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ \r
+ fs_mtime = st.st_mtime;\r
+ \r
+- directory = notmuch_database_get_directory (notmuch, path);\r
+- db_mtime = notmuch_directory_get_mtime (directory);\r
++ status = notmuch_database_get_directory (notmuch, path, &directory);\r
++ if (status) {\r
++ ret = status;\r
++ goto DONE;\r
++ }\r
++ db_mtime = directory ? notmuch_directory_get_mtime (directory) : 0;\r
+ \r
+ new_directory = db_mtime ? FALSE : TRUE;\r
+ \r
+@@ -295,7 +299,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ * by a new out-argument, or by recording this information and\r
+ * providing an accessor.\r
+ */\r
+- if (new_directory)\r
++ if (new_directory && directory)\r
+ notmuch_directory_set_mtime (directory, -1);\r
+ \r
+ /* If the database knows about this directory, then we sort based\r
+@@ -810,7 +814,9 @@ _remove_directory (void *ctx,\r
+ notmuch_filenames_t *files, *subdirs;\r
+ char *absolute;\r
+ \r
+- directory = notmuch_database_get_directory (notmuch, path);\r
++ status = notmuch_database_get_directory (notmuch, path, &directory);\r
++ if (status || !directory)\r
++ return status;\r
+ \r
+ for (files = notmuch_directory_get_child_files (directory);\r
+ notmuch_filenames_valid (files);\r
+@@ -981,9 +987,10 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+ }\r
+ \r
+ for (f = add_files_state.directory_mtimes->head; f && !interrupted; f = f->next) {\r
++ notmuch_status_t status;\r
+ notmuch_directory_t *directory;\r
+- directory = notmuch_database_get_directory (notmuch, f->filename);\r
+- if (directory) {\r
++ status = notmuch_database_get_directory (notmuch, f->filename, &directory);\r
++ if (status == NOTMUCH_STATUS_SUCCESS && directory) {\r
+ notmuch_directory_set_mtime (directory, f->mtime);\r
+ notmuch_directory_destroy (directory);\r
+ }\r
+-- \r
+1.7.10\r
+\r