From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 2/3] libfile: Add copy_fd()
Date: Wed, 23 Jun 2021 06:33:58 +0200 [thread overview]
Message-ID: <20210623043359.18391-3-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20210623043359.18391-1-s.hauer@pengutronix.de>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
include/libfile.h | 1 +
lib/libfile.c | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/include/libfile.h b/include/libfile.h
index 350ddddf70..3c2fe1714d 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -5,6 +5,7 @@
int pwrite_full(int fd, const void *buf, size_t size, loff_t offset);
int write_full(int fd, const void *buf, size_t size);
int read_full(int fd, void *buf, size_t size);
+int copy_fd(int in, int out);
char *read_file_line(const char *fmt, ...);
diff --git a/lib/libfile.c b/lib/libfile.c
index 4ab8db11ad..e42126017d 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -100,6 +100,40 @@ int read_full(int fd, void *buf, size_t size)
}
EXPORT_SYMBOL(read_full);
+int copy_fd(int in, int out)
+{
+ int bs = 4096, ret;
+ void *buf = malloc(bs);
+
+ if (!buf)
+ return -ENOMEM;
+
+ while (1) {
+ int now, wr;
+
+ now = read(in, buf, bs);
+ if (now < 0) {
+ ret = now;
+ goto err;
+ }
+
+ if (!now)
+ break;
+
+ wr = write_full(out, buf, now);
+ if (wr < 0) {
+ ret = wr;
+ goto err;
+ }
+ }
+
+ ret = 0;
+err:
+ free(buf);
+
+ return ret;
+}
+
/*
* read_file_line - read a line from a file
*
--
2.29.2
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2021-06-23 4:35 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-23 4:33 [PATCH 0/3] firmware: Add support for compressed images Sascha Hauer
2021-06-23 4:33 ` [PATCH 1/3] filetype: Add function to check if a filetype is a compressed file Sascha Hauer
2021-06-23 4:33 ` Sascha Hauer [this message]
2021-06-23 6:24 ` [PATCH 2/3] libfile: Add copy_fd() Trent Piepho
2021-06-23 6:43 ` Sascha Hauer
2021-06-23 4:33 ` [PATCH 3/3] firmware: add support for compressed images Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210623043359.18391-3-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox