Merge jrn/db/vcs-svn-housekeeping:vcs-svn into master
[svn-fe.git] / vcs-svn / svndump.c
blobb76a9d3a985fd9b7f1cc186f54dde7d305147a58
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 DATE_RFC2822_LEN 31
43 static struct line_buffer input = LINE_BUFFER_INIT;
45 static struct {
46 uint32_t action, srcRev, type;
47 off_t prop_length, text_length;
48 struct strbuf src, dst;
49 uint32_t text_delta, prop_delta;
50 } node_ctx;
52 static struct {
53 uint32_t revision;
54 unsigned long timestamp;
55 struct strbuf log, author;
56 } rev_ctx;
58 static struct {
59 uint32_t version;
60 struct strbuf uuid, url;
61 } dump_ctx;
63 static void reset_node_ctx(char *fname)
65 node_ctx.type = 0;
66 node_ctx.action = NODEACT_UNKNOWN;
67 node_ctx.prop_length = -1;
68 node_ctx.text_length = -1;
69 strbuf_reset(&node_ctx.src);
70 node_ctx.srcRev = 0;
71 strbuf_reset(&node_ctx.dst);
72 if (fname)
73 strbuf_addstr(&node_ctx.dst, fname);
74 node_ctx.text_delta = 0;
75 node_ctx.prop_delta = 0;
78 static void reset_rev_ctx(uint32_t revision)
80 rev_ctx.revision = revision;
81 rev_ctx.timestamp = 0;
82 strbuf_reset(&rev_ctx.log);
83 strbuf_reset(&rev_ctx.author);
86 static void reset_dump_ctx(const char *url)
88 strbuf_reset(&dump_ctx.url);
89 if (url)
90 strbuf_addstr(&dump_ctx.url, url);
91 dump_ctx.version = 1;
92 strbuf_reset(&dump_ctx.uuid);
95 static void handle_property(const struct strbuf *key_buf,
96 struct strbuf *val,
97 uint32_t *type_set)
99 struct tm tm;
100 const char *key = key_buf->buf;
101 size_t keylen = key_buf->len;
103 switch (keylen + 1) {
104 case sizeof("svn:log"):
105 if (constcmp(key, "svn:log"))
106 break;
107 if (!val)
108 die("invalid dump: unsets svn:log");
109 strbuf_swap(&rev_ctx.log, val);
110 break;
111 case sizeof("svn:author"):
112 if (constcmp(key, "svn:author"))
113 break;
114 if (!val)
115 strbuf_reset(&rev_ctx.author);
116 else
117 strbuf_swap(&rev_ctx.author, val);
118 break;
119 case sizeof("svn:date"):
120 if (constcmp(key, "svn:date"))
121 break;
122 if (!val)
123 die("invalid dump: unsets svn:date");
124 if (!strptime(val->buf, "%FT%T", &tm))
125 fprintf(stderr, "warning: " "invalid timestamp: %s", val->buf);
126 else
127 rev_ctx.timestamp = mkgmtime(&tm);
128 break;
129 case sizeof("svn:executable"):
130 case sizeof("svn:special"):
131 if (keylen == strlen("svn:executable") &&
132 constcmp(key, "svn:executable"))
133 break;
134 if (keylen == strlen("svn:special") &&
135 constcmp(key, "svn:special"))
136 break;
137 if (*type_set) {
138 if (!val)
139 return;
140 die("invalid dump: sets type twice");
142 if (!val) {
143 node_ctx.type = REPO_MODE_BLB;
144 return;
146 *type_set = 1;
147 node_ctx.type = keylen == strlen("svn:executable") ?
148 REPO_MODE_EXE :
149 REPO_MODE_LNK;
153 static void die_short_read(void)
155 if (buffer_ferror(&input))
156 die_errno("error reading dump file");
157 die("invalid dump: unexpected end of file");
160 static void read_props(void)
162 static struct strbuf key = STRBUF_INIT;
163 static struct strbuf val = STRBUF_INIT;
164 const char *t;
166 * NEEDSWORK: to support simple mode changes like
167 * K 11
168 * svn:special
169 * V 1
171 * D 14
172 * svn:executable
173 * we keep track of whether a mode has been set and reset to
174 * plain file only if not. We should be keeping track of the
175 * symlink and executable bits separately instead.
177 uint32_t type_set = 0;
178 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
179 uint32_t len;
180 const char type = t[0];
181 int ch;
183 if (!type || t[1] != ' ')
184 die("invalid property line: %s\n", t);
185 len = atoi(&t[2]);
186 strbuf_reset(&val);
187 buffer_read_binary(&input, &val, len);
188 if (val.len < len)
189 die_short_read();
191 /* Discard trailing newline. */
192 ch = buffer_read_char(&input);
193 if (ch == EOF)
194 die_short_read();
195 if (ch != '\n')
196 die("invalid dump: expected newline after %s", val.buf);
198 switch (type) {
199 case 'K':
200 strbuf_swap(&key, &val);
201 continue;
202 case 'D':
203 handle_property(&val, NULL, &type_set);
204 continue;
205 case 'V':
206 handle_property(&key, &val, &type_set);
207 strbuf_reset(&key);
208 continue;
209 default:
210 die("invalid property line: %s\n", t);
215 static void handle_node(void)
217 const uint32_t type = node_ctx.type;
218 const int have_props = node_ctx.prop_length != -1;
219 const int have_text = node_ctx.text_length != -1;
221 * Old text for this node:
222 * NULL - directory or bug
223 * empty_blob - empty
224 * "<dataref>" - data retrievable from fast-import
226 static const char *const empty_blob = "::empty::";
227 const char *old_data = NULL;
228 uint32_t old_mode = REPO_MODE_BLB;
230 if (node_ctx.action == NODEACT_DELETE) {
231 if (have_text || have_props || node_ctx.srcRev)
232 die("invalid dump: deletion node has "
233 "copyfrom info, text, or properties");
234 repo_delete(node_ctx.dst.buf);
235 return;
237 if (node_ctx.action == NODEACT_REPLACE) {
238 repo_delete(node_ctx.dst.buf);
239 node_ctx.action = NODEACT_ADD;
241 if (node_ctx.srcRev) {
242 repo_copy(node_ctx.srcRev, node_ctx.src.buf, node_ctx.dst.buf);
243 if (node_ctx.action == NODEACT_ADD)
244 node_ctx.action = NODEACT_CHANGE;
246 if (have_text && type == REPO_MODE_DIR)
247 die("invalid dump: directories cannot have text attached");
250 * Find old content (old_data) and decide on the new mode.
252 if (node_ctx.action == NODEACT_CHANGE && !*node_ctx.dst.buf) {
253 if (type != REPO_MODE_DIR)
254 die("invalid dump: root of tree is not a regular file");
255 old_data = NULL;
256 } else if (node_ctx.action == NODEACT_CHANGE) {
257 uint32_t mode;
258 old_data = repo_read_path(node_ctx.dst.buf, &mode);
259 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
260 die("invalid dump: cannot modify a directory into a file");
261 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
262 die("invalid dump: cannot modify a file into a directory");
263 node_ctx.type = mode;
264 old_mode = mode;
265 } else if (node_ctx.action == NODEACT_ADD) {
266 if (type == REPO_MODE_DIR)
267 old_data = NULL;
268 else if (have_text)
269 old_data = empty_blob;
270 else
271 die("invalid dump: adds node without text");
272 } else {
273 die("invalid dump: Node-path block lacks Node-action");
277 * Adjust mode to reflect properties.
279 if (have_props) {
280 if (!node_ctx.prop_delta)
281 node_ctx.type = type;
282 if (node_ctx.prop_length)
283 read_props();
287 * Save the result.
289 if (type == REPO_MODE_DIR) /* directories are not tracked. */
290 return;
291 assert(old_data);
292 if (old_data == empty_blob)
293 /* For the fast_export_* functions, NULL means empty. */
294 old_data = NULL;
295 if (!have_text) {
296 fast_export_modify(node_ctx.dst.buf, node_ctx.type, old_data);
297 return;
299 if (!node_ctx.text_delta) {
300 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
301 fast_export_data(node_ctx.type, node_ctx.text_length, &input);
302 return;
304 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
305 fast_export_blob_delta(node_ctx.type, old_mode, old_data,
306 node_ctx.text_length, &input);
309 static void begin_revision(void)
311 if (!rev_ctx.revision) /* revision 0 gets no git commit. */
312 return;
313 fast_export_begin_commit(rev_ctx.revision, rev_ctx.author.buf,
314 &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
315 rev_ctx.timestamp);
318 static void end_revision(void)
320 if (rev_ctx.revision)
321 fast_export_end_commit(rev_ctx.revision);
324 void svndump_read(const char *url)
326 char *val;
327 char *t;
328 uint32_t active_ctx = DUMP_CTX;
329 uint32_t len;
331 reset_dump_ctx(url);
332 while ((t = buffer_read_line(&input))) {
333 val = strchr(t, ':');
334 if (!val)
335 continue;
336 val++;
337 if (*val != ' ')
338 continue;
339 val++;
341 /* strlen(key) + 1 */
342 switch (val - t - 1) {
343 case sizeof("SVN-fs-dump-format-version"):
344 if (constcmp(t, "SVN-fs-dump-format-version"))
345 continue;
346 dump_ctx.version = atoi(val);
347 if (dump_ctx.version > 3)
348 die("expected svn dump format version <= 3, found %"PRIu32,
349 dump_ctx.version);
350 break;
351 case sizeof("UUID"):
352 if (constcmp(t, "UUID"))
353 continue;
354 strbuf_reset(&dump_ctx.uuid);
355 strbuf_addstr(&dump_ctx.uuid, val);
356 break;
357 case sizeof("Revision-number"):
358 if (constcmp(t, "Revision-number"))
359 continue;
360 if (active_ctx == NODE_CTX)
361 handle_node();
362 if (active_ctx == REV_CTX)
363 begin_revision();
364 if (active_ctx != DUMP_CTX)
365 end_revision();
366 active_ctx = REV_CTX;
367 reset_rev_ctx(atoi(val));
368 break;
369 case sizeof("Node-path"):
370 if (constcmp(t, "Node-"))
371 continue;
372 if (!constcmp(t + strlen("Node-"), "path")) {
373 if (active_ctx == NODE_CTX)
374 handle_node();
375 if (active_ctx == REV_CTX)
376 begin_revision();
377 active_ctx = NODE_CTX;
378 reset_node_ctx(val);
379 break;
381 if (constcmp(t + strlen("Node-"), "kind"))
382 continue;
383 if (!strcmp(val, "dir"))
384 node_ctx.type = REPO_MODE_DIR;
385 else if (!strcmp(val, "file"))
386 node_ctx.type = REPO_MODE_BLB;
387 else
388 fprintf(stderr, "Unknown node-kind: %s\n", val);
389 break;
390 case sizeof("Node-action"):
391 if (constcmp(t, "Node-action"))
392 continue;
393 if (!strcmp(val, "delete")) {
394 node_ctx.action = NODEACT_DELETE;
395 } else if (!strcmp(val, "add")) {
396 node_ctx.action = NODEACT_ADD;
397 } else if (!strcmp(val, "change")) {
398 node_ctx.action = NODEACT_CHANGE;
399 } else if (!strcmp(val, "replace")) {
400 node_ctx.action = NODEACT_REPLACE;
401 } else {
402 fprintf(stderr, "Unknown node-action: %s\n", val);
403 node_ctx.action = NODEACT_UNKNOWN;
405 break;
406 case sizeof("Node-copyfrom-path"):
407 if (constcmp(t, "Node-copyfrom-path"))
408 continue;
409 strbuf_reset(&node_ctx.src);
410 strbuf_addstr(&node_ctx.src, val);
411 break;
412 case sizeof("Node-copyfrom-rev"):
413 if (constcmp(t, "Node-copyfrom-rev"))
414 continue;
415 node_ctx.srcRev = atoi(val);
416 break;
417 case sizeof("Text-content-length"):
418 if (constcmp(t, "Text") && constcmp(t, "Prop"))
419 continue;
420 if (constcmp(t + 4, "-content-length"))
421 continue;
423 char *end;
424 uintmax_t len;
426 len = strtoumax(val, &end, 10);
427 if (!isdigit(*val) || *end)
428 die("invalid dump: non-numeric length %s", val);
429 if (len > maximum_signed_value_of_type(off_t))
430 die("unrepresentable length in dump: %s", val);
432 if (*t == 'T')
433 node_ctx.text_length = (off_t) len;
434 else
435 node_ctx.prop_length = (off_t) len;
436 break;
438 case sizeof("Text-delta"):
439 if (!constcmp(t, "Text-delta")) {
440 node_ctx.text_delta = !strcmp(val, "true");
441 break;
443 if (constcmp(t, "Prop-delta"))
444 continue;
445 node_ctx.prop_delta = !strcmp(val, "true");
446 break;
447 case sizeof("Content-length"):
448 if (constcmp(t, "Content-length"))
449 continue;
450 len = atoi(val);
451 t = buffer_read_line(&input);
452 if (!t)
453 die_short_read();
454 if (*t)
455 die("invalid dump: expected blank line after content length header");
456 if (active_ctx == REV_CTX) {
457 read_props();
458 } else if (active_ctx == NODE_CTX) {
459 handle_node();
460 active_ctx = INTERNODE_CTX;
461 } else {
462 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
463 if (buffer_skip_bytes(&input, len) != len)
464 die_short_read();
468 if (buffer_ferror(&input))
469 die_short_read();
470 if (active_ctx == NODE_CTX)
471 handle_node();
472 if (active_ctx == REV_CTX)
473 begin_revision();
474 if (active_ctx != DUMP_CTX)
475 end_revision();
478 int svndump_init(const char *filename)
480 if (buffer_init(&input, filename))
481 return error("cannot open %s: %s", filename, strerror(errno));
482 fast_export_init(REPORT_FILENO);
483 strbuf_init(&dump_ctx.uuid, 4096);
484 strbuf_init(&dump_ctx.url, 4096);
485 strbuf_init(&rev_ctx.log, 4096);
486 strbuf_init(&rev_ctx.author, 4096);
487 strbuf_init(&node_ctx.src, 4096);
488 strbuf_init(&node_ctx.dst, 4096);
489 reset_dump_ctx(NULL);
490 reset_rev_ctx(0);
491 reset_node_ctx(NULL);
492 return 0;
495 void svndump_deinit(void)
497 fast_export_deinit();
498 reset_dump_ctx(NULL);
499 reset_rev_ctx(0);
500 reset_node_ctx(NULL);
501 strbuf_release(&rev_ctx.log);
502 strbuf_release(&node_ctx.src);
503 strbuf_release(&node_ctx.dst);
504 if (buffer_deinit(&input))
505 fprintf(stderr, "Input error\n");
506 if (ferror(stdout))
507 fprintf(stderr, "Output error\n");
510 void svndump_reset(void)
512 strbuf_release(&dump_ctx.uuid);
513 strbuf_release(&dump_ctx.url);
514 strbuf_release(&rev_ctx.log);
515 strbuf_release(&rev_ctx.author);