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>
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 <fcntl.h>
36 #include <zlib.h>
37 #include <err.h>
38 #include <assert.h>
39 #include <dirent.h>
41 #include "got_error.h"
42 #include "got_object.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_object_idset.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_delta_cache.h"
54 struct got_indexed_object {
55 struct got_object_id id;
57 /*
58 * Has this object been fully resolved?
59 * If so, we know its ID, otherwise we don't and 'id' is invalid.
60 */
61 int valid;
63 /* Offset of type+size field for this object in pack file. */
64 off_t off;
66 /* Type+size values parsed from pack file. */
67 uint8_t type;
68 uint64_t size;
70 /* Length of on-disk type+size data. */
71 size_t tslen;
73 /* Length of object data following type+size. */
74 size_t len;
76 uint32_t crc;
78 union {
79 struct {
80 /* For ref deltas. */
81 struct got_object_id ref_id;
82 } ref;
83 struct {
84 /* For offset deltas. */
85 off_t base_offset;
86 size_t base_offsetlen;
87 } ofs;
88 } delta;
89 };
91 #define PUTBE32(b, n)\
92 do{ \
93 (b)[0] = (n) >> 24; \
94 (b)[1] = (n) >> 16; \
95 (b)[2] = (n) >> 8; \
96 (b)[3] = (n) >> 0; \
97 } while(0)
99 #define PUTBE64(b, n)\
100 do{ \
101 (b)[0] = (n) >> 56; \
102 (b)[1] = (n) >> 48; \
103 (b)[2] = (n) >> 40; \
104 (b)[3] = (n) >> 32; \
105 (b)[4] = (n) >> 24; \
106 (b)[5] = (n) >> 16; \
107 (b)[6] = (n) >> 8; \
108 (b)[7] = (n) >> 0; \
109 } while(0)
111 static const struct got_error *
112 get_obj_type_label(const char **label, int obj_type)
114 const struct got_error *err = NULL;
116 switch (obj_type) {
117 case GOT_OBJ_TYPE_BLOB:
118 *label = GOT_OBJ_LABEL_BLOB;
119 break;
120 case GOT_OBJ_TYPE_TREE:
121 *label = GOT_OBJ_LABEL_TREE;
122 break;
123 case GOT_OBJ_TYPE_COMMIT:
124 *label = GOT_OBJ_LABEL_COMMIT;
125 break;
126 case GOT_OBJ_TYPE_TAG:
127 *label = GOT_OBJ_LABEL_TAG;
128 break;
129 default:
130 *label = NULL;
131 err = got_error(GOT_ERR_OBJ_TYPE);
132 break;
135 return err;
138 static const struct got_error *
139 read_crc(uint32_t *crc, int fd, size_t len)
141 uint8_t buf[8192];
142 size_t n;
143 ssize_t r;
145 for (n = len; n > 0; n -= r){
146 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
147 if (r == -1)
148 return got_error_from_errno("read");
149 if (r == 0)
150 break;
151 *crc = crc32(*crc, buf, r);
154 return NULL;
157 static const struct got_error *
158 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
160 const struct got_error *err = NULL;
161 SHA1_CTX ctx;
162 uint8_t *data;
163 size_t datalen;
164 ssize_t n;
165 char *header;
166 size_t headerlen;
167 const char *obj_label;
169 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
170 &obj->tslen, pack, obj->off);
171 if (err)
172 return err;
174 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
175 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
176 return got_error_from_errno("lseek");
177 err = read_crc(&obj->crc, pack->fd, obj->tslen);
178 if (err)
179 return err;
181 switch (obj->type) {
182 case GOT_OBJ_TYPE_BLOB:
183 case GOT_OBJ_TYPE_COMMIT:
184 case GOT_OBJ_TYPE_TREE:
185 case GOT_OBJ_TYPE_TAG:
186 /* XXX TODO reading large objects into memory is bad! */
187 err = got_inflate_to_mem_fd(&data, &datalen, &obj->len,
188 &obj->crc, obj->size, pack->fd);
189 if (err)
190 break;
191 SHA1Init(&ctx);
192 err = get_obj_type_label(&obj_label, obj->type);
193 if (err) {
194 free(data);
195 break;
197 if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
198 err = got_error_from_errno("asprintf");
199 free(data);
200 break;
202 headerlen = strlen(header) + 1;
203 SHA1Update(&ctx, header, headerlen);
204 SHA1Update(&ctx, data, datalen);
205 SHA1Final(obj->id.sha1, &ctx);
206 free(header);
207 free(data);
208 break;
209 case GOT_OBJ_TYPE_REF_DELTA:
210 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
211 n = read(pack->fd, &obj->delta.ref.ref_id.sha1,
212 SHA1_DIGEST_LENGTH);
213 if (n == -1) {
214 err = got_error_from_errno("read");
215 break;
217 if (n < sizeof(obj->id)) {
218 err = got_error(GOT_ERR_BAD_PACKFILE);
219 break;
221 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
222 SHA1_DIGEST_LENGTH);
223 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
224 &obj->crc, obj->size, pack->fd);
225 if (err)
226 break;
227 obj->len += SHA1_DIGEST_LENGTH;
228 break;
229 case GOT_OBJ_TYPE_OFFSET_DELTA:
230 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
231 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
232 &obj->delta.ofs.base_offsetlen, pack, obj->off,
233 obj->tslen);
234 if (err)
235 break;
237 /* XXX Seek back and get the CRC of on-disk offset bytes. */
238 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET) == -1) {
239 err = got_error_from_errno("lseek");
240 break;
242 err = read_crc(&obj->crc, pack->fd,
243 obj->delta.ofs.base_offsetlen);
244 if (err)
245 break;
247 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
248 &obj->crc, obj->size, pack->fd);
249 if (err)
250 break;
251 obj->len += obj->delta.ofs.base_offsetlen;
252 break;
253 default:
254 err = got_error(GOT_ERR_OBJ_TYPE);
255 break;
258 return err;
261 static const struct got_error *
262 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
264 ssize_t w;
266 SHA1Update(ctx, buf, len);
268 w = write(fd, buf, len);
269 if (w == -1)
270 return got_error_from_errno("write");
271 if (w != len)
272 return got_error(GOT_ERR_IO);
274 return NULL;
277 static const struct got_error *
278 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
279 struct got_indexed_object *obj)
281 const struct got_error *err = NULL;
282 struct got_delta_chain deltas;
283 struct got_delta *delta;
284 uint8_t *buf = NULL;
285 size_t len;
286 SHA1_CTX ctx;
287 char *header;
288 size_t headerlen;
289 int base_obj_type;
290 const char *obj_label;
292 deltas.nentries = 0;
293 SIMPLEQ_INIT(&deltas.entries);
295 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
296 obj->off, obj->tslen, obj->type, obj->size,
297 GOT_DELTA_CHAIN_RECURSION_MAX);
298 if (err)
299 goto done;
301 /* XXX TODO reading large objects into memory is bad! */
302 err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
303 if (err)
304 goto done;
306 SHA1Init(&ctx);
308 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
309 if (err)
310 goto done;
311 err = get_obj_type_label(&obj_label, base_obj_type);
312 if (err)
313 goto done;
314 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
315 err = got_error_from_errno("asprintf");
316 goto done;
318 headerlen = strlen(header) + 1;
319 SHA1Update(&ctx, header, headerlen);
320 SHA1Update(&ctx, buf, len);
321 SHA1Final(obj->id.sha1, &ctx);
322 done:
323 free(buf);
324 while (!SIMPLEQ_EMPTY(&deltas.entries)) {
325 delta = SIMPLEQ_FIRST(&deltas.entries);
326 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
327 free(delta);
329 return err;
332 /* Determine the slot in the pack index a given object ID should use. */
333 static int
334 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
336 u_int8_t id0 = sha1[0];
337 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
338 int left = 0, right = nindexed - 1;
339 int cmp = 0, i = 0;
341 if (id0 > 0)
342 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
344 while (left <= right) {
345 struct got_packidx_object_id *oid;
347 i = ((left + right) / 2);
348 oid = &packidx->hdr.sorted_ids[i];
350 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
351 if (cmp == 0)
352 return -1; /* object already indexed */
353 else if (cmp > 0)
354 left = i + 1;
355 else if (cmp < 0)
356 right = i - 1;
359 return left;
362 #if 0
363 static void
364 print_packidx(struct got_packidx *packidx)
366 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
367 int i;
369 fprintf(stderr, "object IDs:\n");
370 for (i = 0; i < nindexed; i++) {
371 char hex[SHA1_DIGEST_STRING_LENGTH];
372 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
373 hex, sizeof(hex));
374 fprintf(stderr, "%s\n", hex);
376 fprintf(stderr, "\n");
378 fprintf(stderr, "object offsets:\n");
379 for (i = 0; i < nindexed; i++) {
380 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
381 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
382 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
383 fprintf(stderr, "%u -> %llu\n", offset,
384 be64toh(packidx->hdr.large_offsets[j]));
385 } else
386 fprintf(stderr, "%u\n", offset);
388 fprintf(stderr, "\n");
390 fprintf(stderr, "fanout table:");
391 for (i = 0; i <= 0xff; i++)
392 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
393 fprintf(stderr, "\n");
395 #endif
397 static void
398 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
399 struct got_indexed_object *obj)
401 int i;
403 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
404 SHA1_DIGEST_LENGTH);
405 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
406 packidx->hdr.offsets[idx] = htobe32(obj->off);
407 else {
408 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
409 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
410 packidx->hdr.large_offsets[packidx->nlargeobj] =
411 htobe64(obj->off);
412 packidx->nlargeobj++;
415 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
416 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
417 packidx->hdr.fanout_table[i] = htobe32(n + 1);
421 static int
422 indexed_obj_cmp(const void *pa, const void *pb)
424 struct got_indexed_object *a, *b;
426 a = *(struct got_indexed_object **)pa;
427 b = *(struct got_indexed_object **)pb;
428 return got_object_id_cmp(&a->id, &b->id);
431 static void
432 make_packidx(struct got_packidx *packidx, int nobj,
433 struct got_indexed_object **objects)
435 struct got_indexed_object *obj;
436 int i;
437 uint32_t idx = 0;
439 mergesort(objects, nobj, sizeof(struct got_indexed_object *),
440 indexed_obj_cmp);
442 memset(packidx->hdr.fanout_table, 0,
443 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
444 packidx->nlargeobj = 0;
446 for (i = 0; i < nobj; i++) {
447 obj = objects[i];
448 if (obj->valid)
449 add_indexed_object(packidx, idx++, obj);
453 static void
454 update_packidx(struct got_packidx *packidx, int nobj,
455 struct got_indexed_object *obj)
457 uint32_t idx;
458 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
460 idx = find_object_idx(packidx, obj->id.sha1);
461 if (idx == -1) {
462 char hex[SHA1_DIGEST_STRING_LENGTH];
463 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
464 return; /* object already indexed */
467 memmove(&packidx->hdr.sorted_ids[idx + 1],
468 &packidx->hdr.sorted_ids[idx],
469 sizeof(struct got_packidx_object_id) * (nindexed - idx));
470 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
471 sizeof(uint32_t) * (nindexed - idx));
473 add_indexed_object(packidx, idx, obj);
476 static const struct got_error *
477 index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
478 struct imsgbuf *ibuf)
480 const struct got_error *err;
481 struct got_packfile_hdr hdr;
482 struct got_packidx packidx;
483 char buf[8];
484 int nobj, nvalid, nloose, nresolved = 0, i;
485 struct got_indexed_object **objects = NULL, *obj;
486 SHA1_CTX ctx;
487 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
488 ssize_t r, w;
489 int pass, have_ref_deltas = 0;
491 /* Check pack file header. */
492 r = read(pack->fd, &hdr, sizeof(hdr));
493 if (r == -1)
494 return got_error_from_errno("read");
495 if (r < sizeof(hdr))
496 return got_error_msg(GOT_ERR_BAD_PACKFILE,
497 "short packfile header");
499 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
500 return got_error_msg(GOT_ERR_BAD_PACKFILE,
501 "bad packfile signature");
502 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
503 return got_error_msg(GOT_ERR_BAD_PACKFILE,
504 "bad packfile version");
505 nobj = betoh32(hdr.nobjects);
506 if (nobj == 0)
507 return got_error_msg(GOT_ERR_BAD_PACKFILE,
508 "bad packfile with zero objects");
510 /*
511 * Create an in-memory pack index which will grow as objects
512 * IDs in the pack file are discovered. Only fields used to
513 * read deltified objects will be needed by the pack.c library
514 * code, so setting up just a pack index header is sufficient.
515 */
516 memset(&packidx, 0, sizeof(packidx));
517 packidx.hdr.magic = malloc(sizeof(uint32_t));
518 if (packidx.hdr.magic == NULL)
519 return got_error_from_errno("calloc");
520 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
521 packidx.hdr.version = malloc(sizeof(uint32_t));
522 if (packidx.hdr.version == NULL) {
523 err = got_error_from_errno("malloc");
524 goto done;
526 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
527 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
528 sizeof(uint32_t));
529 if (packidx.hdr.fanout_table == NULL) {
530 err = got_error_from_errno("calloc");
531 goto done;
533 packidx.hdr.sorted_ids = calloc(nobj,
534 sizeof(struct got_packidx_object_id));
535 if (packidx.hdr.sorted_ids == NULL) {
536 err = got_error_from_errno("calloc");
537 goto done;
539 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
540 if (packidx.hdr.offsets == NULL) {
541 err = got_error_from_errno("calloc");
542 goto done;
544 /* Large offsets table is empty for pack files < 2 GB. */
545 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
546 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
547 if (packidx.hdr.large_offsets == NULL) {
548 err = got_error_from_errno("calloc");
549 goto done;
553 nvalid = 0;
554 nloose = 0;
555 objects = calloc(nobj, sizeof(struct got_indexed_object *));
556 if (objects == NULL)
557 return got_error_from_errno("calloc");
559 /*
560 * First pass: locate all objects and identify un-deltified objects.
562 * When this pass has completed we will know offset, type, size, and
563 * CRC information for all objects in this pack file. We won't know
564 * any of the actual object IDs of deltified objects yet since we
565 * will not yet attempt to combine deltas.
566 */
567 pass = 1;
568 for (i = 0; i < nobj; i++) {
569 err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
570 nloose, 0);
571 if (err)
572 goto done;
574 obj = calloc(1, sizeof(*obj));
575 if (obj == NULL) {
576 err = got_error_from_errno("calloc");
577 goto done;
579 obj->crc = crc32(0L, NULL, 0);
581 /* Store offset to type+size information for this object. */
582 obj->off = lseek(pack->fd, 0, SEEK_CUR);
583 if (obj->off == -1) {
584 err = got_error_from_errno("lseek");
585 goto done;
588 err = read_packed_object(pack, obj);
589 if (err)
590 goto done;
592 objects[i] = obj;
594 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
595 SEEK_SET) == -1) {
596 err = got_error_from_errno("lseek");
597 goto done;
600 if (obj->type == GOT_OBJ_TYPE_BLOB ||
601 obj->type == GOT_OBJ_TYPE_TREE ||
602 obj->type == GOT_OBJ_TYPE_COMMIT ||
603 obj->type == GOT_OBJ_TYPE_TAG) {
604 objects[i]->valid = 1;
605 nloose++;
606 } else if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
607 have_ref_deltas = 1;
609 nvalid = nloose;
611 /* In order to resolve ref deltas we need an in-progress pack index. */
612 if (have_ref_deltas)
613 make_packidx(&packidx, nobj, objects);
615 /*
616 * Second pass: We can now resolve deltas to compute the IDs of
617 * objects which appear in deltified form. Because deltas can be
618 * chained this pass may require a couple of iterations until all
619 * IDs of deltified objects have been discovered.
620 */
621 pass++;
622 while (nvalid != nobj) {
623 int n = 0;
624 for (i = 0; i < nobj; i++) {
625 if (objects[i]->type != GOT_OBJ_TYPE_REF_DELTA &&
626 objects[i]->type != GOT_OBJ_TYPE_OFFSET_DELTA)
627 continue;
629 if (objects[i]->valid)
630 continue;
632 obj = objects[i];
633 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
634 == -1) {
635 err = got_error_from_errno("lseek");
636 goto done;
639 err = resolve_deltified_object(pack, &packidx, obj);
640 if (err) {
641 if (err->code != GOT_ERR_NO_OBJ)
642 goto done;
643 /*
644 * We cannot resolve this object yet because
645 * a delta base is unknown. Try again later.
646 */
647 continue;
650 objects[i]->valid = 1;
651 n++;
652 if (have_ref_deltas)
653 update_packidx(&packidx, nobj, obj);
654 err = got_privsep_send_index_pack_progress(ibuf,
655 nobj, nobj, nloose, nresolved + n);
656 if (err)
657 goto done;
660 if (pass++ > 3 && n == 0) {
661 static char msg[64];
662 snprintf(msg, sizeof(msg), "could not resolve "
663 "any of deltas; packfile could be corrupt");
664 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
665 goto done;
668 if (nloose + nresolved == nobj) {
669 static char msg[64];
670 snprintf(msg, sizeof(msg),
671 "fix point reached too early: %d/%d/%d",
672 nvalid, nresolved, nobj);
673 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
674 goto done;
676 nresolved += n;
677 nvalid += nresolved;
680 if (nloose + nresolved != nobj) {
681 static char msg[64];
682 snprintf(msg, sizeof(msg),
683 "discovered only %d of %d objects",
684 nloose + nresolved, nobj);
685 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
686 goto done;
689 make_packidx(&packidx, nobj, objects);
691 SHA1Init(&ctx);
692 err = hwrite(idxfd, "\xfftOc\x00\x00\x00\x02", 8, &ctx);
693 if (err)
694 goto done;
695 err = hwrite(idxfd, packidx.hdr.fanout_table,
696 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
697 if (err)
698 goto done;
699 err = hwrite(idxfd, packidx.hdr.sorted_ids,
700 nobj * SHA1_DIGEST_LENGTH, &ctx);
701 if (err)
702 goto done;
703 for(i = 0; i < nobj; i++){
704 PUTBE32(buf, objects[i]->crc);
705 err = hwrite(idxfd, buf, 4, &ctx);
706 if (err)
707 goto done;
709 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
710 &ctx);
711 if (err)
712 goto done;
713 if (packidx.nlargeobj > 0) {
714 err = hwrite(idxfd, packidx.hdr.large_offsets,
715 packidx.nlargeobj * sizeof(uint64_t), &ctx);
716 if (err)
717 goto done;
719 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
720 if (err)
721 goto done;
723 SHA1Final(packidx_hash, &ctx);
724 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
725 if (w == -1) {
726 err = got_error_from_errno("write");
727 goto done;
729 if (w != sizeof(packidx_hash)) {
730 err = got_error(GOT_ERR_IO);
731 goto done;
733 done:
734 free(packidx.hdr.magic);
735 free(packidx.hdr.version);
736 free(packidx.hdr.fanout_table);
737 free(packidx.hdr.sorted_ids);
738 free(packidx.hdr.offsets);
739 free(packidx.hdr.large_offsets);
740 return err;
743 int
744 main(int argc, char **argv)
746 const struct got_error *err = NULL, *close_err;
747 struct imsgbuf ibuf;
748 struct imsg imsg;
749 int idxfd = -1;
750 struct got_pack pack;
751 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
752 off_t packfile_size;
753 #if 0
754 static int attached;
755 while (!attached)
756 sleep(1);
757 #endif
759 memset(&pack, 0, sizeof(pack));
760 pack.fd = -1;
761 pack.delta_cache = got_delta_cache_alloc(100,
762 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
763 if (pack.delta_cache == NULL) {
764 err = got_error_from_errno("got_delta_cache_alloc");
765 goto done;
768 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
770 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
771 if (err)
772 goto done;
773 if (imsg.hdr.type == GOT_IMSG_STOP)
774 goto done;
775 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
776 err = got_error(GOT_ERR_PRIVSEP_MSG);
777 goto done;
779 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
780 err = got_error(GOT_ERR_PRIVSEP_LEN);
781 goto done;
783 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
784 pack.fd = imsg.fd;
786 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
787 if (err)
788 goto done;
789 if (imsg.hdr.type == GOT_IMSG_STOP)
790 goto done;
791 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
793 goto done;
795 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
796 err = got_error(GOT_ERR_PRIVSEP_LEN);
797 goto done;
799 idxfd = imsg.fd;
801 if (lseek(pack.fd, 0, SEEK_END) == -1) {
802 err = got_error_from_errno("lseek");
803 goto done;
805 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
806 if (packfile_size == -1) {
807 err = got_error_from_errno("lseek");
808 goto done;
810 pack.filesize = packfile_size; /* XXX off_t vs size_t */
812 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
813 err = got_error_from_errno("lseek");
814 goto done;
817 err = index_pack(&pack, idxfd, pack_hash, &ibuf);
818 done:
819 close_err = got_pack_close(&pack);
820 if (close_err && err == NULL)
821 err = close_err;
822 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
823 err = got_error_from_errno("close");
825 if (err == NULL)
826 err = got_privsep_send_index_pack_done(&ibuf);
827 if (err) {
828 got_privsep_send_error(&ibuf, err);
829 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
830 got_privsep_send_error(&ibuf, err);
831 exit(1);
834 exit(0);