From 77e99c991eca166bff098f99d7d176f12b9fd2d4 Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Mon, 1 Feb 2016 15:40:00 +1900 Subject: [PATCH] [PATCH v3 15/16] added notmuch_message_reindex --- ff/4af82e1203af910880b3dad6fe78d2e166fa15 | 191 ++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 ff/4af82e1203af910880b3dad6fe78d2e166fa15 diff --git a/ff/4af82e1203af910880b3dad6fe78d2e166fa15 b/ff/4af82e1203af910880b3dad6fe78d2e166fa15 new file mode 100644 index 000000000..c4765e449 --- /dev/null +++ b/ff/4af82e1203af910880b3dad6fe78d2e166fa15 @@ -0,0 +1,191 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by arlo.cworth.org (Postfix) with ESMTP id A76D66DE1BA2 + for ; Sun, 31 Jan 2016 12:40:22 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: 0 +X-Spam-Level: +X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none] + autolearn=disabled +Received: from arlo.cworth.org ([127.0.0.1]) + by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id fyvCM6BOp48V for ; + Sun, 31 Jan 2016 12:40:20 -0800 (PST) +Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108]) + by arlo.cworth.org (Postfix) with ESMTP id B1CBC6DE1AD2 + for ; Sun, 31 Jan 2016 12:40:09 -0800 (PST) +Received: from fifthhorseman.net (ip-64-134-185-108.public.wayport.net + [64.134.185.108]) + by che.mayfirst.org (Postfix) with ESMTPSA id F2200F9A5 + for ; Sun, 31 Jan 2016 15:40:06 -0500 (EST) +Received: by fifthhorseman.net (Postfix, from userid 1000) + id 647F52119A; Sun, 31 Jan 2016 15:40:06 -0500 (EST) +From: Daniel Kahn Gillmor +To: Notmuch Mail +Subject: [PATCH v3 15/16] added notmuch_message_reindex +Date: Sun, 31 Jan 2016 15:40:00 -0500 +Message-Id: <1454272801-23623-16-git-send-email-dkg@fifthhorseman.net> +X-Mailer: git-send-email 2.7.0.rc3 +In-Reply-To: <1454272801-23623-1-git-send-email-dkg@fifthhorseman.net> +References: <1454272801-23623-1-git-send-email-dkg@fifthhorseman.net> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.20 +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: Sun, 31 Jan 2016 20:40:22 -0000 + +This new function asks the database to reindex a given message, using +the supplied indexopts. + +This can be used, for example, to index the cleartext of an encrypted +message. +--- + lib/message.cc | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- + lib/notmuch.h | 14 +++++++++ + 2 files changed, 104 insertions(+), 1 deletion(-) + +diff --git a/lib/message.cc b/lib/message.cc +index 8d72ea2..3b35418 100644 +--- a/lib/message.cc ++++ b/lib/message.cc +@@ -529,7 +529,9 @@ static void + _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix) + { + Xapian::TermIterator i; +- size_t prefix_len = strlen (prefix); ++ size_t prefix_len = 0; ++ ++ prefix_len = strlen (prefix); + + while (1) { + i = message->doc.termlist_begin (); +@@ -1667,3 +1669,90 @@ _notmuch_message_database (notmuch_message_t *message) + { + return message->notmuch; + } ++ ++notmuch_status_t ++notmuch_message_reindex (notmuch_message_t *message, ++ notmuch_indexopts_t *indexopts) ++{ ++ notmuch_database_t *notmuch = NULL; ++ notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS, status; ++ notmuch_tags_t *tags = NULL; ++ notmuch_filenames_t *filenames, *orig_filenames = NULL; ++ const char *filename = NULL, *tag = NULL; ++ notmuch_message_t *newmsg = NULL; ++ notmuch_bool_t readded = FALSE, skip; ++ const char *autotags[] = { ++ "attachment", ++ "encrypted", ++ "signed", ++ "index-decrypted", ++ "index-decryption-failed" }; ++ ++ if (message == NULL) ++ return NOTMUCH_STATUS_NULL_POINTER; ++ ++ notmuch = _notmuch_message_database (message); ++ ++ /* cache tags and filenames */ ++ tags = notmuch_message_get_tags(message); ++ filenames = notmuch_message_get_filenames(message); ++ orig_filenames = notmuch_message_get_filenames(message); ++ ++ /* walk through filenames, removing them until the message is gone */ ++ for ( ; notmuch_filenames_valid (filenames); ++ notmuch_filenames_move_to_next (filenames)) { ++ filename = notmuch_filenames_get (filenames); ++ ++ ret = notmuch_database_remove_message (notmuch, filename); ++ if (ret != NOTMUCH_STATUS_SUCCESS && ++ ret != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ++ return ret; ++ } ++ if (ret != NOTMUCH_STATUS_SUCCESS) ++ return ret; ++ ++ /* re-add the filenames with the associated indexopts */ ++ for (; notmuch_filenames_valid (orig_filenames); ++ notmuch_filenames_move_to_next (orig_filenames)) { ++ filename = notmuch_filenames_get (orig_filenames); ++ ++ status = notmuch_database_add_message_with_indexopts(notmuch, ++ filename, ++ indexopts, ++ readded ? NULL : &newmsg); ++ if (status == NOTMUCH_STATUS_SUCCESS || ++ status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) { ++ if (!readded) { ++ /* re-add tags */ ++ for (; notmuch_tags_valid (tags); ++ notmuch_tags_move_to_next (tags)) { ++ tag = notmuch_tags_get (tags); ++ skip = FALSE; ++ ++ for (size_t i = 0; i < ARRAY_SIZE(autotags); i++) ++ if (strcmp (tag, autotags[i]) == 0) ++ skip = TRUE; ++ ++ if (!skip) { ++ status = notmuch_message_add_tag (newmsg, tag); ++ if (status != NOTMUCH_STATUS_SUCCESS) ++ ret = status; ++ } ++ } ++ readded = TRUE; ++ } ++ } else { ++ /* if we failed to add this filename, go ahead and try the ++ * next one as though it were first, but report the ++ * error... */ ++ ret = status; ++ } ++ } ++ if (newmsg) ++ notmuch_message_destroy (newmsg); ++ ++ /* should we also destroy the incoming message object? at the ++ * moment, we leave that to the caller */ ++ return ret; ++} ++ +diff --git a/lib/notmuch.h b/lib/notmuch.h +index 854a451..e6287cd 100644 +--- a/lib/notmuch.h ++++ b/lib/notmuch.h +@@ -1377,6 +1377,20 @@ notmuch_filenames_t * + notmuch_message_get_filenames (notmuch_message_t *message); + + /** ++ * Re-index the e-mail corresponding to 'message' using the supplied index options ++ * ++ * Returns the status of the re-index operation. (see the return ++ * codes documented in notmuch_database_add_message) ++ * ++ * After reindexing, the user should discard the message object passed ++ * in here by calling notmuch_message_destroy, since it refers to the ++ * original message, not to the reindexed message. ++ */ ++notmuch_status_t ++notmuch_message_reindex (notmuch_message_t *message, ++ notmuch_indexopts_t *indexopts); ++ ++/** + * Message flags. + */ + typedef enum _notmuch_message_flag { +-- +2.7.0.rc3 + -- 2.26.2