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.
This commit is contained in:
Austin Clements 2012-04-30 12:25:33 -04:00 committed by David Bremner
parent 1579fb334e
commit 5fddc07dc3
11 changed files with 60 additions and 37 deletions

View file

@ -556,8 +556,9 @@ notmuch_database_create (const char *path)
goto DONE; goto DONE;
} }
notmuch = notmuch_database_open (path, notmuch_database_open (path,
NOTMUCH_DATABASE_MODE_READ_WRITE); NOTMUCH_DATABASE_MODE_READ_WRITE,
&notmuch);
notmuch_database_upgrade (notmuch, NULL, NULL); notmuch_database_upgrade (notmuch, NULL, NULL);
DONE: DONE:
@ -578,10 +579,12 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)
return NOTMUCH_STATUS_SUCCESS; return NOTMUCH_STATUS_SUCCESS;
} }
notmuch_database_t * notmuch_status_t
notmuch_database_open (const char *path, 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); void *local = talloc_new (NULL);
notmuch_database_t *notmuch = NULL; notmuch_database_t *notmuch = NULL;
char *notmuch_path, *xapian_path; char *notmuch_path, *xapian_path;
@ -590,8 +593,15 @@ notmuch_database_open (const char *path,
unsigned int i, version; unsigned int i, version;
static int initialized = 0; 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"))) { if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
status = NOTMUCH_STATUS_OUT_OF_MEMORY;
goto DONE; goto DONE;
} }
@ -599,11 +609,13 @@ notmuch_database_open (const char *path,
if (err) { if (err) {
fprintf (stderr, "Error opening database at %s: %s\n", fprintf (stderr, "Error opening database at %s: %s\n",
notmuch_path, strerror (errno)); notmuch_path, strerror (errno));
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE; goto DONE;
} }
if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) { if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
status = NOTMUCH_STATUS_OUT_OF_MEMORY;
goto DONE; goto DONE;
} }
@ -644,6 +656,7 @@ notmuch_database_open (const char *path,
notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY; notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
notmuch_database_destroy (notmuch); notmuch_database_destroy (notmuch);
notmuch = NULL; notmuch = NULL;
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE; goto DONE;
} }
@ -704,12 +717,17 @@ notmuch_database_open (const char *path,
error.get_msg().c_str()); error.get_msg().c_str());
notmuch_database_destroy (notmuch); notmuch_database_destroy (notmuch);
notmuch = NULL; notmuch = NULL;
status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
} }
DONE: DONE:
talloc_free (local); talloc_free (local);
return notmuch; if (database)
*database = notmuch;
else
talloc_free (notmuch);
return status;
} }
void void

View file

@ -151,9 +151,6 @@ typedef enum {
NOTMUCH_DATABASE_MODE_READ_WRITE NOTMUCH_DATABASE_MODE_READ_WRITE
} notmuch_database_mode_t; } 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'. /* Open an existing notmuch database located at 'path'.
* *
* The database should have been created at some time in the past, * 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 * The caller should call notmuch_database_destroy when finished with
* this database. * this database.
* *
* In case of any failure, this function returns NULL, (after printing * In case of any failure, this function returns an error status and
* an error message on stderr). * 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_open (const char *path,
notmuch_database_mode_t mode); notmuch_database_mode_t mode,
notmuch_database_t **database);
/* Close the given notmuch database. /* Close the given notmuch database.
* *

View file

@ -66,9 +66,8 @@ notmuch_count_command (void *ctx, int argc, char *argv[])
if (config == NULL) if (config == NULL)
return 1; return 1;
notmuch = notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY); NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
if (notmuch == NULL)
return 1; return 1;
query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index); query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index);

View file

@ -36,9 +36,8 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
if (config == NULL) if (config == NULL)
return 1; return 1;
notmuch = notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY); NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
if (notmuch == NULL)
return 1; return 1;
char *output_file_name = NULL; char *output_file_name = NULL;

View file

@ -903,9 +903,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
notmuch = notmuch_database_create (db_path); notmuch = notmuch_database_create (db_path);
add_files_state.total_files = count; add_files_state.total_files = count;
} else { } else {
notmuch = notmuch_database_open (db_path, if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
NOTMUCH_DATABASE_MODE_READ_WRITE); &notmuch))
if (notmuch == NULL)
return 1; return 1;
if (notmuch_database_needs_upgrade (notmuch)) { if (notmuch_database_needs_upgrade (notmuch)) {

View file

@ -740,9 +740,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
return 1; return 1;
} }
notmuch = notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY); NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
if (notmuch == NULL)
return 1; return 1;
query = notmuch_query_create (notmuch, query_string); query = notmuch_query_create (notmuch, query_string);

View file

@ -115,9 +115,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
if (config == NULL) if (config == NULL)
return 1; return 1;
notmuch = notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_WRITE); NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
if (notmuch == NULL)
return 1; return 1;
synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config); synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);

View file

@ -486,9 +486,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
if (config == NULL) if (config == NULL)
return 1; return 1;
notmuch = notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY); NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
if (notmuch == NULL)
return 1; return 1;
query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index); query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);

View file

@ -1081,9 +1081,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
return 1; return 1;
} }
notmuch = notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY); NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
if (notmuch == NULL)
return 1; return 1;
query = notmuch_query_create (notmuch, query_string); query = notmuch_query_create (notmuch, query_string);

View file

@ -229,9 +229,8 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
if (config == NULL) if (config == NULL)
return 1; return 1;
notmuch = notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_WRITE); NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
if (notmuch == NULL)
return 1; return 1;
synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config); synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);

View file

@ -4,7 +4,8 @@
int main() { int main() {
(void) notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY); notmuch_database_t *notmuch;
notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch);
try { try {
(void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN); (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);