Merge branch 'rs/archive-excluded-directory' into maint
[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 "fast_export.h"
12 #include "line_buffer.h"
13 #include "strbuf.h"
14 #include "svndump.h"
15
16 /*
17  * Compare start of string to literal of equal length;
18  * must be guarded by length test.
19  */
20 #define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
21
22 #define REPORT_FILENO 3
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 /* States: */
31 #define DUMP_CTX 0      /* dump metadata */
32 #define REV_CTX  1      /* revision metadata */
33 #define NODE_CTX 2      /* node metadata */
34 #define INTERNODE_CTX 3 /* between nodes */
35
36 #define DATE_RFC2822_LEN 31
37
38 static struct line_buffer input = LINE_BUFFER_INIT;
39
40 static struct {
41         uint32_t action, srcRev, type;
42         off_t prop_length, text_length;
43         struct strbuf src, dst;
44         uint32_t text_delta, prop_delta;
45 } node_ctx;
46
47 static struct {
48         uint32_t revision;
49         timestamp_t timestamp;
50         struct strbuf log, author, note;
51 } rev_ctx;
52
53 static struct {
54         uint32_t version;
55         struct strbuf uuid, url;
56 } dump_ctx;
57
58 static void reset_node_ctx(char *fname)
59 {
60         node_ctx.type = 0;
61         node_ctx.action = NODEACT_UNKNOWN;
62         node_ctx.prop_length = -1;
63         node_ctx.text_length = -1;
64         strbuf_reset(&node_ctx.src);
65         node_ctx.srcRev = 0;
66         strbuf_reset(&node_ctx.dst);
67         if (fname)
68                 strbuf_addstr(&node_ctx.dst, fname);
69         node_ctx.text_delta = 0;
70         node_ctx.prop_delta = 0;
71 }
72
73 static void reset_rev_ctx(uint32_t revision)
74 {
75         rev_ctx.revision = revision;
76         rev_ctx.timestamp = 0;
77         strbuf_reset(&rev_ctx.log);
78         strbuf_reset(&rev_ctx.author);
79         strbuf_reset(&rev_ctx.note);
80 }
81
82 static void reset_dump_ctx(const char *url)
83 {
84         strbuf_reset(&dump_ctx.url);
85         if (url)
86                 strbuf_addstr(&dump_ctx.url, url);
87         dump_ctx.version = 1;
88         strbuf_reset(&dump_ctx.uuid);
89 }
90
91 static void handle_property(const struct strbuf *key_buf,
92                                 struct strbuf *val,
93                                 uint32_t *type_set)
94 {
95         const char *key = key_buf->buf;
96         size_t keylen = key_buf->len;
97
98         switch (keylen + 1) {
99         case sizeof("svn:log"):
100                 if (constcmp(key, "svn:log"))
101                         break;
102                 if (!val)
103                         die("invalid dump: unsets svn:log");
104                 strbuf_swap(&rev_ctx.log, val);
105                 break;
106         case sizeof("svn:author"):
107                 if (constcmp(key, "svn:author"))
108                         break;
109                 if (!val)
110                         strbuf_reset(&rev_ctx.author);
111                 else
112                         strbuf_swap(&rev_ctx.author, val);
113                 break;
114         case sizeof("svn:date"):
115                 if (constcmp(key, "svn:date"))
116                         break;
117                 if (!val)
118                         die("invalid dump: unsets svn:date");
119                 if (parse_date_basic(val->buf, &rev_ctx.timestamp, NULL))
120                         warning("invalid timestamp: %s", val->buf);
121                 break;
122         case sizeof("svn:executable"):
123         case sizeof("svn:special"):
124                 if (keylen == strlen("svn:executable") &&
125                     constcmp(key, "svn:executable"))
126                         break;
127                 if (keylen == strlen("svn:special") &&
128                     constcmp(key, "svn:special"))
129                         break;
130                 if (*type_set) {
131                         if (!val)
132                                 return;
133                         die("invalid dump: sets type twice");
134                 }
135                 if (!val) {
136                         node_ctx.type = S_IFREG | 0644;
137                         return;
138                 }
139                 *type_set = 1;
140                 node_ctx.type = keylen == strlen("svn:executable") ?
141                                 (S_IFREG | 0755) :
142                                 S_IFLNK;
143         }
144 }
145
146 static void die_short_read(void)
147 {
148         if (buffer_ferror(&input))
149                 die_errno("error reading dump file");
150         die("invalid dump: unexpected end of file");
151 }
152
153 static void read_props(void)
154 {
155         static struct strbuf key = STRBUF_INIT;
156         static struct strbuf val = STRBUF_INIT;
157         const char *t;
158         /*
159          * NEEDSWORK: to support simple mode changes like
160          *      K 11
161          *      svn:special
162          *      V 1
163          *      *
164          *      D 14
165          *      svn:executable
166          * we keep track of whether a mode has been set and reset to
167          * plain file only if not.  We should be keeping track of the
168          * symlink and executable bits separately instead.
169          */
170         uint32_t type_set = 0;
171         while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
172                 uint32_t len;
173                 const char type = t[0];
174                 int ch;
175
176                 if (!type || t[1] != ' ')
177                         die("invalid property line: %s", t);
178                 len = atoi(&t[2]);
179                 strbuf_reset(&val);
180                 buffer_read_binary(&input, &val, len);
181                 if (val.len < len)
182                         die_short_read();
183
184                 /* Discard trailing newline. */
185                 ch = buffer_read_char(&input);
186                 if (ch == EOF)
187                         die_short_read();
188                 if (ch != '\n')
189                         die("invalid dump: expected newline after %s", val.buf);
190
191                 switch (type) {
192                 case 'K':
193                         strbuf_swap(&key, &val);
194                         continue;
195                 case 'D':
196                         handle_property(&val, NULL, &type_set);
197                         continue;
198                 case 'V':
199                         handle_property(&key, &val, &type_set);
200                         strbuf_reset(&key);
201                         continue;
202                 default:
203                         die("invalid property line: %s", t);
204                 }
205         }
206 }
207
208 static void handle_node(void)
209 {
210         const uint32_t type = node_ctx.type;
211         const int have_props = node_ctx.prop_length != -1;
212         const int have_text = node_ctx.text_length != -1;
213         /*
214          * Old text for this node:
215          *  NULL        - directory or bug
216          *  empty_blob  - empty
217          *  "<dataref>" - data retrievable from fast-import
218          */
219         static const char *const empty_blob = "::empty::";
220         const char *old_data = NULL;
221         uint32_t old_mode = S_IFREG | 0644;
222
223         if (node_ctx.action == NODEACT_DELETE) {
224                 if (have_text || have_props || node_ctx.srcRev)
225                         die("invalid dump: deletion node has "
226                                 "copyfrom info, text, or properties");
227                 fast_export_delete(node_ctx.dst.buf);
228                 return;
229         }
230         if (node_ctx.action == NODEACT_REPLACE) {
231                 fast_export_delete(node_ctx.dst.buf);
232                 node_ctx.action = NODEACT_ADD;
233         }
234         if (node_ctx.srcRev) {
235                 fast_export_copy(node_ctx.srcRev, node_ctx.src.buf, node_ctx.dst.buf);
236                 if (node_ctx.action == NODEACT_ADD)
237                         node_ctx.action = NODEACT_CHANGE;
238         }
239         if (have_text && type == S_IFDIR)
240                 die("invalid dump: directories cannot have text attached");
241
242         /*
243          * Find old content (old_data) and decide on the new mode.
244          */
245         if (node_ctx.action == NODEACT_CHANGE && !*node_ctx.dst.buf) {
246                 if (type != S_IFDIR)
247                         die("invalid dump: root of tree is not a regular file");
248                 old_data = NULL;
249         } else if (node_ctx.action == NODEACT_CHANGE) {
250                 uint32_t mode;
251                 old_data = fast_export_read_path(node_ctx.dst.buf, &mode);
252                 if (mode == S_IFDIR && type != S_IFDIR)
253                         die("invalid dump: cannot modify a directory into a file");
254                 if (mode != S_IFDIR && type == S_IFDIR)
255                         die("invalid dump: cannot modify a file into a directory");
256                 node_ctx.type = mode;
257                 old_mode = mode;
258         } else if (node_ctx.action == NODEACT_ADD) {
259                 if (type == S_IFDIR)
260                         old_data = NULL;
261                 else if (have_text)
262                         old_data = empty_blob;
263                 else
264                         die("invalid dump: adds node without text");
265         } else {
266                 die("invalid dump: Node-path block lacks Node-action");
267         }
268
269         /*
270          * Adjust mode to reflect properties.
271          */
272         if (have_props) {
273                 if (!node_ctx.prop_delta)
274                         node_ctx.type = type;
275                 if (node_ctx.prop_length)
276                         read_props();
277         }
278
279         /*
280          * Save the result.
281          */
282         if (type == S_IFDIR)    /* directories are not tracked. */
283                 return;
284         assert(old_data);
285         if (old_data == empty_blob)
286                 /* For the fast_export_* functions, NULL means empty. */
287                 old_data = NULL;
288         if (!have_text) {
289                 fast_export_modify(node_ctx.dst.buf, node_ctx.type, old_data);
290                 return;
291         }
292         if (!node_ctx.text_delta) {
293                 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
294                 fast_export_data(node_ctx.type, node_ctx.text_length, &input);
295                 return;
296         }
297         fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
298         fast_export_blob_delta(node_ctx.type, old_mode, old_data,
299                                 node_ctx.text_length, &input);
300 }
301
302 static void begin_revision(const char *remote_ref)
303 {
304         if (!rev_ctx.revision)  /* revision 0 gets no git commit. */
305                 return;
306         fast_export_begin_commit(rev_ctx.revision, rev_ctx.author.buf,
307                 &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
308                 rev_ctx.timestamp, remote_ref);
309 }
310
311 static void end_revision(const char *note_ref)
312 {
313         struct strbuf mark = STRBUF_INIT;
314         if (rev_ctx.revision) {
315                 fast_export_end_commit(rev_ctx.revision);
316                 fast_export_begin_note(rev_ctx.revision, "remote-svn",
317                                 "Note created by remote-svn.", rev_ctx.timestamp, note_ref);
318                 strbuf_addf(&mark, ":%"PRIu32, rev_ctx.revision);
319                 fast_export_note(mark.buf, "inline");
320                 fast_export_buf_to_data(&rev_ctx.note);
321         }
322 }
323
324 void svndump_read(const char *url, const char *local_ref, const char *notes_ref)
325 {
326         char *val;
327         char *t;
328         uint32_t active_ctx = DUMP_CTX;
329         uint32_t len;
330
331         reset_dump_ctx(url);
332         while ((t = buffer_read_line(&input))) {
333                 val = strchr(t, ':');
334                 if (!val)
335                         continue;
336                 val++;
337                 if (*val != ' ')
338                         continue;
339                 val++;
340
341                 /* strlen(key) + 1 */
342                 switch (val - t - 1) {
343                 case sizeof("SVN-fs-dump-format-version"):
344                         if (constcmp(t, "SVN-fs-dump-format-version"))
345                                 continue;
346                         dump_ctx.version = atoi(val);
347                         if (dump_ctx.version > 3)
348                                 die("expected svn dump format version <= 3, found %"PRIu32,
349                                     dump_ctx.version);
350                         break;
351                 case sizeof("UUID"):
352                         if (constcmp(t, "UUID"))
353                                 continue;
354                         strbuf_reset(&dump_ctx.uuid);
355                         strbuf_addstr(&dump_ctx.uuid, val);
356                         break;
357                 case sizeof("Revision-number"):
358                         if (constcmp(t, "Revision-number"))
359                                 continue;
360                         if (active_ctx == NODE_CTX)
361                                 handle_node();
362                         if (active_ctx == REV_CTX)
363                                 begin_revision(local_ref);
364                         if (active_ctx != DUMP_CTX)
365                                 end_revision(notes_ref);
366                         active_ctx = REV_CTX;
367                         reset_rev_ctx(atoi(val));
368                         strbuf_addf(&rev_ctx.note, "%s\n", t);
369                         break;
370                 case sizeof("Node-path"):
371                         if (constcmp(t, "Node-"))
372                                 continue;
373                         if (!constcmp(t + strlen("Node-"), "path")) {
374                                 if (active_ctx == NODE_CTX)
375                                         handle_node();
376                                 if (active_ctx == REV_CTX)
377                                         begin_revision(local_ref);
378                                 active_ctx = NODE_CTX;
379                                 reset_node_ctx(val);
380                                 strbuf_addf(&rev_ctx.note, "%s\n", t);
381                                 break;
382                         }
383                         if (constcmp(t + strlen("Node-"), "kind"))
384                                 continue;
385                         strbuf_addf(&rev_ctx.note, "%s\n", t);
386                         if (!strcmp(val, "dir"))
387                                 node_ctx.type = S_IFDIR;
388                         else if (!strcmp(val, "file"))
389                                 node_ctx.type = S_IFREG | 0644;
390                         else
391                                 fprintf(stderr, "Unknown node-kind: %s\n", val);
392                         break;
393                 case sizeof("Node-action"):
394                         if (constcmp(t, "Node-action"))
395                                 continue;
396                         strbuf_addf(&rev_ctx.note, "%s\n", t);
397                         if (!strcmp(val, "delete")) {
398                                 node_ctx.action = NODEACT_DELETE;
399                         } else if (!strcmp(val, "add")) {
400                                 node_ctx.action = NODEACT_ADD;
401                         } else if (!strcmp(val, "change")) {
402                                 node_ctx.action = NODEACT_CHANGE;
403                         } else if (!strcmp(val, "replace")) {
404                                 node_ctx.action = NODEACT_REPLACE;
405                         } else {
406                                 fprintf(stderr, "Unknown node-action: %s\n", val);
407                                 node_ctx.action = NODEACT_UNKNOWN;
408                         }
409                         break;
410                 case sizeof("Node-copyfrom-path"):
411                         if (constcmp(t, "Node-copyfrom-path"))
412                                 continue;
413                         strbuf_reset(&node_ctx.src);
414                         strbuf_addstr(&node_ctx.src, val);
415                         strbuf_addf(&rev_ctx.note, "%s\n", t);
416                         break;
417                 case sizeof("Node-copyfrom-rev"):
418                         if (constcmp(t, "Node-copyfrom-rev"))
419                                 continue;
420                         node_ctx.srcRev = atoi(val);
421                         strbuf_addf(&rev_ctx.note, "%s\n", t);
422                         break;
423                 case sizeof("Text-content-length"):
424                         if (constcmp(t, "Text") && constcmp(t, "Prop"))
425                                 continue;
426                         if (constcmp(t + 4, "-content-length"))
427                                 continue;
428                         {
429                                 char *end;
430                                 uintmax_t len;
431
432                                 len = strtoumax(val, &end, 10);
433                                 if (!isdigit(*val) || *end)
434                                         die("invalid dump: non-numeric length %s", val);
435                                 if (len > maximum_signed_value_of_type(off_t))
436                                         die("unrepresentable length in dump: %s", val);
437
438                                 if (*t == 'T')
439                                         node_ctx.text_length = (off_t) len;
440                                 else
441                                         node_ctx.prop_length = (off_t) len;
442                                 break;
443                         }
444                 case sizeof("Text-delta"):
445                         if (!constcmp(t, "Text-delta")) {
446                                 node_ctx.text_delta = !strcmp(val, "true");
447                                 break;
448                         }
449                         if (constcmp(t, "Prop-delta"))
450                                 continue;
451                         node_ctx.prop_delta = !strcmp(val, "true");
452                         break;
453                 case sizeof("Content-length"):
454                         if (constcmp(t, "Content-length"))
455                                 continue;
456                         len = atoi(val);
457                         t = buffer_read_line(&input);
458                         if (!t)
459                                 die_short_read();
460                         if (*t)
461                                 die("invalid dump: expected blank line after content length header");
462                         if (active_ctx == REV_CTX) {
463                                 read_props();
464                         } else if (active_ctx == NODE_CTX) {
465                                 handle_node();
466                                 active_ctx = INTERNODE_CTX;
467                         } else {
468                                 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
469                                 if (buffer_skip_bytes(&input, len) != len)
470                                         die_short_read();
471                         }
472                 }
473         }
474         if (buffer_ferror(&input))
475                 die_short_read();
476         if (active_ctx == NODE_CTX)
477                 handle_node();
478         if (active_ctx == REV_CTX)
479                 begin_revision(local_ref);
480         if (active_ctx != DUMP_CTX)
481                 end_revision(notes_ref);
482 }
483
484 static void init(int report_fd)
485 {
486         fast_export_init(report_fd);
487         strbuf_init(&dump_ctx.uuid, 4096);
488         strbuf_init(&dump_ctx.url, 4096);
489         strbuf_init(&rev_ctx.log, 4096);
490         strbuf_init(&rev_ctx.author, 4096);
491         strbuf_init(&rev_ctx.note, 4096);
492         strbuf_init(&node_ctx.src, 4096);
493         strbuf_init(&node_ctx.dst, 4096);
494         reset_dump_ctx(NULL);
495         reset_rev_ctx(0);
496         reset_node_ctx(NULL);
497         return;
498 }
499
500 int svndump_init(const char *filename)
501 {
502         if (buffer_init(&input, filename))
503                 return error_errno("cannot open %s", filename ? filename : "NULL");
504         init(REPORT_FILENO);
505         return 0;
506 }
507
508 int svndump_init_fd(int in_fd, int back_fd)
509 {
510         if(buffer_fdinit(&input, xdup(in_fd)))
511                 return error_errno("cannot open fd %d", in_fd);
512         init(xdup(back_fd));
513         return 0;
514 }
515
516 void svndump_deinit(void)
517 {
518         fast_export_deinit();
519         reset_dump_ctx(NULL);
520         reset_rev_ctx(0);
521         reset_node_ctx(NULL);
522         strbuf_release(&rev_ctx.log);
523         strbuf_release(&rev_ctx.author);
524         strbuf_release(&rev_ctx.note);
525         strbuf_release(&node_ctx.src);
526         strbuf_release(&node_ctx.dst);
527         if (buffer_deinit(&input))
528                 fprintf(stderr, "Input error\n");
529         if (ferror(stdout))
530                 fprintf(stderr, "Output error\n");
531 }
532
533 void svndump_reset(void)
534 {
535         strbuf_release(&dump_ctx.uuid);
536         strbuf_release(&dump_ctx.url);
537         strbuf_release(&rev_ctx.log);
538         strbuf_release(&rev_ctx.author);
539 }