aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorGravatar Austin Clements <amdragon@MIT.EDU>2012-04-30 12:25:33 -0400
committerGravatar David Bremner <bremner@debian.org>2012-05-05 10:11:57 -0300
commit5fddc07dc31481453c1af186bf7da241c00cdbf1 (patch)
treedfbe4bbabbac3b4c095c7528acdbcf54a2e3c78d /lib
parent1579fb334e0bbc42d5ae7692b184127f43ed7027 (diff)
lib/cli: Make notmuch_database_open return a status code
It has been a long-standing issue that notmuch_database_open doesn't return any indication of why it failed. This patch changes its prototype to return a notmuch_status_t and set an out-argument to the database itself, like other functions that return both a status and an object. In the interest of atomicity, this also updates every use in the CLI so that notmuch still compiles. Since this patch does not update the bindings, the Python bindings test fails.
Diffstat (limited to 'lib')
-rw-r--r--lib/database.cc28
-rw-r--r--lib/notmuch.h26
2 files changed, 42 insertions, 12 deletions
diff --git a/lib/database.cc b/lib/database.cc
index 2fefcad7..1e665992 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -556,8 +556,9 @@ notmuch_database_create (const char *path)
goto DONE;
}
- notmuch = notmuch_database_open (path,
- NOTMUCH_DATABASE_MODE_READ_WRITE);
+ notmuch_database_open (path,
+ NOTMUCH_DATABASE_MODE_READ_WRITE,
+ &notmuch);
notmuch_database_upgrade (notmuch, NULL, NULL);
DONE:
@@ -578,10 +579,12 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)
return NOTMUCH_STATUS_SUCCESS;
}
-notmuch_database_t *
+notmuch_status_t
notmuch_database_open (const char *path,
- notmuch_database_mode_t mode)
+ notmuch_database_mode_t mode,
+ notmuch_database_t **database)
{
+ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
void *local = talloc_new (NULL);
notmuch_database_t *notmuch = NULL;
char *notmuch_path, *xapian_path;
@@ -590,8 +593,15 @@ notmuch_database_open (const char *path,
unsigned int i, version;
static int initialized = 0;
+ if (path == NULL) {
+ fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");
+ status = NOTMUCH_STATUS_NULL_POINTER;
+ goto DONE;
+ }
+
if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
fprintf (stderr, "Out of memory\n");
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;
goto DONE;
}
@@ -599,11 +609,13 @@ notmuch_database_open (const char *path,
if (err) {
fprintf (stderr, "Error opening database at %s: %s\n",
notmuch_path, strerror (errno));
+ status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
fprintf (stderr, "Out of memory\n");
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;
goto DONE;
}
@@ -644,6 +656,7 @@ notmuch_database_open (const char *path,
notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
notmuch_database_destroy (notmuch);
notmuch = NULL;
+ status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
@@ -704,12 +717,17 @@ notmuch_database_open (const char *path,
error.get_msg().c_str());
notmuch_database_destroy (notmuch);
notmuch = NULL;
+ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
}
DONE:
talloc_free (local);
- return notmuch;
+ if (database)
+ *database = notmuch;
+ else
+ talloc_free (notmuch);
+ return status;
}
void
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 7d9e0921..44b0c460 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -151,9 +151,6 @@ typedef enum {
NOTMUCH_DATABASE_MODE_READ_WRITE
} notmuch_database_mode_t;
-/* XXX: I think I'd like this to take an extra argument of
- * notmuch_status_t* for returning a status value on failure. */
-
/* Open an existing notmuch database located at 'path'.
*
* The database should have been created at some time in the past,
@@ -168,12 +165,27 @@ typedef enum {
* The caller should call notmuch_database_destroy when finished with
* this database.
*
- * 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 opened 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 open the
+ * database file (such as permission denied, or file not found,
+ * etc.), or the database version is unknown.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
*/
-notmuch_database_t *
+notmuch_status_t
notmuch_database_open (const char *path,
- notmuch_database_mode_t mode);
+ notmuch_database_mode_t mode,
+ notmuch_database_t **database);
/* Close the given notmuch database.
*