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