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>
21 a1fd68d8 2018-01-12 stsp #include <dirent.h>
22 7e656b93 2018-03-17 stsp #include <fcntl.h>
23 a1fd68d8 2018-01-12 stsp #include <errno.h>
24 0a0a3048 2018-01-10 stsp #include <stdio.h>
25 a1fd68d8 2018-01-12 stsp #include <stdint.h>
26 0a0a3048 2018-01-10 stsp #include <stdlib.h>
27 0a0a3048 2018-01-10 stsp #include <string.h>
28 0a0a3048 2018-01-10 stsp #include <limits.h>
29 0a0a3048 2018-01-10 stsp #include <sha1.h>
30 0a0a3048 2018-01-10 stsp #include <endian.h>
31 a1fd68d8 2018-01-12 stsp #include <zlib.h>
33 0a0a3048 2018-01-10 stsp #include "got_error.h"
34 a1fd68d8 2018-01-12 stsp #include "got_object.h"
35 a1fd68d8 2018-01-12 stsp #include "got_repository.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
38 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
45 79b11c62 2018-03-09 stsp #ifndef nitems
46 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
50 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
51 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
52 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
53 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
54 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
55 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
56 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
57 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
60 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 0a0a3048 2018-01-10 stsp static const struct got_error *
64 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
68 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
69 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
70 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
73 0a0a3048 2018-01-10 stsp return NULL;
76 24541888 2018-01-10 stsp static const struct got_error *
77 1c7e24f1 2018-04-02 stsp get_packfile_size(size_t *size, const char *path)
79 0a0a3048 2018-01-10 stsp struct stat sb;
84 1c7e24f1 2018-04-02 stsp dot = strrchr(path, '.');
85 0a0a3048 2018-01-10 stsp if (dot == NULL)
86 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
88 1c7e24f1 2018-04-02 stsp /* Path must point to a pack index or to a pack file. */
89 1c7e24f1 2018-04-02 stsp if (strcmp(dot, ".idx") == 0) {
90 97128b57 2018-04-02 stsp const struct got_error *err = NULL;
91 1c7e24f1 2018-04-02 stsp char *path_pack;
92 1c7e24f1 2018-04-02 stsp char base_path[PATH_MAX];
94 1c7e24f1 2018-04-02 stsp /* Convert pack index path to pack file path. */
95 1c7e24f1 2018-04-02 stsp if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
96 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_NO_SPACE);
97 1c7e24f1 2018-04-02 stsp dot = strrchr(base_path, '.');
98 1c7e24f1 2018-04-02 stsp if (dot == NULL)
99 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
100 1c7e24f1 2018-04-02 stsp *dot = '\0';
101 1c7e24f1 2018-04-02 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
102 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
104 97128b57 2018-04-02 stsp if (stat(path_pack, &sb) != 0)
105 97128b57 2018-04-02 stsp err = got_error_from_errno();
106 0a0a3048 2018-01-10 stsp free(path_pack);
108 97128b57 2018-04-02 stsp return err;
109 1c7e24f1 2018-04-02 stsp } else if (strcmp(dot, ".pack") == 0) {
110 1c7e24f1 2018-04-02 stsp if (stat(path, &sb) != 0)
111 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
113 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
115 0a0a3048 2018-01-10 stsp *size = sb.st_size;
119 0a0a3048 2018-01-10 stsp const struct got_error *
120 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
122 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
124 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
125 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
126 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
127 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
129 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
131 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
132 0a0a3048 2018-01-10 stsp if (f == NULL)
133 9feb4ff2 2018-03-14 stsp return got_error_from_errno();
135 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
137 0a0a3048 2018-01-10 stsp return err;
139 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
140 0a0a3048 2018-01-10 stsp if (p == NULL) {
141 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
145 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
146 0a0a3048 2018-01-10 stsp if (n != 1) {
147 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
151 0a0a3048 2018-01-10 stsp if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
152 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
156 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
158 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
159 0a0a3048 2018-01-10 stsp if (n != 1) {
160 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
164 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
165 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
169 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
171 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
172 0a0a3048 2018-01-10 stsp if (n != 1) {
173 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
177 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
181 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
183 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
185 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
186 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
187 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
191 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
192 0a0a3048 2018-01-10 stsp if (n != nobj) {
193 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
197 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
198 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
200 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
201 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
202 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
206 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
207 0a0a3048 2018-01-10 stsp if (n != nobj) {
208 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
212 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
214 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
215 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
216 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
220 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
221 0a0a3048 2018-01-10 stsp if (n != nobj) {
222 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
226 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
228 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
229 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
230 0a0a3048 2018-01-10 stsp goto checksum;
232 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
233 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
234 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
238 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
239 0a0a3048 2018-01-10 stsp if (n != nobj) {
240 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
244 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
245 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
248 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
249 0a0a3048 2018-01-10 stsp if (n != 1) {
250 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
254 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
255 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
256 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
257 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
261 0a0a3048 2018-01-10 stsp got_packidx_close(p);
263 0a0a3048 2018-01-10 stsp *packidx = p;
264 0a0a3048 2018-01-10 stsp return err;
268 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
270 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
271 0a0a3048 2018-01-10 stsp free(packidx->offsets);
272 0a0a3048 2018-01-10 stsp free(packidx->crc32);
273 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
274 0a0a3048 2018-01-10 stsp free(packidx);
278 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
280 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
283 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
286 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
287 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
293 a1fd68d8 2018-01-12 stsp static off_t
294 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
296 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
297 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
298 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
299 a1fd68d8 2018-01-12 stsp uint64_t loffset;
300 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
301 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
303 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
304 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
306 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
309 72eb3431 2018-04-01 stsp static const struct got_error *
310 72eb3431 2018-04-01 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
311 72eb3431 2018-04-01 stsp struct got_packidx_v2_hdr *packidx);
314 72eb3431 2018-04-01 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id, struct got_repository *repo)
316 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
317 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
320 a1fd68d8 2018-01-12 stsp if (id0 > 0)
321 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
323 a1fd68d8 2018-01-12 stsp while (i < totobj) {
324 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
325 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
327 16dcbf91 2018-04-01 stsp if (cmp == 0)
335 79b11c62 2018-03-09 stsp static struct got_packidx_v2_hdr *
336 79b11c62 2018-03-09 stsp dup_packidx(struct got_packidx_v2_hdr *packidx)
338 79b11c62 2018-03-09 stsp struct got_packidx_v2_hdr *p;
339 79b11c62 2018-03-09 stsp size_t nobj;
341 79b11c62 2018-03-09 stsp p = calloc(1, sizeof(*p));
342 79b11c62 2018-03-09 stsp if (p == NULL)
343 79b11c62 2018-03-09 stsp return NULL;
345 79b11c62 2018-03-09 stsp memcpy(p, packidx, sizeof(*p));
346 79b11c62 2018-03-09 stsp p->sorted_ids = NULL;
347 79b11c62 2018-03-09 stsp p->crc32 = NULL;
348 79b11c62 2018-03-09 stsp p->offsets = NULL;
349 79b11c62 2018-03-09 stsp p->large_offsets = NULL;
351 79b11c62 2018-03-09 stsp nobj = betoh32(p->fanout_table[0xff]);
353 79b11c62 2018-03-09 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
354 79b11c62 2018-03-09 stsp if (p->sorted_ids == NULL)
356 dfb54902 2018-04-02 stsp memcpy(p->sorted_ids, packidx->sorted_ids,
357 dfb54902 2018-04-02 stsp nobj * sizeof(*p->sorted_ids));
359 79b11c62 2018-03-09 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
360 79b11c62 2018-03-09 stsp if (p->crc32 == NULL)
362 79b11c62 2018-03-09 stsp memcpy(p->crc32, packidx->crc32, nobj * sizeof(*p->crc32));
364 79b11c62 2018-03-09 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
365 79b11c62 2018-03-09 stsp if (p->offsets == NULL)
367 79b11c62 2018-03-09 stsp memcpy(p->offsets, packidx->offsets, nobj * sizeof(*p->offsets));
369 79b11c62 2018-03-09 stsp if (p->large_offsets) {
370 79b11c62 2018-03-09 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
371 79b11c62 2018-03-09 stsp if (p->large_offsets == NULL)
373 79b11c62 2018-03-09 stsp memcpy(p->large_offsets, packidx->large_offsets,
374 79b11c62 2018-03-09 stsp nobj * sizeof(*p->large_offsets));
380 79b11c62 2018-03-09 stsp free(p->large_offsets);
381 79b11c62 2018-03-09 stsp free(p->offsets);
382 79b11c62 2018-03-09 stsp free(p->crc32);
383 79b11c62 2018-03-09 stsp free(p->sorted_ids);
385 79b11c62 2018-03-09 stsp return NULL;
388 65cf1e80 2018-03-16 stsp static void
389 65cf1e80 2018-03-16 stsp cache_packidx(struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
393 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
394 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
398 65cf1e80 2018-03-16 stsp if (i == nitems(repo->packidx_cache)) {
399 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i - 1]);
400 65cf1e80 2018-03-16 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
401 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache) -
402 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache[0]));
406 65cf1e80 2018-03-16 stsp repo->packidx_cache[i] = dup_packidx(packidx);
409 6b9c9673 2018-01-23 stsp static const struct got_error *
410 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
411 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
413 6b9c9673 2018-01-23 stsp const struct got_error *err;
414 6b9c9673 2018-01-23 stsp char *path_packdir;
415 6b9c9673 2018-01-23 stsp DIR *packdir;
416 6b9c9673 2018-01-23 stsp struct dirent *dent;
417 6b9c9673 2018-01-23 stsp char *path_packidx;
420 65cf1e80 2018-03-16 stsp /* Search pack index cache. */
421 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
422 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
424 72eb3431 2018-04-01 stsp *idx = get_object_idx(repo->packidx_cache[i], id, repo);
425 79b11c62 2018-03-09 stsp if (*idx != -1) {
426 6bb255dc 2018-03-17 stsp *packidx = repo->packidx_cache[i];
427 79b11c62 2018-03-09 stsp return NULL;
430 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
432 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
433 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
434 0a585a0d 2018-03-17 stsp return got_error_from_errno();
436 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
437 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
438 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
442 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
443 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
446 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
447 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
448 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
452 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
453 6b9c9673 2018-01-23 stsp free(path_packidx);
457 72eb3431 2018-04-01 stsp *idx = get_object_idx(*packidx, id, repo);
458 6b9c9673 2018-01-23 stsp if (*idx != -1) {
459 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
460 65cf1e80 2018-03-16 stsp cache_packidx(*packidx, repo);
464 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
465 6b9c9673 2018-01-23 stsp *packidx = NULL;
468 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
470 6b9c9673 2018-01-23 stsp free(path_packdir);
471 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
472 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
473 6b9c9673 2018-01-23 stsp return err;
476 c7fe698a 2018-01-23 stsp static const struct got_error *
477 65cf1e80 2018-03-16 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
478 65cf1e80 2018-03-16 stsp struct got_packidx_v2_hdr *packidx)
480 65cf1e80 2018-03-16 stsp char *path_packdir;
481 65cf1e80 2018-03-16 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
482 65cf1e80 2018-03-16 stsp char *sha1str;
484 65cf1e80 2018-03-16 stsp path_packdir = got_repo_get_path_objects_pack(repo);
485 65cf1e80 2018-03-16 stsp if (path_packdir == NULL)
486 0a585a0d 2018-03-17 stsp return got_error_from_errno();
488 65cf1e80 2018-03-16 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
489 65cf1e80 2018-03-16 stsp hex, sizeof(hex));
490 65cf1e80 2018-03-16 stsp if (sha1str == NULL)
491 65cf1e80 2018-03-16 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
493 65cf1e80 2018-03-16 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
494 65cf1e80 2018-03-16 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
495 65cf1e80 2018-03-16 stsp *path_packfile = NULL;
496 0a585a0d 2018-03-17 stsp return got_error_from_errno();
499 65cf1e80 2018-03-16 stsp return NULL;
502 65cf1e80 2018-03-16 stsp const struct got_error *
503 8b2180d4 2018-04-26 stsp read_packfile_hdr(int fd, struct got_packidx_v2_hdr *packidx)
505 65cf1e80 2018-03-16 stsp const struct got_error *err = NULL;
506 65cf1e80 2018-03-16 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
507 65cf1e80 2018-03-16 stsp struct got_packfile_hdr hdr;
510 8b2180d4 2018-04-26 stsp n = read(fd, &hdr, sizeof(hdr));
512 8b2180d4 2018-04-26 stsp return got_error_from_errno();
513 8b2180d4 2018-04-26 stsp if (n != sizeof(hdr))
514 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
516 65cf1e80 2018-03-16 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
517 65cf1e80 2018-03-16 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
518 65cf1e80 2018-03-16 stsp betoh32(hdr.nobjects) != totobj)
519 65cf1e80 2018-03-16 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
521 65cf1e80 2018-03-16 stsp return err;
524 7e656b93 2018-03-17 stsp static const struct got_error *
525 8b2180d4 2018-04-26 stsp open_packfile(int *fd, const char *path_packfile,
526 7e656b93 2018-03-17 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
528 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
530 8b2180d4 2018-04-26 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
531 8b2180d4 2018-04-26 stsp if (*fd == -1)
532 8b2180d4 2018-04-26 stsp return got_error_from_errno();
534 7e656b93 2018-03-17 stsp if (packidx) {
535 8b2180d4 2018-04-26 stsp err = read_packfile_hdr(*fd, packidx);
537 8b2180d4 2018-04-26 stsp close(*fd);
541 7e656b93 2018-03-17 stsp return err;
545 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
547 8b2180d4 2018-04-26 stsp close(pack->fd);
548 8b2180d4 2018-04-26 stsp pack->fd = -1;
549 4589e373 2018-03-17 stsp free(pack->path_packfile);
550 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
551 4589e373 2018-03-17 stsp pack->filesize = 0;
554 7e656b93 2018-03-17 stsp static const struct got_error *
555 7e656b93 2018-03-17 stsp cache_pack(struct got_pack **packp, const char *path_packfile,
556 7e656b93 2018-03-17 stsp struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
558 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
559 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
563 7e656b93 2018-03-17 stsp *packp = NULL;
565 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
566 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
567 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
569 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
570 7e656b93 2018-03-17 stsp return NULL;
573 7e656b93 2018-03-17 stsp if (i == nitems(repo->packs) - 1) {
574 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i - 1]);
575 7e656b93 2018-03-17 stsp memmove(&repo->packs[1], &repo->packs[0],
576 7e656b93 2018-03-17 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
580 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
582 7e656b93 2018-03-17 stsp pack->path_packfile = strdup(path_packfile);
583 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL) {
584 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
588 8b2180d4 2018-04-26 stsp err = open_packfile(&pack->fd, path_packfile, repo, packidx);
592 7e656b93 2018-03-17 stsp err = get_packfile_size(&pack->filesize, path_packfile);
596 7e656b93 2018-03-17 stsp free(pack->path_packfile);
597 7e656b93 2018-03-17 stsp free(pack);
598 7e656b93 2018-03-17 stsp } else if (packp)
599 7e656b93 2018-03-17 stsp *packp = pack;
600 c7fe698a 2018-01-23 stsp return err;
603 7e656b93 2018-03-17 stsp struct got_pack *
604 7e656b93 2018-03-17 stsp get_cached_pack(const char *path_packfile, struct got_repository *repo)
606 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
609 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
610 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
611 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
613 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
614 7e656b93 2018-03-17 stsp return pack;
617 7e656b93 2018-03-17 stsp return NULL;
620 c7fe698a 2018-01-23 stsp static const struct got_error *
621 8b2180d4 2018-04-26 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len, int fd)
623 a487c1d0 2018-01-14 stsp uint8_t t = 0;
624 a487c1d0 2018-01-14 stsp uint64_t s = 0;
625 a487c1d0 2018-01-14 stsp uint8_t sizeN;
630 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
632 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
634 8b2180d4 2018-04-26 stsp n = read(fd, &sizeN, sizeof(sizeN));
636 8b2180d4 2018-04-26 stsp return got_error_from_errno();
637 8b2180d4 2018-04-26 stsp if (n != sizeof(sizeN))
638 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
640 a1fd68d8 2018-01-12 stsp if (i == 0) {
641 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
642 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
643 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
645 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
646 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
649 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
653 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
654 a487c1d0 2018-01-14 stsp return NULL;
657 a1fd68d8 2018-01-12 stsp static const struct got_error *
658 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
659 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
661 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
662 6ccb713b 2018-01-19 stsp if (*obj == NULL)
663 0a585a0d 2018-03-17 stsp return got_error_from_errno();
665 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
666 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
667 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
668 6ccb713b 2018-01-19 stsp free(*obj);
669 6ccb713b 2018-01-19 stsp *obj = NULL;
670 0a585a0d 2018-03-17 stsp return err;
673 6ccb713b 2018-01-19 stsp (*obj)->type = type;
674 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
675 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
676 6ccb713b 2018-01-19 stsp (*obj)->size = size;
677 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
678 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
680 b107e67f 2018-01-19 stsp return NULL;
683 b107e67f 2018-01-19 stsp static const struct got_error *
684 8b2180d4 2018-04-26 stsp parse_negative_offset(int64_t *offset, size_t *len, int fd)
686 b107e67f 2018-01-19 stsp int64_t o = 0;
687 b107e67f 2018-01-19 stsp uint8_t offN;
692 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
694 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
696 8b2180d4 2018-04-26 stsp n = read(fd, &offN, sizeof(offN));
698 8b2180d4 2018-04-26 stsp return got_error_from_errno();
699 8b2180d4 2018-04-26 stsp if (n != sizeof(offN))
700 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
702 b107e67f 2018-01-19 stsp if (i == 0)
703 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
707 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
710 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
712 b107e67f 2018-01-19 stsp *offset = o;
713 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
714 6ccb713b 2018-01-19 stsp return NULL;
717 6ccb713b 2018-01-19 stsp static const struct got_error *
718 8b2180d4 2018-04-26 stsp parse_offset_delta(off_t *base_offset, int fd, off_t offset)
720 96f5e8b3 2018-01-23 stsp const struct got_error *err;
721 b107e67f 2018-01-19 stsp int64_t negoffset;
722 b107e67f 2018-01-19 stsp size_t negofflen;
724 8b2180d4 2018-04-26 stsp err = parse_negative_offset(&negoffset, &negofflen, fd);
726 b107e67f 2018-01-19 stsp return err;
728 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
729 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
730 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
731 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
733 96f5e8b3 2018-01-23 stsp return NULL;
736 0e22967e 2018-02-11 stsp static const struct got_error *
737 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
738 8b2180d4 2018-04-26 stsp int, const char *, off_t, size_t, int, size_t, unsigned int);
740 96f5e8b3 2018-01-23 stsp static const struct got_error *
741 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
742 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
743 a53d2f13 2018-03-17 stsp size_t delta_data_offset, uint8_t *delta_buf, size_t delta_len)
745 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
747 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
748 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf,
749 a53d2f13 2018-03-17 stsp delta_len);
750 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
751 0a585a0d 2018-03-17 stsp return got_error_from_errno();
752 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
753 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
754 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
755 bdd2fbb3 2018-02-11 stsp return NULL;
758 bdd2fbb3 2018-02-11 stsp static const struct got_error *
759 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
760 8b2180d4 2018-04-26 stsp struct got_repository *repo, int fd, const char *path_packfile,
761 5b7e13a7 2018-04-02 stsp off_t delta_offset,size_t tslen, int delta_type, size_t delta_size,
762 5b7e13a7 2018-04-02 stsp unsigned int recursion)
765 a3500804 2018-01-23 stsp const struct got_error *err;
766 c3703302 2018-01-23 stsp off_t base_offset;
767 c3703302 2018-01-23 stsp uint8_t base_type;
768 c3703302 2018-01-23 stsp uint64_t base_size;
769 c3703302 2018-01-23 stsp size_t base_tslen;
770 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
771 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
772 a53d2f13 2018-03-17 stsp size_t delta_len;
774 8b2180d4 2018-04-26 stsp err = parse_offset_delta(&base_offset, fd, delta_offset);
776 a3500804 2018-01-23 stsp return err;
778 8b2180d4 2018-04-26 stsp delta_data_offset = lseek(fd, 0, SEEK_CUR);
779 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
780 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
782 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, fd);
784 a53d2f13 2018-03-17 stsp return err;
786 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
787 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
789 bdd2fbb3 2018-02-11 stsp return err;
791 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
792 8b2180d4 2018-04-26 stsp if (lseek(fd, base_offset, SEEK_SET) == -1)
793 b107e67f 2018-01-19 stsp return got_error_from_errno();
795 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
798 b107e67f 2018-01-19 stsp return err;
800 8b2180d4 2018-04-26 stsp return resolve_delta_chain(deltas, repo, fd, path_packfile,
801 5b7e13a7 2018-04-02 stsp base_offset, base_tslen, base_type, base_size, recursion - 1);
804 c3703302 2018-01-23 stsp static const struct got_error *
805 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
806 8b2180d4 2018-04-26 stsp int fd, const char *path_packfile, off_t delta_offset,
807 5b7e13a7 2018-04-02 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
809 6b9c9673 2018-01-23 stsp const struct got_error *err;
810 6b9c9673 2018-01-23 stsp struct got_object_id id;
811 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
813 6b9c9673 2018-01-23 stsp off_t base_offset;
814 6b9c9673 2018-01-23 stsp uint8_t base_type;
815 6b9c9673 2018-01-23 stsp uint64_t base_size;
816 6b9c9673 2018-01-23 stsp size_t base_tslen;
818 65cf1e80 2018-03-16 stsp char *path_base_packfile;
819 999f19f6 2018-03-17 stsp struct got_pack *base_pack;
820 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
821 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
822 a53d2f13 2018-03-17 stsp size_t delta_len;
824 8b2180d4 2018-04-26 stsp n = read(fd, &id, sizeof(id));
826 8b2180d4 2018-04-26 stsp return got_error_from_errno();
827 8b2180d4 2018-04-26 stsp if (n != sizeof(id))
828 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
830 8b2180d4 2018-04-26 stsp delta_data_offset = lseek(fd, 0, SEEK_CUR);
831 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
832 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
834 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, fd);
836 a53d2f13 2018-03-17 stsp return err;
838 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
839 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
841 bdd2fbb3 2018-02-11 stsp return err;
843 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
845 6b9c9673 2018-01-23 stsp return err;
847 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
848 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
849 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
852 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_base_packfile, repo, packidx);
854 7e656b93 2018-03-17 stsp return err;
856 999f19f6 2018-03-17 stsp base_pack = get_cached_pack(path_base_packfile, repo);
857 999f19f6 2018-03-17 stsp if (base_pack == NULL) {
858 999f19f6 2018-03-17 stsp err = cache_pack(&base_pack, path_base_packfile, NULL, repo);
863 8b2180d4 2018-04-26 stsp if (lseek(base_pack->fd, base_offset, SEEK_SET) == -1) {
864 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
868 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
869 8b2180d4 2018-04-26 stsp base_pack->fd);
873 8b2180d4 2018-04-26 stsp err = resolve_delta_chain(deltas, repo, base_pack->fd,
874 0e22967e 2018-02-11 stsp path_base_packfile, base_offset, base_tslen, base_type,
875 5b7e13a7 2018-04-02 stsp base_size, recursion - 1);
877 65cf1e80 2018-03-16 stsp free(path_base_packfile);
878 6b9c9673 2018-01-23 stsp return err;
881 6b9c9673 2018-01-23 stsp static const struct got_error *
882 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
883 8b2180d4 2018-04-26 stsp int fd, const char *path_packfile, off_t delta_offset, size_t tslen,
884 5b7e13a7 2018-04-02 stsp int delta_type, size_t delta_size, unsigned int recursion)
886 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
888 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
889 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
891 c3703302 2018-01-23 stsp switch (delta_type) {
892 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
893 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
894 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
895 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
896 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
897 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
898 a53d2f13 2018-03-17 stsp delta_type, delta_size, 0, NULL, 0);
900 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
901 8b2180d4 2018-04-26 stsp err = resolve_offset_delta(deltas, repo, fd, path_packfile,
902 8b2180d4 2018-04-26 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
904 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
905 8b2180d4 2018-04-26 stsp err = resolve_ref_delta(deltas, repo, fd, path_packfile,
906 8b2180d4 2018-04-26 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
909 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
912 96f5e8b3 2018-01-23 stsp return err;
915 96f5e8b3 2018-01-23 stsp static const struct got_error *
916 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
917 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
918 8b2180d4 2018-04-26 stsp int fd, struct got_object_id *id, off_t offset, size_t tslen,
919 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
921 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
922 96f5e8b3 2018-01-23 stsp int resolved_type;
924 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
925 b107e67f 2018-01-19 stsp if (*obj == NULL)
926 0a585a0d 2018-03-17 stsp return got_error_from_errno();
928 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
929 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
930 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
931 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
932 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
934 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
935 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
936 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
939 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
941 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
942 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
944 8b2180d4 2018-04-26 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, fd,
945 5b7e13a7 2018-04-02 stsp path_packfile, offset, tslen, delta_type, delta_size,
946 5b7e13a7 2018-04-02 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
950 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
953 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
957 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
958 96f5e8b3 2018-01-23 stsp *obj = NULL;
960 96f5e8b3 2018-01-23 stsp return err;
963 b107e67f 2018-01-19 stsp static const struct got_error *
964 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
965 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
967 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
968 a1fd68d8 2018-01-12 stsp off_t offset;
969 65cf1e80 2018-03-16 stsp char *path_packfile;
970 6d89869a 2018-03-17 stsp struct got_pack *pack;
971 6c00b545 2018-01-17 stsp uint8_t type;
972 6c00b545 2018-01-17 stsp uint64_t size;
973 3ee5fc21 2018-01-17 stsp size_t tslen;
975 6c00b545 2018-01-17 stsp *obj = NULL;
977 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
978 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
979 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
981 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_packfile, repo, packidx);
983 6b9c9673 2018-01-23 stsp return err;
985 6d89869a 2018-03-17 stsp pack = get_cached_pack(path_packfile, repo);
986 6d89869a 2018-03-17 stsp if (pack == NULL) {
987 6d89869a 2018-03-17 stsp err = cache_pack(&pack, path_packfile, packidx, repo);
992 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1) {
993 6c00b545 2018-01-17 stsp err = got_error_from_errno();
997 8b2180d4 2018-04-26 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack->fd);
1001 6c00b545 2018-01-17 stsp switch (type) {
1002 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
1003 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
1004 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
1005 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
1006 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
1007 6ccb713b 2018-01-19 stsp offset + tslen, size);
1010 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1011 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1012 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
1013 8b2180d4 2018-04-26 stsp pack->fd, id, offset, tslen, type, size);
1017 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1018 6c00b545 2018-01-17 stsp goto done;
1021 65cf1e80 2018-03-16 stsp free(path_packfile);
1022 a1fd68d8 2018-01-12 stsp return err;
1025 a1fd68d8 2018-01-12 stsp const struct got_error *
1026 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1027 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
1029 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1030 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
1033 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
1035 6b9c9673 2018-01-23 stsp return err;
1037 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
1039 7e656b93 2018-03-17 stsp return err;
1041 7e656b93 2018-03-17 stsp err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1042 3ee5fc21 2018-01-17 stsp return err;
1045 efd2a263 2018-01-19 stsp static const struct got_error *
1046 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1048 22484865 2018-03-13 stsp struct got_delta *delta;
1049 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1051 22484865 2018-03-13 stsp *max_size = 0;
1052 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1053 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1054 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1055 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1056 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1057 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1058 22484865 2018-03-13 stsp const struct got_error *err;
1059 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1060 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
1062 22484865 2018-03-13 stsp return err;
1064 22484865 2018-03-13 stsp base_size = delta->size;
1065 22484865 2018-03-13 stsp if (base_size > *max_size)
1066 22484865 2018-03-13 stsp *max_size = base_size;
1067 22484865 2018-03-13 stsp if (result_size > *max_size)
1068 22484865 2018-03-13 stsp *max_size = result_size;
1071 9feb4ff2 2018-03-14 stsp return NULL;
1074 22484865 2018-03-13 stsp static const struct got_error *
1075 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1076 72eb3431 2018-04-01 stsp FILE *outfile, struct got_repository *repo)
1078 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1079 6691714a 2018-01-23 stsp struct got_delta *delta;
1080 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1081 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1082 b29656e2 2018-03-16 stsp size_t accum_size = 0;
1083 22484865 2018-03-13 stsp uint64_t max_size;
1084 6691714a 2018-01-23 stsp int n = 0;
1086 b29656e2 2018-03-16 stsp *result_size = 0;
1088 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1089 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1091 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1092 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1094 22484865 2018-03-13 stsp return err;
1095 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1096 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1097 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1098 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1100 22484865 2018-03-13 stsp base_file = got_opentemp();
1101 8628c62d 2018-03-15 stsp if (base_file == NULL)
1102 8628c62d 2018-03-15 stsp return got_error_from_errno();
1104 22484865 2018-03-13 stsp accum_file = got_opentemp();
1105 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1106 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1107 8628c62d 2018-03-15 stsp fclose(base_file);
1108 8628c62d 2018-03-15 stsp return err;
1112 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1113 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1114 a6b158cc 2018-02-11 stsp if (n == 0) {
1115 72eb3431 2018-04-01 stsp struct got_pack *pack;
1116 8628c62d 2018-03-15 stsp size_t base_len;
1118 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1119 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1120 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1121 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1122 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1123 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1124 72eb3431 2018-04-01 stsp goto done;
1127 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1128 72eb3431 2018-04-01 stsp if (pack == NULL) {
1129 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1130 a6b158cc 2018-02-11 stsp goto done;
1133 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, delta->offset + delta->tslen,
1134 8b2180d4 2018-04-26 stsp SEEK_SET) == -1) {
1135 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
1136 bdd2fbb3 2018-02-11 stsp goto done;
1138 8628c62d 2018-03-15 stsp if (base_file)
1139 8b2180d4 2018-04-26 stsp err = got_inflate_to_file_fd(&base_len,
1140 8b2180d4 2018-04-26 stsp pack->fd, base_file);
1142 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(&base_buf, &base_len,
1143 8b2180d4 2018-04-26 stsp pack->fd);
1144 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1145 8628c62d 2018-03-15 stsp uint8_t *p;
1146 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1147 8628c62d 2018-03-15 stsp if (p == NULL) {
1148 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1149 8628c62d 2018-03-15 stsp goto done;
1151 8628c62d 2018-03-15 stsp base_buf = p;
1155 a6b158cc 2018-02-11 stsp goto done;
1157 8628c62d 2018-03-15 stsp if (base_file)
1158 8628c62d 2018-03-15 stsp rewind(base_file);
1162 8628c62d 2018-03-15 stsp if (base_buf) {
1163 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1164 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1167 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1168 a53d2f13 2018-03-17 stsp delta->delta_len,
1169 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1170 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1171 b29656e2 2018-03-16 stsp &accum_size);
1174 6691714a 2018-01-23 stsp goto done;
1176 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1177 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1178 8628c62d 2018-03-15 stsp if (base_buf) {
1179 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1180 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1181 8628c62d 2018-03-15 stsp base_buf = tmp;
1183 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1184 8628c62d 2018-03-15 stsp accum_file = base_file;
1185 8628c62d 2018-03-15 stsp base_file = tmp;
1186 8628c62d 2018-03-15 stsp rewind(base_file);
1187 8628c62d 2018-03-15 stsp rewind(accum_file);
1193 8628c62d 2018-03-15 stsp free(base_buf);
1194 8628c62d 2018-03-15 stsp if (accum_buf) {
1195 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1196 8628c62d 2018-03-15 stsp free(accum_buf);
1197 8628c62d 2018-03-15 stsp if (len != accum_size)
1198 8628c62d 2018-03-15 stsp return got_ferror(outfile, GOT_ERR_IO);
1200 8628c62d 2018-03-15 stsp if (base_file)
1201 8628c62d 2018-03-15 stsp fclose(base_file);
1202 8628c62d 2018-03-15 stsp if (accum_file)
1203 8628c62d 2018-03-15 stsp fclose(accum_file);
1204 6691714a 2018-01-23 stsp rewind(outfile);
1205 b29656e2 2018-03-16 stsp if (err == NULL)
1206 b29656e2 2018-03-16 stsp *result_size = accum_size;
1207 e0ab43e7 2018-03-16 stsp return err;
1210 e0ab43e7 2018-03-16 stsp static const struct got_error *
1211 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1212 72eb3431 2018-04-01 stsp struct got_delta_chain *deltas, struct got_repository *repo)
1214 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1215 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1216 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1217 e0ab43e7 2018-03-16 stsp size_t accum_size;
1218 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1219 e0ab43e7 2018-03-16 stsp int n = 0;
1221 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1222 e0ab43e7 2018-03-16 stsp *outlen = 0;
1224 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1225 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1227 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1229 e0ab43e7 2018-03-16 stsp return err;
1230 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1231 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1232 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1234 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1235 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1236 e0ab43e7 2018-03-16 stsp if (n == 0) {
1237 72eb3431 2018-04-01 stsp struct got_pack *pack;
1238 e0ab43e7 2018-03-16 stsp size_t base_len;
1240 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1241 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1242 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1243 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1244 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1245 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1246 72eb3431 2018-04-01 stsp goto done;
1249 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1250 72eb3431 2018-04-01 stsp if (pack == NULL) {
1251 e0ab43e7 2018-03-16 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1252 e0ab43e7 2018-03-16 stsp goto done;
1255 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, delta->offset + delta->tslen,
1256 8b2180d4 2018-04-26 stsp SEEK_SET) == -1) {
1257 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1258 e0ab43e7 2018-03-16 stsp goto done;
1260 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(&base_buf, &base_len,
1261 8b2180d4 2018-04-26 stsp pack->fd);
1262 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1263 e0ab43e7 2018-03-16 stsp uint8_t *p;
1264 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1265 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1266 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1267 e0ab43e7 2018-03-16 stsp goto done;
1269 e0ab43e7 2018-03-16 stsp base_buf = p;
1272 e0ab43e7 2018-03-16 stsp goto done;
1277 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1278 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1281 e0ab43e7 2018-03-16 stsp goto done;
1283 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1284 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1285 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1286 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1287 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1292 e0ab43e7 2018-03-16 stsp free(base_buf);
1293 e0ab43e7 2018-03-16 stsp if (err) {
1294 e0ab43e7 2018-03-16 stsp free(accum_buf);
1295 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1296 e0ab43e7 2018-03-16 stsp *outlen = 0;
1298 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1299 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1301 efd2a263 2018-01-19 stsp return err;
1304 3ee5fc21 2018-01-17 stsp const struct got_error *
1305 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1306 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1308 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1310 8b2180d4 2018-04-26 stsp *f = NULL;
1312 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1313 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1315 040bf4a1 2018-04-01 stsp *f = got_opentemp();
1316 040bf4a1 2018-04-01 stsp if (*f == NULL) {
1317 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1318 040bf4a1 2018-04-01 stsp goto done;
1321 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1322 72eb3431 2018-04-01 stsp struct got_pack *pack;
1324 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1325 72eb3431 2018-04-01 stsp if (pack == NULL) {
1326 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1328 72eb3431 2018-04-01 stsp goto done;
1331 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1332 6691714a 2018-01-23 stsp err = got_error_from_errno();
1333 c52ac529 2018-03-17 stsp goto done;
1336 8b2180d4 2018-04-26 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1338 8b2180d4 2018-04-26 stsp err = dump_delta_chain_to_file(&obj->size,
1339 8b2180d4 2018-04-26 stsp &obj->deltas, *f, repo);
1341 d0f3be7c 2018-03-17 stsp if (err && *f) {
1342 3ee5fc21 2018-01-17 stsp fclose(*f);
1343 d0f3be7c 2018-03-17 stsp *f = NULL;
1345 a1fd68d8 2018-01-12 stsp return err;
1348 e0ab43e7 2018-03-16 stsp const struct got_error *
1349 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1350 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1352 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1354 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1355 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1357 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1358 72eb3431 2018-04-01 stsp struct got_pack *pack;
1360 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1361 72eb3431 2018-04-01 stsp if (pack == NULL) {
1362 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1364 72eb3431 2018-04-01 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;