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 1411938b 2018-02-12 stsp #include "got_sha1_priv.h"
37 1411938b 2018-02-12 stsp #include "got_pack_priv.h"
38 1411938b 2018-02-12 stsp #include "got_path_priv.h"
39 1411938b 2018-02-12 stsp #include "got_delta_priv.h"
40 1411938b 2018-02-12 stsp #include "got_zb_priv.h"
41 1411938b 2018-02-12 stsp #include "got_object_priv.h"
42 79b11c62 2018-03-09 stsp #include "got_repository_priv.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 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
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 a1fd68d8 2018-01-12 stsp uint32_t offset;
306 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
308 6c00b545 2018-01-17 stsp if (cmp == 0)
316 79b11c62 2018-03-09 stsp static struct got_packidx_v2_hdr *
317 79b11c62 2018-03-09 stsp dup_packidx(struct got_packidx_v2_hdr *packidx)
319 79b11c62 2018-03-09 stsp struct got_packidx_v2_hdr *p;
320 79b11c62 2018-03-09 stsp size_t nobj;
322 79b11c62 2018-03-09 stsp p = calloc(1, sizeof(*p));
323 79b11c62 2018-03-09 stsp if (p == NULL)
324 79b11c62 2018-03-09 stsp return NULL;
326 79b11c62 2018-03-09 stsp memcpy(p, packidx, sizeof(*p));
327 79b11c62 2018-03-09 stsp p->sorted_ids = NULL;
328 79b11c62 2018-03-09 stsp p->crc32 = NULL;
329 79b11c62 2018-03-09 stsp p->offsets = NULL;
330 79b11c62 2018-03-09 stsp p->large_offsets = NULL;
332 79b11c62 2018-03-09 stsp nobj = betoh32(p->fanout_table[0xff]);
334 79b11c62 2018-03-09 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
335 79b11c62 2018-03-09 stsp if (p->sorted_ids == NULL)
337 79b11c62 2018-03-09 stsp memcpy(p->sorted_ids, packidx->sorted_ids, nobj * sizeof(*p->sorted_ids));
339 79b11c62 2018-03-09 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
340 79b11c62 2018-03-09 stsp if (p->crc32 == NULL)
342 79b11c62 2018-03-09 stsp memcpy(p->crc32, packidx->crc32, nobj * sizeof(*p->crc32));
344 79b11c62 2018-03-09 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
345 79b11c62 2018-03-09 stsp if (p->offsets == NULL)
347 79b11c62 2018-03-09 stsp memcpy(p->offsets, packidx->offsets, nobj * sizeof(*p->offsets));
349 79b11c62 2018-03-09 stsp if (p->large_offsets) {
350 79b11c62 2018-03-09 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
351 79b11c62 2018-03-09 stsp if (p->large_offsets == NULL)
353 79b11c62 2018-03-09 stsp memcpy(p->large_offsets, packidx->large_offsets,
354 79b11c62 2018-03-09 stsp nobj * sizeof(*p->large_offsets));
360 79b11c62 2018-03-09 stsp free(p->large_offsets);
361 79b11c62 2018-03-09 stsp free(p->offsets);
362 79b11c62 2018-03-09 stsp free(p->crc32);
363 79b11c62 2018-03-09 stsp free(p->sorted_ids);
365 79b11c62 2018-03-09 stsp return NULL;
368 79b11c62 2018-03-09 stsp static void
369 63afc6c6 2018-03-10 stsp cache_packidx(struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
371 79b11c62 2018-03-09 stsp struct got_packidx_v2_hdr *p;
374 79b11c62 2018-03-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
375 79b11c62 2018-03-09 stsp if (repo->packidx_cache[i] == NULL)
379 79b11c62 2018-03-09 stsp if (i == nitems(repo->packidx_cache)) {
380 79b11c62 2018-03-09 stsp got_packidx_close(repo->packidx_cache[i - 1]);
381 79b11c62 2018-03-09 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
382 79b11c62 2018-03-09 stsp sizeof(repo->packidx_cache) -
383 79b11c62 2018-03-09 stsp sizeof(repo->packidx_cache[0]));
387 79b11c62 2018-03-09 stsp repo->packidx_cache[i] = dup_packidx(packidx);
390 6b9c9673 2018-01-23 stsp static const struct got_error *
391 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
392 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
394 6b9c9673 2018-01-23 stsp const struct got_error *err;
395 6b9c9673 2018-01-23 stsp char *path_packdir;
396 6b9c9673 2018-01-23 stsp DIR *packdir;
397 6b9c9673 2018-01-23 stsp struct dirent *dent;
398 6b9c9673 2018-01-23 stsp char *path_packidx;
401 79b11c62 2018-03-09 stsp /* Search pack index cache. */
402 79b11c62 2018-03-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
403 79b11c62 2018-03-09 stsp if (repo->packidx_cache[i] == NULL)
405 79b11c62 2018-03-09 stsp *idx = get_object_idx(repo->packidx_cache[i], id);
406 79b11c62 2018-03-09 stsp if (*idx != -1) {
407 79b11c62 2018-03-09 stsp *packidx = dup_packidx(repo->packidx_cache[i]);
408 79b11c62 2018-03-09 stsp if (*packidx == NULL)
409 0a71ee67 2018-03-09 stsp return got_error(GOT_ERR_NO_MEM);
410 79b11c62 2018-03-09 stsp return NULL;
413 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
415 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
416 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
417 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
419 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
420 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
421 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
425 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
426 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
429 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
430 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
431 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
435 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
436 6b9c9673 2018-01-23 stsp free(path_packidx);
440 6b9c9673 2018-01-23 stsp *idx = get_object_idx(*packidx, id);
441 6b9c9673 2018-01-23 stsp if (*idx != -1) {
442 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
443 79b11c62 2018-03-09 stsp cache_packidx(*packidx, repo);
447 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
448 6b9c9673 2018-01-23 stsp *packidx = NULL;
451 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
453 6b9c9673 2018-01-23 stsp free(path_packdir);
454 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
455 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
456 6b9c9673 2018-01-23 stsp return err;
459 c7fe698a 2018-01-23 stsp static const struct got_error *
460 6b9c9673 2018-01-23 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
461 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx)
463 6b9c9673 2018-01-23 stsp char *path_packdir;
464 6b9c9673 2018-01-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
465 6b9c9673 2018-01-23 stsp char *sha1str;
466 6b9c9673 2018-01-23 stsp char *path_packidx;
468 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
470 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
471 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
472 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
474 6b9c9673 2018-01-23 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
475 6b9c9673 2018-01-23 stsp hex, sizeof(hex));
476 6b9c9673 2018-01-23 stsp if (sha1str == NULL)
477 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
479 6b9c9673 2018-01-23 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
480 6b9c9673 2018-01-23 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
481 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
482 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
485 6b9c9673 2018-01-23 stsp return NULL;
488 a1fd68d8 2018-01-12 stsp const struct got_error *
489 a1fd68d8 2018-01-12 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
491 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
492 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
493 a1fd68d8 2018-01-12 stsp struct got_packfile_hdr hdr;
496 a1fd68d8 2018-01-12 stsp n = fread(&hdr, sizeof(hdr), 1, f);
497 a1fd68d8 2018-01-12 stsp if (n != 1)
498 8251fdbc 2018-01-12 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
500 a1fd68d8 2018-01-12 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
501 a1fd68d8 2018-01-12 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
502 a1fd68d8 2018-01-12 stsp betoh32(hdr.nobjects) != totobj)
503 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
505 a1fd68d8 2018-01-12 stsp return err;
508 a487c1d0 2018-01-14 stsp static const struct got_error *
509 c7fe698a 2018-01-23 stsp open_packfile(FILE **packfile, char **path_packfile,
510 c7fe698a 2018-01-23 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
512 c7fe698a 2018-01-23 stsp const struct got_error *err;
514 c7fe698a 2018-01-23 stsp *packfile = NULL;
516 c7fe698a 2018-01-23 stsp err = get_packfile_path(path_packfile, repo, packidx);
518 c7fe698a 2018-01-23 stsp return err;
520 c7fe698a 2018-01-23 stsp *packfile = fopen(*path_packfile, "rb");
521 c7fe698a 2018-01-23 stsp if (*packfile == NULL) {
522 c7fe698a 2018-01-23 stsp err = got_error_from_errno();
523 c7fe698a 2018-01-23 stsp free(*path_packfile);
524 c7fe698a 2018-01-23 stsp return err;
527 c7fe698a 2018-01-23 stsp err = read_packfile_hdr(*packfile, packidx);
529 c7fe698a 2018-01-23 stsp fclose(*packfile);
530 c7fe698a 2018-01-23 stsp *packfile = NULL;
532 c7fe698a 2018-01-23 stsp return err;
535 c7fe698a 2018-01-23 stsp static const struct got_error *
536 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
537 c3703302 2018-01-23 stsp FILE *packfile)
539 a487c1d0 2018-01-14 stsp uint8_t t = 0;
540 a487c1d0 2018-01-14 stsp uint64_t s = 0;
541 a487c1d0 2018-01-14 stsp uint8_t sizeN;
546 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
548 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
550 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
551 a487c1d0 2018-01-14 stsp if (n != 1)
552 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
554 a1fd68d8 2018-01-12 stsp if (i == 0) {
555 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
556 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
557 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
559 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
560 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
563 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
567 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
568 a487c1d0 2018-01-14 stsp return NULL;
571 a1fd68d8 2018-01-12 stsp static const struct got_error *
572 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
573 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
575 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
576 6ccb713b 2018-01-19 stsp if (*obj == NULL)
577 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
579 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
580 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
581 6ccb713b 2018-01-19 stsp free(*obj);
582 6ccb713b 2018-01-19 stsp *obj = NULL;
583 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
586 6ccb713b 2018-01-19 stsp (*obj)->type = type;
587 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
588 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
589 6ccb713b 2018-01-19 stsp (*obj)->size = size;
590 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
591 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
593 b107e67f 2018-01-19 stsp return NULL;
596 b107e67f 2018-01-19 stsp static const struct got_error *
597 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
599 b107e67f 2018-01-19 stsp int64_t o = 0;
600 b107e67f 2018-01-19 stsp uint8_t offN;
605 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
607 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
609 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
610 b107e67f 2018-01-19 stsp if (n != 1)
611 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
613 b107e67f 2018-01-19 stsp if (i == 0)
614 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
618 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
621 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
623 b107e67f 2018-01-19 stsp *offset = o;
624 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
625 6ccb713b 2018-01-19 stsp return NULL;
628 6ccb713b 2018-01-19 stsp static const struct got_error *
629 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
631 96f5e8b3 2018-01-23 stsp const struct got_error *err;
632 b107e67f 2018-01-19 stsp int64_t negoffset;
633 b107e67f 2018-01-19 stsp size_t negofflen;
635 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
637 b107e67f 2018-01-19 stsp return err;
639 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
640 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
641 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
642 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
644 96f5e8b3 2018-01-23 stsp return NULL;
647 0e22967e 2018-02-11 stsp static const struct got_error *
648 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
649 0e22967e 2018-02-11 stsp FILE *, const char *, off_t, size_t, int, size_t);
651 96f5e8b3 2018-01-23 stsp static const struct got_error *
652 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
653 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
654 bdd2fbb3 2018-02-11 stsp size_t delta_data_offset)
656 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
658 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
659 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
660 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
661 bdd2fbb3 2018-02-11 stsp return got_error(GOT_ERR_NO_MEM);
662 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
663 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
664 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
665 bdd2fbb3 2018-02-11 stsp return NULL;
668 bdd2fbb3 2018-02-11 stsp static const struct got_error *
669 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
670 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
671 bdd2fbb3 2018-02-11 stsp off_t delta_offset,size_t tslen, int delta_type, size_t delta_size)
674 a3500804 2018-01-23 stsp const struct got_error *err;
675 c3703302 2018-01-23 stsp off_t base_offset;
676 c3703302 2018-01-23 stsp uint8_t base_type;
677 c3703302 2018-01-23 stsp uint64_t base_size;
678 c3703302 2018-01-23 stsp size_t base_tslen;
679 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
681 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
683 a3500804 2018-01-23 stsp return err;
685 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
686 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
687 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
689 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
690 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
692 bdd2fbb3 2018-02-11 stsp return err;
694 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
695 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
696 b107e67f 2018-01-19 stsp return got_error_from_errno();
698 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
701 b107e67f 2018-01-19 stsp return err;
703 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
704 0e22967e 2018-02-11 stsp base_offset, base_tslen, base_type, base_size);
707 c3703302 2018-01-23 stsp static const struct got_error *
708 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
709 bdd2fbb3 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset,
710 bdd2fbb3 2018-02-11 stsp size_t tslen, int delta_type, size_t delta_size)
712 6b9c9673 2018-01-23 stsp const struct got_error *err;
713 6b9c9673 2018-01-23 stsp struct got_object_id id;
714 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
716 6b9c9673 2018-01-23 stsp off_t base_offset;
717 6b9c9673 2018-01-23 stsp uint8_t base_type;
718 6b9c9673 2018-01-23 stsp uint64_t base_size;
719 6b9c9673 2018-01-23 stsp size_t base_tslen;
721 6b9c9673 2018-01-23 stsp FILE *base_packfile;
722 6b9c9673 2018-01-23 stsp char *path_base_packfile;
723 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
725 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
726 6b9c9673 2018-01-23 stsp if (n != 1)
727 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
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 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
740 6b9c9673 2018-01-23 stsp return err;
742 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
743 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
744 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
745 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
748 c7fe698a 2018-01-23 stsp err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
749 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
751 6b9c9673 2018-01-23 stsp return err;
753 6b9c9673 2018-01-23 stsp if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
754 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
758 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
759 6b9c9673 2018-01-23 stsp base_packfile);
763 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(deltas, repo, base_packfile,
764 0e22967e 2018-02-11 stsp path_base_packfile, base_offset, base_tslen, base_type,
765 0e22967e 2018-02-11 stsp base_size);
767 6b9c9673 2018-01-23 stsp free(path_base_packfile);
768 6b9c9673 2018-01-23 stsp if (base_packfile && fclose(base_packfile) == -1 && err == 0)
769 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
770 6b9c9673 2018-01-23 stsp return err;
773 6b9c9673 2018-01-23 stsp static const struct got_error *
774 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
775 0e22967e 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
776 0e22967e 2018-02-11 stsp int delta_type, size_t delta_size)
778 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
780 c3703302 2018-01-23 stsp switch (delta_type) {
781 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
782 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
783 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
784 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
785 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
786 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
787 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, 0);
789 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
790 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
791 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
792 bdd2fbb3 2018-02-11 stsp delta_size);
794 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
795 bdd2fbb3 2018-02-11 stsp err = resolve_ref_delta(deltas, repo, packfile,
796 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
797 bdd2fbb3 2018-02-11 stsp delta_size);
800 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
803 96f5e8b3 2018-01-23 stsp return err;
806 96f5e8b3 2018-01-23 stsp static const struct got_error *
807 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
808 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
809 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
810 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
812 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
813 96f5e8b3 2018-01-23 stsp struct got_object_id base_id;
814 96f5e8b3 2018-01-23 stsp uint8_t base_type;
815 96f5e8b3 2018-01-23 stsp int resolved_type;
816 96f5e8b3 2018-01-23 stsp uint64_t base_size;
817 96f5e8b3 2018-01-23 stsp size_t base_tslen;
819 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
820 b107e67f 2018-01-19 stsp if (*obj == NULL)
821 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
823 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
824 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
825 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
826 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
827 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
829 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
830 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
831 96f5e8b3 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
834 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
836 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
837 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
839 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
840 0e22967e 2018-02-11 stsp path_packfile, offset, tslen, delta_type, delta_size);
844 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
847 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
851 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
852 96f5e8b3 2018-01-23 stsp *obj = NULL;
854 96f5e8b3 2018-01-23 stsp return err;
857 b107e67f 2018-01-19 stsp static const struct got_error *
858 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
859 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
861 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
862 a1fd68d8 2018-01-12 stsp off_t offset;
863 6c00b545 2018-01-17 stsp char *path_packfile;
864 6c00b545 2018-01-17 stsp FILE *packfile;
865 6c00b545 2018-01-17 stsp uint8_t type;
866 6c00b545 2018-01-17 stsp uint64_t size;
867 3ee5fc21 2018-01-17 stsp size_t tslen;
869 6c00b545 2018-01-17 stsp *obj = NULL;
871 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
872 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
873 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
875 c7fe698a 2018-01-23 stsp err = open_packfile(&packfile, &path_packfile, repo, packidx);
877 6b9c9673 2018-01-23 stsp return err;
879 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
880 6c00b545 2018-01-17 stsp err = got_error_from_errno();
884 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&type, &size, &tslen, packfile);
888 6c00b545 2018-01-17 stsp switch (type) {
889 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
890 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
891 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
892 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
893 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
894 6ccb713b 2018-01-19 stsp offset + tslen, size);
897 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
898 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
899 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
900 a48db7e5 2018-01-23 stsp packfile, id, offset, tslen, type, size);
904 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
908 a1fd68d8 2018-01-12 stsp free(path_packfile);
909 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
910 f334529e 2018-01-12 stsp err = got_error_from_errno();
911 a1fd68d8 2018-01-12 stsp return err;
914 a1fd68d8 2018-01-12 stsp const struct got_error *
915 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
916 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
918 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
919 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
922 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
924 6b9c9673 2018-01-23 stsp return err;
926 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
927 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
928 3ee5fc21 2018-01-17 stsp return err;
931 efd2a263 2018-01-19 stsp static const struct got_error *
932 710bb5ca 2018-01-23 stsp dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
934 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
935 6691714a 2018-01-23 stsp struct got_delta *delta;
936 6691714a 2018-01-23 stsp FILE *base_file, *accum_file;
939 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
940 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
942 6691714a 2018-01-23 stsp base_file = got_opentemp();
943 6691714a 2018-01-23 stsp if (base_file == NULL)
944 6691714a 2018-01-23 stsp return got_error_from_errno();
946 6691714a 2018-01-23 stsp accum_file = got_opentemp();
947 6691714a 2018-01-23 stsp if (accum_file == NULL) {
948 6691714a 2018-01-23 stsp err = got_error_from_errno();
949 6691714a 2018-01-23 stsp fclose(base_file);
950 efd2a263 2018-01-19 stsp return err;
953 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
954 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
955 885d3e02 2018-01-27 stsp uint8_t *delta_buf = NULL;
956 885d3e02 2018-01-27 stsp size_t delta_len = 0;
957 885d3e02 2018-01-27 stsp FILE *delta_file;
959 885d3e02 2018-01-27 stsp delta_file = fopen(delta->path_packfile, "rb");
960 6691714a 2018-01-23 stsp if (delta_file == NULL) {
961 6691714a 2018-01-23 stsp err = got_error_from_errno();
966 a6b158cc 2018-02-11 stsp if (n == 0) {
967 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
968 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
969 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
970 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
971 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
972 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
976 bdd2fbb3 2018-02-11 stsp if (fseeko(delta_file, delta->offset + delta->tslen,
977 bdd2fbb3 2018-02-11 stsp SEEK_SET) != 0) {
978 bdd2fbb3 2018-02-11 stsp fclose(delta_file);
979 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
982 a6b158cc 2018-02-11 stsp err = got_inflate_to_file(&delta_len, delta_file,
983 a6b158cc 2018-02-11 stsp base_file);
984 a6b158cc 2018-02-11 stsp fclose(delta_file);
988 a6b158cc 2018-02-11 stsp rewind(base_file);
992 bdd2fbb3 2018-02-11 stsp if (fseeko(delta_file, delta->data_offset, SEEK_CUR) != 0) {
993 0e22967e 2018-02-11 stsp fclose(delta_file);
994 0e22967e 2018-02-11 stsp err = got_error_from_errno();
998 885d3e02 2018-01-27 stsp /* Delta streams should always fit in memory. */
999 61d262a8 2018-02-11 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, delta_file);
1000 a6b158cc 2018-02-11 stsp fclose(delta_file);
1002 a6b158cc 2018-02-11 stsp goto done;
1004 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, delta_buf, delta_len,
1005 6691714a 2018-01-23 stsp /* Final delta application writes to the output file. */
1006 710bb5ca 2018-01-23 stsp ++n < deltas->nentries ? accum_file : outfile);
1007 885d3e02 2018-01-27 stsp free(delta_buf);
1009 6691714a 2018-01-23 stsp goto done;
1011 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1012 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1013 6691714a 2018-01-23 stsp FILE *tmp = accum_file;
1014 6691714a 2018-01-23 stsp accum_file = base_file;
1015 6691714a 2018-01-23 stsp base_file = tmp;
1016 6691714a 2018-01-23 stsp rewind(base_file);
1017 6691714a 2018-01-23 stsp rewind(accum_file);
1022 6691714a 2018-01-23 stsp fclose(base_file);
1023 6691714a 2018-01-23 stsp fclose(accum_file);
1024 6691714a 2018-01-23 stsp rewind(outfile);
1025 efd2a263 2018-01-19 stsp return err;
1028 3ee5fc21 2018-01-17 stsp const struct got_error *
1029 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1030 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1032 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1033 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
1035 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1036 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1038 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
1039 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
1040 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
1041 3ee5fc21 2018-01-17 stsp goto done;
1044 6691714a 2018-01-23 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1045 6691714a 2018-01-23 stsp packfile = fopen(obj->path_packfile, "rb");
1046 6691714a 2018-01-23 stsp if (packfile == NULL) {
1047 6691714a 2018-01-23 stsp err = got_error_from_errno();
1048 6691714a 2018-01-23 stsp goto done;
1051 6691714a 2018-01-23 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
1052 6691714a 2018-01-23 stsp err = got_error_from_errno();
1053 6691714a 2018-01-23 stsp goto done;
1056 3606d7fc 2018-02-11 stsp err = got_inflate_to_file(&obj->size, packfile, *f);
1058 710bb5ca 2018-01-23 stsp err = dump_delta_chain(&obj->deltas, *f);
1060 3ee5fc21 2018-01-17 stsp if (packfile)
1061 3ee5fc21 2018-01-17 stsp fclose(packfile);
1062 3ee5fc21 2018-01-17 stsp if (err && *f)
1063 3ee5fc21 2018-01-17 stsp fclose(*f);
1064 a1fd68d8 2018-01-12 stsp return err;