3efde20a0cac6309e51a51745be8919ee63dccde
1/*
2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
4 */
5
6#include "git-compat-util.h"
7#include "strbuf.h"
8#include "quote.h"
9#include "fast_export.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
18static uint32_t first_commit_done;
19static struct line_buffer postimage = LINE_BUFFER_INIT;
20static struct line_buffer report_buffer = LINE_BUFFER_INIT;
21
22/* NEEDSWORK: move to fast_export_init() */
23static int init_postimage(void)
24{
25 static int postimage_initialized;
26 if (postimage_initialized)
27 return 0;
28 postimage_initialized = 1;
29 return buffer_tmpfile_init(&postimage);
30}
31
32void fast_export_init(int fd)
33{
34 if (buffer_fdinit(&report_buffer, fd))
35 die_errno("cannot read from file descriptor %d", fd);
36}
37
38void fast_export_deinit(void)
39{
40 if (buffer_deinit(&report_buffer))
41 die_errno("error closing fast-import feedback stream");
42}
43
44void fast_export_reset(void)
45{
46 buffer_reset(&report_buffer);
47}
48
49void fast_export_delete(const char *path)
50{
51 putchar('D');
52 putchar(' ');
53 quote_c_style(path, NULL, stdout, 0);
54 putchar('\n');
55}
56
57static void fast_export_truncate(const char *path, uint32_t mode)
58{
59 fast_export_modify(path, mode, "inline");
60 printf("data 0\n\n");
61}
62
63void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
64{
65 /* Mode must be 100644, 100755, 120000, or 160000. */
66 if (!dataref) {
67 fast_export_truncate(path, mode);
68 return;
69 }
70 printf("M %06"PRIo32" %s ", mode, dataref);
71 quote_c_style(path, NULL, stdout, 0);
72 putchar('\n');
73}
74
75static char gitsvnline[MAX_GITSVN_LINE_LEN];
76void fast_export_begin_commit(uint32_t revision, const char *author,
77 const struct strbuf *log,
78 const char *uuid, const char *url,
79 unsigned long timestamp)
80{
81 static const struct strbuf empty = STRBUF_INIT;
82 if (!log)
83 log = ∅
84 if (*uuid && *url) {
85 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
86 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
87 url, revision, uuid);
88 } else {
89 *gitsvnline = '\0';
90 }
91 printf("commit refs/heads/master\n");
92 printf("mark :%"PRIu32"\n", revision);
93 printf("committer %s <%s@%s> %ld +0000\n",
94 *author ? author : "nobody",
95 *author ? author : "nobody",
96 *uuid ? uuid : "local", timestamp);
97 printf("data %"PRIuMAX"\n",
98 (uintmax_t) (log->len + strlen(gitsvnline)));
99 fwrite(log->buf, log->len, 1, stdout);
100 printf("%s\n", gitsvnline);
101 if (!first_commit_done) {
102 if (revision > 1)
103 printf("from :%"PRIu32"\n", revision - 1);
104 first_commit_done = 1;
105 }
106}
107
108void fast_export_end_commit(uint32_t revision)
109{
110 printf("progress Imported commit %"PRIu32".\n\n", revision);
111}
112
113static void ls_from_rev(uint32_t rev, const char *path)
114{
115 /* ls :5 path/to/old/file */
116 printf("ls :%"PRIu32" ", rev);
117 quote_c_style(path, NULL, stdout, 0);
118 putchar('\n');
119 fflush(stdout);
120}
121
122static void ls_from_active_commit(const char *path)
123{
124 /* ls "path/to/file" */
125 printf("ls \"");
126 quote_c_style(path, NULL, stdout, 1);
127 printf("\"\n");
128 fflush(stdout);
129}
130
131static const char *get_response_line(void)
132{
133 const char *line = buffer_read_line(&report_buffer);
134 if (line)
135 return line;
136 if (buffer_ferror(&report_buffer))
137 die_errno("error reading from fast-import");
138 die("unexpected end of fast-import feedback");
139}
140
141static void die_short_read(struct line_buffer *input)
142{
143 if (buffer_ferror(input))
144 die_errno("error reading dump file");
145 die("invalid dump: unexpected end of file");
146}
147
148static int ends_with(const char *s, size_t len, const char *suffix)
149{
150 const size_t suffixlen = strlen(suffix);
151 if (len < suffixlen)
152 return 0;
153 return !memcmp(s + len - suffixlen, suffix, suffixlen);
154}
155
156static int parse_cat_response_line(const char *header, off_t *len)
157{
158 size_t headerlen = strlen(header);
159 uintmax_t n;
160 const char *type;
161 const char *end;
162
163 if (ends_with(header, headerlen, " missing"))
164 return error("cat-blob reports missing blob: %s", header);
165 type = memmem(header, headerlen, " blob ", strlen(" blob "));
166 if (!type)
167 return error("cat-blob header has wrong object type: %s", header);
168 n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
169 if (end == type + strlen(" blob "))
170 return error("cat-blob header does not contain length: %s", header);
171 if (memchr(type + strlen(" blob "), '-', end - type - strlen(" blob ")))
172 return error("cat-blob header contains negative length: %s", header);
173 if (n == UINTMAX_MAX || n > maximum_signed_value_of_type(off_t))
174 return error("blob too large for current definition of off_t");
175 *len = n;
176 if (*end)
177 return error("cat-blob header contains garbage after length: %s", header);
178 return 0;
179}
180
181static void check_preimage_overflow(off_t a, off_t b)
182{
183 if (signed_add_overflows(a, b))
184 die("blob too large for current definition of off_t");
185}
186
187static long apply_delta(off_t len, struct line_buffer *input,
188 const char *old_data, uint32_t old_mode)
189{
190 long ret;
191 struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0);
192 FILE *out;
193
194 if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
195 die("cannot open temporary file for blob retrieval");
196 if (old_data) {
197 const char *response;
198 printf("cat-blob %s\n", old_data);
199 fflush(stdout);
200 response = get_response_line();
201 if (parse_cat_response_line(response, &preimage.max_off))
202 die("invalid cat-blob response: %s", response);
203 check_preimage_overflow(preimage.max_off, 1);
204 }
205 if (old_mode == REPO_MODE_LNK) {
206 strbuf_addstr(&preimage.buf, "link ");
207 check_preimage_overflow(preimage.max_off, strlen("link "));
208 preimage.max_off += strlen("link ");
209 check_preimage_overflow(preimage.max_off, 1);
210 }
211 if (svndiff0_apply(input, len, &preimage, out))
212 die("cannot apply delta");
213 if (old_data) {
214 /* Read the remainder of preimage and trailing newline. */
215 assert(!signed_add_overflows(preimage.max_off, 1));
216 preimage.max_off++; /* room for newline */
217 if (move_window(&preimage, preimage.max_off - 1, 1))
218 die("cannot seek to end of input");
219 if (preimage.buf.buf[0] != '\n')
220 die("missing newline after cat-blob response");
221 }
222 ret = buffer_tmpfile_prepare_to_read(&postimage);
223 if (ret < 0)
224 die("cannot read temporary file for blob retrieval");
225 strbuf_release(&preimage.buf);
226 return ret;
227}
228
229void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
230{
231 if (mode == REPO_MODE_LNK) {
232 /* svn symlink blobs start with "link " */
233 len -= 5;
234 if (buffer_skip_bytes(input, 5) != 5)
235 die_short_read(input);
236 }
237 printf("data %"PRIu32"\n", len);
238 if (buffer_copy_bytes(input, len) != len)
239 die_short_read(input);
240 fputc('\n', stdout);
241}
242
243static int parse_ls_response(const char *response, uint32_t *mode,
244 struct strbuf *dataref)
245{
246 const char *tab;
247 const char *response_end;
248
249 assert(response);
250 response_end = response + strlen(response);
251
252 if (*response == 'm') { /* Missing. */
253 errno = ENOENT;
254 return -1;
255 }
256
257 /* Mode. */
258 if (response_end - response < strlen("100644") ||
259 response[strlen("100644")] != ' ')
260 die("invalid ls response: missing mode: %s", response);
261 *mode = 0;
262 for (; *response != ' '; response++) {
263 char ch = *response;
264 if (ch < '0' || ch > '7')
265 die("invalid ls response: mode is not octal: %s", response);
266 *mode *= 8;
267 *mode += ch - '0';
268 }
269
270 /* ' blob ' or ' tree ' */
271 if (response_end - response < strlen(" blob ") ||
272 (response[1] != 'b' && response[1] != 't'))
273 die("unexpected ls response: not a tree or blob: %s", response);
274 response += strlen(" blob ");
275
276 /* Dataref. */
277 tab = memchr(response, '\t', response_end - response);
278 if (!tab)
279 die("invalid ls response: missing tab: %s", response);
280 strbuf_add(dataref, response, tab - response);
281 return 0;
282}
283
284int fast_export_ls_rev(uint32_t rev, const char *path,
285 uint32_t *mode, struct strbuf *dataref)
286{
287 ls_from_rev(rev, path);
288 return parse_ls_response(get_response_line(), mode, dataref);
289}
290
291int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
292{
293 ls_from_active_commit(path);
294 return parse_ls_response(get_response_line(), mode, dataref);
295}
296
297void fast_export_blob_delta(uint32_t mode,
298 uint32_t old_mode, const char *old_data,
299 uint32_t len, struct line_buffer *input)
300{
301 long postimage_len;
302 if (len > maximum_signed_value_of_type(off_t))
303 die("enormous delta");
304 postimage_len = apply_delta((off_t) len, input, old_data, old_mode);
305 if (mode == REPO_MODE_LNK) {
306 buffer_skip_bytes(&postimage, strlen("link "));
307 postimage_len -= strlen("link ");
308 }
309 printf("data %ld\n", postimage_len);
310 buffer_copy_bytes(&postimage, postimage_len);
311 fputc('\n', stdout);
312}