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 static const struct got_error *
312 search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
313 struct got_repository *repo, struct got_object_id *id)
315 const struct got_error *err;
316 char *path_packdir;
317 DIR *packdir;
318 struct dirent *dent;
319 char *path_packidx;
321 path_packdir = got_repo_get_path_objects_pack(repo);
322 if (path_packdir == NULL)
323 return got_error(GOT_ERR_NO_MEM);
325 packdir = opendir(path_packdir);
326 if (packdir == NULL) {
327 err = got_error_from_errno();
328 goto done;
331 while ((dent = readdir(packdir)) != NULL) {
332 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
333 continue;
335 if (asprintf(&path_packidx, "%s/%s", path_packdir,
336 dent->d_name) == -1) {
337 err = got_error(GOT_ERR_NO_MEM);
338 goto done;
341 err = got_packidx_open(packidx, path_packidx);
342 free(path_packidx);
343 if (err)
344 goto done;
346 *idx = get_object_idx(*packidx, id);
347 if (*idx != -1) {
348 err = NULL; /* found the object */
349 goto done;
352 got_packidx_close(*packidx);
353 *packidx = NULL;
356 err = got_error(GOT_ERR_NO_OBJ);
357 done:
358 free(path_packdir);
359 if (closedir(packdir) != 0 && err == 0)
360 err = got_error_from_errno();
361 return err;
364 const struct got_error *
365 get_packfile_path(char **path_packfile, struct got_repository *repo,
366 struct got_packidx_v2_hdr *packidx)
368 char *path_packdir;
369 char hex[SHA1_DIGEST_STRING_LENGTH];
370 char *sha1str;
371 char *path_packidx;
373 *path_packfile = NULL;
375 path_packdir = got_repo_get_path_objects_pack(repo);
376 if (path_packdir == NULL)
377 return got_error(GOT_ERR_NO_MEM);
379 sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
380 hex, sizeof(hex));
381 if (sha1str == NULL)
382 return got_error(GOT_ERR_PACKIDX_CSUM);
384 if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
385 GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
386 *path_packfile = NULL;
387 return got_error(GOT_ERR_NO_MEM);
390 return NULL;
393 const struct got_error *
394 read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
396 const struct got_error *err = NULL;
397 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
398 struct got_packfile_hdr hdr;
399 size_t n;
401 n = fread(&hdr, sizeof(hdr), 1, f);
402 if (n != 1)
403 return got_ferror(f, GOT_ERR_BAD_PACKIDX);
405 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
406 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
407 betoh32(hdr.nobjects) != totobj)
408 err = got_error(GOT_ERR_BAD_PACKFILE);
410 return err;
413 static const struct got_error *
414 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
415 FILE *packfile)
417 uint8_t t = 0;
418 uint64_t s = 0;
419 uint8_t sizeN;
420 size_t n;
421 int i = 0;
423 do {
424 /* We do not support size values which don't fit in 64 bit. */
425 if (i > 9)
426 return got_error(GOT_ERR_NO_SPACE);
428 n = fread(&sizeN, sizeof(sizeN), 1, packfile);
429 if (n != 1)
430 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
432 if (i == 0) {
433 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
434 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
435 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
436 } else {
437 size_t shift = 4 + 7 * (i - 1);
438 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
440 i++;
441 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
443 *type = t;
444 *size = s;
445 *len = i * sizeof(sizeN);
446 return NULL;
449 static const struct got_error *
450 open_plain_object(struct got_object **obj, const char *path_packfile,
451 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
453 *obj = calloc(1, sizeof(**obj));
454 if (*obj == NULL)
455 return got_error(GOT_ERR_NO_MEM);
457 (*obj)->path_packfile = strdup(path_packfile);
458 if ((*obj)->path_packfile == NULL) {
459 free(*obj);
460 *obj = NULL;
461 return got_error(GOT_ERR_NO_MEM);
464 (*obj)->type = type;
465 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
466 (*obj)->hdrlen = 0;
467 (*obj)->size = size;
468 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
469 (*obj)->pack_offset = offset;
471 return NULL;
474 static const struct got_error *
475 parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
477 int64_t o = 0;
478 uint8_t offN;
479 size_t n;
480 int i = 0;
482 do {
483 /* We do not support offset values which don't fit in 64 bit. */
484 if (i > 8)
485 return got_error(GOT_ERR_NO_SPACE);
487 n = fread(&offN, sizeof(offN), 1, packfile);
488 if (n != 1)
489 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
491 if (i == 0)
492 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
493 else {
494 o++;
495 o <<= 7;
496 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
498 i++;
499 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
501 *offset = o;
502 *len = i * sizeof(offN);
503 return NULL;
506 static const struct got_error *
507 parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
509 const struct got_error *err;
510 int64_t negoffset;
511 size_t negofflen;
513 err = parse_negative_offset(&negoffset, &negofflen, packfile);
514 if (err)
515 return err;
517 /* Compute the base object's offset (must be in the same pack file). */
518 *base_offset = (offset - negoffset);
519 if (*base_offset <= 0)
520 return got_error(GOT_ERR_BAD_PACKFILE);
522 return NULL;
525 static const struct got_error *resolve_delta_chain(struct got_delta_chain *,
526 struct got_repository *repo, FILE *, const char *, int, off_t, size_t);
528 static const struct got_error *
529 resolve_offset_delta(struct got_delta_chain *deltas,
530 struct got_repository *repo, FILE *packfile, const char *path_packfile,
531 off_t delta_offset)
533 const struct got_error *err;
534 off_t base_offset;
535 uint8_t base_type;
536 uint64_t base_size;
537 size_t base_tslen;
539 err = parse_offset_delta(&base_offset, packfile, delta_offset);
540 if (err)
541 return err;
543 /* An offset delta must be in the same packfile. */
544 if (fseeko(packfile, base_offset, SEEK_SET) != 0)
545 return got_error_from_errno();
547 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
548 packfile);
549 if (err)
550 return err;
552 return resolve_delta_chain(deltas, repo, packfile, path_packfile,
553 base_type, base_offset + base_tslen, base_size);
556 static const struct got_error *
557 resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
558 FILE *packfile, const char *path_packfile, off_t delta_offset)
560 const struct got_error *err;
561 struct got_object_id id;
562 struct got_packidx_v2_hdr *packidx;
563 int idx;
564 off_t base_offset;
565 uint8_t base_type;
566 uint64_t base_size;
567 size_t base_tslen;
568 size_t n;
569 FILE *base_packfile;
570 char *path_base_packfile;
572 n = fread(&id, sizeof(id), 1, packfile);
573 if (n != 1)
574 return got_ferror(packfile, GOT_ERR_IO);
576 err = search_packidx(&packidx, &idx, repo, &id);
577 if (err)
578 return err;
580 base_offset = get_object_offset(packidx, idx);
581 if (base_offset == (uint64_t)-1) {
582 got_packidx_close(packidx);
583 return got_error(GOT_ERR_BAD_PACKIDX);
586 err = get_packfile_path(&path_base_packfile, repo, packidx);
587 got_packidx_close(packidx);
588 if (err)
589 return err;
591 base_packfile = fopen(path_base_packfile, "rb");
592 if (base_packfile == NULL) {
593 err = got_error_from_errno();
594 goto done;
597 if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
598 err = got_error_from_errno();
599 goto done;
602 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
603 base_packfile);
604 if (err)
605 goto done;
607 err = resolve_delta_chain(deltas, repo, base_packfile,
608 path_base_packfile, base_type, base_offset + base_tslen, base_size);
609 done:
610 free(path_base_packfile);
611 if (base_packfile && fclose(base_packfile) == -1 && err == 0)
612 err = got_error_from_errno();
613 return err;
616 static const struct got_error *
617 resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
618 FILE *packfile, const char *path_packfile, int delta_type,
619 off_t delta_offset, size_t delta_size)
621 const struct got_error *err = NULL;
622 struct got_delta *delta;
624 delta = got_delta_open(path_packfile, delta_type, delta_offset,
625 delta_size);
626 if (delta == NULL)
627 return got_error(GOT_ERR_NO_MEM);
628 deltas->nentries++;
629 SIMPLEQ_INSERT_TAIL(&deltas->entries, delta, entry);
630 /* In case of error below, delta is freed in got_object_close(). */
632 switch (delta_type) {
633 case GOT_OBJ_TYPE_COMMIT:
634 case GOT_OBJ_TYPE_TREE:
635 case GOT_OBJ_TYPE_BLOB:
636 case GOT_OBJ_TYPE_TAG:
637 /* Plain types are the final delta base. Recursion ends. */
638 break;
639 case GOT_OBJ_TYPE_OFFSET_DELTA:
640 err = resolve_offset_delta(deltas, repo, packfile,
641 path_packfile, delta_offset);
642 break;
643 case GOT_OBJ_TYPE_REF_DELTA:
644 err = resolve_ref_delta(deltas, repo, packfile, path_packfile,
645 delta_offset);
646 break;
647 default:
648 return got_error(GOT_ERR_NOT_IMPL);
651 return err;
654 static const struct got_error *
655 open_delta_object(struct got_object **obj, struct got_repository *repo,
656 struct got_packidx_v2_hdr *packidx, const char *path_packfile,
657 FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
658 int delta_type, size_t delta_size)
660 const struct got_error *err = NULL;
661 struct got_object_id base_id;
662 uint8_t base_type;
663 int resolved_type;
664 uint64_t base_size;
665 size_t base_tslen;
667 *obj = calloc(1, sizeof(**obj));
668 if (*obj == NULL)
669 return got_error(GOT_ERR_NO_MEM);
671 (*obj)->flags = 0;
672 (*obj)->hdrlen = 0;
673 (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
674 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
675 (*obj)->pack_offset = offset + tslen;
677 (*obj)->path_packfile = strdup(path_packfile);
678 if ((*obj)->path_packfile == NULL) {
679 err = got_error(GOT_ERR_NO_MEM);
680 goto done;
682 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
684 SIMPLEQ_INIT(&(*obj)->deltas.entries);
685 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
687 err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
688 path_packfile, delta_type, offset, delta_size);
689 if (err)
690 goto done;
692 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
693 if (err)
694 goto done;
695 (*obj)->type = resolved_type;
697 done:
698 if (err) {
699 got_object_close(*obj);
700 *obj = NULL;
702 return err;
705 static const struct got_error *
706 open_packed_object(struct got_object **obj, struct got_repository *repo,
707 struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
709 const struct got_error *err = NULL;
710 off_t offset;
711 char *path_packfile;
712 FILE *packfile;
713 uint8_t type;
714 uint64_t size;
715 size_t tslen;
717 *obj = NULL;
719 offset = get_object_offset(packidx, idx);
720 if (offset == (uint64_t)-1)
721 return got_error(GOT_ERR_BAD_PACKIDX);
723 err = get_packfile_path(&path_packfile, repo, packidx);
724 if (err)
725 return err;
727 packfile = fopen(path_packfile, "rb");
728 if (packfile == NULL) {
729 err = got_error_from_errno();
730 goto done;
733 err = read_packfile_hdr(packfile, packidx);
734 if (err)
735 goto done;
737 if (fseeko(packfile, offset, SEEK_SET) != 0) {
738 err = got_error_from_errno();
739 goto done;
742 err = parse_object_type_and_size(&type, &size, &tslen, packfile);
743 if (err)
744 goto done;
746 switch (type) {
747 case GOT_OBJ_TYPE_COMMIT:
748 case GOT_OBJ_TYPE_TREE:
749 case GOT_OBJ_TYPE_BLOB:
750 err = open_plain_object(obj, path_packfile, id, type,
751 offset + tslen, size);
752 break;
754 case GOT_OBJ_TYPE_OFFSET_DELTA:
755 case GOT_OBJ_TYPE_REF_DELTA:
756 err = open_delta_object(obj, repo, packidx, path_packfile,
757 packfile, id, offset, tslen, type, size);
758 break;
760 case GOT_OBJ_TYPE_TAG:
761 default:
762 err = got_error(GOT_ERR_NOT_IMPL);
763 goto done;
765 done:
766 free(path_packfile);
767 if (packfile && fclose(packfile) == -1 && err == 0)
768 err = got_error_from_errno();
769 return err;
772 const struct got_error *
773 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
774 struct got_repository *repo)
776 const struct got_error *err = NULL;
777 struct got_packidx_v2_hdr *packidx = NULL;
778 int idx;
780 err = search_packidx(&packidx, &idx, repo, id);
781 if (err)
782 return err;
784 err = open_packed_object(obj, repo, packidx, idx, id);
785 got_packidx_close(packidx);
786 return err;
789 static const struct got_error *
790 dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
792 size_t n;
794 while (size > 0) {
795 uint8_t data[2048];
796 size_t len = MIN(size, sizeof(data));
798 n = fread(data, len, 1, infile);
799 if (n != 1)
800 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
802 n = fwrite(data, len, 1, outfile);
803 if (n != 1)
804 return got_ferror(outfile, GOT_ERR_IO);
806 size -= len;
809 rewind(outfile);
810 return NULL;
813 static const struct got_error *
814 dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
815 size_t size, FILE *outfile)
817 const struct got_error *err = NULL;
818 struct got_object_id base_id;
819 struct got_object *base_obj;
820 size_t n;
822 if (size < sizeof(base_id))
823 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
825 n = fread(&base_id, sizeof(base_id), 1, infile);
826 if (n != 1)
827 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
829 size -= sizeof(base_id);
830 if (size <= 0)
831 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
833 err = got_object_open(&base_obj, repo, &base_id);
834 if (err)
835 return err;
837 err = got_delta_apply(repo, infile, size, base_obj, outfile);
838 got_object_close(base_obj);
839 return err;
842 const struct got_error *
843 got_packfile_extract_object(FILE **f, struct got_object *obj,
844 struct got_repository *repo)
846 const struct got_error *err = NULL;
847 FILE *packfile = NULL;
849 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
850 return got_error(GOT_ERR_OBJ_NOT_PACKED);
852 *f = got_opentemp();
853 if (*f == NULL) {
854 err = got_error(GOT_ERR_FILE_OPEN);
855 goto done;
858 packfile = fopen(obj->path_packfile, "rb");
859 if (packfile == NULL) {
860 err = got_error_from_errno();
861 goto done;
864 if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
865 err = got_error_from_errno();
866 goto done;
869 switch (obj->type) {
870 case GOT_OBJ_TYPE_COMMIT:
871 case GOT_OBJ_TYPE_TREE:
872 case GOT_OBJ_TYPE_BLOB:
873 err = dump_plain_object(packfile, obj->type, obj->size, *f);
874 break;
875 case GOT_OBJ_TYPE_REF_DELTA:
876 err = dump_ref_delta_object(repo, packfile, obj->type,
877 obj->size, *f);
878 break;
879 case GOT_OBJ_TYPE_TAG:
880 case GOT_OBJ_TYPE_OFFSET_DELTA:
881 default:
882 err = got_error(GOT_ERR_NOT_IMPL);
883 goto done;
885 done:
886 if (packfile)
887 fclose(packfile);
888 if (err && *f)
889 fclose(*f);
890 return err;