2 0a0a3048 2018-01-10 stsp * Copyright (c) 2018 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 57b35b75 2018-06-22 stsp #include <sys/mman.h>
22 a1fd68d8 2018-01-12 stsp #include <dirent.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>
34 0a0a3048 2018-01-10 stsp #include "got_error.h"
35 a1fd68d8 2018-01-12 stsp #include "got_object.h"
36 a1fd68d8 2018-01-12 stsp #include "got_repository.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_pack.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
43 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
47 79b11c62 2018-03-09 stsp #ifndef nitems
48 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
51 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
52 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
53 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
54 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
55 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
56 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
57 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
58 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
59 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
62 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 0a0a3048 2018-01-10 stsp static const struct got_error *
66 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
70 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
71 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
72 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
75 0a0a3048 2018-01-10 stsp return NULL;
78 24541888 2018-01-10 stsp static const struct got_error *
79 1c7e24f1 2018-04-02 stsp get_packfile_size(size_t *size, const char *path)
81 0a0a3048 2018-01-10 stsp struct stat sb;
86 1c7e24f1 2018-04-02 stsp dot = strrchr(path, '.');
87 0a0a3048 2018-01-10 stsp if (dot == NULL)
88 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
90 1c7e24f1 2018-04-02 stsp /* Path must point to a pack index or to a pack file. */
91 72fb0363 2018-06-04 stsp if (strcmp(dot, GOT_PACKIDX_SUFFIX) == 0) {
92 97128b57 2018-04-02 stsp const struct got_error *err = NULL;
93 1c7e24f1 2018-04-02 stsp char *path_pack;
94 1c7e24f1 2018-04-02 stsp char base_path[PATH_MAX];
96 1c7e24f1 2018-04-02 stsp /* Convert pack index path to pack file path. */
97 1c7e24f1 2018-04-02 stsp if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
98 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_NO_SPACE);
99 1c7e24f1 2018-04-02 stsp dot = strrchr(base_path, '.');
100 1c7e24f1 2018-04-02 stsp if (dot == NULL)
101 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
102 1c7e24f1 2018-04-02 stsp *dot = '\0';
103 1c7e24f1 2018-04-02 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
104 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
106 97128b57 2018-04-02 stsp if (stat(path_pack, &sb) != 0)
107 97128b57 2018-04-02 stsp err = got_error_from_errno();
108 0a0a3048 2018-01-10 stsp free(path_pack);
110 97128b57 2018-04-02 stsp return err;
111 56cca8e5 2018-06-04 stsp } else if (strcmp(dot, GOT_PACKFILE_SUFFIX) == 0) {
112 1c7e24f1 2018-04-02 stsp if (stat(path, &sb) != 0)
113 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
115 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
117 0a0a3048 2018-01-10 stsp *size = sb.st_size;
121 0a0a3048 2018-01-10 stsp const struct got_error *
122 817c5a18 2018-09-09 stsp got_packidx_init_hdr(struct got_packidx *p, int verify)
124 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
125 817c5a18 2018-09-09 stsp struct got_packidx_v2_hdr *h;
126 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
127 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
128 817c5a18 2018-09-09 stsp size_t nobj, len_fanout, len_ids, offset, remain;
131 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
133 57b35b75 2018-06-22 stsp h = &p->hdr;
134 57b35b75 2018-06-22 stsp offset = 0;
135 57b35b75 2018-06-22 stsp remain = p->len;
137 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->magic)) {
138 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
141 fc79a48d 2018-07-09 stsp if (p->map)
142 fc79a48d 2018-07-09 stsp h->magic = (uint32_t *)(p->map + offset);
144 fc79a48d 2018-07-09 stsp h->magic = malloc(sizeof(*h->magic));
145 fc79a48d 2018-07-09 stsp if (h->magic == NULL) {
146 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
149 fc79a48d 2018-07-09 stsp n = read(p->fd, h->magic, sizeof(*h->magic));
150 fc79a48d 2018-07-09 stsp if (n != sizeof(*h->magic)) {
151 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
155 57b35b75 2018-06-22 stsp if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
156 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
159 57b35b75 2018-06-22 stsp offset += sizeof(*h->magic);
160 57b35b75 2018-06-22 stsp remain -= sizeof(*h->magic);
162 0cb74cf4 2018-07-08 stsp if (verify)
163 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
165 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->version)) {
166 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
169 fc79a48d 2018-07-09 stsp if (p->map)
170 fc79a48d 2018-07-09 stsp h->version = (uint32_t *)(p->map + offset);
172 fc79a48d 2018-07-09 stsp h->version = malloc(sizeof(*h->version));
173 fc79a48d 2018-07-09 stsp if (h->version == NULL) {
174 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
177 fc79a48d 2018-07-09 stsp n = read(p->fd, h->version, sizeof(*h->version));
178 fc79a48d 2018-07-09 stsp if (n != sizeof(*h->version)) {
179 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
183 57b35b75 2018-06-22 stsp if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
184 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
187 57b35b75 2018-06-22 stsp offset += sizeof(*h->version);
188 57b35b75 2018-06-22 stsp remain -= sizeof(*h->version);
190 0cb74cf4 2018-07-08 stsp if (verify)
191 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
193 57b35b75 2018-06-22 stsp len_fanout =
194 57b35b75 2018-06-22 stsp sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
195 57b35b75 2018-06-22 stsp if (remain < len_fanout) {
196 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
199 fc79a48d 2018-07-09 stsp if (p->map)
200 fc79a48d 2018-07-09 stsp h->fanout_table = (uint32_t *)(p->map + offset);
202 fc79a48d 2018-07-09 stsp h->fanout_table = malloc(len_fanout);
203 fc79a48d 2018-07-09 stsp if (h->fanout_table == NULL) {
204 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
207 fc79a48d 2018-07-09 stsp n = read(p->fd, h->fanout_table, len_fanout);
208 fc79a48d 2018-07-09 stsp if (n != len_fanout) {
209 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
213 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
216 0cb74cf4 2018-07-08 stsp if (verify)
217 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
218 57b35b75 2018-06-22 stsp offset += len_fanout;
219 57b35b75 2018-06-22 stsp remain -= len_fanout;
221 c8262310 2018-06-04 stsp nobj = betoh32(h->fanout_table[0xff]);
222 57b35b75 2018-06-22 stsp len_ids = nobj * sizeof(*h->sorted_ids);
223 57b35b75 2018-06-22 stsp if (len_ids <= nobj || len_ids > remain) {
224 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
227 fc79a48d 2018-07-09 stsp if (p->map)
228 fc79a48d 2018-07-09 stsp h->sorted_ids =
229 fc79a48d 2018-07-09 stsp (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
231 fc79a48d 2018-07-09 stsp h->sorted_ids = malloc(len_ids);
232 fc79a48d 2018-07-09 stsp if (h->sorted_ids == NULL) {
233 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
236 fc79a48d 2018-07-09 stsp n = read(p->fd, h->sorted_ids, len_ids);
237 fc79a48d 2018-07-09 stsp if (n != len_ids) {
238 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
242 0cb74cf4 2018-07-08 stsp if (verify)
243 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
244 57b35b75 2018-06-22 stsp offset += len_ids;
245 57b35b75 2018-06-22 stsp remain -= len_ids;
247 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->crc32)) {
248 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
251 fc79a48d 2018-07-09 stsp if (p->map)
252 fc79a48d 2018-07-09 stsp h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
254 fc79a48d 2018-07-09 stsp h->crc32 = malloc(nobj * sizeof(*h->crc32));
255 fc79a48d 2018-07-09 stsp if (h->crc32 == NULL) {
256 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
259 fc79a48d 2018-07-09 stsp n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
260 fc79a48d 2018-07-09 stsp 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));
283 fc79a48d 2018-07-09 stsp if (n != nobj * sizeof(*h->offsets)) {
284 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
288 0cb74cf4 2018-07-08 stsp if (verify)
289 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->offsets,
290 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->offsets));
291 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->offsets);
292 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->offsets);
294 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
295 57b35b75 2018-06-22 stsp if (p->len <= 0x80000000)
296 0a0a3048 2018-01-10 stsp goto checksum;
298 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->large_offsets)) {
299 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
302 fc79a48d 2018-07-09 stsp if (p->map)
303 fc79a48d 2018-07-09 stsp h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
305 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->large_offsets));
306 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
307 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
310 fc79a48d 2018-07-09 stsp n = read(p->fd, h->large_offsets,
311 fc79a48d 2018-07-09 stsp nobj * sizeof(*h->large_offsets));
312 fc79a48d 2018-07-09 stsp if (n != nobj * sizeof(*h->large_offsets)) {
313 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
317 0cb74cf4 2018-07-08 stsp if (verify)
318 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
319 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->large_offsets));
320 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->large_offsets);
321 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->large_offsets);
324 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->trailer)) {
325 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
328 fc79a48d 2018-07-09 stsp if (p->map)
329 fc79a48d 2018-07-09 stsp h->trailer =
330 fc79a48d 2018-07-09 stsp (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
332 fc79a48d 2018-07-09 stsp h->trailer = malloc(sizeof(*h->trailer));
333 fc79a48d 2018-07-09 stsp if (h->trailer == NULL) {
334 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
337 fc79a48d 2018-07-09 stsp n = read(p->fd, h->trailer, sizeof(*h->trailer));
338 fc79a48d 2018-07-09 stsp if (n != sizeof(*h->trailer)) {
339 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
343 0cb74cf4 2018-07-08 stsp if (verify) {
344 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
345 0cb74cf4 2018-07-08 stsp SHA1Final(sha1, &ctx);
346 0cb74cf4 2018-07-08 stsp if (memcmp(h->trailer->packidx_sha1, sha1,
347 0cb74cf4 2018-07-08 stsp SHA1_DIGEST_LENGTH) != 0)
348 0cb74cf4 2018-07-08 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
351 817c5a18 2018-09-09 stsp return err;
354 817c5a18 2018-09-09 stsp const struct got_error *
355 817c5a18 2018-09-09 stsp got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
357 817c5a18 2018-09-09 stsp const struct got_error *err = NULL;
358 817c5a18 2018-09-09 stsp struct got_packidx *p;
360 817c5a18 2018-09-09 stsp *packidx = NULL;
362 817c5a18 2018-09-09 stsp p = calloc(1, sizeof(*p));
363 817c5a18 2018-09-09 stsp if (p == NULL)
364 817c5a18 2018-09-09 stsp return got_error_from_errno();
366 817c5a18 2018-09-09 stsp p->fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
367 817c5a18 2018-09-09 stsp if (p->fd == -1)
368 817c5a18 2018-09-09 stsp return got_error_from_errno();
370 817c5a18 2018-09-09 stsp err = get_packfile_size(&p->len, path);
372 817c5a18 2018-09-09 stsp close(p->fd);
374 817c5a18 2018-09-09 stsp return err;
376 817c5a18 2018-09-09 stsp if (p->len < sizeof(p->hdr)) {
377 817c5a18 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
378 817c5a18 2018-09-09 stsp close(p->fd);
380 817c5a18 2018-09-09 stsp return err;
383 817c5a18 2018-09-09 stsp p->path_packidx = strdup(path);
384 817c5a18 2018-09-09 stsp if (p->path_packidx == NULL) {
385 817c5a18 2018-09-09 stsp err = got_error_from_errno();
389 817c5a18 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
390 817c5a18 2018-09-09 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
391 817c5a18 2018-09-09 stsp if (p->map == MAP_FAILED)
392 817c5a18 2018-09-09 stsp p->map = NULL; /* fall back to read(2) */
395 817c5a18 2018-09-09 stsp err = got_packidx_init_hdr(p, verify);
398 0a0a3048 2018-01-10 stsp got_packidx_close(p);
400 0a0a3048 2018-01-10 stsp *packidx = p;
402 0a0a3048 2018-01-10 stsp return err;
405 57b35b75 2018-06-22 stsp const struct got_error *
406 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
408 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
410 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
411 fc79a48d 2018-07-09 stsp if (packidx->map) {
412 57b35b75 2018-06-22 stsp if (munmap(packidx->map, packidx->len) == -1)
413 57b35b75 2018-06-22 stsp err = got_error_from_errno();
415 fc79a48d 2018-07-09 stsp free(packidx->hdr.magic);
416 fc79a48d 2018-07-09 stsp free(packidx->hdr.version);
417 fc79a48d 2018-07-09 stsp free(packidx->hdr.fanout_table);
418 fc79a48d 2018-07-09 stsp free(packidx->hdr.sorted_ids);
419 fc79a48d 2018-07-09 stsp free(packidx->hdr.crc32);
420 fc79a48d 2018-07-09 stsp free(packidx->hdr.offsets);
421 fc79a48d 2018-07-09 stsp free(packidx->hdr.large_offsets);
422 fc79a48d 2018-07-09 stsp free(packidx->hdr.trailer);
424 57b35b75 2018-06-22 stsp close(packidx->fd);
425 0a0a3048 2018-01-10 stsp free(packidx);
427 57b35b75 2018-06-22 stsp return err;
431 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
433 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
436 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
439 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
440 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
446 a1fd68d8 2018-01-12 stsp static off_t
447 6fd11751 2018-06-04 stsp get_object_offset(struct got_packidx *packidx, int idx)
449 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
450 6fd11751 2018-06-04 stsp uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
451 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
452 a1fd68d8 2018-01-12 stsp uint64_t loffset;
453 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
454 6fd11751 2018-06-04 stsp if (idx < 0 || idx > totobj ||
455 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
457 6fd11751 2018-06-04 stsp loffset = betoh64(packidx->hdr.large_offsets[idx]);
458 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
460 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
464 3413160a 2018-09-09 stsp get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
466 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
467 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
468 40aeb19c 2018-06-22 stsp int left = 0, right = totobj - 1;
470 a1fd68d8 2018-01-12 stsp if (id0 > 0)
471 40aeb19c 2018-06-22 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
473 40aeb19c 2018-06-22 stsp while (left <= right) {
474 57b35b75 2018-06-22 stsp struct got_packidx_object_id *oid;
475 40aeb19c 2018-06-22 stsp int i, cmp;
477 40aeb19c 2018-06-22 stsp i = ((left + right) / 2);
478 40aeb19c 2018-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
479 57b35b75 2018-06-22 stsp cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
480 16dcbf91 2018-04-01 stsp if (cmp == 0)
482 40aeb19c 2018-06-22 stsp else if (cmp > 0)
483 40aeb19c 2018-06-22 stsp left = i + 1;
484 40aeb19c 2018-06-22 stsp else if (cmp < 0)
485 40aeb19c 2018-06-22 stsp right = i - 1;
491 57b35b75 2018-06-22 stsp static const struct got_error *
492 6fd11751 2018-06-04 stsp cache_packidx(struct got_packidx *packidx, struct got_repository *repo)
494 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
497 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
498 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
502 65cf1e80 2018-03-16 stsp if (i == nitems(repo->packidx_cache)) {
503 57b35b75 2018-06-22 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
505 57b35b75 2018-06-22 stsp return err;
506 65cf1e80 2018-03-16 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
507 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache) -
508 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache[0]));
512 49c99f91 2018-06-22 stsp repo->packidx_cache[i] = packidx;
513 57b35b75 2018-06-22 stsp return NULL;
516 6b9c9673 2018-01-23 stsp static const struct got_error *
517 6fd11751 2018-06-04 stsp search_packidx(struct got_packidx **packidx, int *idx,
518 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
520 6b9c9673 2018-01-23 stsp const struct got_error *err;
521 6b9c9673 2018-01-23 stsp char *path_packdir;
522 6b9c9673 2018-01-23 stsp DIR *packdir;
523 6b9c9673 2018-01-23 stsp struct dirent *dent;
524 6b9c9673 2018-01-23 stsp char *path_packidx;
527 65cf1e80 2018-03-16 stsp /* Search pack index cache. */
528 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
529 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
531 3413160a 2018-09-09 stsp *idx = get_object_idx(repo->packidx_cache[i], id);
532 79b11c62 2018-03-09 stsp if (*idx != -1) {
533 6bb255dc 2018-03-17 stsp *packidx = repo->packidx_cache[i];
534 79b11c62 2018-03-09 stsp return NULL;
537 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
539 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
540 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
541 0a585a0d 2018-03-17 stsp return got_error_from_errno();
543 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
544 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
545 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
549 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
550 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
553 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
554 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
555 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
559 0cb74cf4 2018-07-08 stsp err = got_packidx_open(packidx, path_packidx, 0);
560 6b9c9673 2018-01-23 stsp free(path_packidx);
564 3413160a 2018-09-09 stsp *idx = get_object_idx(*packidx, id);
565 6b9c9673 2018-01-23 stsp if (*idx != -1) {
566 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
567 57b35b75 2018-06-22 stsp err = cache_packidx(*packidx, repo);
571 57b35b75 2018-06-22 stsp err = got_packidx_close(*packidx);
572 6b9c9673 2018-01-23 stsp *packidx = NULL;
577 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
579 6b9c9673 2018-01-23 stsp free(path_packdir);
580 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
581 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
582 6b9c9673 2018-01-23 stsp return err;
585 c7fe698a 2018-01-23 stsp static const struct got_error *
586 4a9c75d9 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
588 6fd11751 2018-06-04 stsp size_t size;
590 6fd11751 2018-06-04 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
591 6fd11751 2018-06-04 stsp size = strlen(packidx->path_packidx) + 2;
592 6fd11751 2018-06-04 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
593 6fd11751 2018-06-04 stsp return got_error(GOT_ERR_BAD_PATH);
595 6fd11751 2018-06-04 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
596 6fd11751 2018-06-04 stsp if (*path_packfile == NULL)
597 0a585a0d 2018-03-17 stsp return got_error_from_errno();
599 6fd11751 2018-06-04 stsp /* Copy up to and excluding ".idx". */
600 d475dd0d 2018-06-04 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
601 72fb0363 2018-06-04 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
602 d475dd0d 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
604 6fd11751 2018-06-04 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
605 6fd11751 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
607 65cf1e80 2018-03-16 stsp return NULL;
610 d7464085 2018-07-09 stsp static const struct got_error *
611 6fd11751 2018-06-04 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
613 65cf1e80 2018-03-16 stsp const struct got_error *err = NULL;
614 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
615 65cf1e80 2018-03-16 stsp struct got_packfile_hdr hdr;
618 8b2180d4 2018-04-26 stsp n = read(fd, &hdr, sizeof(hdr));
620 8b2180d4 2018-04-26 stsp return got_error_from_errno();
621 8b2180d4 2018-04-26 stsp if (n != sizeof(hdr))
622 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
624 65cf1e80 2018-03-16 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
625 65cf1e80 2018-03-16 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
626 65cf1e80 2018-03-16 stsp betoh32(hdr.nobjects) != totobj)
627 65cf1e80 2018-03-16 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
629 65cf1e80 2018-03-16 stsp return err;
632 7e656b93 2018-03-17 stsp static const struct got_error *
633 4fb0d740 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
635 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
637 8b2180d4 2018-04-26 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
638 8b2180d4 2018-04-26 stsp if (*fd == -1)
639 8b2180d4 2018-04-26 stsp return got_error_from_errno();
641 7e656b93 2018-03-17 stsp if (packidx) {
642 8b2180d4 2018-04-26 stsp err = read_packfile_hdr(*fd, packidx);
644 8b2180d4 2018-04-26 stsp close(*fd);
648 7e656b93 2018-03-17 stsp return err;
651 d7464085 2018-07-09 stsp const struct got_error *
652 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
654 d7464085 2018-07-09 stsp const struct got_error *err = NULL;
656 e8f89a81 2018-07-13 stsp if (pack->map && munmap(pack->map, pack->filesize) == -1)
657 d7464085 2018-07-09 stsp err = got_error_from_errno();
658 8b2180d4 2018-04-26 stsp close(pack->fd);
659 8b2180d4 2018-04-26 stsp pack->fd = -1;
660 4589e373 2018-03-17 stsp free(pack->path_packfile);
661 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
662 4589e373 2018-03-17 stsp pack->filesize = 0;
664 d7464085 2018-07-09 stsp return err;
667 7e656b93 2018-03-17 stsp static const struct got_error *
668 7e656b93 2018-03-17 stsp cache_pack(struct got_pack **packp, const char *path_packfile,
669 6fd11751 2018-06-04 stsp struct got_packidx *packidx, struct got_repository *repo)
671 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
672 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
676 7e656b93 2018-03-17 stsp *packp = NULL;
678 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
679 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
680 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
682 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
683 7e656b93 2018-03-17 stsp return NULL;
686 7e656b93 2018-03-17 stsp if (i == nitems(repo->packs) - 1) {
687 d7464085 2018-07-09 stsp err = got_pack_close(&repo->packs[i - 1]);
689 d7464085 2018-07-09 stsp return err;
690 7e656b93 2018-03-17 stsp memmove(&repo->packs[1], &repo->packs[0],
691 7e656b93 2018-03-17 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
695 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
697 7e656b93 2018-03-17 stsp pack->path_packfile = strdup(path_packfile);
698 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL) {
699 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
703 4fb0d740 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
707 7e656b93 2018-03-17 stsp err = get_packfile_size(&pack->filesize, path_packfile);
711 af9b7fee 2018-07-09 stsp #ifndef GOT_PACK_NO_MMAP
712 d7464085 2018-07-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
713 d7464085 2018-07-09 stsp pack->fd, 0);
714 57363308 2018-07-09 stsp if (pack->map == MAP_FAILED)
715 57363308 2018-07-09 stsp pack->map = NULL; /* fall back to read(2) */
719 f5feadcc 2018-06-04 stsp if (pack) {
720 7e656b93 2018-03-17 stsp free(pack->path_packfile);
721 f5feadcc 2018-06-04 stsp memset(pack, 0, sizeof(*pack));
723 7e656b93 2018-03-17 stsp } else if (packp)
724 7e656b93 2018-03-17 stsp *packp = pack;
725 c7fe698a 2018-01-23 stsp return err;
728 7e656b93 2018-03-17 stsp struct got_pack *
729 7e656b93 2018-03-17 stsp get_cached_pack(const char *path_packfile, struct got_repository *repo)
731 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
734 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
735 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
736 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
738 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
739 7e656b93 2018-03-17 stsp return pack;
742 7e656b93 2018-03-17 stsp return NULL;
745 c7fe698a 2018-01-23 stsp static const struct got_error *
746 d7464085 2018-07-09 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
747 d7464085 2018-07-09 stsp struct got_pack *pack, off_t offset)
749 a487c1d0 2018-01-14 stsp uint8_t t = 0;
750 a487c1d0 2018-01-14 stsp uint64_t s = 0;
751 a487c1d0 2018-01-14 stsp uint8_t sizeN;
752 d7464085 2018-07-09 stsp size_t mapoff = 0;
757 d7464085 2018-07-09 stsp if (offset >= pack->filesize)
758 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
760 d7464085 2018-07-09 stsp if (pack->map) {
761 d7464085 2018-07-09 stsp mapoff = (size_t)offset;
763 d7464085 2018-07-09 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1)
764 d7464085 2018-07-09 stsp return got_error_from_errno();
768 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
770 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
772 d7464085 2018-07-09 stsp if (pack->map) {
773 d7464085 2018-07-09 stsp sizeN = *(pack->map + mapoff);
774 d7464085 2018-07-09 stsp mapoff += sizeof(sizeN);
776 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
778 d7464085 2018-07-09 stsp return got_error_from_errno();
779 d7464085 2018-07-09 stsp if (n != sizeof(sizeN))
780 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
782 d7464085 2018-07-09 stsp *len += sizeof(sizeN);
784 a1fd68d8 2018-01-12 stsp if (i == 0) {
785 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
786 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
787 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
789 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
790 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
793 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
797 a487c1d0 2018-01-14 stsp return NULL;
800 a1fd68d8 2018-01-12 stsp static const struct got_error *
801 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
802 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
804 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
805 6ccb713b 2018-01-19 stsp if (*obj == NULL)
806 0a585a0d 2018-03-17 stsp return got_error_from_errno();
808 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
809 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
810 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
811 6ccb713b 2018-01-19 stsp free(*obj);
812 6ccb713b 2018-01-19 stsp *obj = NULL;
813 0a585a0d 2018-03-17 stsp return err;
816 6ccb713b 2018-01-19 stsp (*obj)->type = type;
817 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
818 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
819 6ccb713b 2018-01-19 stsp (*obj)->size = size;
820 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
821 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
823 b107e67f 2018-01-19 stsp return NULL;
826 b107e67f 2018-01-19 stsp static const struct got_error *
827 d7464085 2018-07-09 stsp parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
828 d7464085 2018-07-09 stsp off_t delta_offset)
830 b107e67f 2018-01-19 stsp int64_t o = 0;
831 b107e67f 2018-01-19 stsp uint8_t offN;
837 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
839 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
841 d7464085 2018-07-09 stsp if (pack->map) {
842 d7464085 2018-07-09 stsp size_t mapoff;
843 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
844 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
845 d7464085 2018-07-09 stsp mapoff = (size_t)delta_offset + *len;
846 d7464085 2018-07-09 stsp offN = *(pack->map + mapoff);
849 d7464085 2018-07-09 stsp n = read(pack->fd, &offN, sizeof(offN));
851 d7464085 2018-07-09 stsp return got_error_from_errno();
852 d7464085 2018-07-09 stsp if (n != sizeof(offN))
853 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
855 d7464085 2018-07-09 stsp *len += sizeof(offN);
857 b107e67f 2018-01-19 stsp if (i == 0)
858 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
862 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
865 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
867 b107e67f 2018-01-19 stsp *offset = o;
868 6ccb713b 2018-01-19 stsp return NULL;
871 6ccb713b 2018-01-19 stsp static const struct got_error *
872 d7464085 2018-07-09 stsp parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
873 d7464085 2018-07-09 stsp off_t offset, int tslen)
875 96f5e8b3 2018-01-23 stsp const struct got_error *err;
876 b107e67f 2018-01-19 stsp int64_t negoffset;
877 b107e67f 2018-01-19 stsp size_t negofflen;
881 d7464085 2018-07-09 stsp err = parse_negative_offset(&negoffset, &negofflen, pack,
882 d7464085 2018-07-09 stsp offset + tslen);
884 b107e67f 2018-01-19 stsp return err;
886 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
887 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
888 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
889 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
891 d7464085 2018-07-09 stsp *len = negofflen;
892 96f5e8b3 2018-01-23 stsp return NULL;
895 0e22967e 2018-02-11 stsp static const struct got_error *
896 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
897 c8ecd499 2018-09-09 stsp struct got_pack *, off_t, size_t, int, size_t, unsigned int);
899 96f5e8b3 2018-01-23 stsp static const struct got_error *
900 c336f889 2018-07-23 stsp add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
901 c336f889 2018-07-23 stsp int delta_type, size_t delta_size, size_t delta_data_offset,
902 c336f889 2018-07-23 stsp uint8_t *delta_buf, size_t delta_len)
904 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
906 c336f889 2018-07-23 stsp delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
907 c336f889 2018-07-23 stsp delta_data_offset, delta_buf,
908 a53d2f13 2018-03-17 stsp delta_len);
909 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
910 0a585a0d 2018-03-17 stsp return got_error_from_errno();
911 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
912 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
913 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
914 bdd2fbb3 2018-02-11 stsp return NULL;
917 bdd2fbb3 2018-02-11 stsp static const struct got_error *
918 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
919 c8ecd499 2018-09-09 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
920 c8ecd499 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
923 a3500804 2018-01-23 stsp const struct got_error *err;
924 c3703302 2018-01-23 stsp off_t base_offset;
925 c3703302 2018-01-23 stsp uint8_t base_type;
926 c3703302 2018-01-23 stsp uint64_t base_size;
927 c3703302 2018-01-23 stsp size_t base_tslen;
928 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
929 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
930 d7464085 2018-07-09 stsp size_t delta_len, consumed;
932 d7464085 2018-07-09 stsp err = parse_offset_delta(&base_offset, &consumed, pack,
933 d7464085 2018-07-09 stsp delta_offset, tslen);
935 a3500804 2018-01-23 stsp return err;
937 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + consumed;
938 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
939 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
941 d7464085 2018-07-09 stsp if (pack->map == NULL) {
942 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
943 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
944 d7464085 2018-07-09 stsp return got_error_from_errno();
947 d7464085 2018-07-09 stsp if (pack->map) {
948 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
949 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
950 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
952 d7464085 2018-07-09 stsp return err;
955 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
957 d7464085 2018-07-09 stsp return err;
960 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
961 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
965 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
966 ab3ad429 2018-07-23 stsp if (base_offset >= pack->filesize) {
967 ab3ad429 2018-07-23 stsp err = got_error(GOT_ERR_PACK_OFFSET);
971 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
972 d7464085 2018-07-09 stsp pack, base_offset);
976 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(deltas, packidx, pack, base_offset,
977 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
980 ab3ad429 2018-07-23 stsp free(delta_buf);
981 ab3ad429 2018-07-23 stsp return err;
984 c3703302 2018-01-23 stsp static const struct got_error *
985 c8ecd499 2018-09-09 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
986 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
987 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
989 6b9c9673 2018-01-23 stsp const struct got_error *err;
990 6b9c9673 2018-01-23 stsp struct got_object_id id;
992 6b9c9673 2018-01-23 stsp off_t base_offset;
993 6b9c9673 2018-01-23 stsp uint8_t base_type;
994 6b9c9673 2018-01-23 stsp uint64_t base_size;
995 6b9c9673 2018-01-23 stsp size_t base_tslen;
996 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
997 3efa19e7 2018-07-13 stsp uint8_t *delta_buf = NULL;
998 a53d2f13 2018-03-17 stsp size_t delta_len;
1000 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
1001 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1002 45202a8f 2018-07-11 stsp delta_data_offset = delta_offset + tslen;
1003 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
1004 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1006 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1007 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1008 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
1009 d7464085 2018-07-09 stsp return got_error_from_errno();
1013 d7464085 2018-07-09 stsp if (pack->map) {
1014 45202a8f 2018-07-11 stsp size_t mapoff = (size_t)delta_data_offset;
1015 d7464085 2018-07-09 stsp memcpy(&id, pack->map + mapoff, sizeof(id));
1016 d7464085 2018-07-09 stsp mapoff += sizeof(id);
1017 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
1018 45202a8f 2018-07-11 stsp mapoff, pack->filesize - mapoff);
1020 3efa19e7 2018-07-13 stsp goto done;
1022 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &id, sizeof(id));
1023 d7464085 2018-07-09 stsp if (n < 0)
1024 d7464085 2018-07-09 stsp return got_error_from_errno();
1025 d7464085 2018-07-09 stsp if (n != sizeof(id))
1026 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1027 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
1029 3efa19e7 2018-07-13 stsp goto done;
1032 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1033 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
1035 3efa19e7 2018-07-13 stsp goto done;
1037 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
1038 3413160a 2018-09-09 stsp idx = get_object_idx(packidx, &id);
1039 3efa19e7 2018-07-13 stsp if (idx == -1) {
1040 3efa19e7 2018-07-13 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
1041 3efa19e7 2018-07-13 stsp goto done;
1044 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
1045 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
1046 3efa19e7 2018-07-13 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
1047 3efa19e7 2018-07-13 stsp goto done;
1050 b0e2201a 2018-06-22 stsp if (base_offset >= pack->filesize) {
1051 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1052 0c048b15 2018-04-27 stsp goto done;
1055 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
1056 d7464085 2018-07-09 stsp pack, base_offset);
1058 6b9c9673 2018-01-23 stsp goto done;
1060 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(deltas, packidx, pack, base_offset,
1061 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
1064 3efa19e7 2018-07-13 stsp free(delta_buf);
1065 6b9c9673 2018-01-23 stsp return err;
1068 6b9c9673 2018-01-23 stsp static const struct got_error *
1069 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
1070 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
1071 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
1073 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
1075 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
1076 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
1078 c3703302 2018-01-23 stsp switch (delta_type) {
1079 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
1080 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
1081 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
1082 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
1083 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
1084 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type,
1085 c336f889 2018-07-23 stsp delta_size, 0, NULL, 0);
1087 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1088 c8ecd499 2018-09-09 stsp err = resolve_offset_delta(deltas, packidx, pack,
1089 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1091 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
1092 c8ecd499 2018-09-09 stsp err = resolve_ref_delta(deltas, packidx, pack,
1093 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1096 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
1099 96f5e8b3 2018-01-23 stsp return err;
1102 96f5e8b3 2018-01-23 stsp static const struct got_error *
1103 a98c62b1 2018-09-09 stsp open_delta_object(struct got_object **obj, struct got_packidx *packidx,
1104 a98c62b1 2018-09-09 stsp struct got_pack *pack, struct got_object_id *id, off_t offset,
1105 a98c62b1 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size)
1107 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
1108 96f5e8b3 2018-01-23 stsp int resolved_type;
1110 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
1111 b107e67f 2018-01-19 stsp if (*obj == NULL)
1112 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1114 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
1115 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
1116 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1117 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1118 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
1120 b0e2201a 2018-06-22 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
1121 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
1122 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1123 96f5e8b3 2018-01-23 stsp goto done;
1125 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1127 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
1128 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1130 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
1131 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
1133 96f5e8b3 2018-01-23 stsp goto done;
1135 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1137 96f5e8b3 2018-01-23 stsp goto done;
1138 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
1141 96f5e8b3 2018-01-23 stsp if (err) {
1142 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
1143 96f5e8b3 2018-01-23 stsp *obj = NULL;
1145 96f5e8b3 2018-01-23 stsp return err;
1148 b107e67f 2018-01-19 stsp static const struct got_error *
1149 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
1150 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1152 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1153 a1fd68d8 2018-01-12 stsp off_t offset;
1154 65cf1e80 2018-03-16 stsp char *path_packfile;
1155 6d89869a 2018-03-17 stsp struct got_pack *pack;
1156 6c00b545 2018-01-17 stsp uint8_t type;
1157 6c00b545 2018-01-17 stsp uint64_t size;
1158 3ee5fc21 2018-01-17 stsp size_t tslen;
1160 6c00b545 2018-01-17 stsp *obj = NULL;
1162 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
1163 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
1164 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1166 4a9c75d9 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
1168 6b9c9673 2018-01-23 stsp return err;
1170 6d89869a 2018-03-17 stsp pack = get_cached_pack(path_packfile, repo);
1171 6d89869a 2018-03-17 stsp if (pack == NULL) {
1172 6d89869a 2018-03-17 stsp err = cache_pack(&pack, path_packfile, packidx, repo);
1174 6d89869a 2018-03-17 stsp goto done;
1177 d7464085 2018-07-09 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
1179 a1fd68d8 2018-01-12 stsp goto done;
1181 6c00b545 2018-01-17 stsp switch (type) {
1182 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
1183 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
1184 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
1185 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
1186 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
1187 6ccb713b 2018-01-19 stsp offset + tslen, size);
1190 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1191 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1192 a98c62b1 2018-09-09 stsp err = open_delta_object(obj, packidx, pack, id, offset,
1193 b0e2201a 2018-06-22 stsp tslen, type, size);
1197 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1198 6c00b545 2018-01-17 stsp goto done;
1201 65cf1e80 2018-03-16 stsp free(path_packfile);
1202 a1fd68d8 2018-01-12 stsp return err;
1205 a1fd68d8 2018-01-12 stsp const struct got_error *
1206 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1207 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
1209 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1210 6fd11751 2018-06-04 stsp struct got_packidx *packidx = NULL;
1213 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
1215 6b9c9673 2018-01-23 stsp return err;
1217 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
1219 7e656b93 2018-03-17 stsp return err;
1221 7e656b93 2018-03-17 stsp err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1222 3ee5fc21 2018-01-17 stsp return err;
1225 efd2a263 2018-01-19 stsp static const struct got_error *
1226 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1228 22484865 2018-03-13 stsp struct got_delta *delta;
1229 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1231 22484865 2018-03-13 stsp *max_size = 0;
1232 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1233 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1234 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1235 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1236 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1237 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1238 22484865 2018-03-13 stsp const struct got_error *err;
1239 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1240 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
1242 22484865 2018-03-13 stsp return err;
1244 22484865 2018-03-13 stsp base_size = delta->size;
1245 22484865 2018-03-13 stsp if (base_size > *max_size)
1246 22484865 2018-03-13 stsp *max_size = base_size;
1247 22484865 2018-03-13 stsp if (result_size > *max_size)
1248 22484865 2018-03-13 stsp *max_size = result_size;
1251 9feb4ff2 2018-03-14 stsp return NULL;
1254 22484865 2018-03-13 stsp static const struct got_error *
1255 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1256 2ce68b2f 2018-09-09 stsp struct got_pack *pack, FILE *outfile)
1258 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1259 6691714a 2018-01-23 stsp struct got_delta *delta;
1260 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1261 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1262 b29656e2 2018-03-16 stsp size_t accum_size = 0;
1263 22484865 2018-03-13 stsp uint64_t max_size;
1264 6691714a 2018-01-23 stsp int n = 0;
1266 b29656e2 2018-03-16 stsp *result_size = 0;
1268 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1269 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1271 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1272 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1274 22484865 2018-03-13 stsp return err;
1275 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1276 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1277 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1278 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1280 22484865 2018-03-13 stsp base_file = got_opentemp();
1281 8628c62d 2018-03-15 stsp if (base_file == NULL)
1282 8628c62d 2018-03-15 stsp return got_error_from_errno();
1284 22484865 2018-03-13 stsp accum_file = got_opentemp();
1285 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1286 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1287 8628c62d 2018-03-15 stsp fclose(base_file);
1288 8628c62d 2018-03-15 stsp return err;
1292 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1293 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1294 a6b158cc 2018-02-11 stsp if (n == 0) {
1295 d7464085 2018-07-09 stsp size_t base_len, mapoff;
1296 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1298 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1299 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1300 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1301 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1302 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1303 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1304 72eb3431 2018-04-01 stsp goto done;
1307 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1308 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1309 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1310 0c048b15 2018-04-27 stsp goto done;
1312 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1313 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1315 d7464085 2018-07-09 stsp err = got_error_from_errno();
1316 d7464085 2018-07-09 stsp goto done;
1319 d7464085 2018-07-09 stsp if (base_file) {
1320 d7464085 2018-07-09 stsp if (pack->map) {
1321 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1322 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1323 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1324 d7464085 2018-07-09 stsp pack->filesize - mapoff, base_file);
1326 d7464085 2018-07-09 stsp err = got_inflate_to_file_fd(&base_len,
1327 d7464085 2018-07-09 stsp pack->fd, base_file);
1329 d7464085 2018-07-09 stsp if (pack->map) {
1330 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1331 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1332 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1333 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1335 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1336 d7464085 2018-07-09 stsp &base_len, pack->fd);
1337 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1338 8628c62d 2018-03-15 stsp uint8_t *p;
1339 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1340 8628c62d 2018-03-15 stsp if (p == NULL) {
1341 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1342 8628c62d 2018-03-15 stsp goto done;
1344 8628c62d 2018-03-15 stsp base_buf = p;
1348 a6b158cc 2018-02-11 stsp goto done;
1350 8628c62d 2018-03-15 stsp if (base_file)
1351 8628c62d 2018-03-15 stsp rewind(base_file);
1355 8628c62d 2018-03-15 stsp if (base_buf) {
1356 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1357 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1360 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1361 a53d2f13 2018-03-17 stsp delta->delta_len,
1362 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1363 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1364 b29656e2 2018-03-16 stsp &accum_size);
1367 6691714a 2018-01-23 stsp goto done;
1369 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1370 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1371 8628c62d 2018-03-15 stsp if (base_buf) {
1372 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1373 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1374 8628c62d 2018-03-15 stsp base_buf = tmp;
1376 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1377 8628c62d 2018-03-15 stsp accum_file = base_file;
1378 8628c62d 2018-03-15 stsp base_file = tmp;
1379 8628c62d 2018-03-15 stsp rewind(base_file);
1380 8628c62d 2018-03-15 stsp rewind(accum_file);
1386 8628c62d 2018-03-15 stsp free(base_buf);
1387 8628c62d 2018-03-15 stsp if (accum_buf) {
1388 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1389 8628c62d 2018-03-15 stsp free(accum_buf);
1390 8628c62d 2018-03-15 stsp if (len != accum_size)
1391 673702af 2018-07-23 stsp err = got_ferror(outfile, GOT_ERR_IO);
1393 8628c62d 2018-03-15 stsp if (base_file)
1394 8628c62d 2018-03-15 stsp fclose(base_file);
1395 8628c62d 2018-03-15 stsp if (accum_file)
1396 8628c62d 2018-03-15 stsp fclose(accum_file);
1397 6691714a 2018-01-23 stsp rewind(outfile);
1398 b29656e2 2018-03-16 stsp if (err == NULL)
1399 b29656e2 2018-03-16 stsp *result_size = accum_size;
1400 e0ab43e7 2018-03-16 stsp return err;
1403 e0ab43e7 2018-03-16 stsp static const struct got_error *
1404 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1405 48095039 2018-09-09 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1407 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1408 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1409 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1410 e0ab43e7 2018-03-16 stsp size_t accum_size;
1411 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1412 e0ab43e7 2018-03-16 stsp int n = 0;
1414 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1415 e0ab43e7 2018-03-16 stsp *outlen = 0;
1417 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1418 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1420 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1422 e0ab43e7 2018-03-16 stsp return err;
1423 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1424 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1425 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1427 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1428 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1429 e0ab43e7 2018-03-16 stsp if (n == 0) {
1430 e0ab43e7 2018-03-16 stsp size_t base_len;
1431 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1433 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1434 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1435 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1436 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1437 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1438 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1439 72eb3431 2018-04-01 stsp goto done;
1442 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1443 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1444 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1445 0c048b15 2018-04-27 stsp goto done;
1447 d7464085 2018-07-09 stsp if (pack->map) {
1448 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
1449 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1450 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1451 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1453 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1455 d7464085 2018-07-09 stsp err = got_error_from_errno();
1456 d7464085 2018-07-09 stsp goto done;
1458 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1459 d7464085 2018-07-09 stsp &base_len, pack->fd);
1462 d7464085 2018-07-09 stsp goto done;
1463 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1464 e0ab43e7 2018-03-16 stsp uint8_t *p;
1465 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1466 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1467 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1468 e0ab43e7 2018-03-16 stsp goto done;
1470 e0ab43e7 2018-03-16 stsp base_buf = p;
1476 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1477 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1480 e0ab43e7 2018-03-16 stsp goto done;
1482 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1483 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1484 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1485 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1486 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1491 e0ab43e7 2018-03-16 stsp free(base_buf);
1492 e0ab43e7 2018-03-16 stsp if (err) {
1493 e0ab43e7 2018-03-16 stsp free(accum_buf);
1494 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1495 e0ab43e7 2018-03-16 stsp *outlen = 0;
1497 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1498 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1500 efd2a263 2018-01-19 stsp return err;
1503 3ee5fc21 2018-01-17 stsp const struct got_error *
1504 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1505 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1507 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1508 2ce68b2f 2018-09-09 stsp struct got_pack *pack;
1510 8b2180d4 2018-04-26 stsp *f = NULL;
1512 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1513 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1515 2ce68b2f 2018-09-09 stsp pack = get_cached_pack(obj->path_packfile, repo);
1516 2ce68b2f 2018-09-09 stsp if (pack == NULL) {
1517 2ce68b2f 2018-09-09 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1519 2ce68b2f 2018-09-09 stsp return err;
1522 040bf4a1 2018-04-01 stsp *f = got_opentemp();
1523 040bf4a1 2018-04-01 stsp if (*f == NULL) {
1524 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1525 040bf4a1 2018-04-01 stsp goto done;
1528 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1529 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1530 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1531 0c048b15 2018-04-27 stsp goto done;
1534 d7464085 2018-07-09 stsp if (pack->map) {
1535 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1536 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(&obj->size, pack->map,
1537 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff, *f);
1539 d7464085 2018-07-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1540 d7464085 2018-07-09 stsp err = got_error_from_errno();
1541 d7464085 2018-07-09 stsp goto done;
1543 d7464085 2018-07-09 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1546 2ce68b2f 2018-09-09 stsp err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1549 d0f3be7c 2018-03-17 stsp if (err && *f) {
1550 3ee5fc21 2018-01-17 stsp fclose(*f);
1551 d0f3be7c 2018-03-17 stsp *f = NULL;
1553 a1fd68d8 2018-01-12 stsp return err;
1556 e0ab43e7 2018-03-16 stsp const struct got_error *
1557 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1558 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1560 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1561 48095039 2018-09-09 stsp struct got_pack *pack;
1563 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1564 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1566 48095039 2018-09-09 stsp pack = get_cached_pack(obj->path_packfile, repo);
1567 48095039 2018-09-09 stsp if (pack == NULL) {
1568 48095039 2018-09-09 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1570 48095039 2018-09-09 stsp goto done;
1573 48095039 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1574 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1575 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1576 0c048b15 2018-04-27 stsp goto done;
1578 d7464085 2018-07-09 stsp if (pack->map) {
1579 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1580 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(buf, len, pack->map,
1581 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
1583 d7464085 2018-07-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1584 d7464085 2018-07-09 stsp err = got_error_from_errno();
1585 d7464085 2018-07-09 stsp goto done;
1587 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(buf, len, pack->fd);
1590 48095039 2018-09-09 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);
1592 e0ab43e7 2018-03-16 stsp return err;