diff options
author | David Robillard <d@drobilla.net> | 2021-10-09 13:44:31 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:07 -0500 |
commit | cbf01be4126cbc0f6d80364a7e0b6ad777a7d8ae (patch) | |
tree | 20cd2919e0d4c47caa346123c5701daa70a05ac3 /src | |
parent | 5ea7c0d763685c23dc6933e273dfa11eec5bec14 (diff) | |
download | serd-cbf01be4126cbc0f6d80364a7e0b6ad777a7d8ae.tar.gz serd-cbf01be4126cbc0f6d80364a7e0b6ad777a7d8ae.tar.bz2 serd-cbf01be4126cbc0f6d80364a7e0b6ad777a7d8ae.zip |
Fix handling of deferred write errors that happen when closing
Diffstat (limited to 'src')
-rw-r--r-- | src/output_stream.c | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/src/output_stream.c b/src/output_stream.c index b98c834a..54397735 100644 --- a/src/output_stream.c +++ b/src/output_stream.c @@ -21,6 +21,7 @@ // IWYU pragma: no_include <features.h> #include <assert.h> +#include <stdbool.h> #include <stddef.h> #include <stdio.h> @@ -30,12 +31,13 @@ SerdOutputStream serd_open_output_stream(SerdWriteFunc const write_func, + SerdErrorFunc const error_func, SerdCloseFunc const close_func, void* const stream) { assert(write_func); - SerdOutputStream output = {stream, write_func, close_func}; + SerdOutputStream output = {stream, write_func, error_func, close_func}; return output; } @@ -44,7 +46,8 @@ serd_open_output_buffer(SerdBuffer* const buffer) { assert(buffer); - return serd_open_output_stream(serd_buffer_write, serd_buffer_close, buffer); + return serd_open_output_stream( + serd_buffer_write, NULL, serd_buffer_close, buffer); } SerdOutputStream @@ -59,7 +62,7 @@ serd_open_output_file(const char* const path) #endif if (!file) { - const SerdOutputStream failure = {NULL, NULL, NULL}; + const SerdOutputStream failure = {NULL, NULL, NULL, NULL}; return failure; } @@ -68,22 +71,20 @@ serd_open_output_file(const char* const path) #endif return serd_open_output_stream( - (SerdWriteFunc)fwrite, (SerdCloseFunc)fclose, file); + (SerdWriteFunc)fwrite, (SerdErrorFunc)ferror, (SerdCloseFunc)fclose, file); } SerdStatus serd_close_output(SerdOutputStream* const output) { - int ret = 0; + if (!output || !output->stream) { + return SERD_FAILURE; + } - if (output) { - if (output->close && output->stream) { - ret = output->close(output->stream); - output->stream = NULL; - } + const bool had_error = output->error ? output->error(output->stream) : false; + int close_st = output->close ? output->close(output->stream) : 0; - output->stream = NULL; - } + output->stream = NULL; - return ret ? SERD_BAD_WRITE : SERD_SUCCESS; + return (had_error || close_st) ? SERD_BAD_WRITE : SERD_SUCCESS; } |