vcs-svn: use strbuf for revision log
[svn-fe.git] / svndump.c
blob559a8084ab10bd56f7d0033e465b42f30bd797aa
1 /*
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.
8 */
10 #include "cache.h"
11 #include "repo_tree.h"
12 #include "fast_export.h"
13 #include "line_buffer.h"
14 #include "string_pool.h"
15 #include "strbuf.h"
17 #define NODEACT_REPLACE 4
18 #define NODEACT_DELETE 3
19 #define NODEACT_ADD 2
20 #define NODEACT_CHANGE 1
21 #define NODEACT_UNKNOWN 0
23 #define DUMP_CTX 0
24 #define REV_CTX 1
25 #define NODE_CTX 2
27 #define LENGTH_UNKNOWN (~0)
28 #define DATE_RFC2822_LEN 31
30 static struct line_buffer input = LINE_BUFFER_INIT;
32 static struct {
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;
36 } node_ctx;
38 static struct {
39 uint32_t revision, author;
40 unsigned long timestamp;
41 struct strbuf log;
42 } rev_ctx;
44 static struct {
45 uint32_t version, uuid, url;
46 } dump_ctx;
48 static struct {
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;
55 } keys;
57 static void reset_node_ctx(char *fname)
59 node_ctx.type = 0;
60 node_ctx.action = NODEACT_UNKNOWN;
61 node_ctx.propLength = LENGTH_UNKNOWN;
62 node_ctx.textLength = LENGTH_UNKNOWN;
63 node_ctx.src[0] = ~0;
64 node_ctx.srcRev = 0;
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);
75 rev_ctx.author = ~0;
78 static void reset_dump_ctx(uint32_t url)
80 dump_ctx.url = url;
81 dump_ctx.version = 1;
82 dump_ctx.uuid = ~0;
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,
109 uint32_t *type_set)
111 if (key == keys.svn_log) {
112 if (!val)
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) {
119 if (!val)
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) {
124 if (*type_set) {
125 if (!val)
126 return;
127 die("invalid dump: sets type twice");
129 if (!val) {
130 node_ctx.type = REPO_MODE_BLB;
131 return;
133 *type_set = 1;
134 node_ctx.type = key == keys.svn_executable ?
135 REPO_MODE_EXE :
136 REPO_MODE_LNK;
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)
149 uint32_t key = ~0;
150 const char *t;
152 * NEEDSWORK: to support simple mode changes like
153 * K 11
154 * svn:special
155 * V 1
157 * D 14
158 * svn:executable
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")) {
165 uint32_t len;
166 const char *val;
167 const char type = t[0];
168 int ch;
170 if (!type || t[1] != ' ')
171 die("invalid property line: %s\n", t);
172 len = atoi(&t[2]);
173 val = buffer_read_string(&input, len);
174 if (!val || strlen(val) != len)
175 die_short_read();
177 /* Discard trailing newline. */
178 ch = buffer_read_char(&input);
179 if (ch == EOF)
180 die_short_read();
181 if (ch != '\n')
182 die("invalid dump: expected newline after %s", val);
184 switch (type) {
185 case 'K':
186 key = pool_intern(val);
187 continue;
188 case 'D':
189 key = pool_intern(val);
190 val = NULL;
191 len = 0;
192 /* fall through */
193 case 'V':
194 handle_property(key, val, len, &type_set);
195 key = ~0;
196 continue;
197 default:
198 die("invalid property line: %s\n", t);
203 static void handle_node(void)
205 uint32_t mark = 0;
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");
212 if (have_text)
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) {
239 uint32_t mode;
240 if (!have_text)
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");
251 } else {
252 die("invalid dump: Node-path block lacks Node-action");
256 * Adjust mode to reflect properties.
258 if (have_props) {
259 if (!node_ctx.prop_delta)
260 node_ctx.type = type;
261 if (node_ctx.propLength)
262 read_props();
266 * Save the result.
268 repo_add(node_ctx.dst, node_ctx.type, mark);
269 if (have_text)
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)
283 char *val;
284 char *t;
285 uint32_t active_ctx = DUMP_CTX;
286 uint32_t len;
287 uint32_t key;
289 reset_dump_ctx(pool_intern(url));
290 while ((t = buffer_read_line(&input))) {
291 val = strstr(t, ": ");
292 if (!val)
293 continue;
294 *val++ = '\0';
295 *val++ = '\0';
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,
302 dump_ctx.version);
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)
307 handle_node();
308 if (active_ctx != DUMP_CTX)
309 handle_revision();
310 active_ctx = REV_CTX;
311 reset_rev_ctx(atoi(val));
312 } else if (key == keys.node_path) {
313 if (active_ctx == NODE_CTX)
314 handle_node();
315 active_ctx = NODE_CTX;
316 reset_node_ctx(val);
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;
322 else
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;
333 } else {
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) {
350 len = atoi(val);
351 t = buffer_read_line(&input);
352 if (!t)
353 die_short_read();
354 if (*t)
355 die("invalid dump: expected blank line after content length header");
356 if (active_ctx == REV_CTX) {
357 read_props();
358 } else if (active_ctx == NODE_CTX) {
359 handle_node();
360 active_ctx = REV_CTX;
361 } else {
362 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
363 if (buffer_skip_bytes(&input, len) != len)
364 die_short_read();
368 if (buffer_ferror(&input))
369 die_short_read();
370 if (active_ctx == NODE_CTX)
371 handle_node();
372 if (active_ctx != DUMP_CTX)
373 handle_revision();
376 int svndump_init(const char *filename)
378 if (buffer_init(&input, filename))
379 return error("cannot open %s: %s", filename, strerror(errno));
380 repo_init();
381 strbuf_init(&rev_ctx.log, 4096);
382 reset_dump_ctx(~0);
383 reset_rev_ctx(0);
384 reset_node_ctx(NULL);
385 init_keys();
386 return 0;
389 void svndump_deinit(void)
391 repo_reset();
392 reset_dump_ctx(~0);
393 reset_rev_ctx(0);
394 reset_node_ctx(NULL);
395 strbuf_release(&rev_ctx.log);
396 if (buffer_deinit(&input))
397 fprintf(stderr, "Input error\n");
398 if (ferror(stdout))
399 fprintf(stderr, "Output error\n");
402 void svndump_reset(void)
404 buffer_reset(&input);
405 repo_reset();
406 reset_dump_ctx(~0);
407 reset_rev_ctx(0);
408 reset_node_ctx(NULL);