[PATCH] mime_node_open: check if the file is in mbox format, and inform gmime.

David Bremner david at tethera.net
Thu Mar 8 13:48:15 PST 2012


From: David Bremner <bremner at debian.org>

It seems that it has always been an error to try to parse an mbox
format file with gmime without calling g_mime_parser_set_scan_from.

This change reads the first 5 bytes of the file, and if they are "From ",
declares the file to be an mbox.
---

This patch seems to fix the problem for me. I don't think the
performance impact should be too bad, but I didn't really test it.

 mime-node.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/mime-node.c b/mime-node.c
index a95bdab..8939147 100644
--- a/mime-node.c
+++ b/mime-node.c
@@ -72,6 +72,8 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
     mime_node_context_t *mctx;
     mime_node_t *root;
     notmuch_status_t status;
+    char from_buf[6]; /* From space NULL */
+    int is_mbox = 0;
 
     root = talloc_zero (ctx, mime_node_t);
     if (root == NULL) {
@@ -96,6 +98,23 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
 	goto DONE;
     }
 
+    if (fread (from_buf, 1, 5, mctx->file) != 5) {
+	fprintf (stderr, "Failed to read 5 bytes from %s: %s\n",
+		 filename, strerror (errno));
+	status = NOTMUCH_STATUS_FILE_ERROR;
+	goto DONE;
+    }
+    from_buf[5] = '\0';
+
+    if (fseek (mctx->file, 0L, SEEK_SET)) {
+	fprintf (stderr, "Failed to rewind %s: %s\n",
+		 filename, strerror (errno));
+	status = NOTMUCH_STATUS_FILE_ERROR;
+	goto DONE;
+    }
+
+    is_mbox = (strcmp (from_buf, "From ") == 0);
+
     mctx->stream = g_mime_stream_file_new (mctx->file);
     if (!mctx->stream) {
 	fprintf (stderr, "Out of memory.\n");
@@ -111,7 +130,9 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
 	goto DONE;
     }
 
+    g_mime_parser_set_scan_from (mctx->parser, is_mbox);
     mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
+
     if (!mctx->mime_message) {
 	fprintf (stderr, "Failed to parse %s\n", filename);
 	status = NOTMUCH_STATUS_FILE_ERROR;
-- 
1.7.9.1



More information about the notmuch mailing list