[RFC PATCH 06/14] maildir URIs can be used in tags_to_maildir_flags

Ethan Glasser-Camp ethan.glasser.camp at gmail.com
Mon Jun 25 13:51:49 PDT 2012


A better fix would probably be based on scheme.

Signed-off-by: Ethan Glasser-Camp <ethan at betacantrips.com>
---
 lib/message.cc |   51 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 46 insertions(+), 5 deletions(-)

diff --git a/lib/message.cc b/lib/message.cc
index c9857f5..8ecec71 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -23,6 +23,7 @@
 
 #include <stdint.h>
 
+#include <uriparser/Uri.h>
 #include <gmime/gmime.h>
 
 struct visible _notmuch_message {
@@ -1093,7 +1094,6 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
     {
 	filename = notmuch_filenames_get (filenames);
 	dir = _filename_is_in_maildir (filename);
-
 	if (! dir)
 	    continue;
 
@@ -1304,12 +1304,46 @@ _new_maildir_filename (void *ctx,
     return filename_new;
 }
 
+/* Parses a maildir URI and returns the filename corresponding to its
+ * path.
+ *
+ * Returns NULL if either the URI couldn't be parsed or if the
+ * scheme isn't maildir:.
+ */
+static char *
+_get_maildir_filename (const char *filename)
+{
+    UriParserStateA parser_state;
+    UriUriA parsed;
+    char *path;
+    parser_state.uri = &parsed;
+
+    if (uriParseUriA (&parser_state, filename) != URI_SUCCESS) {
+	uriFreeUriMembersA (&parsed);
+	return NULL;
+    }
+
+    if (parsed.scheme.first != NULL &&
+	0 != strncmp(parsed.scheme.first, "maildir",
+		     parsed.scheme.afterLast-parsed.scheme.first)) {
+	/* Full URI with non-maildir scheme. */
+	uriFreeUriMembersA (&parsed);
+	return NULL;
+    }
+
+    path = (char *)parsed.pathHead->text.first - 1;
+    uriFreeUriMembersA (&parsed);
+    return path;
+
+}
+
+
 notmuch_status_t
 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
 {
     notmuch_filenames_t *filenames;
     const char *filename;
-    char *filename_new;
+    char *filename_new, *filename_old, *filename_new_uri;
     char *to_set, *to_clear;
     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
 
@@ -1324,16 +1358,22 @@ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
 	if (! _filename_is_in_maildir (filename))
 	    continue;
 
-	filename_new = _new_maildir_filename (message, filename,
+	filename_old = _get_maildir_filename (filename);
+	if (filename_old == NULL)
+	    continue;
+
+	filename_new = _new_maildir_filename (message, filename_old,
 					      to_set, to_clear);
 	if (filename_new == NULL)
 	    continue;
 
+	filename_new_uri = talloc_asprintf (message, "maildir://%s", filename_new);
+
 	if (strcmp (filename, filename_new)) {
 	    int err;
 	    notmuch_status_t new_status;
 
-	    err = rename (filename, filename_new);
+	    err = rename (filename_old, filename_new);
 	    if (err)
 		continue;
 
@@ -1347,7 +1387,7 @@ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
 	    }
 
 	    new_status = _notmuch_message_add_filename (message,
-							filename_new);
+							filename_new_uri);
 	    /* Hold on to only the first error. */
 	    if (! status && new_status) {
 		status = new_status;
@@ -1358,6 +1398,7 @@ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
 	}
 
 	talloc_free (filename_new);
+	talloc_free (filename_new_uri);
     }
 
     talloc_free (to_set);
-- 
1.7.9.5



More information about the notmuch mailing list