]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: add sys_write_full()
authorRalph Boehme <slow@samba.org>
Sun, 24 Nov 2024 07:08:26 +0000 (08:08 +0100)
committerJeremy Allison <jra@samba.org>
Tue, 7 Jan 2025 22:04:33 +0000 (22:04 +0000)
Basically a copy of sys_pwrite_full(), calling write() instead of pwrite().

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/sys_rw.c
lib/util/sys_rw.h

index d25a42b7082c555b808685eac5393bc1511119c6..957eafb4aa869dc4d9d7e3987e65680cb60e0eae 100644 (file)
@@ -293,3 +293,41 @@ ssize_t sys_pwrite_full(int fd, const void *buf, size_t count, off_t off)
 
        return total_written;
 }
+
+/*******************************************************************
+ A write wrapper that will deal with EINTR and never allow a short
+ write unless the file system returns an error.
+********************************************************************/
+
+ssize_t sys_write_full(int fd, const void *buf, size_t count)
+{
+       ssize_t total_written = 0;
+       const uint8_t *curr_buf = (const uint8_t *)buf;
+       size_t curr_count = count;
+
+       while (curr_count != 0) {
+               ssize_t ret = sys_write(fd,
+                                       curr_buf,
+                                       curr_count);
+               if (ret == -1) {
+                       return -1;
+               }
+               if (ret == 0) {
+                       /* Ensure we can never spin. */
+                       errno = ENOSPC;
+                       return -1;
+               }
+
+               if (ret > curr_count) {
+                       errno = EIO;
+                       return -1;
+               }
+
+               curr_buf += ret;
+               curr_count -= ret;
+
+               total_written += ret;
+       }
+
+       return total_written;
+}
index 3812159a362215402c81db8a1e5f5fd23aac495f..f62d797ec774146b7aa470dbac34e72e0345ca52 100644 (file)
@@ -41,5 +41,6 @@ ssize_t sys_pread(int fd, void *buf, size_t count, off_t off);
 ssize_t sys_pread_full(int fd, void *buf, size_t count, off_t off);
 ssize_t sys_pwrite(int fd, const void *buf, size_t count, off_t off);
 ssize_t sys_pwrite_full(int fd, const void *buf, size_t count, off_t off);
+ssize_t sys_write_full(int fd, const void *buf, size_t count);
 
 #endif