zdump: write_dump: use FILE *

Use the more common `FILE *` for writing to the output.

Reviewed-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Marc Hartmayer
2021-11-19 11:42:14 +01:00
committed by Jan Höppner
parent e9deec4c62
commit 697c5dc405
3 changed files with 21 additions and 17 deletions

View File

@@ -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);
};

View File

@@ -8,6 +8,8 @@
#ifndef OUTPUT_H
#define OUTPUT_H
int write_dump(int fd);
#include <stdio.h>
int write_dump(FILE *stream);
#endif /* OUTPUT_H */

View File

@@ -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;
}