]> git.notmuchmail.org Git - notmuch/commitdiff
lib/cli: Make notmuch_database_create return a status code
authorAustin Clements <amdragon@MIT.EDU>
Mon, 30 Apr 2012 16:25:34 +0000 (12:25 -0400)
committerDavid Bremner <bremner@debian.org>
Sat, 5 May 2012 13:12:26 +0000 (10:12 -0300)
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
lib/notmuch.h
notmuch-new.c

index 1e66599292855138a91db954d21affc11a7ac7ad..07f186ed942399b6f3947f57eb15a32ce357f945 100644 (file)
@@ -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,30 @@ 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;
+    if (database)
+       *database = notmuch;
+    else
+       talloc_free (notmuch);
+    return status;
 }
 
 notmuch_status_t
index 44b0c4608c39302a3d79666a7b3180eed683cae7..e6e5cc2e01ca84f8a739d44fd6bd7f10891c3171 100644 (file)
@@ -140,11 +140,25 @@ 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).
+ *
+ * Return value:
+ *
+ * NOTMUCH_STATUS_SUCCESS: Successfully created the database.
+ *
+ * NOTMUCH_STATUS_NULL_POINTER: The given 'path' argument is NULL.
+ *
+ * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
+ *
+ * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to create the
+ *     database file (such as permission denied, or file not found,
+ *     etc.), or the database already exists.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
  */
-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,
index 7788743da44dcfca5e8ce5ba1dce085025a79b5c..cb720cc2f9a76d36fd8a4194fd9d9dc07f3029f5 100644 (file)
@@ -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,