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 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 /* Create memory pool for log messages */
31 obj_pool_gen(log
, char, 4096)
33 static char* log_copy(uint32_t length
, char *log
)
36 log_free(log_pool
.size
);
37 buffer
= log_pointer(log_alloc(length
));
38 strncpy(buffer
, log
, length
);
43 uint32_t action
, propLength
, textLength
, srcRev
, mark
, type
;
44 uint32_t src
[REPO_MAX_PATH_DEPTH
], dst
[REPO_MAX_PATH_DEPTH
];
45 uint32_t text_delta
, prop_delta
;
49 uint32_t revision
, author
;
50 unsigned long timestamp
;
55 uint32_t version
, uuid
, url
;
59 uint32_t svn_log
, svn_author
, svn_date
, svn_executable
, svn_special
, uuid
,
60 revision_number
, node_path
, node_kind
, node_action
,
61 node_copyfrom_path
, node_copyfrom_rev
, text_content_length
,
62 prop_content_length
, content_length
, svn_fs_dump_format_version
,
63 /* version 3 format */
64 text_delta
, prop_delta
;
67 static void reset_node_ctx(char *fname
)
70 node_ctx
.action
= NODEACT_UNKNOWN
;
71 node_ctx
.propLength
= LENGTH_UNKNOWN
;
72 node_ctx
.textLength
= LENGTH_UNKNOWN
;
75 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
, "/", fname
);
77 node_ctx
.text_delta
= 0;
78 node_ctx
.prop_delta
= 0;
81 static void reset_rev_ctx(uint32_t revision
)
83 rev_ctx
.revision
= revision
;
84 rev_ctx
.timestamp
= 0;
89 static void reset_dump_ctx(uint32_t url
)
96 static void init_keys(void)
98 keys
.svn_log
= pool_intern("svn:log");
99 keys
.svn_author
= pool_intern("svn:author");
100 keys
.svn_date
= pool_intern("svn:date");
101 keys
.svn_executable
= pool_intern("svn:executable");
102 keys
.svn_special
= pool_intern("svn:special");
103 keys
.uuid
= pool_intern("UUID");
104 keys
.revision_number
= pool_intern("Revision-number");
105 keys
.node_path
= pool_intern("Node-path");
106 keys
.node_kind
= pool_intern("Node-kind");
107 keys
.node_action
= pool_intern("Node-action");
108 keys
.node_copyfrom_path
= pool_intern("Node-copyfrom-path");
109 keys
.node_copyfrom_rev
= pool_intern("Node-copyfrom-rev");
110 keys
.text_content_length
= pool_intern("Text-content-length");
111 keys
.prop_content_length
= pool_intern("Prop-content-length");
112 keys
.content_length
= pool_intern("Content-length");
113 keys
.svn_fs_dump_format_version
= pool_intern("SVN-fs-dump-format-version");
114 /* version 3 format (Subversion 1.1.0) */
115 keys
.text_delta
= pool_intern("Text-delta");
116 keys
.prop_delta
= pool_intern("Prop-delta");
119 static void read_props(void)
125 while ((t
= buffer_read_line()) && strcmp(t
, "PROPS-END")) {
126 if (!strncmp(t
, "K ", 2)) {
128 key
= pool_intern(buffer_read_string(len
));
130 } else if (!strncmp(t
, "V ", 2)) {
132 val
= buffer_read_string(len
);
133 if (key
== keys
.svn_log
) {
134 /* Value length excludes terminating nul. */
135 rev_ctx
.log
= log_copy(len
+ 1, val
);
136 } else if (key
== keys
.svn_author
) {
137 rev_ctx
.author
= pool_intern(val
);
138 } else if (key
== keys
.svn_date
) {
139 if (parse_date_basic(val
, &rev_ctx
.timestamp
, NULL
))
140 fprintf(stderr
, "Invalid timestamp: %s\n", val
);
141 } else if (key
== keys
.svn_executable
) {
142 node_ctx
.type
= REPO_MODE_EXE
;
143 } else if (key
== keys
.svn_special
) {
144 node_ctx
.type
= REPO_MODE_LNK
;
152 static void handle_node(void)
154 uint32_t old_mode
= 0;
156 if (node_ctx
.text_delta
|| node_ctx
.prop_delta
)
157 die("text and property deltas not supported");
159 if (node_ctx
.propLength
!= LENGTH_UNKNOWN
&& node_ctx
.propLength
)
163 old_mode
= repo_copy(node_ctx
.srcRev
, node_ctx
.src
, node_ctx
.dst
);
165 if (node_ctx
.textLength
!= LENGTH_UNKNOWN
&&
166 node_ctx
.type
!= REPO_MODE_DIR
)
167 node_ctx
.mark
= next_blob_mark();
169 if (node_ctx
.action
== NODEACT_DELETE
) {
170 repo_delete(node_ctx
.dst
);
171 } else if (node_ctx
.action
== NODEACT_CHANGE
||
172 node_ctx
.action
== NODEACT_REPLACE
) {
173 if (node_ctx
.action
== NODEACT_REPLACE
&&
174 node_ctx
.type
== REPO_MODE_DIR
)
175 repo_replace(node_ctx
.dst
, node_ctx
.mark
);
176 else if (node_ctx
.propLength
!= LENGTH_UNKNOWN
)
177 repo_modify(node_ctx
.dst
, node_ctx
.type
, node_ctx
.mark
);
178 else if (node_ctx
.textLength
!= LENGTH_UNKNOWN
)
179 old_mode
= repo_replace(node_ctx
.dst
, node_ctx
.mark
);
180 } else if (node_ctx
.action
== NODEACT_ADD
) {
181 if (node_ctx
.srcRev
&& node_ctx
.propLength
!= LENGTH_UNKNOWN
)
182 repo_modify(node_ctx
.dst
, node_ctx
.type
, node_ctx
.mark
);
183 else if (node_ctx
.srcRev
&& node_ctx
.textLength
!= LENGTH_UNKNOWN
)
184 old_mode
= repo_replace(node_ctx
.dst
, node_ctx
.mark
);
185 else if ((node_ctx
.type
== REPO_MODE_DIR
&& !node_ctx
.srcRev
) ||
186 node_ctx
.textLength
!= LENGTH_UNKNOWN
)
187 repo_add(node_ctx
.dst
, node_ctx
.type
, node_ctx
.mark
);
190 if (node_ctx
.propLength
== LENGTH_UNKNOWN
&& old_mode
)
191 node_ctx
.type
= old_mode
;
194 fast_export_blob(node_ctx
.type
, node_ctx
.mark
, node_ctx
.textLength
);
195 else if (node_ctx
.textLength
!= LENGTH_UNKNOWN
)
196 buffer_skip_bytes(node_ctx
.textLength
);
199 static void handle_revision(void)
201 if (rev_ctx
.revision
)
202 repo_commit(rev_ctx
.revision
, rev_ctx
.author
, rev_ctx
.log
,
203 dump_ctx
.uuid
, dump_ctx
.url
, rev_ctx
.timestamp
);
206 void svndump_read(const char *url
)
210 uint32_t active_ctx
= DUMP_CTX
;
214 reset_dump_ctx(pool_intern(url
));
215 while ((t
= buffer_read_line())) {
216 val
= strstr(t
, ": ");
221 key
= pool_intern(t
);
223 if (key
== keys
.svn_fs_dump_format_version
) {
224 dump_ctx
.version
= atoi(val
);
225 if (dump_ctx
.version
> 3)
226 die("expected svn dump format version <= 3, found %d",
228 } else if (key
== keys
.uuid
) {
229 dump_ctx
.uuid
= pool_intern(val
);
230 } else if (key
== keys
.revision_number
) {
231 if (active_ctx
== NODE_CTX
)
233 if (active_ctx
!= DUMP_CTX
)
235 active_ctx
= REV_CTX
;
236 reset_rev_ctx(atoi(val
));
237 } else if (key
== keys
.node_path
) {
238 if (active_ctx
== NODE_CTX
)
240 active_ctx
= NODE_CTX
;
242 } else if (key
== keys
.node_kind
) {
243 if (!strcmp(val
, "dir"))
244 node_ctx
.type
= REPO_MODE_DIR
;
245 else if (!strcmp(val
, "file"))
246 node_ctx
.type
= REPO_MODE_BLB
;
248 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
249 } else if (key
== keys
.node_action
) {
250 if (!strcmp(val
, "delete")) {
251 node_ctx
.action
= NODEACT_DELETE
;
252 } else if (!strcmp(val
, "add")) {
253 node_ctx
.action
= NODEACT_ADD
;
254 } else if (!strcmp(val
, "change")) {
255 node_ctx
.action
= NODEACT_CHANGE
;
256 } else if (!strcmp(val
, "replace")) {
257 node_ctx
.action
= NODEACT_REPLACE
;
259 fprintf(stderr
, "Unknown node-action: %s\n", val
);
260 node_ctx
.action
= NODEACT_UNKNOWN
;
262 } else if (key
== keys
.node_copyfrom_path
) {
263 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.src
, "/", val
);
264 } else if (key
== keys
.node_copyfrom_rev
) {
265 node_ctx
.srcRev
= atoi(val
);
266 } else if (key
== keys
.text_content_length
) {
267 node_ctx
.textLength
= atoi(val
);
268 } else if (key
== keys
.prop_content_length
) {
269 node_ctx
.propLength
= atoi(val
);
270 } else if (key
== keys
.text_delta
) {
271 node_ctx
.text_delta
= !strcmp(val
, "true");
272 } else if (key
== keys
.prop_delta
) {
273 node_ctx
.prop_delta
= !strcmp(val
, "true");
274 } else if (key
== keys
.content_length
) {
277 if (active_ctx
== REV_CTX
) {
279 } else if (active_ctx
== NODE_CTX
) {
281 active_ctx
= REV_CTX
;
283 fprintf(stderr
, "Unexpected content length header: %"PRIu32
"\n", len
);
284 buffer_skip_bytes(len
);
288 if (active_ctx
== NODE_CTX
)
290 if (active_ctx
!= DUMP_CTX
)
294 int svndump_init(const char *filename
)
296 if (buffer_init(filename
))
297 return error("cannot open %s: %s", filename
, strerror(errno
));
301 reset_node_ctx(NULL
);
306 void svndump_deinit(void)
312 reset_node_ctx(NULL
);
314 fprintf(stderr
, "Input error\n");
316 fprintf(stderr
, "Output error\n");
319 void svndump_reset(void)
326 reset_node_ctx(NULL
);