[PATCH 2/6] lib/cli: Make notmuch_database_create return a status code
authorAustin Clements <amdragon@MIT.EDU>
Sat, 28 Apr 2012 22:17:49 +0000 (18:17 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:46:46 +0000 (09:46 -0800)
3e/2b7d7bfccca3378fa6586ae1d3c2e68ea6762f [new file with mode: 0644]

diff --git a/3e/2b7d7bfccca3378fa6586ae1d3c2e68ea6762f b/3e/2b7d7bfccca3378fa6586ae1d3c2e68ea6762f
new file mode 100644 (file)
index 0000000..1b5af54
--- /dev/null
@@ -0,0 +1,194 @@
+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 DB3CE431FB6\r
+       for <notmuch@notmuchmail.org>; Sat, 28 Apr 2012 15:18:00 -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 KBC305-39tM6 for <notmuch@notmuchmail.org>;\r
+       Sat, 28 Apr 2012 15:17:59 -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 A4EEA431FAF\r
+       for <notmuch@notmuchmail.org>; Sat, 28 Apr 2012 15:17:59 -0700 (PDT)\r
+X-AuditID: 1209190d-b7fbf6d0000008ba-5f-4f9c6c9790fe\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 BB.B8.02234.79C6C9F4; Sat, 28 Apr 2012 18:17:59 -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 q3SMHw6Q029824; \r
+       Sat, 28 Apr 2012 18:17:58 -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 q3SMHvw1008919\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Sat, 28 Apr 2012 18:17:58 -0400 (EDT)\r
+Received: from amthrax by drake.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amthrax@drake.mit.edu>)\r
+       id 1SOFxl-00058G-3R; Sat, 28 Apr 2012 18:17:57 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 2/6] lib/cli: Make notmuch_database_create return a status\r
+ code\r
+Date: Sat, 28 Apr 2012 18:17:49 -0400\r
+Message-Id: <1335651473-19652-3-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.9.1\r
+In-Reply-To: <1335651473-19652-1-git-send-email-amdragon@mit.edu>\r
+References: <1335651473-19652-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrEIsWRmVeSWpSXmKPExsUixCmqrDs9Z46/wZNlghbXb85kdmD0eLbq\r
+       FnMAYxSXTUpqTmZZapG+XQJXxqyWO2wFV2Qrju19ztrAuFG8i5GTQ0LARGLB0gssELaYxIV7\r
+       69m6GLk4hAT2MUq837yXHcLZwCjx+PQNJpAqIYH7TBJtS7QhEvMZJfat/MsKkmAT0JDYtn85\r
+       I4gtIiAtsfPubKA4BwezgJrEny4VkLCwgL/Eoj8rmEFsFgFViYX/zoG18go4SLx+dogZ4goF\r
+       iWfrjoPt4hRwlPi/+QQ7xF4HiY55E9gmMPIvYGRYxSibklulm5uYmVOcmqxbnJyYl5dapGuk\r
+       l5tZopeaUrqJERw0krw7GN8dVDrEKMDBqMTDe2j9LH8h1sSy4srcQ4ySHExKorwsWXP8hfiS\r
+       8lMqMxKLM+KLSnNSiw8xSnAwK4nwaosD5XhTEiurUovyYVLSHCxK4ryqWu/8hATSE0tSs1NT\r
+       C1KLYLIyHBxKErxHs4EaBYtS01Mr0jJzShDSTBycIMN5gIbvAanhLS5IzC3OTIfIn2JUlBLn\r
+       XQ+SEABJZJTmwfXCovoVozjQK8K8Z0GqeIAJAa77FdBgJqDBTJ6zQAaXJCKkpBoYHVyFJVbr\r
+       +u+a9DYvoy1dTHt7XlPF3JebLj+Z2vLKcWupmf5Zhudvp20vmu9t9ORbpfMc0wsPg8oPZa3h\r
+       ZT+WEfozSFP7VpFwn+RytwaViC1z1VeZZxU+1Dz+d6u82FUDI+MYTavZuZO+vyh9U2ZSrPS9\r
+       e269gvvKM0l/7tbr/1n85pGTgsoGJZbijERDLeai4kQAETauQ8UCAAA=\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: Sat, 28 Apr 2012 22:18:01 -0000\r
+\r
+This is the notmuch_database_create equivalent of the previous change.\r
+\r
+In this case, there were places where errors were not being propagated\r
+correctly in notmuch_database_create or in calls to it.  These have\r
+been fixed, using the new status value.\r
+---\r
+ lib/database.cc |   26 +++++++++++++++++++-------\r
+ lib/notmuch.h   |    8 ++++----\r
+ notmuch-new.c   |    3 ++-\r
+ 3 files changed, 25 insertions(+), 12 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index a29fe67..a3ae504 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -520,9 +520,10 @@ parse_references (void *ctx,\r
+     }\r
+ }\r
+ \r
+-notmuch_database_t *\r
+-notmuch_database_create (const char *path)\r
++notmuch_status_t\r
++notmuch_database_create (const char *path, notmuch_database_t **database)\r
+ {\r
++    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
+     notmuch_database_t *notmuch = NULL;\r
+     char *notmuch_path = NULL;\r
+     struct stat st;\r
+@@ -530,6 +531,7 @@ notmuch_database_create (const char *path)\r
+ \r
+     if (path == NULL) {\r
+       fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");\r
++      status = NOTMUCH_STATUS_NULL_POINTER;\r
+       goto DONE;\r
+     }\r
+ \r
+@@ -537,12 +539,14 @@ notmuch_database_create (const char *path)\r
+     if (err) {\r
+       fprintf (stderr, "Error: Cannot create database at %s: %s.\n",\r
+                path, strerror (errno));\r
++      status = NOTMUCH_STATUS_FILE_ERROR;\r
+       goto DONE;\r
+     }\r
+ \r
+     if (! S_ISDIR (st.st_mode)) {\r
+       fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",\r
+                path);\r
++      status = NOTMUCH_STATUS_FILE_ERROR;\r
+       goto DONE;\r
+     }\r
+ \r
+@@ -553,19 +557,27 @@ notmuch_database_create (const char *path)\r
+     if (err) {\r
+       fprintf (stderr, "Error: Cannot create directory %s: %s.\n",\r
+                notmuch_path, strerror (errno));\r
++      status = NOTMUCH_STATUS_FILE_ERROR;\r
+       goto DONE;\r
+     }\r
+ \r
+-    notmuch_database_open (path,\r
+-                         NOTMUCH_DATABASE_MODE_READ_WRITE,\r
+-                         &notmuch);\r
+-    notmuch_database_upgrade (notmuch, NULL, NULL);\r
++    status = notmuch_database_open (path,\r
++                                  NOTMUCH_DATABASE_MODE_READ_WRITE,\r
++                                  &notmuch);\r
++    if (status)\r
++      goto DONE;\r
++    status = notmuch_database_upgrade (notmuch, NULL, NULL);\r
++    if (status) {\r
++      notmuch_database_close(notmuch);\r
++      notmuch = NULL;\r
++    }\r
+ \r
+   DONE:\r
+     if (notmuch_path)\r
+       talloc_free (notmuch_path);\r
+ \r
+-    return notmuch;\r
++    *database = notmuch;\r
++    return status;\r
+ }\r
+ \r
+ notmuch_status_t\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 8a011f2..d880aeb 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -140,11 +140,11 @@ typedef struct _notmuch_filenames notmuch_filenames_t;\r
+  * contained within 'path' can be added to the database by calling\r
+  * notmuch_database_add_message.\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
+-notmuch_database_t *\r
+-notmuch_database_create (const char *path);\r
++notmuch_status_t\r
++notmuch_database_create (const char *path, notmuch_database_t **database);\r
+ \r
+ typedef enum {\r
+     NOTMUCH_DATABASE_MODE_READ_ONLY = 0,\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 7788743..cb720cc 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -900,7 +900,8 @@ 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
++      if (notmuch_database_create (db_path, &notmuch))\r
++          return 1;\r
+       add_files_state.total_files = count;\r
+     } else {\r
+       if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
+-- \r
+1.7.9.1\r
+\r