Merge git_v1.7.10.2:vcs-svn into master
[svn-fe.git] / vcs-svn / svndump.c
blob94af8bc1421597ee9bf510749e57bfd467c541fc
1 /*
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.
8 */
10 #include <time.h>
11 #include <ctype.h>
13 #include "compat-util.h"
14 #include "repo_tree.h"
15 #include "fast_export.h"
16 #include "line_buffer.h"
17 #include "strbuf.h"
18 #include "mkgmtime.h"
19 #include "svndump.h"
22 * Compare start of string to literal of equal length;
23 * must be guarded by length test.
25 #define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
27 #define REPORT_FILENO 3
29 #define NODEACT_REPLACE 4
30 #define NODEACT_DELETE 3
31 #define NODEACT_ADD 2
32 #define NODEACT_CHANGE 1
33 #define NODEACT_UNKNOWN 0
35 /* States: */
36 #define DUMP_CTX 0 /* dump metadata */
37 #define REV_CTX 1 /* revision metadata */
38 #define NODE_CTX 2 /* node metadata */
39 #define INTERNODE_CTX 3 /* between nodes */
41 #define LENGTH_UNKNOWN (~0)
42 #define DATE_RFC2822_LEN 31
44 static struct line_buffer input = LINE_BUFFER_INIT;
46 static struct {
47 uint32_t action, propLength, srcRev, type;
48 off_t text_length;
49 struct strbuf src, dst;
50 uint32_t text_delta, prop_delta;
51 } node_ctx;
53 static struct {
54 uint32_t revision;
55 unsigned long timestamp;
56 struct strbuf log, author;
57 } rev_ctx;
59 static struct {
60 uint32_t version;
61 struct strbuf uuid, url;
62 } dump_ctx;
64 static void reset_node_ctx(char *fname)
66 node_ctx.type = 0;
67 node_ctx.action = NODEACT_UNKNOWN;
68 node_ctx.propLength = LENGTH_UNKNOWN;
69 node_ctx.text_length = -1;
70 strbuf_reset(&node_ctx.src);
71 node_ctx.srcRev = 0;
72 strbuf_reset(&node_ctx.dst);
73 if (fname)
74 strbuf_addstr(&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;
83 strbuf_reset(&rev_ctx.log);
84 strbuf_reset(&rev_ctx.author);
87 static void reset_dump_ctx(const char *url)
89 strbuf_reset(&dump_ctx.url);
90 if (url)
91 strbuf_addstr(&dump_ctx.url, url);
92 dump_ctx.version = 1;
93 strbuf_reset(&dump_ctx.uuid);
96 static void handle_property(const struct strbuf *key_buf,
97 struct strbuf *val,
98 uint32_t *type_set)
100 struct tm tm;
101 const char *key = key_buf->buf;
102 size_t keylen = key_buf->len;
104 switch (keylen + 1) {
105 case sizeof("svn:log"):
106 if (constcmp(key, "svn:log"))
107 break;
108 if (!val)
109 die("invalid dump: unsets svn:log");
110 strbuf_swap(&rev_ctx.log, val);
111 break;
112 case sizeof("svn:author"):
113 if (constcmp(key, "svn:author"))
114 break;
115 if (!val)
116 strbuf_reset(&rev_ctx.author);
117 else
118 strbuf_swap(&rev_ctx.author, val);
119 break;
120 case sizeof("svn:date"):
121 if (constcmp(key, "svn:date"))
122 break;
123 if (!val)
124 die("invalid dump: unsets svn:date");
125 if (!strptime(val->buf, "%FT%T", &tm))
126 fprintf(stderr, "warning: " "invalid timestamp: %s", val->buf);
127 else
128 rev_ctx.timestamp = mkgmtime(&tm);
129 break;
130 case sizeof("svn:executable"):
131 case sizeof("svn:special"):
132 if (keylen == strlen("svn:executable") &&
133 constcmp(key, "svn:executable"))
134 break;
135 if (keylen == strlen("svn:special") &&
136 constcmp(key, "svn:special"))
137 break;
138 if (*type_set) {
139 if (!val)
140 return;
141 die("invalid dump: sets type twice");
143 if (!val) {
144 node_ctx.type = REPO_MODE_BLB;
145 return;
147 *type_set = 1;
148 node_ctx.type = keylen == strlen("svn:executable") ?
149 REPO_MODE_EXE :
150 REPO_MODE_LNK;
154 static void die_short_read(void)
156 if (buffer_ferror(&input))
157 die_errno("error reading dump file");
158 die("invalid dump: unexpected end of file");
161 static void read_props(void)
163 static struct strbuf key = STRBUF_INIT;
164 static struct strbuf val = STRBUF_INIT;
165 const char *t;
167 * NEEDSWORK: to support simple mode changes like
168 * K 11
169 * svn:special
170 * V 1
172 * D 14
173 * svn:executable
174 * we keep track of whether a mode has been set and reset to
175 * plain file only if not. We should be keeping track of the
176 * symlink and executable bits separately instead.
178 uint32_t type_set = 0;
179 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
180 uint32_t len;
181 const char type = t[0];
182 int ch;
184 if (!type || t[1] != ' ')
185 die("invalid property line: %s\n", t);
186 len = atoi(&t[2]);
187 strbuf_reset(&val);
188 buffer_read_binary(&input, &val, len);
189 if (val.len < len)
190 die_short_read();
192 /* Discard trailing newline. */
193 ch = buffer_read_char(&input);
194 if (ch == EOF)
195 die_short_read();
196 if (ch != '\n')
197 die("invalid dump: expected newline after %s", val.buf);
199 switch (type) {
200 case 'K':
201 strbuf_swap(&key, &val);
202 continue;
203 case 'D':
204 handle_property(&val, NULL, &type_set);
205 continue;
206 case 'V':
207 handle_property(&key, &val, &type_set);
208 strbuf_reset(&key);
209 continue;
210 default:
211 die("invalid property line: %s\n", t);
216 static void handle_node(void)
218 const uint32_t type = node_ctx.type;
219 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
220 const int have_text = node_ctx.text_length != -1;
222 * Old text for this node:
223 * NULL - directory or bug
224 * empty_blob - empty
225 * "<dataref>" - data retrievable from fast-import
227 static const char *const empty_blob = "::empty::";
228 const char *old_data = NULL;
229 uint32_t old_mode = REPO_MODE_BLB;
231 if (node_ctx.action == NODEACT_DELETE) {
232 if (have_text || have_props || node_ctx.srcRev)
233 die("invalid dump: deletion node has "
234 "copyfrom info, text, or properties");
235 repo_delete(node_ctx.dst.buf);
236 return;
238 if (node_ctx.action == NODEACT_REPLACE) {
239 repo_delete(node_ctx.dst.buf);
240 node_ctx.action = NODEACT_ADD;
242 if (node_ctx.srcRev) {
243 repo_copy(node_ctx.srcRev, node_ctx.src.buf, node_ctx.dst.buf);
244 if (node_ctx.action == NODEACT_ADD)
245 node_ctx.action = NODEACT_CHANGE;
247 if (have_text && type == REPO_MODE_DIR)
248 die("invalid dump: directories cannot have text attached");
251 * Find old content (old_data) and decide on the new mode.
253 if (node_ctx.action == NODEACT_CHANGE && !*node_ctx.dst.buf) {
254 if (type != REPO_MODE_DIR)
255 die("invalid dump: root of tree is not a regular file");
256 old_data = NULL;
257 } else if (node_ctx.action == NODEACT_CHANGE) {
258 uint32_t mode;
259 old_data = repo_read_path(node_ctx.dst.buf, &mode);
260 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
261 die("invalid dump: cannot modify a directory into a file");
262 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
263 die("invalid dump: cannot modify a file into a directory");
264 node_ctx.type = mode;
265 old_mode = mode;
266 } else if (node_ctx.action == NODEACT_ADD) {
267 if (type == REPO_MODE_DIR)
268 old_data = NULL;
269 else if (have_text)
270 old_data = empty_blob;
271 else
272 die("invalid dump: adds node without text");
273 } else {
274 die("invalid dump: Node-path block lacks Node-action");
278 * Adjust mode to reflect properties.
280 if (have_props) {
281 if (!node_ctx.prop_delta)
282 node_ctx.type = type;
283 if (node_ctx.propLength)
284 read_props();
288 * Save the result.
290 if (type == REPO_MODE_DIR) /* directories are not tracked. */
291 return;
292 assert(old_data);
293 if (old_data == empty_blob)
294 /* For the fast_export_* functions, NULL means empty. */
295 old_data = NULL;
296 if (!have_text) {
297 fast_export_modify(node_ctx.dst.buf, node_ctx.type, old_data);
298 return;
300 if (!node_ctx.text_delta) {
301 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
302 fast_export_data(node_ctx.type, node_ctx.text_length, &input);
303 return;
305 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
306 fast_export_blob_delta(node_ctx.type, old_mode, old_data,
307 node_ctx.text_length, &input);
310 static void begin_revision(void)
312 if (!rev_ctx.revision) /* revision 0 gets no git commit. */
313 return;
314 fast_export_begin_commit(rev_ctx.revision, rev_ctx.author.buf,
315 &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
316 rev_ctx.timestamp);
319 static void end_revision(void)
321 if (rev_ctx.revision)
322 fast_export_end_commit(rev_ctx.revision);
325 void svndump_read(const char *url)
327 char *val;
328 char *t;
329 uint32_t active_ctx = DUMP_CTX;
330 uint32_t len;
332 reset_dump_ctx(url);
333 while ((t = buffer_read_line(&input))) {
334 val = strchr(t, ':');
335 if (!val)
336 continue;
337 val++;
338 if (*val != ' ')
339 continue;
340 val++;
342 /* strlen(key) + 1 */
343 switch (val - t - 1) {
344 case sizeof("SVN-fs-dump-format-version"):
345 if (constcmp(t, "SVN-fs-dump-format-version"))
346 continue;
347 dump_ctx.version = atoi(val);
348 if (dump_ctx.version > 3)
349 die("expected svn dump format version <= 3, found %"PRIu32,
350 dump_ctx.version);
351 break;
352 case sizeof("UUID"):
353 if (constcmp(t, "UUID"))
354 continue;
355 strbuf_reset(&dump_ctx.uuid);
356 strbuf_addstr(&dump_ctx.uuid, val);
357 break;
358 case sizeof("Revision-number"):
359 if (constcmp(t, "Revision-number"))
360 continue;
361 if (active_ctx == NODE_CTX)
362 handle_node();
363 if (active_ctx == REV_CTX)
364 begin_revision();
365 if (active_ctx != DUMP_CTX)
366 end_revision();
367 active_ctx = REV_CTX;
368 reset_rev_ctx(atoi(val));
369 break;
370 case sizeof("Node-path"):
371 if (prefixcmp(t, "Node-"))
372 continue;
373 if (!constcmp(t + strlen("Node-"), "path")) {
374 if (active_ctx == NODE_CTX)
375 handle_node();
376 if (active_ctx == REV_CTX)
377 begin_revision();
378 active_ctx = NODE_CTX;
379 reset_node_ctx(val);
380 break;
382 if (constcmp(t + strlen("Node-"), "kind"))
383 continue;
384 if (!strcmp(val, "dir"))
385 node_ctx.type = REPO_MODE_DIR;
386 else if (!strcmp(val, "file"))
387 node_ctx.type = REPO_MODE_BLB;
388 else
389 fprintf(stderr, "Unknown node-kind: %s\n", val);
390 break;
391 case sizeof("Node-action"):
392 if (constcmp(t, "Node-action"))
393 continue;
394 if (!strcmp(val, "delete")) {
395 node_ctx.action = NODEACT_DELETE;
396 } else if (!strcmp(val, "add")) {
397 node_ctx.action = NODEACT_ADD;
398 } else if (!strcmp(val, "change")) {
399 node_ctx.action = NODEACT_CHANGE;
400 } else if (!strcmp(val, "replace")) {
401 node_ctx.action = NODEACT_REPLACE;
402 } else {
403 fprintf(stderr, "Unknown node-action: %s\n", val);
404 node_ctx.action = NODEACT_UNKNOWN;
406 break;
407 case sizeof("Node-copyfrom-path"):
408 if (constcmp(t, "Node-copyfrom-path"))
409 continue;
410 strbuf_reset(&node_ctx.src);
411 strbuf_addstr(&node_ctx.src, val);
412 break;
413 case sizeof("Node-copyfrom-rev"):
414 if (constcmp(t, "Node-copyfrom-rev"))
415 continue;
416 node_ctx.srcRev = atoi(val);
417 break;
418 case sizeof("Text-content-length"):
419 if (!constcmp(t, "Text-content-length")) {
420 char *end;
421 uintmax_t textlen;
423 textlen = strtoumax(val, &end, 10);
424 if (!isdigit(*val) || *end)
425 die("invalid dump: non-numeric length %s", val);
426 if (textlen > maximum_signed_value_of_type(off_t))
427 die("unrepresentable length in dump: %s", val);
428 node_ctx.text_length = (off_t) textlen;
429 break;
431 if (constcmp(t, "Prop-content-length"))
432 continue;
433 node_ctx.propLength = atoi(val);
434 break;
435 case sizeof("Text-delta"):
436 if (!constcmp(t, "Text-delta")) {
437 node_ctx.text_delta = !strcmp(val, "true");
438 break;
440 if (constcmp(t, "Prop-delta"))
441 continue;
442 node_ctx.prop_delta = !strcmp(val, "true");
443 break;
444 case sizeof("Content-length"):
445 if (constcmp(t, "Content-length"))
446 continue;
447 len = atoi(val);
448 t = buffer_read_line(&input);
449 if (!t)
450 die_short_read();
451 if (*t)
452 die("invalid dump: expected blank line after content length header");
453 if (active_ctx == REV_CTX) {
454 read_props();
455 } else if (active_ctx == NODE_CTX) {
456 handle_node();
457 active_ctx = INTERNODE_CTX;
458 } else {
459 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
460 if (buffer_skip_bytes(&input, len) != len)
461 die_short_read();
465 if (buffer_ferror(&input))
466 die_short_read();
467 if (active_ctx == NODE_CTX)
468 handle_node();
469 if (active_ctx == REV_CTX)
470 begin_revision();
471 if (active_ctx != DUMP_CTX)
472 end_revision();
475 int svndump_init(const char *filename)
477 if (buffer_init(&input, filename))
478 return error("cannot open %s: %s", filename, strerror(errno));
479 fast_export_init(REPORT_FILENO);
480 strbuf_init(&dump_ctx.uuid, 4096);
481 strbuf_init(&dump_ctx.url, 4096);
482 strbuf_init(&rev_ctx.log, 4096);
483 strbuf_init(&rev_ctx.author, 4096);
484 strbuf_init(&node_ctx.src, 4096);
485 strbuf_init(&node_ctx.dst, 4096);
486 reset_dump_ctx(NULL);
487 reset_rev_ctx(0);
488 reset_node_ctx(NULL);
489 return 0;
492 void svndump_deinit(void)
494 fast_export_deinit();
495 reset_dump_ctx(NULL);
496 reset_rev_ctx(0);
497 reset_node_ctx(NULL);
498 strbuf_release(&rev_ctx.log);
499 strbuf_release(&node_ctx.src);
500 strbuf_release(&node_ctx.dst);
501 if (buffer_deinit(&input))
502 fprintf(stderr, "Input error\n");
503 if (ferror(stdout))
504 fprintf(stderr, "Output error\n");
507 void svndump_reset(void)
509 fast_export_reset();
510 buffer_reset(&input);
511 strbuf_release(&dump_ctx.uuid);
512 strbuf_release(&dump_ctx.url);
513 strbuf_release(&rev_ctx.log);
514 strbuf_release(&rev_ctx.author);