aboutsummaryrefslogtreecommitdiff
path: root/libbb/copy_file_chunk.c
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 /libbb/copy_file_chunk.c
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.
Diffstat (limited to 'libbb/copy_file_chunk.c')
-rw-r--r--libbb/copy_file_chunk.c62
1 files changed, 35 insertions, 27 deletions
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*/