]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
database: Abstract _filename_to_direntry from _add_message
[notmuch] / lib / database.cc
index f122c2e4b52f70115052aa963fb4783726641599..3ed19772759f2d653fc303c722ec639400a8fa46 100644 (file)
@@ -63,6 +63,11 @@ typedef struct {
  *
  *     tag:       Any tags associated with this message by the user.
  *
+ *     direntry:  A colon-separated pair of values (INTEGER:STRING),
+ *                where INTEGER is the document ID of a directory
+ *                document, and STRING is the name of a file within
+ *                that directory for this mail message.
+ *
  *    A mail document also has two values:
  *
  *     TIMESTAMP:      The time_t value corresponding to the message's
@@ -75,8 +80,7 @@ typedef struct {
  * user in searching. But the database doesn't really care itself
  * about any of these.
  *
- * Finally, the data portion of a mail document contains the path name
- * of the mail message (relative to the database path).
+ * The data portion of a mail document is empty.
  *
  * Directory document
  * ------------------
@@ -122,6 +126,7 @@ prefix_t BOOLEAN_PREFIX_INTERNAL[] = {
     { "reference", "XREFERENCE" },
     { "replyto", "XREPLYTO" },
     { "directory", "XDIRECTORY" },
+    { "direntry", "XDIRENTRY" },
     { "parent", "XPARENT" },
 };
 
@@ -717,6 +722,50 @@ _notmuch_database_find_directory_id (notmuch_database_t *notmuch,
     return status;
 }
 
+const char *
+_notmuch_database_get_directory_path (void *ctx,
+                                     notmuch_database_t *notmuch,
+                                     unsigned int doc_id)
+{
+    Xapian::Document document;
+
+    document = find_document_for_doc_id (notmuch, doc_id);
+
+    return talloc_strdup (ctx, document.get_data ().c_str ());
+}
+
+/* Given a legal 'filename' for the database, (either relative to
+ * database path or absolute with initial components identical to
+ * database path), return a new string (with 'ctx' as the talloc
+ * owner) suitable for use as a direntry term value.
+ */
+notmuch_status_t
+_notmuch_database_filename_to_direntry (void *ctx,
+                                       notmuch_database_t *notmuch,
+                                       const char *filename,
+                                       char **direntry)
+{
+    const char *relative, *directory, *basename;
+    Xapian::docid directory_id;
+    notmuch_status_t status;
+
+    relative = _notmuch_database_relative_path (notmuch, filename);
+
+    status = _notmuch_database_split_path (ctx, relative,
+                                          &directory, &basename);
+    if (status)
+       return status;
+
+    status = _notmuch_database_find_directory_id (notmuch, directory,
+                                                 &directory_id);
+    if (status)
+       return status;
+
+    *direntry = talloc_asprintf (ctx, "%u:%s", directory_id, basename);
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 /* Given a legal 'path' for the database, return the relative path.
  *
  * The return value will be a pointer to the originl path contents,
@@ -812,6 +861,8 @@ notmuch_database_set_directory_mtime (notmuch_database_t *notmuch,
     if (db_path != path)
        free ((char *) db_path);
 
+    talloc_free (local);
+
     return ret;
 }
 
@@ -824,7 +875,6 @@ notmuch_database_get_directory_mtime (notmuch_database_t *notmuch,
     notmuch_private_status_t status;
     const char *db_path = NULL;
     time_t ret = 0;
-    void *local = talloc_new (notmuch);
 
     db_path = directory_db_path (path);
 
@@ -844,8 +894,6 @@ notmuch_database_get_directory_mtime (notmuch_database_t *notmuch,
     if (db_path != path)
        free ((char *) db_path);
 
-    talloc_free (local);
-
     return ret;
 }
 
@@ -1171,23 +1219,24 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
            goto DONE;
        }
 
+       _notmuch_message_add_filename (message, filename);
+
        /* Is this a newly created message object? */
        if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
-           _notmuch_message_set_filename (message, filename);
            _notmuch_message_add_term (message, "type", "mail");
-       } else {
-           ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
-           goto DONE;
-       }
 
-       ret = _notmuch_database_link_message (notmuch, message, message_file);
-       if (ret)
-           goto DONE;
+           ret = _notmuch_database_link_message (notmuch, message,
+                                                 message_file);
+           if (ret)
+               goto DONE;
 
-       date = notmuch_message_file_get_header (message_file, "date");
-       _notmuch_message_set_date (message, date);
+           date = notmuch_message_file_get_header (message_file, "date");
+           _notmuch_message_set_date (message, date);
 
-       _notmuch_message_index_file (message, filename);
+           _notmuch_message_index_file (message, filename);
+       } else {
+           ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
+       }
 
        _notmuch_message_sync (message);
     } catch (const Xapian::Error &error) {