goto DONE;
}
- notmuch = notmuch_database_open (path,
- NOTMUCH_DATABASE_MODE_READ_WRITE);
+ notmuch_database_open (path,
+ NOTMUCH_DATABASE_MODE_READ_WRITE,
+ ¬much);
notmuch_database_upgrade (notmuch, NULL, NULL);
DONE:
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;
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;
}
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;
}
notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
notmuch_database_destroy (notmuch);
notmuch = NULL;
+ status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
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
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,
* 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.
*
if (config == NULL)
return 1;
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
- NOTMUCH_DATABASE_MODE_READ_ONLY);
- if (notmuch == NULL)
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
return 1;
query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
if (config == NULL)
return 1;
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
- NOTMUCH_DATABASE_MODE_READ_ONLY);
- if (notmuch == NULL)
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
return 1;
char *output_file_name = NULL;
notmuch = notmuch_database_create (db_path);
add_files_state.total_files = count;
} else {
- notmuch = notmuch_database_open (db_path,
- NOTMUCH_DATABASE_MODE_READ_WRITE);
- if (notmuch == NULL)
+ if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
+ ¬much))
return 1;
if (notmuch_database_needs_upgrade (notmuch)) {
return 1;
}
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
- NOTMUCH_DATABASE_MODE_READ_ONLY);
- if (notmuch == NULL)
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
return 1;
query = notmuch_query_create (notmuch, query_string);
if (config == NULL)
return 1;
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
- NOTMUCH_DATABASE_MODE_READ_WRITE);
- if (notmuch == NULL)
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
return 1;
synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
if (config == NULL)
return 1;
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
- NOTMUCH_DATABASE_MODE_READ_ONLY);
- if (notmuch == NULL)
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
return 1;
query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
return 1;
}
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
- NOTMUCH_DATABASE_MODE_READ_ONLY);
- if (notmuch == NULL)
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
return 1;
query = notmuch_query_create (notmuch, query_string);
if (config == NULL)
return 1;
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
- NOTMUCH_DATABASE_MODE_READ_WRITE);
- if (notmuch == NULL)
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
return 1;
synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
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, ¬much);
try {
(void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);