2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
41 #define GOT_PACK_PREFIX "pack-"
42 #define GOT_PACKFILE_SUFFIX ".pack"
43 #define GOT_PACKIDX_SUFFIX ".idx"
44 #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
45 SHA1_DIGEST_STRING_LENGTH - 1 + \
46 strlen(GOT_PACKFILE_SUFFIX))
47 #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
48 SHA1_DIGEST_STRING_LENGTH - 1 + \
49 strlen(GOT_PACKIDX_SUFFIX))
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 static const struct got_error *
56 verify_fanout_table(uint32_t *fanout_table)
60 for (i = 0; i < 0xff - 1; i++) {
61 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
62 return got_error(GOT_ERR_BAD_PACKIDX);
68 static const struct got_error *
69 get_packfile_size(size_t *size, const char *path_idx)
73 char base_path[PATH_MAX];
76 if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
77 return got_error(GOT_ERR_NO_SPACE);
79 dot = strrchr(base_path, '.');
81 return got_error(GOT_ERR_BAD_PATH);
83 if (asprintf(&path_pack, "%s.pack", base_path) == -1)
84 return got_error(GOT_ERR_NO_MEM);
86 if (stat(path_pack, &sb) != 0) {
88 return got_error_from_errno();
96 const struct got_error *
97 got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
99 struct got_packidx_v2_hdr *p;
101 const struct got_error *err = NULL;
102 size_t n, nobj, packfile_size;
104 uint8_t sha1[SHA1_DIGEST_LENGTH];
108 f = fopen(path, "rb");
110 return got_error(GOT_ERR_BAD_PATH);
112 err = get_packfile_size(&packfile_size, path);
116 p = calloc(1, sizeof(*p));
118 err = got_error(GOT_ERR_NO_MEM);
122 n = fread(&p->magic, sizeof(p->magic), 1, f);
124 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
128 if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
129 err = got_error(GOT_ERR_BAD_PACKIDX);
133 SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
135 n = fread(&p->version, sizeof(p->version), 1, f);
137 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
141 if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
142 err = got_error(GOT_ERR_BAD_PACKIDX);
146 SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
148 n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
150 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
154 err = verify_fanout_table(p->fanout_table);
158 SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
160 nobj = betoh32(p->fanout_table[0xff]);
162 p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
163 if (p->sorted_ids == NULL) {
164 err = got_error(GOT_ERR_NO_MEM);
168 n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
170 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
174 SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
175 nobj * sizeof(*p->sorted_ids));
177 p->crc32 = calloc(nobj, sizeof(*p->crc32));
178 if (p->crc32 == NULL) {
179 err = got_error(GOT_ERR_NO_MEM);
183 n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
185 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
189 SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
191 p->offsets = calloc(nobj, sizeof(*p->offsets));
192 if (p->offsets == NULL) {
193 err = got_error(GOT_ERR_NO_MEM);
197 n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
199 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
203 SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
205 /* Large file offsets are contained only in files > 2GB. */
206 if (packfile_size <= 0x80000000)
209 p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
210 if (p->large_offsets == NULL) {
211 err = got_error(GOT_ERR_NO_MEM);
215 n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
217 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
221 SHA1Update(&ctx, (uint8_t*)p->large_offsets,
222 nobj * sizeof(*p->large_offsets));
225 n = fread(&p->trailer, sizeof(p->trailer), 1, f);
227 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
231 SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
232 SHA1Final(sha1, &ctx);
233 if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
234 err = got_error(GOT_ERR_PACKIDX_CSUM);
238 got_packidx_close(p);
245 got_packidx_close(struct got_packidx_v2_hdr *packidx)
247 free(packidx->sorted_ids);
248 free(packidx->offsets);
249 free(packidx->crc32);
250 free(packidx->large_offsets);
255 is_packidx_filename(const char *name, size_t len)
257 if (len != GOT_PACKIDX_NAMELEN)
260 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
263 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
264 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
271 get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
273 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
274 uint32_t offset = betoh32(packidx->offsets[idx]);
275 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
277 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
278 if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
280 loffset = betoh64(packidx->large_offsets[idx]);
281 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
283 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
287 get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
289 u_int8_t id0 = id->sha1[0];
290 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
294 i = betoh32(packidx->fanout_table[id0 - 1]);
297 struct got_object_id *oid = &packidx->sorted_ids[i];
299 int cmp = got_object_id_cmp(id, oid);
311 const struct got_error *
312 read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
314 const struct got_error *err = NULL;
315 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
316 struct got_packfile_hdr hdr;
319 n = fread(&hdr, sizeof(hdr), 1, f);
321 return got_ferror(f, GOT_ERR_BAD_PACKIDX);
323 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
324 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
325 betoh32(hdr.nobjects) != totobj)
326 err = got_error(GOT_ERR_BAD_PACKFILE);
331 static const struct got_error *
332 decode_type_and_size(uint8_t *type, uint64_t *size, size_t *len, FILE *packfile)
341 /* We do not support size values which don't fit in 64 bit. */
343 return got_error(GOT_ERR_NO_SPACE);
345 n = fread(&sizeN, sizeof(sizeN), 1, packfile);
347 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
350 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
351 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
352 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
354 size_t shift = 4 + 7 * (i - 1);
355 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
358 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
362 *len = i * sizeof(sizeN);
366 static const struct got_error *
367 open_plain_object(struct got_object **obj, const char *path_packfile,
368 struct got_object_id *id, uint8_t type, off_t offset, size_t size)
370 *obj = calloc(1, sizeof(**obj));
372 return got_error(GOT_ERR_NO_MEM);
374 (*obj)->path_packfile = strdup(path_packfile);
375 if ((*obj)->path_packfile == NULL) {
378 return got_error(GOT_ERR_NO_MEM);
382 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
385 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
386 (*obj)->pack_offset = offset;
391 static const struct got_error *
392 decode_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
400 /* We do not support offset values which don't fit in 64 bit. */
402 return got_error(GOT_ERR_NO_SPACE);
404 n = fread(&offN, sizeof(offN), 1, packfile);
406 return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
409 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
413 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
416 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
419 *len = i * sizeof(offN);
423 static const struct got_error *
424 parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
426 const struct got_error *err;
430 err = decode_negative_offset(&negoffset, &negofflen, packfile);
434 /* Compute the base object's offset (must be in the same pack file). */
435 *base_offset = (offset - negoffset);
436 if (*base_offset <= 0)
437 return got_error(GOT_ERR_BAD_PACKFILE);
442 static const struct got_error *resolve_delta_chain(struct got_delta_chain *,
443 FILE *, const char *, off_t, size_t);
445 static const struct got_error *
446 resolve_offset_delta(struct got_delta_chain *deltas, FILE *packfile,
447 const char *path_packfile, off_t offset, size_t delta_size)
449 const struct got_error *err;
452 err = parse_offset_delta(&next_offset, packfile, offset);
456 /* Next offset must be in the same packfile. */
457 return resolve_delta_chain(deltas, packfile, path_packfile,
458 next_offset, delta_size);
461 static const struct got_error *
462 resolve_delta_chain(struct got_delta_chain *deltas, FILE *packfile,
463 const char *path_packfile, off_t offset, size_t delta_size)
465 const struct got_error *err = NULL;
469 struct got_delta_base *base;
471 if (fseeko(packfile, offset, SEEK_SET) != 0)
472 return got_error_from_errno();
474 err = decode_type_and_size(&base_type, &base_size, &base_tslen,
479 base = got_delta_base_open(path_packfile, base_type, offset,
482 return got_error(GOT_ERR_NO_MEM);
484 SIMPLEQ_INSERT_TAIL(&deltas->entries, base, entry);
485 /* In case of error below, base will be freed in got_object_close(). */
488 case GOT_OBJ_TYPE_COMMIT:
489 case GOT_OBJ_TYPE_TREE:
490 case GOT_OBJ_TYPE_BLOB:
491 case GOT_OBJ_TYPE_TAG:
493 case GOT_OBJ_TYPE_OFFSET_DELTA:
494 err = resolve_offset_delta(deltas, packfile, path_packfile,
497 case GOT_OBJ_TYPE_REF_DELTA:
499 return got_error(GOT_ERR_NOT_IMPL);
505 static const struct got_error *
506 open_offset_delta_object(struct got_object **obj,
507 struct got_repository *repo, struct got_packidx_v2_hdr *packidx,
508 const char *path_packfile, FILE *packfile, struct got_object_id *id,
509 off_t offset, size_t tslen, size_t size)
511 const struct got_error *err = NULL;
513 struct got_object_id base_id;
519 err = parse_offset_delta(&base_offset, packfile, offset);
523 *obj = calloc(1, sizeof(**obj));
525 return got_error(GOT_ERR_NO_MEM);
529 (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
530 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
531 (*obj)->pack_offset = offset + tslen;
533 (*obj)->path_packfile = strdup(path_packfile);
534 if ((*obj)->path_packfile == NULL) {
535 err = got_error(GOT_ERR_NO_MEM);
538 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
540 SIMPLEQ_INIT(&(*obj)->deltas.entries);
541 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
542 err = resolve_delta_chain(&(*obj)->deltas, packfile, path_packfile,
547 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
550 (*obj)->type = resolved_type;
554 got_object_close(*obj);
560 static const struct got_error *
561 open_packed_object(struct got_object **obj, struct got_repository *repo,
562 const char *path_packdir, struct got_packidx_v2_hdr *packidx,
563 struct got_object_id *id)
565 const struct got_error *err = NULL;
566 int idx = get_object_idx(packidx, id);
568 char hex[SHA1_DIGEST_STRING_LENGTH];
577 if (idx == -1) /* object not found in pack index */
580 offset = get_object_offset(packidx, idx);
581 if (offset == (uint64_t)-1)
582 return got_error(GOT_ERR_BAD_PACKIDX);
584 sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
587 return got_error(GOT_ERR_PACKIDX_CSUM);
589 if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
590 GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
591 return got_error(GOT_ERR_NO_MEM);
593 packfile = fopen(path_packfile, "rb");
594 if (packfile == NULL) {
595 err = got_error_from_errno();
599 err = read_packfile_hdr(packfile, packidx);
603 if (fseeko(packfile, offset, SEEK_SET) != 0) {
604 err = got_error_from_errno();
608 err = decode_type_and_size(&type, &size, &tslen, packfile);
613 case GOT_OBJ_TYPE_COMMIT:
614 case GOT_OBJ_TYPE_TREE:
615 case GOT_OBJ_TYPE_BLOB:
616 err = open_plain_object(obj, path_packfile, id, type,
617 offset + tslen, size);
620 case GOT_OBJ_TYPE_OFFSET_DELTA:
621 err = open_offset_delta_object(obj, repo, packidx,
622 path_packfile, packfile, id, offset, tslen, size);
625 case GOT_OBJ_TYPE_REF_DELTA:
626 case GOT_OBJ_TYPE_TAG:
628 err = got_error(GOT_ERR_NOT_IMPL);
633 if (packfile && fclose(packfile) == -1 && err == 0)
634 err = got_error_from_errno();
638 const struct got_error *
639 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
640 struct got_repository *repo)
642 const struct got_error *err = NULL;
645 char *path_packdir = got_repo_get_path_objects_pack(repo);
647 if (path_packdir == NULL) {
648 err = got_error(GOT_ERR_NO_MEM);
652 packdir = opendir(path_packdir);
653 if (packdir == NULL) {
654 err = got_error_from_errno();
658 while ((dent = readdir(packdir)) != NULL) {
659 struct got_packidx_v2_hdr *packidx;
660 char *path_packidx, *path_object;
662 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
665 if (asprintf(&path_packidx, "%s/%s", path_packdir,
666 dent->d_name) == -1) {
667 err = got_error(GOT_ERR_NO_MEM);
671 err = got_packidx_open(&packidx, path_packidx);
676 err = open_packed_object(obj, repo, path_packdir, packidx, id);
677 got_packidx_close(packidx);
686 if (packdir && closedir(packdir) != 0 && err == 0)
687 err = got_error_from_errno();
691 static const struct got_error *
692 dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
698 size_t len = MIN(size, sizeof(data));
700 n = fread(data, len, 1, infile);
702 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
704 n = fwrite(data, len, 1, outfile);
706 return got_ferror(outfile, GOT_ERR_IO);
715 static const struct got_error *
716 dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
717 size_t size, FILE *outfile)
719 const struct got_error *err = NULL;
720 struct got_object_id base_id;
721 struct got_object *base_obj;
724 if (size < sizeof(base_id))
725 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
727 n = fread(&base_id, sizeof(base_id), 1, infile);
729 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
731 size -= sizeof(base_id);
733 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
735 err = got_object_open(&base_obj, repo, &base_id);
739 err = got_delta_apply(repo, infile, size, base_obj, outfile);
740 got_object_close(base_obj);
744 const struct got_error *
745 got_packfile_extract_object(FILE **f, struct got_object *obj,
746 struct got_repository *repo)
748 const struct got_error *err = NULL;
749 FILE *packfile = NULL;
751 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
752 return got_error(GOT_ERR_OBJ_NOT_PACKED);
756 err = got_error(GOT_ERR_FILE_OPEN);
760 packfile = fopen(obj->path_packfile, "rb");
761 if (packfile == NULL) {
762 err = got_error_from_errno();
766 if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
767 err = got_error_from_errno();
772 case GOT_OBJ_TYPE_COMMIT:
773 case GOT_OBJ_TYPE_TREE:
774 case GOT_OBJ_TYPE_BLOB:
775 err = dump_plain_object(packfile, obj->type, obj->size, *f);
777 case GOT_OBJ_TYPE_REF_DELTA:
778 err = dump_ref_delta_object(repo, packfile, obj->type,
781 case GOT_OBJ_TYPE_TAG:
782 case GOT_OBJ_TYPE_OFFSET_DELTA:
784 err = got_error(GOT_ERR_NOT_IMPL);