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"
43 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
44 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
45 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
46 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
47 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
48 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
49 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
50 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
51 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
54 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 0a0a3048 2018-01-10 stsp static const struct got_error *
58 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
62 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
63 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
64 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
67 0a0a3048 2018-01-10 stsp return NULL;
70 24541888 2018-01-10 stsp static const struct got_error *
71 0a0a3048 2018-01-10 stsp get_packfile_size(size_t *size, const char *path_idx)
73 0a0a3048 2018-01-10 stsp struct stat sb;
74 0a0a3048 2018-01-10 stsp char *path_pack;
75 0a0a3048 2018-01-10 stsp char base_path[PATH_MAX];
78 0a0a3048 2018-01-10 stsp if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
79 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_SPACE);
81 0a0a3048 2018-01-10 stsp dot = strrchr(base_path, '.');
82 0a0a3048 2018-01-10 stsp if (dot == NULL)
83 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
84 0a0a3048 2018-01-10 stsp *dot = '\0';
85 0a0a3048 2018-01-10 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
86 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_MEM);
88 0a0a3048 2018-01-10 stsp if (stat(path_pack, &sb) != 0) {
89 0a0a3048 2018-01-10 stsp free(path_pack);
90 8251fdbc 2018-01-12 stsp return got_error_from_errno();
93 0a0a3048 2018-01-10 stsp free(path_pack);
94 0a0a3048 2018-01-10 stsp *size = sb.st_size;
98 0a0a3048 2018-01-10 stsp const struct got_error *
99 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
101 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
103 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
104 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
105 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
106 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
108 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
110 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
111 0a0a3048 2018-01-10 stsp if (f == NULL)
112 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
114 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
116 0a0a3048 2018-01-10 stsp return err;
118 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
119 0a0a3048 2018-01-10 stsp if (p == NULL) {
120 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
124 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
125 0a0a3048 2018-01-10 stsp if (n != 1) {
126 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
130 0a0a3048 2018-01-10 stsp if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
131 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
135 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
137 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
138 0a0a3048 2018-01-10 stsp if (n != 1) {
139 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
143 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
144 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
148 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
150 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
151 0a0a3048 2018-01-10 stsp if (n != 1) {
152 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
156 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
160 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
162 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
164 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
165 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
166 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
170 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
171 0a0a3048 2018-01-10 stsp if (n != nobj) {
172 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
176 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
177 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
179 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
180 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
181 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
185 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
186 0a0a3048 2018-01-10 stsp if (n != nobj) {
187 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
191 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
193 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
194 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
195 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
199 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
200 0a0a3048 2018-01-10 stsp if (n != nobj) {
201 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
205 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
207 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
208 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
209 0a0a3048 2018-01-10 stsp goto checksum;
211 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
212 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
213 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
217 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
218 0a0a3048 2018-01-10 stsp if (n != nobj) {
219 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
223 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
224 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
227 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
228 0a0a3048 2018-01-10 stsp if (n != 1) {
229 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
233 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
234 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
235 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
236 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
240 0a0a3048 2018-01-10 stsp got_packidx_close(p);
242 0a0a3048 2018-01-10 stsp *packidx = p;
243 0a0a3048 2018-01-10 stsp return err;
247 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
249 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
250 0a0a3048 2018-01-10 stsp free(packidx->offsets);
251 0a0a3048 2018-01-10 stsp free(packidx->crc32);
252 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
253 0a0a3048 2018-01-10 stsp free(packidx);
257 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
259 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
262 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
265 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
266 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
272 a1fd68d8 2018-01-12 stsp static off_t
273 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
275 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
276 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
277 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
278 a1fd68d8 2018-01-12 stsp uint64_t loffset;
279 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
280 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
282 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
283 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
285 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
289 a1fd68d8 2018-01-12 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
291 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
292 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
295 a1fd68d8 2018-01-12 stsp if (id0 > 0)
296 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
298 a1fd68d8 2018-01-12 stsp while (i < totobj) {
299 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
300 a1fd68d8 2018-01-12 stsp uint32_t offset;
301 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
303 6c00b545 2018-01-17 stsp if (cmp == 0)
311 6b9c9673 2018-01-23 stsp static const struct got_error *
312 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
313 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
315 6b9c9673 2018-01-23 stsp const struct got_error *err;
316 6b9c9673 2018-01-23 stsp char *path_packdir;
317 6b9c9673 2018-01-23 stsp DIR *packdir;
318 6b9c9673 2018-01-23 stsp struct dirent *dent;
319 6b9c9673 2018-01-23 stsp char *path_packidx;
321 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
322 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
323 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
325 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
326 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
327 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
331 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
332 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
335 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
336 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
337 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
341 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
342 6b9c9673 2018-01-23 stsp free(path_packidx);
346 6b9c9673 2018-01-23 stsp *idx = get_object_idx(*packidx, id);
347 6b9c9673 2018-01-23 stsp if (*idx != -1) {
348 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
352 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
353 6b9c9673 2018-01-23 stsp *packidx = NULL;
356 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
358 6b9c9673 2018-01-23 stsp free(path_packdir);
359 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
360 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
361 6b9c9673 2018-01-23 stsp return err;
364 c7fe698a 2018-01-23 stsp static const struct got_error *
365 6b9c9673 2018-01-23 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
366 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx)
368 6b9c9673 2018-01-23 stsp char *path_packdir;
369 6b9c9673 2018-01-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
370 6b9c9673 2018-01-23 stsp char *sha1str;
371 6b9c9673 2018-01-23 stsp char *path_packidx;
373 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
375 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
376 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
377 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
379 6b9c9673 2018-01-23 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
380 6b9c9673 2018-01-23 stsp hex, sizeof(hex));
381 6b9c9673 2018-01-23 stsp if (sha1str == NULL)
382 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
384 6b9c9673 2018-01-23 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
385 6b9c9673 2018-01-23 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
386 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
387 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
390 6b9c9673 2018-01-23 stsp return NULL;
393 a1fd68d8 2018-01-12 stsp const struct got_error *
394 a1fd68d8 2018-01-12 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
396 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
397 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
398 a1fd68d8 2018-01-12 stsp struct got_packfile_hdr hdr;
401 a1fd68d8 2018-01-12 stsp n = fread(&hdr, sizeof(hdr), 1, f);
402 a1fd68d8 2018-01-12 stsp if (n != 1)
403 8251fdbc 2018-01-12 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
405 a1fd68d8 2018-01-12 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
406 a1fd68d8 2018-01-12 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
407 a1fd68d8 2018-01-12 stsp betoh32(hdr.nobjects) != totobj)
408 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
410 a1fd68d8 2018-01-12 stsp return err;
413 a487c1d0 2018-01-14 stsp static const struct got_error *
414 c7fe698a 2018-01-23 stsp open_packfile(FILE **packfile, char **path_packfile,
415 c7fe698a 2018-01-23 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
417 c7fe698a 2018-01-23 stsp const struct got_error *err;
419 c7fe698a 2018-01-23 stsp *packfile = NULL;
421 c7fe698a 2018-01-23 stsp err = get_packfile_path(path_packfile, repo, packidx);
423 c7fe698a 2018-01-23 stsp return err;
425 c7fe698a 2018-01-23 stsp *packfile = fopen(*path_packfile, "rb");
426 c7fe698a 2018-01-23 stsp if (*packfile == NULL) {
427 c7fe698a 2018-01-23 stsp err = got_error_from_errno();
428 c7fe698a 2018-01-23 stsp free(*path_packfile);
429 c7fe698a 2018-01-23 stsp return err;
432 c7fe698a 2018-01-23 stsp err = read_packfile_hdr(*packfile, packidx);
434 c7fe698a 2018-01-23 stsp fclose(*packfile);
435 c7fe698a 2018-01-23 stsp *packfile = NULL;
437 c7fe698a 2018-01-23 stsp return err;
440 c7fe698a 2018-01-23 stsp static const struct got_error *
441 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
442 c3703302 2018-01-23 stsp FILE *packfile)
444 a487c1d0 2018-01-14 stsp uint8_t t = 0;
445 a487c1d0 2018-01-14 stsp uint64_t s = 0;
446 a487c1d0 2018-01-14 stsp uint8_t sizeN;
451 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
453 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
455 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
456 a487c1d0 2018-01-14 stsp if (n != 1)
457 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
459 a1fd68d8 2018-01-12 stsp if (i == 0) {
460 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
461 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
462 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
464 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
465 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
468 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
472 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
473 a487c1d0 2018-01-14 stsp return NULL;
476 a1fd68d8 2018-01-12 stsp static const struct got_error *
477 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
478 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
480 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
481 6ccb713b 2018-01-19 stsp if (*obj == NULL)
482 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
484 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
485 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
486 6ccb713b 2018-01-19 stsp free(*obj);
487 6ccb713b 2018-01-19 stsp *obj = NULL;
488 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
491 6ccb713b 2018-01-19 stsp (*obj)->type = type;
492 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
493 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
494 6ccb713b 2018-01-19 stsp (*obj)->size = size;
495 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
496 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
498 b107e67f 2018-01-19 stsp return NULL;
501 b107e67f 2018-01-19 stsp static const struct got_error *
502 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
504 b107e67f 2018-01-19 stsp int64_t o = 0;
505 b107e67f 2018-01-19 stsp uint8_t offN;
510 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
512 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
514 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
515 b107e67f 2018-01-19 stsp if (n != 1)
516 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
518 b107e67f 2018-01-19 stsp if (i == 0)
519 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
523 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
526 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
528 b107e67f 2018-01-19 stsp *offset = o;
529 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
530 6ccb713b 2018-01-19 stsp return NULL;
533 6ccb713b 2018-01-19 stsp static const struct got_error *
534 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
536 96f5e8b3 2018-01-23 stsp const struct got_error *err;
537 b107e67f 2018-01-19 stsp int64_t negoffset;
538 b107e67f 2018-01-19 stsp size_t negofflen;
540 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
542 b107e67f 2018-01-19 stsp return err;
544 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
545 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
546 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
547 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
549 96f5e8b3 2018-01-23 stsp return NULL;
552 0e22967e 2018-02-11 stsp static const struct got_error *
553 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
554 0e22967e 2018-02-11 stsp FILE *, const char *, off_t, size_t, int, size_t);
556 96f5e8b3 2018-01-23 stsp static const struct got_error *
557 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
558 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
559 bdd2fbb3 2018-02-11 stsp size_t delta_data_offset)
561 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
563 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
564 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
565 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
566 bdd2fbb3 2018-02-11 stsp return got_error(GOT_ERR_NO_MEM);
567 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
568 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
569 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
570 bdd2fbb3 2018-02-11 stsp return NULL;
573 bdd2fbb3 2018-02-11 stsp static const struct got_error *
574 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
575 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
576 bdd2fbb3 2018-02-11 stsp off_t delta_offset,size_t tslen, int delta_type, size_t delta_size)
579 a3500804 2018-01-23 stsp const struct got_error *err;
580 c3703302 2018-01-23 stsp off_t base_offset;
581 c3703302 2018-01-23 stsp uint8_t base_type;
582 c3703302 2018-01-23 stsp uint64_t base_size;
583 c3703302 2018-01-23 stsp size_t base_tslen;
584 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
586 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
588 a3500804 2018-01-23 stsp return err;
590 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
591 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
592 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
594 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
595 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
597 bdd2fbb3 2018-02-11 stsp return err;
599 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
600 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
601 b107e67f 2018-01-19 stsp return got_error_from_errno();
603 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
606 b107e67f 2018-01-19 stsp return err;
608 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
609 0e22967e 2018-02-11 stsp base_offset, base_tslen, base_type, base_size);
612 c3703302 2018-01-23 stsp static const struct got_error *
613 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
614 bdd2fbb3 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset,
615 bdd2fbb3 2018-02-11 stsp size_t tslen, int delta_type, size_t delta_size)
617 6b9c9673 2018-01-23 stsp const struct got_error *err;
618 6b9c9673 2018-01-23 stsp struct got_object_id id;
619 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
621 6b9c9673 2018-01-23 stsp off_t base_offset;
622 6b9c9673 2018-01-23 stsp uint8_t base_type;
623 6b9c9673 2018-01-23 stsp uint64_t base_size;
624 6b9c9673 2018-01-23 stsp size_t base_tslen;
626 6b9c9673 2018-01-23 stsp FILE *base_packfile;
627 6b9c9673 2018-01-23 stsp char *path_base_packfile;
628 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
630 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
631 6b9c9673 2018-01-23 stsp if (n != 1)
632 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
634 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
635 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
636 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
638 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
639 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
641 bdd2fbb3 2018-02-11 stsp return err;
643 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
645 6b9c9673 2018-01-23 stsp return err;
647 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
648 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
649 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
650 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
653 c7fe698a 2018-01-23 stsp err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
654 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
656 6b9c9673 2018-01-23 stsp return err;
658 6b9c9673 2018-01-23 stsp if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
659 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
663 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
664 6b9c9673 2018-01-23 stsp base_packfile);
668 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(deltas, repo, base_packfile,
669 0e22967e 2018-02-11 stsp path_base_packfile, base_offset, base_tslen, base_type,
670 0e22967e 2018-02-11 stsp base_size);
672 6b9c9673 2018-01-23 stsp free(path_base_packfile);
673 6b9c9673 2018-01-23 stsp if (base_packfile && fclose(base_packfile) == -1 && err == 0)
674 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
675 6b9c9673 2018-01-23 stsp return err;
678 6b9c9673 2018-01-23 stsp static const struct got_error *
679 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
680 0e22967e 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
681 0e22967e 2018-02-11 stsp int delta_type, size_t delta_size)
683 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
685 c3703302 2018-01-23 stsp switch (delta_type) {
686 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
687 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
688 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
689 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
690 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
691 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
692 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, 0);
694 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
695 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
696 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
697 bdd2fbb3 2018-02-11 stsp delta_size);
699 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
700 bdd2fbb3 2018-02-11 stsp err = resolve_ref_delta(deltas, repo, packfile,
701 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
702 bdd2fbb3 2018-02-11 stsp delta_size);
705 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
708 96f5e8b3 2018-01-23 stsp return err;
711 96f5e8b3 2018-01-23 stsp static const struct got_error *
712 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
713 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
714 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
715 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
717 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
718 96f5e8b3 2018-01-23 stsp struct got_object_id base_id;
719 96f5e8b3 2018-01-23 stsp uint8_t base_type;
720 96f5e8b3 2018-01-23 stsp int resolved_type;
721 96f5e8b3 2018-01-23 stsp uint64_t base_size;
722 96f5e8b3 2018-01-23 stsp size_t base_tslen;
724 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
725 b107e67f 2018-01-19 stsp if (*obj == NULL)
726 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
728 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
729 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
730 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
731 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
732 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
734 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
735 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
736 96f5e8b3 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
739 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
741 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
742 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
744 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
745 0e22967e 2018-02-11 stsp path_packfile, offset, tslen, delta_type, delta_size);
749 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
752 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
756 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
757 96f5e8b3 2018-01-23 stsp *obj = NULL;
759 96f5e8b3 2018-01-23 stsp return err;
762 b107e67f 2018-01-19 stsp static const struct got_error *
763 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
764 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
766 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
767 a1fd68d8 2018-01-12 stsp off_t offset;
768 6c00b545 2018-01-17 stsp char *path_packfile;
769 6c00b545 2018-01-17 stsp FILE *packfile;
770 6c00b545 2018-01-17 stsp uint8_t type;
771 6c00b545 2018-01-17 stsp uint64_t size;
772 3ee5fc21 2018-01-17 stsp size_t tslen;
774 6c00b545 2018-01-17 stsp *obj = NULL;
776 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
777 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
778 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
780 c7fe698a 2018-01-23 stsp err = open_packfile(&packfile, &path_packfile, repo, packidx);
782 6b9c9673 2018-01-23 stsp return err;
784 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
785 6c00b545 2018-01-17 stsp err = got_error_from_errno();
789 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&type, &size, &tslen, packfile);
793 6c00b545 2018-01-17 stsp switch (type) {
794 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
795 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
796 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
797 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
798 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
799 6ccb713b 2018-01-19 stsp offset + tslen, size);
802 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
803 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
804 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
805 a48db7e5 2018-01-23 stsp packfile, id, offset, tslen, type, size);
809 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
813 a1fd68d8 2018-01-12 stsp free(path_packfile);
814 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
815 f334529e 2018-01-12 stsp err = got_error_from_errno();
816 a1fd68d8 2018-01-12 stsp return err;
819 a1fd68d8 2018-01-12 stsp const struct got_error *
820 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
821 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
823 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
824 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
827 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
829 6b9c9673 2018-01-23 stsp return err;
831 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
832 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
833 3ee5fc21 2018-01-17 stsp return err;
836 efd2a263 2018-01-19 stsp static const struct got_error *
837 710bb5ca 2018-01-23 stsp dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
839 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
840 6691714a 2018-01-23 stsp struct got_delta *delta;
841 6691714a 2018-01-23 stsp FILE *base_file, *accum_file;
844 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
845 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
847 6691714a 2018-01-23 stsp base_file = got_opentemp();
848 6691714a 2018-01-23 stsp if (base_file == NULL)
849 6691714a 2018-01-23 stsp return got_error_from_errno();
851 6691714a 2018-01-23 stsp accum_file = got_opentemp();
852 6691714a 2018-01-23 stsp if (accum_file == NULL) {
853 6691714a 2018-01-23 stsp err = got_error_from_errno();
854 6691714a 2018-01-23 stsp fclose(base_file);
855 efd2a263 2018-01-19 stsp return err;
858 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
859 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
860 885d3e02 2018-01-27 stsp uint8_t *delta_buf = NULL;
861 885d3e02 2018-01-27 stsp size_t delta_len = 0;
862 885d3e02 2018-01-27 stsp FILE *delta_file;
864 885d3e02 2018-01-27 stsp delta_file = fopen(delta->path_packfile, "rb");
865 6691714a 2018-01-23 stsp if (delta_file == NULL) {
866 6691714a 2018-01-23 stsp err = got_error_from_errno();
871 a6b158cc 2018-02-11 stsp if (n == 0) {
872 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
873 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
874 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
875 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
876 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
877 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
881 bdd2fbb3 2018-02-11 stsp if (fseeko(delta_file, delta->offset + delta->tslen,
882 bdd2fbb3 2018-02-11 stsp SEEK_SET) != 0) {
883 bdd2fbb3 2018-02-11 stsp fclose(delta_file);
884 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
887 a6b158cc 2018-02-11 stsp err = got_inflate_to_file(&delta_len, delta_file,
888 a6b158cc 2018-02-11 stsp base_file);
889 a6b158cc 2018-02-11 stsp fclose(delta_file);
893 a6b158cc 2018-02-11 stsp rewind(base_file);
897 bdd2fbb3 2018-02-11 stsp if (fseeko(delta_file, delta->data_offset, SEEK_CUR) != 0) {
898 0e22967e 2018-02-11 stsp fclose(delta_file);
899 0e22967e 2018-02-11 stsp err = got_error_from_errno();
903 885d3e02 2018-01-27 stsp /* Delta streams should always fit in memory. */
904 61d262a8 2018-02-11 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, delta_file);
905 a6b158cc 2018-02-11 stsp fclose(delta_file);
909 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, delta_buf, delta_len,
910 6691714a 2018-01-23 stsp /* Final delta application writes to the output file. */
911 710bb5ca 2018-01-23 stsp ++n < deltas->nentries ? accum_file : outfile);
912 885d3e02 2018-01-27 stsp free(delta_buf);
916 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
917 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
918 6691714a 2018-01-23 stsp FILE *tmp = accum_file;
919 6691714a 2018-01-23 stsp accum_file = base_file;
920 6691714a 2018-01-23 stsp base_file = tmp;
921 6691714a 2018-01-23 stsp rewind(base_file);
922 6691714a 2018-01-23 stsp rewind(accum_file);
927 6691714a 2018-01-23 stsp fclose(base_file);
928 6691714a 2018-01-23 stsp fclose(accum_file);
929 6691714a 2018-01-23 stsp rewind(outfile);
930 efd2a263 2018-01-19 stsp return err;
933 3ee5fc21 2018-01-17 stsp const struct got_error *
934 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
935 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
937 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
938 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
940 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
941 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
943 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
944 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
945 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
949 6691714a 2018-01-23 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
950 6691714a 2018-01-23 stsp packfile = fopen(obj->path_packfile, "rb");
951 6691714a 2018-01-23 stsp if (packfile == NULL) {
952 6691714a 2018-01-23 stsp err = got_error_from_errno();
956 6691714a 2018-01-23 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
957 6691714a 2018-01-23 stsp err = got_error_from_errno();
961 3606d7fc 2018-02-11 stsp err = got_inflate_to_file(&obj->size, packfile, *f);
963 710bb5ca 2018-01-23 stsp err = dump_delta_chain(&obj->deltas, *f);
965 3ee5fc21 2018-01-17 stsp if (packfile)
966 3ee5fc21 2018-01-17 stsp fclose(packfile);
967 3ee5fc21 2018-01-17 stsp if (err && *f)
968 3ee5fc21 2018-01-17 stsp fclose(*f);
969 a1fd68d8 2018-01-12 stsp return err;