]> git.ipfire.org Git - thirdparty/git.git/blame - vcs-svn/svndump.c
Merge branch 'nm/maint-conflicted-submodule-entries'
[thirdparty/git.git] / vcs-svn / svndump.c
CommitLineData
21746aa3
DB
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"
21746aa3 14#include "string_pool.h"
dce33c9c 15#include "strbuf.h"
21746aa3 16
044ad290
DB
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
21746aa3
DB
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
e5e45ca1
JN
36static struct line_buffer input = LINE_BUFFER_INIT;
37
21746aa3 38static struct {
da3e2174 39 uint32_t action, propLength, textLength, srcRev, type;
21746aa3 40 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
1f05d07c 41 uint32_t text_delta, prop_delta;
21746aa3
DB
42} node_ctx;
43
44static struct {
7c5817d3 45 uint32_t revision;
21746aa3 46 unsigned long timestamp;
7c5817d3 47 struct strbuf log, author;
21746aa3
DB
48} rev_ctx;
49
50static struct {
7c5817d3
DB
51 uint32_t version;
52 struct strbuf uuid, url;
21746aa3
DB
53} dump_ctx;
54
21746aa3
DB
55static 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;
21746aa3 63 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
1f05d07c
DB
64 node_ctx.text_delta = 0;
65 node_ctx.prop_delta = 0;
21746aa3
DB
66}
67
68static void reset_rev_ctx(uint32_t revision)
69{
70 rev_ctx.revision = revision;
71 rev_ctx.timestamp = 0;
dce33c9c 72 strbuf_reset(&rev_ctx.log);
7c5817d3 73 strbuf_reset(&rev_ctx.author);
21746aa3
DB
74}
75
7c5817d3 76static void reset_dump_ctx(const char *url)
21746aa3 77{
7c5817d3
DB
78 strbuf_reset(&dump_ctx.url);
79 if (url)
80 strbuf_addstr(&dump_ctx.url, url);
b3e5bce1 81 dump_ctx.version = 1;
7c5817d3 82 strbuf_reset(&dump_ctx.uuid);
21746aa3
DB
83}
84
044ad290 85static void handle_property(const struct strbuf *key_buf,
4c3169b0 86 struct strbuf *val,
6b01b676 87 uint32_t *type_set)
2a48afe1 88{
044ad290
DB
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;
6b01b676
DB
96 if (!val)
97 die("invalid dump: unsets svn:log");
4c3169b0 98 strbuf_swap(&rev_ctx.log, val);
044ad290
DB
99 break;
100 case sizeof("svn:author"):
101 if (constcmp(key, "svn:author"))
102 break;
4c3169b0
JN
103 if (!val)
104 strbuf_reset(&rev_ctx.author);
105 else
106 strbuf_swap(&rev_ctx.author, val);
044ad290
DB
107 break;
108 case sizeof("svn:date"):
109 if (constcmp(key, "svn:date"))
110 break;
6b01b676
DB
111 if (!val)
112 die("invalid dump: unsets svn:date");
4c3169b0
JN
113 if (parse_date_basic(val->buf, &rev_ctx.timestamp, NULL))
114 warning("invalid timestamp: %s", val->buf);
044ad290
DB
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;
6b01b676
DB
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;
044ad290 134 node_ctx.type = keylen == strlen("svn:executable") ?
6b01b676
DB
135 REPO_MODE_EXE :
136 REPO_MODE_LNK;
2a48afe1 137 }
21746aa3
DB
138}
139
c9d1c8ba
JN
140static 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
21746aa3
DB
147static void read_props(void)
148{
044ad290 149 static struct strbuf key = STRBUF_INIT;
e7d04ee1 150 static struct strbuf val = STRBUF_INIT;
6263c06d 151 const char *t;
6b01b676
DB
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;
e5e45ca1 165 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
6263c06d 166 uint32_t len;
6263c06d 167 const char type = t[0];
c9d1c8ba 168 int ch;
6263c06d
JN
169
170 if (!type || t[1] != ' ')
171 die("invalid property line: %s\n", t);
172 len = atoi(&t[2]);
e7d04ee1
JN
173 strbuf_reset(&val);
174 buffer_read_binary(&input, &val, len);
175 if (val.len < len)
c9d1c8ba
JN
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')
e7d04ee1 183 die("invalid dump: expected newline after %s", val.buf);
6263c06d
JN
184
185 switch (type) {
186 case 'K':
e7d04ee1
JN
187 strbuf_swap(&key, &val);
188 continue;
6b01b676 189 case 'D':
4c3169b0 190 handle_property(&val, NULL, &type_set);
e7d04ee1 191 continue;
6263c06d 192 case 'V':
4c3169b0 193 handle_property(&key, &val, &type_set);
044ad290 194 strbuf_reset(&key);
6263c06d
JN
195 continue;
196 default:
197 die("invalid property line: %s\n", t);
21746aa3
DB
198 }
199 }
200}
201
202static void handle_node(void)
203{
1c7bb316
JN
204 uint32_t mark = 0;
205 const uint32_t type = node_ctx.type;
d6e81a03 206 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
5a38b186 207 const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
21746aa3 208
6b01b676
DB
209 if (node_ctx.text_delta)
210 die("text deltas not supported");
5a38b186 211 if (have_text)
462e1f51 212 mark = next_blob_mark();
21746aa3 213 if (node_ctx.action == NODEACT_DELETE) {
5a38b186 214 if (have_text || have_props || node_ctx.srcRev)
5af8fae2
JN
215 die("invalid dump: deletion node has "
216 "copyfrom info, text, or properties");
9e113988
MW
217 repo_delete(node_ctx.dst);
218 return;
5af8fae2 219 }
6ee4a9be 220 if (node_ctx.action == NODEACT_REPLACE) {
21746aa3 221 repo_delete(node_ctx.dst);
6ee4a9be
JN
222 node_ctx.action = NODEACT_ADD;
223 }
1c7bb316
JN
224 if (node_ctx.srcRev) {
225 repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
414e569e
JN
226 if (node_ctx.action == NODEACT_ADD)
227 node_ctx.action = NODEACT_CHANGE;
1c7bb316 228 }
5a38b186 229 if (have_text && type == REPO_MODE_DIR)
462e1f51 230 die("invalid dump: directories cannot have text attached");
5a38b186
JN
231
232 /*
233 * Decide on the new content (mark) and mode (node_ctx.type).
234 */
9e8c5321
JN
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) {
5a38b186
JN
239 uint32_t mode;
240 if (!have_text)
241 mark = repo_read_path(node_ctx.dst);
e75316de 242 mode = repo_read_mode(node_ctx.dst);
c7dbf35e
JN
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;
21746aa3 248 } else if (node_ctx.action == NODEACT_ADD) {
5a38b186 249 if (!have_text && type != REPO_MODE_DIR)
c7dbf35e 250 die("invalid dump: adds node without text");
c7dbf35e 251 } else {
414e569e 252 die("invalid dump: Node-path block lacks Node-action");
21746aa3 253 }
5a38b186
JN
254
255 /*
256 * Adjust mode to reflect properties.
257 */
1c7bb316 258 if (have_props) {
6b01b676
DB
259 if (!node_ctx.prop_delta)
260 node_ctx.type = type;
1c7bb316
JN
261 if (node_ctx.propLength)
262 read_props();
21746aa3 263 }
5a38b186
JN
264
265 /*
266 * Save the result.
267 */
268 repo_add(node_ctx.dst, node_ctx.type, mark);
269 if (have_text)
a62bbf8f
JN
270 fast_export_blob(node_ctx.type, mark,
271 node_ctx.textLength, &input);
21746aa3
DB
272}
273
274static void handle_revision(void)
275{
276 if (rev_ctx.revision)
7c5817d3 277 repo_commit(rev_ctx.revision, rev_ctx.author.buf,
195b7ca6 278 &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
7c5817d3 279 rev_ctx.timestamp);
21746aa3
DB
280}
281
282void svndump_read(const char *url)
283{
284 char *val;
285 char *t;
286 uint32_t active_ctx = DUMP_CTX;
287 uint32_t len;
21746aa3 288
7c5817d3 289 reset_dump_ctx(url);
e5e45ca1 290 while ((t = buffer_read_line(&input))) {
f1602054 291 val = strchr(t, ':');
21746aa3
DB
292 if (!val)
293 continue;
f1602054
DB
294 val++;
295 if (*val != ' ')
296 continue;
297 val++;
21746aa3 298
90c0a3cf
DB
299 /* strlen(key) + 1 */
300 switch (val - t - 1) {
301 case sizeof("SVN-fs-dump-format-version"):
302 if (constcmp(t, "SVN-fs-dump-format-version"))
303 continue;
b3e5bce1 304 dump_ctx.version = atoi(val);
1f05d07c 305 if (dump_ctx.version > 3)
a62bbf8f 306 die("expected svn dump format version <= 3, found %"PRIu32,
b3e5bce1 307 dump_ctx.version);
90c0a3cf
DB
308 break;
309 case sizeof("UUID"):
310 if (constcmp(t, "UUID"))
311 continue;
7c5817d3
DB
312 strbuf_reset(&dump_ctx.uuid);
313 strbuf_addstr(&dump_ctx.uuid, val);
90c0a3cf
DB
314 break;
315 case sizeof("Revision-number"):
316 if (constcmp(t, "Revision-number"))
317 continue;
21746aa3
DB
318 if (active_ctx == NODE_CTX)
319 handle_node();
320 if (active_ctx != DUMP_CTX)
321 handle_revision();
322 active_ctx = REV_CTX;
323 reset_rev_ctx(atoi(val));
90c0a3cf
DB
324 break;
325 case sizeof("Node-path"):
326 if (prefixcmp(t, "Node-"))
327 continue;
328 if (!constcmp(t + strlen("Node-"), "path")) {
329 if (active_ctx == NODE_CTX)
330 handle_node();
331 active_ctx = NODE_CTX;
332 reset_node_ctx(val);
333 break;
334 }
335 if (constcmp(t + strlen("Node-"), "kind"))
336 continue;
21746aa3
DB
337 if (!strcmp(val, "dir"))
338 node_ctx.type = REPO_MODE_DIR;
339 else if (!strcmp(val, "file"))
340 node_ctx.type = REPO_MODE_BLB;
341 else
342 fprintf(stderr, "Unknown node-kind: %s\n", val);
90c0a3cf
DB
343 break;
344 case sizeof("Node-action"):
345 if (constcmp(t, "Node-action"))
346 continue;
21746aa3
DB
347 if (!strcmp(val, "delete")) {
348 node_ctx.action = NODEACT_DELETE;
349 } else if (!strcmp(val, "add")) {
350 node_ctx.action = NODEACT_ADD;
351 } else if (!strcmp(val, "change")) {
352 node_ctx.action = NODEACT_CHANGE;
353 } else if (!strcmp(val, "replace")) {
354 node_ctx.action = NODEACT_REPLACE;
355 } else {
356 fprintf(stderr, "Unknown node-action: %s\n", val);
357 node_ctx.action = NODEACT_UNKNOWN;
358 }
90c0a3cf
DB
359 break;
360 case sizeof("Node-copyfrom-path"):
361 if (constcmp(t, "Node-copyfrom-path"))
362 continue;
21746aa3 363 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
90c0a3cf
DB
364 break;
365 case sizeof("Node-copyfrom-rev"):
366 if (constcmp(t, "Node-copyfrom-rev"))
367 continue;
21746aa3 368 node_ctx.srcRev = atoi(val);
90c0a3cf
DB
369 break;
370 case sizeof("Text-content-length"):
371 if (!constcmp(t, "Text-content-length")) {
372 node_ctx.textLength = atoi(val);
373 break;
374 }
375 if (constcmp(t, "Prop-content-length"))
376 continue;
21746aa3 377 node_ctx.propLength = atoi(val);
90c0a3cf
DB
378 break;
379 case sizeof("Text-delta"):
380 if (!constcmp(t, "Text-delta")) {
381 node_ctx.text_delta = !strcmp(val, "true");
382 break;
383 }
384 if (constcmp(t, "Prop-delta"))
385 continue;
1f05d07c 386 node_ctx.prop_delta = !strcmp(val, "true");
90c0a3cf
DB
387 break;
388 case sizeof("Content-length"):
389 if (constcmp(t, "Content-length"))
390 continue;
21746aa3 391 len = atoi(val);
c9d1c8ba
JN
392 t = buffer_read_line(&input);
393 if (!t)
394 die_short_read();
395 if (*t)
396 die("invalid dump: expected blank line after content length header");
21746aa3
DB
397 if (active_ctx == REV_CTX) {
398 read_props();
399 } else if (active_ctx == NODE_CTX) {
400 handle_node();
401 active_ctx = REV_CTX;
402 } else {
5418d96d 403 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
c9d1c8ba
JN
404 if (buffer_skip_bytes(&input, len) != len)
405 die_short_read();
21746aa3
DB
406 }
407 }
408 }
c9d1c8ba
JN
409 if (buffer_ferror(&input))
410 die_short_read();
21746aa3
DB
411 if (active_ctx == NODE_CTX)
412 handle_node();
413 if (active_ctx != DUMP_CTX)
414 handle_revision();
415}
416
5c28a8b0 417int svndump_init(const char *filename)
21746aa3 418{
a62bbf8f 419 if (buffer_init(&input, filename))
5c28a8b0 420 return error("cannot open %s: %s", filename, strerror(errno));
21746aa3 421 repo_init();
7c5817d3
DB
422 strbuf_init(&dump_ctx.uuid, 4096);
423 strbuf_init(&dump_ctx.url, 4096);
dce33c9c 424 strbuf_init(&rev_ctx.log, 4096);
7c5817d3
DB
425 strbuf_init(&rev_ctx.author, 4096);
426 reset_dump_ctx(NULL);
21746aa3
DB
427 reset_rev_ctx(0);
428 reset_node_ctx(NULL);
5c28a8b0 429 return 0;
21746aa3
DB
430}
431
432void svndump_deinit(void)
433{
21746aa3 434 repo_reset();
7c5817d3 435 reset_dump_ctx(NULL);
21746aa3
DB
436 reset_rev_ctx(0);
437 reset_node_ctx(NULL);
dce33c9c 438 strbuf_release(&rev_ctx.log);
e5e45ca1 439 if (buffer_deinit(&input))
21746aa3
DB
440 fprintf(stderr, "Input error\n");
441 if (ferror(stdout))
442 fprintf(stderr, "Output error\n");
443}
444
445void svndump_reset(void)
446{
e5e45ca1 447 buffer_reset(&input);
21746aa3 448 repo_reset();
7c5817d3
DB
449 strbuf_release(&dump_ctx.uuid);
450 strbuf_release(&dump_ctx.url);
451 strbuf_release(&rev_ctx.log);
452 strbuf_release(&rev_ctx.author);
21746aa3 453}