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"
15 #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 /* Create memory pool for log messages */
41 obj_pool_gen(log
, char, 4096)
43 static struct line_buffer input
= LINE_BUFFER_INIT
;
45 #define REPORT_FILENO 3
47 static char *log_copy(uint32_t length
, const char *log
)
50 log_free(log_pool
.size
);
51 buffer
= log_pointer(log_alloc(length
));
52 strncpy(buffer
, log
, length
);
57 uint32_t action
, propLength
, textLength
, srcRev
, type
;
58 uint32_t src
[REPO_MAX_PATH_DEPTH
], dst
[REPO_MAX_PATH_DEPTH
];
59 uint32_t text_delta
, prop_delta
;
63 uint32_t revision
, author
;
64 unsigned long timestamp
;
69 uint32_t version
, uuid
, url
;
72 static void reset_node_ctx(char *fname
)
75 node_ctx
.action
= NODEACT_UNKNOWN
;
76 node_ctx
.propLength
= LENGTH_UNKNOWN
;
77 node_ctx
.textLength
= LENGTH_UNKNOWN
;
80 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
, "/", fname
);
81 node_ctx
.text_delta
= 0;
82 node_ctx
.prop_delta
= 0;
85 static void reset_rev_ctx(uint32_t revision
)
87 rev_ctx
.revision
= revision
;
88 rev_ctx
.timestamp
= 0;
93 static void reset_dump_ctx(uint32_t url
)
100 static void handle_property(const struct strbuf
*key_buf
,
101 const char *val
, uint32_t len
,
104 const char *key
= key_buf
->buf
;
105 size_t keylen
= key_buf
->len
;
107 switch (keylen
+ 1) {
108 case sizeof("svn:log"):
109 if (constcmp(key
, "svn:log"))
112 die("invalid dump: unsets svn:log");
113 /* Value length excludes terminating nul. */
114 rev_ctx
.log
= log_copy(len
+ 1, val
);
116 case sizeof("svn:author"):
117 if (constcmp(key
, "svn:author"))
119 rev_ctx
.author
= pool_intern(val
);
121 case sizeof("svn:date"):
122 if (constcmp(key
, "svn:date"))
125 die("invalid dump: unsets svn:date");
126 if (parse_date_basic(val
, &rev_ctx
.timestamp
, NULL
))
127 warning("invalid timestamp: %s", val
);
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
;
165 * NEEDSWORK: to support simple mode changes like
172 * we keep track of whether a mode has been set and reset to
173 * plain file only if not. We should be keeping track of the
174 * symlink and executable bits separately instead.
176 uint32_t type_set
= 0;
177 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
);
186 val
= buffer_read_string(&input
, len
);
187 if (!val
|| strlen(val
) != len
)
190 /* Discard trailing newline. */
191 ch
= buffer_read_char(&input
);
195 die("invalid dump: expected newline after %s", val
);
202 strbuf_add(&key
, val
, len
);
210 handle_property(&key
, val
, len
, &type_set
);
214 die("invalid property line: %s\n", t
);
219 static void handle_node(void)
221 const uint32_t type
= node_ctx
.type
;
222 const int have_props
= node_ctx
.propLength
!= LENGTH_UNKNOWN
;
223 const int have_text
= node_ctx
.textLength
!= LENGTH_UNKNOWN
;
225 * Old text for this node:
226 * NULL - directory or bug
228 * "<dataref>" - data retrievable from fast-import
230 static const char *const empty_blob
= "::empty::";
231 const char *old_data
= NULL
;
233 if (node_ctx
.text_delta
)
234 die("text deltas not supported");
236 if (node_ctx
.action
== NODEACT_DELETE
) {
237 if (have_text
|| have_props
|| node_ctx
.srcRev
)
238 die("invalid dump: deletion node has "
239 "copyfrom info, text, or properties");
240 return repo_delete(node_ctx
.dst
);
242 if (node_ctx
.action
== NODEACT_REPLACE
) {
243 repo_delete(node_ctx
.dst
);
244 node_ctx
.action
= NODEACT_ADD
;
246 if (node_ctx
.srcRev
) {
247 repo_copy(node_ctx
.srcRev
, node_ctx
.src
, node_ctx
.dst
);
248 if (node_ctx
.action
== NODEACT_ADD
)
249 node_ctx
.action
= NODEACT_CHANGE
;
251 if (have_text
&& type
== REPO_MODE_DIR
)
252 die("invalid dump: directories cannot have text attached");
255 * Find old content (old_data) and decide on the new mode.
257 if (node_ctx
.action
== NODEACT_CHANGE
&& !~*node_ctx
.dst
) {
258 if (type
!= REPO_MODE_DIR
)
259 die("invalid dump: root of tree is not a regular file");
261 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
263 old_data
= repo_read_path(node_ctx
.dst
);
264 mode
= repo_read_mode(node_ctx
.dst
);
265 if (mode
== REPO_MODE_DIR
&& type
!= REPO_MODE_DIR
)
266 die("invalid dump: cannot modify a directory into a file");
267 if (mode
!= REPO_MODE_DIR
&& type
== REPO_MODE_DIR
)
268 die("invalid dump: cannot modify a file into a directory");
269 node_ctx
.type
= mode
;
270 } else if (node_ctx
.action
== NODEACT_ADD
) {
271 if (type
== REPO_MODE_DIR
)
274 old_data
= empty_blob
;
276 die("invalid dump: adds node without text");
278 die("invalid dump: Node-path block lacks Node-action");
282 * Adjust mode to reflect properties.
285 if (!node_ctx
.prop_delta
)
286 node_ctx
.type
= type
;
287 if (node_ctx
.propLength
)
294 if (type
== REPO_MODE_DIR
) /* directories are not tracked. */
297 if (old_data
== empty_blob
)
298 /* For the fast_export_* functions, NULL means empty. */
301 fast_export_modify(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
,
302 node_ctx
.type
, old_data
);
305 fast_export_modify(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
,
306 node_ctx
.type
, "inline");
307 fast_export_data(node_ctx
.type
, node_ctx
.textLength
, &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
, rev_ctx
.log
,
315 dump_ctx
.uuid
, dump_ctx
.url
, rev_ctx
.timestamp
);
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
;
331 reset_dump_ctx(pool_intern(url
));
332 while ((t
= buffer_read_line(&input
))) {
333 val
= strstr(t
, ": ");
338 /* strlen(key) + 1 */
339 switch (val
- t
- 1) {
340 case sizeof("SVN-fs-dump-format-version"):
341 if (constcmp(t
, "SVN-fs-dump-format-version"))
343 dump_ctx
.version
= atoi(val
);
344 if (dump_ctx
.version
> 3)
345 die("expected svn dump format version <= 3, found %"PRIu32
,
349 if (constcmp(t
, "UUID"))
351 dump_ctx
.uuid
= pool_intern(val
);
353 case sizeof("Revision-number"):
354 if (constcmp(t
, "Revision-number"))
356 if (active_ctx
== NODE_CTX
)
358 if (active_ctx
== REV_CTX
)
360 if (active_ctx
!= DUMP_CTX
)
362 active_ctx
= REV_CTX
;
363 reset_rev_ctx(atoi(val
));
365 case sizeof("Node-path"):
366 if (prefixcmp(t
, "Node-"))
368 if (!constcmp(t
+ strlen("Node-"), "path")) {
369 if (active_ctx
== NODE_CTX
)
371 if (active_ctx
== REV_CTX
)
373 active_ctx
= NODE_CTX
;
377 if (constcmp(t
+ strlen("Node-"), "kind"))
379 if (!strcmp(val
, "dir"))
380 node_ctx
.type
= REPO_MODE_DIR
;
381 else if (!strcmp(val
, "file"))
382 node_ctx
.type
= REPO_MODE_BLB
;
384 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
386 case sizeof("Node-action"):
387 if (constcmp(t
, "Node-action"))
389 if (!strcmp(val
, "delete")) {
390 node_ctx
.action
= NODEACT_DELETE
;
391 } else if (!strcmp(val
, "add")) {
392 node_ctx
.action
= NODEACT_ADD
;
393 } else if (!strcmp(val
, "change")) {
394 node_ctx
.action
= NODEACT_CHANGE
;
395 } else if (!strcmp(val
, "replace")) {
396 node_ctx
.action
= NODEACT_REPLACE
;
398 fprintf(stderr
, "Unknown node-action: %s\n", val
);
399 node_ctx
.action
= NODEACT_UNKNOWN
;
402 case sizeof("Node-copyfrom-path"):
403 if (constcmp(t
, "Node-copyfrom-path"))
405 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.src
, "/", val
);
407 case sizeof("Node-copyfrom-rev"):
408 if (constcmp(t
, "Node-copyfrom-rev"))
410 node_ctx
.srcRev
= atoi(val
);
412 case sizeof("Text-content-length"):
413 if (!constcmp(t
, "Text-content-length")) {
414 node_ctx
.textLength
= atoi(val
);
417 if (constcmp(t
, "Prop-content-length"))
419 node_ctx
.propLength
= atoi(val
);
421 case sizeof("Text-delta"):
422 if (!constcmp(t
, "Text-delta")) {
423 node_ctx
.text_delta
= !strcmp(val
, "true");
426 if (constcmp(t
, "Prop-delta"))
428 node_ctx
.prop_delta
= !strcmp(val
, "true");
430 case sizeof("Content-length"):
431 if (constcmp(t
, "Content-length"))
434 t
= buffer_read_line(&input
);
438 die("invalid dump: expected blank line after content length header");
439 if (active_ctx
== REV_CTX
) {
441 } else if (active_ctx
== NODE_CTX
) {
443 active_ctx
= INTERNODE_CTX
;
445 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
446 if (buffer_skip_bytes(&input
, len
) != len
)
451 if (buffer_ferror(&input
))
453 if (active_ctx
== NODE_CTX
)
455 if (active_ctx
== REV_CTX
)
457 if (active_ctx
!= DUMP_CTX
)
461 int svndump_init(const char *filename
)
463 if (buffer_init(&input
, filename
))
464 return error("cannot open %s: %s", filename
, strerror(errno
));
465 fast_export_init(REPORT_FILENO
);
468 reset_node_ctx(NULL
);
472 void svndump_deinit(void)
475 fast_export_deinit();
478 reset_node_ctx(NULL
);
479 if (buffer_deinit(&input
))
480 fprintf(stderr
, "Input error\n");
482 fprintf(stderr
, "Output error\n");
485 void svndump_reset(void)
489 buffer_reset(&input
);
492 reset_node_ctx(NULL
);