]> git.notmuchmail.org Git - notmuch/blobdiff - database.cc
add_message: Fix to not add multiple documents with the same message ID
[notmuch] / database.cc
index 1c0410c2c176a24f4897a33a70f945129c617aa9..e315f8dc08bf0c2d4411efc157ab75c9c26f4865 100644 (file)
@@ -50,42 +50,6 @@ notmuch_status_to_string (notmuch_status_t status)
     }
 }
 
-/* "128 bits of thread-id ought to be enough for anybody" */
-#define NOTMUCH_THREAD_ID_BITS  128
-#define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
-typedef struct _thread_id {
-    char str[NOTMUCH_THREAD_ID_DIGITS + 1];
-} thread_id_t;
-
-static void
-thread_id_generate (thread_id_t *thread_id)
-{
-    static int seeded = 0;
-    FILE *dev_random;
-    uint32_t value;
-    char *s;
-    int i;
-
-    if (! seeded) {
-       dev_random = fopen ("/dev/random", "r");
-       if (dev_random == NULL) {
-           srand (time (NULL));
-       } else {
-           fread ((void *) &value, sizeof (value), 1, dev_random);
-           srand (value);
-           fclose (dev_random);
-       }
-       seeded = 1;
-    }
-
-    s = thread_id->str;
-    for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
-       value = rand ();
-       sprintf (s, "%08x", value);
-       s += 8;
-    }
-}
-
 /* XXX: We should drop this function and convert all callers to call
  * _notmuch_message_add_term instead. */
 static void
