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_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
333 FILE *packfile)
335 uint8_t t = 0;
336 uint64_t s = 0;
337 uint8_t sizeN;
338 size_t n;
339 int i = 0;
341 do {
342 /* We do not support size values which don't fit in 64 bit. */
343 if (i > 9)
344 return got_error(GOT_ERR_NO_SPACE);
346 n = fread(&sizeN, sizeof(sizeN), 1, packfile);
347 if (n != 1)
348 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
350 if (i == 0) {
351 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
352 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
353 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
354 } else {
355 size_t shift = 4 + 7 * (i - 1);
356 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
358 i++;
359 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
361 *type = t;
362 *size = s;
363 *len = i * sizeof(sizeN);
364 return NULL;
367 static const struct got_error *
368 open_plain_object(struct got_object **obj, const char *path_packfile,
369 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
371 *obj = calloc(1, sizeof(**obj));
372 if (*obj == NULL)
373 return got_error(GOT_ERR_NO_MEM);
375 (*obj)->path_packfile = strdup(path_packfile);
376 if ((*obj)->path_packfile == NULL) {
377 free(*obj);
378 *obj = NULL;
379 return got_error(GOT_ERR_NO_MEM);
382 (*obj)->type = type;
383 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
384 (*obj)->hdrlen = 0;
385 (*obj)->size = size;
386 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
387 (*obj)->pack_offset = offset;
389 return NULL;
392 static const struct got_error *
393 decode_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
395 int64_t o = 0;
396 uint8_t offN;
397 size_t n;
398 int i = 0;
400 do {
401 /* We do not support offset values which don't fit in 64 bit. */
402 if (i > 8)
403 return got_error(GOT_ERR_NO_SPACE);
405 n = fread(&offN, sizeof(offN), 1, packfile);
406 if (n != 1)
407 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
409 if (i == 0)
410 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
411 else {
412 o++;
413 o <<= 7;
414 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
416 i++;
417 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
419 *offset = o;
420 *len = i * sizeof(offN);
421 return NULL;
424 static const struct got_error *
425 parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
427 const struct got_error *err;
428 int64_t negoffset;
429 size_t negofflen;
431 err = decode_negative_offset(&negoffset, &negofflen, packfile);
432 if (err)
433 return err;
435 /* Compute the base object's offset (must be in the same pack file). */
436 *base_offset = (offset - negoffset);
437 if (*base_offset <= 0)
438 return got_error(GOT_ERR_BAD_PACKFILE);
440 return NULL;
443 static const struct got_error *resolve_delta_chain(struct got_delta_chain *,
444 FILE *, const char *, int, off_t, size_t);
446 static const struct got_error *
447 resolve_offset_delta(struct got_delta_chain *deltas, FILE *packfile,
448 const char *path_packfile, off_t delta_offset)
450 const struct got_error *err;
451 off_t base_offset;
452 uint8_t base_type;
453 uint64_t base_size;
454 size_t base_tslen;
456 err = parse_offset_delta(&base_offset, packfile, delta_offset);
457 if (err)
458 return err;
460 /* An offset delta must be in the same packfile. */
461 if (fseeko(packfile, base_offset, SEEK_SET) != 0)
462 return got_error_from_errno();
464 err = decode_object_type_and_size(&base_type, &base_size, &base_tslen,
465 packfile);
466 if (err)
467 return err;
469 return resolve_delta_chain(deltas, packfile, path_packfile,
470 base_type, base_offset + base_tslen, base_size);
473 static const struct got_error *
474 resolve_delta_chain(struct got_delta_chain *deltas, FILE *packfile,
475 const char *path_packfile, int delta_type, off_t delta_offset,
476 size_t delta_size)
478 const struct got_error *err = NULL;
479 struct got_delta *delta;
481 delta = got_delta_open(path_packfile, delta_type, delta_offset,
482 delta_size);
483 if (delta == NULL)
484 return got_error(GOT_ERR_NO_MEM);
485 deltas->nentries++;
486 SIMPLEQ_INSERT_TAIL(&deltas->entries, delta, entry);
487 /* In case of error below, delta is freed in got_object_close(). */
489 switch (delta_type) {
490 case GOT_OBJ_TYPE_COMMIT:
491 case GOT_OBJ_TYPE_TREE:
492 case GOT_OBJ_TYPE_BLOB:
493 case GOT_OBJ_TYPE_TAG:
494 /* Plain types are the final delta base. Recursion ends. */
495 break;
496 case GOT_OBJ_TYPE_OFFSET_DELTA:
497 err = resolve_offset_delta(deltas, packfile, path_packfile,
498 delta_offset);
499 break;
500 case GOT_OBJ_TYPE_REF_DELTA:
501 default:
502 return got_error(GOT_ERR_NOT_IMPL);
505 return err;
508 static const struct got_error *
509 open_offset_delta_object(struct got_object **obj,
510 struct got_repository *repo, struct got_packidx_v2_hdr *packidx,
511 const char *path_packfile, FILE *packfile, struct got_object_id *id,
512 off_t offset, size_t tslen, size_t delta_size)
514 const struct got_error *err = NULL;
515 struct got_object_id base_id;
516 uint8_t base_type;
517 int resolved_type;
518 uint64_t base_size;
519 size_t base_tslen;
521 *obj = calloc(1, sizeof(**obj));
522 if (*obj == NULL)
523 return got_error(GOT_ERR_NO_MEM);
525 (*obj)->flags = 0;
526 (*obj)->hdrlen = 0;
527 (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
528 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
529 (*obj)->pack_offset = offset + tslen;
531 (*obj)->path_packfile = strdup(path_packfile);
532 if ((*obj)->path_packfile == NULL) {
533 err = got_error(GOT_ERR_NO_MEM);
534 goto done;
536 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
538 SIMPLEQ_INIT(&(*obj)->deltas.entries);
539 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
541 err = resolve_delta_chain(&(*obj)->deltas, packfile, path_packfile,
542 GOT_OBJ_TYPE_OFFSET_DELTA, offset, delta_size);
543 if (err)
544 goto done;
546 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
547 if (err)
548 goto done;
549 (*obj)->type = resolved_type;
551 done:
552 if (err) {
553 got_object_close(*obj);
554 *obj = NULL;
556 return err;
559 static const struct got_error *
560 open_packed_object(struct got_object **obj, struct got_repository *repo,
561 const char *path_packdir, struct got_packidx_v2_hdr *packidx,
562 struct got_object_id *id)
564 const struct got_error *err = NULL;
565 int idx = get_object_idx(packidx, id);
566 off_t offset;
567 char hex[SHA1_DIGEST_STRING_LENGTH];
568 char *sha1str;
569 char *path_packfile;
570 FILE *packfile;
571 uint8_t type;
572 uint64_t size;
573 size_t tslen;
575 *obj = NULL;
576 if (idx == -1) /* object not found in pack index */
577 return NULL;
579 offset = get_object_offset(packidx, idx);
580 if (offset == (uint64_t)-1)
581 return got_error(GOT_ERR_BAD_PACKIDX);
583 sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
584 hex, sizeof(hex));
585 if (sha1str == NULL)
586 return got_error(GOT_ERR_PACKIDX_CSUM);
588 if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
589 GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
590 return got_error(GOT_ERR_NO_MEM);
592 packfile = fopen(path_packfile, "rb");
593 if (packfile == NULL) {
594 err = got_error_from_errno();
595 goto done;
598 err = read_packfile_hdr(packfile, packidx);
599 if (err)
600 goto done;
602 if (fseeko(packfile, offset, SEEK_SET) != 0) {
603 err = got_error_from_errno();
604 goto done;
607 err = decode_object_type_and_size(&type, &size, &tslen, packfile);
608 if (err)
609 goto done;
611 switch (type) {
612 case GOT_OBJ_TYPE_COMMIT:
613 case GOT_OBJ_TYPE_TREE:
614 case GOT_OBJ_TYPE_BLOB:
615 err = open_plain_object(obj, path_packfile, id, type,
616 offset + tslen, size);
617 break;
619 case GOT_OBJ_TYPE_OFFSET_DELTA:
620 err = open_offset_delta_object(obj, repo, packidx,
621 path_packfile, packfile, id, offset, tslen, size);
622 break;
624 case GOT_OBJ_TYPE_REF_DELTA:
625 case GOT_OBJ_TYPE_TAG:
626 default:
627 err = got_error(GOT_ERR_NOT_IMPL);
628 goto done;
630 done:
631 free(path_packfile);
632 if (packfile && fclose(packfile) == -1 && err == 0)
633 err = got_error_from_errno();
634 return err;
637 const struct got_error *
638 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
639 struct got_repository *repo)
641 const struct got_error *err = NULL;
642 DIR *packdir = NULL;
643 struct dirent *dent;
644 char *path_packdir = got_repo_get_path_objects_pack(repo);
646 if (path_packdir == NULL) {
647 err = got_error(GOT_ERR_NO_MEM);
648 goto done;
651 packdir = opendir(path_packdir);
652 if (packdir == NULL) {
653 err = got_error_from_errno();
654 goto done;
657 while ((dent = readdir(packdir)) != NULL) {
658 struct got_packidx_v2_hdr *packidx;
659 char *path_packidx, *path_object;
661 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
662 continue;
664 if (asprintf(&path_packidx, "%s/%s", path_packdir,
665 dent->d_name) == -1) {
666 err = got_error(GOT_ERR_NO_MEM);
667 goto done;
670 err = got_packidx_open(&packidx, path_packidx);
671 free(path_packidx);
672 if (err)
673 goto done;
675 err = open_packed_object(obj, repo, path_packdir, packidx, id);
676 got_packidx_close(packidx);
677 if (err)
678 goto done;
679 if (*obj != NULL)
680 break;
683 done:
684 free(path_packdir);
685 if (packdir && closedir(packdir) != 0 && err == 0)
686 err = got_error_from_errno();
687 return err;
690 static const struct got_error *
691 dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
693 size_t n;
695 while (size > 0) {
696 uint8_t data[2048];
697 size_t len = MIN(size, sizeof(data));
699 n = fread(data, len, 1, infile);
700 if (n != 1)
701 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
703 n = fwrite(data, len, 1, outfile);
704 if (n != 1)
705 return got_ferror(outfile, GOT_ERR_IO);
707 size -= len;
710 rewind(outfile);
711 return NULL;
714 static const struct got_error *
715 dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
716 size_t size, FILE *outfile)
718 const struct got_error *err = NULL;
719 struct got_object_id base_id;
720 struct got_object *base_obj;
721 size_t n;
723 if (size < sizeof(base_id))
724 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
726 n = fread(&base_id, sizeof(base_id), 1, infile);
727 if (n != 1)
728 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
730 size -= sizeof(base_id);
731 if (size <= 0)
732 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
734 err = got_object_open(&base_obj, repo, &base_id);
735 if (err)
736 return err;
738 err = got_delta_apply(repo, infile, size, base_obj, outfile);
739 got_object_close(base_obj);
740 return err;
743 const struct got_error *
744 got_packfile_extract_object(FILE **f, struct got_object *obj,
745 struct got_repository *repo)
747 const struct got_error *err = NULL;
748 FILE *packfile = NULL;
750 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
751 return got_error(GOT_ERR_OBJ_NOT_PACKED);
753 *f = got_opentemp();
754 if (*f == NULL) {
755 err = got_error(GOT_ERR_FILE_OPEN);
756 goto done;
759 packfile = fopen(obj->path_packfile, "rb");
760 if (packfile == NULL) {
761 err = got_error_from_errno();
762 goto done;
765 if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
766 err = got_error_from_errno();
767 goto done;
770 switch (obj->type) {
771 case GOT_OBJ_TYPE_COMMIT:
772 case GOT_OBJ_TYPE_TREE:
773 case GOT_OBJ_TYPE_BLOB:
774 err = dump_plain_object(packfile, obj->type, obj->size, *f);
775 break;
776 case GOT_OBJ_TYPE_REF_DELTA:
777 err = dump_ref_delta_object(repo, packfile, obj->type,
778 obj->size, *f);
779 break;
780 case GOT_OBJ_TYPE_TAG:
781 case GOT_OBJ_TYPE_OFFSET_DELTA:
782 default:
783 err = got_error(GOT_ERR_NOT_IMPL);
784 goto done;
786 done:
787 if (packfile)
788 fclose(packfile);
789 if (err && *f)
790 fclose(*f);
791 return err;