summaryrefslogtreecommitdiffstats
path: root/lib/libfile.c
diff options
context:
space:
mode:
authorAndrey Smirnov <andrew.smirnov@gmail.com>2018-08-23 19:52:38 -0700
committerSascha Hauer <s.hauer@pengutronix.de>2018-08-31 08:30:08 +0200
commit38829761d78eafb25309b5b3ba5ce85d91bf5ba3 (patch)
tree35f5662a953786ceb91e34f2f13c80b28547664f /lib/libfile.c
parenta4fbb73e3b7933225287a136e19866e0580660c0 (diff)
downloadbarebox-38829761d78eafb25309b5b3ba5ce85d91bf5ba3.tar.gz
barebox-38829761d78eafb25309b5b3ba5ce85d91bf5ba3.tar.xz
libfile: Introduce pwrite_full()
Analogous to what we have with write()/write_full(), introduce a lightweight wrapper around pwrite() that guarantees the either all data is going to be written or a negative error code would be returned. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib/libfile.c')
-rw-r--r--lib/libfile.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/libfile.c b/lib/libfile.c
index 0052e789fc..39c85b2fc0 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -24,6 +24,30 @@
#include <linux/stat.h>
/*
+ * pwrite_full - write to filedescriptor at offset
+ *
+ * Like pwrite, but guarantees to write the full buffer out, else it
+ * returns with an error.
+ */
+int pwrite_full(int fd, const void *buf, size_t size, loff_t offset)
+{
+ size_t insize = size;
+ int now;
+
+ while (size) {
+ now = pwrite(fd, buf, size, offset);
+ if (now <= 0)
+ return now;
+ size -= now;
+ buf += now;
+ offset += now;
+ }
+
+ return insize;
+}
+EXPORT_SYMBOL(pwrite_full);
+
+/*
* write_full - write to filedescriptor
*
* Like write, but guarantees to write the full buffer out, else