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"
14 #include "string_pool.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 NODEACT_REPLACE 4
24 #define NODEACT_DELETE 3
26 #define NODEACT_CHANGE 1
27 #define NODEACT_UNKNOWN 0
33 #define LENGTH_UNKNOWN (~0)
34 #define DATE_RFC2822_LEN 31
36 static struct line_buffer input = LINE_BUFFER_INIT;
39 uint32_t action, propLength, textLength, srcRev, type;
40 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
41 uint32_t text_delta, prop_delta;
46 unsigned long timestamp;
47 struct strbuf log, author;
52 struct strbuf uuid, url;
55 static void reset_node_ctx(char *fname)
58 node_ctx.action = NODEACT_UNKNOWN;
59 node_ctx.propLength = LENGTH_UNKNOWN;
60 node_ctx.textLength = LENGTH_UNKNOWN;
63 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
64 node_ctx.text_delta = 0;
65 node_ctx.prop_delta = 0;
68 static void reset_rev_ctx(uint32_t revision)
70 rev_ctx.revision = revision;
71 rev_ctx.timestamp = 0;
72 strbuf_reset(&rev_ctx.log);
73 strbuf_reset(&rev_ctx.author);
76 static void reset_dump_ctx(const char *url)
78 strbuf_reset(&dump_ctx.url);
80 strbuf_addstr(&dump_ctx.url, url);
82 strbuf_reset(&dump_ctx.uuid);
85 static void handle_property(const struct strbuf *key_buf,
89 const char *key = key_buf->buf;
90 size_t keylen = key_buf->len;
93 case sizeof("svn:log"):
94 if (constcmp(key, "svn:log"))
97 die("invalid dump: unsets svn:log");
98 strbuf_swap(&rev_ctx.log, val);
100 case sizeof("svn:author"):
101 if (constcmp(key, "svn:author"))
104 strbuf_reset(&rev_ctx.author);
106 strbuf_swap(&rev_ctx.author, val);
108 case sizeof("svn:date"):
109 if (constcmp(key, "svn:date"))
112 die("invalid dump: unsets svn:date");
113 if (parse_date_basic(val->buf, &rev_ctx.timestamp, NULL))
114 warning("invalid timestamp: %s", val->buf);
116 case sizeof("svn:executable"):
117 case sizeof("svn:special"):
118 if (keylen == strlen("svn:executable") &&
119 constcmp(key, "svn:executable"))
121 if (keylen == strlen("svn:special") &&
122 constcmp(key, "svn:special"))
127 die("invalid dump: sets type twice");
130 node_ctx.type = REPO_MODE_BLB;
134 node_ctx.type = keylen == strlen("svn:executable") ?
140 static void die_short_read(void)
142 if (buffer_ferror(&input))
143 die_errno("error reading dump file");
144 die("invalid dump: unexpected end of file");
147 static void read_props(void)
149 static struct strbuf key = STRBUF_INIT;
150 static struct strbuf val = STRBUF_INIT;
153 * NEEDSWORK: to support simple mode changes like
160 * we keep track of whether a mode has been set and reset to
161 * plain file only if not. We should be keeping track of the
162 * symlink and executable bits separately instead.
164 uint32_t type_set = 0;
165 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
167 const char type = t[0];
170 if (!type || t[1] != ' ')
171 die("invalid property line: %s\n", t);
174 buffer_read_binary(&input, &val, len);
178 /* Discard trailing newline. */
179 ch = buffer_read_char(&input);
183 die("invalid dump: expected newline after %s", val.buf);
187 strbuf_swap(&key, &val);
190 handle_property(&val, NULL, &type_set);
193 handle_property(&key, &val, &type_set);
197 die("invalid property line: %s\n", t);
202 static void handle_node(void)
205 const uint32_t type = node_ctx.type;
206 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
207 const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
209 if (node_ctx.text_delta)
210 die("text deltas not supported");
212 mark = next_blob_mark();
213 if (node_ctx.action == NODEACT_DELETE) {
214 if (have_text || have_props || node_ctx.srcRev)
215 die("invalid dump: deletion node has "
216 "copyfrom info, text, or properties");
217 repo_delete(node_ctx.dst);
220 if (node_ctx.action == NODEACT_REPLACE) {
221 repo_delete(node_ctx.dst);
222 node_ctx.action = NODEACT_ADD;
224 if (node_ctx.srcRev) {
225 repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
226 if (node_ctx.action == NODEACT_ADD)
227 node_ctx.action = NODEACT_CHANGE;
229 if (have_text && type == REPO_MODE_DIR)
230 die("invalid dump: directories cannot have text attached");
233 * Decide on the new content (mark) and mode (node_ctx.type).
235 if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
236 if (type != REPO_MODE_DIR)
237 die("invalid dump: root of tree is not a regular file");
238 } else if (node_ctx.action == NODEACT_CHANGE) {
241 mark = repo_read_path(node_ctx.dst);
242 mode = repo_read_mode(node_ctx.dst);
243 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
244 die("invalid dump: cannot modify a directory into a file");
245 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
246 die("invalid dump: cannot modify a file into a directory");
247 node_ctx.type = mode;
248 } else if (node_ctx.action == NODEACT_ADD) {
249 if (!have_text && type != REPO_MODE_DIR)
250 die("invalid dump: adds node without text");
252 die("invalid dump: Node-path block lacks Node-action");
256 * Adjust mode to reflect properties.
259 if (!node_ctx.prop_delta)
260 node_ctx.type = type;
261 if (node_ctx.propLength)
268 repo_add(node_ctx.dst, node_ctx.type, mark);
270 fast_export_blob(node_ctx.type, mark,
271 node_ctx.textLength, &input);
274 static void handle_revision(void)
276 if (rev_ctx.revision)
277 repo_commit(rev_ctx.revision, rev_ctx.author.buf,
278 &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
282 void svndump_read(const char *url)
286 uint32_t active_ctx = DUMP_CTX;
290 while ((t = buffer_read_line(&input))) {
291 val = strchr(t, ':');
299 /* strlen(key) + 1 */
300 switch (val - t - 1) {
301 case sizeof("SVN-fs-dump-format-version"):
302 if (constcmp(t, "SVN-fs-dump-format-version"))
304 dump_ctx.version = atoi(val);
305 if (dump_ctx.version > 3)
306 die("expected svn dump format version <= 3, found %"PRIu32,
310 if (constcmp(t, "UUID"))
312 strbuf_reset(&dump_ctx.uuid);
313 strbuf_addstr(&dump_ctx.uuid, val);
315 case sizeof("Revision-number"):
316 if (constcmp(t, "Revision-number"))
318 if (active_ctx == NODE_CTX)
320 if (active_ctx != DUMP_CTX)
322 active_ctx = REV_CTX;
323 reset_rev_ctx(atoi(val));
325 case sizeof("Node-path"):
326 if (prefixcmp(t, "Node-"))
328 if (!constcmp(t + strlen("Node-"), "path")) {
329 if (active_ctx == NODE_CTX)
331 active_ctx = NODE_CTX;
335 if (constcmp(t + strlen("Node-"), "kind"))
337 if (!strcmp(val, "dir"))
338 node_ctx.type = REPO_MODE_DIR;
339 else if (!strcmp(val, "file"))
340 node_ctx.type = REPO_MODE_BLB;
342 fprintf(stderr, "Unknown node-kind: %s\n", val);
344 case sizeof("Node-action"):
345 if (constcmp(t, "Node-action"))
347 if (!strcmp(val, "delete")) {
348 node_ctx.action = NODEACT_DELETE;
349 } else if (!strcmp(val, "add")) {
350 node_ctx.action = NODEACT_ADD;
351 } else if (!strcmp(val, "change")) {
352 node_ctx.action = NODEACT_CHANGE;
353 } else if (!strcmp(val, "replace")) {
354 node_ctx.action = NODEACT_REPLACE;
356 fprintf(stderr, "Unknown node-action: %s\n", val);
357 node_ctx.action = NODEACT_UNKNOWN;
360 case sizeof("Node-copyfrom-path"):
361 if (constcmp(t, "Node-copyfrom-path"))
363 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
365 case sizeof("Node-copyfrom-rev"):
366 if (constcmp(t, "Node-copyfrom-rev"))
368 node_ctx.srcRev = atoi(val);
370 case sizeof("Text-content-length"):
371 if (!constcmp(t, "Text-content-length")) {
372 node_ctx.textLength = atoi(val);
375 if (constcmp(t, "Prop-content-length"))
377 node_ctx.propLength = atoi(val);
379 case sizeof("Text-delta"):
380 if (!constcmp(t, "Text-delta")) {
381 node_ctx.text_delta = !strcmp(val, "true");
384 if (constcmp(t, "Prop-delta"))
386 node_ctx.prop_delta = !strcmp(val, "true");
388 case sizeof("Content-length"):
389 if (constcmp(t, "Content-length"))
392 t = buffer_read_line(&input);
396 die("invalid dump: expected blank line after content length header");
397 if (active_ctx == REV_CTX) {
399 } else if (active_ctx == NODE_CTX) {
401 active_ctx = REV_CTX;
403 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
404 if (buffer_skip_bytes(&input, len) != len)
409 if (buffer_ferror(&input))
411 if (active_ctx == NODE_CTX)
413 if (active_ctx != DUMP_CTX)
417 int svndump_init(const char *filename)
419 if (buffer_init(&input, filename))
420 return error("cannot open %s: %s", filename, strerror(errno));
422 strbuf_init(&dump_ctx.uuid, 4096);
423 strbuf_init(&dump_ctx.url, 4096);
424 strbuf_init(&rev_ctx.log, 4096);
425 strbuf_init(&rev_ctx.author, 4096);
426 reset_dump_ctx(NULL);
428 reset_node_ctx(NULL);
432 void svndump_deinit(void)
435 reset_dump_ctx(NULL);
437 reset_node_ctx(NULL);
438 strbuf_release(&rev_ctx.log);
439 if (buffer_deinit(&input))
440 fprintf(stderr, "Input error\n");
442 fprintf(stderr, "Output error\n");
445 void svndump_reset(void)
447 buffer_reset(&input);
449 strbuf_release(&dump_ctx.uuid);
450 strbuf_release(&dump_ctx.url);
451 strbuf_release(&rev_ctx.log);
452 strbuf_release(&rev_ctx.author);