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