2 * Parse and rearrange a svnadmin dump.
3 * Create the dump with:
4 * svnadmin dump --incremental -r<startrev>:<endrev> <repository> >outfile
6 * Licensed under a two-clause BSD-style license.
7 * See LICENSE for details.
11 #include "repo_tree.h"
12 #include "fast_export.h"
13 #include "line_buffer.h"
18 * Compare start of string to literal of equal length;
19 * must be guarded by length test.
21 #define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
23 #define REPORT_FILENO 3
25 #define NODEACT_REPLACE 4
26 #define NODEACT_DELETE 3
28 #define NODEACT_CHANGE 1
29 #define NODEACT_UNKNOWN 0
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 */
37 #define DATE_RFC2822_LEN 31
39 static struct line_buffer input = LINE_BUFFER_INIT;
42 uint32_t action, srcRev, type;
43 off_t prop_length, text_length;
44 struct strbuf src, dst;
45 uint32_t text_delta, prop_delta;
50 unsigned long timestamp;
51 struct strbuf log, author, note;
56 struct strbuf uuid, url;
59 static void reset_node_ctx(char *fname)
62 node_ctx.action = NODEACT_UNKNOWN;
63 node_ctx.prop_length = -1;
64 node_ctx.text_length = -1;
65 strbuf_reset(&node_ctx.src);
67 strbuf_reset(&node_ctx.dst);
69 strbuf_addstr(&node_ctx.dst, fname);
70 node_ctx.text_delta = 0;
71 node_ctx.prop_delta = 0;
74 static void reset_rev_ctx(uint32_t revision)
76 rev_ctx.revision = revision;
77 rev_ctx.timestamp = 0;
78 strbuf_reset(&rev_ctx.log);
79 strbuf_reset(&rev_ctx.author);
80 strbuf_reset(&rev_ctx.note);
83 static void reset_dump_ctx(const char *url)
85 strbuf_reset(&dump_ctx.url);
87 strbuf_addstr(&dump_ctx.url, url);
89 strbuf_reset(&dump_ctx.uuid);
92 static void handle_property(const struct strbuf *key_buf,
96 const char *key = key_buf->buf;
97 size_t keylen = key_buf->len;
100 case sizeof("svn:log"):
101 if (constcmp(key, "svn:log"))
104 die("invalid dump: unsets svn:log");
105 strbuf_swap(&rev_ctx.log, val);
107 case sizeof("svn:author"):
108 if (constcmp(key, "svn:author"))
111 strbuf_reset(&rev_ctx.author);
113 strbuf_swap(&rev_ctx.author, val);
115 case sizeof("svn:date"):
116 if (constcmp(key, "svn:date"))
119 die("invalid dump: unsets svn:date");
120 if (parse_date_basic(val->buf, &rev_ctx.timestamp, NULL))
121 warning("invalid timestamp: %s", val->buf);
123 case sizeof("svn:executable"):
124 case sizeof("svn:special"):
125 if (keylen == strlen("svn:executable") &&
126 constcmp(key, "svn:executable"))
128 if (keylen == strlen("svn:special") &&
129 constcmp(key, "svn:special"))
134 die("invalid dump: sets type twice");
137 node_ctx.type = REPO_MODE_BLB;
141 node_ctx.type = keylen == strlen("svn:executable") ?
147 static void die_short_read(void)
149 if (buffer_ferror(&input))
150 die_errno("error reading dump file");
151 die("invalid dump: unexpected end of file");
154 static void read_props(void)
156 static struct strbuf key = STRBUF_INIT;
157 static struct strbuf val = STRBUF_INIT;
160 * NEEDSWORK: to support simple mode changes like
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.
171 uint32_t type_set = 0;
172 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
174 const char type = t[0];
177 if (!type || t[1] != ' ')
178 die("invalid property line: %s", t);
181 buffer_read_binary(&input, &val, len);
185 /* Discard trailing newline. */
186 ch = buffer_read_char(&input);
190 die("invalid dump: expected newline after %s", val.buf);
194 strbuf_swap(&key, &val);
197 handle_property(&val, NULL, &type_set);
200 handle_property(&key, &val, &type_set);
204 die("invalid property line: %s", t);
209 static void handle_node(void)
211 const uint32_t type = node_ctx.type;
212 const int have_props = node_ctx.prop_length != -1;
213 const int have_text = node_ctx.text_length != -1;
215 * Old text for this node:
216 * NULL - directory or bug
218 * "<dataref>" - data retrievable from fast-import
220 static const char *const empty_blob = "::empty::";
221 const char *old_data = NULL;
222 uint32_t old_mode = REPO_MODE_BLB;
224 if (node_ctx.action == NODEACT_DELETE) {
225 if (have_text || have_props || node_ctx.srcRev)
226 die("invalid dump: deletion node has "
227 "copyfrom info, text, or properties");
228 repo_delete(node_ctx.dst.buf);
231 if (node_ctx.action == NODEACT_REPLACE) {
232 repo_delete(node_ctx.dst.buf);
233 node_ctx.action = NODEACT_ADD;
235 if (node_ctx.srcRev) {
236 repo_copy(node_ctx.srcRev, node_ctx.src.buf, node_ctx.dst.buf);
237 if (node_ctx.action == NODEACT_ADD)
238 node_ctx.action = NODEACT_CHANGE;
240 if (have_text && type == REPO_MODE_DIR)
241 die("invalid dump: directories cannot have text attached");
244 * Find old content (old_data) and decide on the new mode.
246 if (node_ctx.action == NODEACT_CHANGE && !*node_ctx.dst.buf) {
247 if (type != REPO_MODE_DIR)
248 die("invalid dump: root of tree is not a regular file");
250 } else if (node_ctx.action == NODEACT_CHANGE) {
252 old_data = repo_read_path(node_ctx.dst.buf, &mode);
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;
259 } else if (node_ctx.action == NODEACT_ADD) {
260 if (type == REPO_MODE_DIR)
263 old_data = empty_blob;
265 die("invalid dump: adds node without text");
267 die("invalid dump: Node-path block lacks Node-action");
271 * Adjust mode to reflect properties.
274 if (!node_ctx.prop_delta)
275 node_ctx.type = type;
276 if (node_ctx.prop_length)
283 if (type == REPO_MODE_DIR) /* directories are not tracked. */
286 if (old_data == empty_blob)
287 /* For the fast_export_* functions, NULL means empty. */
290 fast_export_modify(node_ctx.dst.buf, node_ctx.type, old_data);
293 if (!node_ctx.text_delta) {
294 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
295 fast_export_data(node_ctx.type, node_ctx.text_length, &input);
298 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
299 fast_export_blob_delta(node_ctx.type, old_mode, old_data,
300 node_ctx.text_length, &input);
303 static void begin_revision(const char *remote_ref)
305 if (!rev_ctx.revision) /* revision 0 gets no git commit. */
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, remote_ref);
312 static void end_revision(const char *note_ref)
314 struct strbuf mark = STRBUF_INIT;
315 if (rev_ctx.revision) {
316 fast_export_end_commit(rev_ctx.revision);
317 fast_export_begin_note(rev_ctx.revision, "remote-svn",
318 "Note created by remote-svn.", rev_ctx.timestamp, note_ref);
319 strbuf_addf(&mark, ":%"PRIu32, rev_ctx.revision);
320 fast_export_note(mark.buf, "inline");
321 fast_export_buf_to_data(&rev_ctx.note);
325 void svndump_read(const char *url, const char *local_ref, const char *notes_ref)
329 uint32_t active_ctx = DUMP_CTX;
333 while ((t = buffer_read_line(&input))) {
334 val = strchr(t, ':');
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"))
347 dump_ctx.version = atoi(val);
348 if (dump_ctx.version > 3)
349 die("expected svn dump format version <= 3, found %"PRIu32,
353 if (constcmp(t, "UUID"))
355 strbuf_reset(&dump_ctx.uuid);
356 strbuf_addstr(&dump_ctx.uuid, val);
358 case sizeof("Revision-number"):
359 if (constcmp(t, "Revision-number"))
361 if (active_ctx == NODE_CTX)
363 if (active_ctx == REV_CTX)
364 begin_revision(local_ref);
365 if (active_ctx != DUMP_CTX)
366 end_revision(notes_ref);
367 active_ctx = REV_CTX;
368 reset_rev_ctx(atoi(val));
369 strbuf_addf(&rev_ctx.note, "%s\n", t);
371 case sizeof("Node-path"):
372 if (constcmp(t, "Node-"))
374 if (!constcmp(t + strlen("Node-"), "path")) {
375 if (active_ctx == NODE_CTX)
377 if (active_ctx == REV_CTX)
378 begin_revision(local_ref);
379 active_ctx = NODE_CTX;
381 strbuf_addf(&rev_ctx.note, "%s\n", t);
384 if (constcmp(t + strlen("Node-"), "kind"))
386 strbuf_addf(&rev_ctx.note, "%s\n", t);
387 if (!strcmp(val, "dir"))
388 node_ctx.type = REPO_MODE_DIR;
389 else if (!strcmp(val, "file"))
390 node_ctx.type = REPO_MODE_BLB;
392 fprintf(stderr, "Unknown node-kind: %s\n", val);
394 case sizeof("Node-action"):
395 if (constcmp(t, "Node-action"))
397 strbuf_addf(&rev_ctx.note, "%s\n", t);
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;
407 fprintf(stderr, "Unknown node-action: %s\n", val);
408 node_ctx.action = NODEACT_UNKNOWN;
411 case sizeof("Node-copyfrom-path"):
412 if (constcmp(t, "Node-copyfrom-path"))
414 strbuf_reset(&node_ctx.src);
415 strbuf_addstr(&node_ctx.src, val);
416 strbuf_addf(&rev_ctx.note, "%s\n", t);
418 case sizeof("Node-copyfrom-rev"):
419 if (constcmp(t, "Node-copyfrom-rev"))
421 node_ctx.srcRev = atoi(val);
422 strbuf_addf(&rev_ctx.note, "%s\n", t);
424 case sizeof("Text-content-length"):
425 if (constcmp(t, "Text") && constcmp(t, "Prop"))
427 if (constcmp(t + 4, "-content-length"))
433 len = strtoumax(val, &end, 10);
434 if (!isdigit(*val) || *end)
435 die("invalid dump: non-numeric length %s", val);
436 if (len > maximum_signed_value_of_type(off_t))
437 die("unrepresentable length in dump: %s", val);
440 node_ctx.text_length = (off_t) len;
442 node_ctx.prop_length = (off_t) len;
445 case sizeof("Text-delta"):
446 if (!constcmp(t, "Text-delta")) {
447 node_ctx.text_delta = !strcmp(val, "true");
450 if (constcmp(t, "Prop-delta"))
452 node_ctx.prop_delta = !strcmp(val, "true");
454 case sizeof("Content-length"):
455 if (constcmp(t, "Content-length"))
458 t = buffer_read_line(&input);
462 die("invalid dump: expected blank line after content length header");
463 if (active_ctx == REV_CTX) {
465 } else if (active_ctx == NODE_CTX) {
467 active_ctx = INTERNODE_CTX;
469 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
470 if (buffer_skip_bytes(&input, len) != len)
475 if (buffer_ferror(&input))
477 if (active_ctx == NODE_CTX)
479 if (active_ctx == REV_CTX)
480 begin_revision(local_ref);
481 if (active_ctx != DUMP_CTX)
482 end_revision(notes_ref);
485 static void init(int report_fd)
487 fast_export_init(report_fd);
488 strbuf_init(&dump_ctx.uuid, 4096);
489 strbuf_init(&dump_ctx.url, 4096);
490 strbuf_init(&rev_ctx.log, 4096);
491 strbuf_init(&rev_ctx.author, 4096);
492 strbuf_init(&rev_ctx.note, 4096);
493 strbuf_init(&node_ctx.src, 4096);
494 strbuf_init(&node_ctx.dst, 4096);
495 reset_dump_ctx(NULL);
497 reset_node_ctx(NULL);
501 int svndump_init(const char *filename)
503 if (buffer_init(&input, filename))
504 return error_errno("cannot open %s", filename ? filename : "NULL");
509 int svndump_init_fd(int in_fd, int back_fd)
511 if(buffer_fdinit(&input, xdup(in_fd)))
512 return error_errno("cannot open fd %d", in_fd);
517 void svndump_deinit(void)
519 fast_export_deinit();
520 reset_dump_ctx(NULL);
522 reset_node_ctx(NULL);
523 strbuf_release(&rev_ctx.log);
524 strbuf_release(&rev_ctx.author);
525 strbuf_release(&rev_ctx.note);
526 strbuf_release(&node_ctx.src);
527 strbuf_release(&node_ctx.dst);
528 if (buffer_deinit(&input))
529 fprintf(stderr, "Input error\n");
531 fprintf(stderr, "Output error\n");
534 void svndump_reset(void)
536 strbuf_release(&dump_ctx.uuid);
537 strbuf_release(&dump_ctx.url);
538 strbuf_release(&rev_ctx.log);
539 strbuf_release(&rev_ctx.author);