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
19 #define NODEACT_REPLACE 4
20 #define NODEACT_DELETE 3
22 #define NODEACT_CHANGE 1
23 #define NODEACT_UNKNOWN 0
26 #define DUMP_CTX 0 /* dump metadata */
27 #define REV_CTX 1 /* revision metadata */
28 #define NODE_CTX 2 /* node metadata */
29 #define INTERNODE_CTX 3 /* between nodes */
31 #define LENGTH_UNKNOWN (~0)
32 #define DATE_RFC2822_LEN 31
34 /* Create memory pool for log messages */
35 obj_pool_gen(log
, char, 4096)
37 static struct line_buffer input
= LINE_BUFFER_INIT
;
39 #define REPORT_FILENO 3
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 svn_log
, svn_author
, svn_date
, svn_executable
, svn_special
, uuid
,
68 revision_number
, node_path
, node_kind
, node_action
,
69 node_copyfrom_path
, node_copyfrom_rev
, text_content_length
,
70 prop_content_length
, content_length
, svn_fs_dump_format_version
,
71 /* version 3 format */
72 text_delta
, prop_delta
;
75 static void reset_node_ctx(char *fname
)
78 node_ctx
.action
= NODEACT_UNKNOWN
;
79 node_ctx
.propLength
= LENGTH_UNKNOWN
;
80 node_ctx
.textLength
= LENGTH_UNKNOWN
;
83 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
, "/", fname
);
84 node_ctx
.text_delta
= 0;
85 node_ctx
.prop_delta
= 0;
88 static void reset_rev_ctx(uint32_t revision
)
90 rev_ctx
.revision
= revision
;
91 rev_ctx
.timestamp
= 0;
96 static void reset_dump_ctx(uint32_t url
)
103 static void init_keys(void)
105 keys
.svn_log
= pool_intern("svn:log");
106 keys
.svn_author
= pool_intern("svn:author");
107 keys
.svn_date
= pool_intern("svn:date");
108 keys
.svn_executable
= pool_intern("svn:executable");
109 keys
.svn_special
= pool_intern("svn:special");
110 keys
.uuid
= pool_intern("UUID");
111 keys
.revision_number
= pool_intern("Revision-number");
112 keys
.node_path
= pool_intern("Node-path");
113 keys
.node_kind
= pool_intern("Node-kind");
114 keys
.node_action
= pool_intern("Node-action");
115 keys
.node_copyfrom_path
= pool_intern("Node-copyfrom-path");
116 keys
.node_copyfrom_rev
= pool_intern("Node-copyfrom-rev");
117 keys
.text_content_length
= pool_intern("Text-content-length");
118 keys
.prop_content_length
= pool_intern("Prop-content-length");
119 keys
.content_length
= pool_intern("Content-length");
120 keys
.svn_fs_dump_format_version
= pool_intern("SVN-fs-dump-format-version");
121 /* version 3 format (Subversion 1.1.0) */
122 keys
.text_delta
= pool_intern("Text-delta");
123 keys
.prop_delta
= pool_intern("Prop-delta");
126 static void handle_property(uint32_t key
, const char *val
, uint32_t len
,
129 if (key
== keys
.svn_log
) {
131 die("invalid dump: unsets svn:log");
132 /* Value length excludes terminating nul. */
133 rev_ctx
.log
= log_copy(len
+ 1, val
);
134 } else if (key
== keys
.svn_author
) {
135 rev_ctx
.author
= pool_intern(val
);
136 } else if (key
== keys
.svn_date
) {
138 die("invalid dump: unsets svn:date");
139 if (parse_date_basic(val
, &rev_ctx
.timestamp
, NULL
))
140 warning("invalid timestamp: %s", val
);
141 } else if (key
== keys
.svn_executable
|| key
== keys
.svn_special
) {
145 die("invalid dump: sets type twice");
148 node_ctx
.type
= REPO_MODE_BLB
;
152 node_ctx
.type
= key
== keys
.svn_executable
?
158 static void read_props(void)
163 * NEEDSWORK: to support simple mode changes like
170 * we keep track of whether a mode has been set and reset to
171 * plain file only if not. We should be keeping track of the
172 * symlink and executable bits separately instead.
174 uint32_t type_set
= 0;
175 while ((t
= buffer_read_line(&input
)) && strcmp(t
, "PROPS-END")) {
178 const char type
= t
[0];
180 if (!type
|| t
[1] != ' ')
181 die("invalid property line: %s\n", t
);
183 val
= buffer_read_string(&input
, len
);
184 buffer_skip_bytes(&input
, 1); /* Discard trailing newline. */
188 key
= pool_intern(val
);
191 key
= pool_intern(val
);
196 handle_property(key
, val
, len
, &type_set
);
200 die("invalid property line: %s\n", t
);
205 static void handle_node(void)
207 const uint32_t type
= node_ctx
.type
;
208 const int have_props
= node_ctx
.propLength
!= LENGTH_UNKNOWN
;
209 const int have_text
= node_ctx
.textLength
!= LENGTH_UNKNOWN
;
211 * Old text for this node:
212 * NULL - directory or bug
214 * "<dataref>" - data retrievable from fast-import
216 static const char *const empty_blob
= "::empty::";
217 const char *old_data
= NULL
;
219 if (node_ctx
.text_delta
)
220 die("text deltas not supported");
222 if (node_ctx
.action
== NODEACT_DELETE
) {
223 if (have_text
|| have_props
|| node_ctx
.srcRev
)
224 die("invalid dump: deletion node has "
225 "copyfrom info, text, or properties");
226 return repo_delete(node_ctx
.dst
);
228 if (node_ctx
.action
== NODEACT_REPLACE
) {
229 repo_delete(node_ctx
.dst
);
230 node_ctx
.action
= NODEACT_ADD
;
232 if (node_ctx
.srcRev
) {
233 repo_copy(node_ctx
.srcRev
, node_ctx
.src
, node_ctx
.dst
);
234 if (node_ctx
.action
== NODEACT_ADD
)
235 node_ctx
.action
= NODEACT_CHANGE
;
237 if (have_text
&& type
== REPO_MODE_DIR
)
238 die("invalid dump: directories cannot have text attached");
241 * Find old content (old_data) and decide on the new mode.
243 if (node_ctx
.action
== NODEACT_CHANGE
&& !~*node_ctx
.dst
) {
244 if (type
!= REPO_MODE_DIR
)
245 die("invalid dump: root of tree is not a regular file");
247 } else if (node_ctx
.action
== NODEACT_CHANGE
) {
249 old_data
= repo_read_path(node_ctx
.dst
, &mode
);
250 if (mode
== REPO_MODE_DIR
&& type
!= REPO_MODE_DIR
)
251 die("invalid dump: cannot modify a directory into a file");
252 if (mode
!= REPO_MODE_DIR
&& type
== REPO_MODE_DIR
)
253 die("invalid dump: cannot modify a file into a directory");
254 node_ctx
.type
= mode
;
255 } else if (node_ctx
.action
== NODEACT_ADD
) {
256 if (type
== REPO_MODE_DIR
)
259 old_data
= empty_blob
;
261 die("invalid dump: adds node without text");
263 die("invalid dump: Node-path block lacks Node-action");
267 * Adjust mode to reflect properties.
270 if (!node_ctx
.prop_delta
)
271 node_ctx
.type
= type
;
272 if (node_ctx
.propLength
)
279 if (type
== REPO_MODE_DIR
) /* directories are not tracked. */
282 if (old_data
== empty_blob
)
283 /* For the fast_export_* functions, NULL means empty. */
286 fast_export_modify(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
,
287 node_ctx
.type
, old_data
);
290 fast_export_modify(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
,
291 node_ctx
.type
, "inline");
292 fast_export_data(node_ctx
.type
, node_ctx
.textLength
, &input
);
295 static void begin_revision(void)
297 if (!rev_ctx
.revision
) /* revision 0 gets no git commit. */
299 fast_export_begin_commit(rev_ctx
.revision
, rev_ctx
.author
, rev_ctx
.log
,
300 dump_ctx
.uuid
, dump_ctx
.url
, rev_ctx
.timestamp
);
303 static void end_revision(void)
305 if (rev_ctx
.revision
)
306 fast_export_end_commit(rev_ctx
.revision
);
309 void svndump_read(const char *url
)
313 uint32_t active_ctx
= DUMP_CTX
;
317 reset_dump_ctx(pool_intern(url
));
318 while ((t
= buffer_read_line(&input
))) {
319 val
= strstr(t
, ": ");
324 key
= pool_intern(t
);
326 if (key
== keys
.svn_fs_dump_format_version
) {
327 dump_ctx
.version
= atoi(val
);
328 if (dump_ctx
.version
> 3)
329 die("expected svn dump format version <= 3, found %"PRIu32
,
331 } else if (key
== keys
.uuid
) {
332 dump_ctx
.uuid
= pool_intern(val
);
333 } else if (key
== keys
.revision_number
) {
334 if (active_ctx
== NODE_CTX
)
336 if (active_ctx
== REV_CTX
)
338 if (active_ctx
!= DUMP_CTX
)
340 active_ctx
= REV_CTX
;
341 reset_rev_ctx(atoi(val
));
342 } else if (key
== keys
.node_path
) {
343 if (active_ctx
== NODE_CTX
)
345 if (active_ctx
== REV_CTX
)
347 active_ctx
= NODE_CTX
;
349 } else if (key
== keys
.node_kind
) {
350 if (!strcmp(val
, "dir"))
351 node_ctx
.type
= REPO_MODE_DIR
;
352 else if (!strcmp(val
, "file"))
353 node_ctx
.type
= REPO_MODE_BLB
;
355 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
356 } else if (key
== keys
.node_action
) {
357 if (!strcmp(val
, "delete")) {
358 node_ctx
.action
= NODEACT_DELETE
;
359 } else if (!strcmp(val
, "add")) {
360 node_ctx
.action
= NODEACT_ADD
;
361 } else if (!strcmp(val
, "change")) {
362 node_ctx
.action
= NODEACT_CHANGE
;
363 } else if (!strcmp(val
, "replace")) {
364 node_ctx
.action
= NODEACT_REPLACE
;
366 fprintf(stderr
, "Unknown node-action: %s\n", val
);
367 node_ctx
.action
= NODEACT_UNKNOWN
;
369 } else if (key
== keys
.node_copyfrom_path
) {
370 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.src
, "/", val
);
371 } else if (key
== keys
.node_copyfrom_rev
) {
372 node_ctx
.srcRev
= atoi(val
);
373 } else if (key
== keys
.text_content_length
) {
374 node_ctx
.textLength
= atoi(val
);
375 } else if (key
== keys
.prop_content_length
) {
376 node_ctx
.propLength
= atoi(val
);
377 } else if (key
== keys
.text_delta
) {
378 node_ctx
.text_delta
= !strcmp(val
, "true");
379 } else if (key
== keys
.prop_delta
) {
380 node_ctx
.prop_delta
= !strcmp(val
, "true");
381 } else if (key
== keys
.content_length
) {
383 buffer_read_line(&input
);
384 if (active_ctx
== REV_CTX
) {
386 } else if (active_ctx
== NODE_CTX
) {
388 active_ctx
= INTERNODE_CTX
;
390 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
391 buffer_skip_bytes(&input
, len
);
395 if (active_ctx
== NODE_CTX
)
397 if (active_ctx
== REV_CTX
)
399 if (active_ctx
!= DUMP_CTX
)
403 int svndump_init(const char *filename
)
405 if (buffer_init(&input
, filename
))
406 return error("cannot open %s: %s", filename
, strerror(errno
));
407 fast_export_init(REPORT_FILENO
);
410 reset_node_ctx(NULL
);
415 void svndump_deinit(void)
418 fast_export_deinit();
421 reset_node_ctx(NULL
);
422 if (buffer_deinit(&input
))
423 fprintf(stderr
, "Input error\n");
425 fprintf(stderr
, "Output error\n");
428 void svndump_reset(void)
432 buffer_reset(&input
);
435 reset_node_ctx(NULL
);