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_zbuf.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_open(struct got_packidx **packidx, const char *path, int verify)
124 struct got_packidx *p;
125 struct got_packidx_v2_hdr *h;
126 const struct got_error *err = NULL;
127 size_t nobj, len_fanout, len_ids, offset, remain;
129 uint8_t sha1[SHA1_DIGEST_LENGTH];
135 p = calloc(1, sizeof(*p));
137 return got_error_from_errno();
139 p->fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
141 return got_error_from_errno();
143 err = get_packfile_size(&p->len, path);
149 if (p->len < sizeof(p->hdr)) {
150 err = got_error(GOT_ERR_BAD_PACKIDX);
156 p->path_packidx = strdup(path);
157 if (p->path_packidx == NULL) {
158 err = got_error_from_errno();
162 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
163 if (p->map == MAP_FAILED) {
164 err = got_error_from_errno();
171 if (remain < sizeof(*h->magic)) {
172 err = got_error(GOT_ERR_BAD_PACKIDX);
175 h->magic = (uint32_t *)(p->map + offset);
176 if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
177 err = got_error(GOT_ERR_BAD_PACKIDX);
180 offset += sizeof(*h->magic);
181 remain -= sizeof(*h->magic);
184 SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
186 if (remain < sizeof(*h->version)) {
187 err = got_error(GOT_ERR_BAD_PACKIDX);
190 h->version = (uint32_t *)(p->map + offset);
191 if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
192 err = got_error(GOT_ERR_BAD_PACKIDX);
195 offset += sizeof(*h->version);
196 remain -= sizeof(*h->version);
199 SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
202 sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
203 if (remain < len_fanout) {
204 err = got_error(GOT_ERR_BAD_PACKIDX);
207 h->fanout_table = (uint32_t *)(p->map + offset);
208 err = verify_fanout_table(h->fanout_table);
212 SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
213 offset += len_fanout;
214 remain -= len_fanout;
216 nobj = betoh32(h->fanout_table[0xff]);
217 len_ids = nobj * sizeof(*h->sorted_ids);
218 if (len_ids <= nobj || len_ids > remain) {
219 err = got_error(GOT_ERR_BAD_PACKIDX);
223 (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
225 SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
229 if (remain < nobj * sizeof(*h->crc32)) {
230 err = got_error(GOT_ERR_BAD_PACKIDX);
233 h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
235 SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
236 remain -= nobj * sizeof(*h->crc32);
237 offset += nobj * sizeof(*h->crc32);
239 if (remain < nobj * sizeof(*h->offsets)) {
240 err = got_error(GOT_ERR_BAD_PACKIDX);
243 h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
245 SHA1Update(&ctx, (uint8_t *)h->offsets,
246 nobj * sizeof(*h->offsets));
247 remain -= nobj * sizeof(*h->offsets);
248 offset += nobj * sizeof(*h->offsets);
250 /* Large file offsets are contained only in files > 2GB. */
251 if (p->len <= 0x80000000)
254 if (remain < nobj * sizeof(*h->large_offsets)) {
255 err = got_error(GOT_ERR_BAD_PACKIDX);
258 h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
260 SHA1Update(&ctx, (uint8_t*)h->large_offsets,
261 nobj * sizeof(*h->large_offsets));
262 remain -= nobj * sizeof(*h->large_offsets);
263 offset += nobj * sizeof(*h->large_offsets);
266 if (remain < sizeof(*h->trailer)) {
267 err = got_error(GOT_ERR_BAD_PACKIDX);
271 (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
273 SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
274 SHA1Final(sha1, &ctx);
275 if (memcmp(h->trailer->packidx_sha1, sha1,
276 SHA1_DIGEST_LENGTH) != 0)
277 err = got_error(GOT_ERR_PACKIDX_CSUM);
281 got_packidx_close(p);
287 const struct got_error *
288 got_packidx_close(struct got_packidx *packidx)
290 const struct got_error *err = NULL;
292 free(packidx->path_packidx);
293 if (packidx->map != NULL && packidx->map != MAP_FAILED) {
294 if (munmap(packidx->map, packidx->len) == -1)
295 err = got_error_from_errno();
304 is_packidx_filename(const char *name, size_t len)
306 if (len != GOT_PACKIDX_NAMELEN)
309 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
312 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
313 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
320 get_object_offset(struct got_packidx *packidx, int idx)
322 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
323 uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
324 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
326 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
327 if (idx < 0 || idx > totobj ||
328 packidx->hdr.large_offsets == NULL)
330 loffset = betoh64(packidx->hdr.large_offsets[idx]);
331 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
333 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
337 get_object_idx(struct got_packidx *packidx, struct got_object_id *id,
338 struct got_repository *repo)
340 u_int8_t id0 = id->sha1[0];
341 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
342 int left = 0, right = totobj - 1;
345 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
347 while (left <= right) {
348 struct got_packidx_object_id *oid;
351 i = ((left + right) / 2);
352 oid = &packidx->hdr.sorted_ids[i];
353 cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
365 static const struct got_error *
366 cache_packidx(struct got_packidx *packidx, struct got_repository *repo)
368 const struct got_error *err = NULL;
371 for (i = 0; i < nitems(repo->packidx_cache); i++) {
372 if (repo->packidx_cache[i] == NULL)
376 if (i == nitems(repo->packidx_cache)) {
377 err = got_packidx_close(repo->packidx_cache[i - 1]);
380 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
381 sizeof(repo->packidx_cache) -
382 sizeof(repo->packidx_cache[0]));
386 repo->packidx_cache[i] = packidx;
390 static const struct got_error *
391 search_packidx(struct got_packidx **packidx, int *idx,
392 struct got_repository *repo, struct got_object_id *id)
394 const struct got_error *err;
401 /* Search pack index cache. */
402 for (i = 0; i < nitems(repo->packidx_cache); i++) {
403 if (repo->packidx_cache[i] == NULL)
405 *idx = get_object_idx(repo->packidx_cache[i], id, repo);
407 *packidx = repo->packidx_cache[i];
411 /* No luck. Search the filesystem. */
413 path_packdir = got_repo_get_path_objects_pack(repo);
414 if (path_packdir == NULL)
415 return got_error_from_errno();
417 packdir = opendir(path_packdir);
418 if (packdir == NULL) {
419 err = got_error_from_errno();
423 while ((dent = readdir(packdir)) != NULL) {
424 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
427 if (asprintf(&path_packidx, "%s/%s", path_packdir,
428 dent->d_name) == -1) {
429 err = got_error_from_errno();
433 err = got_packidx_open(packidx, path_packidx, 0);
438 *idx = get_object_idx(*packidx, id, repo);
440 err = NULL; /* found the object */
441 err = cache_packidx(*packidx, repo);
445 err = got_packidx_close(*packidx);
451 err = got_error(GOT_ERR_NO_OBJ);
454 if (packdir && closedir(packdir) != 0 && err == 0)
455 err = got_error_from_errno();
459 static const struct got_error *
460 get_packfile_path(char **path_packfile, struct got_repository *repo,
461 struct got_packidx *packidx)
465 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
466 size = strlen(packidx->path_packidx) + 2;
467 if (size < GOT_PACKFILE_NAMELEN + 1)
468 return got_error(GOT_ERR_BAD_PATH);
470 *path_packfile = calloc(size, sizeof(**path_packfile));
471 if (*path_packfile == NULL)
472 return got_error_from_errno();
474 /* Copy up to and excluding ".idx". */
475 if (strlcpy(*path_packfile, packidx->path_packidx,
476 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
477 return got_error(GOT_ERR_NO_SPACE);
479 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
480 return got_error(GOT_ERR_NO_SPACE);
485 const struct got_error *
486 read_packfile_hdr(int fd, struct got_packidx *packidx)
488 const struct got_error *err = NULL;
489 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
490 struct got_packfile_hdr hdr;
493 n = read(fd, &hdr, sizeof(hdr));
495 return got_error_from_errno();
496 if (n != sizeof(hdr))
497 return got_error(GOT_ERR_BAD_PACKFILE);
499 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
500 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
501 betoh32(hdr.nobjects) != totobj)
502 err = got_error(GOT_ERR_BAD_PACKFILE);
507 static const struct got_error *
508 open_packfile(int *fd, const char *path_packfile,
509 struct got_repository *repo, struct got_packidx *packidx)
511 const struct got_error *err = NULL;
513 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
515 return got_error_from_errno();
518 err = read_packfile_hdr(*fd, packidx);
528 got_pack_close(struct got_pack *pack)
532 free(pack->path_packfile);
533 pack->path_packfile = NULL;
537 static const struct got_error *
538 cache_pack(struct got_pack **packp, const char *path_packfile,
539 struct got_packidx *packidx, struct got_repository *repo)
541 const struct got_error *err = NULL;
542 struct got_pack *pack = NULL;
548 for (i = 0; i < nitems(repo->packs); i++) {
549 pack = &repo->packs[i];
550 if (pack->path_packfile == NULL)
552 if (strcmp(pack->path_packfile, path_packfile) == 0)
556 if (i == nitems(repo->packs) - 1) {
557 got_pack_close(&repo->packs[i - 1]);
558 memmove(&repo->packs[1], &repo->packs[0],
559 sizeof(repo->packs) - sizeof(repo->packs[0]));
563 pack = &repo->packs[i];
565 pack->path_packfile = strdup(path_packfile);
566 if (pack->path_packfile == NULL) {
567 err = got_error_from_errno();
571 err = open_packfile(&pack->fd, path_packfile, repo, packidx);
575 err = get_packfile_size(&pack->filesize, path_packfile);
579 free(pack->path_packfile);
580 memset(pack, 0, sizeof(*pack));
588 get_cached_pack(const char *path_packfile, struct got_repository *repo)
590 struct got_pack *pack = NULL;
593 for (i = 0; i < nitems(repo->packs); i++) {
594 pack = &repo->packs[i];
595 if (pack->path_packfile == NULL)
597 if (strcmp(pack->path_packfile, path_packfile) == 0)
604 static const struct got_error *
605 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len, int fd)
614 /* We do not support size values which don't fit in 64 bit. */
616 return got_error(GOT_ERR_NO_SPACE);
618 n = read(fd, &sizeN, sizeof(sizeN));
620 return got_error_from_errno();
621 if (n != sizeof(sizeN))
622 return got_error(GOT_ERR_BAD_PACKFILE);
625 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
626 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
627 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
629 size_t shift = 4 + 7 * (i - 1);
630 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
633 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
637 *len = i * sizeof(sizeN);
641 static const struct got_error *
642 open_plain_object(struct got_object **obj, const char *path_packfile,
643 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
645 *obj = calloc(1, sizeof(**obj));
647 return got_error_from_errno();
649 (*obj)->path_packfile = strdup(path_packfile);
650 if ((*obj)->path_packfile == NULL) {
651 const struct got_error *err = got_error_from_errno();
658 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
661 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
662 (*obj)->pack_offset = offset;
667 static const struct got_error *
668 parse_negative_offset(int64_t *offset, size_t *len, int fd)
676 /* We do not support offset values which don't fit in 64 bit. */
678 return got_error(GOT_ERR_NO_SPACE);
680 n = read(fd, &offN, sizeof(offN));
682 return got_error_from_errno();
683 if (n != sizeof(offN))
684 return got_error(GOT_ERR_BAD_PACKFILE);
687 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
691 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
694 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
697 *len = i * sizeof(offN);
701 static const struct got_error *
702 parse_offset_delta(off_t *base_offset, int fd, off_t offset)
704 const struct got_error *err;
708 err = parse_negative_offset(&negoffset, &negofflen, fd);
712 /* Compute the base object's offset (must be in the same pack file). */
713 *base_offset = (offset - negoffset);
714 if (*base_offset <= 0)
715 return got_error(GOT_ERR_BAD_PACKFILE);
720 static const struct got_error *
721 resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
722 struct got_packidx *, struct got_pack *, off_t, size_t, int,
723 size_t, unsigned int);
725 static const struct got_error *
726 add_delta(struct got_delta_chain *deltas, const char *path_packfile,
727 off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
728 size_t delta_data_offset, uint8_t *delta_buf, size_t delta_len)
730 struct got_delta *delta;
732 delta = got_delta_open(path_packfile, delta_offset, tslen,
733 delta_type, delta_size, delta_data_offset, delta_buf,
736 return got_error_from_errno();
737 /* delta is freed in got_object_close() */
739 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
743 static const struct got_error *
744 resolve_offset_delta(struct got_delta_chain *deltas,
745 struct got_repository *repo, struct got_packidx *packidx,
746 struct got_pack *pack, off_t delta_offset, size_t tslen,
747 int delta_type, size_t delta_size, unsigned int recursion)
750 const struct got_error *err;
755 off_t delta_data_offset;
759 err = parse_offset_delta(&base_offset, pack->fd, delta_offset);
763 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
764 if (delta_data_offset == -1)
765 return got_error_from_errno();
767 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
771 err = add_delta(deltas, pack->path_packfile, delta_offset, tslen,
772 delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
776 /* An offset delta must be in the same packfile. */
777 if (base_offset >= pack->filesize)
778 return got_error(GOT_ERR_PACK_OFFSET);
779 if (lseek(pack->fd, base_offset, SEEK_SET) == -1)
780 return got_error_from_errno();
782 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
787 return resolve_delta_chain(deltas, repo, packidx, pack, base_offset,
788 base_tslen, base_type, base_size, recursion - 1);
791 static const struct got_error *
792 resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
793 struct got_packidx *packidx, struct got_pack *pack,
794 off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
795 unsigned int recursion)
797 const struct got_error *err;
798 struct got_object_id id;
805 off_t delta_data_offset;
809 n = read(pack->fd, &id, sizeof(id));
811 return got_error_from_errno();
813 return got_error(GOT_ERR_BAD_PACKFILE);
815 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
816 if (delta_data_offset == -1)
817 return got_error_from_errno();
819 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
823 err = add_delta(deltas, pack->path_packfile, delta_offset, tslen,
824 delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
828 /* Delta base must be in the same pack file. */
829 idx = get_object_idx(packidx, &id, repo);
831 return got_error(GOT_ERR_BAD_PACKFILE);
833 base_offset = get_object_offset(packidx, idx);
834 if (base_offset == (uint64_t)-1) {
835 return got_error(GOT_ERR_BAD_PACKIDX);
838 if (base_offset >= pack->filesize) {
839 err = got_error(GOT_ERR_PACK_OFFSET);
842 if (lseek(pack->fd, base_offset, SEEK_SET) == -1) {
843 err = got_error_from_errno();
847 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
852 err = resolve_delta_chain(deltas, repo, packidx, pack, base_offset,
853 base_tslen, base_type, base_size, recursion - 1);
858 static const struct got_error *
859 resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
860 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
861 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
863 const struct got_error *err = NULL;
865 if (--recursion == 0)
866 return got_error(GOT_ERR_RECURSION);
868 switch (delta_type) {
869 case GOT_OBJ_TYPE_COMMIT:
870 case GOT_OBJ_TYPE_TREE:
871 case GOT_OBJ_TYPE_BLOB:
872 case GOT_OBJ_TYPE_TAG:
873 /* Plain types are the final delta base. Recursion ends. */
874 err = add_delta(deltas, pack->path_packfile, delta_offset,
875 tslen, delta_type, delta_size, 0, NULL, 0);
877 case GOT_OBJ_TYPE_OFFSET_DELTA:
878 err = resolve_offset_delta(deltas, repo, packidx, pack,
879 delta_offset, tslen, delta_type, delta_size, recursion - 1);
881 case GOT_OBJ_TYPE_REF_DELTA:
882 err = resolve_ref_delta(deltas, repo, packidx, pack,
883 delta_offset, tslen, delta_type, delta_size, recursion - 1);
886 return got_error(GOT_ERR_OBJ_TYPE);
892 static const struct got_error *
893 open_delta_object(struct got_object **obj, struct got_repository *repo,
894 struct got_packidx *packidx, struct got_pack *pack,
895 struct got_object_id *id, off_t offset, size_t tslen,
896 int delta_type, size_t delta_size)
898 const struct got_error *err = NULL;
901 *obj = calloc(1, sizeof(**obj));
903 return got_error_from_errno();
907 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
908 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
909 (*obj)->pack_offset = offset + tslen;
911 (*obj)->path_packfile = strdup(pack->path_packfile);
912 if ((*obj)->path_packfile == NULL) {
913 err = got_error_from_errno();
916 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
918 SIMPLEQ_INIT(&(*obj)->deltas.entries);
919 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
921 err = resolve_delta_chain(&(*obj)->deltas, repo, packidx, pack, offset,
922 tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
926 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
929 (*obj)->type = resolved_type;
933 got_object_close(*obj);
939 static const struct got_error *
940 open_packed_object(struct got_object **obj, struct got_repository *repo,
941 struct got_packidx *packidx, int idx, struct got_object_id *id)
943 const struct got_error *err = NULL;
946 struct got_pack *pack;
953 offset = get_object_offset(packidx, idx);
954 if (offset == (uint64_t)-1)
955 return got_error(GOT_ERR_BAD_PACKIDX);
957 err = get_packfile_path(&path_packfile, repo, packidx);
961 pack = get_cached_pack(path_packfile, repo);
963 err = cache_pack(&pack, path_packfile, packidx, repo);
968 if (offset >= pack->filesize) {
969 err = got_error(GOT_ERR_PACK_OFFSET);
972 if (lseek(pack->fd, offset, SEEK_SET) == -1) {
973 err = got_error_from_errno();
977 err = parse_object_type_and_size(&type, &size, &tslen, pack->fd);
982 case GOT_OBJ_TYPE_COMMIT:
983 case GOT_OBJ_TYPE_TREE:
984 case GOT_OBJ_TYPE_BLOB:
985 case GOT_OBJ_TYPE_TAG:
986 err = open_plain_object(obj, path_packfile, id, type,
987 offset + tslen, size);
990 case GOT_OBJ_TYPE_OFFSET_DELTA:
991 case GOT_OBJ_TYPE_REF_DELTA:
992 err = open_delta_object(obj, repo, packidx, pack, id, offset,
997 err = got_error(GOT_ERR_OBJ_TYPE);
1001 free(path_packfile);
1005 const struct got_error *
1006 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1007 struct got_repository *repo)
1009 const struct got_error *err = NULL;
1010 struct got_packidx *packidx = NULL;
1013 err = search_packidx(&packidx, &idx, repo, id);
1017 err = open_packed_object(obj, repo, packidx, idx, id);
1021 err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1025 static const struct got_error *
1026 get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1028 struct got_delta *delta;
1029 uint64_t base_size = 0, result_size = 0;
1032 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1033 /* Plain object types are the delta base. */
1034 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1035 delta->type != GOT_OBJ_TYPE_TREE &&
1036 delta->type != GOT_OBJ_TYPE_BLOB &&
1037 delta->type != GOT_OBJ_TYPE_TAG) {
1038 const struct got_error *err;
1039 err = got_delta_get_sizes(&base_size, &result_size,
1040 delta->delta_buf, delta->delta_len);
1044 base_size = delta->size;
1045 if (base_size > *max_size)
1046 *max_size = base_size;
1047 if (result_size > *max_size)
1048 *max_size = result_size;
1054 static const struct got_error *
1055 dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1056 FILE *outfile, struct got_repository *repo)
1058 const struct got_error *err = NULL;
1059 struct got_delta *delta;
1060 FILE *base_file = NULL, *accum_file = NULL;
1061 uint8_t *base_buf = NULL, *accum_buf = NULL;
1062 size_t accum_size = 0;
1068 if (SIMPLEQ_EMPTY(&deltas->entries))
1069 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1071 /* We process small enough files entirely in memory for speed. */
1072 err = get_delta_chain_max_size(&max_size, deltas);
1075 if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1076 accum_buf = malloc(max_size);
1077 if (accum_buf == NULL)
1078 return got_error_from_errno();
1080 base_file = got_opentemp();
1081 if (base_file == NULL)
1082 return got_error_from_errno();
1084 accum_file = got_opentemp();
1085 if (accum_file == NULL) {
1086 err = got_error_from_errno();
1092 /* Deltas are ordered in ascending order. */
1093 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1095 struct got_pack *pack;
1097 off_t delta_data_offset;
1099 /* Plain object types are the delta base. */
1100 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1101 delta->type != GOT_OBJ_TYPE_TREE &&
1102 delta->type != GOT_OBJ_TYPE_BLOB &&
1103 delta->type != GOT_OBJ_TYPE_TAG) {
1104 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1108 pack = get_cached_pack(delta->path_packfile, repo);
1110 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1114 delta_data_offset = delta->offset + delta->tslen;
1115 if (delta_data_offset >= pack->filesize) {
1116 err = got_error(GOT_ERR_PACK_OFFSET);
1119 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1121 err = got_error_from_errno();
1125 err = got_inflate_to_file_fd(&base_len,
1126 pack->fd, base_file);
1128 err = got_inflate_to_mem_fd(&base_buf,
1129 &base_len, pack->fd);
1130 if (base_len < max_size) {
1132 p = reallocarray(base_buf, 1, max_size);
1134 err = got_error_from_errno();
1149 err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1150 delta->delta_len, accum_buf, &accum_size);
1153 err = got_delta_apply(base_file, delta->delta_buf,
1155 /* Final delta application writes to output file. */
1156 ++n < deltas->nentries ? accum_file : outfile,
1162 if (n < deltas->nentries) {
1163 /* Accumulated delta becomes the new base. */
1165 uint8_t *tmp = accum_buf;
1166 accum_buf = base_buf;
1169 FILE *tmp = accum_file;
1170 accum_file = base_file;
1181 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1183 if (len != accum_size)
1184 return got_ferror(outfile, GOT_ERR_IO);
1192 *result_size = accum_size;
1196 static const struct got_error *
1197 dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1198 struct got_delta_chain *deltas, struct got_repository *repo)
1200 const struct got_error *err = NULL;
1201 struct got_delta *delta;
1202 uint8_t *base_buf = NULL, *accum_buf = NULL;
1210 if (SIMPLEQ_EMPTY(&deltas->entries))
1211 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1213 err = get_delta_chain_max_size(&max_size, deltas);
1216 accum_buf = malloc(max_size);
1217 if (accum_buf == NULL)
1218 return got_error_from_errno();
1220 /* Deltas are ordered in ascending order. */
1221 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1223 struct got_pack *pack;
1225 size_t delta_data_offset;
1227 /* Plain object types are the delta base. */
1228 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1229 delta->type != GOT_OBJ_TYPE_TREE &&
1230 delta->type != GOT_OBJ_TYPE_BLOB &&
1231 delta->type != GOT_OBJ_TYPE_TAG) {
1232 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1236 pack = get_cached_pack(delta->path_packfile, repo);
1238 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1242 delta_data_offset = delta->offset + delta->tslen;
1243 if (delta_data_offset >= pack->filesize) {
1244 err = got_error(GOT_ERR_PACK_OFFSET);
1247 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1249 err = got_error_from_errno();
1252 err = got_inflate_to_mem_fd(&base_buf, &base_len,
1254 if (base_len < max_size) {
1256 p = reallocarray(base_buf, 1, max_size);
1258 err = got_error_from_errno();
1269 err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1270 delta->delta_len, accum_buf, &accum_size);
1275 if (n < deltas->nentries) {
1276 /* Accumulated delta becomes the new base. */
1277 uint8_t *tmp = accum_buf;
1278 accum_buf = base_buf;
1290 *outbuf = accum_buf;
1291 *outlen = accum_size;
1296 const struct got_error *
1297 got_packfile_extract_object(FILE **f, struct got_object *obj,
1298 struct got_repository *repo)
1300 const struct got_error *err = NULL;
1304 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1305 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1307 *f = got_opentemp();
1309 err = got_error_from_errno();
1313 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1314 struct got_pack *pack;
1316 pack = get_cached_pack(obj->path_packfile, repo);
1318 err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1323 if (obj->pack_offset >= pack->filesize) {
1324 err = got_error(GOT_ERR_PACK_OFFSET);
1327 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1328 err = got_error_from_errno();
1332 err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1334 err = dump_delta_chain_to_file(&obj->size,
1335 &obj->deltas, *f, repo);
1344 const struct got_error *
1345 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1346 struct got_object *obj, struct got_repository *repo)
1348 const struct got_error *err = NULL;
1350 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1351 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1353 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1354 struct got_pack *pack;
1356 pack = get_cached_pack(obj->path_packfile, repo);
1358 err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1363 if (obj->pack_offset >= pack->filesize) {
1364 err = got_error(GOT_ERR_PACK_OFFSET);
1367 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1368 err = got_error_from_errno();
1372 err = got_inflate_to_mem_fd(buf, len, pack->fd);
1374 err = dump_delta_chain_to_mem(buf, len, &obj->deltas, repo);