unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Austin Clements <amdragon@MIT.EDU>
To: Jameson Graef Rollins <jrollins@finestructure.net>
Cc: Notmuch Mail <notmuch@notmuchmail.org>
Subject: Re: [PATCH v2 5/5] cli: lazily create the crypto gpg context only when needed
Date: Fri, 18 May 2012 15:21:57 -0400	[thread overview]
Message-ID: <20120518192157.GV11804@mit.edu> (raw)
In-Reply-To: <1337362357-31281-6-git-send-email-jrollins@finestructure.net>

Quoth Jameson Graef Rollins on May 18 at 10:32 am:
> 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) {

These if conditions could be combined, like

    if ((GMIME_IS_MULTIPART_ENCRYPTED (part) || GMIME_IS_MULTIPART_SIGNED (part))
	&& (node->ctx->crypto->verify || node->ctx->crypto->decrypt)
	&& !node->ctx->crypto->gpgctx) {

When I see two nested 'if's like this, I expect there to be an else
part to the inner if or something after the inner if (why else would
it be separate?) and then I wind up matching braces when I don't see
anything.  Also, one 'if' would save a level of indentation.

> +#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.

Perhaps "If crypto->gpgctx is NULL, it will be lazily initialized."?
The variable does have to be "initialized", in the sense that it can't
be uninitialized data.

It's slightly awkward that it's the caller's responsibility to free
this lazily constructed object.  That should probably be documented.
We could more carefully reference count it, but I think that would
actually be worse because the reference count would probably bounce
through zero frequently.

>   *
>   * 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;

  reply	other threads:[~2012-05-18 19:22 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-18 17:32 [PATCH v2 0/5] cli: improve handling of crypto parameters and contexts Jameson Graef Rollins
2012-05-18 17:32 ` [PATCH v2 1/5] cli: new crypto structure to store crypto contexts and parameters Jameson Graef Rollins
2012-05-18 17:32   ` [PATCH v2 2/5] cli: modify mime_node_open to take crypto struct as argument Jameson Graef Rollins
2012-05-18 17:32     ` [PATCH v2 3/5] cli: modify mime_node_context to use the new notmuch_crypto_t Jameson Graef Rollins
2012-05-18 17:32       ` [PATCH v2 4/5] cli: new crypto verify flag to handle verification Jameson Graef Rollins
2012-05-18 17:32         ` [PATCH v2 5/5] cli: lazily create the crypto gpg context only when needed Jameson Graef Rollins
2012-05-18 19:21           ` Austin Clements [this message]
2012-05-18 19:45             ` Jameson Graef Rollins
2012-05-18 20:37               ` Daniel Kahn Gillmor
2012-05-18 20:42                 ` Jameson Graef Rollins
2012-05-19 11:26         ` [PATCH v2 4/5] cli: new crypto verify flag to handle verification Jani Nikula
2012-05-19 16:28           ` Jameson Graef Rollins
2012-05-18 19:09       ` [PATCH v2 3/5] cli: modify mime_node_context to use the new notmuch_crypto_t Austin Clements
2012-05-18 19:17         ` Jameson Graef Rollins

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120518192157.GV11804@mit.edu \
    --to=amdragon@mit.edu \
    --cc=jrollins@finestructure.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).