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"
18 * Compare start of string to literal of equal length;
19 * must be guarded by length test.
21 #define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
23 #define NODEACT_REPLACE 4
24 #define NODEACT_DELETE 3
26 #define NODEACT_CHANGE 1
27 #define NODEACT_UNKNOWN 0
33 #define LENGTH_UNKNOWN (~0)
34 #define DATE_RFC2822_LEN 31
36 /* Create memory pool for log messages */
37 obj_pool_gen(log
, char, 4096)
39 static struct line_buffer input
= LINE_BUFFER_INIT
;
41 static char *log_copy(uint32_t length
, const char *log
)
44 log_free(log_pool
.size
);
45 buffer
= log_pointer(log_alloc(length
));
46 strncpy(buffer
, log
, length
);
51 uint32_t action
, propLength
, textLength
, srcRev
, type
;
52 uint32_t src
[REPO_MAX_PATH_DEPTH
], dst
[REPO_MAX_PATH_DEPTH
];
53 uint32_t text_delta
, prop_delta
;
57 uint32_t revision
, author
;
58 unsigned long timestamp
;
63 uint32_t version
, uuid
, url
;
66 static void reset_node_ctx(char *fname
)
69 node_ctx
.action
= NODEACT_UNKNOWN
;
70 node_ctx
.propLength
= LENGTH_UNKNOWN
;
71 node_ctx
.textLength
= LENGTH_UNKNOWN
;
74 pool_tok_seq(REPO_MAX_PATH_DEPTH
, 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;
87 static void reset_dump_ctx(uint32_t url
)
94 static void handle_property(const struct strbuf
*key_buf
,
95 const char *val
, uint32_t len
,
98 const char *key
= key_buf
->buf
;
99 size_t keylen
= key_buf
->len
;
101 switch (keylen
+ 1) {
102 case sizeof("svn:log"):
103 if (constcmp(key
, "svn:log"))
106 die("invalid dump: unsets svn:log");
107 /* Value length excludes terminating nul. */
108 rev_ctx
.log
= log_copy(len
+ 1, val
);
110 case sizeof("svn:author"):
111 if (constcmp(key
, "svn:author"))
113 rev_ctx
.author
= pool_intern(val
);
115 case sizeof("svn:date"):
116 if (constcmp(key
, "svn:date"))
119 die("invalid dump: unsets svn:date");
120 if (parse_date_basic(val
, &rev_ctx
.timestamp
, NULL
))
121 warning("invalid timestamp: %s", val
);
123 case sizeof("svn:executable"):
124 case sizeof("svn:special"):
125 if (keylen
== strlen("svn:executable") &&
126 constcmp(key
, "svn:executable"))
128 if (keylen
== strlen("svn:special") &&
129 constcmp(key
, "svn:special"))
134 die("invalid dump: sets type twice");
137 node_ctx
.type
= REPO_MODE_BLB
;
141 node_ctx
.type
= keylen
== strlen("svn:executable") ?
147 static void die_short_read(void)
149 if (buffer_ferror(&input
))
150 die_errno("error reading dump file");
151 die("invalid dump: unexpected end of file");
154 static void read_props(void)
156 static struct strbuf key
= STRBUF_INIT
;
159 * NEEDSWORK: to support simple mode changes like
166 * we keep track of whether a mode has been set and reset to
167 * plain file only if not. We should be keeping track of the
168 * symlink and executable bits separately instead.
170 uint32_t type_set
= 0;
171 while ((t
= buffer_read_line(&input
)) && strcmp(t
, "PROPS-END")) {
174 const char type
= t
[0];
177 if (!type
|| t
[1] != ' ')
178 die("invalid property line: %s\n", t
);
180 val
= buffer_read_string(&input
, len
);
181 if (!val
|| strlen(val
) != len
)
184 /* Discard trailing newline. */
185 ch
= buffer_read_char(&input
);
189 die("invalid dump: expected newline after %s", val
);
196 strbuf_add(&key
, val
, len
);
204 handle_property(&key
, val
, len
, &type_set
);
208 die("invalid property line: %s\n", t
);
213 static void handle_node(void)
216 const uint32_t type
= node_ctx
.type
;
217 const int have_props
= node_ctx
.propLength
!= LENGTH_UNKNOWN
;
218 const int have_text
= node_ctx
.textLength
!= LENGTH_UNKNOWN
;
220 if (node_ctx
.text_delta
)
221 die("text deltas not supported");
223 mark
= next_blob_mark();
224 if (node_ctx
.action
== NODEACT_DELETE
) {
225 if (have_text
|| have_props
|| node_ctx
.srcRev
)
226 die("invalid dump: deletion node has "
227 "copyfrom info, text, or properties");
228 return repo_delete(node_ctx
.dst
);
230 if (node_ctx
.action
== NODEACT_REPLACE
) {
231 repo_delete(node_ctx
.dst
);
232 node_ctx
.action
= NODEACT_ADD
;
234 if (node_ctx
.srcRev
) {
235 repo_copy(node_ctx
.srcRev
, node_ctx
.src
, node_ctx
.dst
);
236 if (node_ctx
.action
== NODEACT_ADD
)
237 node_ctx
.action
= NODEACT_CHANGE
;
239 if (have_text
&& type
== REPO_MODE_DIR
)
240 die("invalid dump: directories cannot have text attached");
243 * Decide on the new content (mark) and mode (node_ctx.type).
245 if (node_ctx
.action
== NODEACT_CHANGE
&& !~*node_ctx
.dst
) {
246 if (type
!= REPO_MODE_DIR
)
247 die("invalid dump: root of tree is not a regular file");
248 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
251 mark
= repo_read_path(node_ctx
.dst
);
252 mode
= repo_read_mode(node_ctx
.dst
);
253 if (mode
== REPO_MODE_DIR
&& type
!= REPO_MODE_DIR
)
254 die("invalid dump: cannot modify a directory into a file");
255 if (mode
!= REPO_MODE_DIR
&& type
== REPO_MODE_DIR
)
256 die("invalid dump: cannot modify a file into a directory");
257 node_ctx
.type
= mode
;
258 } else if (node_ctx
.action
== NODEACT_ADD
) {
259 if (!have_text
&& type
!= REPO_MODE_DIR
)
260 die("invalid dump: adds node without text");
262 die("invalid dump: Node-path block lacks Node-action");
266 * Adjust mode to reflect properties.
269 if (!node_ctx
.prop_delta
)
270 node_ctx
.type
= type
;
271 if (node_ctx
.propLength
)
278 repo_add(node_ctx
.dst
, node_ctx
.type
, mark
);
280 fast_export_blob(node_ctx
.type
, mark
,
281 node_ctx
.textLength
, &input
);
284 static void handle_revision(void)
286 if (rev_ctx
.revision
)
287 repo_commit(rev_ctx
.revision
, rev_ctx
.author
, rev_ctx
.log
,
288 dump_ctx
.uuid
, dump_ctx
.url
, rev_ctx
.timestamp
);
291 void svndump_read(const char *url
)
295 uint32_t active_ctx
= DUMP_CTX
;
298 reset_dump_ctx(pool_intern(url
));
299 while ((t
= buffer_read_line(&input
))) {
300 val
= strstr(t
, ": ");
305 /* strlen(key) + 1 */
306 switch (val
- t
- 1) {
307 case sizeof("SVN-fs-dump-format-version"):
308 if (constcmp(t
, "SVN-fs-dump-format-version"))
310 dump_ctx
.version
= atoi(val
);
311 if (dump_ctx
.version
> 3)
312 die("expected svn dump format version <= 3, found %"PRIu32
,
316 if (constcmp(t
, "UUID"))
318 dump_ctx
.uuid
= pool_intern(val
);
320 case sizeof("Revision-number"):
321 if (constcmp(t
, "Revision-number"))
323 if (active_ctx
== NODE_CTX
)
325 if (active_ctx
!= DUMP_CTX
)
327 active_ctx
= REV_CTX
;
328 reset_rev_ctx(atoi(val
));
330 case sizeof("Node-path"):
331 if (prefixcmp(t
, "Node-"))
333 if (!constcmp(t
+ strlen("Node-"), "path")) {
334 if (active_ctx
== NODE_CTX
)
336 active_ctx
= NODE_CTX
;
340 if (constcmp(t
+ strlen("Node-"), "kind"))
342 if (!strcmp(val
, "dir"))
343 node_ctx
.type
= REPO_MODE_DIR
;
344 else if (!strcmp(val
, "file"))
345 node_ctx
.type
= REPO_MODE_BLB
;
347 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
349 case sizeof("Node-action"):
350 if (constcmp(t
, "Node-action"))
352 if (!strcmp(val
, "delete")) {
353 node_ctx
.action
= NODEACT_DELETE
;
354 } else if (!strcmp(val
, "add")) {
355 node_ctx
.action
= NODEACT_ADD
;
356 } else if (!strcmp(val
, "change")) {
357 node_ctx
.action
= NODEACT_CHANGE
;
358 } else if (!strcmp(val
, "replace")) {
359 node_ctx
.action
= NODEACT_REPLACE
;
361 fprintf(stderr
, "Unknown node-action: %s\n", val
);
362 node_ctx
.action
= NODEACT_UNKNOWN
;
365 case sizeof("Node-copyfrom-path"):
366 if (constcmp(t
, "Node-copyfrom-path"))
368 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.src
, "/", val
);
370 case sizeof("Node-copyfrom-rev"):
371 if (constcmp(t
, "Node-copyfrom-rev"))
373 node_ctx
.srcRev
= atoi(val
);
375 case sizeof("Text-content-length"):
376 if (!constcmp(t
, "Text-content-length")) {
377 node_ctx
.textLength
= atoi(val
);
380 if (constcmp(t
, "Prop-content-length"))
382 node_ctx
.propLength
= atoi(val
);
384 case sizeof("Text-delta"):
385 if (!constcmp(t
, "Text-delta")) {
386 node_ctx
.text_delta
= !strcmp(val
, "true");
389 if (constcmp(t
, "Prop-delta"))
391 node_ctx
.prop_delta
= !strcmp(val
, "true");
393 case sizeof("Content-length"):
394 if (constcmp(t
, "Content-length"))
397 t
= buffer_read_line(&input
);
401 die("invalid dump: expected blank line after content length header");
402 if (active_ctx
== REV_CTX
) {
404 } else if (active_ctx
== NODE_CTX
) {
406 active_ctx
= REV_CTX
;
408 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
409 if (buffer_skip_bytes(&input
, len
) != len
)
414 if (buffer_ferror(&input
))
416 if (active_ctx
== NODE_CTX
)
418 if (active_ctx
!= DUMP_CTX
)
422 int svndump_init(const char *filename
)
424 if (buffer_init(&input
, filename
))
425 return error("cannot open %s: %s", filename
, strerror(errno
));
429 reset_node_ctx(NULL
);
433 void svndump_deinit(void)
439 reset_node_ctx(NULL
);
440 if (buffer_deinit(&input
))
441 fprintf(stderr
, "Input error\n");
443 fprintf(stderr
, "Output error\n");
446 void svndump_reset(void)
449 buffer_reset(&input
);
453 reset_node_ctx(NULL
);