2 * Copyright (c) 2018 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>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_opentemp.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_pack.h"
41 #include "got_lib_path.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_repository.h"
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
51 #define GOT_PACK_PREFIX "pack-"
52 #define GOT_PACKFILE_SUFFIX ".pack"
53 #define GOT_PACKIDX_SUFFIX ".idx"
54 #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
55 SHA1_DIGEST_STRING_LENGTH - 1 + \
56 strlen(GOT_PACKFILE_SUFFIX))
57 #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
58 SHA1_DIGEST_STRING_LENGTH - 1 + \
59 strlen(GOT_PACKIDX_SUFFIX))
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 static const struct got_error *
66 verify_fanout_table(uint32_t *fanout_table)
70 for (i = 0; i < 0xff - 1; i++) {
71 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
72 return got_error(GOT_ERR_BAD_PACKIDX);
78 static const struct got_error *
79 get_packfile_size(size_t *size, const char *path)
86 dot = strrchr(path, '.');
88 return got_error(GOT_ERR_BAD_PATH);
90 /* Path must point to a pack index or to a pack file. */
91 if (strcmp(dot, GOT_PACKIDX_SUFFIX) == 0) {
92 const struct got_error *err = NULL;
94 char base_path[PATH_MAX];
96 /* Convert pack index path to pack file path. */
97 if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
98 return got_error(GOT_ERR_NO_SPACE);
99 dot = strrchr(base_path, '.');
101 return got_error(GOT_ERR_BAD_PATH);
103 if (asprintf(&path_pack, "%s.pack", base_path) == -1)
104 return got_error_from_errno();
106 if (stat(path_pack, &sb) != 0)
107 err = got_error_from_errno();
111 } else if (strcmp(dot, GOT_PACKFILE_SUFFIX) == 0) {
112 if (stat(path, &sb) != 0)
113 return got_error_from_errno();
115 return got_error(GOT_ERR_BAD_PATH);
121 const struct got_error *
122 got_packidx_init_hdr(struct got_packidx *p, int verify)
124 const struct got_error *err = NULL;
125 struct got_packidx_v2_hdr *h;
127 uint8_t sha1[SHA1_DIGEST_LENGTH];
128 size_t nobj, len_fanout, len_ids, offset, remain;
137 if (remain < sizeof(*h->magic)) {
138 err = got_error(GOT_ERR_BAD_PACKIDX);
142 h->magic = (uint32_t *)(p->map + offset);
144 h->magic = malloc(sizeof(*h->magic));
145 if (h->magic == NULL) {
146 err = got_error_from_errno();
149 n = read(p->fd, h->magic, sizeof(*h->magic));
150 if (n != sizeof(*h->magic)) {
151 err = got_error(GOT_ERR_BAD_PACKIDX);
155 if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
156 err = got_error(GOT_ERR_BAD_PACKIDX);
159 offset += sizeof(*h->magic);
160 remain -= sizeof(*h->magic);
163 SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
165 if (remain < sizeof(*h->version)) {
166 err = got_error(GOT_ERR_BAD_PACKIDX);
170 h->version = (uint32_t *)(p->map + offset);
172 h->version = malloc(sizeof(*h->version));
173 if (h->version == NULL) {
174 err = got_error_from_errno();
177 n = read(p->fd, h->version, sizeof(*h->version));
178 if (n != sizeof(*h->version)) {
179 err = got_error(GOT_ERR_BAD_PACKIDX);
183 if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
184 err = got_error(GOT_ERR_BAD_PACKIDX);
187 offset += sizeof(*h->version);
188 remain -= sizeof(*h->version);
191 SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
194 sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
195 if (remain < len_fanout) {
196 err = got_error(GOT_ERR_BAD_PACKIDX);
200 h->fanout_table = (uint32_t *)(p->map + offset);
202 h->fanout_table = malloc(len_fanout);
203 if (h->fanout_table == NULL) {
204 err = got_error_from_errno();
207 n = read(p->fd, h->fanout_table, len_fanout);
208 if (n != len_fanout) {
209 err = got_error(GOT_ERR_BAD_PACKIDX);
213 err = verify_fanout_table(h->fanout_table);
217 SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
218 offset += len_fanout;
219 remain -= len_fanout;
221 nobj = betoh32(h->fanout_table[0xff]);
222 len_ids = nobj * sizeof(*h->sorted_ids);
223 if (len_ids <= nobj || len_ids > remain) {
224 err = got_error(GOT_ERR_BAD_PACKIDX);
229 (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
231 h->sorted_ids = malloc(len_ids);
232 if (h->sorted_ids == NULL) {
233 err = got_error(GOT_ERR_BAD_PACKIDX);
236 n = read(p->fd, h->sorted_ids, len_ids);
238 err = got_error(GOT_ERR_BAD_PACKIDX);
243 SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
247 if (remain < nobj * sizeof(*h->crc32)) {
248 err = got_error(GOT_ERR_BAD_PACKIDX);
252 h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
254 h->crc32 = malloc(nobj * sizeof(*h->crc32));
255 if (h->crc32 == NULL) {
256 err = got_error_from_errno();
259 n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
260 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));
283 if (n != nobj * sizeof(*h->offsets)) {
284 err = got_error(GOT_ERR_BAD_PACKIDX);
289 SHA1Update(&ctx, (uint8_t *)h->offsets,
290 nobj * sizeof(*h->offsets));
291 remain -= nobj * sizeof(*h->offsets);
292 offset += nobj * sizeof(*h->offsets);
294 /* Large file offsets are contained only in files > 2GB. */
295 if (p->len <= 0x80000000)
298 if (remain < nobj * sizeof(*h->large_offsets)) {
299 err = got_error(GOT_ERR_BAD_PACKIDX);
303 h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
305 h->offsets = malloc(nobj * sizeof(*h->large_offsets));
306 if (h->offsets == NULL) {
307 err = got_error_from_errno();
310 n = read(p->fd, h->large_offsets,
311 nobj * sizeof(*h->large_offsets));
312 if (n != nobj * sizeof(*h->large_offsets)) {
313 err = got_error(GOT_ERR_BAD_PACKIDX);
318 SHA1Update(&ctx, (uint8_t*)h->large_offsets,
319 nobj * sizeof(*h->large_offsets));
320 remain -= nobj * sizeof(*h->large_offsets);
321 offset += nobj * sizeof(*h->large_offsets);
324 if (remain < sizeof(*h->trailer)) {
325 err = got_error(GOT_ERR_BAD_PACKIDX);
330 (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
332 h->trailer = malloc(sizeof(*h->trailer));
333 if (h->trailer == NULL) {
334 err = got_error_from_errno();
337 n = read(p->fd, h->trailer, sizeof(*h->trailer));
338 if (n != sizeof(*h->trailer)) {
339 err = got_error(GOT_ERR_BAD_PACKIDX);
344 SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
345 SHA1Final(sha1, &ctx);
346 if (memcmp(h->trailer->packidx_sha1, sha1,
347 SHA1_DIGEST_LENGTH) != 0)
348 err = got_error(GOT_ERR_PACKIDX_CSUM);
354 const struct got_error *
355 got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
357 const struct got_error *err = NULL;
358 struct got_packidx *p;
362 p = calloc(1, sizeof(*p));
364 return got_error_from_errno();
366 p->fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
368 return got_error_from_errno();
370 err = get_packfile_size(&p->len, path);
376 if (p->len < sizeof(p->hdr)) {
377 err = got_error(GOT_ERR_BAD_PACKIDX);
383 p->path_packidx = strdup(path);
384 if (p->path_packidx == NULL) {
385 err = got_error_from_errno();
389 #ifndef GOT_PACK_NO_MMAP
390 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
391 if (p->map == MAP_FAILED)
392 p->map = NULL; /* fall back to read(2) */
395 err = got_packidx_init_hdr(p, verify);
398 got_packidx_close(p);
405 const struct got_error *
406 got_packidx_close(struct got_packidx *packidx)
408 const struct got_error *err = NULL;
410 free(packidx->path_packidx);
412 if (munmap(packidx->map, packidx->len) == -1)
413 err = got_error_from_errno();
415 free(packidx->hdr.magic);
416 free(packidx->hdr.version);
417 free(packidx->hdr.fanout_table);
418 free(packidx->hdr.sorted_ids);
419 free(packidx->hdr.crc32);
420 free(packidx->hdr.offsets);
421 free(packidx->hdr.large_offsets);
422 free(packidx->hdr.trailer);
431 is_packidx_filename(const char *name, size_t len)
433 if (len != GOT_PACKIDX_NAMELEN)
436 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
439 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
440 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
447 get_object_offset(struct got_packidx *packidx, int idx)
449 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
450 uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
451 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
453 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
454 if (idx < 0 || idx > totobj ||
455 packidx->hdr.large_offsets == NULL)
457 loffset = betoh64(packidx->hdr.large_offsets[idx]);
458 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
460 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
464 get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
466 u_int8_t id0 = id->sha1[0];
467 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
468 int left = 0, right = totobj - 1;
471 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
473 while (left <= right) {
474 struct got_packidx_object_id *oid;
477 i = ((left + right) / 2);
478 oid = &packidx->hdr.sorted_ids[i];
479 cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
491 static const struct got_error *
492 cache_packidx(struct got_packidx *packidx, struct got_repository *repo)
494 const struct got_error *err = NULL;
497 for (i = 0; i < nitems(repo->packidx_cache); i++) {
498 if (repo->packidx_cache[i] == NULL)
502 if (i == nitems(repo->packidx_cache)) {
503 err = got_packidx_close(repo->packidx_cache[i - 1]);
506 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
507 sizeof(repo->packidx_cache) -
508 sizeof(repo->packidx_cache[0]));
512 repo->packidx_cache[i] = packidx;
516 static const struct got_error *
517 search_packidx(struct got_packidx **packidx, int *idx,
518 struct got_repository *repo, struct got_object_id *id)
520 const struct got_error *err;
527 /* Search pack index cache. */
528 for (i = 0; i < nitems(repo->packidx_cache); i++) {
529 if (repo->packidx_cache[i] == NULL)
531 *idx = get_object_idx(repo->packidx_cache[i], id);
533 *packidx = repo->packidx_cache[i];
537 /* No luck. Search the filesystem. */
539 path_packdir = got_repo_get_path_objects_pack(repo);
540 if (path_packdir == NULL)
541 return got_error_from_errno();
543 packdir = opendir(path_packdir);
544 if (packdir == NULL) {
545 err = got_error_from_errno();
549 while ((dent = readdir(packdir)) != NULL) {
550 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
553 if (asprintf(&path_packidx, "%s/%s", path_packdir,
554 dent->d_name) == -1) {
555 err = got_error_from_errno();
559 err = got_packidx_open(packidx, path_packidx, 0);
564 *idx = get_object_idx(*packidx, id);
566 err = NULL; /* found the object */
567 err = cache_packidx(*packidx, repo);
571 err = got_packidx_close(*packidx);
577 err = got_error(GOT_ERR_NO_OBJ);
580 if (packdir && closedir(packdir) != 0 && err == 0)
581 err = got_error_from_errno();
585 static const struct got_error *
586 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
590 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
591 size = strlen(packidx->path_packidx) + 2;
592 if (size < GOT_PACKFILE_NAMELEN + 1)
593 return got_error(GOT_ERR_BAD_PATH);
595 *path_packfile = calloc(size, sizeof(**path_packfile));
596 if (*path_packfile == NULL)
597 return got_error_from_errno();
599 /* Copy up to and excluding ".idx". */
600 if (strlcpy(*path_packfile, packidx->path_packidx,
601 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
602 return got_error(GOT_ERR_NO_SPACE);
604 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
605 return got_error(GOT_ERR_NO_SPACE);
610 static const struct got_error *
611 read_packfile_hdr(int fd, struct got_packidx *packidx)
613 const struct got_error *err = NULL;
614 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
615 struct got_packfile_hdr hdr;
618 n = read(fd, &hdr, sizeof(hdr));
620 return got_error_from_errno();
621 if (n != sizeof(hdr))
622 return got_error(GOT_ERR_BAD_PACKFILE);
624 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
625 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
626 betoh32(hdr.nobjects) != totobj)
627 err = got_error(GOT_ERR_BAD_PACKFILE);
632 static const struct got_error *
633 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
635 const struct got_error *err = NULL;
637 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
639 return got_error_from_errno();
642 err = read_packfile_hdr(*fd, packidx);
651 const struct got_error *
652 got_pack_close(struct got_pack *pack)
654 const struct got_error *err = NULL;
656 if (pack->map && munmap(pack->map, pack->filesize) == -1)
657 err = got_error_from_errno();
660 free(pack->path_packfile);
661 pack->path_packfile = NULL;
667 static const struct got_error *
668 cache_pack(struct got_pack **packp, const char *path_packfile,
669 struct got_packidx *packidx, struct got_repository *repo)
671 const struct got_error *err = NULL;
672 struct got_pack *pack = NULL;
678 for (i = 0; i < nitems(repo->packs); i++) {
679 pack = &repo->packs[i];
680 if (pack->path_packfile == NULL)
682 if (strcmp(pack->path_packfile, path_packfile) == 0)
686 if (i == nitems(repo->packs) - 1) {
687 err = got_pack_close(&repo->packs[i - 1]);
690 memmove(&repo->packs[1], &repo->packs[0],
691 sizeof(repo->packs) - sizeof(repo->packs[0]));
695 pack = &repo->packs[i];
697 pack->path_packfile = strdup(path_packfile);
698 if (pack->path_packfile == NULL) {
699 err = got_error_from_errno();
703 err = open_packfile(&pack->fd, path_packfile, packidx);
707 err = get_packfile_size(&pack->filesize, path_packfile);
711 #ifndef GOT_PACK_NO_MMAP
712 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
714 if (pack->map == MAP_FAILED)
715 pack->map = NULL; /* fall back to read(2) */
720 free(pack->path_packfile);
721 memset(pack, 0, sizeof(*pack));
729 get_cached_pack(const char *path_packfile, struct got_repository *repo)
731 struct got_pack *pack = NULL;
734 for (i = 0; i < nitems(repo->packs); i++) {
735 pack = &repo->packs[i];
736 if (pack->path_packfile == NULL)
738 if (strcmp(pack->path_packfile, path_packfile) == 0)
745 static const struct got_error *
746 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
747 struct got_pack *pack, off_t offset)
757 if (offset >= pack->filesize)
758 return got_error(GOT_ERR_PACK_OFFSET);
761 mapoff = (size_t)offset;
763 if (lseek(pack->fd, offset, SEEK_SET) == -1)
764 return got_error_from_errno();
768 /* We do not support size values which don't fit in 64 bit. */
770 return got_error(GOT_ERR_NO_SPACE);
773 sizeN = *(pack->map + mapoff);
774 mapoff += sizeof(sizeN);
776 ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
778 return got_error_from_errno();
779 if (n != sizeof(sizeN))
780 return got_error(GOT_ERR_BAD_PACKFILE);
782 *len += sizeof(sizeN);
785 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
786 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
787 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
789 size_t shift = 4 + 7 * (i - 1);
790 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
793 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
800 static const struct got_error *
801 open_plain_object(struct got_object **obj, const char *path_packfile,
802 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
804 *obj = calloc(1, sizeof(**obj));
806 return got_error_from_errno();
808 (*obj)->path_packfile = strdup(path_packfile);
809 if ((*obj)->path_packfile == NULL) {
810 const struct got_error *err = got_error_from_errno();
817 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
820 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
821 (*obj)->pack_offset = offset;
826 static const struct got_error *
827 parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
837 /* We do not support offset values which don't fit in 64 bit. */
839 return got_error(GOT_ERR_NO_SPACE);
843 if (delta_offset >= pack->filesize)
844 return got_error(GOT_ERR_PACK_OFFSET);
845 mapoff = (size_t)delta_offset + *len;
846 offN = *(pack->map + mapoff);
849 n = read(pack->fd, &offN, sizeof(offN));
851 return got_error_from_errno();
852 if (n != sizeof(offN))
853 return got_error(GOT_ERR_BAD_PACKFILE);
855 *len += sizeof(offN);
858 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
862 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
865 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
871 static const struct got_error *
872 parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
873 off_t offset, int tslen)
875 const struct got_error *err;
881 err = parse_negative_offset(&negoffset, &negofflen, pack,
886 /* Compute the base object's offset (must be in the same pack file). */
887 *base_offset = (offset - negoffset);
888 if (*base_offset <= 0)
889 return got_error(GOT_ERR_BAD_PACKFILE);
895 static const struct got_error *
896 resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
897 struct got_pack *, off_t, size_t, int, size_t, unsigned int);
899 static const struct got_error *
900 add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
901 int delta_type, size_t delta_size, size_t delta_data_offset,
902 uint8_t *delta_buf, size_t delta_len)
904 struct got_delta *delta;
906 delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
907 delta_data_offset, delta_buf,
910 return got_error_from_errno();
911 /* delta is freed in got_object_close() */
913 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
917 static const struct got_error *
918 resolve_offset_delta(struct got_delta_chain *deltas,
919 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
920 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
923 const struct got_error *err;
928 off_t delta_data_offset;
930 size_t delta_len, consumed;
932 err = parse_offset_delta(&base_offset, &consumed, pack,
933 delta_offset, tslen);
937 delta_data_offset = delta_offset + tslen + consumed;
938 if (delta_data_offset >= pack->filesize)
939 return got_error(GOT_ERR_PACK_OFFSET);
941 if (pack->map == NULL) {
942 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
943 if (delta_data_offset == -1)
944 return got_error_from_errno();
948 size_t mapoff = (size_t)delta_data_offset;
949 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
950 mapoff, pack->filesize - mapoff);
955 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
960 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
961 delta_data_offset, delta_buf, delta_len);
965 /* An offset delta must be in the same packfile. */
966 if (base_offset >= pack->filesize) {
967 err = got_error(GOT_ERR_PACK_OFFSET);
971 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
976 err = resolve_delta_chain(deltas, packidx, pack, base_offset,
977 base_tslen, base_type, base_size, recursion - 1);
984 static const struct got_error *
985 resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
986 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
987 size_t delta_size, unsigned int recursion)
989 const struct got_error *err;
990 struct got_object_id id;
996 off_t delta_data_offset;
997 uint8_t *delta_buf = NULL;
1000 if (delta_offset >= pack->filesize)
1001 return got_error(GOT_ERR_PACK_OFFSET);
1002 delta_data_offset = delta_offset + tslen;
1003 if (delta_data_offset >= pack->filesize)
1004 return got_error(GOT_ERR_PACK_OFFSET);
1006 if (pack->map == NULL) {
1007 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1008 if (delta_data_offset == -1)
1009 return got_error_from_errno();
1014 size_t mapoff = (size_t)delta_data_offset;
1015 memcpy(&id, pack->map + mapoff, sizeof(id));
1016 mapoff += sizeof(id);
1017 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
1018 mapoff, pack->filesize - mapoff);
1022 ssize_t n = read(pack->fd, &id, sizeof(id));
1024 return got_error_from_errno();
1025 if (n != sizeof(id))
1026 return got_error(GOT_ERR_BAD_PACKFILE);
1027 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
1032 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1033 delta_data_offset, delta_buf, delta_len);
1037 /* Delta base must be in the same pack file. */
1038 idx = get_object_idx(packidx, &id);
1040 err = got_error(GOT_ERR_BAD_PACKFILE);
1044 base_offset = get_object_offset(packidx, idx);
1045 if (base_offset == (uint64_t)-1) {
1046 err = got_error(GOT_ERR_BAD_PACKIDX);
1050 if (base_offset >= pack->filesize) {
1051 err = got_error(GOT_ERR_PACK_OFFSET);
1055 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
1060 err = resolve_delta_chain(deltas, packidx, pack, base_offset,
1061 base_tslen, base_type, base_size, recursion - 1);
1068 static const struct got_error *
1069 resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
1070 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
1071 size_t delta_size, unsigned int recursion)
1073 const struct got_error *err = NULL;
1075 if (--recursion == 0)
1076 return got_error(GOT_ERR_RECURSION);
1078 switch (delta_type) {
1079 case GOT_OBJ_TYPE_COMMIT:
1080 case GOT_OBJ_TYPE_TREE:
1081 case GOT_OBJ_TYPE_BLOB:
1082 case GOT_OBJ_TYPE_TAG:
1083 /* Plain types are the final delta base. Recursion ends. */
1084 err = add_delta(deltas, delta_offset, tslen, delta_type,
1085 delta_size, 0, NULL, 0);
1087 case GOT_OBJ_TYPE_OFFSET_DELTA:
1088 err = resolve_offset_delta(deltas, packidx, pack,
1089 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1091 case GOT_OBJ_TYPE_REF_DELTA:
1092 err = resolve_ref_delta(deltas, packidx, pack,
1093 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1096 return got_error(GOT_ERR_OBJ_TYPE);
1102 static const struct got_error *
1103 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
1104 struct got_pack *pack, struct got_object_id *id, off_t offset,
1105 size_t tslen, int delta_type, size_t delta_size)
1107 const struct got_error *err = NULL;
1110 *obj = calloc(1, sizeof(**obj));
1112 return got_error_from_errno();
1116 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1117 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1118 (*obj)->pack_offset = offset + tslen;
1120 (*obj)->path_packfile = strdup(pack->path_packfile);
1121 if ((*obj)->path_packfile == NULL) {
1122 err = got_error_from_errno();
1125 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1127 SIMPLEQ_INIT(&(*obj)->deltas.entries);
1128 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1130 err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
1131 tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
1135 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1138 (*obj)->type = resolved_type;
1142 got_object_close(*obj);
1148 static const struct got_error *
1149 open_packed_object(struct got_object **obj, struct got_repository *repo,
1150 struct got_packidx *packidx, int idx, struct got_object_id *id)
1152 const struct got_error *err = NULL;
1154 char *path_packfile;
1155 struct got_pack *pack;
1162 offset = get_object_offset(packidx, idx);
1163 if (offset == (uint64_t)-1)
1164 return got_error(GOT_ERR_BAD_PACKIDX);
1166 err = get_packfile_path(&path_packfile, packidx);
1170 pack = get_cached_pack(path_packfile, repo);
1172 err = cache_pack(&pack, path_packfile, packidx, repo);
1177 err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
1182 case GOT_OBJ_TYPE_COMMIT:
1183 case GOT_OBJ_TYPE_TREE:
1184 case GOT_OBJ_TYPE_BLOB:
1185 case GOT_OBJ_TYPE_TAG:
1186 err = open_plain_object(obj, path_packfile, id, type,
1187 offset + tslen, size);
1190 case GOT_OBJ_TYPE_OFFSET_DELTA:
1191 case GOT_OBJ_TYPE_REF_DELTA:
1192 err = open_delta_object(obj, packidx, pack, id, offset,
1197 err = got_error(GOT_ERR_OBJ_TYPE);
1201 free(path_packfile);
1205 const struct got_error *
1206 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1207 struct got_repository *repo)
1209 const struct got_error *err = NULL;
1210 struct got_packidx *packidx = NULL;
1213 err = search_packidx(&packidx, &idx, repo, id);
1217 err = open_packed_object(obj, repo, packidx, idx, id);
1221 err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1225 static const struct got_error *
1226 get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1228 struct got_delta *delta;
1229 uint64_t base_size = 0, result_size = 0;
1232 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1233 /* Plain object types are the delta base. */
1234 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1235 delta->type != GOT_OBJ_TYPE_TREE &&
1236 delta->type != GOT_OBJ_TYPE_BLOB &&
1237 delta->type != GOT_OBJ_TYPE_TAG) {
1238 const struct got_error *err;
1239 err = got_delta_get_sizes(&base_size, &result_size,
1240 delta->delta_buf, delta->delta_len);
1244 base_size = delta->size;
1245 if (base_size > *max_size)
1246 *max_size = base_size;
1247 if (result_size > *max_size)
1248 *max_size = result_size;
1254 static const struct got_error *
1255 dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1256 struct got_pack *pack, FILE *outfile)
1258 const struct got_error *err = NULL;
1259 struct got_delta *delta;
1260 FILE *base_file = NULL, *accum_file = NULL;
1261 uint8_t *base_buf = NULL, *accum_buf = NULL;
1262 size_t accum_size = 0;
1268 if (SIMPLEQ_EMPTY(&deltas->entries))
1269 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1271 /* We process small enough files entirely in memory for speed. */
1272 err = get_delta_chain_max_size(&max_size, deltas);
1275 if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1276 accum_buf = malloc(max_size);
1277 if (accum_buf == NULL)
1278 return got_error_from_errno();
1280 base_file = got_opentemp();
1281 if (base_file == NULL)
1282 return got_error_from_errno();
1284 accum_file = got_opentemp();
1285 if (accum_file == NULL) {
1286 err = got_error_from_errno();
1292 /* Deltas are ordered in ascending order. */
1293 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1295 size_t base_len, mapoff;
1296 off_t delta_data_offset;
1298 /* Plain object types are the delta base. */
1299 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1300 delta->type != GOT_OBJ_TYPE_TREE &&
1301 delta->type != GOT_OBJ_TYPE_BLOB &&
1302 delta->type != GOT_OBJ_TYPE_TAG) {
1303 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1307 delta_data_offset = delta->offset + delta->tslen;
1308 if (delta_data_offset >= pack->filesize) {
1309 err = got_error(GOT_ERR_PACK_OFFSET);
1312 if (pack->map == NULL) {
1313 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1315 err = got_error_from_errno();
1321 mapoff = (size_t)delta_data_offset;
1322 err = got_inflate_to_file_mmap(
1323 &base_len, pack->map, mapoff,
1324 pack->filesize - mapoff, base_file);
1326 err = got_inflate_to_file_fd(&base_len,
1327 pack->fd, base_file);
1330 mapoff = (size_t)delta_data_offset;
1331 err = got_inflate_to_mem_mmap(&base_buf,
1332 &base_len, pack->map, mapoff,
1333 pack->filesize - mapoff);
1335 err = got_inflate_to_mem_fd(&base_buf,
1336 &base_len, pack->fd);
1337 if (base_len < max_size) {
1339 p = reallocarray(base_buf, 1, max_size);
1341 err = got_error_from_errno();
1356 err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1357 delta->delta_len, accum_buf, &accum_size);
1360 err = got_delta_apply(base_file, delta->delta_buf,
1362 /* Final delta application writes to output file. */
1363 ++n < deltas->nentries ? accum_file : outfile,
1369 if (n < deltas->nentries) {
1370 /* Accumulated delta becomes the new base. */
1372 uint8_t *tmp = accum_buf;
1373 accum_buf = base_buf;
1376 FILE *tmp = accum_file;
1377 accum_file = base_file;
1388 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1390 if (len != accum_size)
1391 err = got_ferror(outfile, GOT_ERR_IO);
1399 *result_size = accum_size;
1403 static const struct got_error *
1404 dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1405 struct got_delta_chain *deltas, struct got_pack *pack)
1407 const struct got_error *err = NULL;
1408 struct got_delta *delta;
1409 uint8_t *base_buf = NULL, *accum_buf = NULL;
1417 if (SIMPLEQ_EMPTY(&deltas->entries))
1418 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1420 err = get_delta_chain_max_size(&max_size, deltas);
1423 accum_buf = malloc(max_size);
1424 if (accum_buf == NULL)
1425 return got_error_from_errno();
1427 /* Deltas are ordered in ascending order. */
1428 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1431 size_t delta_data_offset;
1433 /* Plain object types are the delta base. */
1434 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1435 delta->type != GOT_OBJ_TYPE_TREE &&
1436 delta->type != GOT_OBJ_TYPE_BLOB &&
1437 delta->type != GOT_OBJ_TYPE_TAG) {
1438 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1442 delta_data_offset = delta->offset + delta->tslen;
1443 if (delta_data_offset >= pack->filesize) {
1444 err = got_error(GOT_ERR_PACK_OFFSET);
1448 size_t mapoff = (size_t)delta_data_offset;
1449 err = got_inflate_to_mem_mmap(&base_buf,
1450 &base_len, pack->map, mapoff,
1451 pack->filesize - mapoff);
1453 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1455 err = got_error_from_errno();
1458 err = got_inflate_to_mem_fd(&base_buf,
1459 &base_len, pack->fd);
1463 if (base_len < max_size) {
1465 p = reallocarray(base_buf, 1, max_size);
1467 err = got_error_from_errno();
1476 err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1477 delta->delta_len, accum_buf, &accum_size);
1482 if (n < deltas->nentries) {
1483 /* Accumulated delta becomes the new base. */
1484 uint8_t *tmp = accum_buf;
1485 accum_buf = base_buf;
1497 *outbuf = accum_buf;
1498 *outlen = accum_size;
1503 const struct got_error *
1504 got_packfile_extract_object(FILE **f, struct got_object *obj,
1505 struct got_repository *repo)
1507 const struct got_error *err = NULL;
1508 struct got_pack *pack;
1512 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1513 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1515 pack = get_cached_pack(obj->path_packfile, repo);
1517 err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1522 *f = got_opentemp();
1524 err = got_error_from_errno();
1528 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1529 if (obj->pack_offset >= pack->filesize) {
1530 err = got_error(GOT_ERR_PACK_OFFSET);
1535 size_t mapoff = (size_t)obj->pack_offset;
1536 err = got_inflate_to_file_mmap(&obj->size, pack->map,
1537 mapoff, pack->filesize - mapoff, *f);
1539 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1540 err = got_error_from_errno();
1543 err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1546 err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1556 const struct got_error *
1557 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1558 struct got_object *obj, struct got_repository *repo)
1560 const struct got_error *err = NULL;
1561 struct got_pack *pack;
1563 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1564 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1566 pack = get_cached_pack(obj->path_packfile, repo);
1568 err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1573 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1574 if (obj->pack_offset >= pack->filesize) {
1575 err = got_error(GOT_ERR_PACK_OFFSET);
1579 size_t mapoff = (size_t)obj->pack_offset;
1580 err = got_inflate_to_mem_mmap(buf, len, pack->map,
1581 mapoff, pack->filesize - mapoff);
1583 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1584 err = got_error_from_errno();
1587 err = got_inflate_to_mem_fd(buf, len, pack->fd);
1590 err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);