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.
13 #include "compat-util.h"
14 #include "repo_tree.h"
15 #include "fast_export.h"
16 #include "line_buffer.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
32 #define NODEACT_CHANGE 1
33 #define NODEACT_UNKNOWN 0
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
;
47 uint32_t action
, propLength
, srcRev
, type
;
49 struct strbuf src
, dst
;
50 uint32_t text_delta
, prop_delta
;
55 unsigned long timestamp
;
56 struct strbuf log
, author
;
61 struct strbuf uuid
, url
;
64 static void reset_node_ctx(char *fname
)
67 node_ctx
.action
= NODEACT_UNKNOWN
;
68 node_ctx
.propLength
= LENGTH_UNKNOWN
;
69 node_ctx
.text_length
= -1;
70 strbuf_reset(&node_ctx
.src
);
72 strbuf_reset(&node_ctx
.dst
);
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
);
91 strbuf_addstr(&dump_ctx
.url
, url
);
93 strbuf_reset(&dump_ctx
.uuid
);
96 static void handle_property(const struct strbuf
*key_buf
,
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"))
109 die("invalid dump: unsets svn:log");
110 strbuf_swap(&rev_ctx
.log
, val
);
112 case sizeof("svn:author"):
113 if (constcmp(key
, "svn:author"))
116 strbuf_reset(&rev_ctx
.author
);
118 strbuf_swap(&rev_ctx
.author
, val
);
120 case sizeof("svn:date"):
121 if (constcmp(key
, "svn:date"))
124 die("invalid dump: unsets svn:date");
125 if (!strptime(val
->buf
, "%FT%T", &tm
))
126 fprintf(stderr
, "warning: " "invalid timestamp: %s", val
->buf
);
128 rev_ctx
.timestamp
= mkgmtime(&tm
);
130 case sizeof("svn:executable"):
131 case sizeof("svn:special"):
132 if (keylen
== strlen("svn:executable") &&
133 constcmp(key
, "svn:executable"))
135 if (keylen
== strlen("svn:special") &&
136 constcmp(key
, "svn:special"))
141 die("invalid dump: sets type twice");
144 node_ctx
.type
= REPO_MODE_BLB
;
148 node_ctx
.type
= keylen
== strlen("svn:executable") ?
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
;
167 * NEEDSWORK: to support simple mode changes like
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")) {
181 const char type
= t
[0];
184 if (!type
|| t
[1] != ' ')
185 die("invalid property line: %s\n", t
);
188 buffer_read_binary(&input
, &val
, len
);
192 /* Discard trailing newline. */
193 ch
= buffer_read_char(&input
);
197 die("invalid dump: expected newline after %s", val
.buf
);
201 strbuf_swap(&key
, &val
);
204 handle_property(&val
, NULL
, &type_set
);
207 handle_property(&key
, &val
, &type_set
);
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
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
);
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");
257 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
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
;
266 } else if (node_ctx
.action
== NODEACT_ADD
) {
267 if (type
== REPO_MODE_DIR
)
270 old_data
= empty_blob
;
272 die("invalid dump: adds node without text");
274 die("invalid dump: Node-path block lacks Node-action");
278 * Adjust mode to reflect properties.
281 if (!node_ctx
.prop_delta
)
282 node_ctx
.type
= type
;
283 if (node_ctx
.propLength
)
290 if (type
== REPO_MODE_DIR
) /* directories are not tracked. */
293 if (old_data
== empty_blob
)
294 /* For the fast_export_* functions, NULL means empty. */
297 fast_export_modify(node_ctx
.dst
.buf
, node_ctx
.type
, old_data
);
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
);
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. */
314 fast_export_begin_commit(rev_ctx
.revision
, rev_ctx
.author
.buf
,
315 &rev_ctx
.log
, dump_ctx
.uuid
.buf
, dump_ctx
.url
.buf
,
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
)
329 uint32_t active_ctx
= DUMP_CTX
;
333 while ((t
= buffer_read_line(&input
))) {
334 val
= strchr(t
, ':');
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"))
347 dump_ctx
.version
= atoi(val
);
348 if (dump_ctx
.version
> 3)
349 die("expected svn dump format version <= 3, found %"PRIu32
,
353 if (constcmp(t
, "UUID"))
355 strbuf_reset(&dump_ctx
.uuid
);
356 strbuf_addstr(&dump_ctx
.uuid
, val
);
358 case sizeof("Revision-number"):
359 if (constcmp(t
, "Revision-number"))
361 if (active_ctx
== NODE_CTX
)
363 if (active_ctx
== REV_CTX
)
365 if (active_ctx
!= DUMP_CTX
)
367 active_ctx
= REV_CTX
;
368 reset_rev_ctx(atoi(val
));
370 case sizeof("Node-path"):
371 if (prefixcmp(t
, "Node-"))
373 if (!constcmp(t
+ strlen("Node-"), "path")) {
374 if (active_ctx
== NODE_CTX
)
376 if (active_ctx
== REV_CTX
)
378 active_ctx
= NODE_CTX
;
382 if (constcmp(t
+ strlen("Node-"), "kind"))
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
;
389 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
391 case sizeof("Node-action"):
392 if (constcmp(t
, "Node-action"))
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
;
403 fprintf(stderr
, "Unknown node-action: %s\n", val
);
404 node_ctx
.action
= NODEACT_UNKNOWN
;
407 case sizeof("Node-copyfrom-path"):
408 if (constcmp(t
, "Node-copyfrom-path"))
410 strbuf_reset(&node_ctx
.src
);
411 strbuf_addstr(&node_ctx
.src
, val
);
413 case sizeof("Node-copyfrom-rev"):
414 if (constcmp(t
, "Node-copyfrom-rev"))
416 node_ctx
.srcRev
= atoi(val
);
418 case sizeof("Text-content-length"):
419 if (!constcmp(t
, "Text-content-length")) {
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
;
431 if (constcmp(t
, "Prop-content-length"))
433 node_ctx
.propLength
= atoi(val
);
435 case sizeof("Text-delta"):
436 if (!constcmp(t
, "Text-delta")) {
437 node_ctx
.text_delta
= !strcmp(val
, "true");
440 if (constcmp(t
, "Prop-delta"))
442 node_ctx
.prop_delta
= !strcmp(val
, "true");
444 case sizeof("Content-length"):
445 if (constcmp(t
, "Content-length"))
448 t
= buffer_read_line(&input
);
452 die("invalid dump: expected blank line after content length header");
453 if (active_ctx
== REV_CTX
) {
455 } else if (active_ctx
== NODE_CTX
) {
457 active_ctx
= INTERNODE_CTX
;
459 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
460 if (buffer_skip_bytes(&input
, len
) != len
)
465 if (buffer_ferror(&input
))
467 if (active_ctx
== NODE_CTX
)
469 if (active_ctx
== REV_CTX
)
471 if (active_ctx
!= DUMP_CTX
)
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
);
488 reset_node_ctx(NULL
);
492 void svndump_deinit(void)
494 fast_export_deinit();
495 reset_dump_ctx(NULL
);
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");
504 fprintf(stderr
, "Output error\n");
507 void svndump_reset(void)
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
);