diff --git a/g10/keydb.c b/g10/keydb.c index 860187fde..b30819963 100644 --- a/g10/keydb.c +++ b/g10/keydb.c @@ -1717,6 +1717,12 @@ keydb_search (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc, /* (DESCINDEX is already set). */ if (DBG_CLOCK) log_clock ("keydb_search leave (cached)"); + + hd->current = hd->keyblock_cache.resource; + /* HD->KEYBLOCK_CACHE.OFFSET is the last byte in the record. + Seek just beyond that. */ + keybox_seek (hd->active[hd->current].u.kb, + hd->keyblock_cache.offset + 1); return 0; } diff --git a/kbx/keybox-search.c b/kbx/keybox-search.c index df959b67d..1edb4ae38 100644 --- a/kbx/keybox-search.c +++ b/kbx/keybox-search.c @@ -1196,3 +1196,32 @@ keybox_offset (KEYBOX_HANDLE hd) return 0; return ftello (hd->fp); } + +gpg_error_t +keybox_seek (KEYBOX_HANDLE hd, off_t offset) +{ + int err; + + if (hd->error) + return hd->error; /* still in error state */ + + if (! hd->fp) + { + if (offset == 0) + /* No need to open the file. An unopened file is effectively at + offset 0. */ + return 0; + + hd->fp = fopen (hd->kb->fname, "rb"); + if (!hd->fp) + { + hd->error = gpg_error_from_syserror (); + return hd->error; + } + } + + err = fseeko (hd->fp, offset, SEEK_SET); + hd->error = gpg_error_from_errno (err); + + return hd->error; +} diff --git a/kbx/keybox.h b/kbx/keybox.h index c91a28299..8b75db45c 100644 --- a/kbx/keybox.h +++ b/kbx/keybox.h @@ -77,8 +77,6 @@ int keybox_set_ephemeral (KEYBOX_HANDLE hd, int yes); int keybox_lock (KEYBOX_HANDLE hd, int yes); -off_t keybox_offset (KEYBOX_HANDLE hd); - /*-- keybox-file.c --*/ /* Fixme: This function does not belong here: Provide a better interface to create a new keybox file. */ @@ -97,6 +95,8 @@ int keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc, keybox_blobtype_t want_blobtype, size_t *r_descindex, unsigned long *r_skipped); +off_t keybox_offset (KEYBOX_HANDLE hd); +gpg_error_t keybox_seek (KEYBOX_HANDLE hd, off_t offset); /*-- keybox-update.c --*/ gpg_error_t keybox_insert_keyblock (KEYBOX_HANDLE hd,