From 7f9f49626a3fbc5e7271acfadc7f7994a3459f5b Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Mon, 16 Nov 2026 14:22:40 +0100 Subject: [PATCH] Report read and write errors from copyfile() copyfile() returns a byte count, with errno cleared, when the copy fails after the first block. movefile() across file systems then removes the source and leaves a truncated copy. The copy loop adds the result of write() to the total without checking it, takes a failed read() for end of file, and the result of close() on the destination is dropped. Return -1 with errno set on any of them, and retry short writes. Signed-off-by: Joachim Wiberg --- src/copyfile.c | 47 ++++++++++++++++++++++++++++------------------- test/copyfile.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/src/copyfile.c b/src/copyfile.c index 593f1a9..5291d31 100644 --- a/src/copyfile.c +++ b/src/copyfile.c @@ -71,33 +71,38 @@ static int adjust_target(const char *src, char **dst) return isdir; } -/* - * Actual copy loop, used by both copyfile() and fcopyfile() breaks loop - * on error and EOF - */ +/* Copy up to num bytes, stops at EOF. Returns bytes copied, or -1 */ static ssize_t do_copy(int in, int out, size_t num, char *buffer, size_t len) { - ssize_t ret = 0, size = -1; + ssize_t size = 0; - do { + while (num > 0) { size_t count = num > len ? len : num; + ssize_t ret, off = 0; ret = read(in, buffer, count); - if (ret <= 0) { - if (ret == -1 && EINTR == errno) + if (ret == -1) { + if (errno == EINTR) continue; - if (ret == 0 && size == -1) - size = 0; + return -1; + } + if (ret == 0) break; + + while (off < ret) { + ssize_t n = write(out, buffer + off, ret - off); + + if (n == -1) { + if (errno == EINTR) + continue; + return -1; + } + off += n; } - if (ret > 0) { - if (size == -1) - size = 0; - size += write(out, buffer, ret); - } - num -= count; - } while (num > 0); + size += ret; + num -= ret; + } return size; } @@ -137,7 +142,8 @@ static void set_mtime(int in, int out) * * @returns The number of bytes copied, or zero, which may be an error * (check @a errno, see Exceptions below), but it may also indicate that - * @a src was empty. See exceptions, below. + * @a src was empty. If reading or writing fails -1 is returned, with + * @a errno set. See exceptions, below. * * @exception EISDIR if @a src is a directory, since copyfile() is not recursive. */ @@ -230,7 +236,10 @@ ssize_t copyfile(const char *src, const char *dst, int len, int opt) set_mtime(in, out); close_exit: - close(out); + if (close(out) && size >= 0) { + saved_errno = errno; + size = -1; + } close(in); exit: diff --git a/test/copyfile.c b/test/copyfile.c index b09f177..b9ef80f 100644 --- a/test/copyfile.c +++ b/test/copyfile.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include "check.h" #define ERR(fmt, args...) { printf(fmt ": %s\n", ##args, strerror(errno)); rc = 1; } @@ -145,10 +147,37 @@ static int _mode(void) return test(rc, "copyfile() preserves mode 0644 with umask 0077"); } +/* A write error halfway through is an error, not a shorter copy */ +static int _werr(void) +{ + const char *dst = "/tmp/libite-werr"; + struct rlimit old, lim; + ssize_t len; + int rc; + + signal(SIGXFSZ, SIG_IGN); + if (getrlimit(RLIMIT_FSIZE, &old)) + return test(1, "copyfile() write error, getrlimit()"); + + lim = old; + lim.rlim_cur = BUFSIZ + BUFSIZ / 2; + if (setrlimit(RLIMIT_FSIZE, &lim)) + return test(1, "copyfile() write error, setrlimit()"); + + len = copyfile("/dev/zero", dst, 4 * BUFSIZ, 0); + rc = len != -1 || errno != EFBIG; + + setrlimit(RLIMIT_FSIZE, &old); + erase(dst); + + return test(rc, "copyfile() reports write error"); +} + int main(void) { return _fcopyfile() || _copyfile() || _sz() || - _mode(); + _mode() || + _werr(); }