aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-11-21 22:24:57 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-11-21 22:24:57 +0000
commit7ffe1338647d2b890df3ae46e526410b21a90d18 (patch)
treee949e5879a7083bac9e2da249363f9c613d0a409 /libbb
parent1a2d75fd7273e8de936e5fd91462bdff381f9b62 (diff)
downloadbusybox-7ffe1338647d2b890df3ae46e526410b21a90d18.tar.gz
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.
Diffstat (limited to 'libbb')
-rw-r--r--libbb/copy_file.c2
-rw-r--r--libbb/copyfd.c61
-rw-r--r--libbb/print_file.c2
3 files changed, 38 insertions, 27 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index 5808ca48c..c9d239f9a 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -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) {
diff --git a/libbb/copyfd.c b/libbb/copyfd.c
index 05bed6b73..0787c812f 100644
--- a/libbb/copyfd.c
+++ b/libbb/copyfd.c
@@ -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;
+ }
+
+ RELEASE_CONFIG_BUFFER(buffer);
- return -1;
+ 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));
}
diff --git a/libbb/print_file.c b/libbb/print_file.c
index 6d3667b60..161b398fa 100644
--- a/libbb/print_file.c
+++ b/libbb/print_file.c
@@ -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);
}