unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Subject: [PATCH 2/6] lib/cli: Make notmuch_database_create return a status code
Date: Sat, 28 Apr 2012 18:17:49 -0400	[thread overview]
Message-ID: <1335651473-19652-3-git-send-email-amdragon@mit.edu> (raw)
In-Reply-To: <1335651473-19652-1-git-send-email-amdragon@mit.edu>

This is the notmuch_database_create equivalent of the previous change.

In this case, there were places where errors were not being propagated
correctly in notmuch_database_create or in calls to it.  These have
been fixed, using the new status value.
---
 lib/database.cc |   26 +++++++++++++++++++-------
 lib/notmuch.h   |    8 ++++----
 notmuch-new.c   |    3 ++-
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index a29fe67..a3ae504 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -520,9 +520,10 @@ parse_references (void *ctx,
     }
 }
 
-notmuch_database_t *
-notmuch_database_create (const char *path)
+notmuch_status_t
+notmuch_database_create (const char *path, notmuch_database_t **database)
 {
+    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
     notmuch_database_t *notmuch = NULL;
     char *notmuch_path = NULL;
     struct stat st;
@@ -530,6 +531,7 @@ notmuch_database_create (const char *path)
 
     if (path == NULL) {
 	fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
+	status = NOTMUCH_STATUS_NULL_POINTER;
 	goto DONE;
     }
 
@@ -537,12 +539,14 @@ notmuch_database_create (const char *path)
     if (err) {
 	fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
 		 path, strerror (errno));
+	status = NOTMUCH_STATUS_FILE_ERROR;
 	goto DONE;
     }
 
     if (! S_ISDIR (st.st_mode)) {
 	fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",
 		 path);
+	status = NOTMUCH_STATUS_FILE_ERROR;
 	goto DONE;
     }
 
@@ -553,19 +557,27 @@ notmuch_database_create (const char *path)
     if (err) {
 	fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
 		 notmuch_path, strerror (errno));
+	status = NOTMUCH_STATUS_FILE_ERROR;
 	goto DONE;
     }
 
-    notmuch_database_open (path,
-			   NOTMUCH_DATABASE_MODE_READ_WRITE,
-			   &notmuch);
-    notmuch_database_upgrade (notmuch, NULL, NULL);
+    status = notmuch_database_open (path,
+				    NOTMUCH_DATABASE_MODE_READ_WRITE,
+				    &notmuch);
+    if (status)
+	goto DONE;
+    status = notmuch_database_upgrade (notmuch, NULL, NULL);
+    if (status) {
+	notmuch_database_close(notmuch);
+	notmuch = NULL;
+    }
 
   DONE:
     if (notmuch_path)
 	talloc_free (notmuch_path);
 
-    return notmuch;
+    *database = notmuch;
+    return status;
 }
 
 notmuch_status_t
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 8a011f2..d880aeb 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -140,11 +140,11 @@ typedef struct _notmuch_filenames notmuch_filenames_t;
  * contained within 'path' can be added to the database by calling
  * notmuch_database_add_message.
  *
- * In case of any failure, this function returns NULL, (after printing
- * an error message on stderr).
+ * In case of any failure, this function returns an error status and
+ * sets *database to NULL (after printing an error message on stderr).
  */
-notmuch_database_t *
-notmuch_database_create (const char *path);
+notmuch_status_t
+notmuch_database_create (const char *path, notmuch_database_t **database);
 
 typedef enum {
     NOTMUCH_DATABASE_MODE_READ_ONLY = 0,
diff --git a/notmuch-new.c b/notmuch-new.c
index 7788743..cb720cc 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -900,7 +900,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 	    return 1;
 
 	printf ("Found %d total files (that's not much mail).\n", count);
-	notmuch = notmuch_database_create (db_path);
+	if (notmuch_database_create (db_path, &notmuch))
+	    return 1;
 	add_files_state.total_files = count;
     } else {
 	if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
-- 
1.7.9.1

  parent reply	other threads:[~2012-04-28 22:18 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-28 22:17 [PATCH 0/6] Make notmuch_database_{open,create} return status codes Austin Clements
2012-04-28 22:17 ` [PATCH 1/6] lib/cli: Make notmuch_database_open return a status code Austin Clements
2012-04-30 11:39   ` David Bremner
2012-04-30 16:15     ` Austin Clements
2012-04-28 22:17 ` Austin Clements [this message]
2012-04-28 22:17 ` [PATCH 3/6] go: Update Go bindings for new notmuch_database_{open, create} signatures Austin Clements
2012-04-28 22:17 ` [PATCH 4/6] python: Update Python " Austin Clements
2012-04-28 22:17 ` [PATCH 5/6] ruby: Update Ruby " Austin Clements
2012-04-28 22:17 ` [PATCH 6/6] News for changes to notmuch_database_{open,create} Austin Clements
2012-04-29 11:23 ` [PATCH 0/6] Make notmuch_database_{open, create} return status codes Justus Winter
2012-04-30 16:25 ` [PATCH v2 " Austin Clements
2012-04-30 16:25   ` [PATCH v2 1/6] lib/cli: Make notmuch_database_open return a status code Austin Clements
2012-05-05 23:21     ` David Bremner
2012-04-30 16:25   ` [PATCH v2 2/6] lib/cli: Make notmuch_database_create " Austin Clements
2012-04-30 16:25   ` [PATCH v2 3/6] go: Update Go bindings for new notmuch_database_{open, create} signatures Austin Clements
2012-04-30 16:25   ` [PATCH v2 4/6] python: Update Python " Austin Clements
2012-04-30 16:25   ` [PATCH v2 5/6] ruby: Update Ruby " Austin Clements
2012-04-30 16:25   ` [PATCH v2 6/6] News for changes to notmuch_database_{open,create} Austin Clements
2012-05-02 19:08   ` [PATCH v2 0/6] Make notmuch_database_{open, create} return status codes Tomi Ollila
2012-05-03  0:23     ` Austin Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1335651473-19652-3-git-send-email-amdragon@mit.edu \
    --to=amdragon@mit.edu \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).