diff options
author | Carl Worth <cworth@cworth.org> | 2009-12-21 15:14:32 -0800 |
---|---|---|
committer | Carl Worth <cworth@cworth.org> | 2010-01-06 10:32:06 -0800 |
commit | f11aaa3678ab9d02d4ea98db1164772ed637404b (patch) | |
tree | cacb126e04b9123dd38795f1862b3a9884672db5 /lib/database.cc | |
parent | 44a74912c78526c1942a101a8172206a32885425 (diff) |
database: Add new, public notmuch_database_remove_message
This will allow applications to support the removal of messages, (such
as when a file is deleted from the mail store). No removal support is
provided yet in commands such as "notmuch new".
Diffstat (limited to 'lib/database.cc')
-rw-r--r-- | lib/database.cc | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/database.cc b/lib/database.cc index cd3346fd..a3824956 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -1278,6 +1278,59 @@ notmuch_database_add_message (notmuch_database_t *notmuch, return ret; } +notmuch_status_t +notmuch_database_remove_message (notmuch_database_t *notmuch, + const char *filename) +{ + Xapian::WritableDatabase *db; + void *local = talloc_new (notmuch); + const char *direntry_prefix = _find_prefix ("direntry"); + char *direntry, *term; + Xapian::PostingIterator i, end; + 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; + } + + db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db); + + status = _notmuch_database_filename_to_direntry (local, notmuch, + filename, &direntry); + if (status) + return status; + + term = talloc_asprintf (notmuch, "%s%s", direntry_prefix, direntry); + + find_doc_ids_for_term (notmuch, term, &i, &end); + + for ( ; i != end; i++) { + Xapian::TermIterator j; + + document = find_document_for_doc_id (notmuch, *i); + + document.remove_term (term); + + j = document.termlist_begin (); + j.skip_to (direntry_prefix); + + /* Was this the last direntry in the message? */ + if (j == document.termlist_end () || + strncmp ((*j).c_str (), direntry_prefix, strlen (direntry_prefix))) + { + db->delete_document (document.get_docid ()); + } else { + db->replace_document (document.get_docid (), document); + } + } + + talloc_free (local); + + return NOTMUCH_STATUS_SUCCESS; +} + notmuch_tags_t * _notmuch_convert_tags (void *ctx, Xapian::TermIterator &i, Xapian::TermIterator &end) |