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"
36 #include "got_sha1_lib.h"
37 #include "got_pack_lib.h"
38 #include "got_path_lib.h"
39 #include "got_delta_lib.h"
40 #include "got_zbuf_lib.h"
41 #include "got_object_lib.h"
42 #include "got_repository_lib.h"
44 #ifndef nitems
45 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 #endif
48 #define GOT_PACK_PREFIX "pack-"
49 #define GOT_PACKFILE_SUFFIX ".pack"
50 #define GOT_PACKIDX_SUFFIX ".idx"
51 #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
52 SHA1_DIGEST_STRING_LENGTH - 1 + \
53 strlen(GOT_PACKFILE_SUFFIX))
54 #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
55 SHA1_DIGEST_STRING_LENGTH - 1 + \
56 strlen(GOT_PACKIDX_SUFFIX))
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 static const struct got_error *
63 verify_fanout_table(uint32_t *fanout_table)
64 {
65 int i;
67 for (i = 0; i < 0xff - 1; i++) {
68 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
69 return got_error(GOT_ERR_BAD_PACKIDX);
70 }
72 return NULL;
73 }
75 static const struct got_error *
76 get_packfile_size(size_t *size, const char *path_idx)
77 {
78 struct stat sb;
79 char *path_pack;
80 char base_path[PATH_MAX];
81 char *dot;
83 if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
84 return got_error(GOT_ERR_NO_SPACE);
86 dot = strrchr(base_path, '.');
87 if (dot == NULL)
88 return got_error(GOT_ERR_BAD_PATH);
89 *dot = '\0';
90 if (asprintf(&path_pack, "%s.pack", base_path) == -1)
91 return got_error(GOT_ERR_NO_MEM);
93 if (stat(path_pack, &sb) != 0) {
94 free(path_pack);
95 return got_error_from_errno();
96 }
98 free(path_pack);
99 *size = sb.st_size;
100 return 0;
103 const struct got_error *
104 got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
106 struct got_packidx_v2_hdr *p;
107 FILE *f;
108 const struct got_error *err = NULL;
109 size_t n, nobj, packfile_size;
110 SHA1_CTX ctx;
111 uint8_t sha1[SHA1_DIGEST_LENGTH];
113 SHA1Init(&ctx);
115 f = fopen(path, "rb");
116 if (f == NULL)
117 return got_error_from_errno();
119 err = get_packfile_size(&packfile_size, path);
120 if (err)
121 return err;
123 p = calloc(1, sizeof(*p));
124 if (p == NULL) {
125 err = got_error(GOT_ERR_NO_MEM);
126 goto done;
129 n = fread(&p->magic, sizeof(p->magic), 1, f);
130 if (n != 1) {
131 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
132 goto done;
135 if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
136 err = got_error(GOT_ERR_BAD_PACKIDX);
137 goto done;
140 SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
142 n = fread(&p->version, sizeof(p->version), 1, f);
143 if (n != 1) {
144 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
145 goto done;
148 if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
149 err = got_error(GOT_ERR_BAD_PACKIDX);
150 goto done;
153 SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
155 n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
156 if (n != 1) {
157 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
158 goto done;
161 err = verify_fanout_table(p->fanout_table);
162 if (err)
163 goto done;
165 SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
167 nobj = betoh32(p->fanout_table[0xff]);
169 p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
170 if (p->sorted_ids == NULL) {
171 err = got_error(GOT_ERR_NO_MEM);
172 goto done;
175 n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
176 if (n != nobj) {
177 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
178 goto done;
181 SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
182 nobj * sizeof(*p->sorted_ids));
184 p->crc32 = calloc(nobj, sizeof(*p->crc32));
185 if (p->crc32 == NULL) {
186 err = got_error(GOT_ERR_NO_MEM);
187 goto done;
190 n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
191 if (n != nobj) {
192 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
193 goto done;
196 SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
198 p->offsets = calloc(nobj, sizeof(*p->offsets));
199 if (p->offsets == NULL) {
200 err = got_error(GOT_ERR_NO_MEM);
201 goto done;
204 n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
205 if (n != nobj) {
206 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
207 goto done;
210 SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
212 /* Large file offsets are contained only in files > 2GB. */
213 if (packfile_size <= 0x80000000)
214 goto checksum;
216 p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
217 if (p->large_offsets == NULL) {
218 err = got_error(GOT_ERR_NO_MEM);
219 goto done;
222 n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
223 if (n != nobj) {
224 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
225 goto done;
228 SHA1Update(&ctx, (uint8_t*)p->large_offsets,
229 nobj * sizeof(*p->large_offsets));
231 checksum:
232 n = fread(&p->trailer, sizeof(p->trailer), 1, f);
233 if (n != 1) {
234 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
235 goto done;
238 SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
239 SHA1Final(sha1, &ctx);
240 if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
241 err = got_error(GOT_ERR_PACKIDX_CSUM);
242 done:
243 fclose(f);
244 if (err)
245 got_packidx_close(p);
246 else
247 *packidx = p;
248 return err;
251 void
252 got_packidx_close(struct got_packidx_v2_hdr *packidx)
254 free(packidx->sorted_ids);
255 free(packidx->offsets);
256 free(packidx->crc32);
257 free(packidx->large_offsets);
258 free(packidx);
261 static int
262 is_packidx_filename(const char *name, size_t len)
264 if (len != GOT_PACKIDX_NAMELEN)
265 return 0;
267 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
268 return 0;
270 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
271 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
272 return 0;
274 return 1;
277 static off_t
278 get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
280 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
281 uint32_t offset = betoh32(packidx->offsets[idx]);
282 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
283 uint64_t loffset;
284 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
285 if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
286 return -1;
287 loffset = betoh64(packidx->large_offsets[idx]);
288 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
290 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
293 static int
294 get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
296 u_int8_t id0 = id->sha1[0];
297 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
298 int i = 0;
300 if (id0 > 0)
301 i = betoh32(packidx->fanout_table[id0 - 1]);
303 while (i < totobj) {
304 struct got_object_id *oid = &packidx->sorted_ids[i];
305 int cmp = got_object_id_cmp(id, oid);
307 if (cmp == 0)
308 return i;
309 i++;
312 return -1;
315 static struct got_packidx_v2_hdr *
316 dup_packidx(struct got_packidx_v2_hdr *packidx)
318 struct got_packidx_v2_hdr *p;
319 size_t nobj;
321 p = calloc(1, sizeof(*p));
322 if (p == NULL)
323 return NULL;
325 memcpy(p, packidx, sizeof(*p));
326 p->sorted_ids = NULL;
327 p->crc32 = NULL;
328 p->offsets = NULL;
329 p->large_offsets = NULL;
331 nobj = betoh32(p->fanout_table[0xff]);
333 p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
334 if (p->sorted_ids == NULL)
335 goto err;
336 memcpy(p->sorted_ids, packidx->sorted_ids, nobj * sizeof(*p->sorted_ids));
338 p->crc32 = calloc(nobj, sizeof(*p->crc32));
339 if (p->crc32 == NULL)
340 goto err;
341 memcpy(p->crc32, packidx->crc32, nobj * sizeof(*p->crc32));
343 p->offsets = calloc(nobj, sizeof(*p->offsets));
344 if (p->offsets == NULL)
345 goto err;
346 memcpy(p->offsets, packidx->offsets, nobj * sizeof(*p->offsets));
348 if (p->large_offsets) {
349 p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
350 if (p->large_offsets == NULL)
351 goto err;
352 memcpy(p->large_offsets, packidx->large_offsets,
353 nobj * sizeof(*p->large_offsets));
356 return p;
358 err:
359 free(p->large_offsets);
360 free(p->offsets);
361 free(p->crc32);
362 free(p->sorted_ids);
363 free(p);
364 return NULL;
367 static const struct got_error *
368 get_packfile_path(char **path_packfile, struct got_repository *repo,
369 struct got_packidx_v2_hdr *packidx)
371 char *path_packdir;
372 char hex[SHA1_DIGEST_STRING_LENGTH];
373 char *sha1str;
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 static 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 cache_pack(struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
416 const struct got_error *err;
417 FILE *packfile;
418 char *path_packfile;
419 int i;
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;
431 err = read_packfile_hdr(packfile, packidx);
432 if (err) {
433 fclose(packfile);
434 return err;
437 for (i = 0; i < nitems(repo->pack_cache); i++) {
438 if (repo->pack_cache[i].packidx == NULL)
439 break;
442 if (i == nitems(repo->pack_cache)) {
443 got_packidx_close(repo->pack_cache[i - 1].packidx);
444 fclose(repo->pack_cache[i - 1].packfile);
445 free(repo->pack_cache[i - 1].path_packfile);
446 memmove(&repo->pack_cache[1], &repo->pack_cache[0],
447 sizeof(repo->pack_cache) - sizeof(repo->pack_cache[0]));
448 i = 0;
451 repo->pack_cache[i].packidx = dup_packidx(packidx);
452 if (repo->pack_cache[i].packidx != NULL) {
453 repo->pack_cache[i].packfile = packfile;
454 repo->pack_cache[i].path_packfile = path_packfile;
455 } else {
456 fclose(packfile);
457 free(path_packfile);
459 return NULL;
462 static const struct got_error *
463 search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
464 struct got_repository *repo, struct got_object_id *id)
466 const struct got_error *err;
467 char *path_packdir;
468 DIR *packdir;
469 struct dirent *dent;
470 char *path_packidx;
471 int i;
473 /* Search pack cache. */
474 for (i = 0; i < nitems(repo->pack_cache); i++) {
475 if (repo->pack_cache[i].packidx == NULL)
476 break;
477 *idx = get_object_idx(repo->pack_cache[i].packidx, id);
478 if (*idx != -1) {
479 *packidx = dup_packidx(repo->pack_cache[i].packidx);
480 if (*packidx == NULL)
481 return got_error(GOT_ERR_NO_MEM);
482 return NULL;
485 /* No luck. Search the filesystem. */
487 path_packdir = got_repo_get_path_objects_pack(repo);
488 if (path_packdir == NULL)
489 return got_error(GOT_ERR_NO_MEM);
491 packdir = opendir(path_packdir);
492 if (packdir == NULL) {
493 err = got_error_from_errno();
494 goto done;
497 while ((dent = readdir(packdir)) != NULL) {
498 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
499 continue;
501 if (asprintf(&path_packidx, "%s/%s", path_packdir,
502 dent->d_name) == -1) {
503 err = got_error(GOT_ERR_NO_MEM);
504 goto done;
507 err = got_packidx_open(packidx, path_packidx);
508 free(path_packidx);
509 if (err)
510 goto done;
512 *idx = get_object_idx(*packidx, id);
513 if (*idx != -1) {
514 err = NULL; /* found the object */
515 cache_pack(*packidx, repo);
516 goto done;
519 got_packidx_close(*packidx);
520 *packidx = NULL;
523 err = got_error(GOT_ERR_NO_OBJ);
524 done:
525 free(path_packdir);
526 if (packdir && closedir(packdir) != 0 && err == 0)
527 err = got_error_from_errno();
528 return err;
531 static const struct got_error *
532 open_packfile(FILE **packfile, char **path_packfile, int *is_cached,
533 struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
535 const struct got_error *err;
536 int i;
538 *packfile = NULL;
539 *path_packfile = NULL;
540 *is_cached = 0;
542 /* The pack could already be cached after an object search. */
543 for (i = 0; i < nitems(repo->pack_cache); i++) {
544 if (repo->pack_cache[i].packidx == NULL)
545 break;
547 /* The pack index trailer acts as a cache key. */
548 if (memcmp(&packidx->trailer,
549 &repo->pack_cache[i].packidx->trailer,
550 sizeof(packidx->trailer)) != 0)
551 continue;
553 *packfile = repo->pack_cache[i].packfile;
554 *path_packfile = repo->pack_cache[i].path_packfile;
555 *is_cached = 1;
556 return NULL;
558 /* No luck. Try the filesystem. */
560 err = get_packfile_path(path_packfile, repo, packidx);
561 if (err)
562 return err;
564 *packfile = fopen(*path_packfile, "rb");
565 if (*packfile == NULL) {
566 err = got_error_from_errno();
567 free(*path_packfile);
568 return err;
571 err = read_packfile_hdr(*packfile, packidx);
572 if (err) {
573 fclose(*packfile);
574 *packfile = NULL;
576 return err;
579 static const struct got_error *
580 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
581 FILE *packfile)
583 uint8_t t = 0;
584 uint64_t s = 0;
585 uint8_t sizeN;
586 size_t n;
587 int i = 0;
589 do {
590 /* We do not support size values which don't fit in 64 bit. */
591 if (i > 9)
592 return got_error(GOT_ERR_NO_SPACE);
594 n = fread(&sizeN, sizeof(sizeN), 1, packfile);
595 if (n != 1)
596 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
598 if (i == 0) {
599 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
600 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
601 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
602 } else {
603 size_t shift = 4 + 7 * (i - 1);
604 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
606 i++;
607 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
609 *type = t;
610 *size = s;
611 *len = i * sizeof(sizeN);
612 return NULL;
615 static const struct got_error *
616 open_plain_object(struct got_object **obj, const char *path_packfile,
617 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
619 *obj = calloc(1, sizeof(**obj));
620 if (*obj == NULL)
621 return got_error(GOT_ERR_NO_MEM);
623 (*obj)->path_packfile = strdup(path_packfile);
624 if ((*obj)->path_packfile == NULL) {
625 free(*obj);
626 *obj = NULL;
627 return got_error(GOT_ERR_NO_MEM);
630 (*obj)->type = type;
631 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
632 (*obj)->hdrlen = 0;
633 (*obj)->size = size;
634 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
635 (*obj)->pack_offset = offset;
637 return NULL;
640 static const struct got_error *
641 parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
643 int64_t o = 0;
644 uint8_t offN;
645 size_t n;
646 int i = 0;
648 do {
649 /* We do not support offset values which don't fit in 64 bit. */
650 if (i > 8)
651 return got_error(GOT_ERR_NO_SPACE);
653 n = fread(&offN, sizeof(offN), 1, packfile);
654 if (n != 1)
655 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
657 if (i == 0)
658 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
659 else {
660 o++;
661 o <<= 7;
662 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
664 i++;
665 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
667 *offset = o;
668 *len = i * sizeof(offN);
669 return NULL;
672 static const struct got_error *
673 parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
675 const struct got_error *err;
676 int64_t negoffset;
677 size_t negofflen;
679 err = parse_negative_offset(&negoffset, &negofflen, packfile);
680 if (err)
681 return err;
683 /* Compute the base object's offset (must be in the same pack file). */
684 *base_offset = (offset - negoffset);
685 if (*base_offset <= 0)
686 return got_error(GOT_ERR_BAD_PACKFILE);
688 return NULL;
691 static const struct got_error *
692 resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
693 FILE *, const char *, off_t, size_t, int, size_t);
695 static const struct got_error *
696 add_delta(struct got_delta_chain *deltas, const char *path_packfile,
697 off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
698 size_t delta_data_offset)
700 struct got_delta *delta;
702 delta = got_delta_open(path_packfile, delta_offset, tslen,
703 delta_type, delta_size, delta_data_offset);
704 if (delta == NULL)
705 return got_error(GOT_ERR_NO_MEM);
706 /* delta is freed in got_object_close() */
707 deltas->nentries++;
708 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
709 return NULL;
712 static const struct got_error *
713 resolve_offset_delta(struct got_delta_chain *deltas,
714 struct got_repository *repo, FILE *packfile, const char *path_packfile,
715 off_t delta_offset,size_t tslen, int delta_type, size_t delta_size)
718 const struct got_error *err;
719 off_t base_offset;
720 uint8_t base_type;
721 uint64_t base_size;
722 size_t base_tslen;
723 off_t delta_data_offset;
725 err = parse_offset_delta(&base_offset, packfile, delta_offset);
726 if (err)
727 return err;
729 delta_data_offset = ftello(packfile);
730 if (delta_data_offset == -1)
731 return got_error_from_errno();
733 err = add_delta(deltas, path_packfile, delta_offset, tslen,
734 delta_type, delta_size, delta_data_offset);
735 if (err)
736 return err;
738 /* An offset delta must be in the same packfile. */
739 if (fseeko(packfile, base_offset, SEEK_SET) != 0)
740 return got_error_from_errno();
742 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
743 packfile);
744 if (err)
745 return err;
747 return resolve_delta_chain(deltas, repo, packfile, path_packfile,
748 base_offset, base_tslen, base_type, base_size);
751 static const struct got_error *
752 resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
753 FILE *packfile, const char *path_packfile, off_t delta_offset,
754 size_t tslen, int delta_type, size_t delta_size)
756 const struct got_error *err;
757 struct got_object_id id;
758 struct got_packidx_v2_hdr *packidx;
759 int idx;
760 off_t base_offset;
761 uint8_t base_type;
762 uint64_t base_size;
763 size_t base_tslen;
764 size_t n;
765 FILE *base_packfile = NULL;
766 char *path_base_packfile = NULL;
767 int pack_cached = 0;
768 off_t delta_data_offset;
770 n = fread(&id, sizeof(id), 1, packfile);
771 if (n != 1)
772 return got_ferror(packfile, GOT_ERR_IO);
774 delta_data_offset = ftello(packfile);
775 if (delta_data_offset == -1)
776 return got_error_from_errno();
778 err = add_delta(deltas, path_packfile, delta_offset, tslen,
779 delta_type, delta_size, delta_data_offset);
780 if (err)
781 return err;
783 err = search_packidx(&packidx, &idx, repo, &id);
784 if (err)
785 return err;
787 base_offset = get_object_offset(packidx, idx);
788 if (base_offset == (uint64_t)-1) {
789 got_packidx_close(packidx);
790 return got_error(GOT_ERR_BAD_PACKIDX);
793 err = open_packfile(&base_packfile, &path_base_packfile, &pack_cached,
794 repo, packidx);
795 got_packidx_close(packidx);
796 if (err)
797 return err;
799 if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
800 err = got_error_from_errno();
801 goto done;
804 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
805 base_packfile);
806 if (err)
807 goto done;
809 err = resolve_delta_chain(deltas, repo, base_packfile,
810 path_base_packfile, base_offset, base_tslen, base_type,
811 base_size);
812 done:
813 if (!pack_cached) {
814 free(path_base_packfile);
815 if (base_packfile && fclose(base_packfile) == -1 && err == 0)
816 err = got_error_from_errno();
818 return err;
821 static const struct got_error *
822 resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
823 FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
824 int delta_type, size_t delta_size)
826 const struct got_error *err = NULL;
828 switch (delta_type) {
829 case GOT_OBJ_TYPE_COMMIT:
830 case GOT_OBJ_TYPE_TREE:
831 case GOT_OBJ_TYPE_BLOB:
832 case GOT_OBJ_TYPE_TAG:
833 /* Plain types are the final delta base. Recursion ends. */
834 err = add_delta(deltas, path_packfile, delta_offset, tslen,
835 delta_type, delta_size, 0);
836 break;
837 case GOT_OBJ_TYPE_OFFSET_DELTA:
838 err = resolve_offset_delta(deltas, repo, packfile,
839 path_packfile, delta_offset, tslen, delta_type,
840 delta_size);
841 break;
842 case GOT_OBJ_TYPE_REF_DELTA:
843 err = resolve_ref_delta(deltas, repo, packfile,
844 path_packfile, delta_offset, tslen, delta_type,
845 delta_size);
846 break;
847 default:
848 return got_error(GOT_ERR_NOT_IMPL);
851 return err;
854 static const struct got_error *
855 open_delta_object(struct got_object **obj, struct got_repository *repo,
856 struct got_packidx_v2_hdr *packidx, const char *path_packfile,
857 FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
858 int delta_type, size_t delta_size)
860 const struct got_error *err = NULL;
861 int resolved_type;
863 *obj = calloc(1, sizeof(**obj));
864 if (*obj == NULL)
865 return got_error(GOT_ERR_NO_MEM);
867 (*obj)->flags = 0;
868 (*obj)->hdrlen = 0;
869 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
870 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
871 (*obj)->pack_offset = offset + tslen;
873 (*obj)->path_packfile = strdup(path_packfile);
874 if ((*obj)->path_packfile == NULL) {
875 err = got_error(GOT_ERR_NO_MEM);
876 goto done;
878 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
880 SIMPLEQ_INIT(&(*obj)->deltas.entries);
881 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
883 err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
884 path_packfile, offset, tslen, delta_type, delta_size);
885 if (err)
886 goto done;
888 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
889 if (err)
890 goto done;
891 (*obj)->type = resolved_type;
893 done:
894 if (err) {
895 got_object_close(*obj);
896 *obj = NULL;
898 return err;
901 static const struct got_error *
902 open_packed_object(struct got_object **obj, struct got_repository *repo,
903 struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
905 const struct got_error *err = NULL;
906 off_t offset;
907 char *path_packfile = NULL;
908 FILE *packfile = NULL;
909 int pack_cached = 0;
910 uint8_t type;
911 uint64_t size;
912 size_t tslen;
914 *obj = NULL;
916 offset = get_object_offset(packidx, idx);
917 if (offset == (uint64_t)-1)
918 return got_error(GOT_ERR_BAD_PACKIDX);
920 err = open_packfile(&packfile, &path_packfile, &pack_cached,
921 repo, packidx);
922 if (err)
923 return err;
925 if (fseeko(packfile, offset, SEEK_SET) != 0) {
926 err = got_error_from_errno();
927 goto done;
930 err = parse_object_type_and_size(&type, &size, &tslen, packfile);
931 if (err)
932 goto done;
934 switch (type) {
935 case GOT_OBJ_TYPE_COMMIT:
936 case GOT_OBJ_TYPE_TREE:
937 case GOT_OBJ_TYPE_BLOB:
938 case GOT_OBJ_TYPE_TAG:
939 err = open_plain_object(obj, path_packfile, id, type,
940 offset + tslen, size);
941 break;
943 case GOT_OBJ_TYPE_OFFSET_DELTA:
944 case GOT_OBJ_TYPE_REF_DELTA:
945 err = open_delta_object(obj, repo, packidx, path_packfile,
946 packfile, id, offset, tslen, type, size);
947 break;
949 default:
950 err = got_error(GOT_ERR_NOT_IMPL);
951 goto done;
953 done:
954 if (!pack_cached) {
955 free(path_packfile);
956 if (packfile && fclose(packfile) == -1 && err == 0)
957 err = got_error_from_errno();
959 return err;
962 const struct got_error *
963 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
964 struct got_repository *repo)
966 const struct got_error *err = NULL;
967 struct got_packidx_v2_hdr *packidx = NULL;
968 int idx;
970 err = search_packidx(&packidx, &idx, repo, id);
971 if (err)
972 return err;
974 err = open_packed_object(obj, repo, packidx, idx, id);
975 got_packidx_close(packidx);
976 return err;
979 static const struct got_error *
980 get_delta_sizes(uint64_t *base_size, uint64_t *result_size,
981 struct got_delta *delta)
983 const struct got_error *err;
984 uint8_t *delta_buf = NULL;
985 size_t delta_len = 0;
986 FILE *delta_file;
988 delta_file = fopen(delta->path_packfile, "rb");
989 if (delta_file == NULL)
990 return got_error_from_errno();
992 if (fseeko(delta_file, delta->data_offset, SEEK_SET) != 0) {
993 err = got_error_from_errno();
994 fclose(delta_file);
995 return err;
998 err = got_inflate_to_mem(&delta_buf, &delta_len, delta_file);
999 fclose(delta_file);
1000 if (err)
1001 return err;
1003 err = got_delta_get_sizes(base_size, result_size, delta_buf, delta_len);
1004 free(delta_buf);
1005 return err;
1008 static const struct got_error *
1009 get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1011 struct got_delta *delta;
1012 uint64_t base_size = 0, result_size = 0;
1014 *max_size = 0;
1015 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1016 /* Plain object types are the delta base. */
1017 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1018 delta->type != GOT_OBJ_TYPE_TREE &&
1019 delta->type != GOT_OBJ_TYPE_BLOB &&
1020 delta->type != GOT_OBJ_TYPE_TAG) {
1021 const struct got_error *err;
1022 err = get_delta_sizes(&base_size, &result_size, delta);
1023 if (err)
1024 return err;
1025 } else
1026 base_size = delta->size;
1027 if (base_size > *max_size)
1028 *max_size = base_size;
1029 if (result_size > *max_size)
1030 *max_size = result_size;
1033 return NULL;
1036 void
1037 clear_delta_cache_entry(struct got_delta_cache_entry *entry)
1039 entry->data_offset = 0;
1040 free(entry->delta_buf);
1041 entry->delta_buf = NULL;
1042 entry->delta_len = 0;
1045 const struct got_error *
1046 add_delta_cache_entry(struct got_delta_cache *cache, off_t data_offset,
1047 uint8_t *delta_buf, size_t delta_len)
1049 int i;
1050 struct got_delta_cache_entry *entry;
1052 for (i = 0; i < nitems(cache->deltas); i++) {
1053 entry = &cache->deltas[i];
1054 if (entry->data_offset == 0)
1055 break;
1058 if (i == nitems(cache->deltas)) {
1059 entry = &cache->deltas[i - 1];
1060 clear_delta_cache_entry(entry);
1061 memmove(&cache->deltas[1], &cache->deltas[0],
1062 sizeof(cache->deltas) - sizeof(cache->deltas[0]));
1063 i = 0;
1066 entry = &cache->deltas[i];
1067 entry->data_offset = data_offset;
1068 entry->delta_buf = delta_buf;
1069 entry->delta_len = delta_len;
1070 return NULL;
1073 const struct got_error *
1074 cache_delta(off_t data_offset, uint8_t *delta_buf, size_t delta_len,
1075 const char *path_packfile, struct got_repository *repo)
1077 struct got_delta_cache *cache;
1078 int i;
1080 for (i = 0; i < nitems(repo->delta_cache); i++) {
1081 cache = &repo->delta_cache[i];
1082 if (cache->path_packfile == NULL)
1083 break;
1084 if (strcmp(cache->path_packfile, path_packfile) == 0)
1085 return add_delta_cache_entry(cache, data_offset,
1086 delta_buf, delta_len);
1089 if (i == nitems(repo->delta_cache)) {
1090 int j;
1091 cache = &repo->delta_cache[i - 1];
1092 free(cache->path_packfile);
1093 cache->path_packfile = NULL;
1094 for (j = 0; j < nitems(cache->deltas); j++) {
1095 struct got_delta_cache_entry *entry = &cache->deltas[j];
1096 if (entry->data_offset == 0)
1097 break;
1098 clear_delta_cache_entry(entry);
1100 memmove(&repo->delta_cache[1], &repo->delta_cache[0],
1101 sizeof(repo->delta_cache) - sizeof(repo->delta_cache[0]));
1102 i = 0;
1105 cache = &repo->delta_cache[i];
1106 cache->path_packfile = strdup(path_packfile);
1107 if (cache->path_packfile == NULL)
1108 return got_error(GOT_ERR_NO_MEM);
1109 return add_delta_cache_entry(cache, data_offset, delta_buf, delta_len);
1112 void
1113 get_cached_delta(uint8_t **delta_buf, size_t *delta_len,
1114 off_t data_offset, const char *path_packfile, struct got_repository *repo)
1116 struct got_delta_cache *cache;
1117 struct got_delta_cache_entry *entry;
1118 int i;
1120 *delta_buf = NULL;
1121 *delta_len = 0;
1123 for (i = 0; i < nitems(repo->delta_cache); i++) {
1124 cache = &repo->delta_cache[i];
1125 if (cache->path_packfile == NULL)
1126 return;
1127 if (strcmp(cache->path_packfile, path_packfile) == 0)
1128 break;
1131 if (i == nitems(repo->delta_cache))
1132 return;
1134 for (i = 0; i < nitems(cache->deltas); i++) {
1135 entry = &cache->deltas[i];
1136 if (entry->data_offset == 0)
1137 break;
1138 if (entry->data_offset == data_offset) {
1139 *delta_buf = entry->delta_buf;
1140 *delta_len = entry->delta_len;
1141 break;
1146 static const struct got_error *
1147 dump_delta_chain_to_file(struct got_delta_chain *deltas, FILE *outfile,
1148 const char *path_packfile, struct got_repository *repo)
1150 const struct got_error *err = NULL;
1151 struct got_delta *delta;
1152 FILE *base_file = NULL, *accum_file = NULL;
1153 uint8_t *base_buf = NULL, *accum_buf = NULL;
1154 size_t accum_size;
1155 uint64_t max_size;
1156 int n = 0;
1158 if (SIMPLEQ_EMPTY(&deltas->entries))
1159 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1161 /* We process small enough files entirely in memory for speed. */
1162 err = get_delta_chain_max_size(&max_size, deltas);
1163 if (err)
1164 return err;
1165 if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1166 accum_buf = malloc(max_size);
1167 if (accum_buf == NULL)
1168 return got_error(GOT_ERR_NO_MEM);
1169 } else {
1170 base_file = got_opentemp();
1171 if (base_file == NULL)
1172 return got_error_from_errno();
1174 accum_file = got_opentemp();
1175 if (accum_file == NULL) {
1176 err = got_error_from_errno();
1177 fclose(base_file);
1178 return err;
1182 /* Deltas are ordered in ascending order. */
1183 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1184 uint8_t *delta_buf = NULL;
1185 size_t delta_len = 0;
1187 if (n == 0) {
1188 FILE *delta_file;
1189 size_t base_len;
1191 /* Plain object types are the delta base. */
1192 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1193 delta->type != GOT_OBJ_TYPE_TREE &&
1194 delta->type != GOT_OBJ_TYPE_BLOB &&
1195 delta->type != GOT_OBJ_TYPE_TAG) {
1196 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1197 goto done;
1200 delta_file = fopen(delta->path_packfile, "rb");
1201 if (delta_file == NULL) {
1202 err = got_error_from_errno();
1203 goto done;
1206 if (fseeko(delta_file, delta->offset + delta->tslen,
1207 SEEK_SET) != 0) {
1208 fclose(delta_file);
1209 err = got_error_from_errno();
1210 goto done;
1212 if (base_file)
1213 err = got_inflate_to_file(&delta_len,
1214 delta_file, base_file);
1215 else {
1216 err = got_inflate_to_mem(&base_buf, &base_len,
1217 delta_file);
1218 if (base_len < max_size) {
1219 uint8_t *p;
1220 p = reallocarray(base_buf, 1, max_size);
1221 if (p == NULL) {
1222 err = got_error(GOT_ERR_NO_MEM);
1223 goto done;
1225 base_buf = p;
1228 fclose(delta_file);
1229 if (err)
1230 goto done;
1231 n++;
1232 if (base_file)
1233 rewind(base_file);
1234 continue;
1237 get_cached_delta(&delta_buf, &delta_len, delta->data_offset,
1238 path_packfile, repo);
1239 if (delta_buf == NULL) {
1240 FILE *delta_file = fopen(delta->path_packfile, "rb");
1241 if (delta_file == NULL) {
1242 err = got_error_from_errno();
1243 goto done;
1245 if (fseeko(delta_file, delta->data_offset, SEEK_SET)
1246 != 0) {
1247 fclose(delta_file);
1248 err = got_error_from_errno();
1249 goto done;
1252 /* Delta streams should always fit in memory. */
1253 err = got_inflate_to_mem(&delta_buf, &delta_len,
1254 delta_file);
1255 fclose(delta_file);
1256 if (err)
1257 goto done;
1259 err = cache_delta(delta->data_offset, delta_buf,
1260 delta_len, path_packfile, repo);
1261 if (err)
1262 goto done;
1264 /* delta_buf is now cached */
1266 if (base_buf) {
1267 err = got_delta_apply_in_mem(base_buf, delta_buf,
1268 delta_len, accum_buf, &accum_size);
1269 n++;
1270 } else {
1271 err = got_delta_apply(base_file, delta_buf, delta_len,
1272 /* Final delta application writes to output file. */
1273 ++n < deltas->nentries ? accum_file : outfile);
1275 if (err)
1276 goto done;
1278 if (n < deltas->nentries) {
1279 /* Accumulated delta becomes the new base. */
1280 if (base_buf) {
1281 uint8_t *tmp = accum_buf;
1282 accum_buf = base_buf;
1283 base_buf = tmp;
1284 } else {
1285 FILE *tmp = accum_file;
1286 accum_file = base_file;
1287 base_file = tmp;
1288 rewind(base_file);
1289 rewind(accum_file);
1294 done:
1295 free(base_buf);
1296 if (accum_buf) {
1297 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1298 free(accum_buf);
1299 if (len != accum_size)
1300 return got_ferror(outfile, GOT_ERR_IO);
1302 if (base_file)
1303 fclose(base_file);
1304 if (accum_file)
1305 fclose(accum_file);
1306 rewind(outfile);
1307 return err;
1310 static const struct got_error *
1311 dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1312 struct got_delta_chain *deltas, const char *path_packfile,
1313 struct got_repository *repo)
1315 const struct got_error *err = NULL;
1316 struct got_delta *delta;
1317 uint8_t *base_buf = NULL, *accum_buf = NULL;
1318 size_t accum_size;
1319 uint64_t max_size;
1320 int n = 0;
1322 *outbuf = NULL;
1323 *outlen = 0;
1325 if (SIMPLEQ_EMPTY(&deltas->entries))
1326 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1328 err = get_delta_chain_max_size(&max_size, deltas);
1329 if (err)
1330 return err;
1331 accum_buf = malloc(max_size);
1332 if (accum_buf == NULL)
1333 return got_error(GOT_ERR_NO_MEM);
1335 /* Deltas are ordered in ascending order. */
1336 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1337 uint8_t *delta_buf = NULL;
1338 size_t delta_len = 0;
1340 if (n == 0) {
1341 FILE *delta_file;
1342 size_t base_len;
1344 /* Plain object types are the delta base. */
1345 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1346 delta->type != GOT_OBJ_TYPE_TREE &&
1347 delta->type != GOT_OBJ_TYPE_BLOB &&
1348 delta->type != GOT_OBJ_TYPE_TAG) {
1349 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1350 goto done;
1353 delta_file = fopen(delta->path_packfile, "rb");
1354 if (delta_file == NULL) {
1355 err = got_error_from_errno();
1356 goto done;
1359 if (fseeko(delta_file, delta->offset + delta->tslen,
1360 SEEK_SET) != 0) {
1361 fclose(delta_file);
1362 err = got_error_from_errno();
1363 goto done;
1365 err = got_inflate_to_mem(&base_buf, &base_len,
1366 delta_file);
1367 if (base_len < max_size) {
1368 uint8_t *p;
1369 p = reallocarray(base_buf, 1, max_size);
1370 if (p == NULL) {
1371 err = got_error(GOT_ERR_NO_MEM);
1372 goto done;
1374 base_buf = p;
1376 fclose(delta_file);
1377 if (err)
1378 goto done;
1379 n++;
1380 continue;
1383 get_cached_delta(&delta_buf, &delta_len, delta->data_offset,
1384 path_packfile, repo);
1385 if (delta_buf == NULL) {
1386 FILE *delta_file = fopen(delta->path_packfile, "rb");
1387 if (delta_file == NULL) {
1388 err = got_error_from_errno();
1389 goto done;
1391 if (fseeko(delta_file, delta->data_offset, SEEK_SET)
1392 != 0) {
1393 fclose(delta_file);
1394 err = got_error_from_errno();
1395 goto done;
1398 /* Delta streams should always fit in memory. */
1399 err = got_inflate_to_mem(&delta_buf, &delta_len,
1400 delta_file);
1401 fclose(delta_file);
1402 if (err)
1403 goto done;
1405 err = cache_delta(delta->data_offset, delta_buf,
1406 delta_len, path_packfile, repo);
1407 if (err)
1408 goto done;
1410 /* delta_buf is now cached */
1412 err = got_delta_apply_in_mem(base_buf, delta_buf,
1413 delta_len, accum_buf, &accum_size);
1414 n++;
1415 if (err)
1416 goto done;
1418 if (n < deltas->nentries) {
1419 /* Accumulated delta becomes the new base. */
1420 uint8_t *tmp = accum_buf;
1421 accum_buf = base_buf;
1422 base_buf = tmp;
1426 done:
1427 free(base_buf);
1428 if (err) {
1429 free(accum_buf);
1430 *outbuf = NULL;
1431 *outlen = 0;
1432 } else {
1433 *outbuf = accum_buf;
1434 *outlen = accum_size;
1436 return err;
1439 const struct got_error *
1440 got_packfile_extract_object(FILE **f, struct got_object *obj,
1441 struct got_repository *repo)
1443 const struct got_error *err = NULL;
1444 FILE *packfile = NULL;
1446 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1447 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1449 *f = got_opentemp();
1450 if (*f == NULL) {
1451 err = got_error(GOT_ERR_FILE_OPEN);
1452 goto done;
1455 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1456 packfile = fopen(obj->path_packfile, "rb");
1457 if (packfile == NULL) {
1458 err = got_error_from_errno();
1459 goto done;
1462 if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
1463 err = got_error_from_errno();
1464 goto done;
1467 err = got_inflate_to_file(&obj->size, packfile, *f);
1468 } else
1469 err = dump_delta_chain_to_file(&obj->deltas, *f,
1470 obj->path_packfile, repo);
1471 done:
1472 if (packfile)
1473 fclose(packfile);
1474 if (err && *f)
1475 fclose(*f);
1476 return err;
1479 const struct got_error *
1480 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1481 struct got_object *obj, struct got_repository *repo)
1483 const struct got_error *err = NULL;
1484 FILE *packfile = NULL;
1486 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1487 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1489 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1490 packfile = fopen(obj->path_packfile, "rb");
1491 if (packfile == NULL) {
1492 err = got_error_from_errno();
1493 goto done;
1496 if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
1497 err = got_error_from_errno();
1498 goto done;
1501 err = got_inflate_to_mem(buf, len, packfile);
1502 } else
1503 err = dump_delta_chain_to_mem(buf, len, &obj->deltas,
1504 obj->path_packfile, repo);
1505 done:
1506 if (packfile)
1507 fclose(packfile);
1508 return err;