X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;f=lib%2Fdatabase.cc;h=807e39eda50917b5f46ad800f381641cabc60461;hb=f93b7218c3e2d11c5b3cdd4c367a42ca7a7ede77;hp=dc967c8c8ae62b65e25168747468a68d46c14d0e;hpb=4b418343f656c2de5503d907d075019626561373;p=notmuch diff --git a/lib/database.cc b/lib/database.cc index dc967c8c..807e39ed 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -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_READONLY_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 (notmuch->xapian_db);