From 8e149e4803919aedeb373a19b609f7e2eb78c473 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20Mi=C5=82ecki?= Date: Sat, 27 Jun 2020 20:38:17 +0200 Subject: [PATCH] lxlfw: move code copying data to helper function MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit It will be reused for inserting blobs. Signed-off-by: Rafał Miłecki --- src/lxlfw.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/lxlfw.c b/src/lxlfw.c index 1f5da46..a78690c 100644 --- a/src/lxlfw.c +++ b/src/lxlfw.c @@ -130,6 +130,44 @@ err_out: return NULL; } +/** + * lxlfw_copy_data - read data from one stream and write to another + * + * @from: input stream + * @to: output stream + * @size: amount of bytes to copy (0 to copy all data) + */ +static ssize_t lxlfw_copy_data(FILE *from, FILE *to, size_t size) +{ + int copy_all = size == 0; + char buf[512]; + size_t ret = 0; + + while (copy_all || size) { + size_t to_read = copy_all ? sizeof(buf) : min(size, sizeof(buf)); + size_t bytes; + + bytes = fread(buf, 1, to_read, from); + if (bytes == 0 && copy_all) { + break; + } else if (bytes <= 0) { + fprintf(stderr, "Failed to read data\n"); + return -EIO; + } + + if (fwrite(buf, 1, bytes, to) != bytes) { + fprintf(stderr, "Failed to write data\n"); + return -EIO; + } + + if (!copy_all) + size -= bytes; + ret += bytes; + } + + return ret; +} + /************************************************** * Info **************************************************/ @@ -269,12 +307,11 @@ static int lxlfw_create(int argc, char **argv) { goto err_close_lxl; } - while ((bytes = fread(buf, 1, sizeof(buf), in)) > 0) { - if (fwrite(buf, 1, bytes, lxl) != bytes) { - fprintf(stderr, "Could not copy %zu bytes from input file\n", bytes); - err = -EIO; - goto err_close_lxl; - } + bytes = lxlfw_copy_data(in, lxl, 0); + if (bytes < 0) { + fprintf(stderr, "Could not copy %zu bytes from input file\n", bytes); + err = -EIO; + goto err_close_lxl; } err_close_lxl: -- 2.30.2