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