Re: [RFC PATCH 01/14] All access to mail files goes through the mailstore module
authorMark Walters <markwalters1009@gmail.com>
Thu, 28 Jun 2012 20:48:03 +0000 (21:48 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:52 +0000 (09:47 -0800)
63/e0acb9fb4dd8f2436a8095e28a6909a000d854 [new file with mode: 0644]

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