]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
Prefer READ_ONLY consistently over READONLY.
[notmuch] / lib / database.cc
index dc967c8c8ae62b65e25168747468a68d46c14d0e..3de7f2950d6d073b5f5599558902f6c2efb00e8e 100644 (file)
@@ -198,7 +198,7 @@ notmuch_status_to_string (notmuch_status_t status)
        return "No error occurred";
     case NOTMUCH_STATUS_OUT_OF_MEMORY:
        return "Out of memory";
-    case NOTMUCH_STATUS_READONLY_DATABASE:
+    case NOTMUCH_STATUS_READ_ONLY_DATABASE:
        return "Attempt to write to a read-only database";
     case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
        return "A Xapian exception occurred";
@@ -475,6 +475,17 @@ notmuch_database_create (const char *path)
     return notmuch;
 }
 
+notmuch_status_t
+_notmuch_database_ensure_writable (notmuch_database_t *notmuch)
+{
+    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
+       fprintf (stderr, "Cannot write to a read-only database.\n");
+       return NOTMUCH_STATUS_READ_ONLY_DATABASE;
+    }
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 notmuch_database_t *
 notmuch_database_open (const char *path,
                       notmuch_database_mode_t mode)
@@ -723,6 +734,9 @@ _notmuch_database_get_directory_path (void *ctx,
  * database path or absolute with initial components identical to
  * database path), return a new string (with 'ctx' as the talloc
  * owner) suitable for use as a direntry term value.
+ *
+ * The necessary directory documents will be created in the database
+ * as needed.
  */
 notmuch_status_t
 _notmuch_database_filename_to_direntry (void *ctx,
@@ -1031,11 +1045,13 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
     if (message_ret)
        *message_ret = NULL;
 
+    ret = _notmuch_database_ensure_writable (notmuch);
+    if (ret)
+       return ret;
+
     message_file = notmuch_message_file_open (filename);
-    if (message_file == NULL) {
-       ret = NOTMUCH_STATUS_FILE_ERROR;
-       goto DONE;
-    }
+    if (message_file == NULL)
+       return NOTMUCH_STATUS_FILE_ERROR;
 
     notmuch_message_file_restrict_headers (message_file,
                                           "date",
@@ -1170,10 +1186,9 @@ notmuch_database_remove_message (notmuch_database_t *notmuch,
     Xapian::Document document;
     notmuch_status_t status;
 
-    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
-       fprintf (stderr, "Attempted to update a read-only database.\n");
-       return NOTMUCH_STATUS_READONLY_DATABASE;
-    }
+    status = _notmuch_database_ensure_writable (notmuch);
+    if (status)
+       return status;
 
     db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);