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 DATE_RFC2822_LEN 31
43 static struct line_buffer input
= LINE_BUFFER_INIT
;
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
;
54 unsigned long timestamp
;
55 struct strbuf log
, author
;
60 struct strbuf uuid
, url
;
63 static void reset_node_ctx(char *fname
)
66 node_ctx
.action
= NODEACT_UNKNOWN
;
67 node_ctx
.prop_length
= -1;
68 node_ctx
.text_length
= -1;
69 strbuf_reset(&node_ctx
.src
);
71 strbuf_reset(&node_ctx
.dst
);
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
);
90 strbuf_addstr(&dump_ctx
.url
, url
);
92 strbuf_reset(&dump_ctx
.uuid
);
95 static void handle_property(const struct strbuf
*key_buf
,
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"))
108 die("invalid dump: unsets svn:log");
109 strbuf_swap(&rev_ctx
.log
, val
);
111 case sizeof("svn:author"):
112 if (constcmp(key
, "svn:author"))
115 strbuf_reset(&rev_ctx
.author
);
117 strbuf_swap(&rev_ctx
.author
, val
);
119 case sizeof("svn:date"):
120 if (constcmp(key
, "svn:date"))
123 die("invalid dump: unsets svn:date");
124 if (!strptime(val
->buf
, "%FT%T", &tm
))
125 fprintf(stderr
, "warning: " "invalid timestamp: %s", val
->buf
);
127 rev_ctx
.timestamp
= mkgmtime(&tm
);
129 case sizeof("svn:executable"):
130 case sizeof("svn:special"):
131 if (keylen
== strlen("svn:executable") &&
132 constcmp(key
, "svn:executable"))
134 if (keylen
== strlen("svn:special") &&
135 constcmp(key
, "svn:special"))
140 die("invalid dump: sets type twice");
143 node_ctx
.type
= REPO_MODE_BLB
;
147 node_ctx
.type
= keylen
== strlen("svn:executable") ?
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
;
166 * NEEDSWORK: to support simple mode changes like
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")) {
180 const char type
= t
[0];
183 if (!type
|| t
[1] != ' ')
184 die("invalid property line: %s\n", t
);
187 buffer_read_binary(&input
, &val
, len
);
191 /* Discard trailing newline. */
192 ch
= buffer_read_char(&input
);
196 die("invalid dump: expected newline after %s", val
.buf
);
200 strbuf_swap(&key
, &val
);
203 handle_property(&val
, NULL
, &type_set
);
206 handle_property(&key
, &val
, &type_set
);
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
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
);
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");
256 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
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
;
265 } else if (node_ctx
.action
== NODEACT_ADD
) {
266 if (type
== REPO_MODE_DIR
)
269 old_data
= empty_blob
;
271 die("invalid dump: adds node without text");
273 die("invalid dump: Node-path block lacks Node-action");
277 * Adjust mode to reflect properties.
280 if (!node_ctx
.prop_delta
)
281 node_ctx
.type
= type
;
282 if (node_ctx
.prop_length
)
289 if (type
== REPO_MODE_DIR
) /* directories are not tracked. */
292 if (old_data
== empty_blob
)
293 /* For the fast_export_* functions, NULL means empty. */
296 fast_export_modify(node_ctx
.dst
.buf
, node_ctx
.type
, old_data
);
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
);
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. */
313 fast_export_begin_commit(rev_ctx
.revision
, rev_ctx
.author
.buf
,
314 &rev_ctx
.log
, dump_ctx
.uuid
.buf
, dump_ctx
.url
.buf
,
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
)
328 uint32_t active_ctx
= DUMP_CTX
;
332 while ((t
= buffer_read_line(&input
))) {
333 val
= strchr(t
, ':');
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"))
346 dump_ctx
.version
= atoi(val
);
347 if (dump_ctx
.version
> 3)
348 die("expected svn dump format version <= 3, found %"PRIu32
,
352 if (constcmp(t
, "UUID"))
354 strbuf_reset(&dump_ctx
.uuid
);
355 strbuf_addstr(&dump_ctx
.uuid
, val
);
357 case sizeof("Revision-number"):
358 if (constcmp(t
, "Revision-number"))
360 if (active_ctx
== NODE_CTX
)
362 if (active_ctx
== REV_CTX
)
364 if (active_ctx
!= DUMP_CTX
)
366 active_ctx
= REV_CTX
;
367 reset_rev_ctx(atoi(val
));
369 case sizeof("Node-path"):
370 if (constcmp(t
, "Node-"))
372 if (!constcmp(t
+ strlen("Node-"), "path")) {
373 if (active_ctx
== NODE_CTX
)
375 if (active_ctx
== REV_CTX
)
377 active_ctx
= NODE_CTX
;
381 if (constcmp(t
+ strlen("Node-"), "kind"))
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
;
388 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
390 case sizeof("Node-action"):
391 if (constcmp(t
, "Node-action"))
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
;
402 fprintf(stderr
, "Unknown node-action: %s\n", val
);
403 node_ctx
.action
= NODEACT_UNKNOWN
;
406 case sizeof("Node-copyfrom-path"):
407 if (constcmp(t
, "Node-copyfrom-path"))
409 strbuf_reset(&node_ctx
.src
);
410 strbuf_addstr(&node_ctx
.src
, val
);
412 case sizeof("Node-copyfrom-rev"):
413 if (constcmp(t
, "Node-copyfrom-rev"))
415 node_ctx
.srcRev
= atoi(val
);
417 case sizeof("Text-content-length"):
418 if (constcmp(t
, "Text") && constcmp(t
, "Prop"))
420 if (constcmp(t
+ 4, "-content-length"))
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
);
433 node_ctx
.text_length
= (off_t
) len
;
435 node_ctx
.prop_length
= (off_t
) len
;
438 case sizeof("Text-delta"):
439 if (!constcmp(t
, "Text-delta")) {
440 node_ctx
.text_delta
= !strcmp(val
, "true");
443 if (constcmp(t
, "Prop-delta"))
445 node_ctx
.prop_delta
= !strcmp(val
, "true");
447 case sizeof("Content-length"):
448 if (constcmp(t
, "Content-length"))
451 t
= buffer_read_line(&input
);
455 die("invalid dump: expected blank line after content length header");
456 if (active_ctx
== REV_CTX
) {
458 } else if (active_ctx
== NODE_CTX
) {
460 active_ctx
= INTERNODE_CTX
;
462 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
463 if (buffer_skip_bytes(&input
, len
) != len
)
468 if (buffer_ferror(&input
))
470 if (active_ctx
== NODE_CTX
)
472 if (active_ctx
== REV_CTX
)
474 if (active_ctx
!= DUMP_CTX
)
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
);
491 reset_node_ctx(NULL
);
495 void svndump_deinit(void)
497 fast_export_deinit();
498 reset_dump_ctx(NULL
);
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");
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
);