]> git.notmuchmail.org Git - notmuch/blobdiff - lib/message.cc
lib: replace use of static_cast for writable databases
[notmuch] / lib / message.cc
index 8e43a393c55171b425175537298c0ff0fecb6c3b..fca99082a8483cba035fba37f9a1bc1ebaab0b4e 100644 (file)
@@ -96,7 +96,7 @@ static void
 _log_xapian_exception (const char *where, notmuch_message_t *message,  const Xapian::Error error) {
     notmuch_database_t *notmuch = notmuch_message_get_database (message);
     _notmuch_database_log (notmuch,
-                          "A Xapian exception occurred %s retrieving %s : %s\n",
+                          "A Xapian exception occurred at %s: %s\n",
                           where,
                           error.get_msg ().c_str ());
     notmuch->exception_reported = true;
@@ -275,7 +275,7 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
        return NULL;
     }
 
-    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
+    if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
        INTERNAL_ERROR ("Failure to ensure database is writable.");
 
     try {
@@ -1156,20 +1156,50 @@ notmuch_message_get_filenames (notmuch_message_t *message)
 int
 notmuch_message_count_files (notmuch_message_t *message)
 {
-    _notmuch_message_ensure_filename_list (message);
+    try {
+       _notmuch_message_ensure_filename_list (message);
+    } catch (Xapian::Error &error) {
+       LOG_XAPIAN_EXCEPTION (message, error);
+       return -1;
+    }
 
     return _notmuch_string_list_length (message->filename_list);
 }
 
+notmuch_status_t
+notmuch_message_get_flag_st (notmuch_message_t *message,
+                            notmuch_message_flag_t flag,
+                            notmuch_bool_t *is_set)
+{
+    if (! is_set)
+       return NOTMUCH_STATUS_NULL_POINTER;
+
+    try {
+       if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
+           ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
+           _notmuch_message_ensure_metadata (message, NULL);
+    } catch (Xapian::Error &error) {
+       LOG_XAPIAN_EXCEPTION (message, error);
+       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
+    }
+
+    *is_set = NOTMUCH_TEST_BIT (message->flags, flag);
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 notmuch_bool_t
 notmuch_message_get_flag (notmuch_message_t *message,
                          notmuch_message_flag_t flag)
 {
-    if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
-       ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
-       _notmuch_message_ensure_metadata (message, NULL);
+    notmuch_bool_t is_set;
+    notmuch_status_t status;
+
+    status = notmuch_message_get_flag_st (message, flag, &is_set);
 
-    return NOTMUCH_TEST_BIT (message->flags, flag);
+    if (status)
+       return FALSE;
+    else
+       return is_set;
 }
 
 void
@@ -1191,9 +1221,7 @@ notmuch_message_get_date (notmuch_message_t *message)
     try {
        value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
     } catch (Xapian::Error &error) {
-       _notmuch_database_log (notmuch_message_get_database (message), "A Xapian exception occurred when reading date: %s\n",
-                              error.get_msg ().c_str ());
-       message->notmuch->exception_reported = true;
+       LOG_XAPIAN_EXCEPTION (message, error);
        return 0;
     }
 
@@ -1208,7 +1236,12 @@ notmuch_message_get_tags (notmuch_message_t *message)
 {
     notmuch_tags_t *tags;
 
-    _notmuch_message_ensure_metadata (message, message->tag_list);
+    try {
+       _notmuch_message_ensure_metadata (message, message->tag_list);
+    } catch (Xapian::Error &error) {
+       LOG_XAPIAN_EXCEPTION (message, error);
+       return NULL;
+    }
 
     tags = _notmuch_tags_create (message, message->tag_list);
     /* _notmuch_tags_create steals the reference to the tag_list, but
@@ -1289,9 +1322,7 @@ _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
 void
 _notmuch_message_sync (notmuch_message_t *message)
 {
-    Xapian::WritableDatabase *db;
-
-    if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
+    if (_notmuch_database_mode (message->notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
        return;
 
     if (! message->modified)
@@ -1309,8 +1340,8 @@ _notmuch_message_sync (notmuch_message_t *message)
                                    _notmuch_database_new_revision (
                                        message->notmuch)));
 
-    db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
-    db->replace_document (message->doc_id, message->doc);
+    message->notmuch->writable_xapian_db->
+       replace_document (message->doc_id, message->doc);
     message->modified = false;
 }
 
@@ -1320,7 +1351,6 @@ notmuch_status_t
 _notmuch_message_delete (notmuch_message_t *message)
 {
     notmuch_status_t status;
-    Xapian::WritableDatabase *db;
     const char *mid, *tid, *query_string;
     notmuch_message_t *ghost;
     notmuch_private_status_t private_status;
@@ -1337,8 +1367,7 @@ _notmuch_message_delete (notmuch_message_t *message)
     if (status)
        return status;
 
-    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
-    db->delete_document (message->doc_id);
+    message->notmuch->writable_xapian_db->delete_document (message->doc_id);
 
     /* if this was a ghost to begin with, we are done */
     private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
@@ -1581,24 +1610,31 @@ notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
     notmuch_private_status_t private_status;
     notmuch_status_t status;
 
-    status = _notmuch_database_ensure_writable (message->notmuch);
-    if (status)
-       return status;
+    try {
+       status = _notmuch_database_ensure_writable (message->notmuch);
+       if (status)
+           return status;
 
-    if (tag == NULL)
-       return NOTMUCH_STATUS_NULL_POINTER;
+       if (tag == NULL)
+           return NOTMUCH_STATUS_NULL_POINTER;
 
-    if (strlen (tag) > NOTMUCH_TAG_MAX)
-       return NOTMUCH_STATUS_TAG_TOO_LONG;
+       if (strlen (tag) > NOTMUCH_TAG_MAX)
+           return NOTMUCH_STATUS_TAG_TOO_LONG;
 
-    private_status = _notmuch_message_add_term (message, "tag", tag);
-    if (private_status) {
-       INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
-                       private_status);
-    }
+       private_status = _notmuch_message_add_term (message, "tag", tag);
+       if (private_status) {
+           return COERCE_STATUS (private_status,
+                                 "_notmuch_message_remove_term return unexpected value: %d\n",
+                                 private_status);
+       }
 
-    if (! message->frozen)
-       _notmuch_message_sync (message);
+       if (! message->frozen)
+           _notmuch_message_sync (message);
+
+    } catch (Xapian::Error &error) {
+       LOG_XAPIAN_EXCEPTION (message, error);
+       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
+    }
 
     return NOTMUCH_STATUS_SUCCESS;
 }
@@ -1609,24 +1645,30 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
     notmuch_private_status_t private_status;
     notmuch_status_t status;
 
-    status = _notmuch_database_ensure_writable (message->notmuch);
-    if (status)
-       return status;
+    try {
+       status = _notmuch_database_ensure_writable (message->notmuch);
+       if (status)
+           return status;
 
-    if (tag == NULL)
-       return NOTMUCH_STATUS_NULL_POINTER;
+       if (tag == NULL)
+           return NOTMUCH_STATUS_NULL_POINTER;
 
-    if (strlen (tag) > NOTMUCH_TAG_MAX)
-       return NOTMUCH_STATUS_TAG_TOO_LONG;
+       if (strlen (tag) > NOTMUCH_TAG_MAX)
+           return NOTMUCH_STATUS_TAG_TOO_LONG;
 
-    private_status = _notmuch_message_remove_term (message, "tag", tag);
-    if (private_status) {
-       INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
-                       private_status);
-    }
+       private_status = _notmuch_message_remove_term (message, "tag", tag);
+       if (private_status) {
+           return COERCE_STATUS (private_status,
+                                 "_notmuch_message_remove_term return unexpected value: %d\n",
+                                 private_status);
+       }
 
-    if (! message->frozen)
-       _notmuch_message_sync (message);
+       if (! message->frozen)
+           _notmuch_message_sync (message);
+    } catch (Xapian::Error &error) {
+       LOG_XAPIAN_EXCEPTION (message, error);
+       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
+    }
 
     return NOTMUCH_STATUS_SUCCESS;
 }
