add mmap support to got-index-pack
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
diff --git a/lib/got_lib_inflate.h b/lib/got_lib_inflate.h
index 1d8212e..7a61421 100644
--- a/lib/got_lib_inflate.h
+++ b/lib/got_lib_inflate.h
@@ -41,8 +41,8 @@ const struct got_error *got_inflate_to_mem(uint8_t **, size_t *, size_t *,
FILE *);
const struct got_error *got_inflate_to_mem_fd(uint8_t **, size_t *, size_t *,
uint32_t *, size_t, int);
-const struct got_error *got_inflate_to_mem_mmap(uint8_t **, size_t *, uint8_t *,
- size_t, size_t);
+const struct got_error *got_inflate_to_mem_mmap(uint8_t **, size_t *, size_t *,
+ uint32_t *, uint8_t *, size_t, size_t);
const struct got_error *got_inflate_to_file(size_t *, FILE *, FILE *);
const struct got_error *got_inflate_to_file_fd(size_t *, int, FILE *);
const struct got_error *got_inflate_to_fd(size_t *, FILE *, int);
diff --git a/lib/inflate.c b/lib/inflate.c
index e1e7cd5..e94e223 100644
--- a/lib/inflate.c
+++ b/lib/inflate.c
@@ -370,8 +370,9 @@ done:
}
const struct got_error *
-got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
- size_t offset, size_t len)
+got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
+ size_t *consumed_total, uint32_t *input_crc, uint8_t *map, size_t offset,
+ size_t len)
{
const struct got_error *err;
size_t avail, consumed;
@@ -379,29 +380,40 @@ got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
void *newbuf;
int nbuf = 1;
- *outbuf = malloc(GOT_INFLATE_BUFSIZE);
- if (*outbuf == NULL)
- return got_error_from_errno("malloc");
- err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
- if (err) {
- free(*outbuf);
- *outbuf = NULL;
- return err;
+ if (outbuf) {
+ *outbuf = malloc(GOT_INFLATE_BUFSIZE);
+ if (*outbuf == NULL)
+ return got_error_from_errno("malloc");
+ err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
+ input_crc);
+ if (err) {
+ free(*outbuf);
+ *outbuf = NULL;
+ return err;
+ }
+ } else {
+ err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE,
+ input_crc);
}
*outlen = 0;
-
+ if (consumed_total)
+ *consumed_total = 0;
do {
err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
&consumed);
if (err)
goto done;
offset += consumed;
+ if (consumed_total)
+ *consumed_total += consumed;
len -= consumed;
*outlen += avail;
if (len == 0)
break;
if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
+ if (outbuf == NULL)
+ continue;
newbuf = reallocarray(*outbuf, ++nbuf,
GOT_INFLATE_BUFSIZE);
if (newbuf == NULL) {
diff --git a/lib/pack.c b/lib/pack.c
index e5817bc..8375932 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -714,8 +714,9 @@ read_delta_data(uint8_t **delta_buf, size_t *delta_len,
if (pack->map) {
if (delta_data_offset >= pack->filesize)
return got_error(GOT_ERR_PACK_OFFSET);
- err = got_inflate_to_mem_mmap(delta_buf, delta_len, pack->map,
- delta_data_offset, pack->filesize - delta_data_offset);
+ err = got_inflate_to_mem_mmap(delta_buf, delta_len,
+ NULL, NULL, pack->map, delta_data_offset,
+ pack->filesize - delta_data_offset);
} else {
if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
return got_error_from_errno("lseek");
@@ -1097,7 +1098,8 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
if (pack->map) {
mapoff = (size_t)delta_data_offset;
err = got_inflate_to_mem_mmap(&base_buf,
- &base_bufsz, pack->map, mapoff,
+ &base_bufsz, NULL, NULL,
+ pack->map, mapoff,
pack->filesize - mapoff);
} else
err = got_inflate_to_mem_fd(&base_buf,
@@ -1240,8 +1242,8 @@ got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
if (pack->map) {
size_t mapoff = (size_t)delta_data_offset;
err = got_inflate_to_mem_mmap(&base_buf,
- &base_bufsz, pack->map, mapoff,
- pack->filesize - mapoff);
+ &base_bufsz, NULL, NULL, pack->map,
+ mapoff, pack->filesize - mapoff);
} else {
if (lseek(pack->fd, delta_data_offset, SEEK_SET)
== -1) {
@@ -1365,8 +1367,8 @@ got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
return got_error(GOT_ERR_PACK_OFFSET);
if (pack->map) {
size_t mapoff = (size_t)obj->pack_offset;
- err = got_inflate_to_mem_mmap(buf, len, pack->map,
- mapoff, pack->filesize - mapoff);
+ err = got_inflate_to_mem_mmap(buf, len, NULL, NULL,
+ pack->map, mapoff, pack->filesize - mapoff);
} else {
if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
return got_error_from_errno("lseek");
diff --git a/libexec/got-index-pack/got-index-pack.c b/libexec/got-index-pack/got-index-pack.c
index 53e4b08..05e55c2 100644
--- a/libexec/got-index-pack/got-index-pack.c
+++ b/libexec/got-index-pack/got-index-pack.c
@@ -21,6 +21,7 @@
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
+#include <sys/mman.h>
#include <stdint.h>
#include <errno.h>
@@ -165,18 +166,24 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
char *header;
size_t headerlen;
const char *obj_label;
+ size_t mapoff = obj->off;
err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
&obj->tslen, pack, obj->off);
if (err)
return err;
- /* XXX Seek back and get the CRC of on-disk type+size bytes. */
- if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
- return got_error_from_errno("lseek");
- err = read_crc(&obj->crc, pack->fd, obj->tslen);
- if (err)
- return err;
+ if (pack->map) {
+ obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
+ mapoff += obj->tslen;
+ } else {
+ /* XXX Seek back and get the CRC of on-disk type+size bytes. */
+ if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
+ return got_error_from_errno("lseek");
+ err = read_crc(&obj->crc, pack->fd, obj->tslen);
+ if (err)
+ return err;
+ }
switch (obj->type) {
case GOT_OBJ_TYPE_BLOB:
@@ -184,8 +191,14 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
case GOT_OBJ_TYPE_TREE:
case GOT_OBJ_TYPE_TAG:
/* XXX TODO reading large objects into memory is bad! */
- err = got_inflate_to_mem_fd(&data, &datalen, &obj->len,
- &obj->crc, obj->size, pack->fd);
+ if (pack->map) {
+ err = got_inflate_to_mem_mmap(&data, &datalen,
+ &obj->len, &obj->crc, pack->map, mapoff,
+ pack->filesize - mapoff);
+ } else {
+ err = got_inflate_to_mem_fd(&data, &datalen,
+ &obj->len, &obj->crc, obj->size, pack->fd);
+ }
if (err)
break;
SHA1Init(&ctx);
@@ -208,22 +221,39 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
break;
case GOT_OBJ_TYPE_REF_DELTA:
memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
- n = read(pack->fd, obj->delta.ref.ref_id.sha1,
- SHA1_DIGEST_LENGTH);
- if (n == -1) {
- err = got_error_from_errno("read");
- break;
- }
- if (n < sizeof(obj->id)) {
- err = got_error(GOT_ERR_BAD_PACKFILE);
- break;
+ if (pack->map) {
+ if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
+ err = got_error(GOT_ERR_BAD_PACKFILE);
+ break;
+ }
+ memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
+ SHA1_DIGEST_LENGTH);
+ obj->crc = crc32(obj->crc, pack->map + mapoff,
+ SHA1_DIGEST_LENGTH);
+ mapoff += SHA1_DIGEST_LENGTH;
+ err = got_inflate_to_mem_mmap(NULL, &datalen,
+ &obj->len, &obj->crc, pack->map, mapoff,
+ pack->filesize - mapoff);
+ if (err)
+ break;
+ } else {
+ n = read(pack->fd, obj->delta.ref.ref_id.sha1,
+ SHA1_DIGEST_LENGTH);
+ if (n == -1) {
+ err = got_error_from_errno("read");
+ break;
+ }
+ if (n < sizeof(obj->id)) {
+ err = got_error(GOT_ERR_BAD_PACKFILE);
+ break;
+ }
+ obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
+ SHA1_DIGEST_LENGTH);
+ err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
+ &obj->crc, obj->size, pack->fd);
+ if (err)
+ break;
}
- obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
- SHA1_DIGEST_LENGTH);
- err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
- &obj->crc, obj->size, pack->fd);
- if (err)
- break;
obj->len += SHA1_DIGEST_LENGTH;
break;
case GOT_OBJ_TYPE_OFFSET_DELTA:
@@ -234,20 +264,35 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
if (err)
break;
- /* XXX Seek back and get the CRC of on-disk offset bytes. */
- if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET) == -1) {
- err = got_error_from_errno("lseek");
- break;
- }
- err = read_crc(&obj->crc, pack->fd,
- obj->delta.ofs.base_offsetlen);
- if (err)
- break;
+ if (pack->map) {
+ obj->crc = crc32(obj->crc, pack->map + mapoff,
+ obj->delta.ofs.base_offsetlen);
+ mapoff += obj->delta.ofs.base_offsetlen;
+ err = got_inflate_to_mem_mmap(NULL, &datalen,
+ &obj->len, &obj->crc, pack->map, mapoff,
+ pack->filesize - mapoff);
+ if (err)
+ break;
+ } else {
+ /*
+ * XXX Seek back and get the CRC of on-disk
+ * offset bytes.
+ */
+ if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
+ == -1) {
+ err = got_error_from_errno("lseek");
+ break;
+ }
+ err = read_crc(&obj->crc, pack->fd,
+ obj->delta.ofs.base_offsetlen);
+ if (err)
+ break;
- err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
- &obj->crc, obj->size, pack->fd);
- if (err)
- break;
+ err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
+ &obj->crc, obj->size, pack->fd);
+ if (err)
+ break;
+ }
obj->len += obj->delta.ofs.base_offsetlen;
break;
default:
@@ -488,14 +533,23 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
ssize_t r, w;
int pass, have_ref_deltas = 0;
+ size_t mapoff = 0;
/* Check pack file header. */
- r = read(pack->fd, &hdr, sizeof(hdr));
- if (r == -1)
- return got_error_from_errno("read");
- if (r < sizeof(hdr))
- return got_error_msg(GOT_ERR_BAD_PACKFILE,
- "short packfile header");
+ if (pack->map) {
+ if (pack->filesize < sizeof(hdr))
+ return got_error_msg(GOT_ERR_BAD_PACKFILE,
+ "short packfile header");
+ memcpy(&hdr, pack->map, sizeof(hdr));
+ mapoff += sizeof(hdr);
+ } else {
+ r = read(pack->fd, &hdr, sizeof(hdr));
+ if (r == -1)
+ return got_error_from_errno("read");
+ if (r < sizeof(hdr))
+ return got_error_msg(GOT_ERR_BAD_PACKFILE,
+ "short packfile header");
+ }
if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
return got_error_msg(GOT_ERR_BAD_PACKFILE,
@@ -576,20 +630,28 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
obj->crc = crc32(0L, NULL, 0);
/* Store offset to type+size information for this object. */
- obj->off = lseek(pack->fd, 0, SEEK_CUR);
- if (obj->off == -1) {
- err = got_error_from_errno("lseek");
- goto done;
+ if (pack->map) {
+ obj->off = mapoff;
+ } else {
+ obj->off = lseek(pack->fd, 0, SEEK_CUR);
+ if (obj->off == -1) {
+ err = got_error_from_errno("lseek");
+ goto done;
+ }
}
err = read_packed_object(pack, obj);
if (err)
goto done;
- if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
- SEEK_SET) == -1) {
- err = got_error_from_errno("lseek");
- goto done;
+ if (pack->map) {
+ mapoff += obj->tslen + obj->len;
+ } else {
+ if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
+ SEEK_SET) == -1) {
+ err = got_error_from_errno("lseek");
+ goto done;
+ }
}
if (obj->type == GOT_OBJ_TYPE_BLOB ||
@@ -625,10 +687,11 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
if (obj->valid)
continue;
- if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
+ if (pack->map == NULL &&
+ lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
== -1) {
- err = got_error_from_errno("lseek");
- goto done;
+ err = got_error_from_errno("lseek");
+ goto done;
}
err = resolve_deltified_object(pack, &packidx, obj);
@@ -817,6 +880,12 @@ main(int argc, char **argv)
goto done;
}
+#ifndef GOT_PACK_NO_MMAP
+ pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
+ pack.fd, 0);
+ if (pack.map == MAP_FAILED)
+ pack.map = NULL; /* fall back to read(2) */
+#endif
err = index_pack(&pack, idxfd, pack_hash, &ibuf);
done:
close_err = got_pack_close(&pack);