Skip to content

Commit b1c9b79

Browse files
committed
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 <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]>
1 parent cb3f87c commit b1c9b79

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

vcs-svn/line_buffer.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ int buffer_fdinit(struct line_buffer *buf, int fd)
2525
return 0;
2626
}
2727

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+
2836
int buffer_deinit(struct line_buffer *buf)
2937
{
3038
int err;
@@ -35,6 +43,22 @@ int buffer_deinit(struct line_buffer *buf)
3543
return err;
3644
}
3745

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+
3862
int buffer_read_char(struct line_buffer *buf)
3963
{
4064
return fgetc(buf->infile);

vcs-svn/line_buffer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ struct line_buffer {
1515
int buffer_init(struct line_buffer *buf, const char *filename);
1616
int buffer_fdinit(struct line_buffer *buf, int fd);
1717
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+
1824
char *buffer_read_line(struct line_buffer *buf);
1925
char *buffer_read_string(struct line_buffer *buf, uint32_t len);
2026
int buffer_read_char(struct line_buffer *buf);
2127
void buffer_read_binary(struct line_buffer *buf, struct strbuf *sb, uint32_t len);
2228
void buffer_copy_bytes(struct line_buffer *buf, uint32_t len);
2329
void buffer_skip_bytes(struct line_buffer *buf, uint32_t len);
24-
void buffer_reset(struct line_buffer *buf);
2530

2631
#endif

vcs-svn/line_buffer.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ The calling program:
2424
When finished, the caller can use `buffer_reset` to deallocate
2525
resources.
2626

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+
2749
Functions
2850
---------
2951

0 commit comments

Comments
 (0)