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 LENGTH_UNKNOWN (~0)
38 #define DATE_RFC2822_LEN 31
40 static struct line_buffer input = LINE_BUFFER_INIT;
43 uint32_t action, propLength, srcRev, type;
45 struct strbuf src, dst;
46 uint32_t text_delta, prop_delta;
51 unsigned long timestamp;
52 struct strbuf log, author;
57 struct strbuf uuid, url;
60 static void reset_node_ctx(char *fname)
63 node_ctx.action = NODEACT_UNKNOWN;
64 node_ctx.propLength = LENGTH_UNKNOWN;
65 node_ctx.text_length = -1;
66 strbuf_reset(&node_ctx.src);
68 strbuf_reset(&node_ctx.dst);
70 strbuf_addstr(&node_ctx.dst, fname);
71 node_ctx.text_delta = 0;
72 node_ctx.prop_delta = 0;
75 static void reset_rev_ctx(uint32_t revision)
77 rev_ctx.revision = revision;
78 rev_ctx.timestamp = 0;
79 strbuf_reset(&rev_ctx.log);
80 strbuf_reset(&rev_ctx.author);
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.propLength != LENGTH_UNKNOWN;
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.propLength)
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(void)
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,
312 static void end_revision(void)
314 if (rev_ctx.revision)
315 fast_export_end_commit(rev_ctx.revision);
318 void svndump_read(const char *url)
322 uint32_t active_ctx = DUMP_CTX;
326 while ((t = buffer_read_line(&input))) {
327 val = strchr(t, ':');
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"))
340 dump_ctx.version = atoi(val);
341 if (dump_ctx.version > 3)
342 die("expected svn dump format version <= 3, found %"PRIu32,
346 if (constcmp(t, "UUID"))
348 strbuf_reset(&dump_ctx.uuid);
349 strbuf_addstr(&dump_ctx.uuid, val);
351 case sizeof("Revision-number"):
352 if (constcmp(t, "Revision-number"))
354 if (active_ctx == NODE_CTX)
356 if (active_ctx == REV_CTX)
358 if (active_ctx != DUMP_CTX)
360 active_ctx = REV_CTX;
361 reset_rev_ctx(atoi(val));
363 case sizeof("Node-path"):
364 if (prefixcmp(t, "Node-"))
366 if (!constcmp(t + strlen("Node-"), "path")) {
367 if (active_ctx == NODE_CTX)
369 if (active_ctx == REV_CTX)
371 active_ctx = NODE_CTX;
375 if (constcmp(t + strlen("Node-"), "kind"))
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;
382 fprintf(stderr, "Unknown node-kind: %s\n", val);
384 case sizeof("Node-action"):
385 if (constcmp(t, "Node-action"))
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;
396 fprintf(stderr, "Unknown node-action: %s\n", val);
397 node_ctx.action = NODEACT_UNKNOWN;
400 case sizeof("Node-copyfrom-path"):
401 if (constcmp(t, "Node-copyfrom-path"))
403 strbuf_reset(&node_ctx.src);
404 strbuf_addstr(&node_ctx.src, val);
406 case sizeof("Node-copyfrom-rev"):
407 if (constcmp(t, "Node-copyfrom-rev"))
409 node_ctx.srcRev = atoi(val);
411 case sizeof("Text-content-length"):
412 if (!constcmp(t, "Text-content-length")) {
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;
424 if (constcmp(t, "Prop-content-length"))
426 node_ctx.propLength = atoi(val);
428 case sizeof("Text-delta"):
429 if (!constcmp(t, "Text-delta")) {
430 node_ctx.text_delta = !strcmp(val, "true");
433 if (constcmp(t, "Prop-delta"))
435 node_ctx.prop_delta = !strcmp(val, "true");
437 case sizeof("Content-length"):
438 if (constcmp(t, "Content-length"))
441 t = buffer_read_line(&input);
445 die("invalid dump: expected blank line after content length header");
446 if (active_ctx == REV_CTX) {
448 } else if (active_ctx == NODE_CTX) {
450 active_ctx = INTERNODE_CTX;
452 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
453 if (buffer_skip_bytes(&input, len) != len)
458 if (buffer_ferror(&input))
460 if (active_ctx == NODE_CTX)
462 if (active_ctx == REV_CTX)
464 if (active_ctx != DUMP_CTX)
468 int svndump_init(const char *filename)
470 if (buffer_init(&input, filename))
471 return error("cannot open %s: %s", filename, strerror(errno));
472 fast_export_init(REPORT_FILENO);
473 strbuf_init(&dump_ctx.uuid, 4096);
474 strbuf_init(&dump_ctx.url, 4096);
475 strbuf_init(&rev_ctx.log, 4096);
476 strbuf_init(&rev_ctx.author, 4096);
477 strbuf_init(&node_ctx.src, 4096);
478 strbuf_init(&node_ctx.dst, 4096);
479 reset_dump_ctx(NULL);
481 reset_node_ctx(NULL);
485 void svndump_deinit(void)
487 fast_export_deinit();
488 reset_dump_ctx(NULL);
490 reset_node_ctx(NULL);
491 strbuf_release(&rev_ctx.log);
492 strbuf_release(&node_ctx.src);
493 strbuf_release(&node_ctx.dst);
494 if (buffer_deinit(&input))
495 fprintf(stderr, "Input error\n");
497 fprintf(stderr, "Output error\n");
500 void svndump_reset(void)
503 buffer_reset(&input);
504 strbuf_release(&dump_ctx.uuid);
505 strbuf_release(&dump_ctx.url);
506 strbuf_release(&rev_ctx.log);
507 strbuf_release(&rev_ctx.author);