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"
19 * Compare start of string to literal of equal length;
20 * must be guarded by length test.
22 #define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
24 #define NODEACT_REPLACE 4
25 #define NODEACT_DELETE 3
27 #define NODEACT_CHANGE 1
28 #define NODEACT_UNKNOWN 0
34 #define LENGTH_UNKNOWN (~0)
35 #define DATE_RFC2822_LEN 31
37 static struct line_buffer input = LINE_BUFFER_INIT;
40 uint32_t action, propLength, textLength, srcRev, type;
41 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
42 uint32_t text_delta, prop_delta;
47 unsigned long timestamp;
48 struct strbuf log, author;
53 struct strbuf uuid, url;
56 static void reset_node_ctx(char *fname)
59 node_ctx.action = NODEACT_UNKNOWN;
60 node_ctx.propLength = LENGTH_UNKNOWN;
61 node_ctx.textLength = LENGTH_UNKNOWN;
64 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
65 node_ctx.text_delta = 0;
66 node_ctx.prop_delta = 0;
69 static void reset_rev_ctx(uint32_t revision)
71 rev_ctx.revision = revision;
72 rev_ctx.timestamp = 0;
73 strbuf_reset(&rev_ctx.log);
74 strbuf_reset(&rev_ctx.author);
77 static void reset_dump_ctx(const char *url)
79 strbuf_reset(&dump_ctx.url);
81 strbuf_addstr(&dump_ctx.url, url);
83 strbuf_reset(&dump_ctx.uuid);
86 static void handle_property(const struct strbuf *key_buf,
90 const char *key = key_buf->buf;
91 size_t keylen = key_buf->len;
94 case sizeof("svn:log"):
95 if (constcmp(key, "svn:log"))
98 die("invalid dump: unsets svn:log");
99 strbuf_swap(&rev_ctx.log, val);
101 case sizeof("svn:author"):
102 if (constcmp(key, "svn:author"))
105 strbuf_reset(&rev_ctx.author);
107 strbuf_swap(&rev_ctx.author, val);
109 case sizeof("svn:date"):
110 if (constcmp(key, "svn:date"))
113 die("invalid dump: unsets svn:date");
114 if (parse_date_basic(val->buf, &rev_ctx.timestamp, NULL))
115 warning("invalid timestamp: %s", val->buf);
117 case sizeof("svn:executable"):
118 case sizeof("svn:special"):
119 if (keylen == strlen("svn:executable") &&
120 constcmp(key, "svn:executable"))
122 if (keylen == strlen("svn:special") &&
123 constcmp(key, "svn:special"))
128 die("invalid dump: sets type twice");
131 node_ctx.type = REPO_MODE_BLB;
135 node_ctx.type = keylen == strlen("svn:executable") ?
141 static void die_short_read(void)
143 if (buffer_ferror(&input))
144 die_errno("error reading dump file");
145 die("invalid dump: unexpected end of file");
148 static void read_props(void)
150 static struct strbuf key = STRBUF_INIT;
151 static struct strbuf val = STRBUF_INIT;
154 * NEEDSWORK: to support simple mode changes like
161 * we keep track of whether a mode has been set and reset to
162 * plain file only if not. We should be keeping track of the
163 * symlink and executable bits separately instead.
165 uint32_t type_set = 0;
166 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
168 const char type = t[0];
171 if (!type || t[1] != ' ')
172 die("invalid property line: %s\n", t);
175 buffer_read_binary(&input, &val, len);
179 /* Discard trailing newline. */
180 ch = buffer_read_char(&input);
184 die("invalid dump: expected newline after %s", val.buf);
188 strbuf_swap(&key, &val);
191 handle_property(&val, NULL, &type_set);
194 handle_property(&key, &val, &type_set);
198 die("invalid property line: %s\n", t);
203 static void handle_node(void)
206 const uint32_t type = node_ctx.type;
207 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
208 const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
210 if (node_ctx.text_delta)
211 die("text deltas not supported");
213 mark = next_blob_mark();
214 if (node_ctx.action == NODEACT_DELETE) {
215 if (have_text || have_props || node_ctx.srcRev)
216 die("invalid dump: deletion node has "
217 "copyfrom info, text, or properties");
218 repo_delete(node_ctx.dst);
221 if (node_ctx.action == NODEACT_REPLACE) {
222 repo_delete(node_ctx.dst);
223 node_ctx.action = NODEACT_ADD;
225 if (node_ctx.srcRev) {
226 repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
227 if (node_ctx.action == NODEACT_ADD)
228 node_ctx.action = NODEACT_CHANGE;
230 if (have_text && type == REPO_MODE_DIR)
231 die("invalid dump: directories cannot have text attached");
234 * Decide on the new content (mark) and mode (node_ctx.type).
236 if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
237 if (type != REPO_MODE_DIR)
238 die("invalid dump: root of tree is not a regular file");
239 } else if (node_ctx.action == NODEACT_CHANGE) {
242 mark = repo_read_path(node_ctx.dst);
243 mode = repo_read_mode(node_ctx.dst);
244 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
245 die("invalid dump: cannot modify a directory into a file");
246 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
247 die("invalid dump: cannot modify a file into a directory");
248 node_ctx.type = mode;
249 } else if (node_ctx.action == NODEACT_ADD) {
250 if (!have_text && type != REPO_MODE_DIR)
251 die("invalid dump: adds node without text");
253 die("invalid dump: Node-path block lacks Node-action");
257 * Adjust mode to reflect properties.
260 if (!node_ctx.prop_delta)
261 node_ctx.type = type;
262 if (node_ctx.propLength)
269 repo_add(node_ctx.dst, node_ctx.type, mark);
271 fast_export_blob(node_ctx.type, mark,
272 node_ctx.textLength, &input);
275 static void handle_revision(void)
277 if (rev_ctx.revision)
278 repo_commit(rev_ctx.revision, rev_ctx.author.buf,
279 &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
283 void svndump_read(const char *url)
287 uint32_t active_ctx = DUMP_CTX;
291 while ((t = buffer_read_line(&input))) {
292 val = strchr(t, ':');
300 /* strlen(key) + 1 */
301 switch (val - t - 1) {
302 case sizeof("SVN-fs-dump-format-version"):
303 if (constcmp(t, "SVN-fs-dump-format-version"))
305 dump_ctx.version = atoi(val);
306 if (dump_ctx.version > 3)
307 die("expected svn dump format version <= 3, found %"PRIu32,
311 if (constcmp(t, "UUID"))
313 strbuf_reset(&dump_ctx.uuid);
314 strbuf_addstr(&dump_ctx.uuid, val);
316 case sizeof("Revision-number"):
317 if (constcmp(t, "Revision-number"))
319 if (active_ctx == NODE_CTX)
321 if (active_ctx != DUMP_CTX)
323 active_ctx = REV_CTX;
324 reset_rev_ctx(atoi(val));
326 case sizeof("Node-path"):
327 if (prefixcmp(t, "Node-"))
329 if (!constcmp(t + strlen("Node-"), "path")) {
330 if (active_ctx == NODE_CTX)
332 active_ctx = NODE_CTX;
336 if (constcmp(t + strlen("Node-"), "kind"))
338 if (!strcmp(val, "dir"))
339 node_ctx.type = REPO_MODE_DIR;
340 else if (!strcmp(val, "file"))
341 node_ctx.type = REPO_MODE_BLB;
343 fprintf(stderr, "Unknown node-kind: %s\n", val);
345 case sizeof("Node-action"):
346 if (constcmp(t, "Node-action"))
348 if (!strcmp(val, "delete")) {
349 node_ctx.action = NODEACT_DELETE;
350 } else if (!strcmp(val, "add")) {
351 node_ctx.action = NODEACT_ADD;
352 } else if (!strcmp(val, "change")) {
353 node_ctx.action = NODEACT_CHANGE;
354 } else if (!strcmp(val, "replace")) {
355 node_ctx.action = NODEACT_REPLACE;
357 fprintf(stderr, "Unknown node-action: %s\n", val);
358 node_ctx.action = NODEACT_UNKNOWN;
361 case sizeof("Node-copyfrom-path"):
362 if (constcmp(t, "Node-copyfrom-path"))
364 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
366 case sizeof("Node-copyfrom-rev"):
367 if (constcmp(t, "Node-copyfrom-rev"))
369 node_ctx.srcRev = atoi(val);
371 case sizeof("Text-content-length"):
372 if (!constcmp(t, "Text-content-length")) {
373 node_ctx.textLength = atoi(val);
376 if (constcmp(t, "Prop-content-length"))
378 node_ctx.propLength = atoi(val);
380 case sizeof("Text-delta"):
381 if (!constcmp(t, "Text-delta")) {
382 node_ctx.text_delta = !strcmp(val, "true");
385 if (constcmp(t, "Prop-delta"))
387 node_ctx.prop_delta = !strcmp(val, "true");
389 case sizeof("Content-length"):
390 if (constcmp(t, "Content-length"))
393 t = buffer_read_line(&input);
397 die("invalid dump: expected blank line after content length header");
398 if (active_ctx == REV_CTX) {
400 } else if (active_ctx == NODE_CTX) {
402 active_ctx = REV_CTX;
404 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
405 if (buffer_skip_bytes(&input, len) != len)
410 if (buffer_ferror(&input))
412 if (active_ctx == NODE_CTX)
414 if (active_ctx != DUMP_CTX)
418 int svndump_init(const char *filename)
420 if (buffer_init(&input, filename))
421 return error("cannot open %s: %s", filename, strerror(errno));
423 strbuf_init(&dump_ctx.uuid, 4096);
424 strbuf_init(&dump_ctx.url, 4096);
425 strbuf_init(&rev_ctx.log, 4096);
426 strbuf_init(&rev_ctx.author, 4096);
427 reset_dump_ctx(NULL);
429 reset_node_ctx(NULL);
433 void svndump_deinit(void)
436 reset_dump_ctx(NULL);
438 reset_node_ctx(NULL);
439 strbuf_release(&rev_ctx.log);
440 if (buffer_deinit(&input))
441 fprintf(stderr, "Input error\n");
443 fprintf(stderr, "Output error\n");
446 void svndump_reset(void)
448 buffer_reset(&input);
450 strbuf_release(&dump_ctx.uuid);
451 strbuf_release(&dump_ctx.url);
452 strbuf_release(&rev_ctx.log);
453 strbuf_release(&rev_ctx.author);