Merge branch 'jc/checkout-out-of-unborn'
[gitweb.git] / vcs-svn / fast_export.c
index 96a75d51d16c75d562758f62be2ca31e6eaf0f39..b823b8519c6dc76aa534e056c44eea7210e716f5 100644 (file)
@@ -14,7 +14,6 @@
 #include "line_buffer.h"
 
 #define MAX_GITSVN_LINE_LEN 4096
-#define REPORT_FILENO 3
 
 static uint32_t first_commit_done;
 static struct line_buffer postimage = LINE_BUFFER_INIT;
@@ -30,17 +29,9 @@ static int init_postimage(void)
        return buffer_tmpfile_init(&postimage);
 }
 
-static int init_report_buffer(int fd)
-{
-       static int report_buffer_initialized;
-       if (report_buffer_initialized)
-               return 0;
-       report_buffer_initialized = 1;
-       return buffer_fdinit(&report_buffer, fd);
-}
-
 void fast_export_init(int fd)
 {
+       first_commit_done = 0;
        if (buffer_fdinit(&report_buffer, fd))
                die_errno("cannot read from file descriptor %d", fd);
 }
@@ -198,32 +189,33 @@ static long apply_delta(off_t len, struct line_buffer *input,
                        const char *old_data, uint32_t old_mode)
 {
        long ret;
-       off_t preimage_len = 0;
-       struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, -1);
+       struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0);
        FILE *out;
 
        if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
                die("cannot open temporary file for blob retrieval");
-       if (init_report_buffer(REPORT_FILENO))
-               die("cannot open fd 3 for feedback from fast-import");
        if (old_data) {
                const char *response;
                printf("cat-blob %s\n", old_data);
                fflush(stdout);
                response = get_response_line();
-               if (parse_cat_response_line(response, &preimage_len))
+               if (parse_cat_response_line(response, &preimage.max_off))
                        die("invalid cat-blob response: %s", response);
+               check_preimage_overflow(preimage.max_off, 1);
        }
        if (old_mode == REPO_MODE_LNK) {
                strbuf_addstr(&preimage.buf, "link ");
-               check_preimage_overflow(preimage_len, strlen("link "));
-               preimage_len += strlen("link ");
+               check_preimage_overflow(preimage.max_off, strlen("link "));
+               preimage.max_off += strlen("link ");
+               check_preimage_overflow(preimage.max_off, 1);
        }
        if (svndiff0_apply(input, len, &preimage, out))
                die("cannot apply delta");
        if (old_data) {
                /* Read the remainder of preimage and trailing newline. */
-               if (move_window(&preimage, preimage_len, 1))
+               assert(!signed_add_overflows(preimage.max_off, 1));
+               preimage.max_off++;     /* room for newline */
+               if (move_window(&preimage, preimage.max_off - 1, 1))
                        die("cannot seek to end of input");
                if (preimage.buf.buf[0] != '\n')
                        die("missing newline after cat-blob response");
@@ -235,15 +227,18 @@ static long apply_delta(off_t len, struct line_buffer *input,
        return ret;
 }
 
-void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
+void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input)
 {
+       assert(len >= 0);
        if (mode == REPO_MODE_LNK) {
                /* svn symlink blobs start with "link " */
+               if (len < 5)
+                       die("invalid dump: symlink too short for \"link\" prefix");
                len -= 5;
                if (buffer_skip_bytes(input, 5) != 5)
                        die_short_read(input);
        }
-       printf("data %"PRIu32"\n", len);
+       printf("data %"PRIuMAX"\n", (uintmax_t) len);
        if (buffer_copy_bytes(input, len) != len)
                die_short_read(input);
        fputc('\n', stdout);
@@ -305,12 +300,12 @@ int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
 
 void fast_export_blob_delta(uint32_t mode,
                                uint32_t old_mode, const char *old_data,
-                               uint32_t len, struct line_buffer *input)
+                               off_t len, struct line_buffer *input)
 {
        long postimage_len;
-       if (len > maximum_signed_value_of_type(off_t))
-               die("enormous delta");
-       postimage_len = apply_delta((off_t) len, input, old_data, old_mode);
+
+       assert(len >= 0);
+       postimage_len = apply_delta(len, input, old_data, old_mode);
        if (mode == REPO_MODE_LNK) {
                buffer_skip_bytes(&postimage, strlen("link "));
                postimage_len -= strlen("link ");