From 714701c890b5f03253c5ecdb7367c4258ce78715 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Fri, 22 Dec 2006 00:21:07 +0000 Subject: tar et al: die if bb_copyfd_size copies less than asked for. (we have bb_copyfd_exact_size now for that kind of usage) --- libbb/copyfd.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'libbb/copyfd.c') diff --git a/libbb/copyfd.c b/libbb/copyfd.c index c6b886647..17bf4fbc2 100644 --- a/libbb/copyfd.c +++ b/libbb/copyfd.c @@ -39,7 +39,7 @@ static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size) rd = safe_read(src_fd, buffer, size > BUFSIZ ? BUFSIZ : size); - if (!rd) { /* eof - all done. */ + if (!rd) { /* eof - all done */ status = 0; break; } @@ -56,22 +56,31 @@ static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size) } } total += rd; - if (status < 0) { + if (status < 0) { /* if we aren't copying till EOF... */ size -= rd; if (!size) { - status = 0; + /* 'size' bytes copied - all done */ + status = 0; break; } } } - -out: + out: RELEASE_CONFIG_BUFFER(buffer); - return status ? -1 : total; } +#if 0 +void complain_copyfd_and_die(off_t sz) +{ + if (sz != -1) + bb_error_msg_and_die("short read"); + /* if sz == -1, bb_copyfd_XX already complained */ + exit(xfunc_error_retval); +} +#endif + off_t bb_copyfd_size(int fd1, int fd2, off_t size) { if (size) { @@ -80,6 +89,17 @@ off_t bb_copyfd_size(int fd1, int fd2, off_t size) return 0; } +void bb_copyfd_exact_size(int fd1, int fd2, off_t size) +{ + off_t sz = bb_copyfd_size(fd1, fd2, size); + if (sz == size) + return; + if (sz != -1) + bb_error_msg_and_die("short read"); + /* if sz == -1, bb_copyfd_XX already complained */ + exit(xfunc_error_retval); +} + off_t bb_copyfd_eof(int fd1, int fd2) { return bb_full_fd_action(fd1, fd2, 0); -- cgit v1.2.3