really store the fulltext of delta-base objects in fulltext cache
[got-portable.git] / lib / pack.c
blobb48c1a816df9f8131038bba282e7b4e86dedb373
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 STAILQ_INIT(matched_ids);
677 if (prefix_len < 2)
678 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
680 hex[0] = id_str_prefix[0];
681 hex[1] = id_str_prefix[1];
682 hex[2] = '\0';
683 if (!got_parse_xdigit(&id0, hex))
684 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
686 if (id0 > 0)
687 i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
688 oid = &packidx->hdr.sorted_ids[i];
689 while (i < totobj && oid->sha1[0] == id0) {
690 char id_str[SHA1_DIGEST_STRING_LENGTH];
691 struct got_object_qid *qid;
692 int cmp;
694 if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
695 return got_error(GOT_ERR_NO_SPACE);
697 cmp = strncmp(id_str, id_str_prefix, prefix_len);
698 if (cmp < 0) {
699 oid = &packidx->hdr.sorted_ids[++i];
700 continue;
701 } else if (cmp > 0)
702 break;
704 err = got_object_qid_alloc_partial(&qid);
705 if (err)
706 break;
707 memcpy(qid->id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
708 STAILQ_INSERT_TAIL(matched_ids, qid, entry);
710 oid = &packidx->hdr.sorted_ids[++i];
713 if (err)
714 got_object_id_queue_free(matched_ids);
715 return err;
718 static void
719 set_max_datasize(void)
721 struct rlimit rl;
723 if (getrlimit(RLIMIT_DATA, &rl) != 0)
724 return;
726 rl.rlim_cur = rl.rlim_max;
727 setrlimit(RLIMIT_DATA, &rl);
730 const struct got_error *
731 got_pack_start_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
733 const struct got_error *err = NULL;
734 int imsg_fds[2];
735 pid_t pid;
736 struct imsgbuf *ibuf;
738 ibuf = calloc(1, sizeof(*ibuf));
739 if (ibuf == NULL)
740 return got_error_from_errno("calloc");
742 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
743 if (pack->privsep_child == NULL) {
744 err = got_error_from_errno("calloc");
745 free(ibuf);
746 return err;
748 pack->child_has_tempfiles = 0;
749 pack->child_has_delta_outfd = 0;
751 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
752 err = got_error_from_errno("socketpair");
753 goto done;
756 pid = fork();
757 if (pid == -1) {
758 err = got_error_from_errno("fork");
759 goto done;
760 } else if (pid == 0) {
761 set_max_datasize();
762 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
763 pack->path_packfile);
764 /* not reached */
767 if (close(imsg_fds[1]) == -1)
768 return got_error_from_errno("close");
769 pack->privsep_child->imsg_fd = imsg_fds[0];
770 pack->privsep_child->pid = pid;
771 imsg_init(ibuf, imsg_fds[0]);
772 pack->privsep_child->ibuf = ibuf;
774 err = got_privsep_init_pack_child(ibuf, pack, packidx);
775 if (err) {
776 const struct got_error *child_err;
777 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
778 child_err = got_privsep_wait_for_child(
779 pack->privsep_child->pid);
780 if (child_err && err == NULL)
781 err = child_err;
783 done:
784 if (err) {
785 free(ibuf);
786 free(pack->privsep_child);
787 pack->privsep_child = NULL;
789 return err;
792 static const struct got_error *
793 pack_stop_privsep_child(struct got_pack *pack)
795 const struct got_error *err = NULL, *close_err = NULL;
797 if (pack->privsep_child == NULL)
798 return NULL;
800 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
801 if (err)
802 return err;
803 if (close(pack->privsep_child->imsg_fd) == -1)
804 close_err = got_error_from_errno("close");
805 err = got_privsep_wait_for_child(pack->privsep_child->pid);
806 if (close_err && err == NULL)
807 err = close_err;
808 imsg_clear(pack->privsep_child->ibuf);
809 free(pack->privsep_child->ibuf);
810 free(pack->privsep_child);
811 pack->privsep_child = NULL;
812 return err;
815 const struct got_error *
816 got_pack_close(struct got_pack *pack)
818 const struct got_error *err = NULL;
820 err = pack_stop_privsep_child(pack);
821 if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
822 err = got_error_from_errno("munmap");
823 if (pack->fd != -1 && close(pack->fd) == -1 && err == NULL)
824 err = got_error_from_errno("close");
825 pack->fd = -1;
826 free(pack->path_packfile);
827 pack->path_packfile = NULL;
828 pack->filesize = 0;
829 if (pack->delta_cache) {
830 got_delta_cache_free(pack->delta_cache);
831 pack->delta_cache = NULL;
835 * Leave accumfd and basefd alone. They are managed by the
836 * repository layer and can be reused.
839 return err;
842 const struct got_error *
843 got_pack_parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
844 struct got_pack *pack, off_t offset)
846 uint8_t t = 0;
847 uint64_t s = 0;
848 uint8_t sizeN;
849 size_t mapoff = 0;
850 int i = 0;
852 *len = 0;
854 if (offset >= pack->filesize)
855 return got_error(GOT_ERR_PACK_OFFSET);
857 if (pack->map) {
858 if (offset > SIZE_MAX) {
859 return got_error_fmt(GOT_ERR_PACK_OFFSET,
860 "offset %lld overflows size_t",
861 (long long)offset);
864 mapoff = (size_t)offset;
865 } else {
866 if (lseek(pack->fd, offset, SEEK_SET) == -1)
867 return got_error_from_errno("lseek");
870 do {
871 /* We do not support size values which don't fit in 64 bit. */
872 if (i > 9)
873 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
874 "packfile offset %lld", (long long)offset);
876 if (pack->map) {
877 if (mapoff + sizeof(sizeN) >= pack->filesize)
878 return got_error(GOT_ERR_BAD_PACKFILE);
879 sizeN = *(pack->map + mapoff);
880 mapoff += sizeof(sizeN);
881 } else {
882 ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
883 if (n < 0)
884 return got_error_from_errno("read");
885 if (n != sizeof(sizeN))
886 return got_error(GOT_ERR_BAD_PACKFILE);
888 *len += sizeof(sizeN);
890 if (i == 0) {
891 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
892 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
893 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
894 } else {
895 size_t shift = 4 + 7 * (i - 1);
896 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
898 i++;
899 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
901 *type = t;
902 *size = s;
903 return NULL;
906 static const struct got_error *
907 open_plain_object(struct got_object **obj, struct got_object_id *id,
908 uint8_t type, off_t offset, size_t size, int idx)
910 *obj = calloc(1, sizeof(**obj));
911 if (*obj == NULL)
912 return got_error_from_errno("calloc");
914 (*obj)->type = type;
915 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
916 (*obj)->pack_idx = idx;
917 (*obj)->hdrlen = 0;
918 (*obj)->size = size;
919 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
920 (*obj)->pack_offset = offset;
922 return NULL;
925 static const struct got_error *
926 parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
927 off_t delta_offset)
929 int64_t o = 0;
930 uint8_t offN;
931 int i = 0;
933 *offset = 0;
934 *len = 0;
936 do {
937 /* We do not support offset values which don't fit in 64 bit. */
938 if (i > 8)
939 return got_error(GOT_ERR_NO_SPACE);
941 if (pack->map) {
942 size_t mapoff;
944 if (delta_offset + *len > SIZE_MAX) {
945 return got_error_fmt(GOT_ERR_PACK_OFFSET,
946 "mapoff %lld would overflow size_t",
947 (long long)delta_offset + *len);
950 mapoff = (size_t)delta_offset + *len;
951 if (mapoff + sizeof(offN) >= pack->filesize)
952 return got_error(GOT_ERR_PACK_OFFSET);
953 offN = *(pack->map + mapoff);
954 } else {
955 ssize_t n;
956 n = read(pack->fd, &offN, sizeof(offN));
957 if (n < 0)
958 return got_error_from_errno("read");
959 if (n != sizeof(offN))
960 return got_error(GOT_ERR_BAD_PACKFILE);
962 *len += sizeof(offN);
964 if (i == 0)
965 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
966 else {
967 o++;
968 o <<= 7;
969 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
971 i++;
972 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
974 *offset = o;
975 return NULL;
978 const struct got_error *
979 got_pack_parse_offset_delta(off_t *base_offset, size_t *len,
980 struct got_pack *pack, off_t offset, size_t tslen)
982 const struct got_error *err;
983 int64_t negoffset;
984 size_t negofflen;
986 *len = 0;
988 err = parse_negative_offset(&negoffset, &negofflen, pack,
989 offset + tslen);
990 if (err)
991 return err;
993 /* Compute the base object's offset (must be in the same pack file). */
994 *base_offset = (offset - negoffset);
995 if (*base_offset <= 0)
996 return got_error(GOT_ERR_BAD_PACKFILE);
998 *len = negofflen;
999 return NULL;
1002 static const struct got_error *
1003 read_delta_data(uint8_t **delta_buf, size_t *delta_len,
1004 size_t *delta_compressed_len, size_t delta_data_offset,
1005 struct got_pack *pack)
1007 const struct got_error *err = NULL;
1008 size_t consumed = 0;
1010 if (pack->map) {
1011 if (delta_data_offset >= pack->filesize)
1012 return got_error(GOT_ERR_PACK_OFFSET);
1013 err = got_inflate_to_mem_mmap(delta_buf, delta_len,
1014 &consumed, NULL, pack->map, delta_data_offset,
1015 pack->filesize - delta_data_offset);
1016 if (err)
1017 return err;
1018 } else {
1019 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
1020 return got_error_from_errno("lseek");
1021 err = got_inflate_to_mem_fd(delta_buf, delta_len,
1022 &consumed, NULL, 0, pack->fd);
1023 if (err)
1024 return err;
1027 if (delta_compressed_len)
1028 *delta_compressed_len = consumed;
1030 return NULL;
1033 static const struct got_error *
1034 add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
1035 int delta_type, size_t delta_size, off_t delta_data_offset)
1037 struct got_delta *delta;
1039 delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
1040 delta_data_offset);
1041 if (delta == NULL)
1042 return got_error_from_errno("got_delta_open");
1043 /* delta is freed in got_object_close() */
1044 deltas->nentries++;
1045 STAILQ_INSERT_HEAD(&deltas->entries, delta, entry);
1046 return NULL;
1049 static const struct got_error *
1050 resolve_offset_delta(struct got_delta_chain *deltas,
1051 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1052 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1054 const struct got_error *err;
1055 off_t base_offset;
1056 uint8_t base_type;
1057 uint64_t base_size;
1058 size_t base_tslen;
1059 off_t delta_data_offset;
1060 size_t consumed;
1062 err = got_pack_parse_offset_delta(&base_offset, &consumed, pack,
1063 delta_offset, tslen);
1064 if (err)
1065 return err;
1067 delta_data_offset = delta_offset + tslen + consumed;
1068 if (delta_data_offset >= pack->filesize)
1069 return got_error(GOT_ERR_PACK_OFFSET);
1071 if (pack->map == NULL) {
1072 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1073 if (delta_data_offset == -1)
1074 return got_error_from_errno("lseek");
1077 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1078 delta_data_offset);
1079 if (err)
1080 return err;
1082 /* An offset delta must be in the same packfile. */
1083 if (base_offset >= pack->filesize)
1084 return got_error(GOT_ERR_PACK_OFFSET);
1086 err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1087 &base_tslen, pack, base_offset);
1088 if (err)
1089 return err;
1091 return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1092 base_tslen, base_type, base_size, recursion - 1);
1095 const struct got_error *
1096 got_pack_parse_ref_delta(struct got_object_id *id,
1097 struct got_pack *pack, off_t delta_offset, int tslen)
1099 if (pack->map) {
1100 size_t mapoff;
1102 if (delta_offset + tslen > SIZE_MAX) {
1103 return got_error_fmt(GOT_ERR_PACK_OFFSET,
1104 "mapoff %lld would overflow size_t",
1105 (long long)delta_offset + tslen);
1108 mapoff = delta_offset + tslen;
1109 if (mapoff + sizeof(*id) >= pack->filesize)
1110 return got_error(GOT_ERR_PACK_OFFSET);
1111 memcpy(id, pack->map + mapoff, sizeof(*id));
1112 } else {
1113 ssize_t n;
1114 n = read(pack->fd, id, sizeof(*id));
1115 if (n < 0)
1116 return got_error_from_errno("read");
1117 if (n != sizeof(*id))
1118 return got_error(GOT_ERR_BAD_PACKFILE);
1121 return NULL;
1124 static const struct got_error *
1125 resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
1126 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
1127 size_t delta_size, unsigned int recursion)
1129 const struct got_error *err;
1130 struct got_object_id id;
1131 int idx;
1132 off_t base_offset;
1133 uint8_t base_type;
1134 uint64_t base_size;
1135 size_t base_tslen;
1136 off_t delta_data_offset;
1138 if (delta_offset + tslen >= pack->filesize)
1139 return got_error(GOT_ERR_PACK_OFFSET);
1141 err = got_pack_parse_ref_delta(&id, pack, delta_offset, tslen);
1142 if (err)
1143 return err;
1144 if (pack->map) {
1145 delta_data_offset = delta_offset + tslen + SHA1_DIGEST_LENGTH;
1146 } else {
1147 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1148 if (delta_data_offset == -1)
1149 return got_error_from_errno("lseek");
1152 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1153 delta_data_offset);
1154 if (err)
1155 return err;
1157 /* Delta base must be in the same pack file. */
1158 idx = got_packidx_get_object_idx(packidx, &id);
1159 if (idx == -1)
1160 return got_error(GOT_ERR_NO_OBJ);
1162 base_offset = got_packidx_get_object_offset(packidx, idx);
1163 if (base_offset == -1)
1164 return got_error(GOT_ERR_BAD_PACKIDX);
1166 if (base_offset >= pack->filesize)
1167 return got_error(GOT_ERR_PACK_OFFSET);
1169 err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1170 &base_tslen, pack, base_offset);
1171 if (err)
1172 return err;
1174 return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1175 base_tslen, base_type, base_size, recursion - 1);
1178 const struct got_error *
1179 got_pack_resolve_delta_chain(struct got_delta_chain *deltas,
1180 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1181 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1183 const struct got_error *err = NULL;
1185 if (--recursion == 0)
1186 return got_error(GOT_ERR_RECURSION);
1188 switch (delta_type) {
1189 case GOT_OBJ_TYPE_COMMIT:
1190 case GOT_OBJ_TYPE_TREE:
1191 case GOT_OBJ_TYPE_BLOB:
1192 case GOT_OBJ_TYPE_TAG:
1193 /* Plain types are the final delta base. Recursion ends. */
1194 err = add_delta(deltas, delta_offset, tslen, delta_type,
1195 delta_size, 0);
1196 break;
1197 case GOT_OBJ_TYPE_OFFSET_DELTA:
1198 err = resolve_offset_delta(deltas, packidx, pack,
1199 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1200 break;
1201 case GOT_OBJ_TYPE_REF_DELTA:
1202 err = resolve_ref_delta(deltas, packidx, pack,
1203 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1204 break;
1205 default:
1206 return got_error(GOT_ERR_OBJ_TYPE);
1209 return err;
1212 static const struct got_error *
1213 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
1214 struct got_pack *pack, struct got_object_id *id, off_t offset,
1215 size_t tslen, int delta_type, size_t delta_size, int idx)
1217 const struct got_error *err = NULL;
1218 int resolved_type;
1220 *obj = calloc(1, sizeof(**obj));
1221 if (*obj == NULL)
1222 return got_error_from_errno("calloc");
1224 (*obj)->flags = 0;
1225 (*obj)->hdrlen = 0;
1226 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1227 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1228 (*obj)->pack_offset = offset + tslen;
1230 STAILQ_INIT(&(*obj)->deltas.entries);
1231 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1232 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1233 (*obj)->pack_idx = idx;
1235 err = got_pack_resolve_delta_chain(&(*obj)->deltas, packidx, pack,
1236 offset, tslen, delta_type, delta_size,
1237 GOT_DELTA_CHAIN_RECURSION_MAX);
1238 if (err)
1239 goto done;
1241 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1242 if (err)
1243 goto done;
1244 (*obj)->type = resolved_type;
1245 done:
1246 if (err) {
1247 got_object_close(*obj);
1248 *obj = NULL;
1250 return err;
1253 const struct got_error *
1254 got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
1255 struct got_packidx *packidx, int idx, struct got_object_id *id)
1257 const struct got_error *err = NULL;
1258 off_t offset;
1259 uint8_t type;
1260 uint64_t size;
1261 size_t tslen;
1263 *obj = NULL;
1265 offset = got_packidx_get_object_offset(packidx, idx);
1266 if (offset == -1)
1267 return got_error(GOT_ERR_BAD_PACKIDX);
1269 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1270 pack, offset);
1271 if (err)
1272 return err;
1274 switch (type) {
1275 case GOT_OBJ_TYPE_COMMIT:
1276 case GOT_OBJ_TYPE_TREE:
1277 case GOT_OBJ_TYPE_BLOB:
1278 case GOT_OBJ_TYPE_TAG:
1279 err = open_plain_object(obj, id, type, offset + tslen,
1280 size, idx);
1281 break;
1282 case GOT_OBJ_TYPE_OFFSET_DELTA:
1283 case GOT_OBJ_TYPE_REF_DELTA:
1284 err = open_delta_object(obj, packidx, pack, id, offset,
1285 tslen, type, size, idx);
1286 break;
1287 default:
1288 err = got_error(GOT_ERR_OBJ_TYPE);
1289 break;
1292 return err;
1295 const struct got_error *
1296 got_pack_get_delta_chain_max_size(uint64_t *max_size,
1297 struct got_delta_chain *deltas, struct got_pack *pack)
1299 struct got_delta *delta;
1300 uint64_t base_size = 0, result_size = 0;
1302 *max_size = 0;
1303 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1304 /* Plain object types are the delta base. */
1305 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1306 delta->type != GOT_OBJ_TYPE_TREE &&
1307 delta->type != GOT_OBJ_TYPE_BLOB &&
1308 delta->type != GOT_OBJ_TYPE_TAG) {
1309 const struct got_error *err;
1310 uint8_t *delta_buf = NULL;
1311 size_t delta_len;
1312 int cached = 1;
1314 if (pack->delta_cache) {
1315 got_delta_cache_get(&delta_buf, &delta_len,
1316 NULL, NULL, pack->delta_cache,
1317 delta->data_offset);
1319 if (delta_buf == NULL) {
1320 cached = 0;
1321 err = read_delta_data(&delta_buf, &delta_len,
1322 NULL, delta->data_offset, pack);
1323 if (err)
1324 return err;
1326 if (pack->delta_cache && !cached) {
1327 err = got_delta_cache_add(pack->delta_cache,
1328 delta->data_offset, delta_buf, delta_len);
1329 if (err == NULL)
1330 cached = 1;
1331 else if (err->code != GOT_ERR_NO_SPACE) {
1332 free(delta_buf);
1333 return err;
1336 err = got_delta_get_sizes(&base_size, &result_size,
1337 delta_buf, delta_len);
1338 if (!cached)
1339 free(delta_buf);
1340 if (err)
1341 return err;
1342 } else
1343 base_size = delta->size;
1344 if (base_size > *max_size)
1345 *max_size = base_size;
1346 if (result_size > *max_size)
1347 *max_size = result_size;
1350 return NULL;
1353 const struct got_error *
1354 got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj,
1355 struct got_pack *pack)
1357 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1358 return got_error(GOT_ERR_OBJ_TYPE);
1360 return got_pack_get_delta_chain_max_size(size, &obj->deltas, pack);
1363 const struct got_error *
1364 got_pack_dump_delta_chain_to_file(size_t *result_size,
1365 struct got_delta_chain *deltas, struct got_pack *pack, FILE *outfile,
1366 FILE *base_file, FILE *accum_file)
1368 const struct got_error *err = NULL;
1369 struct got_delta *delta;
1370 uint8_t *base_buf = NULL, *accum_buf = NULL;
1371 size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0;
1372 /* We process small enough files entirely in memory for speed. */
1373 const size_t max_bufsize = GOT_DELTA_RESULT_SIZE_CACHED_MAX;
1374 uint64_t max_size = 0;
1375 int n = 0;
1377 *result_size = 0;
1379 if (STAILQ_EMPTY(&deltas->entries))
1380 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1382 if (pack->delta_cache) {
1383 uint8_t *delta_buf = NULL, *fulltext = NULL;
1384 size_t delta_len, fulltext_len;
1386 delta = STAILQ_LAST(&deltas->entries, got_delta, entry);
1387 got_delta_cache_get(&delta_buf, &delta_len,
1388 &fulltext, &fulltext_len,
1389 pack->delta_cache, delta->data_offset);
1390 if (fulltext) {
1391 size_t w;
1393 w = fwrite(fulltext, 1, fulltext_len, outfile);
1394 if (w != fulltext_len)
1395 return got_ferror(outfile, GOT_ERR_IO);
1396 *result_size = fulltext_len;
1397 return NULL;
1401 if (fseeko(base_file, 0L, SEEK_SET) == -1)
1402 return got_error_from_errno("fseeko");
1403 if (fseeko(accum_file, 0L, SEEK_SET) == -1)
1404 return got_error_from_errno("fseeko");
1406 /* Deltas are ordered in ascending order. */
1407 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1408 uint8_t *delta_buf = NULL, *fulltext = NULL;
1409 size_t delta_len, fulltext_len;
1410 uint64_t base_size, result_size = 0;
1411 int cached = 1;
1412 if (n == 0) {
1413 size_t mapoff;
1414 off_t delta_data_offset;
1416 /* Plain object types are the delta base. */
1417 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1418 delta->type != GOT_OBJ_TYPE_TREE &&
1419 delta->type != GOT_OBJ_TYPE_BLOB &&
1420 delta->type != GOT_OBJ_TYPE_TAG) {
1421 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1422 goto done;
1425 delta_data_offset = delta->offset + delta->tslen;
1426 if (delta_data_offset >= pack->filesize) {
1427 err = got_error(GOT_ERR_PACK_OFFSET);
1428 goto done;
1430 if (pack->map == NULL) {
1431 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1432 == -1) {
1433 err = got_error_from_errno("lseek");
1434 goto done;
1437 if (delta->size > max_size)
1438 max_size = delta->size;
1439 if (max_size > max_bufsize) {
1440 if (pack->map) {
1441 if (delta_data_offset > SIZE_MAX) {
1442 return got_error_fmt(
1443 GOT_ERR_RANGE,
1444 "delta offset %lld "
1445 "overflows size_t",
1446 (long long)
1447 delta_data_offset);
1450 mapoff = delta_data_offset;
1451 err = got_inflate_to_file_mmap(
1452 &base_bufsz, NULL, NULL, pack->map,
1453 mapoff, pack->filesize - mapoff,
1454 base_file);
1455 } else
1456 err = got_inflate_to_file_fd(
1457 &base_bufsz, NULL, NULL, pack->fd,
1458 base_file);
1459 } else {
1460 accum_buf = malloc(max_size);
1461 if (accum_buf == NULL) {
1462 err = got_error_from_errno("malloc");
1463 goto done;
1465 accum_bufsz = max_size;
1466 if (pack->map) {
1467 if (delta_data_offset > SIZE_MAX) {
1468 return got_error_fmt(
1469 GOT_ERR_RANGE,
1470 "delta offset %lld "
1471 "overflows size_t",
1472 (long long)
1473 delta_data_offset);
1476 mapoff = delta_data_offset;
1477 err = got_inflate_to_mem_mmap(&base_buf,
1478 &base_bufsz, NULL, NULL,
1479 pack->map, mapoff,
1480 pack->filesize - mapoff);
1481 } else
1482 err = got_inflate_to_mem_fd(&base_buf,
1483 &base_bufsz, NULL, NULL, max_size,
1484 pack->fd);
1486 if (err)
1487 goto done;
1488 n++;
1489 if (base_buf == NULL)
1490 rewind(base_file);
1491 continue;
1494 if (pack->delta_cache) {
1495 got_delta_cache_get(&delta_buf, &delta_len,
1496 &fulltext, &fulltext_len,
1497 pack->delta_cache, delta->data_offset);
1499 if (delta_buf == NULL) {
1500 cached = 0;
1501 err = read_delta_data(&delta_buf, &delta_len, NULL,
1502 delta->data_offset, pack);
1503 if (err)
1504 goto done;
1506 if (pack->delta_cache && !cached) {
1507 err = got_delta_cache_add(pack->delta_cache,
1508 delta->data_offset, delta_buf, delta_len);
1509 if (err == NULL)
1510 cached = 1;
1511 else if (err->code != GOT_ERR_NO_SPACE) {
1512 free(delta_buf);
1513 goto done;
1517 err = got_delta_get_sizes(&base_size, &result_size,
1518 delta_buf, delta_len);
1519 if (err) {
1520 if (!cached)
1521 free(delta_buf);
1522 goto done;
1524 if (base_size > max_size)
1525 max_size = base_size;
1526 if (result_size > max_size)
1527 max_size = result_size;
1528 if (fulltext_len > max_size)
1529 max_size = fulltext_len;
1531 if (base_buf && max_size > max_bufsize) {
1532 /* Switch from buffers to temporary files. */
1533 size_t w = fwrite(base_buf, 1, base_bufsz,
1534 base_file);
1535 if (w != base_bufsz) {
1536 err = got_ferror(outfile, GOT_ERR_IO);
1537 if (!cached)
1538 free(delta_buf);
1539 goto done;
1541 free(base_buf);
1542 base_buf = NULL;
1543 free(accum_buf);
1544 accum_buf = NULL;
1547 if (base_buf && max_size > base_bufsz) {
1548 uint8_t *p = realloc(base_buf, max_size);
1549 if (p == NULL) {
1550 err = got_error_from_errno("realloc");
1551 if (!cached)
1552 free(delta_buf);
1553 goto done;
1555 base_buf = p;
1556 base_bufsz = max_size;
1559 if (accum_buf && max_size > accum_bufsz) {
1560 uint8_t *p = realloc(accum_buf, max_size);
1561 if (p == NULL) {
1562 err = got_error_from_errno("realloc");
1563 if (!cached)
1564 free(delta_buf);
1565 goto done;
1567 accum_buf = p;
1568 accum_bufsz = max_size;
1571 if (base_buf) {
1572 if (fulltext) {
1573 memcpy(accum_buf, fulltext, fulltext_len);
1574 accum_size = fulltext_len;
1575 err = NULL;
1576 } else {
1577 err = got_delta_apply_in_mem(base_buf,
1578 base_bufsz, delta_buf, delta_len,
1579 accum_buf, &accum_size, max_size);
1581 n++;
1582 if (!cached)
1583 free(delta_buf);
1584 if (err)
1585 goto done;
1586 if (fulltext == NULL) {
1587 err = got_delta_cache_add_fulltext(
1588 pack->delta_cache, delta->data_offset,
1589 accum_buf, accum_size);
1590 if (err) {
1591 if (err->code != GOT_ERR_NO_SPACE)
1592 goto done;
1593 err = NULL;
1596 } else {
1597 err = got_delta_apply(base_file, delta_buf,
1598 delta_len,
1599 /* Final delta application writes to output file. */
1600 ++n < deltas->nentries ? accum_file : outfile,
1601 &accum_size);
1602 if (!cached)
1603 free(delta_buf);
1604 if (err)
1605 goto done;
1608 if (n < deltas->nentries) {
1609 /* Accumulated delta becomes the new base. */
1610 if (base_buf) {
1611 uint8_t *tmp = accum_buf;
1612 size_t tmp_size = accum_bufsz;
1613 accum_buf = base_buf;
1614 accum_bufsz = base_bufsz;
1615 base_buf = tmp;
1616 base_bufsz = tmp_size;
1617 } else {
1618 FILE *tmp = accum_file;
1619 accum_file = base_file;
1620 base_file = tmp;
1621 rewind(base_file);
1622 rewind(accum_file);
1627 done:
1628 free(base_buf);
1629 if (accum_buf) {
1630 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1631 free(accum_buf);
1632 if (len != accum_size)
1633 err = got_ferror(outfile, GOT_ERR_IO);
1635 rewind(outfile);
1636 if (err == NULL)
1637 *result_size = accum_size;
1638 return err;
1641 const struct got_error *
1642 got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1643 struct got_delta_chain *deltas, struct got_pack *pack)
1645 const struct got_error *err = NULL;
1646 struct got_delta *delta;
1647 uint8_t *base_buf = NULL, *accum_buf = NULL;
1648 size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0;
1649 uint64_t max_size = 0;
1650 int n = 0;
1652 *outbuf = NULL;
1653 *outlen = 0;
1655 if (STAILQ_EMPTY(&deltas->entries))
1656 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1658 if (pack->delta_cache) {
1659 uint8_t *delta_buf = NULL, *fulltext = NULL;
1660 size_t delta_len, fulltext_len;
1662 delta = STAILQ_LAST(&deltas->entries, got_delta, entry);
1663 got_delta_cache_get(&delta_buf, &delta_len,
1664 &fulltext, &fulltext_len,
1665 pack->delta_cache, delta->data_offset);
1666 if (fulltext) {
1667 *outbuf = malloc(fulltext_len);
1668 if (*outbuf == NULL)
1669 return got_error_from_errno("malloc");
1670 memcpy(*outbuf, fulltext, fulltext_len);
1671 *outlen = fulltext_len;
1672 return NULL;
1676 /* Deltas are ordered in ascending order. */
1677 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1678 uint8_t *delta_buf = NULL, *fulltext = NULL;
1679 size_t delta_len, fulltext_len = 0;
1680 uint64_t base_size, result_size = 0;
1681 int cached = 1;
1682 if (n == 0) {
1683 off_t delta_data_offset;
1685 /* Plain object types are the delta base. */
1686 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1687 delta->type != GOT_OBJ_TYPE_TREE &&
1688 delta->type != GOT_OBJ_TYPE_BLOB &&
1689 delta->type != GOT_OBJ_TYPE_TAG) {
1690 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1691 goto done;
1694 delta_data_offset = delta->offset + delta->tslen;
1695 if (delta_data_offset >= pack->filesize) {
1696 err = got_error(GOT_ERR_PACK_OFFSET);
1697 goto done;
1700 if (pack->delta_cache) {
1701 got_delta_cache_get(&delta_buf, &delta_len,
1702 &fulltext, &fulltext_len,
1703 pack->delta_cache, delta_data_offset);
1706 if (delta->size > max_size)
1707 max_size = delta->size;
1708 if (delta->size > fulltext_len)
1709 max_size = fulltext_len;
1711 if (fulltext) {
1712 base_buf = malloc(fulltext_len);
1713 if (base_buf == NULL) {
1714 err = got_error_from_errno("malloc");
1715 goto done;
1717 memcpy(base_buf, fulltext, fulltext_len);
1718 base_bufsz = fulltext_len;
1719 } else if (pack->map) {
1720 size_t mapoff;
1722 if (delta_data_offset > SIZE_MAX) {
1723 return got_error_fmt(GOT_ERR_RANGE,
1724 "delta %lld offset would "
1725 "overflow size_t",
1726 (long long)delta_data_offset);
1729 mapoff = delta_data_offset;
1730 err = got_inflate_to_mem_mmap(&base_buf,
1731 &base_bufsz, NULL, NULL, pack->map,
1732 mapoff, pack->filesize - mapoff);
1733 } else {
1734 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1735 == -1) {
1736 err = got_error_from_errno("lseek");
1737 goto done;
1739 err = got_inflate_to_mem_fd(&base_buf,
1740 &base_bufsz, NULL, NULL, max_size,
1741 pack->fd);
1743 if (err)
1744 goto done;
1745 n++;
1747 if (pack->delta_cache && fulltext == NULL) {
1748 err = got_delta_cache_add(pack->delta_cache,
1749 delta_data_offset, NULL, 0);
1750 if (err) {
1751 if (err->code != GOT_ERR_NO_SPACE)
1752 goto done;
1753 err = NULL;
1754 } else {
1755 err = got_delta_cache_add_fulltext(
1756 pack->delta_cache,
1757 delta_data_offset,
1758 base_buf, base_bufsz);
1759 if (err &&
1760 err->code != GOT_ERR_NO_SPACE)
1761 goto done;
1762 err = NULL;
1765 continue;
1768 if (pack->delta_cache) {
1769 got_delta_cache_get(&delta_buf, &delta_len,
1770 &fulltext, &fulltext_len,
1771 pack->delta_cache, delta->data_offset);
1773 if (delta_buf == NULL) {
1774 cached = 0;
1775 err = read_delta_data(&delta_buf, &delta_len, NULL,
1776 delta->data_offset, pack);
1777 if (err)
1778 goto done;
1780 if (pack->delta_cache && !cached) {
1781 err = got_delta_cache_add(pack->delta_cache,
1782 delta->data_offset, delta_buf, delta_len);
1783 if (err == NULL)
1784 cached = 1;
1785 else if (err->code != GOT_ERR_NO_SPACE) {
1786 free(delta_buf);
1787 goto done;
1791 err = got_delta_get_sizes(&base_size, &result_size,
1792 delta_buf, delta_len);
1793 if (err) {
1794 if (!cached)
1795 free(delta_buf);
1796 goto done;
1798 if (base_size > max_size)
1799 max_size = base_size;
1800 if (result_size > max_size)
1801 max_size = result_size;
1802 if (fulltext_len > max_size)
1803 max_size = fulltext_len;
1805 if (max_size > base_bufsz) {
1806 uint8_t *p = realloc(base_buf, max_size);
1807 if (p == NULL) {
1808 err = got_error_from_errno("realloc");
1809 if (!cached)
1810 free(delta_buf);
1811 goto done;
1813 base_buf = p;
1814 base_bufsz = max_size;
1817 if (max_size > accum_bufsz) {
1818 uint8_t *p = realloc(accum_buf, max_size);
1819 if (p == NULL) {
1820 err = got_error_from_errno("realloc");
1821 if (!cached)
1822 free(delta_buf);
1823 goto done;
1825 accum_buf = p;
1826 accum_bufsz = max_size;
1829 if (fulltext) {
1830 memcpy(accum_buf, fulltext, fulltext_len);
1831 accum_size = fulltext_len;
1832 err = NULL;
1833 } else {
1834 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1835 delta_buf, delta_len, accum_buf,
1836 &accum_size, max_size);
1838 if (!cached)
1839 free(delta_buf);
1840 n++;
1841 if (err)
1842 goto done;
1844 if (fulltext == NULL) {
1845 err = got_delta_cache_add_fulltext(pack->delta_cache,
1846 delta->data_offset, accum_buf, accum_size);
1847 if (err) {
1848 if (err->code != GOT_ERR_NO_SPACE)
1849 goto done;
1850 err = NULL;
1854 if (n < deltas->nentries) {
1855 /* Accumulated delta becomes the new base. */
1856 uint8_t *tmp = accum_buf;
1857 size_t tmp_size = accum_bufsz;
1858 accum_buf = base_buf;
1859 accum_bufsz = base_bufsz;
1860 base_buf = tmp;
1861 base_bufsz = tmp_size;
1865 done:
1866 free(base_buf);
1867 if (err) {
1868 free(accum_buf);
1869 *outbuf = NULL;
1870 *outlen = 0;
1871 } else {
1872 *outbuf = accum_buf;
1873 *outlen = accum_size;
1875 return err;
1878 const struct got_error *
1879 got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1880 FILE *outfile, FILE *base_file, FILE *accum_file)
1882 const struct got_error *err = NULL;
1884 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1885 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1887 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1888 if (obj->pack_offset >= pack->filesize)
1889 return got_error(GOT_ERR_PACK_OFFSET);
1891 if (pack->map) {
1892 size_t mapoff;
1894 if (obj->pack_offset > SIZE_MAX) {
1895 return got_error_fmt(GOT_ERR_RANGE,
1896 "pack offset %lld would overflow size_t",
1897 (long long)obj->pack_offset);
1900 mapoff = obj->pack_offset;
1901 err = got_inflate_to_file_mmap(&obj->size, NULL, NULL,
1902 pack->map, mapoff, pack->filesize - mapoff,
1903 outfile);
1904 } else {
1905 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1906 return got_error_from_errno("lseek");
1907 err = got_inflate_to_file_fd(&obj->size, NULL, NULL,
1908 pack->fd, outfile);
1910 } else
1911 err = got_pack_dump_delta_chain_to_file(&obj->size,
1912 &obj->deltas, pack, outfile, base_file, accum_file);
1914 return err;
1917 const struct got_error *
1918 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1919 struct got_object *obj, struct got_pack *pack)
1921 const struct got_error *err = NULL;
1923 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1924 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1926 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1927 if (obj->pack_offset >= pack->filesize)
1928 return got_error(GOT_ERR_PACK_OFFSET);
1929 if (pack->map) {
1930 size_t mapoff;
1932 if (obj->pack_offset > SIZE_MAX) {
1933 return got_error_fmt(GOT_ERR_RANGE,
1934 "pack offset %lld would overflow size_t",
1935 (long long)obj->pack_offset);
1938 mapoff = obj->pack_offset;
1939 err = got_inflate_to_mem_mmap(buf, len, NULL, NULL,
1940 pack->map, mapoff, pack->filesize - mapoff);
1941 } else {
1942 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1943 return got_error_from_errno("lseek");
1944 err = got_inflate_to_mem_fd(buf, len, NULL, NULL,
1945 obj->size, pack->fd);
1947 } else
1948 err = got_pack_dump_delta_chain_to_mem(buf, len, &obj->deltas,
1949 pack);
1951 return err;
1954 static const struct got_error *
1955 read_raw_delta_data(uint8_t **delta_buf, size_t *delta_len,
1956 size_t *delta_len_compressed, uint64_t *base_size, uint64_t *result_size,
1957 off_t delta_data_offset, struct got_pack *pack, struct got_packidx *packidx)
1959 const struct got_error *err = NULL;
1961 /* Validate decompression and obtain the decompressed size. */
1962 err = read_delta_data(delta_buf, delta_len, delta_len_compressed,
1963 delta_data_offset, pack);
1964 if (err)
1965 return err;
1967 /* Read delta base/result sizes from head of delta stream. */
1968 err = got_delta_get_sizes(base_size, result_size,
1969 *delta_buf, *delta_len);
1970 if (err)
1971 goto done;
1973 /* Discard decompressed delta and read it again in compressed form. */
1974 free(*delta_buf);
1975 *delta_buf = malloc(*delta_len_compressed);
1976 if (*delta_buf == NULL) {
1977 err = got_error_from_errno("malloc");
1978 goto done;
1980 if (pack->map) {
1981 if (delta_data_offset >= pack->filesize) {
1982 err = got_error(GOT_ERR_PACK_OFFSET);
1983 goto done;
1985 memcpy(*delta_buf, pack->map + delta_data_offset,
1986 *delta_len_compressed);
1987 } else {
1988 ssize_t n;
1989 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
1990 err = got_error_from_errno("lseek");
1991 goto done;
1993 n = read(pack->fd, *delta_buf, *delta_len_compressed);
1994 if (n < 0) {
1995 err = got_error_from_errno("read");
1996 goto done;
1997 } else if (n != *delta_len_compressed) {
1998 err = got_error(GOT_ERR_IO);
1999 goto done;
2002 done:
2003 if (err) {
2004 free(*delta_buf);
2005 *delta_buf = NULL;
2006 *delta_len = 0;
2007 *delta_len_compressed = 0;
2008 *base_size = 0;
2009 *result_size = 0;
2011 return err;
2014 const struct got_error *
2015 got_packfile_extract_raw_delta(uint8_t **delta_buf, size_t *delta_size,
2016 size_t *delta_compressed_size, off_t *delta_offset,
2017 off_t *delta_data_offset, off_t *base_offset,
2018 struct got_object_id *base_id, uint64_t *base_size, uint64_t *result_size,
2019 struct got_pack *pack, struct got_packidx *packidx, int idx)
2021 const struct got_error *err = NULL;
2022 off_t offset;
2023 uint8_t type;
2024 uint64_t size;
2025 size_t tslen, delta_hdrlen;
2027 *delta_buf = NULL;
2028 *delta_size = 0;
2029 *delta_compressed_size = 0;
2030 *delta_offset = 0;
2031 *delta_data_offset = 0;
2032 *base_offset = 0;
2033 *base_size = 0;
2034 *result_size = 0;
2036 offset = got_packidx_get_object_offset(packidx, idx);
2037 if (offset == -1)
2038 return got_error(GOT_ERR_BAD_PACKIDX);
2040 if (offset >= pack->filesize)
2041 return got_error(GOT_ERR_PACK_OFFSET);
2043 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
2044 pack, offset);
2045 if (err)
2046 return err;
2048 if (tslen + size < tslen || offset + size < size ||
2049 tslen + offset < tslen)
2050 return got_error(GOT_ERR_PACK_OFFSET);
2052 switch (type) {
2053 case GOT_OBJ_TYPE_OFFSET_DELTA:
2054 err = got_pack_parse_offset_delta(base_offset, &delta_hdrlen,
2055 pack, offset, tslen);
2056 if (err)
2057 return err;
2058 break;
2059 case GOT_OBJ_TYPE_REF_DELTA:
2060 err = got_pack_parse_ref_delta(base_id, pack, offset, tslen);
2061 if (err)
2062 return err;
2063 delta_hdrlen = SHA1_DIGEST_LENGTH;
2064 break;
2065 default:
2066 return got_error_fmt(GOT_ERR_OBJ_TYPE,
2067 "non-delta object type %d found at offset %lld",
2068 type, (long long)offset);
2071 if (tslen + delta_hdrlen < delta_hdrlen ||
2072 offset + delta_hdrlen < delta_hdrlen)
2073 return got_error(GOT_ERR_BAD_DELTA);
2075 *delta_data_offset = offset + tslen + delta_hdrlen;
2076 err = read_raw_delta_data(delta_buf, delta_size, delta_compressed_size,
2077 base_size, result_size, *delta_data_offset, pack, packidx);
2078 if (err)
2079 return err;
2081 if (*delta_size != size) {
2082 err = got_error(GOT_ERR_BAD_DELTA);
2083 goto done;
2086 *delta_offset = offset;
2087 done:
2088 if (err) {
2089 free(*delta_buf);
2090 *delta_buf = NULL;
2091 *delta_size = 0;
2092 *delta_compressed_size = 0;
2093 *delta_offset = 0;
2094 *base_offset = 0;
2095 *base_size = 0;
2096 *result_size = 0;
2098 return err;