struct _notmuch_database {
char *path;
- Xapian::WritableDatabase *xapian_db;
+ notmuch_database_mode_t mode;
+ Xapian::Database *xapian_db;
Xapian::QueryParser *query_parser;
Xapian::TermGenerator *term_gen;
};
return "No error occurred";
case NOTMUCH_STATUS_OUT_OF_MEMORY:
return "Out of memory";
+ case NOTMUCH_STATUS_READONLY_DATABASE:
+ return "The database is read-only";
case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
return "A Xapian exception occurred";
case NOTMUCH_STATUS_FILE_ERROR:
goto DONE;
}
- notmuch = notmuch_database_open (path);
+ notmuch = notmuch_database_open (path,
+ NOTMUCH_DATABASE_MODE_WRITABLE);
DONE:
if (notmuch_path)
}
notmuch_database_t *
-notmuch_database_open (const char *path)
+notmuch_database_open (const char *path,
+ notmuch_database_mode_t mode)
{
notmuch_database_t *notmuch = NULL;
char *notmuch_path = NULL, *xapian_path = NULL;
if (notmuch->path[strlen (notmuch->path) - 1] == '/')
notmuch->path[strlen (notmuch->path) - 1] = '\0';
+ notmuch->mode = mode;
try {
- notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
- Xapian::DB_CREATE_OR_OPEN);
+ if (mode == NOTMUCH_DATABASE_MODE_WRITABLE) {
+ notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
+ Xapian::DB_CREATE_OR_OPEN);
+ } else {
+ notmuch->xapian_db = new Xapian::Database (xapian_path);
+ }
notmuch->query_parser = new Xapian::QueryParser;
notmuch->term_gen = new Xapian::TermGenerator;
notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
void
notmuch_database_close (notmuch_database_t *notmuch)
{
- notmuch->xapian_db->flush ();
+ if (notmuch->mode == NOTMUCH_DATABASE_MODE_WRITABLE)
+ (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();
delete notmuch->term_gen;
delete notmuch->query_parser;
const char *key, time_t timestamp)
{
Xapian::Document doc;
+ Xapian::WritableDatabase *db;
unsigned int doc_id;
notmuch_private_status_t status;
notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
char *db_key = NULL;
+ if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {
+ fprintf (stderr, "Attempted to update a read-only database.\n");
+ return NOTMUCH_STATUS_READONLY_DATABASE;
+ }
+
+ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
db_key = timestamp_db_key (key);
try {
doc.add_term (term);
talloc_free (term);
- notmuch->xapian_db->add_document (doc);
+ db->add_document (doc);
} else {
- notmuch->xapian_db->replace_document (doc_id, doc);
+ db->replace_document (doc_id, doc);
}
} catch (Xapian::Error &error) {
{
notmuch_message_t *message;
Xapian::Document doc;
+ Xapian::WritableDatabase *db;
unsigned int doc_id;
char *term;
+ if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {
+ *status_ret = NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE;
+ return NULL;
+ }
+
*status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
message = notmuch_database_find_message (notmuch, message_id);
return NULL;
}
+ db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
try {
doc.add_term (term);
talloc_free (term);
doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
- doc_id = notmuch->xapian_db->add_document (doc);
+ doc_id = db->add_document (doc);
} catch (const Xapian::Error &error) {
*status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
return NULL;
void
_notmuch_message_sync (notmuch_message_t *message)
{
- Xapian::WritableDatabase *db = message->notmuch->xapian_db;
+ Xapian::WritableDatabase *db;
+
+ if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY)
+ return;
+ db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
db->replace_document (message->doc_id, message->doc);
}
/* First, copy all the public status values. */
NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
+ NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE = NOTMUCH_STATUS_READONLY_DATABASE,
NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
typedef enum _notmuch_status {
NOTMUCH_STATUS_SUCCESS = 0,
NOTMUCH_STATUS_OUT_OF_MEMORY,
+ NOTMUCH_STATUS_READONLY_DATABASE,
NOTMUCH_STATUS_XAPIAN_EXCEPTION,
NOTMUCH_STATUS_FILE_ERROR,
NOTMUCH_STATUS_FILE_NOT_EMAIL,
/* XXX: I think I'd like this to take an extra argument of
* notmuch_status_t* for returning a status value on failure. */
+typedef enum {
+ NOTMUCH_DATABASE_MODE_READONLY = 0,
+ NOTMUCH_DATABASE_MODE_WRITABLE
+} notmuch_database_mode_t;
+
/* Open an existing notmuch database located at 'path'.
*
* The database should have been created at some time in the past,
* (not necessarily by this process), by calling
- * notmuch_database_create with 'path'.
+ * notmuch_database_create with 'path'. By default the database should be
+ * opened for reading only. In order to write to the database you need to
+ * pass the NOTMUCH_DATABASE_MODE_WRITABLE mode.
*
* An existing notmuch database can be identified by the presence of a
* directory named ".notmuch" below 'path'.
* an error message on stderr).
*/
notmuch_database_t *
-notmuch_database_open (const char *path);
+notmuch_database_open (const char *path,
+ notmuch_database_mode_t mode);
/* Close the given notmuch database, freeing all associated
* resources. See notmuch_database_open. */
if (config == NULL)
return 1;
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+ notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READONLY);
if (notmuch == NULL)
return 1;
next);
break;
/* Fatal issues. Don't process anymore. */
+ case NOTMUCH_STATUS_READONLY_DATABASE:
case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
case NOTMUCH_STATUS_OUT_OF_MEMORY:
fprintf (stderr, "Error: %s. Halting processing.\n",
add_files_state.ignore_read_only_directories = FALSE;
add_files_state.total_files = count;
} else {
- notmuch = notmuch_database_open (db_path);
+ notmuch = notmuch_database_open (db_path,
+ NOTMUCH_DATABASE_MODE_READONLY);
add_files_state.ignore_read_only_directories = TRUE;
add_files_state.total_files = 0;
}
return 1;
}
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+ notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READONLY);
if (notmuch == NULL)
return 1;
if (config == NULL)
return 1;
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+ notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_WRITABLE);
if (notmuch == NULL)
return 1;
if (config == NULL)
return 1;
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+ notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READONLY);
if (notmuch == NULL)
return 1;
return 1;
}
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+ notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READONLY);
if (notmuch == NULL)
return 1;
if (config == NULL)
return 1;
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+ notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_WRITABLE);
if (notmuch == NULL)
return 1;