From: Mark Walters Date: Thu, 28 Jun 2012 20:48:03 +0000 (+0100) Subject: Re: [RFC PATCH 01/14] All access to mail files goes through the mailstore module X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=f7d6727d3cb6a3c7b4bb9ff2e1ef7729121f18cb;p=notmuch-archives.git Re: [RFC PATCH 01/14] All access to mail files goes through the mailstore module --- diff --git a/63/e0acb9fb4dd8f2436a8095e28a6909a000d854 b/63/e0acb9fb4dd8f2436a8095e28a6909a000d854 new file mode 100644 index 000000000..9c9cfb340 --- /dev/null +++ b/63/e0acb9fb4dd8f2436a8095e28a6909a000d854 @@ -0,0 +1,457 @@ +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 DD7C1431FB6 + for ; Thu, 28 Jun 2012 13:48:19 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -1.098 +X-Spam-Level: +X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5 + tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001, + NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] 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 xiUrUEVhmjx3 for ; + Thu, 28 Jun 2012 13:48:16 -0700 (PDT) +Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id E5E54431FAF + for ; Thu, 28 Jun 2012 13:48:15 -0700 (PDT) +Received: from smtp.qmul.ac.uk ([138.37.6.40]) + by mail2.qmul.ac.uk with esmtp (Exim 4.71) + (envelope-from ) + id 1SkLdK-0001MW-W2; Thu, 28 Jun 2012 21:48:14 +0100 +Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223] + helo=localhost) + by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69) + (envelope-from ) + id 1SkLdK-0004kd-8q; Thu, 28 Jun 2012 21:48:10 +0100 +From: Mark Walters +To: Ethan Glasser-Camp , notmuch@notmuchmail.org +Subject: Re: [RFC PATCH 01/14] All access to mail files goes through the + mailstore module +In-Reply-To: <1340656899-5644-2-git-send-email-ethan@betacantrips.com> +References: <1340656899-5644-1-git-send-email-ethan@betacantrips.com> + <1340656899-5644-2-git-send-email-ethan@betacantrips.com> +User-Agent: Notmuch/0.13.2+63~g548a9bf (http://notmuchmail.org) Emacs/23.4.1 + (x86_64-pc-linux-gnu) +Date: Thu, 28 Jun 2012 21:48:03 +0100 +Message-ID: <87hatvmad8.fsf@qmul.ac.uk> +MIME-Version: 1.0 +Content-Type: text/plain; charset=utf-8 +Content-Transfer-Encoding: quoted-printable +X-Sender-Host-Address: 94.192.233.223 +X-QM-SPAM-Info: Sender has good ham record. :) +X-QM-Body-MD5: ed266fc29b565e957cea11b36eb1212c (of first 20000 bytes) +X-SpamAssassin-Score: -1.8 +X-SpamAssassin-SpamBar: - +X-SpamAssassin-Report: The QM spam filters have analysed this message to + determine if it is + spam. We require at least 5.0 points to mark a message as spam. + This message scored -1.8 points. + Summary of the scoring: + * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/, + * medium trust + * [138.37.6.40 listed in list.dnswl.org] + * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail + provider * (markwalters1009[at]gmail.com) + * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay + * domain + * 0.5 AWL AWL: From: address is in the auto white-list +X-QM-Scan-Virus: ClamAV says the message is clean +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: Thu, 28 Jun 2012 20:48:20 -0000 + + +On Mon, 25 Jun 2012, Ethan Glasser-Camp wrot= +e: +> 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. + +I would prefer this patch split into two: one doing the stuff mentioned +in the commit message and one the notmuch_sha1_of_message/ +notmuch_sha1_of_file change (or at least mention this latter change in +the commit message).=20 + +Best wishes + +Mark=20 + +> Signed-off-by: Ethan Glasser-Camp +> --- +> 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 | 3 +++ +> lib/notmuch.h | 16 +++++++++++ +> lib/sha1.c | 70 +++++++++++++++++++++++++++++++++++++------= +------ +> mime-node.c | 4 +-- +> notmuch-show.c | 12 ++++----- +> 10 files changed, 120 insertions(+), 30 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 =3D \ +> $(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 *n= +otmuch, +> if (message_id =3D=3D NULL ) { +> /* No message-id at all, let's generate one by taking a +> * hash over the file's contents. */ +> - char *sha1 =3D notmuch_sha1_of_file (filename); +> + char *sha1 =3D notmuch_sha1_of_message (filename); +>=20=20 +> /* If that failed too, something is really wrong. Give up. */ +> if (sha1 =3D=3D 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 *messa= +ge, +> initialized =3D 1; +> } +>=20=20 +> - file =3D fopen (filename, "r"); +> + file =3D notmuch_mailstore_open (filename); +> if (! file) { +> fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno)); +> ret =3D 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 =C2=A9 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); +>=20=20 +> if (message->file) +> - fclose (message->file); +> + notmuch_mailstore_close (message->file); +>=20=20 +> return 0; +> } +> @@ -104,7 +104,7 @@ _notmuch_message_file_open_ctx (void *ctx, const char= + *filename) +>=20=20 +> talloc_set_destructor (message, _notmuch_message_file_destructor); +>=20=20 +> - message->file =3D fopen (filename, "r"); +> + message->file =3D notmuch_mailstore_open (filename); +> if (message->file =3D=3D NULL) +> goto FAIL; +>=20=20 +> @@ -361,7 +361,7 @@ notmuch_message_file_get_header (notmuch_message_file= +_t *message, +> } +>=20=20 +> if (message->parsing_finished) { +> - fclose (message->file); +> + notmuch_mailstore_close (message->file); +> message->file =3D NULL; +> } +>=20=20 +> diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h +> index bfb4111..5dbe821 100644 +> --- a/lib/notmuch-private.h +> +++ b/lib/notmuch-private.h +> @@ -468,6 +468,9 @@ notmuch_sha1_of_string (const char *str); +> char * +> notmuch_sha1_of_file (const char *filename); +>=20=20 +> +char * +> +notmuch_sha1_of_message (const char *filename); +> + +> /* string-list.c */ +>=20=20 +> typedef struct _notmuch_string_node { +> 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); +>=20=20 +> +/* 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..462c07e 100644 +> --- a/lib/sha1.c +> +++ b/lib/sha1.c +> @@ -64,19 +64,11 @@ notmuch_sha1_of_string (const char *str) +> return _hex_of_sha1_digest (digest); +> } +>=20=20 +> -/* Create a hexadecimal string version of the SHA-1 digest of the +> - * contents of the named file. +> - * +> - * This function returns a newly allocated string which the caller +> - * should free() when finished. +> - * +> - * If any error occurs while reading the file, (permission denied, +> - * file not found, etc.), this function returns NULL. +> +/* Internal function to feed the contents of a FILE * to the sha1 functi= +ons. +> */ +> -char * +> -notmuch_sha1_of_file (const char *filename) +> -{ +> - FILE *file; +> + +> +static char * +> +_notmuch_sha1_of_filep (FILE *file){ +> #define BLOCK_SIZE 4096 +> unsigned char block[BLOCK_SIZE]; +> size_t bytes_read; +> @@ -84,14 +76,10 @@ notmuch_sha1_of_file (const char *filename) +> unsigned char digest[SHA1_DIGEST_SIZE]; +> char *result; +>=20=20 +> - file =3D fopen (filename, "r"); +> - if (file =3D=3D NULL) +> - return NULL; +> - +> sha1_begin (&sha1); +>=20=20 +> while (1) { +> - bytes_read =3D fread (block, 1, 4096, file); +> + bytes_read =3D fread (block, 1, BLOCK_SIZE, file); +> if (bytes_read =3D=3D 0) { +> if (feof (file)) { +> break; +> @@ -108,8 +96,56 @@ notmuch_sha1_of_file (const char *filename) +>=20=20 +> result =3D _hex_of_sha1_digest (digest); +>=20=20 +> + return result; +> +} +> + +> +/* Create a hexadecimal string version of the SHA-1 digest of the +> + * contents of the named file. +> + * +> + * This function returns a newly allocated string which the caller +> + * should free() when finished. +> + * +> + * If any error occurs while reading the file, (permission denied, +> + * file not found, etc.), this function returns NULL. +> + */ +> +char * +> +notmuch_sha1_of_file (const char *filename) +> +{ +> + FILE *file; +> + char *result; +> + file =3D fopen (filename, "r"); +> + if (file =3D=3D NULL) +> + return NULL; +> + +> + result =3D _notmuch_sha1_of_filep (file); +> + +> fclose (file); +>=20=20 +> return result; +> } +>=20=20 +> +/* Create a hexadecimal string version of the SHA-1 digest of the +> + * contents of the named message, which is accessed via a mailstore. +> + * +> + * This function returns a newly allocated string which the caller +> + * should free() when finished. +> + * +> + * If any error occurs while reading the file, (permission denied, +> + * file not found, etc.), this function returns NULL. +> + */ +> +char * +> +notmuch_sha1_of_message (const char *filename) +> +{ +> + FILE *file; +> + char *result; +> + +> + file =3D notmuch_mailstore_open (filename); +> + if (file =3D=3D NULL) +> + return NULL; +> + +> + result =3D _notmuch_sha1_of_filep (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); +>=20=20 +> if (res->file) +> - fclose (res->file); +> + notmuch_mailstore_close (res->file); +>=20=20 +> return 0; +> } +> @@ -79,7 +79,7 @@ mime_node_open (const void *ctx, notmuch_message_t *mes= +sage, +> } +> talloc_set_destructor (mctx, _mime_node_context_free); +>=20=20 +> - mctx->file =3D fopen (filename, "r"); +> + mctx->file =3D notmuch_mailstore_open (filename); +> if (! mctx->file) { +> fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno)); +> status =3D NOTMUCH_STATUS_FILE_ERROR; +> diff --git a/notmuch-show.c b/notmuch-show.c +> index 8247f1d..2847d25 100644 +> --- a/notmuch-show.c +> +++ b/notmuch-show.c +> @@ -692,7 +692,7 @@ format_part_mbox (const void *ctx, mime_node_t *node,= + unused (int indent), +> INTERNAL_ERROR ("format_part_mbox requires a root part"); +>=20=20 +> filename =3D notmuch_message_get_filename (message); +> - file =3D fopen (filename, "r"); +> + file =3D notmuch_mailstore_open (filename); +> if (file =3D=3D NULL) { +> fprintf (stderr, "Failed to open %s: %s\n", +> filename, strerror (errno)); +> @@ -716,7 +716,7 @@ format_part_mbox (const void *ctx, mime_node_t *node,= + unused (int indent), +>=20=20 +> printf ("\n"); +>=20=20 +> - fclose (file); +> + notmuch_mailstore_close (file); +>=20=20 +> return NOTMUCH_STATUS_SUCCESS; +> } +> @@ -739,7 +739,7 @@ format_part_raw (unused (const void *ctx), mime_node_= +t *node, +> return NOTMUCH_STATUS_FILE_ERROR; +> } +>=20=20 +> - file =3D fopen (filename, "r"); +> + file =3D notmuch_mailstore_open (filename); +> if (file =3D=3D NULL) { +> fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, stre= +rror (errno)); +> return NOTMUCH_STATUS_FILE_ERROR; +> @@ -749,18 +749,18 @@ format_part_raw (unused (const void *ctx), mime_nod= +e_t *node, +> size =3D 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; +> } +>=20=20 +> if (fwrite (buf, size, 1, stdout) !=3D 1) { +> fprintf (stderr, "Error: Write failed\n"); +> - fclose (file); +> + notmuch_mailstore_close (file); +> return NOTMUCH_STATUS_FILE_ERROR; +> } +> } +>=20=20 +> - fclose (file); +> + notmuch_mailstore_close (file); +> return NOTMUCH_STATUS_SUCCESS; +> } +>=20=20 +> --=20 +> 1.7.9.5 +> +> _______________________________________________ +> notmuch mailing list +> notmuch@notmuchmail.org +> http://notmuchmail.org/mailman/listinfo/notmuch