]> git.ipfire.org Git - thirdparty/git.git/blob - vcs-svn/svndump.c
Merge branch 'jc/index-update-if-able'
[thirdparty/git.git] / vcs-svn / svndump.c
1 /*
2 * Parse and rearrange a svnadmin dump.
3 * Create the dump with:
4 * svnadmin dump --incremental -r<startrev>:<endrev> <repository> >outfile
5 *
6 * Licensed under a two-clause BSD-style license.
7 * See LICENSE for details.
8 */
9
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"
16
17 /*
18 * Compare start of string to literal of equal length;
19 * must be guarded by length test.
20 */
21 #define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
22
23 #define NODEACT_REPLACE 4
24 #define NODEACT_DELETE 3
25 #define NODEACT_ADD 2
26 #define NODEACT_CHANGE 1
27 #define NODEACT_UNKNOWN 0
28
29 #define DUMP_CTX 0
30 #define REV_CTX 1
31 #define NODE_CTX 2
32
33 #define LENGTH_UNKNOWN (~0)
34 #define DATE_RFC2822_LEN 31
35
36 static struct line_buffer input = LINE_BUFFER_INIT;
37
38 static struct {
39 uint32_t action, propLength, textLength, srcRev, type;
40 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
41 uint32_t text_delta, prop_delta;
42 } node_ctx;
43
44 static struct {
45 uint32_t revision;
46 unsigned long timestamp;
47 struct strbuf log, author;
48 } rev_ctx;
49
50 static struct {
51 uint32_t version;
52 struct strbuf uuid, url;
53 } dump_ctx;
54
55 static void reset_node_ctx(char *fname)
56 {
57 node_ctx.type = 0;
58 node_ctx.action = NODEACT_UNKNOWN;
59 node_ctx.propLength = LENGTH_UNKNOWN;
60 node_ctx.textLength = LENGTH_UNKNOWN;
61 node_ctx.src[0] = ~0;
62 node_ctx.srcRev = 0;
63 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
64 node_ctx.text_delta = 0;
65 node_ctx.prop_delta = 0;
66 }
67
68 static void reset_rev_ctx(uint32_t revision)
69 {
70 rev_ctx.revision = revision;
71 rev_ctx.timestamp = 0;
72 strbuf_reset(&rev_ctx.log);
73 strbuf_reset(&rev_ctx.author);
74 }
75
76 static void reset_dump_ctx(const char *url)
77 {
78 strbuf_reset(&dump_ctx.url);
79 if (url)
80 strbuf_addstr(&dump_ctx.url, url);
81 dump_ctx.version = 1;
82 strbuf_reset(&dump_ctx.uuid);
83 }
84
85 static void handle_property(const struct strbuf *key_buf,
86 struct strbuf *val,
87 uint32_t *type_set)
88 {
89 const char *key = key_buf->buf;
90 size_t keylen = key_buf->len;
91
92 switch (keylen + 1) {
93 case sizeof("svn:log"):
94 if (constcmp(key, "svn:log"))
95 break;
96 if (!val)
97 die("invalid dump: unsets svn:log");
98 strbuf_swap(&rev_ctx.log, val);
99 break;
100 case sizeof("svn:author"):
101 if (constcmp(key, "svn:author"))
102 break;
103 if (!val)
104 strbuf_reset(&rev_ctx.author);
105 else
106 strbuf_swap(&rev_ctx.author, val);
107 break;
108 case sizeof("svn:date"):
109 if (constcmp(key, "svn:date"))
110 break;
111 if (!val)
112 die("invalid dump: unsets svn:date");
113 if (parse_date_basic(val->buf, &rev_ctx.timestamp, NULL))
114 warning("invalid timestamp: %s", val->buf);
115 break;
116 case sizeof("svn:executable"):
117 case sizeof("svn:special"):
118 if (keylen == strlen("svn:executable") &&
119 constcmp(key, "svn:executable"))
120 break;
121 if (keylen == strlen("svn:special") &&
122 constcmp(key, "svn:special"))
123 break;
124 if (*type_set) {
125 if (!val)
126 return;
127 die("invalid dump: sets type twice");
128 }
129 if (!val) {
130 node_ctx.type = REPO_MODE_BLB;
131 return;
132 }
133 *type_set = 1;
134 node_ctx.type = keylen == strlen("svn:executable") ?
135 REPO_MODE_EXE :
136 REPO_MODE_LNK;
137 }
138 }
139
140 static void die_short_read(void)
141 {
142 if (buffer_ferror(&input))
143 die_errno("error reading dump file");
144 die("invalid dump: unexpected end of file");
145 }
146
147 static void read_props(void)
148 {
149 static struct strbuf key = STRBUF_INIT;
150 static struct strbuf val = STRBUF_INIT;
151 const char *t;
152 /*
153 * NEEDSWORK: to support simple mode changes like
154 * K 11
155 * svn:special
156 * V 1
157 * *
158 * D 14
159 * svn:executable
160 * we keep track of whether a mode has been set and reset to
161 * plain file only if not. We should be keeping track of the
162 * symlink and executable bits separately instead.
163 */
164 uint32_t type_set = 0;
165 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
166 uint32_t len;
167 const char type = t[0];
168 int ch;
169
170 if (!type || t[1] != ' ')
171 die("invalid property line: %s\n", t);
172 len = atoi(&t[2]);
173 strbuf_reset(&val);
174 buffer_read_binary(&input, &val, len);
175 if (val.len < len)
176 die_short_read();
177
178 /* Discard trailing newline. */
179 ch = buffer_read_char(&input);
180 if (ch == EOF)
181 die_short_read();
182 if (ch != '\n')
183 die("invalid dump: expected newline after %s", val.buf);
184
185 switch (type) {
186 case 'K':
187 strbuf_swap(&key, &val);
188 continue;
189 case 'D':
190 handle_property(&val, NULL, &type_set);
191 continue;
192 case 'V':
193 handle_property(&key, &val, &type_set);
194 strbuf_reset(&key);
195 continue;
196 default:
197 die("invalid property line: %s\n", t);
198 }
199 }
200 }
201
202 static void handle_node(void)
203 {
204 uint32_t mark = 0;
205 const uint32_t type = node_ctx.type;
206 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
207 const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
208
209 if (node_ctx.text_delta)
210 die("text deltas not supported");
211 if (have_text)
212 mark = next_blob_mark();
213 if (node_ctx.action == NODEACT_DELETE) {
214 if (have_text || have_props || node_ctx.srcRev)
215 die("invalid dump: deletion node has "
216 "copyfrom info, text, or properties");
217 return repo_delete(node_ctx.dst);
218 }
219 if (node_ctx.action == NODEACT_REPLACE) {
220 repo_delete(node_ctx.dst);
221 node_ctx.action = NODEACT_ADD;
222 }
223 if (node_ctx.srcRev) {
224 repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
225 if (node_ctx.action == NODEACT_ADD)
226 node_ctx.action = NODEACT_CHANGE;
227 }
228 if (have_text && type == REPO_MODE_DIR)
229 die("invalid dump: directories cannot have text attached");
230
231 /*
232 * Decide on the new content (mark) and mode (node_ctx.type).
233 */
234 if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
235 if (type != REPO_MODE_DIR)
236 die("invalid dump: root of tree is not a regular file");
237 } else if (node_ctx.action == NODEACT_CHANGE) {
238 uint32_t mode;
239 if (!have_text)
240 mark = repo_read_path(node_ctx.dst);
241 mode = repo_read_mode(node_ctx.dst);
242 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
243 die("invalid dump: cannot modify a directory into a file");
244 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
245 die("invalid dump: cannot modify a file into a directory");
246 node_ctx.type = mode;
247 } else if (node_ctx.action == NODEACT_ADD) {
248 if (!have_text && type != REPO_MODE_DIR)
249 die("invalid dump: adds node without text");
250 } else {
251 die("invalid dump: Node-path block lacks Node-action");
252 }
253
254 /*
255 * Adjust mode to reflect properties.
256 */
257 if (have_props) {
258 if (!node_ctx.prop_delta)
259 node_ctx.type = type;
260 if (node_ctx.propLength)
261 read_props();
262 }
263
264 /*
265 * Save the result.
266 */
267 repo_add(node_ctx.dst, node_ctx.type, mark);
268 if (have_text)
269 fast_export_blob(node_ctx.type, mark,
270 node_ctx.textLength, &input);
271 }
272
273 static void handle_revision(void)
274 {
275 if (rev_ctx.revision)
276 repo_commit(rev_ctx.revision, rev_ctx.author.buf,
277 &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
278 rev_ctx.timestamp);
279 }
280
281 void svndump_read(const char *url)
282 {
283 char *val;
284 char *t;
285 uint32_t active_ctx = DUMP_CTX;
286 uint32_t len;
287
288 reset_dump_ctx(url);
289 while ((t = buffer_read_line(&input))) {
290 val = strchr(t, ':');
291 if (!val)
292 continue;
293 val++;
294 if (*val != ' ')
295 continue;
296 val++;
297
298 /* strlen(key) + 1 */
299 switch (val - t - 1) {
300 case sizeof("SVN-fs-dump-format-version"):
301 if (constcmp(t, "SVN-fs-dump-format-version"))
302 continue;
303 dump_ctx.version = atoi(val);
304 if (dump_ctx.version > 3)
305 die("expected svn dump format version <= 3, found %"PRIu32,
306 dump_ctx.version);
307 break;
308 case sizeof("UUID"):
309 if (constcmp(t, "UUID"))
310 continue;
311 strbuf_reset(&dump_ctx.uuid);
312 strbuf_addstr(&dump_ctx.uuid, val);
313 break;
314 case sizeof("Revision-number"):
315 if (constcmp(t, "Revision-number"))
316 continue;
317 if (active_ctx == NODE_CTX)
318 handle_node();
319 if (active_ctx != DUMP_CTX)
320 handle_revision();
321 active_ctx = REV_CTX;
322 reset_rev_ctx(atoi(val));
323 break;
324 case sizeof("Node-path"):
325 if (prefixcmp(t, "Node-"))
326 continue;
327 if (!constcmp(t + strlen("Node-"), "path")) {
328 if (active_ctx == NODE_CTX)
329 handle_node();
330 active_ctx = NODE_CTX;
331 reset_node_ctx(val);
332 break;
333 }
334 if (constcmp(t + strlen("Node-"), "kind"))
335 continue;
336 if (!strcmp(val, "dir"))
337 node_ctx.type = REPO_MODE_DIR;
338 else if (!strcmp(val, "file"))
339 node_ctx.type = REPO_MODE_BLB;
340 else
341 fprintf(stderr, "Unknown node-kind: %s\n", val);
342 break;
343 case sizeof("Node-action"):
344 if (constcmp(t, "Node-action"))
345 continue;
346 if (!strcmp(val, "delete")) {
347 node_ctx.action = NODEACT_DELETE;
348 } else if (!strcmp(val, "add")) {
349 node_ctx.action = NODEACT_ADD;
350 } else if (!strcmp(val, "change")) {
351 node_ctx.action = NODEACT_CHANGE;
352 } else if (!strcmp(val, "replace")) {
353 node_ctx.action = NODEACT_REPLACE;
354 } else {
355 fprintf(stderr, "Unknown node-action: %s\n", val);
356 node_ctx.action = NODEACT_UNKNOWN;
357 }
358 break;
359 case sizeof("Node-copyfrom-path"):
360 if (constcmp(t, "Node-copyfrom-path"))
361 continue;
362 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
363 break;
364 case sizeof("Node-copyfrom-rev"):
365 if (constcmp(t, "Node-copyfrom-rev"))
366 continue;
367 node_ctx.srcRev = atoi(val);
368 break;
369 case sizeof("Text-content-length"):
370 if (!constcmp(t, "Text-content-length")) {
371 node_ctx.textLength = atoi(val);
372 break;
373 }
374 if (constcmp(t, "Prop-content-length"))
375 continue;
376 node_ctx.propLength = atoi(val);
377 break;
378 case sizeof("Text-delta"):
379 if (!constcmp(t, "Text-delta")) {
380 node_ctx.text_delta = !strcmp(val, "true");
381 break;
382 }
383 if (constcmp(t, "Prop-delta"))
384 continue;
385 node_ctx.prop_delta = !strcmp(val, "true");
386 break;
387 case sizeof("Content-length"):
388 if (constcmp(t, "Content-length"))
389 continue;
390 len = atoi(val);
391 t = buffer_read_line(&input);
392 if (!t)
393 die_short_read();
394 if (*t)
395 die("invalid dump: expected blank line after content length header");
396 if (active_ctx == REV_CTX) {
397 read_props();
398 } else if (active_ctx == NODE_CTX) {
399 handle_node();
400 active_ctx = REV_CTX;
401 } else {
402 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
403 if (buffer_skip_bytes(&input, len) != len)
404 die_short_read();
405 }
406 }
407 }
408 if (buffer_ferror(&input))
409 die_short_read();
410 if (active_ctx == NODE_CTX)
411 handle_node();
412 if (active_ctx != DUMP_CTX)
413 handle_revision();
414 }
415
416 int svndump_init(const char *filename)
417 {
418 if (buffer_init(&input, filename))
419 return error("cannot open %s: %s", filename, strerror(errno));
420 repo_init();
421 strbuf_init(&dump_ctx.uuid, 4096);
422 strbuf_init(&dump_ctx.url, 4096);
423 strbuf_init(&rev_ctx.log, 4096);
424 strbuf_init(&rev_ctx.author, 4096);
425 reset_dump_ctx(NULL);
426 reset_rev_ctx(0);
427 reset_node_ctx(NULL);
428 return 0;
429 }
430
431 void svndump_deinit(void)
432 {
433 repo_reset();
434 reset_dump_ctx(NULL);
435 reset_rev_ctx(0);
436 reset_node_ctx(NULL);
437 strbuf_release(&rev_ctx.log);
438 if (buffer_deinit(&input))
439 fprintf(stderr, "Input error\n");
440 if (ferror(stdout))
441 fprintf(stderr, "Output error\n");
442 }
443
444 void svndump_reset(void)
445 {
446 buffer_reset(&input);
447 repo_reset();
448 strbuf_release(&dump_ctx.uuid);
449 strbuf_release(&dump_ctx.url);
450 strbuf_release(&rev_ctx.log);
451 strbuf_release(&rev_ctx.author);
452 }