@matheusbsbs / matheus-bagliano / commits / 26557fc1b3
Fork 0

vcs-svn: make buffer_copy_bytes return length read

Currently buffer_copy_bytes does not report to its caller whether it encountered an early end of file. Add a return value representing the number of bytes read (but not the number of bytes copied). This way all three unusual conditions can be distinguished: input error with buffer_ferror, output error with ferror(outfile), early end of input by checking the return value. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: David Barr <david.barr@cordelta.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jonathan Nieder committed Dec 28, 2010 at 04:26 UTC 26557fc1b37480d184a32de025b060aa1aa231db
2 files changed +11 -10
vcs-svn/line_buffer.c
+9 -9
@@ -104,20 +104,20 @@ void buffer_read_binary(struct line_buffer *buf,
104 strbuf_fread(sb, size, buf->infile);
105 }
106
107 -void buffer_copy_bytes(struct line_buffer *buf, off_t len)
107 +off_t buffer_copy_bytes(struct line_buffer *buf, off_t nbytes)
108 {
109 char byte_buffer[COPY_BUFFER_LEN];
110 - uint32_t in;
111 - while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
112 - in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
110 + off_t done = 0;
111 + while (done < nbytes && !feof(buf->infile) && !ferror(buf->infile)) {
112 + off_t len = nbytes - done;
113 + size_t in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
114 in = fread(byte_buffer, 1, in, buf->infile);
114 - len -= in;
115 + done += in;
116 fwrite(byte_buffer, 1, in, stdout);
116 - if (ferror(stdout)) {
117 - buffer_skip_bytes(buf, len);
118 - return;
119 - }
117 + if (ferror(stdout))
118 + return done + buffer_skip_bytes(buf, nbytes - done);
119 }
120 + return done;
121 }
122
123 off_t buffer_skip_bytes(struct line_buffer *buf, off_t nbytes)
vcs-svn/line_buffer.h
+2 -1
@@ -26,7 +26,8 @@ char *buffer_read_line(struct line_buffer *buf);
26 char *buffer_read_string(struct line_buffer *buf, uint32_t len);
27 int buffer_read_char(struct line_buffer *buf);
28 void buffer_read_binary(struct line_buffer *buf, struct strbuf *sb, uint32_t len);
29 -void buffer_copy_bytes(struct line_buffer *buf, off_t len);
29 +/* Returns number of bytes read (not necessarily written). */
30 +off_t buffer_copy_bytes(struct line_buffer *buf, off_t len);
31 off_t buffer_skip_bytes(struct line_buffer *buf, off_t len);
32
33 #endif