mirror of
https://github.com/troglobit/libite.git
synced 2026-11-17 19:30:17 +00:00
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 <troglobit@gmail.com>
This commit is contained in:
1 parent
dc90e93812
commit
7f9f49626a
2 files changed
+58
-20
No files matched your search
+28
-19
@@ -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:
|
||||
|
||||
+30
-1
@@ -1,5 +1,7 @@
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <sys/resource.h>
|
||||
#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();
|
||||
}
|
||||
Reference in new issue
Block a user