@@ -487,52 +451,38 @@ notmuch_status_t
 notmuch_database_add_message (notmuch_database_t *notmuch,
                              const char *filename)
 {
-    Xapian::WritableDatabase *db = notmuch->xapian_db;
-    Xapian::Document doc;
-    notmuch_message_file_t *message;
+    notmuch_message_file_t *message_file;
+    notmuch_message_t *message;
     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
 
     GPtrArray *parents, *thread_ids;
 
     const char *refs, *in_reply_to, *date, *header;
-    const char *from, *to, *subject;
+    const char *from, *to, *subject, *old_filename;
     char *message_id;
 
-    time_t time_value;
     unsigned int i;
 
-    message = notmuch_message_file_open (filename);
-    if (message == NULL) {
+    message_file = notmuch_message_file_open (filename);
+    if (message_file == NULL) {
        ret = NOTMUCH_STATUS_FILE_ERROR;
        goto DONE;
     }
 
-    notmuch_message_file_restrict_headers (message,
+    notmuch_message_file_restrict_headers (message_file,
                                           "date",
                                           "from",
                                           "in-reply-to",
                                           "message-id",
                                           "references",
                                           "subject",
+                                          "to",
                                           (char *) NULL);
 
     try {
-       doc.set_data (filename);
-
-       add_term (doc, "type", "mail");
-
-       parents = g_ptr_array_new ();
-
-       refs = notmuch_message_file_get_header (message, "references");
-       parse_references (parents, refs);
-
-       in_reply_to = notmuch_message_file_get_header (message, "in-reply-to");
-       parse_references (parents, in_reply_to);
-
-       for (i = 0; i < parents->len; i++)
-           add_term (doc, "ref", (char *) g_ptr_array_index (parents, i));
+       /* The first order of business is to find/create a message ID. */
 
-       header = notmuch_message_file_get_header (message, "message-id");
+       header = notmuch_message_file_get_header (message_file, "message-id");
        if (header) {
            message_id = parse_message_id (header, NULL);
            /* So the header value isn't RFC-compliant, but it's
@@ -554,17 +504,69 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
            free (sha1);
        }
 
-       thread_ids = find_thread_ids (notmuch, parents, message_id);
+       /* Now that we have a message ID, we get a message object,
+        * (which may or may not reference an existing document in the
+        * database). */
+
+       /* Use NULL for owner since we want to free this locally. */
+
+       /* XXX: This call can fail by either out-of-memory or an
+        * "impossible" Xapian exception. We should rewrite it to
+        * allow us to propagate the error status. */
+       message = _notmuch_message_create_for_message_id (NULL, notmuch,
+                                                         message_id);
+       if (message == NULL) {
+           fprintf (stderr, "Internal error. This shouldn't happen.\n\n");
+           fprintf (stderr, "I mean, it's possible you ran out of memory, but then this code path is still an internal error since it should have detected that and propagated the status value up the stack.\n");
+           exit (1);
+       }
+
+       /* Has a message previously been added with the same ID? */
+       old_filename = notmuch_message_get_filename (message);
+       if (old_filename && strlen (old_filename)) {
+           /* XXX: This is too noisy to actually print, and what do we
+            * really expect the user to do? Go manually delete a
+            * redundant message or merge two similar messages?
+            * Instead we should handle this transparently.
+            *
+            * What we likely want to move to is adding both filenames
+            * to the database so that subsequent indexing will pick up
+            * terms from both files.
+            */
+#if 0
+           fprintf (stderr,
+                    "Note: Attempting to add a message with a duplicate message ID:\n"
+                    "Old: %s\n"   "New: %s\n",
+                    old_filename, filename);
+           fprintf (stderr, "The old filename will be used, but any new terms\n"
+                    "from the new message will added to the database.\n");
+#endif
+       } else {
+           _notmuch_message_set_filename (message, filename);
+           _notmuch_message_add_term (message, "type", "mail");
+       }
+
+       /* Next, find the thread(s) to which this message belongs. */
+       parents = g_ptr_array_new ();
+
+       refs = notmuch_message_file_get_header (message_file, "references");
+       parse_references (parents, refs);
+
+       in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
+       parse_references (parents, in_reply_to);
 
        for (i = 0; i < parents->len; i++)
-           g_free (g_ptr_array_index (parents, i));
-       g_ptr_array_free (parents, TRUE);
+           _notmuch_message_add_term (message, "ref",
+                                      (char *) g_ptr_array_index (parents, i));
 
-       add_term (doc, "msgid", message_id);
-       doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
+       thread_ids = find_thread_ids (notmuch, parents, message_id);
 
        free (message_id);
 
+       for (i = 0; i < parents->len; i++)
+           g_free (g_ptr_array_index (parents, i));
+       g_ptr_array_free (parents, TRUE);
+
        if (thread_ids->len) {
            unsigned int i;
            GString *thread_id;
@@ -572,7 +574,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 
            for (i = 0; i < thread_ids->len; i++) {
                id = (char *) thread_ids->pdata[i];
-               add_term (doc, "thread", id);
+               _notmuch_message_add_thread_id (message, id);
                if (i == 0)
                    thread_id = g_string_new (id);
                else
@@ -580,28 +582,19 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 
                free (id);
            }
-           doc.add_value (NOTMUCH_VALUE_THREAD, thread_id->str);
            g_string_free (thread_id, TRUE);
        } else {
-           /* If not part of any existing thread, generate a new thread_id. */
-           thread_id_t thread_id;
-
-           thread_id_generate (&thread_id);
-           add_term (doc, "thread", thread_id.str);
-           doc.add_value (NOTMUCH_VALUE_THREAD, thread_id.str);
+           _notmuch_message_ensure_thread_id (message);
        }
 
        g_ptr_array_free (thread_ids, TRUE);
 
-       date = notmuch_message_file_get_header (message, "date");
-       time_value = notmuch_parse_date (date, NULL);
-
-       doc.add_value (NOTMUCH_VALUE_DATE,
-                      Xapian::sortable_serialise (time_value));
+       date = notmuch_message_file_get_header (message_file, "date");
+       _notmuch_message_set_date (message, date);
 
-       from = notmuch_message_file_get_header (message, "from");
-       subject = notmuch_message_file_get_header (message, "subject");
-       to = notmuch_message_file_get_header (message, "to");
+       from = notmuch_message_file_get_header (message_file, "from");
+       subject = notmuch_message_file_get_header (message_file, "subject");
+       to = notmuch_message_file_get_header (message_file, "to");
 
        if (from == NULL &&
            subject == NULL &&
@@ -610,7 +603,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
            ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
            goto DONE;
        } else {
-           db->add_document (doc);
+           _notmuch_message_sync (message);
        }
     } catch (const Xapian::Error &error) {
        fprintf (stderr, "A Xapian exception occurred: %s.\n",
@@ -621,7 +614,9 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 
   DONE:
     if (message)
-       notmuch_message_file_close (message);
+       notmuch_message_destroy (message);
+    if (message_file)
+       notmuch_message_file_close (message_file);
 
     return ret;
 }