[PATCH 2/3] libfile: Add copy_fd()
Sascha Hauer
s.hauer at pengutronix.de
Tue Jun 22 21:33:58 PDT 2021
Signed-off-by: Sascha Hauer <s.hauer at 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
More information about the barebox
mailing list