diff options
-rw-r--r-- | WHATS_NEW_DM | 1 | ||||
-rw-r--r-- | libdm/.exported_symbols | 1 | ||||
-rw-r--r-- | libdm/libdevmapper.h | 16 | ||||
-rw-r--r-- | libdm/libdm-file.c | 13 |
4 files changed, 31 insertions, 0 deletions
diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM index 78584cba..d5c294f4 100644 --- a/WHATS_NEW_DM +++ b/WHATS_NEW_DM @@ -1,5 +1,6 @@ Version 1.02.22 - ================================ + dm_fclose: new function Version 1.02.21 - 13th July 2007 ================================ diff --git a/libdm/.exported_symbols b/libdm/.exported_symbols index aafb0a09..dec701a6 100644 --- a/libdm/.exported_symbols +++ b/libdm/.exported_symbols @@ -1,6 +1,7 @@ dm_lib_release dm_lib_exit dm_driver_version +dm_fclose dm_get_library_version dm_log dm_log_init diff --git a/libdm/libdevmapper.h b/libdm/libdevmapper.h index 8711ee0a..d9b06d3b 100644 --- a/libdm/libdevmapper.h +++ b/libdm/libdevmapper.h @@ -27,6 +27,7 @@ #include <limits.h> #include <string.h> #include <stdlib.h> +#include <stdio.h> /***************************************************************** * The first section of this file provides direct access to the @@ -623,6 +624,21 @@ int dm_snprintf(char *buf, size_t bufsize, const char *format, ...); */ char *dm_basename(const char *path); +/************************** + * file/stream manipulation + **************************/ + +/* + * Close a stream, with nicer error checking than fclose's. + * Derived from gnulib's close-stream.c. + * + * Close "stream". Return 0 if successful, and EOF (setting errno) + * otherwise. Upon failure, set errno to 0 if the error number + * cannot be determined. Useful mainly for writable streams. + */ +int dm_fclose(FILE *stream); + + /* * Returns size of a buffer which is allocated with dm_malloc. * Pointer to the buffer is stored in *buf. diff --git a/libdm/libdm-file.c b/libdm/libdm-file.c index 54c38b4e..2f16c80c 100644 --- a/libdm/libdm-file.c +++ b/libdm/libdm-file.c @@ -72,3 +72,16 @@ int create_dir(const char *dir) return 0; } +int dm_fclose(FILE *stream) +{ + int prev_fail = ferror(stream); + int fclose_fail = fclose(stream); + + /* If there was a previous failure, but fclose succeeded, + clear errno, since ferror does not set it, and its value + may be unrelated to the ferror-reported failure. */ + if (prev_fail && !fclose_fail) + errno = 0; + + return prev_fail || fclose_fail ? EOF : 0; +} |