summaryrefslogtreecommitdiffstats
path: root/kbx/keybox-search.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2017-04-01 11:10:47 +0200
committerWerner Koch <wk@gnupg.org>2017-04-01 11:10:47 +0200
commit0039d7107bcdfce6f3b02b46ff0495cfba07882a (patch)
treec68d3fb046ecc0a03e37a18b2ed54113bcb2b613 /kbx/keybox-search.c
parentgpg: Avoid multiple open calls to the keybox file. (diff)
downloadgnupg2-0039d7107bcdfce6f3b02b46ff0495cfba07882a.tar.xz
gnupg2-0039d7107bcdfce6f3b02b46ff0495cfba07882a.zip
kbx: Unify blob reading functions.
* kbx/keybox-file.c (_keybox_read_blob): Remove. (_keybox_read_blob2): Rename to .... (_keybox_read_blob): this. Make arg options. Change all callers. * kbx/keybox-search.c (keybox_search): Factor fopen call out to ... (open_file): new. (keybox_seek): Als use open_file. Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'kbx/keybox-search.c')
-rw-r--r--kbx/keybox-search.c45
1 files changed, 30 insertions, 15 deletions
diff --git a/kbx/keybox-search.c b/kbx/keybox-search.c
index 0bd4e0182..a5fc7fa9d 100644
--- a/kbx/keybox-search.c
+++ b/kbx/keybox-search.c
@@ -725,6 +725,23 @@ release_sn_array (struct sn_array_s *array, size_t size)
xfree (array);
}
+
+/* Helper to open the file. */
+static gpg_error_t
+open_file (KEYBOX_HANDLE hd)
+{
+
+ hd->fp = fopen (hd->kb->fname, "rb");
+ if (!hd->fp)
+ {
+ hd->error = gpg_error_from_syserror ();
+ return hd->error;
+ }
+
+ return 0;
+}
+
+
/*
@@ -822,12 +839,11 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc,
if (!hd->fp)
{
- hd->fp = fopen (hd->kb->fname, "rb");
- if (!hd->fp)
+ rc = open_file (hd);
+ if (rc)
{
- hd->error = gpg_error_from_syserror ();
xfree (sn_array);
- return hd->error;
+ return rc;
}
}
@@ -899,7 +915,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc,
int blobtype;
_keybox_release_blob (blob); blob = NULL;
- rc = _keybox_read_blob (&blob, hd->fp);
+ rc = _keybox_read_blob (&blob, hd->fp, NULL);
if (gpg_err_code (rc) == GPG_ERR_TOO_LARGE
&& gpg_err_source (rc) == GPG_ERR_SOURCE_KEYBOX)
{
@@ -1192,24 +1208,23 @@ keybox_offset (KEYBOX_HANDLE hd)
gpg_error_t
keybox_seek (KEYBOX_HANDLE hd, off_t offset)
{
- int err;
+ gpg_error_t 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)
+ if (!offset)
{
- hd->error = gpg_error_from_syserror ();
- return hd->error;
+ /* No need to open the file. An unopened file is effectively at
+ offset 0. */
+ return 0;
}
+
+ err = open_file (hd);
+ if (err)
+ return err;
}
err = fseeko (hd->fp, offset, SEEK_SET);