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

vcs-svn: read inline data from deltas

Each window of an svndiff0-format delta includes a section for novel text to be copied to the postimage (in the order it appears in the window, possibly interspersed with other data). Slurp in this data when encountering it. It is not actually necessary to do so --- it would be just as easy to copy from delta to output as part of interpreting the relevant instructions --- but this way, the code that interprets svndiff0 instructions can proceed very quickly because it does not require I/O. Subversion's svndiff0 parser rejects deltas that do not consume all the novel text that was provided. Omit that check for now so we can test the new functionality right away, rather than waiting to learn instructions that consume data. Do check for truncated data sections. Subversion's parser rejects deltas that end in the middle of a declared novel-text section, so it should be safe for us to reject them, too. Improved-by: Ramkumar Ramachandra <artagnon@gmail.com> Improved-by: David Barr <david.barr@cordelta.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Acked-by: Ramkumar Ramachandra <artagnon@gmail.com>

Jonathan Nieder committed Oct 13, 2010 at 04:35 UTC fc4ae43b2cbd53da6ac2a0047fb4e53175921696
2 files changed +47 -11
t/t9011-svn-da.sh
+12
@@ -121,4 +121,16 @@ test_expect_success 'preimage view: reject truncated preimage' '
121 test_must_fail test-svn-fe -d preimage clear.longread 9
122 '
123
124 +test_expect_success 'inline data' '
125 + printf "SVNQ%b%s%b%s" "QQQQ\003" "bar" "QQQQ\001" "x" |
126 + q_to_nul >inline.clear &&
127 + test-svn-fe -d preimage inline.clear 18 >actual &&
128 + test_cmp empty actual
129 +'
130 +
131 +test_expect_success 'reject truncated inline data' '
132 + printf "SVNQ%b%s" "QQQQ\003" "b" | q_to_nul >inline.trunc &&
133 + test_must_fail test-svn-fe -d preimage inline.trunc 10
134 +'
135 +
136 test_done
vcs-svn/svndiff.c
+35 -11
@@ -24,6 +24,17 @@
24 #define VLI_DIGIT_MASK 0x7f
25 #define VLI_BITS_PER_DIGIT 7
26
27 +struct window {
28 + struct strbuf data;
29 +};
30 +
31 +#define WINDOW_INIT { STRBUF_INIT }
32 +
33 +static void window_release(struct window *ctx)
34 +{
35 + strbuf_release(&ctx->data);
36 +}
37 +
38 static int error_short_read(struct line_buffer *input)
39 {
40 if (buffer_ferror(input))
@@ -31,24 +42,30 @@ static int error_short_read(struct line_buffer *input)
42 return error("invalid delta: unexpected end of file");
43 }
44
45 +static int read_chunk(struct line_buffer *delta, off_t *delta_len,
46 + struct strbuf *buf, size_t len)
47 +{
48 + strbuf_reset(buf);
49 + if (len > *delta_len ||
50 + buffer_read_binary(delta, buf, len) != len)
51 + return error_short_read(delta);
52 + *delta_len -= buf->len;
53 + return 0;
54 +}
55 +
56 static int read_magic(struct line_buffer *in, off_t *len)
57 {
58 static const char magic[] = {'S', 'V', 'N', '\0'};
59 struct strbuf sb = STRBUF_INIT;
60
39 - if (*len < sizeof(magic) ||
40 - buffer_read_binary(in, &sb, sizeof(magic)) != sizeof(magic)) {
41 - error_short_read(in);
61 + if (read_chunk(in, len, &sb, sizeof(magic))) {
62 strbuf_release(&sb);
63 return -1;
64 }
45 -
65 if (memcmp(sb.buf, magic, sizeof(magic))) {
66 strbuf_release(&sb);
67 return error("invalid delta: unrecognized file type");
68 }
50 -
51 - *len -= sizeof(magic);
69 strbuf_release(&sb);
70 return 0;
71 }
@@ -98,6 +115,7 @@ static int read_length(struct line_buffer *in, size_t *result, off_t *len)
115
116 static int apply_one_window(struct line_buffer *delta, off_t *delta_len)
117 {
118 + struct window ctx = WINDOW_INIT;
119 size_t out_len;
120 size_t instructions_len;
121 size_t data_len;
@@ -107,12 +125,18 @@ static int apply_one_window(struct line_buffer *delta, off_t *delta_len)
125 if (read_length(delta, &out_len, delta_len) ||
126 read_length(delta, &instructions_len, delta_len) ||
127 read_length(delta, &data_len, delta_len))
110 - return -1;
111 - if (instructions_len)
112 - return error("What do you think I am? A delta applier?");
113 - if (data_len)
114 - return error("No support for inline data yet");
128 + goto error_out;
129 + if (instructions_len) {
130 + error("What do you think I am? A delta applier?");
131 + goto error_out;
132 + }
133 + if (read_chunk(delta, delta_len, &ctx.data, data_len))
134 + goto error_out;
135 + window_release(&ctx);
136 return 0;
137 +error_out:
138 + window_release(&ctx);
139 + return -1;
140 }
141
142 int svndiff0_apply(struct line_buffer *delta, off_t delta_len,