1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-06-15 00:29:49 +02:00

kbx: Improve keybox_set_flags

* kbx/keybox-defs.h (struct keybox_handle): Add update_mode flag.
* kbx/keybox-init.c (_keybox_close_file): Clear update_mode flag.
* kbx/keybox-update.c (keybox_set_flags): Avoid a second file handle
but make use of the update mode.
--

This is mostly useful for gpgsm because it updates some flags in the
keyring quite often.  Avoiding extra CreateFile calls may help to
speed up things in case malware^Dantivirus software is slowing down a
Windows system.

The locking pattern also change, thus there is some regression risk
due to this patch.
This commit is contained in:
Werner Koch 2023-05-04 11:47:01 +02:00
parent 2a1e933dd7
commit 30f1eda7d8
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
3 changed files with 21 additions and 5 deletions

View File

@ -77,6 +77,7 @@ struct keybox_handle {
KB_NAME kb;
int secret; /* this is for a secret keybox */
estream_t fp;
int update_mode; /* FP is in update mode ("r+b"). */
int eof;
int error;
int ephemeral;

View File

@ -255,6 +255,7 @@ _keybox_close_file (KEYBOX_HANDLE hd)
{
es_fclose (roverhd->fp);
roverhd->fp = NULL;
roverhd->update_mode = 0;
}
}
log_assert (!hd->fp);

View File

@ -509,6 +509,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
size_t flag_pos, flag_size;
const unsigned char *buffer;
size_t length;
gpgrt_off_t save_off;
(void)idx; /* Not yet used. */
@ -535,11 +536,18 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
off += flag_pos;
_keybox_close_file (hd);
fp = es_fopen (hd->kb->fname, "r+b");
if (!fp)
return gpg_error_from_syserror ();
if (!hd->fp || !hd->update_mode)
{
_keybox_close_file (hd);
fp = es_fopen (hd->kb->fname, "r+b");
if (!fp)
return gpg_error_from_syserror ();
hd->update_mode = 1;
}
else
fp = hd->fp;
save_off = es_ftello (fp);
ec = 0;
if (es_fseeko (fp, off, SEEK_SET))
ec = gpg_err_code_from_syserror ();
@ -566,12 +574,18 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
}
}
if (es_fclose (fp))
if (es_fflush (fp))
{
if (!ec)
ec = gpg_err_code_from_syserror ();
}
/* Get back to the last offset or close the file on error. */
if (save_off == (gpgrt_off_t)(-1) || es_fseeko (fp, save_off, SEEK_SET))
{
_keybox_close_file (hd);
}
return gpg_error (ec);
}