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 struct line_buffer input = LINE_BUFFER_INIT;
35 static char* log_copy(uint32_t length, char *log)
38 log_free(log_pool.size);
39 buffer = log_pointer(log_alloc(length));
40 strncpy(buffer, log, length);
45 uint32_t action, propLength, textLength, srcRev, srcMode, mark, type;
46 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
50 uint32_t revision, author;
51 unsigned long timestamp;
56 uint32_t version, uuid, url;
60 uint32_t svn_log, svn_author, svn_date, svn_executable, svn_special, uuid,
61 revision_number, node_path, node_kind, node_action,
62 node_copyfrom_path, node_copyfrom_rev, text_content_length,
63 prop_content_length, content_length, svn_fs_dump_format_version;
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;
75 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
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 init_keys(void)
96 keys.svn_log = pool_intern("svn:log");
97 keys.svn_author = pool_intern("svn:author");
98 keys.svn_date = pool_intern("svn:date");
99 keys.svn_executable = pool_intern("svn:executable");
100 keys.svn_special = pool_intern("svn:special");
101 keys.uuid = pool_intern("UUID");
102 keys.revision_number = pool_intern("Revision-number");
103 keys.node_path = pool_intern("Node-path");
104 keys.node_kind = pool_intern("Node-kind");
105 keys.node_action = pool_intern("Node-action");
106 keys.node_copyfrom_path = pool_intern("Node-copyfrom-path");
107 keys.node_copyfrom_rev = pool_intern("Node-copyfrom-rev");
108 keys.text_content_length = pool_intern("Text-content-length");
109 keys.prop_content_length = pool_intern("Prop-content-length");
110 keys.content_length = pool_intern("Content-length");
111 keys.svn_fs_dump_format_version = pool_intern("SVN-fs-dump-format-version");
114 static void read_props(void)
120 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
121 if (!strncmp(t, "K ", 2)) {
123 key = pool_intern(buffer_read_string(&input, len));
124 buffer_read_line(&input);
125 } else if (!strncmp(t, "V ", 2)) {
127 val = buffer_read_string(&input, len);
128 if (key == keys.svn_log) {
129 /* Value length excludes terminating nul. */
130 rev_ctx.log = log_copy(len + 1, val);
131 } else if (key == keys.svn_author) {
132 rev_ctx.author = pool_intern(val);
133 } else if (key == keys.svn_date) {
134 if (parse_date_basic(val, &rev_ctx.timestamp, NULL))
135 fprintf(stderr, "Invalid timestamp: %s\n", val);
136 } else if (key == keys.svn_executable) {
137 node_ctx.type = REPO_MODE_EXE;
138 } else if (key == keys.svn_special) {
139 node_ctx.type = REPO_MODE_LNK;
142 buffer_read_line(&input);
147 static void handle_node(void)
149 if (node_ctx.propLength != LENGTH_UNKNOWN && node_ctx.propLength)
153 node_ctx.srcMode = repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
155 if (node_ctx.textLength != LENGTH_UNKNOWN &&
156 node_ctx.type != REPO_MODE_DIR)
157 node_ctx.mark = next_blob_mark();
159 if (node_ctx.action == NODEACT_DELETE) {
160 repo_delete(node_ctx.dst);
161 } else if (node_ctx.action == NODEACT_CHANGE ||
162 node_ctx.action == NODEACT_REPLACE) {
163 if (node_ctx.action == NODEACT_REPLACE &&
164 node_ctx.type == REPO_MODE_DIR)
165 repo_replace(node_ctx.dst, node_ctx.mark);
166 else if (node_ctx.propLength != LENGTH_UNKNOWN)
167 repo_modify(node_ctx.dst, node_ctx.type, node_ctx.mark);
168 else if (node_ctx.textLength != LENGTH_UNKNOWN)
169 node_ctx.srcMode = repo_replace(node_ctx.dst, node_ctx.mark);
170 } else if (node_ctx.action == NODEACT_ADD) {
171 if (node_ctx.srcRev && node_ctx.propLength != LENGTH_UNKNOWN)
172 repo_modify(node_ctx.dst, node_ctx.type, node_ctx.mark);
173 else if (node_ctx.srcRev && node_ctx.textLength != LENGTH_UNKNOWN)
174 node_ctx.srcMode = repo_replace(node_ctx.dst, node_ctx.mark);
175 else if ((node_ctx.type == REPO_MODE_DIR && !node_ctx.srcRev) ||
176 node_ctx.textLength != LENGTH_UNKNOWN)
177 repo_add(node_ctx.dst, node_ctx.type, node_ctx.mark);
180 if (node_ctx.propLength == LENGTH_UNKNOWN && node_ctx.srcMode)
181 node_ctx.type = node_ctx.srcMode;
184 fast_export_blob(node_ctx.type,
185 node_ctx.mark, node_ctx.textLength, &input);
186 else if (node_ctx.textLength != LENGTH_UNKNOWN)
187 buffer_skip_bytes(&input, node_ctx.textLength);
190 static void handle_revision(void)
192 if (rev_ctx.revision)
193 repo_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log,
194 dump_ctx.uuid, dump_ctx.url, rev_ctx.timestamp);
197 void svndump_read(const char *url)
201 uint32_t active_ctx = DUMP_CTX;
205 reset_dump_ctx(pool_intern(url));
206 while ((t = buffer_read_line(&input))) {
207 val = strstr(t, ": ");
212 key = pool_intern(t);
214 if (key == keys.svn_fs_dump_format_version) {
215 dump_ctx.version = atoi(val);
216 if (dump_ctx.version > 2)
217 die("expected svn dump format version <= 2, found %"PRIu32,
219 } else if (key == keys.uuid) {
220 dump_ctx.uuid = pool_intern(val);
221 } else if (key == keys.revision_number) {
222 if (active_ctx == NODE_CTX)
224 if (active_ctx != DUMP_CTX)
226 active_ctx = REV_CTX;
227 reset_rev_ctx(atoi(val));
228 } else if (key == keys.node_path) {
229 if (active_ctx == NODE_CTX)
231 active_ctx = NODE_CTX;
233 } else if (key == keys.node_kind) {
234 if (!strcmp(val, "dir"))
235 node_ctx.type = REPO_MODE_DIR;
236 else if (!strcmp(val, "file"))
237 node_ctx.type = REPO_MODE_BLB;
239 fprintf(stderr, "Unknown node-kind: %s\n", val);
240 } else if (key == keys.node_action) {
241 if (!strcmp(val, "delete")) {
242 node_ctx.action = NODEACT_DELETE;
243 } else if (!strcmp(val, "add")) {
244 node_ctx.action = NODEACT_ADD;
245 } else if (!strcmp(val, "change")) {
246 node_ctx.action = NODEACT_CHANGE;
247 } else if (!strcmp(val, "replace")) {
248 node_ctx.action = NODEACT_REPLACE;
250 fprintf(stderr, "Unknown node-action: %s\n", val);
251 node_ctx.action = NODEACT_UNKNOWN;
253 } else if (key == keys.node_copyfrom_path) {
254 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
255 } else if (key == keys.node_copyfrom_rev) {
256 node_ctx.srcRev = atoi(val);
257 } else if (key == keys.text_content_length) {
258 node_ctx.textLength = atoi(val);
259 } else if (key == keys.prop_content_length) {
260 node_ctx.propLength = atoi(val);
261 } else if (key == keys.content_length) {
263 buffer_read_line(&input);
264 if (active_ctx == REV_CTX) {
266 } else if (active_ctx == NODE_CTX) {
268 active_ctx = REV_CTX;
270 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
271 buffer_skip_bytes(&input, len);
275 if (active_ctx == NODE_CTX)
277 if (active_ctx != DUMP_CTX)
281 void svndump_init(const char *filename)
283 buffer_init(&input, filename);
287 reset_node_ctx(NULL);
291 void svndump_deinit(void)
297 reset_node_ctx(NULL);
298 if (buffer_deinit(&input))
299 fprintf(stderr, "Input error\n");
301 fprintf(stderr, "Output error\n");
304 void svndump_reset(void)
307 buffer_reset(&input);
311 reset_node_ctx(NULL);