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"
17 * Compare start of string to literal of equal length;
18 * must be guarded by length test.
20 #define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
22 #define REPORT_FILENO 3
24 #define NODEACT_REPLACE 4
25 #define NODEACT_DELETE 3
27 #define NODEACT_CHANGE 1
28 #define NODEACT_UNKNOWN 0
31 #define DUMP_CTX 0 /* dump metadata */
32 #define REV_CTX 1 /* revision metadata */
33 #define NODE_CTX 2 /* node metadata */
34 #define INTERNODE_CTX 3 /* between nodes */
36 #define LENGTH_UNKNOWN (~0)
37 #define DATE_RFC2822_LEN 31
39 static struct line_buffer input
= LINE_BUFFER_INIT
;
42 uint32_t action
, propLength
, textLength
, srcRev
, type
;
43 struct strbuf src
, dst
;
44 uint32_t text_delta
, prop_delta
;
49 unsigned long timestamp
;
50 struct strbuf log
, author
;
55 struct strbuf uuid
, url
;
58 static void reset_node_ctx(char *fname
)
61 node_ctx
.action
= NODEACT_UNKNOWN
;
62 node_ctx
.propLength
= LENGTH_UNKNOWN
;
63 node_ctx
.textLength
= LENGTH_UNKNOWN
;
64 strbuf_reset(&node_ctx
.src
);
66 strbuf_reset(&node_ctx
.dst
);
68 strbuf_addstr(&node_ctx
.dst
, fname
);
69 node_ctx
.text_delta
= 0;
70 node_ctx
.prop_delta
= 0;
73 static void reset_rev_ctx(uint32_t revision
)
75 rev_ctx
.revision
= revision
;
76 rev_ctx
.timestamp
= 0;
77 strbuf_reset(&rev_ctx
.log
);
78 strbuf_reset(&rev_ctx
.author
);
81 static void reset_dump_ctx(const char *url
)
83 strbuf_reset(&dump_ctx
.url
);
85 strbuf_addstr(&dump_ctx
.url
, url
);
87 strbuf_reset(&dump_ctx
.uuid
);
90 static void handle_property(const struct strbuf
*key_buf
,
94 const char *key
= key_buf
->buf
;
95 size_t keylen
= key_buf
->len
;
98 case sizeof("svn:log"):
99 if (constcmp(key
, "svn:log"))
102 die("invalid dump: unsets svn:log");
103 strbuf_swap(&rev_ctx
.log
, val
);
105 case sizeof("svn:author"):
106 if (constcmp(key
, "svn:author"))
109 strbuf_reset(&rev_ctx
.author
);
111 strbuf_swap(&rev_ctx
.author
, val
);
113 case sizeof("svn:date"):
114 if (constcmp(key
, "svn:date"))
117 die("invalid dump: unsets svn:date");
118 if (parse_date_basic(val
->buf
, &rev_ctx
.timestamp
, NULL
))
119 warning("invalid timestamp: %s", val
->buf
);
121 case sizeof("svn:executable"):
122 case sizeof("svn:special"):
123 if (keylen
== strlen("svn:executable") &&
124 constcmp(key
, "svn:executable"))
126 if (keylen
== strlen("svn:special") &&
127 constcmp(key
, "svn:special"))
132 die("invalid dump: sets type twice");
135 node_ctx
.type
= REPO_MODE_BLB
;
139 node_ctx
.type
= keylen
== strlen("svn:executable") ?
145 static void die_short_read(void)
147 if (buffer_ferror(&input
))
148 die_errno("error reading dump file");
149 die("invalid dump: unexpected end of file");
152 static void read_props(void)
154 static struct strbuf key
= STRBUF_INIT
;
155 static struct strbuf val
= STRBUF_INIT
;
158 * NEEDSWORK: to support simple mode changes like
165 * we keep track of whether a mode has been set and reset to
166 * plain file only if not. We should be keeping track of the
167 * symlink and executable bits separately instead.
169 uint32_t type_set
= 0;
170 while ((t
= buffer_read_line(&input
)) && strcmp(t
, "PROPS-END")) {
172 const char type
= t
[0];
175 if (!type
|| t
[1] != ' ')
176 die("invalid property line: %s\n", t
);
179 buffer_read_binary(&input
, &val
, len
);
183 /* Discard trailing newline. */
184 ch
= buffer_read_char(&input
);
188 die("invalid dump: expected newline after %s", val
.buf
);
192 strbuf_swap(&key
, &val
);
195 handle_property(&val
, NULL
, &type_set
);
198 handle_property(&key
, &val
, &type_set
);
202 die("invalid property line: %s\n", t
);
207 static void handle_node(void)
209 const uint32_t type
= node_ctx
.type
;
210 const int have_props
= node_ctx
.propLength
!= LENGTH_UNKNOWN
;
211 const int have_text
= node_ctx
.textLength
!= LENGTH_UNKNOWN
;
213 * Old text for this node:
214 * NULL - directory or bug
216 * "<dataref>" - data retrievable from fast-import
218 static const char *const empty_blob
= "::empty::";
219 const char *old_data
= NULL
;
220 uint32_t old_mode
= REPO_MODE_BLB
;
222 if (node_ctx
.action
== NODEACT_DELETE
) {
223 if (have_text
|| have_props
|| node_ctx
.srcRev
)
224 die("invalid dump: deletion node has "
225 "copyfrom info, text, or properties");
226 repo_delete(node_ctx
.dst
.buf
);
229 if (node_ctx
.action
== NODEACT_REPLACE
) {
230 repo_delete(node_ctx
.dst
.buf
);
231 node_ctx
.action
= NODEACT_ADD
;
233 if (node_ctx
.srcRev
) {
234 repo_copy(node_ctx
.srcRev
, node_ctx
.src
.buf
, node_ctx
.dst
.buf
);
235 if (node_ctx
.action
== NODEACT_ADD
)
236 node_ctx
.action
= NODEACT_CHANGE
;
238 if (have_text
&& type
== REPO_MODE_DIR
)
239 die("invalid dump: directories cannot have text attached");
242 * Find old content (old_data) and decide on the new mode.
244 if (node_ctx
.action
== NODEACT_CHANGE
&& !*node_ctx
.dst
.buf
) {
245 if (type
!= REPO_MODE_DIR
)
246 die("invalid dump: root of tree is not a regular file");
248 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
250 old_data
= repo_read_path(node_ctx
.dst
.buf
, &mode
);
251 if (mode
== REPO_MODE_DIR
&& type
!= REPO_MODE_DIR
)
252 die("invalid dump: cannot modify a directory into a file");
253 if (mode
!= REPO_MODE_DIR
&& type
== REPO_MODE_DIR
)
254 die("invalid dump: cannot modify a file into a directory");
255 node_ctx
.type
= mode
;
257 } else if (node_ctx
.action
== NODEACT_ADD
) {
258 if (type
== REPO_MODE_DIR
)
261 old_data
= empty_blob
;
263 die("invalid dump: adds node without text");
265 die("invalid dump: Node-path block lacks Node-action");
269 * Adjust mode to reflect properties.
272 if (!node_ctx
.prop_delta
)
273 node_ctx
.type
= type
;
274 if (node_ctx
.propLength
)
281 if (type
== REPO_MODE_DIR
) /* directories are not tracked. */
284 if (old_data
== empty_blob
)
285 /* For the fast_export_* functions, NULL means empty. */
288 fast_export_modify(node_ctx
.dst
.buf
, node_ctx
.type
, old_data
);
291 if (!node_ctx
.text_delta
) {
292 fast_export_modify(node_ctx
.dst
.buf
, node_ctx
.type
, "inline");
293 fast_export_data(node_ctx
.type
, node_ctx
.textLength
, &input
);
296 fast_export_modify(node_ctx
.dst
.buf
, node_ctx
.type
, "inline");
297 fast_export_blob_delta(node_ctx
.type
, old_mode
, old_data
,
298 node_ctx
.textLength
, &input
);
301 static void begin_revision(void)
303 if (!rev_ctx
.revision
) /* revision 0 gets no git commit. */
305 fast_export_begin_commit(rev_ctx
.revision
, rev_ctx
.author
.buf
,
306 &rev_ctx
.log
, dump_ctx
.uuid
.buf
, dump_ctx
.url
.buf
,
310 static void end_revision(void)
312 if (rev_ctx
.revision
)
313 fast_export_end_commit(rev_ctx
.revision
);
316 void svndump_read(const char *url
)
320 uint32_t active_ctx
= DUMP_CTX
;
324 while ((t
= buffer_read_line(&input
))) {
325 val
= strchr(t
, ':');
333 /* strlen(key) + 1 */
334 switch (val
- t
- 1) {
335 case sizeof("SVN-fs-dump-format-version"):
336 if (constcmp(t
, "SVN-fs-dump-format-version"))
338 dump_ctx
.version
= atoi(val
);
339 if (dump_ctx
.version
> 3)
340 die("expected svn dump format version <= 3, found %"PRIu32
,
344 if (constcmp(t
, "UUID"))
346 strbuf_reset(&dump_ctx
.uuid
);
347 strbuf_addstr(&dump_ctx
.uuid
, val
);
349 case sizeof("Revision-number"):
350 if (constcmp(t
, "Revision-number"))
352 if (active_ctx
== NODE_CTX
)
354 if (active_ctx
== REV_CTX
)
356 if (active_ctx
!= DUMP_CTX
)
358 active_ctx
= REV_CTX
;
359 reset_rev_ctx(atoi(val
));
361 case sizeof("Node-path"):
362 if (prefixcmp(t
, "Node-"))
364 if (!constcmp(t
+ strlen("Node-"), "path")) {
365 if (active_ctx
== NODE_CTX
)
367 if (active_ctx
== REV_CTX
)
369 active_ctx
= NODE_CTX
;
373 if (constcmp(t
+ strlen("Node-"), "kind"))
375 if (!strcmp(val
, "dir"))
376 node_ctx
.type
= REPO_MODE_DIR
;
377 else if (!strcmp(val
, "file"))
378 node_ctx
.type
= REPO_MODE_BLB
;
380 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
382 case sizeof("Node-action"):
383 if (constcmp(t
, "Node-action"))
385 if (!strcmp(val
, "delete")) {
386 node_ctx
.action
= NODEACT_DELETE
;
387 } else if (!strcmp(val
, "add")) {
388 node_ctx
.action
= NODEACT_ADD
;
389 } else if (!strcmp(val
, "change")) {
390 node_ctx
.action
= NODEACT_CHANGE
;
391 } else if (!strcmp(val
, "replace")) {
392 node_ctx
.action
= NODEACT_REPLACE
;
394 fprintf(stderr
, "Unknown node-action: %s\n", val
);
395 node_ctx
.action
= NODEACT_UNKNOWN
;
398 case sizeof("Node-copyfrom-path"):
399 if (constcmp(t
, "Node-copyfrom-path"))
401 strbuf_reset(&node_ctx
.src
);
402 strbuf_addstr(&node_ctx
.src
, val
);
404 case sizeof("Node-copyfrom-rev"):
405 if (constcmp(t
, "Node-copyfrom-rev"))
407 node_ctx
.srcRev
= atoi(val
);
409 case sizeof("Text-content-length"):
410 if (!constcmp(t
, "Text-content-length")) {
411 node_ctx
.textLength
= atoi(val
);
414 if (constcmp(t
, "Prop-content-length"))
416 node_ctx
.propLength
= atoi(val
);
418 case sizeof("Text-delta"):
419 if (!constcmp(t
, "Text-delta")) {
420 node_ctx
.text_delta
= !strcmp(val
, "true");
423 if (constcmp(t
, "Prop-delta"))
425 node_ctx
.prop_delta
= !strcmp(val
, "true");
427 case sizeof("Content-length"):
428 if (constcmp(t
, "Content-length"))
431 t
= buffer_read_line(&input
);
435 die("invalid dump: expected blank line after content length header");
436 if (active_ctx
== REV_CTX
) {
438 } else if (active_ctx
== NODE_CTX
) {
440 active_ctx
= INTERNODE_CTX
;
442 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
443 if (buffer_skip_bytes(&input
, len
) != len
)
448 if (buffer_ferror(&input
))
450 if (active_ctx
== NODE_CTX
)
452 if (active_ctx
== REV_CTX
)
454 if (active_ctx
!= DUMP_CTX
)
458 int svndump_init(const char *filename
)
460 if (buffer_init(&input
, filename
))
461 return error("cannot open %s: %s", filename
, strerror(errno
));
462 fast_export_init(REPORT_FILENO
);
463 strbuf_init(&dump_ctx
.uuid
, 4096);
464 strbuf_init(&dump_ctx
.url
, 4096);
465 strbuf_init(&rev_ctx
.log
, 4096);
466 strbuf_init(&rev_ctx
.author
, 4096);
467 strbuf_init(&node_ctx
.src
, 4096);
468 strbuf_init(&node_ctx
.dst
, 4096);
469 reset_dump_ctx(NULL
);
471 reset_node_ctx(NULL
);
475 void svndump_deinit(void)
477 fast_export_deinit();
478 reset_dump_ctx(NULL
);
480 reset_node_ctx(NULL
);
481 strbuf_release(&rev_ctx
.log
);
482 strbuf_release(&node_ctx
.src
);
483 strbuf_release(&node_ctx
.dst
);
484 if (buffer_deinit(&input
))
485 fprintf(stderr
, "Input error\n");
487 fprintf(stderr
, "Output error\n");
490 void svndump_reset(void)
493 buffer_reset(&input
);
494 strbuf_release(&dump_ctx
.uuid
);
495 strbuf_release(&dump_ctx
.url
);
496 strbuf_release(&rev_ctx
.log
);
497 strbuf_release(&rev_ctx
.author
);