0af8ac6807c289599237ccc73a0eeed9fad5277a
   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, 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 read_props(void)
 119{
 120        uint32_t len;
 121        uint32_t key = ~0;
 122        char *val = NULL;
 123        char *t;
 124        while ((t = buffer_read_line()) && strcmp(t, "PROPS-END")) {
 125                if (!strncmp(t, "K ", 2)) {
 126                        len = atoi(&t[2]);
 127                        key = pool_intern(buffer_read_string(len));
 128                        buffer_read_line();
 129                } else if (!strncmp(t, "V ", 2)) {
 130                        len = atoi(&t[2]);
 131                        val = buffer_read_string(len);
 132                        if (key == keys.svn_log) {
 133                                /* Value length excludes terminating nul. */
 134                                rev_ctx.log = log_copy(len + 1, val);
 135                        } else if (key == keys.svn_author) {
 136                                rev_ctx.author = pool_intern(val);
 137                        } else if (key == keys.svn_date) {
 138                                if (parse_date_basic(val, &rev_ctx.timestamp, NULL))
 139                                        fprintf(stderr, "Invalid timestamp: %s\n", val);
 140                        } else if (key == keys.svn_executable) {
 141                                node_ctx.type = REPO_MODE_EXE;
 142                        } else if (key == keys.svn_special) {
 143                                node_ctx.type = REPO_MODE_LNK;
 144                        }
 145                        key = ~0;
 146                        buffer_read_line();
 147                }
 148        }
 149}
 150
 151static void handle_node(void)
 152{
 153        uint32_t mark = 0;
 154        const uint32_t type = node_ctx.type;
 155        const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
 156
 157        if (node_ctx.text_delta || node_ctx.prop_delta)
 158                die("text and property deltas not supported");
 159
 160        if (node_ctx.textLength != LENGTH_UNKNOWN)
 161                mark = next_blob_mark();
 162
 163        if (node_ctx.action == NODEACT_DELETE) {
 164                if (mark || have_props || node_ctx.srcRev)
 165                        die("invalid dump: deletion node has "
 166                                "copyfrom info, text, or properties");
 167                return repo_delete(node_ctx.dst);
 168        }
 169
 170        if (node_ctx.action == NODEACT_REPLACE) {
 171                repo_delete(node_ctx.dst);
 172                node_ctx.action = NODEACT_ADD;
 173        }
 174
 175        if (node_ctx.srcRev) {
 176                repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
 177                if (node_ctx.action == NODEACT_ADD)
 178                        node_ctx.action = NODEACT_CHANGE;
 179        }
 180
 181        if (mark && type == REPO_MODE_DIR)
 182                die("invalid dump: directories cannot have text attached");
 183
 184        if (node_ctx.action == NODEACT_CHANGE)
 185                node_ctx.type = repo_modify_path(node_ctx.dst, 0, mark);
 186        else if (node_ctx.action == NODEACT_ADD)
 187                repo_add(node_ctx.dst, type, mark);
 188        else
 189                die("invalid dump: Node-path block lacks Node-action");
 190
 191        if (have_props) {
 192                const uint32_t old_mode = node_ctx.type;
 193                node_ctx.type = type;
 194                if (node_ctx.propLength)
 195                        read_props();
 196                if (node_ctx.type != old_mode)
 197                        repo_modify_path(node_ctx.dst, node_ctx.type, mark);
 198        }
 199
 200        if (mark)
 201                fast_export_blob(node_ctx.type, mark, node_ctx.textLength);
 202}
 203
 204static void handle_revision(void)
 205{
 206        if (rev_ctx.revision)
 207                repo_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log,
 208                        dump_ctx.uuid, dump_ctx.url, rev_ctx.timestamp);
 209}
 210
 211void svndump_read(const char *url)
 212{
 213        char *val;
 214        char *t;
 215        uint32_t active_ctx = DUMP_CTX;
 216        uint32_t len;
 217        uint32_t key;
 218
 219        reset_dump_ctx(pool_intern(url));
 220        while ((t = buffer_read_line())) {
 221                val = strstr(t, ": ");
 222                if (!val)
 223                        continue;
 224                *val++ = '\0';
 225                *val++ = '\0';
 226                key = pool_intern(t);
 227
 228                if (key == keys.svn_fs_dump_format_version) {
 229                        dump_ctx.version = atoi(val);
 230                        if (dump_ctx.version > 3)
 231                                die("expected svn dump format version <= 3, found %d",
 232                                    dump_ctx.version);
 233                } else if (key == keys.uuid) {
 234                        dump_ctx.uuid = pool_intern(val);
 235                } else if (key == keys.revision_number) {
 236                        if (active_ctx == NODE_CTX)
 237                                handle_node();
 238                        if (active_ctx != DUMP_CTX)
 239                                handle_revision();
 240                        active_ctx = REV_CTX;
 241                        reset_rev_ctx(atoi(val));
 242                } else if (key == keys.node_path) {
 243                        if (active_ctx == NODE_CTX)
 244                                handle_node();
 245                        active_ctx = NODE_CTX;
 246                        reset_node_ctx(val);
 247                } else if (key == keys.node_kind) {
 248                        if (!strcmp(val, "dir"))
 249                                node_ctx.type = REPO_MODE_DIR;
 250                        else if (!strcmp(val, "file"))
 251                                node_ctx.type = REPO_MODE_BLB;
 252                        else
 253                                fprintf(stderr, "Unknown node-kind: %s\n", val);
 254                } else if (key == keys.node_action) {
 255                        if (!strcmp(val, "delete")) {
 256                                node_ctx.action = NODEACT_DELETE;
 257                        } else if (!strcmp(val, "add")) {
 258                                node_ctx.action = NODEACT_ADD;
 259                        } else if (!strcmp(val, "change")) {
 260                                node_ctx.action = NODEACT_CHANGE;
 261                        } else if (!strcmp(val, "replace")) {
 262                                node_ctx.action = NODEACT_REPLACE;
 263                        } else {
 264                                fprintf(stderr, "Unknown node-action: %s\n", val);
 265                                node_ctx.action = NODEACT_UNKNOWN;
 266                        }
 267                } else if (key == keys.node_copyfrom_path) {
 268                        pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
 269                } else if (key == keys.node_copyfrom_rev) {
 270                        node_ctx.srcRev = atoi(val);
 271                } else if (key == keys.text_content_length) {
 272                        node_ctx.textLength = atoi(val);
 273                } else if (key == keys.prop_content_length) {
 274                        node_ctx.propLength = atoi(val);
 275                } else if (key == keys.text_delta) {
 276                        node_ctx.text_delta = !strcmp(val, "true");
 277                } else if (key == keys.prop_delta) {
 278                        node_ctx.prop_delta = !strcmp(val, "true");
 279                } else if (key == keys.content_length) {
 280                        len = atoi(val);
 281                        buffer_read_line();
 282                        if (active_ctx == REV_CTX) {
 283                                read_props();
 284                        } else if (active_ctx == NODE_CTX) {
 285                                handle_node();
 286                                active_ctx = REV_CTX;
 287                        } else {
 288                                fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
 289                                buffer_skip_bytes(len);
 290                        }
 291                }
 292        }
 293        if (active_ctx == NODE_CTX)
 294                handle_node();
 295        if (active_ctx != DUMP_CTX)
 296                handle_revision();
 297}
 298
 299int svndump_init(const char *filename)
 300{
 301        if (buffer_init(filename))
 302                return error("cannot open %s: %s", filename, strerror(errno));
 303        repo_init();
 304        reset_dump_ctx(~0);
 305        reset_rev_ctx(0);
 306        reset_node_ctx(NULL);
 307        init_keys();
 308        return 0;
 309}
 310
 311void svndump_deinit(void)
 312{
 313        log_reset();
 314        repo_reset();
 315        reset_dump_ctx(~0);
 316        reset_rev_ctx(0);
 317        reset_node_ctx(NULL);
 318        if (buffer_deinit())
 319                fprintf(stderr, "Input error\n");
 320        if (ferror(stdout))
 321                fprintf(stderr, "Output error\n");
 322}
 323
 324void svndump_reset(void)
 325{
 326        log_reset();
 327        buffer_reset();
 328        repo_reset();
 329        reset_dump_ctx(~0);
 330        reset_rev_ctx(0);
 331        reset_node_ctx(NULL);
 332}