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