]> git.ipfire.org Git - thirdparty/git.git/blobdiff - vcs-svn/svndump.c
vcs-svn: implement perfect hash for top-level keys
[thirdparty/git.git] / vcs-svn / svndump.c
index 4fdfcbbc0d6ac01987229c99907367fa5bf38483..77680a31e8fed797715bc7c6727082574951ad0c 100644 (file)
 #include "obj_pool.h"
 #include "string_pool.h"
 
+/*
+ * Compare start of string to literal of equal length;
+ * must be guarded by length test.
+ */
+#define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
+
 #define NODEACT_REPLACE 4
 #define NODEACT_DELETE 3
 #define NODEACT_ADD 2
@@ -30,7 +36,9 @@
 /* Create memory pool for log messages */
 obj_pool_gen(log, char, 4096)
 
-static char* log_copy(uint32_t length, char *log)
+static struct line_buffer input = LINE_BUFFER_INIT;
+
+static char *log_copy(uint32_t length, const char *log)
 {
        char *buffer;
        log_free(log_pool.size);
@@ -55,15 +63,6 @@ static struct {
        uint32_t version, uuid, url;
 } dump_ctx;
 
-static struct {
-       uint32_t svn_log, svn_author, svn_date, svn_executable, svn_special, uuid,
-               revision_number, node_path, node_kind, node_action,
-               node_copyfrom_path, node_copyfrom_rev, text_content_length,
-               prop_content_length, content_length, svn_fs_dump_format_version,
-               /* version 3 format */
-               text_delta, prop_delta;
-} keys;
-
 static void reset_node_ctx(char *fname)
 {
        node_ctx.type = 0;
@@ -92,58 +91,121 @@ static void reset_dump_ctx(uint32_t url)
        dump_ctx.uuid = ~0;
 }
 
-static void init_keys(void)
+static void handle_property(const struct strbuf *key_buf,
+                               const char *val, uint32_t len,
+                               uint32_t *type_set)
+{
+       const char *key = key_buf->buf;
+       size_t keylen = key_buf->len;
+
+       switch (keylen + 1) {
+       case sizeof("svn:log"):
+               if (constcmp(key, "svn:log"))
+                       break;
+               if (!val)
+                       die("invalid dump: unsets svn:log");
+               /* Value length excludes terminating nul. */
+               rev_ctx.log = log_copy(len + 1, val);
+               break;
+       case sizeof("svn:author"):
+               if (constcmp(key, "svn:author"))
+                       break;
+               rev_ctx.author = pool_intern(val);
+               break;
+       case sizeof("svn:date"):
+               if (constcmp(key, "svn:date"))
+                       break;
+               if (!val)
+                       die("invalid dump: unsets svn:date");
+               if (parse_date_basic(val, &rev_ctx.timestamp, NULL))
+                       warning("invalid timestamp: %s", val);
+               break;
+       case sizeof("svn:executable"):
+       case sizeof("svn:special"):
+               if (keylen == strlen("svn:executable") &&
+                   constcmp(key, "svn:executable"))
+                       break;
+               if (keylen == strlen("svn:special") &&
+                   constcmp(key, "svn:special"))
+                       break;
+               if (*type_set) {
+                       if (!val)
+                               return;
+                       die("invalid dump: sets type twice");
+               }
+               if (!val) {
+                       node_ctx.type = REPO_MODE_BLB;
+                       return;
+               }
+               *type_set = 1;
+               node_ctx.type = keylen == strlen("svn:executable") ?
+                               REPO_MODE_EXE :
+                               REPO_MODE_LNK;
+       }
+}
+
+static void die_short_read(void)
 {
-       keys.svn_log = pool_intern("svn:log");
-       keys.svn_author = pool_intern("svn:author");
-       keys.svn_date = pool_intern("svn:date");
-       keys.svn_executable = pool_intern("svn:executable");
-       keys.svn_special = pool_intern("svn:special");
-       keys.uuid = pool_intern("UUID");
-       keys.revision_number = pool_intern("Revision-number");
-       keys.node_path = pool_intern("Node-path");
-       keys.node_kind = pool_intern("Node-kind");
-       keys.node_action = pool_intern("Node-action");
-       keys.node_copyfrom_path = pool_intern("Node-copyfrom-path");
-       keys.node_copyfrom_rev = pool_intern("Node-copyfrom-rev");
-       keys.text_content_length = pool_intern("Text-content-length");
-       keys.prop_content_length = pool_intern("Prop-content-length");
-       keys.content_length = pool_intern("Content-length");
-       keys.svn_fs_dump_format_version = pool_intern("SVN-fs-dump-format-version");
-       /* version 3 format (Subversion 1.1.0) */
-       keys.text_delta = pool_intern("Text-delta");
-       keys.prop_delta = pool_intern("Prop-delta");
+       if (buffer_ferror(&input))
+               die_errno("error reading dump file");
+       die("invalid dump: unexpected end of file");
 }
 
 static void read_props(void)
 {
-       uint32_t len;
-       uint32_t key = ~0;
-       char *val = NULL;
-       char *t;
-       while ((t = buffer_read_line()) && strcmp(t, "PROPS-END")) {
-               if (!strncmp(t, "K ", 2)) {
-                       len = atoi(&t[2]);
-                       key = pool_intern(buffer_read_string(len));
-                       buffer_read_line();
-               } else if (!strncmp(t, "V ", 2)) {
-                       len = atoi(&t[2]);
-                       val = buffer_read_string(len);
-                       if (key == keys.svn_log) {
-                               /* Value length excludes terminating nul. */
-                               rev_ctx.log = log_copy(len + 1, val);
-                       } else if (key == keys.svn_author) {
-                               rev_ctx.author = pool_intern(val);
-                       } else if (key == keys.svn_date) {
-                               if (parse_date_basic(val, &rev_ctx.timestamp, NULL))
-                                       fprintf(stderr, "Invalid timestamp: %s\n", val);
-                       } else if (key == keys.svn_executable) {
-                               node_ctx.type = REPO_MODE_EXE;
-                       } else if (key == keys.svn_special) {
-                               node_ctx.type = REPO_MODE_LNK;
-                       }
-                       key = ~0;
-                       buffer_read_line();
+       static struct strbuf key = STRBUF_INIT;
+       const char *t;
+       /*
+        * NEEDSWORK: to support simple mode changes like
+        *      K 11
+        *      svn:special
+        *      V 1
+        *      *
+        *      D 14
+        *      svn:executable
+        * we keep track of whether a mode has been set and reset to
+        * plain file only if not.  We should be keeping track of the
+        * symlink and executable bits separately instead.
+        */
+       uint32_t type_set = 0;
+       while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
+               uint32_t len;
+               const char *val;
+               const char type = t[0];
+               int ch;
+
+               if (!type || t[1] != ' ')
+                       die("invalid property line: %s\n", t);
+               len = atoi(&t[2]);
+               val = buffer_read_string(&input, len);
+               if (!val || strlen(val) != len)
+                       die_short_read();
+
+               /* Discard trailing newline. */
+               ch = buffer_read_char(&input);
+               if (ch == EOF)
+                       die_short_read();
+               if (ch != '\n')
+                       die("invalid dump: expected newline after %s", val);
+
+               switch (type) {
+               case 'K':
+               case 'D':
+                       strbuf_reset(&key);
+                       if (val)
+                               strbuf_add(&key, val, len);
+                       if (type == 'K')
+                               continue;
+                       assert(type == 'D');
+                       val = NULL;
+                       len = 0;
+                       /* fall through */
+               case 'V':
+                       handle_property(&key, val, len, &type_set);
+                       strbuf_reset(&key);
+                       continue;
+               default:
+                       die("invalid property line: %s\n", t);
                }
        }
 }
@@ -153,49 +215,70 @@ static void handle_node(void)
        uint32_t mark = 0;
        const uint32_t type = node_ctx.type;
        const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
+       const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
 
-       if (node_ctx.text_delta || node_ctx.prop_delta)
-               die("text and property deltas not supported");
-
-       if (node_ctx.textLength != LENGTH_UNKNOWN)
+       if (node_ctx.text_delta)
+               die("text deltas not supported");
+       if (have_text)
                mark = next_blob_mark();
-
        if (node_ctx.action == NODEACT_DELETE) {
-               if (mark || have_props || node_ctx.srcRev)
+               if (have_text || have_props || node_ctx.srcRev)
                        die("invalid dump: deletion node has "
                                "copyfrom info, text, or properties");
                return repo_delete(node_ctx.dst);
        }
-
        if (node_ctx.action == NODEACT_REPLACE) {
                repo_delete(node_ctx.dst);
                node_ctx.action = NODEACT_ADD;
        }
-
        if (node_ctx.srcRev) {
                repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
-               node_ctx.action = NODEACT_CHANGE;
+               if (node_ctx.action == NODEACT_ADD)
+                       node_ctx.action = NODEACT_CHANGE;
        }
-
-       if (mark && type == REPO_MODE_DIR)
+       if (have_text && type == REPO_MODE_DIR)
                die("invalid dump: directories cannot have text attached");
 
-       if (node_ctx.action == NODEACT_CHANGE)
-               node_ctx.type = repo_modify_path(node_ctx.dst, 0, mark);
-       else    /* Node-action: add */
-               repo_add(node_ctx.dst, type, mark);
+       /*
+        * Decide on the new content (mark) and mode (node_ctx.type).
+        */
+       if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
+               if (type != REPO_MODE_DIR)
+                       die("invalid dump: root of tree is not a regular file");
+       } else if (node_ctx.action == NODEACT_CHANGE) {
+               uint32_t mode;
+               if (!have_text)
+                       mark = repo_read_path(node_ctx.dst);
+               mode = repo_read_mode(node_ctx.dst);
+               if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
+                       die("invalid dump: cannot modify a directory into a file");
+               if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
+                       die("invalid dump: cannot modify a file into a directory");
+               node_ctx.type = mode;
+       } else if (node_ctx.action == NODEACT_ADD) {
+               if (!have_text && type != REPO_MODE_DIR)
+                       die("invalid dump: adds node without text");
+       } else {
+               die("invalid dump: Node-path block lacks Node-action");
+       }
 
+       /*
+        * Adjust mode to reflect properties.
+        */
        if (have_props) {
-               const uint32_t old_mode = node_ctx.type;
-               node_ctx.type = type;
+               if (!node_ctx.prop_delta)
+                       node_ctx.type = type;
                if (node_ctx.propLength)
                        read_props();
-               if (node_ctx.type != old_mode)
-                       repo_modify_path(node_ctx.dst, node_ctx.type, mark);
        }
 
-       if (mark)
-               fast_export_blob(node_ctx.type, mark, node_ctx.textLength);
+       /*
+        * Save the result.
+        */
+       repo_add(node_ctx.dst, node_ctx.type, mark);
+       if (have_text)
+               fast_export_blob(node_ctx.type, mark,
+                                node_ctx.textLength, &input);
 }
 
 static void handle_revision(void)
