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 <fcntl.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <sha1.h>
30 #include <endian.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_opentemp.h"
38 #include "got_lib_sha1.h"
39 #include "got_lib_pack.h"
40 #include "got_lib_path.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_zbuf.h"
43 #include "got_lib_object.h"
44 #include "got_lib_repository.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #endif
50 #define GOT_PACK_PREFIX "pack-"
51 #define GOT_PACKFILE_SUFFIX ".pack"
52 #define GOT_PACKIDX_SUFFIX ".idx"
53 #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
54 SHA1_DIGEST_STRING_LENGTH - 1 + \
55 strlen(GOT_PACKFILE_SUFFIX))
56 #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
57 SHA1_DIGEST_STRING_LENGTH - 1 + \
58 strlen(GOT_PACKIDX_SUFFIX))
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 static const struct got_error *
65 verify_fanout_table(uint32_t *fanout_table)
66 {
67 int i;
69 for (i = 0; i < 0xff - 1; i++) {
70 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
71 return got_error(GOT_ERR_BAD_PACKIDX);
72 }
74 return NULL;
75 }
77 static const struct got_error *
78 get_packfile_size(size_t *size, const char *path)
79 {
80 struct stat sb;
81 char *dot;
83 *size = 0;
85 dot = strrchr(path, '.');
86 if (dot == NULL)
87 return got_error(GOT_ERR_BAD_PATH);
89 /* Path must point to a pack index or to a pack file. */
90 if (strcmp(dot, ".idx") == 0) {
91 const struct got_error *err = NULL;
92 char *path_pack;
93 char base_path[PATH_MAX];
95 /* Convert pack index path to pack file path. */
96 if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
97 return got_error(GOT_ERR_NO_SPACE);
98 dot = strrchr(base_path, '.');
99 if (dot == NULL)
100 return got_error(GOT_ERR_BAD_PATH);
101 *dot = '\0';
102 if (asprintf(&path_pack, "%s.pack", base_path) == -1)
103 return got_error_from_errno();
105 if (stat(path_pack, &sb) != 0)
106 err = got_error_from_errno();
107 free(path_pack);
108 if (err)
109 return err;
110 } else if (strcmp(dot, ".pack") == 0) {
111 if (stat(path, &sb) != 0)
112 return got_error_from_errno();
113 } else
114 return got_error(GOT_ERR_BAD_PATH);
116 *size = sb.st_size;
117 return 0;
120 const struct got_error *
121 got_packidx_open(struct got_packidx **packidx, const char *path)
123 struct got_packidx *p;
124 struct got_packidx_v2_hdr *h;
125 FILE *f;
126 const struct got_error *err = NULL;
127 size_t n, nobj, packfile_size;
128 SHA1_CTX ctx;
129 uint8_t sha1[SHA1_DIGEST_LENGTH];
131 *packidx = NULL;
133 SHA1Init(&ctx);
135 f = fopen(path, "rb");
136 if (f == NULL)
137 return got_error_from_errno();
139 err = get_packfile_size(&packfile_size, path);
140 if (err)
141 return err;
143 p = calloc(1, sizeof(*p));
144 if (p == NULL)
145 return got_error_from_errno();
146 p->path_packidx = strdup(path);
147 if (p->path_packidx == NULL) {
148 err = got_error_from_errno();
149 free(p->path_packidx);
150 free(p);
151 return err;
154 h = &p->hdr;
155 n = fread(&h->magic, sizeof(h->magic), 1, f);
156 if (n != 1) {
157 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
158 goto done;
161 if (betoh32(h->magic) != GOT_PACKIDX_V2_MAGIC) {
162 err = got_error(GOT_ERR_BAD_PACKIDX);
163 goto done;
166 SHA1Update(&ctx, (uint8_t *)&h->magic, sizeof(h->magic));
168 n = fread(&h->version, sizeof(h->version), 1, f);
169 if (n != 1) {
170 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
171 goto done;
174 if (betoh32(h->version) != GOT_PACKIDX_VERSION) {
175 err = got_error(GOT_ERR_BAD_PACKIDX);
176 goto done;
179 SHA1Update(&ctx, (uint8_t *)&h->version, sizeof(h->version));
181 n = fread(&h->fanout_table, sizeof(h->fanout_table), 1, f);
182 if (n != 1) {
183 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
184 goto done;
187 err = verify_fanout_table(h->fanout_table);
188 if (err)
189 goto done;
191 SHA1Update(&ctx, (uint8_t *)h->fanout_table, sizeof(h->fanout_table));
193 nobj = betoh32(h->fanout_table[0xff]);
195 h->sorted_ids = calloc(nobj, sizeof(*h->sorted_ids));
196 if (h->sorted_ids == NULL) {
197 err = got_error_from_errno();
198 goto done;
201 n = fread(h->sorted_ids, sizeof(*h->sorted_ids), nobj, f);
202 if (n != nobj) {
203 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
204 goto done;
207 SHA1Update(&ctx, (uint8_t *)h->sorted_ids,
208 nobj * sizeof(*h->sorted_ids));
210 h->crc32 = calloc(nobj, sizeof(*h->crc32));
211 if (h->crc32 == NULL) {
212 err = got_error_from_errno();
213 goto done;
216 n = fread(h->crc32, sizeof(*h->crc32), nobj, f);
217 if (n != nobj) {
218 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
219 goto done;
222 SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
224 h->offsets = calloc(nobj, sizeof(*h->offsets));
225 if (h->offsets == NULL) {
226 err = got_error_from_errno();
227 goto done;
230 n = fread(h->offsets, sizeof(*h->offsets), nobj, f);
231 if (n != nobj) {
232 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
233 goto done;
236 SHA1Update(&ctx, (uint8_t *)h->offsets, nobj * sizeof(*h->offsets));
238 /* Large file offsets are contained only in files > 2GB. */
239 if (packfile_size <= 0x80000000)
240 goto checksum;
242 h->large_offsets = calloc(nobj, sizeof(*h->large_offsets));
243 if (h->large_offsets == NULL) {
244 err = got_error_from_errno();
245 goto done;
248 n = fread(h->large_offsets, sizeof(*h->large_offsets), nobj, f);
249 if (n != nobj) {
250 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
251 goto done;
254 SHA1Update(&ctx, (uint8_t*)h->large_offsets,
255 nobj * sizeof(*h->large_offsets));
257 checksum:
258 n = fread(&h->trailer, sizeof(h->trailer), 1, f);
259 if (n != 1) {
260 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
261 goto done;
264 SHA1Update(&ctx, h->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
265 SHA1Final(sha1, &ctx);
266 if (memcmp(h->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
267 err = got_error(GOT_ERR_PACKIDX_CSUM);
268 done:
269 fclose(f);
270 if (err)
271 got_packidx_close(p);
272 else
273 *packidx = p;
274 return err;
277 void
278 got_packidx_close(struct got_packidx *packidx)
280 free(packidx->hdr.sorted_ids);
281 free(packidx->hdr.offsets);
282 free(packidx->hdr.crc32);
283 free(packidx->hdr.large_offsets);
284 free(packidx->path_packidx);
285 free(packidx);
288 static int
289 is_packidx_filename(const char *name, size_t len)
291 if (len != GOT_PACKIDX_NAMELEN)
292 return 0;
294 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
295 return 0;
297 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
298 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
299 return 0;
301 return 1;
304 static off_t
305 get_object_offset(struct got_packidx *packidx, int idx)
307 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
308 uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
309 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
310 uint64_t loffset;
311 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
312 if (idx < 0 || idx > totobj ||
313 packidx->hdr.large_offsets == NULL)
314 return -1;
315 loffset = betoh64(packidx->hdr.large_offsets[idx]);
316 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
318 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
321 static const struct got_error *
322 get_packfile_path(char **path_packfile, struct got_repository *repo,
323 struct got_packidx *packidx);
325 static int
326 get_object_idx(struct got_packidx *packidx, struct got_object_id *id,
327 struct got_repository *repo)
329 u_int8_t id0 = id->sha1[0];
330 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
331 int i = 0;
333 if (id0 > 0)
334 i = betoh32(packidx->hdr.fanout_table[id0 - 1]);
336 while (i < totobj) {
337 struct got_object_id *oid = &packidx->hdr.sorted_ids[i];
338 int cmp = got_object_id_cmp(id, oid);
340 if (cmp == 0)
341 return i;
342 i++;
345 return -1;
348 static struct got_packidx *
349 dup_packidx(struct got_packidx *packidx)
351 struct got_packidx *p;
352 size_t nobj;
354 p = calloc(1, sizeof(*p));
355 if (p == NULL)
356 return NULL;
358 p->path_packidx = strdup(packidx->path_packidx);
359 if (p->path_packidx == NULL) {
360 free(p);
361 return NULL;
363 memcpy(&p->hdr, &packidx->hdr, sizeof(p->hdr));
364 p->hdr.sorted_ids = NULL;
365 p->hdr.crc32 = NULL;
366 p->hdr.offsets = NULL;
367 p->hdr.large_offsets = NULL;
369 nobj = betoh32(p->hdr.fanout_table[0xff]);
371 p->hdr.sorted_ids = calloc(nobj, sizeof(*p->hdr.sorted_ids));
372 if (p->hdr.sorted_ids == NULL)
373 goto err;
374 memcpy(p->hdr.sorted_ids, packidx->hdr.sorted_ids,
375 nobj * sizeof(*p->hdr.sorted_ids));
377 p->hdr.crc32 = calloc(nobj, sizeof(*p->hdr.crc32));
378 if (p->hdr.crc32 == NULL)
379 goto err;
380 memcpy(p->hdr.crc32, packidx->hdr.crc32, nobj * sizeof(*p->hdr.crc32));
382 p->hdr.offsets = calloc(nobj, sizeof(*p->hdr.offsets));
383 if (p->hdr.offsets == NULL)
384 goto err;
385 memcpy(p->hdr.offsets, packidx->hdr.offsets,
386 nobj * sizeof(*p->hdr.offsets));
388 if (p->hdr.large_offsets) {
389 p->hdr.large_offsets = calloc(nobj,
390 sizeof(*p->hdr.large_offsets));
391 if (p->hdr.large_offsets == NULL)
392 goto err;
393 memcpy(p->hdr.large_offsets, packidx->hdr.large_offsets,
394 nobj * sizeof(*p->hdr.large_offsets));
397 return p;
399 err:
400 free(p->hdr.large_offsets);
401 free(p->hdr.offsets);
402 free(p->hdr.crc32);
403 free(p->hdr.sorted_ids);
404 free(p->path_packidx);
405 free(p);
406 return NULL;
409 static void
410 cache_packidx(struct got_packidx *packidx, struct got_repository *repo)
412 int i;
414 for (i = 0; i < nitems(repo->packidx_cache); i++) {
415 if (repo->packidx_cache[i] == NULL)
416 break;
419 if (i == nitems(repo->packidx_cache)) {
420 got_packidx_close(repo->packidx_cache[i - 1]);
421 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
422 sizeof(repo->packidx_cache) -
423 sizeof(repo->packidx_cache[0]));
424 i = 0;
427 repo->packidx_cache[i] = dup_packidx(packidx);
430 static const struct got_error *
431 search_packidx(struct got_packidx **packidx, int *idx,
432 struct got_repository *repo, struct got_object_id *id)
434 const struct got_error *err;
435 char *path_packdir;
436 DIR *packdir;
437 struct dirent *dent;
438 char *path_packidx;
439 int i;
441 /* Search pack index cache. */
442 for (i = 0; i < nitems(repo->packidx_cache); i++) {
443 if (repo->packidx_cache[i] == NULL)
444 break;
445 *idx = get_object_idx(repo->packidx_cache[i], id, repo);
446 if (*idx != -1) {
447 *packidx = repo->packidx_cache[i];
448 return NULL;
451 /* No luck. Search the filesystem. */
453 path_packdir = got_repo_get_path_objects_pack(repo);
454 if (path_packdir == NULL)
455 return got_error_from_errno();
457 packdir = opendir(path_packdir);
458 if (packdir == NULL) {
459 err = got_error_from_errno();
460 goto done;
463 while ((dent = readdir(packdir)) != NULL) {
464 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
465 continue;
467 if (asprintf(&path_packidx, "%s/%s", path_packdir,
468 dent->d_name) == -1) {
469 err = got_error_from_errno();
470 goto done;
473 err = got_packidx_open(packidx, path_packidx);
474 free(path_packidx);
475 if (err)
476 goto done;
478 *idx = get_object_idx(*packidx, id, repo);
479 if (*idx != -1) {
480 err = NULL; /* found the object */
481 cache_packidx(*packidx, repo);
482 goto done;
485 got_packidx_close(*packidx);
486 *packidx = NULL;
489 err = got_error(GOT_ERR_NO_OBJ);
490 done:
491 free(path_packdir);
492 if (packdir && closedir(packdir) != 0 && err == 0)
493 err = got_error_from_errno();
494 return err;
497 static const struct got_error *
498 get_packfile_path(char **path_packfile, struct got_repository *repo,
499 struct got_packidx *packidx)
501 size_t size;
503 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
504 size = strlen(packidx->path_packidx) + 2;
505 if (size < GOT_PACKFILE_NAMELEN + 1)
506 return got_error(GOT_ERR_BAD_PATH);
508 *path_packfile = calloc(size, sizeof(**path_packfile));
509 if (*path_packfile == NULL)
510 return got_error_from_errno();
512 /* Copy up to and excluding ".idx". */
513 strncpy(*path_packfile, packidx->path_packidx,
514 size - strlen(".idx") - 2);
516 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
517 return got_error(GOT_ERR_NO_SPACE);
519 return NULL;
522 const struct got_error *
523 read_packfile_hdr(int fd, struct got_packidx *packidx)
525 const struct got_error *err = NULL;
526 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
527 struct got_packfile_hdr hdr;
528 ssize_t n;
530 n = read(fd, &hdr, sizeof(hdr));
531 if (n < 0)
532 return got_error_from_errno();
533 if (n != sizeof(hdr))
534 return got_error(GOT_ERR_BAD_PACKFILE);
536 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
537 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
538 betoh32(hdr.nobjects) != totobj)
539 err = got_error(GOT_ERR_BAD_PACKFILE);
541 return err;
544 static const struct got_error *
545 open_packfile(int *fd, const char *path_packfile,
546 struct got_repository *repo, struct got_packidx *packidx)
548 const struct got_error *err = NULL;
550 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
551 if (*fd == -1)
552 return got_error_from_errno();
554 if (packidx) {
555 err = read_packfile_hdr(*fd, packidx);
556 if (err) {
557 close(*fd);
558 *fd = -1;
561 return err;
564 void
565 got_pack_close(struct got_pack *pack)
567 close(pack->fd);
568 pack->fd = -1;
569 free(pack->path_packfile);
570 pack->path_packfile = NULL;
571 pack->filesize = 0;
574 static const struct got_error *
575 cache_pack(struct got_pack **packp, const char *path_packfile,
576 struct got_packidx *packidx, struct got_repository *repo)
578 const struct got_error *err = NULL;
579 struct got_pack *pack = NULL;
580 int i;
582 if (packp)
583 *packp = NULL;
585 for (i = 0; i < nitems(repo->packs); i++) {
586 pack = &repo->packs[i];
587 if (pack->path_packfile == NULL)
588 break;
589 if (strcmp(pack->path_packfile, path_packfile) == 0)
590 return NULL;
593 if (i == nitems(repo->packs) - 1) {
594 got_pack_close(&repo->packs[i - 1]);
595 memmove(&repo->packs[1], &repo->packs[0],
596 sizeof(repo->packs) - sizeof(repo->packs[0]));
597 i = 0;
600 pack = &repo->packs[i];
602 pack->path_packfile = strdup(path_packfile);
603 if (pack->path_packfile == NULL) {
604 err = got_error_from_errno();
605 goto done;
608 err = open_packfile(&pack->fd, path_packfile, repo, packidx);
609 if (err)
610 goto done;
612 err = get_packfile_size(&pack->filesize, path_packfile);
613 done:
614 if (err) {
615 if (pack) {
616 free(pack->path_packfile);
617 memset(pack, 0, sizeof(*pack));
619 } else if (packp)
620 *packp = pack;
621 return err;
624 struct got_pack *
625 get_cached_pack(const char *path_packfile, struct got_repository *repo)
627 struct got_pack *pack = NULL;
628 int i;
630 for (i = 0; i < nitems(repo->packs); i++) {
631 pack = &repo->packs[i];
632 if (pack->path_packfile == NULL)
633 break;
634 if (strcmp(pack->path_packfile, path_packfile) == 0)
635 return pack;
638 return NULL;
641 static const struct got_error *
642 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len, int fd)
644 uint8_t t = 0;
645 uint64_t s = 0;
646 uint8_t sizeN;
647 ssize_t n;
648 int i = 0;
650 do {
651 /* We do not support size values which don't fit in 64 bit. */
652 if (i > 9)
653 return got_error(GOT_ERR_NO_SPACE);
655 n = read(fd, &sizeN, sizeof(sizeN));
656 if (n < 0)
657 return got_error_from_errno();
658 if (n != sizeof(sizeN))
659 return got_error(GOT_ERR_BAD_PACKFILE);
661 if (i == 0) {
662 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
663 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
664 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
665 } else {
666 size_t shift = 4 + 7 * (i - 1);
667 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
669 i++;
670 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
672 *type = t;
673 *size = s;
674 *len = i * sizeof(sizeN);
675 return NULL;
678 static const struct got_error *
679 open_plain_object(struct got_object **obj, const char *path_packfile,
680 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
682 *obj = calloc(1, sizeof(**obj));
683 if (*obj == NULL)
684 return got_error_from_errno();
686 (*obj)->path_packfile = strdup(path_packfile);
687 if ((*obj)->path_packfile == NULL) {
688 const struct got_error *err = got_error_from_errno();
689 free(*obj);
690 *obj = NULL;
691 return err;
694 (*obj)->type = type;
695 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
696 (*obj)->hdrlen = 0;
697 (*obj)->size = size;
698 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
699 (*obj)->pack_offset = offset;
701 return NULL;
704 static const struct got_error *
705 parse_negative_offset(int64_t *offset, size_t *len, int fd)
707 int64_t o = 0;
708 uint8_t offN;
709 ssize_t n;
710 int i = 0;
712 do {
713 /* We do not support offset values which don't fit in 64 bit. */
714 if (i > 8)
715 return got_error(GOT_ERR_NO_SPACE);
717 n = read(fd, &offN, sizeof(offN));
718 if (n < 0)
719 return got_error_from_errno();
720 if (n != sizeof(offN))
721 return got_error(GOT_ERR_BAD_PACKFILE);
723 if (i == 0)
724 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
725 else {
726 o++;
727 o <<= 7;
728 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
730 i++;
731 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
733 *offset = o;
734 *len = i * sizeof(offN);
735 return NULL;
738 static const struct got_error *
739 parse_offset_delta(off_t *base_offset, int fd, off_t offset)
741 const struct got_error *err;
742 int64_t negoffset;
743 size_t negofflen;
745 err = parse_negative_offset(&negoffset, &negofflen, fd);
746 if (err)
747 return err;
749 /* Compute the base object's offset (must be in the same pack file). */
750 *base_offset = (offset - negoffset);
751 if (*base_offset <= 0)
752 return got_error(GOT_ERR_BAD_PACKFILE);
754 return NULL;
757 static const struct got_error *
758 resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
759 int, size_t, const char *, off_t, size_t, int, size_t, unsigned int);
761 static const struct got_error *
762 add_delta(struct got_delta_chain *deltas, const char *path_packfile,
763 off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
764 size_t delta_data_offset, uint8_t *delta_buf, size_t delta_len)
766 struct got_delta *delta;
768 delta = got_delta_open(path_packfile, delta_offset, tslen,
769 delta_type, delta_size, delta_data_offset, delta_buf,
770 delta_len);
771 if (delta == NULL)
772 return got_error_from_errno();
773 /* delta is freed in got_object_close() */
774 deltas->nentries++;
775 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
776 return NULL;
779 static const struct got_error *
780 resolve_offset_delta(struct got_delta_chain *deltas,
781 struct got_repository *repo, int fd, size_t packfile_size,
782 const char *path_packfile, off_t delta_offset, size_t tslen,
783 int delta_type, size_t delta_size, unsigned int recursion)
786 const struct got_error *err;
787 off_t base_offset;
788 uint8_t base_type;
789 uint64_t base_size;
790 size_t base_tslen;
791 off_t delta_data_offset;
792 uint8_t *delta_buf;
793 size_t delta_len;
795 err = parse_offset_delta(&base_offset, fd, delta_offset);
796 if (err)
797 return err;
799 delta_data_offset = lseek(fd, 0, SEEK_CUR);
800 if (delta_data_offset == -1)
801 return got_error_from_errno();
803 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, fd);
804 if (err)
805 return err;
807 err = add_delta(deltas, path_packfile, delta_offset, tslen,
808 delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
809 if (err)
810 return err;
812 /* An offset delta must be in the same packfile. */
813 if (base_offset >= packfile_size)
814 return got_error(GOT_ERR_PACK_OFFSET);
815 if (lseek(fd, base_offset, SEEK_SET) == -1)
816 return got_error_from_errno();
818 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
819 fd);
820 if (err)
821 return err;
823 return resolve_delta_chain(deltas, repo, fd, packfile_size,
824 path_packfile, base_offset, base_tslen, base_type, base_size,
825 recursion - 1);
828 static const struct got_error *
829 resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
830 int fd, const char *path_packfile, off_t delta_offset,
831 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
833 const struct got_error *err;
834 struct got_object_id id;
835 struct got_packidx *packidx;
836 int idx;
837 off_t base_offset;
838 uint8_t base_type;
839 uint64_t base_size;
840 size_t base_tslen;
841 ssize_t n;
842 char *path_base_packfile;
843 struct got_pack *base_pack;
844 off_t delta_data_offset;
845 uint8_t *delta_buf;
846 size_t delta_len;
848 n = read(fd, &id, sizeof(id));
849 if (n < 0)
850 return got_error_from_errno();
851 if (n != sizeof(id))
852 return got_error(GOT_ERR_BAD_PACKFILE);
854 delta_data_offset = lseek(fd, 0, SEEK_CUR);
855 if (delta_data_offset == -1)
856 return got_error_from_errno();
858 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, fd);
859 if (err)
860 return err;
862 err = add_delta(deltas, path_packfile, delta_offset, tslen,
863 delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
864 if (err)
865 return err;
867 err = search_packidx(&packidx, &idx, repo, &id);
868 if (err)
869 return err;
871 base_offset = get_object_offset(packidx, idx);
872 if (base_offset == (uint64_t)-1) {
873 return got_error(GOT_ERR_BAD_PACKIDX);
876 err = get_packfile_path(&path_base_packfile, repo, packidx);
877 if (err)
878 return err;
880 base_pack = get_cached_pack(path_base_packfile, repo);
881 if (base_pack == NULL) {
882 err = cache_pack(&base_pack, path_base_packfile, NULL, repo);
883 if (err)
884 goto done;
887 if (base_offset >= base_pack->filesize) {
888 err = got_error(GOT_ERR_PACK_OFFSET);
889 goto done;
891 if (lseek(base_pack->fd, base_offset, SEEK_SET) == -1) {
892 err = got_error_from_errno();
893 goto done;
896 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
897 base_pack->fd);
898 if (err)
899 goto done;
901 err = resolve_delta_chain(deltas, repo, base_pack->fd,
902 base_pack->filesize, path_base_packfile, base_offset,
903 base_tslen, base_type, base_size, recursion - 1);
904 done:
905 free(path_base_packfile);
906 return err;
909 static const struct got_error *
910 resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
911 int fd, size_t packfile_size, const char *path_packfile, off_t delta_offset,
912 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
914 const struct got_error *err = NULL;
916 if (--recursion == 0)
917 return got_error(GOT_ERR_RECURSION);
919 switch (delta_type) {
920 case GOT_OBJ_TYPE_COMMIT:
921 case GOT_OBJ_TYPE_TREE:
922 case GOT_OBJ_TYPE_BLOB:
923 case GOT_OBJ_TYPE_TAG:
924 /* Plain types are the final delta base. Recursion ends. */
925 err = add_delta(deltas, path_packfile, delta_offset, tslen,
926 delta_type, delta_size, 0, NULL, 0);
927 break;
928 case GOT_OBJ_TYPE_OFFSET_DELTA:
929 err = resolve_offset_delta(deltas, repo, fd, packfile_size,
930 path_packfile, delta_offset, tslen, delta_type, delta_size,
931 recursion - 1);
932 break;
933 case GOT_OBJ_TYPE_REF_DELTA:
934 err = resolve_ref_delta(deltas, repo, fd, path_packfile,
935 delta_offset, tslen, delta_type, delta_size, recursion - 1);
936 break;
937 default:
938 return got_error(GOT_ERR_OBJ_TYPE);
941 return err;
944 static const struct got_error *
945 open_delta_object(struct got_object **obj, struct got_repository *repo,
946 const char *path_packfile, int fd, size_t packfile_size,
947 struct got_object_id *id, off_t offset, size_t tslen,
948 int delta_type, size_t delta_size)
950 const struct got_error *err = NULL;
951 int resolved_type;
953 *obj = calloc(1, sizeof(**obj));
954 if (*obj == NULL)
955 return got_error_from_errno();
957 (*obj)->flags = 0;
958 (*obj)->hdrlen = 0;
959 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
960 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
961 (*obj)->pack_offset = offset + tslen;
963 (*obj)->path_packfile = strdup(path_packfile);
964 if ((*obj)->path_packfile == NULL) {
965 err = got_error_from_errno();
966 goto done;
968 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
970 SIMPLEQ_INIT(&(*obj)->deltas.entries);
971 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
973 err = resolve_delta_chain(&(*obj)->deltas, repo, fd, packfile_size,
974 path_packfile, offset, tslen, delta_type, delta_size,
975 GOT_DELTA_CHAIN_RECURSION_MAX);
976 if (err)
977 goto done;
979 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
980 if (err)
981 goto done;
982 (*obj)->type = resolved_type;
984 done:
985 if (err) {
986 got_object_close(*obj);
987 *obj = NULL;
989 return err;
992 static const struct got_error *
993 open_packed_object(struct got_object **obj, struct got_repository *repo,
994 struct got_packidx *packidx, int idx, struct got_object_id *id)
996 const struct got_error *err = NULL;
997 off_t offset;
998 char *path_packfile;
999 struct got_pack *pack;
1000 uint8_t type;
1001 uint64_t size;
1002 size_t tslen;
1004 *obj = NULL;
1006 offset = get_object_offset(packidx, idx);
1007 if (offset == (uint64_t)-1)
1008 return got_error(GOT_ERR_BAD_PACKIDX);
1010 err = get_packfile_path(&path_packfile, repo, packidx);
1011 if (err)
1012 return err;
1014 pack = get_cached_pack(path_packfile, repo);
1015 if (pack == NULL) {
1016 err = cache_pack(&pack, path_packfile, packidx, repo);
1017 if (err)
1018 goto done;
1021 if (offset >= pack->filesize) {
1022 err = got_error(GOT_ERR_PACK_OFFSET);
1023 goto done;
1025 if (lseek(pack->fd, offset, SEEK_SET) == -1) {
1026 err = got_error_from_errno();
1027 goto done;
1030 err = parse_object_type_and_size(&type, &size, &tslen, pack->fd);
1031 if (err)
1032 goto done;
1034 switch (type) {
1035 case GOT_OBJ_TYPE_COMMIT:
1036 case GOT_OBJ_TYPE_TREE:
1037 case GOT_OBJ_TYPE_BLOB:
1038 case GOT_OBJ_TYPE_TAG:
1039 err = open_plain_object(obj, path_packfile, id, type,
1040 offset + tslen, size);
1041 break;
1043 case GOT_OBJ_TYPE_OFFSET_DELTA:
1044 case GOT_OBJ_TYPE_REF_DELTA:
1045 err = open_delta_object(obj, repo, path_packfile, pack->fd,
1046 pack->filesize, id, offset, tslen, type, size);
1047 break;
1049 default:
1050 err = got_error(GOT_ERR_OBJ_TYPE);
1051 goto done;
1053 done:
1054 free(path_packfile);
1055 return err;
1058 const struct got_error *
1059 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1060 struct got_repository *repo)
1062 const struct got_error *err = NULL;
1063 struct got_packidx *packidx = NULL;
1064 int idx;
1066 err = search_packidx(&packidx, &idx, repo, id);
1067 if (err)
1068 return err;
1070 err = open_packed_object(obj, repo, packidx, idx, id);
1071 if (err)
1072 return err;
1074 err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1075 return err;
1078 static const struct got_error *
1079 get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1081 struct got_delta *delta;
1082 uint64_t base_size = 0, result_size = 0;
1084 *max_size = 0;
1085 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1086 /* Plain object types are the delta base. */
1087 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1088 delta->type != GOT_OBJ_TYPE_TREE &&
1089 delta->type != GOT_OBJ_TYPE_BLOB &&
1090 delta->type != GOT_OBJ_TYPE_TAG) {
1091 const struct got_error *err;
1092 err = got_delta_get_sizes(&base_size, &result_size,
1093 delta->delta_buf, delta->delta_len);
1094 if (err)
1095 return err;
1096 } else
1097 base_size = delta->size;
1098 if (base_size > *max_size)
1099 *max_size = base_size;
1100 if (result_size > *max_size)
1101 *max_size = result_size;
1104 return NULL;
1107 static const struct got_error *
1108 dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1109 FILE *outfile, struct got_repository *repo)
1111 const struct got_error *err = NULL;
1112 struct got_delta *delta;
1113 FILE *base_file = NULL, *accum_file = NULL;
1114 uint8_t *base_buf = NULL, *accum_buf = NULL;
1115 size_t accum_size = 0;
1116 uint64_t max_size;
1117 int n = 0;
1119 *result_size = 0;
1121 if (SIMPLEQ_EMPTY(&deltas->entries))
1122 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1124 /* We process small enough files entirely in memory for speed. */
1125 err = get_delta_chain_max_size(&max_size, deltas);
1126 if (err)
1127 return err;
1128 if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1129 accum_buf = malloc(max_size);
1130 if (accum_buf == NULL)
1131 return got_error_from_errno();
1132 } else {
1133 base_file = got_opentemp();
1134 if (base_file == NULL)
1135 return got_error_from_errno();
1137 accum_file = got_opentemp();
1138 if (accum_file == NULL) {
1139 err = got_error_from_errno();
1140 fclose(base_file);
1141 return err;
1145 /* Deltas are ordered in ascending order. */
1146 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1147 if (n == 0) {
1148 struct got_pack *pack;
1149 size_t base_len;
1150 off_t delta_data_offset;
1152 /* Plain object types are the delta base. */
1153 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1154 delta->type != GOT_OBJ_TYPE_TREE &&
1155 delta->type != GOT_OBJ_TYPE_BLOB &&
1156 delta->type != GOT_OBJ_TYPE_TAG) {
1157 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1158 goto done;
1161 pack = get_cached_pack(delta->path_packfile, repo);
1162 if (pack == NULL) {
1163 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1164 goto done;
1167 delta_data_offset = delta->offset + delta->tslen;
1168 if (delta_data_offset >= pack->filesize) {
1169 err = got_error(GOT_ERR_PACK_OFFSET);
1170 goto done;
1172 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
1173 err = got_error_from_errno();
1174 goto done;
1176 if (base_file)
1177 err = got_inflate_to_file_fd(&base_len,
1178 pack->fd, base_file);
1179 else {
1180 err = got_inflate_to_mem_fd(&base_buf,
1181 &base_len, pack->fd);
1182 if (base_len < max_size) {
1183 uint8_t *p;
1184 p = reallocarray(base_buf, 1, max_size);
1185 if (p == NULL) {
1186 err = got_error_from_errno();
1187 goto done;
1189 base_buf = p;
1192 if (err)
1193 goto done;
1194 n++;
1195 if (base_file)
1196 rewind(base_file);
1197 continue;
1200 if (base_buf) {
1201 err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1202 delta->delta_len, accum_buf, &accum_size);
1203 n++;
1204 } else {
1205 err = got_delta_apply(base_file, delta->delta_buf,
1206 delta->delta_len,
1207 /* Final delta application writes to output file. */
1208 ++n < deltas->nentries ? accum_file : outfile,
1209 &accum_size);
1211 if (err)
1212 goto done;
1214 if (n < deltas->nentries) {
1215 /* Accumulated delta becomes the new base. */
1216 if (base_buf) {
1217 uint8_t *tmp = accum_buf;
1218 accum_buf = base_buf;
1219 base_buf = tmp;
1220 } else {
1221 FILE *tmp = accum_file;
1222 accum_file = base_file;
1223 base_file = tmp;
1224 rewind(base_file);
1225 rewind(accum_file);
1230 done:
1231 free(base_buf);
1232 if (accum_buf) {
1233 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1234 free(accum_buf);
1235 if (len != accum_size)
1236 return got_ferror(outfile, GOT_ERR_IO);
1238 if (base_file)
1239 fclose(base_file);
1240 if (accum_file)
1241 fclose(accum_file);
1242 rewind(outfile);
1243 if (err == NULL)
1244 *result_size = accum_size;
1245 return err;
1248 static const struct got_error *
1249 dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1250 struct got_delta_chain *deltas, struct got_repository *repo)
1252 const struct got_error *err = NULL;
1253 struct got_delta *delta;
1254 uint8_t *base_buf = NULL, *accum_buf = NULL;
1255 size_t accum_size;
1256 uint64_t max_size;
1257 int n = 0;
1259 *outbuf = NULL;
1260 *outlen = 0;
1262 if (SIMPLEQ_EMPTY(&deltas->entries))
1263 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1265 err = get_delta_chain_max_size(&max_size, deltas);
1266 if (err)
1267 return err;
1268 accum_buf = malloc(max_size);
1269 if (accum_buf == NULL)
1270 return got_error_from_errno();
1272 /* Deltas are ordered in ascending order. */
1273 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1274 if (n == 0) {
1275 struct got_pack *pack;
1276 size_t base_len;
1277 size_t delta_data_offset;
1279 /* Plain object types are the delta base. */
1280 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1281 delta->type != GOT_OBJ_TYPE_TREE &&
1282 delta->type != GOT_OBJ_TYPE_BLOB &&
1283 delta->type != GOT_OBJ_TYPE_TAG) {
1284 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1285 goto done;
1288 pack = get_cached_pack(delta->path_packfile, repo);
1289 if (pack == NULL) {
1290 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1291 goto done;
1294 delta_data_offset = delta->offset + delta->tslen;
1295 if (delta_data_offset >= pack->filesize) {
1296 err = got_error(GOT_ERR_PACK_OFFSET);
1297 goto done;
1299 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
1300 err = got_error_from_errno();
1301 goto done;
1303 err = got_inflate_to_mem_fd(&base_buf, &base_len,
1304 pack->fd);
1305 if (base_len < max_size) {
1306 uint8_t *p;
1307 p = reallocarray(base_buf, 1, max_size);
1308 if (p == NULL) {
1309 err = got_error_from_errno();
1310 goto done;
1312 base_buf = p;
1314 if (err)
1315 goto done;
1316 n++;
1317 continue;
1320 err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1321 delta->delta_len, accum_buf, &accum_size);
1322 n++;
1323 if (err)
1324 goto done;
1326 if (n < deltas->nentries) {
1327 /* Accumulated delta becomes the new base. */
1328 uint8_t *tmp = accum_buf;
1329 accum_buf = base_buf;
1330 base_buf = tmp;
1334 done:
1335 free(base_buf);
1336 if (err) {
1337 free(accum_buf);
1338 *outbuf = NULL;
1339 *outlen = 0;
1340 } else {
1341 *outbuf = accum_buf;
1342 *outlen = accum_size;
1344 return err;
1347 const struct got_error *
1348 got_packfile_extract_object(FILE **f, struct got_object *obj,
1349 struct got_repository *repo)
1351 const struct got_error *err = NULL;
1353 *f = NULL;
1355 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1356 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1358 *f = got_opentemp();
1359 if (*f == NULL) {
1360 err = got_error_from_errno();
1361 goto done;
1364 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1365 struct got_pack *pack;
1367 pack = get_cached_pack(obj->path_packfile, repo);
1368 if (pack == NULL) {
1369 err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1370 if (err)
1371 goto done;
1374 if (obj->pack_offset >= pack->filesize) {
1375 err = got_error(GOT_ERR_PACK_OFFSET);
1376 goto done;
1378 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1379 err = got_error_from_errno();
1380 goto done;
1383 err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1384 } else
1385 err = dump_delta_chain_to_file(&obj->size,
1386 &obj->deltas, *f, repo);
1387 done:
1388 if (err && *f) {
1389 fclose(*f);
1390 *f = NULL;
1392 return err;
1395 const struct got_error *
1396 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1397 struct got_object *obj, struct got_repository *repo)
1399 const struct got_error *err = NULL;
1401 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1402 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1404 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1405 struct got_pack *pack;
1407 pack = get_cached_pack(obj->path_packfile, repo);
1408 if (pack == NULL) {
1409 err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1410 if (err)
1411 goto done;
1414 if (obj->pack_offset >= pack->filesize) {
1415 err = got_error(GOT_ERR_PACK_OFFSET);
1416 goto done;
1418 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1419 err = got_error_from_errno();
1420 goto done;
1423 err = got_inflate_to_mem_fd(buf, len, pack->fd);
1424 } else
1425 err = dump_delta_chain_to_mem(buf, len, &obj->deltas, repo);
1426 done:
1427 return err;