aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-11-21 22:24:57 +0000
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-11-21 22:24:57 +0000
commitd77eade56a2e7bf8bef3caafb69d2079c16b57d6 (patch)
treee949e5879a7083bac9e2da249363f9c613d0a409 /libbb
parent6ed9d590d5323fae0933b685181eb8dcd640844b (diff)
downloadbusybox-w32-d77eade56a2e7bf8bef3caafb69d2079c16b57d6.tar.gz
busybox-w32-d77eade56a2e7bf8bef3caafb69d2079c16b57d6.tar.bz2
busybox-w32-d77eade56a2e7bf8bef3caafb69d2079c16b57d6.zip
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. git-svn-id: svn://busybox.net/trunk/busybox@7984 69ca8d6d-28ef-0310-b511-8ec308f3f277
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)
181 } 181 }
182 } 182 }
183 183
184 if (bb_copyfd(fileno(sfp), fileno(dfp), 0) == -1) 184 if (bb_copyfd_eof(fileno(sfp), fileno(dfp)) == -1)
185 status = -1; 185 status = -1;
186 186
187 if (fclose(dfp) < 0) { 187 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 @@
29#define BUFSIZ 4096 29#define BUFSIZ 4096
30#endif 30#endif
31 31
32/* If chunksize is 0 copy until EOF */ 32/* If size is 0 copy until EOF */
33extern int bb_copyfd(int fd1, int fd2, const off_t chunksize) 33extern 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))
34{ 34{
35 ssize_t nread; 35 size_t read_total = 0;
36 size_t size;
37 off_t remaining;
38 RESERVE_CONFIG_BUFFER(buffer,BUFSIZ); 36 RESERVE_CONFIG_BUFFER(buffer,BUFSIZ);
39 37
40 remaining = size = BUFSIZ; 38 while ((size == 0) || (read_total < size)) {
41 if (chunksize) { 39 size_t read_try;
42 remaining = chunksize; 40 ssize_t read_actual;
43 }
44 41
45 do { 42 if ((size == 0) || (size - read_total > BUFSIZ)) {
46 if (size > remaining) { 43 read_try = BUFSIZ;
47 size = remaining; 44 } else {
45 read_try = size - read_total;
48 } 46 }
49 47
50 if ((nread = safe_read(fd1, buffer, size)) > 0) { 48 read_actual = safe_read(src_fd, buffer, read_try);
51 if (bb_full_write(fd2, buffer, nread) < 0) { 49 if (read_actual > 0) {
50 if (action && (action(dst_fd, buffer, (size_t) read_actual) != read_actual)) {
52 bb_perror_msg(bb_msg_write_error); /* match Read error below */ 51 bb_perror_msg(bb_msg_write_error); /* match Read error below */
53 break; 52 break;
54 } 53 }
55 if (chunksize && ((remaining -= nread) == 0)) { 54 }
56 return 0; 55 else if (read_actual == 0) {
57 } 56 if (size) {
58 } else if (!nread) {
59 if (chunksize) {
60 bb_error_msg("Unable to read all data"); 57 bb_error_msg("Unable to read all data");
61 break;
62 } 58 }
63 return 0; 59 break;
64 } else { /* nread < 0 */ 60 } else {
65 bb_perror_msg("Read error"); /* match bb_msg_write_error above */ 61 /* read_actual < 0 */
62 bb_perror_msg("Read error");
66 break; 63 break;
67 } 64 }
68 65
69 } while (1); 66 read_total += read_actual;
67 }
68
69 RELEASE_CONFIG_BUFFER(buffer);
70 70
71 return -1; 71 return(read_total);
72}
73
74
75extern int bb_copyfd_size(int fd1, int fd2, const off_t size)
76{
77 return(bb_full_fd_action(fd1, fd2, size, bb_full_write));
78}
79
80extern int bb_copyfd_eof(int fd1, int fd2)
81{
82 return(bb_full_fd_action(fd1, fd2, 0, bb_full_write));
72} 83}
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)
28 bb_xfflush_stdout(); 28 bb_xfflush_stdout();
29 /* Note: Do not use STDOUT_FILENO here, as this is a lib routine 29 /* Note: Do not use STDOUT_FILENO here, as this is a lib routine
30 * and the calling code may have reassigned stdout. */ 30 * and the calling code may have reassigned stdout. */
31 if (bb_copyfd(fileno(file), fileno(stdout), 0) == -1) { 31 if (bb_copyfd_eof(fileno(file), fileno(stdout)) == -1) {
32 /* bb_copyfd outputs any needed messages, so just die. */ 32 /* bb_copyfd outputs any needed messages, so just die. */
33 exit(bb_default_error_retval); 33 exit(bb_default_error_retval);
34 } 34 }