7
#include "strbuf.h"
8
#include "quote.h"
9
#include "fast_export.h"
10
-#include "line_buffer.h"
10
#include "repo_tree.h"
11
#include "strbuf.h"
12
+#include "svndiff.h"
13
+#include "sliding_window.h"
14
+#include "line_buffer.h"
15
16
#define MAX_GITSVN_LINE_LEN 4096
17
+#define REPORT_FILENO 3
18
19
static uint32_t first_commit_done;
20
+static struct line_buffer postimage = LINE_BUFFER_INIT;
21
static struct line_buffer report_buffer = LINE_BUFFER_INIT;
22
23
+/* NEEDSWORK: move to fast_export_init() */
24
+static int init_postimage(void)
25
+{
26
+ static int postimage_initialized;
27
+ if (postimage_initialized)
28
+ return 0;
29
+ postimage_initialized = 1;
30
+ return buffer_tmpfile_init(&postimage);
31
+}
32
+
33
+static int init_report_buffer(int fd)
34
+{
35
+ static int report_buffer_initialized;
36
+ if (report_buffer_initialized)
37
+ return 0;
38
+ report_buffer_initialized = 1;
39
+ return buffer_fdinit(&report_buffer, fd);
40
+}
41
+
42
void fast_export_init(int fd)
43
{
44
if (buffer_fdinit(&report_buffer, fd))
155
die("invalid dump: unexpected end of file");
156
}
157
158
+static int ends_with(const char *s, size_t len, const char *suffix)
159
+{
160
+ const size_t suffixlen = strlen(suffix);
161
+ if (len < suffixlen)
162
+ return 0;
163
+ return !memcmp(s + len - suffixlen, suffix, suffixlen);
164
+}
165
+
166
+static int parse_cat_response_line(const char *header, off_t *len)
167
+{
168
+ size_t headerlen = strlen(header);
169
+ const char *type;
170
+ const char *end;
171
+
172
+ if (ends_with(header, headerlen, " missing"))
173
+ return error("cat-blob reports missing blob: %s", header);
174
+ type = memmem(header, headerlen, " blob ", strlen(" blob "));
175
+ if (!type)
176
+ return error("cat-blob header has wrong object type: %s", header);
177
+ *len = strtoumax(type + strlen(" blob "), (char **) &end, 10);
178
+ if (end == type + strlen(" blob "))
179
+ return error("cat-blob header does not contain length: %s", header);
180
+ if (*end)
181
+ return error("cat-blob header contains garbage after length: %s", header);
182
+ return 0;
183
+}
184
+
185
+static long apply_delta(off_t len, struct line_buffer *input,
186
+ const char *old_data, uint32_t old_mode)
187
+{
188
+ long ret;
189
+ off_t preimage_len = 0;
190
+ struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer);
191
+ FILE *out;
192
+
193
+ if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
194
+ die("cannot open temporary file for blob retrieval");
195
+ if (init_report_buffer(REPORT_FILENO))
196
+ die("cannot open fd 3 for feedback from fast-import");
197
+ if (old_data) {
198
+ const char *response;
199
+ printf("cat-blob %s\n", old_data);
200
+ fflush(stdout);
201
+ response = get_response_line();
202
+ if (parse_cat_response_line(response, &preimage_len))
203
+ die("invalid cat-blob response: %s", response);
204
+ }
205
+ if (old_mode == REPO_MODE_LNK) {
206
+ strbuf_addstr(&preimage.buf, "link ");
207
+ preimage_len += strlen("link ");
208
+ }
209
+ if (svndiff0_apply(input, len, &preimage, out))
210
+ die("cannot apply delta");
211
+ if (old_data) {
212
+ /* Read the remainder of preimage and trailing newline. */
213
+ if (move_window(&preimage, preimage_len, 1))
214
+ die("cannot seek to end of input");
215
+ if (preimage.buf.buf[0] != '\n')
216
+ die("missing newline after cat-blob response");
217
+ }
218
+ ret = buffer_tmpfile_prepare_to_read(&postimage);
219
+ if (ret < 0)
220
+ die("cannot read temporary file for blob retrieval");
221
+ strbuf_release(&preimage.buf);
222
+ return ret;
223
+}
224
+
225
void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
226
{
227
if (mode == REPO_MODE_LNK) {
289
ls_from_active_commit(path);
290
return parse_ls_response(get_response_line(), mode, dataref);
291
}
292
+
293
+void fast_export_blob_delta(uint32_t mode,
294
+ uint32_t old_mode, const char *old_data,
295
+ uint32_t len, struct line_buffer *input)
296
+{
297
+ long postimage_len;
298
+ if (len > maximum_signed_value_of_type(off_t))
299
+ die("enormous delta");
300
+ postimage_len = apply_delta((off_t) len, input, old_data, old_mode);
301
+ if (mode == REPO_MODE_LNK) {
302
+ buffer_skip_bytes(&postimage, strlen("link "));
303
+ postimage_len -= strlen("link ");
304
+ }
305
+ printf("data %ld\n", postimage_len);
306
+ buffer_copy_bytes(&postimage, postimage_len);
307
+ fputc('\n', stdout);
308
+}