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 a1fd68d8 2018-01-12 stsp #include <errno.h>
23 0a0a3048 2018-01-10 stsp #include <stdio.h>
24 a1fd68d8 2018-01-12 stsp #include <stdint.h>
25 0a0a3048 2018-01-10 stsp #include <stdlib.h>
26 0a0a3048 2018-01-10 stsp #include <string.h>
27 0a0a3048 2018-01-10 stsp #include <limits.h>
28 0a0a3048 2018-01-10 stsp #include <sha1.h>
29 0a0a3048 2018-01-10 stsp #include <endian.h>
30 a1fd68d8 2018-01-12 stsp #include <zlib.h>
32 0a0a3048 2018-01-10 stsp #include "got_error.h"
33 a1fd68d8 2018-01-12 stsp #include "got_object.h"
34 a1fd68d8 2018-01-12 stsp #include "got_repository.h"
36 32cb896c 2018-03-11 stsp #include "got_sha1_lib.h"
37 32cb896c 2018-03-11 stsp #include "got_pack_lib.h"
38 32cb896c 2018-03-11 stsp #include "got_path_lib.h"
39 32cb896c 2018-03-11 stsp #include "got_delta_lib.h"
40 32cb896c 2018-03-11 stsp #include "got_zbuf_lib.h"
41 32cb896c 2018-03-11 stsp #include "got_object_lib.h"
42 32cb896c 2018-03-11 stsp #include "got_repository_lib.h"
44 79b11c62 2018-03-09 stsp #ifndef nitems
45 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
49 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
50 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
51 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
52 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
53 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
54 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
55 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
56 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
59 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 0a0a3048 2018-01-10 stsp static const struct got_error *
63 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
67 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
68 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
69 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
72 0a0a3048 2018-01-10 stsp return NULL;
75 24541888 2018-01-10 stsp static const struct got_error *
76 0a0a3048 2018-01-10 stsp get_packfile_size(size_t *size, const char *path_idx)
78 0a0a3048 2018-01-10 stsp struct stat sb;
79 0a0a3048 2018-01-10 stsp char *path_pack;
80 0a0a3048 2018-01-10 stsp char base_path[PATH_MAX];
83 0a0a3048 2018-01-10 stsp if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
84 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_SPACE);
86 0a0a3048 2018-01-10 stsp dot = strrchr(base_path, '.');
87 0a0a3048 2018-01-10 stsp if (dot == NULL)
88 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
89 0a0a3048 2018-01-10 stsp *dot = '\0';
90 0a0a3048 2018-01-10 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
91 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_MEM);
93 0a0a3048 2018-01-10 stsp if (stat(path_pack, &sb) != 0) {
94 0a0a3048 2018-01-10 stsp free(path_pack);
95 8251fdbc 2018-01-12 stsp return got_error_from_errno();
98 0a0a3048 2018-01-10 stsp free(path_pack);
99 0a0a3048 2018-01-10 stsp *size = sb.st_size;
103 0a0a3048 2018-01-10 stsp const struct got_error *
104 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
106 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
108 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
109 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
110 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
111 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
113 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
115 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
116 0a0a3048 2018-01-10 stsp if (f == NULL)
117 9feb4ff2 2018-03-14 stsp return got_error_from_errno();
119 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
121 0a0a3048 2018-01-10 stsp return err;
123 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
124 0a0a3048 2018-01-10 stsp if (p == NULL) {
125 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
129 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
130 0a0a3048 2018-01-10 stsp if (n != 1) {
131 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
135 0a0a3048 2018-01-10 stsp if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
136 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
140 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
142 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
143 0a0a3048 2018-01-10 stsp if (n != 1) {
144 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
148 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
149 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
153 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
155 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 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 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
165 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
167 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
169 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
170 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
171 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
175 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
176 0a0a3048 2018-01-10 stsp if (n != nobj) {
177 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
181 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
182 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
184 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
185 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
186 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
190 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
191 0a0a3048 2018-01-10 stsp if (n != nobj) {
192 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
196 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
198 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
199 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
200 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
204 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
205 0a0a3048 2018-01-10 stsp if (n != nobj) {
206 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
210 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
212 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
213 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
214 0a0a3048 2018-01-10 stsp goto checksum;
216 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
217 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
218 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
222 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
223 0a0a3048 2018-01-10 stsp if (n != nobj) {
224 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
228 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
229 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
232 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
233 0a0a3048 2018-01-10 stsp if (n != 1) {
234 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
238 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
239 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
240 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
241 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
245 0a0a3048 2018-01-10 stsp got_packidx_close(p);
247 0a0a3048 2018-01-10 stsp *packidx = p;
248 0a0a3048 2018-01-10 stsp return err;
252 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
254 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
255 0a0a3048 2018-01-10 stsp free(packidx->offsets);
256 0a0a3048 2018-01-10 stsp free(packidx->crc32);
257 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
258 0a0a3048 2018-01-10 stsp free(packidx);
262 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
264 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
267 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
270 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
271 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
277 a1fd68d8 2018-01-12 stsp static off_t
278 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
280 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
281 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
282 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
283 a1fd68d8 2018-01-12 stsp uint64_t loffset;
284 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
285 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
287 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
288 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
290 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
294 a1fd68d8 2018-01-12 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
296 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
297 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
300 a1fd68d8 2018-01-12 stsp if (id0 > 0)
301 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
303 a1fd68d8 2018-01-12 stsp while (i < totobj) {
304 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
305 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
307 6c00b545 2018-01-17 stsp if (cmp == 0)
315 79b11c62 2018-03-09 stsp static struct got_packidx_v2_hdr *
316 79b11c62 2018-03-09 stsp dup_packidx(struct got_packidx_v2_hdr *packidx)
318 79b11c62 2018-03-09 stsp struct got_packidx_v2_hdr *p;
319 79b11c62 2018-03-09 stsp size_t nobj;
321 79b11c62 2018-03-09 stsp p = calloc(1, sizeof(*p));
322 79b11c62 2018-03-09 stsp if (p == NULL)
323 79b11c62 2018-03-09 stsp return NULL;
325 79b11c62 2018-03-09 stsp memcpy(p, packidx, sizeof(*p));
326 79b11c62 2018-03-09 stsp p->sorted_ids = NULL;
327 79b11c62 2018-03-09 stsp p->crc32 = NULL;
328 79b11c62 2018-03-09 stsp p->offsets = NULL;
329 79b11c62 2018-03-09 stsp p->large_offsets = NULL;
331 79b11c62 2018-03-09 stsp nobj = betoh32(p->fanout_table[0xff]);
333 79b11c62 2018-03-09 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
334 79b11c62 2018-03-09 stsp if (p->sorted_ids == NULL)
336 79b11c62 2018-03-09 stsp memcpy(p->sorted_ids, packidx->sorted_ids, nobj * sizeof(*p->sorted_ids));
338 79b11c62 2018-03-09 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
339 79b11c62 2018-03-09 stsp if (p->crc32 == NULL)
341 79b11c62 2018-03-09 stsp memcpy(p->crc32, packidx->crc32, nobj * sizeof(*p->crc32));
343 79b11c62 2018-03-09 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
344 79b11c62 2018-03-09 stsp if (p->offsets == NULL)
346 79b11c62 2018-03-09 stsp memcpy(p->offsets, packidx->offsets, nobj * sizeof(*p->offsets));
348 79b11c62 2018-03-09 stsp if (p->large_offsets) {
349 79b11c62 2018-03-09 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
350 79b11c62 2018-03-09 stsp if (p->large_offsets == NULL)
352 79b11c62 2018-03-09 stsp memcpy(p->large_offsets, packidx->large_offsets,
353 79b11c62 2018-03-09 stsp nobj * sizeof(*p->large_offsets));
359 79b11c62 2018-03-09 stsp free(p->large_offsets);
360 79b11c62 2018-03-09 stsp free(p->offsets);
361 79b11c62 2018-03-09 stsp free(p->crc32);
362 79b11c62 2018-03-09 stsp free(p->sorted_ids);
364 79b11c62 2018-03-09 stsp return NULL;
367 87c99799 2018-03-16 stsp static const struct got_error *
368 87c99799 2018-03-16 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
369 87c99799 2018-03-16 stsp struct got_packidx_v2_hdr *packidx)
371 87c99799 2018-03-16 stsp char *path_packdir;
372 87c99799 2018-03-16 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
373 87c99799 2018-03-16 stsp char *sha1str;
375 87c99799 2018-03-16 stsp path_packdir = got_repo_get_path_objects_pack(repo);
376 87c99799 2018-03-16 stsp if (path_packdir == NULL)
377 87c99799 2018-03-16 stsp return got_error(GOT_ERR_NO_MEM);
379 87c99799 2018-03-16 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
380 87c99799 2018-03-16 stsp hex, sizeof(hex));
381 87c99799 2018-03-16 stsp if (sha1str == NULL)
382 87c99799 2018-03-16 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
384 87c99799 2018-03-16 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
385 87c99799 2018-03-16 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
386 87c99799 2018-03-16 stsp *path_packfile = NULL;
387 87c99799 2018-03-16 stsp return got_error(GOT_ERR_NO_MEM);
390 87c99799 2018-03-16 stsp return NULL;
393 87c99799 2018-03-16 stsp static const struct got_error *
394 87c99799 2018-03-16 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
396 87c99799 2018-03-16 stsp const struct got_error *err = NULL;
397 87c99799 2018-03-16 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
398 87c99799 2018-03-16 stsp struct got_packfile_hdr hdr;
401 87c99799 2018-03-16 stsp n = fread(&hdr, sizeof(hdr), 1, f);
402 87c99799 2018-03-16 stsp if (n != 1)
403 87c99799 2018-03-16 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
405 87c99799 2018-03-16 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
406 87c99799 2018-03-16 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
407 87c99799 2018-03-16 stsp betoh32(hdr.nobjects) != totobj)
408 87c99799 2018-03-16 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
410 87c99799 2018-03-16 stsp return err;
413 87c99799 2018-03-16 stsp static const struct got_error *
414 87c99799 2018-03-16 stsp cache_pack(struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
416 87c99799 2018-03-16 stsp const struct got_error *err;
417 87c99799 2018-03-16 stsp FILE *packfile;
418 87c99799 2018-03-16 stsp char *path_packfile;
421 87c99799 2018-03-16 stsp err = get_packfile_path(&path_packfile, repo, packidx);
423 87c99799 2018-03-16 stsp return err;
425 87c99799 2018-03-16 stsp packfile = fopen(path_packfile, "rb");
426 87c99799 2018-03-16 stsp if (packfile == NULL) {
427 87c99799 2018-03-16 stsp err = got_error_from_errno();
428 87c99799 2018-03-16 stsp free(path_packfile);
429 87c99799 2018-03-16 stsp return err;
431 87c99799 2018-03-16 stsp err = read_packfile_hdr(packfile, packidx);
433 87c99799 2018-03-16 stsp fclose(packfile);
434 87c99799 2018-03-16 stsp return err;
437 87c99799 2018-03-16 stsp for (i = 0; i < nitems(repo->pack_cache); i++) {
438 87c99799 2018-03-16 stsp if (repo->pack_cache[i].packidx == NULL)
442 87c99799 2018-03-16 stsp if (i == nitems(repo->pack_cache)) {
443 87c99799 2018-03-16 stsp got_packidx_close(repo->pack_cache[i - 1].packidx);
444 87c99799 2018-03-16 stsp fclose(repo->pack_cache[i - 1].packfile);
445 87c99799 2018-03-16 stsp free(repo->pack_cache[i - 1].path_packfile);
446 87c99799 2018-03-16 stsp memmove(&repo->pack_cache[1], &repo->pack_cache[0],
447 87c99799 2018-03-16 stsp sizeof(repo->pack_cache) - sizeof(repo->pack_cache[0]));
451 87c99799 2018-03-16 stsp repo->pack_cache[i].packidx = dup_packidx(packidx);
452 87c99799 2018-03-16 stsp if (repo->pack_cache[i].packidx != NULL) {
453 87c99799 2018-03-16 stsp repo->pack_cache[i].packfile = packfile;
454 87c99799 2018-03-16 stsp repo->pack_cache[i].path_packfile = path_packfile;
456 87c99799 2018-03-16 stsp fclose(packfile);
457 87c99799 2018-03-16 stsp free(path_packfile);
459 87c99799 2018-03-16 stsp return NULL;
462 6b9c9673 2018-01-23 stsp static const struct got_error *
463 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
464 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
466 6b9c9673 2018-01-23 stsp const struct got_error *err;
467 6b9c9673 2018-01-23 stsp char *path_packdir;
468 6b9c9673 2018-01-23 stsp DIR *packdir;
469 6b9c9673 2018-01-23 stsp struct dirent *dent;
470 6b9c9673 2018-01-23 stsp char *path_packidx;
473 87c99799 2018-03-16 stsp /* Search pack cache. */
474 87c99799 2018-03-16 stsp for (i = 0; i < nitems(repo->pack_cache); i++) {
475 87c99799 2018-03-16 stsp if (repo->pack_cache[i].packidx == NULL)
477 87c99799 2018-03-16 stsp *idx = get_object_idx(repo->pack_cache[i].packidx, id);
478 79b11c62 2018-03-09 stsp if (*idx != -1) {
479 87c99799 2018-03-16 stsp *packidx = dup_packidx(repo->pack_cache[i].packidx);
480 79b11c62 2018-03-09 stsp if (*packidx == NULL)
481 0a71ee67 2018-03-09 stsp return got_error(GOT_ERR_NO_MEM);
482 79b11c62 2018-03-09 stsp return NULL;
485 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
487 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
488 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
489 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
491 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
492 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
493 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
497 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
498 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
501 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
502 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
503 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
507 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
508 6b9c9673 2018-01-23 stsp free(path_packidx);
512 6b9c9673 2018-01-23 stsp *idx = get_object_idx(*packidx, id);
513 6b9c9673 2018-01-23 stsp if (*idx != -1) {
514 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
515 87c99799 2018-03-16 stsp cache_pack(*packidx, repo);
519 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
520 6b9c9673 2018-01-23 stsp *packidx = NULL;
523 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
525 6b9c9673 2018-01-23 stsp free(path_packdir);
526 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
527 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
528 6b9c9673 2018-01-23 stsp return err;
531 c7fe698a 2018-01-23 stsp static const struct got_error *
532 87c99799 2018-03-16 stsp open_packfile(FILE **packfile, char **path_packfile, int *is_cached,
533 c7fe698a 2018-01-23 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
535 c7fe698a 2018-01-23 stsp const struct got_error *err;
538 c7fe698a 2018-01-23 stsp *packfile = NULL;
539 87c99799 2018-03-16 stsp *path_packfile = NULL;
540 87c99799 2018-03-16 stsp *is_cached = 0;
542 87c99799 2018-03-16 stsp /* The pack could already be cached after an object search. */
543 87c99799 2018-03-16 stsp for (i = 0; i < nitems(repo->pack_cache); i++) {
544 87c99799 2018-03-16 stsp if (repo->pack_cache[i].packidx == NULL)
547 87c99799 2018-03-16 stsp /* The pack index trailer acts as a cache key. */
548 87c99799 2018-03-16 stsp if (memcmp(&packidx->trailer,
549 87c99799 2018-03-16 stsp &repo->pack_cache[i].packidx->trailer,
550 87c99799 2018-03-16 stsp sizeof(packidx->trailer)) != 0)
553 87c99799 2018-03-16 stsp *packfile = repo->pack_cache[i].packfile;
554 87c99799 2018-03-16 stsp *path_packfile = repo->pack_cache[i].path_packfile;
555 87c99799 2018-03-16 stsp *is_cached = 1;
556 87c99799 2018-03-16 stsp return NULL;
558 87c99799 2018-03-16 stsp /* No luck. Try the filesystem. */
560 c7fe698a 2018-01-23 stsp err = get_packfile_path(path_packfile, repo, packidx);
562 c7fe698a 2018-01-23 stsp return err;
564 c7fe698a 2018-01-23 stsp *packfile = fopen(*path_packfile, "rb");
565 c7fe698a 2018-01-23 stsp if (*packfile == NULL) {
566 c7fe698a 2018-01-23 stsp err = got_error_from_errno();
567 c7fe698a 2018-01-23 stsp free(*path_packfile);
568 c7fe698a 2018-01-23 stsp return err;
571 c7fe698a 2018-01-23 stsp err = read_packfile_hdr(*packfile, packidx);
573 c7fe698a 2018-01-23 stsp fclose(*packfile);
574 c7fe698a 2018-01-23 stsp *packfile = NULL;
576 c7fe698a 2018-01-23 stsp return err;
579 c7fe698a 2018-01-23 stsp static const struct got_error *
580 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
581 c3703302 2018-01-23 stsp FILE *packfile)
583 a487c1d0 2018-01-14 stsp uint8_t t = 0;
584 a487c1d0 2018-01-14 stsp uint64_t s = 0;
585 a487c1d0 2018-01-14 stsp uint8_t sizeN;
590 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
592 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
594 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
595 a487c1d0 2018-01-14 stsp if (n != 1)
596 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
598 a1fd68d8 2018-01-12 stsp if (i == 0) {
599 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
600 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
601 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
603 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
604 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
607 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
611 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
612 a487c1d0 2018-01-14 stsp return NULL;
615 a1fd68d8 2018-01-12 stsp static const struct got_error *
616 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
617 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
619 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
620 6ccb713b 2018-01-19 stsp if (*obj == NULL)
621 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
623 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
624 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
625 6ccb713b 2018-01-19 stsp free(*obj);
626 6ccb713b 2018-01-19 stsp *obj = NULL;
627 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
630 6ccb713b 2018-01-19 stsp (*obj)->type = type;
631 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
632 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
633 6ccb713b 2018-01-19 stsp (*obj)->size = size;
634 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
635 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
637 b107e67f 2018-01-19 stsp return NULL;
640 b107e67f 2018-01-19 stsp static const struct got_error *
641 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
643 b107e67f 2018-01-19 stsp int64_t o = 0;
644 b107e67f 2018-01-19 stsp uint8_t offN;
649 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
651 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
653 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
654 b107e67f 2018-01-19 stsp if (n != 1)
655 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
657 b107e67f 2018-01-19 stsp if (i == 0)
658 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
662 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
665 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
667 b107e67f 2018-01-19 stsp *offset = o;
668 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
669 6ccb713b 2018-01-19 stsp return NULL;
672 6ccb713b 2018-01-19 stsp static const struct got_error *
673 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
675 96f5e8b3 2018-01-23 stsp const struct got_error *err;
676 b107e67f 2018-01-19 stsp int64_t negoffset;
677 b107e67f 2018-01-19 stsp size_t negofflen;
679 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
681 b107e67f 2018-01-19 stsp return err;
683 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
684 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
685 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
686 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
688 96f5e8b3 2018-01-23 stsp return NULL;
691 0e22967e 2018-02-11 stsp static const struct got_error *
692 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
693 0e22967e 2018-02-11 stsp FILE *, const char *, off_t, size_t, int, size_t);
695 96f5e8b3 2018-01-23 stsp static const struct got_error *
696 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
697 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
698 bdd2fbb3 2018-02-11 stsp size_t delta_data_offset)
700 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
702 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
703 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
704 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
705 bdd2fbb3 2018-02-11 stsp return got_error(GOT_ERR_NO_MEM);
706 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
707 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
708 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
709 bdd2fbb3 2018-02-11 stsp return NULL;
712 bdd2fbb3 2018-02-11 stsp static const struct got_error *
713 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
714 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
715 bdd2fbb3 2018-02-11 stsp off_t delta_offset,size_t tslen, int delta_type, size_t delta_size)
718 a3500804 2018-01-23 stsp const struct got_error *err;
719 c3703302 2018-01-23 stsp off_t base_offset;
720 c3703302 2018-01-23 stsp uint8_t base_type;
721 c3703302 2018-01-23 stsp uint64_t base_size;
722 c3703302 2018-01-23 stsp size_t base_tslen;
723 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
725 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
727 a3500804 2018-01-23 stsp return err;
729 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
730 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
731 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
733 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
734 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
736 bdd2fbb3 2018-02-11 stsp return err;
738 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
739 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
740 b107e67f 2018-01-19 stsp return got_error_from_errno();
742 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
745 b107e67f 2018-01-19 stsp return err;
747 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
748 0e22967e 2018-02-11 stsp base_offset, base_tslen, base_type, base_size);
751 c3703302 2018-01-23 stsp static const struct got_error *
752 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
753 bdd2fbb3 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset,
754 bdd2fbb3 2018-02-11 stsp size_t tslen, int delta_type, size_t delta_size)
756 6b9c9673 2018-01-23 stsp const struct got_error *err;
757 6b9c9673 2018-01-23 stsp struct got_object_id id;
758 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
760 6b9c9673 2018-01-23 stsp off_t base_offset;
761 6b9c9673 2018-01-23 stsp uint8_t base_type;
762 6b9c9673 2018-01-23 stsp uint64_t base_size;
763 6b9c9673 2018-01-23 stsp size_t base_tslen;
765 87c99799 2018-03-16 stsp FILE *base_packfile = NULL;
766 87c99799 2018-03-16 stsp char *path_base_packfile = NULL;
767 87c99799 2018-03-16 stsp int pack_cached = 0;
768 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
770 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
771 6b9c9673 2018-01-23 stsp if (n != 1)
772 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
774 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
775 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
776 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
778 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
779 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
781 bdd2fbb3 2018-02-11 stsp return err;
783 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
785 6b9c9673 2018-01-23 stsp return err;
787 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
788 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
789 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
790 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
793 87c99799 2018-03-16 stsp err = open_packfile(&base_packfile, &path_base_packfile, &pack_cached,
794 87c99799 2018-03-16 stsp repo, packidx);
795 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
797 6b9c9673 2018-01-23 stsp return err;
799 6b9c9673 2018-01-23 stsp if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
800 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
804 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
805 6b9c9673 2018-01-23 stsp base_packfile);
809 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(deltas, repo, base_packfile,
810 0e22967e 2018-02-11 stsp path_base_packfile, base_offset, base_tslen, base_type,
811 0e22967e 2018-02-11 stsp base_size);
813 87c99799 2018-03-16 stsp if (!pack_cached) {
814 87c99799 2018-03-16 stsp free(path_base_packfile);
815 87c99799 2018-03-16 stsp if (base_packfile && fclose(base_packfile) == -1 && err == 0)
816 87c99799 2018-03-16 stsp err = got_error_from_errno();
818 6b9c9673 2018-01-23 stsp return err;
821 6b9c9673 2018-01-23 stsp static const struct got_error *
822 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
823 0e22967e 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
824 0e22967e 2018-02-11 stsp int delta_type, size_t delta_size)
826 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
828 c3703302 2018-01-23 stsp switch (delta_type) {
829 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
830 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
831 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
832 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
833 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
834 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
835 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, 0);
837 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
838 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
839 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
840 bdd2fbb3 2018-02-11 stsp delta_size);
842 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
843 bdd2fbb3 2018-02-11 stsp err = resolve_ref_delta(deltas, repo, packfile,
844 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
845 bdd2fbb3 2018-02-11 stsp delta_size);
848 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
851 96f5e8b3 2018-01-23 stsp return err;
854 96f5e8b3 2018-01-23 stsp static const struct got_error *
855 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
856 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
857 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
858 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
860 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
861 96f5e8b3 2018-01-23 stsp int resolved_type;
863 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
864 b107e67f 2018-01-19 stsp if (*obj == NULL)
865 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
867 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
868 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
869 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
870 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
871 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
873 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
874 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
875 96f5e8b3 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
878 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
880 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
881 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
883 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
884 0e22967e 2018-02-11 stsp path_packfile, offset, tslen, delta_type, delta_size);
888 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
891 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
895 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
896 96f5e8b3 2018-01-23 stsp *obj = NULL;
898 96f5e8b3 2018-01-23 stsp return err;
901 b107e67f 2018-01-19 stsp static const struct got_error *
902 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
903 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
905 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
906 a1fd68d8 2018-01-12 stsp off_t offset;
907 87c99799 2018-03-16 stsp char *path_packfile = NULL;
908 87c99799 2018-03-16 stsp FILE *packfile = NULL;
909 87c99799 2018-03-16 stsp int pack_cached = 0;
910 6c00b545 2018-01-17 stsp uint8_t type;
911 6c00b545 2018-01-17 stsp uint64_t size;
912 3ee5fc21 2018-01-17 stsp size_t tslen;
914 6c00b545 2018-01-17 stsp *obj = NULL;
916 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
917 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
918 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
920 87c99799 2018-03-16 stsp err = open_packfile(&packfile, &path_packfile, &pack_cached,
921 87c99799 2018-03-16 stsp repo, packidx);
923 6b9c9673 2018-01-23 stsp return err;
925 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
926 6c00b545 2018-01-17 stsp err = got_error_from_errno();
930 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&type, &size, &tslen, packfile);
934 6c00b545 2018-01-17 stsp switch (type) {
935 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
936 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
937 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
938 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
939 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
940 6ccb713b 2018-01-19 stsp offset + tslen, size);
943 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
944 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
945 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
946 a48db7e5 2018-01-23 stsp packfile, id, offset, tslen, type, size);
950 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
954 87c99799 2018-03-16 stsp if (!pack_cached) {
955 87c99799 2018-03-16 stsp free(path_packfile);
956 87c99799 2018-03-16 stsp if (packfile && fclose(packfile) == -1 && err == 0)
957 87c99799 2018-03-16 stsp err = got_error_from_errno();
959 a1fd68d8 2018-01-12 stsp return err;
962 a1fd68d8 2018-01-12 stsp const struct got_error *
963 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
964 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
966 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
967 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
970 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
972 6b9c9673 2018-01-23 stsp return err;
974 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
975 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
976 3ee5fc21 2018-01-17 stsp return err;
979 efd2a263 2018-01-19 stsp static const struct got_error *
980 22484865 2018-03-13 stsp get_delta_sizes(uint64_t *base_size, uint64_t *result_size,
981 22484865 2018-03-13 stsp struct got_delta *delta)
983 22484865 2018-03-13 stsp const struct got_error *err;
984 22484865 2018-03-13 stsp uint8_t *delta_buf = NULL;
985 22484865 2018-03-13 stsp size_t delta_len = 0;
986 22484865 2018-03-13 stsp FILE *delta_file;
988 22484865 2018-03-13 stsp delta_file = fopen(delta->path_packfile, "rb");
989 22484865 2018-03-13 stsp if (delta_file == NULL)
990 22484865 2018-03-13 stsp return got_error_from_errno();
992 22484865 2018-03-13 stsp if (fseeko(delta_file, delta->data_offset, SEEK_CUR) != 0) {
993 22484865 2018-03-13 stsp err = got_error_from_errno();
994 22484865 2018-03-13 stsp fclose(delta_file);
995 22484865 2018-03-13 stsp return err;
998 22484865 2018-03-13 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, delta_file);
999 22484865 2018-03-13 stsp fclose(delta_file);
1001 22484865 2018-03-13 stsp return err;
1003 22484865 2018-03-13 stsp err = got_delta_get_sizes(base_size, result_size, delta_buf, delta_len);
1004 22484865 2018-03-13 stsp free(delta_buf);
1005 22484865 2018-03-13 stsp return err;
1008 22484865 2018-03-13 stsp static const struct got_error *
1009 22484865 2018-03-13 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1011 22484865 2018-03-13 stsp struct got_delta *delta;
1012 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1014 22484865 2018-03-13 stsp *max_size = 0;
1015 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1016 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1017 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1018 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1019 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1020 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1021 22484865 2018-03-13 stsp const struct got_error *err;
1022 22484865 2018-03-13 stsp err = get_delta_sizes(&base_size, &result_size, delta);
1024 22484865 2018-03-13 stsp return err;
1026 22484865 2018-03-13 stsp base_size = delta->size;
1027 22484865 2018-03-13 stsp if (base_size > *max_size)
1028 22484865 2018-03-13 stsp *max_size = base_size;
1029 22484865 2018-03-13 stsp if (result_size > *max_size)
1030 22484865 2018-03-13 stsp *max_size = result_size;
1033 22484865 2018-03-13 stsp return NULL;
1037 bd1223b9 2018-03-14 stsp clear_delta_cache_entry(struct got_delta_cache_entry *entry)
1039 bd1223b9 2018-03-14 stsp entry->data_offset = 0;
1040 bd1223b9 2018-03-14 stsp free(entry->delta_buf);
1041 bd1223b9 2018-03-14 stsp entry->delta_buf = NULL;
1042 bd1223b9 2018-03-14 stsp entry->delta_len = 0;
1045 9feb4ff2 2018-03-14 stsp const struct got_error *
1046 bd1223b9 2018-03-14 stsp add_delta_cache_entry(struct got_delta_cache *cache, off_t data_offset,
1047 bd1223b9 2018-03-14 stsp uint8_t *delta_buf, size_t delta_len)
1050 bd1223b9 2018-03-14 stsp struct got_delta_cache_entry *entry;
1052 bd1223b9 2018-03-14 stsp for (i = 0; i < nitems(cache->deltas); i++) {
1053 bd1223b9 2018-03-14 stsp entry = &cache->deltas[i];
1054 bd1223b9 2018-03-14 stsp if (entry->data_offset == 0)
1058 bd1223b9 2018-03-14 stsp if (i == nitems(cache->deltas)) {
1059 bd1223b9 2018-03-14 stsp entry = &cache->deltas[i - 1];
1060 bd1223b9 2018-03-14 stsp clear_delta_cache_entry(entry);
1061 bd1223b9 2018-03-14 stsp memmove(&cache->deltas[1], &cache->deltas[0],
1062 bd1223b9 2018-03-14 stsp sizeof(cache->deltas) - sizeof(cache->deltas[0]));
1066 bd1223b9 2018-03-14 stsp entry = &cache->deltas[i];
1067 bd1223b9 2018-03-14 stsp entry->data_offset = data_offset;
1068 9feb4ff2 2018-03-14 stsp entry->delta_buf = delta_buf;
1069 bd1223b9 2018-03-14 stsp entry->delta_len = delta_len;
1070 9feb4ff2 2018-03-14 stsp return NULL;
1073 9feb4ff2 2018-03-14 stsp const struct got_error *
1074 bd1223b9 2018-03-14 stsp cache_delta(off_t data_offset, uint8_t *delta_buf, size_t delta_len,
1075 bd1223b9 2018-03-14 stsp const char *path_packfile, struct got_repository *repo)
1077 bd1223b9 2018-03-14 stsp struct got_delta_cache *cache;
1080 bd1223b9 2018-03-14 stsp for (i = 0; i < nitems(repo->delta_cache); i++) {
1081 bd1223b9 2018-03-14 stsp cache = &repo->delta_cache[i];
1082 bd1223b9 2018-03-14 stsp if (cache->path_packfile == NULL)
1084 9feb4ff2 2018-03-14 stsp if (strcmp(cache->path_packfile, path_packfile) == 0)
1085 9feb4ff2 2018-03-14 stsp return add_delta_cache_entry(cache, data_offset,
1086 9feb4ff2 2018-03-14 stsp delta_buf, delta_len);
1089 bd1223b9 2018-03-14 stsp if (i == nitems(repo->delta_cache)) {
1091 bd1223b9 2018-03-14 stsp cache = &repo->delta_cache[i - 1];
1092 bd1223b9 2018-03-14 stsp free(cache->path_packfile);
1093 bd1223b9 2018-03-14 stsp cache->path_packfile = NULL;
1094 bd1223b9 2018-03-14 stsp for (j = 0; j < nitems(cache->deltas); j++) {
1095 bd1223b9 2018-03-14 stsp struct got_delta_cache_entry *entry = &cache->deltas[j];
1096 bd1223b9 2018-03-14 stsp if (entry->data_offset == 0)
1098 bd1223b9 2018-03-14 stsp clear_delta_cache_entry(entry);
1100 bd1223b9 2018-03-14 stsp memmove(&repo->delta_cache[1], &repo->delta_cache[0],
1101 bd1223b9 2018-03-14 stsp sizeof(repo->delta_cache) - sizeof(repo->delta_cache[0]));
1105 bd1223b9 2018-03-14 stsp cache = &repo->delta_cache[i];
1106 bd1223b9 2018-03-14 stsp cache->path_packfile = strdup(path_packfile);
1107 bd1223b9 2018-03-14 stsp if (cache->path_packfile == NULL)
1108 9feb4ff2 2018-03-14 stsp return got_error(GOT_ERR_NO_MEM);
1109 9feb4ff2 2018-03-14 stsp return add_delta_cache_entry(cache, data_offset, delta_buf, delta_len);
1113 bd1223b9 2018-03-14 stsp get_cached_delta(uint8_t **delta_buf, size_t *delta_len,
1114 bd1223b9 2018-03-14 stsp off_t data_offset, const char *path_packfile, struct got_repository *repo)
1116 bd1223b9 2018-03-14 stsp struct got_delta_cache *cache;
1117 bd1223b9 2018-03-14 stsp struct got_delta_cache_entry *entry;
1120 bd1223b9 2018-03-14 stsp *delta_buf = NULL;
1121 bd1223b9 2018-03-14 stsp *delta_len = 0;
1123 bd1223b9 2018-03-14 stsp for (i = 0; i < nitems(repo->delta_cache); i++) {
1124 bd1223b9 2018-03-14 stsp cache = &repo->delta_cache[i];
1125 bd1223b9 2018-03-14 stsp if (cache->path_packfile == NULL)
1127 bd1223b9 2018-03-14 stsp if (strcmp(cache->path_packfile, path_packfile) == 0)
1131 bd1223b9 2018-03-14 stsp if (i == nitems(repo->delta_cache))
1134 bd1223b9 2018-03-14 stsp for (i = 0; i < nitems(cache->deltas); i++) {
1135 bd1223b9 2018-03-14 stsp entry = &cache->deltas[i];
1136 bd1223b9 2018-03-14 stsp if (entry->data_offset == 0)
1138 bd1223b9 2018-03-14 stsp if (entry->data_offset == data_offset) {
1139 bd1223b9 2018-03-14 stsp *delta_buf = entry->delta_buf;
1140 bd1223b9 2018-03-14 stsp *delta_len = entry->delta_len;
1146 22484865 2018-03-13 stsp static const struct got_error *
1147 40dc5ff8 2018-03-16 stsp dump_delta_chain_to_file(struct got_delta_chain *deltas, FILE *outfile,
1148 bd1223b9 2018-03-14 stsp const char *path_packfile, struct got_repository *repo)
1150 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1151 6691714a 2018-01-23 stsp struct got_delta *delta;
1152 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1153 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1154 8628c62d 2018-03-15 stsp size_t accum_size;
1155 22484865 2018-03-13 stsp uint64_t max_size;
1156 6691714a 2018-01-23 stsp int n = 0;
1158 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1159 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1161 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1162 22484865 2018-03-13 stsp err = get_delta_chain_max_size(&max_size, deltas);
1164 22484865 2018-03-13 stsp return err;
1165 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1166 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1167 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1168 8628c62d 2018-03-15 stsp return got_error(GOT_ERR_NO_MEM);
1170 22484865 2018-03-13 stsp base_file = got_opentemp();
1171 8628c62d 2018-03-15 stsp if (base_file == NULL)
1172 8628c62d 2018-03-15 stsp return got_error_from_errno();
1174 22484865 2018-03-13 stsp accum_file = got_opentemp();
1175 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1176 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1177 8628c62d 2018-03-15 stsp fclose(base_file);
1178 8628c62d 2018-03-15 stsp return err;
1182 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1183 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1184 885d3e02 2018-01-27 stsp uint8_t *delta_buf = NULL;
1185 885d3e02 2018-01-27 stsp size_t delta_len = 0;
1187 a6b158cc 2018-02-11 stsp if (n == 0) {
1188 9db65d41 2018-03-14 stsp FILE *delta_file;
1189 8628c62d 2018-03-15 stsp size_t base_len;
1191 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1192 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1193 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1194 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1195 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1196 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1197 a6b158cc 2018-02-11 stsp goto done;
1200 9db65d41 2018-03-14 stsp delta_file = fopen(delta->path_packfile, "rb");
1201 9db65d41 2018-03-14 stsp if (delta_file == NULL) {
1202 9db65d41 2018-03-14 stsp err = got_error_from_errno();
1203 9db65d41 2018-03-14 stsp goto done;
1206 bdd2fbb3 2018-02-11 stsp if (fseeko(delta_file, delta->offset + delta->tslen,
1207 bdd2fbb3 2018-02-11 stsp SEEK_SET) != 0) {
1208 bdd2fbb3 2018-02-11 stsp fclose(delta_file);
1209 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
1210 bdd2fbb3 2018-02-11 stsp goto done;
1212 8628c62d 2018-03-15 stsp if (base_file)
1213 8628c62d 2018-03-15 stsp err = got_inflate_to_file(&delta_len,
1214 8628c62d 2018-03-15 stsp delta_file, base_file);
1216 8628c62d 2018-03-15 stsp err = got_inflate_to_mem(&base_buf, &base_len,
1217 8628c62d 2018-03-15 stsp delta_file);
1218 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1219 8628c62d 2018-03-15 stsp uint8_t *p;
1220 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1221 8628c62d 2018-03-15 stsp if (p == NULL) {
1222 8628c62d 2018-03-15 stsp err = got_error(GOT_ERR_NO_MEM);
1223 8628c62d 2018-03-15 stsp goto done;
1225 8628c62d 2018-03-15 stsp base_buf = p;
1228 a6b158cc 2018-02-11 stsp fclose(delta_file);
1230 a6b158cc 2018-02-11 stsp goto done;
1232 8628c62d 2018-03-15 stsp if (base_file)
1233 8628c62d 2018-03-15 stsp rewind(base_file);
1237 bd1223b9 2018-03-14 stsp get_cached_delta(&delta_buf, &delta_len, delta->data_offset,
1238 bd1223b9 2018-03-14 stsp path_packfile, repo);
1239 bd1223b9 2018-03-14 stsp if (delta_buf == NULL) {
1240 9db65d41 2018-03-14 stsp FILE *delta_file = fopen(delta->path_packfile, "rb");
1241 9db65d41 2018-03-14 stsp if (delta_file == NULL) {
1242 9db65d41 2018-03-14 stsp err = got_error_from_errno();
1243 9db65d41 2018-03-14 stsp goto done;
1245 bd1223b9 2018-03-14 stsp if (fseeko(delta_file, delta->data_offset, SEEK_CUR)
1247 bd1223b9 2018-03-14 stsp fclose(delta_file);
1248 bd1223b9 2018-03-14 stsp err = got_error_from_errno();
1249 bd1223b9 2018-03-14 stsp goto done;
1252 bd1223b9 2018-03-14 stsp /* Delta streams should always fit in memory. */
1253 bd1223b9 2018-03-14 stsp err = got_inflate_to_mem(&delta_buf, &delta_len,
1254 bd1223b9 2018-03-14 stsp delta_file);
1255 0e22967e 2018-02-11 stsp fclose(delta_file);
1257 bd1223b9 2018-03-14 stsp goto done;
1259 9feb4ff2 2018-03-14 stsp err = cache_delta(delta->data_offset, delta_buf,
1260 9feb4ff2 2018-03-14 stsp delta_len, path_packfile, repo);
1262 9feb4ff2 2018-03-14 stsp goto done;
1264 9feb4ff2 2018-03-14 stsp /* delta_buf is now cached */
1266 8628c62d 2018-03-15 stsp if (base_buf) {
1267 8628c62d 2018-03-15 stsp err = got_delta_apply_in_mem(base_buf, delta_buf,
1268 8628c62d 2018-03-15 stsp delta_len, accum_buf, &accum_size);
1271 8628c62d 2018-03-15 stsp err = got_delta_apply(base_file, delta_buf, delta_len,
1272 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1273 8628c62d 2018-03-15 stsp ++n < deltas->nentries ? accum_file : outfile);
1276 6691714a 2018-01-23 stsp goto done;
1278 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1279 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1280 8628c62d 2018-03-15 stsp if (base_buf) {
1281 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1282 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1283 8628c62d 2018-03-15 stsp base_buf = tmp;
1285 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1286 8628c62d 2018-03-15 stsp accum_file = base_file;
1287 8628c62d 2018-03-15 stsp base_file = tmp;
1288 8628c62d 2018-03-15 stsp rewind(base_file);
1289 8628c62d 2018-03-15 stsp rewind(accum_file);
1295 8628c62d 2018-03-15 stsp free(base_buf);
1296 8628c62d 2018-03-15 stsp if (accum_buf) {
1297 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1298 8628c62d 2018-03-15 stsp free(accum_buf);
1299 8628c62d 2018-03-15 stsp if (len != accum_size)
1300 8628c62d 2018-03-15 stsp return got_ferror(outfile, GOT_ERR_IO);
1302 8628c62d 2018-03-15 stsp if (base_file)
1303 8628c62d 2018-03-15 stsp fclose(base_file);
1304 8628c62d 2018-03-15 stsp if (accum_file)
1305 8628c62d 2018-03-15 stsp fclose(accum_file);
1306 6691714a 2018-01-23 stsp rewind(outfile);
1307 e0ab43e7 2018-03-16 stsp return err;
1310 e0ab43e7 2018-03-16 stsp static const struct got_error *
1311 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1312 e0ab43e7 2018-03-16 stsp struct got_delta_chain *deltas, const char *path_packfile,
1313 e0ab43e7 2018-03-16 stsp struct got_repository *repo)
1315 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1316 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1317 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1318 e0ab43e7 2018-03-16 stsp size_t accum_size;
1319 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1320 e0ab43e7 2018-03-16 stsp int n = 0;
1322 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1323 e0ab43e7 2018-03-16 stsp *outlen = 0;
1325 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1326 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1328 e0ab43e7 2018-03-16 stsp err = get_delta_chain_max_size(&max_size, deltas);
1330 e0ab43e7 2018-03-16 stsp return err;
1331 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1332 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1333 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_NO_MEM);
1335 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1336 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1337 e0ab43e7 2018-03-16 stsp uint8_t *delta_buf = NULL;
1338 e0ab43e7 2018-03-16 stsp size_t delta_len = 0;
1340 e0ab43e7 2018-03-16 stsp if (n == 0) {
1341 e0ab43e7 2018-03-16 stsp FILE *delta_file;
1342 e0ab43e7 2018-03-16 stsp size_t base_len;
1344 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1345 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1346 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1347 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1348 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1349 e0ab43e7 2018-03-16 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1350 e0ab43e7 2018-03-16 stsp goto done;
1353 e0ab43e7 2018-03-16 stsp delta_file = fopen(delta->path_packfile, "rb");
1354 e0ab43e7 2018-03-16 stsp if (delta_file == NULL) {
1355 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1356 e0ab43e7 2018-03-16 stsp goto done;
1359 e0ab43e7 2018-03-16 stsp if (fseeko(delta_file, delta->offset + delta->tslen,
1360 e0ab43e7 2018-03-16 stsp SEEK_SET) != 0) {
1361 e0ab43e7 2018-03-16 stsp fclose(delta_file);
1362 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1363 e0ab43e7 2018-03-16 stsp goto done;
1365 e0ab43e7 2018-03-16 stsp err = got_inflate_to_mem(&base_buf, &base_len,
1366 e0ab43e7 2018-03-16 stsp delta_file);
1367 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1368 e0ab43e7 2018-03-16 stsp uint8_t *p;
1369 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1370 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1371 e0ab43e7 2018-03-16 stsp err = got_error(GOT_ERR_NO_MEM);
1372 e0ab43e7 2018-03-16 stsp goto done;
1374 e0ab43e7 2018-03-16 stsp base_buf = p;
1376 e0ab43e7 2018-03-16 stsp fclose(delta_file);
1378 e0ab43e7 2018-03-16 stsp goto done;
1383 e0ab43e7 2018-03-16 stsp get_cached_delta(&delta_buf, &delta_len, delta->data_offset,
1384 e0ab43e7 2018-03-16 stsp path_packfile, repo);
1385 e0ab43e7 2018-03-16 stsp if (delta_buf == NULL) {
1386 e0ab43e7 2018-03-16 stsp FILE *delta_file = fopen(delta->path_packfile, "rb");
1387 e0ab43e7 2018-03-16 stsp if (delta_file == NULL) {
1388 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1389 e0ab43e7 2018-03-16 stsp goto done;
1391 e0ab43e7 2018-03-16 stsp if (fseeko(delta_file, delta->data_offset, SEEK_CUR)
1393 e0ab43e7 2018-03-16 stsp fclose(delta_file);
1394 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1395 e0ab43e7 2018-03-16 stsp goto done;
1398 e0ab43e7 2018-03-16 stsp /* Delta streams should always fit in memory. */
1399 e0ab43e7 2018-03-16 stsp err = got_inflate_to_mem(&delta_buf, &delta_len,
1400 e0ab43e7 2018-03-16 stsp delta_file);
1401 e0ab43e7 2018-03-16 stsp fclose(delta_file);
1403 e0ab43e7 2018-03-16 stsp goto done;
1405 e0ab43e7 2018-03-16 stsp err = cache_delta(delta->data_offset, delta_buf,
1406 e0ab43e7 2018-03-16 stsp delta_len, path_packfile, repo);
1408 e0ab43e7 2018-03-16 stsp goto done;
1410 e0ab43e7 2018-03-16 stsp /* delta_buf is now cached */
1412 e0ab43e7 2018-03-16 stsp err = got_delta_apply_in_mem(base_buf, delta_buf,
1413 e0ab43e7 2018-03-16 stsp delta_len, accum_buf, &accum_size);
1416 e0ab43e7 2018-03-16 stsp goto done;
1418 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1419 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1420 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1421 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1422 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1427 e0ab43e7 2018-03-16 stsp free(base_buf);
1428 e0ab43e7 2018-03-16 stsp if (err) {
1429 e0ab43e7 2018-03-16 stsp free(accum_buf);
1430 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1431 e0ab43e7 2018-03-16 stsp *outlen = 0;
1433 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1434 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1436 efd2a263 2018-01-19 stsp return err;
1439 3ee5fc21 2018-01-17 stsp const struct got_error *
1440 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1441 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1443 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1444 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
1446 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1447 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1449 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
1450 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
1451 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
1452 3ee5fc21 2018-01-17 stsp goto done;
1455 6691714a 2018-01-23 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1456 6691714a 2018-01-23 stsp packfile = fopen(obj->path_packfile, "rb");
1457 6691714a 2018-01-23 stsp if (packfile == NULL) {
1458 6691714a 2018-01-23 stsp err = got_error_from_errno();
1459 6691714a 2018-01-23 stsp goto done;
1462 6691714a 2018-01-23 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
1463 6691714a 2018-01-23 stsp err = got_error_from_errno();
1464 6691714a 2018-01-23 stsp goto done;
1467 3606d7fc 2018-02-11 stsp err = got_inflate_to_file(&obj->size, packfile, *f);
1469 40dc5ff8 2018-03-16 stsp err = dump_delta_chain_to_file(&obj->deltas, *f,
1470 40dc5ff8 2018-03-16 stsp obj->path_packfile, repo);
1472 3ee5fc21 2018-01-17 stsp if (packfile)
1473 3ee5fc21 2018-01-17 stsp fclose(packfile);
1474 3ee5fc21 2018-01-17 stsp if (err && *f)
1475 3ee5fc21 2018-01-17 stsp fclose(*f);
1476 a1fd68d8 2018-01-12 stsp return err;
1479 e0ab43e7 2018-03-16 stsp const struct got_error *
1480 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1481 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1483 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1484 e0ab43e7 2018-03-16 stsp FILE *packfile = NULL;
1486 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1487 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1489 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1490 e0ab43e7 2018-03-16 stsp packfile = fopen(obj->path_packfile, "rb");
1491 e0ab43e7 2018-03-16 stsp if (packfile == NULL) {
1492 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1493 e0ab43e7 2018-03-16 stsp goto done;
1496 e0ab43e7 2018-03-16 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
1497 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1498 e0ab43e7 2018-03-16 stsp goto done;
1501 e0ab43e7 2018-03-16 stsp err = got_inflate_to_mem(buf, len, packfile);
1503 e0ab43e7 2018-03-16 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas,
1504 e0ab43e7 2018-03-16 stsp obj->path_packfile, repo);
1506 e0ab43e7 2018-03-16 stsp if (packfile)
1507 e0ab43e7 2018-03-16 stsp fclose(packfile);
1508 e0ab43e7 2018-03-16 stsp return err;