As we no longer use function pointers for read in common archiving code

archive_xread can be replaced with bb_full_read, and archive_copy_file
with bb_copyfd*
bb_copyfd is split into two functions bb_copyfd_size and bb_copyfd_eof,
they share a common backend.
This commit is contained in:
Glenn L McGrath
2003-11-21 22:24:57 +00:00
parent 1a2d75fd72
commit 7ffe133864
18 changed files with 55 additions and 148 deletions

View File

@@ -181,7 +181,7 @@ int copy_file(const char *source, const char *dest, int flags)
}
}
if (bb_copyfd(fileno(sfp), fileno(dfp), 0) == -1)
if (bb_copyfd_eof(fileno(sfp), fileno(dfp)) == -1)
status = -1;
if (fclose(dfp) < 0) {

View File

@@ -29,44 +29,55 @@
#define BUFSIZ 4096
#endif
/* If chunksize is 0 copy until EOF */
extern int bb_copyfd(int fd1, int fd2, const off_t chunksize)
/* If size is 0 copy until EOF */
extern size_t bb_full_fd_action(int src_fd, int dst_fd, const size_t size, ssize_t (*action)(int fd, const void *, size_t))
{
ssize_t nread;
size_t size;
off_t remaining;
size_t read_total = 0;
RESERVE_CONFIG_BUFFER(buffer,BUFSIZ);
remaining = size = BUFSIZ;
if (chunksize) {
remaining = chunksize;
}
while ((size == 0) || (read_total < size)) {
size_t read_try;
ssize_t read_actual;
do {
if (size > remaining) {
size = remaining;
if ((size == 0) || (size - read_total > BUFSIZ)) {
read_try = BUFSIZ;
} else {
read_try = size - read_total;
}
if ((nread = safe_read(fd1, buffer, size)) > 0) {
if (bb_full_write(fd2, buffer, nread) < 0) {
read_actual = safe_read(src_fd, buffer, read_try);
if (read_actual > 0) {
if (action && (action(dst_fd, buffer, (size_t) read_actual) != read_actual)) {
bb_perror_msg(bb_msg_write_error); /* match Read error below */
break;
}
if (chunksize && ((remaining -= nread) == 0)) {
return 0;
}
} else if (!nread) {
if (chunksize) {
}
else if (read_actual == 0) {
if (size) {
bb_error_msg("Unable to read all data");
break;
}
return 0;
} else { /* nread < 0 */
bb_perror_msg("Read error"); /* match bb_msg_write_error above */
break;
} else {
/* read_actual < 0 */
bb_perror_msg("Read error");
break;
}
} while (1);
read_total += read_actual;
}
return -1;
RELEASE_CONFIG_BUFFER(buffer);
return(read_total);
}
extern int bb_copyfd_size(int fd1, int fd2, const off_t size)
{
return(bb_full_fd_action(fd1, fd2, size, bb_full_write));
}
extern int bb_copyfd_eof(int fd1, int fd2)
{
return(bb_full_fd_action(fd1, fd2, 0, bb_full_write));
}

View File

@@ -28,7 +28,7 @@ extern void bb_xprint_and_close_file(FILE *file)
bb_xfflush_stdout();
/* Note: Do not use STDOUT_FILENO here, as this is a lib routine
* and the calling code may have reassigned stdout. */
if (bb_copyfd(fileno(file), fileno(stdout), 0) == -1) {
if (bb_copyfd_eof(fileno(file), fileno(stdout)) == -1) {
/* bb_copyfd outputs any needed messages, so just die. */
exit(bb_default_error_retval);
}