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"
36 511a516b 2018-05-19 stsp #include "got_opentemp.h"
38 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
46 79b11c62 2018-03-09 stsp #ifndef nitems
47 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
51 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
52 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
53 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
54 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
55 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
56 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
57 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
58 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
61 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 0a0a3048 2018-01-10 stsp static const struct got_error *
65 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
69 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
70 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
71 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
74 0a0a3048 2018-01-10 stsp return NULL;
77 24541888 2018-01-10 stsp static const struct got_error *
78 1c7e24f1 2018-04-02 stsp get_packfile_size(size_t *size, const char *path)
80 0a0a3048 2018-01-10 stsp struct stat sb;
85 1c7e24f1 2018-04-02 stsp dot = strrchr(path, '.');
86 0a0a3048 2018-01-10 stsp if (dot == NULL)
87 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
89 1c7e24f1 2018-04-02 stsp /* Path must point to a pack index or to a pack file. */
90 72fb0363 2018-06-04 stsp if (strcmp(dot, GOT_PACKIDX_SUFFIX) == 0) {
91 97128b57 2018-04-02 stsp const struct got_error *err = NULL;
92 1c7e24f1 2018-04-02 stsp char *path_pack;
93 1c7e24f1 2018-04-02 stsp char base_path[PATH_MAX];
95 1c7e24f1 2018-04-02 stsp /* Convert pack index path to pack file path. */
96 1c7e24f1 2018-04-02 stsp if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
97 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_NO_SPACE);
98 1c7e24f1 2018-04-02 stsp dot = strrchr(base_path, '.');
99 1c7e24f1 2018-04-02 stsp if (dot == NULL)
100 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
101 1c7e24f1 2018-04-02 stsp *dot = '\0';
102 1c7e24f1 2018-04-02 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
103 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
105 97128b57 2018-04-02 stsp if (stat(path_pack, &sb) != 0)
106 97128b57 2018-04-02 stsp err = got_error_from_errno();
107 0a0a3048 2018-01-10 stsp free(path_pack);
109 97128b57 2018-04-02 stsp return err;
110 56cca8e5 2018-06-04 stsp } else if (strcmp(dot, GOT_PACKFILE_SUFFIX) == 0) {
111 1c7e24f1 2018-04-02 stsp if (stat(path, &sb) != 0)
112 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
114 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
116 0a0a3048 2018-01-10 stsp *size = sb.st_size;
120 0a0a3048 2018-01-10 stsp const struct got_error *
121 6fd11751 2018-06-04 stsp got_packidx_open(struct got_packidx **packidx, const char *path)
123 6fd11751 2018-06-04 stsp struct got_packidx *p;
124 c8262310 2018-06-04 stsp struct got_packidx_v2_hdr *h;
126 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
127 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
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 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
136 0a0a3048 2018-01-10 stsp if (f == NULL)
137 9feb4ff2 2018-03-14 stsp return got_error_from_errno();
139 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
141 0a0a3048 2018-01-10 stsp return err;
143 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
144 6fd11751 2018-06-04 stsp if (p == NULL)
145 6fd11751 2018-06-04 stsp return got_error_from_errno();
146 6fd11751 2018-06-04 stsp p->path_packidx = strdup(path);
147 6fd11751 2018-06-04 stsp if (p->path_packidx == NULL) {
148 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
149 6fd11751 2018-06-04 stsp free(p->path_packidx);
151 6fd11751 2018-06-04 stsp return err;
154 c8262310 2018-06-04 stsp h = &p->hdr;
155 c8262310 2018-06-04 stsp n = fread(&h->magic, sizeof(h->magic), 1, f);
156 0a0a3048 2018-01-10 stsp if (n != 1) {
157 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
161 c8262310 2018-06-04 stsp if (betoh32(h->magic) != GOT_PACKIDX_V2_MAGIC) {
162 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
166 c8262310 2018-06-04 stsp SHA1Update(&ctx, (uint8_t *)&h->magic, sizeof(h->magic));
168 c8262310 2018-06-04 stsp n = fread(&h->version, sizeof(h->version), 1, f);
169 0a0a3048 2018-01-10 stsp if (n != 1) {
170 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
174 c8262310 2018-06-04 stsp if (betoh32(h->version) != GOT_PACKIDX_VERSION) {
175 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
179 c8262310 2018-06-04 stsp SHA1Update(&ctx, (uint8_t *)&h->version, sizeof(h->version));
181 c8262310 2018-06-04 stsp n = fread(&h->fanout_table, sizeof(h->fanout_table), 1, f);
182 0a0a3048 2018-01-10 stsp if (n != 1) {
183 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
187 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
191 c8262310 2018-06-04 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, sizeof(h->fanout_table));
193 c8262310 2018-06-04 stsp nobj = betoh32(h->fanout_table[0xff]);
195 c8262310 2018-06-04 stsp h->sorted_ids = calloc(nobj, sizeof(*h->sorted_ids));
196 c8262310 2018-06-04 stsp if (h->sorted_ids == NULL) {
197 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
201 c8262310 2018-06-04 stsp n = fread(h->sorted_ids, sizeof(*h->sorted_ids), nobj, f);
202 0a0a3048 2018-01-10 stsp if (n != nobj) {
203 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
207 c8262310 2018-06-04 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids,
208 c8262310 2018-06-04 stsp nobj * sizeof(*h->sorted_ids));
210 c8262310 2018-06-04 stsp h->crc32 = calloc(nobj, sizeof(*h->crc32));
211 c8262310 2018-06-04 stsp if (h->crc32 == NULL) {
212 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
216 c8262310 2018-06-04 stsp n = fread(h->crc32, sizeof(*h->crc32), nobj, f);
217 0a0a3048 2018-01-10 stsp if (n != nobj) {
218 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
222 c8262310 2018-06-04 stsp SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
224 c8262310 2018-06-04 stsp h->offsets = calloc(nobj, sizeof(*h->offsets));
225 c8262310 2018-06-04 stsp if (h->offsets == NULL) {
226 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
230 c8262310 2018-06-04 stsp n = fread(h->offsets, sizeof(*h->offsets), nobj, f);
231 0a0a3048 2018-01-10 stsp if (n != nobj) {
232 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
236 c8262310 2018-06-04 stsp SHA1Update(&ctx, (uint8_t *)h->offsets, nobj * sizeof(*h->offsets));
238 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
239 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
240 0a0a3048 2018-01-10 stsp goto checksum;
242 c8262310 2018-06-04 stsp h->large_offsets = calloc(nobj, sizeof(*h->large_offsets));
243 c8262310 2018-06-04 stsp if (h->large_offsets == NULL) {
244 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
248 c8262310 2018-06-04 stsp n = fread(h->large_offsets, sizeof(*h->large_offsets), nobj, f);
249 0a0a3048 2018-01-10 stsp if (n != nobj) {
250 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
254 c8262310 2018-06-04 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
255 c8262310 2018-06-04 stsp nobj * sizeof(*h->large_offsets));
258 c8262310 2018-06-04 stsp n = fread(&h->trailer, sizeof(h->trailer), 1, f);
259 0a0a3048 2018-01-10 stsp if (n != 1) {
260 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
264 c8262310 2018-06-04 stsp SHA1Update(&ctx, h->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
265 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
266 c8262310 2018-06-04 stsp if (memcmp(h->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
267 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
271 0a0a3048 2018-01-10 stsp got_packidx_close(p);
273 0a0a3048 2018-01-10 stsp *packidx = p;
274 0a0a3048 2018-01-10 stsp return err;
278 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
280 6fd11751 2018-06-04 stsp free(packidx->hdr.sorted_ids);
281 6fd11751 2018-06-04 stsp free(packidx->hdr.offsets);
282 6fd11751 2018-06-04 stsp free(packidx->hdr.crc32);
283 6fd11751 2018-06-04 stsp free(packidx->hdr.large_offsets);
284 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
285 0a0a3048 2018-01-10 stsp free(packidx);
289 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
291 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
294 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
297 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
298 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
304 a1fd68d8 2018-01-12 stsp static off_t
305 6fd11751 2018-06-04 stsp get_object_offset(struct got_packidx *packidx, int idx)
307 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
308 6fd11751 2018-06-04 stsp uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
309 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
310 a1fd68d8 2018-01-12 stsp uint64_t loffset;
311 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
312 6fd11751 2018-06-04 stsp if (idx < 0 || idx > totobj ||
313 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
315 6fd11751 2018-06-04 stsp loffset = betoh64(packidx->hdr.large_offsets[idx]);
316 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
318 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
322 6fd11751 2018-06-04 stsp get_object_idx(struct got_packidx *packidx, struct got_object_id *id,
323 6fd11751 2018-06-04 stsp struct got_repository *repo)
325 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
326 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
329 a1fd68d8 2018-01-12 stsp if (id0 > 0)
330 6fd11751 2018-06-04 stsp i = betoh32(packidx->hdr.fanout_table[id0 - 1]);
332 a1fd68d8 2018-01-12 stsp while (i < totobj) {
333 6fd11751 2018-06-04 stsp struct got_object_id *oid = &packidx->hdr.sorted_ids[i];
334 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
336 16dcbf91 2018-04-01 stsp if (cmp == 0)
344 6fd11751 2018-06-04 stsp static struct got_packidx *
345 6fd11751 2018-06-04 stsp dup_packidx(struct got_packidx *packidx)
347 6fd11751 2018-06-04 stsp struct got_packidx *p;
348 79b11c62 2018-03-09 stsp size_t nobj;
350 79b11c62 2018-03-09 stsp p = calloc(1, sizeof(*p));
351 79b11c62 2018-03-09 stsp if (p == NULL)
352 79b11c62 2018-03-09 stsp return NULL;
354 6fd11751 2018-06-04 stsp p->path_packidx = strdup(packidx->path_packidx);
355 6fd11751 2018-06-04 stsp if (p->path_packidx == NULL) {
357 6fd11751 2018-06-04 stsp return NULL;
359 6fd11751 2018-06-04 stsp memcpy(&p->hdr, &packidx->hdr, sizeof(p->hdr));
360 6fd11751 2018-06-04 stsp p->hdr.sorted_ids = NULL;
361 6fd11751 2018-06-04 stsp p->hdr.crc32 = NULL;
362 6fd11751 2018-06-04 stsp p->hdr.offsets = NULL;
363 6fd11751 2018-06-04 stsp p->hdr.large_offsets = NULL;
365 6fd11751 2018-06-04 stsp nobj = betoh32(p->hdr.fanout_table[0xff]);
367 6fd11751 2018-06-04 stsp p->hdr.sorted_ids = calloc(nobj, sizeof(*p->hdr.sorted_ids));
368 6fd11751 2018-06-04 stsp if (p->hdr.sorted_ids == NULL)
370 6fd11751 2018-06-04 stsp memcpy(p->hdr.sorted_ids, packidx->hdr.sorted_ids,
371 6fd11751 2018-06-04 stsp nobj * sizeof(*p->hdr.sorted_ids));
373 6fd11751 2018-06-04 stsp p->hdr.crc32 = calloc(nobj, sizeof(*p->hdr.crc32));
374 6fd11751 2018-06-04 stsp if (p->hdr.crc32 == NULL)
376 6fd11751 2018-06-04 stsp memcpy(p->hdr.crc32, packidx->hdr.crc32, nobj * sizeof(*p->hdr.crc32));
378 6fd11751 2018-06-04 stsp p->hdr.offsets = calloc(nobj, sizeof(*p->hdr.offsets));
379 6fd11751 2018-06-04 stsp if (p->hdr.offsets == NULL)
381 6fd11751 2018-06-04 stsp memcpy(p->hdr.offsets, packidx->hdr.offsets,
382 6fd11751 2018-06-04 stsp nobj * sizeof(*p->hdr.offsets));
384 6fd11751 2018-06-04 stsp if (p->hdr.large_offsets) {
385 0b48ab23 2018-06-04 stsp p->hdr.large_offsets = calloc(nobj,
386 0b48ab23 2018-06-04 stsp sizeof(*p->hdr.large_offsets));
387 6fd11751 2018-06-04 stsp if (p->hdr.large_offsets == NULL)
389 6fd11751 2018-06-04 stsp memcpy(p->hdr.large_offsets, packidx->hdr.large_offsets,
390 6fd11751 2018-06-04 stsp nobj * sizeof(*p->hdr.large_offsets));
396 6fd11751 2018-06-04 stsp free(p->hdr.large_offsets);
397 6fd11751 2018-06-04 stsp free(p->hdr.offsets);
398 6fd11751 2018-06-04 stsp free(p->hdr.crc32);
399 6fd11751 2018-06-04 stsp free(p->hdr.sorted_ids);
400 6fd11751 2018-06-04 stsp free(p->path_packidx);
402 79b11c62 2018-03-09 stsp return NULL;
405 65cf1e80 2018-03-16 stsp static void
406 6fd11751 2018-06-04 stsp cache_packidx(struct got_packidx *packidx, struct got_repository *repo)
410 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
411 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
415 65cf1e80 2018-03-16 stsp if (i == nitems(repo->packidx_cache)) {
416 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i - 1]);
417 65cf1e80 2018-03-16 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
418 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache) -
419 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache[0]));
423 65cf1e80 2018-03-16 stsp repo->packidx_cache[i] = dup_packidx(packidx);
426 6b9c9673 2018-01-23 stsp static const struct got_error *
427 6fd11751 2018-06-04 stsp search_packidx(struct got_packidx **packidx, int *idx,
428 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
430 6b9c9673 2018-01-23 stsp const struct got_error *err;
431 6b9c9673 2018-01-23 stsp char *path_packdir;
432 6b9c9673 2018-01-23 stsp DIR *packdir;
433 6b9c9673 2018-01-23 stsp struct dirent *dent;
434 6b9c9673 2018-01-23 stsp char *path_packidx;
437 65cf1e80 2018-03-16 stsp /* Search pack index cache. */
438 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
439 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
441 72eb3431 2018-04-01 stsp *idx = get_object_idx(repo->packidx_cache[i], id, repo);
442 79b11c62 2018-03-09 stsp if (*idx != -1) {
443 6bb255dc 2018-03-17 stsp *packidx = repo->packidx_cache[i];
444 79b11c62 2018-03-09 stsp return NULL;
447 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
449 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
450 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
451 0a585a0d 2018-03-17 stsp return got_error_from_errno();
453 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
454 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
455 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
459 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
460 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
463 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
464 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
465 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
469 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
470 6b9c9673 2018-01-23 stsp free(path_packidx);
474 72eb3431 2018-04-01 stsp *idx = get_object_idx(*packidx, id, repo);
475 6b9c9673 2018-01-23 stsp if (*idx != -1) {
476 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
477 65cf1e80 2018-03-16 stsp cache_packidx(*packidx, repo);
481 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
482 6b9c9673 2018-01-23 stsp *packidx = NULL;
485 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
487 6b9c9673 2018-01-23 stsp free(path_packdir);
488 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
489 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
490 6b9c9673 2018-01-23 stsp return err;
493 c7fe698a 2018-01-23 stsp static const struct got_error *
494 65cf1e80 2018-03-16 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
495 6fd11751 2018-06-04 stsp struct got_packidx *packidx)
497 6fd11751 2018-06-04 stsp size_t size;
499 6fd11751 2018-06-04 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
500 6fd11751 2018-06-04 stsp size = strlen(packidx->path_packidx) + 2;
501 6fd11751 2018-06-04 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
502 6fd11751 2018-06-04 stsp return got_error(GOT_ERR_BAD_PATH);
504 6fd11751 2018-06-04 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
505 6fd11751 2018-06-04 stsp if (*path_packfile == NULL)
506 0a585a0d 2018-03-17 stsp return got_error_from_errno();
508 6fd11751 2018-06-04 stsp /* Copy up to and excluding ".idx". */
509 d475dd0d 2018-06-04 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
510 72fb0363 2018-06-04 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
511 d475dd0d 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
513 6fd11751 2018-06-04 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
514 6fd11751 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
516 65cf1e80 2018-03-16 stsp return NULL;
519 65cf1e80 2018-03-16 stsp const struct got_error *
520 6fd11751 2018-06-04 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
522 65cf1e80 2018-03-16 stsp const struct got_error *err = NULL;
523 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
524 65cf1e80 2018-03-16 stsp struct got_packfile_hdr hdr;
527 8b2180d4 2018-04-26 stsp n = read(fd, &hdr, sizeof(hdr));
529 8b2180d4 2018-04-26 stsp return got_error_from_errno();
530 8b2180d4 2018-04-26 stsp if (n != sizeof(hdr))
531 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
533 65cf1e80 2018-03-16 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
534 65cf1e80 2018-03-16 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
535 65cf1e80 2018-03-16 stsp betoh32(hdr.nobjects) != totobj)
536 65cf1e80 2018-03-16 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
538 65cf1e80 2018-03-16 stsp return err;
541 7e656b93 2018-03-17 stsp static const struct got_error *
542 8b2180d4 2018-04-26 stsp open_packfile(int *fd, const char *path_packfile,
543 6fd11751 2018-06-04 stsp struct got_repository *repo, struct got_packidx *packidx)
545 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
547 8b2180d4 2018-04-26 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
548 8b2180d4 2018-04-26 stsp if (*fd == -1)
549 8b2180d4 2018-04-26 stsp return got_error_from_errno();
551 7e656b93 2018-03-17 stsp if (packidx) {
552 8b2180d4 2018-04-26 stsp err = read_packfile_hdr(*fd, packidx);
554 8b2180d4 2018-04-26 stsp close(*fd);
558 7e656b93 2018-03-17 stsp return err;
562 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
564 8b2180d4 2018-04-26 stsp close(pack->fd);
565 8b2180d4 2018-04-26 stsp pack->fd = -1;
566 4589e373 2018-03-17 stsp free(pack->path_packfile);
567 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
568 4589e373 2018-03-17 stsp pack->filesize = 0;
571 7e656b93 2018-03-17 stsp static const struct got_error *
572 7e656b93 2018-03-17 stsp cache_pack(struct got_pack **packp, const char *path_packfile,
573 6fd11751 2018-06-04 stsp struct got_packidx *packidx, struct got_repository *repo)
575 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
576 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
580 7e656b93 2018-03-17 stsp *packp = NULL;
582 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
583 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
584 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
586 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
587 7e656b93 2018-03-17 stsp return NULL;
590 7e656b93 2018-03-17 stsp if (i == nitems(repo->packs) - 1) {
591 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i - 1]);
592 7e656b93 2018-03-17 stsp memmove(&repo->packs[1], &repo->packs[0],
593 7e656b93 2018-03-17 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
597 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
599 7e656b93 2018-03-17 stsp pack->path_packfile = strdup(path_packfile);
600 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL) {
601 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
605 8b2180d4 2018-04-26 stsp err = open_packfile(&pack->fd, path_packfile, repo, packidx);
609 7e656b93 2018-03-17 stsp err = get_packfile_size(&pack->filesize, path_packfile);
612 f5feadcc 2018-06-04 stsp if (pack) {
613 7e656b93 2018-03-17 stsp free(pack->path_packfile);
614 f5feadcc 2018-06-04 stsp memset(pack, 0, sizeof(*pack));
616 7e656b93 2018-03-17 stsp } else if (packp)
617 7e656b93 2018-03-17 stsp *packp = pack;
618 c7fe698a 2018-01-23 stsp return err;
621 7e656b93 2018-03-17 stsp struct got_pack *
622 7e656b93 2018-03-17 stsp get_cached_pack(const char *path_packfile, struct got_repository *repo)
624 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
627 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
628 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
629 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
631 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
632 7e656b93 2018-03-17 stsp return pack;
635 7e656b93 2018-03-17 stsp return NULL;
638 c7fe698a 2018-01-23 stsp static const struct got_error *
639 8b2180d4 2018-04-26 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len, int fd)
641 a487c1d0 2018-01-14 stsp uint8_t t = 0;
642 a487c1d0 2018-01-14 stsp uint64_t s = 0;
643 a487c1d0 2018-01-14 stsp uint8_t sizeN;
648 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
650 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
652 8b2180d4 2018-04-26 stsp n = read(fd, &sizeN, sizeof(sizeN));
654 8b2180d4 2018-04-26 stsp return got_error_from_errno();
655 8b2180d4 2018-04-26 stsp if (n != sizeof(sizeN))
656 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
658 a1fd68d8 2018-01-12 stsp if (i == 0) {
659 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
660 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
661 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
663 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
664 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
667 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
671 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
672 a487c1d0 2018-01-14 stsp return NULL;
675 a1fd68d8 2018-01-12 stsp static const struct got_error *
676 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
677 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
679 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
680 6ccb713b 2018-01-19 stsp if (*obj == NULL)
681 0a585a0d 2018-03-17 stsp return got_error_from_errno();
683 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
684 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
685 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
686 6ccb713b 2018-01-19 stsp free(*obj);
687 6ccb713b 2018-01-19 stsp *obj = NULL;
688 0a585a0d 2018-03-17 stsp return err;
691 6ccb713b 2018-01-19 stsp (*obj)->type = type;
692 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
693 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
694 6ccb713b 2018-01-19 stsp (*obj)->size = size;
695 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
696 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
698 b107e67f 2018-01-19 stsp return NULL;
701 b107e67f 2018-01-19 stsp static const struct got_error *
702 8b2180d4 2018-04-26 stsp parse_negative_offset(int64_t *offset, size_t *len, int fd)
704 b107e67f 2018-01-19 stsp int64_t o = 0;
705 b107e67f 2018-01-19 stsp uint8_t offN;
710 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
712 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
714 8b2180d4 2018-04-26 stsp n = read(fd, &offN, sizeof(offN));
716 8b2180d4 2018-04-26 stsp return got_error_from_errno();
717 8b2180d4 2018-04-26 stsp if (n != sizeof(offN))
718 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
720 b107e67f 2018-01-19 stsp if (i == 0)
721 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
725 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
728 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
730 b107e67f 2018-01-19 stsp *offset = o;
731 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
732 6ccb713b 2018-01-19 stsp return NULL;
735 6ccb713b 2018-01-19 stsp static const struct got_error *
736 8b2180d4 2018-04-26 stsp parse_offset_delta(off_t *base_offset, int fd, off_t offset)
738 96f5e8b3 2018-01-23 stsp const struct got_error *err;
739 b107e67f 2018-01-19 stsp int64_t negoffset;
740 b107e67f 2018-01-19 stsp size_t negofflen;
742 8b2180d4 2018-04-26 stsp err = parse_negative_offset(&negoffset, &negofflen, fd);
744 b107e67f 2018-01-19 stsp return err;
746 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
747 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
748 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
749 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
751 96f5e8b3 2018-01-23 stsp return NULL;
754 0e22967e 2018-02-11 stsp static const struct got_error *
755 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
756 0c048b15 2018-04-27 stsp int, size_t, const char *, off_t, size_t, int, size_t, unsigned int);
758 96f5e8b3 2018-01-23 stsp static const struct got_error *
759 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
760 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
761 a53d2f13 2018-03-17 stsp size_t delta_data_offset, uint8_t *delta_buf, size_t delta_len)
763 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
765 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
766 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf,
767 a53d2f13 2018-03-17 stsp delta_len);
768 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
769 0a585a0d 2018-03-17 stsp return got_error_from_errno();
770 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
771 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
772 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
773 bdd2fbb3 2018-02-11 stsp return NULL;
776 bdd2fbb3 2018-02-11 stsp static const struct got_error *
777 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
778 0c048b15 2018-04-27 stsp struct got_repository *repo, int fd, size_t packfile_size,
779 0c048b15 2018-04-27 stsp const char *path_packfile, off_t delta_offset, size_t tslen,
780 0c048b15 2018-04-27 stsp int delta_type, size_t delta_size, unsigned int recursion)
783 a3500804 2018-01-23 stsp const struct got_error *err;
784 c3703302 2018-01-23 stsp off_t base_offset;
785 c3703302 2018-01-23 stsp uint8_t base_type;
786 c3703302 2018-01-23 stsp uint64_t base_size;
787 c3703302 2018-01-23 stsp size_t base_tslen;
788 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
789 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
790 a53d2f13 2018-03-17 stsp size_t delta_len;
792 8b2180d4 2018-04-26 stsp err = parse_offset_delta(&base_offset, fd, delta_offset);
794 a3500804 2018-01-23 stsp return err;
796 8b2180d4 2018-04-26 stsp delta_data_offset = lseek(fd, 0, SEEK_CUR);
797 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
798 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
800 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, fd);
802 a53d2f13 2018-03-17 stsp return err;
804 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
805 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
807 bdd2fbb3 2018-02-11 stsp return err;
809 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
810 0c048b15 2018-04-27 stsp if (base_offset >= packfile_size)
811 0c048b15 2018-04-27 stsp return got_error(GOT_ERR_PACK_OFFSET);
812 8b2180d4 2018-04-26 stsp if (lseek(fd, base_offset, SEEK_SET) == -1)
813 b107e67f 2018-01-19 stsp return got_error_from_errno();
815 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
818 b107e67f 2018-01-19 stsp return err;
820 0c048b15 2018-04-27 stsp return resolve_delta_chain(deltas, repo, fd, packfile_size,
821 0c048b15 2018-04-27 stsp path_packfile, base_offset, base_tslen, base_type, base_size,
822 0c048b15 2018-04-27 stsp recursion - 1);
825 c3703302 2018-01-23 stsp static const struct got_error *
826 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
827 8b2180d4 2018-04-26 stsp int fd, const char *path_packfile, off_t delta_offset,
828 5b7e13a7 2018-04-02 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
830 6b9c9673 2018-01-23 stsp const struct got_error *err;
831 6b9c9673 2018-01-23 stsp struct got_object_id id;
832 6fd11751 2018-06-04 stsp struct got_packidx *packidx;
834 6b9c9673 2018-01-23 stsp off_t base_offset;
835 6b9c9673 2018-01-23 stsp uint8_t base_type;
836 6b9c9673 2018-01-23 stsp uint64_t base_size;
837 6b9c9673 2018-01-23 stsp size_t base_tslen;
839 65cf1e80 2018-03-16 stsp char *path_base_packfile;
840 999f19f6 2018-03-17 stsp struct got_pack *base_pack;
841 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
842 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
843 a53d2f13 2018-03-17 stsp size_t delta_len;
845 8b2180d4 2018-04-26 stsp n = read(fd, &id, sizeof(id));
847 8b2180d4 2018-04-26 stsp return got_error_from_errno();
848 8b2180d4 2018-04-26 stsp if (n != sizeof(id))
849 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
851 8b2180d4 2018-04-26 stsp delta_data_offset = lseek(fd, 0, SEEK_CUR);
852 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
853 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
855 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, fd);
857 a53d2f13 2018-03-17 stsp return err;
859 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
860 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
862 bdd2fbb3 2018-02-11 stsp return err;
864 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
866 6b9c9673 2018-01-23 stsp return err;
868 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
869 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
870 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
873 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_base_packfile, repo, packidx);
875 7e656b93 2018-03-17 stsp return err;
877 999f19f6 2018-03-17 stsp base_pack = get_cached_pack(path_base_packfile, repo);
878 999f19f6 2018-03-17 stsp if (base_pack == NULL) {
879 999f19f6 2018-03-17 stsp err = cache_pack(&base_pack, path_base_packfile, NULL, repo);
884 0c048b15 2018-04-27 stsp if (base_offset >= base_pack->filesize) {
885 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
888 8b2180d4 2018-04-26 stsp if (lseek(base_pack->fd, base_offset, SEEK_SET) == -1) {
889 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
893 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
894 8b2180d4 2018-04-26 stsp base_pack->fd);
898 8b2180d4 2018-04-26 stsp err = resolve_delta_chain(deltas, repo, base_pack->fd,
899 0c048b15 2018-04-27 stsp base_pack->filesize, path_base_packfile, base_offset,
900 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
902 65cf1e80 2018-03-16 stsp free(path_base_packfile);
903 6b9c9673 2018-01-23 stsp return err;
906 6b9c9673 2018-01-23 stsp static const struct got_error *
907 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
908 0c048b15 2018-04-27 stsp int fd, size_t packfile_size, const char *path_packfile, off_t delta_offset,
909 0c048b15 2018-04-27 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
911 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
913 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
914 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
916 c3703302 2018-01-23 stsp switch (delta_type) {
917 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
918 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
919 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
920 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
921 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
922 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
923 a53d2f13 2018-03-17 stsp delta_type, delta_size, 0, NULL, 0);
925 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
926 0c048b15 2018-04-27 stsp err = resolve_offset_delta(deltas, repo, fd, packfile_size,
927 0c048b15 2018-04-27 stsp path_packfile, delta_offset, tslen, delta_type, delta_size,
928 0c048b15 2018-04-27 stsp recursion - 1);
930 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
931 8b2180d4 2018-04-26 stsp err = resolve_ref_delta(deltas, repo, fd, path_packfile,
932 8b2180d4 2018-04-26 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
935 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
938 96f5e8b3 2018-01-23 stsp return err;
941 96f5e8b3 2018-01-23 stsp static const struct got_error *
942 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
943 6fd11751 2018-06-04 stsp const char *path_packfile, int fd, size_t packfile_size,
944 6fd11751 2018-06-04 stsp struct got_object_id *id, off_t offset, size_t tslen,
945 6fd11751 2018-06-04 stsp int delta_type, size_t delta_size)
947 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
948 96f5e8b3 2018-01-23 stsp int resolved_type;
950 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
951 b107e67f 2018-01-19 stsp if (*obj == NULL)
952 0a585a0d 2018-03-17 stsp return got_error_from_errno();
954 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
955 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
956 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
957 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
958 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
960 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
961 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
962 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
965 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
967 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
968 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
970 0c048b15 2018-04-27 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, fd, packfile_size,
971 5b7e13a7 2018-04-02 stsp path_packfile, offset, tslen, delta_type, delta_size,
972 5b7e13a7 2018-04-02 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
976 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
979 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
983 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
984 96f5e8b3 2018-01-23 stsp *obj = NULL;
986 96f5e8b3 2018-01-23 stsp return err;
989 b107e67f 2018-01-19 stsp static const struct got_error *
990 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
991 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
993 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
994 a1fd68d8 2018-01-12 stsp off_t offset;
995 65cf1e80 2018-03-16 stsp char *path_packfile;
996 6d89869a 2018-03-17 stsp struct got_pack *pack;
997 6c00b545 2018-01-17 stsp uint8_t type;
998 6c00b545 2018-01-17 stsp uint64_t size;
999 3ee5fc21 2018-01-17 stsp size_t tslen;
1001 6c00b545 2018-01-17 stsp *obj = NULL;
1003 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
1004 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
1005 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1007 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_packfile, repo, packidx);
1009 6b9c9673 2018-01-23 stsp return err;
1011 6d89869a 2018-03-17 stsp pack = get_cached_pack(path_packfile, repo);
1012 6d89869a 2018-03-17 stsp if (pack == NULL) {
1013 6d89869a 2018-03-17 stsp err = cache_pack(&pack, path_packfile, packidx, repo);
1015 6d89869a 2018-03-17 stsp goto done;
1018 0c048b15 2018-04-27 stsp if (offset >= pack->filesize) {
1019 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1020 0c048b15 2018-04-27 stsp goto done;
1022 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1) {
1023 6c00b545 2018-01-17 stsp err = got_error_from_errno();
1024 6c00b545 2018-01-17 stsp goto done;
1027 8b2180d4 2018-04-26 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack->fd);
1029 a1fd68d8 2018-01-12 stsp goto done;
1031 6c00b545 2018-01-17 stsp switch (type) {
1032 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
1033 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
1034 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
1035 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
1036 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
1037 6ccb713b 2018-01-19 stsp offset + tslen, size);
1040 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1041 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1042 6fd11751 2018-06-04 stsp err = open_delta_object(obj, repo, path_packfile, pack->fd,
1043 6fd11751 2018-06-04 stsp pack->filesize, id, offset, tslen, type, size);
1047 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1048 6c00b545 2018-01-17 stsp goto done;
1051 65cf1e80 2018-03-16 stsp free(path_packfile);
1052 a1fd68d8 2018-01-12 stsp return err;
1055 a1fd68d8 2018-01-12 stsp const struct got_error *
1056 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1057 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
1059 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1060 6fd11751 2018-06-04 stsp struct got_packidx *packidx = NULL;
1063 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
1065 6b9c9673 2018-01-23 stsp return err;
1067 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
1069 7e656b93 2018-03-17 stsp return err;
1071 7e656b93 2018-03-17 stsp err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1072 3ee5fc21 2018-01-17 stsp return err;
1075 efd2a263 2018-01-19 stsp static const struct got_error *
1076 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1078 22484865 2018-03-13 stsp struct got_delta *delta;
1079 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1081 22484865 2018-03-13 stsp *max_size = 0;
1082 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1083 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1084 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1085 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1086 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1087 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1088 22484865 2018-03-13 stsp const struct got_error *err;
1089 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1090 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
1092 22484865 2018-03-13 stsp return err;
1094 22484865 2018-03-13 stsp base_size = delta->size;
1095 22484865 2018-03-13 stsp if (base_size > *max_size)
1096 22484865 2018-03-13 stsp *max_size = base_size;
1097 22484865 2018-03-13 stsp if (result_size > *max_size)
1098 22484865 2018-03-13 stsp *max_size = result_size;
1101 9feb4ff2 2018-03-14 stsp return NULL;
1104 22484865 2018-03-13 stsp static const struct got_error *
1105 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1106 72eb3431 2018-04-01 stsp FILE *outfile, struct got_repository *repo)
1108 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1109 6691714a 2018-01-23 stsp struct got_delta *delta;
1110 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1111 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1112 b29656e2 2018-03-16 stsp size_t accum_size = 0;
1113 22484865 2018-03-13 stsp uint64_t max_size;
1114 6691714a 2018-01-23 stsp int n = 0;
1116 b29656e2 2018-03-16 stsp *result_size = 0;
1118 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1119 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1121 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1122 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1124 22484865 2018-03-13 stsp return err;
1125 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1126 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1127 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1128 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1130 22484865 2018-03-13 stsp base_file = got_opentemp();
1131 8628c62d 2018-03-15 stsp if (base_file == NULL)
1132 8628c62d 2018-03-15 stsp return got_error_from_errno();
1134 22484865 2018-03-13 stsp accum_file = got_opentemp();
1135 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1136 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1137 8628c62d 2018-03-15 stsp fclose(base_file);
1138 8628c62d 2018-03-15 stsp return err;
1142 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1143 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1144 a6b158cc 2018-02-11 stsp if (n == 0) {
1145 72eb3431 2018-04-01 stsp struct got_pack *pack;
1146 8628c62d 2018-03-15 stsp size_t base_len;
1147 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1149 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1150 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1151 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1152 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1153 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1154 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1155 72eb3431 2018-04-01 stsp goto done;
1158 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1159 72eb3431 2018-04-01 stsp if (pack == NULL) {
1160 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1161 a6b158cc 2018-02-11 stsp goto done;
1164 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1165 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1166 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1167 0c048b15 2018-04-27 stsp goto done;
1169 0c048b15 2018-04-27 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
1170 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
1171 bdd2fbb3 2018-02-11 stsp goto done;
1173 8628c62d 2018-03-15 stsp if (base_file)
1174 8b2180d4 2018-04-26 stsp err = got_inflate_to_file_fd(&base_len,
1175 8b2180d4 2018-04-26 stsp pack->fd, base_file);
1177 0b48ab23 2018-06-04 stsp err = got_inflate_to_mem_fd(&base_buf,
1178 0b48ab23 2018-06-04 stsp &base_len, pack->fd);
1179 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1180 8628c62d 2018-03-15 stsp uint8_t *p;
1181 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1182 8628c62d 2018-03-15 stsp if (p == NULL) {
1183 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1184 8628c62d 2018-03-15 stsp goto done;
1186 8628c62d 2018-03-15 stsp base_buf = p;
1190 a6b158cc 2018-02-11 stsp goto done;
1192 8628c62d 2018-03-15 stsp if (base_file)
1193 8628c62d 2018-03-15 stsp rewind(base_file);
1197 8628c62d 2018-03-15 stsp if (base_buf) {
1198 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1199 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1202 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1203 a53d2f13 2018-03-17 stsp delta->delta_len,
1204 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1205 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1206 b29656e2 2018-03-16 stsp &accum_size);
1209 6691714a 2018-01-23 stsp goto done;
1211 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1212 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1213 8628c62d 2018-03-15 stsp if (base_buf) {
1214 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1215 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1216 8628c62d 2018-03-15 stsp base_buf = tmp;
1218 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1219 8628c62d 2018-03-15 stsp accum_file = base_file;
1220 8628c62d 2018-03-15 stsp base_file = tmp;
1221 8628c62d 2018-03-15 stsp rewind(base_file);
1222 8628c62d 2018-03-15 stsp rewind(accum_file);
1228 8628c62d 2018-03-15 stsp free(base_buf);
1229 8628c62d 2018-03-15 stsp if (accum_buf) {
1230 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1231 8628c62d 2018-03-15 stsp free(accum_buf);
1232 8628c62d 2018-03-15 stsp if (len != accum_size)
1233 8628c62d 2018-03-15 stsp return got_ferror(outfile, GOT_ERR_IO);
1235 8628c62d 2018-03-15 stsp if (base_file)
1236 8628c62d 2018-03-15 stsp fclose(base_file);
1237 8628c62d 2018-03-15 stsp if (accum_file)
1238 8628c62d 2018-03-15 stsp fclose(accum_file);
1239 6691714a 2018-01-23 stsp rewind(outfile);
1240 b29656e2 2018-03-16 stsp if (err == NULL)
1241 b29656e2 2018-03-16 stsp *result_size = accum_size;
1242 e0ab43e7 2018-03-16 stsp return err;
1245 e0ab43e7 2018-03-16 stsp static const struct got_error *
1246 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1247 72eb3431 2018-04-01 stsp struct got_delta_chain *deltas, struct got_repository *repo)
1249 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1250 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1251 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1252 e0ab43e7 2018-03-16 stsp size_t accum_size;
1253 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1254 e0ab43e7 2018-03-16 stsp int n = 0;
1256 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1257 e0ab43e7 2018-03-16 stsp *outlen = 0;
1259 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1260 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1262 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1264 e0ab43e7 2018-03-16 stsp return err;
1265 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1266 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1267 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1269 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1270 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1271 e0ab43e7 2018-03-16 stsp if (n == 0) {
1272 72eb3431 2018-04-01 stsp struct got_pack *pack;
1273 e0ab43e7 2018-03-16 stsp size_t base_len;
1274 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1276 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1277 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1278 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1279 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1280 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1281 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1282 72eb3431 2018-04-01 stsp goto done;
1285 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1286 72eb3431 2018-04-01 stsp if (pack == NULL) {
1287 e0ab43e7 2018-03-16 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1288 e0ab43e7 2018-03-16 stsp goto done;
1291 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1292 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1293 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1294 0c048b15 2018-04-27 stsp goto done;
1296 0c048b15 2018-04-27 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
1297 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1298 e0ab43e7 2018-03-16 stsp goto done;
1300 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(&base_buf, &base_len,
1301 8b2180d4 2018-04-26 stsp pack->fd);
1302 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1303 e0ab43e7 2018-03-16 stsp uint8_t *p;
1304 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1305 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1306 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1307 e0ab43e7 2018-03-16 stsp goto done;
1309 e0ab43e7 2018-03-16 stsp base_buf = p;
1312 e0ab43e7 2018-03-16 stsp goto done;
1317 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1318 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1321 e0ab43e7 2018-03-16 stsp goto done;
1323 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1324 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1325 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1326 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1327 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1332 e0ab43e7 2018-03-16 stsp free(base_buf);
1333 e0ab43e7 2018-03-16 stsp if (err) {
1334 e0ab43e7 2018-03-16 stsp free(accum_buf);
1335 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1336 e0ab43e7 2018-03-16 stsp *outlen = 0;
1338 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1339 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1341 efd2a263 2018-01-19 stsp return err;
1344 3ee5fc21 2018-01-17 stsp const struct got_error *
1345 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1346 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1348 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1350 8b2180d4 2018-04-26 stsp *f = NULL;
1352 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1353 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1355 040bf4a1 2018-04-01 stsp *f = got_opentemp();
1356 040bf4a1 2018-04-01 stsp if (*f == NULL) {
1357 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1358 040bf4a1 2018-04-01 stsp goto done;
1361 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1362 72eb3431 2018-04-01 stsp struct got_pack *pack;
1364 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1365 72eb3431 2018-04-01 stsp if (pack == NULL) {
1366 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1368 72eb3431 2018-04-01 stsp goto done;
1371 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1372 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1373 0c048b15 2018-04-27 stsp goto done;
1375 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1376 6691714a 2018-01-23 stsp err = got_error_from_errno();
1377 c52ac529 2018-03-17 stsp goto done;
1380 8b2180d4 2018-04-26 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1382 8b2180d4 2018-04-26 stsp err = dump_delta_chain_to_file(&obj->size,
1383 8b2180d4 2018-04-26 stsp &obj->deltas, *f, repo);
1385 d0f3be7c 2018-03-17 stsp if (err && *f) {
1386 3ee5fc21 2018-01-17 stsp fclose(*f);
1387 d0f3be7c 2018-03-17 stsp *f = NULL;
1389 a1fd68d8 2018-01-12 stsp return err;
1392 e0ab43e7 2018-03-16 stsp const struct got_error *
1393 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1394 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1396 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1398 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1399 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1401 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1402 72eb3431 2018-04-01 stsp struct got_pack *pack;
1404 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1405 72eb3431 2018-04-01 stsp if (pack == NULL) {
1406 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1408 72eb3431 2018-04-01 stsp goto done;
1411 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1412 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1413 0c048b15 2018-04-27 stsp goto done;
1415 8b2180d4 2018-04-26 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1416 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1417 e0ab43e7 2018-03-16 stsp goto done;
1420 8b2180d4 2018-04-26 stsp err = got_inflate_to_mem_fd(buf, len, pack->fd);
1422 72eb3431 2018-04-01 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, repo);
1424 e0ab43e7 2018-03-16 stsp return err;