Code

vcs-svn: implement perfect hash for node-prop keys
[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 /*
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)
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
29 #define DUMP_CTX 0
30 #define REV_CTX  1
31 #define NODE_CTX 2
33 #define LENGTH_UNKNOWN (~0)
34 #define DATE_RFC2822_LEN 31
36 /* Create memory pool for log messages */
37 obj_pool_gen(log, char, 4096)
39 static struct line_buffer input = LINE_BUFFER_INIT;
41 static 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 }
50 static 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;
56 static struct {
57         uint32_t revision, author;
58         unsigned long timestamp;
59         char *log;
60 } rev_ctx;
62 static struct {
63         uint32_t version, uuid, url;
64 } dump_ctx;
66 static struct {
67         uint32_t uuid, revision_number, node_path, node_kind, node_action,
68                 node_copyfrom_path, node_copyfrom_rev, text_content_length,
69                 prop_content_length, content_length, svn_fs_dump_format_version,
70                 /* version 3 format */
71                 text_delta, prop_delta;
72 } keys;
74 static void reset_node_ctx(char *fname)
75 {
76         node_ctx.type = 0;
77         node_ctx.action = NODEACT_UNKNOWN;
78         node_ctx.propLength = LENGTH_UNKNOWN;
79         node_ctx.textLength = LENGTH_UNKNOWN;
80         node_ctx.src[0] = ~0;
81         node_ctx.srcRev = 0;
82         pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
83         node_ctx.text_delta = 0;
84         node_ctx.prop_delta = 0;
85 }
87 static void reset_rev_ctx(uint32_t revision)
88 {
89         rev_ctx.revision = revision;
90         rev_ctx.timestamp = 0;
91         rev_ctx.log = NULL;
92         rev_ctx.author = ~0;
93 }
95 static void reset_dump_ctx(uint32_t url)
96 {
97         dump_ctx.url = url;
98         dump_ctx.version = 1;
99         dump_ctx.uuid = ~0;
102 static void init_keys(void)
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(const struct strbuf *key_buf,
121                                 const char *val, uint32_t len,
122                                 uint32_t *type_set)
124         const char *key = key_buf->buf;
125         size_t keylen = key_buf->len;
127         switch (keylen + 1) {
128         case sizeof("svn:log"):
129                 if (constcmp(key, "svn:log"))
130                         break;
131                 if (!val)
132                         die("invalid dump: unsets svn:log");
133                 /* Value length excludes terminating nul. */
134                 rev_ctx.log = log_copy(len + 1, val);
135                 break;
136         case sizeof("svn:author"):
137                 if (constcmp(key, "svn:author"))
138                         break;
139                 rev_ctx.author = pool_intern(val);
140                 break;
141         case sizeof("svn:date"):
142                 if (constcmp(key, "svn:date"))
143                         break;
144                 if (!val)
145                         die("invalid dump: unsets svn:date");
146                 if (parse_date_basic(val, &rev_ctx.timestamp, NULL))
147                         warning("invalid timestamp: %s", val);
148                 break;
149         case sizeof("svn:executable"):
150         case sizeof("svn:special"):
151                 if (keylen == strlen("svn:executable") &&
152                     constcmp(key, "svn:executable"))
153                         break;
154                 if (keylen == strlen("svn:special") &&
155                     constcmp(key, "svn:special"))
156                         break;
157                 if (*type_set) {
158                         if (!val)
159                                 return;
160                         die("invalid dump: sets type twice");
161                 }
162                 if (!val) {
163                         node_ctx.type = REPO_MODE_BLB;
164                         return;
165                 }
166                 *type_set = 1;
167                 node_ctx.type = keylen == strlen("svn:executable") ?
168                                 REPO_MODE_EXE :
169                                 REPO_MODE_LNK;
170         }
173 static void die_short_read(void)
175         if (buffer_ferror(&input))
176                 die_errno("error reading dump file");
177         die("invalid dump: unexpected end of file");
180 static void read_props(void)
182         static struct strbuf key = STRBUF_INIT;
183         const char *t;
184         /*
185          * NEEDSWORK: to support simple mode changes like
186          *      K 11
187          *      svn:special
188          *      V 1
189          *      *
190          *      D 14
191          *      svn:executable
192          * we keep track of whether a mode has been set and reset to
193          * plain file only if not.  We should be keeping track of the
194          * symlink and executable bits separately instead.
195          */
196         uint32_t type_set = 0;
197         while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
198                 uint32_t len;
199                 const char *val;
200                 const char type = t[0];
201                 int ch;
203                 if (!type || t[1] != ' ')
204                         die("invalid property line: %s\n", t);
205                 len = atoi(&t[2]);
206                 val = buffer_read_string(&input, len);
207                 if (!val || strlen(val) != len)
208                         die_short_read();
210                 /* Discard trailing newline. */
211                 ch = buffer_read_char(&input);
212                 if (ch == EOF)
213                         die_short_read();
214                 if (ch != '\n')
215                         die("invalid dump: expected newline after %s", val);
217                 switch (type) {
218                 case 'K':
219                 case 'D':
220                         strbuf_reset(&key);
221                         if (val)
222                                 strbuf_add(&key, val, len);
223                         if (type == 'K')
224                                 continue;
225                         assert(type == 'D');
226                         val = NULL;
227                         len = 0;
228                         /* fall through */
229                 case 'V':
230                         handle_property(&key, val, len, &type_set);
231                         strbuf_reset(&key);
232                         continue;
233                 default:
234                         die("invalid property line: %s\n", t);
235                 }
236         }
239 static void handle_node(void)
241         uint32_t mark = 0;
242         const uint32_t type = node_ctx.type;
243         const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
244         const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
246         if (node_ctx.text_delta)
247                 die("text deltas not supported");
248         if (have_text)
249                 mark = next_blob_mark();
250         if (node_ctx.action == NODEACT_DELETE) {
251                 if (have_text || have_props || node_ctx.srcRev)
252                         die("invalid dump: deletion node has "
253                                 "copyfrom info, text, or properties");
254                 return repo_delete(node_ctx.dst);
255         }
256         if (node_ctx.action == NODEACT_REPLACE) {
257                 repo_delete(node_ctx.dst);
258                 node_ctx.action = NODEACT_ADD;
259         }
260         if (node_ctx.srcRev) {
261                 repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
262                 if (node_ctx.action == NODEACT_ADD)
263                         node_ctx.action = NODEACT_CHANGE;
264         }
265         if (have_text && type == REPO_MODE_DIR)
266                 die("invalid dump: directories cannot have text attached");
268         /*
269          * Decide on the new content (mark) and mode (node_ctx.type).
270          */
271         if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
272                 if (type != REPO_MODE_DIR)
273                         die("invalid dump: root of tree is not a regular file");
274         } else if (node_ctx.action == NODEACT_CHANGE) {
275                 uint32_t mode;
276                 if (!have_text)
277                         mark = repo_read_path(node_ctx.dst);
278                 mode = repo_read_mode(node_ctx.dst);
279                 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
280                         die("invalid dump: cannot modify a directory into a file");
281                 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
282                         die("invalid dump: cannot modify a file into a directory");
283                 node_ctx.type = mode;
284         } else if (node_ctx.action == NODEACT_ADD) {
285                 if (!have_text && type != REPO_MODE_DIR)
286                         die("invalid dump: adds node without text");
287         } else {
288                 die("invalid dump: Node-path block lacks Node-action");
289         }
291         /*
292          * Adjust mode to reflect properties.
293          */
294         if (have_props) {
295                 if (!node_ctx.prop_delta)
296                         node_ctx.type = type;
297                 if (node_ctx.propLength)
298                         read_props();
299         }
301         /*
302          * Save the result.
303          */
304         repo_add(node_ctx.dst, node_ctx.type, mark);
305         if (have_text)
306                 fast_export_blob(node_ctx.type, mark,
307                                  node_ctx.textLength, &input);
310 static void handle_revision(void)
312         if (rev_ctx.revision)
313                 repo_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log,
314                         dump_ctx.uuid, dump_ctx.url, rev_ctx.timestamp);
317 void svndump_read(const char *url)
319         char *val;
320         char *t;
321         uint32_t active_ctx = DUMP_CTX;
322         uint32_t len;
323         uint32_t key;
325         reset_dump_ctx(pool_intern(url));
326         while ((t = buffer_read_line(&input))) {
327                 val = strstr(t, ": ");
328                 if (!val)
329                         continue;
330                 *val++ = '\0';
331                 *val++ = '\0';
332                 key = pool_intern(t);
334                 if (key == keys.svn_fs_dump_format_version) {
335                         dump_ctx.version = atoi(val);
336                         if (dump_ctx.version > 3)
337                                 die("expected svn dump format version <= 3, found %"PRIu32,
338                                     dump_ctx.version);
339                 } else if (key == keys.uuid) {
340                         dump_ctx.uuid = pool_intern(val);
341                 } else if (key == keys.revision_number) {
342                         if (active_ctx == NODE_CTX)
343                                 handle_node();
344                         if (active_ctx != DUMP_CTX)
345                                 handle_revision();
346                         active_ctx = REV_CTX;
347                         reset_rev_ctx(atoi(val));
348                 } else if (key == keys.node_path) {
349                         if (active_ctx == NODE_CTX)
350                                 handle_node();
351                         active_ctx = NODE_CTX;
352                         reset_node_ctx(val);
353                 } else if (key == keys.node_kind) {
354                         if (!strcmp(val, "dir"))
355                                 node_ctx.type = REPO_MODE_DIR;
356                         else if (!strcmp(val, "file"))
357                                 node_ctx.type = REPO_MODE_BLB;
358                         else
359                                 fprintf(stderr, "Unknown node-kind: %s\n", val);
360                 } else if (key == keys.node_action) {
361                         if (!strcmp(val, "delete")) {
362                                 node_ctx.action = NODEACT_DELETE;
363                         } else if (!strcmp(val, "add")) {
364                                 node_ctx.action = NODEACT_ADD;
365                         } else if (!strcmp(val, "change")) {
366                                 node_ctx.action = NODEACT_CHANGE;
367                         } else if (!strcmp(val, "replace")) {
368                                 node_ctx.action = NODEACT_REPLACE;
369                         } else {
370                                 fprintf(stderr, "Unknown node-action: %s\n", val);
371                                 node_ctx.action = NODEACT_UNKNOWN;
372                         }
373                 } else if (key == keys.node_copyfrom_path) {
374                         pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
375                 } else if (key == keys.node_copyfrom_rev) {
376                         node_ctx.srcRev = atoi(val);
377                 } else if (key == keys.text_content_length) {
378                         node_ctx.textLength = atoi(val);
379                 } else if (key == keys.prop_content_length) {
380                         node_ctx.propLength = atoi(val);
381                 } else if (key == keys.text_delta) {
382                         node_ctx.text_delta = !strcmp(val, "true");
383                 } else if (key == keys.prop_delta) {
384                         node_ctx.prop_delta = !strcmp(val, "true");
385                 } else if (key == keys.content_length) {
386                         len = atoi(val);
387                         t = buffer_read_line(&input);
388                         if (!t)
389                                 die_short_read();
390                         if (*t)
391                                 die("invalid dump: expected blank line after content length header");
392                         if (active_ctx == REV_CTX) {
393                                 read_props();
394                         } else if (active_ctx == NODE_CTX) {
395                                 handle_node();
396                                 active_ctx = REV_CTX;
397                         } else {
398                                 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
399                                 if (buffer_skip_bytes(&input, len) != len)
400                                         die_short_read();
401                         }
402                 }
403         }
404         if (buffer_ferror(&input))
405                 die_short_read();
406         if (active_ctx == NODE_CTX)
407                 handle_node();
408         if (active_ctx != DUMP_CTX)
409                 handle_revision();
412 int svndump_init(const char *filename)
414         if (buffer_init(&input, filename))
415                 return error("cannot open %s: %s", filename, strerror(errno));
416         repo_init();
417         reset_dump_ctx(~0);
418         reset_rev_ctx(0);
419         reset_node_ctx(NULL);
420         init_keys();
421         return 0;
424 void svndump_deinit(void)
426         log_reset();
427         repo_reset();
428         reset_dump_ctx(~0);
429         reset_rev_ctx(0);
430         reset_node_ctx(NULL);
431         if (buffer_deinit(&input))
432                 fprintf(stderr, "Input error\n");
433         if (ferror(stdout))
434                 fprintf(stderr, "Output error\n");
437 void svndump_reset(void)
439         log_reset();
440         buffer_reset(&input);
441         repo_reset();
442         reset_dump_ctx(~0);
443         reset_rev_ctx(0);
444         reset_node_ctx(NULL);