[RFC PATCH 05/13] notmuch_database_add_message calculates sha1 of messages using...
authorEthan Glasser-Camp <glasse@cs.rpi.edu>
Wed, 15 Feb 2012 22:01:58 +0000 (17:01 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:44:34 +0000 (09:44 -0800)
37/22fced2cd8f920010fd3945092e4b756ad59ca [new file with mode: 0644]

diff --git a/37/22fced2cd8f920010fd3945092e4b756ad59ca b/37/22fced2cd8f920010fd3945092e4b756ad59ca
new file mode 100644 (file)
index 0000000..619641d
--- /dev/null
@@ -0,0 +1,208 @@
+Return-Path: <glasse@cs.rpi.edu>\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 0C02F431E84\r
+       for <notmuch@notmuchmail.org>; Wed, 15 Feb 2012 14:08:14 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.54\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.54 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+       RCVD_IN_BL_SPAMCOP_NET=1.246, RCVD_IN_DNSWL_MED=-2.3,\r
+       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 Z5T4hwHmdNMx for <notmuch@notmuchmail.org>;\r
+       Wed, 15 Feb 2012 14:08:10 -0800 (PST)\r
+Received: from cliffclavin.cs.rpi.edu (cliffclavin.cs.rpi.edu\r
+ [128.113.126.25])     (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)       by olra.theworths.org (Postfix) with ESMTPS\r
+ id CC196431E62        for <notmuch@notmuchmail.org>; Wed, 15 Feb 2012 14:08:09 -0800\r
+ (PST)\r
+X-Hash:\r
+ S|ab6d52123042524f17ae1cbf5371e0f7ad136fdb|dc8a24c5bb94d39980edb37ffe428d08\r
+X-Countries: Cameroon, United States\r
+X-SMTP-From: accepted <glasse@cs.rpi.edu> [195.24.209.20] [195.24.209.20]\r
+       (localhost) {Cameroon}\r
+DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=cs.rpi.edu; h=from\r
+       :to:cc:subject:date:message-id:in-reply-to:references; s=\r
+       default; i=glasse@cs.rpi.edu; t=1329343687; x=1329948487; l=4043;\r
+       bh=LUFsjn2hMG717omq6T++K0okDCY=; b=o51Wm0PLX0QMhVbynToyM41\r
+       xT4a+6d2m6W0EejAWZNNjdTViDbWwcK9/NvLNQOPPz4rTI4rL4JmwFGJ/Q5xRjCB\r
+       lX/ywNJfjtu2Uzo4CbBtt9xBMyhrx2lcNAYNSwg2UFyzn9zAAY/9UTwa9LwSn1e2\r
+       6g4rqDeMntMfk2Lj7/do=\r
+DomainKey-Signature: a=rsa-sha1; c=nofws; d=cs.rpi.edu; h=from:to:cc\r
+       :subject:date:message-id:in-reply-to:references; q=dns; s=\r
+       default; b=qgZc53kWMg5BGk85wGJEKucipxOa6jej8bYqNYC4MYBQnXdPtHFJm\r
+       c5FuSmR7ewW9uUU0+aHkS2Or/r+tBAkCIQGXTs4JoDzJDdt9CF010LqZY6hR93IZ\r
+       /6+peXGf/vD2zRDkM8BMmM9ZDB5XvUQgrrbEvnZ7vWNtGrGzGEtoOE=\r
+X-Spam-Info: -2.7; ALL_TRUSTED,AWL,BAYES_00\r
+X-Spam-Scanned-By: cliffclavin.cs.rpi.edu using SpamAssassin 3.2.5 (hard limit\r
+       15)\r
+Authentication-Results: cliffclavin.cs.rpi.edu;\r
+       DKIM=neutral (none) header.from=glasse@cs.rpi.edu;\r
+       SPF=neutral (mfrom;\r
+       Mechanism '?all' matched) smtp.mail=glasse@cs.rpi.edu\r
+X-Auth-Passed: cliffclavin.cs.rpi.edu:q1FM7Ywh008805 Auth:glasse\r
+X-Virus-Scanned-By: cliffclavin.cs.rpi.edu\r
+Received: from localhost ([195.24.209.20]) (authenticated bits=0)\r
+       by cliffclavin.cs.rpi.edu (8.14.3/8.14.3) with ESMTP id q1FM7Ywh008805\r
+       (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO);\r
+       Wed, 15 Feb 2012 17:07:48 -0500 (EST)\r
+       (envelope-from glasse@cs.rpi.edu)\r
+From: Ethan Glasser-Camp <glasse@cs.rpi.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH 05/13] notmuch_database_add_message calculates sha1 of\r
+       messages using mailstore\r
+Date: Wed, 15 Feb 2012 17:01:58 -0500\r
+Message-Id: <1329343326-16410-6-git-send-email-glasse@cs.rpi.edu>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <1329343326-16410-1-git-send-email-glasse@cs.rpi.edu>\r
+References: <1329343326-16410-1-git-send-email-glasse@cs.rpi.edu>\r
+X-Scanned-By: MIMEDefang 2.67 on 128.113.126.25\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: Wed, 15 Feb 2012 22:08:14 -0000\r
+\r
+From: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+\r
+Previously, notmuch_database_add_message used the notmuch_sha1_of_file\r
+function, which accesses a mail file directly. Create a new function\r
+called notmuch_sha1_of_message which uses a mailstore to access the\r
+file, and use that instead.\r
+\r
+Also, as a drive-by cleanup, use the named constant BLOCK_SIZE instead\r
+of the integer literal 4096.\r
+\r
+Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+---\r
+ lib/database.cc       |    2 +-\r
+ lib/notmuch-private.h |    3 ++\r
+ lib/sha1.c            |   52 ++++++++++++++++++++++++++++++++++++------------\r
+ 3 files changed, 43 insertions(+), 14 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index e3c8095..ff44e76 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -1700,7 +1700,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 (notmuch->mailstore, filename);\r
+ \r
+           /* If that failed too, something is really wrong. Give up. */\r
+           if (sha1 == NULL) {\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 0f01437..2589928 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -454,6 +454,9 @@ notmuch_sha1_of_string (const char *str);\r
+ char *\r
+ notmuch_sha1_of_file (const char *filename);\r
+ \r
++char *\r
++notmuch_sha1_of_message (notmuch_mailstore_t *mailstore, const char *filename);\r
++\r
+ /* string-list.c */\r
+ \r
+ typedef struct _notmuch_string_node {\r
+diff --git a/lib/sha1.c b/lib/sha1.c\r
+index cc48108..ea25999 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
+ \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 functions.\r
+  */\r
+-char *\r
+-notmuch_sha1_of_file (const char *filename)\r
++static char *\r
++_notmuch_sha1_of_filep (FILE *file)\r
+ {\r
+-    FILE *file;\r
+ #define BLOCK_SIZE 4096\r
+     unsigned char block[BLOCK_SIZE];\r
+     size_t bytes_read;\r
+@@ -84,14 +76,13 @@ notmuch_sha1_of_file (const char *filename)\r
+     unsigned char digest[SHA1_DIGEST_SIZE];\r
+     char *result;\r
+ \r
+-    file = fopen (filename, "r");\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
+@@ -113,3 +104,38 @@ notmuch_sha1_of_file (const char *filename)\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
++\r
++    file = fopen (filename, "r");\r
++    return _notmuch_sha1_of_filep (file);\r
++}\r
++\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 (notmuch_mailstore_t *mailstore, const char *filename)\r
++{\r
++    FILE *file;\r
++\r
++    file = notmuch_mailstore_open (mailstore, filename);\r
++    return _notmuch_sha1_of_filep (file);\r
++}\r
+-- \r
+1.7.5.4\r
+\r