[PATCH v2 1/5] lib/cli: Make notmuch_database_get_directory return a status code
authorAustin Clements <amdragon@MIT.EDU>
Sun, 13 May 2012 23:36:09 +0000 (19:36 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:05 +0000 (09:47 -0800)
fd/6fe48693c7a80334551ec31dcc712032e2d9e2 [new file with mode: 0644]

diff --git a/fd/6fe48693c7a80334551ec31dcc712032e2d9e2 b/fd/6fe48693c7a80334551ec31dcc712032e2d9e2
new file mode 100644 (file)
index 0000000..f4efad4
--- /dev/null
@@ -0,0 +1,243 @@
+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 02E04431FC0\r
+       for <notmuch@notmuchmail.org>; Sun, 13 May 2012 16:36: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 74Zp-v00edOG for <notmuch@notmuchmail.org>;\r
+       Sun, 13 May 2012 16:36:22 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-2.mit.edu (DMZ-MAILSEC-SCANNER-2.MIT.EDU\r
+       [18.9.25.13])\r
+       by olra.theworths.org (Postfix) with ESMTP id 925BE431FAE\r
+       for <notmuch@notmuchmail.org>; Sun, 13 May 2012 16:36:22 -0700 (PDT)\r
+X-AuditID: 1209190d-b7fbf6d0000008ba-27-4fb045761c29\r
+Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])\r
+       by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 5A.DF.02234.67540BF4; Sun, 13 May 2012 19:36:22 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id q4DNaLSx025782; \r
+       Sun, 13 May 2012 19:36:21 -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 q4DNaK2s001096\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Sun, 13 May 2012 19:36:20 -0400 (EDT)\r
+Received: from amthrax by drake.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1STiKp-0004YJ-Sz; Sun, 13 May 2012 19:36:19 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/5] lib/cli: Make notmuch_database_get_directory return a\r
+       status code\r
+Date: Sun, 13 May 2012 19:36:09 -0400\r
+Message-Id: <1336952173-17422-2-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10\r
+In-Reply-To: <1336952173-17422-1-git-send-email-amdragon@mit.edu>\r
+References: <1336939029-13969-1-git-send-email-amdragon@mit.edu>\r
+       <1336952173-17422-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFrrNIsWRmVeSWpSXmKPExsUixCmqrFvmusHfYGU/t8Xs1h9MFjdauxkt\r
+       mqY7W1y/OZPZ4s3KeawOrB6Hvy5k8Zh4/jSbx637r9k9nq26xeyx5dB75gDWKC6blNSczLLU\r
+       In27BK6M5u0v2Au2GVZsXTybrYHxlXoXIyeHhICJxLo1fxkhbDGJC/fWs3UxcnEICexjlHhy\r
+       fxUjhLOBUaKn4z87hHOfSWJm51wmCGcWo8Tt7sWsIP1sAhoS2/YvB5slIiAtsfPubFaQImaB\r
+       LkaJ32+amUESwgKREk93zmQHsVkEVCVWLX0DtJCDg1fAQWLtEkOIO+Qlnt7vYwOxOQUcJfoe\r
+       fAQrFxIol5jRfp1lAiP/AkaGVYyyKblVurmJmTnFqcm6xcmJeXmpRbpGermZJXqpKaWbGMFh\r
+       KMm7g/HdQaVDjAIcjEo8vK9MN/gLsSaWFVfmHmKU5GBSEuVlcgIK8SXlp1RmJBZnxBeV5qQW\r
+       H2KU4GBWEuFd/3C9vxBvSmJlVWpRPkxKmoNFSZxXVeudn5BAemJJanZqakFqEUxWhoNDSYL3\r
+       gQvQUMGi1PTUirTMnBKENBMHJ8hwHqDhZ0BqeIsLEnOLM9Mh8qcYFaXEea+AJARAEhmleXC9\r
+       sDTxilEc6BVh3r0gVTzAFAPX/QpoMBPQYH6BdSCDSxIRUlINjFW9VdJPHiRWG+ZF6fBsnBA3\r
+       3WH5Tp7Qb0cy+3ad+NS/evYsOcb1z+e9TJ32ZccUy5+3/Z7syZpqPe3zHYGtLaXBbLHXj0w+\r
+       4iHqqh5TnJLasaqveIfgR9kpbhHaHV8eS8hpN5dfMS75Y+UUZ3szw/hrltFe5o6lT/T9tqh7\r
+       71825+oj+ZycViWW4oxEQy3mouJEAEREPaXuAgAA\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 23:36:24 -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   |   23 ++++++++++++++++++++---\r
+ notmuch-new.c   |   21 ++++++++++++++-------\r
+ 3 files changed, 50 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..bbb17e4 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -300,11 +300,28 @@ 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.  In the future, when a directory object does not exist this\r
++ * will return NOTMUCH_STATUS_SUCCESS and set *directory to NULL.\r
++ * Callers should be prepared for this.\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