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"
40 #include "zb.h"
42 #define GOT_PACK_PREFIX "pack-"
43 #define GOT_PACKFILE_SUFFIX ".pack"
44 #define GOT_PACKIDX_SUFFIX ".idx"
45 #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
46 SHA1_DIGEST_STRING_LENGTH - 1 + \
47 strlen(GOT_PACKFILE_SUFFIX))
48 #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
49 SHA1_DIGEST_STRING_LENGTH - 1 + \
50 strlen(GOT_PACKIDX_SUFFIX))
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 static const struct got_error *
57 verify_fanout_table(uint32_t *fanout_table)
58 {
59 int i;
61 for (i = 0; i < 0xff - 1; i++) {
62 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
63 return got_error(GOT_ERR_BAD_PACKIDX);
64 }
66 return NULL;
67 }
69 static const struct got_error *
70 get_packfile_size(size_t *size, const char *path_idx)
71 {
72 struct stat sb;
73 char *path_pack;
74 char base_path[PATH_MAX];
75 char *dot;
77 if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
78 return got_error(GOT_ERR_NO_SPACE);
80 dot = strrchr(base_path, '.');
81 if (dot == NULL)
82 return got_error(GOT_ERR_BAD_PATH);
83 *dot = '\0';
84 if (asprintf(&path_pack, "%s.pack", base_path) == -1)
85 return got_error(GOT_ERR_NO_MEM);
87 if (stat(path_pack, &sb) != 0) {
88 free(path_pack);
89 return got_error_from_errno();
90 }
92 free(path_pack);
93 *size = sb.st_size;
94 return 0;
95 }
97 const struct got_error *
98 got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
99 {
100 struct got_packidx_v2_hdr *p;
101 FILE *f;
102 const struct got_error *err = NULL;
103 size_t n, nobj, packfile_size;
104 SHA1_CTX ctx;
105 uint8_t sha1[SHA1_DIGEST_LENGTH];
107 SHA1Init(&ctx);
109 f = fopen(path, "rb");
110 if (f == NULL)
111 return got_error(GOT_ERR_BAD_PATH);
113 err = get_packfile_size(&packfile_size, path);
114 if (err)
115 return err;
117 p = calloc(1, sizeof(*p));
118 if (p == NULL) {
119 err = got_error(GOT_ERR_NO_MEM);
120 goto done;
123 n = fread(&p->magic, sizeof(p->magic), 1, f);
124 if (n != 1) {
125 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
126 goto done;
129 if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
130 err = got_error(GOT_ERR_BAD_PACKIDX);
131 goto done;
134 SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
136 n = fread(&p->version, sizeof(p->version), 1, f);
137 if (n != 1) {
138 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
139 goto done;
142 if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
143 err = got_error(GOT_ERR_BAD_PACKIDX);
144 goto done;
147 SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
149 n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
150 if (n != 1) {
151 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
152 goto done;
155 err = verify_fanout_table(p->fanout_table);
156 if (err)
157 goto done;
159 SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
161 nobj = betoh32(p->fanout_table[0xff]);
163 p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
164 if (p->sorted_ids == NULL) {
165 err = got_error(GOT_ERR_NO_MEM);
166 goto done;
169 n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
170 if (n != nobj) {
171 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
172 goto done;
175 SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
176 nobj * sizeof(*p->sorted_ids));
178 p->crc32 = calloc(nobj, sizeof(*p->crc32));
179 if (p->crc32 == NULL) {
180 err = got_error(GOT_ERR_NO_MEM);
181 goto done;
184 n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
185 if (n != nobj) {
186 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
187 goto done;
190 SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
192 p->offsets = calloc(nobj, sizeof(*p->offsets));
193 if (p->offsets == NULL) {
194 err = got_error(GOT_ERR_NO_MEM);
195 goto done;
198 n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
199 if (n != nobj) {
200 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
201 goto done;
204 SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
206 /* Large file offsets are contained only in files > 2GB. */
207 if (packfile_size <= 0x80000000)
208 goto checksum;
210 p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
211 if (p->large_offsets == NULL) {
212 err = got_error(GOT_ERR_NO_MEM);
213 goto done;
216 n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
217 if (n != nobj) {
218 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
219 goto done;
222 SHA1Update(&ctx, (uint8_t*)p->large_offsets,
223 nobj * sizeof(*p->large_offsets));
225 checksum:
226 n = fread(&p->trailer, sizeof(p->trailer), 1, f);
227 if (n != 1) {
228 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
229 goto done;
232 SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
233 SHA1Final(sha1, &ctx);
234 if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
235 err = got_error(GOT_ERR_PACKIDX_CSUM);
236 done:
237 fclose(f);
238 if (err)
239 got_packidx_close(p);
240 else
241 *packidx = p;
242 return err;
245 void
246 got_packidx_close(struct got_packidx_v2_hdr *packidx)
248 free(packidx->sorted_ids);
249 free(packidx->offsets);
250 free(packidx->crc32);
251 free(packidx->large_offsets);
252 free(packidx);
255 static int
256 is_packidx_filename(const char *name, size_t len)
258 if (len != GOT_PACKIDX_NAMELEN)
259 return 0;
261 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
262 return 0;
264 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
265 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
266 return 0;
268 return 1;
271 static off_t
272 get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
274 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
275 uint32_t offset = betoh32(packidx->offsets[idx]);
276 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
277 uint64_t loffset;
278 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
279 if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
280 return -1;
281 loffset = betoh64(packidx->large_offsets[idx]);
282 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
284 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
287 static int
288 get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
290 u_int8_t id0 = id->sha1[0];
291 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
292 int i = 0;
294 if (id0 > 0)
295 i = betoh32(packidx->fanout_table[id0 - 1]);
297 while (i < totobj) {
298 struct got_object_id *oid = &packidx->sorted_ids[i];
299 uint32_t offset;
300 int cmp = got_object_id_cmp(id, oid);
302 if (cmp == 0)
303 return i;
304 i++;
307 return -1;
310 static const struct got_error *
311 search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
312 struct got_repository *repo, struct got_object_id *id)
314 const struct got_error *err;
315 char *path_packdir;
316 DIR *packdir;
317 struct dirent *dent;
318 char *path_packidx;
320 path_packdir = got_repo_get_path_objects_pack(repo);
321 if (path_packdir == NULL)
322 return got_error(GOT_ERR_NO_MEM);
324 packdir = opendir(path_packdir);
325 if (packdir == NULL) {
326 err = got_error_from_errno();
327 goto done;
330 while ((dent = readdir(packdir)) != NULL) {
331 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
332 continue;
334 if (asprintf(&path_packidx, "%s/%s", path_packdir,
335 dent->d_name) == -1) {
336 err = got_error(GOT_ERR_NO_MEM);
337 goto done;
340 err = got_packidx_open(packidx, path_packidx);
341 free(path_packidx);
342 if (err)
343 goto done;
345 *idx = get_object_idx(*packidx, id);
346 if (*idx != -1) {
347 err = NULL; /* found the object */
348 goto done;
351 got_packidx_close(*packidx);
352 *packidx = NULL;
355 err = got_error(GOT_ERR_NO_OBJ);
356 done:
357 free(path_packdir);
358 if (packdir && closedir(packdir) != 0 && err == 0)
359 err = got_error_from_errno();
360 return err;
363 static const struct got_error *
364 get_packfile_path(char **path_packfile, struct got_repository *repo,
365 struct got_packidx_v2_hdr *packidx)
367 char *path_packdir;
368 char hex[SHA1_DIGEST_STRING_LENGTH];
369 char *sha1str;
370 char *path_packidx;
372 *path_packfile = NULL;
374 path_packdir = got_repo_get_path_objects_pack(repo);
375 if (path_packdir == NULL)
376 return got_error(GOT_ERR_NO_MEM);
378 sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
379 hex, sizeof(hex));
380 if (sha1str == NULL)
381 return got_error(GOT_ERR_PACKIDX_CSUM);
383 if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
384 GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
385 *path_packfile = NULL;
386 return got_error(GOT_ERR_NO_MEM);
389 return NULL;
392 const struct got_error *
393 read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
395 const struct got_error *err = NULL;
396 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
397 struct got_packfile_hdr hdr;
398 size_t n;
400 n = fread(&hdr, sizeof(hdr), 1, f);
401 if (n != 1)
402 return got_ferror(f, GOT_ERR_BAD_PACKIDX);
404 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
405 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
406 betoh32(hdr.nobjects) != totobj)
407 err = got_error(GOT_ERR_BAD_PACKFILE);
409 return err;
412 static const struct got_error *
413 open_packfile(FILE **packfile, char **path_packfile,
414 struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
416 const struct got_error *err;
418 *packfile = NULL;
420 err = get_packfile_path(path_packfile, repo, packidx);
421 if (err)
422 return err;
424 *packfile = fopen(*path_packfile, "rb");
425 if (*packfile == NULL) {
426 err = got_error_from_errno();
427 free(*path_packfile);
428 return err;
431 err = read_packfile_hdr(*packfile, packidx);
432 if (err) {
433 fclose(*packfile);
434 *packfile = NULL;
436 return err;
439 static const struct got_error *
440 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
441 FILE *packfile)
443 uint8_t t = 0;
444 uint64_t s = 0;
445 uint8_t sizeN;
446 size_t n;
447 int i = 0;
449 do {
450 /* We do not support size values which don't fit in 64 bit. */
451 if (i > 9)
452 return got_error(GOT_ERR_NO_SPACE);
454 n = fread(&sizeN, sizeof(sizeN), 1, packfile);
455 if (n != 1)
456 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
458 if (i == 0) {
459 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
460 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
461 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
462 } else {
463 size_t shift = 4 + 7 * (i - 1);
464 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
466 i++;
467 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
469 *type = t;
470 *size = s;
471 *len = i * sizeof(sizeN);
472 return NULL;
475 static const struct got_error *
476 open_plain_object(struct got_object **obj, const char *path_packfile,
477 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
479 *obj = calloc(1, sizeof(**obj));
480 if (*obj == NULL)
481 return got_error(GOT_ERR_NO_MEM);
483 (*obj)->path_packfile = strdup(path_packfile);
484 if ((*obj)->path_packfile == NULL) {
485 free(*obj);
486 *obj = NULL;
487 return got_error(GOT_ERR_NO_MEM);
490 (*obj)->type = type;
491 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
492 (*obj)->hdrlen = 0;
493 (*obj)->size = size;
494 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
495 (*obj)->pack_offset = offset;
497 return NULL;
500 static const struct got_error *
501 parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
503 int64_t o = 0;
504 uint8_t offN;
505 size_t n;
506 int i = 0;
508 do {
509 /* We do not support offset values which don't fit in 64 bit. */
510 if (i > 8)
511 return got_error(GOT_ERR_NO_SPACE);
513 n = fread(&offN, sizeof(offN), 1, packfile);
514 if (n != 1)
515 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
517 if (i == 0)
518 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
519 else {
520 o++;
521 o <<= 7;
522 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
524 i++;
525 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
527 *offset = o;
528 *len = i * sizeof(offN);
529 return NULL;
532 static const struct got_error *
533 parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
535 const struct got_error *err;
536 int64_t negoffset;
537 size_t negofflen;
539 err = parse_negative_offset(&negoffset, &negofflen, packfile);
540 if (err)
541 return err;
543 /* Compute the base object's offset (must be in the same pack file). */
544 *base_offset = (offset - negoffset);
545 if (*base_offset <= 0)
546 return got_error(GOT_ERR_BAD_PACKFILE);
548 return NULL;
551 static const struct got_error *
552 resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
553 FILE *, const char *, off_t, size_t, int, 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_offset, base_tslen, base_type, 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_offset, base_tslen, base_type,
630 base_size);
631 done:
632 free(path_base_packfile);
633 if (base_packfile && fclose(base_packfile) == -1 && err == 0)
634 err = got_error_from_errno();
635 return err;
638 static const struct got_error *
639 resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
640 FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
641 int delta_type, size_t delta_size)
643 const struct got_error *err = NULL;
644 struct got_delta *delta;
646 delta = got_delta_open(path_packfile, delta_offset, tslen,
647 delta_type, delta_size);
648 if (delta == NULL)
649 return got_error(GOT_ERR_NO_MEM);
650 deltas->nentries++;
651 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
652 /* In case of error below, delta is freed in got_object_close(). */
654 switch (delta_type) {
655 case GOT_OBJ_TYPE_COMMIT:
656 case GOT_OBJ_TYPE_TREE:
657 case GOT_OBJ_TYPE_BLOB:
658 case GOT_OBJ_TYPE_TAG:
659 /* Plain types are the final delta base. Recursion ends. */
660 break;
661 case GOT_OBJ_TYPE_OFFSET_DELTA:
662 err = resolve_offset_delta(deltas, repo, packfile,
663 path_packfile, delta_offset);
664 break;
665 case GOT_OBJ_TYPE_REF_DELTA:
666 err = resolve_ref_delta(deltas, repo, packfile, path_packfile,
667 delta_offset);
668 break;
669 default:
670 return got_error(GOT_ERR_NOT_IMPL);
673 return err;
676 static const struct got_error *
677 open_delta_object(struct got_object **obj, struct got_repository *repo,
678 struct got_packidx_v2_hdr *packidx, const char *path_packfile,
679 FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
680 int delta_type, size_t delta_size)
682 const struct got_error *err = NULL;
683 struct got_object_id base_id;
684 uint8_t base_type;
685 int resolved_type;
686 uint64_t base_size;
687 size_t base_tslen;
689 *obj = calloc(1, sizeof(**obj));
690 if (*obj == NULL)
691 return got_error(GOT_ERR_NO_MEM);
693 (*obj)->flags = 0;
694 (*obj)->hdrlen = 0;
695 (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
696 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
697 (*obj)->pack_offset = offset + tslen;
699 (*obj)->path_packfile = strdup(path_packfile);
700 if ((*obj)->path_packfile == NULL) {
701 err = got_error(GOT_ERR_NO_MEM);
702 goto done;
704 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
706 SIMPLEQ_INIT(&(*obj)->deltas.entries);
707 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
709 err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
710 path_packfile, offset, tslen, delta_type, delta_size);
711 if (err)
712 goto done;
714 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
715 if (err)
716 goto done;
717 (*obj)->type = resolved_type;
719 done:
720 if (err) {
721 got_object_close(*obj);
722 *obj = NULL;
724 return err;
727 static const struct got_error *
728 open_packed_object(struct got_object **obj, struct got_repository *repo,
729 struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
731 const struct got_error *err = NULL;
732 off_t offset;
733 char *path_packfile;
734 FILE *packfile;
735 uint8_t type;
736 uint64_t size;
737 size_t tslen;
739 *obj = NULL;
741 offset = get_object_offset(packidx, idx);
742 if (offset == (uint64_t)-1)
743 return got_error(GOT_ERR_BAD_PACKIDX);
745 err = open_packfile(&packfile, &path_packfile, repo, packidx);
746 if (err)
747 return err;
749 if (fseeko(packfile, offset, SEEK_SET) != 0) {
750 err = got_error_from_errno();
751 goto done;
754 err = parse_object_type_and_size(&type, &size, &tslen, packfile);
755 if (err)
756 goto done;
758 switch (type) {
759 case GOT_OBJ_TYPE_COMMIT:
760 case GOT_OBJ_TYPE_TREE:
761 case GOT_OBJ_TYPE_BLOB:
762 case GOT_OBJ_TYPE_TAG:
763 err = open_plain_object(obj, path_packfile, id, type,
764 offset + tslen, size);
765 break;
767 case GOT_OBJ_TYPE_OFFSET_DELTA:
768 case GOT_OBJ_TYPE_REF_DELTA:
769 err = open_delta_object(obj, repo, packidx, path_packfile,
770 packfile, id, offset, tslen, type, size);
771 break;
773 default:
774 err = got_error(GOT_ERR_NOT_IMPL);
775 goto done;
777 done:
778 free(path_packfile);
779 if (packfile && fclose(packfile) == -1 && err == 0)
780 err = got_error_from_errno();
781 return err;
784 const struct got_error *
785 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
786 struct got_repository *repo)
788 const struct got_error *err = NULL;
789 struct got_packidx_v2_hdr *packidx = NULL;
790 int idx;
792 err = search_packidx(&packidx, &idx, repo, id);
793 if (err)
794 return err;
796 err = open_packed_object(obj, repo, packidx, idx, id);
797 got_packidx_close(packidx);
798 return err;
801 static const struct got_error *
802 dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
804 const struct got_error *err = NULL;
805 struct got_delta *delta;
806 FILE *base_file, *accum_file;
807 int n = 0;
809 if (SIMPLEQ_EMPTY(&deltas->entries))
810 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
812 base_file = got_opentemp();
813 if (base_file == NULL)
814 return got_error_from_errno();
816 accum_file = got_opentemp();
817 if (accum_file == NULL) {
818 err = got_error_from_errno();
819 fclose(base_file);
820 return err;
823 /* Deltas are ordered in ascending order. */
824 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
825 uint8_t *delta_buf = NULL;
826 size_t delta_len = 0;
827 FILE *delta_file;
829 delta_file = fopen(delta->path_packfile, "rb");
830 if (delta_file == NULL) {
831 err = got_error_from_errno();
832 goto done;
835 if (fseeko(delta_file, delta->offset + delta->tslen,
836 SEEK_SET) != 0) {
837 fclose(delta_file);
838 err = got_error_from_errno();
839 goto done;
842 if (n == 0) {
843 /* Plain object types are the delta base. */
844 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
845 delta->type != GOT_OBJ_TYPE_TREE &&
846 delta->type != GOT_OBJ_TYPE_BLOB &&
847 delta->type != GOT_OBJ_TYPE_TAG) {
848 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
849 goto done;
852 err = got_inflate_to_file(&delta_len, delta_file,
853 base_file);
854 fclose(delta_file);
855 if (err)
856 goto done;
857 n++;
858 rewind(base_file);
859 continue;
862 if (delta->type == GOT_OBJ_TYPE_REF_DELTA &&
863 fseeko(delta_file, SHA1_DIGEST_LENGTH, SEEK_CUR) != 0) {
864 fclose(delta_file);
865 err = got_error_from_errno();
866 goto done;
869 /* Delta streams should always fit in memory. */
870 err = got_inflate_to_mem(&delta_buf, &delta_len, delta_file);
871 fclose(delta_file);
872 if (err)
873 goto done;
875 err = got_delta_apply(base_file, delta_buf, delta_len,
876 /* Final delta application writes to the output file. */
877 ++n < deltas->nentries ? accum_file : outfile);
878 free(delta_buf);
879 if (err)
880 goto done;
882 if (n < deltas->nentries) {
883 /* Accumulated delta becomes the new base. */
884 FILE *tmp = accum_file;
885 accum_file = base_file;
886 base_file = tmp;
887 rewind(base_file);
888 rewind(accum_file);
892 done:
893 fclose(base_file);
894 fclose(accum_file);
895 rewind(outfile);
896 return err;
899 const struct got_error *
900 got_packfile_extract_object(FILE **f, struct got_object *obj,
901 struct got_repository *repo)
903 const struct got_error *err = NULL;
904 FILE *packfile = NULL;
906 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
907 return got_error(GOT_ERR_OBJ_NOT_PACKED);
909 *f = got_opentemp();
910 if (*f == NULL) {
911 err = got_error(GOT_ERR_FILE_OPEN);
912 goto done;
915 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
916 packfile = fopen(obj->path_packfile, "rb");
917 if (packfile == NULL) {
918 err = got_error_from_errno();
919 goto done;
922 if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
923 err = got_error_from_errno();
924 goto done;
927 err = got_inflate_to_file(&obj->size, packfile, *f);
928 } else
929 err = dump_delta_chain(&obj->deltas, *f);
930 done:
931 if (packfile)
932 fclose(packfile);
933 if (err && *f)
934 fclose(*f);
935 return err;