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, GOT_DEFAULT_FILE_MODE);
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 p->map = NULL; /* fall back to read(2) */
401 err = got_packidx_init_hdr(p, verify);
404 got_packidx_close(p);
411 const struct got_error *
412 got_packidx_close(struct got_packidx *packidx)
414 const struct got_error *err = NULL;
416 free(packidx->path_packidx);
418 if (munmap(packidx->map, packidx->len) == -1)
419 err = got_error_from_errno();
421 free(packidx->hdr.magic);
422 free(packidx->hdr.version);
423 free(packidx->hdr.fanout_table);
424 free(packidx->hdr.sorted_ids);
425 free(packidx->hdr.crc32);
426 free(packidx->hdr.offsets);
427 free(packidx->hdr.large_offsets);
428 free(packidx->hdr.trailer);
430 if (close(packidx->fd) != 0 && err == NULL)
431 err = got_error_from_errno();
438 get_object_offset(struct got_packidx *packidx, int idx)
440 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
441 uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
442 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
444 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
445 if (idx < 0 || idx > totobj ||
446 packidx->hdr.large_offsets == NULL)
448 loffset = betoh64(packidx->hdr.large_offsets[idx]);
449 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
451 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
455 got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
457 u_int8_t id0 = id->sha1[0];
458 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
459 int left = 0, right = totobj - 1;
462 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
464 while (left <= right) {
465 struct got_packidx_object_id *oid;
468 i = ((left + right) / 2);
469 oid = &packidx->hdr.sorted_ids[i];
470 cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
482 const struct got_error *
483 got_pack_stop_privsep_child(struct got_pack *pack)
485 const struct got_error *err = NULL;
487 if (pack->privsep_child == NULL)
490 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
493 err = got_privsep_wait_for_child(pack->privsep_child->pid);
494 free(pack->privsep_child);
495 pack->privsep_child = NULL;
499 const struct got_error *
500 got_pack_close(struct got_pack *pack)
502 const struct got_error *err = NULL;
504 err = got_pack_stop_privsep_child(pack);
505 if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
506 err = got_error_from_errno();
507 if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL)
508 err = got_error_from_errno();
510 free(pack->path_packfile);
511 pack->path_packfile = NULL;
517 static const struct got_error *
518 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
519 struct got_pack *pack, off_t offset)
529 if (offset >= pack->filesize)
530 return got_error(GOT_ERR_PACK_OFFSET);
533 mapoff = (size_t)offset;
535 if (lseek(pack->fd, offset, SEEK_SET) == -1)
536 return got_error_from_errno();
540 /* We do not support size values which don't fit in 64 bit. */
542 return got_error(GOT_ERR_NO_SPACE);
545 sizeN = *(pack->map + mapoff);
546 mapoff += sizeof(sizeN);
548 ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
550 return got_error_from_errno();
551 if (n != sizeof(sizeN))
552 return got_error(GOT_ERR_BAD_PACKFILE);
554 *len += sizeof(sizeN);
557 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
558 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
559 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
561 size_t shift = 4 + 7 * (i - 1);
562 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
565 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
572 static const struct got_error *
573 open_plain_object(struct got_object **obj, const char *path_packfile,
574 struct got_object_id *id, uint8_t type, off_t offset, size_t size, int idx)
576 *obj = calloc(1, sizeof(**obj));
578 return got_error_from_errno();
580 (*obj)->path_packfile = strdup(path_packfile);
581 if ((*obj)->path_packfile == NULL) {
582 const struct got_error *err = got_error_from_errno();
589 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
590 (*obj)->pack_idx = idx;
593 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
594 (*obj)->pack_offset = offset;
599 static const struct got_error *
600 parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
610 /* We do not support offset values which don't fit in 64 bit. */
612 return got_error(GOT_ERR_NO_SPACE);
616 if (delta_offset >= pack->filesize)
617 return got_error(GOT_ERR_PACK_OFFSET);
618 mapoff = (size_t)delta_offset + *len;
619 offN = *(pack->map + mapoff);
622 n = read(pack->fd, &offN, sizeof(offN));
624 return got_error_from_errno();
625 if (n != sizeof(offN))
626 return got_error(GOT_ERR_BAD_PACKFILE);
628 *len += sizeof(offN);
631 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
635 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
638 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
644 static const struct got_error *
645 parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
646 off_t offset, int tslen)
648 const struct got_error *err;
654 err = parse_negative_offset(&negoffset, &negofflen, pack,
659 /* Compute the base object's offset (must be in the same pack file). */
660 *base_offset = (offset - negoffset);
661 if (*base_offset <= 0)
662 return got_error(GOT_ERR_BAD_PACKFILE);
668 static const struct got_error *
669 resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
670 struct got_pack *, off_t, size_t, int, size_t, unsigned int);
672 static const struct got_error *
673 add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
674 int delta_type, size_t delta_size, size_t delta_data_offset,
675 uint8_t *delta_buf, size_t delta_len)
677 struct got_delta *delta;
679 delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
680 delta_data_offset, delta_buf,
683 return got_error_from_errno();
684 /* delta is freed in got_object_close() */
686 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
690 static const struct got_error *
691 resolve_offset_delta(struct got_delta_chain *deltas,
692 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
693 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
696 const struct got_error *err;
701 off_t delta_data_offset;
703 size_t delta_len, consumed;
705 err = parse_offset_delta(&base_offset, &consumed, pack,
706 delta_offset, tslen);
710 delta_data_offset = delta_offset + tslen + consumed;
711 if (delta_data_offset >= pack->filesize)
712 return got_error(GOT_ERR_PACK_OFFSET);
714 if (pack->map == NULL) {
715 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
716 if (delta_data_offset == -1)
717 return got_error_from_errno();
721 size_t mapoff = (size_t)delta_data_offset;
722 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
723 mapoff, pack->filesize - mapoff);
728 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
733 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
734 delta_data_offset, delta_buf, delta_len);
738 /* An offset delta must be in the same packfile. */
739 if (base_offset >= pack->filesize) {
740 err = got_error(GOT_ERR_PACK_OFFSET);
744 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
749 err = resolve_delta_chain(deltas, packidx, pack, base_offset,
750 base_tslen, base_type, base_size, recursion - 1);
757 static const struct got_error *
758 resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
759 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
760 size_t delta_size, unsigned int recursion)
762 const struct got_error *err;
763 struct got_object_id id;
769 off_t delta_data_offset;
770 uint8_t *delta_buf = NULL;
773 if (delta_offset >= pack->filesize)
774 return got_error(GOT_ERR_PACK_OFFSET);
775 delta_data_offset = delta_offset + tslen;
776 if (delta_data_offset >= pack->filesize)
777 return got_error(GOT_ERR_PACK_OFFSET);
779 if (pack->map == NULL) {
780 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
781 if (delta_data_offset == -1)
782 return got_error_from_errno();
787 size_t mapoff = (size_t)delta_data_offset;
788 memcpy(&id, pack->map + mapoff, sizeof(id));
789 mapoff += sizeof(id);
790 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
791 mapoff, pack->filesize - mapoff);
795 ssize_t n = read(pack->fd, &id, sizeof(id));
797 return got_error_from_errno();
799 return got_error(GOT_ERR_BAD_PACKFILE);
800 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
805 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
806 delta_data_offset, delta_buf, delta_len);
810 /* Delta base must be in the same pack file. */
811 idx = got_packidx_get_object_idx(packidx, &id);
813 err = got_error(GOT_ERR_BAD_PACKFILE);
817 base_offset = get_object_offset(packidx, idx);
818 if (base_offset == (uint64_t)-1) {
819 err = got_error(GOT_ERR_BAD_PACKIDX);
823 if (base_offset >= pack->filesize) {
824 err = got_error(GOT_ERR_PACK_OFFSET);
828 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
833 err = resolve_delta_chain(deltas, packidx, pack, base_offset,
834 base_tslen, base_type, base_size, recursion - 1);
841 static const struct got_error *
842 resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
843 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
844 size_t delta_size, unsigned int recursion)
846 const struct got_error *err = NULL;
848 if (--recursion == 0)
849 return got_error(GOT_ERR_RECURSION);
851 switch (delta_type) {
852 case GOT_OBJ_TYPE_COMMIT:
853 case GOT_OBJ_TYPE_TREE:
854 case GOT_OBJ_TYPE_BLOB:
855 case GOT_OBJ_TYPE_TAG:
856 /* Plain types are the final delta base. Recursion ends. */
857 err = add_delta(deltas, delta_offset, tslen, delta_type,
858 delta_size, 0, NULL, 0);
860 case GOT_OBJ_TYPE_OFFSET_DELTA:
861 err = resolve_offset_delta(deltas, packidx, pack,
862 delta_offset, tslen, delta_type, delta_size, recursion - 1);
864 case GOT_OBJ_TYPE_REF_DELTA:
865 err = resolve_ref_delta(deltas, packidx, pack,
866 delta_offset, tslen, delta_type, delta_size, recursion - 1);
869 return got_error(GOT_ERR_OBJ_TYPE);
875 static const struct got_error *
876 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
877 struct got_pack *pack, struct got_object_id *id, off_t offset,
878 size_t tslen, int delta_type, size_t delta_size, int idx)
880 const struct got_error *err = NULL;
883 *obj = calloc(1, sizeof(**obj));
885 return got_error_from_errno();
889 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
890 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
891 (*obj)->pack_offset = offset + tslen;
893 (*obj)->path_packfile = strdup(pack->path_packfile);
894 if ((*obj)->path_packfile == NULL) {
895 err = got_error_from_errno();
898 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
899 (*obj)->pack_idx = idx;
901 SIMPLEQ_INIT(&(*obj)->deltas.entries);
902 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
904 err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
905 tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
909 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
912 (*obj)->type = resolved_type;
915 got_object_close(*obj);
921 const struct got_error *
922 got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
923 struct got_packidx *packidx, int idx, struct got_object_id *id)
925 const struct got_error *err = NULL;
933 offset = get_object_offset(packidx, idx);
934 if (offset == (uint64_t)-1)
935 return got_error(GOT_ERR_BAD_PACKIDX);
937 err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
942 case GOT_OBJ_TYPE_COMMIT:
943 case GOT_OBJ_TYPE_TREE:
944 case GOT_OBJ_TYPE_BLOB:
945 case GOT_OBJ_TYPE_TAG:
946 err = open_plain_object(obj, pack->path_packfile, id, type,
947 offset + tslen, size, idx);
949 case GOT_OBJ_TYPE_OFFSET_DELTA:
950 case GOT_OBJ_TYPE_REF_DELTA:
951 err = open_delta_object(obj, packidx, pack, id, offset,
952 tslen, type, size, idx);
955 err = got_error(GOT_ERR_OBJ_TYPE);
962 static const struct got_error *
963 get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
965 struct got_delta *delta;
966 uint64_t base_size = 0, result_size = 0;
969 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
970 /* Plain object types are the delta base. */
971 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
972 delta->type != GOT_OBJ_TYPE_TREE &&
973 delta->type != GOT_OBJ_TYPE_BLOB &&
974 delta->type != GOT_OBJ_TYPE_TAG) {
975 const struct got_error *err;
976 err = got_delta_get_sizes(&base_size, &result_size,
977 delta->delta_buf, delta->delta_len);
981 base_size = delta->size;
982 if (base_size > *max_size)
983 *max_size = base_size;
984 if (result_size > *max_size)
985 *max_size = result_size;
991 const struct got_error *
992 got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj)
994 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
995 return got_error(GOT_ERR_OBJ_TYPE);
997 return get_delta_chain_max_size(size, &obj->deltas);
1000 static const struct got_error *
1001 dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1002 struct got_pack *pack, FILE *outfile, FILE *base_file, FILE *accum_file)
1004 const struct got_error *err = NULL;
1005 struct got_delta *delta;
1006 uint8_t *base_buf = NULL, *accum_buf = NULL;
1007 size_t base_bufsz = 0, accum_size = 0;
1013 if (SIMPLEQ_EMPTY(&deltas->entries))
1014 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1016 /* We process small enough files entirely in memory for speed. */
1017 err = get_delta_chain_max_size(&max_size, deltas);
1020 if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1021 accum_buf = malloc(max_size);
1022 if (accum_buf == NULL)
1023 return got_error_from_errno();
1028 /* Deltas are ordered in ascending order. */
1029 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1032 off_t delta_data_offset;
1034 /* Plain object types are the delta base. */
1035 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1036 delta->type != GOT_OBJ_TYPE_TREE &&
1037 delta->type != GOT_OBJ_TYPE_BLOB &&
1038 delta->type != GOT_OBJ_TYPE_TAG) {
1039 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1043 delta_data_offset = delta->offset + delta->tslen;
1044 if (delta_data_offset >= pack->filesize) {
1045 err = got_error(GOT_ERR_PACK_OFFSET);
1048 if (pack->map == NULL) {
1049 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1051 err = got_error_from_errno();
1057 mapoff = (size_t)delta_data_offset;
1058 err = got_inflate_to_file_mmap(
1059 &base_bufsz, pack->map, mapoff,
1060 pack->filesize - mapoff, base_file);
1062 err = got_inflate_to_file_fd(
1063 &base_bufsz, pack->fd, base_file);
1066 mapoff = (size_t)delta_data_offset;
1067 err = got_inflate_to_mem_mmap(&base_buf,
1068 &base_bufsz, pack->map, mapoff,
1069 pack->filesize - mapoff);
1071 err = got_inflate_to_mem_fd(&base_buf,
1072 &base_bufsz, pack->fd);
1083 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1084 delta->delta_buf, delta->delta_len, accum_buf,
1085 &accum_size, max_size);
1088 err = got_delta_apply(base_file, delta->delta_buf,
1090 /* Final delta application writes to output file. */
1091 ++n < deltas->nentries ? accum_file : outfile,
1097 if (n < deltas->nentries) {
1098 /* Accumulated delta becomes the new base. */
1100 uint8_t *tmp = accum_buf;
1102 * Base buffer switches roles with accumulation
1103 * buffer. Ensure it can hold the largest
1104 * result in the delta chain. The initial
1105 * allocation might have been smaller.
1107 if (base_bufsz < max_size) {
1109 p = reallocarray(base_buf, 1, max_size);
1111 err = got_error_from_errno();
1115 base_bufsz = max_size;
1117 accum_buf = base_buf;
1120 FILE *tmp = accum_file;
1121 accum_file = base_file;
1132 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1134 if (len != accum_size)
1135 err = got_ferror(outfile, GOT_ERR_IO);
1137 if (base_file && fclose(base_file) != 0 && err == NULL)
1138 err = got_error_from_errno();
1139 if (accum_file && fclose(accum_file) != 0 && err == NULL)
1140 err = got_error_from_errno();
1143 *result_size = accum_size;
1147 static const struct got_error *
1148 dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1149 struct got_delta_chain *deltas, struct got_pack *pack)
1151 const struct got_error *err = NULL;
1152 struct got_delta *delta;
1153 uint8_t *base_buf = NULL, *accum_buf = NULL;
1154 size_t base_bufsz = 0, accum_size = 0;
1161 if (SIMPLEQ_EMPTY(&deltas->entries))
1162 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1164 err = get_delta_chain_max_size(&max_size, deltas);
1167 accum_buf = malloc(max_size);
1168 if (accum_buf == NULL)
1169 return got_error_from_errno();
1171 /* Deltas are ordered in ascending order. */
1172 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1174 size_t delta_data_offset;
1176 /* Plain object types are the delta base. */
1177 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1178 delta->type != GOT_OBJ_TYPE_TREE &&
1179 delta->type != GOT_OBJ_TYPE_BLOB &&
1180 delta->type != GOT_OBJ_TYPE_TAG) {
1181 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1185 delta_data_offset = delta->offset + delta->tslen;
1186 if (delta_data_offset >= pack->filesize) {
1187 err = got_error(GOT_ERR_PACK_OFFSET);
1191 size_t mapoff = (size_t)delta_data_offset;
1192 err = got_inflate_to_mem_mmap(&base_buf,
1193 &base_bufsz, pack->map, mapoff,
1194 pack->filesize - mapoff);
1196 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1198 err = got_error_from_errno();
1201 err = got_inflate_to_mem_fd(&base_buf,
1202 &base_bufsz, pack->fd);
1210 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1211 delta->delta_buf, delta->delta_len, accum_buf,
1212 &accum_size, max_size);
1217 if (n < deltas->nentries) {
1218 /* Accumulated delta becomes the new base. */
1219 uint8_t *tmp = accum_buf;
1221 * Base buffer switches roles with accumulation buffer.
1222 * Ensure it can hold the largest result in the delta
1223 * chain. Initial allocation might have been smaller.
1225 if (base_bufsz < max_size) {
1227 p = reallocarray(base_buf, 1, max_size);
1229 err = got_error_from_errno();
1233 base_bufsz = max_size;
1235 accum_buf = base_buf;
1247 *outbuf = accum_buf;
1248 *outlen = accum_size;
1253 const struct got_error *
1254 got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1255 FILE *outfile, FILE *base_file, FILE *accum_file)
1257 const struct got_error *err = NULL;
1259 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1260 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1262 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1263 if (obj->pack_offset >= pack->filesize)
1264 return got_error(GOT_ERR_PACK_OFFSET);
1267 size_t mapoff = (size_t)obj->pack_offset;
1268 err = got_inflate_to_file_mmap(&obj->size, pack->map,
1269 mapoff, pack->filesize - mapoff, outfile);
1271 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1272 return got_error_from_errno();
1273 err = got_inflate_to_file_fd(&obj->size, pack->fd,
1277 err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1278 outfile, base_file, accum_file);
1283 const struct got_error *
1284 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1285 struct got_object *obj, struct got_pack *pack)
1287 const struct got_error *err = NULL;
1289 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1290 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1292 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1293 if (obj->pack_offset >= pack->filesize)
1294 return got_error(GOT_ERR_PACK_OFFSET);
1296 size_t mapoff = (size_t)obj->pack_offset;
1297 err = got_inflate_to_mem_mmap(buf, len, pack->map,
1298 mapoff, pack->filesize - mapoff);
1300 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1301 return got_error_from_errno();
1302 err = got_inflate_to_mem_fd(buf, len, pack->fd);
1305 err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);