process small deltas in memory; unfortunately it is not faster...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
diff --git a/lib/delta.c b/lib/delta.c
index 8b82432..c187dc3 100644
--- a/lib/delta.c
+++ b/lib/delta.c
@@ -261,6 +261,66 @@ got_delta_get_sizes(uint64_t *base_size, uint64_t *result_size,
}
const struct got_error *
+got_delta_apply_in_mem(uint8_t *base_buf, const uint8_t *delta_buf,
+ size_t delta_len, uint8_t *outbuf, size_t *outsize)
+{
+ const struct got_error *err = NULL;
+ uint64_t base_size, result_size;
+ size_t remain;
+ const uint8_t *p;
+
+ *outsize= 0;
+
+ if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
+ return got_error(GOT_ERR_BAD_DELTA);
+
+ p = delta_buf;
+ remain = delta_len;
+ err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
+ if (err)
+ return err;
+
+ /* Decode and execute copy instructions from the delta stream. */
+ err = next_delta_byte(&p, &remain);
+ while (err == NULL && remain > 0) {
+ if (*p & GOT_DELTA_BASE_COPY) {
+ off_t offset = 0;
+ size_t len = 0;
+ err = parse_opcode(&offset, &len, &p, &remain);
+ if (err)
+ break;
+ memcpy(outbuf + *outsize, base_buf + offset, len);
+ if (err == NULL) {
+ *outsize += len;
+ if (remain > 0) {
+ p++;
+ remain--;
+ }
+ }
+ } else {
+ size_t len = (size_t)*p;
+ if (len == 0) {
+ err = got_error(GOT_ERR_BAD_DELTA);
+ break;
+ }
+ err = next_delta_byte(&p, &remain);
+ if (err)
+ break;
+ if (remain < len)
+ return got_error(GOT_ERR_BAD_DELTA);
+ memcpy(outbuf + *outsize, p, len);
+ p += len;
+ remain -= len;
+ *outsize += len;
+ }
+ }
+
+ if (*outsize != result_size)
+ err = got_error(GOT_ERR_BAD_DELTA);
+ return err;
+}
+
+const struct got_error *
got_delta_apply(FILE *base_file, const uint8_t *delta_buf,
size_t delta_len, FILE *outfile)
{
diff --git a/lib/got_delta_lib.h b/lib/got_delta_lib.h
index 3c7bb23..27f892a 100644
--- a/lib/got_delta_lib.h
+++ b/lib/got_delta_lib.h
@@ -36,6 +36,8 @@ const struct got_error *got_delta_chain_get_base_type(int *,
struct got_delta_chain *);
const struct got_error *got_delta_get_sizes(uint64_t *, uint64_t *,
const uint8_t *, size_t);
+const struct got_error *got_delta_apply_in_mem(uint8_t *, const uint8_t *,
+ size_t, uint8_t *, size_t *);
const struct got_error *got_delta_apply(FILE *, const uint8_t *, size_t,
FILE *);
diff --git a/lib/pack.c b/lib/pack.c
index 06e9700..569429d 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -1093,31 +1093,33 @@ dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile,
const struct got_error *err = NULL;
struct got_delta *delta;
FILE *base_file = NULL, *accum_file = NULL;
+ uint8_t *base_buf = NULL, *accum_buf = NULL;
+ size_t accum_size;
uint64_t max_size;
int n = 0;
if (SIMPLEQ_EMPTY(&deltas->entries))
return got_error(GOT_ERR_BAD_DELTA_CHAIN);
+ /* We process small enough files entirely in memory for speed. */
err = get_delta_chain_max_size(&max_size, deltas);
if (err)
return err;
-
- if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
- base_file = fmemopen(NULL, max_size, "w+");
- else
+ if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
+ accum_buf = malloc(max_size);
+ if (accum_buf == NULL)
+ return got_error(GOT_ERR_NO_MEM);
+ } else {
base_file = got_opentemp();
- if (base_file == NULL)
- return got_error_from_errno();
+ if (base_file == NULL)
+ return got_error_from_errno();
- if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
- accum_file = fmemopen(NULL, max_size, "w+");
- else
accum_file = got_opentemp();
- if (accum_file == NULL) {
- err = got_error_from_errno();
- fclose(base_file);
- return err;
+ if (accum_file == NULL) {
+ err = got_error_from_errno();
+ fclose(base_file);
+ return err;
+ }
}
/* Deltas are ordered in ascending order. */
@@ -1127,6 +1129,7 @@ dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile,
if (n == 0) {
FILE *delta_file;
+ size_t base_len;
/* Plain object types are the delta base. */
if (delta->type != GOT_OBJ_TYPE_COMMIT &&
@@ -1149,13 +1152,28 @@ dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile,
err = got_error_from_errno();
goto done;
}
- err = got_inflate_to_file(&delta_len, delta_file,
- base_file);
+ if (base_file)
+ err = got_inflate_to_file(&delta_len,
+ delta_file, base_file);
+ else {
+ err = got_inflate_to_mem(&base_buf, &base_len,
+ delta_file);
+ if (base_len < max_size) {
+ uint8_t *p;
+ p = reallocarray(base_buf, 1, max_size);
+ if (p == NULL) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+ base_buf = p;
+ }
+ }
fclose(delta_file);
if (err)
goto done;
n++;
- rewind(base_file);
+ if (base_file)
+ rewind(base_file);
continue;
}
@@ -1188,25 +1206,46 @@ dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile,
}
/* delta_buf is now cached */
- err = got_delta_apply(base_file, delta_buf, delta_len,
- /* Final delta application writes to the output file. */
- ++n < deltas->nentries ? accum_file : outfile);
+ if (base_buf) {
+ err = got_delta_apply_in_mem(base_buf, delta_buf,
+ delta_len, accum_buf, &accum_size);
+ n++;
+ } else {
+ err = got_delta_apply(base_file, delta_buf, delta_len,
+ /* Final delta application writes to output file. */
+ ++n < deltas->nentries ? accum_file : outfile);
+ }
if (err)
goto done;
if (n < deltas->nentries) {
/* Accumulated delta becomes the new base. */
- FILE *tmp = accum_file;
- accum_file = base_file;
- base_file = tmp;
- rewind(base_file);
- rewind(accum_file);
+ if (base_buf) {
+ uint8_t *tmp = accum_buf;
+ accum_buf = base_buf;
+ base_buf = tmp;
+ } else {
+ FILE *tmp = accum_file;
+ accum_file = base_file;
+ base_file = tmp;
+ rewind(base_file);
+ rewind(accum_file);
+ }
}
}
done:
- fclose(base_file);
- fclose(accum_file);
+ free(base_buf);
+ if (accum_buf) {
+ size_t len = fwrite(accum_buf, 1, accum_size, outfile);
+ free(accum_buf);
+ if (len != accum_size)
+ return got_ferror(outfile, GOT_ERR_IO);
+ }
+ if (base_file)
+ fclose(base_file);
+ if (accum_file)
+ fclose(accum_file);
rewind(outfile);
return err;
}