]> git.notmuchmail.org Git - notmuch/blobdiff - lib/message.cc
notmuch_message_get_filename: Support old-style filename storage.
[notmuch] / lib / message.cc
index 3f5334239e0839d5d0bcae3595e4e28b7f1b5280..0fc54668cb9a362454551d8880bade40b7c6583e 100644 (file)
@@ -385,22 +385,17 @@ notmuch_message_get_replies (notmuch_message_t *message)
     return _notmuch_messages_create (message->replies);
 }
 
-/* Set the filename for 'message' to 'filename'.
- *
- * XXX: We should still figure out if we think it's important to store
- * multiple filenames for email messages with identical message IDs.
+/* Add an additional 'filename' for 'message'.
  *
  * This change will not be reflected in the database until the next
  * call to _notmuch_message_set_sync. */
 notmuch_status_t
-_notmuch_message_set_filename (notmuch_message_t *message,
+_notmuch_message_add_filename (notmuch_message_t *message,
                               const char *filename)
 {
-    const char *relative, *directory, *basename;
-    char *term;
-    Xapian::docid directory_id;
     notmuch_status_t status;
     void *local = talloc_new (message);
+    char *direntry;
 
     if (message->filename) {
        talloc_free (message->filename);
@@ -410,22 +405,13 @@ _notmuch_message_set_filename (notmuch_message_t *message,
     if (filename == NULL)
        INTERNAL_ERROR ("Message filename cannot be NULL.");
 
-    relative = _notmuch_database_relative_path (message->notmuch, filename);
-
-    status = _notmuch_database_split_path (local, relative,
-                                          &directory, &basename);
+    status = _notmuch_database_filename_to_direntry (local,
+                                                    message->notmuch,
+                                                    filename, &direntry);
     if (status)
        return status;
 
-    status = _notmuch_database_find_directory_id (message->notmuch, directory,
-                                                 &directory_id);
-    if (status)
-       return status;
-
-    term = talloc_asprintf (local, "%s%u:%s",
-                           _find_prefix ("direntry"), directory_id, basename);
-
-    message->doc.add_term (term);
+    _notmuch_message_add_term (message, "file-direntry", direntry);
 
     talloc_free (local);
 
@@ -435,7 +421,7 @@ _notmuch_message_set_filename (notmuch_message_t *message,
 const char *
 notmuch_message_get_filename (notmuch_message_t *message)
 {
-    const char *prefix = _find_prefix ("direntry");
+    const char *prefix = _find_prefix ("file-direntry");
     int prefix_len = strlen (prefix);
     Xapian::TermIterator i;
     char *direntry, *colon;
@@ -455,7 +441,19 @@ notmuch_message_get_filename (notmuch_message_t *message)
     if (i == message->doc.termlist_end () ||
        strncmp (direntry, prefix, prefix_len))
     {
-       INTERNAL_ERROR ("message with no filename");
+       /* A message document created by an old version of notmuch
+        * (prior to rename support) will have the filename in the
+        * data of the document rather than as a file-direntry term. */
+       const char *data;
+
+       data = message->doc.get_data ().c_str ();
+
+       if (data == NULL)
+           INTERNAL_ERROR ("message with no filename");
+
+       message->filename = talloc_strdup (message, data);
+
+       return message->filename;
     }
 
     direntry += prefix_len;