Re: [PATCH v2 1/8] All access to mail files goes through the mailstore module
authorMark Walters <markwalters1009@gmail.com>
Sun, 1 Jul 2012 19:48:41 +0000 (20:48 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:55 +0000 (09:47 -0800)
80/49b5d0a47338e7a895d56c459f5685a1b7d897 [new file with mode: 0644]

diff --git a/80/49b5d0a47338e7a895d56c459f5685a1b7d897 b/80/49b5d0a47338e7a895d56c459f5685a1b7d897
new file mode 100644 (file)
index 0000000..673d8c4
--- /dev/null
@@ -0,0 +1,422 @@
+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 17452431FC9\r
+       for <notmuch@notmuchmail.org>; Sun,  1 Jul 2012 12:49:02 -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 jdj8feGhOoTY for <notmuch@notmuchmail.org>;\r
+       Sun,  1 Jul 2012 12:48:58 -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 9654C431FAF\r
+       for <notmuch@notmuchmail.org>; Sun,  1 Jul 2012 12:48:57 -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 1SlQ8V-0004Tu-D8; Sun, 01 Jul 2012 20:48:53 +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 1SlQ8U-0002IS-NH; Sun, 01 Jul 2012 20:48:47 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v2 1/8] All access to mail files goes through the\r
+       mailstore module\r
+In-Reply-To: <1341160790-14525-2-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
+       <1341160790-14525-2-git-send-email-ethan@betacantrips.com>\r
+User-Agent: Notmuch/0.13.2+70~gb6a56e7 (http://notmuchmail.org) Emacs/23.4.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Sun, 01 Jul 2012 20:48:41 +0100\r
+Message-ID: <87hatr2rfq.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: 16882383536cbdacb938b727e7ef7d51 (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
+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 19:49:02 -0000\r
+\r
+On Sun, 01 Jul 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
+> 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
+This patch, though large, is simple and looks obviously correct. Indeed,\r
+given that it is really just abstracting out fopen/fclose into notmuch\r
+specific versions it would be a good first step for any more general\r
+mail store so it might be reasonable to accept it independently of the\r
+rest of the series.\r
+\r
+Best wishes\r
+\r
+Mark\r
+\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..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
+>=20=20\r
+>  char *\r
+> -notmuch_sha1_of_file (const char *filename);\r
+> +notmuch_sha1_of_message (const char *filename);\r
+>=20=20\r
+>  /* string-list.c */\r
+>=20=20\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..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
+>=20=20\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
+>=20=20\r
+> -    file =3D fopen (filename, "r");\r
+> +    file =3D notmuch_mailstore_open (filename);\r
+>      if (file =3D=3D NULL)\r
+>      return NULL;\r
+>=20=20\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 +108,7 @@ notmuch_sha1_of_file (const char *filename)\r
+>=20=20\r
+>      result =3D _hex_of_sha1_digest (digest);\r
+>=20=20\r
+> -    fclose (file);\r
+> +    notmuch_mailstore_close (file);\r
+>=20=20\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
+>=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 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,=\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
+> @@ -717,7 +717,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
+> @@ -740,7 +740,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
+> @@ -750,18 +750,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