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 NODEACT_REPLACE 4
18 #define NODEACT_DELETE 3
20 #define NODEACT_CHANGE 1
21 #define NODEACT_UNKNOWN 0
27 #define LENGTH_UNKNOWN (~0)
28 #define DATE_RFC2822_LEN 31
30 static struct line_buffer input
= LINE_BUFFER_INIT
;
33 uint32_t action
, propLength
, textLength
, srcRev
, type
;
34 uint32_t src
[REPO_MAX_PATH_DEPTH
], dst
[REPO_MAX_PATH_DEPTH
];
35 uint32_t text_delta
, prop_delta
;
39 uint32_t revision
, author
;
40 unsigned long timestamp
;
45 uint32_t version
, uuid
, url
;
49 uint32_t svn_log
, svn_author
, svn_date
, svn_executable
, svn_special
, uuid
,
50 revision_number
, node_path
, node_kind
, node_action
,
51 node_copyfrom_path
, node_copyfrom_rev
, text_content_length
,
52 prop_content_length
, content_length
, svn_fs_dump_format_version
,
53 /* version 3 format */
54 text_delta
, prop_delta
;
57 static void reset_node_ctx(char *fname
)
60 node_ctx
.action
= NODEACT_UNKNOWN
;
61 node_ctx
.propLength
= LENGTH_UNKNOWN
;
62 node_ctx
.textLength
= LENGTH_UNKNOWN
;
65 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
, "/", fname
);
66 node_ctx
.text_delta
= 0;
67 node_ctx
.prop_delta
= 0;
70 static void reset_rev_ctx(uint32_t revision
)
72 rev_ctx
.revision
= revision
;
73 rev_ctx
.timestamp
= 0;
74 strbuf_reset(&rev_ctx
.log
);
78 static void reset_dump_ctx(uint32_t url
)
85 static void init_keys(void)
87 keys
.svn_log
= pool_intern("svn:log");
88 keys
.svn_author
= pool_intern("svn:author");
89 keys
.svn_date
= pool_intern("svn:date");
90 keys
.svn_executable
= pool_intern("svn:executable");
91 keys
.svn_special
= pool_intern("svn:special");
92 keys
.uuid
= pool_intern("UUID");
93 keys
.revision_number
= pool_intern("Revision-number");
94 keys
.node_path
= pool_intern("Node-path");
95 keys
.node_kind
= pool_intern("Node-kind");
96 keys
.node_action
= pool_intern("Node-action");
97 keys
.node_copyfrom_path
= pool_intern("Node-copyfrom-path");
98 keys
.node_copyfrom_rev
= pool_intern("Node-copyfrom-rev");
99 keys
.text_content_length
= pool_intern("Text-content-length");
100 keys
.prop_content_length
= pool_intern("Prop-content-length");
101 keys
.content_length
= pool_intern("Content-length");
102 keys
.svn_fs_dump_format_version
= pool_intern("SVN-fs-dump-format-version");
103 /* version 3 format (Subversion 1.1.0) */
104 keys
.text_delta
= pool_intern("Text-delta");
105 keys
.prop_delta
= pool_intern("Prop-delta");
108 static void handle_property(uint32_t key
, const char *val
, uint32_t len
,
111 if (key
== keys
.svn_log
) {
113 die("invalid dump: unsets svn:log");
114 strbuf_reset(&rev_ctx
.log
);
115 strbuf_add(&rev_ctx
.log
, val
, len
);
116 } else if (key
== keys
.svn_author
) {
117 rev_ctx
.author
= pool_intern(val
);
118 } else if (key
== keys
.svn_date
) {
120 die("invalid dump: unsets svn:date");
121 if (parse_date_basic(val
, &rev_ctx
.timestamp
, NULL
))
122 warning("invalid timestamp: %s", val
);
123 } else if (key
== keys
.svn_executable
|| key
== keys
.svn_special
) {
127 die("invalid dump: sets type twice");
130 node_ctx
.type
= REPO_MODE_BLB
;
134 node_ctx
.type
= key
== keys
.svn_executable
?
140 static void die_short_read(void)
142 if (buffer_ferror(&input
))
143 die_errno("error reading dump file");
144 die("invalid dump: unexpected end of file");
147 static void read_props(void)
152 * NEEDSWORK: to support simple mode changes like
159 * we keep track of whether a mode has been set and reset to
160 * plain file only if not. We should be keeping track of the
161 * symlink and executable bits separately instead.
163 uint32_t type_set
= 0;
164 while ((t
= buffer_read_line(&input
)) && strcmp(t
, "PROPS-END")) {
167 const char type
= t
[0];
170 if (!type
|| t
[1] != ' ')
171 die("invalid property line: %s\n", t
);
173 val
= buffer_read_string(&input
, len
);
174 if (!val
|| strlen(val
) != len
)
177 /* Discard trailing newline. */
178 ch
= buffer_read_char(&input
);
182 die("invalid dump: expected newline after %s", val
);
186 key
= pool_intern(val
);
189 key
= pool_intern(val
);
194 handle_property(key
, val
, len
, &type_set
);
198 die("invalid property line: %s\n", t
);
203 static void handle_node(void)
206 const uint32_t type
= node_ctx
.type
;
207 const int have_props
= node_ctx
.propLength
!= LENGTH_UNKNOWN
;
208 const int have_text
= node_ctx
.textLength
!= LENGTH_UNKNOWN
;
210 if (node_ctx
.text_delta
)
211 die("text deltas not supported");
213 mark
= next_blob_mark();
214 if (node_ctx
.action
== NODEACT_DELETE
) {
215 if (have_text
|| have_props
|| node_ctx
.srcRev
)
216 die("invalid dump: deletion node has "
217 "copyfrom info, text, or properties");
218 return repo_delete(node_ctx
.dst
);
220 if (node_ctx
.action
== NODEACT_REPLACE
) {
221 repo_delete(node_ctx
.dst
);
222 node_ctx
.action
= NODEACT_ADD
;
224 if (node_ctx
.srcRev
) {
225 repo_copy(node_ctx
.srcRev
, node_ctx
.src
, node_ctx
.dst
);
226 if (node_ctx
.action
== NODEACT_ADD
)
227 node_ctx
.action
= NODEACT_CHANGE
;
229 if (have_text
&& type
== REPO_MODE_DIR
)
230 die("invalid dump: directories cannot have text attached");
233 * Decide on the new content (mark) and mode (node_ctx.type).
235 if (node_ctx
.action
== NODEACT_CHANGE
&& !~*node_ctx
.dst
) {
236 if (type
!= REPO_MODE_DIR
)
237 die("invalid dump: root of tree is not a regular file");
238 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
241 mark
= repo_read_path(node_ctx
.dst
);
242 mode
= repo_read_mode(node_ctx
.dst
);
243 if (mode
== REPO_MODE_DIR
&& type
!= REPO_MODE_DIR
)
244 die("invalid dump: cannot modify a directory into a file");
245 if (mode
!= REPO_MODE_DIR
&& type
== REPO_MODE_DIR
)
246 die("invalid dump: cannot modify a file into a directory");
247 node_ctx
.type
= mode
;
248 } else if (node_ctx
.action
== NODEACT_ADD
) {
249 if (!have_text
&& type
!= REPO_MODE_DIR
)
250 die("invalid dump: adds node without text");
252 die("invalid dump: Node-path block lacks Node-action");
256 * Adjust mode to reflect properties.
259 if (!node_ctx
.prop_delta
)
260 node_ctx
.type
= type
;
261 if (node_ctx
.propLength
)
268 repo_add(node_ctx
.dst
, node_ctx
.type
, mark
);
270 fast_export_blob(node_ctx
.type
, mark
,
271 node_ctx
.textLength
, &input
);
274 static void handle_revision(void)
276 if (rev_ctx
.revision
)
277 repo_commit(rev_ctx
.revision
, rev_ctx
.author
, rev_ctx
.log
.buf
,
278 dump_ctx
.uuid
, dump_ctx
.url
, rev_ctx
.timestamp
);
281 void svndump_read(const char *url
)
285 uint32_t active_ctx
= DUMP_CTX
;
289 reset_dump_ctx(pool_intern(url
));
290 while ((t
= buffer_read_line(&input
))) {
291 val
= strstr(t
, ": ");
296 key
= pool_intern(t
);
298 if (key
== keys
.svn_fs_dump_format_version
) {
299 dump_ctx
.version
= atoi(val
);
300 if (dump_ctx
.version
> 3)
301 die("expected svn dump format version <= 3, found %"PRIu32
,
303 } else if (key
== keys
.uuid
) {
304 dump_ctx
.uuid
= pool_intern(val
);
305 } else if (key
== keys
.revision_number
) {
306 if (active_ctx
== NODE_CTX
)
308 if (active_ctx
!= DUMP_CTX
)
310 active_ctx
= REV_CTX
;
311 reset_rev_ctx(atoi(val
));
312 } else if (key
== keys
.node_path
) {
313 if (active_ctx
== NODE_CTX
)
315 active_ctx
= NODE_CTX
;
317 } else if (key
== keys
.node_kind
) {
318 if (!strcmp(val
, "dir"))
319 node_ctx
.type
= REPO_MODE_DIR
;
320 else if (!strcmp(val
, "file"))
321 node_ctx
.type
= REPO_MODE_BLB
;
323 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
324 } else if (key
== keys
.node_action
) {
325 if (!strcmp(val
, "delete")) {
326 node_ctx
.action
= NODEACT_DELETE
;
327 } else if (!strcmp(val
, "add")) {
328 node_ctx
.action
= NODEACT_ADD
;
329 } else if (!strcmp(val
, "change")) {
330 node_ctx
.action
= NODEACT_CHANGE
;
331 } else if (!strcmp(val
, "replace")) {
332 node_ctx
.action
= NODEACT_REPLACE
;
334 fprintf(stderr
, "Unknown node-action: %s\n", val
);
335 node_ctx
.action
= NODEACT_UNKNOWN
;
337 } else if (key
== keys
.node_copyfrom_path
) {
338 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.src
, "/", val
);
339 } else if (key
== keys
.node_copyfrom_rev
) {
340 node_ctx
.srcRev
= atoi(val
);
341 } else if (key
== keys
.text_content_length
) {
342 node_ctx
.textLength
= atoi(val
);
343 } else if (key
== keys
.prop_content_length
) {
344 node_ctx
.propLength
= atoi(val
);
345 } else if (key
== keys
.text_delta
) {
346 node_ctx
.text_delta
= !strcmp(val
, "true");
347 } else if (key
== keys
.prop_delta
) {
348 node_ctx
.prop_delta
= !strcmp(val
, "true");
349 } else if (key
== keys
.content_length
) {
351 t
= buffer_read_line(&input
);
355 die("invalid dump: expected blank line after content length header");
356 if (active_ctx
== REV_CTX
) {
358 } else if (active_ctx
== NODE_CTX
) {
360 active_ctx
= REV_CTX
;
362 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
363 if (buffer_skip_bytes(&input
, len
) != len
)
368 if (buffer_ferror(&input
))
370 if (active_ctx
== NODE_CTX
)
372 if (active_ctx
!= DUMP_CTX
)
376 int svndump_init(const char *filename
)
378 if (buffer_init(&input
, filename
))
379 return error("cannot open %s: %s", filename
, strerror(errno
));
381 strbuf_init(&rev_ctx
.log
, 4096);
384 reset_node_ctx(NULL
);
389 void svndump_deinit(void)
394 reset_node_ctx(NULL
);
395 strbuf_release(&rev_ctx
.log
);
396 if (buffer_deinit(&input
))
397 fprintf(stderr
, "Input error\n");
399 fprintf(stderr
, "Output error\n");
402 void svndump_reset(void)
404 buffer_reset(&input
);
408 reset_node_ctx(NULL
);