--- /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 4BCCD431FAF\r
+ for <notmuch@notmuchmail.org>; Sun, 1 Jul 2012 09:43:58 -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 IcWCwRaGnQ-Z for <notmuch@notmuchmail.org>;\r
+ Sun, 1 Jul 2012 09:43:57 -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 CDE2B431FAE\r
+ for <notmuch@notmuchmail.org>; Sun, 1 Jul 2012 09:43:56 -0700 (PDT)\r
+Received: by werl57 with SMTP id l57so1618265wer.26\r
+ for <notmuch@notmuchmail.org>; Sun, 01 Jul 2012 09:43:55 -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=HFZYPZWE2tJvh0L84N2VyKBmN4UECENqiYAfodHg8V0=;\r
+ b=GMSYcygYsu6LEb0kW/wqXrV96HAdWkw94GLrOZS9s5ZSo9wIlhiFt9VE4lqI+BzDbB\r
+ kK6LMWhETNF9z7DUT5aF8RxpLitpBKRmS3C4GsxC4He5qqeYzO19NY0aqVgpRqkKJ89L\r
+ G1M4FLkT+OpAZncoD8B+9Fwl/BWXsbUzAuOlTF009ph8/0PDXIvYjbR0pcgCjMPko+IC\r
+ zVKi98OnKgE+Kg+M6VlxWezoCrh1cIFmxiBucuBWHS6ZwdcQebcfj/MLl5uVlZ6WKrRy\r
+ m5UecMnRQD3QN0hKlFeQOes7hkq3YRGK9xMIfLBqcsKByAZq46G+JIxwyisreG74x6c2\r
+ VDJw==\r
+Received: by 10.216.150.166 with SMTP id z38mr456739wej.78.1341161035546;\r
+ Sun, 01 Jul 2012 09:43:55 -0700 (PDT)\r
+Received: from localhost ([195.24.209.21])\r
+ by mx.google.com with ESMTPS id db7sm19694889wib.6.2012.07.01.09.43.05\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Sun, 01 Jul 2012 09:43:54 -0700 (PDT)\r
+From: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject:\r
+ [PATCH v2 1/8] All access to mail files goes through the mailstore module\r
+Date: Sun, 1 Jul 2012 12:39:43 -0400\r
+Message-Id: <1341160790-14525-2-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
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\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:43:58 -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
+No functional changes are made, but calls to fopen and fclose are\r
+replaced with notmuch_mailstore_open and notmuch_mailstore_close\r
+wherever they are being used to access messages, but not when talking\r
+about real files like in notmuch-dump or notmuch-restore.\r
+\r
+Since the sha1 function notmuch_sha1_of_file is only used on messages,\r
+we convert it to using notmuch_mailstore_open and\r
+notmuch_mailstore_close, and rename it notmuch_sha1_of_message. While\r
+we are there, we also replace a numeric constant with its symbolic\r
+name BLOCK_SIZE.\r
+\r
+Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+---\r
+This patch does not split notmuch_sha1_of_file the way the previous\r
+patch series did, because there are no other callers of\r
+notmuch_sha1_of_file. Instead it renames the function to\r
+notmuch_sha1_of_message.\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 | 2 +-\r
+ lib/notmuch.h | 16 ++++++++++++++++\r
+ lib/sha1.c | 11 +++++------\r
+ mime-node.c | 4 ++--\r
+ notmuch-show.c | 12 ++++++------\r
+ 10 files changed, 70 insertions(+), 20 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..ef1f994 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -466,7 +466,7 @@ char *\r
+ notmuch_sha1_of_string (const char *str);\r
+ \r
+ char *\r
+-notmuch_sha1_of_file (const char *filename);\r
++notmuch_sha1_of_message (const char *filename);\r
+ \r
+ /* string-list.c */\r
+ \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..f4d213a 100644\r
+--- a/lib/sha1.c\r
++++ b/lib/sha1.c\r
+@@ -65,7 +65,7 @@ notmuch_sha1_of_string (const char *str)\r
+ }\r
+ \r
+ /* Create a hexadecimal string version of the SHA-1 digest of the\r
+- * contents of the named file.\r
++ * contents of the named message.\r
+ *\r
+ * This function returns a newly allocated string which the caller\r
+ * should free() when finished.\r
+@@ -74,7 +74,7 @@ notmuch_sha1_of_string (const char *str)\r
+ * file not found, etc.), this function returns NULL.\r
+ */\r
+ char *\r
+-notmuch_sha1_of_file (const char *filename)\r
++notmuch_sha1_of_message (const char *filename)\r
+ {\r
+ FILE *file;\r
+ #define BLOCK_SIZE 4096\r
+@@ -84,14 +84,14 @@ notmuch_sha1_of_file (const char *filename)\r
+ unsigned char digest[SHA1_DIGEST_SIZE];\r
+ char *result;\r
+ \r
+- file = fopen (filename, "r");\r
++ file = notmuch_mailstore_open (filename);\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 +108,7 @@ notmuch_sha1_of_file (const char *filename)\r
+ \r
+ result = _hex_of_sha1_digest (digest);\r
+ \r
+- fclose (file);\r
++ notmuch_mailstore_close (file);\r
+ \r
+ return result;\r
+ }\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 8f3c60e..daffb59 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -693,7 +693,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
+@@ -717,7 +717,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
+@@ -740,7 +740,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
+@@ -750,18 +750,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