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"
15 #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 /* Create memory pool for log messages */
37 obj_pool_gen(log, char, 4096)
39 static struct line_buffer input = LINE_BUFFER_INIT;
41 static char *log_copy(uint32_t length, const char *log)
44 log_free(log_pool.size);
45 buffer = log_pointer(log_alloc(length));
46 strncpy(buffer, log, length);
51 uint32_t action, propLength, textLength, srcRev, type;
52 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
53 uint32_t text_delta, prop_delta;
57 uint32_t revision, author;
58 unsigned long timestamp;
63 uint32_t version, uuid, url;
66 static void reset_node_ctx(char *fname)
69 node_ctx.action = NODEACT_UNKNOWN;
70 node_ctx.propLength = LENGTH_UNKNOWN;
71 node_ctx.textLength = LENGTH_UNKNOWN;
74 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
75 node_ctx.text_delta = 0;
76 node_ctx.prop_delta = 0;
79 static void reset_rev_ctx(uint32_t revision)
81 rev_ctx.revision = revision;
82 rev_ctx.timestamp = 0;
87 static void reset_dump_ctx(uint32_t url)
94 static void handle_property(const struct strbuf *key_buf,
95 const char *val, uint32_t len,
98 const char *key = key_buf->buf;
99 size_t keylen = key_buf->len;
101 switch (keylen + 1) {
102 case sizeof("svn:log"):
103 if (constcmp(key, "svn:log"))
106 die("invalid dump: unsets svn:log");
107 /* Value length excludes terminating nul. */
108 rev_ctx.log = log_copy(len + 1, val);
110 case sizeof("svn:author"):
111 if (constcmp(key, "svn:author"))
113 rev_ctx.author = pool_intern(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, &rev_ctx.timestamp, NULL))
121 warning("invalid timestamp: %s", val);
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;
159 * NEEDSWORK: to support simple mode changes like
166 * we keep track of whether a mode has been set and reset to
167 * plain file only if not. We should be keeping track of the
168 * symlink and executable bits separately instead.
170 uint32_t type_set = 0;
171 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\n", t);
180 val = buffer_read_string(&input, len);
181 if (!val || strlen(val) != len)
184 /* Discard trailing newline. */
185 ch = buffer_read_char(&input);
189 die("invalid dump: expected newline after %s", val);
196 strbuf_add(&key, val, len);
204 handle_property(&key, val, len, &type_set);
208 die("invalid property line: %s\n", t);
213 static void handle_node(void)
216 const uint32_t type = node_ctx.type;
217 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
218 const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
220 if (node_ctx.text_delta)
221 die("text deltas not supported");
223 mark = next_blob_mark();
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 return repo_delete(node_ctx.dst);
230 if (node_ctx.action == NODEACT_REPLACE) {
231 repo_delete(node_ctx.dst);
232 node_ctx.action = NODEACT_ADD;
234 if (node_ctx.srcRev) {
235 repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
236 if (node_ctx.action == NODEACT_ADD)
237 node_ctx.action = NODEACT_CHANGE;
239 if (have_text && type == REPO_MODE_DIR)
240 die("invalid dump: directories cannot have text attached");
243 * Decide on the new content (mark) and mode (node_ctx.type).
245 if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
246 if (type != REPO_MODE_DIR)
247 die("invalid dump: root of tree is not a regular file");
248 } else if (node_ctx.action == NODEACT_CHANGE) {
251 mark = repo_read_path(node_ctx.dst);
252 mode = repo_read_mode(node_ctx.dst);
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;
258 } else if (node_ctx.action == NODEACT_ADD) {
259 if (!have_text && type != REPO_MODE_DIR)
260 die("invalid dump: adds node without text");
262 die("invalid dump: Node-path block lacks Node-action");
266 * Adjust mode to reflect properties.
269 if (!node_ctx.prop_delta)
270 node_ctx.type = type;
271 if (node_ctx.propLength)
278 repo_add(node_ctx.dst, node_ctx.type, mark);
280 fast_export_blob(node_ctx.type, mark,
281 node_ctx.textLength, &input);
284 static void handle_revision(void)
286 if (rev_ctx.revision)
287 repo_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log,
288 dump_ctx.uuid, dump_ctx.url, rev_ctx.timestamp);
291 void svndump_read(const char *url)
295 uint32_t active_ctx = DUMP_CTX;
298 reset_dump_ctx(pool_intern(url));
299 while ((t = buffer_read_line(&input))) {
300 val = strchr(t, ':');
308 /* strlen(key) + 1 */
309 switch (val - t - 1) {
310 case sizeof("SVN-fs-dump-format-version"):
311 if (constcmp(t, "SVN-fs-dump-format-version"))
313 dump_ctx.version = atoi(val);
314 if (dump_ctx.version > 3)
315 die("expected svn dump format version <= 3, found %"PRIu32,
319 if (constcmp(t, "UUID"))
321 dump_ctx.uuid = pool_intern(val);
323 case sizeof("Revision-number"):
324 if (constcmp(t, "Revision-number"))
326 if (active_ctx == NODE_CTX)
328 if (active_ctx != DUMP_CTX)
330 active_ctx = REV_CTX;
331 reset_rev_ctx(atoi(val));
333 case sizeof("Node-path"):
334 if (prefixcmp(t, "Node-"))
336 if (!constcmp(t + strlen("Node-"), "path")) {
337 if (active_ctx == NODE_CTX)
339 active_ctx = NODE_CTX;
343 if (constcmp(t + strlen("Node-"), "kind"))
345 if (!strcmp(val, "dir"))
346 node_ctx.type = REPO_MODE_DIR;
347 else if (!strcmp(val, "file"))
348 node_ctx.type = REPO_MODE_BLB;
350 fprintf(stderr, "Unknown node-kind: %s\n", val);
352 case sizeof("Node-action"):
353 if (constcmp(t, "Node-action"))
355 if (!strcmp(val, "delete")) {
356 node_ctx.action = NODEACT_DELETE;
357 } else if (!strcmp(val, "add")) {
358 node_ctx.action = NODEACT_ADD;
359 } else if (!strcmp(val, "change")) {
360 node_ctx.action = NODEACT_CHANGE;
361 } else if (!strcmp(val, "replace")) {
362 node_ctx.action = NODEACT_REPLACE;
364 fprintf(stderr, "Unknown node-action: %s\n", val);
365 node_ctx.action = NODEACT_UNKNOWN;
368 case sizeof("Node-copyfrom-path"):
369 if (constcmp(t, "Node-copyfrom-path"))
371 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
373 case sizeof("Node-copyfrom-rev"):
374 if (constcmp(t, "Node-copyfrom-rev"))
376 node_ctx.srcRev = atoi(val);
378 case sizeof("Text-content-length"):
379 if (!constcmp(t, "Text-content-length")) {
380 node_ctx.textLength = atoi(val);
383 if (constcmp(t, "Prop-content-length"))
385 node_ctx.propLength = atoi(val);
387 case sizeof("Text-delta"):
388 if (!constcmp(t, "Text-delta")) {
389 node_ctx.text_delta = !strcmp(val, "true");
392 if (constcmp(t, "Prop-delta"))
394 node_ctx.prop_delta = !strcmp(val, "true");
396 case sizeof("Content-length"):
397 if (constcmp(t, "Content-length"))
400 t = buffer_read_line(&input);
404 die("invalid dump: expected blank line after content length header");
405 if (active_ctx == REV_CTX) {
407 } else if (active_ctx == NODE_CTX) {
409 active_ctx = REV_CTX;
411 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
412 if (buffer_skip_bytes(&input, len) != len)
417 if (buffer_ferror(&input))
419 if (active_ctx == NODE_CTX)
421 if (active_ctx != DUMP_CTX)
425 int svndump_init(const char *filename)
427 if (buffer_init(&input, filename))
428 return error("cannot open %s: %s", filename, strerror(errno));
432 reset_node_ctx(NULL);
436 void svndump_deinit(void)
442 reset_node_ctx(NULL);
443 if (buffer_deinit(&input))
444 fprintf(stderr, "Input error\n");
446 fprintf(stderr, "Output error\n");
449 void svndump_reset(void)
452 buffer_reset(&input);
456 reset_node_ctx(NULL);