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