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 open_offset_delta_object(struct got_object **obj, struct got_repository *repo,
425 const char *path_packfile, FILE *packfile, struct got_object_id *id,
426 off_t offset, size_t tslen, size_t size)
428 const struct got_error *err = NULL;
431 off_t base_obj_offset;
432 struct got_object *base_obj;
433 struct got_object_id base_id;
438 err = decode_negative_offset(&negoffset, &negofflen, packfile);
442 /* Compute the base object's offset (must be in the same pack file). */
443 base_obj_offset = (offset - negoffset);
444 if (base_obj_offset <= 0)
445 return got_error(GOT_ERR_BAD_PACKFILE);
447 if (fseeko(packfile, base_obj_offset, SEEK_SET) != 0)
448 return got_error_from_errno();
450 err = decode_type_and_size(&base_type, &base_size, &base_tslen,
456 * XXX We currently only support plain objects as a delta base,
457 * i.e. deltas cannot be chained. Is this a problem?
458 * If so, we would have to resolve a plain object base type here.
461 case GOT_OBJ_TYPE_COMMIT:
462 case GOT_OBJ_TYPE_TREE:
463 case GOT_OBJ_TYPE_BLOB:
464 case GOT_OBJ_TYPE_TAG:
466 case GOT_OBJ_TYPE_OFFSET_DELTA:
467 case GOT_OBJ_TYPE_REF_DELTA:
469 return got_error(GOT_ERR_NOT_IMPL);
472 *obj = calloc(1, sizeof(**obj));
474 return got_error(GOT_ERR_NO_MEM);
476 (*obj)->path_packfile = strdup(path_packfile);
477 if ((*obj)->path_packfile == NULL) {
479 return got_error(GOT_ERR_NO_MEM);
481 (*obj)->type = GOT_OBJ_TYPE_OFFSET_DELTA;
482 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
485 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
486 (*obj)->pack_offset = offset + tslen;
487 (*obj)->base_type = base_type;
488 (*obj)->base_size = base_size;
489 (*obj)->base_obj_offset = base_obj_offset;
494 static const struct got_error *
495 open_packed_object(struct got_object **obj, struct got_repository *repo,
496 const char *path_packdir, struct got_packidx_v2_hdr *packidx,
497 struct got_object_id *id)
499 const struct got_error *err = NULL;
500 int idx = get_object_idx(packidx, id);
502 char hex[SHA1_DIGEST_STRING_LENGTH];
511 if (idx == -1) /* object not found in pack index */
514 offset = get_object_offset(packidx, idx);
515 if (offset == (uint64_t)-1)
516 return got_error(GOT_ERR_BAD_PACKIDX);
518 sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
521 return got_error(GOT_ERR_PACKIDX_CSUM);
523 if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
524 GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
525 return got_error(GOT_ERR_NO_MEM);
527 packfile = fopen(path_packfile, "rb");
528 if (packfile == NULL) {
529 err = got_error_from_errno();
533 err = read_packfile_hdr(packfile, packidx);
537 if (fseeko(packfile, offset, SEEK_SET) != 0) {
538 err = got_error_from_errno();
542 err = decode_type_and_size(&type, &size, &tslen, packfile);
547 case GOT_OBJ_TYPE_COMMIT:
548 case GOT_OBJ_TYPE_TREE:
549 case GOT_OBJ_TYPE_BLOB:
550 err = open_plain_object(obj, path_packfile, id, type,
551 offset + tslen, size);
554 case GOT_OBJ_TYPE_OFFSET_DELTA:
555 err = open_offset_delta_object(obj, repo, path_packfile,
556 packfile, id, offset, tslen, size);
559 case GOT_OBJ_TYPE_REF_DELTA:
560 case GOT_OBJ_TYPE_TAG:
563 err = got_error(GOT_ERR_NOT_IMPL);
570 if (packfile && fclose(packfile) == -1 && err == 0)
571 err = got_error_from_errno();
575 const struct got_error *
576 got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
577 struct got_repository *repo)
579 const struct got_error *err = NULL;
582 char *path_packdir = got_repo_get_path_objects_pack(repo);
584 if (path_packdir == NULL) {
585 err = got_error(GOT_ERR_NO_MEM);
589 packdir = opendir(path_packdir);
590 if (packdir == NULL) {
591 err = got_error_from_errno();
595 while ((dent = readdir(packdir)) != NULL) {
596 struct got_packidx_v2_hdr *packidx;
597 char *path_packidx, *path_object;
599 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
602 if (asprintf(&path_packidx, "%s/%s", path_packdir,
603 dent->d_name) == -1) {
604 err = got_error(GOT_ERR_NO_MEM);
608 err = got_packidx_open(&packidx, path_packidx);
613 err = open_packed_object(obj, repo, path_packdir, packidx, id);
614 got_packidx_close(packidx);
623 if (packdir && closedir(packdir) != 0 && err == 0)
624 err = got_error_from_errno();
628 static const struct got_error *
629 dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
635 size_t len = MIN(size, sizeof(data));
637 n = fread(data, len, 1, infile);
639 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
641 n = fwrite(data, len, 1, outfile);
643 return got_ferror(outfile, GOT_ERR_IO);
652 static const struct got_error *
653 dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
654 size_t size, FILE *outfile)
656 const struct got_error *err = NULL;
657 struct got_object_id base_id;
658 struct got_object *base_obj;
661 if (size < sizeof(base_id))
662 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
664 n = fread(&base_id, sizeof(base_id), 1, infile);
666 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
668 size -= sizeof(base_id);
670 return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
672 err = got_object_open(&base_obj, repo, &base_id);
676 err = got_delta_apply(repo, infile, size, base_obj, outfile);
677 got_object_close(base_obj);
681 const struct got_error *
682 got_packfile_extract_object(FILE **f, struct got_object *obj,
683 struct got_repository *repo)
685 const struct got_error *err = NULL;
686 FILE *packfile = NULL;
688 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
689 return got_error(GOT_ERR_OBJ_NOT_PACKED);
693 err = got_error(GOT_ERR_FILE_OPEN);
697 packfile = fopen(obj->path_packfile, "rb");
698 if (packfile == NULL) {
699 err = got_error_from_errno();
703 if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
704 err = got_error_from_errno();
709 case GOT_OBJ_TYPE_COMMIT:
710 case GOT_OBJ_TYPE_TREE:
711 case GOT_OBJ_TYPE_BLOB:
712 err = dump_plain_object(packfile, obj->type, obj->size, *f);
714 case GOT_OBJ_TYPE_REF_DELTA:
715 err = dump_ref_delta_object(repo, packfile, obj->type,
718 case GOT_OBJ_TYPE_TAG:
719 case GOT_OBJ_TYPE_OFFSET_DELTA:
721 err = got_error(GOT_ERR_NOT_IMPL);