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 (packdir && closedir(packdir) != 0 && err == 0)
360 err = got_error_from_errno();
361 return err;
364 static 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 open_packfile(FILE **packfile, char **path_packfile,
415 struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
417 const struct got_error *err;
419 *packfile = NULL;
421 err = get_packfile_path(path_packfile, repo, packidx);
422 if (err)
423 return err;
425 *packfile = fopen(*path_packfile, "rb");
426 if (*packfile == NULL) {
427 err = got_error_from_errno();
428 free(*path_packfile);
429 return err;
432 err = read_packfile_hdr(*packfile, packidx);
433 if (err) {
434 fclose(*packfile);
435 *packfile = NULL;
437 return err;
440 static const struct got_error *
441 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
442 FILE *packfile)
444 uint8_t t = 0;
445 uint64_t s = 0;
446 uint8_t sizeN;
447 size_t n;
448 int i = 0;
450 do {
451 /* We do not support size values which don't fit in 64 bit. */
452 if (i > 9)
453 return got_error(GOT_ERR_NO_SPACE);
455 n = fread(&sizeN, sizeof(sizeN), 1, packfile);
456 if (n != 1)
457 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
459 if (i == 0) {
460 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
461 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
462 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
463 } else {
464 size_t shift = 4 + 7 * (i - 1);
465 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
467 i++;
468 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
470 *type = t;
471 *size = s;
472 *len = i * sizeof(sizeN);
473 return NULL;
476 static const struct got_error *
477 open_plain_object(struct got_object **obj, const char *path_packfile,
478 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
480 *obj = calloc(1, sizeof(**obj));
481 if (*obj == NULL)
482 return got_error(GOT_ERR_NO_MEM);
484 (*obj)->path_packfile = strdup(path_packfile);
485 if ((*obj)->path_packfile == NULL) {
486 free(*obj);
487 *obj = NULL;
488 return got_error(GOT_ERR_NO_MEM);
491 (*obj)->type = type;
492 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
493 (*obj)->hdrlen = 0;
494 (*obj)->size = size;
495 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
496 (*obj)->pack_offset = offset;
498 return NULL;
501 static const struct got_error *
502 parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
504 int64_t o = 0;
505 uint8_t offN;
506 size_t n;
507 int i = 0;
509 do {
510 /* We do not support offset values which don't fit in 64 bit. */
511 if (i > 8)
512 return got_error(GOT_ERR_NO_SPACE);
514 n = fread(&offN, sizeof(offN), 1, packfile);
515 if (n != 1)
516 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
518 if (i == 0)
519 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
520 else {
521 o++;
522 o <<= 7;
523 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
525 i++;
526 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
528 *offset = o;
529 *len = i * sizeof(offN);
530 return NULL;
533 static const struct got_error *
534 parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
536 const struct got_error *err;
537 int64_t negoffset;
538 size_t negofflen;
540 err = parse_negative_offset(&negoffset, &negofflen, packfile);
541 if (err)
542 return err;
544 /* Compute the base object's offset (must be in the same pack file). */
545 *base_offset = (offset - negoffset);
546 if (*base_offset <= 0)
547 return got_error(GOT_ERR_BAD_PACKFILE);
549 return NULL;
552 static const struct got_error *resolve_delta_chain(struct got_delta_chain *,
553 struct got_repository *repo, FILE *, const char *, int, off_t, size_t);
555 static const struct got_error *
556 resolve_offset_delta(struct got_delta_chain *deltas,
557 struct got_repository *repo, FILE *packfile, const char *path_packfile,
558 off_t delta_offset)
560 const struct got_error *err;
561 off_t base_offset;
562 uint8_t base_type;
563 uint64_t base_size;
564 size_t base_tslen;
566 err = parse_offset_delta(&base_offset, packfile, delta_offset);
567 if (err)
568 return err;
570 /* An offset delta must be in the same packfile. */
571 if (fseeko(packfile, base_offset, SEEK_SET) != 0)
572 return got_error_from_errno();
574 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
575 packfile);
576 if (err)
577 return err;
579 return resolve_delta_chain(deltas, repo, packfile, path_packfile,
580 base_type, base_offset + base_tslen, base_size);
583 static const struct got_error *
584 resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
585 FILE *packfile, const char *path_packfile, off_t delta_offset)
587 const struct got_error *err;
588 struct got_object_id id;
589 struct got_packidx_v2_hdr *packidx;
590 int idx;
591 off_t base_offset;
592 uint8_t base_type;
593 uint64_t base_size;
594 size_t base_tslen;
595 size_t n;
596 FILE *base_packfile;
597 char *path_base_packfile;
599 n = fread(&id, sizeof(id), 1, packfile);
600 if (n != 1)
601 return got_ferror(packfile, GOT_ERR_IO);
603 err = search_packidx(&packidx, &idx, repo, &id);
604 if (err)
605 return err;
607 base_offset = get_object_offset(packidx, idx);
608 if (base_offset == (uint64_t)-1) {
609 got_packidx_close(packidx);
610 return got_error(GOT_ERR_BAD_PACKIDX);
613 err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
614 got_packidx_close(packidx);
615 if (err)
616 return err;
618 if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
619 err = got_error_from_errno();
620 goto done;
623 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
624 base_packfile);
625 if (err)
626 goto done;
628 err = resolve_delta_chain(deltas, repo, base_packfile,
629 path_base_packfile, base_type, base_offset + base_tslen, base_size);
630 done:
631 free(path_base_packfile);
632 if (base_packfile && fclose(base_packfile) == -1 && err == 0)
633 err = got_error_from_errno();
634 return err;
637 static const struct got_error *
638 resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
639 FILE *packfile, const char *path_packfile, int delta_type,
640 off_t delta_offset, size_t delta_size)
642 const struct got_error *err = NULL;
643 struct got_delta *delta;
645 delta = got_delta_open(path_packfile, delta_type, delta_offset,
646 delta_size);
647 if (delta == NULL)
648 return got_error(GOT_ERR_NO_MEM);
649 deltas->nentries++;
650 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
651 /* In case of error below, delta is freed in got_object_close(). */
653 switch (delta_type) {
654 case GOT_OBJ_TYPE_COMMIT:
655 case GOT_OBJ_TYPE_TREE:
656 case GOT_OBJ_TYPE_BLOB:
657 case GOT_OBJ_TYPE_TAG:
658 /* Plain types are the final delta base. Recursion ends. */
659 break;
660 case GOT_OBJ_TYPE_OFFSET_DELTA:
661 err = resolve_offset_delta(deltas, repo, packfile,
662 path_packfile, delta_offset);
663 break;
664 case GOT_OBJ_TYPE_REF_DELTA:
665 err = resolve_ref_delta(deltas, repo, packfile, path_packfile,
666 delta_offset);
667 break;
668 default:
669 return got_error(GOT_ERR_NOT_IMPL);
672 return err;
675 static const struct got_error *
676 open_delta_object(struct got_object **obj, struct got_repository *repo,
677 struct got_packidx_v2_hdr *packidx, const char *path_packfile,
678 FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
679 int delta_type, size_t delta_size)
681 const struct got_error *err = NULL;
682 struct got_object_id base_id;
683 uint8_t base_type;
684 int resolved_type;
685 uint64_t base_size;
686 size_t base_tslen;
688 *obj = calloc(1, sizeof(**obj));
689 if (*obj == NULL)
690 return got_error(GOT_ERR_NO_MEM);
692 (*obj)->flags = 0;
693 (*obj)->hdrlen = 0;
694 (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
695 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
696 (*obj)->pack_offset = offset + tslen;
698 (*obj)->path_packfile = strdup(path_packfile);
699 if ((*obj)->path_packfile == NULL) {
700 err = got_error(GOT_ERR_NO_MEM);
701 goto done;
703 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
705 SIMPLEQ_INIT(&(*obj)->deltas.entries);
706 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
708 err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
709 path_packfile, delta_type, offset, delta_size);
710 if (err)
711 goto done;
713 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
714 if (err)
715 goto done;
716 (*obj)->type = resolved_type;
718 done:
719 if (err) {
720 got_object_close(*obj);
721 *obj = NULL;
723 return err;
726 static const struct got_error *
727 open_packed_object(struct got_object **obj, struct got_repository *repo,
728 struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
730 const struct got_error *err = NULL;
731 off_t offset;
732 char *path_packfile;
733 FILE *packfile;
734 uint8_t type;
735 uint64_t size;
736 size_t tslen;
738 *obj = NULL;
740 offset = get_object_offset(packidx, idx);
741 if (offset == (uint64_t)-1)
742 return got_error(GOT_ERR_BAD_PACKIDX);
744 err = open_packfile(&packfile, &path_packfile, repo, packidx);
745 if (err)
746 return err;
748 if (fseeko(packfile, offset, SEEK_SET) != 0) {
749 err = got_error_from_errno();
750 goto done;
753 err = parse_object_type_and_size(&type, &size, &tslen, packfile);
754 if (err)
755 goto done;
757 switch (type) {
758 case GOT_OBJ_TYPE_COMMIT:
759 case GOT_OBJ_TYPE_TREE:
760 case GOT_OBJ_TYPE_BLOB:
761 case GOT_OBJ_TYPE_TAG:
762 err = open_plain_object(obj, path_packfile, id, type,
763 offset + tslen, size);
764 break;
766 case GOT_OBJ_TYPE_OFFSET_DELTA:
767 case GOT_OBJ_TYPE_REF_DELTA:
768 err = open_delta_object(obj, repo, packidx, path_packfile,
769 packfile, id, offset, tslen, type, size);
770 break;
772 default:
773 err = got_error(GOT_ERR_NOT_IMPL);
774 goto done;
776 done:
777 free(path_packfile);
778 if (packfile && fclose(packfile) == -1 && err == 0)
779 err = got_error_from_errno();
780 return err;
783 const struct got_error *
784 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
785 struct got_repository *repo)
787 const struct got_error *err = NULL;
788 struct got_packidx_v2_hdr *packidx = NULL;
789 int idx;
791 err = search_packidx(&packidx, &idx, repo, id);
792 if (err)
793 return err;
795 err = open_packed_object(obj, repo, packidx, idx, id);
796 got_packidx_close(packidx);
797 return err;
800 static const struct got_error *
801 dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
803 size_t n;
805 while (size > 0) {
806 uint8_t data[2048];
807 size_t len = MIN(size, sizeof(data));
809 n = fread(data, len, 1, infile);
810 if (n != 1)
811 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
813 n = fwrite(data, len, 1, outfile);
814 if (n != 1)
815 return got_ferror(outfile, GOT_ERR_IO);
817 size -= len;
820 rewind(outfile);
821 return NULL;
824 static const struct got_error *
825 dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
827 const struct got_error *err = NULL;
828 struct got_delta *delta;
829 FILE *base_file, *accum_file;
830 int n = 0;
832 if (SIMPLEQ_EMPTY(&deltas->entries))
833 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
835 base_file = got_opentemp();
836 if (base_file == NULL)
837 return got_error_from_errno();
839 accum_file = got_opentemp();
840 if (accum_file == NULL) {
841 err = got_error_from_errno();
842 fclose(base_file);
843 return err;
846 /* Deltas are ordered in ascending order. */
847 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
848 FILE *delta_file = fopen(delta->path_packfile, "rb");
849 if (delta_file == NULL) {
850 err = got_error_from_errno();
851 goto done;
854 if (fseeko(delta_file, delta->offset, SEEK_SET) != 0) {
855 fclose(delta_file);
856 err = got_error_from_errno();
857 goto done;
860 err = got_delta_apply(delta, base_file, delta_file,
861 /* Final delta application writes to the output file. */
862 ++n < deltas->nentries ? accum_file : outfile);
863 fclose(delta_file);
864 if (err)
865 goto done;
867 if (n < deltas->nentries) {
868 /* Accumulated delta becomes the new base. */
869 FILE *tmp = accum_file;
870 accum_file = base_file;
871 base_file = tmp;
872 rewind(base_file);
873 rewind(accum_file);
877 done:
878 fclose(base_file);
879 fclose(accum_file);
880 rewind(outfile);
881 return err;
884 const struct got_error *
885 got_packfile_extract_object(FILE **f, struct got_object *obj,
886 struct got_repository *repo)
888 const struct got_error *err = NULL;
889 FILE *packfile = NULL;
891 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
892 return got_error(GOT_ERR_OBJ_NOT_PACKED);
894 *f = got_opentemp();
895 if (*f == NULL) {
896 err = got_error(GOT_ERR_FILE_OPEN);
897 goto done;
900 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
901 packfile = fopen(obj->path_packfile, "rb");
902 if (packfile == NULL) {
903 err = got_error_from_errno();
904 goto done;
907 if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
908 err = got_error_from_errno();
909 goto done;
912 err = dump_plain_object(packfile, obj->type, obj->size, *f);
913 } else
914 err = dump_delta_chain(&obj->deltas, *f);
915 done:
916 if (packfile)
917 fclose(packfile);
918 if (err && *f)
919 fclose(*f);
920 return err;