[Pkg-gnupg-commit] [gnupg2] 103/124: gpg: Avoid multiple open calls to the keybox file.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Wed Apr 5 15:55:37 UTC 2017


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch experimental
in repository gnupg2.

commit 5556eca5acd46983bff0b38a1ffbc2f07fbaba9f
Author: Werner Koch <wk at gnupg.org>
Date:   Fri Mar 31 20:44:05 2017 +0200

    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 <wk at gnupg.org>
---
 g10/getkey.c        | 23 ++++++++++++++++++-----
 g10/gpg.c           |  2 ++
 g10/gpg.h           |  7 ++++++-
 g10/keydb.h         |  3 ---
 kbx/keybox-search.c |  9 +++++++--
 5 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/g10/getkey.c b/g10/getkey.c
index d8a1058..dab63fa 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);
diff --git a/g10/gpg.c b/g10/gpg.c
index 24636aa..e228427 100644
--- a/g10/gpg.c
+++ b/g10/gpg.c
@@ -2195,6 +2195,8 @@ gpg_deinit_default_ctrl (ctrl_t ctrl)
   tofu_closedbs (ctrl);
 #endif
   gpg_dirmngr_deinit_session_data (ctrl);
+
+  keydb_release (ctrl->cached_getkey_kdb);
 }
 
 
diff --git a/g10/gpg.h b/g10/gpg.h
index c663585..9b8b77c 100644
--- a/g10/gpg.h
+++ b/g10/gpg.h
@@ -59,10 +59,13 @@ struct server_local_s;
 struct dirmngr_local_s;
 typedef struct dirmngr_local_s *dirmngr_local_t;
 
-/* Object used to describe a keyblok node.  */
+/* Object used to describe a keyblock node.  */
 typedef struct kbnode_struct *KBNODE;   /* Deprecated use kbnode_t. */
 typedef struct kbnode_struct *kbnode_t;
 
+/* The handle for keydb operations.  */
+typedef struct keydb_handle *KEYDB_HANDLE;
+
 /* TOFU database meta object.  */
 struct tofu_dbs_s;
 typedef struct tofu_dbs_s *tofu_dbs_t;
@@ -94,6 +97,8 @@ struct server_control_s
     int batch_updated_wanted;
   } tofu;
 
+  /* This is used to cache a key data base handle.  */
+  KEYDB_HANDLE cached_getkey_kdb;
 };
 
 
diff --git a/g10/keydb.h b/g10/keydb.h
index 271e68f..7f42738 100644
--- a/g10/keydb.h
+++ b/g10/keydb.h
@@ -109,9 +109,6 @@ struct pubkey_find_info {
 };
 
 
-typedef struct keydb_handle *KEYDB_HANDLE;
-
-
 /* Helper type for preference fucntions. */
 union pref_hint
 {
diff --git a/kbx/keybox-search.c b/kbx/keybox-search.c
index 56515d1..0bd4e01 100644
--- a/kbx/keybox-search.c
+++ b/kbx/keybox-search.c
@@ -746,8 +746,13 @@ keybox_search_reset (KEYBOX_HANDLE hd)
 
   if (hd->fp)
     {
-      fclose (hd->fp);
-      hd->fp = NULL;
+      if (fseeko (hd->fp, 0, SEEK_SET))
+        {
+          /* Ooops.  Seek did not work.  Close so that the search will
+           * open the file again.  */
+          fclose (hd->fp);
+          hd->fp = NULL;
+        }
     }
   hd->error = 0;
   hd->eof = 0;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gnupg2.git



More information about the Pkg-gnupg-commit mailing list