From 697c5dc405035b4b1aecb6a44ae11252e934d3e5 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Fri, 19 Nov 2021 11:42:14 +0100 Subject: [PATCH] zdump: write_dump: use `FILE *` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the more common `FILE *` for writing to the output. Reviewed-by: Alexander Egorenkov Signed-off-by: Marc Hartmayer Signed-off-by: Jan Höppner --- zdump/output.c | 15 +++++---------- zdump/output.h | 4 +++- zdump/zgetdump.c | 19 +++++++++++++------ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/zdump/output.c b/zdump/output.c index d2b54436..0112c100 100644 --- a/zdump/output.c +++ b/zdump/output.c @@ -14,15 +14,12 @@ #include "dfo.h" #include "output.h" -int write_dump(int fd) +int write_dump(FILE *stream) { const u64 output_size = dfo_size(); char buf[8UL * PAGE_SIZE]; u64 written = 0; - if (fd < 0) - ERR_EXIT("fd must be a valid file descriptor"); - if (!dfi_feat_copy()) ERR_EXIT("Copying not possible for %s dumps", dfi_name()); STDERR("Format Info:\n"); @@ -31,15 +28,13 @@ int write_dump(int fd) STDERR("\n"); zg_progress_init("Copying dump", output_size); while (written != output_size) { - ssize_t rc; + size_t rc; u64 cnt; cnt = dfo_read(buf, sizeof(buf)); - rc = write(fd, buf, cnt); - if (rc == -1) - ERR_EXIT_ERRNO("Error: Write failed"); - if (rc != (ssize_t) cnt) - ERR_EXIT("Error: Could not write full block"); + rc = fwrite(buf, cnt, 1, stream); + if (rc != 1 && ferror(stream)) + ERR_EXIT("Error: Write failed"); written += cnt; zg_progress(written); }; diff --git a/zdump/output.h b/zdump/output.h index 054fe7be..a0eed0a7 100644 --- a/zdump/output.h +++ b/zdump/output.h @@ -8,6 +8,8 @@ #ifndef OUTPUT_H #define OUTPUT_H -int write_dump(int fd); +#include + +int write_dump(FILE *stream); #endif /* OUTPUT_H */ diff --git a/zdump/zgetdump.c b/zdump/zgetdump.c index 259ef0e9..6096afee 100644 --- a/zdump/zgetdump.c +++ b/zdump/zgetdump.c @@ -156,8 +156,9 @@ static int do_mount(void) return rc; } -static int open_file_for_writing(const char *output) +static FILE *open_file_for_writing(const char *output) { + FILE *stream; int fd; if (output) { @@ -172,7 +173,12 @@ static int open_file_for_writing(const char *output) ERR_EXIT_ERRNO("Could not dup() stdout"); } - return fd; + stream = fdopen(fd, "w"); + if (!stream) + ERR_EXIT_ERRNO("Could not fdopen()"); + fd = -1; + + return stream; } /* @@ -180,15 +186,16 @@ static int open_file_for_writing(const char *output) */ static int do_copy(const char *output) { - int rc, fd; + FILE *stream; + int rc; if (dfi_init() != 0) ERR_EXIT("Dump cannot be processed (is not complete)"); dfo_init(); kdump_select_check(); - fd = open_file_for_writing(output); - rc = write_dump(fd); - close(fd); + stream = open_file_for_writing(output); + rc = write_dump(stream); + fclose(stream); dfi_exit(); return rc; }