From: Carl Worth <cworth@cworth.org>
Date: Sat, 19 Dec 2009 20:32:11 +0000 (-0800)
Subject: lib: Abstract the extraction of a relative path from set_filename
X-Git-Tag: 0.1~201
X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ba12bf1f2625a34f960502a06ba8907445ef5257;p=notmuch.git

lib: Abstract the extraction of a relative path from set_filename

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.
---

diff --git a/lib/database.cc b/lib/database.cc
index b6c4d07b..261be016 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -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)
diff --git a/lib/message.cc b/lib/message.cc
index 49519f1e..7c7ea7a1 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -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 *
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index b9566340..50f93eec 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -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 *