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