aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/database.cc
diff options
context:
space:
mode:
authorGravatar Carl Worth <cworth@cworth.org>2010-01-07 10:19:44 -0800
committerGravatar Carl Worth <cworth@cworth.org>2010-01-07 10:19:44 -0800
commitf93b7218c3e2d11c5b3cdd4c367a42ca7a7ede77 (patch)
tree7d1cd35cb6e21d2c1dbf3bf79799fbeb35ae6422 /lib/database.cc
parent6ed606c19edfe06d2dfd48854fc97f8502eaaf7c (diff)
lib: Consolidate checks for read-only database.
Previously, many checks were deep in the library just before a cast operation. These have now been replaced with internal errors and new checks have instead been added at the beginning of all top-levelentry points requiring a read-write database. The new checks now also use a single function for checking and printing the error message. This will give us a convenient location to extend the check, (such as based on database version as well).
Diffstat (limited to 'lib/database.cc')
-rw-r--r--lib/database.cc28
1 files changed, 20 insertions, 8 deletions
diff --git a/lib/database.cc b/lib/database.cc
index 125b37eb..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)
@@ -1034,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",
@@ -1173,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);