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