]> git.notmuchmail.org Git - notmuch/commitdiff
lib: Add an API to find a message by filename.
authorAustin Clements <amdragon@mit.edu>
Sat, 11 Jun 2011 04:19:31 +0000 (00:19 -0400)
committerDavid Bremner <bremner@debian.org>
Sat, 24 Sep 2011 23:00:28 +0000 (20:00 -0300)
notmuch_database_find_message_by_filename is mostly stolen from
notmuch_database_remove_message, so this patch also vastly simplfies
the latter using the former.

This API is also useful in its own right and will be used in a later
patch for eager maildir flag synchronization.

lib/database.cc
lib/notmuch.h

index cf87d08d06c42cd0b41dfa1ba06d1c504b337ba7..f8b95bc46f5fcacf61839c30b4b1cf17a124c9f4 100644 (file)
@@ -1746,58 +1746,61 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 notmuch_status_t
 notmuch_database_remove_message (notmuch_database_t *notmuch,
                                 const char *filename)
+{
+    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);
 
     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.");
-
-           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);
        }
     } 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 *
index bfa2ced88a3f3893bc43b63ec495ce68fb41cd29..0ed4db5f641d7885db36f3f85bc16c86fba6c0f7 100644 (file)
@@ -356,6 +356,22 @@ notmuch_message_t *
 notmuch_database_find_message (notmuch_database_t *database,
                               const char *message_id);
 
+/* Find a message with the given filename.
+ *
+ * If the database contains a message with the given filename, then a
+ * new notmuch_message_t object is returned.  The caller should call 
+ * notmuch_message_destroy when done with the message.
+ *
+ * This function returns NULL in the following situations:
+ *
+ *     * No message is found with the given filename
+ *     * An out-of-memory situation occurs
+ *     * A Xapian exception occurs
+ */
+notmuch_message_t *
+notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
+                                          const char *filename);
+
 /* Return a list of all tags found in the database.
  *
  * This function creates a list of all tags found in the database. The