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 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.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 0cb74cf4 2018-07-08 stsp got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
124 6fd11751 2018-06-04 stsp struct got_packidx *p;
125 c8262310 2018-06-04 stsp struct got_packidx_v2_hdr *h;
126 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
127 57b35b75 2018-06-22 stsp size_t nobj, len_fanout, len_ids, offset, remain;
128 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
129 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
131 6fd11751 2018-06-04 stsp *packidx = NULL;
133 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
135 57b35b75 2018-06-22 stsp p = calloc(1, sizeof(*p));
136 57b35b75 2018-06-22 stsp if (p == NULL)
137 9feb4ff2 2018-03-14 stsp return got_error_from_errno();
139 57b35b75 2018-06-22 stsp p->fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
140 57b35b75 2018-06-22 stsp if (p->fd == -1)
141 57b35b75 2018-06-22 stsp return got_error_from_errno();
143 57b35b75 2018-06-22 stsp err = get_packfile_size(&p->len, path);
145 57b35b75 2018-06-22 stsp close(p->fd);
147 0a0a3048 2018-01-10 stsp return err;
149 57b35b75 2018-06-22 stsp if (p->len < sizeof(p->hdr)) {
150 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
151 57b35b75 2018-06-22 stsp close(p->fd);
153 57b35b75 2018-06-22 stsp return err;
156 6fd11751 2018-06-04 stsp p->path_packidx = strdup(path);
157 6fd11751 2018-06-04 stsp if (p->path_packidx == NULL) {
158 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
162 57b35b75 2018-06-22 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
163 57b35b75 2018-06-22 stsp if (p->map == MAP_FAILED) {
164 57b35b75 2018-06-22 stsp err = got_error_from_errno();
167 57b35b75 2018-06-22 stsp h = &p->hdr;
168 57b35b75 2018-06-22 stsp offset = 0;
169 57b35b75 2018-06-22 stsp remain = p->len;
171 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->magic)) {
172 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
175 57b35b75 2018-06-22 stsp h->magic = (uint32_t *)(p->map + offset);
176 57b35b75 2018-06-22 stsp if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
177 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
180 57b35b75 2018-06-22 stsp offset += sizeof(*h->magic);
181 57b35b75 2018-06-22 stsp remain -= sizeof(*h->magic);
183 0cb74cf4 2018-07-08 stsp if (verify)
184 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
186 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->version)) {
187 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
190 57b35b75 2018-06-22 stsp h->version = (uint32_t *)(p->map + offset);
191 57b35b75 2018-06-22 stsp if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
192 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
195 57b35b75 2018-06-22 stsp offset += sizeof(*h->version);
196 57b35b75 2018-06-22 stsp remain -= sizeof(*h->version);
198 0cb74cf4 2018-07-08 stsp if (verify)
199 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
201 57b35b75 2018-06-22 stsp len_fanout =
202 57b35b75 2018-06-22 stsp sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
203 57b35b75 2018-06-22 stsp if (remain < len_fanout) {
204 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
207 57b35b75 2018-06-22 stsp h->fanout_table = (uint32_t *)(p->map + offset);
208 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
211 0cb74cf4 2018-07-08 stsp if (verify)
212 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
213 57b35b75 2018-06-22 stsp offset += len_fanout;
214 57b35b75 2018-06-22 stsp remain -= len_fanout;
216 c8262310 2018-06-04 stsp nobj = betoh32(h->fanout_table[0xff]);
217 57b35b75 2018-06-22 stsp len_ids = nobj * sizeof(*h->sorted_ids);
218 57b35b75 2018-06-22 stsp if (len_ids <= nobj || len_ids > remain) {
219 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
222 57b35b75 2018-06-22 stsp h->sorted_ids =
223 57b35b75 2018-06-22 stsp (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
224 0cb74cf4 2018-07-08 stsp if (verify)
225 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
226 57b35b75 2018-06-22 stsp offset += len_ids;
227 57b35b75 2018-06-22 stsp remain -= len_ids;
229 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->crc32)) {
230 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
233 57b35b75 2018-06-22 stsp h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
234 0cb74cf4 2018-07-08 stsp if (verify)
235 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
236 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->crc32);
237 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->crc32);
239 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->offsets)) {
240 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
243 57b35b75 2018-06-22 stsp h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
244 0cb74cf4 2018-07-08 stsp if (verify)
245 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->offsets,
246 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->offsets));
247 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->offsets);
248 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->offsets);
250 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
251 57b35b75 2018-06-22 stsp if (p->len <= 0x80000000)
252 0a0a3048 2018-01-10 stsp goto checksum;
254 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->large_offsets)) {
255 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
258 57b35b75 2018-06-22 stsp h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
259 0cb74cf4 2018-07-08 stsp if (verify)
260 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
261 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->large_offsets));
262 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->large_offsets);
263 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->large_offsets);
266 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->trailer)) {
267 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
270 57b35b75 2018-06-22 stsp h->trailer =
271 57b35b75 2018-06-22 stsp (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
272 0cb74cf4 2018-07-08 stsp if (verify) {
273 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
274 0cb74cf4 2018-07-08 stsp SHA1Final(sha1, &ctx);
275 0cb74cf4 2018-07-08 stsp if (memcmp(h->trailer->packidx_sha1, sha1,
276 0cb74cf4 2018-07-08 stsp SHA1_DIGEST_LENGTH) != 0)
277 0cb74cf4 2018-07-08 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
281 0a0a3048 2018-01-10 stsp got_packidx_close(p);
283 0a0a3048 2018-01-10 stsp *packidx = p;
284 0a0a3048 2018-01-10 stsp return err;
287 57b35b75 2018-06-22 stsp const struct got_error *
288 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
290 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
292 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
293 57b35b75 2018-06-22 stsp if (packidx->map != NULL && packidx->map != MAP_FAILED) {
294 57b35b75 2018-06-22 stsp if (munmap(packidx->map, packidx->len) == -1)
295 57b35b75 2018-06-22 stsp err = got_error_from_errno();
297 57b35b75 2018-06-22 stsp close(packidx->fd);
298 0a0a3048 2018-01-10 stsp free(packidx);
300 57b35b75 2018-06-22 stsp return err;
304 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
306 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
309 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
312 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
313 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
319 a1fd68d8 2018-01-12 stsp static off_t
320 6fd11751 2018-06-04 stsp get_object_offset(struct got_packidx *packidx, int idx)
322 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
323 6fd11751 2018-06-04 stsp uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
324 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
325 a1fd68d8 2018-01-12 stsp uint64_t loffset;
326 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
327 6fd11751 2018-06-04 stsp if (idx < 0 || idx > totobj ||
328 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
330 6fd11751 2018-06-04 stsp loffset = betoh64(packidx->hdr.large_offsets[idx]);
331 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
333 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
337 6fd11751 2018-06-04 stsp get_object_idx(struct got_packidx *packidx, struct got_object_id *id,
338 6fd11751 2018-06-04 stsp struct got_repository *repo)
340 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
341 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
342 40aeb19c 2018-06-22 stsp int left = 0, right = totobj - 1;
344 a1fd68d8 2018-01-12 stsp if (id0 > 0)
345 40aeb19c 2018-06-22 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
347 40aeb19c 2018-06-22 stsp while (left <= right) {
348 57b35b75 2018-06-22 stsp struct got_packidx_object_id *oid;
349 40aeb19c 2018-06-22 stsp int i, cmp;
351 40aeb19c 2018-06-22 stsp i = ((left + right) / 2);
352 40aeb19c 2018-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
353 57b35b75 2018-06-22 stsp cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
354 16dcbf91 2018-04-01 stsp if (cmp == 0)
356 40aeb19c 2018-06-22 stsp else if (cmp > 0)
357 40aeb19c 2018-06-22 stsp left = i + 1;
358 40aeb19c 2018-06-22 stsp else if (cmp < 0)
359 40aeb19c 2018-06-22 stsp right = i - 1;
365 57b35b75 2018-06-22 stsp static const struct got_error *
366 6fd11751 2018-06-04 stsp cache_packidx(struct got_packidx *packidx, struct got_repository *repo)
368 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
371 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
372 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
376 65cf1e80 2018-03-16 stsp if (i == nitems(repo->packidx_cache)) {
377 57b35b75 2018-06-22 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
379 57b35b75 2018-06-22 stsp return err;
380 65cf1e80 2018-03-16 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
381 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache) -
382 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache[0]));
386 49c99f91 2018-06-22 stsp repo->packidx_cache[i] = packidx;
387 57b35b75 2018-06-22 stsp return NULL;
390 6b9c9673 2018-01-23 stsp static const struct got_error *
391 6fd11751 2018-06-04 stsp search_packidx(struct got_packidx **packidx, int *idx,
392 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
394 6b9c9673 2018-01-23 stsp const struct got_error *err;
395 6b9c9673 2018-01-23 stsp char *path_packdir;
396 6b9c9673 2018-01-23 stsp DIR *packdir;
397 6b9c9673 2018-01-23 stsp struct dirent *dent;
398 6b9c9673 2018-01-23 stsp char *path_packidx;
401 65cf1e80 2018-03-16 stsp /* Search pack index cache. */
402 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
403 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
405 72eb3431 2018-04-01 stsp *idx = get_object_idx(repo->packidx_cache[i], id, repo);
406 79b11c62 2018-03-09 stsp if (*idx != -1) {
407 6bb255dc 2018-03-17 stsp *packidx = repo->packidx_cache[i];
408 79b11c62 2018-03-09 stsp return NULL;
411 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
413 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
414 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
415 0a585a0d 2018-03-17 stsp return got_error_from_errno();
417 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
418 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
419 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
423 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
424 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
427 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
428 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
429 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
433 0cb74cf4 2018-07-08 stsp err = got_packidx_open(packidx, path_packidx, 0);
434 6b9c9673 2018-01-23 stsp free(path_packidx);
438 72eb3431 2018-04-01 stsp *idx = get_object_idx(*packidx, id, repo);
439 6b9c9673 2018-01-23 stsp if (*idx != -1) {
440 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
441 57b35b75 2018-06-22 stsp err = cache_packidx(*packidx, repo);
445 57b35b75 2018-06-22 stsp err = got_packidx_close(*packidx);
446 6b9c9673 2018-01-23 stsp *packidx = NULL;
451 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
453 6b9c9673 2018-01-23 stsp free(path_packdir);
454 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
455 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
456 6b9c9673 2018-01-23 stsp return err;
459 c7fe698a 2018-01-23 stsp static const struct got_error *
460 65cf1e80 2018-03-16 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
461 6fd11751 2018-06-04 stsp struct got_packidx *packidx)
463 6fd11751 2018-06-04 stsp size_t size;
465 6fd11751 2018-06-04 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
466 6fd11751 2018-06-04 stsp size = strlen(packidx->path_packidx) + 2;
467 6fd11751 2018-06-04 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
468 6fd11751 2018-06-04 stsp return got_error(GOT_ERR_BAD_PATH);
470 6fd11751 2018-06-04 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
471 6fd11751 2018-06-04 stsp if (*path_packfile == NULL)
472 0a585a0d 2018-03-17 stsp return got_error_from_errno();
474 6fd11751 2018-06-04 stsp /* Copy up to and excluding ".idx". */
475 d475dd0d 2018-06-04 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
476 72fb0363 2018-06-04 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
477 d475dd0d 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
479 6fd11751 2018-06-04 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
480 6fd11751 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
482 65cf1e80 2018-03-16 stsp return NULL;
485 65cf1e80 2018-03-16 stsp const struct got_error *
486 6fd11751 2018-06-04 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
488 65cf1e80 2018-03-16 stsp const struct got_error *err = NULL;
489 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
490 65cf1e80 2018-03-16 stsp struct got_packfile_hdr hdr;
493 8b2180d4 2018-04-26 stsp n = read(fd, &hdr, sizeof(hdr));
495 8b2180d4 2018-04-26 stsp return got_error_from_errno();
496 8b2180d4 2018-04-26 stsp if (n != sizeof(hdr))
497 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
499 65cf1e80 2018-03-16 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
500 65cf1e80 2018-03-16 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
501 65cf1e80 2018-03-16 stsp betoh32(hdr.nobjects) != totobj)
502 65cf1e80 2018-03-16 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
504 65cf1e80 2018-03-16 stsp return err;
507 7e656b93 2018-03-17 stsp static const struct got_error *
508 8b2180d4 2018-04-26 stsp open_packfile(int *fd, const char *path_packfile,
509 6fd11751 2018-06-04 stsp struct got_repository *repo, struct got_packidx *packidx)
511 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
513 8b2180d4 2018-04-26 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
514 8b2180d4 2018-04-26 stsp if (*fd == -1)
515 8b2180d4 2018-04-26 stsp return got_error_from_errno();
517 7e656b93 2018-03-17 stsp if (packidx) {
518 8b2180d4 2018-04-26 stsp err = read_packfile_hdr(*fd, packidx);
520 8b2180d4 2018-04-26 stsp close(*fd);
524 7e656b93 2018-03-17 stsp return err;
528 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
530 8b2180d4 2018-04-26 stsp close(pack->fd);
531 8b2180d4 2018-04-26 stsp pack->fd = -1;
532 4589e373 2018-03-17 stsp free(pack->path_packfile);
533 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
534 4589e373 2018-03-17 stsp pack->filesize = 0;
537 7e656b93 2018-03-17 stsp static const struct got_error *
538 7e656b93 2018-03-17 stsp cache_pack(struct got_pack **packp, const char *path_packfile,
539 6fd11751 2018-06-04 stsp struct got_packidx *packidx, struct got_repository *repo)
541 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
542 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
546 7e656b93 2018-03-17 stsp *packp = NULL;
548 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
549 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
550 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
552 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
553 7e656b93 2018-03-17 stsp return NULL;
556 7e656b93 2018-03-17 stsp if (i == nitems(repo->packs) - 1) {
557 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i - 1]);
558 7e656b93 2018-03-17 stsp memmove(&repo->packs[1], &repo->packs[0],
559 7e656b93 2018-03-17 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
563 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
565 7e656b93 2018-03-17 stsp pack->path_packfile = strdup(path_packfile);
566 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL) {
567 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
571 8b2180d4 2018-04-26 stsp err = open_packfile(&pack->fd, path_packfile, repo, packidx);
575 7e656b93 2018-03-17 stsp err = get_packfile_size(&pack->filesize, path_packfile);
578 f5feadcc 2018-06-04 stsp if (pack) {
579 7e656b93 2018-03-17 stsp free(pack->path_packfile);
580 f5feadcc 2018-06-04 stsp memset(pack, 0, sizeof(*pack));
582 7e656b93 2018-03-17 stsp } else if (packp)
583 7e656b93 2018-03-17 stsp *packp = pack;
584 c7fe698a 2018-01-23 stsp return err;
587 7e656b93 2018-03-17 stsp struct got_pack *
588 7e656b93 2018-03-17 stsp get_cached_pack(const char *path_packfile, struct got_repository *repo)
590 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
593 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
594 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
595 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
597 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
598 7e656b93 2018-03-17 stsp return pack;
601 7e656b93 2018-03-17 stsp return NULL;
604 c7fe698a 2018-01-23 stsp static const struct got_error *
605 8b2180d4 2018-04-26 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len, int fd)
607 a487c1d0 2018-01-14 stsp uint8_t t = 0;
608 a487c1d0 2018-01-14 stsp uint64_t s = 0;
609 a487c1d0 2018-01-14 stsp uint8_t sizeN;
614 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
616 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
618 8b2180d4 2018-04-26 stsp n = read(fd, &sizeN, sizeof(sizeN));
620 8b2180d4 2018-04-26 stsp return got_error_from_errno();
621 8b2180d4 2018-04-26 stsp if (n != sizeof(sizeN))
622 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
624 a1fd68d8 2018-01-12 stsp if (i == 0) {
625 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
626 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
627 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
629 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
630 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
633 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
637 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
638 a487c1d0 2018-01-14 stsp return NULL;
641 a1fd68d8 2018-01-12 stsp static const struct got_error *
642 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
643 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
645 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
646 6ccb713b 2018-01-19 stsp if (*obj == NULL)
647 0a585a0d 2018-03-17 stsp return got_error_from_errno();
649 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
650 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
651 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
652 6ccb713b 2018-01-19 stsp free(*obj);
653 6ccb713b 2018-01-19 stsp *obj = NULL;
654 0a585a0d 2018-03-17 stsp return err;
657 6ccb713b 2018-01-19 stsp (*obj)->type = type;
658 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
659 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
660 6ccb713b 2018-01-19 stsp (*obj)->size = size;
661 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
662 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
664 b107e67f 2018-01-19 stsp return NULL;
667 b107e67f 2018-01-19 stsp static const struct got_error *
668 8b2180d4 2018-04-26 stsp parse_negative_offset(int64_t *offset, size_t *len, int fd)
670 b107e67f 2018-01-19 stsp int64_t o = 0;
671 b107e67f 2018-01-19 stsp uint8_t offN;
676 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
678 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
680 8b2180d4 2018-04-26 stsp n = read(fd, &offN, sizeof(offN));
682 8b2180d4 2018-04-26 stsp return got_error_from_errno();
683 8b2180d4 2018-04-26 stsp if (n != sizeof(offN))
684 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
686 b107e67f 2018-01-19 stsp if (i == 0)
687 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
691 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
694 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
696 b107e67f 2018-01-19 stsp *offset = o;
697 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
698 6ccb713b 2018-01-19 stsp return NULL;
701 6ccb713b 2018-01-19 stsp static const struct got_error *
702 8b2180d4 2018-04-26 stsp parse_offset_delta(off_t *base_offset, int fd, off_t offset)
704 96f5e8b3 2018-01-23 stsp const struct got_error *err;
705 b107e67f 2018-01-19 stsp int64_t negoffset;
706 b107e67f 2018-01-19 stsp size_t negofflen;
708 8b2180d4 2018-04-26 stsp err = parse_negative_offset(&negoffset, &negofflen, fd);
710 b107e67f 2018-01-19 stsp return err;
712 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
713 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
714 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
715 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
717 96f5e8b3 2018-01-23 stsp return NULL;
720 0e22967e 2018-02-11 stsp static const struct got_error *
721 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
722 b0e2201a 2018-06-22 stsp struct got_packidx *, struct got_pack *, off_t, size_t, int,
723 652b2703 2018-06-22 stsp size_t, unsigned int);
725 96f5e8b3 2018-01-23 stsp static const struct got_error *
726 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
727 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
728 a53d2f13 2018-03-17 stsp size_t delta_data_offset, uint8_t *delta_buf, size_t delta_len)
730 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
732 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
733 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf,
734 a53d2f13 2018-03-17 stsp delta_len);
735 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
736 0a585a0d 2018-03-17 stsp return got_error_from_errno();
737 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
738 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
739 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
740 bdd2fbb3 2018-02-11 stsp return NULL;
743 bdd2fbb3 2018-02-11 stsp static const struct got_error *
744 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
745 b0e2201a 2018-06-22 stsp struct got_repository *repo, struct got_packidx *packidx,
746 b0e2201a 2018-06-22 stsp struct got_pack *pack, off_t delta_offset, size_t tslen,
747 b0e2201a 2018-06-22 stsp int delta_type, size_t delta_size, unsigned int recursion)
750 a3500804 2018-01-23 stsp const struct got_error *err;
751 c3703302 2018-01-23 stsp off_t base_offset;
752 c3703302 2018-01-23 stsp uint8_t base_type;
753 c3703302 2018-01-23 stsp uint64_t base_size;
754 c3703302 2018-01-23 stsp size_t base_tslen;
755 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
756 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
757 a53d2f13 2018-03-17 stsp size_t delta_len;
759 b0e2201a 2018-06-22 stsp err = parse_offset_delta(&base_offset, pack->fd, delta_offset);
761 a3500804 2018-01-23 stsp return err;
763 b0e2201a 2018-06-22 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
764 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
765 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
767 b0e2201a 2018-06-22 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
769 a53d2f13 2018-03-17 stsp return err;
771 b0e2201a 2018-06-22 stsp err = add_delta(deltas, pack->path_packfile, delta_offset, tslen,
772 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
774 bdd2fbb3 2018-02-11 stsp return err;
776 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
777 b0e2201a 2018-06-22 stsp if (base_offset >= pack->filesize)
778 0c048b15 2018-04-27 stsp return got_error(GOT_ERR_PACK_OFFSET);
779 b0e2201a 2018-06-22 stsp if (lseek(pack->fd, base_offset, SEEK_SET) == -1)
780 b107e67f 2018-01-19 stsp return got_error_from_errno();
782 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
785 b107e67f 2018-01-19 stsp return err;
787 b0e2201a 2018-06-22 stsp return resolve_delta_chain(deltas, repo, packidx, pack, base_offset,
788 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
791 c3703302 2018-01-23 stsp static const struct got_error *
792 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
793 b0e2201a 2018-06-22 stsp struct got_packidx *packidx, struct got_pack *pack,
794 652b2703 2018-06-22 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
795 652b2703 2018-06-22 stsp unsigned int recursion)
797 6b9c9673 2018-01-23 stsp const struct got_error *err;
798 6b9c9673 2018-01-23 stsp struct got_object_id id;
800 6b9c9673 2018-01-23 stsp off_t base_offset;
801 6b9c9673 2018-01-23 stsp uint8_t base_type;
802 6b9c9673 2018-01-23 stsp uint64_t base_size;
803 6b9c9673 2018-01-23 stsp size_t base_tslen;
805 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
806 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
807 a53d2f13 2018-03-17 stsp size_t delta_len;
809 b0e2201a 2018-06-22 stsp n = read(pack->fd, &id, sizeof(id));
811 8b2180d4 2018-04-26 stsp return got_error_from_errno();
812 8b2180d4 2018-04-26 stsp if (n != sizeof(id))
813 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
815 b0e2201a 2018-06-22 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
816 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
817 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
819 b0e2201a 2018-06-22 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
821 a53d2f13 2018-03-17 stsp return err;
823 b0e2201a 2018-06-22 stsp err = add_delta(deltas, pack->path_packfile, delta_offset, tslen,
824 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
826 bdd2fbb3 2018-02-11 stsp return err;
828 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
829 652b2703 2018-06-22 stsp idx = get_object_idx(packidx, &id, repo);
830 652b2703 2018-06-22 stsp if (idx == -1)
831 652b2703 2018-06-22 stsp return got_error(GOT_ERR_BAD_PACKFILE);
833 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
834 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
835 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
838 b0e2201a 2018-06-22 stsp if (base_offset >= pack->filesize) {
839 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
842 b0e2201a 2018-06-22 stsp if (lseek(pack->fd, base_offset, SEEK_SET) == -1) {
843 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
847 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
852 b0e2201a 2018-06-22 stsp err = resolve_delta_chain(deltas, repo, packidx, pack, base_offset,
853 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
855 6b9c9673 2018-01-23 stsp return err;
858 6b9c9673 2018-01-23 stsp static const struct got_error *
859 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
860 b0e2201a 2018-06-22 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
861 b0e2201a 2018-06-22 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
863 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
865 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
866 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
868 c3703302 2018-01-23 stsp switch (delta_type) {
869 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
870 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
871 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
872 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
873 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
874 b0e2201a 2018-06-22 stsp err = add_delta(deltas, pack->path_packfile, delta_offset,
875 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, 0, NULL, 0);
877 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
878 b0e2201a 2018-06-22 stsp err = resolve_offset_delta(deltas, repo, packidx, pack,
879 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
881 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
882 b0e2201a 2018-06-22 stsp err = resolve_ref_delta(deltas, repo, packidx, pack,
883 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
886 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
889 96f5e8b3 2018-01-23 stsp return err;
892 96f5e8b3 2018-01-23 stsp static const struct got_error *
893 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
894 b0e2201a 2018-06-22 stsp struct got_packidx *packidx, struct got_pack *pack,
895 b0e2201a 2018-06-22 stsp struct got_object_id *id, off_t offset, size_t tslen,
896 6fd11751 2018-06-04 stsp int delta_type, size_t delta_size)
898 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
899 96f5e8b3 2018-01-23 stsp int resolved_type;
901 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
902 b107e67f 2018-01-19 stsp if (*obj == NULL)
903 0a585a0d 2018-03-17 stsp return got_error_from_errno();
905 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
906 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
907 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
908 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
909 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
911 b0e2201a 2018-06-22 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
912 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
913 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
916 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
918 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
919 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
921 b0e2201a 2018-06-22 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packidx, pack, offset,
922 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
926 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
929 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
933 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
934 96f5e8b3 2018-01-23 stsp *obj = NULL;
936 96f5e8b3 2018-01-23 stsp return err;
939 b107e67f 2018-01-19 stsp static const struct got_error *
940 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
941 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
943 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
944 a1fd68d8 2018-01-12 stsp off_t offset;
945 65cf1e80 2018-03-16 stsp char *path_packfile;
946 6d89869a 2018-03-17 stsp struct got_pack *pack;
947 6c00b545 2018-01-17 stsp uint8_t type;
948 6c00b545 2018-01-17 stsp uint64_t size;
949 3ee5fc21 2018-01-17 stsp size_t tslen;
951 6c00b545 2018-01-17 stsp *obj = NULL;
953 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
954 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
955 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
957 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_packfile, repo, packidx);
959 6b9c9673 2018-01-23 stsp return err;
961 6d89869a 2018-03-17 stsp pack = get_cached_pack(path_packfile, repo);
962 6d89869a 2018-03-17 stsp if (pack == NULL) {
963 6d89869a 2018-03-17 stsp err = cache_pack(&pack, path_packfile, packidx, repo);
968 0c048b15 2018-04-27 stsp if (offset >= pack->filesize) {
969 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
972 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1) {
973 6c00b545 2018-01-17 stsp err = got_error_from_errno();
977 8b2180d4 2018-04-26 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack->fd);
981 6c00b545 2018-01-17 stsp switch (type) {
982 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
983 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
984 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
985 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
986 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
987 6ccb713b 2018-01-19 stsp offset + tslen, size);
990 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
991 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
992 b0e2201a 2018-06-22 stsp err = open_delta_object(obj, repo, packidx, pack, id, offset,
993 b0e2201a 2018-06-22 stsp tslen, type, size);
997 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1001 65cf1e80 2018-03-16 stsp free(path_packfile);
1002 a1fd68d8 2018-01-12 stsp return err;
1005 a1fd68d8 2018-01-12 stsp const struct got_error *
1006 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1007 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
1009 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1010 6fd11751 2018-06-04 stsp struct got_packidx *packidx = NULL;
1013 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
1015 6b9c9673 2018-01-23 stsp return err;
1017 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
1019 7e656b93 2018-03-17 stsp return err;
1021 7e656b93 2018-03-17 stsp err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1022 3ee5fc21 2018-01-17 stsp return err;
1025 efd2a263 2018-01-19 stsp static const struct got_error *
1026 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1028 22484865 2018-03-13 stsp struct got_delta *delta;
1029 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1031 22484865 2018-03-13 stsp *max_size = 0;
1032 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1033 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1034 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1035 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1036 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1037 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1038 22484865 2018-03-13 stsp const struct got_error *err;
1039 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1040 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
1042 22484865 2018-03-13 stsp return err;
1044 22484865 2018-03-13 stsp base_size = delta->size;
1045 22484865 2018-03-13 stsp if (base_size > *max_size)
1046 22484865 2018-03-13 stsp *max_size = base_size;
1047 22484865 2018-03-13 stsp if (result_size > *max_size)
1048 22484865 2018-03-13 stsp *max_size = result_size;
1051 9feb4ff2 2018-03-14 stsp return NULL;
1054 22484865 2018-03-13 stsp static const struct got_error *
1055 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1056 72eb3431 2018-04-01 stsp FILE *outfile, struct got_repository *repo)
1058 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1059 6691714a 2018-01-23 stsp struct got_delta *delta;
1060 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1061 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1062 b29656e2 2018-03-16 stsp size_t accum_size = 0;
1063 22484865 2018-03-13 stsp uint64_t max_size;
1064 6691714a 2018-01-23 stsp int n = 0;
1066 b29656e2 2018-03-16 stsp *result_size = 0;
1068 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1069 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1071 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1072 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1074 22484865 2018-03-13 stsp return err;
1075 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1076 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1077 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1078 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1080 22484865 2018-03-13 stsp base_file = got_opentemp();
1081 8628c62d 2018-03-15 stsp if (base_file == NULL)
1082 8628c62d 2018-03-15 stsp return got_error_from_errno();
1084 22484865 2018-03-13 stsp accum_file = got_opentemp();
1085 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1086 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1087 8628c62d 2018-03-15 stsp fclose(base_file);
1088 8628c62d 2018-03-15 stsp return err;
1092 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1093 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1094 a6b158cc 2018-02-11 stsp if (n == 0) {
1095 72eb3431 2018-04-01 stsp struct got_pack *pack;
1096 8628c62d 2018-03-15 stsp size_t base_len;
1097 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1099 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1100 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1101 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1102 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1103 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1104 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1105 72eb3431 2018-04-01 stsp goto done;
1108 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1109 72eb3431 2018-04-01 stsp if (pack == NULL) {
1110 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1111 a6b158cc 2018-02-11 stsp goto done;
1114 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1115 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1116 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1117 0c048b15 2018-04-27 stsp goto done;
1119 49847196 2018-06-22 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1121 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
1122 bdd2fbb3 2018-02-11 stsp goto done;
1124 8628c62d 2018-03-15 stsp if (base_file)
1125 8b2180d4 2018-04-26 stsp err = got_inflate_to_file_fd(&base_len,
1126 8b2180d4 2018-04-26 stsp pack->fd, base_file);
1128 0b48ab23 2018-06-04 stsp err = got_inflate_to_mem_fd(&base_buf,
1129 0b48ab23 2018-06-04 stsp &base_len, pack->fd);
1130 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1131 8628c62d 2018-03-15 stsp uint8_t *p;
1132 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1133 8628c62d 2018-03-15 stsp if (p == NULL) {
1134 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1135 8628c62d 2018-03-15 stsp goto done;
1137 8628c62d 2018-03-15 stsp base_buf = p;
1141 a6b158cc 2018-02-11 stsp goto done;
1143 8628c62d 2018-03-15 stsp if (base_file)
1144 8628c62d 2018-03-15 stsp rewind(base_file);
1148 8628c62d 2018-03-15 stsp if (base_buf) {
1149 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1150 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1153 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1154 a53d2f13 2018-03-17 stsp delta->delta_len,
1155 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1156 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1157 b29656e2 2018-03-16 stsp &accum_size);
1160 6691714a 2018-01-23 stsp goto done;
1162 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1163 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1164 8628c62d 2018-03-15 stsp if (base_buf) {
1165 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1166 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1167 8628c62d 2018-03-15 stsp base_buf = tmp;
1169 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1170 8628c62d 2018-03-15 stsp accum_file = base_file;
1171 8628c62d 2018-03-15 stsp base_file = tmp;
1172 8628c62d 2018-03-15 stsp rewind(base_file);
1173 8628c62d 2018-03-15 stsp rewind(accum_file);
1179 8628c62d 2018-03-15 stsp free(base_buf);
1180 8628c62d 2018-03-15 stsp if (accum_buf) {
1181 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1182 8628c62d 2018-03-15 stsp free(accum_buf);
1183 8628c62d 2018-03-15 stsp if (len != accum_size)
1184 8628c62d 2018-03-15 stsp return got_ferror(outfile, GOT_ERR_IO);
1186 8628c62d 2018-03-15 stsp if (base_file)
1187 8628c62d 2018-03-15 stsp fclose(base_file);
1188 8628c62d 2018-03-15 stsp if (accum_file)
1189 8628c62d 2018-03-15 stsp fclose(accum_file);
1190 6691714a 2018-01-23 stsp rewind(outfile);
1191 b29656e2 2018-03-16 stsp if (err == NULL)
1192 b29656e2 2018-03-16 stsp *result_size = accum_size;
1193 e0ab43e7 2018-03-16 stsp return err;
1196 e0ab43e7 2018-03-16 stsp static const struct got_error *
1197 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1198 72eb3431 2018-04-01 stsp struct got_delta_chain *deltas, struct got_repository *repo)
1200 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1201 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1202 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1203 e0ab43e7 2018-03-16 stsp size_t accum_size;
1204 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1205 e0ab43e7 2018-03-16 stsp int n = 0;
1207 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1208 e0ab43e7 2018-03-16 stsp *outlen = 0;
1210 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1211 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1213 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1215 e0ab43e7 2018-03-16 stsp return err;
1216 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1217 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1218 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1220 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1221 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1222 e0ab43e7 2018-03-16 stsp if (n == 0) {
1223 72eb3431 2018-04-01 stsp struct got_pack *pack;
1224 e0ab43e7 2018-03-16 stsp size_t base_len;
1225 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1227 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1228 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1229 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1230 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1231 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1232 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1233 72eb3431 2018-04-01 stsp goto done;
1236 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1237 72eb3431 2018-04-01 stsp if (pack == NULL) {
1238 e0ab43e7 2018-03-16 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1239 e0ab43e7 2018-03-16 stsp goto done;
1242 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1243 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1244 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1245 0c048b15 2018-04-27 stsp goto done;
1247 49847196 2018-06-22 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1249 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1250 e0ab43e7 2018-03-16 stsp goto done;
1252 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(&base_buf, &base_len,
1253 8b2180d4 2018-04-26 stsp pack->fd);
1254 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1255 e0ab43e7 2018-03-16 stsp uint8_t *p;
1256 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1257 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1258 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1259 e0ab43e7 2018-03-16 stsp goto done;
1261 e0ab43e7 2018-03-16 stsp base_buf = p;
1264 e0ab43e7 2018-03-16 stsp goto done;
1269 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1270 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1273 e0ab43e7 2018-03-16 stsp goto done;
1275 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1276 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1277 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1278 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1279 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1284 e0ab43e7 2018-03-16 stsp free(base_buf);
1285 e0ab43e7 2018-03-16 stsp if (err) {
1286 e0ab43e7 2018-03-16 stsp free(accum_buf);
1287 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1288 e0ab43e7 2018-03-16 stsp *outlen = 0;
1290 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1291 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1293 efd2a263 2018-01-19 stsp return err;
1296 3ee5fc21 2018-01-17 stsp const struct got_error *
1297 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1298 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1300 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1302 8b2180d4 2018-04-26 stsp *f = NULL;
1304 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1305 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1307 040bf4a1 2018-04-01 stsp *f = got_opentemp();
1308 040bf4a1 2018-04-01 stsp if (*f == NULL) {
1309 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1310 040bf4a1 2018-04-01 stsp goto done;
1313 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1314 72eb3431 2018-04-01 stsp struct got_pack *pack;
1316 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1317 72eb3431 2018-04-01 stsp if (pack == NULL) {
1318 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1320 72eb3431 2018-04-01 stsp goto done;
1323 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1324 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1325 0c048b15 2018-04-27 stsp goto done;
1327 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1328 6691714a 2018-01-23 stsp err = got_error_from_errno();
1329 c52ac529 2018-03-17 stsp goto done;
1332 8b2180d4 2018-04-26 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1334 8b2180d4 2018-04-26 stsp err = dump_delta_chain_to_file(&obj->size,
1335 8b2180d4 2018-04-26 stsp &obj->deltas, *f, repo);
1337 d0f3be7c 2018-03-17 stsp if (err && *f) {
1338 3ee5fc21 2018-01-17 stsp fclose(*f);
1339 d0f3be7c 2018-03-17 stsp *f = NULL;
1341 a1fd68d8 2018-01-12 stsp return err;
1344 e0ab43e7 2018-03-16 stsp const struct got_error *
1345 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1346 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1348 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1350 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1351 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1353 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1354 72eb3431 2018-04-01 stsp struct got_pack *pack;
1356 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1357 72eb3431 2018-04-01 stsp if (pack == NULL) {
1358 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1360 72eb3431 2018-04-01 stsp goto done;
1363 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1364 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1365 0c048b15 2018-04-27 stsp goto done;
1367 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1368 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1369 e0ab43e7 2018-03-16 stsp goto done;
1372 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(buf, len, pack->fd);
1374 72eb3431 2018-04-01 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, repo);
1376 e0ab43e7 2018-03-16 stsp return err;