Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2020 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/syslimits.h>
21 #include <sys/time.h>
22 #include <sys/types.h>
23 #include <sys/uio.h>
24 #include <sys/mman.h>
26 #include <stdint.h>
27 #include <errno.h>
28 #include <imsg.h>
29 #include <limits.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <sha1.h>
36 #include <fcntl.h>
37 #include <zlib.h>
38 #include <err.h>
39 #include <assert.h>
40 #include <dirent.h>
42 #include "got_error.h"
43 #include "got_object.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_object_idset.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_delta_cache.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
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 get_obj_type_label(const char **label, int obj_type)
108 const struct got_error *err = NULL;
110 switch (obj_type) {
111 case GOT_OBJ_TYPE_BLOB:
112 *label = GOT_OBJ_LABEL_BLOB;
113 break;
114 case GOT_OBJ_TYPE_TREE:
115 *label = GOT_OBJ_LABEL_TREE;
116 break;
117 case GOT_OBJ_TYPE_COMMIT:
118 *label = GOT_OBJ_LABEL_COMMIT;
119 break;
120 case GOT_OBJ_TYPE_TAG:
121 *label = GOT_OBJ_LABEL_TAG;
122 break;
123 default:
124 *label = NULL;
125 err = got_error(GOT_ERR_OBJ_TYPE);
126 break;
129 return err;
132 static const struct got_error *
133 read_crc(uint32_t *crc, int fd, size_t len)
135 uint8_t buf[8192];
136 size_t n;
137 ssize_t r;
139 for (n = len; n > 0; n -= r){
140 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
141 if (r == -1)
142 return got_error_from_errno("read");
143 if (r == 0)
144 break;
145 *crc = crc32(*crc, buf, r);
148 return NULL;
151 static const struct got_error *
152 read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
154 uint8_t buf[8192];
155 size_t n, r;
157 for (n = len; n > 0; n -= r) {
158 r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
159 if (r == 0) {
160 if (feof(f))
161 return NULL;
162 return got_ferror(f, GOT_ERR_IO);
164 SHA1Update(ctx, buf, r);
167 return NULL;
170 static const struct got_error *
171 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
172 FILE *tmpfile)
174 const struct got_error *err = NULL;
175 SHA1_CTX ctx;
176 uint8_t *data = NULL;
177 size_t datalen = 0;
178 ssize_t n;
179 char *header;
180 size_t headerlen;
181 const char *obj_label;
182 size_t mapoff = obj->off;
184 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
185 &obj->tslen, pack, obj->off);
186 if (err)
187 return err;
189 if (pack->map) {
190 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
191 mapoff += obj->tslen;
192 } else {
193 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
194 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
195 return got_error_from_errno("lseek");
196 err = read_crc(&obj->crc, pack->fd, obj->tslen);
197 if (err)
198 return err;
201 switch (obj->type) {
202 case GOT_OBJ_TYPE_BLOB:
203 case GOT_OBJ_TYPE_COMMIT:
204 case GOT_OBJ_TYPE_TREE:
205 case GOT_OBJ_TYPE_TAG:
206 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
207 if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
208 err = got_error_from_errno("fseek");
209 break;
211 if (pack->map) {
212 err = got_inflate_to_file_mmap(&datalen,
213 &obj->len, &obj->crc, pack->map, mapoff,
214 pack->filesize - mapoff, tmpfile);
215 } else {
216 err = got_inflate_to_file_fd(&datalen,
217 &obj->len, &obj->crc, pack->fd, tmpfile);
219 } else {
220 if (pack->map) {
221 err = got_inflate_to_mem_mmap(&data, &datalen,
222 &obj->len, &obj->crc, pack->map, mapoff,
223 pack->filesize - mapoff);
224 } else {
225 err = got_inflate_to_mem_fd(&data, &datalen,
226 &obj->len, &obj->crc, obj->size, pack->fd);
229 if (err)
230 break;
231 SHA1Init(&ctx);
232 err = get_obj_type_label(&obj_label, obj->type);
233 if (err) {
234 free(data);
235 break;
237 if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
238 err = got_error_from_errno("asprintf");
239 free(data);
240 break;
242 headerlen = strlen(header) + 1;
243 SHA1Update(&ctx, header, headerlen);
244 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
245 err = read_file_sha1(&ctx, tmpfile, datalen);
246 if (err)
247 break;
248 } else
249 SHA1Update(&ctx, data, datalen);
250 SHA1Final(obj->id.sha1, &ctx);
251 free(header);
252 free(data);
253 break;
254 case GOT_OBJ_TYPE_REF_DELTA:
255 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
256 if (pack->map) {
257 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
258 err = got_error(GOT_ERR_BAD_PACKFILE);
259 break;
261 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
262 SHA1_DIGEST_LENGTH);
263 obj->crc = crc32(obj->crc, pack->map + mapoff,
264 SHA1_DIGEST_LENGTH);
265 mapoff += SHA1_DIGEST_LENGTH;
266 err = got_inflate_to_mem_mmap(NULL, &datalen,
267 &obj->len, &obj->crc, pack->map, mapoff,
268 pack->filesize - mapoff);
269 if (err)
270 break;
271 } else {
272 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
273 SHA1_DIGEST_LENGTH);
274 if (n == -1) {
275 err = got_error_from_errno("read");
276 break;
278 if (n < sizeof(obj->id)) {
279 err = got_error(GOT_ERR_BAD_PACKFILE);
280 break;
282 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
283 SHA1_DIGEST_LENGTH);
284 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
285 &obj->crc, obj->size, pack->fd);
286 if (err)
287 break;
289 obj->len += SHA1_DIGEST_LENGTH;
290 break;
291 case GOT_OBJ_TYPE_OFFSET_DELTA:
292 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
293 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
294 &obj->delta.ofs.base_offsetlen, pack, obj->off,
295 obj->tslen);
296 if (err)
297 break;
299 if (pack->map) {
300 obj->crc = crc32(obj->crc, pack->map + mapoff,
301 obj->delta.ofs.base_offsetlen);
302 mapoff += obj->delta.ofs.base_offsetlen;
303 err = got_inflate_to_mem_mmap(NULL, &datalen,
304 &obj->len, &obj->crc, pack->map, mapoff,
305 pack->filesize - mapoff);
306 if (err)
307 break;
308 } else {
309 /*
310 * XXX Seek back and get the CRC of on-disk
311 * offset bytes.
312 */
313 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
314 == -1) {
315 err = got_error_from_errno("lseek");
316 break;
318 err = read_crc(&obj->crc, pack->fd,
319 obj->delta.ofs.base_offsetlen);
320 if (err)
321 break;
323 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
324 &obj->crc, obj->size, pack->fd);
325 if (err)
326 break;
328 obj->len += obj->delta.ofs.base_offsetlen;
329 break;
330 default:
331 err = got_error(GOT_ERR_OBJ_TYPE);
332 break;
335 return err;
338 static const struct got_error *
339 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
341 ssize_t w;
343 SHA1Update(ctx, buf, len);
345 w = write(fd, buf, len);
346 if (w == -1)
347 return got_error_from_errno("write");
348 if (w != len)
349 return got_error(GOT_ERR_IO);
351 return NULL;
354 static const struct got_error *
355 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
356 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
357 FILE *delta_accum_file)
359 const struct got_error *err = NULL;
360 struct got_delta_chain deltas;
361 struct got_delta *delta;
362 uint8_t *buf = NULL;
363 size_t len = 0;
364 SHA1_CTX ctx;
365 char *header = NULL;
366 size_t headerlen;
367 uint64_t max_size;
368 int base_obj_type;
369 const char *obj_label;
371 deltas.nentries = 0;
372 SIMPLEQ_INIT(&deltas.entries);
374 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
375 obj->off, obj->tslen, obj->type, obj->size,
376 GOT_DELTA_CHAIN_RECURSION_MAX);
377 if (err)
378 goto done;
380 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
381 if (err)
382 goto done;
383 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
384 rewind(tmpfile);
385 rewind(delta_base_file);
386 rewind(delta_accum_file);
387 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
388 pack, tmpfile, delta_base_file, delta_accum_file);
389 if (err)
390 goto done;
391 } else {
392 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
393 &deltas, pack);
395 if (err)
396 goto done;
398 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
399 if (err)
400 goto done;
401 err = get_obj_type_label(&obj_label, base_obj_type);
402 if (err)
403 goto done;
404 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
405 err = got_error_from_errno("asprintf");
406 goto done;
408 headerlen = strlen(header) + 1;
409 SHA1Init(&ctx);
410 SHA1Update(&ctx, header, headerlen);
411 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
412 err = read_file_sha1(&ctx, tmpfile, len);
413 if (err)
414 goto done;
415 } else
416 SHA1Update(&ctx, buf, len);
417 SHA1Final(obj->id.sha1, &ctx);
418 done:
419 free(buf);
420 free(header);
421 while (!SIMPLEQ_EMPTY(&deltas.entries)) {
422 delta = SIMPLEQ_FIRST(&deltas.entries);
423 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
424 free(delta);
426 return err;
429 /* Determine the slot in the pack index a given object ID should use. */
430 static int
431 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
433 u_int8_t id0 = sha1[0];
434 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
435 int left = 0, right = nindexed - 1;
436 int cmp = 0, i = 0;
438 if (id0 > 0)
439 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
441 while (left <= right) {
442 struct got_packidx_object_id *oid;
444 i = ((left + right) / 2);
445 oid = &packidx->hdr.sorted_ids[i];
447 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
448 if (cmp == 0)
449 return -1; /* object already indexed */
450 else if (cmp > 0)
451 left = i + 1;
452 else if (cmp < 0)
453 right = i - 1;
456 return left;
459 #if 0
460 static void
461 print_packidx(struct got_packidx *packidx)
463 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
464 int i;
466 fprintf(stderr, "object IDs:\n");
467 for (i = 0; i < nindexed; i++) {
468 char hex[SHA1_DIGEST_STRING_LENGTH];
469 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
470 hex, sizeof(hex));
471 fprintf(stderr, "%s\n", hex);
473 fprintf(stderr, "\n");
475 fprintf(stderr, "object offsets:\n");
476 for (i = 0; i < nindexed; i++) {
477 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
478 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
479 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
480 fprintf(stderr, "%u -> %llu\n", offset,
481 be64toh(packidx->hdr.large_offsets[j]));
482 } else
483 fprintf(stderr, "%u\n", offset);
485 fprintf(stderr, "\n");
487 fprintf(stderr, "fanout table:");
488 for (i = 0; i <= 0xff; i++)
489 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
490 fprintf(stderr, "\n");
492 #endif
494 static void
495 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
496 struct got_indexed_object *obj)
498 int i;
500 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
501 SHA1_DIGEST_LENGTH);
502 packidx->hdr.crc32[idx] = htobe32(obj->crc);
503 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
504 packidx->hdr.offsets[idx] = htobe32(obj->off);
505 else {
506 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
507 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
508 packidx->hdr.large_offsets[packidx->nlargeobj] =
509 htobe64(obj->off);
510 packidx->nlargeobj++;
513 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
514 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
515 packidx->hdr.fanout_table[i] = htobe32(n + 1);
519 static int
520 indexed_obj_cmp(const void *pa, const void *pb)
522 struct got_indexed_object *a, *b;
524 a = (struct got_indexed_object *)pa;
525 b = (struct got_indexed_object *)pb;
526 return got_object_id_cmp(&a->id, &b->id);
529 static void
530 make_packidx(struct got_packidx *packidx, int nobj,
531 struct got_indexed_object *objects)
533 struct got_indexed_object *obj;
534 int i;
535 uint32_t idx = 0;
537 qsort(objects, nobj, sizeof(struct got_indexed_object),
538 indexed_obj_cmp);
540 memset(packidx->hdr.fanout_table, 0,
541 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
542 packidx->nlargeobj = 0;
544 for (i = 0; i < nobj; i++) {
545 obj = &objects[i];
546 if (obj->valid)
547 add_indexed_object(packidx, idx++, obj);
551 static void
552 update_packidx(struct got_packidx *packidx, int nobj,
553 struct got_indexed_object *obj)
555 uint32_t idx;
556 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
558 idx = find_object_idx(packidx, obj->id.sha1);
559 if (idx == -1) {
560 char hex[SHA1_DIGEST_STRING_LENGTH];
561 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
562 return; /* object already indexed */
565 memmove(&packidx->hdr.sorted_ids[idx + 1],
566 &packidx->hdr.sorted_ids[idx],
567 sizeof(struct got_packidx_object_id) * (nindexed - idx));
568 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
569 sizeof(uint32_t) * (nindexed - idx));
571 add_indexed_object(packidx, idx, obj);
574 static const struct got_error *
575 index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
576 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_hash,
577 struct imsgbuf *ibuf)
579 const struct got_error *err;
580 struct got_packfile_hdr hdr;
581 struct got_packidx packidx;
582 char buf[8];
583 int nobj, nvalid, nloose, nresolved = 0, i;
584 struct got_indexed_object *objects = NULL, *obj;
585 SHA1_CTX ctx;
586 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
587 ssize_t r, w;
588 int pass, have_ref_deltas = 0, first_delta_idx = -1;
589 size_t mapoff = 0;
590 int p_indexed = 0, last_p_indexed = -1;
591 int p_resolved = 0, last_p_resolved = -1;
593 /* Check pack file header. */
594 if (pack->map) {
595 if (pack->filesize < sizeof(hdr))
596 return got_error_msg(GOT_ERR_BAD_PACKFILE,
597 "short packfile header");
598 memcpy(&hdr, pack->map, sizeof(hdr));
599 mapoff += sizeof(hdr);
600 } else {
601 r = read(pack->fd, &hdr, sizeof(hdr));
602 if (r == -1)
603 return got_error_from_errno("read");
604 if (r < sizeof(hdr))
605 return got_error_msg(GOT_ERR_BAD_PACKFILE,
606 "short packfile header");
609 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
610 return got_error_msg(GOT_ERR_BAD_PACKFILE,
611 "bad packfile signature");
612 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
613 return got_error_msg(GOT_ERR_BAD_PACKFILE,
614 "bad packfile version");
615 nobj = betoh32(hdr.nobjects);
616 if (nobj == 0)
617 return got_error_msg(GOT_ERR_BAD_PACKFILE,
618 "bad packfile with zero objects");
620 /*
621 * Create an in-memory pack index which will grow as objects
622 * IDs in the pack file are discovered. Only fields used to
623 * read deltified objects will be needed by the pack.c library
624 * code, so setting up just a pack index header is sufficient.
625 */
626 memset(&packidx, 0, sizeof(packidx));
627 packidx.hdr.magic = malloc(sizeof(uint32_t));
628 if (packidx.hdr.magic == NULL)
629 return got_error_from_errno("calloc");
630 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
631 packidx.hdr.version = malloc(sizeof(uint32_t));
632 if (packidx.hdr.version == NULL) {
633 err = got_error_from_errno("malloc");
634 goto done;
636 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
637 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
638 sizeof(uint32_t));
639 if (packidx.hdr.fanout_table == NULL) {
640 err = got_error_from_errno("calloc");
641 goto done;
643 packidx.hdr.sorted_ids = calloc(nobj,
644 sizeof(struct got_packidx_object_id));
645 if (packidx.hdr.sorted_ids == NULL) {
646 err = got_error_from_errno("calloc");
647 goto done;
649 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
650 if (packidx.hdr.crc32 == NULL) {
651 err = got_error_from_errno("calloc");
652 goto done;
654 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
655 if (packidx.hdr.offsets == NULL) {
656 err = got_error_from_errno("calloc");
657 goto done;
659 /* Large offsets table is empty for pack files < 2 GB. */
660 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
661 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
662 if (packidx.hdr.large_offsets == NULL) {
663 err = got_error_from_errno("calloc");
664 goto done;
668 nvalid = 0;
669 nloose = 0;
670 objects = calloc(nobj, sizeof(struct got_indexed_object));
671 if (objects == NULL)
672 return got_error_from_errno("calloc");
674 /*
675 * First pass: locate all objects and identify un-deltified objects.
677 * When this pass has completed we will know offset, type, size, and
678 * CRC information for all objects in this pack file. We won't know
679 * any of the actual object IDs of deltified objects yet since we
680 * will not yet attempt to combine deltas.
681 */
682 pass = 1;
683 for (i = 0; i < nobj; i++) {
684 /* Don't send too many progress privsep messages. */
685 p_indexed = ((i + 1) * 100) / nobj;
686 if (p_indexed != last_p_indexed) {
687 err = got_privsep_send_index_pack_progress(ibuf,
688 nobj, i + 1, nloose, 0);
689 if (err)
690 goto done;
691 last_p_indexed = p_indexed;
694 obj = &objects[i];
695 obj->crc = crc32(0L, NULL, 0);
697 /* Store offset to type+size information for this object. */
698 if (pack->map) {
699 obj->off = mapoff;
700 } else {
701 obj->off = lseek(pack->fd, 0, SEEK_CUR);
702 if (obj->off == -1) {
703 err = got_error_from_errno("lseek");
704 goto done;
708 err = read_packed_object(pack, obj, tmpfile);
709 if (err)
710 goto done;
712 if (pack->map) {
713 mapoff += obj->tslen + obj->len;
714 } else {
715 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
716 SEEK_SET) == -1) {
717 err = got_error_from_errno("lseek");
718 goto done;
722 if (obj->type == GOT_OBJ_TYPE_BLOB ||
723 obj->type == GOT_OBJ_TYPE_TREE ||
724 obj->type == GOT_OBJ_TYPE_COMMIT ||
725 obj->type == GOT_OBJ_TYPE_TAG) {
726 obj->valid = 1;
727 nloose++;
728 } else {
729 if (first_delta_idx == -1)
730 first_delta_idx = i;
731 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
732 have_ref_deltas = 1;
735 nvalid = nloose;
737 if (first_delta_idx == -1)
738 first_delta_idx = 0;
740 /* In order to resolve ref deltas we need an in-progress pack index. */
741 if (have_ref_deltas)
742 make_packidx(&packidx, nobj, objects);
744 /*
745 * Second pass: We can now resolve deltas to compute the IDs of
746 * objects which appear in deltified form. Because deltas can be
747 * chained this pass may require a couple of iterations until all
748 * IDs of deltified objects have been discovered.
749 */
750 pass++;
751 while (nvalid != nobj) {
752 int n = 0;
753 /*
754 * This loop will only run once unless the pack file
755 * contains ref deltas which refer to objects located
756 * later in the pack file, which is unusual.
757 * Offset deltas can always be resolved in one pass
758 * unless the packfile is corrupt.
759 */
760 for (i = first_delta_idx; i < nobj; i++) {
761 obj = &objects[i];
762 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
763 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
764 continue;
766 if (obj->valid)
767 continue;
769 if (pack->map == NULL && lseek(pack->fd,
770 obj->off + obj->tslen, SEEK_SET) == -1) {
771 err = got_error_from_errno("lseek");
772 goto done;
775 err = resolve_deltified_object(pack, &packidx, obj,
776 tmpfile, delta_base_file, delta_accum_file);
777 if (err) {
778 if (err->code != GOT_ERR_NO_OBJ)
779 goto done;
780 /*
781 * We cannot resolve this object yet because
782 * a delta base is unknown. Try again later.
783 */
784 continue;
787 obj->valid = 1;
788 n++;
789 if (have_ref_deltas)
790 update_packidx(&packidx, nobj, obj);
791 /* Don't send too many progress privsep messages. */
792 p_resolved = ((nresolved + n) * 100) / nobj;
793 if (p_resolved != last_p_resolved) {
794 err = got_privsep_send_index_pack_progress(ibuf,
795 nobj, nobj, nloose, nresolved + n);
796 if (err)
797 goto done;
798 last_p_resolved = p_resolved;
802 if (pass++ > 3 && n == 0) {
803 static char msg[64];
804 snprintf(msg, sizeof(msg), "could not resolve "
805 "any of deltas; packfile could be corrupt");
806 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
807 goto done;
810 nresolved += n;
811 nvalid += nresolved;
814 if (nloose + nresolved != nobj) {
815 static char msg[64];
816 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
817 nloose + nresolved, nobj);
818 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
819 goto done;
822 err = got_privsep_send_index_pack_progress(ibuf, nobj, nobj,
823 nloose, nresolved);
824 if (err)
825 goto done;
827 make_packidx(&packidx, nobj, objects);
829 free(objects);
830 objects = NULL;
832 SHA1Init(&ctx);
833 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
834 putbe32(buf + 4, GOT_PACKIDX_VERSION);
835 err = hwrite(idxfd, buf, 8, &ctx);
836 if (err)
837 goto done;
838 err = hwrite(idxfd, packidx.hdr.fanout_table,
839 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
840 if (err)
841 goto done;
842 err = hwrite(idxfd, packidx.hdr.sorted_ids,
843 nobj * SHA1_DIGEST_LENGTH, &ctx);
844 if (err)
845 goto done;
846 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
847 if (err)
848 goto done;
849 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
850 &ctx);
851 if (err)
852 goto done;
853 if (packidx.nlargeobj > 0) {
854 err = hwrite(idxfd, packidx.hdr.large_offsets,
855 packidx.nlargeobj * sizeof(uint64_t), &ctx);
856 if (err)
857 goto done;
859 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
860 if (err)
861 goto done;
863 SHA1Final(packidx_hash, &ctx);
864 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
865 if (w == -1) {
866 err = got_error_from_errno("write");
867 goto done;
869 if (w != sizeof(packidx_hash)) {
870 err = got_error(GOT_ERR_IO);
871 goto done;
873 done:
874 free(objects);
875 free(packidx.hdr.magic);
876 free(packidx.hdr.version);
877 free(packidx.hdr.fanout_table);
878 free(packidx.hdr.sorted_ids);
879 free(packidx.hdr.offsets);
880 free(packidx.hdr.large_offsets);
881 return err;
884 int
885 main(int argc, char **argv)
887 const struct got_error *err = NULL, *close_err;
888 struct imsgbuf ibuf;
889 struct imsg imsg;
890 int idxfd = -1, tmpfd = -1, i;
891 FILE *tmpfiles[3];
892 struct got_pack pack;
893 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
894 off_t packfile_size;
895 #if 0
896 static int attached;
897 while (!attached)
898 sleep(1);
899 #endif
901 for (i = 0; i < nitems(tmpfiles); i++)
902 tmpfiles[i] = NULL;
904 memset(&pack, 0, sizeof(pack));
905 pack.fd = -1;
906 pack.delta_cache = got_delta_cache_alloc(500,
907 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
908 if (pack.delta_cache == NULL) {
909 err = got_error_from_errno("got_delta_cache_alloc");
910 goto done;
913 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
914 #ifndef PROFILE
915 /* revoke access to most system calls */
916 if (pledge("stdio recvfd", NULL) == -1) {
917 err = got_error_from_errno("pledge");
918 got_privsep_send_error(&ibuf, err);
919 return 1;
921 #endif
922 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
923 if (err)
924 goto done;
925 if (imsg.hdr.type == GOT_IMSG_STOP)
926 goto done;
927 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
928 err = got_error(GOT_ERR_PRIVSEP_MSG);
929 goto done;
931 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
932 err = got_error(GOT_ERR_PRIVSEP_LEN);
933 goto done;
935 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
936 pack.fd = imsg.fd;
938 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
939 if (err)
940 goto done;
941 if (imsg.hdr.type == GOT_IMSG_STOP)
942 goto done;
943 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
944 err = got_error(GOT_ERR_PRIVSEP_MSG);
945 goto done;
947 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
949 goto done;
951 idxfd = imsg.fd;
953 for (i = 0; i < nitems(tmpfiles); i++) {
954 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
955 if (err)
956 goto done;
957 if (imsg.hdr.type == GOT_IMSG_STOP)
958 goto done;
959 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
960 err = got_error(GOT_ERR_PRIVSEP_MSG);
961 goto done;
963 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
964 err = got_error(GOT_ERR_PRIVSEP_LEN);
965 goto done;
967 tmpfd = imsg.fd;
968 tmpfiles[i] = fdopen(tmpfd, "w+");
969 if (tmpfiles[i] == NULL) {
970 err = got_error_from_errno("fdopen");
971 goto done;
973 tmpfd = -1;
976 if (lseek(pack.fd, 0, SEEK_END) == -1) {
977 err = got_error_from_errno("lseek");
978 goto done;
980 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
981 if (packfile_size == -1) {
982 err = got_error_from_errno("lseek");
983 goto done;
985 pack.filesize = packfile_size; /* XXX off_t vs size_t */
987 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
988 err = got_error_from_errno("lseek");
989 goto done;
992 #ifndef GOT_PACK_NO_MMAP
993 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
994 pack.fd, 0);
995 if (pack.map == MAP_FAILED)
996 pack.map = NULL; /* fall back to read(2) */
997 #endif
998 err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
999 pack_hash, &ibuf);
1000 done:
1001 close_err = got_pack_close(&pack);
1002 if (close_err && err == NULL)
1003 err = close_err;
1004 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1005 err = got_error_from_errno("close");
1006 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1007 err = got_error_from_errno("close");
1008 for (i = 0; i < nitems(tmpfiles); i++) {
1009 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1010 err == NULL)
1011 err = got_error_from_errno("close");
1014 if (err == NULL)
1015 err = got_privsep_send_index_pack_done(&ibuf);
1016 if (err) {
1017 got_privsep_send_error(&ibuf, err);
1018 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1019 got_privsep_send_error(&ibuf, err);
1020 exit(1);
1023 exit(0);