[PATCH v2 1/6] lib/cli: Make notmuch_database_open return a status code
authorAustin Clements <amdragon@MIT.EDU>
Mon, 30 Apr 2012 16:25:33 +0000 (12:25 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:46:51 +0000 (09:46 -0800)
99/082673ed8e3083bf25e49020ee4ced1c60f08a [new file with mode: 0644]

diff --git a/99/082673ed8e3083bf25e49020ee4ced1c60f08a b/99/082673ed8e3083bf25e49020ee4ced1c60f08a
new file mode 100644 (file)
index 0000000..ad322d1
--- /dev/null
@@ -0,0 +1,376 @@
+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 D73F7429E3F\r
+       for <notmuch@notmuchmail.org>; Mon, 30 Apr 2012 09:25:46 -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 gfkNyQd+7Zcj for <notmuch@notmuchmail.org>;\r
+       Mon, 30 Apr 2012 09:25:44 -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 8DBE2431FAE\r
+       for <notmuch@notmuchmail.org>; Mon, 30 Apr 2012 09:25:44 -0700 (PDT)\r
+X-AuditID: 1209190d-b7fbf6d0000008ba-d8-4f9ebd08a8e4\r
+Received: from mailhub-auth-3.mit.edu ( [18.9.21.43])\r
+       by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id F7.17.02234.80DBE9F4; Mon, 30 Apr 2012 12:25:44 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id q3UGPhkr029834; \r
+       Mon, 30 Apr 2012 12:25:43 -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 q3UGPgf9005255\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Mon, 30 Apr 2012 12:25:43 -0400 (EDT)\r
+Received: from amthrax by drake.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amthrax@drake.mit.edu>)\r
+       id 1SOtPx-0006H4-Dw; Mon, 30 Apr 2012 12:25:41 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/6] lib/cli: Make notmuch_database_open return a status\r
+       code\r
+Date: Mon, 30 Apr 2012 12:25:33 -0400\r
+Message-Id: <1335803138-24024-2-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.9.1\r
+In-Reply-To: <1335803138-24024-1-git-send-email-amdragon@mit.edu>\r
+References: <1335651473-19652-1-git-send-email-amdragon@mit.edu>\r
+       <1335803138-24024-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrMIsWRmVeSWpSXmKPExsUixCmqrcuxd56/wb6FrBbXb85kdmD0eLbq\r
+       FnMAYxSXTUpqTmZZapG+XQJXxtVXK9gKPgdWLOtbzNjAONmxi5GTQ0LAROLxowOMELaYxIV7\r
+       69m6GLk4hAT2MUrMu36UHcLZwCjx+OlVFgjnPpNE+6zLTBDOfEaJtZ/esYH0swloSGzbvxxs\r
+       loiAtMTOu7NZuxg5OJgF1CT+dKmAhIUFAiSOTfkPVsIioCrR8PEuWCuvgIPEqu0T2SDOUJB4\r
+       tu44E4jNKeAocevANxYQW0igXGLjhU+MExj5FzAyrGKUTcmt0s1NzMwpTk3WLU5OzMtLLdI1\r
+       0svNLNFLTSndxAgOG0neHYzvDiodYhTgYFTi4Z00f56/EGtiWXFl7iFGSQ4mJVHe9buBQnxJ\r
+       +SmVGYnFGfFFpTmpxYcYJTiYlUR4SycC5XhTEiurUovyYVLSHCxK4ryqWu/8hATSE0tSs1NT\r
+       C1KLYLIyHBxKErx/QYYKFqWmp1akZeaUIKSZODhBhvMADT8OUsNbXJCYW5yZDpE/xagoJc67\r
+       ACQhAJLIKM2D64XF9StGcaBXhHmvgVTxAFMCXPcroMFMQIPzzcEGlyQipKQaGJtX/hRJPfuY\r
+       +/rry6+amRo3rZ9wenuf3sGlK/jLN6/O3Jije1R186stWjllhgc/Ts7QObLfWfz6jYrVK6+X\r
+       z4iXSTuceWDhn2ub5H5f+f7M/FaPTf7DDfKTdvjO0lK+4TRb79fuVfdlKxN8DDcwfLi+fJsF\r
+       Z87UrWE7n1ltLkxTTHzJVSxsK3JCiaU4I9FQi7moOBEAYk6UZMYCAAA=\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: Mon, 30 Apr 2012 16:25:47 -0000\r
+\r
+It has been a long-standing issue that notmuch_database_open doesn't\r
+return any indication of why it failed.  This patch changes its\r
+prototype to return a notmuch_status_t and set an out-argument to the\r
+database itself, like other functions that return both a status and an\r
+object.\r
+\r
+In the interest of atomicity, this also updates every use in the CLI\r
+so that notmuch still compiles.  Since this patch does not update the\r
+bindings, the Python bindings test fails.\r
+---\r
+ lib/database.cc     |   28 +++++++++++++++++++++++-----\r
+ lib/notmuch.h       |   26 +++++++++++++++++++-------\r
+ notmuch-count.c     |    5 ++---\r
+ notmuch-dump.c      |    5 ++---\r
+ notmuch-new.c       |    5 ++---\r
+ notmuch-reply.c     |    5 ++---\r
+ notmuch-restore.c   |    5 ++---\r
+ notmuch-search.c    |    5 ++---\r
+ notmuch-show.c      |    5 ++---\r
+ notmuch-tag.c       |    5 ++---\r
+ test/symbol-test.cc |    3 ++-\r
+ 11 files changed, 60 insertions(+), 37 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 2fefcad..1e66599 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -556,8 +556,9 @@ notmuch_database_create (const char *path)\r
+       goto DONE;\r
+     }\r
\r
+-    notmuch = notmuch_database_open (path,\r
+-                                   NOTMUCH_DATABASE_MODE_READ_WRITE);\r
++    notmuch_database_open (path,\r
++                         NOTMUCH_DATABASE_MODE_READ_WRITE,\r
++                         &notmuch);\r
+     notmuch_database_upgrade (notmuch, NULL, NULL);\r
\r
+   DONE:\r
+@@ -578,10 +579,12 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)\r
+     return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
\r
+-notmuch_database_t *\r
++notmuch_status_t\r
+ notmuch_database_open (const char *path,\r
+-                     notmuch_database_mode_t mode)\r
++                     notmuch_database_mode_t mode,\r
++                     notmuch_database_t **database)\r
+ {\r
++    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
+     void *local = talloc_new (NULL);\r
+     notmuch_database_t *notmuch = NULL;\r
+     char *notmuch_path, *xapian_path;\r
+@@ -590,8 +593,15 @@ notmuch_database_open (const char *path,\r
+     unsigned int i, version;\r
+     static int initialized = 0;\r
\r
++    if (path == NULL) {\r
++      fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");\r
++      status = NOTMUCH_STATUS_NULL_POINTER;\r
++      goto DONE;\r
++    }\r
++\r
+     if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {\r
+       fprintf (stderr, "Out of memory\n");\r
++      status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+       goto DONE;\r
+     }\r
\r
+@@ -599,11 +609,13 @@ notmuch_database_open (const char *path,\r
+     if (err) {\r
+       fprintf (stderr, "Error opening database at %s: %s\n",\r
+                notmuch_path, strerror (errno));\r
++      status = NOTMUCH_STATUS_FILE_ERROR;\r
+       goto DONE;\r
+     }\r
\r
+     if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {\r
+       fprintf (stderr, "Out of memory\n");\r
++      status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+       goto DONE;\r
+     }\r
\r
+@@ -644,6 +656,7 @@ notmuch_database_open (const char *path,\r
+               notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;\r
+               notmuch_database_destroy (notmuch);\r
+               notmuch = NULL;\r
++              status = NOTMUCH_STATUS_FILE_ERROR;\r
+               goto DONE;\r
+           }\r
\r
+@@ -704,12 +717,17 @@ notmuch_database_open (const char *path,\r
+                error.get_msg().c_str());\r
+       notmuch_database_destroy (notmuch);\r
+       notmuch = NULL;\r
++      status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+     }\r
\r
+   DONE:\r
+     talloc_free (local);\r
\r
+-    return notmuch;\r
++    if (database)\r
++      *database = notmuch;\r
++    else\r
++      talloc_free (notmuch);\r
++    return status;\r
+ }\r
\r
+ void\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 7d9e092..44b0c46 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -151,9 +151,6 @@ typedef enum {\r
+     NOTMUCH_DATABASE_MODE_READ_WRITE\r
+ } notmuch_database_mode_t;\r
\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
+  *\r
+  * The database should have been created at some time in the past,\r
+@@ -168,12 +165,27 @@ typedef enum {\r
+  * The caller should call notmuch_database_destroy when finished with\r
+  * this database.\r
+  *\r
+- * In case of any failure, this function returns NULL, (after printing\r
+- * an error message on stderr).\r
++ * In case of any failure, this function returns an error status and\r
++ * sets *database to NULL (after printing an error message on stderr).\r
++ *\r
++ * Return value:\r
++ *\r
++ * NOTMUCH_STATUS_SUCCESS: Successfully opened the database.\r
++ *\r
++ * NOTMUCH_STATUS_NULL_POINTER: The given 'path' argument is NULL.\r
++ *\r
++ * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.\r
++ *\r
++ * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to open the\r
++ *    database file (such as permission denied, or file not found,\r
++ *    etc.), or the database version is unknown.\r
++ *\r
++ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.\r
+  */\r
+-notmuch_database_t *\r
++notmuch_status_t\r
+ notmuch_database_open (const char *path,\r
+-                     notmuch_database_mode_t mode);\r
++                     notmuch_database_mode_t mode,\r
++                     notmuch_database_t **database);\r
\r
+ /* Close the given notmuch database.\r
+  *\r
+diff --git a/notmuch-count.c b/notmuch-count.c\r
+index 9c2ad7b..2f98128 100644\r
+--- a/notmuch-count.c\r
++++ b/notmuch-count.c\r
+@@ -66,9 +66,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
+-    if (notmuch == NULL)\r
++    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
++                             NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
+       return 1;\r
\r
+     query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index);\r
+diff --git a/notmuch-dump.c b/notmuch-dump.c\r
+index 71ab0ea..3743214 100644\r
+--- a/notmuch-dump.c\r
++++ b/notmuch-dump.c\r
+@@ -36,9 +36,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
+-    if (notmuch == NULL)\r
++    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
++                             NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
+       return 1;\r
\r
+     char *output_file_name = NULL;\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 3ff6304..7788743 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -903,9 +903,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+       notmuch = notmuch_database_create (db_path);\r
+       add_files_state.total_files = count;\r
+     } else {\r
+-      notmuch = notmuch_database_open (db_path,\r
+-                                       NOTMUCH_DATABASE_MODE_READ_WRITE);\r
+-      if (notmuch == NULL)\r
++      if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
++                                 &notmuch))\r
+           return 1;\r
\r
+       if (notmuch_database_needs_upgrade (notmuch)) {\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index da99a13..7184a5d 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -740,9 +740,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
+-    if (notmuch == NULL)\r
++    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
++                             NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
+       return 1;\r
\r
+     query = notmuch_query_create (notmuch, query_string);\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index 02b563c..4f4096e 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -115,9 +115,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
+-    if (notmuch == NULL)\r
++    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
++                             NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
+       return 1;\r
\r
+     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 7dfd270..3be296d 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -486,9 +486,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
+-    if (notmuch == NULL)\r
++    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
++                             NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
+       return 1;\r
\r
+     query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);\r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index 3b6667c..95427d4 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -1081,9 +1081,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
+-    if (notmuch == NULL)\r
++    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
++                             NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
+       return 1;\r
\r
+     query = notmuch_query_create (notmuch, query_string);\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index bd56fd1..7d18639 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -229,9 +229,8 @@ notmuch_tag_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_WRITE);\r
+-    if (notmuch == NULL)\r
++    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
++                             NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
+       return 1;\r
\r
+     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+diff --git a/test/symbol-test.cc b/test/symbol-test.cc\r
+index 1548ca4..3e96c03 100644\r
+--- a/test/symbol-test.cc\r
++++ b/test/symbol-test.cc\r
+@@ -4,7 +4,8 @@\r
\r
\r
+ int main() {\r
+-  (void) notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY);\r
++  notmuch_database_t *notmuch;\r
++  notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch);\r
\r
+   try {\r
+     (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);\r
+-- \r
+1.7.9.1\r
+\r