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