]> git.ipfire.org Git - thirdparty/git.git/blame - vcs-svn/svndump.c
vcs-svn: allow import of > 4GiB files
[thirdparty/git.git] / vcs-svn / svndump.c
CommitLineData
21746aa3
DB
1/*
2 * Parse and rearrange a svnadmin dump.
3 * Create the dump with:
4 * svnadmin dump --incremental -r<startrev>:<endrev> <repository> >outfile
5 *
6 * Licensed under a two-clause BSD-style license.
7 * See LICENSE for details.
8 */
9
10#include "cache.h"
11#include "repo_tree.h"
12#include "fast_export.h"
13#include "line_buffer.h"
dce33c9c 14#include "strbuf.h"
c5147722 15#include "svndump.h"
21746aa3 16
044ad290
DB
17/*
18 * Compare start of string to literal of equal length;
19 * must be guarded by length test.
20 */
21#define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
22
41529bbc
DB
23#define REPORT_FILENO 3
24
21746aa3
DB
25#define NODEACT_REPLACE 4
26#define NODEACT_DELETE 3
27#define NODEACT_ADD 2
28#define NODEACT_CHANGE 1
29#define NODEACT_UNKNOWN 0
30
7e11902c
JN
31/* States: */
32#define DUMP_CTX 0 /* dump metadata */
33#define REV_CTX 1 /* revision metadata */
34#define NODE_CTX 2 /* node metadata */
35#define INTERNODE_CTX 3 /* between nodes */
21746aa3
DB
36
37#define LENGTH_UNKNOWN (~0)
38#define DATE_RFC2822_LEN 31
39
e5e45ca1
JN
40static struct line_buffer input = LINE_BUFFER_INIT;
41
21746aa3 42static struct {
150f7546
JN
43 uint32_t action, propLength, srcRev, type;
44 off_t text_length;
03087971 45 struct strbuf src, dst;
1f05d07c 46 uint32_t text_delta, prop_delta;
21746aa3
DB
47} node_ctx;
48
49static struct {
7c5817d3 50 uint32_t revision;
21746aa3 51 unsigned long timestamp;
7c5817d3 52 struct strbuf log, author;
21746aa3
DB
53} rev_ctx;
54
55static struct {
7c5817d3
DB
56 uint32_t version;
57 struct strbuf uuid, url;
21746aa3
DB
58} dump_ctx;
59
21746aa3
DB
60static void reset_node_ctx(char *fname)
61{
62 node_ctx.type = 0;
63 node_ctx.action = NODEACT_UNKNOWN;
64 node_ctx.propLength = LENGTH_UNKNOWN;
150f7546 65 node_ctx.text_length = -1;
03087971 66 strbuf_reset(&node_ctx.src);
21746aa3 67 node_ctx.srcRev = 0;
03087971
DB
68 strbuf_reset(&node_ctx.dst);
69 if (fname)
70 strbuf_addstr(&node_ctx.dst, fname);
1f05d07c
DB
71 node_ctx.text_delta = 0;
72 node_ctx.prop_delta = 0;
21746aa3
DB
73}
74
75static void reset_rev_ctx(uint32_t revision)
76{
77 rev_ctx.revision = revision;
78 rev_ctx.timestamp = 0;
dce33c9c 79 strbuf_reset(&rev_ctx.log);
7c5817d3 80 strbuf_reset(&rev_ctx.author);
21746aa3
DB
81}
82
7c5817d3 83static void reset_dump_ctx(const char *url)
21746aa3 84{
7c5817d3
DB
85 strbuf_reset(&dump_ctx.url);
86 if (url)
87 strbuf_addstr(&dump_ctx.url, url);
b3e5bce1 88 dump_ctx.version = 1;
7c5817d3 89 strbuf_reset(&dump_ctx.uuid);
21746aa3
DB
90}
91
044ad290 92static void handle_property(const struct strbuf *key_buf,
4c3169b0 93 struct strbuf *val,
6b01b676 94 uint32_t *type_set)
2a48afe1 95{
044ad290
DB
96 const char *key = key_buf->buf;
97 size_t keylen = key_buf->len;
98
99 switch (keylen + 1) {
100 case sizeof("svn:log"):
101 if (constcmp(key, "svn:log"))
102 break;
6b01b676
DB
103 if (!val)
104 die("invalid dump: unsets svn:log");
4c3169b0 105 strbuf_swap(&rev_ctx.log, val);
044ad290
DB
106 break;
107 case sizeof("svn:author"):
108 if (constcmp(key, "svn:author"))
109 break;
4c3169b0
JN
110 if (!val)
111 strbuf_reset(&rev_ctx.author);
112 else
113 strbuf_swap(&rev_ctx.author, val);
044ad290
DB
114 break;
115 case sizeof("svn:date"):
116 if (constcmp(key, "svn:date"))
117 break;
6b01b676
DB
118 if (!val)
119 die("invalid dump: unsets svn:date");
4c3169b0
JN
120 if (parse_date_basic(val->buf, &rev_ctx.timestamp, NULL))
121 warning("invalid timestamp: %s", val->buf);
044ad290
DB
122 break;
123 case sizeof("svn:executable"):
124 case sizeof("svn:special"):
125 if (keylen == strlen("svn:executable") &&
126 constcmp(key, "svn:executable"))
127 break;
128 if (keylen == strlen("svn:special") &&
129 constcmp(key, "svn:special"))
130 break;
6b01b676
DB
131 if (*type_set) {
132 if (!val)
133 return;
134 die("invalid dump: sets type twice");
135 }
136 if (!val) {
137 node_ctx.type = REPO_MODE_BLB;
138 return;
139 }
140 *type_set = 1;
044ad290 141 node_ctx.type = keylen == strlen("svn:executable") ?
6b01b676
DB
142 REPO_MODE_EXE :
143 REPO_MODE_LNK;
2a48afe1 144 }
21746aa3
DB
145}
146
c9d1c8ba
JN
147static void die_short_read(void)
148{
149 if (buffer_ferror(&input))
150 die_errno("error reading dump file");
151 die("invalid dump: unexpected end of file");
152}
153
21746aa3
DB
154static void read_props(void)
155{
044ad290 156 static struct strbuf key = STRBUF_INIT;
e7d04ee1 157 static struct strbuf val = STRBUF_INIT;
6263c06d 158 const char *t;
6b01b676
DB
159 /*
160 * NEEDSWORK: to support simple mode changes like
161 * K 11
162 * svn:special
163 * V 1
164 * *
165 * D 14
166 * svn:executable
167 * we keep track of whether a mode has been set and reset to
168 * plain file only if not. We should be keeping track of the
169 * symlink and executable bits separately instead.
170 */
171 uint32_t type_set = 0;
e5e45ca1 172 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
6263c06d 173 uint32_t len;
6263c06d 174 const char type = t[0];
c9d1c8ba 175 int ch;
6263c06d
JN
176
177 if (!type || t[1] != ' ')
178 die("invalid property line: %s\n", t);
179 len = atoi(&t[2]);
e7d04ee1
JN
180 strbuf_reset(&val);
181 buffer_read_binary(&input, &val, len);
182 if (val.len < len)
c9d1c8ba
JN
183 die_short_read();
184
185 /* Discard trailing newline. */
186 ch = buffer_read_char(&input);
187 if (ch == EOF)
188 die_short_read();
189 if (ch != '\n')
e7d04ee1 190 die("invalid dump: expected newline after %s", val.buf);
6263c06d
JN
191
192 switch (type) {
193 case 'K':
e7d04ee1
JN
194 strbuf_swap(&key, &val);
195 continue;
6b01b676 196 case 'D':
4c3169b0 197 handle_property(&val, NULL, &type_set);
e7d04ee1 198 continue;
6263c06d 199 case 'V':
4c3169b0 200 handle_property(&key, &val, &type_set);
044ad290 201 strbuf_reset(&key);
6263c06d
JN
202 continue;
203 default:
204 die("invalid property line: %s\n", t);
21746aa3
DB
205 }
206 }
207}
208
209static void handle_node(void)
210{
1c7bb316 211 const uint32_t type = node_ctx.type;
d6e81a03 212 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
150f7546 213 const int have_text = node_ctx.text_length != -1;
723b7a27
JN
214 /*
215 * Old text for this node:
216 * NULL - directory or bug
217 * empty_blob - empty
218 * "<dataref>" - data retrievable from fast-import
219 */
220 static const char *const empty_blob = "::empty::";
221 const char *old_data = NULL;
7a75e661 222 uint32_t old_mode = REPO_MODE_BLB;
21746aa3
DB
223
224 if (node_ctx.action == NODEACT_DELETE) {
5a38b186 225 if (have_text || have_props || node_ctx.srcRev)
5af8fae2
JN
226 die("invalid dump: deletion node has "
227 "copyfrom info, text, or properties");
c19d653c 228 repo_delete(node_ctx.dst.buf);
9e113988 229 return;
5af8fae2 230 }
6ee4a9be 231 if (node_ctx.action == NODEACT_REPLACE) {
03087971 232 repo_delete(node_ctx.dst.buf);
6ee4a9be
JN
233 node_ctx.action = NODEACT_ADD;
234 }
1c7bb316 235 if (node_ctx.srcRev) {
03087971 236 repo_copy(node_ctx.srcRev, node_ctx.src.buf, node_ctx.dst.buf);
414e569e
JN
237 if (node_ctx.action == NODEACT_ADD)
238 node_ctx.action = NODEACT_CHANGE;
1c7bb316 239 }
5a38b186 240 if (have_text && type == REPO_MODE_DIR)
462e1f51 241 die("invalid dump: directories cannot have text attached");
5a38b186
JN
242
243 /*
723b7a27 244 * Find old content (old_data) and decide on the new mode.
5a38b186 245 */
03087971 246 if (node_ctx.action == NODEACT_CHANGE && !*node_ctx.dst.buf) {
9e8c5321
JN
247 if (type != REPO_MODE_DIR)
248 die("invalid dump: root of tree is not a regular file");
723b7a27 249 old_data = NULL;
9e8c5321 250 } else if (node_ctx.action == NODEACT_CHANGE) {
5a38b186 251 uint32_t mode;
c19d653c 252 old_data = repo_read_path(node_ctx.dst.buf, &mode);
c7dbf35e
JN
253 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
254 die("invalid dump: cannot modify a directory into a file");
255 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
256 die("invalid dump: cannot modify a file into a directory");
257 node_ctx.type = mode;
7a75e661 258 old_mode = mode;
21746aa3 259 } else if (node_ctx.action == NODEACT_ADD) {
723b7a27
JN
260 if (type == REPO_MODE_DIR)
261 old_data = NULL;
262 else if (have_text)
263 old_data = empty_blob;
264 else
c7dbf35e 265 die("invalid dump: adds node without text");
c7dbf35e 266 } else {
414e569e 267 die("invalid dump: Node-path block lacks Node-action");
21746aa3 268 }
5a38b186
JN
269
270 /*
271 * Adjust mode to reflect properties.
272 */
1c7bb316 273 if (have_props) {
6b01b676
DB
274 if (!node_ctx.prop_delta)
275 node_ctx.type = type;
1c7bb316
JN
276 if (node_ctx.propLength)
277 read_props();
21746aa3 278 }
5a38b186
JN
279
280 /*
281 * Save the result.
282 */
723b7a27
JN
283 if (type == REPO_MODE_DIR) /* directories are not tracked. */
284 return;
285 assert(old_data);
286 if (old_data == empty_blob)
287 /* For the fast_export_* functions, NULL means empty. */
288 old_data = NULL;
289 if (!have_text) {
03087971 290 fast_export_modify(node_ctx.dst.buf, node_ctx.type, old_data);
723b7a27
JN
291 return;
292 }
7a75e661
DB
293 if (!node_ctx.text_delta) {
294 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
150f7546 295 fast_export_data(node_ctx.type, node_ctx.text_length, &input);
7a75e661
DB
296 return;
297 }
03087971 298 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
7a75e661 299 fast_export_blob_delta(node_ctx.type, old_mode, old_data,
150f7546 300 node_ctx.text_length, &input);
21746aa3
DB
301}
302
7e11902c
JN
303static void begin_revision(void)
304{
305 if (!rev_ctx.revision) /* revision 0 gets no git commit. */
306 return;
9ecfa8ae
JN
307 fast_export_begin_commit(rev_ctx.revision, rev_ctx.author.buf,
308 &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
309 rev_ctx.timestamp);
21746aa3
DB
310}
311
7e11902c 312static void end_revision(void)
21746aa3
DB
313{
314 if (rev_ctx.revision)
723b7a27 315 fast_export_end_commit(rev_ctx.revision);
21746aa3
DB
316}
317
318void svndump_read(const char *url)
319{
320 char *val;
321 char *t;
322 uint32_t active_ctx = DUMP_CTX;
323 uint32_t len;
21746aa3 324
7c5817d3 325 reset_dump_ctx(url);
e5e45ca1 326 while ((t = buffer_read_line(&input))) {
f1602054 327 val = strchr(t, ':');
21746aa3
DB
328 if (!val)
329 continue;
f1602054
DB
330 val++;
331 if (*val != ' ')
332 continue;
333 val++;
21746aa3 334
90c0a3cf
DB
335 /* strlen(key) + 1 */
336 switch (val - t - 1) {
337 case sizeof("SVN-fs-dump-format-version"):
338 if (constcmp(t, "SVN-fs-dump-format-version"))
339 continue;
b3e5bce1 340 dump_ctx.version = atoi(val);
1f05d07c 341 if (dump_ctx.version > 3)
a62bbf8f 342 die("expected svn dump format version <= 3, found %"PRIu32,
b3e5bce1 343 dump_ctx.version);
90c0a3cf
DB
344 break;
345 case sizeof("UUID"):
346 if (constcmp(t, "UUID"))
347 continue;
7c5817d3
DB
348 strbuf_reset(&dump_ctx.uuid);
349 strbuf_addstr(&dump_ctx.uuid, val);
90c0a3cf
DB
350 break;
351 case sizeof("Revision-number"):
352 if (constcmp(t, "Revision-number"))
353 continue;
21746aa3
DB
354 if (active_ctx == NODE_CTX)
355 handle_node();
7e11902c
JN
356 if (active_ctx == REV_CTX)
357 begin_revision();
21746aa3 358 if (active_ctx != DUMP_CTX)
7e11902c 359 end_revision();
21746aa3
DB
360 active_ctx = REV_CTX;
361 reset_rev_ctx(atoi(val));
90c0a3cf
DB
362 break;
363 case sizeof("Node-path"):
364 if (prefixcmp(t, "Node-"))
365 continue;
366 if (!constcmp(t + strlen("Node-"), "path")) {
367 if (active_ctx == NODE_CTX)
368 handle_node();
9ecfa8ae
JN
369 if (active_ctx == REV_CTX)
370 begin_revision();
90c0a3cf
DB
371 active_ctx = NODE_CTX;
372 reset_node_ctx(val);
373 break;
374 }
375 if (constcmp(t + strlen("Node-"), "kind"))
376 continue;
21746aa3
DB
377 if (!strcmp(val, "dir"))
378 node_ctx.type = REPO_MODE_DIR;
379 else if (!strcmp(val, "file"))
380 node_ctx.type = REPO_MODE_BLB;
381 else
382 fprintf(stderr, "Unknown node-kind: %s\n", val);
90c0a3cf
DB
383 break;
384 case sizeof("Node-action"):
385 if (constcmp(t, "Node-action"))
386 continue;
21746aa3
DB
387 if (!strcmp(val, "delete")) {
388 node_ctx.action = NODEACT_DELETE;
389 } else if (!strcmp(val, "add")) {
390 node_ctx.action = NODEACT_ADD;
391 } else if (!strcmp(val, "change")) {
392 node_ctx.action = NODEACT_CHANGE;
393 } else if (!strcmp(val, "replace")) {
394 node_ctx.action = NODEACT_REPLACE;
395 } else {
396 fprintf(stderr, "Unknown node-action: %s\n", val);
397 node_ctx.action = NODEACT_UNKNOWN;
398 }
90c0a3cf
DB
399 break;
400 case sizeof("Node-copyfrom-path"):
401 if (constcmp(t, "Node-copyfrom-path"))
402 continue;
03087971
DB
403 strbuf_reset(&node_ctx.src);
404 strbuf_addstr(&node_ctx.src, val);
90c0a3cf
DB
405 break;
406 case sizeof("Node-copyfrom-rev"):
407 if (constcmp(t, "Node-copyfrom-rev"))
408 continue;
21746aa3 409 node_ctx.srcRev = atoi(val);
90c0a3cf
DB
410 break;
411 case sizeof("Text-content-length"):
412 if (!constcmp(t, "Text-content-length")) {
150f7546
JN
413 char *end;
414 uintmax_t textlen;
415
416 textlen = strtoumax(val, &end, 10);
417 if (!isdigit(*val) || *end)
418 die("invalid dump: non-numeric length %s", val);
419 if (textlen > maximum_signed_value_of_type(off_t))
420 die("unrepresentable length in dump: %s", val);
421 node_ctx.text_length = (off_t) textlen;
90c0a3cf
DB
422 break;
423 }
424 if (constcmp(t, "Prop-content-length"))
425 continue;
21746aa3 426 node_ctx.propLength = atoi(val);
90c0a3cf
DB
427 break;
428 case sizeof("Text-delta"):
429 if (!constcmp(t, "Text-delta")) {
430 node_ctx.text_delta = !strcmp(val, "true");
431 break;
432 }
433 if (constcmp(t, "Prop-delta"))
434 continue;
1f05d07c 435 node_ctx.prop_delta = !strcmp(val, "true");
90c0a3cf
DB
436 break;
437 case sizeof("Content-length"):
438 if (constcmp(t, "Content-length"))
439 continue;
21746aa3 440 len = atoi(val);
c9d1c8ba
JN
441 t = buffer_read_line(&input);
442 if (!t)
443 die_short_read();
444 if (*t)
445 die("invalid dump: expected blank line after content length header");
21746aa3
DB
446 if (active_ctx == REV_CTX) {
447 read_props();
448 } else if (active_ctx == NODE_CTX) {
449 handle_node();
7e11902c 450 active_ctx = INTERNODE_CTX;
21746aa3 451 } else {
5418d96d 452 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
c9d1c8ba
JN
453 if (buffer_skip_bytes(&input, len) != len)
454 die_short_read();
21746aa3
DB
455 }
456 }
457 }
c9d1c8ba
JN
458 if (buffer_ferror(&input))
459 die_short_read();
21746aa3
DB
460 if (active_ctx == NODE_CTX)
461 handle_node();
7e11902c
JN
462 if (active_ctx == REV_CTX)
463 begin_revision();
21746aa3 464 if (active_ctx != DUMP_CTX)
7e11902c 465 end_revision();
21746aa3
DB
466}
467
5c28a8b0 468int svndump_init(const char *filename)
21746aa3 469{
a62bbf8f 470 if (buffer_init(&input, filename))
5c28a8b0 471 return error("cannot open %s: %s", filename, strerror(errno));
41529bbc 472 fast_export_init(REPORT_FILENO);
7c5817d3
DB
473 strbuf_init(&dump_ctx.uuid, 4096);
474 strbuf_init(&dump_ctx.url, 4096);
dce33c9c 475 strbuf_init(&rev_ctx.log, 4096);
7c5817d3 476 strbuf_init(&rev_ctx.author, 4096);
03087971
DB
477 strbuf_init(&node_ctx.src, 4096);
478 strbuf_init(&node_ctx.dst, 4096);
7c5817d3 479 reset_dump_ctx(NULL);
21746aa3
DB
480 reset_rev_ctx(0);
481 reset_node_ctx(NULL);
5c28a8b0 482 return 0;
21746aa3
DB
483}
484
485void svndump_deinit(void)
486{
41529bbc 487 fast_export_deinit();
7c5817d3 488 reset_dump_ctx(NULL);
21746aa3
DB
489 reset_rev_ctx(0);
490 reset_node_ctx(NULL);
dce33c9c 491 strbuf_release(&rev_ctx.log);
03087971
DB
492 strbuf_release(&node_ctx.src);
493 strbuf_release(&node_ctx.dst);
e5e45ca1 494 if (buffer_deinit(&input))
21746aa3
DB
495 fprintf(stderr, "Input error\n");
496 if (ferror(stdout))
497 fprintf(stderr, "Output error\n");
498}
499
500void svndump_reset(void)
501{
41529bbc 502 fast_export_reset();
e5e45ca1 503 buffer_reset(&input);
7c5817d3
DB
504 strbuf_release(&dump_ctx.uuid);
505 strbuf_release(&dump_ctx.url);
506 strbuf_release(&rev_ctx.log);
507 strbuf_release(&rev_ctx.author);
21746aa3 508}