This commit is contained in:
Eric Andersen 2005-04-16 07:46:53 +00:00
parent 3cd1986195
commit 451c0d2828

View File

@ -2,7 +2,7 @@
/* /*
* Utility routines. * Utility routines.
* *
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -25,6 +25,7 @@
#include <unistd.h> #include <unistd.h>
#include "busybox.h" #include "busybox.h"
#include "libbb.h"
#if BUFSIZ < 4096 #if BUFSIZ < 4096
@ -33,46 +34,54 @@
#endif #endif
/* If size is 0 copy until EOF */ static size_t bb_full_fd_action(int src_fd, int dst_fd, const size_t size2)
static size_t bb_full_fd_action(int src_fd, int dst_fd, const size_t size)
{ {
size_t read_total = 0; int status;
size_t xread, wrote, total, size = size2;
if ((dst_fd < 0) || (src_fd < 0)) {
return -1;
}
if (size == 0) {
/* If size is 0 copy until EOF */
size = ULONG_MAX;
}
{
RESERVE_CONFIG_BUFFER(buffer,BUFSIZ); RESERVE_CONFIG_BUFFER(buffer,BUFSIZ);
total = 0;
while ((size == 0) || (read_total < size)) { wrote = 0;
size_t read_try; status = -1;
ssize_t read_actual; while (total < size)
{
if ((size == 0) || (size - read_total > BUFSIZ)) { xread = BUFSIZ;
read_try = BUFSIZ; if (size < (wrote + BUFSIZ))
} else { xread = size - wrote;
read_try = size - read_total; xread = bb_full_read(src_fd, buffer, xread);
if (xread > 0) {
wrote = bb_full_write(dst_fd, buffer, xread);
if (wrote < xread) {
bb_perror_msg(bb_msg_write_error);
break;
} }
total += wrote;
read_actual = safe_read(src_fd, buffer, read_try); } else if (xread < 0) {
if (read_actual > 0) { bb_perror_msg(bb_msg_read_error);
if ((dst_fd >= 0) && (bb_full_write(dst_fd, buffer, (size_t) read_actual) != read_actual)) { break;
bb_perror_msg(bb_msg_write_error); /* match Read error below */ } else if (xread == 0) {
/* All done. */
status = 0;
break; break;
} }
} }
else if (read_actual == 0) {
if (size) {
bb_error_msg("Unable to read all data");
}
break;
} else {
/* read_actual < 0 */
bb_perror_msg("Read error");
break;
}
read_total += read_actual;
}
RELEASE_CONFIG_BUFFER(buffer); RELEASE_CONFIG_BUFFER(buffer);
}
return(read_total); if (status == 0 || wrote)
return wrote;
/* Some sortof error occured */
return -1;
} }