vcs-svn / svndump.con commit vcs-svn: use strbuf for author, UUID, and URL (7c5817d)
   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#define NODEACT_REPLACE 4
  18#define NODEACT_DELETE 3
  19#define NODEACT_ADD 2
  20#define NODEACT_CHANGE 1
  21#define NODEACT_UNKNOWN 0
  22
  23#define DUMP_CTX 0
  24#define REV_CTX  1
  25#define NODE_CTX 2
  26
  27#define LENGTH_UNKNOWN (~0)
  28#define DATE_RFC2822_LEN 31
  29
  30static struct line_buffer input = LINE_BUFFER_INIT;
  31
  32static struct {
  33        uint32_t action, propLength, textLength, srcRev, type;
  34        uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
  35        uint32_t text_delta, prop_delta;
  36} node_ctx;
  37
  38static struct {
  39        uint32_t revision;
  40        unsigned long timestamp;
  41        struct strbuf log, author;
  42} rev_ctx;
  43
  44static struct {
  45        uint32_t version;
  46        struct strbuf uuid, url;
  47} dump_ctx;
  48
  49static struct {
  50        uint32_t svn_log, svn_author, svn_date, svn_executable, svn_special, uuid,
  51                revision_number, node_path, node_kind, node_action,
  52                node_copyfrom_path, node_copyfrom_rev, text_content_length,
  53                prop_content_length, content_length, svn_fs_dump_format_version,
  54                /* version 3 format */
  55                text_delta, prop_delta;
  56} keys;
  57
  58static void reset_node_ctx(char *fname)
  59{
  60        node_ctx.type = 0;
  61        node_ctx.action = NODEACT_UNKNOWN;
  62        node_ctx.propLength = LENGTH_UNKNOWN;
  63        node_ctx.textLength = LENGTH_UNKNOWN;
  64        node_ctx.src[0] = ~0;
  65        node_ctx.srcRev = 0;
  66        pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
  67        node_ctx.text_delta = 0;
  68        node_ctx.prop_delta = 0;
  69}
  70
  71static void reset_rev_ctx(uint32_t revision)
  72{
  73        rev_ctx.revision = revision;
  74        rev_ctx.timestamp = 0;
  75        strbuf_reset(&rev_ctx.log);
  76        strbuf_reset(&rev_ctx.author);
  77}
  78
  79static void reset_dump_ctx(const char *url)
  80{
  81        strbuf_reset(&dump_ctx.url);
  82        if (url)
  83                strbuf_addstr(&dump_ctx.url, url);
  84        dump_ctx.version = 1;
  85        strbuf_reset(&dump_ctx.uuid);
  86}
  87
  88static void init_keys(void)
  89{
  90        keys.svn_log = pool_intern("svn:log");
  91        keys.svn_author = pool_intern("svn:author");
  92        keys.svn_date = pool_intern("svn:date");
  93        keys.svn_executable = pool_intern("svn:executable");
  94        keys.svn_special = pool_intern("svn:special");
  95        keys.uuid = pool_intern("UUID");
  96        keys.revision_number = pool_intern("Revision-number");
  97        keys.node_path = pool_intern("Node-path");
  98        keys.node_kind = pool_intern("Node-kind");
  99        keys.node_action = pool_intern("Node-action");
 100        keys.node_copyfrom_path = pool_intern("Node-copyfrom-path");
 101        keys.node_copyfrom_rev = pool_intern("Node-copyfrom-rev");
 102        keys.text_content_length = pool_intern("Text-content-length");
 103        keys.prop_content_length = pool_intern("Prop-content-length");
 104        keys.content_length = pool_intern("Content-length");
 105        keys.svn_fs_dump_format_version = pool_intern("SVN-fs-dump-format-version");
 106        /* version 3 format (Subversion 1.1.0) */
 107        keys.text_delta = pool_intern("Text-delta");
 108        keys.prop_delta = pool_intern("Prop-delta");
 109}
 110
 111static void handle_property(uint32_t key, const char *val, uint32_t len,
 112                                uint32_t *type_set)
 113{
 114        if (key == keys.svn_log) {
 115                if (!val)
 116                        die("invalid dump: unsets svn:log");
 117                strbuf_reset(&rev_ctx.log);
 118                strbuf_add(&rev_ctx.log, val, len);
 119        } else if (key == keys.svn_author) {
 120                strbuf_reset(&rev_ctx.author);
 121                if (val)
 122                        strbuf_add(&rev_ctx.author, val, len);
 123        } else if (key == keys.svn_date) {
 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        } else if (key == keys.svn_executable || key == keys.svn_special) {
 129                if (*type_set) {
 130                        if (!val)
 131                                return;
 132                        die("invalid dump: sets type twice");
 133                }
 134                if (!val) {
 135                        node_ctx.type = REPO_MODE_BLB;
 136                        return;
 137                }
 138                *type_set = 1;
 139                node_ctx.type = key == keys.svn_executable ?
 140                                REPO_MODE_EXE :
 141                                REPO_MODE_LNK;
 142        }
 143}
 144
 145static void die_short_read(void)
 146{
 147        if (buffer_ferror(&input))
 148                die_errno("error reading dump file");
 149        die("invalid dump: unexpected end of file");
 150}
 151
 152static void read_props(void)
 153{
 154        uint32_t key = ~0;
 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 *val;
 172                const char type = t[0];
 173                int ch;
 174
 175                if (!type || t[1] != ' ')
 176                        die("invalid property line: %s\n", t);
 177                len = atoi(&t[2]);
 178                val = buffer_read_string(&input, len);
 179                if (!val || strlen(val) != 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);
 188
 189                switch (type) {
 190                case 'K':
 191                        key = pool_intern(val);
 192                        continue;
 193                case 'D':
 194                        key = pool_intern(val);
 195                        val = NULL;
 196                        len = 0;
 197                        /* fall through */
 198                case 'V':
 199                        handle_property(key, val, len, &type_set);
 200                        key = ~0;
 201                        continue;
 202                default:
 203                        die("invalid property line: %s\n", t);
 204                }
 205        }
 206}
 207
 208static void handle_node(void)
 209{
 210        uint32_t mark = 0;
 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.textLength != LENGTH_UNKNOWN;
 214
 215        if (node_ctx.text_delta)
 216                die("text deltas not supported");
 217        if (have_text)
 218                mark = next_blob_mark();
 219        if (node_ctx.action == NODEACT_DELETE) {
 220                if (have_text || have_props || node_ctx.srcRev)
 221                        die("invalid dump: deletion node has "
 222                                "copyfrom info, text, or properties");
 223                return repo_delete(node_ctx.dst);
 224        }
 225        if (node_ctx.action == NODEACT_REPLACE) {
 226                repo_delete(node_ctx.dst);
 227                node_ctx.action = NODEACT_ADD;
 228        }
 229        if (node_ctx.srcRev) {
 230                repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
 231                if (node_ctx.action == NODEACT_ADD)
 232                        node_ctx.action = NODEACT_CHANGE;
 233        }
 234        if (have_text && type == REPO_MODE_DIR)
 235                die("invalid dump: directories cannot have text attached");
 236
 237        /*
 238         * Decide on the new content (mark) and mode (node_ctx.type).
 239         */
 240        if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
 241                if (type != REPO_MODE_DIR)
 242                        die("invalid dump: root of tree is not a regular file");
 243        } else if (node_ctx.action == NODEACT_CHANGE) {
 244                uint32_t mode;
 245                if (!have_text)
 246                        mark = repo_read_path(node_ctx.dst);
 247                mode = repo_read_mode(node_ctx.dst);
 248                if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
 249                        die("invalid dump: cannot modify a directory into a file");
 250                if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
 251                        die("invalid dump: cannot modify a file into a directory");
 252                node_ctx.type = mode;
 253        } else if (node_ctx.action == NODEACT_ADD) {
 254                if (!have_text && type != REPO_MODE_DIR)
 255                        die("invalid dump: adds node without text");
 256        } else {
 257                die("invalid dump: Node-path block lacks Node-action");
 258        }
 259
 260        /*
 261         * Adjust mode to reflect properties.
 262         */
 263        if (have_props) {
 264                if (!node_ctx.prop_delta)
 265                        node_ctx.type = type;
 266                if (node_ctx.propLength)
 267                        read_props();
 268        }
 269
 270        /*
 271         * Save the result.
 272         */
 273        repo_add(node_ctx.dst, node_ctx.type, mark);
 274        if (have_text)
 275                fast_export_blob(node_ctx.type, mark,
 276                                 node_ctx.textLength, &input);
 277}
 278
 279static void handle_revision(void)
 280{
 281        if (rev_ctx.revision)
 282                repo_commit(rev_ctx.revision, rev_ctx.author.buf,
 283                        rev_ctx.log.buf, dump_ctx.uuid.buf, dump_ctx.url.buf,
 284                        rev_ctx.timestamp);
 285}
 286
 287void svndump_read(const char *url)
 288{
 289        char *val;
 290        char *t;
 291        uint32_t active_ctx = DUMP_CTX;
 292        uint32_t len;
 293        uint32_t key;
 294
 295        reset_dump_ctx(url);
 296        while ((t = buffer_read_line(&input))) {
 297                val = strstr(t, ": ");
 298                if (!val)
 299                        continue;
 300                *val++ = '\0';
 301                *val++ = '\0';
 302                key = pool_intern(t);
 303
 304                if (key == keys.svn_fs_dump_format_version) {
 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                } else if (key == keys.uuid) {
 310                        strbuf_reset(&dump_ctx.uuid);
 311                        strbuf_addstr(&dump_ctx.uuid, val);
 312                } else if (key == keys.revision_number) {
 313                        if (active_ctx == NODE_CTX)
 314                                handle_node();
 315                        if (active_ctx != DUMP_CTX)
 316                                handle_revision();
 317                        active_ctx = REV_CTX;
 318                        reset_rev_ctx(atoi(val));
 319                } else if (key == keys.node_path) {
 320                        if (active_ctx == NODE_CTX)
 321                                handle_node();
 322                        active_ctx = NODE_CTX;
 323                        reset_node_ctx(val);
 324                } else if (key == keys.node_kind) {
 325                        if (!strcmp(val, "dir"))
 326                                node_ctx.type = REPO_MODE_DIR;
 327                        else if (!strcmp(val, "file"))
 328                                node_ctx.type = REPO_MODE_BLB;
 329                        else
 330                                fprintf(stderr, "Unknown node-kind: %s\n", val);
 331                } else if (key == keys.node_action) {
 332                        if (!strcmp(val, "delete")) {
 333                                node_ctx.action = NODEACT_DELETE;
 334                        } else if (!strcmp(val, "add")) {
 335                                node_ctx.action = NODEACT_ADD;
 336                        } else if (!strcmp(val, "change")) {
 337                                node_ctx.action = NODEACT_CHANGE;
 338                        } else if (!strcmp(val, "replace")) {
 339                                node_ctx.action = NODEACT_REPLACE;
 340                        } else {
 341                                fprintf(stderr, "Unknown node-action: %s\n", val);
 342                                node_ctx.action = NODEACT_UNKNOWN;
 343                        }
 344                } else if (key == keys.node_copyfrom_path) {
 345                        pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
 346                } else if (key == keys.node_copyfrom_rev) {
 347                        node_ctx.srcRev = atoi(val);
 348                } else if (key == keys.text_content_length) {
 349                        node_ctx.textLength = atoi(val);
 350                } else if (key == keys.prop_content_length) {
 351                        node_ctx.propLength = atoi(val);
 352                } else if (key == keys.text_delta) {
 353                        node_ctx.text_delta = !strcmp(val, "true");
 354                } else if (key == keys.prop_delta) {
 355                        node_ctx.prop_delta = !strcmp(val, "true");
 356                } else if (key == keys.content_length) {
 357                        len = atoi(val);
 358                        t = buffer_read_line(&input);
 359                        if (!t)
 360                                die_short_read();
 361                        if (*t)
 362                                die("invalid dump: expected blank line after content length header");
 363                        if (active_ctx == REV_CTX) {
 364                                read_props();
 365                        } else if (active_ctx == NODE_CTX) {
 366                                handle_node();
 367                                active_ctx = REV_CTX;
 368                        } else {
 369                                fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
 370                                if (buffer_skip_bytes(&input, len) != len)
 371                                        die_short_read();
 372                        }
 373                }
 374        }
 375        if (buffer_ferror(&input))
 376                die_short_read();
 377        if (active_ctx == NODE_CTX)
 378                handle_node();
 379        if (active_ctx != DUMP_CTX)
 380                handle_revision();
 381}
 382
 383int svndump_init(const char *filename)
 384{
 385        if (buffer_init(&input, filename))
 386                return error("cannot open %s: %s", filename, strerror(errno));
 387        repo_init();
 388        strbuf_init(&dump_ctx.uuid, 4096);
 389        strbuf_init(&dump_ctx.url, 4096);
 390        strbuf_init(&rev_ctx.log, 4096);
 391        strbuf_init(&rev_ctx.author, 4096);
 392        reset_dump_ctx(NULL);
 393        reset_rev_ctx(0);
 394        reset_node_ctx(NULL);
 395        init_keys();
 396        return 0;
 397}
 398
 399void svndump_deinit(void)
 400{
 401        repo_reset();
 402        reset_dump_ctx(NULL);
 403        reset_rev_ctx(0);
 404        reset_node_ctx(NULL);
 405        strbuf_release(&rev_ctx.log);
 406        if (buffer_deinit(&input))
 407                fprintf(stderr, "Input error\n");
 408        if (ferror(stdout))
 409                fprintf(stderr, "Output error\n");
 410}
 411
 412void svndump_reset(void)
 413{
 414        buffer_reset(&input);
 415        repo_reset();
 416        strbuf_release(&dump_ctx.uuid);
 417        strbuf_release(&dump_ctx.url);
 418        strbuf_release(&rev_ctx.log);
 419        strbuf_release(&rev_ctx.author);
 420}