diff options
author | Andrey Smirnov <andrew.smirnov@gmail.com> | 2018-08-23 19:52:38 -0700 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2018-08-31 08:30:08 +0200 |
commit | 38829761d78eafb25309b5b3ba5ce85d91bf5ba3 (patch) | |
tree | 35f5662a953786ceb91e34f2f13c80b28547664f /lib/libfile.c | |
parent | a4fbb73e3b7933225287a136e19866e0580660c0 (diff) | |
download | barebox-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.c | 24 |
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 |