Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
21 #include <dirent.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <limits.h>
28 #include <sha1.h>
29 #include <endian.h>
30 #include <zlib.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
35 #include "got_sha1.h"
36 #include "pack.h"
37 #include "path.h"
38 #include "delta.h"
39 #include "object.h"
41 #define GOT_PACK_PREFIX "pack-"
42 #define GOT_PACKFILE_SUFFIX ".pack"
43 #define GOT_PACKIDX_SUFFIX ".idx"
44 #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
45 SHA1_DIGEST_STRING_LENGTH - 1 + \
46 strlen(GOT_PACKFILE_SUFFIX))
47 #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
48 SHA1_DIGEST_STRING_LENGTH - 1 + \
49 strlen(GOT_PACKIDX_SUFFIX))
51 #ifndef MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 static const struct got_error *
56 verify_fanout_table(uint32_t *fanout_table)
57 {
58 int i;
60 for (i = 0; i < 0xff - 1; i++) {
61 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
62 return got_error(GOT_ERR_BAD_PACKIDX);
63 }
65 return NULL;
66 }
68 static const struct got_error *
69 get_packfile_size(size_t *size, const char *path_idx)
70 {
71 struct stat sb;
72 char *path_pack;
73 char base_path[PATH_MAX];
74 char *dot;
76 if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
77 return got_error(GOT_ERR_NO_SPACE);
79 dot = strrchr(base_path, '.');
80 if (dot == NULL)
81 return got_error(GOT_ERR_BAD_PATH);
82 *dot = '\0';
83 if (asprintf(&path_pack, "%s.pack", base_path) == -1)
84 return got_error(GOT_ERR_NO_MEM);
86 if (stat(path_pack, &sb) != 0) {
87 free(path_pack);
88 return got_error_from_errno();
89 }
91 free(path_pack);
92 *size = sb.st_size;
93 return 0;
94 }
96 const struct got_error *
97 got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
98 {
99 struct got_packidx_v2_hdr *p;
100 FILE *f;
101 const struct got_error *err = NULL;
102 size_t n, nobj, packfile_size;
103 SHA1_CTX ctx;
104 uint8_t sha1[SHA1_DIGEST_LENGTH];
106 SHA1Init(&ctx);
108 f = fopen(path, "rb");
109 if (f == NULL)
110 return got_error(GOT_ERR_BAD_PATH);
112 err = get_packfile_size(&packfile_size, path);
113 if (err)
114 return err;
116 p = calloc(1, sizeof(*p));
117 if (p == NULL) {
118 err = got_error(GOT_ERR_NO_MEM);
119 goto done;
122 n = fread(&p->magic, sizeof(p->magic), 1, f);
123 if (n != 1) {
124 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
125 goto done;
128 if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
129 err = got_error(GOT_ERR_BAD_PACKIDX);
130 goto done;
133 SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
135 n = fread(&p->version, sizeof(p->version), 1, f);
136 if (n != 1) {
137 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
138 goto done;
141 if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
142 err = got_error(GOT_ERR_BAD_PACKIDX);
143 goto done;
146 SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
148 n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
149 if (n != 1) {
150 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
151 goto done;
154 err = verify_fanout_table(p->fanout_table);
155 if (err)
156 goto done;
158 SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
160 nobj = betoh32(p->fanout_table[0xff]);
162 p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
163 if (p->sorted_ids == NULL) {
164 err = got_error(GOT_ERR_NO_MEM);
165 goto done;
168 n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
169 if (n != nobj) {
170 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
171 goto done;
174 SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
175 nobj * sizeof(*p->sorted_ids));
177 p->crc32 = calloc(nobj, sizeof(*p->crc32));
178 if (p->crc32 == NULL) {
179 err = got_error(GOT_ERR_NO_MEM);
180 goto done;
183 n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
184 if (n != nobj) {
185 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
186 goto done;
189 SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
191 p->offsets = calloc(nobj, sizeof(*p->offsets));
192 if (p->offsets == NULL) {
193 err = got_error(GOT_ERR_NO_MEM);
194 goto done;
197 n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
198 if (n != nobj) {
199 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
200 goto done;
203 SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
205 /* Large file offsets are contained only in files > 2GB. */
206 if (packfile_size <= 0x80000000)
207 goto checksum;
209 p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
210 if (p->large_offsets == NULL) {
211 err = got_error(GOT_ERR_NO_MEM);
212 goto done;
215 n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
216 if (n != nobj) {
217 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
218 goto done;
221 SHA1Update(&ctx, (uint8_t*)p->large_offsets,
222 nobj * sizeof(*p->large_offsets));
224 checksum:
225 n = fread(&p->trailer, sizeof(p->trailer), 1, f);
226 if (n != 1) {
227 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
228 goto done;
231 SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
232 SHA1Final(sha1, &ctx);
233 if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
234 err = got_error(GOT_ERR_PACKIDX_CSUM);
235 done:
236 fclose(f);
237 if (err)
238 got_packidx_close(p);
239 else
240 *packidx = p;
241 return err;
244 void
245 got_packidx_close(struct got_packidx_v2_hdr *packidx)
247 free(packidx->sorted_ids);
248 free(packidx->offsets);
249 free(packidx->crc32);
250 free(packidx->large_offsets);
251 free(packidx);
254 static int
255 is_packidx_filename(const char *name, size_t len)
257 if (len != GOT_PACKIDX_NAMELEN)
258 return 0;
260 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
261 return 0;
263 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
264 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
265 return 0;
267 return 1;
270 static off_t
271 get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
273 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
274 uint32_t offset = betoh32(packidx->offsets[idx]);
275 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
276 uint64_t loffset;
277 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
278 if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
279 return -1;
280 loffset = betoh64(packidx->large_offsets[idx]);
281 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
283 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
286 static int
287 get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
289 u_int8_t id0 = id->sha1[0];
290 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
291 int i = 0;
293 if (id0 > 0)
294 i = betoh32(packidx->fanout_table[id0 - 1]);
296 while (i < totobj) {
297 struct got_object_id *oid = &packidx->sorted_ids[i];
298 uint32_t offset;
299 int cmp = got_object_id_cmp(id, oid);
301 if (cmp == 0)
302 return i;
303 else if (cmp > 0)
304 break;
305 i++;
308 return -1;
311 const struct got_error *
312 read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
314 const struct got_error *err = NULL;
315 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
316 struct got_packfile_hdr hdr;
317 size_t n;
319 n = fread(&hdr, sizeof(hdr), 1, f);
320 if (n != 1)
321 return got_ferror(f, GOT_ERR_BAD_PACKIDX);
323 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
324 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
325 betoh32(hdr.nobjects) != totobj)
326 err = got_error(GOT_ERR_BAD_PACKFILE);
328 return err;
331 static const struct got_error *
332 decode_type_and_size(uint8_t *type, uint64_t *size, size_t *len, FILE *packfile)
334 uint8_t t = 0;
335 uint64_t s = 0;
336 uint8_t sizeN;
337 size_t n;
338 int i = 0;
340 do {
341 /* We do not support size values which don't fit in 64 bit. */
342 if (i > 9)
343 return got_error(GOT_ERR_NO_SPACE);
345 n = fread(&sizeN, sizeof(sizeN), 1, packfile);
346 if (n != 1)
347 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
349 if (i == 0) {
350 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
351 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
352 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
353 } else {
354 size_t shift = 4 + 7 * (i - 1);
355 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
357 i++;
358 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
360 *type = t;
361 *size = s;
362 *len = i * sizeof(sizeN);
363 return NULL;
366 static const struct got_error *
367 open_plain_object(struct got_object **obj, const char *path_packfile,
368 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
370 *obj = calloc(1, sizeof(**obj));
371 if (*obj == NULL)
372 return got_error(GOT_ERR_NO_MEM);
374 (*obj)->path_packfile = strdup(path_packfile);
375 if ((*obj)->path_packfile == NULL) {
376 free(*obj);
377 *obj = NULL;
378 return got_error(GOT_ERR_NO_MEM);
381 (*obj)->type = type;
382 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
383 (*obj)->hdrlen = 0;
384 (*obj)->size = size;
385 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
386 (*obj)->pack_offset = offset;
388 return NULL;
391 static const struct got_error *
392 decode_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
394 int64_t o = 0;
395 uint8_t offN;
396 size_t n;
397 int i = 0;
399 do {
400 /* We do not support offset values which don't fit in 64 bit. */
401 if (i > 8)
402 return got_error(GOT_ERR_NO_SPACE);
404 n = fread(&offN, sizeof(offN), 1, packfile);
405 if (n != 1)
406 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
408 if (i == 0)
409 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
410 else {
411 o++;
412 o <<= 7;
413 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
415 i++;
416 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
418 *offset = o;
419 *len = i * sizeof(offN);
420 return NULL;
423 static const struct got_error *
424 parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
426 const struct got_error *err;
427 int64_t negoffset;
428 size_t negofflen;
430 err = decode_negative_offset(&negoffset, &negofflen, packfile);
431 if (err)
432 return err;
434 /* Compute the base object's offset (must be in the same pack file). */
435 *base_offset = (offset - negoffset);
436 if (*base_offset <= 0)
437 return got_error(GOT_ERR_BAD_PACKFILE);
439 return NULL;
442 static const struct got_error *resolve_delta_chain(struct got_delta_chain *,
443 FILE *, const char *, off_t, size_t);
445 static const struct got_error *
446 resolve_offset_delta(struct got_delta_chain *deltas, FILE *packfile,
447 const char *path_packfile, off_t offset, size_t delta_size)
449 const struct got_error *err;
450 off_t next_offset;
452 err = parse_offset_delta(&next_offset, packfile, offset);
453 if (err)
454 return err;
456 /* Next offset must be in the same packfile. */
457 return resolve_delta_chain(deltas, packfile, path_packfile,
458 next_offset, delta_size);
461 static const struct got_error *
462 resolve_delta_chain(struct got_delta_chain *deltas, FILE *packfile,
463 const char *path_packfile, off_t offset, size_t delta_size)
465 const struct got_error *err = NULL;
466 uint8_t base_type;
467 uint64_t base_size;
468 size_t base_tslen;
469 struct got_delta_base *base;
471 if (fseeko(packfile, offset, SEEK_SET) != 0)
472 return got_error_from_errno();
474 err = decode_type_and_size(&base_type, &base_size, &base_tslen,
475 packfile);
476 if (err)
477 return err;
479 base = got_delta_base_open(path_packfile, base_type, offset,
480 delta_size);
481 if (base == NULL)
482 return got_error(GOT_ERR_NO_MEM);
483 deltas->nentries++;
484 SIMPLEQ_INSERT_TAIL(&deltas->entries, base, entry);
485 /* In case of error below, base will be freed in got_object_close(). */
487 switch (base_type) {
488 case GOT_OBJ_TYPE_COMMIT:
489 case GOT_OBJ_TYPE_TREE:
490 case GOT_OBJ_TYPE_BLOB:
491 case GOT_OBJ_TYPE_TAG:
492 break;
493 case GOT_OBJ_TYPE_OFFSET_DELTA:
494 err = resolve_offset_delta(deltas, packfile, path_packfile,
495 offset, base_size);
496 break;
497 case GOT_OBJ_TYPE_REF_DELTA:
498 default:
499 return got_error(GOT_ERR_NOT_IMPL);
502 return err;
505 static const struct got_error *
506 open_offset_delta_object(struct got_object **obj,
507 struct got_repository *repo, struct got_packidx_v2_hdr *packidx,
508 const char *path_packfile, FILE *packfile, struct got_object_id *id,
509 off_t offset, size_t tslen, size_t size)
511 const struct got_error *err = NULL;
512 off_t base_offset;
513 struct got_object_id base_id;
514 uint8_t base_type;
515 int resolved_type;
516 uint64_t base_size;
517 size_t base_tslen;
519 err = parse_offset_delta(&base_offset, packfile, offset);
520 if (err)
521 return err;
523 *obj = calloc(1, sizeof(**obj));
524 if (*obj == NULL)
525 return got_error(GOT_ERR_NO_MEM);
527 (*obj)->flags = 0;
528 (*obj)->hdrlen = 0;
529 (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
530 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
531 (*obj)->pack_offset = offset + tslen;
533 (*obj)->path_packfile = strdup(path_packfile);
534 if ((*obj)->path_packfile == NULL) {
535 err = got_error(GOT_ERR_NO_MEM);
536 goto done;
538 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
540 SIMPLEQ_INIT(&(*obj)->deltas.entries);
541 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
542 err = resolve_delta_chain(&(*obj)->deltas, packfile, path_packfile,
543 base_offset, size);
544 if (err)
545 goto done;
547 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
548 if (err)
549 goto done;
550 (*obj)->type = resolved_type;
552 done:
553 if (err) {
554 got_object_close(*obj);
555 *obj = NULL;
557 return err;
560 static const struct got_error *
561 open_packed_object(struct got_object **obj, struct got_repository *repo,
562 const char *path_packdir, struct got_packidx_v2_hdr *packidx,
563 struct got_object_id *id)
565 const struct got_error *err = NULL;
566 int idx = get_object_idx(packidx, id);
567 off_t offset;
568 char hex[SHA1_DIGEST_STRING_LENGTH];
569 char *sha1str;
570 char *path_packfile;
571 FILE *packfile;
572 uint8_t type;
573 uint64_t size;
574 size_t tslen;
576 *obj = NULL;
577 if (idx == -1) /* object not found in pack index */
578 return NULL;
580 offset = get_object_offset(packidx, idx);
581 if (offset == (uint64_t)-1)
582 return got_error(GOT_ERR_BAD_PACKIDX);
584 sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
585 hex, sizeof(hex));
586 if (sha1str == NULL)
587 return got_error(GOT_ERR_PACKIDX_CSUM);
589 if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
590 GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
591 return got_error(GOT_ERR_NO_MEM);
593 packfile = fopen(path_packfile, "rb");
594 if (packfile == NULL) {
595 err = got_error_from_errno();
596 goto done;
599 err = read_packfile_hdr(packfile, packidx);
600 if (err)
601 goto done;
603 if (fseeko(packfile, offset, SEEK_SET) != 0) {
604 err = got_error_from_errno();
605 goto done;
608 err = decode_type_and_size(&type, &size, &tslen, packfile);
609 if (err)
610 goto done;
612 switch (type) {
613 case GOT_OBJ_TYPE_COMMIT:
614 case GOT_OBJ_TYPE_TREE:
615 case GOT_OBJ_TYPE_BLOB:
616 err = open_plain_object(obj, path_packfile, id, type,
617 offset + tslen, size);
618 break;
620 case GOT_OBJ_TYPE_OFFSET_DELTA:
621 err = open_offset_delta_object(obj, repo, packidx,
622 path_packfile, packfile, id, offset, tslen, size);
623 break;
625 case GOT_OBJ_TYPE_REF_DELTA:
626 case GOT_OBJ_TYPE_TAG:
627 default:
628 err = got_error(GOT_ERR_NOT_IMPL);
629 goto done;
631 done:
632 free(path_packfile);
633 if (packfile && fclose(packfile) == -1 && err == 0)
634 err = got_error_from_errno();
635 return err;
638 const struct got_error *
639 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
640 struct got_repository *repo)
642 const struct got_error *err = NULL;
643 DIR *packdir = NULL;
644 struct dirent *dent;
645 char *path_packdir = got_repo_get_path_objects_pack(repo);
647 if (path_packdir == NULL) {
648 err = got_error(GOT_ERR_NO_MEM);
649 goto done;
652 packdir = opendir(path_packdir);
653 if (packdir == NULL) {
654 err = got_error_from_errno();
655 goto done;
658 while ((dent = readdir(packdir)) != NULL) {
659 struct got_packidx_v2_hdr *packidx;
660 char *path_packidx, *path_object;
662 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
663 continue;
665 if (asprintf(&path_packidx, "%s/%s", path_packdir,
666 dent->d_name) == -1) {
667 err = got_error(GOT_ERR_NO_MEM);
668 goto done;
671 err = got_packidx_open(&packidx, path_packidx);
672 free(path_packidx);
673 if (err)
674 goto done;
676 err = open_packed_object(obj, repo, path_packdir, packidx, id);
677 got_packidx_close(packidx);
678 if (err)
679 goto done;
680 if (*obj != NULL)
681 break;
684 done:
685 free(path_packdir);
686 if (packdir && closedir(packdir) != 0 && err == 0)
687 err = got_error_from_errno();
688 return err;
691 static const struct got_error *
692 dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
694 size_t n;
696 while (size > 0) {
697 uint8_t data[2048];
698 size_t len = MIN(size, sizeof(data));
700 n = fread(data, len, 1, infile);
701 if (n != 1)
702 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
704 n = fwrite(data, len, 1, outfile);
705 if (n != 1)
706 return got_ferror(outfile, GOT_ERR_IO);
708 size -= len;
711 rewind(outfile);
712 return NULL;
715 static const struct got_error *
716 dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
717 size_t size, FILE *outfile)
719 const struct got_error *err = NULL;
720 struct got_object_id base_id;
721 struct got_object *base_obj;
722 size_t n;
724 if (size < sizeof(base_id))
725 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
727 n = fread(&base_id, sizeof(base_id), 1, infile);
728 if (n != 1)
729 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
731 size -= sizeof(base_id);
732 if (size <= 0)
733 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
735 err = got_object_open(&base_obj, repo, &base_id);
736 if (err)
737 return err;
739 err = got_delta_apply(repo, infile, size, base_obj, outfile);
740 got_object_close(base_obj);
741 return err;
744 const struct got_error *
745 got_packfile_extract_object(FILE **f, struct got_object *obj,
746 struct got_repository *repo)
748 const struct got_error *err = NULL;
749 FILE *packfile = NULL;
751 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
752 return got_error(GOT_ERR_OBJ_NOT_PACKED);
754 *f = got_opentemp();
755 if (*f == NULL) {
756 err = got_error(GOT_ERR_FILE_OPEN);
757 goto done;
760 packfile = fopen(obj->path_packfile, "rb");
761 if (packfile == NULL) {
762 err = got_error_from_errno();
763 goto done;
766 if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
767 err = got_error_from_errno();
768 goto done;
771 switch (obj->type) {
772 case GOT_OBJ_TYPE_COMMIT:
773 case GOT_OBJ_TYPE_TREE:
774 case GOT_OBJ_TYPE_BLOB:
775 err = dump_plain_object(packfile, obj->type, obj->size, *f);
776 break;
777 case GOT_OBJ_TYPE_REF_DELTA:
778 err = dump_ref_delta_object(repo, packfile, obj->type,
779 obj->size, *f);
780 break;
781 case GOT_OBJ_TYPE_TAG:
782 case GOT_OBJ_TYPE_OFFSET_DELTA:
783 default:
784 err = got_error(GOT_ERR_NOT_IMPL);
785 goto done;
787 done:
788 if (packfile)
789 fclose(packfile);
790 if (err && *f)
791 fclose(*f);
792 return err;