X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;f=lib%2Fdatabase.cc;h=807e39eda50917b5f46ad800f381641cabc60461;hb=f93b7218c3e2d11c5b3cdd4c367a42ca7a7ede77;hp=510d13cbcd8ff09972164603b8adfe32495af4e5;hpb=d807e28f43579ecc91aa40ae3e42760991c2f810;p=notmuch diff --git a/lib/database.cc b/lib/database.cc index 510d13cb..807e39ed 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -199,7 +199,7 @@ notmuch_status_to_string (notmuch_status_t status) case NOTMUCH_STATUS_OUT_OF_MEMORY: return "Out of memory"; case NOTMUCH_STATUS_READONLY_DATABASE: - return "The database is read-only"; + return "Attempt to write to a read-only database"; case NOTMUCH_STATUS_XAPIAN_EXCEPTION: return "A Xapian exception occurred"; case NOTMUCH_STATUS_FILE_ERROR: @@ -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); @@ -1201,14 +1216,16 @@ notmuch_database_remove_message (notmuch_database_t *notmuch, strncmp ((*j).c_str (), prefix, strlen (prefix))) { db->delete_document (document.get_docid ()); + status = NOTMUCH_STATUS_SUCCESS; } else { db->replace_document (document.get_docid (), document); + status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID; } } talloc_free (local); - return NOTMUCH_STATUS_SUCCESS; + return status; } notmuch_tags_t *