From 5556eca5acd46983bff0b38a1ffbc2f07fbaba9f Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 31 Mar 2017 20:44:05 +0200 Subject: gpg: Avoid multiple open calls to the keybox file. * g10/keydb.h (KEYDB_HANDLE): Move typedef to ... * g10/gpg.h: here. (struct server_control_s): Add field 'cached_getkey_kdb'. * g10/gpg.c (gpg_deinit_default_ctrl): Release that keydb handle. * g10/getkey.c (getkey_end): Cache keydb handle. (get_pubkey): Use cached keydb handle. * kbx/keybox-search.c (keybox_search_reset): Use lseek instead of closing the file. -- Before this patch a "gpg --check-sigs" opened and closed the keybox file for almost every signature check. By caching the keydb handle and using lseek(2) this can be limited to just 2 times. This might speed up things on Windows. Signed-off-by: Werner Koch --- g10/getkey.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'g10/getkey.c') diff --git a/g10/getkey.c b/g10/getkey.c index d8a105839..dab63fa5f 100644 --- a/g10/getkey.c +++ b/g10/getkey.c @@ -736,11 +736,21 @@ get_pubkey (ctrl_t ctrl, PKT_public_key * pk, u32 * keyid) memset (&ctx, 0, sizeof ctx); ctx.exact = 1; /* Use the key ID exactly as given. */ ctx.not_allocated = 1; - ctx.kr_handle = keydb_new (); - if (!ctx.kr_handle) + + if (ctrl && ctrl->cached_getkey_kdb) + { + ctx.kr_handle = ctrl->cached_getkey_kdb; + ctrl->cached_getkey_kdb = NULL; + keydb_search_reset (ctx.kr_handle); + } + else { - rc = gpg_error_from_syserror (); - goto leave; + ctx.kr_handle = keydb_new (); + if (!ctx.kr_handle) + { + rc = gpg_error_from_syserror (); + goto leave; + } } ctx.nitems = 1; ctx.items[0].mode = KEYDB_SEARCH_MODE_LONG_KID; @@ -2208,7 +2218,10 @@ getkey_end (ctrl_t ctrl, getkey_ctx_t ctx) { if (ctx) { - keydb_release (ctx->kr_handle); + if (ctrl && !ctrl->cached_getkey_kdb) + ctrl->cached_getkey_kdb = ctx->kr_handle; + else + keydb_release (ctx->kr_handle); free_strlist (ctx->extra_list); if (!ctx->not_allocated) xfree (ctx); -- cgit v1.2.3