Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 4BCCD431FAF for ; Sun, 1 Jul 2012 09:43:58 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 1.061 X-Spam-Level: * X-Spam-Status: No, score=1.061 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_BL_SPAMCOP_NET=1.246, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_SORBS_WEB=0.614] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id IcWCwRaGnQ-Z for ; Sun, 1 Jul 2012 09:43:57 -0700 (PDT) Received: from mail-we0-f181.google.com (mail-we0-f181.google.com [74.125.82.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id CDE2B431FAE for ; Sun, 1 Jul 2012 09:43:56 -0700 (PDT) Received: by werl57 with SMTP id l57so1618265wer.26 for ; Sun, 01 Jul 2012 09:43:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=HFZYPZWE2tJvh0L84N2VyKBmN4UECENqiYAfodHg8V0=; b=GMSYcygYsu6LEb0kW/wqXrV96HAdWkw94GLrOZS9s5ZSo9wIlhiFt9VE4lqI+BzDbB kK6LMWhETNF9z7DUT5aF8RxpLitpBKRmS3C4GsxC4He5qqeYzO19NY0aqVgpRqkKJ89L G1M4FLkT+OpAZncoD8B+9Fwl/BWXsbUzAuOlTF009ph8/0PDXIvYjbR0pcgCjMPko+IC zVKi98OnKgE+Kg+M6VlxWezoCrh1cIFmxiBucuBWHS6ZwdcQebcfj/MLl5uVlZ6WKrRy m5UecMnRQD3QN0hKlFeQOes7hkq3YRGK9xMIfLBqcsKByAZq46G+JIxwyisreG74x6c2 VDJw== Received: by 10.216.150.166 with SMTP id z38mr456739wej.78.1341161035546; Sun, 01 Jul 2012 09:43:55 -0700 (PDT) Received: from localhost ([195.24.209.21]) by mx.google.com with ESMTPS id db7sm19694889wib.6.2012.07.01.09.43.05 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 01 Jul 2012 09:43:54 -0700 (PDT) From: Ethan Glasser-Camp To: notmuch@notmuchmail.org Subject: [PATCH v2 1/8] All access to mail files goes through the mailstore module Date: Sun, 1 Jul 2012 12:39:43 -0400 Message-Id: <1341160790-14525-2-git-send-email-ethan@betacantrips.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1341160790-14525-1-git-send-email-ethan@betacantrips.com> References: <1341160790-14525-1-git-send-email-ethan@betacantrips.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Ethan Glasser-Camp X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 01 Jul 2012 16:43:58 -0000 This commit introduces the mailstore module which provides two functions, notmuch_mailstore_open and notmuch_mailstore_close. These functions are currently just stub calls to fopen and fclose, but later can be made more complex in order to support mail storage systems where one message might not be one file. No functional changes are made, but calls to fopen and fclose are replaced with notmuch_mailstore_open and notmuch_mailstore_close wherever they are being used to access messages, but not when talking about real files like in notmuch-dump or notmuch-restore. Since the sha1 function notmuch_sha1_of_file is only used on messages, we convert it to using notmuch_mailstore_open and notmuch_mailstore_close, and rename it notmuch_sha1_of_message. While we are there, we also replace a numeric constant with its symbolic name BLOCK_SIZE. Signed-off-by: Ethan Glasser-Camp --- This patch does not split notmuch_sha1_of_file the way the previous patch series did, because there are no other callers of notmuch_sha1_of_file. Instead it renames the function to notmuch_sha1_of_message. lib/Makefile.local | 1 + lib/database.cc | 2 +- lib/index.cc | 2 +- lib/mailstore.c | 34 ++++++++++++++++++++++++++++++++++ lib/message-file.c | 6 +++--- lib/notmuch-private.h | 2 +- lib/notmuch.h | 16 ++++++++++++++++ lib/sha1.c | 11 +++++------ mime-node.c | 4 ++-- notmuch-show.c | 12 ++++++------ 10 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 lib/mailstore.c diff --git a/lib/Makefile.local b/lib/Makefile.local index 8a9aa28..cfc77bb 100644 --- a/lib/Makefile.local +++ b/lib/Makefile.local @@ -51,6 +51,7 @@ libnotmuch_c_srcs = \ $(dir)/filenames.c \ $(dir)/string-list.c \ $(dir)/libsha1.c \ + $(dir)/mailstore.c \ $(dir)/message-file.c \ $(dir)/messages.c \ $(dir)/sha1.c \ diff --git a/lib/database.cc b/lib/database.cc index 761dc1a..c035edc 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -1773,7 +1773,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch, if (message_id == NULL ) { /* No message-id at all, let's generate one by taking a * hash over the file's contents. */ - char *sha1 = notmuch_sha1_of_file (filename); + char *sha1 = notmuch_sha1_of_message (filename); /* If that failed too, something is really wrong. Give up. */ if (sha1 == NULL) { diff --git a/lib/index.cc b/lib/index.cc index e377732..b607e82 100644 --- a/lib/index.cc +++ b/lib/index.cc @@ -441,7 +441,7 @@ _notmuch_message_index_file (notmuch_message_t *message, initialized = 1; } - file = fopen (filename, "r"); + file = notmuch_mailstore_open (filename); if (! file) { fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno)); ret = NOTMUCH_STATUS_FILE_ERROR; diff --git a/lib/mailstore.c b/lib/mailstore.c new file mode 100644 index 0000000..48acd47 --- /dev/null +++ b/lib/mailstore.c @@ -0,0 +1,34 @@ +/* mailstore.c - code to access individual messages + * + * Copyright © 2009 Carl Worth + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/ . + * + * Author: Carl Worth + */ +#include + +#include "notmuch-private.h" + +FILE * +notmuch_mailstore_open (const char *filename) +{ + return fopen (filename, "r"); +} + +int +notmuch_mailstore_close (FILE *file) +{ + return fclose (file); +} diff --git a/lib/message-file.c b/lib/message-file.c index 915aba8..271389c 100644 --- a/lib/message-file.c +++ b/lib/message-file.c @@ -86,7 +86,7 @@ _notmuch_message_file_destructor (notmuch_message_file_t *message) g_hash_table_destroy (message->headers); if (message->file) - fclose (message->file); + notmuch_mailstore_close (message->file); return 0; } @@ -104,7 +104,7 @@ _notmuch_message_file_open_ctx (void *ctx, const char *filename) talloc_set_destructor (message, _notmuch_message_file_destructor); - message->file = fopen (filename, "r"); + message->file = notmuch_mailstore_open (filename); if (message->file == NULL) goto FAIL; @@ -361,7 +361,7 @@ notmuch_message_file_get_header (notmuch_message_file_t *message, } if (message->parsing_finished) { - fclose (message->file); + notmuch_mailstore_close (message->file); message->file = NULL; } diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h index bfb4111..ef1f994 100644 --- a/lib/notmuch-private.h +++ b/lib/notmuch-private.h @@ -466,7 +466,7 @@ char * notmuch_sha1_of_string (const char *str); char * -notmuch_sha1_of_file (const char *filename); +notmuch_sha1_of_message (const char *filename); /* string-list.c */ diff --git a/lib/notmuch.h b/lib/notmuch.h index 3633bed..0ca367b 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -1233,6 +1233,22 @@ notmuch_message_thaw (notmuch_message_t *message); void notmuch_message_destroy (notmuch_message_t *message); +/* Get access to the underlying data of a message. + * + * Right now, all messages are simple files in maildirs, but this is + * hopefully subject to change. + * + * If the returned FILE* is not NULL, be sure to call + * notmuch_mailstore_close when you're done with it. + */ +FILE * +notmuch_mailstore_open (const char *filename); + +/* Release any resources associated with this file. + */ +int +notmuch_mailstore_close (FILE *file); + /* Is the given 'tags' iterator pointing at a valid tag. * * When this function returns TRUE, notmuch_tags_get will return a diff --git a/lib/sha1.c b/lib/sha1.c index cc48108..f4d213a 100644 --- a/lib/sha1.c +++ b/lib/sha1.c @@ -65,7 +65,7 @@ notmuch_sha1_of_string (const char *str) } /* Create a hexadecimal string version of the SHA-1 digest of the - * contents of the named file. + * contents of the named message. * * This function returns a newly allocated string which the caller * should free() when finished. @@ -74,7 +74,7 @@ notmuch_sha1_of_string (const char *str) * file not found, etc.), this function returns NULL. */ char * -notmuch_sha1_of_file (const char *filename) +notmuch_sha1_of_message (const char *filename) { FILE *file; #define BLOCK_SIZE 4096 @@ -84,14 +84,14 @@ notmuch_sha1_of_file (const char *filename) unsigned char digest[SHA1_DIGEST_SIZE]; char *result; - file = fopen (filename, "r"); + file = notmuch_mailstore_open (filename); if (file == NULL) return NULL; sha1_begin (&sha1); while (1) { - bytes_read = fread (block, 1, 4096, file); + bytes_read = fread (block, 1, BLOCK_SIZE, file); if (bytes_read == 0) { if (feof (file)) { break; @@ -108,8 +108,7 @@ notmuch_sha1_of_file (const char *filename) result = _hex_of_sha1_digest (digest); - fclose (file); + notmuch_mailstore_close (file); return result; } - diff --git a/mime-node.c b/mime-node.c index 97e8b48..a5c60d0 100644 --- a/mime-node.c +++ b/mime-node.c @@ -49,7 +49,7 @@ _mime_node_context_free (mime_node_context_t *res) g_object_unref (res->stream); if (res->file) - fclose (res->file); + notmuch_mailstore_close (res->file); return 0; } @@ -79,7 +79,7 @@ mime_node_open (const void *ctx, notmuch_message_t *message, } talloc_set_destructor (mctx, _mime_node_context_free); - mctx->file = fopen (filename, "r"); + mctx->file = notmuch_mailstore_open (filename); if (! mctx->file) { fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno)); status = NOTMUCH_STATUS_FILE_ERROR; diff --git a/notmuch-show.c b/notmuch-show.c index 8f3c60e..daffb59 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -693,7 +693,7 @@ format_part_mbox (const void *ctx, mime_node_t *node, unused (int indent), INTERNAL_ERROR ("format_part_mbox requires a root part"); filename = notmuch_message_get_filename (message); - file = fopen (filename, "r"); + file = notmuch_mailstore_open (filename); if (file == NULL) { fprintf (stderr, "Failed to open %s: %s\n", filename, strerror (errno)); @@ -717,7 +717,7 @@ format_part_mbox (const void *ctx, mime_node_t *node, unused (int indent), printf ("\n"); - fclose (file); + notmuch_mailstore_close (file); return NOTMUCH_STATUS_SUCCESS; } @@ -740,7 +740,7 @@ format_part_raw (unused (const void *ctx), mime_node_t *node, return NOTMUCH_STATUS_FILE_ERROR; } - file = fopen (filename, "r"); + file = notmuch_mailstore_open (filename); if (file == NULL) { fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno)); return NOTMUCH_STATUS_FILE_ERROR; @@ -750,18 +750,18 @@ format_part_raw (unused (const void *ctx), mime_node_t *node, size = fread (buf, 1, sizeof (buf), file); if (ferror (file)) { fprintf (stderr, "Error: Read failed from %s\n", filename); - fclose (file); + notmuch_mailstore_close (file); return NOTMUCH_STATUS_FILE_ERROR; } if (fwrite (buf, size, 1, stdout) != 1) { fprintf (stderr, "Error: Write failed\n"); - fclose (file); + notmuch_mailstore_close (file); return NOTMUCH_STATUS_FILE_ERROR; } } - fclose (file); + notmuch_mailstore_close (file); return NOTMUCH_STATUS_SUCCESS; } -- 1.7.9.5