X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=lib%2Fdatabase.cc;h=f8b95bc46f5fcacf61839c30b4b1cf17a124c9f4;hp=92c3c4e0a7f03088f9a8337233fc35056605e9fc;hb=7a8046ced8c0e61ddd0ff463cfc17ed63e6edad3;hpb=e59cc0031fbf84f49e32dedb9927f427d2c49309 diff --git a/lib/database.cc b/lib/database.cc index 92c3c4e0..f8b95bc4 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -1747,72 +1747,60 @@ notmuch_status_t notmuch_database_remove_message (notmuch_database_t *notmuch, const char *filename) { - Xapian::WritableDatabase *db; + notmuch_message_t *message = + notmuch_database_find_message_by_filename (notmuch, filename); + notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; + + if (message) { + status = _notmuch_message_remove_filename (message, filename); + if (status == NOTMUCH_STATUS_SUCCESS) + _notmuch_message_delete (message); + else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) + _notmuch_message_sync (message); + } + + return status; +} + +notmuch_message_t * +notmuch_database_find_message_by_filename (notmuch_database_t *notmuch, + const char *filename) +{ void *local; const char *prefix = _find_prefix ("file-direntry"); char *direntry, *term; Xapian::PostingIterator i, end; - Xapian::Document document; + notmuch_message_t *message = NULL; notmuch_status_t status; - status = _notmuch_database_ensure_writable (notmuch); - if (status) - return status; - local = talloc_new (notmuch); - db = static_cast (notmuch->xapian_db); - try { - status = _notmuch_database_filename_to_direntry (local, notmuch, filename, &direntry); if (status) - return status; + return NULL; term = talloc_asprintf (local, "%s%s", prefix, direntry); find_doc_ids_for_term (notmuch, term, &i, &end); - for ( ; i != end; i++) { - Xapian::TermIterator j; - notmuch_message_t *message; + if (i != end) { notmuch_private_status_t private_status; - message = _notmuch_message_create (local, notmuch, + message = _notmuch_message_create (notmuch, notmuch, *i, &private_status); - if (message == NULL) - return COERCE_STATUS (private_status, - "Inconsistent document ID in datbase."); - - _notmuch_message_remove_filename (message, filename); - _notmuch_message_sync (message); - - /* Take care to find document after sync'ing filename removal. */ - document = find_document_for_doc_id (notmuch, *i); - j = document.termlist_begin (); - j.skip_to (prefix); - - /* Was this the last file-direntry in the message? */ - if (j == document.termlist_end () || - strncmp ((*j).c_str (), prefix, strlen (prefix))) - { - db->delete_document (document.get_docid ()); - status = NOTMUCH_STATUS_SUCCESS; - } else { - status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID; - } } } catch (const Xapian::Error &error) { - fprintf (stderr, "Error: A Xapian exception occurred removing message: %s\n", + fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n", error.get_msg().c_str()); notmuch->exception_reported = TRUE; - status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; + message = NULL; } talloc_free (local); - return status; + return message; } notmuch_string_list_t *