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 /* For ref deltas. */
79 struct got_object_id ref_id;
81 /* For offset deltas. */
82 off_t base_offset;
83 size_t base_offsetlen;
84 };
86 #define PUTBE32(b, n)\
87 do{ \
88 (b)[0] = (n) >> 24; \
89 (b)[1] = (n) >> 16; \
90 (b)[2] = (n) >> 8; \
91 (b)[3] = (n) >> 0; \
92 } while(0)
94 #define PUTBE64(b, n)\
95 do{ \
96 (b)[0] = (n) >> 56; \
97 (b)[1] = (n) >> 48; \
98 (b)[2] = (n) >> 40; \
99 (b)[3] = (n) >> 32; \
100 (b)[4] = (n) >> 24; \
101 (b)[5] = (n) >> 16; \
102 (b)[6] = (n) >> 8; \
103 (b)[7] = (n) >> 0; \
104 } while(0)
106 static const struct got_error *
107 get_obj_type_label(const char **label, int obj_type)
109 const struct got_error *err = NULL;
111 switch (obj_type) {
112 case GOT_OBJ_TYPE_BLOB:
113 *label = GOT_OBJ_LABEL_BLOB;
114 break;
115 case GOT_OBJ_TYPE_TREE:
116 *label = GOT_OBJ_LABEL_TREE;
117 break;
118 case GOT_OBJ_TYPE_COMMIT:
119 *label = GOT_OBJ_LABEL_COMMIT;
120 break;
121 case GOT_OBJ_TYPE_TAG:
122 *label = GOT_OBJ_LABEL_TAG;
123 break;
124 default:
125 *label = NULL;
126 err = got_error(GOT_ERR_OBJ_TYPE);
127 break;
130 return err;
134 static const struct got_error *
135 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
137 const struct got_error *err = NULL;
138 SHA1_CTX ctx;
139 uint8_t *data;
140 size_t datalen;
141 ssize_t n;
142 char *header;
143 size_t headerlen;
144 const char *obj_label;
146 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size, &obj->tslen,
147 pack, obj->off);
148 if (err)
149 return err;
151 switch (obj->type) {
152 case GOT_OBJ_TYPE_BLOB:
153 case GOT_OBJ_TYPE_COMMIT:
154 case GOT_OBJ_TYPE_TREE:
155 case GOT_OBJ_TYPE_TAG:
156 /* XXX TODO reading large objects into memory is bad! */
157 err = got_inflate_to_mem_fd(&data, &datalen, &obj->len, pack->fd);
158 if (err)
159 break;
160 SHA1Init(&ctx);
161 err = get_obj_type_label(&obj_label, obj->type);
162 if (err)
163 break;
164 if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
165 err = got_error_from_errno("asprintf");
166 free(data);
167 break;
169 headerlen = strlen(header) + 1;
170 SHA1Update(&ctx, header, headerlen);
171 SHA1Update(&ctx, data, datalen);
172 SHA1Final(obj->id.sha1, &ctx);
173 free(header);
174 free(data);
175 break;
176 case GOT_OBJ_TYPE_REF_DELTA:
177 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
178 n = read(pack->fd, &obj->ref_id.sha1, SHA1_DIGEST_LENGTH);
179 if (n == -1) {
180 err = got_error_from_errno("read");
181 break;
183 if (n < sizeof(obj->id)) {
184 err = got_error(GOT_ERR_BAD_PACKFILE);
185 break;
187 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len, pack->fd);
188 if (err)
189 break;
190 obj->len += SHA1_DIGEST_LENGTH;
191 break;
192 case GOT_OBJ_TYPE_OFFSET_DELTA:
193 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
194 err = got_pack_parse_offset_delta(&obj->base_offset,
195 &obj->base_offsetlen, pack, obj->off, obj->tslen);
196 if (err)
197 break;
198 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len, pack->fd);
199 if (err)
200 break;
201 obj->len += obj->base_offsetlen;
202 break;
203 default:
204 err = got_error(GOT_ERR_OBJ_TYPE);
205 break;
208 return err;
211 static const struct got_error *
212 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
214 ssize_t w;
216 SHA1Update(ctx, buf, len);
218 w = write(fd, buf, len);
219 if (w == -1)
220 return got_error_from_errno("write");
221 if (w != len)
222 return got_error(GOT_ERR_IO);
224 return NULL;
227 static const struct got_error *
228 object_crc(int packfd, struct got_indexed_object *obj)
230 char buf[8096];
231 size_t n;
232 ssize_t r;
234 obj->crc = 0;
235 if (lseek(packfd, obj->off + obj->tslen, SEEK_SET) == -1)
236 return got_error_from_errno("lseek");
238 obj->crc = crc32(0L, NULL, 0);
239 for (n = obj->len; n > 0; n -= r){
240 r = read(packfd, buf, n > sizeof(buf) ? sizeof(buf) : n);
241 if (r == -1)
242 return got_error_from_errno("read");
243 if (r == 0)
244 return NULL;
245 obj->crc = crc32(obj->crc, buf, r);
247 return 0;
250 #if 0
251 static int
252 indexed_obj_cmp(const void *pa, const void *pb)
254 struct got_indexed_object *a, *b;
256 a = *(struct got_indexed_object **)pa;
257 b = *(struct got_indexed_object **)pb;
258 return got_object_id_cmp(&a->id, &b->id);
260 #endif
262 static const struct got_error *
263 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
264 struct got_indexed_object *obj)
266 const struct got_error *err = NULL;
267 struct got_delta_chain deltas;
268 struct got_delta *delta;
269 uint8_t *buf = NULL;
270 size_t len;
271 SHA1_CTX ctx;
272 char *header;
273 size_t headerlen;
274 int base_obj_type;
275 const char *obj_label;
277 deltas.nentries = 0;
278 SIMPLEQ_INIT(&deltas.entries);
280 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
281 obj->off, obj->tslen, obj->type, obj->size,
282 GOT_DELTA_CHAIN_RECURSION_MAX);
283 if (err)
284 goto done;
286 /* XXX TODO reading large objects into memory is bad! */
287 err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
288 if (err)
289 goto done;
291 SHA1Init(&ctx);
293 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
294 if (err)
295 goto done;
296 err = get_obj_type_label(&obj_label, base_obj_type);
297 if (err)
298 goto done;
299 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
300 err = got_error_from_errno("asprintf");
301 goto done;
303 headerlen = strlen(header) + 1;
304 SHA1Update(&ctx, header, headerlen);
305 SHA1Update(&ctx, buf, len);
306 SHA1Final(obj->id.sha1, &ctx);
307 done:
308 free(buf);
309 while (!SIMPLEQ_EMPTY(&deltas.entries)) {
310 delta = SIMPLEQ_FIRST(&deltas.entries);
311 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
312 free(delta);
314 return err;
317 /* Determine the slot in the pack index a given object ID should use. */
318 static int
319 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
321 u_int8_t id0 = sha1[0];
322 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
323 int left = 0, right = nindexed - 1;
324 int cmp = 0, i = 0;
326 if (id0 > 0)
327 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
329 while (left <= right) {
330 struct got_packidx_object_id *oid;
332 i = ((left + right) / 2);
333 oid = &packidx->hdr.sorted_ids[i];
335 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
336 if (cmp == 0)
337 return -1; /* object already indexed */
338 else if (cmp > 0)
339 left = i + 1;
340 else if (cmp < 0)
341 right = i - 1;
344 return left;
347 #if 0
348 static void
349 print_packidx(struct got_packidx *packidx)
351 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
352 int i;
354 fprintf(stderr, "object IDs:\n");
355 for (i = 0; i < nindexed; i++) {
356 char hex[SHA1_DIGEST_STRING_LENGTH];
357 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
358 hex, sizeof(hex));
359 fprintf(stderr, "%s\n", hex);
361 fprintf(stderr, "\n");
363 fprintf(stderr, "object offsets:\n");
364 for (i = 0; i < nindexed; i++) {
365 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
366 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
367 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
368 fprintf(stderr, "%u -> %llu\n", offset,
369 be64toh(packidx->hdr.large_offsets[j]));
370 } else
371 fprintf(stderr, "%u\n", offset);
373 fprintf(stderr, "\n");
375 fprintf(stderr, "fanout table:");
376 for (i = 0; i <= 0xff; i++)
377 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
378 fprintf(stderr, "\n");
380 #endif
382 static void
383 update_packidx(int *nlarge, struct got_packidx *packidx, int nobj,
384 struct got_indexed_object *obj)
386 int i, n, idx;
387 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
389 idx = find_object_idx(packidx, obj->id.sha1);
390 if (idx == -1) {
391 char hex[SHA1_DIGEST_STRING_LENGTH];
392 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
393 return; /* object already indexed */
396 memmove(&packidx->hdr.sorted_ids[idx + 1],
397 &packidx->hdr.sorted_ids[idx],
398 sizeof(struct got_packidx_object_id) * (nindexed - idx));
399 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
400 sizeof(uint32_t) * (nindexed - idx));
402 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
403 SHA1_DIGEST_LENGTH);
404 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
405 packidx->hdr.offsets[idx] = htobe32(obj->off);
406 else {
407 packidx->hdr.offsets[idx] = htobe32(*nlarge |
408 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
409 packidx->hdr.large_offsets[*nlarge] = htobe64(obj->off);
410 (*nlarge)++;
413 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
414 n = be32toh(packidx->hdr.fanout_table[i]);
415 packidx->hdr.fanout_table[i] = htobe32(n + 1);
419 static const struct got_error *
420 index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
421 struct imsgbuf *ibuf)
423 const struct got_error *err;
424 struct got_packfile_hdr hdr;
425 struct got_packidx packidx;
426 char buf[8];
427 int nobj, nvalid, nloose, nlarge = 0, nresolved = 0, i;
428 struct got_indexed_object **objects = NULL, *obj;
429 SHA1_CTX ctx;
430 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
431 ssize_t r, w;
432 int pass;
434 /* Check pack file header. */
435 r = read(pack->fd, &hdr, sizeof(hdr));
436 if (r == -1)
437 return got_error_from_errno("read");
438 if (r < sizeof(hdr))
439 return got_error_msg(GOT_ERR_BAD_PACKFILE,
440 "short packfile header");
442 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
443 return got_error_msg(GOT_ERR_BAD_PACKFILE,
444 "bad packfile signature");
445 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
446 return got_error_msg(GOT_ERR_BAD_PACKFILE,
447 "bad packfile version");
448 nobj = betoh32(hdr.nobjects);
449 if (nobj == 0)
450 return got_error_msg(GOT_ERR_BAD_PACKFILE,
451 "bad packfile with zero objects");
453 /*
454 * Create an in-memory pack index which will grow as objects
455 * IDs in the pack file are discovered. Only fields used to
456 * read deltified objects will be needed by the pack.c library
457 * code, so setting up just a pack index header is sufficient.
458 */
459 memset(&packidx, 0, sizeof(packidx));
460 packidx.hdr.magic = malloc(sizeof(uint32_t));
461 if (packidx.hdr.magic == NULL)
462 return got_error_from_errno("calloc");
463 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
464 packidx.hdr.version = malloc(sizeof(uint32_t));
465 if (packidx.hdr.version == NULL) {
466 err = got_error_from_errno("malloc");
467 goto done;
469 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
470 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
471 sizeof(uint32_t));
472 if (packidx.hdr.fanout_table == NULL) {
473 err = got_error_from_errno("calloc");
474 goto done;
476 packidx.hdr.sorted_ids = calloc(nobj,
477 sizeof(struct got_packidx_object_id));
478 if (packidx.hdr.sorted_ids == NULL) {
479 err = got_error_from_errno("calloc");
480 goto done;
482 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
483 if (packidx.hdr.offsets == NULL) {
484 err = got_error_from_errno("calloc");
485 goto done;
487 /* Large offsets table is empty for pack files < 2 GB. */
488 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
489 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
490 if (packidx.hdr.large_offsets == NULL) {
491 err = got_error_from_errno("calloc");
492 goto done;
496 nvalid = 0;
497 nloose = 0;
498 objects = calloc(nobj, sizeof(struct got_indexed_object *));
499 if (objects == NULL)
500 return got_error_from_errno("calloc");
502 /*
503 * First pass: locate all objects and identify un-deltified objects.
505 * When this pass has completed we will know offset, type, size, and
506 * CRC information for all objects in this pack file. We won't know
507 * any of the actual object IDs of deltified objects yet since we
508 * will not yet attempt to combine deltas.
509 */
510 pass = 1;
511 for (i = 0; i < nobj; i++) {
512 err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
513 nloose, 0);
514 if (err)
515 goto done;
517 obj = calloc(1, sizeof(*obj));
518 if (obj == NULL) {
519 err = got_error_from_errno("calloc");
520 goto done;
523 /* Store offset to type+size information for this object. */
524 obj->off = lseek(pack->fd, 0, SEEK_CUR);
525 if (obj->off == -1) {
526 err = got_error_from_errno("lseek");
527 goto done;
530 err = read_packed_object(pack, obj);
531 if (err)
532 goto done;
534 objects[i] = obj;
536 if (0) {
537 err = object_crc(pack->fd, obj);
538 if (err)
539 goto done;
542 if (obj->type == GOT_OBJ_TYPE_BLOB ||
543 obj->type == GOT_OBJ_TYPE_TREE ||
544 obj->type == GOT_OBJ_TYPE_COMMIT ||
545 obj->type == GOT_OBJ_TYPE_TAG) {
546 objects[i]->valid = 1;
547 nloose++;
548 update_packidx(&nlarge, &packidx, nobj, obj);
551 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
552 SEEK_SET) == -1) {
553 err = got_error_from_errno("lseek");
554 goto done;
557 nvalid = nloose;
559 /*
560 * Second pass: We can now resolve deltas to compute the IDs of
561 * objects which appear in deltified form. Because deltas can be
562 * chained this pass may require a couple of iterations until all
563 * IDs of deltified objects have been discovered.
564 */
565 pass++;
566 while (nvalid != nobj) {
567 int n = 0;
568 for (i = 0; i < nobj; i++) {
569 if (objects[i]->type != GOT_OBJ_TYPE_REF_DELTA &&
570 objects[i]->type != GOT_OBJ_TYPE_OFFSET_DELTA)
571 continue;
573 if (objects[i]->valid)
574 continue;
576 obj = objects[i];
577 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET) == -1) {
578 err = got_error_from_errno("lseek");
579 goto done;
582 err = resolve_deltified_object(pack, &packidx, obj);
583 if (err) {
584 if (err->code != GOT_ERR_NO_OBJ)
585 goto done;
586 /*
587 * We cannot resolve this object yet because
588 * a delta base is unknown. Try again later.
589 */
590 continue;
593 objects[i]->valid = 1;
594 n++;
595 update_packidx(&nlarge, &packidx, nobj, obj);
596 err = got_privsep_send_index_pack_progress(ibuf, nobj, nobj,
597 nloose, nresolved + n);
598 if (err)
599 goto done;
602 if (pass++ > 3 && n == 0) {
603 static char msg[64];
604 snprintf(msg, sizeof(msg), "could not resolve "
605 "any of deltas; packfile could be corrupt");
606 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
607 goto done;
610 if (nloose + nresolved == nobj) {
611 static char msg[64];
612 snprintf(msg, sizeof(msg),
613 "fix point reached too early: %d/%d/%d", nvalid, nresolved, nobj);
614 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
615 goto done;
617 nresolved += n;
618 nvalid += nresolved;
621 if (nloose + nresolved != nobj) {
622 static char msg[64];
623 snprintf(msg, sizeof(msg),
624 "discovered only %d of %d objects", nloose + nresolved, nobj);
625 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
626 goto done;
629 /* We may have seen duplicates. Update our total object count. */
630 nobj = betoh32(packidx.hdr.fanout_table[0xff]);
632 SHA1Init(&ctx);
633 err = hwrite(idxfd, "\xfftOc\x00\x00\x00\x02", 8, &ctx);
634 if (err)
635 goto done;
636 err = hwrite(idxfd, packidx.hdr.fanout_table,
637 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
638 if (err)
639 goto done;
640 err = hwrite(idxfd, packidx.hdr.sorted_ids,
641 nobj * SHA1_DIGEST_LENGTH, &ctx);
642 if (err)
643 goto done;
644 for(i = 0; i < nobj; i++){
645 PUTBE32(buf, objects[i]->crc);
646 err = hwrite(idxfd, buf, 4, &ctx);
647 if (err)
648 goto done;
650 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t), &ctx);
651 if (err)
652 goto done;
653 if (nlarge > 0) {
654 err = hwrite(idxfd, packidx.hdr.large_offsets,
655 nlarge * sizeof(uint64_t), &ctx);
656 if (err)
657 goto done;
659 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
660 if (err)
661 goto done;
663 SHA1Final(packidx_hash, &ctx);
664 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
665 if (w == -1) {
666 err = got_error_from_errno("write");
667 goto done;
669 if (w != sizeof(packidx_hash)) {
670 err = got_error(GOT_ERR_IO);
671 goto done;
673 done:
674 free(packidx.hdr.magic);
675 free(packidx.hdr.version);
676 free(packidx.hdr.fanout_table);
677 free(packidx.hdr.sorted_ids);
678 free(packidx.hdr.offsets);
679 free(packidx.hdr.large_offsets);
680 return err;
683 int
684 main(int argc, char **argv)
686 const struct got_error *err = NULL, *close_err;
687 struct imsgbuf ibuf;
688 struct imsg imsg;
689 int idxfd = -1;
690 struct got_pack pack;
691 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
692 off_t packfile_size;
693 #if 0
694 static int attached;
695 while (!attached)
696 sleep(1);
697 #endif
699 memset(&pack, 0, sizeof(pack));
700 pack.fd = -1;
701 pack.delta_cache = got_delta_cache_alloc(100,
702 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
703 if (pack.delta_cache == NULL) {
704 err = got_error_from_errno("got_delta_cache_alloc");
705 goto done;
708 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
710 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
711 if (err)
712 goto done;
713 if (imsg.hdr.type == GOT_IMSG_STOP)
714 goto done;
715 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
716 err = got_error(GOT_ERR_PRIVSEP_MSG);
717 goto done;
719 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
720 err = got_error(GOT_ERR_PRIVSEP_LEN);
721 goto done;
723 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
724 pack.fd = imsg.fd;
726 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
727 if (err)
728 goto done;
729 if (imsg.hdr.type == GOT_IMSG_STOP)
730 goto done;
731 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
732 err = got_error(GOT_ERR_PRIVSEP_MSG);
733 goto done;
735 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
736 err = got_error(GOT_ERR_PRIVSEP_LEN);
737 goto done;
739 idxfd = imsg.fd;
741 if (lseek(pack.fd, 0, SEEK_END) == -1) {
742 err = got_error_from_errno("lseek");
743 goto done;
745 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
746 if (packfile_size == -1) {
747 err = got_error_from_errno("lseek");
748 goto done;
750 pack.filesize = packfile_size; /* XXX off_t vs size_t */
752 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
753 err = got_error_from_errno("lseek");
754 goto done;
757 err = index_pack(&pack, idxfd, pack_hash, &ibuf);
758 done:
759 close_err = got_pack_close(&pack);
760 if (close_err && err == NULL)
761 err = close_err;
762 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
763 err = got_error_from_errno("close");
765 if (err == NULL)
766 err = got_privsep_send_index_pack_done(&ibuf);
767 if (err) {
768 got_privsep_send_error(&ibuf, err);
769 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
770 got_privsep_send_error(&ibuf, err);
771 exit(1);
774 exit(0);