[PATCH v2 6/8] mailstore: support for mbox:// URIs
authorEthan Glasser-Camp <ethan.glasser.camp@gmail.com>
Sun, 1 Jul 2012 16:39:48 +0000 (12:39 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:55 +0000 (09:47 -0800)
ae/6d434534e6d75dd8b510b688f78af7e2a5e0ea [new file with mode: 0644]

diff --git a/ae/6d434534e6d75dd8b510b688f78af7e2a5e0ea b/ae/6d434534e6d75dd8b510b688f78af7e2a5e0ea
new file mode 100644 (file)
index 0000000..2e8f5b5
--- /dev/null
@@ -0,0 +1,214 @@
+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 90FC8431FD5\r
+       for <notmuch@notmuchmail.org>; Sun,  1 Jul 2012 09:46:53 -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 Z03ZRztmBC2A for <notmuch@notmuchmail.org>;\r
+       Sun,  1 Jul 2012 09:46:52 -0700 (PDT)\r
+Received: from mail-wg0-f45.google.com (mail-wg0-f45.google.com\r
+ [74.125.82.45])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 9F494431FAF   for <notmuch@notmuchmail.org>; Sun,  1 Jul 2012 09:46:52 -0700\r
+ (PDT)\r
+Received: by wgbdt14 with SMTP id dt14so3721202wgb.2\r
+       for <notmuch@notmuchmail.org>; Sun, 01 Jul 2012 09:46:51 -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=54eATWlqXhrQjA1ylMkbX/ULIrBItf9YQiBbwo+7pI8=;\r
+       b=n/z7wpE7Qot4PKORZumWR2bRqzQLf1OU4CkGmHBrVBsbpvfeZhvwhmtLpNMk2TtLhb\r
+       9FSEqWmKeZXG/25Oo6Dp6XdHODi+EbGReQTrdLD8I7K3Lsz8TpObrEd+rgbtoSDlBztf\r
+       dbqdJUYLBnItSTm2I6P1YXb0OGRDWiGCRnuTeNFtWRr/VeWpGB1vY35OVnnUObtcKLW6\r
+       wziXOfNR8HIjXhi1mbwNd2MUAX4nn0YhyiDS5gndluFJRj7ybgWS/UTYNP3/RM6iF3le\r
+       yINm4d01L3QplolPU9OStvOP5prE0TknkjMoKmK6euNIJSIkgyhIJC5p4lL4awCcVF0u\r
+       cVVw==\r
+Received: by 10.180.81.10 with SMTP id v10mr17893561wix.17.1341161211300;\r
+       Sun, 01 Jul 2012 09:46:51 -0700 (PDT)\r
+Received: from localhost ([195.24.209.21])\r
+       by mx.google.com with ESMTPS id y2sm19711605wix.7.2012.07.01.09.46.35\r
+       (version=TLSv1/SSLv3 cipher=OTHER);\r
+       Sun, 01 Jul 2012 09:46:46 -0700 (PDT)\r
+From: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 6/8] mailstore: support for mbox:// URIs\r
+Date: Sun,  1 Jul 2012 12:39:48 -0400\r
+Message-Id: <1341160790-14525-7-git-send-email-ethan@betacantrips.com>\r
+X-Mailer: git-send-email 1.7.9.5\r
+In-Reply-To: <1341160790-14525-1-git-send-email-ethan@betacantrips.com>\r
+References:\r
+ <CAOJ+Ob0MSOez2MvD2fCgF7t32kFPk4g2+xCud88QmBLt_b5pOA@mail.gmail.com>\r
+       <1341160790-14525-1-git-send-email-ethan@betacantrips.com>\r
+Cc: Ethan Glasser-Camp <ethan@betacantrips.com>\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: Sun, 01 Jul 2012 16:46:53 -0000\r
+\r
+No code uses this yet.\r
+\r
+Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+---\r
+ lib/mailstore.c |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-\r
+ 1 file changed, 116 insertions(+), 1 deletion(-)\r
+\r
+diff --git a/lib/mailstore.c b/lib/mailstore.c\r
+index 48acd47..a29d734 100644\r
+--- a/lib/mailstore.c\r
++++ b/lib/mailstore.c\r
+@@ -17,18 +17,133 @@\r
+  *\r
+  * Author: Carl Worth <cworth@cworth.org>\r
+  */\r
++#include <uriparser/Uri.h>\r
+ #include <stdio.h>\r
++#include <glib.h>\r
\r
+ #include "notmuch-private.h"\r
\r
++static FILE *\r
++notmuch_mailstore_basic_open (const char *filename)\r
++{\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
+-    return fopen (filename, "r");\r
++    FILE *ret = NULL;\r
++    UriUriA parsed;\r
++    UriParserStateA state;\r
++    state.uri = &parsed;\r
++    if (uriParseUriA (&state, filename) != URI_SUCCESS) {\r
++        /* Failure. Fall back to fopen and hope for the best. */\r
++        ret = notmuch_mailstore_basic_open (filename);\r
++        goto DONE;\r
++    }\r
++\r
++    if (parsed.scheme.first == NULL) {\r
++        /* No scheme. Probably not really a URL but just an ordinary\r
++         * filename, such as a Maildir message. */\r
++        ret = notmuch_mailstore_basic_open (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
++    /* Default case */\r
++    fprintf (stderr, "Error: Could not open URI %s: unknown scheme.\n",\r
++             filename);\r
++\r
++DONE:\r
++    uriFreeUriMembersA (&parsed);\r
++    return ret;\r
+ }\r
\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