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"
17 #define REPORT_FILENO 3
19 #define NODEACT_REPLACE 4
20 #define NODEACT_DELETE 3
22 #define NODEACT_CHANGE 1
23 #define NODEACT_UNKNOWN 0
26 #define DUMP_CTX 0 /* dump metadata */
27 #define REV_CTX 1 /* revision metadata */
28 #define NODE_CTX 2 /* node metadata */
29 #define INTERNODE_CTX 3 /* between nodes */
31 #define LENGTH_UNKNOWN (~0)
32 #define DATE_RFC2822_LEN 31
34 /* Create memory pool for log messages */
35 obj_pool_gen(log, char, 4096)
37 static struct line_buffer input = LINE_BUFFER_INIT;
39 static char *log_copy(uint32_t length, const char *log)
42 log_free(log_pool.size);
43 buffer = log_pointer(log_alloc(length));
44 strncpy(buffer, log, length);
49 uint32_t action, propLength, textLength, srcRev, type;
50 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
51 uint32_t text_delta, prop_delta;
55 uint32_t revision, author;
56 unsigned long timestamp;
61 uint32_t version, uuid, url;
65 uint32_t svn_log, svn_author, svn_date, svn_executable, svn_special, uuid,
66 revision_number, node_path, node_kind, node_action,
67 node_copyfrom_path, node_copyfrom_rev, text_content_length,
68 prop_content_length, content_length, svn_fs_dump_format_version,
69 /* version 3 format */
70 text_delta, prop_delta;
73 static void reset_node_ctx(char *fname)
76 node_ctx.action = NODEACT_UNKNOWN;
77 node_ctx.propLength = LENGTH_UNKNOWN;
78 node_ctx.textLength = LENGTH_UNKNOWN;
81 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
82 node_ctx.text_delta = 0;
83 node_ctx.prop_delta = 0;
86 static void reset_rev_ctx(uint32_t revision)
88 rev_ctx.revision = revision;
89 rev_ctx.timestamp = 0;
94 static void reset_dump_ctx(uint32_t url)
101 static void init_keys(void)
103 keys.svn_log = pool_intern("svn:log");
104 keys.svn_author = pool_intern("svn:author");
105 keys.svn_date = pool_intern("svn:date");
106 keys.svn_executable = pool_intern("svn:executable");
107 keys.svn_special = pool_intern("svn:special");
108 keys.uuid = pool_intern("UUID");
109 keys.revision_number = pool_intern("Revision-number");
110 keys.node_path = pool_intern("Node-path");
111 keys.node_kind = pool_intern("Node-kind");
112 keys.node_action = pool_intern("Node-action");
113 keys.node_copyfrom_path = pool_intern("Node-copyfrom-path");
114 keys.node_copyfrom_rev = pool_intern("Node-copyfrom-rev");
115 keys.text_content_length = pool_intern("Text-content-length");
116 keys.prop_content_length = pool_intern("Prop-content-length");
117 keys.content_length = pool_intern("Content-length");
118 keys.svn_fs_dump_format_version = pool_intern("SVN-fs-dump-format-version");
119 /* version 3 format (Subversion 1.1.0) */
120 keys.text_delta = pool_intern("Text-delta");
121 keys.prop_delta = pool_intern("Prop-delta");
124 static void handle_property(uint32_t key, const char *val, uint32_t len,
127 if (key == keys.svn_log) {
129 die("invalid dump: unsets svn:log");
130 /* Value length excludes terminating nul. */
131 rev_ctx.log = log_copy(len + 1, val);
132 } else if (key == keys.svn_author) {
133 rev_ctx.author = pool_intern(val);
134 } else if (key == keys.svn_date) {
136 die("invalid dump: unsets svn:date");
137 if (parse_date_basic(val, &rev_ctx.timestamp, NULL))
138 warning("invalid timestamp: %s", val);
139 } else if (key == keys.svn_executable || key == keys.svn_special) {
143 die("invalid dump: sets type twice");
146 node_ctx.type = REPO_MODE_BLB;
150 node_ctx.type = key == keys.svn_executable ?
156 static void read_props(void)
161 * NEEDSWORK: to support simple mode changes like
168 * we keep track of whether a mode has been set and reset to
169 * plain file only if not. We should be keeping track of the
170 * symlink and executable bits separately instead.
172 uint32_t type_set = 0;
173 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
176 const char type = t[0];
178 if (!type || t[1] != ' ')
179 die("invalid property line: %s\n", t);
181 val = buffer_read_string(&input, len);
182 buffer_skip_bytes(&input, 1); /* Discard trailing newline. */
186 key = pool_intern(val);
189 key = pool_intern(val);
194 handle_property(key, val, len, &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 return 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 begin_revision(void)
276 if (!rev_ctx.revision) /* revision 0 gets no git commit. */
278 fast_export_begin_commit(rev_ctx.revision);
281 static void end_revision(void)
283 if (rev_ctx.revision)
284 repo_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log,
285 dump_ctx.uuid, dump_ctx.url, rev_ctx.timestamp);
288 void svndump_read(const char *url)
292 uint32_t active_ctx = DUMP_CTX;
296 reset_dump_ctx(pool_intern(url));
297 while ((t = buffer_read_line(&input))) {
298 val = strstr(t, ": ");
303 key = pool_intern(t);
305 if (key == keys.svn_fs_dump_format_version) {
306 dump_ctx.version = atoi(val);
307 if (dump_ctx.version > 3)
308 die("expected svn dump format version <= 3, found %"PRIu32,
310 } else if (key == keys.uuid) {
311 dump_ctx.uuid = pool_intern(val);
312 } else if (key == keys.revision_number) {
313 if (active_ctx == NODE_CTX)
315 if (active_ctx == REV_CTX)
317 if (active_ctx != DUMP_CTX)
319 active_ctx = REV_CTX;
320 reset_rev_ctx(atoi(val));
321 } else if (key == keys.node_path) {
322 if (active_ctx == NODE_CTX)
324 if (active_ctx == REV_CTX)
326 active_ctx = NODE_CTX;
328 } else if (key == keys.node_kind) {
329 if (!strcmp(val, "dir"))
330 node_ctx.type = REPO_MODE_DIR;
331 else if (!strcmp(val, "file"))
332 node_ctx.type = REPO_MODE_BLB;
334 fprintf(stderr, "Unknown node-kind: %s\n", val);
335 } else if (key == keys.node_action) {
336 if (!strcmp(val, "delete")) {
337 node_ctx.action = NODEACT_DELETE;
338 } else if (!strcmp(val, "add")) {
339 node_ctx.action = NODEACT_ADD;
340 } else if (!strcmp(val, "change")) {
341 node_ctx.action = NODEACT_CHANGE;
342 } else if (!strcmp(val, "replace")) {
343 node_ctx.action = NODEACT_REPLACE;
345 fprintf(stderr, "Unknown node-action: %s\n", val);
346 node_ctx.action = NODEACT_UNKNOWN;
348 } else if (key == keys.node_copyfrom_path) {
349 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
350 } else if (key == keys.node_copyfrom_rev) {
351 node_ctx.srcRev = atoi(val);
352 } else if (key == keys.text_content_length) {
353 node_ctx.textLength = atoi(val);
354 } else if (key == keys.prop_content_length) {
355 node_ctx.propLength = atoi(val);
356 } else if (key == keys.text_delta) {
357 node_ctx.text_delta = !strcmp(val, "true");
358 } else if (key == keys.prop_delta) {
359 node_ctx.prop_delta = !strcmp(val, "true");
360 } else if (key == keys.content_length) {
362 buffer_read_line(&input);
363 if (active_ctx == REV_CTX) {
365 } else if (active_ctx == NODE_CTX) {
367 active_ctx = INTERNODE_CTX;
369 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
370 buffer_skip_bytes(&input, len);
374 if (active_ctx == NODE_CTX)
376 if (active_ctx == REV_CTX)
378 if (active_ctx != DUMP_CTX)
382 int svndump_init(const char *filename)
384 if (buffer_init(&input, filename))
385 return error("cannot open %s: %s", filename, strerror(errno));
387 fast_export_init(REPORT_FILENO);
390 reset_node_ctx(NULL);
395 void svndump_deinit(void)
398 fast_export_deinit();
402 reset_node_ctx(NULL);
403 if (buffer_deinit(&input))
404 fprintf(stderr, "Input error\n");
406 fprintf(stderr, "Output error\n");
409 void svndump_reset(void)
413 buffer_reset(&input);
417 reset_node_ctx(NULL);