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 "obj_pool.h"
15#include "string_pool.h"
16
17#define REPORT_FILENO 3
18
19#define NODEACT_REPLACE 4
20#define NODEACT_DELETE 3
21#define NODEACT_ADD 2
22#define NODEACT_CHANGE 1
23#define NODEACT_UNKNOWN 0
24
25/* States: */
26#define DUMP_CTX 0 /* dump metadata */
27#define REV_CTX 1 /* revision metadata */
28#define NODE_CTX 2 /* node metadata */
29#define INTERNODE_CTX 3 /* between nodes */
30
31#define LENGTH_UNKNOWN (~0)
32#define DATE_RFC2822_LEN 31
33
34/* Create memory pool for log messages */
35obj_pool_gen(log, char, 4096)
36
37static struct line_buffer input = LINE_BUFFER_INIT;
38
39#define REPORT_FILENO 3
40
41static char *log_copy(uint32_t length, const char *log)
42{
43 char *buffer;
44 log_free(log_pool.size);
45 buffer = log_pointer(log_alloc(length));
46 strncpy(buffer, log, length);
47 return buffer;
48}
49
50static struct {
51 uint32_t action, propLength, textLength, srcRev, type;
52 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
53 uint32_t text_delta, prop_delta;
54} node_ctx;
55
56static struct {
57 uint32_t revision, author;
58 unsigned long timestamp;
59 char *log;
60} rev_ctx;
61
62static struct {
63 uint32_t version, uuid, url;
64} dump_ctx;
65
66static struct {
67 uint32_t svn_log, svn_author, svn_date, svn_executable, svn_special, uuid,
68 revision_number, node_path, node_kind, node_action,
69 node_copyfrom_path, node_copyfrom_rev, text_content_length,
70 prop_content_length, content_length, svn_fs_dump_format_version,
71 /* version 3 format */
72 text_delta, prop_delta;
73} keys;
74
75static void reset_node_ctx(char *fname)
76{
77 node_ctx.type = 0;
78 node_ctx.action = NODEACT_UNKNOWN;
79 node_ctx.propLength = LENGTH_UNKNOWN;
80 node_ctx.textLength = LENGTH_UNKNOWN;
81 node_ctx.src[0] = ~0;
82 node_ctx.srcRev = 0;
83 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
84 node_ctx.text_delta = 0;
85 node_ctx.prop_delta = 0;
86}
87
88static void reset_rev_ctx(uint32_t revision)
89{
90 rev_ctx.revision = revision;
91 rev_ctx.timestamp = 0;
92 rev_ctx.log = NULL;
93 rev_ctx.author = ~0;
94}
95
96static void reset_dump_ctx(uint32_t url)
97{
98 dump_ctx.url = url;
99 dump_ctx.version = 1;
100 dump_ctx.uuid = ~0;
101}
102
103static void init_keys(void)
104{
105 keys.svn_log = pool_intern("svn:log");
106 keys.svn_author = pool_intern("svn:author");
107 keys.svn_date = pool_intern("svn:date");
108 keys.svn_executable = pool_intern("svn:executable");
109 keys.svn_special = pool_intern("svn:special");
110 keys.uuid = pool_intern("UUID");
111 keys.revision_number = pool_intern("Revision-number");
112 keys.node_path = pool_intern("Node-path");
113 keys.node_kind = pool_intern("Node-kind");
114 keys.node_action = pool_intern("Node-action");
115 keys.node_copyfrom_path = pool_intern("Node-copyfrom-path");
116 keys.node_copyfrom_rev = pool_intern("Node-copyfrom-rev");
117 keys.text_content_length = pool_intern("Text-content-length");
118 keys.prop_content_length = pool_intern("Prop-content-length");
119 keys.content_length = pool_intern("Content-length");
120 keys.svn_fs_dump_format_version = pool_intern("SVN-fs-dump-format-version");
121 /* version 3 format (Subversion 1.1.0) */
122 keys.text_delta = pool_intern("Text-delta");
123 keys.prop_delta = pool_intern("Prop-delta");
124}
125
126static void handle_property(uint32_t key, const char *val, uint32_t len,
127 uint32_t *type_set)
128{
129 if (key == keys.svn_log) {
130 if (!val)
131 die("invalid dump: unsets svn:log");
132 /* Value length excludes terminating nul. */
133 rev_ctx.log = log_copy(len + 1, val);
134 } else if (key == keys.svn_author) {
135 rev_ctx.author = pool_intern(val);
136 } else if (key == keys.svn_date) {
137 if (!val)
138 die("invalid dump: unsets svn:date");
139 if (parse_date_basic(val, &rev_ctx.timestamp, NULL))
140 warning("invalid timestamp: %s", val);
141 } else if (key == keys.svn_executable || key == keys.svn_special) {
142 if (*type_set) {
143 if (!val)
144 return;
145 die("invalid dump: sets type twice");
146 }
147 if (!val) {
148 node_ctx.type = REPO_MODE_BLB;
149 return;
150 }
151 *type_set = 1;
152 node_ctx.type = key == keys.svn_executable ?
153 REPO_MODE_EXE :
154 REPO_MODE_LNK;
155 }
156}
157
158static void read_props(void)
159{
160 uint32_t key = ~0;
161 const char *t;
162 /*
163 * NEEDSWORK: to support simple mode changes like
164 * K 11
165 * svn:special
166 * V 1
167 * *
168 * D 14
169 * svn:executable
170 * we keep track of whether a mode has been set and reset to
171 * plain file only if not. We should be keeping track of the
172 * symlink and executable bits separately instead.
173 */
174 uint32_t type_set = 0;
175 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
176 uint32_t len;
177 const char *val;
178 const char type = t[0];
179
180 if (!type || t[1] != ' ')
181 die("invalid property line: %s\n", t);
182 len = atoi(&t[2]);
183 val = buffer_read_string(&input, len);
184 buffer_skip_bytes(&input, 1); /* Discard trailing newline. */
185
186 switch (type) {
187 case 'K':
188 key = pool_intern(val);
189 continue;
190 case 'D':
191 key = pool_intern(val);
192 val = NULL;
193 len = 0;
194 /* fall through */
195 case 'V':
196 handle_property(key, val, len, &type_set);
197 key = ~0;
198 continue;
199 default:
200 die("invalid property line: %s\n", t);
201 }
202 }
203}
204
205static void handle_node(void)
206{
207 const uint32_t type = node_ctx.type;
208 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
209 const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
210 /*
211 * Old text for this node:
212 * NULL - directory or bug
213 * empty_blob - empty
214 * "<dataref>" - data retrievable from fast-import
215 */
216 static const char *const empty_blob = "::empty::";
217 const char *old_data = NULL;
218
219 if (node_ctx.text_delta)
220 die("text deltas not supported");
221
222 if (node_ctx.action == NODEACT_DELETE) {
223 if (have_text || have_props || node_ctx.srcRev)
224 die("invalid dump: deletion node has "
225 "copyfrom info, text, or properties");
226 return repo_delete(node_ctx.dst);
227 }
228 if (node_ctx.action == NODEACT_REPLACE) {
229 repo_delete(node_ctx.dst);
230 node_ctx.action = NODEACT_ADD;
231 }
232 if (node_ctx.srcRev) {
233 repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
234 if (node_ctx.action == NODEACT_ADD)
235 node_ctx.action = NODEACT_CHANGE;
236 }
237 if (have_text && type == REPO_MODE_DIR)
238 die("invalid dump: directories cannot have text attached");
239
240 /*
241 * Find old content (old_data) and decide on the new mode.
242 */
243 if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
244 if (type != REPO_MODE_DIR)
245 die("invalid dump: root of tree is not a regular file");
246 old_data = NULL;
247 } else if (node_ctx.action == NODEACT_CHANGE) {
248 uint32_t mode;
249 old_data = repo_read_path(node_ctx.dst);
250 mode = repo_read_mode(node_ctx.dst);
251 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
252 die("invalid dump: cannot modify a directory into a file");
253 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
254 die("invalid dump: cannot modify a file into a directory");
255 node_ctx.type = mode;
256 } else if (node_ctx.action == NODEACT_ADD) {
257 if (type == REPO_MODE_DIR)
258 old_data = NULL;
259 else if (have_text)
260 old_data = empty_blob;
261 else
262 die("invalid dump: adds node without text");
263 } else {
264 die("invalid dump: Node-path block lacks Node-action");
265 }
266
267 /*
268 * Adjust mode to reflect properties.
269 */
270 if (have_props) {
271 if (!node_ctx.prop_delta)
272 node_ctx.type = type;
273 if (node_ctx.propLength)
274 read_props();
275 }
276
277 /*
278 * Save the result.
279 */
280 if (type == REPO_MODE_DIR) /* directories are not tracked. */
281 return;
282 assert(old_data);
283 if (old_data == empty_blob)
284 /* For the fast_export_* functions, NULL means empty. */
285 old_data = NULL;
286 if (!have_text) {
287 fast_export_modify(REPO_MAX_PATH_DEPTH, node_ctx.dst,
288 node_ctx.type, old_data);
289 return;
290 }
291 fast_export_modify(REPO_MAX_PATH_DEPTH, node_ctx.dst,
292 node_ctx.type, "inline");
293 fast_export_data(node_ctx.type, node_ctx.textLength, &input);
294}
295
296static void begin_revision(void)
297{
298 if (!rev_ctx.revision) /* revision 0 gets no git commit. */
299 return;
300 fast_export_begin_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log,
301 dump_ctx.uuid, dump_ctx.url, rev_ctx.timestamp);
302}
303
304static void end_revision(void)
305{
306 if (rev_ctx.revision)
307 fast_export_end_commit(rev_ctx.revision);
308}
309
310void svndump_read(const char *url)
311{
312 char *val;
313 char *t;
314 uint32_t active_ctx = DUMP_CTX;
315 uint32_t len;
316 uint32_t key;
317
318 reset_dump_ctx(pool_intern(url));
319 while ((t = buffer_read_line(&input))) {
320 val = strstr(t, ": ");
321 if (!val)
322 continue;
323 *val++ = '\0';
324 *val++ = '\0';
325 key = pool_intern(t);
326
327 if (key == keys.svn_fs_dump_format_version) {
328 dump_ctx.version = atoi(val);
329 if (dump_ctx.version > 3)
330 die("expected svn dump format version <= 3, found %"PRIu32,
331 dump_ctx.version);
332 } else if (key == keys.uuid) {
333 dump_ctx.uuid = pool_intern(val);
334 } else if (key == keys.revision_number) {
335 if (active_ctx == NODE_CTX)
336 handle_node();
337 if (active_ctx == REV_CTX)
338 begin_revision();
339 if (active_ctx != DUMP_CTX)
340 end_revision();
341 active_ctx = REV_CTX;
342 reset_rev_ctx(atoi(val));
343 } else if (key == keys.node_path) {
344 if (active_ctx == NODE_CTX)
345 handle_node();
346 if (active_ctx == REV_CTX)
347 begin_revision();
348 active_ctx = NODE_CTX;
349 reset_node_ctx(val);
350 } else if (key == keys.node_kind) {
351 if (!strcmp(val, "dir"))
352 node_ctx.type = REPO_MODE_DIR;
353 else if (!strcmp(val, "file"))
354 node_ctx.type = REPO_MODE_BLB;
355 else
356 fprintf(stderr, "Unknown node-kind: %s\n", val);
357 } else if (key == keys.node_action) {
358 if (!strcmp(val, "delete")) {
359 node_ctx.action = NODEACT_DELETE;
360 } else if (!strcmp(val, "add")) {
361 node_ctx.action = NODEACT_ADD;
362 } else if (!strcmp(val, "change")) {
363 node_ctx.action = NODEACT_CHANGE;
364 } else if (!strcmp(val, "replace")) {
365 node_ctx.action = NODEACT_REPLACE;
366 } else {
367 fprintf(stderr, "Unknown node-action: %s\n", val);
368 node_ctx.action = NODEACT_UNKNOWN;
369 }
370 } else if (key == keys.node_copyfrom_path) {
371 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
372 } else if (key == keys.node_copyfrom_rev) {
373 node_ctx.srcRev = atoi(val);
374 } else if (key == keys.text_content_length) {
375 node_ctx.textLength = atoi(val);
376 } else if (key == keys.prop_content_length) {
377 node_ctx.propLength = atoi(val);
378 } else if (key == keys.text_delta) {
379 node_ctx.text_delta = !strcmp(val, "true");
380 } else if (key == keys.prop_delta) {
381 node_ctx.prop_delta = !strcmp(val, "true");
382 } else if (key == keys.content_length) {
383 len = atoi(val);
384 buffer_read_line(&input);
385 if (active_ctx == REV_CTX) {
386 read_props();
387 } else if (active_ctx == NODE_CTX) {
388 handle_node();
389 active_ctx = INTERNODE_CTX;
390 } else {
391 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
392 buffer_skip_bytes(&input, len);
393 }
394 }
395 }
396 if (active_ctx == NODE_CTX)
397 handle_node();
398 if (active_ctx == REV_CTX)
399 begin_revision();
400 if (active_ctx != DUMP_CTX)
401 end_revision();
402}
403
404int svndump_init(const char *filename)
405{
406 if (buffer_init(&input, filename))
407 return error("cannot open %s: %s", filename, strerror(errno));
408 fast_export_init(REPORT_FILENO);
409 reset_dump_ctx(~0);
410 reset_rev_ctx(0);
411 reset_node_ctx(NULL);
412 init_keys();
413 return 0;
414}
415
416void svndump_deinit(void)
417{
418 log_reset();
419 fast_export_deinit();
420 reset_dump_ctx(~0);
421 reset_rev_ctx(0);
422 reset_node_ctx(NULL);
423 if (buffer_deinit(&input))
424 fprintf(stderr, "Input error\n");
425 if (ferror(stdout))
426 fprintf(stderr, "Output error\n");
427}
428
429void svndump_reset(void)
430{
431 log_reset();
432 fast_export_reset();
433 buffer_reset(&input);
434 reset_dump_ctx(~0);
435 reset_rev_ctx(0);
436 reset_node_ctx(NULL);
437}