1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-21 10:09:57 +01:00

Replace most of the remaining stdio calls by estream calls.

--

We need to use es_fopen on Windows to cope with non-ascii file names.
This is quite a large but fortunately straightforward change.  At a
very few places we keep using stdio (for example due to the use of
popen).

GnuPG-bug-id: 5098
Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2020-10-20 11:52:16 +02:00
parent c94ee1386e
commit 390497ea11
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
23 changed files with 220 additions and 216 deletions

View File

@ -215,7 +215,7 @@ struct ssh_key_type_spec
struct ssh_control_file_s struct ssh_control_file_s
{ {
char *fname; /* Name of the file. */ char *fname; /* Name of the file. */
FILE *fp; /* This is never NULL. */ estream_t fp; /* This is never NULL. */
int lnr; /* The current line number. */ int lnr; /* The current line number. */
struct { struct {
int valid; /* True if the data of this structure is valid. */ int valid; /* True if the data of this structure is valid. */
@ -856,7 +856,7 @@ open_control_file (ssh_control_file_t *r_cf, int append)
} }
/* FIXME: With "a+" we are not able to check whether this will /* FIXME: With "a+" we are not able to check whether this will
be created and thus the blurb needs to be written first. */ be created and thus the blurb needs to be written first. */
cf->fp = fopen (cf->fname, append? "a+":"r"); cf->fp = es_fopen (cf->fname, append? "a+":"r");
if (!cf->fp && errno == ENOENT) if (!cf->fp && errno == ENOENT)
{ {
estream_t stream = es_fopen (cf->fname, "wx,mode=-rw-r"); estream_t stream = es_fopen (cf->fname, "wx,mode=-rw-r");
@ -869,7 +869,7 @@ open_control_file (ssh_control_file_t *r_cf, int append)
} }
es_fputs (sshcontrolblurb, stream); es_fputs (sshcontrolblurb, stream);
es_fclose (stream); es_fclose (stream);
cf->fp = fopen (cf->fname, append? "a+":"r"); cf->fp = es_fopen (cf->fname, append? "a+":"r");
} }
if (!cf->fp) if (!cf->fp)
@ -886,7 +886,7 @@ open_control_file (ssh_control_file_t *r_cf, int append)
if (err && cf) if (err && cf)
{ {
if (cf->fp) if (cf->fp)
fclose (cf->fp); es_fclose (cf->fp);
xfree (cf->fname); xfree (cf->fname);
xfree (cf); xfree (cf);
} }
@ -900,9 +900,9 @@ open_control_file (ssh_control_file_t *r_cf, int append)
static void static void
rewind_control_file (ssh_control_file_t cf) rewind_control_file (ssh_control_file_t cf)
{ {
fseek (cf->fp, 0, SEEK_SET); es_fseek (cf->fp, 0, SEEK_SET);
cf->lnr = 0; cf->lnr = 0;
clearerr (cf->fp); es_clearerr (cf->fp);
} }
@ -911,7 +911,7 @@ close_control_file (ssh_control_file_t cf)
{ {
if (!cf) if (!cf)
return; return;
fclose (cf->fp); es_fclose (cf->fp);
xfree (cf->fname); xfree (cf->fname);
xfree (cf); xfree (cf);
} }
@ -928,13 +928,13 @@ read_control_file_item (ssh_control_file_t cf)
long ttl = 0; long ttl = 0;
cf->item.valid = 0; cf->item.valid = 0;
clearerr (cf->fp); es_clearerr (cf->fp);
do do
{ {
if (!fgets (line, DIM(line)-1, cf->fp) ) if (!es_fgets (line, DIM(line)-1, cf->fp) )
{ {
if (feof (cf->fp)) if (es_feof (cf->fp))
return gpg_error (GPG_ERR_EOF); return gpg_error (GPG_ERR_EOF);
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
} }
@ -943,7 +943,7 @@ read_control_file_item (ssh_control_file_t cf)
if (!*line || line[strlen(line)-1] != '\n') if (!*line || line[strlen(line)-1] != '\n')
{ {
/* Eat until end of line */ /* Eat until end of line */
while ( (c=getc (cf->fp)) != EOF && c != '\n') while ((c = es_getc (cf->fp)) != EOF && c != '\n')
; ;
return gpg_error (*line? GPG_ERR_LINE_TOO_LONG return gpg_error (*line? GPG_ERR_LINE_TOO_LONG
: GPG_ERR_INCOMPLETE_LINE); : GPG_ERR_INCOMPLETE_LINE);
@ -1099,7 +1099,7 @@ add_control_entry (ctrl_t ctrl, ssh_key_type_spec_t *spec,
/* Not yet in the file - add it. Because the file has been /* Not yet in the file - add it. Because the file has been
opened in append mode, we simply need to write to it. */ opened in append mode, we simply need to write to it. */
tp = localtime (&atime); tp = localtime (&atime);
fprintf (cf->fp, es_fprintf (cf->fp,
("# %s key added on: %04d-%02d-%02d %02d:%02d:%02d\n" ("# %s key added on: %04d-%02d-%02d %02d:%02d:%02d\n"
"# Fingerprints: %s\n" "# Fingerprints: %s\n"
"# %s\n" "# %s\n"

View File

@ -41,14 +41,14 @@ static char *
findkey_fname (const char *key, const char *fname) findkey_fname (const char *key, const char *fname)
{ {
gpg_error_t err = 0; gpg_error_t err = 0;
FILE *fp; estream_t fp;
int lnr = 0; int lnr = 0;
int c; int c;
char *p, line[256]; char *p, line[256];
int in_item = 0; int in_item = 0;
membuf_t mb = MEMBUF_ZERO; membuf_t mb = MEMBUF_ZERO;
fp = fopen (fname, "r"); fp = es_fopen (fname, "r");
if (!fp) if (!fp)
{ {
if (errno != ENOENT) if (errno != ENOENT)
@ -59,14 +59,14 @@ findkey_fname (const char *key, const char *fname)
return NULL; return NULL;
} }
while (fgets (line, DIM(line)-1, fp)) while (es_fgets (line, DIM(line)-1, fp))
{ {
lnr++; lnr++;
if (!*line || line[strlen(line)-1] != '\n') if (!*line || line[strlen(line)-1] != '\n')
{ {
/* Eat until end of line. */ /* Eat until end of line. */
while ( (c=getc (fp)) != EOF && c != '\n') while ((c = es_getc (fp)) != EOF && c != '\n')
; ;
err = gpg_error (*line? GPG_ERR_LINE_TOO_LONG err = gpg_error (*line? GPG_ERR_LINE_TOO_LONG
: GPG_ERR_INCOMPLETE_LINE); : GPG_ERR_INCOMPLETE_LINE);
@ -130,14 +130,14 @@ findkey_fname (const char *key, const char *fname)
} }
} }
if ( !err && ferror (fp) ) if ( !err && es_ferror (fp) )
{ {
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
log_error (_("error reading '%s', line %d: %s\n"), log_error (_("error reading '%s', line %d: %s\n"),
fname, lnr, gpg_strerror (err)); fname, lnr, gpg_strerror (err));
} }
fclose (fp); es_fclose (fp);
if (is_membuf_ready (&mb)) if (is_membuf_ready (&mb))
{ {
/* We have collected something. */ /* We have collected something. */

View File

@ -1068,6 +1068,7 @@ gnupg_access (const char *name, int mode)
/* Try to set an envvar. Print only a notice on error. */ /* Try to set an envvar. Print only a notice on error. */
#ifndef HAVE_W32_SYSTEM
static void static void
try_set_envvar (const char *name, const char *value, int silent) try_set_envvar (const char *name, const char *value, int silent)
{ {
@ -1076,6 +1077,7 @@ try_set_envvar (const char *name, const char *value, int silent)
log_info ("error setting envvar %s to '%s': %s\n", name, value, log_info ("error setting envvar %s to '%s': %s\n", name, value,
gpg_strerror (my_error_from_syserror ())); gpg_strerror (my_error_from_syserror ()));
} }
#endif /*!HAVE_W32_SYSTEM*/
/* Switch to USER which is either a name or an UID. This is a nop /* Switch to USER which is either a name or an UID. This is a nop

View File

@ -362,13 +362,13 @@ maybe_create_keyring_or_box (char *filename, int is_box, int force_create)
that the detection magic will work the next time it is used. */ that the detection magic will work the next time it is used. */
if (is_box) if (is_box)
{ {
FILE *fp = fopen (filename, "wb"); estream_t fp = es_fopen (filename, "wb");
if (!fp) if (!fp)
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
else else
{ {
rc = _keybox_write_header_blob (fp, NULL, 1); rc = _keybox_write_header_blob (fp, 1);
fclose (fp); es_fclose (fp);
} }
if (rc) if (rc)
{ {

View File

@ -99,8 +99,8 @@ migrate_secring (ctrl_t ctrl)
log_info ("porting secret keys from '%s' to gpg-agent\n", secring); log_info ("porting secret keys from '%s' to gpg-agent\n", secring);
if (!import_old_secring (ctrl, secring)) if (!import_old_secring (ctrl, secring))
{ {
FILE *fp = fopen (flagfile, "w"); estream_t fp = es_fopen (flagfile, "w");
if (!fp || fclose (fp)) if (!fp || es_fclose (fp))
log_error ("error creating flag file '%s': %s\n", log_error ("error creating flag file '%s': %s\n",
flagfile, gpg_strerror (gpg_error_from_syserror ())); flagfile, gpg_strerror (gpg_error_from_syserror ()));
else else

View File

@ -531,12 +531,12 @@ create_temp_file (struct spawn_info *info, const void *ptr, u32 len)
} }
else else
{ {
FILE *fp = fopen (info->tempfile, "wb"); estream_t fp = es_fopen (info->tempfile, "wb");
if (fp) if (fp)
{ {
fwrite (ptr, len, 1, fp); es_fwrite (ptr, len, 1, fp);
fclose (fp); es_fclose (fp);
return 0; return 0;
} }
else else

View File

@ -725,7 +725,7 @@ tdbio_set_dbname (ctrl_t ctrl, const char *new_dbname,
|| stat (fname, &statbuf) || stat (fname, &statbuf)
|| statbuf.st_size == 0) || statbuf.st_size == 0)
{ {
FILE *fp; estream_t fp;
TRUSTREC rec; TRUSTREC rec;
int rc; int rc;
mode_t oldmask; mode_t oldmask;
@ -747,11 +747,11 @@ tdbio_set_dbname (ctrl_t ctrl, const char *new_dbname,
gpg_err_set_errno (EPERM); gpg_err_set_errno (EPERM);
} }
else else
fp = fopen (fname, "wb"); fp = es_fopen (fname, "wb");
umask(oldmask); umask(oldmask);
if (!fp) if (!fp)
log_fatal (_("can't create '%s': %s\n"), fname, strerror (errno)); log_fatal (_("can't create '%s': %s\n"), fname, strerror (errno));
fclose (fp); es_fclose (fp);
db_fd = open (db_name, O_RDWR | MY_O_BINARY); db_fd = open (db_name, O_RDWR | MY_O_BINARY);
if (db_fd == -1) if (db_fd == -1)

View File

@ -126,7 +126,7 @@ create_keybox (const char *filename)
filename, gpg_strerror (err)); filename, gpg_strerror (err));
goto leave; goto leave;
} }
err = _keybox_write_header_blob (NULL, fp, 1); err = _keybox_write_header_blob (fp, 1);
es_fclose (fp); es_fclose (fp);
if (err) if (err)
{ {

View File

@ -423,7 +423,7 @@ import_openpgp (const char *filename, int dryrun)
} }
else else
{ {
fflush (stdout); es_fflush (es_stdout);
log_info ("%s: failed to parse OpenPGP keyblock: %s\n", log_info ("%s: failed to parse OpenPGP keyblock: %s\n",
filename, gpg_strerror (err)); filename, gpg_strerror (err));
} }
@ -437,17 +437,17 @@ import_openpgp (const char *filename, int dryrun)
err = _keybox_create_openpgp_blob (&blob, &info, p, nparsed, 0); err = _keybox_create_openpgp_blob (&blob, &info, p, nparsed, 0);
if (err) if (err)
{ {
fflush (stdout); es_fflush (es_stdout);
log_error ("%s: failed to create OpenPGP keyblock: %s\n", log_error ("%s: failed to create OpenPGP keyblock: %s\n",
filename, gpg_strerror (err)); filename, gpg_strerror (err));
} }
else else
{ {
err = _keybox_write_blob (blob, stdout); err = _keybox_write_blob (blob, es_stdout, NULL);
_keybox_release_blob (blob); _keybox_release_blob (blob);
if (err) if (err)
{ {
fflush (stdout); es_fflush (es_stdout);
log_error ("%s: failed to write OpenPGP keyblock: %s\n", log_error ("%s: failed to write OpenPGP keyblock: %s\n",
filename, gpg_strerror (err)); filename, gpg_strerror (err));
} }

View File

@ -80,7 +80,7 @@ struct keybox_found_s
struct keybox_handle { struct keybox_handle {
KB_NAME kb; KB_NAME kb;
int secret; /* this is for a secret keybox */ int secret; /* this is for a secret keybox */
FILE *fp; estream_t fp;
int eof; int eof;
int error; int error;
int ephemeral; int ephemeral;
@ -172,8 +172,8 @@ void _keybox_destroy_openpgp_info (keybox_openpgp_info_t info);
/*-- keybox-file.c --*/ /*-- keybox-file.c --*/
int _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted); int _keybox_read_blob (KEYBOXBLOB *r_blob, estream_t fp, int *skipped_deleted);
int _keybox_write_blob (KEYBOXBLOB blob, FILE *fp); int _keybox_write_blob (KEYBOXBLOB blob, estream_t fp, FILE *outfp);
/*-- keybox-search.c --*/ /*-- keybox-search.c --*/
gpg_err_code_t _keybox_get_flag_location (const unsigned char *buffer, gpg_err_code_t _keybox_get_flag_location (const unsigned char *buffer,

View File

@ -647,18 +647,18 @@ update_stats (KEYBOXBLOB blob, struct file_stats_s *s)
static FILE * static estream_t
open_file (const char **filename, FILE *outfp) open_file (const char **filename, FILE *outfp)
{ {
FILE *fp; estream_t fp;
if (!*filename) if (!*filename)
{ {
*filename = "-"; *filename = "-";
fp = stdin; fp = es_stdin;
} }
else else
fp = fopen (*filename, "rb"); fp = es_fopen (*filename, "rb");
if (!fp) if (!fp)
{ {
int save_errno = errno; int save_errno = errno;
@ -673,7 +673,7 @@ open_file (const char **filename, FILE *outfp)
int int
_keybox_dump_file (const char *filename, int stats_only, FILE *outfp) _keybox_dump_file (const char *filename, int stats_only, FILE *outfp)
{ {
FILE *fp; estream_t fp;
KEYBOXBLOB blob; KEYBOXBLOB blob;
int rc; int rc;
unsigned long count = 0; unsigned long count = 0;
@ -726,8 +726,8 @@ _keybox_dump_file (const char *filename, int stats_only, FILE *outfp)
if (rc) if (rc)
fprintf (outfp, "# error reading '%s': %s\n", filename, gpg_strerror (rc)); fprintf (outfp, "# error reading '%s': %s\n", filename, gpg_strerror (rc));
if (fp != stdin) if (fp != es_stdin)
fclose (fp); es_fclose (fp);
if (stats_only) if (stats_only)
{ {
@ -787,7 +787,7 @@ cmp_dupitems (const void *arg_a, const void *arg_b)
int int
_keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp) _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp)
{ {
FILE *fp; estream_t fp;
KEYBOXBLOB blob; KEYBOXBLOB blob;
int rc; int rc;
unsigned long recno = 0; unsigned long recno = 0;
@ -849,8 +849,8 @@ _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp)
rc = 0; rc = 0;
if (rc) if (rc)
fprintf (outfp, "error reading '%s': %s\n", filename, gpg_strerror (rc)); fprintf (outfp, "error reading '%s': %s\n", filename, gpg_strerror (rc));
if (fp != stdin) if (fp != es_stdin)
fclose (fp); es_fclose (fp);
qsort (dupitems, dupitems_count, sizeof *dupitems, cmp_dupitems); qsort (dupitems, dupitems_count, sizeof *dupitems, cmp_dupitems);
@ -880,7 +880,7 @@ int
_keybox_dump_cut_records (const char *filename, unsigned long from, _keybox_dump_cut_records (const char *filename, unsigned long from,
unsigned long to, FILE *outfp) unsigned long to, FILE *outfp)
{ {
FILE *fp; estream_t fp;
KEYBOXBLOB blob; KEYBOXBLOB blob;
int rc; int rc;
unsigned long recno = 0; unsigned long recno = 0;
@ -894,7 +894,7 @@ _keybox_dump_cut_records (const char *filename, unsigned long from,
break; /* Ready. */ break; /* Ready. */
if (recno >= from) if (recno >= from)
{ {
if ((rc = _keybox_write_blob (blob, outfp))) if ((rc = _keybox_write_blob (blob, NULL, outfp)))
{ {
fprintf (stderr, "error writing output: %s\n", fprintf (stderr, "error writing output: %s\n",
gpg_strerror (rc)); gpg_strerror (rc));
@ -909,7 +909,7 @@ _keybox_dump_cut_records (const char *filename, unsigned long from,
if (rc) if (rc)
fprintf (stderr, "error reading '%s': %s\n", filename, gpg_strerror (rc)); fprintf (stderr, "error reading '%s': %s\n", filename, gpg_strerror (rc));
leave: leave:
if (fp != stdin) if (fp != es_stdin)
fclose (fp); es_fclose (fp);
return rc; return rc;
} }

View File

@ -48,7 +48,7 @@ ftello (FILE *stream)
/* Read a block at the current position and return it in R_BLOB. /* Read a block at the current position and return it in R_BLOB.
R_BLOB may be NULL to simply skip the current block. */ R_BLOB may be NULL to simply skip the current block. */
int int
_keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted) _keybox_read_blob (KEYBOXBLOB *r_blob, estream_t fp, int *skipped_deleted)
{ {
unsigned char *image; unsigned char *image;
size_t imagelen = 0; size_t imagelen = 0;
@ -61,19 +61,19 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
again: again:
if (r_blob) if (r_blob)
*r_blob = NULL; *r_blob = NULL;
off = ftello (fp); off = es_ftello (fp);
if (off == (off_t)-1) if (off == (off_t)-1)
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
if ((c1 = getc (fp)) == EOF if ((c1 = es_getc (fp)) == EOF
|| (c2 = getc (fp)) == EOF || (c2 = es_getc (fp)) == EOF
|| (c3 = getc (fp)) == EOF || (c3 = es_getc (fp)) == EOF
|| (c4 = getc (fp)) == EOF || (c4 = es_getc (fp)) == EOF
|| (type = getc (fp)) == EOF) || (type = es_getc (fp)) == EOF)
{ {
if ( c1 == EOF && !ferror (fp) ) if ( c1 == EOF && !es_ferror (fp) )
return -1; /* eof */ return -1; /* eof */
if (!ferror (fp)) if (!es_ferror (fp))
return gpg_error (GPG_ERR_TOO_SHORT); return gpg_error (GPG_ERR_TOO_SHORT);
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
} }
@ -85,7 +85,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
if (!type) if (!type)
{ {
/* Special treatment for empty blobs. */ /* Special treatment for empty blobs. */
if (fseek (fp, imagelen-5, SEEK_CUR)) if (es_fseek (fp, imagelen-5, SEEK_CUR))
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
if (skipped_deleted) if (skipped_deleted)
*skipped_deleted = 1; *skipped_deleted = 1;
@ -96,7 +96,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
{ {
/* Seek forward so that the caller may choose to ignore this /* Seek forward so that the caller may choose to ignore this
record. */ record. */
if (fseek (fp, imagelen-5, SEEK_CUR)) if (es_fseek (fp, imagelen-5, SEEK_CUR))
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
return gpg_error (GPG_ERR_TOO_LARGE); return gpg_error (GPG_ERR_TOO_LARGE);
} }
@ -104,7 +104,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
if (!r_blob) if (!r_blob)
{ {
/* This blob shall be skipped. */ /* This blob shall be skipped. */
if (fseek (fp, imagelen-5, SEEK_CUR)) if (es_fseek (fp, imagelen-5, SEEK_CUR))
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
return 0; return 0;
} }
@ -114,7 +114,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4; image[4] = type; image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4; image[4] = type;
if (fread (image+5, imagelen-5, 1, fp) != 1) if (es_fread (image+5, imagelen-5, 1, fp) != 1)
{ {
gpg_error_t tmperr = gpg_error_from_syserror (); gpg_error_t tmperr = gpg_error_from_syserror ();
xfree (image); xfree (image);
@ -130,7 +130,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
/* Write the block to the current file position */ /* Write the block to the current file position */
int int
_keybox_write_blob (KEYBOXBLOB blob, FILE *fp) _keybox_write_blob (KEYBOXBLOB blob, estream_t fp, FILE *outfp)
{ {
const unsigned char *image; const unsigned char *image;
size_t length; size_t length;
@ -140,15 +140,24 @@ _keybox_write_blob (KEYBOXBLOB blob, FILE *fp)
if (length > IMAGELEN_LIMIT) if (length > IMAGELEN_LIMIT)
return gpg_error (GPG_ERR_TOO_LARGE); return gpg_error (GPG_ERR_TOO_LARGE);
if (fwrite (image, length, 1, fp) != 1) if (fp)
return gpg_error_from_syserror (); {
if (es_fwrite (image, length, 1, fp) != 1)
return gpg_error_from_syserror ();
}
else
{
if (fwrite (image, length, 1, outfp) != 1)
return gpg_error_from_syserror ();
}
return 0; return 0;
} }
/* Write a fresh header type blob. Either FP or STREAM must be used. */ /* Write a fresh header type blob. */
gpg_error_t gpg_error_t
_keybox_write_header_blob (FILE *fp, estream_t stream, int for_openpgp) _keybox_write_header_blob (estream_t fp, int for_openpgp)
{ {
unsigned char image[32]; unsigned char image[32];
u32 val; u32 val;
@ -174,15 +183,8 @@ _keybox_write_header_blob (FILE *fp, estream_t stream, int for_openpgp)
image[20+2] = (val >> 8); image[20+2] = (val >> 8);
image[20+3] = (val ); image[20+3] = (val );
if (fp) if (es_fwrite (image, 32, 1, fp) != 1)
{ return gpg_error_from_syserror ();
if (fwrite (image, 32, 1, fp) != 1)
return gpg_error_from_syserror ();
}
else
{
if (es_fwrite (image, 32, 1, stream) != 1)
return gpg_error_from_syserror ();
}
return 0; return 0;
} }

View File

@ -180,7 +180,7 @@ keybox_release (KEYBOX_HANDLE hd)
_keybox_release_blob (hd->saved_found.blob); _keybox_release_blob (hd->saved_found.blob);
if (hd->fp) if (hd->fp)
{ {
fclose (hd->fp); es_fclose (hd->fp);
hd->fp = NULL; hd->fp = NULL;
} }
xfree (hd->word_match.name); xfree (hd->word_match.name);
@ -253,11 +253,11 @@ _keybox_close_file (KEYBOX_HANDLE hd)
{ {
if (roverhd->fp) if (roverhd->fp)
{ {
fclose (roverhd->fp); es_fclose (roverhd->fp);
roverhd->fp = NULL; roverhd->fp = NULL;
} }
} }
assert (!hd->fp); log_assert (!hd->fp);
} }

View File

@ -880,7 +880,7 @@ static gpg_error_t
open_file (KEYBOX_HANDLE hd) open_file (KEYBOX_HANDLE hd)
{ {
hd->fp = fopen (hd->kb->fname, "rb"); hd->fp = es_fopen (hd->kb->fname, "rb");
if (!hd->fp) if (!hd->fp)
{ {
hd->error = gpg_error_from_syserror (); hd->error = gpg_error_from_syserror ();
@ -912,11 +912,11 @@ keybox_search_reset (KEYBOX_HANDLE hd)
if (hd->fp) if (hd->fp)
{ {
if (fseeko (hd->fp, 0, SEEK_SET)) if (es_fseeko (hd->fp, 0, SEEK_SET))
{ {
/* Ooops. Seek did not work. Close so that the search will /* Ooops. Seek did not work. Close so that the search will
* open the file again. */ * open the file again. */
fclose (hd->fp); es_fclose (hd->fp);
hd->fp = NULL; hd->fp = NULL;
} }
} }
@ -1007,7 +1007,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc,
* returned a blob which also was not the first one. We now * returned a blob which also was not the first one. We now
* need to skip over that blob and hope that the file has * need to skip over that blob and hope that the file has
* not changed. */ * not changed. */
if (fseeko (hd->fp, lastfoundoff, SEEK_SET)) if (es_fseeko (hd->fp, lastfoundoff, SEEK_SET))
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
log_debug ("%s: seeking to last found offset failed: %s\n", log_debug ("%s: seeking to last found offset failed: %s\n",
@ -1462,7 +1462,7 @@ keybox_offset (KEYBOX_HANDLE hd)
{ {
if (!hd->fp) if (!hd->fp)
return 0; return 0;
return ftello (hd->fp); return es_ftello (hd->fp);
} }
gpg_error_t gpg_error_t
@ -1487,7 +1487,7 @@ keybox_seek (KEYBOX_HANDLE hd, off_t offset)
return err; return err;
} }
err = fseeko (hd->fp, offset, SEEK_SET); err = es_fseeko (hd->fp, offset, SEEK_SET);
hd->error = gpg_error_from_errno (err); hd->error = gpg_error_from_errno (err);
return hd->error; return hd->error;

View File

@ -71,14 +71,14 @@ fseeko (FILE * stream, off_t newpos, int whence)
static int static int
create_tmp_file (const char *template, create_tmp_file (const char *template,
char **r_bakfname, char **r_tmpfname, FILE **r_fp) char **r_bakfname, char **r_tmpfname, estream_t *r_fp)
{ {
gpg_error_t err; gpg_error_t err;
err = keybox_tmp_names (template, 0, r_bakfname, r_tmpfname); err = keybox_tmp_names (template, 0, r_bakfname, r_tmpfname);
if (!err) if (!err)
{ {
*r_fp = fopen (*r_tmpfname, "wb"); *r_fp = es_fopen (*r_tmpfname, "wb");
if (!*r_fp) if (!*r_fp)
{ {
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
@ -162,8 +162,8 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
int secret, int for_openpgp, off_t start_offset) int secret, int for_openpgp, off_t start_offset)
{ {
gpg_err_code_t ec; gpg_err_code_t ec;
FILE *fp, *newfp; estream_t fp, newfp;
int rc=0; int rc = 0;
char *bakfname = NULL; char *bakfname = NULL;
char *tmpfname = NULL; char *tmpfname = NULL;
char buffer[4096]; /* (Must be at least 32 bytes) */ char buffer[4096]; /* (Must be at least 32 bytes) */
@ -174,30 +174,30 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
if ((ec = gnupg_access (fname, W_OK))) if ((ec = gnupg_access (fname, W_OK)))
return gpg_error (ec); return gpg_error (ec);
fp = fopen (fname, "rb"); fp = es_fopen (fname, "rb");
if (mode == FILECOPY_INSERT && !fp && errno == ENOENT) if (mode == FILECOPY_INSERT && !fp && errno == ENOENT)
{ {
/* Insert mode but file does not exist: /* Insert mode but file does not exist:
Create a new keybox file. */ Create a new keybox file. */
newfp = fopen (fname, "wb"); newfp = es_fopen (fname, "wb");
if (!newfp ) if (!newfp )
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
rc = _keybox_write_header_blob (newfp, NULL, for_openpgp); rc = _keybox_write_header_blob (newfp, for_openpgp);
if (rc) if (rc)
{ {
fclose (newfp); es_fclose (newfp);
return rc; return rc;
} }
rc = _keybox_write_blob (blob, newfp); rc = _keybox_write_blob (blob, newfp, NULL);
if (rc) if (rc)
{ {
fclose (newfp); es_fclose (newfp);
return rc; return rc;
} }
if ( fclose (newfp) ) if ( es_fclose (newfp) )
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
/* if (chmod( fname, S_IRUSR | S_IWUSR )) */ /* if (chmod( fname, S_IRUSR | S_IWUSR )) */
@ -218,7 +218,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp); rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
if (rc) if (rc)
{ {
fclose (fp); es_fclose (fp);
goto leave; goto leave;
} }
@ -230,7 +230,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
/* Copy everything to the new file. If this is for OpenPGP, we /* Copy everything to the new file. If this is for OpenPGP, we
make sure that the openpgp flag is set in the header. (We make sure that the openpgp flag is set in the header. (We
failsafe the blob type.) */ failsafe the blob type.) */
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 ) while ( (nread = es_fread (buffer, 1, DIM(buffer), fp)) > 0 )
{ {
if (first_record && for_openpgp if (first_record && for_openpgp
&& buffer[4] == KEYBOX_BLOBTYPE_HEADER) && buffer[4] == KEYBOX_BLOBTYPE_HEADER)
@ -239,19 +239,19 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
buffer[7] |= 0x02; /* OpenPGP data may be available. */ buffer[7] |= 0x02; /* OpenPGP data may be available. */
} }
if (fwrite (buffer, nread, 1, newfp) != 1) if (es_fwrite (buffer, nread, 1, newfp) != 1)
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
fclose (fp); es_fclose (fp);
fclose (newfp); es_fclose (newfp);
goto leave; goto leave;
} }
} }
if (ferror (fp)) if (es_ferror (fp))
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
fclose (fp); es_fclose (fp);
fclose (newfp); es_fclose (newfp);
goto leave; goto leave;
} }
} }
@ -267,24 +267,24 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
nbytes = DIM(buffer); nbytes = DIM(buffer);
if (current + nbytes > start_offset) if (current + nbytes > start_offset)
nbytes = start_offset - current; nbytes = start_offset - current;
nread = fread (buffer, 1, nbytes, fp); nread = es_fread (buffer, 1, nbytes, fp);
if (!nread) if (!nread)
break; break;
current += nread; current += nread;
if (fwrite (buffer, nread, 1, newfp) != 1) if (es_fwrite (buffer, nread, 1, newfp) != 1)
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
fclose (fp); es_fclose (fp);
fclose (newfp); es_fclose (newfp);
goto leave; goto leave;
} }
} }
if (ferror (fp)) if (es_ferror (fp))
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
fclose (fp); es_fclose (fp);
fclose (newfp); es_fclose (newfp);
goto leave; goto leave;
} }
@ -292,8 +292,8 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
rc = _keybox_read_blob (NULL, fp, NULL); rc = _keybox_read_blob (NULL, fp, NULL);
if (rc) if (rc)
{ {
fclose (fp); es_fclose (fp);
fclose (newfp); es_fclose (newfp);
return rc; return rc;
} }
} }
@ -301,11 +301,11 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
/* Do an insert or update. */ /* Do an insert or update. */
if ( mode == FILECOPY_INSERT || mode == FILECOPY_UPDATE ) if ( mode == FILECOPY_INSERT || mode == FILECOPY_UPDATE )
{ {
rc = _keybox_write_blob (blob, newfp); rc = _keybox_write_blob (blob, newfp, NULL);
if (rc) if (rc)
{ {
fclose (fp); es_fclose (fp);
fclose (newfp); es_fclose (newfp);
return rc; return rc;
} }
} }
@ -313,33 +313,33 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
/* Copy the rest of the packet for an delete or update. */ /* Copy the rest of the packet for an delete or update. */
if (mode == FILECOPY_DELETE || mode == FILECOPY_UPDATE) if (mode == FILECOPY_DELETE || mode == FILECOPY_UPDATE)
{ {
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 ) while ( (nread = es_fread (buffer, 1, DIM(buffer), fp)) > 0 )
{ {
if (fwrite (buffer, nread, 1, newfp) != 1) if (es_fwrite (buffer, nread, 1, newfp) != 1)
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
fclose (fp); es_fclose (fp);
fclose (newfp); es_fclose (newfp);
goto leave; goto leave;
} }
} }
if (ferror (fp)) if (es_ferror (fp))
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
fclose (fp); es_fclose (fp);
fclose (newfp); es_fclose (newfp);
goto leave; goto leave;
} }
} }
/* Close both files. */ /* Close both files. */
if (fclose(fp)) if (es_fclose(fp))
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
fclose (newfp); es_fclose (newfp);
goto leave; goto leave;
} }
if (fclose(newfp)) if (es_fclose(newfp))
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
goto leave; goto leave;
@ -504,7 +504,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
{ {
off_t off; off_t off;
const char *fname; const char *fname;
FILE *fp; estream_t fp;
gpg_err_code_t ec; gpg_err_code_t ec;
size_t flag_pos, flag_size; size_t flag_pos, flag_size;
const unsigned char *buffer; const unsigned char *buffer;
@ -536,12 +536,12 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
off += flag_pos; off += flag_pos;
_keybox_close_file (hd); _keybox_close_file (hd);
fp = fopen (hd->kb->fname, "r+b"); fp = es_fopen (hd->kb->fname, "r+b");
if (!fp) if (!fp)
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
ec = 0; ec = 0;
if (fseeko (fp, off, SEEK_SET)) if (es_fseeko (fp, off, SEEK_SET))
ec = gpg_err_code_from_syserror (); ec = gpg_err_code_from_syserror ();
else else
{ {
@ -557,7 +557,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
case 1: case 1:
case 2: case 2:
case 4: case 4:
if (fwrite (tmp+4-flag_size, flag_size, 1, fp) != 1) if (es_fwrite (tmp+4-flag_size, flag_size, 1, fp) != 1)
ec = gpg_err_code_from_syserror (); ec = gpg_err_code_from_syserror ();
break; break;
default: default:
@ -566,7 +566,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
} }
} }
if (fclose (fp)) if (es_fclose (fp))
{ {
if (!ec) if (!ec)
ec = gpg_err_code_from_syserror (); ec = gpg_err_code_from_syserror ();
@ -582,7 +582,7 @@ keybox_delete (KEYBOX_HANDLE hd)
{ {
off_t off; off_t off;
const char *fname; const char *fname;
FILE *fp; estream_t fp;
int rc; int rc;
if (!hd) if (!hd)
@ -601,18 +601,18 @@ keybox_delete (KEYBOX_HANDLE hd)
off += 4; off += 4;
_keybox_close_file (hd); _keybox_close_file (hd);
fp = fopen (hd->kb->fname, "r+b"); fp = es_fopen (hd->kb->fname, "r+b");
if (!fp) if (!fp)
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
if (fseeko (fp, off, SEEK_SET)) if (es_fseeko (fp, off, SEEK_SET))
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
else if (putc (0, fp) == EOF) else if (es_fputc (0, fp) == EOF)
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
else else
rc = 0; rc = 0;
if (fclose (fp)) if (es_fclose (fp))
{ {
if (!rc) if (!rc)
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
@ -630,7 +630,7 @@ keybox_compress (KEYBOX_HANDLE hd)
gpg_err_code_t ec; gpg_err_code_t ec;
int read_rc, rc; int read_rc, rc;
const char *fname; const char *fname;
FILE *fp, *newfp; estream_t fp, newfp;
char *bakfname = NULL; char *bakfname = NULL;
char *tmpfname = NULL; char *tmpfname = NULL;
int first_blob; int first_blob;
@ -656,7 +656,7 @@ keybox_compress (KEYBOX_HANDLE hd)
if ((ec = gnupg_access (fname, W_OK))) if ((ec = gnupg_access (fname, W_OK)))
return gpg_error (ec); return gpg_error (ec);
fp = fopen (fname, "rb"); fp = es_fopen (fname, "rb");
if (!fp && errno == ENOENT) if (!fp && errno == ENOENT)
return 0; /* Ready. File has been deleted right after the access above. */ return 0; /* Ready. File has been deleted right after the access above. */
if (!fp) if (!fp)
@ -679,21 +679,21 @@ keybox_compress (KEYBOX_HANDLE hd)
if ( (last_maint + 3*3600) > make_timestamp () ) if ( (last_maint + 3*3600) > make_timestamp () )
{ {
fclose (fp); es_fclose (fp);
_keybox_release_blob (blob); _keybox_release_blob (blob);
return 0; /* Compress run not yet needed. */ return 0; /* Compress run not yet needed. */
} }
} }
_keybox_release_blob (blob); _keybox_release_blob (blob);
fseek (fp, 0, SEEK_SET); es_fseek (fp, 0, SEEK_SET);
clearerr (fp); es_clearerr (fp);
} }
/* Create the new file. */ /* Create the new file. */
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp); rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
if (rc) if (rc)
{ {
fclose (fp); es_fclose (fp);
return rc;; return rc;;
} }
@ -725,14 +725,14 @@ keybox_compress (KEYBOX_HANDLE hd)
stamp and if needed (ie. used by gpg) set the openpgp stamp and if needed (ie. used by gpg) set the openpgp
flag. */ flag. */
_keybox_update_header_blob (blob, hd->for_openpgp); _keybox_update_header_blob (blob, hd->for_openpgp);
rc = _keybox_write_blob (blob, newfp); rc = _keybox_write_blob (blob, newfp, NULL);
if (rc) if (rc)
break; break;
continue; continue;
} }
/* The header blob is missing. Insert it. */ /* The header blob is missing. Insert it. */
rc = _keybox_write_header_blob (newfp, NULL, hd->for_openpgp); rc = _keybox_write_header_blob (newfp, hd->for_openpgp);
if (rc) if (rc)
break; break;
any_changes = 1; any_changes = 1;
@ -769,7 +769,7 @@ keybox_compress (KEYBOX_HANDLE hd)
} }
} }
rc = _keybox_write_blob (blob, newfp); rc = _keybox_write_blob (blob, newfp, NULL);
if (rc) if (rc)
break; break;
} }
@ -782,9 +782,9 @@ keybox_compress (KEYBOX_HANDLE hd)
rc = read_rc; rc = read_rc;
/* Close both files. */ /* Close both files. */
if (fclose(fp) && !rc) if (es_fclose(fp) && !rc)
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
if (fclose(newfp) && !rc) if (es_fclose(newfp) && !rc)
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
/* Rename or remove the temporary file. */ /* Rename or remove the temporary file. */

View File

@ -81,8 +81,7 @@ gpg_error_t keybox_lock (KEYBOX_HANDLE hd, int yes, long timeout);
/*-- keybox-file.c --*/ /*-- keybox-file.c --*/
/* Fixme: This function does not belong here: Provide a better /* Fixme: This function does not belong here: Provide a better
interface to create a new keybox file. */ interface to create a new keybox file. */
gpg_error_t _keybox_write_header_blob (FILE *fp, estream_t stream, gpg_error_t _keybox_write_header_blob (estream_t fp, int openpgp_flag);
int openpgp_flag);
/*-- keybox-search.c --*/ /*-- keybox-search.c --*/
gpg_error_t keybox_get_data (KEYBOX_HANDLE hd, gpg_error_t keybox_get_data (KEYBOX_HANDLE hd,

View File

@ -1935,18 +1935,18 @@ report_change (int slot, int old_status, int cur_status)
char *homestr, *envstr; char *homestr, *envstr;
char *fname; char *fname;
char templ[50]; char templ[50];
FILE *fp; estream_t fp;
snprintf (templ, sizeof templ, "reader_%d.status", slot); snprintf (templ, sizeof templ, "reader_%d.status", slot);
fname = make_filename (gnupg_homedir (), templ, NULL ); fname = make_filename (gnupg_homedir (), templ, NULL );
fp = fopen (fname, "w"); fp = es_fopen (fname, "w");
if (fp) if (fp)
{ {
fprintf (fp, "%s\n", es_fprintf (fp, "%s\n",
(cur_status & 1)? "USABLE": (cur_status & 1)? "USABLE":
(cur_status & 4)? "ACTIVE": (cur_status & 4)? "ACTIVE":
(cur_status & 2)? "PRESENT": "NOCARD"); (cur_status & 2)? "PRESENT": "NOCARD");
fclose (fp); es_fclose (fp);
} }
xfree (fname); xfree (fname);

View File

@ -308,7 +308,7 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
{ {
gpg_error_t err; gpg_error_t err;
char *policies; char *policies;
FILE *fp; estream_t fp;
int any_critical; int any_critical;
err = ksba_cert_get_cert_policies (cert, &policies); err = ksba_cert_get_cert_policies (cert, &policies);
@ -340,7 +340,7 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
return 0; return 0;
} }
fp = fopen (opt.policy_file, "r"); fp = es_fopen (opt.policy_file, "r");
if (!fp) if (!fp)
{ {
if (opt.verbose || errno != ENOENT) if (opt.verbose || errno != ENOENT)
@ -369,14 +369,14 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
/* read line */ /* read line */
do do
{ {
if (!fgets (line, DIM(line)-1, fp) ) if (!es_fgets (line, DIM(line)-1, fp) )
{ {
gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno)); gpg_error_t tmperr = gpg_error_from_syserror ();
xfree (policies); xfree (policies);
if (feof (fp)) if (es_feof (fp))
{ {
fclose (fp); es_fclose (fp);
/* With no critical policies this is only a warning */ /* With no critical policies this is only a warning */
if (!any_critical) if (!any_critical)
{ {
@ -388,16 +388,16 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
_("certificate policy not allowed")); _("certificate policy not allowed"));
return gpg_error (GPG_ERR_NO_POLICY_MATCH); return gpg_error (GPG_ERR_NO_POLICY_MATCH);
} }
fclose (fp); es_fclose (fp);
return tmperr; return tmperr;
} }
if (!*line || line[strlen(line)-1] != '\n') if (!*line || line[strlen(line)-1] != '\n')
{ {
/* eat until end of line */ /* eat until end of line */
while ( (c=getc (fp)) != EOF && c != '\n') while ((c = es_getc (fp)) != EOF && c != '\n')
; ;
fclose (fp); es_fclose (fp);
xfree (policies); xfree (policies);
return gpg_error (*line? GPG_ERR_LINE_TOO_LONG return gpg_error (*line? GPG_ERR_LINE_TOO_LONG
: GPG_ERR_INCOMPLETE_LINE); : GPG_ERR_INCOMPLETE_LINE);
@ -417,7 +417,7 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
p = strpbrk (allowed, " :\n"); p = strpbrk (allowed, " :\n");
if (!*p || p == allowed) if (!*p || p == allowed)
{ {
fclose (fp); es_fclose (fp);
xfree (policies); xfree (policies);
return gpg_error (GPG_ERR_CONFIGURATION); return gpg_error (GPG_ERR_CONFIGURATION);
} }
@ -430,7 +430,7 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
if (p[strlen (allowed)] != ':') if (p[strlen (allowed)] != ':')
continue; /* The length does not match. */ continue; /* The length does not match. */
/* Yep - it does match so return okay. */ /* Yep - it does match so return okay. */
fclose (fp); es_fclose (fp);
xfree (policies); xfree (policies);
return 0; return 0;
} }

View File

@ -208,7 +208,7 @@ maybe_create_keybox (char *filename, int force, int *r_created)
{ {
gpg_err_code_t ec; gpg_err_code_t ec;
dotlock_t lockhd = NULL; dotlock_t lockhd = NULL;
FILE *fp; estream_t fp;
int rc; int rc;
mode_t oldmask; mode_t oldmask;
char *last_slash_in_filename; char *last_slash_in_filename;
@ -299,7 +299,7 @@ maybe_create_keybox (char *filename, int force, int *r_created)
/* The file does not yet exist, create it now. */ /* The file does not yet exist, create it now. */
oldmask = umask (077); oldmask = umask (077);
fp = fopen (filename, "wb"); fp = es_fopen (filename, "wb");
if (!fp) if (!fp)
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
@ -313,10 +313,10 @@ maybe_create_keybox (char *filename, int force, int *r_created)
/* Make sure that at least one record is in a new keybox file, so /* Make sure that at least one record is in a new keybox file, so
that the detection magic for OpenPGP keyboxes works the next time that the detection magic for OpenPGP keyboxes works the next time
it is used. */ it is used. */
rc = _keybox_write_header_blob (fp, NULL, 0); rc = _keybox_write_header_blob (fp, 0);
if (rc) if (rc)
{ {
fclose (fp); es_fclose (fp);
log_error (_("error creating keybox '%s': %s\n"), log_error (_("error creating keybox '%s': %s\n"),
filename, gpg_strerror (rc)); filename, gpg_strerror (rc));
goto leave; goto leave;
@ -327,7 +327,7 @@ maybe_create_keybox (char *filename, int force, int *r_created)
if (r_created) if (r_created)
*r_created = 1; *r_created = 1;
fclose (fp); es_fclose (fp);
rc = 0; rc = 0;
leave: leave:
@ -394,14 +394,15 @@ keydb_add_resource (ctrl_t ctrl, const char *url, int force, int *auto_created)
/* see whether we can determine the filetype */ /* see whether we can determine the filetype */
if (rt == KEYDB_RESOURCE_TYPE_NONE) if (rt == KEYDB_RESOURCE_TYPE_NONE)
{ {
FILE *fp = fopen( filename, "rb" ); estream_t fp;
fp = es_fopen( filename, "rb" );
if (fp) if (fp)
{ {
u32 magic; u32 magic;
/* FIXME: check for the keybox magic */ /* FIXME: check for the keybox magic */
if (fread (&magic, 4, 1, fp) == 1 ) if (es_fread (&magic, 4, 1, fp) == 1 )
{ {
if (magic == 0x13579ace || magic == 0xce9a5713) if (magic == 0x13579ace || magic == 0xce9a5713)
; /* GDBM magic - no more support */ ; /* GDBM magic - no more support */
@ -410,7 +411,8 @@ keydb_add_resource (ctrl_t ctrl, const char *url, int force, int *auto_created)
} }
else /* maybe empty: assume keybox */ else /* maybe empty: assume keybox */
rt = KEYDB_RESOURCE_TYPE_KEYBOX; rt = KEYDB_RESOURCE_TYPE_KEYBOX;
fclose (fp);
es_fclose (fp);
} }
else /* no file yet: create keybox */ else /* no file yet: create keybox */
rt = KEYDB_RESOURCE_TYPE_KEYBOX; rt = KEYDB_RESOURCE_TYPE_KEYBOX;

View File

@ -34,7 +34,7 @@
NULL indicates that this module has been initialized and if the NULL indicates that this module has been initialized and if the
LISTFP is also NULL, no list of qualified signatures exists. */ LISTFP is also NULL, no list of qualified signatures exists. */
static char *listname; static char *listname;
static FILE *listfp; static estream_t listfp;
/* Read the trustlist and return entry by entry. KEY must point to a /* Read the trustlist and return entry by entry. KEY must point to a
@ -58,7 +58,7 @@ read_list (char *key, char *country, int *lnr)
if (!listname) if (!listname)
{ {
listname = make_filename (gnupg_sysconfdir (), "qualified.txt", NULL); listname = make_filename (gnupg_sysconfdir (), "qualified.txt", NULL);
listfp = fopen (listname, "r"); listfp = es_fopen (listname, "r");
if (!listfp && errno != ENOENT) if (!listfp && errno != ENOENT)
{ {
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
@ -72,9 +72,9 @@ read_list (char *key, char *country, int *lnr)
do do
{ {
if (!fgets (line, DIM(line)-1, listfp) ) if (!es_fgets (line, DIM(line)-1, listfp) )
{ {
if (feof (listfp)) if (es_feof (listfp))
return gpg_error (GPG_ERR_EOF); return gpg_error (GPG_ERR_EOF);
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
} }
@ -82,7 +82,7 @@ read_list (char *key, char *country, int *lnr)
if (!*line || line[strlen(line)-1] != '\n') if (!*line || line[strlen(line)-1] != '\n')
{ {
/* Eat until end of line. */ /* Eat until end of line. */
while ( (c=getc (listfp)) != EOF && c != '\n') while ((c = es_getc (listfp)) != EOF && c != '\n')
; ;
return gpg_error (*line? GPG_ERR_LINE_TOO_LONG return gpg_error (*line? GPG_ERR_LINE_TOO_LONG
: GPG_ERR_INCOMPLETE_LINE); : GPG_ERR_INCOMPLETE_LINE);
@ -162,8 +162,8 @@ gpgsm_is_in_qualified_list (ctrl_t ctrl, ksba_cert_t cert, char *country)
if (listfp) if (listfp)
{ {
/* W32ce has no rewind, thus we use the equivalent code. */ /* W32ce has no rewind, thus we use the equivalent code. */
fseek (listfp, 0, SEEK_SET); es_fseek (listfp, 0, SEEK_SET);
clearerr (listfp); es_clearerr (listfp);
} }
while (!(err = read_list (key, mycountry, &lnr))) while (!(err = read_list (key, mycountry, &lnr)))
{ {

View File

@ -231,7 +231,7 @@ main (int argc, char **argv )
static char * static char *
read_file (const char *fname, size_t *r_length) read_file (const char *fname, size_t *r_length)
{ {
FILE *fp; estream_t fp;
char *buf; char *buf;
size_t buflen; size_t buflen;
@ -239,10 +239,8 @@ read_file (const char *fname, size_t *r_length)
{ {
size_t nread, bufsize = 0; size_t nread, bufsize = 0;
fp = stdin; fp = es_stdin;
#ifdef HAVE_DOSISH_SYSTEM es_set_binary (fp);
setmode ( fileno(fp) , O_BINARY );
#endif
buf = NULL; buf = NULL;
buflen = 0; buflen = 0;
#define NCHUNK 8192 #define NCHUNK 8192
@ -254,8 +252,8 @@ read_file (const char *fname, size_t *r_length)
else else
buf = xrealloc (buf, bufsize+1); buf = xrealloc (buf, bufsize+1);
nread = fread (buf+buflen, 1, NCHUNK, fp); nread = es_fread (buf+buflen, 1, NCHUNK, fp);
if (nread < NCHUNK && ferror (fp)) if (nread < NCHUNK && es_ferror (fp))
{ {
log_error ("error reading '[stdin]': %s\n", strerror (errno)); log_error ("error reading '[stdin]': %s\n", strerror (errno));
xfree (buf); xfree (buf);
@ -271,30 +269,30 @@ read_file (const char *fname, size_t *r_length)
{ {
struct stat st; struct stat st;
fp = fopen (fname, "rb"); fp = es_fopen (fname, "rb");
if (!fp) if (!fp)
{ {
log_error ("can't open '%s': %s\n", fname, strerror (errno)); log_error ("can't open '%s': %s\n", fname, strerror (errno));
return NULL; return NULL;
} }
if (fstat (fileno(fp), &st)) if (fstat (es_fileno (fp), &st))
{ {
log_error ("can't stat '%s': %s\n", fname, strerror (errno)); log_error ("can't stat '%s': %s\n", fname, strerror (errno));
fclose (fp); es_fclose (fp);
return NULL; return NULL;
} }
buflen = st.st_size; buflen = st.st_size;
buf = xmalloc (buflen+1); buf = xmalloc (buflen+1);
if (fread (buf, buflen, 1, fp) != 1) if (es_fread (buf, buflen, 1, fp) != 1)
{ {
log_error ("error reading '%s': %s\n", fname, strerror (errno)); log_error ("error reading '%s': %s\n", fname, strerror (errno));
fclose (fp); es_fclose (fp);
xfree (buf); xfree (buf);
return NULL; return NULL;
} }
fclose (fp); es_fclose (fp);
} }
buf[buflen] = 0; buf[buflen] = 0;
*r_length = buflen; *r_length = buflen;

View File

@ -182,7 +182,7 @@ typedef struct loopline_s *loopline_t;
static pid_t server_pid = (pid_t)(-1); static pid_t server_pid = (pid_t)(-1);
/* The current datasink file or NULL. */ /* The current datasink file or NULL. */
static FILE *current_datasink; static estream_t current_datasink;
/* A list of open file descriptors. */ /* A list of open file descriptors. */
static struct static struct
@ -892,7 +892,7 @@ clear_definq (void)
static void static void
do_sendfd (assuan_context_t ctx, char *line) do_sendfd (assuan_context_t ctx, char *line)
{ {
FILE *fp; estream_t fp;
char *name, *mode, *p; char *name, *mode, *p;
int rc, fd; int rc, fd;
@ -918,14 +918,14 @@ do_sendfd (assuan_context_t ctx, char *line)
} }
/* Open and send. */ /* Open and send. */
fp = fopen (name, mode); fp = es_fopen (name, mode);
if (!fp) if (!fp)
{ {
log_error ("can't open '%s' in \"%s\" mode: %s\n", log_error ("can't open '%s' in \"%s\" mode: %s\n",
name, mode, strerror (errno)); name, mode, strerror (errno));
return; return;
} }
fd = fileno (fp); fd = es_fileno (fp);
if (opt.verbose) if (opt.verbose)
log_error ("file '%s' opened in \"%s\" mode, fd=%d\n", log_error ("file '%s' opened in \"%s\" mode, fd=%d\n",
@ -934,7 +934,7 @@ do_sendfd (assuan_context_t ctx, char *line)
rc = assuan_sendfd (ctx, INT2FD (fd) ); rc = assuan_sendfd (ctx, INT2FD (fd) );
if (rc) if (rc)
log_error ("sending descriptor %d failed: %s\n", fd, gpg_strerror (rc)); log_error ("sending descriptor %d failed: %s\n", fd, gpg_strerror (rc));
fclose (fp); es_fclose (fp);
} }
@ -950,7 +950,7 @@ do_recvfd (assuan_context_t ctx, char *line)
static void static void
do_open (char *line) do_open (char *line)
{ {
FILE *fp; estream_t fp;
char *varname, *name, *mode, *p; char *varname, *name, *mode, *p;
int fd; int fd;
@ -994,14 +994,14 @@ do_open (char *line)
} }
/* Open and send. */ /* Open and send. */
fp = fopen (name, mode); fp = es_fopen (name, mode);
if (!fp) if (!fp)
{ {
log_error ("can't open '%s' in \"%s\" mode: %s\n", log_error ("can't open '%s' in \"%s\" mode: %s\n",
name, mode, strerror (errno)); name, mode, strerror (errno));
return; return;
} }
fd = dup (fileno (fp)); fd = dup (es_fileno (fp));
if (fd >= 0 && fd < DIM (open_fd_table)) if (fd >= 0 && fd < DIM (open_fd_table))
{ {
open_fd_table[fd].inuse = 1; open_fd_table[fd].inuse = 1;
@ -1051,7 +1051,7 @@ do_open (char *line)
if (fd != -1) if (fd != -1)
close (fd); /* Table was full. */ close (fd); /* Table was full. */
} }
fclose (fp); es_fclose (fp);
} }
@ -1605,17 +1605,17 @@ main (int argc, char **argv)
if (current_datasink) if (current_datasink)
{ {
if (current_datasink != stdout) if (current_datasink != es_stdout)
fclose (current_datasink); es_fclose (current_datasink);
current_datasink = NULL; current_datasink = NULL;
} }
tmpline = opt.enable_varsubst? substitute_line (p) : NULL; tmpline = opt.enable_varsubst? substitute_line (p) : NULL;
fname = tmpline? tmpline : p; fname = tmpline? tmpline : p;
if (fname && !strcmp (fname, "-")) if (fname && !strcmp (fname, "-"))
current_datasink = stdout; current_datasink = es_stdout;
else if (fname && *fname) else if (fname && *fname)
{ {
current_datasink = fopen (fname, "wb"); current_datasink = es_fopen (fname, "wb");
if (!current_datasink) if (!current_datasink)
log_error ("can't open '%s': %s\n", log_error ("can't open '%s': %s\n",
fname, strerror (errno)); fname, strerror (errno));
@ -1964,6 +1964,7 @@ handle_inquire (assuan_context_t ctx, char *line)
{ {
const char *name; const char *name;
definq_t d; definq_t d;
/* FIXME: Due to the use of popen we can't easily switch to estream. */
FILE *fp = NULL; FILE *fp = NULL;
char buffer[1024]; char buffer[1024];
int rc, n; int rc, n;
@ -2115,7 +2116,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr)
} }
else else
c = *s; c = *s;
putc (c, current_datasink); es_putc (c, current_datasink);
} }
} }
else if (opt.hex) else if (opt.hex)
@ -2186,7 +2187,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr)
{ {
if (need_lf) if (need_lf)
{ {
if (!current_datasink || current_datasink != stdout) if (!current_datasink || current_datasink != es_stdout)
putchar ('\n'); putchar ('\n');
need_lf = 0; need_lf = 0;
} }
@ -2195,7 +2196,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr)
&& line[0] == 'S' && line[0] == 'S'
&& (line[1] == '\0' || line[1] == ' ')) && (line[1] == '\0' || line[1] == ' '))
{ {
if (!current_datasink || current_datasink != stdout) if (!current_datasink || current_datasink != es_stdout)
{ {
fwrite (line, linelen, 1, stdout); fwrite (line, linelen, 1, stdout);
putchar ('\n'); putchar ('\n');
@ -2205,7 +2206,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr)
&& line[0] == 'O' && line[1] == 'K' && line[0] == 'O' && line[1] == 'K'
&& (line[2] == '\0' || line[2] == ' ')) && (line[2] == '\0' || line[2] == ' '))
{ {
if (!current_datasink || current_datasink != stdout) if (!current_datasink || current_datasink != es_stdout)
{ {
fwrite (line, linelen, 1, stdout); fwrite (line, linelen, 1, stdout);
putchar ('\n'); putchar ('\n');
@ -2223,7 +2224,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr)
if (!errval) if (!errval)
errval = -1; errval = -1;
set_int_var ("?", errval); set_int_var ("?", errval);
if (!current_datasink || current_datasink != stdout) if (!current_datasink || current_datasink != es_stdout)
{ {
fwrite (line, linelen, 1, stdout); fwrite (line, linelen, 1, stdout);
putchar ('\n'); putchar ('\n');
@ -2237,7 +2238,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr)
&& line[6] == 'E' && line[6] == 'E'
&& (line[7] == '\0' || line[7] == ' ')) && (line[7] == '\0' || line[7] == ' '))
{ {
if (!current_datasink || current_datasink != stdout) if (!current_datasink || current_datasink != es_stdout)
{ {
fwrite (line, linelen, 1, stdout); fwrite (line, linelen, 1, stdout);
putchar ('\n'); putchar ('\n');
@ -2249,7 +2250,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr)
&& line[0] == 'E' && line[1] == 'N' && line[2] == 'D' && line[0] == 'E' && line[1] == 'N' && line[2] == 'D'
&& (line[3] == '\0' || line[3] == ' ')) && (line[3] == '\0' || line[3] == ' '))
{ {
if (!current_datasink || current_datasink != stdout) if (!current_datasink || current_datasink != es_stdout)
{ {
fwrite (line, linelen, 1, stdout); fwrite (line, linelen, 1, stdout);
putchar ('\n'); putchar ('\n');

View File

@ -912,7 +912,7 @@ ensure_policy_file (const char *addrspec)
{ {
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
if (gpg_err_code (err) == GPG_ERR_EEXIST) if (gpg_err_code (err) == GPG_ERR_EEXIST)
err = 0; /* Was created between the access() and fopen(). */ err = 0; /* Was created between the gnupg_access() and es_fopen(). */
else else
log_error ("domain %s: error creating '%s': %s\n", log_error ("domain %s: error creating '%s': %s\n",
domain, fname, gpg_strerror (err)); domain, fname, gpg_strerror (err));