vcs-svn: remove custom mode constants
[git/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 36
21746aa3
DB
37#define DATE_RFC2822_LEN 31
38
e5e45ca1
JN
39static struct line_buffer input = LINE_BUFFER_INIT;
40
21746aa3 41static struct {
e32b79cb
JN
42 uint32_t action, srcRev, type;
43 off_t prop_length, text_length;
03087971 44 struct strbuf src, dst;
1f05d07c 45 uint32_t text_delta, prop_delta;
21746aa3
DB
46} node_ctx;
47
48static struct {
7c5817d3 49 uint32_t revision;
21746aa3 50 unsigned long timestamp;
a9a55613 51 struct strbuf log, author, note;
21746aa3
DB
52} rev_ctx;
53
54static struct {
7c5817d3
DB
55 uint32_t version;
56 struct strbuf uuid, url;
21746aa3
DB
57} dump_ctx;
58
21746aa3
DB
59static void reset_node_ctx(char *fname)
60{
61 node_ctx.type = 0;
62 node_ctx.action = NODEACT_UNKNOWN;
e32b79cb 63 node_ctx.prop_length = -1;
150f7546 64 node_ctx.text_length = -1;
03087971 65 strbuf_reset(&node_ctx.src);
21746aa3 66 node_ctx.srcRev = 0;
03087971
DB
67 strbuf_reset(&node_ctx.dst);
68 if (fname)
69 strbuf_addstr(&node_ctx.dst, fname);
1f05d07c
DB
70 node_ctx.text_delta = 0;
71 node_ctx.prop_delta = 0;
21746aa3
DB
72}
73
74static void reset_rev_ctx(uint32_t revision)
75{
76 rev_ctx.revision = revision;
77 rev_ctx.timestamp = 0;
dce33c9c 78 strbuf_reset(&rev_ctx.log);
7c5817d3 79 strbuf_reset(&rev_ctx.author);
a9a55613 80 strbuf_reset(&rev_ctx.note);
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) {
21c7c2d9 137 node_ctx.type = S_IFREG | 0644;
6b01b676
DB
138 return;
139 }
140 *type_set = 1;
044ad290 141 node_ctx.type = keylen == strlen("svn:executable") ?
21c7c2d9
JN
142 (S_IFREG | 0755) :
143 S_IFLNK;
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] != ' ')
82247e9b 178 die("invalid property line: %s", t);
6263c06d 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:
82247e9b 204 die("invalid property line: %s", t);
21746aa3
DB
205 }
206 }
207}
208
209static void handle_node(void)
210{
1c7bb316 211 const uint32_t type = node_ctx.type;
e32b79cb 212 const int have_props = node_ctx.prop_length != -1;
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;
21c7c2d9 222 uint32_t old_mode = S_IFREG | 0644;
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");
15d1d095 228 svn_repo_delete(node_ctx.dst.buf);
9e113988 229 return;
5af8fae2 230 }
6ee4a9be 231 if (node_ctx.action == NODEACT_REPLACE) {
15d1d095 232 svn_repo_delete(node_ctx.dst.buf);
6ee4a9be
JN
233 node_ctx.action = NODEACT_ADD;
234 }
1c7bb316 235 if (node_ctx.srcRev) {
15d1d095 236 svn_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 }
21c7c2d9 240 if (have_text && type == S_IFDIR)
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) {
21c7c2d9 247 if (type != S_IFDIR)
9e8c5321 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;
15d1d095 252 old_data = svn_repo_read_path(node_ctx.dst.buf, &mode);
21c7c2d9 253 if (mode == S_IFDIR && type != S_IFDIR)
c7dbf35e 254 die("invalid dump: cannot modify a directory into a file");
21c7c2d9 255 if (mode != S_IFDIR && type == S_IFDIR)
c7dbf35e
JN
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) {
21c7c2d9 260 if (type == S_IFDIR)
723b7a27
JN
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;
e32b79cb 276 if (node_ctx.prop_length)
1c7bb316 277 read_props();
21746aa3 278 }
5a38b186
JN
279
280 /*
281 * Save the result.
282 */
21c7c2d9 283 if (type == S_IFDIR) /* directories are not tracked. */
723b7a27
JN
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
271fd1fc 303static void begin_revision(const char *remote_ref)
7e11902c
JN
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,
271fd1fc 309 rev_ctx.timestamp, remote_ref);
21746aa3
DB
310}
311
8e43a1d0 312static void end_revision(const char *note_ref)
21746aa3 313{
a9a55613
FA
314 struct strbuf mark = STRBUF_INIT;
315 if (rev_ctx.revision) {
723b7a27 316 fast_export_end_commit(rev_ctx.revision);
a9a55613 317 fast_export_begin_note(rev_ctx.revision, "remote-svn",
8e43a1d0 318 "Note created by remote-svn.", rev_ctx.timestamp, note_ref);
a9a55613
FA
319 strbuf_addf(&mark, ":%"PRIu32, rev_ctx.revision);
320 fast_export_note(mark.buf, "inline");
321 fast_export_buf_to_data(&rev_ctx.note);
322 }
21746aa3
DB
323}
324
8e43a1d0 325void svndump_read(const char *url, const char *local_ref, const char *notes_ref)
21746aa3
DB
326{
327 char *val;
328 char *t;
329 uint32_t active_ctx = DUMP_CTX;
330 uint32_t len;
21746aa3 331
7c5817d3 332 reset_dump_ctx(url);
e5e45ca1 333 while ((t = buffer_read_line(&input))) {
f1602054 334 val = strchr(t, ':');
21746aa3
DB
335 if (!val)
336 continue;
f1602054
DB
337 val++;
338 if (*val != ' ')
339 continue;
340 val++;
21746aa3 341
90c0a3cf
DB
342 /* strlen(key) + 1 */
343 switch (val - t - 1) {
344 case sizeof("SVN-fs-dump-format-version"):
345 if (constcmp(t, "SVN-fs-dump-format-version"))
346 continue;
b3e5bce1 347 dump_ctx.version = atoi(val);
1f05d07c 348 if (dump_ctx.version > 3)
a62bbf8f 349 die("expected svn dump format version <= 3, found %"PRIu32,
b3e5bce1 350 dump_ctx.version);
90c0a3cf
DB
351 break;
352 case sizeof("UUID"):
353 if (constcmp(t, "UUID"))
354 continue;
7c5817d3
DB
355 strbuf_reset(&dump_ctx.uuid);
356 strbuf_addstr(&dump_ctx.uuid, val);
90c0a3cf
DB
357 break;
358 case sizeof("Revision-number"):
359 if (constcmp(t, "Revision-number"))
360 continue;
21746aa3
DB
361 if (active_ctx == NODE_CTX)
362 handle_node();
7e11902c 363 if (active_ctx == REV_CTX)
271fd1fc 364 begin_revision(local_ref);
21746aa3 365 if (active_ctx != DUMP_CTX)
8e43a1d0 366 end_revision(notes_ref);
21746aa3
DB
367 active_ctx = REV_CTX;
368 reset_rev_ctx(atoi(val));
a9a55613 369 strbuf_addf(&rev_ctx.note, "%s\n", t);
90c0a3cf
DB
370 break;
371 case sizeof("Node-path"):
d8d8708b 372 if (constcmp(t, "Node-"))
90c0a3cf
DB
373 continue;
374 if (!constcmp(t + strlen("Node-"), "path")) {
375 if (active_ctx == NODE_CTX)
376 handle_node();
9ecfa8ae 377 if (active_ctx == REV_CTX)
271fd1fc 378 begin_revision(local_ref);
90c0a3cf
DB
379 active_ctx = NODE_CTX;
380 reset_node_ctx(val);
a9a55613 381 strbuf_addf(&rev_ctx.note, "%s\n", t);
90c0a3cf
DB
382 break;
383 }
384 if (constcmp(t + strlen("Node-"), "kind"))
385 continue;
a9a55613 386 strbuf_addf(&rev_ctx.note, "%s\n", t);
21746aa3 387 if (!strcmp(val, "dir"))
21c7c2d9 388 node_ctx.type = S_IFDIR;
21746aa3 389 else if (!strcmp(val, "file"))
21c7c2d9 390 node_ctx.type = S_IFREG | 0644;
21746aa3
DB
391 else
392 fprintf(stderr, "Unknown node-kind: %s\n", val);
90c0a3cf
DB
393 break;
394 case sizeof("Node-action"):
395 if (constcmp(t, "Node-action"))
396 continue;
a9a55613 397 strbuf_addf(&rev_ctx.note, "%s\n", t);
21746aa3
DB
398 if (!strcmp(val, "delete")) {
399 node_ctx.action = NODEACT_DELETE;
400 } else if (!strcmp(val, "add")) {
401 node_ctx.action = NODEACT_ADD;
402 } else if (!strcmp(val, "change")) {
403 node_ctx.action = NODEACT_CHANGE;
404 } else if (!strcmp(val, "replace")) {
405 node_ctx.action = NODEACT_REPLACE;
406 } else {
407 fprintf(stderr, "Unknown node-action: %s\n", val);
408 node_ctx.action = NODEACT_UNKNOWN;
409 }
90c0a3cf
DB
410 break;
411 case sizeof("Node-copyfrom-path"):
412 if (constcmp(t, "Node-copyfrom-path"))
413 continue;
03087971
DB
414 strbuf_reset(&node_ctx.src);
415 strbuf_addstr(&node_ctx.src, val);
a9a55613 416 strbuf_addf(&rev_ctx.note, "%s\n", t);
90c0a3cf
DB
417 break;
418 case sizeof("Node-copyfrom-rev"):
419 if (constcmp(t, "Node-copyfrom-rev"))
420 continue;
21746aa3 421 node_ctx.srcRev = atoi(val);
a9a55613 422 strbuf_addf(&rev_ctx.note, "%s\n", t);
90c0a3cf
DB
423 break;
424 case sizeof("Text-content-length"):
e32b79cb
JN
425 if (constcmp(t, "Text") && constcmp(t, "Prop"))
426 continue;
427 if (constcmp(t + 4, "-content-length"))
428 continue;
429 {
150f7546 430 char *end;
e32b79cb 431 uintmax_t len;
150f7546 432
e32b79cb 433 len = strtoumax(val, &end, 10);
150f7546
JN
434 if (!isdigit(*val) || *end)
435 die("invalid dump: non-numeric length %s", val);
e32b79cb 436 if (len > maximum_signed_value_of_type(off_t))
150f7546 437 die("unrepresentable length in dump: %s", val);
e32b79cb
JN
438
439 if (*t == 'T')
440 node_ctx.text_length = (off_t) len;
441 else
442 node_ctx.prop_length = (off_t) len;
90c0a3cf
DB
443 break;
444 }
90c0a3cf
DB
445 case sizeof("Text-delta"):
446 if (!constcmp(t, "Text-delta")) {
447 node_ctx.text_delta = !strcmp(val, "true");
448 break;
449 }
450 if (constcmp(t, "Prop-delta"))
451 continue;
1f05d07c 452 node_ctx.prop_delta = !strcmp(val, "true");
90c0a3cf
DB
453 break;
454 case sizeof("Content-length"):
455 if (constcmp(t, "Content-length"))
456 continue;
21746aa3 457 len = atoi(val);
c9d1c8ba
JN
458 t = buffer_read_line(&input);
459 if (!t)
460 die_short_read();
461 if (*t)
462 die("invalid dump: expected blank line after content length header");
21746aa3
DB
463 if (active_ctx == REV_CTX) {
464 read_props();
465 } else if (active_ctx == NODE_CTX) {
466 handle_node();
7e11902c 467 active_ctx = INTERNODE_CTX;
21746aa3 468 } else {
5418d96d 469 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
c9d1c8ba
JN
470 if (buffer_skip_bytes(&input, len) != len)
471 die_short_read();
21746aa3
DB
472 }
473 }
474 }
c9d1c8ba
JN
475 if (buffer_ferror(&input))
476 die_short_read();
21746aa3
DB
477 if (active_ctx == NODE_CTX)
478 handle_node();
7e11902c 479 if (active_ctx == REV_CTX)
271fd1fc 480 begin_revision(local_ref);
21746aa3 481 if (active_ctx != DUMP_CTX)
8e43a1d0 482 end_revision(notes_ref);
21746aa3
DB
483}
484
fd871b94 485static void init(int report_fd)
21746aa3 486{
fd871b94 487 fast_export_init(report_fd);
7c5817d3
DB
488 strbuf_init(&dump_ctx.uuid, 4096);
489 strbuf_init(&dump_ctx.url, 4096);
dce33c9c 490 strbuf_init(&rev_ctx.log, 4096);
7c5817d3 491 strbuf_init(&rev_ctx.author, 4096);
a9a55613 492 strbuf_init(&rev_ctx.note, 4096);
03087971
DB
493 strbuf_init(&node_ctx.src, 4096);
494 strbuf_init(&node_ctx.dst, 4096);
7c5817d3 495 reset_dump_ctx(NULL);
21746aa3
DB
496 reset_rev_ctx(0);
497 reset_node_ctx(NULL);
fd871b94
FA
498 return;
499}
500
501int svndump_init(const char *filename)
502{
503 if (buffer_init(&input, filename))
1c8ead97 504 return error_errno("cannot open %s", filename ? filename : "NULL");
fd871b94
FA
505 init(REPORT_FILENO);
506 return 0;
507}
508
509int svndump_init_fd(int in_fd, int back_fd)
510{
511 if(buffer_fdinit(&input, xdup(in_fd)))
1c8ead97 512 return error_errno("cannot open fd %d", in_fd);
fd871b94 513 init(xdup(back_fd));
5c28a8b0 514 return 0;
21746aa3
DB
515}
516
517void svndump_deinit(void)
518{
41529bbc 519 fast_export_deinit();
7c5817d3 520 reset_dump_ctx(NULL);
21746aa3
DB
521 reset_rev_ctx(0);
522 reset_node_ctx(NULL);
dce33c9c 523 strbuf_release(&rev_ctx.log);
a9a55613
FA
524 strbuf_release(&rev_ctx.author);
525 strbuf_release(&rev_ctx.note);
03087971
DB
526 strbuf_release(&node_ctx.src);
527 strbuf_release(&node_ctx.dst);
e5e45ca1 528 if (buffer_deinit(&input))
21746aa3
DB
529 fprintf(stderr, "Input error\n");
530 if (ferror(stdout))
531 fprintf(stderr, "Output error\n");
532}
533
534void svndump_reset(void)
535{
7c5817d3
DB
536 strbuf_release(&dump_ctx.uuid);
537 strbuf_release(&dump_ctx.url);
538 strbuf_release(&rev_ctx.log);
539 strbuf_release(&rev_ctx.author);
21746aa3 540}