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