--- /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 02198431FB6\r
+ for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:42: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 HNCt9uOexQxJ for <notmuch@notmuchmail.org>;\r
+ Mon, 25 Jun 2012 13:42:22 -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
+ 55CEA431FAF for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:42:22 -0700\r
+ (PDT)\r
+Received: by mail-wg0-f45.google.com with SMTP id dt14so3702167wgb.2\r
+ for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:42: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
+ :mime-version:content-type:content-transfer-encoding;\r
+ bh=y2Aq3cuCvohCZpgJoMJlo6niZDIsTkvXjckBUhToqLo=;\r
+ b=aMPsBYBAy1u9jtZt+kJhPkIkHsVBKj62Ye1ekrA23+1CFi3dhDlLjZMyP0KA+yQ0Ny\r
+ 2NMYDCwtuia1lEBgrf9YniVmJPe7EG+eXdPGyQaO5Y3mWrzMXk4cLY1wejvgPBOoUrj6\r
+ 8AVJPdH9OnYFFBYPQ441m8hZPLBl3nmfboiWbzJAS+gWINhOHMJiEN8tmFd8cZMNV3mo\r
+ K7gNwgw8UB82JxgqhZJx4G+ukjUiqWrgWJYnL2CwnvYaesrmL1tx4BeCirnIDbZLZhXi\r
+ ZLJI56xXx6NoxGDCdZQR6T2sX28405HEOh+x3vsoq3CbOqKANDGAMNm+zQverGjPwpa+\r
+ r3Lg==\r
+Received: by 10.180.8.41 with SMTP id o9mr27141363wia.0.1340656941888;\r
+ Mon, 25 Jun 2012 13:42:21 -0700 (PDT)\r
+Received: from localhost ([195.24.209.21])\r
+ by mx.google.com with ESMTPS id bg10sm12236082wib.9.2012.06.25.13.42.13\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Mon, 25 Jun 2012 13:42:20 -0700 (PDT)\r
+From: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject:\r
+ [RFC PATCH 01/14] All access to mail files goes through the mailstore module\r
+Date: Mon, 25 Jun 2012 16:41:26 -0400\r
+Message-Id: <1340656899-5644-2-git-send-email-ethan@betacantrips.com>\r
+X-Mailer: git-send-email 1.7.9.5\r
+In-Reply-To: <1340656899-5644-1-git-send-email-ethan@betacantrips.com>\r
+References: <1340656899-5644-1-git-send-email-ethan@betacantrips.com>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\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:42:24 -0000\r
+\r
+This commit introduces the mailstore module which provides two\r
+functions, notmuch_mailstore_open and notmuch_mailstore_close. These\r
+functions are currently just stub calls to fopen and fclose, but later\r
+can be made more complex in order to support mail storage systems\r
+where one message might not be one file.\r
+\r
+Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+---\r
+ lib/Makefile.local | 1 +\r
+ lib/database.cc | 2 +-\r
+ lib/index.cc | 2 +-\r
+ lib/mailstore.c | 34 ++++++++++++++++++++++++\r
+ lib/message-file.c | 6 ++---\r
+ lib/notmuch-private.h | 3 +++\r
+ lib/notmuch.h | 16 +++++++++++\r
+ lib/sha1.c | 70 +++++++++++++++++++++++++++++++++++++------------\r
+ mime-node.c | 4 +--\r
+ notmuch-show.c | 12 ++++-----\r
+ 10 files changed, 120 insertions(+), 30 deletions(-)\r
+ create mode 100644 lib/mailstore.c\r
+\r
+diff --git a/lib/Makefile.local b/lib/Makefile.local\r
+index 8a9aa28..cfc77bb 100644\r
+--- a/lib/Makefile.local\r
++++ b/lib/Makefile.local\r
+@@ -51,6 +51,7 @@ libnotmuch_c_srcs = \\r
+ $(dir)/filenames.c \\r
+ $(dir)/string-list.c \\r
+ $(dir)/libsha1.c \\r
++ $(dir)/mailstore.c \\r
+ $(dir)/message-file.c \\r
+ $(dir)/messages.c \\r
+ $(dir)/sha1.c \\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 761dc1a..c035edc 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -1773,7 +1773,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
+ if (message_id == NULL ) {\r
+ /* No message-id at all, let's generate one by taking a\r
+ * hash over the file's contents. */\r
+- char *sha1 = notmuch_sha1_of_file (filename);\r
++ char *sha1 = notmuch_sha1_of_message (filename);\r
+ \r
+ /* If that failed too, something is really wrong. Give up. */\r
+ if (sha1 == NULL) {\r
+diff --git a/lib/index.cc b/lib/index.cc\r
+index e377732..b607e82 100644\r
+--- a/lib/index.cc\r
++++ b/lib/index.cc\r
+@@ -441,7 +441,7 @@ _notmuch_message_index_file (notmuch_message_t *message,\r
+ initialized = 1;\r
+ }\r
+ \r
+- file = fopen (filename, "r");\r
++ file = notmuch_mailstore_open (filename);\r
+ if (! file) {\r
+ fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));\r
+ ret = NOTMUCH_STATUS_FILE_ERROR;\r
+diff --git a/lib/mailstore.c b/lib/mailstore.c\r
+new file mode 100644\r
+index 0000000..48acd47\r
+--- /dev/null\r
++++ b/lib/mailstore.c\r
+@@ -0,0 +1,34 @@\r
++/* mailstore.c - code to access individual messages\r
++ *\r
++ * Copyright © 2009 Carl Worth\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program. If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Author: Carl Worth <cworth@cworth.org>\r
++ */\r
++#include <stdio.h>\r
++\r
++#include "notmuch-private.h"\r
++\r
++FILE *\r
++notmuch_mailstore_open (const char *filename)\r
++{\r
++ return fopen (filename, "r");\r
++}\r
++\r
++int\r
++notmuch_mailstore_close (FILE *file)\r
++{\r
++ return fclose (file);\r
++}\r
+diff --git a/lib/message-file.c b/lib/message-file.c\r
+index 915aba8..271389c 100644\r
+--- a/lib/message-file.c\r
++++ b/lib/message-file.c\r
+@@ -86,7 +86,7 @@ _notmuch_message_file_destructor (notmuch_message_file_t *message)\r
+ g_hash_table_destroy (message->headers);\r
+ \r
+ if (message->file)\r
+- fclose (message->file);\r
++ notmuch_mailstore_close (message->file);\r
+ \r
+ return 0;\r
+ }\r
+@@ -104,7 +104,7 @@ _notmuch_message_file_open_ctx (void *ctx, const char *filename)\r
+ \r
+ talloc_set_destructor (message, _notmuch_message_file_destructor);\r
+ \r
+- message->file = fopen (filename, "r");\r
++ message->file = notmuch_mailstore_open (filename);\r
+ if (message->file == NULL)\r
+ goto FAIL;\r
+ \r
+@@ -361,7 +361,7 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,\r
+ }\r
+ \r
+ if (message->parsing_finished) {\r
+- fclose (message->file);\r
++ notmuch_mailstore_close (message->file);\r
+ message->file = NULL;\r
+ }\r
+ \r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index bfb4111..5dbe821 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -468,6 +468,9 @@ notmuch_sha1_of_string (const char *str);\r
+ char *\r
+ notmuch_sha1_of_file (const char *filename);\r
+ \r
++char *\r
++notmuch_sha1_of_message (const char *filename);\r
++\r
+ /* string-list.c */\r
+ \r
+ typedef struct _notmuch_string_node {\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 3633bed..0ca367b 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -1233,6 +1233,22 @@ notmuch_message_thaw (notmuch_message_t *message);\r
+ void\r
+ notmuch_message_destroy (notmuch_message_t *message);\r
+ \r
++/* Get access to the underlying data of a message.\r
++ *\r
++ * Right now, all messages are simple files in maildirs, but this is\r
++ * hopefully subject to change.\r
++ *\r
++ * If the returned FILE* is not NULL, be sure to call\r
++ * notmuch_mailstore_close when you're done with it.\r
++ */\r
++FILE *\r
++notmuch_mailstore_open (const char *filename);\r
++\r
++/* Release any resources associated with this file.\r
++ */\r
++int\r
++notmuch_mailstore_close (FILE *file);\r
++\r
+ /* Is the given 'tags' iterator pointing at a valid tag.\r
+ *\r
+ * When this function returns TRUE, notmuch_tags_get will return a\r
+diff --git a/lib/sha1.c b/lib/sha1.c\r
+index cc48108..462c07e 100644\r
+--- a/lib/sha1.c\r
++++ b/lib/sha1.c\r
+@@ -64,19 +64,11 @@ notmuch_sha1_of_string (const char *str)\r
+ return _hex_of_sha1_digest (digest);\r
+ }\r
+ \r
+-/* Create a hexadecimal string version of the SHA-1 digest of the\r
+- * contents of the named file.\r
+- *\r
+- * This function returns a newly allocated string which the caller\r
+- * should free() when finished.\r
+- *\r
+- * If any error occurs while reading the file, (permission denied,\r
+- * file not found, etc.), this function returns NULL.\r
++/* Internal function to feed the contents of a FILE * to the sha1 functions.\r
+ */\r
+-char *\r
+-notmuch_sha1_of_file (const char *filename)\r
+-{\r
+- FILE *file;\r
++\r
++static char *\r
++_notmuch_sha1_of_filep (FILE *file){\r
+ #define BLOCK_SIZE 4096\r
+ unsigned char block[BLOCK_SIZE];\r
+ size_t bytes_read;\r
+@@ -84,14 +76,10 @@ notmuch_sha1_of_file (const char *filename)\r
+ unsigned char digest[SHA1_DIGEST_SIZE];\r
+ char *result;\r
+ \r
+- file = fopen (filename, "r");\r
+- if (file == NULL)\r
+- return NULL;\r
+-\r
+ sha1_begin (&sha1);\r
+ \r
+ while (1) {\r
+- bytes_read = fread (block, 1, 4096, file);\r
++ bytes_read = fread (block, 1, BLOCK_SIZE, file);\r
+ if (bytes_read == 0) {\r
+ if (feof (file)) {\r
+ break;\r
+@@ -108,8 +96,56 @@ notmuch_sha1_of_file (const char *filename)\r
+ \r
+ result = _hex_of_sha1_digest (digest);\r
+ \r
++ return result;\r
++}\r
++\r
++/* Create a hexadecimal string version of the SHA-1 digest of the\r
++ * contents of the named file.\r
++ *\r
++ * This function returns a newly allocated string which the caller\r
++ * should free() when finished.\r
++ *\r
++ * If any error occurs while reading the file, (permission denied,\r
++ * file not found, etc.), this function returns NULL.\r
++ */\r
++char *\r
++notmuch_sha1_of_file (const char *filename)\r
++{\r
++ FILE *file;\r
++ char *result;\r
++ file = fopen (filename, "r");\r
++ if (file == NULL)\r
++ return NULL;\r
++\r
++ result = _notmuch_sha1_of_filep (file);\r
++\r
+ fclose (file);\r
+ \r
+ return result;\r
+ }\r
+ \r
++/* Create a hexadecimal string version of the SHA-1 digest of the\r
++ * contents of the named message, which is accessed via a mailstore.\r
++ *\r
++ * This function returns a newly allocated string which the caller\r
++ * should free() when finished.\r
++ *\r
++ * If any error occurs while reading the file, (permission denied,\r
++ * file not found, etc.), this function returns NULL.\r
++ */\r
++char *\r
++notmuch_sha1_of_message (const char *filename)\r
++{\r
++ FILE *file;\r
++ char *result;\r
++\r
++ file = notmuch_mailstore_open (filename);\r
++ if (file == NULL)\r
++ return NULL;\r
++\r
++ result = _notmuch_sha1_of_filep (file);\r
++\r
++ notmuch_mailstore_close (file);\r
++\r
++ return result;\r
++}\r
+diff --git a/mime-node.c b/mime-node.c\r
+index 97e8b48..a5c60d0 100644\r
+--- a/mime-node.c\r
++++ b/mime-node.c\r
+@@ -49,7 +49,7 @@ _mime_node_context_free (mime_node_context_t *res)\r
+ g_object_unref (res->stream);\r
+ \r
+ if (res->file)\r
+- fclose (res->file);\r
++ notmuch_mailstore_close (res->file);\r
+ \r
+ return 0;\r
+ }\r
+@@ -79,7 +79,7 @@ mime_node_open (const void *ctx, notmuch_message_t *message,\r
+ }\r
+ talloc_set_destructor (mctx, _mime_node_context_free);\r
+ \r
+- mctx->file = fopen (filename, "r");\r
++ mctx->file = notmuch_mailstore_open (filename);\r
+ if (! mctx->file) {\r
+ fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));\r
+ status = NOTMUCH_STATUS_FILE_ERROR;\r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index 8247f1d..2847d25 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -692,7 +692,7 @@ format_part_mbox (const void *ctx, mime_node_t *node, unused (int indent),\r
+ INTERNAL_ERROR ("format_part_mbox requires a root part");\r
+ \r
+ filename = notmuch_message_get_filename (message);\r
+- file = fopen (filename, "r");\r
++ file = notmuch_mailstore_open (filename);\r
+ if (file == NULL) {\r
+ fprintf (stderr, "Failed to open %s: %s\n",\r
+ filename, strerror (errno));\r
+@@ -716,7 +716,7 @@ format_part_mbox (const void *ctx, mime_node_t *node, unused (int indent),\r
+ \r
+ printf ("\n");\r
+ \r
+- fclose (file);\r
++ notmuch_mailstore_close (file);\r
+ \r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
+@@ -739,7 +739,7 @@ format_part_raw (unused (const void *ctx), mime_node_t *node,\r
+ return NOTMUCH_STATUS_FILE_ERROR;\r
+ }\r
+ \r
+- file = fopen (filename, "r");\r
++ file = notmuch_mailstore_open (filename);\r
+ if (file == NULL) {\r
+ fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));\r
+ return NOTMUCH_STATUS_FILE_ERROR;\r
+@@ -749,18 +749,18 @@ format_part_raw (unused (const void *ctx), mime_node_t *node,\r
+ size = fread (buf, 1, sizeof (buf), file);\r
+ if (ferror (file)) {\r
+ fprintf (stderr, "Error: Read failed from %s\n", filename);\r
+- fclose (file);\r
++ notmuch_mailstore_close (file);\r
+ return NOTMUCH_STATUS_FILE_ERROR;\r
+ }\r
+ \r
+ if (fwrite (buf, size, 1, stdout) != 1) {\r
+ fprintf (stderr, "Error: Write failed\n");\r
+- fclose (file);\r
++ notmuch_mailstore_close (file);\r
+ return NOTMUCH_STATUS_FILE_ERROR;\r
+ }\r
+ }\r
+ \r
+- fclose (file);\r
++ notmuch_mailstore_close (file);\r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
+ \r
+-- \r
+1.7.9.5\r
+\r