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