@@ -1671,7 +1713,7 @@ _filename_is_in_maildir (const char *filename)
     return NULL;
 }
 
-static void
+static notmuch_status_t
 _ensure_maildir_flags (notmuch_message_t *message, bool force)
 {
     const char *flags;
@@ -1686,8 +1728,10 @@ _ensure_maildir_flags (notmuch_message_t *message, bool force)
            message->maildir_flags = NULL;
        }
     }
-
-    for (filenames = notmuch_message_get_filenames (message);
+    filenames = notmuch_message_get_filenames (message);
+    if (! filenames)
+       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
+    for (;
         notmuch_filenames_valid (filenames);
         notmuch_filenames_move_to_next (filenames)) {
        filename = notmuch_filenames_get (filenames);
@@ -1713,13 +1757,37 @@ _ensure_maildir_flags (notmuch_message_t *message, bool force)
     }
     if (seen_maildir_info)
        message->maildir_flags = combined_flags;
+    return NOTMUCH_STATUS_SUCCESS;
 }
 
 notmuch_bool_t
 notmuch_message_has_maildir_flag (notmuch_message_t *message, char flag)
 {
-    _ensure_maildir_flags (message, false);
-    return message->maildir_flags && (strchr (message->maildir_flags, flag) != NULL);
+    notmuch_status_t status;
+    notmuch_bool_t ret;
+    status = notmuch_message_has_maildir_flag_st (message, flag, &ret);
+    if (status)
+       return FALSE;
+
+    return ret;
+}
+
+notmuch_status_t
+notmuch_message_has_maildir_flag_st (notmuch_message_t *message,
+                                    char flag,
+                                    notmuch_bool_t *is_set)
+{
+    notmuch_status_t status;
+    
+    if (! is_set)
+       return NOTMUCH_STATUS_NULL_POINTER;
+
+    status = _ensure_maildir_flags (message, false);
+    if (status)
+       return status;
+    
+    *is_set =  message->maildir_flags && (strchr (message->maildir_flags, flag) != NULL);
+    return NOTMUCH_STATUS_SUCCESS;
 }
 
 notmuch_status_t
