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"
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 REPORT_FILENO 3
25 #define NODEACT_REPLACE 4
26 #define NODEACT_DELETE 3
28 #define NODEACT_CHANGE 1
29 #define NODEACT_UNKNOWN 0
32 #define DUMP_CTX 0 /* dump metadata */
33 #define REV_CTX 1 /* revision metadata */
34 #define NODE_CTX 2 /* node metadata */
35 #define INTERNODE_CTX 3 /* between nodes */
37 #define DATE_RFC2822_LEN 31
39 static struct line_buffer input
= LINE_BUFFER_INIT
;
42 uint32_t action
, srcRev
, type
;
43 off_t prop_length
, text_length
;
44 struct strbuf src
, dst
;
45 uint32_t text_delta
, prop_delta
;
50 unsigned long timestamp
;
51 struct strbuf log
, author
;
56 struct strbuf uuid
, url
;
59 static void reset_node_ctx(char *fname
)
62 node_ctx
.action
= NODEACT_UNKNOWN
;
63 node_ctx
.prop_length
= -1;
64 node_ctx
.text_length
= -1;
65 strbuf_reset(&node_ctx
.src
);
67 strbuf_reset(&node_ctx
.dst
);
69 strbuf_addstr(&node_ctx
.dst
, fname
);
70 node_ctx
.text_delta
= 0;
71 node_ctx
.prop_delta
= 0;
74 static void reset_rev_ctx(uint32_t revision
)
76 rev_ctx
.revision
= revision
;
77 rev_ctx
.timestamp
= 0;
78 strbuf_reset(&rev_ctx
.log
);
79 strbuf_reset(&rev_ctx
.author
);
82 static void reset_dump_ctx(const char *url
)
84 strbuf_reset(&dump_ctx
.url
);
86 strbuf_addstr(&dump_ctx
.url
, url
);
88 strbuf_reset(&dump_ctx
.uuid
);
91 static void handle_property(const struct strbuf
*key_buf
,
95 const char *key
= key_buf
->buf
;
96 size_t keylen
= key_buf
->len
;
99 case sizeof("svn:log"):
100 if (constcmp(key
, "svn:log"))
103 die("invalid dump: unsets svn:log");
104 strbuf_swap(&rev_ctx
.log
, val
);
106 case sizeof("svn:author"):
107 if (constcmp(key
, "svn:author"))
110 strbuf_reset(&rev_ctx
.author
);
112 strbuf_swap(&rev_ctx
.author
, val
);
114 case sizeof("svn:date"):
115 if (constcmp(key
, "svn:date"))
118 die("invalid dump: unsets svn:date");
119 if (parse_date_basic(val
->buf
, &rev_ctx
.timestamp
, NULL
))
120 warning("invalid timestamp: %s", val
->buf
);
122 case sizeof("svn:executable"):
123 case sizeof("svn:special"):
124 if (keylen
== strlen("svn:executable") &&
125 constcmp(key
, "svn:executable"))
127 if (keylen
== strlen("svn:special") &&
128 constcmp(key
, "svn:special"))
133 die("invalid dump: sets type twice");
136 node_ctx
.type
= REPO_MODE_BLB
;
140 node_ctx
.type
= keylen
== strlen("svn:executable") ?
146 static void die_short_read(void)
148 if (buffer_ferror(&input
))
149 die_errno("error reading dump file");
150 die("invalid dump: unexpected end of file");
153 static void read_props(void)
155 static struct strbuf key
= STRBUF_INIT
;
156 static struct strbuf val
= 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")) {
173 const char type
= t
[0];
176 if (!type
|| t
[1] != ' ')
177 die("invalid property line: %s", t
);
180 buffer_read_binary(&input
, &val
, len
);
184 /* Discard trailing newline. */
185 ch
= buffer_read_char(&input
);
189 die("invalid dump: expected newline after %s", val
.buf
);
193 strbuf_swap(&key
, &val
);
196 handle_property(&val
, NULL
, &type_set
);
199 handle_property(&key
, &val
, &type_set
);
203 die("invalid property line: %s", t
);
208 static void handle_node(void)
210 const uint32_t type
= node_ctx
.type
;
211 const int have_props
= node_ctx
.prop_length
!= -1;
212 const int have_text
= node_ctx
.text_length
!= -1;
214 * Old text for this node:
215 * NULL - directory or bug
217 * "<dataref>" - data retrievable from fast-import
219 static const char *const empty_blob
= "::empty::";
220 const char *old_data
= NULL
;
221 uint32_t old_mode
= REPO_MODE_BLB
;
223 if (node_ctx
.action
== NODEACT_DELETE
) {
224 if (have_text
|| have_props
|| node_ctx
.srcRev
)
225 die("invalid dump: deletion node has "
226 "copyfrom info, text, or properties");
227 repo_delete(node_ctx
.dst
.buf
);
230 if (node_ctx
.action
== NODEACT_REPLACE
) {
231 repo_delete(node_ctx
.dst
.buf
);
232 node_ctx
.action
= NODEACT_ADD
;
234 if (node_ctx
.srcRev
) {
235 repo_copy(node_ctx
.srcRev
, node_ctx
.src
.buf
, node_ctx
.dst
.buf
);
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 * Find old content (old_data) and decide on the new mode.
245 if (node_ctx
.action
== NODEACT_CHANGE
&& !*node_ctx
.dst
.buf
) {
246 if (type
!= REPO_MODE_DIR
)
247 die("invalid dump: root of tree is not a regular file");
249 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
251 old_data
= repo_read_path(node_ctx
.dst
.buf
, &mode
);
252 if (mode
== REPO_MODE_DIR
&& type
!= REPO_MODE_DIR
)
253 die("invalid dump: cannot modify a directory into a file");
254 if (mode
!= REPO_MODE_DIR
&& type
== REPO_MODE_DIR
)
255 die("invalid dump: cannot modify a file into a directory");
256 node_ctx
.type
= mode
;
258 } else if (node_ctx
.action
== NODEACT_ADD
) {
259 if (type
== REPO_MODE_DIR
)
262 old_data
= empty_blob
;
264 die("invalid dump: adds node without text");
266 die("invalid dump: Node-path block lacks Node-action");
270 * Adjust mode to reflect properties.
273 if (!node_ctx
.prop_delta
)
274 node_ctx
.type
= type
;
275 if (node_ctx
.prop_length
)
282 if (type
== REPO_MODE_DIR
) /* directories are not tracked. */
285 if (old_data
== empty_blob
)
286 /* For the fast_export_* functions, NULL means empty. */
289 fast_export_modify(node_ctx
.dst
.buf
, node_ctx
.type
, old_data
);
292 if (!node_ctx
.text_delta
) {
293 fast_export_modify(node_ctx
.dst
.buf
, node_ctx
.type
, "inline");
294 fast_export_data(node_ctx
.type
, node_ctx
.text_length
, &input
);
297 fast_export_modify(node_ctx
.dst
.buf
, node_ctx
.type
, "inline");
298 fast_export_blob_delta(node_ctx
.type
, old_mode
, old_data
,
299 node_ctx
.text_length
, &input
);
302 static void begin_revision(void)
304 if (!rev_ctx
.revision
) /* revision 0 gets no git commit. */
306 fast_export_begin_commit(rev_ctx
.revision
, rev_ctx
.author
.buf
,
307 &rev_ctx
.log
, dump_ctx
.uuid
.buf
, dump_ctx
.url
.buf
,
311 static void end_revision(void)
313 if (rev_ctx
.revision
)
314 fast_export_end_commit(rev_ctx
.revision
);
317 void svndump_read(const char *url
)
321 uint32_t active_ctx
= DUMP_CTX
;
325 while ((t
= buffer_read_line(&input
))) {
326 val
= strchr(t
, ':');
334 /* strlen(key) + 1 */
335 switch (val
- t
- 1) {
336 case sizeof("SVN-fs-dump-format-version"):
337 if (constcmp(t
, "SVN-fs-dump-format-version"))
339 dump_ctx
.version
= atoi(val
);
340 if (dump_ctx
.version
> 3)
341 die("expected svn dump format version <= 3, found %"PRIu32
,
345 if (constcmp(t
, "UUID"))
347 strbuf_reset(&dump_ctx
.uuid
);
348 strbuf_addstr(&dump_ctx
.uuid
, val
);
350 case sizeof("Revision-number"):
351 if (constcmp(t
, "Revision-number"))
353 if (active_ctx
== NODE_CTX
)
355 if (active_ctx
== REV_CTX
)
357 if (active_ctx
!= DUMP_CTX
)
359 active_ctx
= REV_CTX
;
360 reset_rev_ctx(atoi(val
));
362 case sizeof("Node-path"):
363 if (constcmp(t
, "Node-"))
365 if (!constcmp(t
+ strlen("Node-"), "path")) {
366 if (active_ctx
== NODE_CTX
)
368 if (active_ctx
== REV_CTX
)
370 active_ctx
= NODE_CTX
;
374 if (constcmp(t
+ strlen("Node-"), "kind"))
376 if (!strcmp(val
, "dir"))
377 node_ctx
.type
= REPO_MODE_DIR
;
378 else if (!strcmp(val
, "file"))
379 node_ctx
.type
= REPO_MODE_BLB
;
381 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
383 case sizeof("Node-action"):
384 if (constcmp(t
, "Node-action"))
386 if (!strcmp(val
, "delete")) {
387 node_ctx
.action
= NODEACT_DELETE
;
388 } else if (!strcmp(val
, "add")) {
389 node_ctx
.action
= NODEACT_ADD
;
390 } else if (!strcmp(val
, "change")) {
391 node_ctx
.action
= NODEACT_CHANGE
;
392 } else if (!strcmp(val
, "replace")) {
393 node_ctx
.action
= NODEACT_REPLACE
;
395 fprintf(stderr
, "Unknown node-action: %s\n", val
);
396 node_ctx
.action
= NODEACT_UNKNOWN
;
399 case sizeof("Node-copyfrom-path"):
400 if (constcmp(t
, "Node-copyfrom-path"))
402 strbuf_reset(&node_ctx
.src
);
403 strbuf_addstr(&node_ctx
.src
, val
);
405 case sizeof("Node-copyfrom-rev"):
406 if (constcmp(t
, "Node-copyfrom-rev"))
408 node_ctx
.srcRev
= atoi(val
);
410 case sizeof("Text-content-length"):
411 if (constcmp(t
, "Text") && constcmp(t
, "Prop"))
413 if (constcmp(t
+ 4, "-content-length"))
419 len
= strtoumax(val
, &end
, 10);
420 if (!isdigit(*val
) || *end
)
421 die("invalid dump: non-numeric length %s", val
);
422 if (len
> maximum_signed_value_of_type(off_t
))
423 die("unrepresentable length in dump: %s", val
);
426 node_ctx
.text_length
= (off_t
) len
;
428 node_ctx
.prop_length
= (off_t
) len
;
431 case sizeof("Text-delta"):
432 if (!constcmp(t
, "Text-delta")) {
433 node_ctx
.text_delta
= !strcmp(val
, "true");
436 if (constcmp(t
, "Prop-delta"))
438 node_ctx
.prop_delta
= !strcmp(val
, "true");
440 case sizeof("Content-length"):
441 if (constcmp(t
, "Content-length"))
444 t
= buffer_read_line(&input
);
448 die("invalid dump: expected blank line after content length header");
449 if (active_ctx
== REV_CTX
) {
451 } else if (active_ctx
== NODE_CTX
) {
453 active_ctx
= INTERNODE_CTX
;
455 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
456 if (buffer_skip_bytes(&input
, len
) != len
)
461 if (buffer_ferror(&input
))
463 if (active_ctx
== NODE_CTX
)
465 if (active_ctx
== REV_CTX
)
467 if (active_ctx
!= DUMP_CTX
)
471 int svndump_init(const char *filename
)
473 if (buffer_init(&input
, filename
))
474 return error("cannot open %s: %s", filename
, strerror(errno
));
475 fast_export_init(REPORT_FILENO
);
476 strbuf_init(&dump_ctx
.uuid
, 4096);
477 strbuf_init(&dump_ctx
.url
, 4096);
478 strbuf_init(&rev_ctx
.log
, 4096);
479 strbuf_init(&rev_ctx
.author
, 4096);
480 strbuf_init(&node_ctx
.src
, 4096);
481 strbuf_init(&node_ctx
.dst
, 4096);
482 reset_dump_ctx(NULL
);
484 reset_node_ctx(NULL
);
488 void svndump_deinit(void)
490 fast_export_deinit();
491 reset_dump_ctx(NULL
);
493 reset_node_ctx(NULL
);
494 strbuf_release(&rev_ctx
.log
);
495 strbuf_release(&node_ctx
.src
);
496 strbuf_release(&node_ctx
.dst
);
497 if (buffer_deinit(&input
))
498 fprintf(stderr
, "Input error\n");
500 fprintf(stderr
, "Output error\n");
503 void svndump_reset(void)
505 strbuf_release(&dump_ctx
.uuid
);
506 strbuf_release(&dump_ctx
.url
);
507 strbuf_release(&rev_ctx
.log
);
508 strbuf_release(&rev_ctx
.author
);