1 Return-Path: <ethan.glasser.camp@gmail.com>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 90FC8431FD5
\r
6 for <notmuch@notmuchmail.org>; Sun, 1 Jul 2012 09:46:53 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=1.061 tagged_above=-999 required=5
\r
12 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
\r
13 FREEMAIL_FROM=0.001, RCVD_IN_BL_SPAMCOP_NET=1.246,
\r
14 RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_SORBS_WEB=0.614] autolearn=disabled
\r
15 Received: from olra.theworths.org ([127.0.0.1])
\r
16 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
17 with ESMTP id Z03ZRztmBC2A for <notmuch@notmuchmail.org>;
\r
18 Sun, 1 Jul 2012 09:46:52 -0700 (PDT)
\r
19 Received: from mail-wg0-f45.google.com (mail-wg0-f45.google.com
\r
20 [74.125.82.45]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client
\r
21 certificate requested) by olra.theworths.org (Postfix) with ESMTPS id
\r
22 9F494431FAF for <notmuch@notmuchmail.org>; Sun, 1 Jul 2012 09:46:52 -0700
\r
24 Received: by wgbdt14 with SMTP id dt14so3721202wgb.2
\r
25 for <notmuch@notmuchmail.org>; Sun, 01 Jul 2012 09:46:51 -0700 (PDT)
\r
26 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
\r
27 h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;
\r
28 bh=54eATWlqXhrQjA1ylMkbX/ULIrBItf9YQiBbwo+7pI8=;
\r
29 b=n/z7wpE7Qot4PKORZumWR2bRqzQLf1OU4CkGmHBrVBsbpvfeZhvwhmtLpNMk2TtLhb
\r
30 9FSEqWmKeZXG/25Oo6Dp6XdHODi+EbGReQTrdLD8I7K3Lsz8TpObrEd+rgbtoSDlBztf
\r
31 dbqdJUYLBnItSTm2I6P1YXb0OGRDWiGCRnuTeNFtWRr/VeWpGB1vY35OVnnUObtcKLW6
\r
32 wziXOfNR8HIjXhi1mbwNd2MUAX4nn0YhyiDS5gndluFJRj7ybgWS/UTYNP3/RM6iF3le
\r
33 yINm4d01L3QplolPU9OStvOP5prE0TknkjMoKmK6euNIJSIkgyhIJC5p4lL4awCcVF0u
\r
35 Received: by 10.180.81.10 with SMTP id v10mr17893561wix.17.1341161211300;
\r
36 Sun, 01 Jul 2012 09:46:51 -0700 (PDT)
\r
37 Received: from localhost ([195.24.209.21])
\r
38 by mx.google.com with ESMTPS id y2sm19711605wix.7.2012.07.01.09.46.35
\r
39 (version=TLSv1/SSLv3 cipher=OTHER);
\r
40 Sun, 01 Jul 2012 09:46:46 -0700 (PDT)
\r
41 From: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>
\r
42 To: notmuch@notmuchmail.org
\r
43 Subject: [PATCH v2 6/8] mailstore: support for mbox:// URIs
\r
44 Date: Sun, 1 Jul 2012 12:39:48 -0400
\r
45 Message-Id: <1341160790-14525-7-git-send-email-ethan@betacantrips.com>
\r
46 X-Mailer: git-send-email 1.7.9.5
\r
47 In-Reply-To: <1341160790-14525-1-git-send-email-ethan@betacantrips.com>
\r
49 <CAOJ+Ob0MSOez2MvD2fCgF7t32kFPk4g2+xCud88QmBLt_b5pOA@mail.gmail.com>
\r
50 <1341160790-14525-1-git-send-email-ethan@betacantrips.com>
\r
51 Cc: Ethan Glasser-Camp <ethan@betacantrips.com>
\r
52 X-BeenThere: notmuch@notmuchmail.org
\r
53 X-Mailman-Version: 2.1.13
\r
55 List-Id: "Use and development of the notmuch mail system."
\r
56 <notmuch.notmuchmail.org>
\r
57 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
58 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
59 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
60 List-Post: <mailto:notmuch@notmuchmail.org>
\r
61 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
62 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
63 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
64 X-List-Received-Date: Sun, 01 Jul 2012 16:46:53 -0000
\r
66 No code uses this yet.
\r
68 Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>
\r
70 lib/mailstore.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
\r
71 1 file changed, 116 insertions(+), 1 deletion(-)
\r
73 diff --git a/lib/mailstore.c b/lib/mailstore.c
\r
74 index 48acd47..a29d734 100644
\r
75 --- a/lib/mailstore.c
\r
76 +++ b/lib/mailstore.c
\r
77 @@ -17,18 +17,133 @@
\r
79 * Author: Carl Worth <cworth@cworth.org>
\r
81 +#include <uriparser/Uri.h>
\r
85 #include "notmuch-private.h"
\r
88 +notmuch_mailstore_basic_open (const char *filename)
\r
90 + return fopen (filename, "r");
\r
93 +/* Since we have to return a FILE*, we use fmemopen to turn buffers
\r
94 + * into FILE* streams. But when we close these streams, we have to
\r
95 + * free() the buffers. Use a hash to associate the two.
\r
97 +static GHashTable *_mbox_files_to_strings = NULL;
\r
100 +_ensure_mbox_files_to_strings () {
\r
101 + if (_mbox_files_to_strings == NULL)
\r
102 + _mbox_files_to_strings = g_hash_table_new (NULL, NULL);
\r
106 +notmuch_mailstore_mbox_open (UriUriA *uri)
\r
108 + FILE *ret = NULL, *mbox = NULL;
\r
109 + char *filename, *message, *length_s;
\r
110 + const char *error;
\r
111 + long int offset, length, this_read;
\r
112 + _ensure_mbox_files_to_strings ();
\r
114 + offset = strtol (uri->fragment.first, &length_s, 10);
\r
115 + length = strtol (length_s+1, NULL, 10);
\r
117 + filename = talloc_strndup (NULL, uri->pathHead->text.first-1,
\r
118 + uri->pathTail->text.afterLast-uri->pathHead->text.first+1);
\r
120 + if (filename == NULL)
\r
123 + mbox = fopen (filename, "r");
\r
124 + if (mbox == NULL) {
\r
125 + fprintf (stderr, "Couldn't open message %s: %s.\n", uri->scheme.first,
\r
126 + strerror (errno));
\r
130 + message = talloc_array (NULL, char, length);
\r
131 + fseek (mbox, offset, SEEK_SET);
\r
133 + this_read = fread (message, sizeof(char), length, mbox);
\r
134 + if (this_read != length) {
\r
136 + error = "end of file reached";
\r
137 + if (ferror (mbox))
\r
138 + error = strerror (ferror (mbox));
\r
140 + fprintf (stderr, "Couldn't read message %s: %s.\n", uri->scheme.first, error);
\r
144 + ret = fmemopen (message, length, "r");
\r
145 + if (ret == NULL) {
\r
146 + /* No fclose will ever be called, so let's free message now */
\r
147 + talloc_free (message);
\r
151 + g_hash_table_insert (_mbox_files_to_strings, ret, message);
\r
154 + talloc_free (filename);
\r
162 notmuch_mailstore_open (const char *filename)
\r
164 - return fopen (filename, "r");
\r
165 + FILE *ret = NULL;
\r
167 + UriParserStateA state;
\r
168 + state.uri = &parsed;
\r
169 + if (uriParseUriA (&state, filename) != URI_SUCCESS) {
\r
170 + /* Failure. Fall back to fopen and hope for the best. */
\r
171 + ret = notmuch_mailstore_basic_open (filename);
\r
175 + if (parsed.scheme.first == NULL) {
\r
176 + /* No scheme. Probably not really a URL but just an ordinary
\r
177 + * filename, such as a Maildir message. */
\r
178 + ret = notmuch_mailstore_basic_open (filename);
\r
182 + if (0 == strncmp (parsed.scheme.first, "mbox",
\r
183 + parsed.scheme.afterLast-parsed.scheme.first)) {
\r
184 + /* mbox URI of the form mbox:///path/to/file#offset+length.
\r
185 + * Just pass the parsed URI. */
\r
186 + ret = notmuch_mailstore_mbox_open (&parsed);
\r
190 + /* Default case */
\r
191 + fprintf (stderr, "Error: Could not open URI %s: unknown scheme.\n",
\r
195 + uriFreeUriMembersA (&parsed);
\r
200 notmuch_mailstore_close (FILE *file)
\r
202 + char *file_buffer;
\r
203 + if (_mbox_files_to_strings != NULL) {
\r
204 + file_buffer = g_hash_table_lookup (_mbox_files_to_strings, file);
\r
205 + if (file_buffer != NULL)
\r
206 + talloc_free (file_buffer);
\r
208 + g_hash_table_remove (_mbox_files_to_strings, file);
\r
210 return fclose (file);
\r