]> git.notmuchmail.org Git - notmuch/commitdiff
database: Abstract _filename_to_direntry from _add_message
authorCarl Worth <cworth@cworth.org>
Mon, 21 Dec 2009 23:09:56 +0000 (15:09 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 6 Jan 2010 18:32:05 +0000 (10:32 -0800)
The code to map a filename to a direntry is something that we're going
to want in a future _remove_message function, so put it in a new
function _notmuch_database_filename_to_direntry .

lib/database.cc
lib/message.cc
lib/notmuch-private.h

index 553c9f823588d22f7ac96d162d2399fd63febc02..3ed19772759f2d653fc303c722ec639400a8fa46 100644 (file)
@@ -734,6 +734,38 @@ _notmuch_database_get_directory_path (void *ctx,
     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,
index 7d586903913305b9e654644024b016bbcc1f6f28..bd1795195b361409b918dc5ffdb7ecdc4c1666f8 100644 (file)
@@ -393,11 +393,9 @@ notmuch_status_t
 _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);
@@ -407,22 +405,13 @@ _notmuch_message_add_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, "direntry", direntry);
 
     talloc_free (local);
 
index e9712832aa690f47e08d6cd1ed26ffdb7f13d62f..cb93c397a0265b539f54c407929ebfb90562b350 100644 (file)
@@ -171,6 +171,12 @@ _notmuch_database_get_directory_path (void *ctx,
                                      notmuch_database_t *notmuch,
                                      unsigned int doc_id);
 
+notmuch_status_t
+_notmuch_database_filename_to_direntry (void *ctx,
+                                       notmuch_database_t *notmuch,
+                                       const char *filename,
+                                       char **direntry);
+
 /* thread.cc */
 
 notmuch_thread_t *