From a6c4d6413ae0af3b3ed0d697618699233c8607cc Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 5 May 2023 11:37:44 +0200 Subject: kbx: Use wrapper functions for es_fclose and es_fopen. * kbx/keybox-defs.h (KEYBOX_LL_OPEN_READ) (KEYBOX_LL_OPEN_UPDATE, KEYBOX_LL_OPEN_CREATE): New. * kbx/keybox-init.c (_keybox_ll_open): New. Replace all keybox use of es_fopen by this function. (_keybox_ll_close): New. Replace all keybox use of es_fclose by this function. -- Note that this has not been done for the utilities and the backend-kbx of keyboxd. --- kbx/keybox-init.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'kbx/keybox-init.c') diff --git a/kbx/keybox-init.c b/kbx/keybox-init.c index 48af5c7a1..0e37e3d9e 100644 --- a/kbx/keybox-init.c +++ b/kbx/keybox-init.c @@ -180,7 +180,7 @@ keybox_release (KEYBOX_HANDLE hd) _keybox_release_blob (hd->saved_found.blob); if (hd->fp) { - es_fclose (hd->fp); + _keybox_ll_close (hd->fp); hd->fp = NULL; } xfree (hd->word_match.name); @@ -236,6 +236,47 @@ keybox_set_ephemeral (KEYBOX_HANDLE hd, int yes) } +/* Low-level open function to be used for keybox files. This function + * also manages custom buffering. On success 0 is returned and a new + * file pointer stored at RFP; on error an error code is returned and + * NULL is stored at RFP. MODE is one of + * KEYBOX_LL_OPEN_READ(0) := fopen mode is "rb" + * KEYBOX_LL_OPEN_UPDATE := fopen mode is "r+b" + * KEYBOX_LL_OPEN_CREATE := fopen mode is "wb" + */ +gpg_error_t +_keybox_ll_open (estream_t *rfp, const char *fname, unsigned int mode) +{ + estream_t fp; + + *rfp = NULL; + + fp = es_fopen (fname, + mode == KEYBOX_LL_OPEN_CREATE + ? "wb,sysopen,sequential" : + mode == KEYBOX_LL_OPEN_UPDATE + ? "r+b,sysopen,sequential" : + "rb,sysopen,sequential"); + if (!fp) + return gpg_error_from_syserror (); + + *rfp = fp; + return 0; +} + + +/* Wrapper around es_fclose to be used for file opened with + * _keybox_ll_open. */ +gpg_error_t +_keybox_ll_close (estream_t fp) +{ + if (fp && es_fclose (fp)) + return gpg_error_from_syserror (); + return 0; +} + + + /* Close the file of the resource identified by HD. For consistent results this function closes the files of all handles pointing to the resource identified by HD. */ @@ -253,7 +294,7 @@ _keybox_close_file (KEYBOX_HANDLE hd) { if (roverhd->fp) { - es_fclose (roverhd->fp); + _keybox_ll_close (roverhd->fp); roverhd->fp = NULL; } } -- cgit v1.2.3