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 send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
576 int nobj_indexed, int nobj_loose, int nobj_resolved)
578 struct got_imsg_index_pack_progress iprogress;
580 iprogress.nobj_total = nobj_total;
581 iprogress.nobj_indexed = nobj_indexed;
582 iprogress.nobj_loose = nobj_loose;
583 iprogress.nobj_resolved = nobj_resolved;
585 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
586 &iprogress, sizeof(iprogress)) == -1)
587 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
589 return got_privsep_flush_imsg(ibuf);
592 static const struct got_error *
593 send_index_pack_done(struct imsgbuf *ibuf)
595 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
596 return got_error_from_errno("imsg_compose FETCH");
597 return got_privsep_flush_imsg(ibuf);
601 static const struct got_error *
602 index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
603 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_hash,
604 struct imsgbuf *ibuf)
606 const struct got_error *err;
607 struct got_packfile_hdr hdr;
608 struct got_packidx packidx;
609 char buf[8];
610 int nobj, nvalid, nloose, nresolved = 0, i;
611 struct got_indexed_object *objects = NULL, *obj;
612 SHA1_CTX ctx;
613 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
614 ssize_t r, w;
615 int pass, have_ref_deltas = 0, first_delta_idx = -1;
616 size_t mapoff = 0;
617 int p_indexed = 0, last_p_indexed = -1;
618 int p_resolved = 0, last_p_resolved = -1;
620 /* Check pack file header. */
621 if (pack->map) {
622 if (pack->filesize < sizeof(hdr))
623 return got_error_msg(GOT_ERR_BAD_PACKFILE,
624 "short packfile header");
625 memcpy(&hdr, pack->map, sizeof(hdr));
626 mapoff += sizeof(hdr);
627 } else {
628 r = read(pack->fd, &hdr, sizeof(hdr));
629 if (r == -1)
630 return got_error_from_errno("read");
631 if (r < sizeof(hdr))
632 return got_error_msg(GOT_ERR_BAD_PACKFILE,
633 "short packfile header");
636 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
637 return got_error_msg(GOT_ERR_BAD_PACKFILE,
638 "bad packfile signature");
639 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
640 return got_error_msg(GOT_ERR_BAD_PACKFILE,
641 "bad packfile version");
642 nobj = betoh32(hdr.nobjects);
643 if (nobj == 0)
644 return got_error_msg(GOT_ERR_BAD_PACKFILE,
645 "bad packfile with zero objects");
647 /*
648 * Create an in-memory pack index which will grow as objects
649 * IDs in the pack file are discovered. Only fields used to
650 * read deltified objects will be needed by the pack.c library
651 * code, so setting up just a pack index header is sufficient.
652 */
653 memset(&packidx, 0, sizeof(packidx));
654 packidx.hdr.magic = malloc(sizeof(uint32_t));
655 if (packidx.hdr.magic == NULL)
656 return got_error_from_errno("calloc");
657 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
658 packidx.hdr.version = malloc(sizeof(uint32_t));
659 if (packidx.hdr.version == NULL) {
660 err = got_error_from_errno("malloc");
661 goto done;
663 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
664 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
665 sizeof(uint32_t));
666 if (packidx.hdr.fanout_table == NULL) {
667 err = got_error_from_errno("calloc");
668 goto done;
670 packidx.hdr.sorted_ids = calloc(nobj,
671 sizeof(struct got_packidx_object_id));
672 if (packidx.hdr.sorted_ids == NULL) {
673 err = got_error_from_errno("calloc");
674 goto done;
676 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
677 if (packidx.hdr.crc32 == NULL) {
678 err = got_error_from_errno("calloc");
679 goto done;
681 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
682 if (packidx.hdr.offsets == NULL) {
683 err = got_error_from_errno("calloc");
684 goto done;
686 /* Large offsets table is empty for pack files < 2 GB. */
687 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
688 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
689 if (packidx.hdr.large_offsets == NULL) {
690 err = got_error_from_errno("calloc");
691 goto done;
695 nvalid = 0;
696 nloose = 0;
697 objects = calloc(nobj, sizeof(struct got_indexed_object));
698 if (objects == NULL)
699 return got_error_from_errno("calloc");
701 /*
702 * First pass: locate all objects and identify un-deltified objects.
704 * When this pass has completed we will know offset, type, size, and
705 * CRC information for all objects in this pack file. We won't know
706 * any of the actual object IDs of deltified objects yet since we
707 * will not yet attempt to combine deltas.
708 */
709 pass = 1;
710 for (i = 0; i < nobj; i++) {
711 /* Don't send too many progress privsep messages. */
712 p_indexed = ((i + 1) * 100) / nobj;
713 if (p_indexed != last_p_indexed) {
714 err = send_index_pack_progress(ibuf, nobj, i + 1,
715 nloose, 0);
716 if (err)
717 goto done;
718 last_p_indexed = p_indexed;
721 obj = &objects[i];
722 obj->crc = crc32(0L, NULL, 0);
724 /* Store offset to type+size information for this object. */
725 if (pack->map) {
726 obj->off = mapoff;
727 } else {
728 obj->off = lseek(pack->fd, 0, SEEK_CUR);
729 if (obj->off == -1) {
730 err = got_error_from_errno("lseek");
731 goto done;
735 err = read_packed_object(pack, obj, tmpfile);
736 if (err)
737 goto done;
739 if (pack->map) {
740 mapoff += obj->tslen + obj->len;
741 } else {
742 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
743 SEEK_SET) == -1) {
744 err = got_error_from_errno("lseek");
745 goto done;
749 if (obj->type == GOT_OBJ_TYPE_BLOB ||
750 obj->type == GOT_OBJ_TYPE_TREE ||
751 obj->type == GOT_OBJ_TYPE_COMMIT ||
752 obj->type == GOT_OBJ_TYPE_TAG) {
753 obj->valid = 1;
754 nloose++;
755 } else {
756 if (first_delta_idx == -1)
757 first_delta_idx = i;
758 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
759 have_ref_deltas = 1;
762 nvalid = nloose;
764 if (first_delta_idx == -1)
765 first_delta_idx = 0;
767 /* In order to resolve ref deltas we need an in-progress pack index. */
768 if (have_ref_deltas)
769 make_packidx(&packidx, nobj, objects);
771 /*
772 * Second pass: We can now resolve deltas to compute the IDs of
773 * objects which appear in deltified form. Because deltas can be
774 * chained this pass may require a couple of iterations until all
775 * IDs of deltified objects have been discovered.
776 */
777 pass++;
778 while (nvalid != nobj) {
779 int n = 0;
780 /*
781 * This loop will only run once unless the pack file
782 * contains ref deltas which refer to objects located
783 * later in the pack file, which is unusual.
784 * Offset deltas can always be resolved in one pass
785 * unless the packfile is corrupt.
786 */
787 for (i = first_delta_idx; i < nobj; i++) {
788 obj = &objects[i];
789 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
790 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
791 continue;
793 if (obj->valid)
794 continue;
796 if (pack->map == NULL && lseek(pack->fd,
797 obj->off + obj->tslen, SEEK_SET) == -1) {
798 err = got_error_from_errno("lseek");
799 goto done;
802 err = resolve_deltified_object(pack, &packidx, obj,
803 tmpfile, delta_base_file, delta_accum_file);
804 if (err) {
805 if (err->code != GOT_ERR_NO_OBJ)
806 goto done;
807 /*
808 * We cannot resolve this object yet because
809 * a delta base is unknown. Try again later.
810 */
811 continue;
814 obj->valid = 1;
815 n++;
816 if (have_ref_deltas)
817 update_packidx(&packidx, nobj, obj);
818 /* Don't send too many progress privsep messages. */
819 p_resolved = ((nresolved + n) * 100) / nobj;
820 if (p_resolved != last_p_resolved) {
821 err = send_index_pack_progress(ibuf, nobj,
822 nobj, nloose, nresolved + n);
823 if (err)
824 goto done;
825 last_p_resolved = p_resolved;
829 if (pass++ > 3 && n == 0) {
830 static char msg[64];
831 snprintf(msg, sizeof(msg), "could not resolve "
832 "any of deltas; packfile could be corrupt");
833 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
834 goto done;
837 nresolved += n;
838 nvalid += nresolved;
841 if (nloose + nresolved != nobj) {
842 static char msg[64];
843 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
844 nloose + nresolved, nobj);
845 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
846 goto done;
849 err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved);
850 if (err)
851 goto done;
853 make_packidx(&packidx, nobj, objects);
855 free(objects);
856 objects = NULL;
858 SHA1Init(&ctx);
859 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
860 putbe32(buf + 4, GOT_PACKIDX_VERSION);
861 err = hwrite(idxfd, buf, 8, &ctx);
862 if (err)
863 goto done;
864 err = hwrite(idxfd, packidx.hdr.fanout_table,
865 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
866 if (err)
867 goto done;
868 err = hwrite(idxfd, packidx.hdr.sorted_ids,
869 nobj * SHA1_DIGEST_LENGTH, &ctx);
870 if (err)
871 goto done;
872 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
873 if (err)
874 goto done;
875 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
876 &ctx);
877 if (err)
878 goto done;
879 if (packidx.nlargeobj > 0) {
880 err = hwrite(idxfd, packidx.hdr.large_offsets,
881 packidx.nlargeobj * sizeof(uint64_t), &ctx);
882 if (err)
883 goto done;
885 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
886 if (err)
887 goto done;
889 SHA1Final(packidx_hash, &ctx);
890 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
891 if (w == -1) {
892 err = got_error_from_errno("write");
893 goto done;
895 if (w != sizeof(packidx_hash)) {
896 err = got_error(GOT_ERR_IO);
897 goto done;
899 done:
900 free(objects);
901 free(packidx.hdr.magic);
902 free(packidx.hdr.version);
903 free(packidx.hdr.fanout_table);
904 free(packidx.hdr.sorted_ids);
905 free(packidx.hdr.offsets);
906 free(packidx.hdr.large_offsets);
907 return err;
910 int
911 main(int argc, char **argv)
913 const struct got_error *err = NULL, *close_err;
914 struct imsgbuf ibuf;
915 struct imsg imsg;
916 int idxfd = -1, tmpfd = -1, i;
917 FILE *tmpfiles[3];
918 struct got_pack pack;
919 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
920 off_t packfile_size;
921 #if 0
922 static int attached;
923 while (!attached)
924 sleep(1);
925 #endif
927 for (i = 0; i < nitems(tmpfiles); i++)
928 tmpfiles[i] = NULL;
930 memset(&pack, 0, sizeof(pack));
931 pack.fd = -1;
932 pack.delta_cache = got_delta_cache_alloc(500,
933 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
934 if (pack.delta_cache == NULL) {
935 err = got_error_from_errno("got_delta_cache_alloc");
936 goto done;
939 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
940 #ifndef PROFILE
941 /* revoke access to most system calls */
942 if (pledge("stdio recvfd", NULL) == -1) {
943 err = got_error_from_errno("pledge");
944 got_privsep_send_error(&ibuf, err);
945 return 1;
947 #endif
948 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
949 if (err)
950 goto done;
951 if (imsg.hdr.type == GOT_IMSG_STOP)
952 goto done;
953 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
954 err = got_error(GOT_ERR_PRIVSEP_MSG);
955 goto done;
957 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
958 err = got_error(GOT_ERR_PRIVSEP_LEN);
959 goto done;
961 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
962 pack.fd = imsg.fd;
964 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
965 if (err)
966 goto done;
967 if (imsg.hdr.type == GOT_IMSG_STOP)
968 goto done;
969 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
970 err = got_error(GOT_ERR_PRIVSEP_MSG);
971 goto done;
973 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
974 err = got_error(GOT_ERR_PRIVSEP_LEN);
975 goto done;
977 idxfd = imsg.fd;
979 for (i = 0; i < nitems(tmpfiles); i++) {
980 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
981 if (err)
982 goto done;
983 if (imsg.hdr.type == GOT_IMSG_STOP)
984 goto done;
985 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
986 err = got_error(GOT_ERR_PRIVSEP_MSG);
987 goto done;
989 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
990 err = got_error(GOT_ERR_PRIVSEP_LEN);
991 goto done;
993 tmpfd = imsg.fd;
994 tmpfiles[i] = fdopen(tmpfd, "w+");
995 if (tmpfiles[i] == NULL) {
996 err = got_error_from_errno("fdopen");
997 goto done;
999 tmpfd = -1;
1002 if (lseek(pack.fd, 0, SEEK_END) == -1) {
1003 err = got_error_from_errno("lseek");
1004 goto done;
1006 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
1007 if (packfile_size == -1) {
1008 err = got_error_from_errno("lseek");
1009 goto done;
1011 pack.filesize = packfile_size; /* XXX off_t vs size_t */
1013 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
1014 err = got_error_from_errno("lseek");
1015 goto done;
1018 #ifndef GOT_PACK_NO_MMAP
1019 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
1020 pack.fd, 0);
1021 if (pack.map == MAP_FAILED)
1022 pack.map = NULL; /* fall back to read(2) */
1023 #endif
1024 err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1025 pack_hash, &ibuf);
1026 done:
1027 close_err = got_pack_close(&pack);
1028 if (close_err && err == NULL)
1029 err = close_err;
1030 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1031 err = got_error_from_errno("close");
1032 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1033 err = got_error_from_errno("close");
1034 for (i = 0; i < nitems(tmpfiles); i++) {
1035 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1036 err == NULL)
1037 err = got_error_from_errno("close");
1040 if (err == NULL)
1041 err = send_index_pack_done(&ibuf);
1042 if (err) {
1043 got_privsep_send_error(&ibuf, err);
1044 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1045 got_privsep_send_error(&ibuf, err);
1046 exit(1);
1049 exit(0);