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 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 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 79b11c62 2018-03-09 stsp static void
368 63afc6c6 2018-03-10 stsp cache_packidx(struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
372 79b11c62 2018-03-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
373 79b11c62 2018-03-09 stsp if (repo->packidx_cache[i] == NULL)
377 79b11c62 2018-03-09 stsp if (i == nitems(repo->packidx_cache)) {
378 79b11c62 2018-03-09 stsp got_packidx_close(repo->packidx_cache[i - 1]);
379 79b11c62 2018-03-09 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
380 79b11c62 2018-03-09 stsp sizeof(repo->packidx_cache) -
381 79b11c62 2018-03-09 stsp sizeof(repo->packidx_cache[0]));
385 79b11c62 2018-03-09 stsp repo->packidx_cache[i] = dup_packidx(packidx);
388 6b9c9673 2018-01-23 stsp static const struct got_error *
389 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
390 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
392 6b9c9673 2018-01-23 stsp const struct got_error *err;
393 6b9c9673 2018-01-23 stsp char *path_packdir;
394 6b9c9673 2018-01-23 stsp DIR *packdir;
395 6b9c9673 2018-01-23 stsp struct dirent *dent;
396 6b9c9673 2018-01-23 stsp char *path_packidx;
399 79b11c62 2018-03-09 stsp /* Search pack index cache. */
400 79b11c62 2018-03-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
401 79b11c62 2018-03-09 stsp if (repo->packidx_cache[i] == NULL)
403 79b11c62 2018-03-09 stsp *idx = get_object_idx(repo->packidx_cache[i], id);
404 79b11c62 2018-03-09 stsp if (*idx != -1) {
405 79b11c62 2018-03-09 stsp *packidx = dup_packidx(repo->packidx_cache[i]);
406 79b11c62 2018-03-09 stsp if (*packidx == NULL)
407 0a71ee67 2018-03-09 stsp return got_error(GOT_ERR_NO_MEM);
408 79b11c62 2018-03-09 stsp return NULL;
411 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
413 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
414 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
415 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
417 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
418 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
419 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
423 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
424 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
427 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
428 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
429 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
433 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
434 6b9c9673 2018-01-23 stsp free(path_packidx);
438 6b9c9673 2018-01-23 stsp *idx = get_object_idx(*packidx, id);
439 6b9c9673 2018-01-23 stsp if (*idx != -1) {
440 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
441 79b11c62 2018-03-09 stsp cache_packidx(*packidx, repo);
445 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
446 6b9c9673 2018-01-23 stsp *packidx = NULL;
449 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
451 6b9c9673 2018-01-23 stsp free(path_packdir);
452 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
453 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
454 6b9c9673 2018-01-23 stsp return err;
457 c7fe698a 2018-01-23 stsp static const struct got_error *
458 6b9c9673 2018-01-23 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
459 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx)
461 6b9c9673 2018-01-23 stsp char *path_packdir;
462 6b9c9673 2018-01-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
463 6b9c9673 2018-01-23 stsp char *sha1str;
465 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
466 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
467 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
469 6b9c9673 2018-01-23 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
470 6b9c9673 2018-01-23 stsp hex, sizeof(hex));
471 6b9c9673 2018-01-23 stsp if (sha1str == NULL)
472 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
474 6b9c9673 2018-01-23 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
475 6b9c9673 2018-01-23 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
476 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
477 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
480 6b9c9673 2018-01-23 stsp return NULL;
483 a1fd68d8 2018-01-12 stsp const struct got_error *
484 a1fd68d8 2018-01-12 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
486 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
487 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
488 a1fd68d8 2018-01-12 stsp struct got_packfile_hdr hdr;
491 a1fd68d8 2018-01-12 stsp n = fread(&hdr, sizeof(hdr), 1, f);
492 a1fd68d8 2018-01-12 stsp if (n != 1)
493 8251fdbc 2018-01-12 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
495 a1fd68d8 2018-01-12 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
496 a1fd68d8 2018-01-12 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
497 a1fd68d8 2018-01-12 stsp betoh32(hdr.nobjects) != totobj)
498 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
500 a1fd68d8 2018-01-12 stsp return err;
503 a487c1d0 2018-01-14 stsp static const struct got_error *
504 c7fe698a 2018-01-23 stsp open_packfile(FILE **packfile, char **path_packfile,
505 c7fe698a 2018-01-23 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
507 c7fe698a 2018-01-23 stsp const struct got_error *err;
509 c7fe698a 2018-01-23 stsp *packfile = NULL;
511 c7fe698a 2018-01-23 stsp err = get_packfile_path(path_packfile, repo, packidx);
513 c7fe698a 2018-01-23 stsp return err;
515 c7fe698a 2018-01-23 stsp *packfile = fopen(*path_packfile, "rb");
516 c7fe698a 2018-01-23 stsp if (*packfile == NULL) {
517 c7fe698a 2018-01-23 stsp err = got_error_from_errno();
518 c7fe698a 2018-01-23 stsp free(*path_packfile);
519 c7fe698a 2018-01-23 stsp return err;
522 c7fe698a 2018-01-23 stsp err = read_packfile_hdr(*packfile, packidx);
524 c7fe698a 2018-01-23 stsp fclose(*packfile);
525 c7fe698a 2018-01-23 stsp *packfile = NULL;
527 c7fe698a 2018-01-23 stsp return err;
530 c7fe698a 2018-01-23 stsp static const struct got_error *
531 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
532 c3703302 2018-01-23 stsp FILE *packfile)
534 a487c1d0 2018-01-14 stsp uint8_t t = 0;
535 a487c1d0 2018-01-14 stsp uint64_t s = 0;
536 a487c1d0 2018-01-14 stsp uint8_t sizeN;
541 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
543 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
545 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
546 a487c1d0 2018-01-14 stsp if (n != 1)
547 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
549 a1fd68d8 2018-01-12 stsp if (i == 0) {
550 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
551 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
552 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
554 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
555 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
558 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
562 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
563 a487c1d0 2018-01-14 stsp return NULL;
566 a1fd68d8 2018-01-12 stsp static const struct got_error *
567 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
568 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
570 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
571 6ccb713b 2018-01-19 stsp if (*obj == NULL)
572 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
574 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
575 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
576 6ccb713b 2018-01-19 stsp free(*obj);
577 6ccb713b 2018-01-19 stsp *obj = NULL;
578 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
581 6ccb713b 2018-01-19 stsp (*obj)->type = type;
582 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
583 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
584 6ccb713b 2018-01-19 stsp (*obj)->size = size;
585 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
586 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
588 b107e67f 2018-01-19 stsp return NULL;
591 b107e67f 2018-01-19 stsp static const struct got_error *
592 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
594 b107e67f 2018-01-19 stsp int64_t o = 0;
595 b107e67f 2018-01-19 stsp uint8_t offN;
600 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
602 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
604 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
605 b107e67f 2018-01-19 stsp if (n != 1)
606 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
608 b107e67f 2018-01-19 stsp if (i == 0)
609 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
613 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
616 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
618 b107e67f 2018-01-19 stsp *offset = o;
619 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
620 6ccb713b 2018-01-19 stsp return NULL;
623 6ccb713b 2018-01-19 stsp static const struct got_error *
624 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
626 96f5e8b3 2018-01-23 stsp const struct got_error *err;
627 b107e67f 2018-01-19 stsp int64_t negoffset;
628 b107e67f 2018-01-19 stsp size_t negofflen;
630 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
632 b107e67f 2018-01-19 stsp return err;
634 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
635 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
636 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
637 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
639 96f5e8b3 2018-01-23 stsp return NULL;
642 0e22967e 2018-02-11 stsp static const struct got_error *
643 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
644 0e22967e 2018-02-11 stsp FILE *, const char *, off_t, size_t, int, size_t);
646 96f5e8b3 2018-01-23 stsp static const struct got_error *
647 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
648 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
649 bdd2fbb3 2018-02-11 stsp size_t delta_data_offset)
651 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
653 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
654 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
655 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
656 bdd2fbb3 2018-02-11 stsp return got_error(GOT_ERR_NO_MEM);
657 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
658 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
659 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
660 bdd2fbb3 2018-02-11 stsp return NULL;
663 bdd2fbb3 2018-02-11 stsp static const struct got_error *
664 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
665 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
666 bdd2fbb3 2018-02-11 stsp off_t delta_offset,size_t tslen, int delta_type, size_t delta_size)
669 a3500804 2018-01-23 stsp const struct got_error *err;
670 c3703302 2018-01-23 stsp off_t base_offset;
671 c3703302 2018-01-23 stsp uint8_t base_type;
672 c3703302 2018-01-23 stsp uint64_t base_size;
673 c3703302 2018-01-23 stsp size_t base_tslen;
674 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
676 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
678 a3500804 2018-01-23 stsp return err;
680 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
681 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
682 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
684 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
685 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
687 bdd2fbb3 2018-02-11 stsp return err;
689 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
690 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
691 b107e67f 2018-01-19 stsp return got_error_from_errno();
693 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
696 b107e67f 2018-01-19 stsp return err;
698 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
699 0e22967e 2018-02-11 stsp base_offset, base_tslen, base_type, base_size);
702 c3703302 2018-01-23 stsp static const struct got_error *
703 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
704 bdd2fbb3 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset,
705 bdd2fbb3 2018-02-11 stsp size_t tslen, int delta_type, size_t delta_size)
707 6b9c9673 2018-01-23 stsp const struct got_error *err;
708 6b9c9673 2018-01-23 stsp struct got_object_id id;
709 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
711 6b9c9673 2018-01-23 stsp off_t base_offset;
712 6b9c9673 2018-01-23 stsp uint8_t base_type;
713 6b9c9673 2018-01-23 stsp uint64_t base_size;
714 6b9c9673 2018-01-23 stsp size_t base_tslen;
716 6b9c9673 2018-01-23 stsp FILE *base_packfile;
717 6b9c9673 2018-01-23 stsp char *path_base_packfile;
718 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
720 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
721 6b9c9673 2018-01-23 stsp if (n != 1)
722 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
724 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
725 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
726 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
728 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
729 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
731 bdd2fbb3 2018-02-11 stsp return err;
733 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
735 6b9c9673 2018-01-23 stsp return err;
737 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
738 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
739 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
740 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
743 c7fe698a 2018-01-23 stsp err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
744 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
746 6b9c9673 2018-01-23 stsp return err;
748 6b9c9673 2018-01-23 stsp if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
749 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
753 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
754 6b9c9673 2018-01-23 stsp base_packfile);
758 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(deltas, repo, base_packfile,
759 0e22967e 2018-02-11 stsp path_base_packfile, base_offset, base_tslen, base_type,
760 0e22967e 2018-02-11 stsp base_size);
762 6b9c9673 2018-01-23 stsp free(path_base_packfile);
763 6b9c9673 2018-01-23 stsp if (base_packfile && fclose(base_packfile) == -1 && err == 0)
764 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
765 6b9c9673 2018-01-23 stsp return err;
768 6b9c9673 2018-01-23 stsp static const struct got_error *
769 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
770 0e22967e 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
771 0e22967e 2018-02-11 stsp int delta_type, size_t delta_size)
773 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
775 c3703302 2018-01-23 stsp switch (delta_type) {
776 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
777 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
778 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
779 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
780 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
781 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
782 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, 0);
784 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
785 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
786 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
787 bdd2fbb3 2018-02-11 stsp delta_size);
789 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
790 bdd2fbb3 2018-02-11 stsp err = resolve_ref_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);
795 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
798 96f5e8b3 2018-01-23 stsp return err;
801 96f5e8b3 2018-01-23 stsp static const struct got_error *
802 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
803 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
804 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
805 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
807 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
808 96f5e8b3 2018-01-23 stsp int resolved_type;
810 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
811 b107e67f 2018-01-19 stsp if (*obj == NULL)
812 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
814 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
815 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
816 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
817 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
818 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
820 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
821 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
822 96f5e8b3 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
825 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
827 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
828 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
830 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
831 0e22967e 2018-02-11 stsp path_packfile, offset, tslen, delta_type, delta_size);
835 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
838 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
842 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
843 96f5e8b3 2018-01-23 stsp *obj = NULL;
845 96f5e8b3 2018-01-23 stsp return err;
848 b107e67f 2018-01-19 stsp static const struct got_error *
849 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
850 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
852 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
853 a1fd68d8 2018-01-12 stsp off_t offset;
854 6c00b545 2018-01-17 stsp char *path_packfile;
855 6c00b545 2018-01-17 stsp FILE *packfile;
856 6c00b545 2018-01-17 stsp uint8_t type;
857 6c00b545 2018-01-17 stsp uint64_t size;
858 3ee5fc21 2018-01-17 stsp size_t tslen;
860 6c00b545 2018-01-17 stsp *obj = NULL;
862 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
863 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
864 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
866 c7fe698a 2018-01-23 stsp err = open_packfile(&packfile, &path_packfile, repo, packidx);
868 6b9c9673 2018-01-23 stsp return err;
870 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
871 6c00b545 2018-01-17 stsp err = got_error_from_errno();
875 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&type, &size, &tslen, packfile);
879 6c00b545 2018-01-17 stsp switch (type) {
880 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
881 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
882 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
883 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
884 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
885 6ccb713b 2018-01-19 stsp offset + tslen, size);
888 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
889 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
890 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
891 a48db7e5 2018-01-23 stsp packfile, id, offset, tslen, type, size);
895 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
899 a1fd68d8 2018-01-12 stsp free(path_packfile);
900 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
901 f334529e 2018-01-12 stsp err = got_error_from_errno();
902 a1fd68d8 2018-01-12 stsp return err;
905 a1fd68d8 2018-01-12 stsp const struct got_error *
906 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
907 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
909 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
910 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
913 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
915 6b9c9673 2018-01-23 stsp return err;
917 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
918 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
919 3ee5fc21 2018-01-17 stsp return err;
922 efd2a263 2018-01-19 stsp static const struct got_error *
923 710bb5ca 2018-01-23 stsp dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
925 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
926 6691714a 2018-01-23 stsp struct got_delta *delta;
927 6691714a 2018-01-23 stsp FILE *base_file, *accum_file;
930 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
931 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
933 6691714a 2018-01-23 stsp base_file = got_opentemp();
934 6691714a 2018-01-23 stsp if (base_file == NULL)
935 6691714a 2018-01-23 stsp return got_error_from_errno();
937 6691714a 2018-01-23 stsp accum_file = got_opentemp();
938 6691714a 2018-01-23 stsp if (accum_file == NULL) {
939 6691714a 2018-01-23 stsp err = got_error_from_errno();
940 6691714a 2018-01-23 stsp fclose(base_file);
941 efd2a263 2018-01-19 stsp return err;
944 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
945 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
946 885d3e02 2018-01-27 stsp uint8_t *delta_buf = NULL;
947 885d3e02 2018-01-27 stsp size_t delta_len = 0;
948 885d3e02 2018-01-27 stsp FILE *delta_file;
950 885d3e02 2018-01-27 stsp delta_file = fopen(delta->path_packfile, "rb");
951 6691714a 2018-01-23 stsp if (delta_file == NULL) {
952 6691714a 2018-01-23 stsp err = got_error_from_errno();
957 a6b158cc 2018-02-11 stsp if (n == 0) {
958 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
959 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
960 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
961 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
962 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
963 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
967 bdd2fbb3 2018-02-11 stsp if (fseeko(delta_file, delta->offset + delta->tslen,
968 bdd2fbb3 2018-02-11 stsp SEEK_SET) != 0) {
969 bdd2fbb3 2018-02-11 stsp fclose(delta_file);
970 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
973 a6b158cc 2018-02-11 stsp err = got_inflate_to_file(&delta_len, delta_file,
974 a6b158cc 2018-02-11 stsp base_file);
975 a6b158cc 2018-02-11 stsp fclose(delta_file);
979 a6b158cc 2018-02-11 stsp rewind(base_file);
983 bdd2fbb3 2018-02-11 stsp if (fseeko(delta_file, delta->data_offset, SEEK_CUR) != 0) {
984 0e22967e 2018-02-11 stsp fclose(delta_file);
985 0e22967e 2018-02-11 stsp err = got_error_from_errno();
989 885d3e02 2018-01-27 stsp /* Delta streams should always fit in memory. */
990 61d262a8 2018-02-11 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, delta_file);
991 a6b158cc 2018-02-11 stsp fclose(delta_file);
995 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, delta_buf, delta_len,
996 6691714a 2018-01-23 stsp /* Final delta application writes to the output file. */
997 710bb5ca 2018-01-23 stsp ++n < deltas->nentries ? accum_file : outfile);
998 885d3e02 2018-01-27 stsp free(delta_buf);
1000 6691714a 2018-01-23 stsp goto done;
1002 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1003 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1004 6691714a 2018-01-23 stsp FILE *tmp = accum_file;
1005 6691714a 2018-01-23 stsp accum_file = base_file;
1006 6691714a 2018-01-23 stsp base_file = tmp;
1007 6691714a 2018-01-23 stsp rewind(base_file);
1008 6691714a 2018-01-23 stsp rewind(accum_file);
1013 6691714a 2018-01-23 stsp fclose(base_file);
1014 6691714a 2018-01-23 stsp fclose(accum_file);
1015 6691714a 2018-01-23 stsp rewind(outfile);
1016 efd2a263 2018-01-19 stsp return err;
1019 3ee5fc21 2018-01-17 stsp const struct got_error *
1020 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1021 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1023 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1024 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
1026 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1027 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1029 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
1030 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
1031 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
1032 3ee5fc21 2018-01-17 stsp goto done;
1035 6691714a 2018-01-23 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1036 6691714a 2018-01-23 stsp packfile = fopen(obj->path_packfile, "rb");
1037 6691714a 2018-01-23 stsp if (packfile == NULL) {
1038 6691714a 2018-01-23 stsp err = got_error_from_errno();
1039 6691714a 2018-01-23 stsp goto done;
1042 6691714a 2018-01-23 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
1043 6691714a 2018-01-23 stsp err = got_error_from_errno();
1044 6691714a 2018-01-23 stsp goto done;
1047 3606d7fc 2018-02-11 stsp err = got_inflate_to_file(&obj->size, packfile, *f);
1049 710bb5ca 2018-01-23 stsp err = dump_delta_chain(&obj->deltas, *f);
1051 3ee5fc21 2018-01-17 stsp if (packfile)
1052 3ee5fc21 2018-01-17 stsp fclose(packfile);
1053 3ee5fc21 2018-01-17 stsp if (err && *f)
1054 3ee5fc21 2018-01-17 stsp fclose(*f);
1055 a1fd68d8 2018-01-12 stsp return err;