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
;
67 uint32_t uuid
, revision_number
, node_path
, node_kind
, node_action
,
68 node_copyfrom_path
, node_copyfrom_rev
, text_content_length
,
69 prop_content_length
, content_length
, svn_fs_dump_format_version
,
70 /* version 3 format */
71 text_delta
, prop_delta
;
74 static void reset_node_ctx(char *fname
)
77 node_ctx
.action
= NODEACT_UNKNOWN
;
78 node_ctx
.propLength
= LENGTH_UNKNOWN
;
79 node_ctx
.textLength
= LENGTH_UNKNOWN
;
82 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
, "/", fname
);
83 node_ctx
.text_delta
= 0;
84 node_ctx
.prop_delta
= 0;
87 static void reset_rev_ctx(uint32_t revision
)
89 rev_ctx
.revision
= revision
;
90 rev_ctx
.timestamp
= 0;
95 static void reset_dump_ctx(uint32_t url
)
102 static void init_keys(void)
104 keys
.uuid
= pool_intern("UUID");
105 keys
.revision_number
= pool_intern("Revision-number");
106 keys
.node_path
= pool_intern("Node-path");
107 keys
.node_kind
= pool_intern("Node-kind");
108 keys
.node_action
= pool_intern("Node-action");
109 keys
.node_copyfrom_path
= pool_intern("Node-copyfrom-path");
110 keys
.node_copyfrom_rev
= pool_intern("Node-copyfrom-rev");
111 keys
.text_content_length
= pool_intern("Text-content-length");
112 keys
.prop_content_length
= pool_intern("Prop-content-length");
113 keys
.content_length
= pool_intern("Content-length");
114 keys
.svn_fs_dump_format_version
= pool_intern("SVN-fs-dump-format-version");
115 /* version 3 format (Subversion 1.1.0) */
116 keys
.text_delta
= pool_intern("Text-delta");
117 keys
.prop_delta
= pool_intern("Prop-delta");
120 static void handle_property(const struct strbuf
*key_buf
,
121 const char *val
, uint32_t len
,
124 const char *key
= key_buf
->buf
;
125 size_t keylen
= key_buf
->len
;
127 switch (keylen
+ 1) {
128 case sizeof("svn:log"):
129 if (constcmp(key
, "svn:log"))
132 die("invalid dump: unsets svn:log");
133 /* Value length excludes terminating nul. */
134 rev_ctx
.log
= log_copy(len
+ 1, val
);
136 case sizeof("svn:author"):
137 if (constcmp(key
, "svn:author"))
139 rev_ctx
.author
= pool_intern(val
);
141 case sizeof("svn:date"):
142 if (constcmp(key
, "svn:date"))
145 die("invalid dump: unsets svn:date");
146 if (parse_date_basic(val
, &rev_ctx
.timestamp
, NULL
))
147 warning("invalid timestamp: %s", val
);
149 case sizeof("svn:executable"):
150 case sizeof("svn:special"):
151 if (keylen
== strlen("svn:executable") &&
152 constcmp(key
, "svn:executable"))
154 if (keylen
== strlen("svn:special") &&
155 constcmp(key
, "svn:special"))
160 die("invalid dump: sets type twice");
163 node_ctx
.type
= REPO_MODE_BLB
;
167 node_ctx
.type
= keylen
== strlen("svn:executable") ?
173 static void die_short_read(void)
175 if (buffer_ferror(&input
))
176 die_errno("error reading dump file");
177 die("invalid dump: unexpected end of file");
180 static void read_props(void)
182 static struct strbuf key
= STRBUF_INIT
;
185 * NEEDSWORK: to support simple mode changes like
192 * we keep track of whether a mode has been set and reset to
193 * plain file only if not. We should be keeping track of the
194 * symlink and executable bits separately instead.
196 uint32_t type_set
= 0;
197 while ((t
= buffer_read_line(&input
)) && strcmp(t
, "PROPS-END")) {
200 const char type
= t
[0];
203 if (!type
|| t
[1] != ' ')
204 die("invalid property line: %s\n", t
);
206 val
= buffer_read_string(&input
, len
);
207 if (!val
|| strlen(val
) != len
)
210 /* Discard trailing newline. */
211 ch
= buffer_read_char(&input
);
215 die("invalid dump: expected newline after %s", val
);
222 strbuf_add(&key
, val
, len
);
230 handle_property(&key
, val
, len
, &type_set
);
234 die("invalid property line: %s\n", t
);
239 static void handle_node(void)
242 const uint32_t type
= node_ctx
.type
;
243 const int have_props
= node_ctx
.propLength
!= LENGTH_UNKNOWN
;
244 const int have_text
= node_ctx
.textLength
!= LENGTH_UNKNOWN
;
246 if (node_ctx
.text_delta
)
247 die("text deltas not supported");
249 mark
= next_blob_mark();
250 if (node_ctx
.action
== NODEACT_DELETE
) {
251 if (have_text
|| have_props
|| node_ctx
.srcRev
)
252 die("invalid dump: deletion node has "
253 "copyfrom info, text, or properties");
254 return repo_delete(node_ctx
.dst
);
256 if (node_ctx
.action
== NODEACT_REPLACE
) {
257 repo_delete(node_ctx
.dst
);
258 node_ctx
.action
= NODEACT_ADD
;
260 if (node_ctx
.srcRev
) {
261 repo_copy(node_ctx
.srcRev
, node_ctx
.src
, node_ctx
.dst
);
262 if (node_ctx
.action
== NODEACT_ADD
)
263 node_ctx
.action
= NODEACT_CHANGE
;
265 if (have_text
&& type
== REPO_MODE_DIR
)
266 die("invalid dump: directories cannot have text attached");
269 * Decide on the new content (mark) and mode (node_ctx.type).
271 if (node_ctx
.action
== NODEACT_CHANGE
&& !~*node_ctx
.dst
) {
272 if (type
!= REPO_MODE_DIR
)
273 die("invalid dump: root of tree is not a regular file");
274 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
277 mark
= repo_read_path(node_ctx
.dst
);
278 mode
= repo_read_mode(node_ctx
.dst
);
279 if (mode
== REPO_MODE_DIR
&& type
!= REPO_MODE_DIR
)
280 die("invalid dump: cannot modify a directory into a file");
281 if (mode
!= REPO_MODE_DIR
&& type
== REPO_MODE_DIR
)
282 die("invalid dump: cannot modify a file into a directory");
283 node_ctx
.type
= mode
;
284 } else if (node_ctx
.action
== NODEACT_ADD
) {
285 if (!have_text
&& type
!= REPO_MODE_DIR
)
286 die("invalid dump: adds node without text");
288 die("invalid dump: Node-path block lacks Node-action");
292 * Adjust mode to reflect properties.
295 if (!node_ctx
.prop_delta
)
296 node_ctx
.type
= type
;
297 if (node_ctx
.propLength
)
304 repo_add(node_ctx
.dst
, node_ctx
.type
, mark
);
306 fast_export_blob(node_ctx
.type
, mark
,
307 node_ctx
.textLength
, &input
);
310 static void handle_revision(void)
312 if (rev_ctx
.revision
)
313 repo_commit(rev_ctx
.revision
, rev_ctx
.author
, rev_ctx
.log
,
314 dump_ctx
.uuid
, dump_ctx
.url
, rev_ctx
.timestamp
);
317 void svndump_read(const char *url
)
321 uint32_t active_ctx
= DUMP_CTX
;
325 reset_dump_ctx(pool_intern(url
));
326 while ((t
= buffer_read_line(&input
))) {
327 val
= strstr(t
, ": ");
332 key
= pool_intern(t
);
334 if (key
== keys
.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
,
339 } else if (key
== keys
.uuid
) {
340 dump_ctx
.uuid
= pool_intern(val
);
341 } else if (key
== keys
.revision_number
) {
342 if (active_ctx
== NODE_CTX
)
344 if (active_ctx
!= DUMP_CTX
)
346 active_ctx
= REV_CTX
;
347 reset_rev_ctx(atoi(val
));
348 } else if (key
== keys
.node_path
) {
349 if (active_ctx
== NODE_CTX
)
351 active_ctx
= NODE_CTX
;
353 } else if (key
== keys
.node_kind
) {
354 if (!strcmp(val
, "dir"))
355 node_ctx
.type
= REPO_MODE_DIR
;
356 else if (!strcmp(val
, "file"))
357 node_ctx
.type
= REPO_MODE_BLB
;
359 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
360 } else if (key
== keys
.node_action
) {
361 if (!strcmp(val
, "delete")) {
362 node_ctx
.action
= NODEACT_DELETE
;
363 } else if (!strcmp(val
, "add")) {
364 node_ctx
.action
= NODEACT_ADD
;
365 } else if (!strcmp(val
, "change")) {
366 node_ctx
.action
= NODEACT_CHANGE
;
367 } else if (!strcmp(val
, "replace")) {
368 node_ctx
.action
= NODEACT_REPLACE
;
370 fprintf(stderr
, "Unknown node-action: %s\n", val
);
371 node_ctx
.action
= NODEACT_UNKNOWN
;
373 } else if (key
== keys
.node_copyfrom_path
) {
374 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.src
, "/", val
);
375 } else if (key
== keys
.node_copyfrom_rev
) {
376 node_ctx
.srcRev
= atoi(val
);
377 } else if (key
== keys
.text_content_length
) {
378 node_ctx
.textLength
= atoi(val
);
379 } else if (key
== keys
.prop_content_length
) {
380 node_ctx
.propLength
= atoi(val
);
381 } else if (key
== keys
.text_delta
) {
382 node_ctx
.text_delta
= !strcmp(val
, "true");
383 } else if (key
== keys
.prop_delta
) {
384 node_ctx
.prop_delta
= !strcmp(val
, "true");
385 } else if (key
== keys
.content_length
) {
387 t
= buffer_read_line(&input
);
391 die("invalid dump: expected blank line after content length header");
392 if (active_ctx
== REV_CTX
) {
394 } else if (active_ctx
== NODE_CTX
) {
396 active_ctx
= REV_CTX
;
398 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
399 if (buffer_skip_bytes(&input
, len
) != len
)
404 if (buffer_ferror(&input
))
406 if (active_ctx
== NODE_CTX
)
408 if (active_ctx
!= DUMP_CTX
)
412 int svndump_init(const char *filename
)
414 if (buffer_init(&input
, filename
))
415 return error("cannot open %s: %s", filename
, strerror(errno
));
419 reset_node_ctx(NULL
);
424 void svndump_deinit(void)
430 reset_node_ctx(NULL
);
431 if (buffer_deinit(&input
))
432 fprintf(stderr
, "Input error\n");
434 fprintf(stderr
, "Output error\n");
437 void svndump_reset(void)
440 buffer_reset(&input
);
444 reset_node_ctx(NULL
);