aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2001-06-11 13:58:02 +0000
committerMatt Kraai <kraai@debian.org>2001-06-11 13:58:02 +0000
commitbf0a010cf705d21c75d2c6ba8de38cec038f9aa1 (patch)
tree5a059e12e61728a72d4ec58166ec63794d944ed3
parent5246225596a45de63a994abbef92b7d23180681c (diff)
downloadbusybox-w32-bf0a010cf705d21c75d2c6ba8de38cec038f9aa1.tar.gz
busybox-w32-bf0a010cf705d21c75d2c6ba8de38cec038f9aa1.tar.bz2
busybox-w32-bf0a010cf705d21c75d2c6ba8de38cec038f9aa1.zip
Copy files until EOF, not the reported file size, to deal with bad sizes in
the proc filesystem.
-rw-r--r--libbb/copy_file.c7
-rw-r--r--libbb/copy_file_chunk.c62
2 files changed, 39 insertions, 30 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index 22684be74..24bdf9002 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -94,7 +94,7 @@ int copy_file(const char *source, const char *dest, int flags)
94 94
95 umask(saved_umask); 95 umask(saved_umask);
96 } 96 }
97 97
98 /* Recursively copy files in SOURCE. */ 98 /* Recursively copy files in SOURCE. */
99 if ((dp = opendir(source)) == NULL) { 99 if ((dp = opendir(source)) == NULL) {
100 perror_msg("unable to open directory `%s'", source); 100 perror_msg("unable to open directory `%s'", source);
@@ -116,7 +116,7 @@ int copy_file(const char *source, const char *dest, int flags)
116 free(new_source); 116 free(new_source);
117 free(new_dest); 117 free(new_dest);
118 } 118 }
119 119
120 /* ??? What if an error occurs in readdir? */ 120 /* ??? What if an error occurs in readdir? */
121 121
122 if (closedir(dp) < 0) { 122 if (closedir(dp) < 0) {
@@ -173,7 +173,8 @@ int copy_file(const char *source, const char *dest, int flags)
173 goto end; 173 goto end;
174 } 174 }
175 175
176 copy_file_chunk(sfp, dfp, source_stat.st_size); 176 if (copy_file_chunk(sfp, dfp, -1) < 0)
177 status = -1;
177 178
178 if (fclose(dfp) < 0) { 179 if (fclose(dfp) < 0) {
179 perror_msg("unable to close `%s'", dest); 180 perror_msg("unable to close `%s'", dest);
diff --git a/libbb/copy_file_chunk.c b/libbb/copy_file_chunk.c
index e9663c354..c440a6102 100644
--- a/libbb/copy_file_chunk.c
+++ b/libbb/copy_file_chunk.c
@@ -29,38 +29,46 @@
29#include <sys/stat.h> 29#include <sys/stat.h>
30#include "libbb.h" 30#include "libbb.h"
31 31
32/* 32/* Copy CHUNKSIZE bytes (or until EOF if CHUNKSIZE equals -1) from SRC_FILE
33 * Copy chunksize bytes between two file descriptors 33 * to DST_FILE. */
34 *
35 * unsigned long is used so that if -1 is passed as chunksize it will read as
36 * much as possible, and it will work with off_t or off64_t
37 */
38extern int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize) 34extern int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize)
39{ 35{
40 off_t size, amount_written; 36 size_t nread, nwritten, size;
41 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */ 37 char buffer[BUFSIZ];
42 38
43 while (chunksize > 0) { 39 while (chunksize != 0) {
44 if (chunksize > BUFSIZ) { 40 if (chunksize > BUFSIZ)
45 size = BUFSIZ; 41 size = BUFSIZ;
46 } else { 42 else
47 size = chunksize; 43 size = chunksize;
44
45 nread = fread (buffer, 1, size, src_file);
46
47 if (nread != size && ferror (src_file)) {
48 perror_msg ("read");
49 return -1;
50 } else if (nread == 0) {
51 if (chunksize != -1) {
52 error_msg ("Unable to read all data");
53 return -1;
54 }
55
56 return 0;
48 } 57 }
49 amount_written = fwrite(buffer, 1, fread(buffer, 1, size, src_file), dst_file); 58
50 if (amount_written != size) { 59 nwritten = fwrite (buffer, 1, nread, dst_file);
51 error_msg("Couldnt write correct amount"); 60
52 return(FALSE); 61 if (nwritten != nread) {
62 if (ferror (dst_file))
63 perror_msg ("write");
64 else
65 error_msg ("Unable to write all data");
66 return -1;
53 } 67 }
54 chunksize -= amount_written; 68
69 if (chunksize != -1)
70 chunksize -= nwritten;
55 } 71 }
56 return (TRUE);
57}
58 72
59/* END CODE */ 73 return 0;
60/* 74}
61Local Variables:
62c-file-style: "linux"
63c-basic-offset: 4
64tab-width: 4
65End:
66*/