Code

vcs-svn: improve reporting of input errors
[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  */
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 struct line_buffer input = LINE_BUFFER_INIT;
35 static 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 }
44 static 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;
50 static struct {
51         uint32_t revision, author;
52         unsigned long timestamp;
53         char *log;
54 } rev_ctx;
56 static struct {
57         uint32_t version, uuid, url;
58 } dump_ctx;
60 static 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;
69 static 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 }
82 static 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 }
90 static void reset_dump_ctx(uint32_t url)
91 {
92         dump_ctx.url = url;
93         dump_ctx.version = 1;
94         dump_ctx.uuid = ~0;
95 }
97 static 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");
120 static void handle_property(uint32_t key, const char *val, uint32_t len,
121                                 uint32_t *type_set)
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         }
152 static void die_short_read(void)
154         if (buffer_ferror(&input))
155                 die_errno("error reading dump file");
156         die("invalid dump: unexpected end of file");
159 static void read_props(void)
161         uint32_t key = ~0;
162         const char *t;
163         /*
164          * NEEDSWORK: to support simple mode changes like
165          *      K 11
166          *      svn:special
167          *      V 1
168          *      *
169          *      D 14
170          *      svn:executable
171          * we keep track of whether a mode has been set and reset to
172          * plain file only if not.  We should be keeping track of the
173          * symlink and executable bits separately instead.
174          */
175         uint32_t type_set = 0;
176         while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
177                 uint32_t len;
178                 const char *val;
179                 const char type = t[0];
180                 int ch;
182                 if (!type || t[1] != ' ')
183                         die("invalid property line: %s\n", t);
184                 len = atoi(&t[2]);
185                 val = buffer_read_string(&input, len);
186                 if (!val || strlen(val) != len)
187                         die_short_read();
189                 /* Discard trailing newline. */
190                 ch = buffer_read_char(&input);
191                 if (ch == EOF)
192                         die_short_read();
193                 if (ch != '\n')
194                         die("invalid dump: expected newline after %s", val);
196                 switch (type) {
197                 case 'K':
198                         key = pool_intern(val);
199                         continue;
200                 case 'D':
201                         key = pool_intern(val);
202                         val = NULL;
203                         len = 0;
204                         /* fall through */
205                 case 'V':
206                         handle_property(key, val, len, &type_set);
207                         key = ~0;
208                         continue;
209                 default:
210                         die("invalid property line: %s\n", t);
211                 }
212         }
215 static void handle_node(void)
217         uint32_t mark = 0;
218         const uint32_t type = node_ctx.type;
219         const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
220         const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
222         if (node_ctx.text_delta)
223                 die("text deltas not supported");
224         if (have_text)
225                 mark = next_blob_mark();
226         if (node_ctx.action == NODEACT_DELETE) {
227                 if (have_text || have_props || node_ctx.srcRev)
228                         die("invalid dump: deletion node has "
229                                 "copyfrom info, text, or properties");
230                 return repo_delete(node_ctx.dst);
231         }
232         if (node_ctx.action == NODEACT_REPLACE) {
233                 repo_delete(node_ctx.dst);
234                 node_ctx.action = NODEACT_ADD;
235         }
236         if (node_ctx.srcRev) {
237                 repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
238                 if (node_ctx.action == NODEACT_ADD)
239                         node_ctx.action = NODEACT_CHANGE;
240         }
241         if (have_text && type == REPO_MODE_DIR)
242                 die("invalid dump: directories cannot have text attached");
244         /*
245          * Decide on the new content (mark) and mode (node_ctx.type).
246          */
247         if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
248                 if (type != REPO_MODE_DIR)
249                         die("invalid dump: root of tree is not a regular file");
250         } else if (node_ctx.action == NODEACT_CHANGE) {
251                 uint32_t mode;
252                 if (!have_text)
253                         mark = repo_read_path(node_ctx.dst);
254                 mode = repo_read_mode(node_ctx.dst);
255                 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
256                         die("invalid dump: cannot modify a directory into a file");
257                 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
258                         die("invalid dump: cannot modify a file into a directory");
259                 node_ctx.type = mode;
260         } else if (node_ctx.action == NODEACT_ADD) {
261                 if (!have_text && type != REPO_MODE_DIR)
262                         die("invalid dump: adds node without text");
263         } else {
264                 die("invalid dump: Node-path block lacks Node-action");
265         }
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         }
277         /*
278          * Save the result.
279          */
280         repo_add(node_ctx.dst, node_ctx.type, mark);
281         if (have_text)
282                 fast_export_blob(node_ctx.type, mark,
283                                  node_ctx.textLength, &input);
286 static void handle_revision(void)
288         if (rev_ctx.revision)
289                 repo_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log,
290                         dump_ctx.uuid, dump_ctx.url, rev_ctx.timestamp);
293 void svndump_read(const char *url)
295         char *val;
296         char *t;
297         uint32_t active_ctx = DUMP_CTX;
298         uint32_t len;
299         uint32_t key;
301         reset_dump_ctx(pool_intern(url));
302         while ((t = buffer_read_line(&input))) {
303                 val = strstr(t, ": ");
304                 if (!val)
305                         continue;
306                 *val++ = '\0';
307                 *val++ = '\0';
308                 key = pool_intern(t);
310                 if (key == keys.svn_fs_dump_format_version) {
311                         dump_ctx.version = atoi(val);
312                         if (dump_ctx.version > 3)
313                                 die("expected svn dump format version <= 3, found %"PRIu32,
314                                     dump_ctx.version);
315                 } else if (key == keys.uuid) {
316                         dump_ctx.uuid = pool_intern(val);
317                 } else if (key == keys.revision_number) {
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));
324                 } else if (key == keys.node_path) {
325                         if (active_ctx == NODE_CTX)
326                                 handle_node();
327                         active_ctx = NODE_CTX;
328                         reset_node_ctx(val);
329                 } else if (key == keys.node_kind) {
330                         if (!strcmp(val, "dir"))
331                                 node_ctx.type = REPO_MODE_DIR;
332                         else if (!strcmp(val, "file"))
333                                 node_ctx.type = REPO_MODE_BLB;
334                         else
335                                 fprintf(stderr, "Unknown node-kind: %s\n", val);
336                 } else if (key == keys.node_action) {
337                         if (!strcmp(val, "delete")) {
338                                 node_ctx.action = NODEACT_DELETE;
339                         } else if (!strcmp(val, "add")) {
340                                 node_ctx.action = NODEACT_ADD;
341                         } else if (!strcmp(val, "change")) {
342                                 node_ctx.action = NODEACT_CHANGE;
343                         } else if (!strcmp(val, "replace")) {
344                                 node_ctx.action = NODEACT_REPLACE;
345                         } else {
346                                 fprintf(stderr, "Unknown node-action: %s\n", val);
347                                 node_ctx.action = NODEACT_UNKNOWN;
348                         }
349                 } else if (key == keys.node_copyfrom_path) {
350                         pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
351                 } else if (key == keys.node_copyfrom_rev) {
352                         node_ctx.srcRev = atoi(val);
353                 } else if (key == keys.text_content_length) {
354                         node_ctx.textLength = atoi(val);
355                 } else if (key == keys.prop_content_length) {
356                         node_ctx.propLength = atoi(val);
357                 } else if (key == keys.text_delta) {
358                         node_ctx.text_delta = !strcmp(val, "true");
359                 } else if (key == keys.prop_delta) {
360                         node_ctx.prop_delta = !strcmp(val, "true");
361                 } else if (key == keys.content_length) {
362                         len = atoi(val);
363                         t = buffer_read_line(&input);
364                         if (!t)
365                                 die_short_read();
366                         if (*t)
367                                 die("invalid dump: expected blank line after content length header");
368                         if (active_ctx == REV_CTX) {
369                                 read_props();
370                         } else if (active_ctx == NODE_CTX) {
371                                 handle_node();
372                                 active_ctx = REV_CTX;
373                         } else {
374                                 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
375                                 if (buffer_skip_bytes(&input, len) != len)
376                                         die_short_read();
377                         }
378                 }
379         }
380         if (buffer_ferror(&input))
381                 die_short_read();
382         if (active_ctx == NODE_CTX)
383                 handle_node();
384         if (active_ctx != DUMP_CTX)
385                 handle_revision();
388 int svndump_init(const char *filename)
390         if (buffer_init(&input, filename))
391                 return error("cannot open %s: %s", filename, strerror(errno));
392         repo_init();
393         reset_dump_ctx(~0);
394         reset_rev_ctx(0);
395         reset_node_ctx(NULL);
396         init_keys();
397         return 0;
400 void svndump_deinit(void)
402         log_reset();
403         repo_reset();
404         reset_dump_ctx(~0);
405         reset_rev_ctx(0);
406         reset_node_ctx(NULL);
407         if (buffer_deinit(&input))
408                 fprintf(stderr, "Input error\n");
409         if (ferror(stdout))
410                 fprintf(stderr, "Output error\n");
413 void svndump_reset(void)
415         log_reset();
416         buffer_reset(&input);
417         repo_reset();
418         reset_dump_ctx(~0);
419         reset_rev_ctx(0);
420         reset_node_ctx(NULL);