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"
40 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
41 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
42 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
43 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
44 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
45 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
46 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
47 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
48 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
51 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 0a0a3048 2018-01-10 stsp static const struct got_error *
55 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
59 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
60 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
61 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
64 0a0a3048 2018-01-10 stsp return NULL;
67 24541888 2018-01-10 stsp static const struct got_error *
68 0a0a3048 2018-01-10 stsp get_packfile_size(size_t *size, const char *path_idx)
70 0a0a3048 2018-01-10 stsp struct stat sb;
71 0a0a3048 2018-01-10 stsp char *path_pack;
72 0a0a3048 2018-01-10 stsp char base_path[PATH_MAX];
75 0a0a3048 2018-01-10 stsp if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
76 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_SPACE);
78 0a0a3048 2018-01-10 stsp dot = strrchr(base_path, '.');
79 0a0a3048 2018-01-10 stsp if (dot == NULL)
80 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
81 0a0a3048 2018-01-10 stsp *dot = '\0';
82 0a0a3048 2018-01-10 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
83 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_MEM);
85 0a0a3048 2018-01-10 stsp if (stat(path_pack, &sb) != 0) {
86 0a0a3048 2018-01-10 stsp free(path_pack);
87 8251fdbc 2018-01-12 stsp return got_error_from_errno();
90 0a0a3048 2018-01-10 stsp free(path_pack);
91 0a0a3048 2018-01-10 stsp *size = sb.st_size;
95 0a0a3048 2018-01-10 stsp const struct got_error *
96 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
98 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
100 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
101 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
102 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
103 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
105 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
107 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
108 0a0a3048 2018-01-10 stsp if (f == NULL)
109 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
111 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
113 0a0a3048 2018-01-10 stsp return err;
115 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
116 0a0a3048 2018-01-10 stsp if (p == NULL) {
117 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
121 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
122 0a0a3048 2018-01-10 stsp if (n != 1) {
123 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
127 0a0a3048 2018-01-10 stsp if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
128 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
132 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
134 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
135 0a0a3048 2018-01-10 stsp if (n != 1) {
136 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
140 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
141 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
145 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
147 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
148 0a0a3048 2018-01-10 stsp if (n != 1) {
149 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
153 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
157 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
159 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
161 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
162 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
163 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
167 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
168 0a0a3048 2018-01-10 stsp if (n != nobj) {
169 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
173 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
174 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
176 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
177 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
178 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
182 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
183 0a0a3048 2018-01-10 stsp if (n != nobj) {
184 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
188 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
190 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
191 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
192 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
196 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
197 0a0a3048 2018-01-10 stsp if (n != nobj) {
198 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
202 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
204 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
205 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
206 0a0a3048 2018-01-10 stsp goto checksum;
208 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
209 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
210 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
214 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
215 0a0a3048 2018-01-10 stsp if (n != nobj) {
216 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
220 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
221 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
224 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
225 0a0a3048 2018-01-10 stsp if (n != 1) {
226 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
230 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
231 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
232 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
233 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
237 0a0a3048 2018-01-10 stsp got_packidx_close(p);
239 0a0a3048 2018-01-10 stsp *packidx = p;
240 0a0a3048 2018-01-10 stsp return err;
244 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
246 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
247 0a0a3048 2018-01-10 stsp free(packidx->offsets);
248 0a0a3048 2018-01-10 stsp free(packidx->crc32);
249 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
250 0a0a3048 2018-01-10 stsp free(packidx);
254 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
256 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
259 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
262 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
263 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
269 a1fd68d8 2018-01-12 stsp static off_t
270 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
272 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
273 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
274 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
275 a1fd68d8 2018-01-12 stsp uint64_t loffset;
276 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
277 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
279 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
280 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
282 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
286 a1fd68d8 2018-01-12 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
288 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
289 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
292 a1fd68d8 2018-01-12 stsp if (id0 > 0)
293 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
295 a1fd68d8 2018-01-12 stsp while (i < totobj) {
296 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
297 a1fd68d8 2018-01-12 stsp uint32_t offset;
298 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
300 6c00b545 2018-01-17 stsp if (cmp == 0)
302 6c00b545 2018-01-17 stsp else if (cmp > 0)
310 a1fd68d8 2018-01-12 stsp const struct got_error *
311 a1fd68d8 2018-01-12 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
313 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
314 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
315 a1fd68d8 2018-01-12 stsp struct got_packfile_hdr hdr;
318 a1fd68d8 2018-01-12 stsp n = fread(&hdr, sizeof(hdr), 1, f);
319 a1fd68d8 2018-01-12 stsp if (n != 1)
320 8251fdbc 2018-01-12 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
322 a1fd68d8 2018-01-12 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
323 a1fd68d8 2018-01-12 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
324 a1fd68d8 2018-01-12 stsp betoh32(hdr.nobjects) != totobj)
325 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
327 a1fd68d8 2018-01-12 stsp return err;
330 a487c1d0 2018-01-14 stsp static const struct got_error *
331 3ee5fc21 2018-01-17 stsp decode_type_and_size(uint8_t *type, uint64_t *size, size_t *len, FILE *packfile)
333 a487c1d0 2018-01-14 stsp uint8_t t = 0;
334 a487c1d0 2018-01-14 stsp uint64_t s = 0;
335 a487c1d0 2018-01-14 stsp uint8_t sizeN;
340 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
342 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
344 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
345 a487c1d0 2018-01-14 stsp if (n != 1)
346 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
348 a1fd68d8 2018-01-12 stsp if (i == 0) {
349 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
350 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
351 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
353 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
354 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
357 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
361 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
362 a487c1d0 2018-01-14 stsp return NULL;
365 a1fd68d8 2018-01-12 stsp static const struct got_error *
366 6ccb713b 2018-01-19 stsp open_packed_plain_object(struct got_object **obj, const char *path_packfile,
367 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
369 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
370 6ccb713b 2018-01-19 stsp if (*obj == NULL)
371 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
373 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
374 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
375 6ccb713b 2018-01-19 stsp free(*obj);
376 6ccb713b 2018-01-19 stsp *obj = NULL;
377 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
380 6ccb713b 2018-01-19 stsp (*obj)->type = type;
381 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
382 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
383 6ccb713b 2018-01-19 stsp (*obj)->size = size;
384 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
385 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
387 6ccb713b 2018-01-19 stsp return NULL;
390 6ccb713b 2018-01-19 stsp static const struct got_error *
391 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
392 6c00b545 2018-01-17 stsp const char *path_packdir, struct got_packidx_v2_hdr *packidx,
393 6c00b545 2018-01-17 stsp struct got_object_id *id)
395 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
396 a1fd68d8 2018-01-12 stsp int idx = get_object_idx(packidx, id);
397 a1fd68d8 2018-01-12 stsp off_t offset;
398 a1fd68d8 2018-01-12 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
399 a1fd68d8 2018-01-12 stsp char *sha1str;
400 6c00b545 2018-01-17 stsp char *path_packfile;
401 6c00b545 2018-01-17 stsp FILE *packfile;
402 6c00b545 2018-01-17 stsp uint8_t type;
403 6c00b545 2018-01-17 stsp uint64_t size;
404 3ee5fc21 2018-01-17 stsp size_t tslen;
406 6c00b545 2018-01-17 stsp *obj = NULL;
407 a1fd68d8 2018-01-12 stsp if (idx == -1) /* object not found in pack index */
408 a1fd68d8 2018-01-12 stsp return NULL;
410 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
411 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
412 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
414 a1fd68d8 2018-01-12 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
415 a1fd68d8 2018-01-12 stsp hex, sizeof(hex));
416 a1fd68d8 2018-01-12 stsp if (sha1str == NULL)
417 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
419 a1fd68d8 2018-01-12 stsp if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
420 a1fd68d8 2018-01-12 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
421 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_NO_MEM);
423 a1fd68d8 2018-01-12 stsp packfile = fopen(path_packfile, "rb");
424 a1fd68d8 2018-01-12 stsp if (packfile == NULL) {
425 f334529e 2018-01-12 stsp err = got_error_from_errno();
429 a1fd68d8 2018-01-12 stsp err = read_packfile_hdr(packfile, packidx);
433 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
434 6c00b545 2018-01-17 stsp err = got_error_from_errno();
438 3ee5fc21 2018-01-17 stsp err = decode_type_and_size(&type, &size, &tslen, packfile);
442 6c00b545 2018-01-17 stsp switch (type) {
443 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
444 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
445 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
446 6ccb713b 2018-01-19 stsp err = open_packed_plain_object(obj, path_packfile, id, type,
447 6ccb713b 2018-01-19 stsp offset + tslen, size);
450 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_REF_DELTA:
451 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TAG:
452 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
454 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
458 a1fd68d8 2018-01-12 stsp free(path_packfile);
460 6c00b545 2018-01-17 stsp free(*obj);
461 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
462 f334529e 2018-01-12 stsp err = got_error_from_errno();
463 a1fd68d8 2018-01-12 stsp return err;
466 a1fd68d8 2018-01-12 stsp const struct got_error *
467 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
468 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
470 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
471 a1fd68d8 2018-01-12 stsp DIR *packdir = NULL;
472 a1fd68d8 2018-01-12 stsp struct dirent *dent;
473 a1fd68d8 2018-01-12 stsp char *path_packdir = got_repo_get_path_objects_pack(repo);
475 a1fd68d8 2018-01-12 stsp if (path_packdir == NULL) {
476 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_NO_MEM);
480 a1fd68d8 2018-01-12 stsp packdir = opendir(path_packdir);
481 a1fd68d8 2018-01-12 stsp if (packdir == NULL) {
482 f334529e 2018-01-12 stsp err = got_error_from_errno();
486 a1fd68d8 2018-01-12 stsp while ((dent = readdir(packdir)) != NULL) {
487 a1fd68d8 2018-01-12 stsp struct got_packidx_v2_hdr *packidx;
488 a1fd68d8 2018-01-12 stsp char *path_packidx, *path_object;
490 a1fd68d8 2018-01-12 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
493 a1fd68d8 2018-01-12 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
494 a1fd68d8 2018-01-12 stsp dent->d_name) == -1) {
495 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_NO_MEM);
499 a1fd68d8 2018-01-12 stsp err = got_packidx_open(&packidx, path_packidx);
500 a1fd68d8 2018-01-12 stsp free(path_packidx);
504 6c00b545 2018-01-17 stsp err = open_packed_object(obj, repo, path_packdir, packidx, id);
505 bbcf6d65 2018-01-17 stsp got_packidx_close(packidx);
508 6c00b545 2018-01-17 stsp if (*obj != NULL)
513 a1fd68d8 2018-01-12 stsp free(path_packdir);
514 f334529e 2018-01-12 stsp if (packdir && closedir(packdir) != 0 && err == 0)
515 3ee5fc21 2018-01-17 stsp err = got_error_from_errno();
516 3ee5fc21 2018-01-17 stsp return err;
519 3ee5fc21 2018-01-17 stsp static const struct got_error *
520 3ee5fc21 2018-01-17 stsp dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
524 3ee5fc21 2018-01-17 stsp while (size > 0) {
525 3ee5fc21 2018-01-17 stsp uint8_t data[2048];
526 3ee5fc21 2018-01-17 stsp size_t len = MIN(size, sizeof(data));
528 3ee5fc21 2018-01-17 stsp n = fread(data, len, 1, infile);
529 3ee5fc21 2018-01-17 stsp if (n != 1)
530 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
532 3ee5fc21 2018-01-17 stsp n = fwrite(data, len, 1, outfile);
533 3ee5fc21 2018-01-17 stsp if (n != 1)
534 efd2a263 2018-01-19 stsp return got_ferror(outfile, GOT_ERR_IO);
536 3ee5fc21 2018-01-17 stsp size -= len;
539 3ee5fc21 2018-01-17 stsp rewind(outfile);
540 3ee5fc21 2018-01-17 stsp return NULL;
543 efd2a263 2018-01-19 stsp static const struct got_error *
544 efd2a263 2018-01-19 stsp dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
545 efd2a263 2018-01-19 stsp size_t size, FILE *outfile)
547 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
548 efd2a263 2018-01-19 stsp struct got_object_id base_id;
549 efd2a263 2018-01-19 stsp struct got_object *base_obj;
552 efd2a263 2018-01-19 stsp if (size < sizeof(base_id))
553 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
555 efd2a263 2018-01-19 stsp n = fread(&base_id, sizeof(base_id), 1, infile);
556 efd2a263 2018-01-19 stsp if (n != 1)
557 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
559 efd2a263 2018-01-19 stsp size -= sizeof(base_id);
560 efd2a263 2018-01-19 stsp if (size <= 0)
561 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
563 efd2a263 2018-01-19 stsp err = got_object_open(&base_obj, repo, &base_id);
565 efd2a263 2018-01-19 stsp return err;
567 efd2a263 2018-01-19 stsp err = got_delta_apply(repo, infile, size, base_obj, outfile);
568 efd2a263 2018-01-19 stsp got_object_close(base_obj);
569 efd2a263 2018-01-19 stsp return err;
572 3ee5fc21 2018-01-17 stsp const struct got_error *
573 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
574 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
576 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
577 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
579 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
580 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
582 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
583 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
584 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
588 3ee5fc21 2018-01-17 stsp packfile = fopen(obj->path_packfile, "rb");
589 3ee5fc21 2018-01-17 stsp if (packfile == NULL) {
590 f334529e 2018-01-12 stsp err = got_error_from_errno();
594 3ee5fc21 2018-01-17 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
595 3ee5fc21 2018-01-17 stsp err = got_error_from_errno();
599 3ee5fc21 2018-01-17 stsp switch (obj->type) {
600 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
601 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
602 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
603 3ee5fc21 2018-01-17 stsp err = dump_plain_object(packfile, obj->type, obj->size, *f);
605 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_REF_DELTA:
606 efd2a263 2018-01-19 stsp err = dump_ref_delta_object(repo, packfile, obj->type,
607 efd2a263 2018-01-19 stsp obj->size, *f);
609 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_TAG:
610 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
612 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
616 3ee5fc21 2018-01-17 stsp if (packfile)
617 3ee5fc21 2018-01-17 stsp fclose(packfile);
618 3ee5fc21 2018-01-17 stsp if (err && *f)
619 3ee5fc21 2018-01-17 stsp fclose(*f);
620 a1fd68d8 2018-01-12 stsp return err;