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"
35 a1fd68d8 2018-01-12 stsp #include "got_sha1.h"
36 0a0a3048 2018-01-10 stsp #include "pack.h"
37 a1fd68d8 2018-01-12 stsp #include "path.h"
38 efd2a263 2018-01-19 stsp #include "delta.h"
39 eef6493a 2018-01-19 stsp #include "object.h"
41 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
42 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
43 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
44 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
45 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
46 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
47 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
48 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
49 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
52 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 0a0a3048 2018-01-10 stsp static const struct got_error *
56 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
60 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
61 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
62 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
65 0a0a3048 2018-01-10 stsp return NULL;
68 24541888 2018-01-10 stsp static const struct got_error *
69 0a0a3048 2018-01-10 stsp get_packfile_size(size_t *size, const char *path_idx)
71 0a0a3048 2018-01-10 stsp struct stat sb;
72 0a0a3048 2018-01-10 stsp char *path_pack;
73 0a0a3048 2018-01-10 stsp char base_path[PATH_MAX];
76 0a0a3048 2018-01-10 stsp if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
77 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_SPACE);
79 0a0a3048 2018-01-10 stsp dot = strrchr(base_path, '.');
80 0a0a3048 2018-01-10 stsp if (dot == NULL)
81 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
82 0a0a3048 2018-01-10 stsp *dot = '\0';
83 0a0a3048 2018-01-10 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
84 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_MEM);
86 0a0a3048 2018-01-10 stsp if (stat(path_pack, &sb) != 0) {
87 0a0a3048 2018-01-10 stsp free(path_pack);
88 8251fdbc 2018-01-12 stsp return got_error_from_errno();
91 0a0a3048 2018-01-10 stsp free(path_pack);
92 0a0a3048 2018-01-10 stsp *size = sb.st_size;
96 0a0a3048 2018-01-10 stsp const struct got_error *
97 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
99 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
101 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
102 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
103 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
104 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
106 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
108 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
109 0a0a3048 2018-01-10 stsp if (f == NULL)
110 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
112 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
114 0a0a3048 2018-01-10 stsp return err;
116 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
117 0a0a3048 2018-01-10 stsp if (p == NULL) {
118 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
122 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
123 0a0a3048 2018-01-10 stsp if (n != 1) {
124 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
128 0a0a3048 2018-01-10 stsp if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
129 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
133 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
135 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
136 0a0a3048 2018-01-10 stsp if (n != 1) {
137 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
141 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
142 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
146 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
148 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
149 0a0a3048 2018-01-10 stsp if (n != 1) {
150 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
154 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
158 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
160 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
162 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
163 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
164 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
168 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
169 0a0a3048 2018-01-10 stsp if (n != nobj) {
170 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
174 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
175 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
177 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
178 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
179 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
183 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
184 0a0a3048 2018-01-10 stsp if (n != nobj) {
185 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
189 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
191 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
192 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
193 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
197 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
198 0a0a3048 2018-01-10 stsp if (n != nobj) {
199 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
203 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
205 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
206 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
207 0a0a3048 2018-01-10 stsp goto checksum;
209 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
210 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
211 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
215 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
216 0a0a3048 2018-01-10 stsp if (n != nobj) {
217 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
221 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
222 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
225 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
226 0a0a3048 2018-01-10 stsp if (n != 1) {
227 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
231 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
232 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
233 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
234 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
238 0a0a3048 2018-01-10 stsp got_packidx_close(p);
240 0a0a3048 2018-01-10 stsp *packidx = p;
241 0a0a3048 2018-01-10 stsp return err;
245 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
247 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
248 0a0a3048 2018-01-10 stsp free(packidx->offsets);
249 0a0a3048 2018-01-10 stsp free(packidx->crc32);
250 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
251 0a0a3048 2018-01-10 stsp free(packidx);
255 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
257 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
260 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
263 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
264 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
270 a1fd68d8 2018-01-12 stsp static off_t
271 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
273 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
274 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
275 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
276 a1fd68d8 2018-01-12 stsp uint64_t loffset;
277 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
278 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
280 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
281 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
283 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
287 a1fd68d8 2018-01-12 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
289 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
290 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
293 a1fd68d8 2018-01-12 stsp if (id0 > 0)
294 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
296 a1fd68d8 2018-01-12 stsp while (i < totobj) {
297 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
298 a1fd68d8 2018-01-12 stsp uint32_t offset;
299 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
301 6c00b545 2018-01-17 stsp if (cmp == 0)
303 6c00b545 2018-01-17 stsp else if (cmp > 0)
311 a1fd68d8 2018-01-12 stsp const struct got_error *
312 a1fd68d8 2018-01-12 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
314 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
315 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
316 a1fd68d8 2018-01-12 stsp struct got_packfile_hdr hdr;
319 a1fd68d8 2018-01-12 stsp n = fread(&hdr, sizeof(hdr), 1, f);
320 a1fd68d8 2018-01-12 stsp if (n != 1)
321 8251fdbc 2018-01-12 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
323 a1fd68d8 2018-01-12 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
324 a1fd68d8 2018-01-12 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
325 a1fd68d8 2018-01-12 stsp betoh32(hdr.nobjects) != totobj)
326 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
328 a1fd68d8 2018-01-12 stsp return err;
331 a487c1d0 2018-01-14 stsp static const struct got_error *
332 3ee5fc21 2018-01-17 stsp decode_type_and_size(uint8_t *type, uint64_t *size, size_t *len, FILE *packfile)
334 a487c1d0 2018-01-14 stsp uint8_t t = 0;
335 a487c1d0 2018-01-14 stsp uint64_t s = 0;
336 a487c1d0 2018-01-14 stsp uint8_t sizeN;
341 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
343 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
345 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
346 a487c1d0 2018-01-14 stsp if (n != 1)
347 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
349 a1fd68d8 2018-01-12 stsp if (i == 0) {
350 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
351 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
352 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
354 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
355 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
358 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
362 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
363 a487c1d0 2018-01-14 stsp return NULL;
366 a1fd68d8 2018-01-12 stsp static const struct got_error *
367 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
368 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
370 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
371 6ccb713b 2018-01-19 stsp if (*obj == NULL)
372 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
374 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
375 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
376 6ccb713b 2018-01-19 stsp free(*obj);
377 6ccb713b 2018-01-19 stsp *obj = NULL;
378 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
381 6ccb713b 2018-01-19 stsp (*obj)->type = type;
382 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
383 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
384 6ccb713b 2018-01-19 stsp (*obj)->size = size;
385 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
386 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
388 b107e67f 2018-01-19 stsp return NULL;
391 b107e67f 2018-01-19 stsp static const struct got_error *
392 b107e67f 2018-01-19 stsp decode_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
394 b107e67f 2018-01-19 stsp int64_t o = 0;
395 b107e67f 2018-01-19 stsp uint8_t offN;
400 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
402 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
404 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
405 b107e67f 2018-01-19 stsp if (n != 1)
406 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
408 b107e67f 2018-01-19 stsp if (i == 0)
409 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
413 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
416 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
418 b107e67f 2018-01-19 stsp *offset = o;
419 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
420 6ccb713b 2018-01-19 stsp return NULL;
423 6ccb713b 2018-01-19 stsp static const struct got_error *
424 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
426 96f5e8b3 2018-01-23 stsp const struct got_error *err;
427 b107e67f 2018-01-19 stsp int64_t negoffset;
428 b107e67f 2018-01-19 stsp size_t negofflen;
430 b107e67f 2018-01-19 stsp err = decode_negative_offset(&negoffset, &negofflen, packfile);
432 b107e67f 2018-01-19 stsp return err;
434 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
435 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
436 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
437 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
439 96f5e8b3 2018-01-23 stsp return NULL;
442 a3500804 2018-01-23 stsp static const struct got_error *resolve_delta_chain(struct got_delta_chain *,
443 a3500804 2018-01-23 stsp FILE *, const char *, off_t, size_t);
445 96f5e8b3 2018-01-23 stsp static const struct got_error *
446 a3500804 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas, FILE *packfile,
447 a3500804 2018-01-23 stsp const char *path_packfile, off_t offset, size_t delta_size)
449 a3500804 2018-01-23 stsp const struct got_error *err;
450 a3500804 2018-01-23 stsp off_t next_offset;
452 a3500804 2018-01-23 stsp err = parse_offset_delta(&next_offset, packfile, offset);
454 a3500804 2018-01-23 stsp return err;
456 a3500804 2018-01-23 stsp /* Next offset must be in the same packfile. */
457 a3500804 2018-01-23 stsp return resolve_delta_chain(deltas, packfile, path_packfile,
458 a3500804 2018-01-23 stsp next_offset, delta_size);
461 a3500804 2018-01-23 stsp static const struct got_error *
462 96f5e8b3 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, FILE *packfile,
463 96f5e8b3 2018-01-23 stsp const char *path_packfile, off_t offset, size_t delta_size)
465 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
466 96f5e8b3 2018-01-23 stsp uint8_t base_type;
467 96f5e8b3 2018-01-23 stsp uint64_t base_size;
468 96f5e8b3 2018-01-23 stsp size_t base_tslen;
469 96f5e8b3 2018-01-23 stsp struct got_delta_base *base;
471 96f5e8b3 2018-01-23 stsp if (fseeko(packfile, offset, SEEK_SET) != 0)
472 b107e67f 2018-01-19 stsp return got_error_from_errno();
474 b107e67f 2018-01-19 stsp err = decode_type_and_size(&base_type, &base_size, &base_tslen,
477 b107e67f 2018-01-19 stsp return err;
479 96f5e8b3 2018-01-23 stsp base = got_delta_base_open(path_packfile, base_type, offset,
480 96f5e8b3 2018-01-23 stsp delta_size);
481 96f5e8b3 2018-01-23 stsp if (base == NULL)
482 96f5e8b3 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
483 96f5e8b3 2018-01-23 stsp deltas->nentries++;
484 96f5e8b3 2018-01-23 stsp SIMPLEQ_INSERT_TAIL(&deltas->entries, base, entry);
485 96f5e8b3 2018-01-23 stsp /* In case of error below, base will be freed in got_object_close(). */
487 4e8cda55 2018-01-19 stsp switch (base_type) {
488 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
489 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
490 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
491 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
493 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
494 a3500804 2018-01-23 stsp err = resolve_offset_delta(deltas, packfile, path_packfile,
495 a3500804 2018-01-23 stsp offset, base_size);
497 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
499 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
502 96f5e8b3 2018-01-23 stsp return err;
505 96f5e8b3 2018-01-23 stsp static const struct got_error *
506 96f5e8b3 2018-01-23 stsp open_offset_delta_object(struct got_object **obj,
507 96f5e8b3 2018-01-23 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx,
508 96f5e8b3 2018-01-23 stsp const char *path_packfile, FILE *packfile, struct got_object_id *id,
509 96f5e8b3 2018-01-23 stsp off_t offset, size_t tslen, size_t size)
511 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
512 96f5e8b3 2018-01-23 stsp off_t base_offset;
513 96f5e8b3 2018-01-23 stsp struct got_object_id base_id;
514 96f5e8b3 2018-01-23 stsp uint8_t base_type;
515 96f5e8b3 2018-01-23 stsp int resolved_type;
516 96f5e8b3 2018-01-23 stsp uint64_t base_size;
517 96f5e8b3 2018-01-23 stsp size_t base_tslen;
519 96f5e8b3 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, offset);
521 96f5e8b3 2018-01-23 stsp return err;
523 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
524 b107e67f 2018-01-19 stsp if (*obj == NULL)
525 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
527 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
528 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
529 96f5e8b3 2018-01-23 stsp (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
530 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
531 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
533 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
534 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
535 96f5e8b3 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
538 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
540 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
541 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
542 96f5e8b3 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, packfile, path_packfile,
543 96f5e8b3 2018-01-23 stsp base_offset, size);
547 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
550 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
554 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
555 96f5e8b3 2018-01-23 stsp *obj = NULL;
557 96f5e8b3 2018-01-23 stsp return err;
560 b107e67f 2018-01-19 stsp static const struct got_error *
561 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
562 6c00b545 2018-01-17 stsp const char *path_packdir, struct got_packidx_v2_hdr *packidx,
563 6c00b545 2018-01-17 stsp struct got_object_id *id)
565 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
566 a1fd68d8 2018-01-12 stsp int idx = get_object_idx(packidx, id);
567 a1fd68d8 2018-01-12 stsp off_t offset;
568 a1fd68d8 2018-01-12 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
569 a1fd68d8 2018-01-12 stsp char *sha1str;
570 6c00b545 2018-01-17 stsp char *path_packfile;
571 6c00b545 2018-01-17 stsp FILE *packfile;
572 6c00b545 2018-01-17 stsp uint8_t type;
573 6c00b545 2018-01-17 stsp uint64_t size;
574 3ee5fc21 2018-01-17 stsp size_t tslen;
576 6c00b545 2018-01-17 stsp *obj = NULL;
577 a1fd68d8 2018-01-12 stsp if (idx == -1) /* object not found in pack index */
578 a1fd68d8 2018-01-12 stsp return NULL;
580 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
581 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
582 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
584 a1fd68d8 2018-01-12 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
585 a1fd68d8 2018-01-12 stsp hex, sizeof(hex));
586 a1fd68d8 2018-01-12 stsp if (sha1str == NULL)
587 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
589 a1fd68d8 2018-01-12 stsp if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
590 a1fd68d8 2018-01-12 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
591 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_NO_MEM);
593 a1fd68d8 2018-01-12 stsp packfile = fopen(path_packfile, "rb");
594 a1fd68d8 2018-01-12 stsp if (packfile == NULL) {
595 f334529e 2018-01-12 stsp err = got_error_from_errno();
599 a1fd68d8 2018-01-12 stsp err = read_packfile_hdr(packfile, packidx);
603 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
604 6c00b545 2018-01-17 stsp err = got_error_from_errno();
608 3ee5fc21 2018-01-17 stsp err = decode_type_and_size(&type, &size, &tslen, packfile);
612 6c00b545 2018-01-17 stsp switch (type) {
613 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
614 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
615 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
616 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
617 6ccb713b 2018-01-19 stsp offset + tslen, size);
620 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
621 96f5e8b3 2018-01-23 stsp err = open_offset_delta_object(obj, repo, packidx,
622 96f5e8b3 2018-01-23 stsp path_packfile, packfile, id, offset, tslen, size);
625 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_REF_DELTA:
626 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TAG:
628 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
632 a1fd68d8 2018-01-12 stsp free(path_packfile);
633 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
634 f334529e 2018-01-12 stsp err = got_error_from_errno();
635 a1fd68d8 2018-01-12 stsp return err;
638 a1fd68d8 2018-01-12 stsp const struct got_error *
639 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
640 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
642 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
643 a1fd68d8 2018-01-12 stsp DIR *packdir = NULL;
644 a1fd68d8 2018-01-12 stsp struct dirent *dent;
645 a1fd68d8 2018-01-12 stsp char *path_packdir = got_repo_get_path_objects_pack(repo);
647 a1fd68d8 2018-01-12 stsp if (path_packdir == NULL) {
648 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_NO_MEM);
652 a1fd68d8 2018-01-12 stsp packdir = opendir(path_packdir);
653 a1fd68d8 2018-01-12 stsp if (packdir == NULL) {
654 f334529e 2018-01-12 stsp err = got_error_from_errno();
658 a1fd68d8 2018-01-12 stsp while ((dent = readdir(packdir)) != NULL) {
659 a1fd68d8 2018-01-12 stsp struct got_packidx_v2_hdr *packidx;
660 a1fd68d8 2018-01-12 stsp char *path_packidx, *path_object;
662 a1fd68d8 2018-01-12 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
665 a1fd68d8 2018-01-12 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
666 a1fd68d8 2018-01-12 stsp dent->d_name) == -1) {
667 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_NO_MEM);
671 a1fd68d8 2018-01-12 stsp err = got_packidx_open(&packidx, path_packidx);
672 a1fd68d8 2018-01-12 stsp free(path_packidx);
676 6c00b545 2018-01-17 stsp err = open_packed_object(obj, repo, path_packdir, packidx, id);
677 bbcf6d65 2018-01-17 stsp got_packidx_close(packidx);
680 6c00b545 2018-01-17 stsp if (*obj != NULL)
685 a1fd68d8 2018-01-12 stsp free(path_packdir);
686 f334529e 2018-01-12 stsp if (packdir && closedir(packdir) != 0 && err == 0)
687 3ee5fc21 2018-01-17 stsp err = got_error_from_errno();
688 3ee5fc21 2018-01-17 stsp return err;
691 3ee5fc21 2018-01-17 stsp static const struct got_error *
692 3ee5fc21 2018-01-17 stsp dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
696 3ee5fc21 2018-01-17 stsp while (size > 0) {
697 3ee5fc21 2018-01-17 stsp uint8_t data[2048];
698 3ee5fc21 2018-01-17 stsp size_t len = MIN(size, sizeof(data));
700 3ee5fc21 2018-01-17 stsp n = fread(data, len, 1, infile);
701 3ee5fc21 2018-01-17 stsp if (n != 1)
702 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
704 3ee5fc21 2018-01-17 stsp n = fwrite(data, len, 1, outfile);
705 3ee5fc21 2018-01-17 stsp if (n != 1)
706 efd2a263 2018-01-19 stsp return got_ferror(outfile, GOT_ERR_IO);
708 3ee5fc21 2018-01-17 stsp size -= len;
711 3ee5fc21 2018-01-17 stsp rewind(outfile);
712 3ee5fc21 2018-01-17 stsp return NULL;
715 efd2a263 2018-01-19 stsp static const struct got_error *
716 efd2a263 2018-01-19 stsp dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
717 efd2a263 2018-01-19 stsp size_t size, FILE *outfile)
719 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
720 efd2a263 2018-01-19 stsp struct got_object_id base_id;
721 efd2a263 2018-01-19 stsp struct got_object *base_obj;
724 efd2a263 2018-01-19 stsp if (size < sizeof(base_id))
725 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
727 efd2a263 2018-01-19 stsp n = fread(&base_id, sizeof(base_id), 1, infile);
728 efd2a263 2018-01-19 stsp if (n != 1)
729 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
731 efd2a263 2018-01-19 stsp size -= sizeof(base_id);
732 efd2a263 2018-01-19 stsp if (size <= 0)
733 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
735 efd2a263 2018-01-19 stsp err = got_object_open(&base_obj, repo, &base_id);
737 efd2a263 2018-01-19 stsp return err;
739 efd2a263 2018-01-19 stsp err = got_delta_apply(repo, infile, size, base_obj, outfile);
740 efd2a263 2018-01-19 stsp got_object_close(base_obj);
741 efd2a263 2018-01-19 stsp return err;
744 3ee5fc21 2018-01-17 stsp const struct got_error *
745 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
746 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
748 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
749 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
751 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
752 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
754 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
755 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
756 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
760 3ee5fc21 2018-01-17 stsp packfile = fopen(obj->path_packfile, "rb");
761 3ee5fc21 2018-01-17 stsp if (packfile == NULL) {
762 f334529e 2018-01-12 stsp err = got_error_from_errno();
766 3ee5fc21 2018-01-17 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
767 3ee5fc21 2018-01-17 stsp err = got_error_from_errno();
771 3ee5fc21 2018-01-17 stsp switch (obj->type) {
772 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
773 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
774 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
775 3ee5fc21 2018-01-17 stsp err = dump_plain_object(packfile, obj->type, obj->size, *f);
777 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_REF_DELTA:
778 efd2a263 2018-01-19 stsp err = dump_ref_delta_object(repo, packfile, obj->type,
779 efd2a263 2018-01-19 stsp obj->size, *f);
781 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_TAG:
782 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
784 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
788 3ee5fc21 2018-01-17 stsp if (packfile)
789 3ee5fc21 2018-01-17 stsp fclose(packfile);
790 3ee5fc21 2018-01-17 stsp if (err && *f)
791 3ee5fc21 2018-01-17 stsp fclose(*f);
792 a1fd68d8 2018-01-12 stsp return err;