]> git.notmuchmail.org Git - notmuch/commitdiff
lib: Abstract the extraction of a relative path from set_filename
authorCarl Worth <cworth@cworth.org>
Sat, 19 Dec 2009 20:32:11 +0000 (12:32 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 6 Jan 2010 18:32:05 +0000 (10:32 -0800)
We'll soon be having multiple entry points that accept a filename
path, so we want common code for getting a relative path from a
potentially absolute path.

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

index b6c4d07b794eb37ccdb73b68a31c993fc2326be8..261be01604cb5bffa34143ca9f1d6212d64725e1 100644 (file)
@@ -587,6 +587,40 @@ timestamp_db_key (const char *key)
        return strdup (key);
 }
 
+/* Given a legal 'path' for the database, return the relative path.
+ *
+ * The return value will be a pointer to the originl path contents,
+ * and will be either the original string (if 'path' was relative) or
+ * a portion of the string (if path was absolute and begins with the
+ * database path).
+ */
+const char *
+_notmuch_database_relative_path (notmuch_database_t *notmuch,
+                                const char *path)
+{
+    const char *db_path, *relative;
+    unsigned int db_path_len;
+
+    db_path = notmuch_database_get_path (notmuch);
+    db_path_len = strlen (db_path);
+
+    relative = path;
+
+    if (*relative == '/') {
+       while (*relative == '/' && *(relative+1) == '/')
+           relative++;
+
+       if (strncmp (relative, db_path, db_path_len) == 0)
+       {
+           relative += db_path_len;
+           while (*relative == '/')
+               relative++;
+       }
+    }
+
+    return relative;
+}
+
 notmuch_status_t
 notmuch_database_set_timestamp (notmuch_database_t *notmuch,
                                const char *key, time_t timestamp)
index 49519f1e63e40830ac123373c6e24bf813afbcf1..7c7ea7a1c02aa84d47e9ff8f3db82216d9aedbce 100644 (file)
@@ -396,9 +396,7 @@ void
 _notmuch_message_set_filename (notmuch_message_t *message,
                               const char *filename)
 {
-    const char *s;
-    const char *db_path;
-    unsigned int db_path_len;
+    const char *relative;
 
     if (message->filename) {
        talloc_free (message->filename);
@@ -408,22 +406,8 @@ _notmuch_message_set_filename (notmuch_message_t *message,
     if (filename == NULL)
        INTERNAL_ERROR ("Message filename cannot be NULL.");
 
-    s = filename;
-
-    db_path = notmuch_database_get_path (message->notmuch);
-    db_path_len = strlen (db_path);
-
-    if (*s == '/' && strlen (s) > db_path_len
-       && strncmp (s, db_path, db_path_len) == 0)
-    {
-       s += db_path_len;
-       while (*s == '/') s++;
-
-       if (!*s)
-               INTERNAL_ERROR ("Message filename was same as db prefix.");
-    }
-
-    message->doc.set_data (s);
+    relative = _notmuch_database_relative_path (message->notmuch, filename);
+    message->doc.set_data (relative);
 }
 
 const char *
index b956634057f9dcae62b6b9778ff5415250574a0a..50f93eec27472839caa45956f3df1def86767cbb 100644 (file)
@@ -151,6 +151,10 @@ typedef enum _notmuch_private_status {
 const char *
 _find_prefix (const char *name);
 
+const char *
+_notmuch_database_relative_path (notmuch_database_t *notmuch,
+                                const char *path);
+
 /* thread.cc */
 
 notmuch_thread_t *