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 <endian.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <err.h>
40 #include <assert.h>
41 #include <dirent.h>
43 #include "got_error.h"
44 #include "got_object.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_object_idset.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_ratelimit.h"
55 #include "got_lib_pack_index.h"
56 #include "got_lib_delta_cache.h"
58 struct got_indexed_object {
59 struct got_object_id id;
61 /*
62 * Has this object been fully resolved?
63 * If so, we know its ID, otherwise we don't and 'id' is invalid.
64 */
65 int valid;
67 /* Offset of type+size field for this object in pack file. */
68 off_t off;
70 /* Type+size values parsed from pack file. */
71 uint8_t type;
72 uint64_t size;
74 /* Length of on-disk type+size data. */
75 size_t tslen;
77 /* Length of object data following type+size. */
78 size_t len;
80 uint32_t crc;
82 union {
83 struct {
84 /* For ref deltas. */
85 struct got_object_id ref_id;
86 } ref;
87 struct {
88 /* For offset deltas. */
89 off_t base_offset;
90 size_t base_offsetlen;
91 } ofs;
92 } delta;
93 };
95 static void
96 putbe32(char *b, uint32_t n)
97 {
98 b[0] = n >> 24;
99 b[1] = n >> 16;
100 b[2] = n >> 8;
101 b[3] = n >> 0;
104 static const struct got_error *
105 get_obj_type_label(const char **label, int obj_type)
107 const struct got_error *err = NULL;
109 switch (obj_type) {
110 case GOT_OBJ_TYPE_BLOB:
111 *label = GOT_OBJ_LABEL_BLOB;
112 break;
113 case GOT_OBJ_TYPE_TREE:
114 *label = GOT_OBJ_LABEL_TREE;
115 break;
116 case GOT_OBJ_TYPE_COMMIT:
117 *label = GOT_OBJ_LABEL_COMMIT;
118 break;
119 case GOT_OBJ_TYPE_TAG:
120 *label = GOT_OBJ_LABEL_TAG;
121 break;
122 default:
123 *label = NULL;
124 err = got_error(GOT_ERR_OBJ_TYPE);
125 break;
128 return err;
131 static const struct got_error *
132 read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
134 uint8_t buf[8192];
135 size_t n;
136 ssize_t r;
138 for (n = len; n > 0; n -= r){
139 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
140 if (r == -1)
141 return got_error_from_errno("read");
142 if (r == 0)
143 break;
144 if (crc)
145 *crc = crc32(*crc, buf, r);
146 if (sha1_ctx)
147 SHA1Update(sha1_ctx, buf, r);
150 return NULL;
153 static const struct got_error *
154 read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
156 uint8_t buf[8192];
157 size_t n, r;
159 for (n = len; n > 0; n -= r) {
160 r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
161 if (r == 0) {
162 if (feof(f))
163 return NULL;
164 return got_ferror(f, GOT_ERR_IO);
166 SHA1Update(ctx, buf, r);
169 return NULL;
172 static const struct got_error *
173 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
174 FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
176 const struct got_error *err = NULL;
177 SHA1_CTX ctx;
178 uint8_t *data = NULL;
179 size_t datalen = 0;
180 ssize_t n;
181 char *header;
182 size_t headerlen;
183 const char *obj_label;
184 size_t mapoff = obj->off;
185 struct got_inflate_checksum csum;
187 memset(&csum, 0, sizeof(csum));
188 csum.input_sha1 = pack_sha1_ctx;
189 csum.input_crc = &obj->crc;
191 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
192 &obj->tslen, pack, obj->off);
193 if (err)
194 return err;
196 if (pack->map) {
197 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
198 SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
199 mapoff += obj->tslen;
200 } else {
201 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
202 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
203 return got_error_from_errno("lseek");
204 err = read_checksum(&obj->crc, pack_sha1_ctx,
205 pack->fd, obj->tslen);
206 if (err)
207 return err;
210 switch (obj->type) {
211 case GOT_OBJ_TYPE_BLOB:
212 case GOT_OBJ_TYPE_COMMIT:
213 case GOT_OBJ_TYPE_TREE:
214 case GOT_OBJ_TYPE_TAG:
215 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
216 if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
217 err = got_error_from_errno("fseek");
218 break;
220 if (pack->map) {
221 err = got_inflate_to_file_mmap(&datalen,
222 &obj->len, &csum, pack->map, mapoff,
223 pack->filesize - mapoff, tmpfile);
224 } else {
225 err = got_inflate_to_file_fd(&datalen,
226 &obj->len, &csum, pack->fd, tmpfile);
228 } else {
229 if (pack->map) {
230 err = got_inflate_to_mem_mmap(&data, &datalen,
231 &obj->len, &csum, pack->map, mapoff,
232 pack->filesize - mapoff);
233 } else {
234 err = got_inflate_to_mem_fd(&data, &datalen,
235 &obj->len, &csum, obj->size, pack->fd);
238 if (err)
239 break;
240 SHA1Init(&ctx);
241 err = get_obj_type_label(&obj_label, obj->type);
242 if (err) {
243 free(data);
244 break;
246 if (asprintf(&header, "%s %lld", obj_label,
247 (long long)obj->size) == -1) {
248 err = got_error_from_errno("asprintf");
249 free(data);
250 break;
252 headerlen = strlen(header) + 1;
253 SHA1Update(&ctx, header, headerlen);
254 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
255 err = read_file_sha1(&ctx, tmpfile, datalen);
256 if (err) {
257 free(header);
258 free(data);
259 break;
261 } else
262 SHA1Update(&ctx, data, datalen);
263 SHA1Final(obj->id.sha1, &ctx);
264 free(header);
265 free(data);
266 break;
267 case GOT_OBJ_TYPE_REF_DELTA:
268 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
269 if (pack->map) {
270 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
271 err = got_error(GOT_ERR_BAD_PACKFILE);
272 break;
274 if (mapoff + SHA1_DIGEST_LENGTH > SIZE_MAX) {
275 err = got_error_fmt(GOT_ERR_RANGE,
276 "mapoff %lld would overflow size_t",
277 (long long)mapoff + SHA1_DIGEST_LENGTH);
278 break;
280 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
281 SHA1_DIGEST_LENGTH);
282 obj->crc = crc32(obj->crc, pack->map + mapoff,
283 SHA1_DIGEST_LENGTH);
284 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
285 SHA1_DIGEST_LENGTH);
286 mapoff += SHA1_DIGEST_LENGTH;
287 err = got_inflate_to_mem_mmap(NULL, &datalen,
288 &obj->len, &csum, pack->map, mapoff,
289 pack->filesize - mapoff);
290 if (err)
291 break;
292 } else {
293 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
294 SHA1_DIGEST_LENGTH);
295 if (n == -1) {
296 err = got_error_from_errno("read");
297 break;
299 if (n < sizeof(obj->id)) {
300 err = got_error(GOT_ERR_BAD_PACKFILE);
301 break;
303 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
304 SHA1_DIGEST_LENGTH);
305 SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
306 SHA1_DIGEST_LENGTH);
307 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
308 &csum, obj->size, pack->fd);
309 if (err)
310 break;
312 obj->len += SHA1_DIGEST_LENGTH;
313 break;
314 case GOT_OBJ_TYPE_OFFSET_DELTA:
315 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
316 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
317 &obj->delta.ofs.base_offsetlen, pack, obj->off,
318 obj->tslen);
319 if (err)
320 break;
322 if (pack->map) {
323 if (mapoff + obj->delta.ofs.base_offsetlen >=
324 pack->filesize) {
325 err = got_error(GOT_ERR_BAD_PACKFILE);
326 break;
329 if (mapoff + obj->delta.ofs.base_offsetlen >
330 SIZE_MAX) {
331 err = got_error_fmt(GOT_ERR_RANGE,
332 "mapoff %lld would overflow size_t",
333 (long long)mapoff
334 + obj->delta.ofs.base_offsetlen);
337 obj->crc = crc32(obj->crc, pack->map + mapoff,
338 obj->delta.ofs.base_offsetlen);
339 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
340 obj->delta.ofs.base_offsetlen);
341 mapoff += obj->delta.ofs.base_offsetlen;
342 err = got_inflate_to_mem_mmap(NULL, &datalen,
343 &obj->len, &csum, pack->map, mapoff,
344 pack->filesize - mapoff);
345 if (err)
346 break;
347 } else {
348 /*
349 * XXX Seek back and get CRC and SHA1 of on-disk
350 * offset bytes.
351 */
352 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
353 == -1) {
354 err = got_error_from_errno("lseek");
355 break;
357 err = read_checksum(&obj->crc, pack_sha1_ctx,
358 pack->fd, obj->delta.ofs.base_offsetlen);
359 if (err)
360 break;
362 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
363 &csum, obj->size, pack->fd);
364 if (err)
365 break;
367 obj->len += obj->delta.ofs.base_offsetlen;
368 break;
369 default:
370 err = got_error(GOT_ERR_OBJ_TYPE);
371 break;
374 return err;
377 const struct got_error *
378 got_pack_hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
380 ssize_t w;
382 SHA1Update(ctx, buf, len);
384 w = write(fd, buf, len);
385 if (w == -1)
386 return got_error_from_errno("write");
387 if (w != len)
388 return got_error(GOT_ERR_IO);
390 return NULL;
393 static const struct got_error *
394 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
395 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
396 FILE *delta_accum_file)
398 const struct got_error *err = NULL;
399 struct got_delta_chain deltas;
400 struct got_delta *delta;
401 uint8_t *buf = NULL;
402 size_t len = 0;
403 SHA1_CTX ctx;
404 char *header = NULL;
405 size_t headerlen;
406 uint64_t max_size;
407 int base_obj_type;
408 const char *obj_label;
410 deltas.nentries = 0;
411 STAILQ_INIT(&deltas.entries);
413 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
414 obj->off, obj->tslen, obj->type, obj->size,
415 GOT_DELTA_CHAIN_RECURSION_MAX);
416 if (err)
417 goto done;
419 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
420 if (err)
421 goto done;
422 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
423 rewind(tmpfile);
424 rewind(delta_base_file);
425 rewind(delta_accum_file);
426 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
427 pack, tmpfile, delta_base_file, delta_accum_file);
428 if (err)
429 goto done;
430 } else {
431 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
432 &deltas, pack);
434 if (err)
435 goto done;
437 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
438 if (err)
439 goto done;
440 err = get_obj_type_label(&obj_label, base_obj_type);
441 if (err)
442 goto done;
443 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
444 err = got_error_from_errno("asprintf");
445 goto done;
447 headerlen = strlen(header) + 1;
448 SHA1Init(&ctx);
449 SHA1Update(&ctx, header, headerlen);
450 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
451 err = read_file_sha1(&ctx, tmpfile, len);
452 if (err)
453 goto done;
454 } else
455 SHA1Update(&ctx, buf, len);
456 SHA1Final(obj->id.sha1, &ctx);
457 done:
458 free(buf);
459 free(header);
460 while (!STAILQ_EMPTY(&deltas.entries)) {
461 delta = STAILQ_FIRST(&deltas.entries);
462 STAILQ_REMOVE_HEAD(&deltas.entries, entry);
463 free(delta);
465 return err;
468 /* Determine the slot in the pack index a given object ID should use. */
469 static int
470 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
472 u_int8_t id0 = sha1[0];
473 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
474 int left = 0, right = nindexed - 1;
475 int cmp = 0, i = 0;
477 if (id0 > 0)
478 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
480 while (left <= right) {
481 struct got_packidx_object_id *oid;
483 i = ((left + right) / 2);
484 oid = &packidx->hdr.sorted_ids[i];
486 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
487 if (cmp == 0)
488 return -1; /* object already indexed */
489 else if (cmp > 0)
490 left = i + 1;
491 else if (cmp < 0)
492 right = i - 1;
495 return left;
498 #if 0
499 static void
500 print_packidx(struct got_packidx *packidx)
502 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
503 int i;
505 fprintf(stderr, "object IDs:\n");
506 for (i = 0; i < nindexed; i++) {
507 char hex[SHA1_DIGEST_STRING_LENGTH];
508 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
509 hex, sizeof(hex));
510 fprintf(stderr, "%s\n", hex);
512 fprintf(stderr, "\n");
514 fprintf(stderr, "object offsets:\n");
515 for (i = 0; i < nindexed; i++) {
516 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
517 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
518 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
519 fprintf(stderr, "%u -> %llu\n", offset,
520 be64toh(packidx->hdr.large_offsets[j]));
521 } else
522 fprintf(stderr, "%u\n", offset);
524 fprintf(stderr, "\n");
526 fprintf(stderr, "fanout table:");
527 for (i = 0; i <= 0xff; i++)
528 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
529 fprintf(stderr, "\n");
531 #endif
533 static void
534 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
535 struct got_indexed_object *obj)
537 int i;
539 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
540 SHA1_DIGEST_LENGTH);
541 packidx->hdr.crc32[idx] = htobe32(obj->crc);
542 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
543 packidx->hdr.offsets[idx] = htobe32(obj->off);
544 else {
545 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
546 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
547 packidx->hdr.large_offsets[packidx->nlargeobj] =
548 htobe64(obj->off);
549 packidx->nlargeobj++;
552 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
553 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
554 packidx->hdr.fanout_table[i] = htobe32(n + 1);
558 static int
559 indexed_obj_cmp(const void *pa, const void *pb)
561 struct got_indexed_object *a, *b;
563 a = (struct got_indexed_object *)pa;
564 b = (struct got_indexed_object *)pb;
565 return got_object_id_cmp(&a->id, &b->id);
568 static void
569 make_packidx(struct got_packidx *packidx, uint32_t nobj,
570 struct got_indexed_object *objects)
572 struct got_indexed_object *obj;
573 int i;
574 uint32_t idx = 0;
576 qsort(objects, nobj, sizeof(struct got_indexed_object),
577 indexed_obj_cmp);
579 memset(packidx->hdr.fanout_table, 0,
580 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
581 packidx->nlargeobj = 0;
583 for (i = 0; i < nobj; i++) {
584 obj = &objects[i];
585 if (obj->valid)
586 add_indexed_object(packidx, idx++, obj);
590 static void
591 update_packidx(struct got_packidx *packidx, uint32_t nobj,
592 struct got_indexed_object *obj)
594 int idx;
595 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
597 idx = find_object_idx(packidx, obj->id.sha1);
598 if (idx == -1) {
599 char hex[SHA1_DIGEST_STRING_LENGTH];
600 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
601 return; /* object already indexed */
604 memmove(&packidx->hdr.sorted_ids[idx + 1],
605 &packidx->hdr.sorted_ids[idx],
606 sizeof(struct got_packidx_object_id) * (nindexed - idx));
607 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
608 sizeof(uint32_t) * (nindexed - idx));
610 add_indexed_object(packidx, idx, obj);
613 static const struct got_error *
614 report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
615 uint32_t nobj_resolved, struct got_ratelimit *rl,
616 got_pack_index_progress_cb progress_cb, void *progress_arg)
618 const struct got_error *err;
619 int elapsed = 0;
621 if (rl) {
622 err = got_ratelimit_check(&elapsed, rl);
623 if (err || !elapsed)
624 return err;
627 return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
628 nobj_resolved);
631 const struct got_error *
632 got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
633 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
634 got_pack_index_progress_cb progress_cb, void *progress_arg,
635 struct got_ratelimit *rl)
637 const struct got_error *err;
638 struct got_packfile_hdr hdr;
639 struct got_packidx packidx;
640 char buf[8];
641 char pack_sha1[SHA1_DIGEST_LENGTH];
642 uint32_t nobj, nvalid, nloose, nresolved = 0, i;
643 struct got_indexed_object *objects = NULL, *obj;
644 SHA1_CTX ctx;
645 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
646 ssize_t r, w;
647 int pass, have_ref_deltas = 0, first_delta_idx = -1;
648 size_t mapoff = 0;
649 int p_indexed = 0, last_p_indexed = -1;
650 int p_resolved = 0, last_p_resolved = -1;
652 /* Require that pack file header and SHA1 trailer are present. */
653 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
654 return got_error_msg(GOT_ERR_BAD_PACKFILE,
655 "short pack file");
657 if (pack->map) {
658 memcpy(&hdr, pack->map, sizeof(hdr));
659 mapoff += sizeof(hdr);
660 } else {
661 r = read(pack->fd, &hdr, sizeof(hdr));
662 if (r == -1)
663 return got_error_from_errno("read");
664 if (r < sizeof(hdr))
665 return got_error_msg(GOT_ERR_BAD_PACKFILE,
666 "short pack file");
669 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
670 return got_error_msg(GOT_ERR_BAD_PACKFILE,
671 "bad packfile signature");
672 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
673 return got_error_msg(GOT_ERR_BAD_PACKFILE,
674 "bad packfile version");
675 nobj = be32toh(hdr.nobjects);
676 if (nobj == 0)
677 return got_error_msg(GOT_ERR_BAD_PACKFILE,
678 "bad packfile with zero objects");
680 /* We compute the SHA1 of pack file contents and verify later on. */
681 SHA1Init(&ctx);
682 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
684 /*
685 * Create an in-memory pack index which will grow as objects
686 * IDs in the pack file are discovered. Only fields used to
687 * read deltified objects will be needed by the pack.c library
688 * code, so setting up just a pack index header is sufficient.
689 */
690 memset(&packidx, 0, sizeof(packidx));
691 packidx.hdr.magic = malloc(sizeof(uint32_t));
692 if (packidx.hdr.magic == NULL)
693 return got_error_from_errno("malloc");
694 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
695 packidx.hdr.version = malloc(sizeof(uint32_t));
696 if (packidx.hdr.version == NULL) {
697 err = got_error_from_errno("malloc");
698 goto done;
700 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
701 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
702 sizeof(uint32_t));
703 if (packidx.hdr.fanout_table == NULL) {
704 err = got_error_from_errno("calloc");
705 goto done;
707 packidx.hdr.sorted_ids = calloc(nobj,
708 sizeof(struct got_packidx_object_id));
709 if (packidx.hdr.sorted_ids == NULL) {
710 err = got_error_from_errno("calloc");
711 goto done;
713 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
714 if (packidx.hdr.crc32 == NULL) {
715 err = got_error_from_errno("calloc");
716 goto done;
718 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
719 if (packidx.hdr.offsets == NULL) {
720 err = got_error_from_errno("calloc");
721 goto done;
723 /* Large offsets table is empty for pack files < 2 GB. */
724 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
725 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
726 if (packidx.hdr.large_offsets == NULL) {
727 err = got_error_from_errno("calloc");
728 goto done;
732 nvalid = 0;
733 nloose = 0;
734 objects = calloc(nobj, sizeof(struct got_indexed_object));
735 if (objects == NULL)
736 return got_error_from_errno("calloc");
738 /*
739 * First pass: locate all objects and identify un-deltified objects.
741 * When this pass has completed we will know offset, type, size, and
742 * CRC information for all objects in this pack file. We won't know
743 * any of the actual object IDs of deltified objects yet since we
744 * will not yet attempt to combine deltas.
745 */
746 pass = 1;
747 for (i = 0; i < nobj; i++) {
748 /* Don't send too many progress privsep messages. */
749 p_indexed = ((i + 1) * 100) / nobj;
750 if (p_indexed != last_p_indexed) {
751 err = report_progress(nobj, i + 1, nloose, 0,
752 rl, progress_cb, progress_arg);
753 if (err)
754 goto done;
755 last_p_indexed = p_indexed;
758 obj = &objects[i];
759 obj->crc = crc32(0L, NULL, 0);
761 /* Store offset to type+size information for this object. */
762 if (pack->map) {
763 obj->off = mapoff;
764 } else {
765 obj->off = lseek(pack->fd, 0, SEEK_CUR);
766 if (obj->off == -1) {
767 err = got_error_from_errno("lseek");
768 goto done;
772 err = read_packed_object(pack, obj, tmpfile, &ctx);
773 if (err)
774 goto done;
776 if (pack->map) {
777 mapoff += obj->tslen + obj->len;
778 } else {
779 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
780 SEEK_SET) == -1) {
781 err = got_error_from_errno("lseek");
782 goto done;
786 if (obj->type == GOT_OBJ_TYPE_BLOB ||
787 obj->type == GOT_OBJ_TYPE_TREE ||
788 obj->type == GOT_OBJ_TYPE_COMMIT ||
789 obj->type == GOT_OBJ_TYPE_TAG) {
790 obj->valid = 1;
791 nloose++;
792 } else {
793 if (first_delta_idx == -1)
794 first_delta_idx = i;
795 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
796 have_ref_deltas = 1;
799 nvalid = nloose;
801 /*
802 * Having done a full pass over the pack file and can now
803 * verify its checksum.
804 */
805 SHA1Final(pack_sha1, &ctx);
807 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
808 err = got_error(GOT_ERR_PACKFILE_CSUM);
809 goto done;
812 /* Verify the SHA1 checksum stored at the end of the pack file. */
813 if (pack->map) {
814 if (pack->filesize > SIZE_MAX) {
815 err = got_error_fmt(GOT_ERR_RANGE,
816 "filesize %lld overflows size_t",
817 (long long)pack->filesize);
818 goto done;
821 memcpy(pack_sha1_expected, pack->map +
822 pack->filesize - SHA1_DIGEST_LENGTH,
823 SHA1_DIGEST_LENGTH);
824 } else {
825 ssize_t n;
826 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
827 err = got_error_from_errno("lseek");
828 goto done;
830 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
831 if (n == -1) {
832 err = got_error_from_errno("read");
833 goto done;
835 if (n != SHA1_DIGEST_LENGTH) {
836 err = got_error(GOT_ERR_IO);
837 goto done;
840 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
841 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
842 "bad checksum in pack file trailer");
843 goto done;
846 if (first_delta_idx == -1)
847 first_delta_idx = 0;
849 /* In order to resolve ref deltas we need an in-progress pack index. */
850 if (have_ref_deltas)
851 make_packidx(&packidx, nobj, objects);
853 /*
854 * Second pass: We can now resolve deltas to compute the IDs of
855 * objects which appear in deltified form. Because deltas can be
856 * chained this pass may require a couple of iterations until all
857 * IDs of deltified objects have been discovered.
858 */
859 pass++;
860 while (nvalid != nobj) {
861 int n = 0;
862 /*
863 * This loop will only run once unless the pack file
864 * contains ref deltas which refer to objects located
865 * later in the pack file, which is unusual.
866 * Offset deltas can always be resolved in one pass
867 * unless the packfile is corrupt.
868 */
869 for (i = first_delta_idx; i < nobj; i++) {
870 obj = &objects[i];
871 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
872 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
873 continue;
875 if (obj->valid)
876 continue;
878 if (pack->map == NULL && lseek(pack->fd,
879 obj->off + obj->tslen, SEEK_SET) == -1) {
880 err = got_error_from_errno("lseek");
881 goto done;
884 err = resolve_deltified_object(pack, &packidx, obj,
885 tmpfile, delta_base_file, delta_accum_file);
886 if (err) {
887 if (err->code != GOT_ERR_NO_OBJ)
888 goto done;
889 /*
890 * We cannot resolve this object yet because
891 * a delta base is unknown. Try again later.
892 */
893 continue;
896 obj->valid = 1;
897 n++;
898 if (have_ref_deltas)
899 update_packidx(&packidx, nobj, obj);
900 /* Don't send too many progress privsep messages. */
901 p_resolved = ((nresolved + n) * 100) / nobj;
902 if (p_resolved != last_p_resolved) {
903 err = report_progress(nobj, nobj,
904 nloose, nresolved + n, rl,
905 progress_cb, progress_arg);
906 if (err)
907 goto done;
908 last_p_resolved = p_resolved;
912 if (pass++ > 3 && n == 0) {
913 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
914 "could not resolve any of deltas; packfile could "
915 "be corrupt");
916 goto done;
918 nresolved += n;
919 nvalid += nresolved;
922 if (nloose + nresolved != nobj) {
923 static char msg[64];
924 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
925 nloose + nresolved, nobj);
926 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
927 goto done;
930 err = report_progress(nobj, nobj, nloose, nresolved, NULL,
931 progress_cb, progress_arg);
932 if (err)
933 goto done;
935 make_packidx(&packidx, nobj, objects);
937 free(objects);
938 objects = NULL;
940 SHA1Init(&ctx);
941 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
942 putbe32(buf + 4, GOT_PACKIDX_VERSION);
943 err = got_pack_hwrite(idxfd, buf, 8, &ctx);
944 if (err)
945 goto done;
946 err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
947 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
948 if (err)
949 goto done;
950 err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
951 nobj * SHA1_DIGEST_LENGTH, &ctx);
952 if (err)
953 goto done;
954 err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
955 nobj * sizeof(uint32_t), &ctx);
956 if (err)
957 goto done;
958 err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
959 nobj * sizeof(uint32_t), &ctx);
960 if (err)
961 goto done;
962 if (packidx.nlargeobj > 0) {
963 err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
964 packidx.nlargeobj * sizeof(uint64_t), &ctx);
965 if (err)
966 goto done;
968 err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
969 if (err)
970 goto done;
972 SHA1Final(packidx_hash, &ctx);
973 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
974 if (w == -1) {
975 err = got_error_from_errno("write");
976 goto done;
978 if (w != sizeof(packidx_hash)) {
979 err = got_error(GOT_ERR_IO);
980 goto done;
982 done:
983 free(objects);
984 free(packidx.hdr.magic);
985 free(packidx.hdr.version);
986 free(packidx.hdr.fanout_table);
987 free(packidx.hdr.sorted_ids);
988 free(packidx.hdr.offsets);
989 free(packidx.hdr.large_offsets);
990 return err;