2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 0a0a3048 2018-01-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 0a0a3048 2018-01-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 0a0a3048 2018-01-10 stsp * copyright notice and this permission notice appear in all copies.
8 0a0a3048 2018-01-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0a0a3048 2018-01-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0a0a3048 2018-01-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0a0a3048 2018-01-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0a0a3048 2018-01-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0a0a3048 2018-01-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0a0a3048 2018-01-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a1fd68d8 2018-01-12 stsp #include <sys/types.h>
18 0a0a3048 2018-01-10 stsp #include <sys/stat.h>
19 a1fd68d8 2018-01-12 stsp #include <sys/queue.h>
20 876c234b 2018-09-10 stsp #include <sys/uio.h>
21 57b35b75 2018-06-22 stsp #include <sys/mman.h>
23 7e656b93 2018-03-17 stsp #include <fcntl.h>
24 a1fd68d8 2018-01-12 stsp #include <errno.h>
25 0a0a3048 2018-01-10 stsp #include <stdio.h>
26 a1fd68d8 2018-01-12 stsp #include <stdint.h>
27 0a0a3048 2018-01-10 stsp #include <stdlib.h>
28 0a0a3048 2018-01-10 stsp #include <string.h>
29 0a0a3048 2018-01-10 stsp #include <limits.h>
30 0a0a3048 2018-01-10 stsp #include <sha1.h>
31 0a0a3048 2018-01-10 stsp #include <endian.h>
32 a1fd68d8 2018-01-12 stsp #include <zlib.h>
33 876c234b 2018-09-10 stsp #include <imsg.h>
35 0a0a3048 2018-01-10 stsp #include "got_error.h"
36 a1fd68d8 2018-01-12 stsp #include "got_object.h"
37 511a516b 2018-05-19 stsp #include "got_opentemp.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
42 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
44 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
45 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
47 79b11c62 2018-03-09 stsp #ifndef nitems
48 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 0a0a3048 2018-01-10 stsp static const struct got_error *
56 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
60 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
61 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
62 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
65 0a0a3048 2018-01-10 stsp return NULL;
68 1510f469 2018-09-09 stsp const struct got_error *
69 1510f469 2018-09-09 stsp got_pack_get_packfile_size(size_t *size, const char *path)
71 0a0a3048 2018-01-10 stsp struct stat sb;
76 1c7e24f1 2018-04-02 stsp dot = strrchr(path, '.');
77 0a0a3048 2018-01-10 stsp if (dot == NULL)
78 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
80 1c7e24f1 2018-04-02 stsp /* Path must point to a pack index or to a pack file. */
81 72fb0363 2018-06-04 stsp if (strcmp(dot, GOT_PACKIDX_SUFFIX) == 0) {
82 97128b57 2018-04-02 stsp const struct got_error *err = NULL;
83 1c7e24f1 2018-04-02 stsp char *path_pack;
84 1c7e24f1 2018-04-02 stsp char base_path[PATH_MAX];
86 1c7e24f1 2018-04-02 stsp /* Convert pack index path to pack file path. */
87 1c7e24f1 2018-04-02 stsp if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
88 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_NO_SPACE);
89 1c7e24f1 2018-04-02 stsp dot = strrchr(base_path, '.');
90 1c7e24f1 2018-04-02 stsp if (dot == NULL)
91 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
92 1c7e24f1 2018-04-02 stsp *dot = '\0';
93 1c7e24f1 2018-04-02 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
94 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
96 97128b57 2018-04-02 stsp if (stat(path_pack, &sb) != 0)
97 97128b57 2018-04-02 stsp err = got_error_from_errno();
98 0a0a3048 2018-01-10 stsp free(path_pack);
100 97128b57 2018-04-02 stsp return err;
101 56cca8e5 2018-06-04 stsp } else if (strcmp(dot, GOT_PACKFILE_SUFFIX) == 0) {
102 1c7e24f1 2018-04-02 stsp if (stat(path, &sb) != 0)
103 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
105 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
107 0a0a3048 2018-01-10 stsp *size = sb.st_size;
111 0a0a3048 2018-01-10 stsp const struct got_error *
112 817c5a18 2018-09-09 stsp got_packidx_init_hdr(struct got_packidx *p, int verify)
114 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
115 817c5a18 2018-09-09 stsp struct got_packidx_v2_hdr *h;
116 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
117 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
118 817c5a18 2018-09-09 stsp size_t nobj, len_fanout, len_ids, offset, remain;
121 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
123 57b35b75 2018-06-22 stsp h = &p->hdr;
124 57b35b75 2018-06-22 stsp offset = 0;
125 57b35b75 2018-06-22 stsp remain = p->len;
127 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->magic)) {
128 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
131 fc79a48d 2018-07-09 stsp if (p->map)
132 fc79a48d 2018-07-09 stsp h->magic = (uint32_t *)(p->map + offset);
134 fc79a48d 2018-07-09 stsp h->magic = malloc(sizeof(*h->magic));
135 fc79a48d 2018-07-09 stsp if (h->magic == NULL) {
136 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
139 fc79a48d 2018-07-09 stsp n = read(p->fd, h->magic, sizeof(*h->magic));
141 faaa1c0f 2018-09-15 stsp err = got_error_from_errno();
142 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->magic)) {
143 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
147 57b35b75 2018-06-22 stsp if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
148 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
151 57b35b75 2018-06-22 stsp offset += sizeof(*h->magic);
152 57b35b75 2018-06-22 stsp remain -= sizeof(*h->magic);
154 0cb74cf4 2018-07-08 stsp if (verify)
155 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
157 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->version)) {
158 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
161 fc79a48d 2018-07-09 stsp if (p->map)
162 fc79a48d 2018-07-09 stsp h->version = (uint32_t *)(p->map + offset);
164 fc79a48d 2018-07-09 stsp h->version = malloc(sizeof(*h->version));
165 fc79a48d 2018-07-09 stsp if (h->version == NULL) {
166 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
169 fc79a48d 2018-07-09 stsp n = read(p->fd, h->version, sizeof(*h->version));
171 faaa1c0f 2018-09-15 stsp err = got_error_from_errno();
172 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->version)) {
173 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
177 57b35b75 2018-06-22 stsp if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
178 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
181 57b35b75 2018-06-22 stsp offset += sizeof(*h->version);
182 57b35b75 2018-06-22 stsp remain -= sizeof(*h->version);
184 0cb74cf4 2018-07-08 stsp if (verify)
185 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
187 57b35b75 2018-06-22 stsp len_fanout =
188 57b35b75 2018-06-22 stsp sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
189 57b35b75 2018-06-22 stsp if (remain < len_fanout) {
190 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
193 fc79a48d 2018-07-09 stsp if (p->map)
194 fc79a48d 2018-07-09 stsp h->fanout_table = (uint32_t *)(p->map + offset);
196 fc79a48d 2018-07-09 stsp h->fanout_table = malloc(len_fanout);
197 fc79a48d 2018-07-09 stsp if (h->fanout_table == NULL) {
198 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
201 fc79a48d 2018-07-09 stsp n = read(p->fd, h->fanout_table, len_fanout);
203 faaa1c0f 2018-09-15 stsp err = got_error_from_errno();
204 faaa1c0f 2018-09-15 stsp else if (n != len_fanout) {
205 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
209 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
212 0cb74cf4 2018-07-08 stsp if (verify)
213 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
214 57b35b75 2018-06-22 stsp offset += len_fanout;
215 57b35b75 2018-06-22 stsp remain -= len_fanout;
217 c8262310 2018-06-04 stsp nobj = betoh32(h->fanout_table[0xff]);
218 57b35b75 2018-06-22 stsp len_ids = nobj * sizeof(*h->sorted_ids);
219 57b35b75 2018-06-22 stsp if (len_ids <= nobj || len_ids > remain) {
220 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
223 fc79a48d 2018-07-09 stsp if (p->map)
224 fc79a48d 2018-07-09 stsp h->sorted_ids =
225 fc79a48d 2018-07-09 stsp (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
227 fc79a48d 2018-07-09 stsp h->sorted_ids = malloc(len_ids);
228 fc79a48d 2018-07-09 stsp if (h->sorted_ids == NULL) {
229 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
232 fc79a48d 2018-07-09 stsp n = read(p->fd, h->sorted_ids, len_ids);
234 faaa1c0f 2018-09-15 stsp err = got_error_from_errno();
235 faaa1c0f 2018-09-15 stsp else if (n != len_ids) {
236 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
240 0cb74cf4 2018-07-08 stsp if (verify)
241 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
242 57b35b75 2018-06-22 stsp offset += len_ids;
243 57b35b75 2018-06-22 stsp remain -= len_ids;
245 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->crc32)) {
246 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
249 fc79a48d 2018-07-09 stsp if (p->map)
250 fc79a48d 2018-07-09 stsp h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
252 fc79a48d 2018-07-09 stsp h->crc32 = malloc(nobj * sizeof(*h->crc32));
253 fc79a48d 2018-07-09 stsp if (h->crc32 == NULL) {
254 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
257 fc79a48d 2018-07-09 stsp n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
259 faaa1c0f 2018-09-15 stsp err = got_error_from_errno();
260 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->crc32)) {
261 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
265 0cb74cf4 2018-07-08 stsp if (verify)
266 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
267 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->crc32);
268 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->crc32);
270 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->offsets)) {
271 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
274 fc79a48d 2018-07-09 stsp if (p->map)
275 fc79a48d 2018-07-09 stsp h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
277 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->offsets));
278 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
279 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
282 fc79a48d 2018-07-09 stsp n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
284 faaa1c0f 2018-09-15 stsp err = got_error_from_errno();
285 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->offsets)) {
286 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
290 0cb74cf4 2018-07-08 stsp if (verify)
291 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->offsets,
292 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->offsets));
293 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->offsets);
294 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->offsets);
296 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
297 57b35b75 2018-06-22 stsp if (p->len <= 0x80000000)
298 0a0a3048 2018-01-10 stsp goto checksum;
300 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->large_offsets)) {
301 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
304 fc79a48d 2018-07-09 stsp if (p->map)
305 fc79a48d 2018-07-09 stsp h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
307 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->large_offsets));
308 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
309 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
312 fc79a48d 2018-07-09 stsp n = read(p->fd, h->large_offsets,
313 fc79a48d 2018-07-09 stsp nobj * sizeof(*h->large_offsets));
315 faaa1c0f 2018-09-15 stsp err = got_error_from_errno();
316 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->large_offsets)) {
317 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
321 0cb74cf4 2018-07-08 stsp if (verify)
322 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
323 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->large_offsets));
324 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->large_offsets);
325 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->large_offsets);
328 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->trailer)) {
329 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
332 fc79a48d 2018-07-09 stsp if (p->map)
333 fc79a48d 2018-07-09 stsp h->trailer =
334 fc79a48d 2018-07-09 stsp (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
336 fc79a48d 2018-07-09 stsp h->trailer = malloc(sizeof(*h->trailer));
337 fc79a48d 2018-07-09 stsp if (h->trailer == NULL) {
338 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
341 fc79a48d 2018-07-09 stsp n = read(p->fd, h->trailer, sizeof(*h->trailer));
343 faaa1c0f 2018-09-15 stsp err = got_error_from_errno();
344 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->trailer)) {
345 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
349 0cb74cf4 2018-07-08 stsp if (verify) {
350 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
351 0cb74cf4 2018-07-08 stsp SHA1Final(sha1, &ctx);
352 0cb74cf4 2018-07-08 stsp if (memcmp(h->trailer->packidx_sha1, sha1,
353 0cb74cf4 2018-07-08 stsp SHA1_DIGEST_LENGTH) != 0)
354 0cb74cf4 2018-07-08 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
357 817c5a18 2018-09-09 stsp return err;
360 817c5a18 2018-09-09 stsp const struct got_error *
361 817c5a18 2018-09-09 stsp got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
363 817c5a18 2018-09-09 stsp const struct got_error *err = NULL;
364 817c5a18 2018-09-09 stsp struct got_packidx *p;
366 817c5a18 2018-09-09 stsp *packidx = NULL;
368 817c5a18 2018-09-09 stsp p = calloc(1, sizeof(*p));
369 817c5a18 2018-09-09 stsp if (p == NULL)
370 817c5a18 2018-09-09 stsp return got_error_from_errno();
372 817c5a18 2018-09-09 stsp p->fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
373 817c5a18 2018-09-09 stsp if (p->fd == -1)
374 817c5a18 2018-09-09 stsp return got_error_from_errno();
376 1510f469 2018-09-09 stsp err = got_pack_get_packfile_size(&p->len, path);
378 817c5a18 2018-09-09 stsp close(p->fd);
380 817c5a18 2018-09-09 stsp return err;
382 817c5a18 2018-09-09 stsp if (p->len < sizeof(p->hdr)) {
383 817c5a18 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
384 817c5a18 2018-09-09 stsp close(p->fd);
386 817c5a18 2018-09-09 stsp return err;
389 817c5a18 2018-09-09 stsp p->path_packidx = strdup(path);
390 817c5a18 2018-09-09 stsp if (p->path_packidx == NULL) {
391 817c5a18 2018-09-09 stsp err = got_error_from_errno();
395 817c5a18 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
396 817c5a18 2018-09-09 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
397 817c5a18 2018-09-09 stsp if (p->map == MAP_FAILED)
398 817c5a18 2018-09-09 stsp p->map = NULL; /* fall back to read(2) */
401 817c5a18 2018-09-09 stsp err = got_packidx_init_hdr(p, verify);
404 0a0a3048 2018-01-10 stsp got_packidx_close(p);
406 0a0a3048 2018-01-10 stsp *packidx = p;
408 0a0a3048 2018-01-10 stsp return err;
411 57b35b75 2018-06-22 stsp const struct got_error *
412 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
414 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
416 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
417 fc79a48d 2018-07-09 stsp if (packidx->map) {
418 57b35b75 2018-06-22 stsp if (munmap(packidx->map, packidx->len) == -1)
419 57b35b75 2018-06-22 stsp err = got_error_from_errno();
421 fc79a48d 2018-07-09 stsp free(packidx->hdr.magic);
422 fc79a48d 2018-07-09 stsp free(packidx->hdr.version);
423 fc79a48d 2018-07-09 stsp free(packidx->hdr.fanout_table);
424 fc79a48d 2018-07-09 stsp free(packidx->hdr.sorted_ids);
425 fc79a48d 2018-07-09 stsp free(packidx->hdr.crc32);
426 fc79a48d 2018-07-09 stsp free(packidx->hdr.offsets);
427 fc79a48d 2018-07-09 stsp free(packidx->hdr.large_offsets);
428 fc79a48d 2018-07-09 stsp free(packidx->hdr.trailer);
430 3a6ce05a 2019-02-11 stsp if (close(packidx->fd) != 0 && err == NULL)
431 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
432 0a0a3048 2018-01-10 stsp free(packidx);
434 57b35b75 2018-06-22 stsp return err;
437 a1fd68d8 2018-01-12 stsp static off_t
438 6fd11751 2018-06-04 stsp get_object_offset(struct got_packidx *packidx, int idx)
440 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
441 6fd11751 2018-06-04 stsp uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
442 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
443 a1fd68d8 2018-01-12 stsp uint64_t loffset;
444 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
445 6fd11751 2018-06-04 stsp if (idx < 0 || idx > totobj ||
446 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
448 6fd11751 2018-06-04 stsp loffset = betoh64(packidx->hdr.large_offsets[idx]);
449 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
451 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
455 1510f469 2018-09-09 stsp got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
457 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
458 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
459 40aeb19c 2018-06-22 stsp int left = 0, right = totobj - 1;
461 a1fd68d8 2018-01-12 stsp if (id0 > 0)
462 40aeb19c 2018-06-22 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
464 40aeb19c 2018-06-22 stsp while (left <= right) {
465 57b35b75 2018-06-22 stsp struct got_packidx_object_id *oid;
466 40aeb19c 2018-06-22 stsp int i, cmp;
468 40aeb19c 2018-06-22 stsp i = ((left + right) / 2);
469 40aeb19c 2018-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
470 57b35b75 2018-06-22 stsp cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
471 16dcbf91 2018-04-01 stsp if (cmp == 0)
473 40aeb19c 2018-06-22 stsp else if (cmp > 0)
474 40aeb19c 2018-06-22 stsp left = i + 1;
475 40aeb19c 2018-06-22 stsp else if (cmp < 0)
476 40aeb19c 2018-06-22 stsp right = i - 1;
482 876c234b 2018-09-10 stsp const struct got_error *
483 876c234b 2018-09-10 stsp got_pack_stop_privsep_child(struct got_pack *pack)
485 96732e0b 2018-11-11 stsp const struct got_error *err = NULL;
487 876c234b 2018-09-10 stsp if (pack->privsep_child == NULL)
488 876c234b 2018-09-10 stsp return NULL;
490 876c234b 2018-09-10 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
492 96732e0b 2018-11-11 stsp return err;
493 96732e0b 2018-11-11 stsp err = got_privsep_wait_for_child(pack->privsep_child->pid);
494 876c234b 2018-09-10 stsp free(pack->privsep_child);
495 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
496 876c234b 2018-09-10 stsp return err;
499 d7464085 2018-07-09 stsp const struct got_error *
500 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
502 d7464085 2018-07-09 stsp const struct got_error *err = NULL;
504 876c234b 2018-09-10 stsp err = got_pack_stop_privsep_child(pack);
505 876c234b 2018-09-10 stsp if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
506 d7464085 2018-07-09 stsp err = got_error_from_errno();
507 3a6ce05a 2019-02-11 stsp if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL)
508 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
509 8b2180d4 2018-04-26 stsp pack->fd = -1;
510 4589e373 2018-03-17 stsp free(pack->path_packfile);
511 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
512 4589e373 2018-03-17 stsp pack->filesize = 0;
514 c7fe698a 2018-01-23 stsp return err;
517 c7fe698a 2018-01-23 stsp static const struct got_error *
518 d7464085 2018-07-09 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
519 d7464085 2018-07-09 stsp struct got_pack *pack, off_t offset)
521 a487c1d0 2018-01-14 stsp uint8_t t = 0;
522 a487c1d0 2018-01-14 stsp uint64_t s = 0;
523 a487c1d0 2018-01-14 stsp uint8_t sizeN;
524 d7464085 2018-07-09 stsp size_t mapoff = 0;
529 d7464085 2018-07-09 stsp if (offset >= pack->filesize)
530 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
532 d7464085 2018-07-09 stsp if (pack->map) {
533 d7464085 2018-07-09 stsp mapoff = (size_t)offset;
535 d7464085 2018-07-09 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1)
536 d7464085 2018-07-09 stsp return got_error_from_errno();
540 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
542 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
544 d7464085 2018-07-09 stsp if (pack->map) {
545 d7464085 2018-07-09 stsp sizeN = *(pack->map + mapoff);
546 d7464085 2018-07-09 stsp mapoff += sizeof(sizeN);
548 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
550 d7464085 2018-07-09 stsp return got_error_from_errno();
551 d7464085 2018-07-09 stsp if (n != sizeof(sizeN))
552 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
554 d7464085 2018-07-09 stsp *len += sizeof(sizeN);
556 a1fd68d8 2018-01-12 stsp if (i == 0) {
557 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
558 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
559 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
561 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
562 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
565 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
569 a487c1d0 2018-01-14 stsp return NULL;
572 a1fd68d8 2018-01-12 stsp static const struct got_error *
573 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
574 c59b3346 2018-09-11 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size, int idx)
576 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
577 6ccb713b 2018-01-19 stsp if (*obj == NULL)
578 0a585a0d 2018-03-17 stsp return got_error_from_errno();
580 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
581 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
582 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
583 6ccb713b 2018-01-19 stsp free(*obj);
584 6ccb713b 2018-01-19 stsp *obj = NULL;
585 0a585a0d 2018-03-17 stsp return err;
588 6ccb713b 2018-01-19 stsp (*obj)->type = type;
589 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
590 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
591 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
592 6ccb713b 2018-01-19 stsp (*obj)->size = size;
593 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
594 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
596 b107e67f 2018-01-19 stsp return NULL;
599 b107e67f 2018-01-19 stsp static const struct got_error *
600 d7464085 2018-07-09 stsp parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
601 d7464085 2018-07-09 stsp off_t delta_offset)
603 b107e67f 2018-01-19 stsp int64_t o = 0;
604 b107e67f 2018-01-19 stsp uint8_t offN;
610 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
612 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
614 d7464085 2018-07-09 stsp if (pack->map) {
615 d7464085 2018-07-09 stsp size_t mapoff;
616 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
617 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
618 d7464085 2018-07-09 stsp mapoff = (size_t)delta_offset + *len;
619 d7464085 2018-07-09 stsp offN = *(pack->map + mapoff);
622 d7464085 2018-07-09 stsp n = read(pack->fd, &offN, sizeof(offN));
624 d7464085 2018-07-09 stsp return got_error_from_errno();
625 d7464085 2018-07-09 stsp if (n != sizeof(offN))
626 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
628 d7464085 2018-07-09 stsp *len += sizeof(offN);
630 b107e67f 2018-01-19 stsp if (i == 0)
631 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
635 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
638 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
640 b107e67f 2018-01-19 stsp *offset = o;
641 6ccb713b 2018-01-19 stsp return NULL;
644 6ccb713b 2018-01-19 stsp static const struct got_error *
645 d7464085 2018-07-09 stsp parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
646 d7464085 2018-07-09 stsp off_t offset, int tslen)
648 96f5e8b3 2018-01-23 stsp const struct got_error *err;
649 b107e67f 2018-01-19 stsp int64_t negoffset;
650 b107e67f 2018-01-19 stsp size_t negofflen;
654 d7464085 2018-07-09 stsp err = parse_negative_offset(&negoffset, &negofflen, pack,
655 d7464085 2018-07-09 stsp offset + tslen);
657 b107e67f 2018-01-19 stsp return err;
659 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
660 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
661 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
662 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
664 d7464085 2018-07-09 stsp *len = negofflen;
665 96f5e8b3 2018-01-23 stsp return NULL;
668 0e22967e 2018-02-11 stsp static const struct got_error *
669 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
670 c8ecd499 2018-09-09 stsp struct got_pack *, off_t, size_t, int, size_t, unsigned int);
672 96f5e8b3 2018-01-23 stsp static const struct got_error *
673 c336f889 2018-07-23 stsp add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
674 c336f889 2018-07-23 stsp int delta_type, size_t delta_size, size_t delta_data_offset,
675 c336f889 2018-07-23 stsp uint8_t *delta_buf, size_t delta_len)
677 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
679 c336f889 2018-07-23 stsp delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
680 c336f889 2018-07-23 stsp delta_data_offset, delta_buf,
681 a53d2f13 2018-03-17 stsp delta_len);
682 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
683 0a585a0d 2018-03-17 stsp return got_error_from_errno();
684 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
685 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
686 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
687 bdd2fbb3 2018-02-11 stsp return NULL;
690 bdd2fbb3 2018-02-11 stsp static const struct got_error *
691 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
692 c8ecd499 2018-09-09 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
693 c8ecd499 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
696 a3500804 2018-01-23 stsp const struct got_error *err;
697 c3703302 2018-01-23 stsp off_t base_offset;
698 c3703302 2018-01-23 stsp uint8_t base_type;
699 c3703302 2018-01-23 stsp uint64_t base_size;
700 c3703302 2018-01-23 stsp size_t base_tslen;
701 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
702 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
703 d7464085 2018-07-09 stsp size_t delta_len, consumed;
705 d7464085 2018-07-09 stsp err = parse_offset_delta(&base_offset, &consumed, pack,
706 d7464085 2018-07-09 stsp delta_offset, tslen);
708 a3500804 2018-01-23 stsp return err;
710 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + consumed;
711 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
712 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
714 d7464085 2018-07-09 stsp if (pack->map == NULL) {
715 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
716 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
717 d7464085 2018-07-09 stsp return got_error_from_errno();
720 d7464085 2018-07-09 stsp if (pack->map) {
721 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
722 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
723 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
725 d7464085 2018-07-09 stsp return err;
728 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
730 d7464085 2018-07-09 stsp return err;
733 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
734 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
738 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
739 ab3ad429 2018-07-23 stsp if (base_offset >= pack->filesize) {
740 ab3ad429 2018-07-23 stsp err = got_error(GOT_ERR_PACK_OFFSET);
744 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
745 d7464085 2018-07-09 stsp pack, base_offset);
749 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(deltas, packidx, pack, base_offset,
750 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
753 ab3ad429 2018-07-23 stsp free(delta_buf);
754 ab3ad429 2018-07-23 stsp return err;
757 c3703302 2018-01-23 stsp static const struct got_error *
758 c8ecd499 2018-09-09 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
759 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
760 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
762 6b9c9673 2018-01-23 stsp const struct got_error *err;
763 6b9c9673 2018-01-23 stsp struct got_object_id id;
765 6b9c9673 2018-01-23 stsp off_t base_offset;
766 6b9c9673 2018-01-23 stsp uint8_t base_type;
767 6b9c9673 2018-01-23 stsp uint64_t base_size;
768 6b9c9673 2018-01-23 stsp size_t base_tslen;
769 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
770 3efa19e7 2018-07-13 stsp uint8_t *delta_buf = NULL;
771 a53d2f13 2018-03-17 stsp size_t delta_len;
773 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
774 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
775 45202a8f 2018-07-11 stsp delta_data_offset = delta_offset + tslen;
776 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
777 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
779 d7464085 2018-07-09 stsp if (pack->map == NULL) {
780 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
781 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
782 d7464085 2018-07-09 stsp return got_error_from_errno();
786 d7464085 2018-07-09 stsp if (pack->map) {
787 45202a8f 2018-07-11 stsp size_t mapoff = (size_t)delta_data_offset;
788 d7464085 2018-07-09 stsp memcpy(&id, pack->map + mapoff, sizeof(id));
789 d7464085 2018-07-09 stsp mapoff += sizeof(id);
790 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
791 45202a8f 2018-07-11 stsp mapoff, pack->filesize - mapoff);
795 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &id, sizeof(id));
797 d7464085 2018-07-09 stsp return got_error_from_errno();
798 d7464085 2018-07-09 stsp if (n != sizeof(id))
799 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
800 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
805 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
806 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
810 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
811 1510f469 2018-09-09 stsp idx = got_packidx_get_object_idx(packidx, &id);
812 3efa19e7 2018-07-13 stsp if (idx == -1) {
813 3efa19e7 2018-07-13 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
817 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
818 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
819 3efa19e7 2018-07-13 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
823 b0e2201a 2018-06-22 stsp if (base_offset >= pack->filesize) {
824 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
828 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
829 d7464085 2018-07-09 stsp pack, base_offset);
833 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(deltas, packidx, pack, base_offset,
834 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
837 3efa19e7 2018-07-13 stsp free(delta_buf);
838 6b9c9673 2018-01-23 stsp return err;
841 6b9c9673 2018-01-23 stsp static const struct got_error *
842 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
843 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
844 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
846 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
848 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
849 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
851 c3703302 2018-01-23 stsp switch (delta_type) {
852 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
853 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
854 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
855 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
856 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
857 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type,
858 c336f889 2018-07-23 stsp delta_size, 0, NULL, 0);
860 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
861 c8ecd499 2018-09-09 stsp err = resolve_offset_delta(deltas, packidx, pack,
862 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
864 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
865 c8ecd499 2018-09-09 stsp err = resolve_ref_delta(deltas, packidx, pack,
866 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
869 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
872 96f5e8b3 2018-01-23 stsp return err;
875 96f5e8b3 2018-01-23 stsp static const struct got_error *
876 a98c62b1 2018-09-09 stsp open_delta_object(struct got_object **obj, struct got_packidx *packidx,
877 a98c62b1 2018-09-09 stsp struct got_pack *pack, struct got_object_id *id, off_t offset,
878 c59b3346 2018-09-11 stsp size_t tslen, int delta_type, size_t delta_size, int idx)
880 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
881 96f5e8b3 2018-01-23 stsp int resolved_type;
883 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
884 b107e67f 2018-01-19 stsp if (*obj == NULL)
885 0a585a0d 2018-03-17 stsp return got_error_from_errno();
887 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
888 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
889 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
890 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
891 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
893 b0e2201a 2018-06-22 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
894 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
895 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
898 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
899 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
901 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
902 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
904 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
905 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
909 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
912 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
915 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
916 96f5e8b3 2018-01-23 stsp *obj = NULL;
918 96f5e8b3 2018-01-23 stsp return err;
921 2090a03d 2018-09-09 stsp const struct got_error *
922 2090a03d 2018-09-09 stsp got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
923 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
925 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
926 a1fd68d8 2018-01-12 stsp off_t offset;
927 6c00b545 2018-01-17 stsp uint8_t type;
928 6c00b545 2018-01-17 stsp uint64_t size;
929 3ee5fc21 2018-01-17 stsp size_t tslen;
931 6c00b545 2018-01-17 stsp *obj = NULL;
933 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
934 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
935 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
937 d7464085 2018-07-09 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
939 35c73765 2018-09-09 stsp return err;
941 6c00b545 2018-01-17 stsp switch (type) {
942 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
943 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
944 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
945 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
946 35c73765 2018-09-09 stsp err = open_plain_object(obj, pack->path_packfile, id, type,
947 c59b3346 2018-09-11 stsp offset + tslen, size, idx);
949 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
950 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
951 a98c62b1 2018-09-09 stsp err = open_delta_object(obj, packidx, pack, id, offset,
952 c59b3346 2018-09-11 stsp tslen, type, size, idx);
955 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
959 3ee5fc21 2018-01-17 stsp return err;
962 efd2a263 2018-01-19 stsp static const struct got_error *
963 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
965 22484865 2018-03-13 stsp struct got_delta *delta;
966 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
968 22484865 2018-03-13 stsp *max_size = 0;
969 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
970 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
971 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
972 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
973 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
974 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
975 22484865 2018-03-13 stsp const struct got_error *err;
976 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
977 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
979 22484865 2018-03-13 stsp return err;
981 22484865 2018-03-13 stsp base_size = delta->size;
982 22484865 2018-03-13 stsp if (base_size > *max_size)
983 22484865 2018-03-13 stsp *max_size = base_size;
984 22484865 2018-03-13 stsp if (result_size > *max_size)
985 22484865 2018-03-13 stsp *max_size = result_size;
988 9feb4ff2 2018-03-14 stsp return NULL;
991 ac544f8c 2019-01-13 stsp const struct got_error *
992 85a703fa 2019-01-13 stsp got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj)
994 85a703fa 2019-01-13 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
995 85a703fa 2019-01-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
997 ac544f8c 2019-01-13 stsp return get_delta_chain_max_size(size, &obj->deltas);
1000 22484865 2018-03-13 stsp static const struct got_error *
1001 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1002 3840f4c9 2018-09-12 stsp struct got_pack *pack, FILE *outfile, FILE *base_file, FILE *accum_file)
1004 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1005 6691714a 2018-01-23 stsp struct got_delta *delta;
1006 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1007 34fca9c3 2018-11-11 stsp size_t base_bufsz = 0, accum_size = 0;
1008 22484865 2018-03-13 stsp uint64_t max_size;
1009 6691714a 2018-01-23 stsp int n = 0;
1011 b29656e2 2018-03-16 stsp *result_size = 0;
1013 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1014 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1016 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1017 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1019 22484865 2018-03-13 stsp return err;
1020 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1021 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1022 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1023 8628c62d 2018-03-15 stsp return got_error_from_errno();
1024 3840f4c9 2018-09-12 stsp base_file = NULL;
1025 3840f4c9 2018-09-12 stsp accum_file = NULL;
1028 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1029 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1030 a6b158cc 2018-02-11 stsp if (n == 0) {
1031 34fca9c3 2018-11-11 stsp size_t mapoff;
1032 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1034 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1035 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1036 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1037 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1038 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1039 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1040 72eb3431 2018-04-01 stsp goto done;
1043 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1044 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1045 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1046 0c048b15 2018-04-27 stsp goto done;
1048 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1049 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1051 d7464085 2018-07-09 stsp err = got_error_from_errno();
1052 d7464085 2018-07-09 stsp goto done;
1055 d7464085 2018-07-09 stsp if (base_file) {
1056 d7464085 2018-07-09 stsp if (pack->map) {
1057 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1058 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1059 34fca9c3 2018-11-11 stsp &base_bufsz, pack->map, mapoff,
1060 d7464085 2018-07-09 stsp pack->filesize - mapoff, base_file);
1062 34fca9c3 2018-11-11 stsp err = got_inflate_to_file_fd(
1063 34fca9c3 2018-11-11 stsp &base_bufsz, pack->fd, base_file);
1065 d7464085 2018-07-09 stsp if (pack->map) {
1066 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1067 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1068 34fca9c3 2018-11-11 stsp &base_bufsz, pack->map, mapoff,
1069 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1071 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1072 34fca9c3 2018-11-11 stsp &base_bufsz, pack->fd);
1075 a6b158cc 2018-02-11 stsp goto done;
1077 8628c62d 2018-03-15 stsp if (base_file)
1078 8628c62d 2018-03-15 stsp rewind(base_file);
1082 8628c62d 2018-03-15 stsp if (base_buf) {
1083 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1084 34fca9c3 2018-11-11 stsp delta->delta_buf, delta->delta_len, accum_buf,
1085 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1088 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1089 a53d2f13 2018-03-17 stsp delta->delta_len,
1090 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1091 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1092 b29656e2 2018-03-16 stsp &accum_size);
1095 6691714a 2018-01-23 stsp goto done;
1097 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1098 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1099 8628c62d 2018-03-15 stsp if (base_buf) {
1100 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1102 34fca9c3 2018-11-11 stsp * Base buffer switches roles with accumulation
1103 34fca9c3 2018-11-11 stsp * buffer. Ensure it can hold the largest
1104 34fca9c3 2018-11-11 stsp * result in the delta chain. The initial
1105 34fca9c3 2018-11-11 stsp * allocation might have been smaller.
1107 34fca9c3 2018-11-11 stsp if (base_bufsz < max_size) {
1108 34fca9c3 2018-11-11 stsp uint8_t *p;
1109 34fca9c3 2018-11-11 stsp p = reallocarray(base_buf, 1, max_size);
1110 34fca9c3 2018-11-11 stsp if (p == NULL) {
1111 34fca9c3 2018-11-11 stsp err = got_error_from_errno();
1112 34fca9c3 2018-11-11 stsp goto done;
1114 34fca9c3 2018-11-11 stsp base_buf = p;
1115 34fca9c3 2018-11-11 stsp base_bufsz = max_size;
1117 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1118 8628c62d 2018-03-15 stsp base_buf = tmp;
1120 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1121 8628c62d 2018-03-15 stsp accum_file = base_file;
1122 8628c62d 2018-03-15 stsp base_file = tmp;
1123 8628c62d 2018-03-15 stsp rewind(base_file);
1124 8628c62d 2018-03-15 stsp rewind(accum_file);
1130 8628c62d 2018-03-15 stsp free(base_buf);
1131 8628c62d 2018-03-15 stsp if (accum_buf) {
1132 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1133 8628c62d 2018-03-15 stsp free(accum_buf);
1134 8628c62d 2018-03-15 stsp if (len != accum_size)
1135 673702af 2018-07-23 stsp err = got_ferror(outfile, GOT_ERR_IO);
1137 fb43ecf1 2019-02-11 stsp if (base_file && fclose(base_file) != 0 && err == NULL)
1138 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
1139 fb43ecf1 2019-02-11 stsp if (accum_file && fclose(accum_file) != 0 && err == NULL)
1140 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
1141 6691714a 2018-01-23 stsp rewind(outfile);
1142 b29656e2 2018-03-16 stsp if (err == NULL)
1143 b29656e2 2018-03-16 stsp *result_size = accum_size;
1144 e0ab43e7 2018-03-16 stsp return err;
1147 e0ab43e7 2018-03-16 stsp static const struct got_error *
1148 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1149 48095039 2018-09-09 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1151 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1152 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1153 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1154 34fca9c3 2018-11-11 stsp size_t base_bufsz = 0, accum_size = 0;
1155 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1156 e0ab43e7 2018-03-16 stsp int n = 0;
1158 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1159 e0ab43e7 2018-03-16 stsp *outlen = 0;
1161 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1162 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1164 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1166 e0ab43e7 2018-03-16 stsp return err;
1167 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1168 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1169 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1171 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1172 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1173 e0ab43e7 2018-03-16 stsp if (n == 0) {
1174 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1176 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1177 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1178 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1179 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1180 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1181 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1182 72eb3431 2018-04-01 stsp goto done;
1185 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1186 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1187 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1188 0c048b15 2018-04-27 stsp goto done;
1190 d7464085 2018-07-09 stsp if (pack->map) {
1191 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
1192 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1193 34fca9c3 2018-11-11 stsp &base_bufsz, pack->map, mapoff,
1194 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1196 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1198 d7464085 2018-07-09 stsp err = got_error_from_errno();
1199 d7464085 2018-07-09 stsp goto done;
1201 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1202 34fca9c3 2018-11-11 stsp &base_bufsz, pack->fd);
1205 d7464085 2018-07-09 stsp goto done;
1210 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1211 34fca9c3 2018-11-11 stsp delta->delta_buf, delta->delta_len, accum_buf,
1212 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1215 e0ab43e7 2018-03-16 stsp goto done;
1217 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1218 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1219 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1221 34fca9c3 2018-11-11 stsp * Base buffer switches roles with accumulation buffer.
1222 34fca9c3 2018-11-11 stsp * Ensure it can hold the largest result in the delta
1223 34fca9c3 2018-11-11 stsp * chain. Initial allocation might have been smaller.
1225 34fca9c3 2018-11-11 stsp if (base_bufsz < max_size) {
1226 34fca9c3 2018-11-11 stsp uint8_t *p;
1227 34fca9c3 2018-11-11 stsp p = reallocarray(base_buf, 1, max_size);
1228 34fca9c3 2018-11-11 stsp if (p == NULL) {
1229 34fca9c3 2018-11-11 stsp err = got_error_from_errno();
1230 34fca9c3 2018-11-11 stsp goto done;
1232 34fca9c3 2018-11-11 stsp base_buf = p;
1233 34fca9c3 2018-11-11 stsp base_bufsz = max_size;
1235 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1236 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1241 e0ab43e7 2018-03-16 stsp free(base_buf);
1242 e0ab43e7 2018-03-16 stsp if (err) {
1243 e0ab43e7 2018-03-16 stsp free(accum_buf);
1244 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1245 e0ab43e7 2018-03-16 stsp *outlen = 0;
1247 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1248 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1250 efd2a263 2018-01-19 stsp return err;
1253 3ee5fc21 2018-01-17 stsp const struct got_error *
1254 24140570 2018-09-09 stsp got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1255 3840f4c9 2018-09-12 stsp FILE *outfile, FILE *base_file, FILE *accum_file)
1257 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1259 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1260 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1262 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1263 24140570 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1264 24140570 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1266 d7464085 2018-07-09 stsp if (pack->map) {
1267 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1268 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(&obj->size, pack->map,
1269 24140570 2018-09-09 stsp mapoff, pack->filesize - mapoff, outfile);
1271 24140570 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1272 24140570 2018-09-09 stsp return got_error_from_errno();
1273 24140570 2018-09-09 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd,
1277 2ce68b2f 2018-09-09 stsp err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1278 3840f4c9 2018-09-12 stsp outfile, base_file, accum_file);
1280 a1fd68d8 2018-01-12 stsp return err;
1283 e0ab43e7 2018-03-16 stsp const struct got_error *
1284 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1285 7e212e3d 2018-09-09 stsp struct got_object *obj, struct got_pack *pack)
1287 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1289 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1290 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1292 48095039 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1293 7e212e3d 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1294 7e212e3d 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1295 d7464085 2018-07-09 stsp if (pack->map) {
1296 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1297 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(buf, len, pack->map,
1298 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
1300 7e212e3d 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1301 7e212e3d 2018-09-09 stsp return got_error_from_errno();
1302 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(buf, len, pack->fd);
1305 48095039 2018-09-09 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);
1307 e0ab43e7 2018-03-16 stsp return err;