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"
14 #include "string_pool.h"
17 #define REPORT_FILENO 3
20 * Compare start of string to literal of equal length;
21 * must be guarded by length test.
23 #define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
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 LENGTH_UNKNOWN (~0)
38 #define DATE_RFC2822_LEN 31
40 static struct line_buffer input
= LINE_BUFFER_INIT
;
43 uint32_t action
, propLength
, textLength
, srcRev
, type
;
44 uint32_t src
[REPO_MAX_PATH_DEPTH
], dst
[REPO_MAX_PATH_DEPTH
];
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
.propLength
= LENGTH_UNKNOWN
;
64 node_ctx
.textLength
= LENGTH_UNKNOWN
;
67 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
, "/", fname
);
68 node_ctx
.text_delta
= 0;
69 node_ctx
.prop_delta
= 0;
72 static void reset_rev_ctx(uint32_t revision
)
74 rev_ctx
.revision
= revision
;
75 rev_ctx
.timestamp
= 0;
76 strbuf_reset(&rev_ctx
.log
);
77 strbuf_reset(&rev_ctx
.author
);
80 static void reset_dump_ctx(const char *url
)
82 strbuf_reset(&dump_ctx
.url
);
84 strbuf_addstr(&dump_ctx
.url
, url
);
86 strbuf_reset(&dump_ctx
.uuid
);
89 static void handle_property(const struct strbuf
*key_buf
,
90 const char *val
, uint32_t len
,
93 const char *key
= key_buf
->buf
;
94 size_t keylen
= key_buf
->len
;
97 case sizeof("svn:log"):
98 if (constcmp(key
, "svn:log"))
101 die("invalid dump: unsets svn:log");
102 strbuf_reset(&rev_ctx
.log
);
103 strbuf_add(&rev_ctx
.log
, val
, len
);
105 case sizeof("svn:author"):
106 if (constcmp(key
, "svn:author"))
108 strbuf_reset(&rev_ctx
.author
);
110 strbuf_add(&rev_ctx
.author
, val
, len
);
112 case sizeof("svn:date"):
113 if (constcmp(key
, "svn:date"))
116 die("invalid dump: unsets svn:date");
117 if (parse_date_basic(val
, &rev_ctx
.timestamp
, NULL
))
118 warning("invalid timestamp: %s", val
);
120 case sizeof("svn:executable"):
121 case sizeof("svn:special"):
122 if (keylen
== strlen("svn:executable") &&
123 constcmp(key
, "svn:executable"))
125 if (keylen
== strlen("svn:special") &&
126 constcmp(key
, "svn:special"))
131 die("invalid dump: sets type twice");
134 node_ctx
.type
= REPO_MODE_BLB
;
138 node_ctx
.type
= keylen
== strlen("svn:executable") ?
144 static void die_short_read(void)
146 if (buffer_ferror(&input
))
147 die_errno("error reading dump file");
148 die("invalid dump: unexpected end of file");
151 static void read_props(void)
153 static struct strbuf key
= STRBUF_INIT
;
156 * NEEDSWORK: to support simple mode changes like
163 * we keep track of whether a mode has been set and reset to
164 * plain file only if not. We should be keeping track of the
165 * symlink and executable bits separately instead.
167 uint32_t type_set
= 0;
168 while ((t
= buffer_read_line(&input
)) && strcmp(t
, "PROPS-END")) {
171 const char type
= t
[0];
174 if (!type
|| t
[1] != ' ')
175 die("invalid property line: %s\n", t
);
177 val
= buffer_read_string(&input
, len
);
178 if (!val
|| strlen(val
) != len
)
181 /* Discard trailing newline. */
182 ch
= buffer_read_char(&input
);
186 die("invalid dump: expected newline after %s", val
);
193 strbuf_add(&key
, val
, len
);
201 handle_property(&key
, val
, len
, &type_set
);
205 die("invalid property line: %s\n", t
);
210 static void handle_node(void)
212 const uint32_t type
= node_ctx
.type
;
213 const int have_props
= node_ctx
.propLength
!= LENGTH_UNKNOWN
;
214 const int have_text
= node_ctx
.textLength
!= LENGTH_UNKNOWN
;
216 * Old text for this node:
217 * NULL - directory or bug
219 * "<dataref>" - data retrievable from fast-import
221 static const char *const empty_blob
= "::empty::";
222 const char *old_data
= NULL
;
224 if (node_ctx
.text_delta
)
225 die("text deltas not supported");
227 if (node_ctx
.action
== NODEACT_DELETE
) {
228 if (have_text
|| have_props
|| node_ctx
.srcRev
)
229 die("invalid dump: deletion node has "
230 "copyfrom info, text, or properties");
231 return repo_delete(node_ctx
.dst
);
233 if (node_ctx
.action
== NODEACT_REPLACE
) {
234 repo_delete(node_ctx
.dst
);
235 node_ctx
.action
= NODEACT_ADD
;
237 if (node_ctx
.srcRev
) {
238 repo_copy(node_ctx
.srcRev
, node_ctx
.src
, node_ctx
.dst
);
239 if (node_ctx
.action
== NODEACT_ADD
)
240 node_ctx
.action
= NODEACT_CHANGE
;
242 if (have_text
&& type
== REPO_MODE_DIR
)
243 die("invalid dump: directories cannot have text attached");
246 * Find old content (old_data) and decide on the new mode.
248 if (node_ctx
.action
== NODEACT_CHANGE
&& !~*node_ctx
.dst
) {
249 if (type
!= REPO_MODE_DIR
)
250 die("invalid dump: root of tree is not a regular file");
252 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
254 old_data
= repo_read_path(node_ctx
.dst
);
255 mode
= repo_read_mode(node_ctx
.dst
);
256 if (mode
== REPO_MODE_DIR
&& type
!= REPO_MODE_DIR
)
257 die("invalid dump: cannot modify a directory into a file");
258 if (mode
!= REPO_MODE_DIR
&& type
== REPO_MODE_DIR
)
259 die("invalid dump: cannot modify a file into a directory");
260 node_ctx
.type
= mode
;
261 } else if (node_ctx
.action
== NODEACT_ADD
) {
262 if (type
== REPO_MODE_DIR
)
265 old_data
= empty_blob
;
267 die("invalid dump: adds node without text");
269 die("invalid dump: Node-path block lacks Node-action");
273 * Adjust mode to reflect properties.
276 if (!node_ctx
.prop_delta
)
277 node_ctx
.type
= type
;
278 if (node_ctx
.propLength
)
285 if (type
== REPO_MODE_DIR
) /* directories are not tracked. */
288 if (old_data
== empty_blob
)
289 /* For the fast_export_* functions, NULL means empty. */
292 fast_export_modify(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
,
293 node_ctx
.type
, old_data
);
296 fast_export_modify(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
,
297 node_ctx
.type
, "inline");
298 fast_export_data(node_ctx
.type
, 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
.buf
, 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
= strstr(t
, ": ");
330 /* strlen(key) + 1 */
331 switch (val
- t
- 1) {
332 case sizeof("SVN-fs-dump-format-version"):
333 if (constcmp(t
, "SVN-fs-dump-format-version"))
335 dump_ctx
.version
= atoi(val
);
336 if (dump_ctx
.version
> 3)
337 die("expected svn dump format version <= 3, found %"PRIu32
,
341 if (constcmp(t
, "UUID"))
343 strbuf_reset(&dump_ctx
.uuid
);
344 strbuf_addstr(&dump_ctx
.uuid
, val
);
346 case sizeof("Revision-number"):
347 if (constcmp(t
, "Revision-number"))
349 if (active_ctx
== NODE_CTX
)
351 if (active_ctx
== REV_CTX
)
353 if (active_ctx
!= DUMP_CTX
)
355 active_ctx
= REV_CTX
;
356 reset_rev_ctx(atoi(val
));
358 case sizeof("Node-path"):
359 if (prefixcmp(t
, "Node-"))
361 if (!constcmp(t
+ strlen("Node-"), "path")) {
362 if (active_ctx
== NODE_CTX
)
364 if (active_ctx
== REV_CTX
)
366 active_ctx
= NODE_CTX
;
370 if (constcmp(t
+ strlen("Node-"), "kind"))
372 if (!strcmp(val
, "dir"))
373 node_ctx
.type
= REPO_MODE_DIR
;
374 else if (!strcmp(val
, "file"))
375 node_ctx
.type
= REPO_MODE_BLB
;
377 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
379 case sizeof("Node-action"):
380 if (constcmp(t
, "Node-action"))
382 if (!strcmp(val
, "delete")) {
383 node_ctx
.action
= NODEACT_DELETE
;
384 } else if (!strcmp(val
, "add")) {
385 node_ctx
.action
= NODEACT_ADD
;
386 } else if (!strcmp(val
, "change")) {
387 node_ctx
.action
= NODEACT_CHANGE
;
388 } else if (!strcmp(val
, "replace")) {
389 node_ctx
.action
= NODEACT_REPLACE
;
391 fprintf(stderr
, "Unknown node-action: %s\n", val
);
392 node_ctx
.action
= NODEACT_UNKNOWN
;
395 case sizeof("Node-copyfrom-path"):
396 if (constcmp(t
, "Node-copyfrom-path"))
398 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.src
, "/", val
);
400 case sizeof("Node-copyfrom-rev"):
401 if (constcmp(t
, "Node-copyfrom-rev"))
403 node_ctx
.srcRev
= atoi(val
);
405 case sizeof("Text-content-length"):
406 if (!constcmp(t
, "Text-content-length")) {
407 node_ctx
.textLength
= atoi(val
);
410 if (constcmp(t
, "Prop-content-length"))
412 node_ctx
.propLength
= atoi(val
);
414 case sizeof("Text-delta"):
415 if (!constcmp(t
, "Text-delta")) {
416 node_ctx
.text_delta
= !strcmp(val
, "true");
419 if (constcmp(t
, "Prop-delta"))
421 node_ctx
.prop_delta
= !strcmp(val
, "true");
423 case sizeof("Content-length"):
424 if (constcmp(t
, "Content-length"))
427 t
= buffer_read_line(&input
);
431 die("invalid dump: expected blank line after content length header");
432 if (active_ctx
== REV_CTX
) {
434 } else if (active_ctx
== NODE_CTX
) {
436 active_ctx
= INTERNODE_CTX
;
438 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
439 if (buffer_skip_bytes(&input
, len
) != len
)
444 if (buffer_ferror(&input
))
446 if (active_ctx
== NODE_CTX
)
448 if (active_ctx
== REV_CTX
)
450 if (active_ctx
!= DUMP_CTX
)
454 int svndump_init(const char *filename
)
456 if (buffer_init(&input
, filename
))
457 return error("cannot open %s: %s", filename
, strerror(errno
));
458 fast_export_init(REPORT_FILENO
);
459 strbuf_init(&dump_ctx
.uuid
, 4096);
460 strbuf_init(&dump_ctx
.url
, 4096);
461 strbuf_init(&rev_ctx
.log
, 4096);
462 strbuf_init(&rev_ctx
.author
, 4096);
463 reset_dump_ctx(NULL
);
465 reset_node_ctx(NULL
);
469 void svndump_deinit(void)
471 fast_export_deinit();
472 reset_dump_ctx(NULL
);
474 reset_node_ctx(NULL
);
475 strbuf_release(&rev_ctx
.log
);
476 if (buffer_deinit(&input
))
477 fprintf(stderr
, "Input error\n");
479 fprintf(stderr
, "Output error\n");
482 void svndump_reset(void)
485 buffer_reset(&input
);
486 strbuf_release(&dump_ctx
.uuid
);
487 strbuf_release(&dump_ctx
.url
);
488 strbuf_release(&rev_ctx
.log
);
489 strbuf_release(&rev_ctx
.author
);