--- /dev/null
+Return-Path: <ethan.glasser.camp@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 8DBDB431FAF\r
+ for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:54:23 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 1.061\r
+X-Spam-Level: *\r
+X-Spam-Status: No, score=1.061 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, RCVD_IN_BL_SPAMCOP_NET=1.246,\r
+ RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_SORBS_WEB=0.614] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id DGKHb36IH1Pn for <notmuch@notmuchmail.org>;\r
+ Mon, 25 Jun 2012 13:54:23 -0700 (PDT)\r
+Received: from mail-we0-f181.google.com (mail-we0-f181.google.com\r
+ [74.125.82.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id D2450431FB6\r
+ for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:54:22 -0700 (PDT)\r
+Received: by mail-we0-f181.google.com with SMTP id j55so3649559wer.26\r
+ for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:54:22 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
+ bh=qFV8lIM0wUbaJb3QQa15HFk6nHSx8wYDwaZm03WSkbI=;\r
+ b=iat6tx0LAge9nov/ohkNvdcU945bed9EnVRVIbB3SXGqSTMTF6XhiuXN2DOZfMxOoK\r
+ v++QNoItw+0LQokxxMzx1+6b81GICc+yGrF58xlMp6ppycZ5mEdN2Cp1pSOuVZaHrXmE\r
+ /bs520PzcPESReLwYku88QS788/Ms6/gXmdhUNFstZwvaeMBmRoHBjXw8n4OoOhjCvG2\r
+ a7b9Z0HqHK4CvBy8usrCAcYKU/FD+pZBmeyhpQjAjEf/PObcLk662IyDeeY0fLA7wBq7\r
+ kpryupytyemu0AZQFd/5OU4YI5ksaTtmDEi1jppHxSTdv5di//+7XPs7aaelvID6fNev\r
+ OzYQ==\r
+Received: by 10.180.96.4 with SMTP id do4mr27121547wib.18.1340657662442;\r
+ Mon, 25 Jun 2012 13:54:22 -0700 (PDT)\r
+Received: from localhost ([195.24.209.21])\r
+ by mx.google.com with ESMTPS id t8sm35405wiy.3.2012.06.25.13.54.16\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Mon, 25 Jun 2012 13:54:21 -0700 (PDT)\r
+From: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH 12/14] mailstore: support for mbox:// URIs\r
+Date: Mon, 25 Jun 2012 16:51:55 -0400\r
+Message-Id: <1340657517-6539-8-git-send-email-ethan@betacantrips.com>\r
+X-Mailer: git-send-email 1.7.9.5\r
+In-Reply-To: <1340657517-6539-1-git-send-email-ethan@betacantrips.com>\r
+References: <1340657517-6539-1-git-send-email-ethan@betacantrips.com>\r
+X-Mailman-Approved-At: Tue, 26 Jun 2012 03:51:54 -0700\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 25 Jun 2012 20:54:23 -0000\r
+\r
+\r
+Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+---\r
+ lib/mailstore.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 1 file changed, 85 insertions(+)\r
+\r
+diff --git a/lib/mailstore.c b/lib/mailstore.c\r
+index ae02c12..e8d9bc1 100644\r
+--- a/lib/mailstore.c\r
++++ b/lib/mailstore.c\r
+@@ -19,6 +19,7 @@\r
+ */\r
+ #include <uriparser/Uri.h>\r
+ #include <stdio.h>\r
++#include <glib.h>\r
+ \r
+ #include "notmuch-private.h"\r
+ \r
+@@ -28,6 +29,74 @@ notmuch_mailstore_basic_open (const char *filename)\r
+ return fopen (filename, "r");\r
+ }\r
+ \r
++/* Since we have to return a FILE*, we use fmemopen to turn buffers\r
++ * into FILE* streams. But when we close these streams, we have to\r
++ * free() the buffers. Use a hash to associate the two.\r
++ */\r
++static GHashTable *_mbox_files_to_strings = NULL;\r
++\r
++static void\r
++_ensure_mbox_files_to_strings () {\r
++ if (_mbox_files_to_strings == NULL)\r
++ _mbox_files_to_strings = g_hash_table_new (NULL, NULL);\r
++}\r
++\r
++static FILE *\r
++notmuch_mailstore_mbox_open (UriUriA *uri)\r
++{\r
++ FILE *ret = NULL, *mbox = NULL;\r
++ char *filename, *message, *length_s;\r
++ const char *error;\r
++ long int offset, length, this_read;\r
++ _ensure_mbox_files_to_strings ();\r
++\r
++ offset = strtol (uri->fragment.first, &length_s, 10);\r
++ length = strtol (length_s+1, NULL, 10);\r
++\r
++ filename = talloc_strndup (NULL, uri->pathHead->text.first-1,\r
++ uri->pathTail->text.afterLast-uri->pathHead->text.first+1);\r
++\r
++ if (filename == NULL)\r
++ goto DONE;\r
++\r
++ mbox = fopen (filename, "r");\r
++ if (mbox == NULL) {\r
++ fprintf (stderr, "Couldn't open message %s: %s.\n", uri->scheme.first,\r
++ strerror (errno));\r
++ goto DONE;\r
++ }\r
++\r
++ message = talloc_array (NULL, char, length);\r
++ fseek (mbox, offset, SEEK_SET);\r
++\r
++ this_read = fread (message, sizeof(char), length, mbox);\r
++ if (this_read != length) {\r
++ if (feof (mbox))\r
++ error = "end of file reached";\r
++ if (ferror (mbox))\r
++ error = strerror (ferror (mbox));\r
++\r
++ fprintf (stderr, "Couldn't read message %s: %s.\n", uri->scheme.first, error);\r
++ goto DONE;\r
++ }\r
++\r
++ ret = fmemopen (message, length, "r");\r
++ if (ret == NULL) {\r
++ /* No fclose will ever be called, so let's free message now */\r
++ talloc_free (message);\r
++ goto DONE;\r
++ }\r
++\r
++ g_hash_table_insert (_mbox_files_to_strings, ret, message);\r
++DONE:\r
++ if (filename)\r
++ talloc_free (filename);\r
++ if (mbox)\r
++ fclose (mbox);\r
++\r
++ return ret;\r
++}\r
++\r
+ FILE *\r
+ notmuch_mailstore_open (const char *filename)\r
+ {\r
+@@ -57,6 +126,14 @@ notmuch_mailstore_open (const char *filename)\r
+ goto DONE;\r
+ }\r
+ \r
++ if (0 == strncmp (parsed.scheme.first, "mbox",\r
++ parsed.scheme.afterLast-parsed.scheme.first)) {\r
++ /* mbox URI of the form mbox:///path/to/file#offset+length.\r
++ * Just pass the parsed URI. */\r
++ ret = notmuch_mailstore_mbox_open (&parsed);\r
++ goto DONE;\r
++ }\r
++\r
+ DONE:\r
+ uriFreeUriMembersA (&parsed);\r
+ return ret;\r
+@@ -65,5 +142,13 @@ DONE:\r
+ int\r
+ notmuch_mailstore_close (FILE *file)\r
+ {\r
++ char *file_buffer;\r
++ if (_mbox_files_to_strings != NULL) {\r
++ file_buffer = g_hash_table_lookup (_mbox_files_to_strings, file);\r
++ if (file_buffer != NULL) {\r
++ talloc_free (file_buffer);\r
++ }\r
++ g_hash_table_remove (_mbox_files_to_strings, file);\r
++ }\r
+ return fclose (file);\r
+ }\r
+-- \r
+1.7.9.5\r
+\r