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

vcs-svn: teach line_buffer about temporary files

It can sometimes be useful to write information temporarily to file, to read back later. These functions allow a program to use the line_buffer facilities when doing so. It works like this: 1. find a unique filename with buffer_tmpfile_init. 2. rewind with buffer_tmpfile_rewind. This returns a stdio handle for writing. 3. when finished writing, declare so with buffer_tmpfile_prepare_to_read. The return value indicates how many bytes were written. 4. read whatever portion of the file is needed. 5. if finished, remove the temporary file with buffer_deinit. otherwise, go back to step 2, The svn support would use this to buffer the postimage from delta application until the length is known and fast-import can receive the resulting blob. 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:10 UTC b1c9b798a6dd391aeaea31663a65164815701244
3 files changed +52 -1
vcs-svn/line_buffer.c
+24
@@ -25,6 +25,14 @@ int buffer_fdinit(struct line_buffer *buf, int fd)
25 return 0;
26 }
27
28 +int buffer_tmpfile_init(struct line_buffer *buf)
29 +{
30 + buf->infile = tmpfile();
31 + if (!buf->infile)
32 + return -1;
33 + return 0;
34 +}
35 +
36 int buffer_deinit(struct line_buffer *buf)
37 {
38 int err;
@@ -35,6 +43,22 @@ int buffer_deinit(struct line_buffer *buf)
43 return err;
44 }
45
46 +FILE *buffer_tmpfile_rewind(struct line_buffer *buf)
47 +{
48 + rewind(buf->infile);
49 + return buf->infile;
50 +}
51 +
52 +long buffer_tmpfile_prepare_to_read(struct line_buffer *buf)
53 +{
54 + long pos = ftell(buf->infile);
55 + if (pos < 0)
56 + return error("ftell error: %s", strerror(errno));
57 + if (fseek(buf->infile, 0, SEEK_SET))
58 + return error("seek error: %s", strerror(errno));
59 + return pos;
60 +}
61 +
62 int buffer_read_char(struct line_buffer *buf)
63 {
64 return fgetc(buf->infile);
vcs-svn/line_buffer.h
+6 -1
@@ -15,12 +15,17 @@ struct line_buffer {
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 +void buffer_reset(struct line_buffer *buf);
19 +
20 +int buffer_tmpfile_init(struct line_buffer *buf);
21 +FILE *buffer_tmpfile_rewind(struct line_buffer *buf); /* prepare to write. */
22 +long buffer_tmpfile_prepare_to_read(struct line_buffer *buf);
23 +
24 char *buffer_read_line(struct line_buffer *buf);
25 char *buffer_read_string(struct line_buffer *buf, uint32_t len);
26 int buffer_read_char(struct line_buffer *buf);
27 void buffer_read_binary(struct line_buffer *buf, struct strbuf *sb, uint32_t len);
28 void buffer_copy_bytes(struct line_buffer *buf, uint32_t len);
29 void buffer_skip_bytes(struct line_buffer *buf, uint32_t len);
24 -void buffer_reset(struct line_buffer *buf);
30
31 #endif
vcs-svn/line_buffer.txt
+22
@@ -24,6 +24,28 @@ The calling program:
24 When finished, the caller can use `buffer_reset` to deallocate
25 resources.
26
27 +Using temporary files
28 +---------------------
29 +
30 +Temporary files provide a place to store data that should not outlive
31 +the calling program. A program
32 +
33 + - initializes a `struct line_buffer` to LINE_BUFFER_INIT
34 + - requests a temporary file with `buffer_tmpfile_init`
35 + - acquires an output handle by calling `buffer_tmpfile_rewind`
36 + - uses standard I/O functions like `fprintf` and `fwrite` to fill
37 + the temporary file
38 + - declares writing is over with `buffer_tmpfile_prepare_to_read`
39 + - can re-read what was written with `buffer_read_line`,
40 + `buffer_read_string`, and so on
41 + - can reuse the temporary file by calling `buffer_tmpfile_rewind`
42 + again
43 + - removes the temporary file with `buffer_deinit`, perhaps to
44 + reuse the line_buffer for some other file.
45 +
46 +When finished, the calling program can use `buffer_reset` to deallocate
47 +resources.
48 +
49 Functions
50 ---------
51