[PATCH 6/6] cli: lazily create the crypto gpg context only when needed
authorJameson Graef Rollins <jrollins@finestructure.net>
Wed, 16 May 2012 21:55:59 +0000 (14:55 +1700)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:10 +0000 (09:47 -0800)
a5/50d75a0d49e662546f4ebe245d51eff86ac48b [new file with mode: 0644]

diff --git a/a5/50d75a0d49e662546f4ebe245d51eff86ac48b b/a5/50d75a0d49e662546f4ebe245d51eff86ac48b
new file mode 100644 (file)
index 0000000..e041659
--- /dev/null
@@ -0,0 +1,172 @@
+Return-Path: <jrollins@finestructure.net>\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 2EF00431FD2\r
+       for <notmuch@notmuchmail.org>; Wed, 16 May 2012 14:56:35 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+       tests=[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 bD5+-yg4zxJg for <notmuch@notmuchmail.org>;\r
+       Wed, 16 May 2012 14:56:33 -0700 (PDT)\r
+Received: from outgoing-mail.its.caltech.edu (outgoing-mail.its.caltech.edu\r
+       [131.215.239.19])\r
+       by olra.theworths.org (Postfix) with ESMTP id B4E7B431FBD\r
+       for <notmuch@notmuchmail.org>; Wed, 16 May 2012 14:56:15 -0700 (PDT)\r
+Received: from fire-doxen.imss.caltech.edu (localhost [127.0.0.1])\r
+       by fire-doxen-postvirus (Postfix) with ESMTP id 7EFDB328056\r
+       for <notmuch@notmuchmail.org>; Wed, 16 May 2012 14:56:15 -0700 (PDT)\r
+X-Spam-Scanned: at Caltech-IMSS on fire-doxen by amavisd-new\r
+Received: from finestructure.net (rrcs-184-74-199-219.nyc.biz.rr.com\r
+       [184.74.199.219]) (Authenticated sender: jrollins)\r
+       by fire-doxen-submit (Postfix) with ESMTP id C553F2E50AE0\r
+       for <notmuch@notmuchmail.org>; Wed, 16 May 2012 14:56:12 -0700 (PDT)\r
+Received: by finestructure.net (Postfix, from userid 1000)\r
+       id 23A8461B; Wed, 16 May 2012 14:56:02 -0700 (PDT)\r
+From: Jameson Graef Rollins <jrollins@finestructure.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH 6/6] cli: lazily create the crypto gpg context only when\r
+ needed\r
+Date: Wed, 16 May 2012 14:55:59 -0700\r
+Message-Id: <1337205359-2444-7-git-send-email-jrollins@finestructure.net>\r
+X-Mailer: git-send-email 1.7.10\r
+In-Reply-To: <1337205359-2444-6-git-send-email-jrollins@finestructure.net>\r
+References: <1337205359-2444-1-git-send-email-jrollins@finestructure.net>\r
+       <1337205359-2444-2-git-send-email-jrollins@finestructure.net>\r
+       <1337205359-2444-3-git-send-email-jrollins@finestructure.net>\r
+       <1337205359-2444-4-git-send-email-jrollins@finestructure.net>\r
+       <1337205359-2444-5-git-send-email-jrollins@finestructure.net>\r
+       <1337205359-2444-6-git-send-email-jrollins@finestructure.net>\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, 16 May 2012 21:56:35 -0000\r
+\r
+Move the creation of the crypto ctx into mime-node.c and create it\r
+only when needed.  This removes code duplication from notmuch-show and\r
+notmuch-reply, and should speed up these functions considerably if the\r
+crypto flags are provided but the messages don't have any\r
+cryptographic parts.\r
+---\r
+ mime-node.c     |   25 +++++++++++++++++++++++++\r
+ notmuch-reply.c |   19 -------------------\r
+ notmuch-show.c  |   23 -----------------------\r
+ 3 files changed, 25 insertions(+), 42 deletions(-)\r
+\r
+diff --git a/mime-node.c b/mime-node.c\r
+index 8cdabc8..7278c74 100644\r
+--- a/mime-node.c\r
++++ b/mime-node.c\r
+@@ -182,6 +182,31 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
+       return NULL;\r
+     }\r
+ \r
++    /* Lazily create the gpgctx if it's needed and hasn't been initialized yet */\r
++    if ((GMIME_IS_MULTIPART_ENCRYPTED (part) || GMIME_IS_MULTIPART_SIGNED (part))\r
++      && (node->ctx->crypto.verify || node->ctx->crypto.decrypt)) {\r
++      if (!node->ctx->crypto.gpgctx) {\r
++#ifdef GMIME_ATLEAST_26\r
++          /* TODO: GMimePasswordRequestFunc */\r
++          node->ctx->crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");\r
++#else\r
++          GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
++          node->ctx->crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");\r
++          g_object_unref (session);\r
++#endif\r
++          if (node->ctx->crypto.gpgctx) {\r
++              g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) node->ctx->crypto.gpgctx, FALSE);\r
++          } else {\r
++              /* If we fail to create the gpgctx set the verify and\r
++               * decrypt flags to FALSE so we don't try to do any\r
++               * further verification or decryption */\r
++              node->ctx->crypto.verify = FALSE;\r
++              node->ctx->crypto.decrypt = FALSE;\r
++              fprintf (stderr, "Failed to construct gpg context.\n");\r
++          }\r
++      }\r
++    }\r
++\r
+     /* Handle PGP/MIME parts */\r
+     if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto.decrypt) {\r
+       if (node->nchildren != 2) {\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index 997fdd1..a56cf9f 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -708,25 +708,6 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
+     else\r
+       reply_format_func = notmuch_reply_format_default;\r
+ \r
+-    if (crypto.decrypt) {\r
+-#ifdef GMIME_ATLEAST_26\r
+-      /* TODO: GMimePasswordRequestFunc */\r
+-      crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");\r
+-#else\r
+-      GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
+-      crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");\r
+-#endif\r
+-      if (crypto.gpgctx) {\r
+-          g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) crypto.gpgctx, FALSE);\r
+-      } else {\r
+-          crypto.decrypt = FALSE;\r
+-          fprintf (stderr, "Failed to construct gpg context.\n");\r
+-      }\r
+-#ifndef GMIME_ATLEAST_26\r
+-      g_object_unref (session);\r
+-#endif\r
+-    }\r
+-\r
+     config = notmuch_config_open (ctx, NULL, NULL);\r
+     if (config == NULL)\r
+       return 1;\r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index 99a10bd..0616f68 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -1058,29 +1058,6 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+       break;\r
+     }\r
+ \r
+-    if (params.crypto.decrypt || params.crypto.verify) {\r
+-#ifdef GMIME_ATLEAST_26\r
+-      /* TODO: GMimePasswordRequestFunc */\r
+-      params.crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");\r
+-#else\r
+-      GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
+-      params.crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");\r
+-#endif\r
+-      if (params.crypto.gpgctx) {\r
+-          g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.crypto.gpgctx, FALSE);\r
+-      } else {\r
+-          /* If we fail to create the gpgctx set the verify and\r
+-           * decrypt flags to FALSE so we don't try to do any\r
+-           * further verification or decryption */\r
+-          params.crypto.verify = FALSE;\r
+-          params.crypto.decrypt = FALSE;\r
+-          fprintf (stderr, "Failed to construct gpg context.\n");\r
+-      }\r
+-#ifndef GMIME_ATLEAST_26\r
+-      g_object_unref (session);\r
+-#endif\r
+-    }\r
+-\r
+     config = notmuch_config_open (ctx, NULL, NULL);\r
+     if (config == NULL)\r
+       return 1;\r
+-- \r
+1.7.10\r
+\r