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>
This commit is contained in:
Werner Koch 2017-04-01 11:10:47 +02:00
parent 5556eca5ac
commit 0039d7107b
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
5 changed files with 54 additions and 37 deletions

View File

@ -177,8 +177,7 @@ void _keybox_destroy_openpgp_info (keybox_openpgp_info_t info);
/*-- keybox-file.c --*/
int _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp);
int _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted);
int _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted);
int _keybox_write_blob (KEYBOXBLOB blob, FILE *fp);
/*-- keybox-search.c --*/

View File

@ -581,7 +581,7 @@ _keybox_dump_file (const char *filename, int stats_only, FILE *outfp)
for (;;)
{
rc = _keybox_read_blob (&blob, fp);
rc = _keybox_read_blob (&blob, fp, NULL);
if (gpg_err_code (rc) == GPG_ERR_TOO_LARGE
&& gpg_err_source (rc) == GPG_ERR_SOURCE_KEYBOX)
{
@ -704,7 +704,7 @@ _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp)
}
dupitems_count = 0;
while ( !(rc = _keybox_read_blob (&blob, fp)) )
while ( !(rc = _keybox_read_blob (&blob, fp, NULL)) )
{
unsigned char digest[20];
@ -778,7 +778,7 @@ _keybox_dump_cut_records (const char *filename, unsigned long from,
if (!(fp = open_file (&filename, stderr)))
return gpg_error_from_syserror ();
while ( !(rc = _keybox_read_blob (&blob, fp)) )
while ( !(rc = _keybox_read_blob (&blob, fp, NULL)) )
{
if (recno > to)
break; /* Ready. */

View File

@ -45,10 +45,10 @@ ftello (FILE *stream)
/* Read a block at the current position and return it in r_blob.
r_blob may be NULL to simply skip the current block. */
/* Read a block at the current position and return it in R_BLOB.
R_BLOB may be NULL to simply skip the current block. */
int
_keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
_keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
{
unsigned char *image;
size_t imagelen = 0;
@ -56,7 +56,8 @@ _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
int rc;
off_t off;
*skipped_deleted = 0;
if (skipped_deleted)
*skipped_deleted = 0;
again:
if (r_blob)
*r_blob = NULL;
@ -86,7 +87,8 @@ _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
/* Special treatment for empty blobs. */
if (fseek (fp, imagelen-5, SEEK_CUR))
return gpg_error_from_syserror ();
*skipped_deleted = 1;
if (skipped_deleted)
*skipped_deleted = 1;
goto again;
}
@ -99,6 +101,14 @@ _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
return gpg_error (GPG_ERR_TOO_LARGE);
}
if (!r_blob)
{
/* This blob shall be skipped. */
if (fseek (fp, imagelen-5, SEEK_CUR))
return gpg_error_from_syserror ();
return 0;
}
image = xtrymalloc (imagelen);
if (!image)
return gpg_error_from_syserror ();
@ -111,19 +121,12 @@ _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
return tmperr;
}
rc = r_blob? _keybox_new_blob (r_blob, image, imagelen, off) : 0;
if (rc || !r_blob)
rc = _keybox_new_blob (r_blob, image, imagelen, off);
if (rc)
xfree (image);
return rc;
}
int
_keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp)
{
int dummy;
return _keybox_read_blob2 (r_blob, fp, &dummy);
}
/* Write the block to the current file position */
int

View File

@ -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);

View File

@ -288,7 +288,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
}
/* Skip this blob. */
rc = _keybox_read_blob (NULL, fp);
rc = _keybox_read_blob (NULL, fp, NULL);
if (rc)
{
fclose (fp);
@ -665,7 +665,7 @@ keybox_compress (KEYBOX_HANDLE hd)
/* A quick test to see if we need to compress the file at all. We
schedule a compress run after 3 hours. */
if ( !_keybox_read_blob (&blob, fp) )
if ( !_keybox_read_blob (&blob, fp, NULL) )
{
const unsigned char *buffer;
size_t length;
@ -703,7 +703,7 @@ keybox_compress (KEYBOX_HANDLE hd)
cut_time = time(NULL) - 86400;
first_blob = 1;
skipped_deleted = 0;
for (rc=0; !(read_rc = _keybox_read_blob2 (&blob, fp, &skipped_deleted));
for (rc=0; !(read_rc = _keybox_read_blob (&blob, fp, &skipped_deleted));
_keybox_release_blob (blob), blob = NULL )
{
unsigned int blobflags;