From: Jameson Graef Rollins Date: Fri, 18 May 2012 17:32:37 +0000 (+1700) Subject: [PATCH v2 5/5] cli: lazily create the crypto gpg context only when needed X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=ddaf7608e81f8b2c3f159629d47fb1e2c13396ed;p=notmuch-archives.git [PATCH v2 5/5] cli: lazily create the crypto gpg context only when needed --- diff --git a/80/bb01790d8a5fcc59ddf6476a4cc4e831e32486 b/80/bb01790d8a5fcc59ddf6476a4cc4e831e32486 new file mode 100644 index 000000000..96ad0e02e --- /dev/null +++ b/80/bb01790d8a5fcc59ddf6476a4cc4e831e32486 @@ -0,0 +1,186 @@ +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 4677D431FBC + for ; Fri, 18 May 2012 10:32:52 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -2.3 +X-Spam-Level: +X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_MED=-2.3] 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 crdDL4yPwtqo for ; + Fri, 18 May 2012 10:32:50 -0700 (PDT) +Received: from outgoing-mail.its.caltech.edu (outgoing-mail.its.caltech.edu + [131.215.239.19]) + by olra.theworths.org (Postfix) with ESMTP id BAB54431FC2 + for ; Fri, 18 May 2012 10:32:46 -0700 (PDT) +Received: from earth-doxen.imss.caltech.edu (localhost [127.0.0.1]) + by earth-doxen-postvirus (Postfix) with ESMTP id 7194F66E00A3 + for ; Fri, 18 May 2012 10:32:46 -0700 (PDT) +X-Spam-Scanned: at Caltech-IMSS on earth-doxen by amavisd-new +Received: from finestructure.net (cpe-98-14-81-93.nyc.res.rr.com + [98.14.81.93]) (Authenticated sender: jrollins) by earth-doxen-submit + (Postfix) with ESMTP id 4571D66E01B3 for ; Fri, 18 + May 2012 10:32:44 -0700 (PDT) +Received: by finestructure.net (Postfix, from userid 1000) + id 4BBE3628; Fri, 18 May 2012 10:32:40 -0700 (PDT) +From: Jameson Graef Rollins +To: Notmuch Mail +Subject: [PATCH v2 5/5] cli: lazily create the crypto gpg context only when + needed +Date: Fri, 18 May 2012 10:32:37 -0700 +Message-Id: <1337362357-31281-6-git-send-email-jrollins@finestructure.net> +X-Mailer: git-send-email 1.7.10 +In-Reply-To: <1337362357-31281-5-git-send-email-jrollins@finestructure.net> +References: <1337362357-31281-1-git-send-email-jrollins@finestructure.net> + <1337362357-31281-2-git-send-email-jrollins@finestructure.net> + <1337362357-31281-3-git-send-email-jrollins@finestructure.net> + <1337362357-31281-4-git-send-email-jrollins@finestructure.net> + <1337362357-31281-5-git-send-email-jrollins@finestructure.net> +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: Fri, 18 May 2012 17:32:52 -0000 + +Move the creation of the crypto ctx into mime-node.c and create it +only when needed. This removes code duplication from notmuch-show and +notmuch-reply, and should speed up these functions considerably if the +crypto flags are provided but the messages don't have any +cryptographic parts. +--- + mime-node.c | 25 +++++++++++++++++++++++++ + notmuch-client.h | 3 ++- + notmuch-reply.c | 19 ------------------- + notmuch-show.c | 23 ----------------------- + 4 files changed, 27 insertions(+), 43 deletions(-) + +diff --git a/mime-node.c b/mime-node.c +index 3adbe5a..592e0b6 100644 +--- a/mime-node.c ++++ b/mime-node.c +@@ -182,6 +182,31 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part) + return NULL; + } + ++ /* Lazily create the gpgctx if it's needed and hasn't been initialized yet */ ++ if ((GMIME_IS_MULTIPART_ENCRYPTED (part) || GMIME_IS_MULTIPART_SIGNED (part)) ++ && (node->ctx->crypto->verify || node->ctx->crypto->decrypt)) { ++ if (!node->ctx->crypto->gpgctx) { ++#ifdef GMIME_ATLEAST_26 ++ /* TODO: GMimePasswordRequestFunc */ ++ node->ctx->crypto->gpgctx = g_mime_gpg_context_new (NULL, "gpg"); ++#else ++ GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL); ++ node->ctx->crypto->gpgctx = g_mime_gpg_context_new (session, "gpg"); ++ g_object_unref (session); ++#endif ++ if (node->ctx->crypto->gpgctx) { ++ g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) node->ctx->crypto->gpgctx, FALSE); ++ } else { ++ /* If we fail to create the gpgctx set the verify and ++ * decrypt flags to FALSE so we don't try to do any ++ * further verification or decryption */ ++ node->ctx->crypto->verify = FALSE; ++ node->ctx->crypto->decrypt = FALSE; ++ fprintf (stderr, "Failed to construct gpg context.\n"); ++ } ++ } ++ } ++ + /* Handle PGP/MIME parts */ + if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt) { + if (node->nchildren != 2) { +diff --git a/notmuch-client.h b/notmuch-client.h +index c671c13..c79eaa9 100644 +--- a/notmuch-client.h ++++ b/notmuch-client.h +@@ -348,7 +348,8 @@ struct mime_node { + /* Construct a new MIME node pointing to the root message part of + * message. If crypto->verify is true, signed child parts will be + * verified. If crypto->decrypt is true, encrypted child parts will be +- * decrypted. ++ * decrypted. The GPG context crypto->gpgctx does not need to be ++ * pre-initialized as it will be initialized lazily as needed. + * + * Return value: + * +diff --git a/notmuch-reply.c b/notmuch-reply.c +index 345be76..1a61aa7 100644 +--- a/notmuch-reply.c ++++ b/notmuch-reply.c +@@ -707,25 +707,6 @@ notmuch_reply_command (void *ctx, int argc, char *argv[]) + else + reply_format_func = notmuch_reply_format_default; + +- if (crypto.decrypt) { +-#ifdef GMIME_ATLEAST_26 +- /* TODO: GMimePasswordRequestFunc */ +- crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg"); +-#else +- GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL); +- crypto.gpgctx = g_mime_gpg_context_new (session, "gpg"); +-#endif +- if (crypto.gpgctx) { +- g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) crypto.gpgctx, FALSE); +- } else { +- crypto.decrypt = FALSE; +- fprintf (stderr, "Failed to construct gpg context.\n"); +- } +-#ifndef GMIME_ATLEAST_26 +- g_object_unref (session); +-#endif +- } +- + config = notmuch_config_open (ctx, NULL, NULL); + if (config == NULL) + return 1; +diff --git a/notmuch-show.c b/notmuch-show.c +index f4ee038..5f785d0 100644 +--- a/notmuch-show.c ++++ b/notmuch-show.c +@@ -1056,29 +1056,6 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[])) + break; + } + +- if (params.crypto.decrypt || params.crypto.verify) { +-#ifdef GMIME_ATLEAST_26 +- /* TODO: GMimePasswordRequestFunc */ +- params.crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg"); +-#else +- GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL); +- params.crypto.gpgctx = g_mime_gpg_context_new (session, "gpg"); +-#endif +- if (params.crypto.gpgctx) { +- g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.crypto.gpgctx, FALSE); +- } else { +- /* If we fail to create the gpgctx set the verify and +- * decrypt flags to FALSE so we don't try to do any +- * further verification or decryption */ +- params.crypto.verify = FALSE; +- params.crypto.decrypt = FALSE; +- fprintf (stderr, "Failed to construct gpg context.\n"); +- } +-#ifndef GMIME_ATLEAST_26 +- g_object_unref (session); +-#endif +- } +- + config = notmuch_config_open (ctx, NULL, NULL); + if (config == NULL) + return 1; +-- +1.7.10 +