2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
19 #include <sys/queue.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_opentemp.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_path.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_privsep.h"
45 #include "got_lib_pack.h"
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 static const struct got_error *
56 verify_fanout_table(uint32_t *fanout_table)
60 for (i = 0; i < 0xff - 1; i++) {
61 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
62 return got_error(GOT_ERR_BAD_PACKIDX);
68 const struct got_error *
69 got_pack_get_packfile_size(size_t *size, const char *path)
76 dot = strrchr(path, '.');
78 return got_error(GOT_ERR_BAD_PATH);
80 /* Path must point to a pack index or to a pack file. */
81 if (strcmp(dot, GOT_PACKIDX_SUFFIX) == 0) {
82 const struct got_error *err = NULL;
84 char base_path[PATH_MAX];
86 /* Convert pack index path to pack file path. */
87 if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
88 return got_error(GOT_ERR_NO_SPACE);
89 dot = strrchr(base_path, '.');
91 return got_error(GOT_ERR_BAD_PATH);
93 if (asprintf(&path_pack, "%s.pack", base_path) == -1)
94 return got_error_from_errno();
96 if (stat(path_pack, &sb) != 0)
97 err = got_error_from_errno();
101 } else if (strcmp(dot, GOT_PACKFILE_SUFFIX) == 0) {
102 if (stat(path, &sb) != 0)
103 return got_error_from_errno();
105 return got_error(GOT_ERR_BAD_PATH);
111 const struct got_error *
112 got_packidx_init_hdr(struct got_packidx *p, int verify)
114 const struct got_error *err = NULL;
115 struct got_packidx_v2_hdr *h;
117 uint8_t sha1[SHA1_DIGEST_LENGTH];
118 size_t nobj, len_fanout, len_ids, offset, remain;
127 if (remain < sizeof(*h->magic)) {
128 err = got_error(GOT_ERR_BAD_PACKIDX);
132 h->magic = (uint32_t *)(p->map + offset);
134 h->magic = malloc(sizeof(*h->magic));
135 if (h->magic == NULL) {
136 err = got_error_from_errno();
139 n = read(p->fd, h->magic, sizeof(*h->magic));
141 err = got_error_from_errno();
142 else if (n != sizeof(*h->magic)) {
143 err = got_error(GOT_ERR_BAD_PACKIDX);
147 if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
148 err = got_error(GOT_ERR_BAD_PACKIDX);
151 offset += sizeof(*h->magic);
152 remain -= sizeof(*h->magic);
155 SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
157 if (remain < sizeof(*h->version)) {
158 err = got_error(GOT_ERR_BAD_PACKIDX);
162 h->version = (uint32_t *)(p->map + offset);
164 h->version = malloc(sizeof(*h->version));
165 if (h->version == NULL) {
166 err = got_error_from_errno();
169 n = read(p->fd, h->version, sizeof(*h->version));
171 err = got_error_from_errno();
172 else if (n != sizeof(*h->version)) {
173 err = got_error(GOT_ERR_BAD_PACKIDX);
177 if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
178 err = got_error(GOT_ERR_BAD_PACKIDX);
181 offset += sizeof(*h->version);
182 remain -= sizeof(*h->version);
185 SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
188 sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
189 if (remain < len_fanout) {
190 err = got_error(GOT_ERR_BAD_PACKIDX);
194 h->fanout_table = (uint32_t *)(p->map + offset);
196 h->fanout_table = malloc(len_fanout);
197 if (h->fanout_table == NULL) {
198 err = got_error_from_errno();
201 n = read(p->fd, h->fanout_table, len_fanout);
203 err = got_error_from_errno();
204 else if (n != len_fanout) {
205 err = got_error(GOT_ERR_BAD_PACKIDX);
209 err = verify_fanout_table(h->fanout_table);
213 SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
214 offset += len_fanout;
215 remain -= len_fanout;
217 nobj = betoh32(h->fanout_table[0xff]);
218 len_ids = nobj * sizeof(*h->sorted_ids);
219 if (len_ids <= nobj || len_ids > remain) {
220 err = got_error(GOT_ERR_BAD_PACKIDX);
225 (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
227 h->sorted_ids = malloc(len_ids);
228 if (h->sorted_ids == NULL) {
229 err = got_error(GOT_ERR_BAD_PACKIDX);
232 n = read(p->fd, h->sorted_ids, len_ids);
234 err = got_error_from_errno();
235 else if (n != len_ids) {
236 err = got_error(GOT_ERR_BAD_PACKIDX);
241 SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
245 if (remain < nobj * sizeof(*h->crc32)) {
246 err = got_error(GOT_ERR_BAD_PACKIDX);
250 h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
252 h->crc32 = malloc(nobj * sizeof(*h->crc32));
253 if (h->crc32 == NULL) {
254 err = got_error_from_errno();
257 n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
259 err = got_error_from_errno();
260 else if (n != nobj * sizeof(*h->crc32)) {
261 err = got_error(GOT_ERR_BAD_PACKIDX);
266 SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
267 remain -= nobj * sizeof(*h->crc32);
268 offset += nobj * sizeof(*h->crc32);
270 if (remain < nobj * sizeof(*h->offsets)) {
271 err = got_error(GOT_ERR_BAD_PACKIDX);
275 h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
277 h->offsets = malloc(nobj * sizeof(*h->offsets));
278 if (h->offsets == NULL) {
279 err = got_error_from_errno();
282 n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
284 err = got_error_from_errno();
285 else if (n != nobj * sizeof(*h->offsets)) {
286 err = got_error(GOT_ERR_BAD_PACKIDX);
291 SHA1Update(&ctx, (uint8_t *)h->offsets,
292 nobj * sizeof(*h->offsets));
293 remain -= nobj * sizeof(*h->offsets);
294 offset += nobj * sizeof(*h->offsets);
296 /* Large file offsets are contained only in files > 2GB. */
297 if (p->len <= 0x80000000)
300 if (remain < nobj * sizeof(*h->large_offsets)) {
301 err = got_error(GOT_ERR_BAD_PACKIDX);
305 h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
307 h->offsets = malloc(nobj * sizeof(*h->large_offsets));
308 if (h->offsets == NULL) {
309 err = got_error_from_errno();
312 n = read(p->fd, h->large_offsets,
313 nobj * sizeof(*h->large_offsets));
315 err = got_error_from_errno();
316 else if (n != nobj * sizeof(*h->large_offsets)) {
317 err = got_error(GOT_ERR_BAD_PACKIDX);
322 SHA1Update(&ctx, (uint8_t*)h->large_offsets,
323 nobj * sizeof(*h->large_offsets));
324 remain -= nobj * sizeof(*h->large_offsets);
325 offset += nobj * sizeof(*h->large_offsets);
328 if (remain < sizeof(*h->trailer)) {
329 err = got_error(GOT_ERR_BAD_PACKIDX);
334 (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
336 h->trailer = malloc(sizeof(*h->trailer));
337 if (h->trailer == NULL) {
338 err = got_error_from_errno();
341 n = read(p->fd, h->trailer, sizeof(*h->trailer));
343 err = got_error_from_errno();
344 else if (n != sizeof(*h->trailer)) {
345 err = got_error(GOT_ERR_BAD_PACKIDX);
350 SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
351 SHA1Final(sha1, &ctx);
352 if (memcmp(h->trailer->packidx_sha1, sha1,
353 SHA1_DIGEST_LENGTH) != 0)
354 err = got_error(GOT_ERR_PACKIDX_CSUM);
360 const struct got_error *
361 got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
363 const struct got_error *err = NULL;
364 struct got_packidx *p;
368 p = calloc(1, sizeof(*p));
370 return got_error_from_errno();
372 p->fd = open(path, O_RDONLY | O_NOFOLLOW);
374 return got_error_from_errno();
376 err = got_pack_get_packfile_size(&p->len, path);
382 if (p->len < sizeof(p->hdr)) {
383 err = got_error(GOT_ERR_BAD_PACKIDX);
389 p->path_packidx = strdup(path);
390 if (p->path_packidx == NULL) {
391 err = got_error_from_errno();
395 #ifndef GOT_PACK_NO_MMAP
396 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
397 if (p->map == MAP_FAILED) {
398 if (errno != ENOMEM) {
399 err = got_error_from_errno();
402 p->map = NULL; /* fall back to read(2) */
406 err = got_packidx_init_hdr(p, verify);
409 got_packidx_close(p);
416 const struct got_error *
417 got_packidx_close(struct got_packidx *packidx)
419 const struct got_error *err = NULL;
421 free(packidx->path_packidx);
423 if (munmap(packidx->map, packidx->len) == -1)
424 err = got_error_from_errno();
426 free(packidx->hdr.magic);
427 free(packidx->hdr.version);
428 free(packidx->hdr.fanout_table);
429 free(packidx->hdr.sorted_ids);
430 free(packidx->hdr.crc32);
431 free(packidx->hdr.offsets);
432 free(packidx->hdr.large_offsets);
433 free(packidx->hdr.trailer);
435 if (close(packidx->fd) != 0 && err == NULL)
436 err = got_error_from_errno();
443 get_object_offset(struct got_packidx *packidx, int idx)
445 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
446 uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
447 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
449 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
450 if (idx < 0 || idx > totobj ||
451 packidx->hdr.large_offsets == NULL)
453 loffset = betoh64(packidx->hdr.large_offsets[idx]);
454 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
456 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
460 got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
462 u_int8_t id0 = id->sha1[0];
463 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
464 int left = 0, right = totobj - 1;
467 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
469 while (left <= right) {
470 struct got_packidx_object_id *oid;
473 i = ((left + right) / 2);
474 oid = &packidx->hdr.sorted_ids[i];
475 cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
487 const struct got_error *
488 got_pack_stop_privsep_child(struct got_pack *pack)
490 const struct got_error *err = NULL;
492 if (pack->privsep_child == NULL)
495 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
498 err = got_privsep_wait_for_child(pack->privsep_child->pid);
499 free(pack->privsep_child);
500 pack->privsep_child = NULL;
504 const struct got_error *
505 got_pack_close(struct got_pack *pack)
507 const struct got_error *err = NULL;
509 err = got_pack_stop_privsep_child(pack);
510 if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
511 err = got_error_from_errno();
512 if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL)
513 err = got_error_from_errno();
515 free(pack->path_packfile);
516 pack->path_packfile = NULL;
522 static const struct got_error *
523 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
524 struct got_pack *pack, off_t offset)
534 if (offset >= pack->filesize)
535 return got_error(GOT_ERR_PACK_OFFSET);
538 mapoff = (size_t)offset;
540 if (lseek(pack->fd, offset, SEEK_SET) == -1)
541 return got_error_from_errno();
545 /* We do not support size values which don't fit in 64 bit. */
547 return got_error(GOT_ERR_NO_SPACE);
550 sizeN = *(pack->map + mapoff);
551 mapoff += sizeof(sizeN);
553 ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
555 return got_error_from_errno();
556 if (n != sizeof(sizeN))
557 return got_error(GOT_ERR_BAD_PACKFILE);
559 *len += sizeof(sizeN);
562 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
563 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
564 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
566 size_t shift = 4 + 7 * (i - 1);
567 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
570 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
577 static const struct got_error *
578 open_plain_object(struct got_object **obj, const char *path_packfile,
579 struct got_object_id *id, uint8_t type, off_t offset, size_t size, int idx)
581 *obj = calloc(1, sizeof(**obj));
583 return got_error_from_errno();
585 (*obj)->path_packfile = strdup(path_packfile);
586 if ((*obj)->path_packfile == NULL) {
587 const struct got_error *err = got_error_from_errno();
594 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
595 (*obj)->pack_idx = idx;
598 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
599 (*obj)->pack_offset = offset;
604 static const struct got_error *
605 parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
615 /* We do not support offset values which don't fit in 64 bit. */
617 return got_error(GOT_ERR_NO_SPACE);
621 if (delta_offset >= pack->filesize)
622 return got_error(GOT_ERR_PACK_OFFSET);
623 mapoff = (size_t)delta_offset + *len;
624 offN = *(pack->map + mapoff);
627 n = read(pack->fd, &offN, sizeof(offN));
629 return got_error_from_errno();
630 if (n != sizeof(offN))
631 return got_error(GOT_ERR_BAD_PACKFILE);
633 *len += sizeof(offN);
636 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
640 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
643 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
649 static const struct got_error *
650 parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
651 off_t offset, int tslen)
653 const struct got_error *err;
659 err = parse_negative_offset(&negoffset, &negofflen, pack,
664 /* Compute the base object's offset (must be in the same pack file). */
665 *base_offset = (offset - negoffset);
666 if (*base_offset <= 0)
667 return got_error(GOT_ERR_BAD_PACKFILE);
673 static const struct got_error *
674 resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
675 struct got_pack *, off_t, size_t, int, size_t, unsigned int);
677 static const struct got_error *
678 add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
679 int delta_type, size_t delta_size, size_t delta_data_offset,
680 uint8_t *delta_buf, size_t delta_len)
682 struct got_delta *delta;
684 delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
685 delta_data_offset, delta_buf,
688 return got_error_from_errno();
689 /* delta is freed in got_object_close() */
691 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
695 static const struct got_error *
696 resolve_offset_delta(struct got_delta_chain *deltas,
697 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
698 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
701 const struct got_error *err;
706 off_t delta_data_offset;
708 size_t delta_len, consumed;
710 err = parse_offset_delta(&base_offset, &consumed, pack,
711 delta_offset, tslen);
715 delta_data_offset = delta_offset + tslen + consumed;
716 if (delta_data_offset >= pack->filesize)
717 return got_error(GOT_ERR_PACK_OFFSET);
719 if (pack->map == NULL) {
720 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
721 if (delta_data_offset == -1)
722 return got_error_from_errno();
726 size_t mapoff = (size_t)delta_data_offset;
727 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
728 mapoff, pack->filesize - mapoff);
733 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
738 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
739 delta_data_offset, delta_buf, delta_len);
743 /* An offset delta must be in the same packfile. */
744 if (base_offset >= pack->filesize) {
745 err = got_error(GOT_ERR_PACK_OFFSET);
749 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
754 err = resolve_delta_chain(deltas, packidx, pack, base_offset,
755 base_tslen, base_type, base_size, recursion - 1);
762 static const struct got_error *
763 resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
764 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
765 size_t delta_size, unsigned int recursion)
767 const struct got_error *err;
768 struct got_object_id id;
774 off_t delta_data_offset;
775 uint8_t *delta_buf = NULL;
778 if (delta_offset >= pack->filesize)
779 return got_error(GOT_ERR_PACK_OFFSET);
780 delta_data_offset = delta_offset + tslen;
781 if (delta_data_offset >= pack->filesize)
782 return got_error(GOT_ERR_PACK_OFFSET);
784 if (pack->map == NULL) {
785 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
786 if (delta_data_offset == -1)
787 return got_error_from_errno();
792 size_t mapoff = (size_t)delta_data_offset;
793 memcpy(&id, pack->map + mapoff, sizeof(id));
794 mapoff += sizeof(id);
795 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
796 mapoff, pack->filesize - mapoff);
800 ssize_t n = read(pack->fd, &id, sizeof(id));
802 return got_error_from_errno();
804 return got_error(GOT_ERR_BAD_PACKFILE);
805 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
810 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
811 delta_data_offset, delta_buf, delta_len);
815 /* Delta base must be in the same pack file. */
816 idx = got_packidx_get_object_idx(packidx, &id);
818 err = got_error(GOT_ERR_BAD_PACKFILE);
822 base_offset = get_object_offset(packidx, idx);
823 if (base_offset == (uint64_t)-1) {
824 err = got_error(GOT_ERR_BAD_PACKIDX);
828 if (base_offset >= pack->filesize) {
829 err = got_error(GOT_ERR_PACK_OFFSET);
833 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
838 err = resolve_delta_chain(deltas, packidx, pack, base_offset,
839 base_tslen, base_type, base_size, recursion - 1);
846 static const struct got_error *
847 resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
848 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
849 size_t delta_size, unsigned int recursion)
851 const struct got_error *err = NULL;
853 if (--recursion == 0)
854 return got_error(GOT_ERR_RECURSION);
856 switch (delta_type) {
857 case GOT_OBJ_TYPE_COMMIT:
858 case GOT_OBJ_TYPE_TREE:
859 case GOT_OBJ_TYPE_BLOB:
860 case GOT_OBJ_TYPE_TAG:
861 /* Plain types are the final delta base. Recursion ends. */
862 err = add_delta(deltas, delta_offset, tslen, delta_type,
863 delta_size, 0, NULL, 0);
865 case GOT_OBJ_TYPE_OFFSET_DELTA:
866 err = resolve_offset_delta(deltas, packidx, pack,
867 delta_offset, tslen, delta_type, delta_size, recursion - 1);
869 case GOT_OBJ_TYPE_REF_DELTA:
870 err = resolve_ref_delta(deltas, packidx, pack,
871 delta_offset, tslen, delta_type, delta_size, recursion - 1);
874 return got_error(GOT_ERR_OBJ_TYPE);
880 static const struct got_error *
881 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
882 struct got_pack *pack, struct got_object_id *id, off_t offset,
883 size_t tslen, int delta_type, size_t delta_size, int idx)
885 const struct got_error *err = NULL;
888 *obj = calloc(1, sizeof(**obj));
890 return got_error_from_errno();
894 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
895 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
896 (*obj)->pack_offset = offset + tslen;
898 (*obj)->path_packfile = strdup(pack->path_packfile);
899 if ((*obj)->path_packfile == NULL) {
900 err = got_error_from_errno();
903 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
904 (*obj)->pack_idx = idx;
906 SIMPLEQ_INIT(&(*obj)->deltas.entries);
907 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
909 err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
910 tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
914 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
917 (*obj)->type = resolved_type;
920 got_object_close(*obj);
926 const struct got_error *
927 got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
928 struct got_packidx *packidx, int idx, struct got_object_id *id)
930 const struct got_error *err = NULL;
938 offset = get_object_offset(packidx, idx);
939 if (offset == (uint64_t)-1)
940 return got_error(GOT_ERR_BAD_PACKIDX);
942 err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
947 case GOT_OBJ_TYPE_COMMIT:
948 case GOT_OBJ_TYPE_TREE:
949 case GOT_OBJ_TYPE_BLOB:
950 case GOT_OBJ_TYPE_TAG:
951 err = open_plain_object(obj, pack->path_packfile, id, type,
952 offset + tslen, size, idx);
954 case GOT_OBJ_TYPE_OFFSET_DELTA:
955 case GOT_OBJ_TYPE_REF_DELTA:
956 err = open_delta_object(obj, packidx, pack, id, offset,
957 tslen, type, size, idx);
960 err = got_error(GOT_ERR_OBJ_TYPE);
967 static const struct got_error *
968 get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
970 struct got_delta *delta;
971 uint64_t base_size = 0, result_size = 0;
974 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
975 /* Plain object types are the delta base. */
976 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
977 delta->type != GOT_OBJ_TYPE_TREE &&
978 delta->type != GOT_OBJ_TYPE_BLOB &&
979 delta->type != GOT_OBJ_TYPE_TAG) {
980 const struct got_error *err;
981 err = got_delta_get_sizes(&base_size, &result_size,
982 delta->delta_buf, delta->delta_len);
986 base_size = delta->size;
987 if (base_size > *max_size)
988 *max_size = base_size;
989 if (result_size > *max_size)
990 *max_size = result_size;
996 const struct got_error *
997 got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj)
999 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1000 return got_error(GOT_ERR_OBJ_TYPE);
1002 return get_delta_chain_max_size(size, &obj->deltas);
1005 static const struct got_error *
1006 dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1007 struct got_pack *pack, FILE *outfile, FILE *base_file, FILE *accum_file)
1009 const struct got_error *err = NULL;
1010 struct got_delta *delta;
1011 uint8_t *base_buf = NULL, *accum_buf = NULL;
1012 size_t base_bufsz = 0, accum_size = 0;
1018 if (SIMPLEQ_EMPTY(&deltas->entries))
1019 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1021 /* We process small enough files entirely in memory for speed. */
1022 err = get_delta_chain_max_size(&max_size, deltas);
1025 if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1026 accum_buf = malloc(max_size);
1027 if (accum_buf == NULL)
1028 return got_error_from_errno();
1033 /* Deltas are ordered in ascending order. */
1034 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1037 off_t delta_data_offset;
1039 /* Plain object types are the delta base. */
1040 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1041 delta->type != GOT_OBJ_TYPE_TREE &&
1042 delta->type != GOT_OBJ_TYPE_BLOB &&
1043 delta->type != GOT_OBJ_TYPE_TAG) {
1044 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1048 delta_data_offset = delta->offset + delta->tslen;
1049 if (delta_data_offset >= pack->filesize) {
1050 err = got_error(GOT_ERR_PACK_OFFSET);
1053 if (pack->map == NULL) {
1054 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1056 err = got_error_from_errno();
1062 mapoff = (size_t)delta_data_offset;
1063 err = got_inflate_to_file_mmap(
1064 &base_bufsz, pack->map, mapoff,
1065 pack->filesize - mapoff, base_file);
1067 err = got_inflate_to_file_fd(
1068 &base_bufsz, pack->fd, base_file);
1071 mapoff = (size_t)delta_data_offset;
1072 err = got_inflate_to_mem_mmap(&base_buf,
1073 &base_bufsz, pack->map, mapoff,
1074 pack->filesize - mapoff);
1076 err = got_inflate_to_mem_fd(&base_buf,
1077 &base_bufsz, pack->fd);
1088 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1089 delta->delta_buf, delta->delta_len, accum_buf,
1090 &accum_size, max_size);
1093 err = got_delta_apply(base_file, delta->delta_buf,
1095 /* Final delta application writes to output file. */
1096 ++n < deltas->nentries ? accum_file : outfile,
1102 if (n < deltas->nentries) {
1103 /* Accumulated delta becomes the new base. */
1105 uint8_t *tmp = accum_buf;
1107 * Base buffer switches roles with accumulation
1108 * buffer. Ensure it can hold the largest
1109 * result in the delta chain. The initial
1110 * allocation might have been smaller.
1112 if (base_bufsz < max_size) {
1114 p = reallocarray(base_buf, 1, max_size);
1116 err = got_error_from_errno();
1120 base_bufsz = max_size;
1122 accum_buf = base_buf;
1125 FILE *tmp = accum_file;
1126 accum_file = base_file;
1137 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1139 if (len != accum_size)
1140 err = got_ferror(outfile, GOT_ERR_IO);
1144 *result_size = accum_size;
1148 static const struct got_error *
1149 dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1150 struct got_delta_chain *deltas, struct got_pack *pack)
1152 const struct got_error *err = NULL;
1153 struct got_delta *delta;
1154 uint8_t *base_buf = NULL, *accum_buf = NULL;
1155 size_t base_bufsz = 0, accum_size = 0;
1162 if (SIMPLEQ_EMPTY(&deltas->entries))
1163 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1165 err = get_delta_chain_max_size(&max_size, deltas);
1168 accum_buf = malloc(max_size);
1169 if (accum_buf == NULL)
1170 return got_error_from_errno();
1172 /* Deltas are ordered in ascending order. */
1173 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1175 size_t delta_data_offset;
1177 /* Plain object types are the delta base. */
1178 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1179 delta->type != GOT_OBJ_TYPE_TREE &&
1180 delta->type != GOT_OBJ_TYPE_BLOB &&
1181 delta->type != GOT_OBJ_TYPE_TAG) {
1182 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1186 delta_data_offset = delta->offset + delta->tslen;
1187 if (delta_data_offset >= pack->filesize) {
1188 err = got_error(GOT_ERR_PACK_OFFSET);
1192 size_t mapoff = (size_t)delta_data_offset;
1193 err = got_inflate_to_mem_mmap(&base_buf,
1194 &base_bufsz, pack->map, mapoff,
1195 pack->filesize - mapoff);
1197 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1199 err = got_error_from_errno();
1202 err = got_inflate_to_mem_fd(&base_buf,
1203 &base_bufsz, pack->fd);
1211 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1212 delta->delta_buf, delta->delta_len, accum_buf,
1213 &accum_size, max_size);
1218 if (n < deltas->nentries) {
1219 /* Accumulated delta becomes the new base. */
1220 uint8_t *tmp = accum_buf;
1222 * Base buffer switches roles with accumulation buffer.
1223 * Ensure it can hold the largest result in the delta
1224 * chain. Initial allocation might have been smaller.
1226 if (base_bufsz < max_size) {
1228 p = reallocarray(base_buf, 1, max_size);
1230 err = got_error_from_errno();
1234 base_bufsz = max_size;
1236 accum_buf = base_buf;
1248 *outbuf = accum_buf;
1249 *outlen = accum_size;
1254 const struct got_error *
1255 got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1256 FILE *outfile, FILE *base_file, FILE *accum_file)
1258 const struct got_error *err = NULL;
1260 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1261 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1263 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1264 if (obj->pack_offset >= pack->filesize)
1265 return got_error(GOT_ERR_PACK_OFFSET);
1268 size_t mapoff = (size_t)obj->pack_offset;
1269 err = got_inflate_to_file_mmap(&obj->size, pack->map,
1270 mapoff, pack->filesize - mapoff, outfile);
1272 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1273 return got_error_from_errno();
1274 err = got_inflate_to_file_fd(&obj->size, pack->fd,
1278 err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1279 outfile, base_file, accum_file);
1284 const struct got_error *
1285 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1286 struct got_object *obj, struct got_pack *pack)
1288 const struct got_error *err = NULL;
1290 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1291 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1293 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1294 if (obj->pack_offset >= pack->filesize)
1295 return got_error(GOT_ERR_PACK_OFFSET);
1297 size_t mapoff = (size_t)obj->pack_offset;
1298 err = got_inflate_to_mem_mmap(buf, len, pack->map,
1299 mapoff, pack->filesize - mapoff);
1301 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1302 return got_error_from_errno();
1303 err = got_inflate_to_mem_fd(buf, len, pack->fd);
1306 err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);