portable: include utf8d.h in dist tarball
[got-portable.git] / lib / pack.c
blob909e0630a41461a27ffa9e8dd02d673281d4e527
1 /*
2 * Copyright (c) 2018, 2019, 2020 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.
16 #include "got_compat.h"
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <unistd.h>
34 #include <zlib.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_path.h"
40 #include "got_lib_hash.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_delta_cache.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_qid.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 #endif
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 static const struct got_error *
59 verify_fanout_table(uint32_t *fanout_table)
61 int i;
63 for (i = 0; i < 0xff - 1; i++) {
64 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
65 return got_error(GOT_ERR_BAD_PACKIDX);
68 return NULL;
71 const struct got_error *
72 got_packidx_init_hdr(struct got_packidx *p, int verify, off_t packfile_size)
74 const struct got_error *err = NULL;
75 enum got_hash_algorithm algo = GOT_HASH_SHA1;
76 struct got_packidx_v2_hdr *h;
77 struct got_hash ctx;
78 uint8_t hash[GOT_HASH_DIGEST_MAXLEN];
79 size_t nobj, len_fanout, len_ids, offset, remain;
80 ssize_t n;
81 int i;
83 got_hash_init(&ctx, algo);
85 h = &p->hdr;
86 offset = 0;
87 remain = p->len;
89 if (remain < sizeof(*h->magic)) {
90 err = got_error(GOT_ERR_BAD_PACKIDX);
91 goto done;
93 if (p->map)
94 h->magic = (uint32_t *)(p->map + offset);
95 else {
96 h->magic = malloc(sizeof(*h->magic));
97 if (h->magic == NULL) {
98 err = got_error_from_errno("malloc");
99 goto done;
101 n = read(p->fd, h->magic, sizeof(*h->magic));
102 if (n < 0) {
103 err = got_error_from_errno("read");
104 goto done;
105 } else if (n != sizeof(*h->magic)) {
106 err = got_error(GOT_ERR_BAD_PACKIDX);
107 goto done;
110 if (*h->magic != htobe32(GOT_PACKIDX_V2_MAGIC)) {
111 err = got_error(GOT_ERR_BAD_PACKIDX);
112 goto done;
114 offset += sizeof(*h->magic);
115 remain -= sizeof(*h->magic);
117 if (verify)
118 got_hash_update(&ctx, h->magic, sizeof(*h->magic));
120 if (remain < sizeof(*h->version)) {
121 err = got_error(GOT_ERR_BAD_PACKIDX);
122 goto done;
124 if (p->map)
125 h->version = (uint32_t *)(p->map + offset);
126 else {
127 h->version = malloc(sizeof(*h->version));
128 if (h->version == NULL) {
129 err = got_error_from_errno("malloc");
130 goto done;
132 n = read(p->fd, h->version, sizeof(*h->version));
133 if (n < 0) {
134 err = got_error_from_errno("read");
135 goto done;
136 } else if (n != sizeof(*h->version)) {
137 err = got_error(GOT_ERR_BAD_PACKIDX);
138 goto done;
141 if (*h->version != htobe32(GOT_PACKIDX_VERSION)) {
142 err = got_error(GOT_ERR_BAD_PACKIDX);
143 goto done;
145 offset += sizeof(*h->version);
146 remain -= sizeof(*h->version);
148 if (verify)
149 got_hash_update(&ctx, h->version, sizeof(*h->version));
151 len_fanout =
152 sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
153 if (remain < len_fanout) {
154 err = got_error(GOT_ERR_BAD_PACKIDX);
155 goto done;
157 if (p->map)
158 h->fanout_table = (uint32_t *)(p->map + offset);
159 else {
160 h->fanout_table = malloc(len_fanout);
161 if (h->fanout_table == NULL) {
162 err = got_error_from_errno("malloc");
163 goto done;
165 n = read(p->fd, h->fanout_table, len_fanout);
166 if (n < 0) {
167 err = got_error_from_errno("read");
168 goto done;
169 } else if (n != len_fanout) {
170 err = got_error(GOT_ERR_BAD_PACKIDX);
171 goto done;
174 err = verify_fanout_table(h->fanout_table);
175 if (err)
176 goto done;
177 if (verify)
178 got_hash_update(&ctx, h->fanout_table, len_fanout);
179 offset += len_fanout;
180 remain -= len_fanout;
182 nobj = be32toh(h->fanout_table[0xff]);
183 len_ids = nobj * sizeof(*h->sorted_ids);
184 if (len_ids <= nobj || len_ids > remain) {
185 err = got_error(GOT_ERR_BAD_PACKIDX);
186 goto done;
188 if (p->map)
189 h->sorted_ids =
190 (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
191 else {
192 h->sorted_ids = malloc(len_ids);
193 if (h->sorted_ids == NULL) {
194 err = got_error(GOT_ERR_BAD_PACKIDX);
195 goto done;
197 n = read(p->fd, h->sorted_ids, len_ids);
198 if (n < 0)
199 err = got_error_from_errno("read");
200 else if (n != len_ids) {
201 err = got_error(GOT_ERR_BAD_PACKIDX);
202 goto done;
205 if (verify)
206 got_hash_update(&ctx, h->sorted_ids, len_ids);
207 offset += len_ids;
208 remain -= len_ids;
210 if (remain < nobj * sizeof(*h->crc32)) {
211 err = got_error(GOT_ERR_BAD_PACKIDX);
212 goto done;
214 if (p->map)
215 h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
216 else {
217 h->crc32 = malloc(nobj * sizeof(*h->crc32));
218 if (h->crc32 == NULL) {
219 err = got_error_from_errno("malloc");
220 goto done;
222 n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
223 if (n < 0)
224 err = got_error_from_errno("read");
225 else if (n != nobj * sizeof(*h->crc32)) {
226 err = got_error(GOT_ERR_BAD_PACKIDX);
227 goto done;
230 if (verify)
231 got_hash_update(&ctx, h->crc32, nobj * sizeof(*h->crc32));
232 remain -= nobj * sizeof(*h->crc32);
233 offset += nobj * sizeof(*h->crc32);
235 if (remain < nobj * sizeof(*h->offsets)) {
236 err = got_error(GOT_ERR_BAD_PACKIDX);
237 goto done;
239 if (p->map)
240 h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
241 else {
242 h->offsets = malloc(nobj * sizeof(*h->offsets));
243 if (h->offsets == NULL) {
244 err = got_error_from_errno("malloc");
245 goto done;
247 n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
248 if (n < 0)
249 err = got_error_from_errno("read");
250 else if (n != nobj * sizeof(*h->offsets)) {
251 err = got_error(GOT_ERR_BAD_PACKIDX);
252 goto done;
255 if (verify)
256 got_hash_update(&ctx, h->offsets, nobj * sizeof(*h->offsets));
257 remain -= nobj * sizeof(*h->offsets);
258 offset += nobj * sizeof(*h->offsets);
260 /* Large file offsets are contained only in files > 2GB. */
261 if (verify || packfile_size > 0x7fffffff) {
262 for (i = 0; i < nobj; i++) {
263 uint32_t o = h->offsets[i];
264 if (o & htobe32(GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX))
265 p->nlargeobj++;
268 if (p->nlargeobj == 0)
269 goto checksum;
270 else if (packfile_size <= 0x7fffffff) {
271 err = got_error(GOT_ERR_BAD_PACKIDX);
272 goto done;
275 if (remain < p->nlargeobj * sizeof(*h->large_offsets)) {
276 err = got_error(GOT_ERR_BAD_PACKIDX);
277 goto done;
279 if (p->map)
280 h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
281 else {
282 h->large_offsets = malloc(p->nlargeobj *
283 sizeof(*h->large_offsets));
284 if (h->large_offsets == NULL) {
285 err = got_error_from_errno("malloc");
286 goto done;
288 n = read(p->fd, h->large_offsets,
289 p->nlargeobj * sizeof(*h->large_offsets));
290 if (n < 0)
291 err = got_error_from_errno("read");
292 else if (n != p->nlargeobj * sizeof(*h->large_offsets)) {
293 err = got_error(GOT_ERR_BAD_PACKIDX);
294 goto done;
297 if (verify)
298 got_hash_update(&ctx, h->large_offsets,
299 p->nlargeobj * sizeof(*h->large_offsets));
300 remain -= p->nlargeobj * sizeof(*h->large_offsets);
301 offset += p->nlargeobj * sizeof(*h->large_offsets);
303 checksum:
304 if (remain < sizeof(*h->trailer)) {
305 err = got_error(GOT_ERR_BAD_PACKIDX);
306 goto done;
308 if (p->map)
309 h->trailer =
310 (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
311 else {
312 h->trailer = malloc(sizeof(*h->trailer));
313 if (h->trailer == NULL) {
314 err = got_error_from_errno("malloc");
315 goto done;
317 n = read(p->fd, h->trailer, sizeof(*h->trailer));
318 if (n < 0)
319 err = got_error_from_errno("read");
320 else if (n != sizeof(*h->trailer)) {
321 err = got_error(GOT_ERR_BAD_PACKIDX);
322 goto done;
325 if (verify) {
326 got_hash_update(&ctx, h->trailer->packfile_sha1,
327 SHA1_DIGEST_LENGTH);
328 got_hash_final(&ctx, hash);
329 if (got_hash_cmp(ctx.algo, hash, h->trailer->packidx_sha1) != 0)
330 err = got_error(GOT_ERR_PACKIDX_CSUM);
332 done:
333 return err;
336 const struct got_error *
337 got_packidx_open(struct got_packidx **packidx,
338 int dir_fd, const char *relpath, int verify)
340 const struct got_error *err = NULL;
341 struct got_packidx *p = NULL;
342 char *pack_relpath;
343 struct stat idx_sb, pack_sb;
345 *packidx = NULL;
347 err = got_packidx_get_packfile_path(&pack_relpath, relpath);
348 if (err)
349 return err;
352 * Ensure that a corresponding pack file exists.
353 * Some Git repositories have this problem. Git seems to ignore
354 * the existence of lonely pack index files but we do not.
356 if (fstatat(dir_fd, pack_relpath, &pack_sb, 0) == -1) {
357 if (errno == ENOENT) {
358 err = got_error_fmt(GOT_ERR_LONELY_PACKIDX,
359 "%s", relpath);
360 } else
361 err = got_error_from_errno2("fstatat", pack_relpath);
362 goto done;
365 p = calloc(1, sizeof(*p));
366 if (p == NULL) {
367 err = got_error_from_errno("calloc");
368 goto done;
371 p->fd = openat(dir_fd, relpath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
372 if (p->fd == -1) {
373 err = got_error_from_errno2("openat", relpath);
374 goto done;
377 if (fstat(p->fd, &idx_sb) != 0) {
378 err = got_error_from_errno2("fstat", relpath);
379 goto done;
381 p->len = idx_sb.st_size;
382 if (p->len < sizeof(p->hdr)) {
383 err = got_error(GOT_ERR_BAD_PACKIDX);
384 goto done;
387 p->path_packidx = strdup(relpath);
388 if (p->path_packidx == NULL) {
389 err = got_error_from_errno("strdup");
390 goto done;
393 #ifndef GOT_PACK_NO_MMAP
394 if (p->len > 0 && p->len <= SIZE_MAX) {
395 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
396 if (p->map == MAP_FAILED) {
397 if (errno != ENOMEM) {
398 err = got_error_from_errno("mmap");
399 goto done;
401 p->map = NULL; /* fall back to read(2) */
404 #endif
406 err = got_packidx_init_hdr(p, verify, pack_sb.st_size);
407 done:
408 if (err) {
409 if (p)
410 got_packidx_close(p);
411 } else
412 *packidx = p;
413 free(pack_relpath);
414 return err;
417 const struct got_error *
418 got_packidx_close(struct got_packidx *packidx)
420 const struct got_error *err = NULL;
422 free(packidx->path_packidx);
423 if (packidx->map) {
424 if (munmap(packidx->map, packidx->len) == -1)
425 err = got_error_from_errno("munmap");
426 } else {
427 free(packidx->hdr.magic);
428 free(packidx->hdr.version);
429 free(packidx->hdr.fanout_table);
430 free(packidx->hdr.sorted_ids);
431 free(packidx->hdr.crc32);
432 free(packidx->hdr.offsets);
433 free(packidx->hdr.large_offsets);
434 free(packidx->hdr.trailer);
436 if (close(packidx->fd) == -1 && err == NULL)
437 err = got_error_from_errno("close");
438 free(packidx->sorted_offsets);
439 free(packidx->sorted_large_offsets);
440 free(packidx);
442 return err;
445 const struct got_error *
446 got_packidx_get_packfile_path(char **path_packfile, const char *path_packidx)
448 size_t size;
450 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
451 size = strlen(path_packidx) + 2;
452 if (size < GOT_PACKFILE_NAMELEN + 1)
453 return got_error_path(path_packidx, GOT_ERR_BAD_PATH);
455 *path_packfile = malloc(size);
456 if (*path_packfile == NULL)
457 return got_error_from_errno("malloc");
459 /* Copy up to and excluding ".idx". */
460 if (strlcpy(*path_packfile, path_packidx,
461 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
462 return got_error(GOT_ERR_NO_SPACE);
464 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
465 return got_error(GOT_ERR_NO_SPACE);
467 return NULL;
470 off_t
471 got_packidx_get_object_offset(struct got_packidx *packidx, int idx)
473 uint32_t offset = be32toh(packidx->hdr.offsets[idx]);
474 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
475 uint64_t loffset;
476 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
477 if (idx < 0 || idx >= packidx->nlargeobj ||
478 packidx->hdr.large_offsets == NULL)
479 return -1;
480 loffset = be64toh(packidx->hdr.large_offsets[idx]);
481 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
483 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
487 got_packidx_get_object_idx(struct got_packidx *packidx,
488 struct got_object_id *id)
490 u_int8_t id0 = id->sha1[0];
491 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
492 int left = 0, right = totobj - 1;
494 if (id0 > 0)
495 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
497 while (left <= right) {
498 struct got_packidx_object_id *oid;
499 int i, cmp;
501 i = ((left + right) / 2);
502 oid = &packidx->hdr.sorted_ids[i];
503 cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
504 if (cmp == 0)
505 return i;
506 else if (cmp > 0)
507 left = i + 1;
508 else if (cmp < 0)
509 right = i - 1;
512 return -1;
515 static int
516 offset_cmp(const void *pa, const void *pb)
518 const struct got_pack_offset_index *a, *b;
520 a = (const struct got_pack_offset_index *)pa;
521 b = (const struct got_pack_offset_index *)pb;
523 if (a->offset < b->offset)
524 return -1;
525 else if (a->offset > b->offset)
526 return 1;
528 return 0;
531 static int
532 large_offset_cmp(const void *pa, const void *pb)
534 const struct got_pack_large_offset_index *a, *b;
536 a = (const struct got_pack_large_offset_index *)pa;
537 b = (const struct got_pack_large_offset_index *)pb;
539 if (a->offset < b->offset)
540 return -1;
541 else if (a->offset > b->offset)
542 return 1;
544 return 0;
547 static const struct got_error *
548 build_offset_index(struct got_packidx *p)
550 uint32_t nobj = be32toh(p->hdr.fanout_table[0xff]);
551 unsigned int i, j, k;
553 p->sorted_offsets = calloc(nobj - p->nlargeobj,
554 sizeof(p->sorted_offsets[0]));
555 if (p->sorted_offsets == NULL)
556 return got_error_from_errno("calloc");
558 if (p->nlargeobj > 0) {
559 p->sorted_large_offsets = calloc(p->nlargeobj,
560 sizeof(p->sorted_large_offsets[0]));
561 if (p->sorted_large_offsets == NULL)
562 return got_error_from_errno("calloc");
565 j = 0;
566 k = 0;
567 for (i = 0; i < nobj; i++) {
568 uint32_t offset = be32toh(p->hdr.offsets[i]);
569 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
570 uint64_t loffset;
571 uint32_t idx;
572 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
573 if (idx >= p->nlargeobj ||
574 p->nlargeobj == 0 ||
575 p->hdr.large_offsets == NULL)
576 return got_error(GOT_ERR_BAD_PACKIDX);
577 loffset = be64toh(p->hdr.large_offsets[idx]);
578 p->sorted_large_offsets[j].offset = loffset;
579 p->sorted_large_offsets[j].idx = i;
580 j++;
581 } else {
582 p->sorted_offsets[k].offset = offset;
583 p->sorted_offsets[k].idx = i;
584 k++;
587 if (j != p->nlargeobj || k != nobj - p->nlargeobj)
588 return got_error(GOT_ERR_BAD_PACKIDX);
590 qsort(p->sorted_offsets, nobj - p->nlargeobj,
591 sizeof(p->sorted_offsets[0]), offset_cmp);
593 if (p->sorted_large_offsets)
594 qsort(p->sorted_large_offsets, p->nlargeobj,
595 sizeof(p->sorted_large_offsets[0]), large_offset_cmp);
597 return NULL;
600 const struct got_error *
601 got_packidx_get_offset_idx(int *idx, struct got_packidx *packidx, off_t offset)
603 const struct got_error *err;
604 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
605 int i, left, right;
607 *idx = -1;
609 if (packidx->sorted_offsets == NULL) {
610 err = build_offset_index(packidx);
611 if (err)
612 return err;
615 if (offset >= 0x7fffffff) {
616 uint64_t lo;
617 left = 0, right = packidx->nlargeobj - 1;
618 while (left <= right) {
619 i = ((left + right) / 2);
620 lo = packidx->sorted_large_offsets[i].offset;
621 if (lo == offset) {
622 *idx = packidx->sorted_large_offsets[i].idx;
623 break;
624 } else if (offset > lo)
625 left = i + 1;
626 else if (offset < lo)
627 right = i - 1;
629 } else {
630 uint32_t o;
631 left = 0, right = totobj - packidx->nlargeobj - 1;
632 while (left <= right) {
633 i = ((left + right) / 2);
634 o = packidx->sorted_offsets[i].offset;
635 if (o == offset) {
636 *idx = packidx->sorted_offsets[i].idx;
637 break;
638 } else if (offset > o)
639 left = i + 1;
640 else if (offset < o)
641 right = i - 1;
645 return NULL;
648 const struct got_error *
649 got_packidx_get_object_id(struct got_object_id *id,
650 struct got_packidx *packidx, int idx)
652 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
653 struct got_packidx_object_id *oid;
655 if (idx < 0 || idx >= totobj)
656 return got_error(GOT_ERR_NO_OBJ);
658 oid = &packidx->hdr.sorted_ids[idx];
659 memcpy(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
660 return NULL;
663 const struct got_error *
664 got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
665 struct got_packidx *packidx, const char *id_str_prefix)
667 const struct got_error *err = NULL;
668 u_int8_t id0;
669 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
670 char hex[3];
671 size_t prefix_len = strlen(id_str_prefix);
672 struct got_packidx_object_id *oid;
673 uint32_t i = 0;
675 if (prefix_len < 2)
676 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
678 hex[0] = id_str_prefix[0];
679 hex[1] = id_str_prefix[1];
680 hex[2] = '\0';
681 if (!got_parse_xdigit(&id0, hex))
682 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
684 if (id0 > 0)
685 i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
686 oid = &packidx->hdr.sorted_ids[i];
687 while (i < totobj && oid->sha1[0] == id0) {
688 char id_str[SHA1_DIGEST_STRING_LENGTH];
689 struct got_object_qid *qid;
690 int cmp;
692 if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
693 return got_error(GOT_ERR_NO_SPACE);
695 cmp = strncmp(id_str, id_str_prefix, prefix_len);
696 if (cmp < 0) {
697 oid = &packidx->hdr.sorted_ids[++i];
698 continue;
699 } else if (cmp > 0)
700 break;
702 err = got_object_qid_alloc_partial(&qid);
703 if (err)
704 return err;
705 memcpy(qid->id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
706 STAILQ_INSERT_TAIL(matched_ids, qid, entry);
708 oid = &packidx->hdr.sorted_ids[++i];
711 return NULL;
714 static void
715 set_max_datasize(void)
717 struct rlimit rl;
719 if (getrlimit(RLIMIT_DATA, &rl) != 0)
720 return;
722 rl.rlim_cur = rl.rlim_max;
723 setrlimit(RLIMIT_DATA, &rl);
726 const struct got_error *
727 got_pack_start_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
729 const struct got_error *err = NULL;
730 int imsg_fds[2];
731 pid_t pid;
732 struct imsgbuf *ibuf;
734 ibuf = calloc(1, sizeof(*ibuf));
735 if (ibuf == NULL)
736 return got_error_from_errno("calloc");
738 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
739 if (pack->privsep_child == NULL) {
740 err = got_error_from_errno("calloc");
741 free(ibuf);
742 return err;
744 pack->child_has_tempfiles = 0;
745 pack->child_has_delta_outfd = 0;
747 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
748 err = got_error_from_errno("socketpair");
749 goto done;
752 pid = fork();
753 if (pid == -1) {
754 err = got_error_from_errno("fork");
755 goto done;
756 } else if (pid == 0) {
757 set_max_datasize();
758 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
759 pack->path_packfile);
760 /* not reached */
763 if (close(imsg_fds[1]) == -1)
764 return got_error_from_errno("close");
765 pack->privsep_child->imsg_fd = imsg_fds[0];
766 pack->privsep_child->pid = pid;
767 imsg_init(ibuf, imsg_fds[0]);
768 pack->privsep_child->ibuf = ibuf;
770 err = got_privsep_init_pack_child(ibuf, pack, packidx);
771 if (err) {
772 const struct got_error *child_err;
773 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
774 child_err = got_privsep_wait_for_child(
775 pack->privsep_child->pid);
776 if (child_err && err == NULL)
777 err = child_err;
779 done:
780 if (err) {
781 free(ibuf);
782 free(pack->privsep_child);
783 pack->privsep_child = NULL;
785 return err;
788 static const struct got_error *
789 pack_stop_privsep_child(struct got_pack *pack)
791 const struct got_error *err = NULL, *close_err = NULL;
793 if (pack->privsep_child == NULL)
794 return NULL;
796 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
797 if (err)
798 return err;
799 if (close(pack->privsep_child->imsg_fd) == -1)
800 close_err = got_error_from_errno("close");
801 err = got_privsep_wait_for_child(pack->privsep_child->pid);
802 if (close_err && err == NULL)
803 err = close_err;
804 imsg_clear(pack->privsep_child->ibuf);
805 free(pack->privsep_child->ibuf);
806 free(pack->privsep_child);
807 pack->privsep_child = NULL;
808 return err;
811 const struct got_error *
812 got_pack_close(struct got_pack *pack)
814 const struct got_error *err = NULL;
816 err = pack_stop_privsep_child(pack);
817 if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
818 err = got_error_from_errno("munmap");
819 if (pack->fd != -1 && close(pack->fd) == -1 && err == NULL)
820 err = got_error_from_errno("close");
821 pack->fd = -1;
822 free(pack->path_packfile);
823 pack->path_packfile = NULL;
824 pack->filesize = 0;
825 if (pack->delta_cache) {
826 got_delta_cache_free(pack->delta_cache);
827 pack->delta_cache = NULL;
831 * Leave accumfd and basefd alone. They are managed by the
832 * repository layer and can be reused.
835 return err;
838 const struct got_error *
839 got_pack_parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
840 struct got_pack *pack, off_t offset)
842 uint8_t t = 0;
843 uint64_t s = 0;
844 uint8_t sizeN;
845 size_t mapoff = 0;
846 int i = 0;
848 *len = 0;
850 if (offset >= pack->filesize)
851 return got_error(GOT_ERR_PACK_OFFSET);
853 if (pack->map) {
854 if (offset > SIZE_MAX) {
855 return got_error_fmt(GOT_ERR_PACK_OFFSET,
856 "offset %lld overflows size_t",
857 (long long)offset);
860 mapoff = (size_t)offset;
861 } else {
862 if (lseek(pack->fd, offset, SEEK_SET) == -1)
863 return got_error_from_errno("lseek");
866 do {
867 /* We do not support size values which don't fit in 64 bit. */
868 if (i > 9)
869 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
870 "packfile offset %lld", (long long)offset);
872 if (pack->map) {
873 if (mapoff + sizeof(sizeN) >= pack->filesize)
874 return got_error(GOT_ERR_BAD_PACKFILE);
875 sizeN = *(pack->map + mapoff);
876 mapoff += sizeof(sizeN);
877 } else {
878 ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
879 if (n < 0)
880 return got_error_from_errno("read");
881 if (n != sizeof(sizeN))
882 return got_error(GOT_ERR_BAD_PACKFILE);
884 *len += sizeof(sizeN);
886 if (i == 0) {
887 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
888 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
889 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
890 } else {
891 size_t shift = 4 + 7 * (i - 1);
892 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
894 i++;
895 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
897 *type = t;
898 *size = s;
899 return NULL;
902 static const struct got_error *
903 open_plain_object(struct got_object **obj, struct got_object_id *id,
904 uint8_t type, off_t offset, size_t size, int idx)
906 *obj = calloc(1, sizeof(**obj));
907 if (*obj == NULL)
908 return got_error_from_errno("calloc");
910 (*obj)->type = type;
911 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
912 (*obj)->pack_idx = idx;
913 (*obj)->hdrlen = 0;
914 (*obj)->size = size;
915 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
916 (*obj)->pack_offset = offset;
918 return NULL;
921 static const struct got_error *
922 parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
923 off_t delta_offset)
925 int64_t o = 0;
926 uint8_t offN;
927 int i = 0;
929 *offset = 0;
930 *len = 0;
932 do {
933 /* We do not support offset values which don't fit in 64 bit. */
934 if (i > 8)
935 return got_error(GOT_ERR_NO_SPACE);
937 if (pack->map) {
938 size_t mapoff;
940 if (delta_offset + *len > SIZE_MAX) {
941 return got_error_fmt(GOT_ERR_PACK_OFFSET,
942 "mapoff %lld would overflow size_t",
943 (long long)delta_offset + *len);
946 mapoff = (size_t)delta_offset + *len;
947 if (mapoff + sizeof(offN) >= pack->filesize)
948 return got_error(GOT_ERR_PACK_OFFSET);
949 offN = *(pack->map + mapoff);
950 } else {
951 ssize_t n;
952 n = read(pack->fd, &offN, sizeof(offN));
953 if (n < 0)
954 return got_error_from_errno("read");
955 if (n != sizeof(offN))
956 return got_error(GOT_ERR_BAD_PACKFILE);
958 *len += sizeof(offN);
960 if (i == 0)
961 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
962 else {
963 o++;
964 o <<= 7;
965 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
967 i++;
968 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
970 *offset = o;
971 return NULL;
974 const struct got_error *
975 got_pack_parse_offset_delta(off_t *base_offset, size_t *len,
976 struct got_pack *pack, off_t offset, size_t tslen)
978 const struct got_error *err;
979 int64_t negoffset;
980 size_t negofflen;
982 *len = 0;
984 err = parse_negative_offset(&negoffset, &negofflen, pack,
985 offset + tslen);
986 if (err)
987 return err;
989 /* Compute the base object's offset (must be in the same pack file). */
990 *base_offset = (offset - negoffset);
991 if (*base_offset <= 0)
992 return got_error(GOT_ERR_BAD_PACKFILE);
994 *len = negofflen;
995 return NULL;
998 static const struct got_error *
999 read_delta_data(uint8_t **delta_buf, size_t *delta_len,
1000 size_t *delta_compressed_len, size_t delta_data_offset,
1001 struct got_pack *pack)
1003 const struct got_error *err = NULL;
1004 size_t consumed = 0;
1006 if (pack->map) {
1007 if (delta_data_offset >= pack->filesize)
1008 return got_error(GOT_ERR_PACK_OFFSET);
1009 err = got_inflate_to_mem_mmap(delta_buf, delta_len,
1010 &consumed, NULL, pack->map, delta_data_offset,
1011 pack->filesize - delta_data_offset);
1012 if (err)
1013 return err;
1014 } else {
1015 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
1016 return got_error_from_errno("lseek");
1017 err = got_inflate_to_mem_fd(delta_buf, delta_len,
1018 &consumed, NULL, 0, pack->fd);
1019 if (err)
1020 return err;
1023 if (delta_compressed_len)
1024 *delta_compressed_len = consumed;
1026 return NULL;
1029 static const struct got_error *
1030 add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
1031 int delta_type, size_t delta_size, off_t delta_data_offset)
1033 struct got_delta *delta;
1035 delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
1036 delta_data_offset);
1037 if (delta == NULL)
1038 return got_error_from_errno("got_delta_open");
1039 /* delta is freed in got_object_close() */
1040 deltas->nentries++;
1041 STAILQ_INSERT_HEAD(&deltas->entries, delta, entry);
1042 return NULL;
1045 static const struct got_error *
1046 resolve_offset_delta(struct got_delta_chain *deltas,
1047 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1048 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1050 const struct got_error *err;
1051 off_t base_offset;
1052 uint8_t base_type;
1053 uint64_t base_size;
1054 size_t base_tslen;
1055 off_t delta_data_offset;
1056 size_t consumed;
1058 err = got_pack_parse_offset_delta(&base_offset, &consumed, pack,
1059 delta_offset, tslen);
1060 if (err)
1061 return err;
1063 delta_data_offset = delta_offset + tslen + consumed;
1064 if (delta_data_offset >= pack->filesize)
1065 return got_error(GOT_ERR_PACK_OFFSET);
1067 if (pack->map == NULL) {
1068 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1069 if (delta_data_offset == -1)
1070 return got_error_from_errno("lseek");
1073 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1074 delta_data_offset);
1075 if (err)
1076 return err;
1078 /* An offset delta must be in the same packfile. */
1079 if (base_offset >= pack->filesize)
1080 return got_error(GOT_ERR_PACK_OFFSET);
1082 err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1083 &base_tslen, pack, base_offset);
1084 if (err)
1085 return err;
1087 return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1088 base_tslen, base_type, base_size, recursion - 1);
1091 const struct got_error *
1092 got_pack_parse_ref_delta(struct got_object_id *id,
1093 struct got_pack *pack, off_t delta_offset, int tslen)
1095 if (pack->map) {
1096 size_t mapoff;
1098 if (delta_offset + tslen > SIZE_MAX) {
1099 return got_error_fmt(GOT_ERR_PACK_OFFSET,
1100 "mapoff %lld would overflow size_t",
1101 (long long)delta_offset + tslen);
1104 mapoff = delta_offset + tslen;
1105 if (mapoff + sizeof(*id) >= pack->filesize)
1106 return got_error(GOT_ERR_PACK_OFFSET);
1107 memcpy(id, pack->map + mapoff, sizeof(*id));
1108 } else {
1109 ssize_t n;
1110 n = read(pack->fd, id, sizeof(*id));
1111 if (n < 0)
1112 return got_error_from_errno("read");
1113 if (n != sizeof(*id))
1114 return got_error(GOT_ERR_BAD_PACKFILE);
1117 return NULL;
1120 static const struct got_error *
1121 resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
1122 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
1123 size_t delta_size, unsigned int recursion)
1125 const struct got_error *err;
1126 struct got_object_id id;
1127 int idx;
1128 off_t base_offset;
1129 uint8_t base_type;
1130 uint64_t base_size;
1131 size_t base_tslen;
1132 off_t delta_data_offset;
1134 if (delta_offset + tslen >= pack->filesize)
1135 return got_error(GOT_ERR_PACK_OFFSET);
1137 err = got_pack_parse_ref_delta(&id, pack, delta_offset, tslen);
1138 if (err)
1139 return err;
1140 if (pack->map) {
1141 delta_data_offset = delta_offset + tslen + SHA1_DIGEST_LENGTH;
1142 } else {
1143 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1144 if (delta_data_offset == -1)
1145 return got_error_from_errno("lseek");
1148 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1149 delta_data_offset);
1150 if (err)
1151 return err;
1153 /* Delta base must be in the same pack file. */
1154 idx = got_packidx_get_object_idx(packidx, &id);
1155 if (idx == -1)
1156 return got_error(GOT_ERR_NO_OBJ);
1158 base_offset = got_packidx_get_object_offset(packidx, idx);
1159 if (base_offset == -1)
1160 return got_error(GOT_ERR_BAD_PACKIDX);
1162 if (base_offset >= pack->filesize)
1163 return got_error(GOT_ERR_PACK_OFFSET);
1165 err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1166 &base_tslen, pack, base_offset);
1167 if (err)
1168 return err;
1170 return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1171 base_tslen, base_type, base_size, recursion - 1);
1174 const struct got_error *
1175 got_pack_resolve_delta_chain(struct got_delta_chain *deltas,
1176 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1177 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1179 const struct got_error *err = NULL;
1181 if (--recursion == 0)
1182 return got_error(GOT_ERR_RECURSION);
1184 switch (delta_type) {
1185 case GOT_OBJ_TYPE_COMMIT:
1186 case GOT_OBJ_TYPE_TREE:
1187 case GOT_OBJ_TYPE_BLOB:
1188 case GOT_OBJ_TYPE_TAG:
1189 /* Plain types are the final delta base. Recursion ends. */
1190 err = add_delta(deltas, delta_offset, tslen, delta_type,
1191 delta_size, 0);
1192 break;
1193 case GOT_OBJ_TYPE_OFFSET_DELTA:
1194 err = resolve_offset_delta(deltas, packidx, pack,
1195 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1196 break;
1197 case GOT_OBJ_TYPE_REF_DELTA:
1198 err = resolve_ref_delta(deltas, packidx, pack,
1199 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1200 break;
1201 default:
1202 return got_error(GOT_ERR_OBJ_TYPE);
1205 return err;
1208 static const struct got_error *
1209 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
1210 struct got_pack *pack, struct got_object_id *id, off_t offset,
1211 size_t tslen, int delta_type, size_t delta_size, int idx)
1213 const struct got_error *err = NULL;
1214 int resolved_type;
1216 *obj = calloc(1, sizeof(**obj));
1217 if (*obj == NULL)
1218 return got_error_from_errno("calloc");
1220 (*obj)->flags = 0;
1221 (*obj)->hdrlen = 0;
1222 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1223 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1224 (*obj)->pack_offset = offset + tslen;
1226 STAILQ_INIT(&(*obj)->deltas.entries);
1227 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1228 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1229 (*obj)->pack_idx = idx;
1231 err = got_pack_resolve_delta_chain(&(*obj)->deltas, packidx, pack,
1232 offset, tslen, delta_type, delta_size,
1233 GOT_DELTA_CHAIN_RECURSION_MAX);
1234 if (err)
1235 goto done;
1237 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1238 if (err)
1239 goto done;
1240 (*obj)->type = resolved_type;
1241 done:
1242 if (err) {
1243 got_object_close(*obj);
1244 *obj = NULL;
1246 return err;
1249 const struct got_error *
1250 got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
1251 struct got_packidx *packidx, int idx, struct got_object_id *id)
1253 const struct got_error *err = NULL;
1254 off_t offset;
1255 uint8_t type;
1256 uint64_t size;
1257 size_t tslen;
1259 *obj = NULL;
1261 offset = got_packidx_get_object_offset(packidx, idx);
1262 if (offset == -1)
1263 return got_error(GOT_ERR_BAD_PACKIDX);
1265 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1266 pack, offset);
1267 if (err)
1268 return err;
1270 switch (type) {
1271 case GOT_OBJ_TYPE_COMMIT:
1272 case GOT_OBJ_TYPE_TREE:
1273 case GOT_OBJ_TYPE_BLOB:
1274 case GOT_OBJ_TYPE_TAG:
1275 err = open_plain_object(obj, id, type, offset + tslen,
1276 size, idx);
1277 break;
1278 case GOT_OBJ_TYPE_OFFSET_DELTA:
1279 case GOT_OBJ_TYPE_REF_DELTA:
1280 err = open_delta_object(obj, packidx, pack, id, offset,
1281 tslen, type, size, idx);
1282 break;
1283 default:
1284 err = got_error(GOT_ERR_OBJ_TYPE);
1285 break;
1288 return err;
1291 const struct got_error *
1292 got_pack_get_delta_chain_max_size(uint64_t *max_size,
1293 struct got_delta_chain *deltas, struct got_pack *pack)
1295 struct got_delta *delta;
1296 uint64_t base_size = 0, result_size = 0;
1298 *max_size = 0;
1299 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1300 /* Plain object types are the delta base. */
1301 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1302 delta->type != GOT_OBJ_TYPE_TREE &&
1303 delta->type != GOT_OBJ_TYPE_BLOB &&
1304 delta->type != GOT_OBJ_TYPE_TAG) {
1305 const struct got_error *err;
1306 uint8_t *delta_buf = NULL;
1307 size_t delta_len;
1308 int cached = 1;
1310 if (pack->delta_cache) {
1311 got_delta_cache_get(&delta_buf, &delta_len,
1312 NULL, NULL, pack->delta_cache,
1313 delta->data_offset);
1315 if (delta_buf == NULL) {
1316 cached = 0;
1317 err = read_delta_data(&delta_buf, &delta_len,
1318 NULL, delta->data_offset, pack);
1319 if (err)
1320 return err;
1322 if (pack->delta_cache && !cached) {
1323 err = got_delta_cache_add(pack->delta_cache,
1324 delta->data_offset, delta_buf, delta_len);
1325 if (err == NULL)
1326 cached = 1;
1327 else if (err->code != GOT_ERR_NO_SPACE) {
1328 free(delta_buf);
1329 return err;
1332 err = got_delta_get_sizes(&base_size, &result_size,
1333 delta_buf, delta_len);
1334 if (!cached)
1335 free(delta_buf);
1336 if (err)
1337 return err;
1338 } else
1339 base_size = delta->size;
1340 if (base_size > *max_size)
1341 *max_size = base_size;
1342 if (result_size > *max_size)
1343 *max_size = result_size;
1346 return NULL;
1349 const struct got_error *
1350 got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj,
1351 struct got_pack *pack)
1353 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1354 return got_error(GOT_ERR_OBJ_TYPE);
1356 return got_pack_get_delta_chain_max_size(size, &obj->deltas, pack);
1359 const struct got_error *
1360 got_pack_dump_delta_chain_to_file(size_t *result_size,
1361 struct got_delta_chain *deltas, struct got_pack *pack, FILE *outfile,
1362 FILE *base_file, FILE *accum_file)
1364 const struct got_error *err = NULL;
1365 struct got_delta *delta;
1366 uint8_t *base_buf = NULL, *accum_buf = NULL;
1367 size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0;
1368 /* We process small enough files entirely in memory for speed. */
1369 const size_t max_bufsize = GOT_DELTA_RESULT_SIZE_CACHED_MAX;
1370 uint64_t max_size = 0;
1371 int n = 0;
1373 *result_size = 0;
1375 if (STAILQ_EMPTY(&deltas->entries))
1376 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1378 if (pack->delta_cache) {
1379 uint8_t *delta_buf = NULL, *fulltext = NULL;
1380 size_t delta_len, fulltext_len;
1382 delta = STAILQ_LAST(&deltas->entries, got_delta, entry);
1383 got_delta_cache_get(&delta_buf, &delta_len,
1384 &fulltext, &fulltext_len,
1385 pack->delta_cache, delta->data_offset);
1386 if (fulltext) {
1387 size_t w;
1389 w = fwrite(fulltext, 1, fulltext_len, outfile);
1390 if (w != fulltext_len)
1391 return got_ferror(outfile, GOT_ERR_IO);
1392 if (fflush(outfile) != 0)
1393 return got_error_from_errno("fflush");
1394 *result_size = fulltext_len;
1395 return NULL;
1399 if (fseeko(base_file, 0L, SEEK_SET) == -1)
1400 return got_error_from_errno("fseeko");
1401 if (fseeko(accum_file, 0L, SEEK_SET) == -1)
1402 return got_error_from_errno("fseeko");
1404 /* Deltas are ordered in ascending order. */
1405 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1406 uint8_t *delta_buf = NULL, *fulltext = NULL;
1407 size_t delta_len, fulltext_len;
1408 uint64_t base_size, result_size = 0;
1409 int cached = 1;
1410 if (n == 0) {
1411 size_t mapoff;
1412 off_t delta_data_offset;
1414 /* Plain object types are the delta base. */
1415 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1416 delta->type != GOT_OBJ_TYPE_TREE &&
1417 delta->type != GOT_OBJ_TYPE_BLOB &&
1418 delta->type != GOT_OBJ_TYPE_TAG) {
1419 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1420 goto done;
1423 delta_data_offset = delta->offset + delta->tslen;
1424 if (delta_data_offset >= pack->filesize) {
1425 err = got_error(GOT_ERR_PACK_OFFSET);
1426 goto done;
1428 if (pack->map == NULL) {
1429 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1430 == -1) {
1431 err = got_error_from_errno("lseek");
1432 goto done;
1435 if (delta->size > max_size)
1436 max_size = delta->size;
1437 if (max_size > max_bufsize) {
1438 if (pack->map) {
1439 if (delta_data_offset > SIZE_MAX) {
1440 return got_error_fmt(
1441 GOT_ERR_RANGE,
1442 "delta offset %lld "
1443 "overflows size_t",
1444 (long long)
1445 delta_data_offset);
1448 mapoff = delta_data_offset;
1449 err = got_inflate_to_file_mmap(
1450 &base_bufsz, NULL, NULL, pack->map,
1451 mapoff, pack->filesize - mapoff,
1452 base_file);
1453 } else
1454 err = got_inflate_to_file_fd(
1455 &base_bufsz, NULL, NULL, pack->fd,
1456 base_file);
1457 } else {
1458 accum_buf = malloc(max_size);
1459 if (accum_buf == NULL) {
1460 err = got_error_from_errno("malloc");
1461 goto done;
1463 accum_bufsz = max_size;
1464 if (pack->map) {
1465 if (delta_data_offset > SIZE_MAX) {
1466 err = got_error_fmt(
1467 GOT_ERR_RANGE,
1468 "delta offset %lld "
1469 "overflows size_t",
1470 (long long)
1471 delta_data_offset);
1472 goto done;
1475 mapoff = delta_data_offset;
1476 err = got_inflate_to_mem_mmap(&base_buf,
1477 &base_bufsz, NULL, NULL,
1478 pack->map, mapoff,
1479 pack->filesize - mapoff);
1480 } else
1481 err = got_inflate_to_mem_fd(&base_buf,
1482 &base_bufsz, NULL, NULL, max_size,
1483 pack->fd);
1485 if (err)
1486 goto done;
1487 n++;
1488 if (base_buf == NULL)
1489 rewind(base_file);
1490 else if (pack->delta_cache && fulltext == NULL) {
1491 err = got_delta_cache_add(pack->delta_cache,
1492 delta_data_offset, NULL, 0);
1493 if (err) {
1494 if (err->code != GOT_ERR_NO_SPACE)
1495 goto done;
1496 err = NULL;
1497 } else {
1498 err = got_delta_cache_add_fulltext(
1499 pack->delta_cache,
1500 delta_data_offset,
1501 base_buf, base_bufsz);
1502 if (err &&
1503 err->code != GOT_ERR_NO_SPACE)
1504 goto done;
1505 err = NULL;
1508 continue;
1511 if (pack->delta_cache) {
1512 got_delta_cache_get(&delta_buf, &delta_len,
1513 &fulltext, &fulltext_len,
1514 pack->delta_cache, delta->data_offset);
1516 if (delta_buf == NULL) {
1517 cached = 0;
1518 err = read_delta_data(&delta_buf, &delta_len, NULL,
1519 delta->data_offset, pack);
1520 if (err)
1521 goto done;
1523 if (pack->delta_cache && !cached) {
1524 err = got_delta_cache_add(pack->delta_cache,
1525 delta->data_offset, delta_buf, delta_len);
1526 if (err == NULL)
1527 cached = 1;
1528 else if (err->code != GOT_ERR_NO_SPACE) {
1529 free(delta_buf);
1530 goto done;
1534 err = got_delta_get_sizes(&base_size, &result_size,
1535 delta_buf, delta_len);
1536 if (err) {
1537 if (!cached)
1538 free(delta_buf);
1539 goto done;
1541 if (base_size > max_size)
1542 max_size = base_size;
1543 if (result_size > max_size)
1544 max_size = result_size;
1545 if (fulltext_len > max_size)
1546 max_size = fulltext_len;
1548 if (base_buf && max_size > max_bufsize) {
1549 /* Switch from buffers to temporary files. */
1550 size_t w = fwrite(base_buf, 1, base_bufsz,
1551 base_file);
1552 if (w != base_bufsz) {
1553 err = got_ferror(outfile, GOT_ERR_IO);
1554 if (!cached)
1555 free(delta_buf);
1556 goto done;
1558 free(base_buf);
1559 base_buf = NULL;
1560 free(accum_buf);
1561 accum_buf = NULL;
1564 if (base_buf && max_size > base_bufsz) {
1565 uint8_t *p = realloc(base_buf, max_size);
1566 if (p == NULL) {
1567 err = got_error_from_errno("realloc");
1568 if (!cached)
1569 free(delta_buf);
1570 goto done;
1572 base_buf = p;
1573 base_bufsz = max_size;
1576 if (accum_buf && max_size > accum_bufsz) {
1577 uint8_t *p = realloc(accum_buf, max_size);
1578 if (p == NULL) {
1579 err = got_error_from_errno("realloc");
1580 if (!cached)
1581 free(delta_buf);
1582 goto done;
1584 accum_buf = p;
1585 accum_bufsz = max_size;
1588 if (base_buf) {
1589 if (fulltext) {
1590 memcpy(accum_buf, fulltext, fulltext_len);
1591 accum_size = fulltext_len;
1592 err = NULL;
1593 } else {
1594 err = got_delta_apply_in_mem(base_buf,
1595 base_bufsz, delta_buf, delta_len,
1596 accum_buf, &accum_size, max_size);
1598 n++;
1599 if (!cached)
1600 free(delta_buf);
1601 if (err)
1602 goto done;
1603 if (fulltext == NULL) {
1604 err = got_delta_cache_add_fulltext(
1605 pack->delta_cache, delta->data_offset,
1606 accum_buf, accum_size);
1607 if (err) {
1608 if (err->code != GOT_ERR_NO_SPACE)
1609 goto done;
1610 err = NULL;
1613 } else {
1614 err = got_delta_apply(base_file, delta_buf,
1615 delta_len,
1616 /* Final delta application writes to output file. */
1617 ++n < deltas->nentries ? accum_file : outfile,
1618 &accum_size);
1619 if (!cached)
1620 free(delta_buf);
1621 if (err)
1622 goto done;
1625 if (n < deltas->nentries) {
1626 /* Accumulated delta becomes the new base. */
1627 if (base_buf) {
1628 uint8_t *tmp = accum_buf;
1629 size_t tmp_size = accum_bufsz;
1630 accum_buf = base_buf;
1631 accum_bufsz = base_bufsz;
1632 base_buf = tmp;
1633 base_bufsz = tmp_size;
1634 } else {
1635 FILE *tmp = accum_file;
1636 accum_file = base_file;
1637 base_file = tmp;
1638 rewind(base_file);
1639 rewind(accum_file);
1644 done:
1645 free(base_buf);
1646 if (err) {
1647 free(accum_buf);
1648 accum_buf = NULL;
1650 if (accum_buf) {
1651 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1652 free(accum_buf);
1653 if (len != accum_size)
1654 err = got_ferror(outfile, GOT_ERR_IO);
1656 rewind(outfile);
1657 if (err == NULL)
1658 *result_size = accum_size;
1659 return err;
1662 const struct got_error *
1663 got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1664 struct got_delta_chain *deltas, struct got_pack *pack)
1666 const struct got_error *err = NULL;
1667 struct got_delta *delta;
1668 uint8_t *base_buf = NULL, *accum_buf = NULL;
1669 size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0;
1670 uint64_t max_size = 0;
1671 int n = 0;
1673 *outbuf = NULL;
1674 *outlen = 0;
1676 if (STAILQ_EMPTY(&deltas->entries))
1677 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1679 if (pack->delta_cache) {
1680 uint8_t *delta_buf = NULL, *fulltext = NULL;
1681 size_t delta_len, fulltext_len;
1683 delta = STAILQ_LAST(&deltas->entries, got_delta, entry);
1684 got_delta_cache_get(&delta_buf, &delta_len,
1685 &fulltext, &fulltext_len,
1686 pack->delta_cache, delta->data_offset);
1687 if (fulltext) {
1688 *outbuf = malloc(fulltext_len);
1689 if (*outbuf == NULL)
1690 return got_error_from_errno("malloc");
1691 memcpy(*outbuf, fulltext, fulltext_len);
1692 *outlen = fulltext_len;
1693 return NULL;
1697 /* Deltas are ordered in ascending order. */
1698 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1699 uint8_t *delta_buf = NULL, *fulltext = NULL;
1700 size_t delta_len, fulltext_len = 0;
1701 uint64_t base_size, result_size = 0;
1702 int cached = 1;
1703 if (n == 0) {
1704 off_t delta_data_offset;
1706 /* Plain object types are the delta base. */
1707 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1708 delta->type != GOT_OBJ_TYPE_TREE &&
1709 delta->type != GOT_OBJ_TYPE_BLOB &&
1710 delta->type != GOT_OBJ_TYPE_TAG) {
1711 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1712 goto done;
1715 delta_data_offset = delta->offset + delta->tslen;
1716 if (delta_data_offset >= pack->filesize) {
1717 err = got_error(GOT_ERR_PACK_OFFSET);
1718 goto done;
1721 if (pack->delta_cache) {
1722 got_delta_cache_get(&delta_buf, &delta_len,
1723 &fulltext, &fulltext_len,
1724 pack->delta_cache, delta_data_offset);
1727 if (delta->size > max_size)
1728 max_size = delta->size;
1729 if (delta->size > fulltext_len)
1730 max_size = fulltext_len;
1732 if (fulltext) {
1733 base_buf = malloc(fulltext_len);
1734 if (base_buf == NULL) {
1735 err = got_error_from_errno("malloc");
1736 goto done;
1738 memcpy(base_buf, fulltext, fulltext_len);
1739 base_bufsz = fulltext_len;
1740 } else if (pack->map) {
1741 size_t mapoff;
1743 if (delta_data_offset > SIZE_MAX) {
1744 return got_error_fmt(GOT_ERR_RANGE,
1745 "delta %lld offset would "
1746 "overflow size_t",
1747 (long long)delta_data_offset);
1750 mapoff = delta_data_offset;
1751 err = got_inflate_to_mem_mmap(&base_buf,
1752 &base_bufsz, NULL, NULL, pack->map,
1753 mapoff, pack->filesize - mapoff);
1754 } else {
1755 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1756 == -1) {
1757 err = got_error_from_errno("lseek");
1758 goto done;
1760 err = got_inflate_to_mem_fd(&base_buf,
1761 &base_bufsz, NULL, NULL, max_size,
1762 pack->fd);
1764 if (err)
1765 goto done;
1766 n++;
1768 if (pack->delta_cache && fulltext == NULL) {
1769 err = got_delta_cache_add(pack->delta_cache,
1770 delta_data_offset, NULL, 0);
1771 if (err) {
1772 if (err->code != GOT_ERR_NO_SPACE)
1773 goto done;
1774 err = NULL;
1775 } else {
1776 err = got_delta_cache_add_fulltext(
1777 pack->delta_cache,
1778 delta_data_offset,
1779 base_buf, base_bufsz);
1780 if (err &&
1781 err->code != GOT_ERR_NO_SPACE)
1782 goto done;
1783 err = NULL;
1786 continue;
1789 if (pack->delta_cache) {
1790 got_delta_cache_get(&delta_buf, &delta_len,
1791 &fulltext, &fulltext_len,
1792 pack->delta_cache, delta->data_offset);
1794 if (delta_buf == NULL) {
1795 cached = 0;
1796 err = read_delta_data(&delta_buf, &delta_len, NULL,
1797 delta->data_offset, pack);
1798 if (err)
1799 goto done;
1801 if (pack->delta_cache && !cached) {
1802 err = got_delta_cache_add(pack->delta_cache,
1803 delta->data_offset, delta_buf, delta_len);
1804 if (err == NULL)
1805 cached = 1;
1806 else if (err->code != GOT_ERR_NO_SPACE) {
1807 free(delta_buf);
1808 goto done;
1812 err = got_delta_get_sizes(&base_size, &result_size,
1813 delta_buf, delta_len);
1814 if (err) {
1815 if (!cached)
1816 free(delta_buf);
1817 goto done;
1819 if (base_size > max_size)
1820 max_size = base_size;
1821 if (result_size > max_size)
1822 max_size = result_size;
1823 if (fulltext_len > max_size)
1824 max_size = fulltext_len;
1826 if (max_size > base_bufsz) {
1827 uint8_t *p = realloc(base_buf, max_size);
1828 if (p == NULL) {
1829 err = got_error_from_errno("realloc");
1830 if (!cached)
1831 free(delta_buf);
1832 goto done;
1834 base_buf = p;
1835 base_bufsz = max_size;
1838 if (max_size > accum_bufsz) {
1839 uint8_t *p = realloc(accum_buf, max_size);
1840 if (p == NULL) {
1841 err = got_error_from_errno("realloc");
1842 if (!cached)
1843 free(delta_buf);
1844 goto done;
1846 accum_buf = p;
1847 accum_bufsz = max_size;
1850 if (fulltext) {
1851 memcpy(accum_buf, fulltext, fulltext_len);
1852 accum_size = fulltext_len;
1853 err = NULL;
1854 } else {
1855 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1856 delta_buf, delta_len, accum_buf,
1857 &accum_size, max_size);
1859 if (!cached)
1860 free(delta_buf);
1861 n++;
1862 if (err)
1863 goto done;
1865 if (fulltext == NULL) {
1866 err = got_delta_cache_add_fulltext(pack->delta_cache,
1867 delta->data_offset, accum_buf, accum_size);
1868 if (err) {
1869 if (err->code != GOT_ERR_NO_SPACE)
1870 goto done;
1871 err = NULL;
1875 if (n < deltas->nentries) {
1876 /* Accumulated delta becomes the new base. */
1877 uint8_t *tmp = accum_buf;
1878 size_t tmp_size = accum_bufsz;
1879 accum_buf = base_buf;
1880 accum_bufsz = base_bufsz;
1881 base_buf = tmp;
1882 base_bufsz = tmp_size;
1886 done:
1887 free(base_buf);
1888 if (err) {
1889 free(accum_buf);
1890 *outbuf = NULL;
1891 *outlen = 0;
1892 } else {
1893 *outbuf = accum_buf;
1894 *outlen = accum_size;
1896 return err;
1899 const struct got_error *
1900 got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1901 FILE *outfile, FILE *base_file, FILE *accum_file)
1903 const struct got_error *err = NULL;
1905 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1906 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1908 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1909 if (obj->pack_offset >= pack->filesize)
1910 return got_error(GOT_ERR_PACK_OFFSET);
1912 if (pack->map) {
1913 size_t mapoff;
1915 if (obj->pack_offset > SIZE_MAX) {
1916 return got_error_fmt(GOT_ERR_RANGE,
1917 "pack offset %lld would overflow size_t",
1918 (long long)obj->pack_offset);
1921 mapoff = obj->pack_offset;
1922 err = got_inflate_to_file_mmap(&obj->size, NULL, NULL,
1923 pack->map, mapoff, pack->filesize - mapoff,
1924 outfile);
1925 } else {
1926 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1927 return got_error_from_errno("lseek");
1928 err = got_inflate_to_file_fd(&obj->size, NULL, NULL,
1929 pack->fd, outfile);
1931 } else
1932 err = got_pack_dump_delta_chain_to_file(&obj->size,
1933 &obj->deltas, pack, outfile, base_file, accum_file);
1935 return err;
1938 const struct got_error *
1939 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1940 struct got_object *obj, struct got_pack *pack)
1942 const struct got_error *err = NULL;
1944 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1945 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1947 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1948 if (obj->pack_offset >= pack->filesize)
1949 return got_error(GOT_ERR_PACK_OFFSET);
1950 if (pack->map) {
1951 size_t mapoff;
1953 if (obj->pack_offset > SIZE_MAX) {
1954 return got_error_fmt(GOT_ERR_RANGE,
1955 "pack offset %lld would overflow size_t",
1956 (long long)obj->pack_offset);
1959 mapoff = obj->pack_offset;
1960 err = got_inflate_to_mem_mmap(buf, len, NULL, NULL,
1961 pack->map, mapoff, pack->filesize - mapoff);
1962 } else {
1963 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1964 return got_error_from_errno("lseek");
1965 err = got_inflate_to_mem_fd(buf, len, NULL, NULL,
1966 obj->size, pack->fd);
1968 } else
1969 err = got_pack_dump_delta_chain_to_mem(buf, len, &obj->deltas,
1970 pack);
1972 return err;
1975 static const struct got_error *
1976 read_raw_delta_data(uint8_t **delta_buf, size_t *delta_len,
1977 size_t *delta_len_compressed, uint64_t *base_size, uint64_t *result_size,
1978 off_t delta_data_offset, struct got_pack *pack, struct got_packidx *packidx)
1980 const struct got_error *err = NULL;
1982 /* Validate decompression and obtain the decompressed size. */
1983 err = read_delta_data(delta_buf, delta_len, delta_len_compressed,
1984 delta_data_offset, pack);
1985 if (err)
1986 return err;
1988 /* Read delta base/result sizes from head of delta stream. */
1989 err = got_delta_get_sizes(base_size, result_size,
1990 *delta_buf, *delta_len);
1991 if (err)
1992 goto done;
1994 /* Discard decompressed delta and read it again in compressed form. */
1995 free(*delta_buf);
1996 *delta_buf = malloc(*delta_len_compressed);
1997 if (*delta_buf == NULL) {
1998 err = got_error_from_errno("malloc");
1999 goto done;
2001 if (pack->map) {
2002 if (delta_data_offset >= pack->filesize) {
2003 err = got_error(GOT_ERR_PACK_OFFSET);
2004 goto done;
2006 memcpy(*delta_buf, pack->map + delta_data_offset,
2007 *delta_len_compressed);
2008 } else {
2009 ssize_t n;
2010 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
2011 err = got_error_from_errno("lseek");
2012 goto done;
2014 n = read(pack->fd, *delta_buf, *delta_len_compressed);
2015 if (n < 0) {
2016 err = got_error_from_errno("read");
2017 goto done;
2018 } else if (n != *delta_len_compressed) {
2019 err = got_error(GOT_ERR_IO);
2020 goto done;
2023 done:
2024 if (err) {
2025 free(*delta_buf);
2026 *delta_buf = NULL;
2027 *delta_len = 0;
2028 *delta_len_compressed = 0;
2029 *base_size = 0;
2030 *result_size = 0;
2032 return err;
2035 const struct got_error *
2036 got_packfile_extract_raw_delta(uint8_t **delta_buf, size_t *delta_size,
2037 size_t *delta_compressed_size, off_t *delta_offset,
2038 off_t *delta_data_offset, off_t *base_offset,
2039 struct got_object_id *base_id, uint64_t *base_size, uint64_t *result_size,
2040 struct got_pack *pack, struct got_packidx *packidx, int idx)
2042 const struct got_error *err = NULL;
2043 off_t offset;
2044 uint8_t type;
2045 uint64_t size;
2046 size_t tslen, delta_hdrlen;
2048 *delta_buf = NULL;
2049 *delta_size = 0;
2050 *delta_compressed_size = 0;
2051 *delta_offset = 0;
2052 *delta_data_offset = 0;
2053 *base_offset = 0;
2054 *base_size = 0;
2055 *result_size = 0;
2057 offset = got_packidx_get_object_offset(packidx, idx);
2058 if (offset == -1)
2059 return got_error(GOT_ERR_BAD_PACKIDX);
2061 if (offset >= pack->filesize)
2062 return got_error(GOT_ERR_PACK_OFFSET);
2064 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
2065 pack, offset);
2066 if (err)
2067 return err;
2069 if (tslen + size < tslen || offset + size < size ||
2070 tslen + offset < tslen)
2071 return got_error(GOT_ERR_PACK_OFFSET);
2073 switch (type) {
2074 case GOT_OBJ_TYPE_OFFSET_DELTA:
2075 err = got_pack_parse_offset_delta(base_offset, &delta_hdrlen,
2076 pack, offset, tslen);
2077 if (err)
2078 return err;
2079 break;
2080 case GOT_OBJ_TYPE_REF_DELTA:
2081 err = got_pack_parse_ref_delta(base_id, pack, offset, tslen);
2082 if (err)
2083 return err;
2084 delta_hdrlen = SHA1_DIGEST_LENGTH;
2085 break;
2086 default:
2087 return got_error_fmt(GOT_ERR_OBJ_TYPE,
2088 "non-delta object type %d found at offset %lld",
2089 type, (long long)offset);
2092 if (tslen + delta_hdrlen < delta_hdrlen ||
2093 offset + delta_hdrlen < delta_hdrlen)
2094 return got_error(GOT_ERR_BAD_DELTA);
2096 *delta_data_offset = offset + tslen + delta_hdrlen;
2097 err = read_raw_delta_data(delta_buf, delta_size, delta_compressed_size,
2098 base_size, result_size, *delta_data_offset, pack, packidx);
2099 if (err)
2100 return err;
2102 if (*delta_size != size) {
2103 err = got_error(GOT_ERR_BAD_DELTA);
2104 goto done;
2107 *delta_offset = offset;
2108 done:
2109 if (err) {
2110 free(*delta_buf);
2111 *delta_buf = NULL;
2112 *delta_size = 0;
2113 *delta_compressed_size = 0;
2114 *delta_offset = 0;
2115 *base_offset = 0;
2116 *base_size = 0;
2117 *result_size = 0;
2119 return err;