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