diff --git a/include/libfile.h b/include/libfile.h index 2c5eef7..f1d6951 100644 --- a/include/libfile.h +++ b/include/libfile.h @@ -1,6 +1,7 @@ #ifndef __LIBFILE_H #define __LIBFILE_H +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); diff --git a/lib/libfile.c b/lib/libfile.c index 0052e78..39c85b2 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -24,6 +24,30 @@ #include /* + * 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