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 struct got_indexed_object {
56 struct got_object_id id;
58 /*
59 * Has this object been fully resolved?
60 * If so, we know its ID, otherwise we don't and 'id' is invalid.
61 */
62 int valid;
64 /* Offset of type+size field for this object in pack file. */
65 off_t off;
67 /* Type+size values parsed from pack file. */
68 uint8_t type;
69 uint64_t size;
71 /* Length of on-disk type+size data. */
72 size_t tslen;
74 /* Length of object data following type+size. */
75 size_t len;
77 uint32_t crc;
79 union {
80 struct {
81 /* For ref deltas. */
82 struct got_object_id ref_id;
83 } ref;
84 struct {
85 /* For offset deltas. */
86 off_t base_offset;
87 size_t base_offsetlen;
88 } ofs;
89 } delta;
90 };
92 static void
93 putbe32(char *b, uint32_t n)
94 {
95 b[0] = n >> 24;
96 b[1] = n >> 16;
97 b[2] = n >> 8;
98 b[3] = n >> 0;
99 }
101 static const struct got_error *
102 get_obj_type_label(const char **label, int obj_type)
104 const struct got_error *err = NULL;
106 switch (obj_type) {
107 case GOT_OBJ_TYPE_BLOB:
108 *label = GOT_OBJ_LABEL_BLOB;
109 break;
110 case GOT_OBJ_TYPE_TREE:
111 *label = GOT_OBJ_LABEL_TREE;
112 break;
113 case GOT_OBJ_TYPE_COMMIT:
114 *label = GOT_OBJ_LABEL_COMMIT;
115 break;
116 case GOT_OBJ_TYPE_TAG:
117 *label = GOT_OBJ_LABEL_TAG;
118 break;
119 default:
120 *label = NULL;
121 err = got_error(GOT_ERR_OBJ_TYPE);
122 break;
125 return err;
128 static const struct got_error *
129 read_crc(uint32_t *crc, int fd, size_t len)
131 uint8_t buf[8192];
132 size_t n;
133 ssize_t r;
135 for (n = len; n > 0; n -= r){
136 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
137 if (r == -1)
138 return got_error_from_errno("read");
139 if (r == 0)
140 break;
141 *crc = crc32(*crc, buf, r);
144 return NULL;
147 static const struct got_error *
148 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
150 const struct got_error *err = NULL;
151 SHA1_CTX ctx;
152 uint8_t *data;
153 size_t datalen;
154 ssize_t n;
155 char *header;
156 size_t headerlen;
157 const char *obj_label;
158 size_t mapoff = obj->off;
160 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
161 &obj->tslen, pack, obj->off);
162 if (err)
163 return err;
165 if (pack->map) {
166 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
167 mapoff += obj->tslen;
168 } else {
169 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
170 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
171 return got_error_from_errno("lseek");
172 err = read_crc(&obj->crc, pack->fd, obj->tslen);
173 if (err)
174 return err;
177 switch (obj->type) {
178 case GOT_OBJ_TYPE_BLOB:
179 case GOT_OBJ_TYPE_COMMIT:
180 case GOT_OBJ_TYPE_TREE:
181 case GOT_OBJ_TYPE_TAG:
182 /* XXX TODO reading large objects into memory is bad! */
183 if (pack->map) {
184 err = got_inflate_to_mem_mmap(&data, &datalen,
185 &obj->len, &obj->crc, pack->map, mapoff,
186 pack->filesize - mapoff);
187 } else {
188 err = got_inflate_to_mem_fd(&data, &datalen,
189 &obj->len, &obj->crc, obj->size, pack->fd);
191 if (err)
192 break;
193 SHA1Init(&ctx);
194 err = get_obj_type_label(&obj_label, obj->type);
195 if (err) {
196 free(data);
197 break;
199 if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
200 err = got_error_from_errno("asprintf");
201 free(data);
202 break;
204 headerlen = strlen(header) + 1;
205 SHA1Update(&ctx, header, headerlen);
206 SHA1Update(&ctx, data, datalen);
207 SHA1Final(obj->id.sha1, &ctx);
208 free(header);
209 free(data);
210 break;
211 case GOT_OBJ_TYPE_REF_DELTA:
212 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
213 if (pack->map) {
214 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
215 err = got_error(GOT_ERR_BAD_PACKFILE);
216 break;
218 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
219 SHA1_DIGEST_LENGTH);
220 obj->crc = crc32(obj->crc, pack->map + mapoff,
221 SHA1_DIGEST_LENGTH);
222 mapoff += SHA1_DIGEST_LENGTH;
223 err = got_inflate_to_mem_mmap(NULL, &datalen,
224 &obj->len, &obj->crc, pack->map, mapoff,
225 pack->filesize - mapoff);
226 if (err)
227 break;
228 } else {
229 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
230 SHA1_DIGEST_LENGTH);
231 if (n == -1) {
232 err = got_error_from_errno("read");
233 break;
235 if (n < sizeof(obj->id)) {
236 err = got_error(GOT_ERR_BAD_PACKFILE);
237 break;
239 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
240 SHA1_DIGEST_LENGTH);
241 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
242 &obj->crc, obj->size, pack->fd);
243 if (err)
244 break;
246 obj->len += SHA1_DIGEST_LENGTH;
247 break;
248 case GOT_OBJ_TYPE_OFFSET_DELTA:
249 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
250 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
251 &obj->delta.ofs.base_offsetlen, pack, obj->off,
252 obj->tslen);
253 if (err)
254 break;
256 if (pack->map) {
257 obj->crc = crc32(obj->crc, pack->map + mapoff,
258 obj->delta.ofs.base_offsetlen);
259 mapoff += obj->delta.ofs.base_offsetlen;
260 err = got_inflate_to_mem_mmap(NULL, &datalen,
261 &obj->len, &obj->crc, pack->map, mapoff,
262 pack->filesize - mapoff);
263 if (err)
264 break;
265 } else {
266 /*
267 * XXX Seek back and get the CRC of on-disk
268 * offset bytes.
269 */
270 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
271 == -1) {
272 err = got_error_from_errno("lseek");
273 break;
275 err = read_crc(&obj->crc, pack->fd,
276 obj->delta.ofs.base_offsetlen);
277 if (err)
278 break;
280 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
281 &obj->crc, obj->size, pack->fd);
282 if (err)
283 break;
285 obj->len += obj->delta.ofs.base_offsetlen;
286 break;
287 default:
288 err = got_error(GOT_ERR_OBJ_TYPE);
289 break;
292 return err;
295 static const struct got_error *
296 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
298 ssize_t w;
300 SHA1Update(ctx, buf, len);
302 w = write(fd, buf, len);
303 if (w == -1)
304 return got_error_from_errno("write");
305 if (w != len)
306 return got_error(GOT_ERR_IO);
308 return NULL;
311 static const struct got_error *
312 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
313 struct got_indexed_object *obj)
315 const struct got_error *err = NULL;
316 struct got_delta_chain deltas;
317 struct got_delta *delta;
318 uint8_t *buf = NULL;
319 size_t len;
320 SHA1_CTX ctx;
321 char *header = NULL;
322 size_t headerlen;
323 int base_obj_type;
324 const char *obj_label;
326 deltas.nentries = 0;
327 SIMPLEQ_INIT(&deltas.entries);
329 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
330 obj->off, obj->tslen, obj->type, obj->size,
331 GOT_DELTA_CHAIN_RECURSION_MAX);
332 if (err)
333 goto done;
335 /* XXX TODO reading large objects into memory is bad! */
336 err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
337 if (err)
338 goto done;
340 SHA1Init(&ctx);
342 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
343 if (err)
344 goto done;
345 err = get_obj_type_label(&obj_label, base_obj_type);
346 if (err)
347 goto done;
348 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
349 err = got_error_from_errno("asprintf");
350 goto done;
352 headerlen = strlen(header) + 1;
353 SHA1Update(&ctx, header, headerlen);
354 SHA1Update(&ctx, buf, len);
355 SHA1Final(obj->id.sha1, &ctx);
356 done:
357 free(buf);
358 free(header);
359 while (!SIMPLEQ_EMPTY(&deltas.entries)) {
360 delta = SIMPLEQ_FIRST(&deltas.entries);
361 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
362 free(delta);
364 return err;
367 /* Determine the slot in the pack index a given object ID should use. */
368 static int
369 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
371 u_int8_t id0 = sha1[0];
372 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
373 int left = 0, right = nindexed - 1;
374 int cmp = 0, i = 0;
376 if (id0 > 0)
377 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
379 while (left <= right) {
380 struct got_packidx_object_id *oid;
382 i = ((left + right) / 2);
383 oid = &packidx->hdr.sorted_ids[i];
385 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
386 if (cmp == 0)
387 return -1; /* object already indexed */
388 else if (cmp > 0)
389 left = i + 1;
390 else if (cmp < 0)
391 right = i - 1;
394 return left;
397 #if 0
398 static void
399 print_packidx(struct got_packidx *packidx)
401 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
402 int i;
404 fprintf(stderr, "object IDs:\n");
405 for (i = 0; i < nindexed; i++) {
406 char hex[SHA1_DIGEST_STRING_LENGTH];
407 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
408 hex, sizeof(hex));
409 fprintf(stderr, "%s\n", hex);
411 fprintf(stderr, "\n");
413 fprintf(stderr, "object offsets:\n");
414 for (i = 0; i < nindexed; i++) {
415 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
416 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
417 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
418 fprintf(stderr, "%u -> %llu\n", offset,
419 be64toh(packidx->hdr.large_offsets[j]));
420 } else
421 fprintf(stderr, "%u\n", offset);
423 fprintf(stderr, "\n");
425 fprintf(stderr, "fanout table:");
426 for (i = 0; i <= 0xff; i++)
427 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
428 fprintf(stderr, "\n");
430 #endif
432 static void
433 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
434 struct got_indexed_object *obj)
436 int i;
438 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
439 SHA1_DIGEST_LENGTH);
440 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
441 packidx->hdr.offsets[idx] = htobe32(obj->off);
442 else {
443 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
444 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
445 packidx->hdr.large_offsets[packidx->nlargeobj] =
446 htobe64(obj->off);
447 packidx->nlargeobj++;
450 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
451 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
452 packidx->hdr.fanout_table[i] = htobe32(n + 1);
456 static int
457 indexed_obj_cmp(const void *pa, const void *pb)
459 struct got_indexed_object *a, *b;
461 a = (struct got_indexed_object *)pa;
462 b = (struct got_indexed_object *)pb;
463 return got_object_id_cmp(&a->id, &b->id);
466 static void
467 make_packidx(struct got_packidx *packidx, int nobj,
468 struct got_indexed_object *objects)
470 struct got_indexed_object *obj;
471 int i;
472 uint32_t idx = 0;
474 mergesort(objects, nobj, sizeof(struct got_indexed_object),
475 indexed_obj_cmp);
477 memset(packidx->hdr.fanout_table, 0,
478 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
479 packidx->nlargeobj = 0;
481 for (i = 0; i < nobj; i++) {
482 obj = &objects[i];
483 if (obj->valid)
484 add_indexed_object(packidx, idx++, obj);
488 static void
489 update_packidx(struct got_packidx *packidx, int nobj,
490 struct got_indexed_object *obj)
492 uint32_t idx;
493 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
495 idx = find_object_idx(packidx, obj->id.sha1);
496 if (idx == -1) {
497 char hex[SHA1_DIGEST_STRING_LENGTH];
498 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
499 return; /* object already indexed */
502 memmove(&packidx->hdr.sorted_ids[idx + 1],
503 &packidx->hdr.sorted_ids[idx],
504 sizeof(struct got_packidx_object_id) * (nindexed - idx));
505 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
506 sizeof(uint32_t) * (nindexed - idx));
508 add_indexed_object(packidx, idx, obj);
511 static const struct got_error *
512 index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
513 struct imsgbuf *ibuf)
515 const struct got_error *err;
516 struct got_packfile_hdr hdr;
517 struct got_packidx packidx;
518 char buf[8];
519 int nobj, nvalid, nloose, nresolved = 0, i;
520 struct got_indexed_object *objects = NULL, *obj;
521 SHA1_CTX ctx;
522 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
523 ssize_t r, w;
524 int pass, have_ref_deltas = 0;
525 size_t mapoff = 0;
527 /* Check pack file header. */
528 if (pack->map) {
529 if (pack->filesize < sizeof(hdr))
530 return got_error_msg(GOT_ERR_BAD_PACKFILE,
531 "short packfile header");
532 memcpy(&hdr, pack->map, sizeof(hdr));
533 mapoff += sizeof(hdr);
534 } else {
535 r = read(pack->fd, &hdr, sizeof(hdr));
536 if (r == -1)
537 return got_error_from_errno("read");
538 if (r < sizeof(hdr))
539 return got_error_msg(GOT_ERR_BAD_PACKFILE,
540 "short packfile header");
543 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
544 return got_error_msg(GOT_ERR_BAD_PACKFILE,
545 "bad packfile signature");
546 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
547 return got_error_msg(GOT_ERR_BAD_PACKFILE,
548 "bad packfile version");
549 nobj = betoh32(hdr.nobjects);
550 if (nobj == 0)
551 return got_error_msg(GOT_ERR_BAD_PACKFILE,
552 "bad packfile with zero objects");
554 /*
555 * Create an in-memory pack index which will grow as objects
556 * IDs in the pack file are discovered. Only fields used to
557 * read deltified objects will be needed by the pack.c library
558 * code, so setting up just a pack index header is sufficient.
559 */
560 memset(&packidx, 0, sizeof(packidx));
561 packidx.hdr.magic = malloc(sizeof(uint32_t));
562 if (packidx.hdr.magic == NULL)
563 return got_error_from_errno("calloc");
564 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
565 packidx.hdr.version = malloc(sizeof(uint32_t));
566 if (packidx.hdr.version == NULL) {
567 err = got_error_from_errno("malloc");
568 goto done;
570 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
571 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
572 sizeof(uint32_t));
573 if (packidx.hdr.fanout_table == NULL) {
574 err = got_error_from_errno("calloc");
575 goto done;
577 packidx.hdr.sorted_ids = calloc(nobj,
578 sizeof(struct got_packidx_object_id));
579 if (packidx.hdr.sorted_ids == NULL) {
580 err = got_error_from_errno("calloc");
581 goto done;
583 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
584 if (packidx.hdr.offsets == NULL) {
585 err = got_error_from_errno("calloc");
586 goto done;
588 /* Large offsets table is empty for pack files < 2 GB. */
589 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
590 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
591 if (packidx.hdr.large_offsets == NULL) {
592 err = got_error_from_errno("calloc");
593 goto done;
597 nvalid = 0;
598 nloose = 0;
599 objects = calloc(nobj, sizeof(struct got_indexed_object));
600 if (objects == NULL)
601 return got_error_from_errno("calloc");
603 /*
604 * First pass: locate all objects and identify un-deltified objects.
606 * When this pass has completed we will know offset, type, size, and
607 * CRC information for all objects in this pack file. We won't know
608 * any of the actual object IDs of deltified objects yet since we
609 * will not yet attempt to combine deltas.
610 */
611 pass = 1;
612 for (i = 0; i < nobj; i++) {
613 err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
614 nloose, 0);
615 if (err)
616 goto done;
618 obj = &objects[i];
619 obj->crc = crc32(0L, NULL, 0);
621 /* Store offset to type+size information for this object. */
622 if (pack->map) {
623 obj->off = mapoff;
624 } else {
625 obj->off = lseek(pack->fd, 0, SEEK_CUR);
626 if (obj->off == -1) {
627 err = got_error_from_errno("lseek");
628 goto done;
632 err = read_packed_object(pack, obj);
633 if (err)
634 goto done;
636 if (pack->map) {
637 mapoff += obj->tslen + obj->len;
638 } else {
639 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
640 SEEK_SET) == -1) {
641 err = got_error_from_errno("lseek");
642 goto done;
646 if (obj->type == GOT_OBJ_TYPE_BLOB ||
647 obj->type == GOT_OBJ_TYPE_TREE ||
648 obj->type == GOT_OBJ_TYPE_COMMIT ||
649 obj->type == GOT_OBJ_TYPE_TAG) {
650 obj->valid = 1;
651 nloose++;
652 } else if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
653 have_ref_deltas = 1;
655 nvalid = nloose;
657 /* In order to resolve ref deltas we need an in-progress pack index. */
658 if (have_ref_deltas)
659 make_packidx(&packidx, nobj, objects);
661 /*
662 * Second pass: We can now resolve deltas to compute the IDs of
663 * objects which appear in deltified form. Because deltas can be
664 * chained this pass may require a couple of iterations until all
665 * IDs of deltified objects have been discovered.
666 */
667 pass++;
668 while (nvalid != nobj) {
669 int n = 0;
670 for (i = 0; i < nobj; i++) {
671 obj = &objects[i];
672 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
673 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
674 continue;
676 if (obj->valid)
677 continue;
679 if (pack->map == NULL &&
680 lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
681 == -1) {
682 err = got_error_from_errno("lseek");
683 goto done;
686 err = resolve_deltified_object(pack, &packidx, obj);
687 if (err) {
688 if (err->code != GOT_ERR_NO_OBJ)
689 goto done;
690 /*
691 * We cannot resolve this object yet because
692 * a delta base is unknown. Try again later.
693 */
694 continue;
697 obj->valid = 1;
698 n++;
699 if (have_ref_deltas)
700 update_packidx(&packidx, nobj, obj);
701 err = got_privsep_send_index_pack_progress(ibuf,
702 nobj, nobj, nloose, nresolved + n);
703 if (err)
704 goto done;
707 if (pass++ > 3 && n == 0) {
708 static char msg[64];
709 snprintf(msg, sizeof(msg), "could not resolve "
710 "any of deltas; packfile could be corrupt");
711 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
712 goto done;
715 if (nloose + nresolved == nobj) {
716 static char msg[64];
717 snprintf(msg, sizeof(msg),
718 "fix point reached too early: %d/%d/%d",
719 nvalid, nresolved, nobj);
720 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
721 goto done;
723 nresolved += n;
724 nvalid += nresolved;
727 if (nloose + nresolved != nobj) {
728 static char msg[64];
729 snprintf(msg, sizeof(msg),
730 "discovered only %d of %d objects",
731 nloose + nresolved, nobj);
732 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
733 goto done;
736 make_packidx(&packidx, nobj, objects);
738 SHA1Init(&ctx);
739 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
740 putbe32(buf + 4, GOT_PACKIDX_VERSION);
741 err = hwrite(idxfd, buf, 8, &ctx);
742 if (err)
743 goto done;
744 err = hwrite(idxfd, packidx.hdr.fanout_table,
745 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
746 if (err)
747 goto done;
748 err = hwrite(idxfd, packidx.hdr.sorted_ids,
749 nobj * SHA1_DIGEST_LENGTH, &ctx);
750 if (err)
751 goto done;
752 for(i = 0; i < nobj; i++){
753 putbe32(buf, objects[i].crc);
754 err = hwrite(idxfd, buf, 4, &ctx);
755 if (err)
756 goto done;
758 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
759 &ctx);
760 if (err)
761 goto done;
762 if (packidx.nlargeobj > 0) {
763 err = hwrite(idxfd, packidx.hdr.large_offsets,
764 packidx.nlargeobj * sizeof(uint64_t), &ctx);
765 if (err)
766 goto done;
768 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
769 if (err)
770 goto done;
772 SHA1Final(packidx_hash, &ctx);
773 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
774 if (w == -1) {
775 err = got_error_from_errno("write");
776 goto done;
778 if (w != sizeof(packidx_hash)) {
779 err = got_error(GOT_ERR_IO);
780 goto done;
782 done:
783 free(objects);
784 free(packidx.hdr.magic);
785 free(packidx.hdr.version);
786 free(packidx.hdr.fanout_table);
787 free(packidx.hdr.sorted_ids);
788 free(packidx.hdr.offsets);
789 free(packidx.hdr.large_offsets);
790 return err;
793 int
794 main(int argc, char **argv)
796 const struct got_error *err = NULL, *close_err;
797 struct imsgbuf ibuf;
798 struct imsg imsg;
799 int idxfd = -1;
800 struct got_pack pack;
801 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
802 off_t packfile_size;
803 #if 0
804 static int attached;
805 while (!attached)
806 sleep(1);
807 #endif
809 memset(&pack, 0, sizeof(pack));
810 pack.fd = -1;
811 pack.delta_cache = got_delta_cache_alloc(100,
812 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
813 if (pack.delta_cache == NULL) {
814 err = got_error_from_errno("got_delta_cache_alloc");
815 goto done;
818 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
819 #ifndef PROFILE
820 /* revoke access to most system calls */
821 if (pledge("stdio recvfd", NULL) == -1) {
822 err = got_error_from_errno("pledge");
823 got_privsep_send_error(&ibuf, err);
824 return 1;
826 #endif
827 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
828 if (err)
829 goto done;
830 if (imsg.hdr.type == GOT_IMSG_STOP)
831 goto done;
832 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
833 err = got_error(GOT_ERR_PRIVSEP_MSG);
834 goto done;
836 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
837 err = got_error(GOT_ERR_PRIVSEP_LEN);
838 goto done;
840 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
841 pack.fd = imsg.fd;
843 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
844 if (err)
845 goto done;
846 if (imsg.hdr.type == GOT_IMSG_STOP)
847 goto done;
848 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
849 err = got_error(GOT_ERR_PRIVSEP_MSG);
850 goto done;
852 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
853 err = got_error(GOT_ERR_PRIVSEP_LEN);
854 goto done;
856 idxfd = imsg.fd;
858 if (lseek(pack.fd, 0, SEEK_END) == -1) {
859 err = got_error_from_errno("lseek");
860 goto done;
862 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
863 if (packfile_size == -1) {
864 err = got_error_from_errno("lseek");
865 goto done;
867 pack.filesize = packfile_size; /* XXX off_t vs size_t */
869 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
870 err = got_error_from_errno("lseek");
871 goto done;
874 #ifndef GOT_PACK_NO_MMAP
875 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
876 pack.fd, 0);
877 if (pack.map == MAP_FAILED)
878 pack.map = NULL; /* fall back to read(2) */
879 #endif
880 err = index_pack(&pack, idxfd, pack_hash, &ibuf);
881 done:
882 close_err = got_pack_close(&pack);
883 if (close_err && err == NULL)
884 err = close_err;
885 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
886 err = got_error_from_errno("close");
888 if (err == NULL)
889 err = got_privsep_send_index_pack_done(&ibuf);
890 if (err) {
891 got_privsep_send_error(&ibuf, err);
892 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
893 got_privsep_send_error(&ibuf, err);
894 exit(1);
897 exit(0);