@matheusbsbs / matheus-bagliano / commits / cb3f87cf1b
Fork 0

vcs-svn: allow input from file descriptor

Based-on-patch-by: David Barr <david.barr@cordelta.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jonathan Nieder committed Jan 2, 2011 at 21:09 UTC cb3f87cf1ba90373fdc240d65a4d65434099d9a3
5 files changed +31 -7
t/t0081-line-buffer.sh
+9
@@ -131,6 +131,15 @@ test_expect_success PIPE,EXPENSIVE 'longer read (around 65536 bytes)' '
131 long_read_test 65536
132 '
133
134 +test_expect_success 'read from file descriptor' '
135 + rm -f input &&
136 + echo hello >expect &&
137 + echo hello >input &&
138 + echo copy 6 |
139 + test-line-buffer "&4" 4<input >actual &&
140 + test_cmp expect actual
141 +'
142 +
143 test_expect_success 'buffer_read_string copes with null byte' '
144 >expect &&
145 q_to_nul <<-\EOF | test-line-buffer >actual &&
test-line-buffer.c
+8 -3
@@ -69,13 +69,18 @@ int main(int argc, char *argv[])
69 else if (argc == 2)
70 filename = argv[1];
71 else
72 - usage("test-line-buffer [file] < script");
72 + usage("test-line-buffer [file | &fd] < script");
73
74 if (buffer_init(&stdin_buf, NULL))
75 die_errno("open error");
76 if (filename) {
77 - if (buffer_init(&file_buf, filename))
78 - die_errno("error opening %s", filename);
77 + if (*filename == '&') {
78 + if (buffer_fdinit(&file_buf, strtouint32(filename + 1)))
79 + die_errno("error opening fd %s", filename + 1);
80 + } else {
81 + if (buffer_init(&file_buf, filename))
82 + die_errno("error opening %s", filename);
83 + }
84 input = &file_buf;
85 }
86
vcs-svn/line_buffer.c
+8
@@ -17,6 +17,14 @@ int buffer_init(struct line_buffer *buf, const char *filename)
17 return 0;
18 }
19
20 +int buffer_fdinit(struct line_buffer *buf, int fd)
21 +{
22 + buf->infile = fdopen(fd, "r");
23 + if (!buf->infile)
24 + return -1;
25 + return 0;
26 +}
27 +
28 int buffer_deinit(struct line_buffer *buf)
29 {
30 int err;
vcs-svn/line_buffer.h
+1
@@ -13,6 +13,7 @@ struct line_buffer {
13 #define LINE_BUFFER_INIT {"", STRBUF_INIT, NULL}
14
15 int buffer_init(struct line_buffer *buf, const char *filename);
16 +int buffer_fdinit(struct line_buffer *buf, int fd);
17 int buffer_deinit(struct line_buffer *buf);
18 char *buffer_read_line(struct line_buffer *buf);
19 char *buffer_read_string(struct line_buffer *buf, uint32_t len);
vcs-svn/line_buffer.txt
+5 -4
@@ -27,10 +27,11 @@ resources.
27 Functions
28 ---------
29
30 -`buffer_init`::
31 - Open the named file for input. If filename is NULL,
32 - start reading from stdin. On failure, returns -1 (with
33 - errno indicating the nature of the failure).
30 +`buffer_init`, `buffer_fdinit`::
31 + Open the named file or file descriptor for input.
32 + buffer_init(buf, NULL) prepares to read from stdin.
33 + On failure, returns -1 (with errno indicating the nature
34 + of the failure).
35
36 `buffer_deinit`::
37 Stop reading from the current file (closing it unless