Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2020, 2022 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <sys/uio.h>
23 #include <sys/mman.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #include <imsg.h>
28 #include <limits.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <sha1.h>
35 #include <sha2.h>
36 #include <endian.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <zlib.h>
40 #include <err.h>
41 #include <assert.h>
42 #include <dirent.h>
44 #include "got_error.h"
45 #include "got_object.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_idset.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_ratelimit.h"
56 #include "got_lib_pack_index.h"
57 #include "got_lib_delta_cache.h"
59 struct got_indexed_object {
60 struct got_object_id id;
62 /*
63 * Has this object been fully resolved?
64 * If so, we know its ID, otherwise we don't and 'id' is invalid.
65 */
66 int valid;
68 /* Offset of type+size field for this object in pack file. */
69 off_t off;
71 /* Type+size values parsed from pack file. */
72 uint8_t type;
73 uint64_t size;
75 /* Length of on-disk type+size data. */
76 size_t tslen;
78 /* Length of object data following type+size. */
79 size_t len;
81 uint32_t crc;
83 union {
84 struct {
85 /* For ref deltas. */
86 struct got_object_id ref_id;
87 } ref;
88 struct {
89 /* For offset deltas. */
90 off_t base_offset;
91 size_t base_offsetlen;
92 } ofs;
93 } delta;
94 };
96 static void
97 putbe32(char *b, uint32_t n)
98 {
99 b[0] = n >> 24;
100 b[1] = n >> 16;
101 b[2] = n >> 8;
102 b[3] = n >> 0;
105 static const struct got_error *
106 read_checksum(uint32_t *crc, struct got_hash *ctx, int fd, size_t len)
108 uint8_t buf[8192];
109 size_t n;
110 ssize_t r;
112 for (n = len; n > 0; n -= r){
113 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
114 if (r == -1)
115 return got_error_from_errno("read");
116 if (r == 0)
117 break;
118 if (crc)
119 *crc = crc32(*crc, buf, r);
120 if (ctx)
121 got_hash_update(ctx, buf, r);
124 return NULL;
127 static const struct got_error *
128 read_file_digest(struct got_hash *ctx, FILE *f, size_t len)
130 uint8_t buf[8192];
131 size_t n, r;
133 for (n = len; n > 0; n -= r) {
134 r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
135 if (r == 0) {
136 if (feof(f))
137 return NULL;
138 return got_ferror(f, GOT_ERR_IO);
140 got_hash_update(ctx, buf, r);
143 return NULL;
146 static const struct got_error *
147 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
148 FILE *tmpfile, struct got_hash *pack_sha1_ctx)
150 const struct got_error *err = NULL;
151 struct got_hash ctx;
152 uint8_t *data = NULL;
153 size_t datalen = 0;
154 ssize_t n;
155 char *header;
156 size_t headerlen;
157 const char *obj_label;
158 size_t mapoff = obj->off;
159 struct got_inflate_checksum csum;
161 memset(&csum, 0, sizeof(csum));
162 csum.input_ctx = pack_sha1_ctx;
163 csum.input_crc = &obj->crc;
165 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
166 &obj->tslen, pack, obj->off);
167 if (err)
168 return err;
170 if (pack->map) {
171 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
172 got_hash_update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
173 mapoff += obj->tslen;
174 } else {
175 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
176 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
177 return got_error_from_errno("lseek");
178 err = read_checksum(&obj->crc, pack_sha1_ctx,
179 pack->fd, obj->tslen);
180 if (err)
181 return err;
184 switch (obj->type) {
185 case GOT_OBJ_TYPE_BLOB:
186 case GOT_OBJ_TYPE_COMMIT:
187 case GOT_OBJ_TYPE_TREE:
188 case GOT_OBJ_TYPE_TAG:
189 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
190 if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
191 err = got_error_from_errno("fseek");
192 break;
194 if (pack->map) {
195 err = got_inflate_to_file_mmap(&datalen,
196 &obj->len, &csum, pack->map, mapoff,
197 pack->filesize - mapoff, tmpfile);
198 } else {
199 err = got_inflate_to_file_fd(&datalen,
200 &obj->len, &csum, pack->fd, tmpfile);
202 } else {
203 if (pack->map) {
204 err = got_inflate_to_mem_mmap(&data, &datalen,
205 &obj->len, &csum, pack->map, mapoff,
206 pack->filesize - mapoff);
207 } else {
208 err = got_inflate_to_mem_fd(&data, &datalen,
209 &obj->len, &csum, obj->size, pack->fd);
212 if (err)
213 break;
214 got_hash_init(&ctx, GOT_HASH_SHA1);
215 err = got_object_type_label(&obj_label, obj->type);
216 if (err) {
217 free(data);
218 break;
220 if (asprintf(&header, "%s %lld", obj_label,
221 (long long)obj->size) == -1) {
222 err = got_error_from_errno("asprintf");
223 free(data);
224 break;
226 headerlen = strlen(header) + 1;
227 got_hash_update(&ctx, header, headerlen);
228 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
229 err = read_file_digest(&ctx, tmpfile, datalen);
230 if (err) {
231 free(header);
232 free(data);
233 break;
235 } else
236 got_hash_update(&ctx, data, datalen);
237 got_hash_final_object_id(&ctx, &obj->id);
238 free(header);
239 free(data);
240 break;
241 case GOT_OBJ_TYPE_REF_DELTA:
242 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
243 if (pack->map) {
244 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
245 err = got_error(GOT_ERR_BAD_PACKFILE);
246 break;
248 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
249 SHA1_DIGEST_LENGTH);
250 obj->crc = crc32(obj->crc, pack->map + mapoff,
251 SHA1_DIGEST_LENGTH);
252 got_hash_update(pack_sha1_ctx, pack->map + mapoff,
253 SHA1_DIGEST_LENGTH);
254 mapoff += SHA1_DIGEST_LENGTH;
255 err = got_inflate_to_mem_mmap(NULL, &datalen,
256 &obj->len, &csum, pack->map, mapoff,
257 pack->filesize - mapoff);
258 if (err)
259 break;
260 } else {
261 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
262 SHA1_DIGEST_LENGTH);
263 if (n == -1) {
264 err = got_error_from_errno("read");
265 break;
267 if (n < sizeof(obj->id)) {
268 err = got_error(GOT_ERR_BAD_PACKFILE);
269 break;
271 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
272 SHA1_DIGEST_LENGTH);
273 got_hash_update(pack_sha1_ctx,
274 obj->delta.ref.ref_id.sha1, SHA1_DIGEST_LENGTH);
275 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
276 &csum, obj->size, pack->fd);
277 if (err)
278 break;
280 obj->len += SHA1_DIGEST_LENGTH;
281 break;
282 case GOT_OBJ_TYPE_OFFSET_DELTA:
283 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
284 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
285 &obj->delta.ofs.base_offsetlen, pack, obj->off,
286 obj->tslen);
287 if (err)
288 break;
290 if (pack->map) {
291 if (mapoff + obj->delta.ofs.base_offsetlen >=
292 pack->filesize) {
293 err = got_error(GOT_ERR_BAD_PACKFILE);
294 break;
297 if (mapoff + obj->delta.ofs.base_offsetlen >
298 SIZE_MAX) {
299 err = got_error_fmt(GOT_ERR_RANGE,
300 "mapoff %lld would overflow size_t",
301 (long long)mapoff
302 + obj->delta.ofs.base_offsetlen);
303 break;
306 obj->crc = crc32(obj->crc, pack->map + mapoff,
307 obj->delta.ofs.base_offsetlen);
308 got_hash_update(pack_sha1_ctx, pack->map + mapoff,
309 obj->delta.ofs.base_offsetlen);
310 mapoff += obj->delta.ofs.base_offsetlen;
311 err = got_inflate_to_mem_mmap(NULL, &datalen,
312 &obj->len, &csum, pack->map, mapoff,
313 pack->filesize - mapoff);
314 if (err)
315 break;
316 } else {
317 /*
318 * XXX Seek back and get CRC and SHA1 of on-disk
319 * offset bytes.
320 */
321 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
322 == -1) {
323 err = got_error_from_errno("lseek");
324 break;
326 err = read_checksum(&obj->crc, pack_sha1_ctx,
327 pack->fd, obj->delta.ofs.base_offsetlen);
328 if (err)
329 break;
331 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
332 &csum, obj->size, pack->fd);
333 if (err)
334 break;
336 obj->len += obj->delta.ofs.base_offsetlen;
337 break;
338 default:
339 err = got_error(GOT_ERR_OBJ_TYPE);
340 break;
343 return err;
346 const struct got_error *
347 got_pack_hwrite(int fd, void *buf, int len, struct got_hash *ctx)
349 ssize_t w;
351 got_hash_update(ctx, buf, len);
353 w = write(fd, buf, len);
354 if (w == -1)
355 return got_error_from_errno("write");
356 if (w != len)
357 return got_error(GOT_ERR_IO);
359 return NULL;
362 static const struct got_error *
363 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
364 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
365 FILE *delta_accum_file)
367 const struct got_error *err = NULL;
368 struct got_delta_chain deltas;
369 struct got_delta *delta;
370 uint8_t *buf = NULL;
371 size_t len = 0;
372 struct got_hash ctx;
373 char *header = NULL;
374 size_t headerlen;
375 uint64_t max_size;
376 int base_obj_type;
377 const char *obj_label;
379 deltas.nentries = 0;
380 STAILQ_INIT(&deltas.entries);
382 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
383 obj->off, obj->tslen, obj->type, obj->size,
384 GOT_DELTA_CHAIN_RECURSION_MAX);
385 if (err)
386 goto done;
388 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
389 if (err)
390 goto done;
391 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
392 rewind(tmpfile);
393 rewind(delta_base_file);
394 rewind(delta_accum_file);
395 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
396 pack, tmpfile, delta_base_file, delta_accum_file);
397 if (err)
398 goto done;
399 } else {
400 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
401 &deltas, pack);
403 if (err)
404 goto done;
406 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
407 if (err)
408 goto done;
409 err = got_object_type_label(&obj_label, base_obj_type);
410 if (err)
411 goto done;
412 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
413 err = got_error_from_errno("asprintf");
414 goto done;
416 headerlen = strlen(header) + 1;
417 got_hash_init(&ctx, GOT_HASH_SHA1);
418 got_hash_update(&ctx, header, headerlen);
419 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
420 err = read_file_digest(&ctx, tmpfile, len);
421 if (err)
422 goto done;
423 } else
424 got_hash_update(&ctx, buf, len);
425 got_hash_final_object_id(&ctx, &obj->id);
426 done:
427 free(buf);
428 free(header);
429 while (!STAILQ_EMPTY(&deltas.entries)) {
430 delta = STAILQ_FIRST(&deltas.entries);
431 STAILQ_REMOVE_HEAD(&deltas.entries, entry);
432 free(delta);
434 return err;
437 /* Determine the slot in the pack index a given object ID should use. */
438 static int
439 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
441 u_int8_t id0 = sha1[0];
442 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
443 int left = 0, right = nindexed - 1;
444 int cmp = 0, i = 0;
446 if (id0 > 0)
447 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
449 while (left <= right) {
450 struct got_packidx_object_id *oid;
452 i = ((left + right) / 2);
453 oid = &packidx->hdr.sorted_ids[i];
455 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
456 if (cmp == 0)
457 return -1; /* object already indexed */
458 else if (cmp > 0)
459 left = i + 1;
460 else if (cmp < 0)
461 right = i - 1;
464 return left;
467 #if 0
468 static void
469 print_packidx(struct got_packidx *packidx)
471 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
472 int i;
474 fprintf(stderr, "object IDs:\n");
475 for (i = 0; i < nindexed; i++) {
476 char hex[SHA1_DIGEST_STRING_LENGTH];
477 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
478 hex, sizeof(hex));
479 fprintf(stderr, "%s\n", hex);
481 fprintf(stderr, "\n");
483 fprintf(stderr, "object offsets:\n");
484 for (i = 0; i < nindexed; i++) {
485 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
486 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
487 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
488 fprintf(stderr, "%u -> %llu\n", offset,
489 be64toh(packidx->hdr.large_offsets[j]));
490 } else
491 fprintf(stderr, "%u\n", offset);
493 fprintf(stderr, "\n");
495 fprintf(stderr, "fanout table:");
496 for (i = 0; i <= 0xff; i++)
497 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
498 fprintf(stderr, "\n");
500 #endif
502 static void
503 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
504 struct got_indexed_object *obj)
506 int i;
508 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
509 SHA1_DIGEST_LENGTH);
510 packidx->hdr.crc32[idx] = htobe32(obj->crc);
511 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
512 packidx->hdr.offsets[idx] = htobe32(obj->off);
513 else {
514 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
515 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
516 packidx->hdr.large_offsets[packidx->nlargeobj] =
517 htobe64(obj->off);
518 packidx->nlargeobj++;
521 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
522 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
523 packidx->hdr.fanout_table[i] = htobe32(n + 1);
527 static int
528 indexed_obj_cmp(const void *pa, const void *pb)
530 struct got_indexed_object *a, *b;
532 a = (struct got_indexed_object *)pa;
533 b = (struct got_indexed_object *)pb;
534 return got_object_id_cmp(&a->id, &b->id);
537 static void
538 make_packidx(struct got_packidx *packidx, uint32_t nobj,
539 struct got_indexed_object *objects)
541 struct got_indexed_object *obj;
542 int i;
543 uint32_t idx = 0;
545 qsort(objects, nobj, sizeof(struct got_indexed_object),
546 indexed_obj_cmp);
548 memset(packidx->hdr.fanout_table, 0,
549 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
550 packidx->nlargeobj = 0;
552 for (i = 0; i < nobj; i++) {
553 obj = &objects[i];
554 if (obj->valid)
555 add_indexed_object(packidx, idx++, obj);
559 static void
560 update_packidx(struct got_packidx *packidx, uint32_t nobj,
561 struct got_indexed_object *obj)
563 int idx;
564 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
566 idx = find_object_idx(packidx, obj->id.sha1);
567 if (idx == -1)
568 return; /* object already indexed */
570 memmove(&packidx->hdr.sorted_ids[idx + 1],
571 &packidx->hdr.sorted_ids[idx],
572 sizeof(struct got_packidx_object_id) * (nindexed - idx));
573 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
574 sizeof(uint32_t) * (nindexed - idx));
576 add_indexed_object(packidx, idx, obj);
579 static const struct got_error *
580 report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
581 uint32_t nobj_resolved, struct got_ratelimit *rl,
582 got_pack_index_progress_cb progress_cb, void *progress_arg)
584 const struct got_error *err;
585 int elapsed = 0;
587 if (rl) {
588 err = got_ratelimit_check(&elapsed, rl);
589 if (err || !elapsed)
590 return err;
593 return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
594 nobj_resolved);
597 const struct got_error *
598 got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
599 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
600 got_pack_index_progress_cb progress_cb, void *progress_arg,
601 struct got_ratelimit *rl)
603 const struct got_error *err;
604 struct got_packfile_hdr hdr;
605 struct got_packidx packidx;
606 char buf[8];
607 char pack_sha1[SHA1_DIGEST_LENGTH];
608 uint32_t nobj, nvalid, nloose, nresolved = 0, i;
609 struct got_indexed_object *objects = NULL, *obj;
610 struct got_hash ctx;
611 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
612 ssize_t r, w;
613 int pass, have_ref_deltas = 0, first_delta_idx = -1;
614 size_t mapoff = 0;
615 int p_indexed = 0, last_p_indexed = -1;
616 int p_resolved = 0, last_p_resolved = -1;
618 /* Require that pack file header and SHA1 trailer are present. */
619 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
620 return got_error_msg(GOT_ERR_BAD_PACKFILE,
621 "short pack file");
623 if (pack->map) {
624 memcpy(&hdr, pack->map, sizeof(hdr));
625 mapoff += sizeof(hdr);
626 } else {
627 r = read(pack->fd, &hdr, sizeof(hdr));
628 if (r == -1)
629 return got_error_from_errno("read");
630 if (r < sizeof(hdr))
631 return got_error_msg(GOT_ERR_BAD_PACKFILE,
632 "short pack file");
635 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
636 return got_error_msg(GOT_ERR_BAD_PACKFILE,
637 "bad packfile signature");
638 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
639 return got_error_msg(GOT_ERR_BAD_PACKFILE,
640 "bad packfile version");
641 nobj = be32toh(hdr.nobjects);
642 if (nobj == 0)
643 return got_error_msg(GOT_ERR_BAD_PACKFILE,
644 "bad packfile with zero objects");
646 /* We compute the SHA1 of pack file contents and verify later on. */
647 got_hash_init(&ctx, GOT_HASH_SHA1);
648 got_hash_update(&ctx, &hdr, sizeof(hdr));
650 /*
651 * Create an in-memory pack index which will grow as objects
652 * IDs in the pack file are discovered. Only fields used to
653 * read deltified objects will be needed by the pack.c library
654 * code, so setting up just a pack index header is sufficient.
655 */
656 memset(&packidx, 0, sizeof(packidx));
657 packidx.hdr.magic = malloc(sizeof(uint32_t));
658 if (packidx.hdr.magic == NULL)
659 return got_error_from_errno("malloc");
660 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
661 packidx.hdr.version = malloc(sizeof(uint32_t));
662 if (packidx.hdr.version == NULL) {
663 err = got_error_from_errno("malloc");
664 goto done;
666 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
667 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
668 sizeof(uint32_t));
669 if (packidx.hdr.fanout_table == NULL) {
670 err = got_error_from_errno("calloc");
671 goto done;
673 packidx.hdr.sorted_ids = calloc(nobj,
674 sizeof(struct got_packidx_object_id));
675 if (packidx.hdr.sorted_ids == NULL) {
676 err = got_error_from_errno("calloc");
677 goto done;
679 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
680 if (packidx.hdr.crc32 == NULL) {
681 err = got_error_from_errno("calloc");
682 goto done;
684 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
685 if (packidx.hdr.offsets == NULL) {
686 err = got_error_from_errno("calloc");
687 goto done;
689 /* Large offsets table is empty for pack files < 2 GB. */
690 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
691 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
692 if (packidx.hdr.large_offsets == NULL) {
693 err = got_error_from_errno("calloc");
694 goto done;
698 nvalid = 0;
699 nloose = 0;
700 objects = calloc(nobj, sizeof(struct got_indexed_object));
701 if (objects == NULL)
702 return got_error_from_errno("calloc");
704 /*
705 * First pass: locate all objects and identify un-deltified objects.
707 * When this pass has completed we will know offset, type, size, and
708 * CRC information for all objects in this pack file. We won't know
709 * any of the actual object IDs of deltified objects yet since we
710 * will not yet attempt to combine deltas.
711 */
712 pass = 1;
713 for (i = 0; i < nobj; i++) {
714 /* Don't send too many progress privsep messages. */
715 p_indexed = ((i + 1) * 100) / nobj;
716 if (p_indexed != last_p_indexed) {
717 err = report_progress(nobj, i + 1, nloose, 0,
718 rl, progress_cb, progress_arg);
719 if (err)
720 goto done;
721 last_p_indexed = p_indexed;
724 obj = &objects[i];
725 obj->crc = crc32(0L, NULL, 0);
727 /* Store offset to type+size information for this object. */
728 if (pack->map) {
729 obj->off = mapoff;
730 } else {
731 obj->off = lseek(pack->fd, 0, SEEK_CUR);
732 if (obj->off == -1) {
733 err = got_error_from_errno("lseek");
734 goto done;
738 err = read_packed_object(pack, obj, tmpfile, &ctx);
739 if (err)
740 goto done;
742 if (pack->map) {
743 mapoff += obj->tslen + obj->len;
744 } else {
745 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
746 SEEK_SET) == -1) {
747 err = got_error_from_errno("lseek");
748 goto done;
752 if (obj->type == GOT_OBJ_TYPE_BLOB ||
753 obj->type == GOT_OBJ_TYPE_TREE ||
754 obj->type == GOT_OBJ_TYPE_COMMIT ||
755 obj->type == GOT_OBJ_TYPE_TAG) {
756 obj->valid = 1;
757 nloose++;
758 } else {
759 if (first_delta_idx == -1)
760 first_delta_idx = i;
761 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
762 have_ref_deltas = 1;
765 nvalid = nloose;
767 /*
768 * Having done a full pass over the pack file and can now
769 * verify its checksum.
770 */
771 got_hash_final(&ctx, pack_sha1);
773 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
774 err = got_error(GOT_ERR_PACKFILE_CSUM);
775 goto done;
778 /* Verify the SHA1 checksum stored at the end of the pack file. */
779 if (pack->map) {
780 if (pack->filesize > SIZE_MAX) {
781 err = got_error_fmt(GOT_ERR_RANGE,
782 "filesize %lld overflows size_t",
783 (long long)pack->filesize);
784 goto done;
787 memcpy(pack_sha1_expected, pack->map +
788 pack->filesize - SHA1_DIGEST_LENGTH,
789 SHA1_DIGEST_LENGTH);
790 } else {
791 ssize_t n;
792 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
793 err = got_error_from_errno("lseek");
794 goto done;
796 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
797 if (n == -1) {
798 err = got_error_from_errno("read");
799 goto done;
801 if (n != SHA1_DIGEST_LENGTH) {
802 err = got_error(GOT_ERR_IO);
803 goto done;
806 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
807 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
808 "bad checksum in pack file trailer");
809 goto done;
812 if (first_delta_idx == -1)
813 first_delta_idx = 0;
815 /* In order to resolve ref deltas we need an in-progress pack index. */
816 if (have_ref_deltas)
817 make_packidx(&packidx, nobj, objects);
819 /*
820 * Second pass: We can now resolve deltas to compute the IDs of
821 * objects which appear in deltified form. Because deltas can be
822 * chained this pass may require a couple of iterations until all
823 * IDs of deltified objects have been discovered.
824 */
825 pass++;
826 while (nvalid != nobj) {
827 int n = 0;
828 /*
829 * This loop will only run once unless the pack file
830 * contains ref deltas which refer to objects located
831 * later in the pack file, which is unusual.
832 * Offset deltas can always be resolved in one pass
833 * unless the packfile is corrupt.
834 */
835 for (i = first_delta_idx; i < nobj; i++) {
836 obj = &objects[i];
837 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
838 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
839 continue;
841 if (obj->valid)
842 continue;
844 if (pack->map == NULL && lseek(pack->fd,
845 obj->off + obj->tslen, SEEK_SET) == -1) {
846 err = got_error_from_errno("lseek");
847 goto done;
850 err = resolve_deltified_object(pack, &packidx, obj,
851 tmpfile, delta_base_file, delta_accum_file);
852 if (err) {
853 if (err->code != GOT_ERR_NO_OBJ)
854 goto done;
855 /*
856 * We cannot resolve this object yet because
857 * a delta base is unknown. Try again later.
858 */
859 continue;
862 obj->valid = 1;
863 n++;
864 if (have_ref_deltas)
865 update_packidx(&packidx, nobj, obj);
866 /* Don't send too many progress privsep messages. */
867 p_resolved = ((nresolved + n) * 100) / nobj;
868 if (p_resolved != last_p_resolved) {
869 err = report_progress(nobj, nobj,
870 nloose, nresolved + n, rl,
871 progress_cb, progress_arg);
872 if (err)
873 goto done;
874 last_p_resolved = p_resolved;
878 if (pass++ > 3 && n == 0) {
879 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
880 "could not resolve any of deltas; packfile could "
881 "be corrupt");
882 goto done;
884 nresolved += n;
885 nvalid += n;
888 if (nloose + nresolved != nobj) {
889 static char msg[64];
890 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
891 nloose + nresolved, nobj);
892 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
893 goto done;
896 err = report_progress(nobj, nobj, nloose, nresolved, NULL,
897 progress_cb, progress_arg);
898 if (err)
899 goto done;
901 make_packidx(&packidx, nobj, objects);
903 free(objects);
904 objects = NULL;
906 got_hash_init(&ctx, GOT_HASH_SHA1);
907 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
908 putbe32(buf + 4, GOT_PACKIDX_VERSION);
909 err = got_pack_hwrite(idxfd, buf, 8, &ctx);
910 if (err)
911 goto done;
912 err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
913 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
914 if (err)
915 goto done;
916 err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
917 nobj * SHA1_DIGEST_LENGTH, &ctx);
918 if (err)
919 goto done;
920 err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
921 nobj * sizeof(uint32_t), &ctx);
922 if (err)
923 goto done;
924 err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
925 nobj * sizeof(uint32_t), &ctx);
926 if (err)
927 goto done;
928 if (packidx.nlargeobj > 0) {
929 err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
930 packidx.nlargeobj * sizeof(uint64_t), &ctx);
931 if (err)
932 goto done;
934 err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
935 if (err)
936 goto done;
938 got_hash_final(&ctx, packidx_hash);
939 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
940 if (w == -1) {
941 err = got_error_from_errno("write");
942 goto done;
944 if (w != sizeof(packidx_hash)) {
945 err = got_error(GOT_ERR_IO);
946 goto done;
948 done:
949 free(objects);
950 free(packidx.hdr.magic);
951 free(packidx.hdr.version);
952 free(packidx.hdr.fanout_table);
953 free(packidx.hdr.sorted_ids);
954 free(packidx.hdr.offsets);
955 free(packidx.hdr.large_offsets);
956 return err;