@@ -211,44 +294,61 @@ void svndump_read(const char *url)
        char *t;
        uint32_t active_ctx = DUMP_CTX;
        uint32_t len;
-       uint32_t key;
 
        reset_dump_ctx(pool_intern(url));
-       while ((t = buffer_read_line())) {
+       while ((t = buffer_read_line(&input))) {
                val = strstr(t, ": ");
                if (!val)
                        continue;
-               *val++ = '\0';
-               *val++ = '\0';
-               key = pool_intern(t);
+               val += 2;
 
-               if (key == keys.svn_fs_dump_format_version) {
+               /* strlen(key) + 1 */
+               switch (val - t - 1) {
+               case sizeof("SVN-fs-dump-format-version"):
+                       if (constcmp(t, "SVN-fs-dump-format-version"))
+                               continue;
                        dump_ctx.version = atoi(val);
                        if (dump_ctx.version > 3)
-                               die("expected svn dump format version <= 3, found %d",
+                               die("expected svn dump format version <= 3, found %"PRIu32,
                                    dump_ctx.version);
-               } else if (key == keys.uuid) {
+                       break;
+               case sizeof("UUID"):
+                       if (constcmp(t, "UUID"))
+                               continue;
                        dump_ctx.uuid = pool_intern(val);
-               } else if (key == keys.revision_number) {
+                       break;
+               case sizeof("Revision-number"):
+                       if (constcmp(t, "Revision-number"))
+                               continue;
                        if (active_ctx == NODE_CTX)
                                handle_node();
                        if (active_ctx != DUMP_CTX)
                                handle_revision();
                        active_ctx = REV_CTX;
                        reset_rev_ctx(atoi(val));
-               } else if (key == keys.node_path) {
-                       if (active_ctx == NODE_CTX)
-                               handle_node();
-                       active_ctx = NODE_CTX;
-                       reset_node_ctx(val);
-               } else if (key == keys.node_kind) {
+                       break;
+               case sizeof("Node-path"):
+                       if (prefixcmp(t, "Node-"))
+                               continue;
+                       if (!constcmp(t + strlen("Node-"), "path")) {
+                               if (active_ctx == NODE_CTX)
+                                       handle_node();
+                               active_ctx = NODE_CTX;
+                               reset_node_ctx(val);
+                               break;
+                       }
+                       if (constcmp(t + strlen("Node-"), "kind"))
+                               continue;
                        if (!strcmp(val, "dir"))
                                node_ctx.type = REPO_MODE_DIR;
                        else if (!strcmp(val, "file"))
                                node_ctx.type = REPO_MODE_BLB;
                        else
                                fprintf(stderr, "Unknown node-kind: %s\n", val);
-               } else if (key == keys.node_action) {
+                       break;
+               case sizeof("Node-action"):
+                       if (constcmp(t, "Node-action"))
+                               continue;
                        if (!strcmp(val, "delete")) {
                                node_ctx.action = NODEACT_DELETE;
                        } else if (!strcmp(val, "add")) {
@@ -261,21 +361,44 @@ void svndump_read(const char *url)
                                fprintf(stderr, "Unknown node-action: %s\n", val);
                                node_ctx.action = NODEACT_UNKNOWN;
                        }
-               } else if (key == keys.node_copyfrom_path) {
+                       break;
+               case sizeof("Node-copyfrom-path"):
+                       if (constcmp(t, "Node-copyfrom-path"))
+                               continue;
                        pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
-               } else if (key == keys.node_copyfrom_rev) {
+                       break;
+               case sizeof("Node-copyfrom-rev"):
+                       if (constcmp(t, "Node-copyfrom-rev"))
+                               continue;
                        node_ctx.srcRev = atoi(val);
-               } else if (key == keys.text_content_length) {
-                       node_ctx.textLength = atoi(val);
-               } else if (key == keys.prop_content_length) {
+                       break;
+               case sizeof("Text-content-length"):
+                       if (!constcmp(t, "Text-content-length")) {
+                               node_ctx.textLength = atoi(val);
+                               break;
+                       }
+                       if (constcmp(t, "Prop-content-length"))
+                               continue;
                        node_ctx.propLength = atoi(val);
-               } else if (key == keys.text_delta) {
-                       node_ctx.text_delta = !strcmp(val, "true");
-               } else if (key == keys.prop_delta) {
+                       break;
+               case sizeof("Text-delta"):
+                       if (!constcmp(t, "Text-delta")) {
+                               node_ctx.text_delta = !strcmp(val, "true");
+                               break;
+                       }
+                       if (constcmp(t, "Prop-delta"))
+                               continue;
                        node_ctx.prop_delta = !strcmp(val, "true");
-               } else if (key == keys.content_length) {
+                       break;
+               case sizeof("Content-length"):
+                       if (constcmp(t, "Content-length"))
+                               continue;
                        len = atoi(val);
-                       buffer_read_line();
+                       t = buffer_read_line(&input);
+                       if (!t)
+                               die_short_read();
+                       if (*t)
+                               die("invalid dump: expected blank line after content length header");
                        if (active_ctx == REV_CTX) {
                                read_props();
                        } else if (active_ctx == NODE_CTX) {
@@ -283,10 +406,13 @@ void svndump_read(const char *url)
                                active_ctx = REV_CTX;
                        } else {
                                fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
-                               buffer_skip_bytes(len);
+                               if (buffer_skip_bytes(&input, len) != len)
+                                       die_short_read();
                        }
                }
        }
+       if (buffer_ferror(&input))
+               die_short_read();
        if (active_ctx == NODE_CTX)
                handle_node();
        if (active_ctx != DUMP_CTX)
@@ -295,13 +421,12 @@ void svndump_read(const char *url)
 
 int svndump_init(const char *filename)
 {
-       if (buffer_init(filename))
+       if (buffer_init(&input, filename))
                return error("cannot open %s: %s", filename, strerror(errno));
        repo_init();
        reset_dump_ctx(~0);
        reset_rev_ctx(0);
        reset_node_ctx(NULL);
-       init_keys();
        return 0;
 }
 
@@ -312,7 +437,7 @@ void svndump_deinit(void)
        reset_dump_ctx(~0);
        reset_rev_ctx(0);
        reset_node_ctx(NULL);
-       if (buffer_deinit())
+       if (buffer_deinit(&input))
                fprintf(stderr, "Input error\n");
        if (ferror(stdout))
                fprintf(stderr, "Output error\n");
@@ -321,7 +446,7 @@ void svndump_deinit(void)
 void svndump_reset(void)
 {
        log_reset();
-       buffer_reset();
+       buffer_reset(&input);
        repo_reset();
        reset_dump_ctx(~0);
        reset_rev_ctx(0);