@@ -1728,7 +1796,9 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
     notmuch_status_t status;
     unsigned i;
 
-    _ensure_maildir_flags (message, true);
+    status = _ensure_maildir_flags (message, true);
+    if (status)
+       return status;
     /* If none of the filenames have any maildir info field (not even
      * an empty info with no flags set) then there's no information to
      * go on, so do nothing. */
@@ -1997,16 +2067,20 @@ notmuch_message_remove_all_tags (notmuch_message_t *message)
     status = _notmuch_database_ensure_writable (message->notmuch);
     if (status)
        return status;
+    tags = notmuch_message_get_tags (message);
+    if (! tags)
+       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
 
-    for (tags = notmuch_message_get_tags (message);
+    for (;
         notmuch_tags_valid (tags);
         notmuch_tags_move_to_next (tags)) {
        tag = notmuch_tags_get (tags);
 
        private_status = _notmuch_message_remove_term (message, "tag", tag);
        if (private_status) {
-           INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
-                           private_status);
+           return COERCE_STATUS (private_status,
+                                  "_notmuch_message_remove_term return unexpected value: %d\n",
+                                  private_status);
        }
     }
 
@@ -2127,8 +2201,10 @@ notmuch_message_reindex (notmuch_message_t *message,
     /* Save in case we need to delete message */
     orig_thread_id = notmuch_message_get_thread_id (message);
     if (! orig_thread_id) {
-       /* XXX TODO: make up new error return? */
-       INTERNAL_ERROR ("message without thread-id");
+       /* the following is correct as long as there is only one reason
+          n_m_get_thread_id returns NULL
+       */
+       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
     }
 
     /* strdup it because the metadata may be invalidated */