tog man page: tweak documentation of vertical/horizontal split for clarity
[got-portable.git] / lib / pack.c
blob054ae905f1c60527376ced496a88d48170382727
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.
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/uio.h>
20 #include <sys/mman.h>
21 #include <sys/resource.h>
22 #include <sys/socket.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <unistd.h>
32 #include <zlib.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_path.h"
38 #include "got_lib_sha1.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_delta_cache.h"
41 #include "got_lib_inflate.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_parse.h"
44 #include "got_lib_privsep.h"
45 #include "got_lib_pack.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 #endif
51 #ifndef MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 static const struct got_error *
56 verify_fanout_table(uint32_t *fanout_table)
58 int i;
60 for (i = 0; i < 0xff - 1; i++) {
61 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
62 return got_error(GOT_ERR_BAD_PACKIDX);
65 return NULL;
68 const struct got_error *
69 got_packidx_init_hdr(struct got_packidx *p, int verify, off_t packfile_size)
71 const struct got_error *err = NULL;
72 struct got_packidx_v2_hdr *h;
73 SHA1_CTX ctx;
74 uint8_t sha1[SHA1_DIGEST_LENGTH];
75 size_t nobj, len_fanout, len_ids, offset, remain;
76 ssize_t n;
77 int i;
79 SHA1Init(&ctx);
81 h = &p->hdr;
82 offset = 0;
83 remain = p->len;
85 if (remain < sizeof(*h->magic)) {
86 err = got_error(GOT_ERR_BAD_PACKIDX);
87 goto done;
89 if (p->map)
90 h->magic = (uint32_t *)(p->map + offset);
91 else {
92 h->magic = malloc(sizeof(*h->magic));
93 if (h->magic == NULL) {
94 err = got_error_from_errno("malloc");
95 goto done;
97 n = read(p->fd, h->magic, sizeof(*h->magic));
98 if (n < 0) {
99 err = got_error_from_errno("read");
100 goto done;
101 } else if (n != sizeof(*h->magic)) {
102 err = got_error(GOT_ERR_BAD_PACKIDX);
103 goto done;
106 if (*h->magic != htobe32(GOT_PACKIDX_V2_MAGIC)) {
107 err = got_error(GOT_ERR_BAD_PACKIDX);
108 goto done;
110 offset += sizeof(*h->magic);
111 remain -= sizeof(*h->magic);
113 if (verify)
114 SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
116 if (remain < sizeof(*h->version)) {
117 err = got_error(GOT_ERR_BAD_PACKIDX);
118 goto done;
120 if (p->map)
121 h->version = (uint32_t *)(p->map + offset);
122 else {
123 h->version = malloc(sizeof(*h->version));
124 if (h->version == NULL) {
125 err = got_error_from_errno("malloc");
126 goto done;
128 n = read(p->fd, h->version, sizeof(*h->version));
129 if (n < 0) {
130 err = got_error_from_errno("read");
131 goto done;
132 } else if (n != sizeof(*h->version)) {
133 err = got_error(GOT_ERR_BAD_PACKIDX);
134 goto done;
137 if (*h->version != htobe32(GOT_PACKIDX_VERSION)) {
138 err = got_error(GOT_ERR_BAD_PACKIDX);
139 goto done;
141 offset += sizeof(*h->version);
142 remain -= sizeof(*h->version);
144 if (verify)
145 SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
147 len_fanout =
148 sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
149 if (remain < len_fanout) {
150 err = got_error(GOT_ERR_BAD_PACKIDX);
151 goto done;
153 if (p->map)
154 h->fanout_table = (uint32_t *)(p->map + offset);
155 else {
156 h->fanout_table = malloc(len_fanout);
157 if (h->fanout_table == NULL) {
158 err = got_error_from_errno("malloc");
159 goto done;
161 n = read(p->fd, h->fanout_table, len_fanout);
162 if (n < 0) {
163 err = got_error_from_errno("read");
164 goto done;
165 } else if (n != len_fanout) {
166 err = got_error(GOT_ERR_BAD_PACKIDX);
167 goto done;
170 err = verify_fanout_table(h->fanout_table);
171 if (err)
172 goto done;
173 if (verify)
174 SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
175 offset += len_fanout;
176 remain -= len_fanout;
178 nobj = be32toh(h->fanout_table[0xff]);
179 len_ids = nobj * sizeof(*h->sorted_ids);
180 if (len_ids <= nobj || len_ids > remain) {
181 err = got_error(GOT_ERR_BAD_PACKIDX);
182 goto done;
184 if (p->map)
185 h->sorted_ids =
186 (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
187 else {
188 h->sorted_ids = malloc(len_ids);
189 if (h->sorted_ids == NULL) {
190 err = got_error(GOT_ERR_BAD_PACKIDX);
191 goto done;
193 n = read(p->fd, h->sorted_ids, len_ids);
194 if (n < 0)
195 err = got_error_from_errno("read");
196 else if (n != len_ids) {
197 err = got_error(GOT_ERR_BAD_PACKIDX);
198 goto done;
201 if (verify)
202 SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
203 offset += len_ids;
204 remain -= len_ids;
206 if (remain < nobj * sizeof(*h->crc32)) {
207 err = got_error(GOT_ERR_BAD_PACKIDX);
208 goto done;
210 if (p->map)
211 h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
212 else {
213 h->crc32 = malloc(nobj * sizeof(*h->crc32));
214 if (h->crc32 == NULL) {
215 err = got_error_from_errno("malloc");
216 goto done;
218 n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
219 if (n < 0)
220 err = got_error_from_errno("read");
221 else if (n != nobj * sizeof(*h->crc32)) {
222 err = got_error(GOT_ERR_BAD_PACKIDX);
223 goto done;
226 if (verify)
227 SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
228 remain -= nobj * sizeof(*h->crc32);
229 offset += nobj * sizeof(*h->crc32);
231 if (remain < nobj * sizeof(*h->offsets)) {
232 err = got_error(GOT_ERR_BAD_PACKIDX);
233 goto done;
235 if (p->map)
236 h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
237 else {
238 h->offsets = malloc(nobj * sizeof(*h->offsets));
239 if (h->offsets == NULL) {
240 err = got_error_from_errno("malloc");
241 goto done;
243 n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
244 if (n < 0)
245 err = got_error_from_errno("read");
246 else if (n != nobj * sizeof(*h->offsets)) {
247 err = got_error(GOT_ERR_BAD_PACKIDX);
248 goto done;
251 if (verify)
252 SHA1Update(&ctx, (uint8_t *)h->offsets,
253 nobj * sizeof(*h->offsets));
254 remain -= nobj * sizeof(*h->offsets);
255 offset += nobj * sizeof(*h->offsets);
257 /* Large file offsets are contained only in files > 2GB. */
258 if (verify || packfile_size > 0x7fffffff) {
259 for (i = 0; i < nobj; i++) {
260 uint32_t o = h->offsets[i];
261 if (o & htobe32(GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX))
262 p->nlargeobj++;
265 if (p->nlargeobj == 0)
266 goto checksum;
267 else if (packfile_size <= 0x7fffffff) {
268 err = got_error(GOT_ERR_BAD_PACKIDX);
269 goto done;
272 if (remain < p->nlargeobj * sizeof(*h->large_offsets)) {
273 err = got_error(GOT_ERR_BAD_PACKIDX);
274 goto done;
276 if (p->map)
277 h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
278 else {
279 h->large_offsets = malloc(p->nlargeobj *
280 sizeof(*h->large_offsets));
281 if (h->large_offsets == NULL) {
282 err = got_error_from_errno("malloc");
283 goto done;
285 n = read(p->fd, h->large_offsets,
286 p->nlargeobj * sizeof(*h->large_offsets));
287 if (n < 0)
288 err = got_error_from_errno("read");
289 else if (n != p->nlargeobj * sizeof(*h->large_offsets)) {
290 err = got_error(GOT_ERR_BAD_PACKIDX);
291 goto done;
294 if (verify)
295 SHA1Update(&ctx, (uint8_t*)h->large_offsets,
296 p->nlargeobj * sizeof(*h->large_offsets));
297 remain -= p->nlargeobj * sizeof(*h->large_offsets);
298 offset += p->nlargeobj * sizeof(*h->large_offsets);
300 checksum:
301 if (remain < sizeof(*h->trailer)) {
302 err = got_error(GOT_ERR_BAD_PACKIDX);
303 goto done;
305 if (p->map)
306 h->trailer =
307 (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
308 else {
309 h->trailer = malloc(sizeof(*h->trailer));
310 if (h->trailer == NULL) {
311 err = got_error_from_errno("malloc");
312 goto done;
314 n = read(p->fd, h->trailer, sizeof(*h->trailer));
315 if (n < 0)
316 err = got_error_from_errno("read");
317 else if (n != sizeof(*h->trailer)) {
318 err = got_error(GOT_ERR_BAD_PACKIDX);
319 goto done;
322 if (verify) {
323 SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
324 SHA1Final(sha1, &ctx);
325 if (memcmp(h->trailer->packidx_sha1, sha1,
326 SHA1_DIGEST_LENGTH) != 0)
327 err = got_error(GOT_ERR_PACKIDX_CSUM);
329 done:
330 return err;
333 const struct got_error *
334 got_packidx_open(struct got_packidx **packidx,
335 int dir_fd, const char *relpath, int verify)
337 const struct got_error *err = NULL;
338 struct got_packidx *p = NULL;
339 char *pack_relpath;
340 struct stat idx_sb, pack_sb;
342 *packidx = NULL;
344 err = got_packidx_get_packfile_path(&pack_relpath, relpath);
345 if (err)
346 return err;
349 * Ensure that a corresponding pack file exists.
350 * Some Git repositories have this problem. Git seems to ignore
351 * the existence of lonely pack index files but we do not.
353 if (fstatat(dir_fd, pack_relpath, &pack_sb, 0) == -1) {
354 if (errno == ENOENT) {
355 err = got_error_fmt(GOT_ERR_LONELY_PACKIDX,
356 "%s", relpath);
357 } else
358 err = got_error_from_errno2("fstatat", pack_relpath);
359 goto done;
362 p = calloc(1, sizeof(*p));
363 if (p == NULL) {
364 err = got_error_from_errno("calloc");
365 goto done;
368 p->fd = openat(dir_fd, relpath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
369 if (p->fd == -1) {
370 err = got_error_from_errno2("openat", relpath);
371 goto done;
374 if (fstat(p->fd, &idx_sb) != 0) {
375 err = got_error_from_errno2("fstat", relpath);
376 goto done;
378 p->len = idx_sb.st_size;
379 if (p->len < sizeof(p->hdr)) {
380 err = got_error(GOT_ERR_BAD_PACKIDX);
381 goto done;
384 p->path_packidx = strdup(relpath);
385 if (p->path_packidx == NULL) {
386 err = got_error_from_errno("strdup");
387 goto done;
390 #ifndef GOT_PACK_NO_MMAP
391 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
392 if (p->map == MAP_FAILED) {
393 if (errno != ENOMEM) {
394 err = got_error_from_errno("mmap");
395 goto done;
397 p->map = NULL; /* fall back to read(2) */
399 #endif
401 err = got_packidx_init_hdr(p, verify, pack_sb.st_size);
402 done:
403 if (err) {
404 if (p)
405 got_packidx_close(p);
406 } else
407 *packidx = p;
408 free(pack_relpath);
409 return err;
412 const struct got_error *
413 got_packidx_close(struct got_packidx *packidx)
415 const struct got_error *err = NULL;
417 free(packidx->path_packidx);
418 if (packidx->map) {
419 if (munmap(packidx->map, packidx->len) == -1)
420 err = got_error_from_errno("munmap");
421 } else {
422 free(packidx->hdr.magic);
423 free(packidx->hdr.version);
424 free(packidx->hdr.fanout_table);
425 free(packidx->hdr.sorted_ids);
426 free(packidx->hdr.crc32);
427 free(packidx->hdr.offsets);
428 free(packidx->hdr.large_offsets);
429 free(packidx->hdr.trailer);
431 if (close(packidx->fd) == -1 && err == NULL)
432 err = got_error_from_errno("close");
433 free(packidx->sorted_offsets);
434 free(packidx->sorted_large_offsets);
435 free(packidx);
437 return err;
440 const struct got_error *
441 got_packidx_get_packfile_path(char **path_packfile, const char *path_packidx)
443 size_t size;
445 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
446 size = strlen(path_packidx) + 2;
447 if (size < GOT_PACKFILE_NAMELEN + 1)
448 return got_error_path(path_packidx, GOT_ERR_BAD_PATH);
450 *path_packfile = malloc(size);
451 if (*path_packfile == NULL)
452 return got_error_from_errno("malloc");
454 /* Copy up to and excluding ".idx". */
455 if (strlcpy(*path_packfile, path_packidx,
456 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
457 return got_error(GOT_ERR_NO_SPACE);
459 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
460 return got_error(GOT_ERR_NO_SPACE);
462 return NULL;
465 off_t
466 got_packidx_get_object_offset(struct got_packidx *packidx, int idx)
468 uint32_t offset = be32toh(packidx->hdr.offsets[idx]);
469 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
470 uint64_t loffset;
471 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
472 if (idx < 0 || idx >= packidx->nlargeobj ||
473 packidx->hdr.large_offsets == NULL)
474 return -1;
475 loffset = be64toh(packidx->hdr.large_offsets[idx]);
476 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
478 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
482 got_packidx_get_object_idx(struct got_packidx *packidx,
483 struct got_object_id *id)
485 u_int8_t id0 = id->sha1[0];
486 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
487 int left = 0, right = totobj - 1;
489 if (id0 > 0)
490 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
492 while (left <= right) {
493 struct got_packidx_object_id *oid;
494 int i, cmp;
496 i = ((left + right) / 2);
497 oid = &packidx->hdr.sorted_ids[i];
498 cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
499 if (cmp == 0)
500 return i;
501 else if (cmp > 0)
502 left = i + 1;
503 else if (cmp < 0)
504 right = i - 1;
507 return -1;
510 static int
511 offset_cmp(const void *pa, const void *pb)
513 const struct got_pack_offset_index *a, *b;
515 a = (const struct got_pack_offset_index *)pa;
516 b = (const struct got_pack_offset_index *)pb;
518 if (a->offset < b->offset)
519 return -1;
520 else if (a->offset > b->offset)
521 return 1;
523 return 0;
526 static int
527 large_offset_cmp(const void *pa, const void *pb)
529 const struct got_pack_large_offset_index *a, *b;
531 a = (const struct got_pack_large_offset_index *)pa;
532 b = (const struct got_pack_large_offset_index *)pb;
534 if (a->offset < b->offset)
535 return -1;
536 else if (a->offset > b->offset)
537 return 1;
539 return 0;
542 static const struct got_error *
543 build_offset_index(struct got_packidx *p)
545 uint32_t nobj = be32toh(p->hdr.fanout_table[0xff]);
546 unsigned int i, j, k;
548 p->sorted_offsets = calloc(nobj - p->nlargeobj,
549 sizeof(p->sorted_offsets[0]));
550 if (p->sorted_offsets == NULL)
551 return got_error_from_errno("calloc");
553 if (p->nlargeobj > 0) {
554 p->sorted_large_offsets = calloc(p->nlargeobj,
555 sizeof(p->sorted_large_offsets[0]));
556 if (p->sorted_large_offsets == NULL)
557 return got_error_from_errno("calloc");
560 j = 0;
561 k = 0;
562 for (i = 0; i < nobj; i++) {
563 uint32_t offset = be32toh(p->hdr.offsets[i]);
564 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
565 uint64_t loffset;
566 uint32_t idx;
567 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
568 if (idx >= p->nlargeobj ||
569 p->nlargeobj == 0 ||
570 p->hdr.large_offsets == NULL)
571 return got_error(GOT_ERR_BAD_PACKIDX);
572 loffset = be64toh(p->hdr.large_offsets[idx]);
573 p->sorted_large_offsets[j].offset = loffset;
574 p->sorted_large_offsets[j].idx = i;
575 j++;
576 } else {
577 p->sorted_offsets[k].offset = offset;
578 p->sorted_offsets[k].idx = i;
579 k++;
582 if (j != p->nlargeobj || k != nobj - p->nlargeobj)
583 return got_error(GOT_ERR_BAD_PACKIDX);
585 qsort(p->sorted_offsets, nobj - p->nlargeobj,
586 sizeof(p->sorted_offsets[0]), offset_cmp);
588 if (p->sorted_large_offsets)
589 qsort(p->sorted_large_offsets, p->nlargeobj,
590 sizeof(p->sorted_large_offsets[0]), large_offset_cmp);
592 return NULL;
595 const struct got_error *
596 got_packidx_get_offset_idx(int *idx, struct got_packidx *packidx, off_t offset)
598 const struct got_error *err;
599 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
600 int i, left, right;
602 *idx = -1;
604 if (packidx->sorted_offsets == NULL) {
605 err = build_offset_index(packidx);
606 if (err)
607 return err;
610 if (offset >= 0x7fffffff) {
611 uint64_t lo;
612 left = 0, right = packidx->nlargeobj - 1;
613 while (left <= right) {
614 i = ((left + right) / 2);
615 lo = packidx->sorted_large_offsets[i].offset;
616 if (lo == offset) {
617 *idx = packidx->sorted_large_offsets[i].idx;
618 break;
619 } else if (offset > lo)
620 left = i + 1;
621 else if (offset < lo)
622 right = i - 1;
624 } else {
625 uint32_t o;
626 left = 0, right = totobj - packidx->nlargeobj - 1;
627 while (left <= right) {
628 i = ((left + right) / 2);
629 o = packidx->sorted_offsets[i].offset;
630 if (o == offset) {
631 *idx = packidx->sorted_offsets[i].idx;
632 break;
633 } else if (offset > o)
634 left = i + 1;
635 else if (offset < o)
636 right = i - 1;
640 return NULL;
643 const struct got_error *
644 got_packidx_get_object_id(struct got_object_id *id,
645 struct got_packidx *packidx, int idx)
647 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
648 struct got_packidx_object_id *oid;
650 if (idx < 0 || idx >= totobj)
651 return got_error(GOT_ERR_NO_OBJ);
653 oid = &packidx->hdr.sorted_ids[idx];
654 memcpy(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
655 return NULL;
658 const struct got_error *
659 got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
660 struct got_packidx *packidx, const char *id_str_prefix)
662 const struct got_error *err = NULL;
663 u_int8_t id0;
664 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
665 char hex[3];
666 size_t prefix_len = strlen(id_str_prefix);
667 struct got_packidx_object_id *oid;
668 uint32_t i = 0;
670 STAILQ_INIT(matched_ids);
672 if (prefix_len < 2)
673 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
675 hex[0] = id_str_prefix[0];
676 hex[1] = id_str_prefix[1];
677 hex[2] = '\0';
678 if (!got_parse_xdigit(&id0, hex))
679 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
681 if (id0 > 0)
682 i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
683 oid = &packidx->hdr.sorted_ids[i];
684 while (i < totobj && oid->sha1[0] == id0) {
685 char id_str[SHA1_DIGEST_STRING_LENGTH];
686 struct got_object_qid *qid;
687 int cmp;
689 if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
690 return got_error(GOT_ERR_NO_SPACE);
692 cmp = strncmp(id_str, id_str_prefix, prefix_len);
693 if (cmp < 0) {
694 oid = &packidx->hdr.sorted_ids[++i];
695 continue;
696 } else if (cmp > 0)
697 break;
699 err = got_object_qid_alloc_partial(&qid);
700 if (err)
701 break;
702 memcpy(qid->id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
703 STAILQ_INSERT_TAIL(matched_ids, qid, entry);
705 oid = &packidx->hdr.sorted_ids[++i];
708 if (err)
709 got_object_id_queue_free(matched_ids);
710 return err;
713 static void
714 set_max_datasize(void)
716 struct rlimit rl;
718 if (getrlimit(RLIMIT_DATA, &rl) != 0)
719 return;
721 rl.rlim_cur = rl.rlim_max;
722 setrlimit(RLIMIT_DATA, &rl);
725 const struct got_error *
726 got_pack_start_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
728 const struct got_error *err = NULL;
729 int imsg_fds[2];
730 pid_t pid;
731 struct imsgbuf *ibuf;
733 ibuf = calloc(1, sizeof(*ibuf));
734 if (ibuf == NULL)
735 return got_error_from_errno("calloc");
737 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
738 if (pack->privsep_child == NULL) {
739 err = got_error_from_errno("calloc");
740 free(ibuf);
741 return err;
743 pack->child_has_tempfiles = 0;
744 pack->child_has_delta_outfd = 0;
746 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
747 err = got_error_from_errno("socketpair");
748 goto done;
751 pid = fork();
752 if (pid == -1) {
753 err = got_error_from_errno("fork");
754 goto done;
755 } else if (pid == 0) {
756 set_max_datasize();
757 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
758 pack->path_packfile);
759 /* not reached */
762 if (close(imsg_fds[1]) == -1)
763 return got_error_from_errno("close");
764 pack->privsep_child->imsg_fd = imsg_fds[0];
765 pack->privsep_child->pid = pid;
766 imsg_init(ibuf, imsg_fds[0]);
767 pack->privsep_child->ibuf = ibuf;
769 err = got_privsep_init_pack_child(ibuf, pack, packidx);
770 if (err) {
771 const struct got_error *child_err;
772 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
773 child_err = got_privsep_wait_for_child(
774 pack->privsep_child->pid);
775 if (child_err && err == NULL)
776 err = child_err;
778 done:
779 if (err) {
780 free(ibuf);
781 free(pack->privsep_child);
782 pack->privsep_child = NULL;
784 return err;
787 static const struct got_error *
788 pack_stop_privsep_child(struct got_pack *pack)
790 const struct got_error *err = NULL;
792 if (pack->privsep_child == NULL)
793 return NULL;
795 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
796 if (err)
797 return err;
798 err = got_privsep_wait_for_child(pack->privsep_child->pid);
799 if (close(pack->privsep_child->imsg_fd) == -1 && err == NULL)
800 err = got_error_from_errno("close");
801 free(pack->privsep_child->ibuf);
802 free(pack->privsep_child);
803 pack->privsep_child = NULL;
804 return err;
807 const struct got_error *
808 got_pack_close(struct got_pack *pack)
810 const struct got_error *err = NULL;
812 err = pack_stop_privsep_child(pack);
813 if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
814 err = got_error_from_errno("munmap");
815 if (pack->fd != -1 && close(pack->fd) == -1 && err == NULL)
816 err = got_error_from_errno("close");
817 pack->fd = -1;
818 free(pack->path_packfile);
819 pack->path_packfile = NULL;
820 pack->filesize = 0;
821 if (pack->delta_cache) {
822 got_delta_cache_free(pack->delta_cache);
823 pack->delta_cache = NULL;
827 * Leave accumfd and basefd alone. They are managed by the
828 * repository layer and can be reused.
831 return err;
834 const struct got_error *
835 got_pack_parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
836 struct got_pack *pack, off_t offset)
838 uint8_t t = 0;
839 uint64_t s = 0;
840 uint8_t sizeN;
841 size_t mapoff = 0;
842 int i = 0;
844 *len = 0;
846 if (offset >= pack->filesize)
847 return got_error(GOT_ERR_PACK_OFFSET);
849 if (pack->map) {
850 mapoff = (size_t)offset;
851 } else {
852 if (lseek(pack->fd, offset, SEEK_SET) == -1)
853 return got_error_from_errno("lseek");
856 do {
857 /* We do not support size values which don't fit in 64 bit. */
858 if (i > 9)
859 return got_error(GOT_ERR_NO_SPACE);
861 if (pack->map) {
862 if (mapoff + sizeof(sizeN) >= pack->filesize)
863 return got_error(GOT_ERR_BAD_PACKFILE);
864 sizeN = *(pack->map + mapoff);
865 mapoff += sizeof(sizeN);
866 } else {
867 ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
868 if (n < 0)
869 return got_error_from_errno("read");
870 if (n != sizeof(sizeN))
871 return got_error(GOT_ERR_BAD_PACKFILE);
873 *len += sizeof(sizeN);
875 if (i == 0) {
876 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
877 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
878 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
879 } else {
880 size_t shift = 4 + 7 * (i - 1);
881 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
883 i++;
884 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
886 *type = t;
887 *size = s;
888 return NULL;
891 static const struct got_error *
892 open_plain_object(struct got_object **obj, struct got_object_id *id,
893 uint8_t type, off_t offset, size_t size, int idx)
895 *obj = calloc(1, sizeof(**obj));
896 if (*obj == NULL)
897 return got_error_from_errno("calloc");
899 (*obj)->type = type;
900 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
901 (*obj)->pack_idx = idx;
902 (*obj)->hdrlen = 0;
903 (*obj)->size = size;
904 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
905 (*obj)->pack_offset = offset;
907 return NULL;
910 static const struct got_error *
911 parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
912 off_t delta_offset)
914 int64_t o = 0;
915 uint8_t offN;
916 int i = 0;
918 *offset = 0;
919 *len = 0;
921 do {
922 /* We do not support offset values which don't fit in 64 bit. */
923 if (i > 8)
924 return got_error(GOT_ERR_NO_SPACE);
926 if (pack->map) {
927 size_t mapoff;
928 mapoff = (size_t)delta_offset + *len;
929 if (mapoff + sizeof(offN) >= pack->filesize)
930 return got_error(GOT_ERR_PACK_OFFSET);
931 offN = *(pack->map + mapoff);
932 } else {
933 ssize_t n;
934 n = read(pack->fd, &offN, sizeof(offN));
935 if (n < 0)
936 return got_error_from_errno("read");
937 if (n != sizeof(offN))
938 return got_error(GOT_ERR_BAD_PACKFILE);
940 *len += sizeof(offN);
942 if (i == 0)
943 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
944 else {
945 o++;
946 o <<= 7;
947 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
949 i++;
950 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
952 *offset = o;
953 return NULL;
956 const struct got_error *
957 got_pack_parse_offset_delta(off_t *base_offset, size_t *len,
958 struct got_pack *pack, off_t offset, int tslen)
960 const struct got_error *err;
961 int64_t negoffset;
962 size_t negofflen;
964 *len = 0;
966 err = parse_negative_offset(&negoffset, &negofflen, pack,
967 offset + tslen);
968 if (err)
969 return err;
971 /* Compute the base object's offset (must be in the same pack file). */
972 *base_offset = (offset - negoffset);
973 if (*base_offset <= 0)
974 return got_error(GOT_ERR_BAD_PACKFILE);
976 *len = negofflen;
977 return NULL;
980 static const struct got_error *
981 read_delta_data(uint8_t **delta_buf, size_t *delta_len,
982 size_t *delta_compressed_len, size_t delta_data_offset,
983 struct got_pack *pack)
985 const struct got_error *err = NULL;
986 size_t consumed = 0;
988 if (pack->map) {
989 if (delta_data_offset >= pack->filesize)
990 return got_error(GOT_ERR_PACK_OFFSET);
991 err = got_inflate_to_mem_mmap(delta_buf, delta_len,
992 &consumed, NULL, pack->map, delta_data_offset,
993 pack->filesize - delta_data_offset);
994 if (err)
995 return err;
996 } else {
997 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
998 return got_error_from_errno("lseek");
999 err = got_inflate_to_mem_fd(delta_buf, delta_len,
1000 &consumed, NULL, 0, pack->fd);
1001 if (err)
1002 return err;
1005 if (delta_compressed_len)
1006 *delta_compressed_len = consumed;
1008 return NULL;
1011 static const struct got_error *
1012 add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
1013 int delta_type, size_t delta_size, size_t delta_data_offset)
1015 struct got_delta *delta;
1017 delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
1018 delta_data_offset);
1019 if (delta == NULL)
1020 return got_error_from_errno("got_delta_open");
1021 /* delta is freed in got_object_close() */
1022 deltas->nentries++;
1023 STAILQ_INSERT_HEAD(&deltas->entries, delta, entry);
1024 return NULL;
1027 static const struct got_error *
1028 resolve_offset_delta(struct got_delta_chain *deltas,
1029 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1030 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1033 const struct got_error *err;
1034 off_t base_offset;
1035 uint8_t base_type;
1036 uint64_t base_size;
1037 size_t base_tslen;
1038 off_t delta_data_offset;
1039 size_t consumed;
1041 err = got_pack_parse_offset_delta(&base_offset, &consumed, pack,
1042 delta_offset, tslen);
1043 if (err)
1044 return err;
1046 delta_data_offset = delta_offset + tslen + consumed;
1047 if (delta_data_offset >= pack->filesize)
1048 return got_error(GOT_ERR_PACK_OFFSET);
1050 if (pack->map == NULL) {
1051 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1052 if (delta_data_offset == -1)
1053 return got_error_from_errno("lseek");
1056 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1057 delta_data_offset);
1058 if (err)
1059 return err;
1061 /* An offset delta must be in the same packfile. */
1062 if (base_offset >= pack->filesize)
1063 return got_error(GOT_ERR_PACK_OFFSET);
1065 err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1066 &base_tslen, pack, base_offset);
1067 if (err)
1068 return err;
1070 return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1071 base_tslen, base_type, base_size, recursion - 1);
1074 const struct got_error *
1075 got_pack_parse_ref_delta(struct got_object_id *id,
1076 struct got_pack *pack, off_t delta_offset, int tslen)
1078 if (pack->map) {
1079 size_t mapoff = delta_offset + tslen;
1080 if (mapoff + sizeof(*id) >= pack->filesize)
1081 return got_error(GOT_ERR_PACK_OFFSET);
1082 memcpy(id, pack->map + mapoff, sizeof(*id));
1083 } else {
1084 ssize_t n;
1085 n = read(pack->fd, id, sizeof(*id));
1086 if (n < 0)
1087 return got_error_from_errno("read");
1088 if (n != sizeof(*id))
1089 return got_error(GOT_ERR_BAD_PACKFILE);
1092 return NULL;
1095 static const struct got_error *
1096 resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
1097 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
1098 size_t delta_size, unsigned int recursion)
1100 const struct got_error *err;
1101 struct got_object_id id;
1102 int idx;
1103 off_t base_offset;
1104 uint8_t base_type;
1105 uint64_t base_size;
1106 size_t base_tslen;
1107 off_t delta_data_offset;
1109 if (delta_offset + tslen >= pack->filesize)
1110 return got_error(GOT_ERR_PACK_OFFSET);
1112 err = got_pack_parse_ref_delta(&id, pack, delta_offset, tslen);
1113 if (err)
1114 return err;
1115 if (pack->map) {
1116 delta_data_offset = delta_offset + tslen + sizeof(id);
1117 } else {
1118 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1119 if (delta_data_offset == -1)
1120 return got_error_from_errno("lseek");
1123 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1124 delta_data_offset);
1125 if (err)
1126 return err;
1128 /* Delta base must be in the same pack file. */
1129 idx = got_packidx_get_object_idx(packidx, &id);
1130 if (idx == -1)
1131 return got_error(GOT_ERR_NO_OBJ);
1133 base_offset = got_packidx_get_object_offset(packidx, idx);
1134 if (base_offset == (uint64_t)-1)
1135 return got_error(GOT_ERR_BAD_PACKIDX);
1137 if (base_offset >= pack->filesize)
1138 return got_error(GOT_ERR_PACK_OFFSET);
1140 err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1141 &base_tslen, pack, base_offset);
1142 if (err)
1143 return err;
1145 return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1146 base_tslen, base_type, base_size, recursion - 1);
1149 const struct got_error *
1150 got_pack_resolve_delta_chain(struct got_delta_chain *deltas,
1151 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1152 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1154 const struct got_error *err = NULL;
1156 if (--recursion == 0)
1157 return got_error(GOT_ERR_RECURSION);
1159 switch (delta_type) {
1160 case GOT_OBJ_TYPE_COMMIT:
1161 case GOT_OBJ_TYPE_TREE:
1162 case GOT_OBJ_TYPE_BLOB:
1163 case GOT_OBJ_TYPE_TAG:
1164 /* Plain types are the final delta base. Recursion ends. */
1165 err = add_delta(deltas, delta_offset, tslen, delta_type,
1166 delta_size, 0);
1167 break;
1168 case GOT_OBJ_TYPE_OFFSET_DELTA:
1169 err = resolve_offset_delta(deltas, packidx, pack,
1170 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1171 break;
1172 case GOT_OBJ_TYPE_REF_DELTA:
1173 err = resolve_ref_delta(deltas, packidx, pack,
1174 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1175 break;
1176 default:
1177 return got_error(GOT_ERR_OBJ_TYPE);
1180 return err;
1183 static const struct got_error *
1184 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
1185 struct got_pack *pack, struct got_object_id *id, off_t offset,
1186 size_t tslen, int delta_type, size_t delta_size, int idx)
1188 const struct got_error *err = NULL;
1189 int resolved_type;
1191 *obj = calloc(1, sizeof(**obj));
1192 if (*obj == NULL)
1193 return got_error_from_errno("calloc");
1195 (*obj)->flags = 0;
1196 (*obj)->hdrlen = 0;
1197 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1198 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1199 (*obj)->pack_offset = offset + tslen;
1201 STAILQ_INIT(&(*obj)->deltas.entries);
1202 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1203 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1204 (*obj)->pack_idx = idx;
1206 err = got_pack_resolve_delta_chain(&(*obj)->deltas, packidx, pack,
1207 offset, tslen, delta_type, delta_size,
1208 GOT_DELTA_CHAIN_RECURSION_MAX);
1209 if (err)
1210 goto done;
1212 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1213 if (err)
1214 goto done;
1215 (*obj)->type = resolved_type;
1216 done:
1217 if (err) {
1218 got_object_close(*obj);
1219 *obj = NULL;
1221 return err;
1224 const struct got_error *
1225 got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
1226 struct got_packidx *packidx, int idx, struct got_object_id *id)
1228 const struct got_error *err = NULL;
1229 off_t offset;
1230 uint8_t type;
1231 uint64_t size;
1232 size_t tslen;
1234 *obj = NULL;
1236 offset = got_packidx_get_object_offset(packidx, idx);
1237 if (offset == (uint64_t)-1)
1238 return got_error(GOT_ERR_BAD_PACKIDX);
1240 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1241 pack, offset);
1242 if (err)
1243 return err;
1245 switch (type) {
1246 case GOT_OBJ_TYPE_COMMIT:
1247 case GOT_OBJ_TYPE_TREE:
1248 case GOT_OBJ_TYPE_BLOB:
1249 case GOT_OBJ_TYPE_TAG:
1250 err = open_plain_object(obj, id, type, offset + tslen,
1251 size, idx);
1252 break;
1253 case GOT_OBJ_TYPE_OFFSET_DELTA:
1254 case GOT_OBJ_TYPE_REF_DELTA:
1255 err = open_delta_object(obj, packidx, pack, id, offset,
1256 tslen, type, size, idx);
1257 break;
1258 default:
1259 err = got_error(GOT_ERR_OBJ_TYPE);
1260 break;
1263 return err;
1266 const struct got_error *
1267 got_pack_get_delta_chain_max_size(uint64_t *max_size,
1268 struct got_delta_chain *deltas, struct got_pack *pack)
1270 struct got_delta *delta;
1271 uint64_t base_size = 0, result_size = 0;
1273 *max_size = 0;
1274 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1275 /* Plain object types are the delta base. */
1276 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1277 delta->type != GOT_OBJ_TYPE_TREE &&
1278 delta->type != GOT_OBJ_TYPE_BLOB &&
1279 delta->type != GOT_OBJ_TYPE_TAG) {
1280 const struct got_error *err;
1281 uint8_t *delta_buf;
1282 size_t delta_len;
1283 int cached = 1;
1285 got_delta_cache_get(&delta_buf, &delta_len,
1286 pack->delta_cache, delta->data_offset);
1287 if (delta_buf == NULL) {
1288 cached = 0;
1289 err = read_delta_data(&delta_buf, &delta_len,
1290 NULL, delta->data_offset, pack);
1291 if (err)
1292 return err;
1293 err = got_delta_cache_add(pack->delta_cache,
1294 delta->data_offset, delta_buf, delta_len);
1295 if (err == NULL)
1296 cached = 1;
1297 else if (err->code != GOT_ERR_NO_SPACE) {
1298 free(delta_buf);
1299 return err;
1302 err = got_delta_get_sizes(&base_size, &result_size,
1303 delta_buf, delta_len);
1304 if (!cached)
1305 free(delta_buf);
1306 if (err)
1307 return err;
1308 } else
1309 base_size = delta->size;
1310 if (base_size > *max_size)
1311 *max_size = base_size;
1312 if (result_size > *max_size)
1313 *max_size = result_size;
1316 return NULL;
1319 const struct got_error *
1320 got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj,
1321 struct got_pack *pack)
1323 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1324 return got_error(GOT_ERR_OBJ_TYPE);
1326 return got_pack_get_delta_chain_max_size(size, &obj->deltas, pack);
1329 const struct got_error *
1330 got_pack_dump_delta_chain_to_file(size_t *result_size,
1331 struct got_delta_chain *deltas, struct got_pack *pack, FILE *outfile,
1332 FILE *base_file, FILE *accum_file)
1334 const struct got_error *err = NULL;
1335 struct got_delta *delta;
1336 uint8_t *base_buf = NULL, *accum_buf = NULL, *delta_buf;
1337 size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0, delta_len;
1338 /* We process small enough files entirely in memory for speed. */
1339 const size_t max_bufsize = GOT_DELTA_RESULT_SIZE_CACHED_MAX;
1340 uint64_t max_size = 0;
1341 int n = 0;
1343 *result_size = 0;
1345 if (STAILQ_EMPTY(&deltas->entries))
1346 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1348 if (fseeko(base_file, 0L, SEEK_SET) == -1)
1349 return got_error_from_errno("fseeko");
1350 if (fseeko(accum_file, 0L, SEEK_SET) == -1)
1351 return got_error_from_errno("fseeko");
1353 /* Deltas are ordered in ascending order. */
1354 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1355 uint64_t base_size, result_size = 0;
1356 int cached = 1;
1357 if (n == 0) {
1358 size_t mapoff;
1359 off_t delta_data_offset;
1361 /* Plain object types are the delta base. */
1362 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1363 delta->type != GOT_OBJ_TYPE_TREE &&
1364 delta->type != GOT_OBJ_TYPE_BLOB &&
1365 delta->type != GOT_OBJ_TYPE_TAG) {
1366 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1367 goto done;
1370 delta_data_offset = delta->offset + delta->tslen;
1371 if (delta_data_offset >= pack->filesize) {
1372 err = got_error(GOT_ERR_PACK_OFFSET);
1373 goto done;
1375 if (pack->map == NULL) {
1376 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1377 == -1) {
1378 err = got_error_from_errno("lseek");
1379 goto done;
1382 if (delta->size > max_size)
1383 max_size = delta->size;
1384 if (max_size > max_bufsize) {
1385 if (pack->map) {
1386 mapoff = (size_t)delta_data_offset;
1387 err = got_inflate_to_file_mmap(
1388 &base_bufsz, NULL, NULL, pack->map,
1389 mapoff, pack->filesize - mapoff,
1390 base_file);
1391 } else
1392 err = got_inflate_to_file_fd(
1393 &base_bufsz, NULL, NULL, pack->fd,
1394 base_file);
1395 } else {
1396 accum_buf = malloc(max_size);
1397 if (accum_buf == NULL) {
1398 err = got_error_from_errno("malloc");
1399 goto done;
1401 accum_bufsz = max_size;
1402 if (pack->map) {
1403 mapoff = (size_t)delta_data_offset;
1404 err = got_inflate_to_mem_mmap(&base_buf,
1405 &base_bufsz, NULL, NULL,
1406 pack->map, mapoff,
1407 pack->filesize - mapoff);
1408 } else
1409 err = got_inflate_to_mem_fd(&base_buf,
1410 &base_bufsz, NULL, NULL, max_size,
1411 pack->fd);
1413 if (err)
1414 goto done;
1415 n++;
1416 if (base_buf == NULL)
1417 rewind(base_file);
1418 continue;
1421 got_delta_cache_get(&delta_buf, &delta_len,
1422 pack->delta_cache, delta->data_offset);
1423 if (delta_buf == NULL) {
1424 cached = 0;
1425 err = read_delta_data(&delta_buf, &delta_len, NULL,
1426 delta->data_offset, pack);
1427 if (err)
1428 goto done;
1429 err = got_delta_cache_add(pack->delta_cache,
1430 delta->data_offset, delta_buf, delta_len);
1431 if (err == NULL)
1432 cached = 1;
1433 else if (err->code != GOT_ERR_NO_SPACE) {
1434 free(delta_buf);
1435 goto done;
1439 err = got_delta_get_sizes(&base_size, &result_size,
1440 delta_buf, delta_len);
1441 if (err)
1442 goto done;
1443 if (base_size > max_size)
1444 max_size = base_size;
1445 if (result_size > max_size)
1446 max_size = result_size;
1448 if (base_buf && max_size > max_bufsize) {
1449 /* Switch from buffers to temporary files. */
1450 size_t w = fwrite(base_buf, 1, base_bufsz,
1451 base_file);
1452 if (w != base_bufsz) {
1453 err = got_ferror(outfile, GOT_ERR_IO);
1454 goto done;
1456 free(base_buf);
1457 base_buf = NULL;
1458 free(accum_buf);
1459 accum_buf = NULL;
1462 if (base_buf && max_size > base_bufsz) {
1463 uint8_t *p = realloc(base_buf, max_size);
1464 if (p == NULL) {
1465 err = got_error_from_errno("realloc");
1466 goto done;
1468 base_buf = p;
1469 base_bufsz = max_size;
1472 if (accum_buf && max_size > accum_bufsz) {
1473 uint8_t *p = realloc(accum_buf, max_size);
1474 if (p == NULL) {
1475 err = got_error_from_errno("realloc");
1476 goto done;
1478 accum_buf = p;
1479 accum_bufsz = max_size;
1482 if (base_buf) {
1483 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1484 delta_buf, delta_len, accum_buf,
1485 &accum_size, max_size);
1486 n++;
1487 } else {
1488 err = got_delta_apply(base_file, delta_buf,
1489 delta_len,
1490 /* Final delta application writes to output file. */
1491 ++n < deltas->nentries ? accum_file : outfile,
1492 &accum_size);
1494 if (!cached)
1495 free(delta_buf);
1496 if (err)
1497 goto done;
1499 if (n < deltas->nentries) {
1500 /* Accumulated delta becomes the new base. */
1501 if (base_buf) {
1502 uint8_t *tmp = accum_buf;
1503 size_t tmp_size = accum_bufsz;
1504 accum_buf = base_buf;
1505 accum_bufsz = base_bufsz;
1506 base_buf = tmp;
1507 base_bufsz = tmp_size;
1508 } else {
1509 FILE *tmp = accum_file;
1510 accum_file = base_file;
1511 base_file = tmp;
1512 rewind(base_file);
1513 rewind(accum_file);
1518 done:
1519 free(base_buf);
1520 if (accum_buf) {
1521 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1522 free(accum_buf);
1523 if (len != accum_size)
1524 err = got_ferror(outfile, GOT_ERR_IO);
1526 rewind(outfile);
1527 if (err == NULL)
1528 *result_size = accum_size;
1529 return err;
1532 const struct got_error *
1533 got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1534 struct got_delta_chain *deltas, struct got_pack *pack)
1536 const struct got_error *err = NULL;
1537 struct got_delta *delta;
1538 uint8_t *base_buf = NULL, *accum_buf = NULL, *delta_buf;
1539 size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0, delta_len;
1540 uint64_t max_size = 0;
1541 int n = 0;
1543 *outbuf = NULL;
1544 *outlen = 0;
1546 if (STAILQ_EMPTY(&deltas->entries))
1547 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1549 /* Deltas are ordered in ascending order. */
1550 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1551 uint64_t base_size, result_size = 0;
1552 int cached = 1;
1553 if (n == 0) {
1554 size_t delta_data_offset;
1556 /* Plain object types are the delta base. */
1557 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1558 delta->type != GOT_OBJ_TYPE_TREE &&
1559 delta->type != GOT_OBJ_TYPE_BLOB &&
1560 delta->type != GOT_OBJ_TYPE_TAG) {
1561 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1562 goto done;
1565 delta_data_offset = delta->offset + delta->tslen;
1566 if (delta_data_offset >= pack->filesize) {
1567 err = got_error(GOT_ERR_PACK_OFFSET);
1568 goto done;
1571 if (delta->size > max_size)
1572 max_size = delta->size;
1574 if (pack->map) {
1575 size_t mapoff = (size_t)delta_data_offset;
1576 err = got_inflate_to_mem_mmap(&base_buf,
1577 &base_bufsz, NULL, NULL, pack->map,
1578 mapoff, pack->filesize - mapoff);
1579 } else {
1580 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1581 == -1) {
1582 err = got_error_from_errno("lseek");
1583 goto done;
1585 err = got_inflate_to_mem_fd(&base_buf,
1586 &base_bufsz, NULL, NULL, max_size,
1587 pack->fd);
1589 if (err)
1590 goto done;
1591 n++;
1592 continue;
1595 got_delta_cache_get(&delta_buf, &delta_len,
1596 pack->delta_cache, delta->data_offset);
1597 if (delta_buf == NULL) {
1598 cached = 0;
1599 err = read_delta_data(&delta_buf, &delta_len, NULL,
1600 delta->data_offset, pack);
1601 if (err)
1602 goto done;
1603 err = got_delta_cache_add(pack->delta_cache,
1604 delta->data_offset, delta_buf, delta_len);
1605 if (err == NULL)
1606 cached = 1;
1607 else if (err->code != GOT_ERR_NO_SPACE) {
1608 free(delta_buf);
1609 goto done;
1613 err = got_delta_get_sizes(&base_size, &result_size,
1614 delta_buf, delta_len);
1615 if (err)
1616 goto done;
1617 if (base_size > max_size)
1618 max_size = base_size;
1619 if (result_size > max_size)
1620 max_size = result_size;
1622 if (max_size > base_bufsz) {
1623 uint8_t *p = realloc(base_buf, max_size);
1624 if (p == NULL) {
1625 err = got_error_from_errno("realloc");
1626 goto done;
1628 base_buf = p;
1629 base_bufsz = max_size;
1632 if (max_size > accum_bufsz) {
1633 uint8_t *p = realloc(accum_buf, max_size);
1634 if (p == NULL) {
1635 err = got_error_from_errno("realloc");
1636 goto done;
1638 accum_buf = p;
1639 accum_bufsz = max_size;
1642 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1643 delta_buf, delta_len, accum_buf,
1644 &accum_size, max_size);
1645 if (!cached)
1646 free(delta_buf);
1647 n++;
1648 if (err)
1649 goto done;
1651 if (n < deltas->nentries) {
1652 /* Accumulated delta becomes the new base. */
1653 uint8_t *tmp = accum_buf;
1654 size_t tmp_size = accum_bufsz;
1655 accum_buf = base_buf;
1656 accum_bufsz = base_bufsz;
1657 base_buf = tmp;
1658 base_bufsz = tmp_size;
1662 done:
1663 free(base_buf);
1664 if (err) {
1665 free(accum_buf);
1666 *outbuf = NULL;
1667 *outlen = 0;
1668 } else {
1669 *outbuf = accum_buf;
1670 *outlen = accum_size;
1672 return err;
1675 const struct got_error *
1676 got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1677 FILE *outfile, FILE *base_file, FILE *accum_file)
1679 const struct got_error *err = NULL;
1681 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1682 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1684 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1685 if (obj->pack_offset >= pack->filesize)
1686 return got_error(GOT_ERR_PACK_OFFSET);
1688 if (pack->map) {
1689 size_t mapoff = (size_t)obj->pack_offset;
1690 err = got_inflate_to_file_mmap(&obj->size, NULL, NULL,
1691 pack->map, mapoff, pack->filesize - mapoff,
1692 outfile);
1693 } else {
1694 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1695 return got_error_from_errno("lseek");
1696 err = got_inflate_to_file_fd(&obj->size, NULL, NULL,
1697 pack->fd, outfile);
1699 } else
1700 err = got_pack_dump_delta_chain_to_file(&obj->size,
1701 &obj->deltas, pack, outfile, base_file, accum_file);
1703 return err;
1706 const struct got_error *
1707 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1708 struct got_object *obj, struct got_pack *pack)
1710 const struct got_error *err = NULL;
1712 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1713 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1715 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1716 if (obj->pack_offset >= pack->filesize)
1717 return got_error(GOT_ERR_PACK_OFFSET);
1718 if (pack->map) {
1719 size_t mapoff = (size_t)obj->pack_offset;
1720 err = got_inflate_to_mem_mmap(buf, len, NULL, NULL,
1721 pack->map, mapoff, pack->filesize - mapoff);
1722 } else {
1723 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1724 return got_error_from_errno("lseek");
1725 err = got_inflate_to_mem_fd(buf, len, NULL, NULL,
1726 obj->size, pack->fd);
1728 } else
1729 err = got_pack_dump_delta_chain_to_mem(buf, len, &obj->deltas,
1730 pack);
1732 return err;
1735 static const struct got_error *
1736 read_raw_delta_data(uint8_t **delta_buf, size_t *delta_len,
1737 size_t *delta_len_compressed, uint64_t *base_size, uint64_t *result_size,
1738 off_t delta_data_offset, struct got_pack *pack, struct got_packidx *packidx)
1740 const struct got_error *err = NULL;
1742 /* Validate decompression and obtain the decompressed size. */
1743 err = read_delta_data(delta_buf, delta_len, delta_len_compressed,
1744 delta_data_offset, pack);
1745 if (err)
1746 return err;
1748 /* Read delta base/result sizes from head of delta stream. */
1749 err = got_delta_get_sizes(base_size, result_size,
1750 *delta_buf, *delta_len);
1751 if (err)
1752 goto done;
1754 /* Discard decompressed delta and read it again in compressed form. */
1755 free(*delta_buf);
1756 *delta_buf = malloc(*delta_len_compressed);
1757 if (*delta_buf == NULL) {
1758 err = got_error_from_errno("malloc");
1759 goto done;
1761 if (pack->map) {
1762 if (delta_data_offset >= pack->filesize)
1763 err = got_error(GOT_ERR_PACK_OFFSET);
1764 memcpy(*delta_buf, pack->map + delta_data_offset,
1765 *delta_len_compressed);
1766 } else {
1767 ssize_t n;
1768 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
1769 err = got_error_from_errno("lseek");
1770 goto done;
1772 n = read(pack->fd, *delta_buf, *delta_len_compressed);
1773 if (n < 0) {
1774 err = got_error_from_errno("read");
1775 goto done;
1776 } else if (n != *delta_len_compressed) {
1777 err = got_error(GOT_ERR_IO);
1778 goto done;
1781 done:
1782 if (err) {
1783 free(*delta_buf);
1784 *delta_buf = NULL;
1785 *delta_len = 0;
1786 *delta_len_compressed = 0;
1787 *base_size = 0;
1788 *result_size = 0;
1790 return err;
1793 const struct got_error *
1794 got_packfile_extract_raw_delta(uint8_t **delta_buf, size_t *delta_size,
1795 size_t *delta_compressed_size, off_t *delta_offset, off_t *base_offset,
1796 struct got_object_id *base_id, uint64_t *base_size, uint64_t *result_size,
1797 struct got_pack *pack, struct got_packidx *packidx, int idx)
1799 const struct got_error *err = NULL;
1800 off_t offset;
1801 uint8_t type;
1802 uint64_t size;
1803 size_t tslen, delta_hdrlen;
1804 off_t delta_data_offset;
1806 *delta_buf = NULL;
1807 *delta_size = 0;
1808 *delta_compressed_size = 0;
1809 *delta_offset = 0;
1810 *base_offset = 0;
1811 *base_size = 0;
1812 *result_size = 0;
1814 offset = got_packidx_get_object_offset(packidx, idx);
1815 if (offset == (uint64_t)-1)
1816 return got_error(GOT_ERR_BAD_PACKIDX);
1818 if (offset >= pack->filesize)
1819 return got_error(GOT_ERR_PACK_OFFSET);
1821 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1822 pack, offset);
1823 if (err)
1824 return err;
1826 if (tslen + size < tslen || offset + size < size ||
1827 tslen + offset < tslen)
1828 return got_error(GOT_ERR_PACK_OFFSET);
1830 switch (type) {
1831 case GOT_OBJ_TYPE_OFFSET_DELTA:
1832 err = got_pack_parse_offset_delta(base_offset, &delta_hdrlen,
1833 pack, offset, tslen);
1834 if (err)
1835 return err;
1836 break;
1837 case GOT_OBJ_TYPE_REF_DELTA:
1838 err = got_pack_parse_ref_delta(base_id, pack, offset, tslen);
1839 if (err)
1840 return err;
1841 delta_hdrlen = SHA1_DIGEST_LENGTH;
1842 break;
1843 default:
1844 return got_error_fmt(GOT_ERR_OBJ_TYPE,
1845 "non-delta object type %d found at offset %llu",
1846 type, offset);
1849 if (tslen + delta_hdrlen < delta_hdrlen ||
1850 offset + delta_hdrlen < delta_hdrlen)
1851 return got_error(GOT_ERR_BAD_DELTA);
1853 delta_data_offset = offset + tslen + delta_hdrlen;
1854 err = read_raw_delta_data(delta_buf, delta_size, delta_compressed_size,
1855 base_size, result_size, delta_data_offset, pack, packidx);
1856 if (err)
1857 return err;
1859 if (*delta_size != size) {
1860 err = got_error(GOT_ERR_BAD_DELTA);
1861 goto done;
1864 *delta_offset = offset;
1865 done:
1866 if (err) {
1867 free(*delta_buf);
1868 *delta_buf = NULL;
1869 *delta_size = 0;
1870 *delta_compressed_size = 0;
1871 *delta_offset = 0;
1872 *base_offset = 0;
1873 *base_size = 0;
1874 *result_size = 0;
1876 return err;