1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-03 22:56:33 +02: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

@ -71,14 +71,14 @@ fseeko (FILE * stream, off_t newpos, int whence)
static int
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;
err = keybox_tmp_names (template, 0, r_bakfname, r_tmpfname);
if (!err)
{
*r_fp = fopen (*r_tmpfname, "wb");
*r_fp = es_fopen (*r_tmpfname, "wb");
if (!*r_fp)
{
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)
{
gpg_err_code_t ec;
FILE *fp, *newfp;
int rc=0;
estream_t fp, newfp;
int rc = 0;
char *bakfname = NULL;
char *tmpfname = NULL;
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)))
return gpg_error (ec);
fp = fopen (fname, "rb");
fp = es_fopen (fname, "rb");
if (mode == FILECOPY_INSERT && !fp && errno == ENOENT)
{
/* Insert mode but file does not exist:
Create a new keybox file. */
newfp = fopen (fname, "wb");
newfp = es_fopen (fname, "wb");
if (!newfp )
return gpg_error_from_syserror ();
rc = _keybox_write_header_blob (newfp, NULL, for_openpgp);
rc = _keybox_write_header_blob (newfp, for_openpgp);
if (rc)
{
fclose (newfp);
es_fclose (newfp);
return rc;
}
rc = _keybox_write_blob (blob, newfp);
rc = _keybox_write_blob (blob, newfp, NULL);
if (rc)
{
fclose (newfp);
es_fclose (newfp);
return rc;
}
if ( fclose (newfp) )
if ( es_fclose (newfp) )
return gpg_error_from_syserror ();
/* 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);
if (rc)
{
fclose (fp);
es_fclose (fp);
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
make sure that the openpgp flag is set in the header. (We
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
&& 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. */
}
if (fwrite (buffer, nread, 1, newfp) != 1)
if (es_fwrite (buffer, nread, 1, newfp) != 1)
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
if (ferror (fp))
if (es_ferror (fp))
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
@ -267,24 +267,24 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
nbytes = DIM(buffer);
if (current + nbytes > start_offset)
nbytes = start_offset - current;
nread = fread (buffer, 1, nbytes, fp);
nread = es_fread (buffer, 1, nbytes, fp);
if (!nread)
break;
current += nread;
if (fwrite (buffer, nread, 1, newfp) != 1)
if (es_fwrite (buffer, nread, 1, newfp) != 1)
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
if (ferror (fp))
if (es_ferror (fp))
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
@ -292,8 +292,8 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
rc = _keybox_read_blob (NULL, fp, NULL);
if (rc)
{
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
return rc;
}
}
@ -301,11 +301,11 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
/* Do an insert or update. */
if ( mode == FILECOPY_INSERT || mode == FILECOPY_UPDATE )
{
rc = _keybox_write_blob (blob, newfp);
rc = _keybox_write_blob (blob, newfp, NULL);
if (rc)
{
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
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. */
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 ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
if (ferror (fp))
if (es_ferror (fp))
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
/* Close both files. */
if (fclose(fp))
if (es_fclose(fp))
{
rc = gpg_error_from_syserror ();
fclose (newfp);
es_fclose (newfp);
goto leave;
}
if (fclose(newfp))
if (es_fclose(newfp))
{
rc = gpg_error_from_syserror ();
goto leave;
@ -504,7 +504,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
{
off_t off;
const char *fname;
FILE *fp;
estream_t fp;
gpg_err_code_t ec;
size_t flag_pos, flag_size;
const unsigned char *buffer;
@ -536,12 +536,12 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
off += flag_pos;
_keybox_close_file (hd);
fp = fopen (hd->kb->fname, "r+b");
fp = es_fopen (hd->kb->fname, "r+b");
if (!fp)
return gpg_error_from_syserror ();
ec = 0;
if (fseeko (fp, off, SEEK_SET))
if (es_fseeko (fp, off, SEEK_SET))
ec = gpg_err_code_from_syserror ();
else
{
@ -557,7 +557,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
case 1:
case 2:
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 ();
break;
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)
ec = gpg_err_code_from_syserror ();
@ -582,7 +582,7 @@ keybox_delete (KEYBOX_HANDLE hd)
{
off_t off;
const char *fname;
FILE *fp;
estream_t fp;
int rc;
if (!hd)
@ -601,18 +601,18 @@ keybox_delete (KEYBOX_HANDLE hd)
off += 4;
_keybox_close_file (hd);
fp = fopen (hd->kb->fname, "r+b");
fp = es_fopen (hd->kb->fname, "r+b");
if (!fp)
return gpg_error_from_syserror ();
if (fseeko (fp, off, SEEK_SET))
if (es_fseeko (fp, off, SEEK_SET))
rc = gpg_error_from_syserror ();
else if (putc (0, fp) == EOF)
else if (es_fputc (0, fp) == EOF)
rc = gpg_error_from_syserror ();
else
rc = 0;
if (fclose (fp))
if (es_fclose (fp))
{
if (!rc)
rc = gpg_error_from_syserror ();
@ -630,7 +630,7 @@ keybox_compress (KEYBOX_HANDLE hd)
gpg_err_code_t ec;
int read_rc, rc;
const char *fname;
FILE *fp, *newfp;
estream_t fp, newfp;
char *bakfname = NULL;
char *tmpfname = NULL;
int first_blob;
@ -656,7 +656,7 @@ keybox_compress (KEYBOX_HANDLE hd)
if ((ec = gnupg_access (fname, W_OK)))
return gpg_error (ec);
fp = fopen (fname, "rb");
fp = es_fopen (fname, "rb");
if (!fp && errno == ENOENT)
return 0; /* Ready. File has been deleted right after the access above. */
if (!fp)
@ -679,21 +679,21 @@ keybox_compress (KEYBOX_HANDLE hd)
if ( (last_maint + 3*3600) > make_timestamp () )
{
fclose (fp);
es_fclose (fp);
_keybox_release_blob (blob);
return 0; /* Compress run not yet needed. */
}
}
_keybox_release_blob (blob);
fseek (fp, 0, SEEK_SET);
clearerr (fp);
es_fseek (fp, 0, SEEK_SET);
es_clearerr (fp);
}
/* Create the new file. */
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
if (rc)
{
fclose (fp);
es_fclose (fp);
return rc;;
}
@ -725,14 +725,14 @@ keybox_compress (KEYBOX_HANDLE hd)
stamp and if needed (ie. used by gpg) set the openpgp
flag. */
_keybox_update_header_blob (blob, hd->for_openpgp);
rc = _keybox_write_blob (blob, newfp);
rc = _keybox_write_blob (blob, newfp, NULL);
if (rc)
break;
continue;
}
/* 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)
break;
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)
break;
}
@ -782,9 +782,9 @@ keybox_compress (KEYBOX_HANDLE hd)
rc = read_rc;
/* Close both files. */
if (fclose(fp) && !rc)
if (es_fclose(fp) && !rc)
rc = gpg_error_from_syserror ();
if (fclose(newfp) && !rc)
if (es_fclose(newfp) && !rc)
rc = gpg_error_from_syserror ();
/* Rename or remove the temporary file. */