[PATCH v2 19/24] libfile: Introduce pwrite_full()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 include/libfile.h |  1 +
 lib/libfile.c     | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/include/libfile.h b/include/libfile.h
index 2c5eef71f..f1d695187 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 0052e789f..39c85b2fc 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -23,6 +23,30 @@
 #include <stdlib.h>
 #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
  *
-- 
2.17.1


_______________________________________________
barebox mailing list
barebox@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux