[PATCH 3/3] crypto: Avoid explicit handling of GMimeCryptoContext in gmime 3.0

Daniel Kahn Gillmor dkg at fifthhorseman.net
Fri Jul 14 06:12:02 PDT 2017


gmime 3.0 knows how to select the correct GMimeCryptoContext
automatically, so a bunch of the code in notmuch can be dropped in
that case.

The #ifdef removal of the crypto stuff is better than #define aliasing
in gmime-extra.h for this stuff.  When built against gmime 3.0:

 * it reduces compiled code,
 * it avoids initializing unused gpgme contexts, and
 * it avoids compiled-time warnings about passing unnecessary
   notmuch_cryptoctx_t*s.
---
 crypto.c         |  2 ++
 mime-node.c      | 28 ++++++++++++++++++++++++----
 notmuch-client.h |  8 ++++++--
 notmuch-reply.c  |  2 ++
 notmuch-show.c   |  2 ++
 5 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/crypto.c b/crypto.c
index 3e8ce7ca..ab1fb77a 100644
--- a/crypto.c
+++ b/crypto.c
@@ -20,6 +20,7 @@
 
 #include "notmuch-client.h"
 
+#if (GMIME_MAJOR_VERSION < 3)
 /* Create a GPG context (GMime 2.6) */
 static notmuch_crypto_context_t *
 create_gpg_context (notmuch_crypto_t *crypto)
@@ -132,3 +133,4 @@ notmuch_crypto_cleanup (notmuch_crypto_t *crypto)
 
     return 0;
 }
+#endif
diff --git a/mime-node.c b/mime-node.c
index f719422e..226e9c2f 100644
--- a/mime-node.c
+++ b/mime-node.c
@@ -150,8 +150,11 @@ set_signature_list_destructor (mime_node_t *node)
 
 /* Verify a signed mime node (GMime 2.6) */
 static void
-node_verify (mime_node_t *node, GMimeObject *part,
-	     notmuch_crypto_context_t *cryptoctx)
+node_verify (mime_node_t *node, GMimeObject *part
+#if (GMIME_MAJOR_VERSION < 3)
+	     , notmuch_crypto_context_t *cryptoctx
+#endif
+    )
 {
     GError *err = NULL;
 
@@ -171,8 +174,11 @@ node_verify (mime_node_t *node, GMimeObject *part,
 
 /* Decrypt and optionally verify an encrypted mime node (GMime 2.6) */
 static void
-node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
-			 notmuch_crypto_context_t *cryptoctx)
+node_decrypt_and_verify (mime_node_t *node, GMimeObject *part
+#if (GMIME_MAJOR_VERSION < 3)
+	     , notmuch_crypto_context_t *cryptoctx
+#endif
+    )
 {
     GError *err = NULL;
     GMimeDecryptResult *decrypt_result = NULL;
@@ -207,7 +213,11 @@ static mime_node_t *
 _mime_node_create (mime_node_t *parent, GMimeObject *part)
 {
     mime_node_t *node = talloc_zero (parent, mime_node_t);
+#if (GMIME_MAJOR_VERSION < 3)
     notmuch_crypto_context_t *cryptoctx = NULL;
+#else
+    notmuch_bool_t cryptoctx = TRUE;
+#endif
 
     /* Set basic node properties */
     node->part = part;
@@ -240,12 +250,14 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
 	return NULL;
     }
 
+#if (GMIME_MAJOR_VERSION < 3)
     if ((GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt)
 	|| (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {
 	GMimeContentType *content_type = g_mime_object_get_content_type (part);
 	const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");
 	cryptoctx = notmuch_crypto_get_context (node->ctx->crypto, protocol);
     }
+#endif
 
     /* Handle PGP/MIME parts */
     if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt && cryptoctx) {
@@ -255,7 +267,11 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
 		     "message (must be exactly 2)\n",
 		     node->nchildren);
 	} else {
+#if (GMIME_MAJOR_VERSION < 3)
 	    node_decrypt_and_verify (node, part, cryptoctx);
+#else
+	    node_decrypt_and_verify (node, part);
+#endif
 	}
     } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify && cryptoctx) {
 	if (node->nchildren != 2) {
@@ -264,7 +280,11 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
 		     "(must be exactly 2)\n",
 		     node->nchildren);
 	} else {
+#if (GMIME_MAJOR_VERSION < 3)
 	    node_verify (node, part, cryptoctx);
+#else
+	    node_verify (node, part);
+#endif
 	}
     }
 
diff --git a/notmuch-client.h b/notmuch-client.h
index 11aefbb4..c520089e 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -31,7 +31,9 @@
 
 #include "gmime-extra.h"
 
+#if (GMIME_MAJOR_VERSION < 3)
 typedef GMimeCryptoContext notmuch_crypto_context_t;
+#endif
 /* This is automatically included only since gmime 2.6.10 */
 #include <gmime/gmime-pkcs7-context.h>
 
@@ -72,11 +74,11 @@ typedef struct notmuch_show_format {
 } notmuch_show_format_t;
 
 typedef struct notmuch_crypto {
-    notmuch_crypto_context_t* gpgctx;
-    notmuch_crypto_context_t* pkcs7ctx;
     notmuch_bool_t verify;
     notmuch_bool_t decrypt;
 #if (GMIME_MAJOR_VERSION < 3)
+    notmuch_crypto_context_t* gpgctx;
+    notmuch_crypto_context_t* pkcs7ctx;
     const char *gpgpath;
 #endif
 } notmuch_crypto_t;
@@ -180,11 +182,13 @@ typedef struct _notmuch_config notmuch_config_t;
 void
 notmuch_exit_if_unsupported_format (void);
 
+#if (GMIME_MAJOR_VERSION < 3)
 notmuch_crypto_context_t *
 notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol);
 
 int
 notmuch_crypto_cleanup (notmuch_crypto_t *crypto);
+#endif
 
 int
 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]);
diff --git a/notmuch-reply.c b/notmuch-reply.c
index b4a55362..69c26359 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -759,7 +759,9 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
     if (do_reply (config, query, &params, format, reply_all) != 0)
 	return EXIT_FAILURE;
 
+#if (GMIME_MAJOR_VERSION < 3)
     notmuch_crypto_cleanup (&params.crypto);
+#endif
     notmuch_query_destroy (query);
     notmuch_database_destroy (notmuch);
 
diff --git a/notmuch-show.c b/notmuch-show.c
index 6c135278..626bca80 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -1257,7 +1257,9 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
     g_mime_stream_flush (params.out_stream);
     g_object_unref (params.out_stream);
 
+#if (GMIME_MAJOR_VERSION < 3)
     notmuch_crypto_cleanup (&params.crypto);
+#endif
     notmuch_query_destroy (query);
     notmuch_database_destroy (notmuch);
 
-- 
2.13.2



More information about the notmuch mailing list