From: Ralph Boehme Date: Sun, 24 Nov 2024 07:08:26 +0000 (+0100) Subject: lib: add sys_write_full() X-Git-Tag: tdb-1.4.13~221 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=127429a38ed4edb238e7d2a38dbae426e22751fe;p=thirdparty%2Fsamba.git lib: add sys_write_full() Basically a copy of sys_pwrite_full(), calling write() instead of pwrite(). Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison --- diff --git a/lib/util/sys_rw.c b/lib/util/sys_rw.c index d25a42b7082..957eafb4aa8 100644 --- a/lib/util/sys_rw.c +++ b/lib/util/sys_rw.c @@ -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; +} diff --git a/lib/util/sys_rw.h b/lib/util/sys_rw.h index 3812159a362..f62d797ec77 100644 --- a/lib/util/sys_rw.h +++ b/lib/util/sys_rw.h @@ -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