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"
39 #define GOT_PACK_PREFIX "pack-"
40 #define GOT_PACKFILE_SUFFIX ".pack"
41 #define GOT_PACKIDX_SUFFIX ".idx"
42 #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
43 SHA1_DIGEST_STRING_LENGTH - 1 + \
44 strlen(GOT_PACKFILE_SUFFIX))
45 #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
46 SHA1_DIGEST_STRING_LENGTH - 1 + \
47 strlen(GOT_PACKIDX_SUFFIX))
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 static const struct got_error *
54 verify_fanout_table(uint32_t *fanout_table)
58 for (i = 0; i < 0xff - 1; i++) {
59 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
60 return got_error(GOT_ERR_BAD_PACKIDX);
66 static const struct got_error *
67 get_packfile_size(size_t *size, const char *path_idx)
71 char base_path[PATH_MAX];
74 if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
75 return got_error(GOT_ERR_NO_SPACE);
77 dot = strrchr(base_path, '.');
79 return got_error(GOT_ERR_BAD_PATH);
81 if (asprintf(&path_pack, "%s.pack", base_path) == -1)
82 return got_error(GOT_ERR_NO_MEM);
84 if (stat(path_pack, &sb) != 0) {
86 return got_error_from_errno();
94 const struct got_error *
95 got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
97 struct got_packidx_v2_hdr *p;
99 const struct got_error *err = NULL;
100 size_t n, nobj, packfile_size;
102 uint8_t sha1[SHA1_DIGEST_LENGTH];
106 f = fopen(path, "rb");
108 return got_error(GOT_ERR_BAD_PATH);
110 err = get_packfile_size(&packfile_size, path);
114 p = calloc(1, sizeof(*p));
116 err = got_error(GOT_ERR_NO_MEM);
120 n = fread(&p->magic, sizeof(p->magic), 1, f);
122 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
126 if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
127 err = got_error(GOT_ERR_BAD_PACKIDX);
131 SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
133 n = fread(&p->version, sizeof(p->version), 1, f);
135 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
139 if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
140 err = got_error(GOT_ERR_BAD_PACKIDX);
144 SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
146 n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
148 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
152 err = verify_fanout_table(p->fanout_table);
156 SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
158 nobj = betoh32(p->fanout_table[0xff]);
160 p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
161 if (p->sorted_ids == NULL) {
162 err = got_error(GOT_ERR_NO_MEM);
166 n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
168 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
172 SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
173 nobj * sizeof(*p->sorted_ids));
175 p->crc32 = calloc(nobj, sizeof(*p->crc32));
176 if (p->crc32 == NULL) {
177 err = got_error(GOT_ERR_NO_MEM);
181 n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
183 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
187 SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
189 p->offsets = calloc(nobj, sizeof(*p->offsets));
190 if (p->offsets == NULL) {
191 err = got_error(GOT_ERR_NO_MEM);
195 n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
197 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
201 SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
203 /* Large file offsets are contained only in files > 2GB. */
204 if (packfile_size <= 0x80000000)
207 p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
208 if (p->large_offsets == NULL) {
209 err = got_error(GOT_ERR_NO_MEM);
213 n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
215 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
219 SHA1Update(&ctx, (uint8_t*)p->large_offsets,
220 nobj * sizeof(*p->large_offsets));
223 n = fread(&p->trailer, sizeof(p->trailer), 1, f);
225 err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
229 SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
230 SHA1Final(sha1, &ctx);
231 if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
232 err = got_error(GOT_ERR_PACKIDX_CSUM);
236 got_packidx_close(p);
243 got_packidx_close(struct got_packidx_v2_hdr *packidx)
245 free(packidx->sorted_ids);
246 free(packidx->offsets);
247 free(packidx->crc32);
248 free(packidx->large_offsets);
253 is_packidx_filename(const char *name, size_t len)
255 if (len != GOT_PACKIDX_NAMELEN)
258 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
261 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
262 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
269 get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
271 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
272 uint32_t offset = betoh32(packidx->offsets[idx]);
273 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
275 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
276 if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
278 loffset = betoh64(packidx->large_offsets[idx]);
279 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
281 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
285 get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
287 u_int8_t id0 = id->sha1[0];
288 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
292 i = betoh32(packidx->fanout_table[id0 - 1]);
295 struct got_object_id *oid = &packidx->sorted_ids[i++];
298 if (got_object_id_cmp(id, oid) < 0)
300 if (got_object_id_cmp(id, oid) > 0)
309 const struct got_error *
310 read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
312 const struct got_error *err = NULL;
313 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
314 struct got_packfile_hdr hdr;
317 n = fread(&hdr, sizeof(hdr), 1, f);
319 return got_ferror(f, GOT_ERR_BAD_PACKIDX);
321 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
322 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
323 betoh32(hdr.nobjects) != totobj)
324 err = got_error(GOT_ERR_BAD_PACKFILE);
329 static const struct got_error *
330 dump_packed_object(FILE **f, FILE *packfile, off_t offset)
332 const struct got_error *err = NULL;
333 const char *template = "/tmp/got.XXXXXXXXXX";
339 const char *type_tag;
343 err = got_error(GOT_ERR_FILE_OPEN);
347 if (fseeko(packfile, offset, SEEK_SET) != 0) {
348 err = got_error_from_errno();
354 /* We do not support size values which don't fit in 64 bit. */
356 err = got_error(GOT_ERR_NO_SPACE);
360 n = fread(&sizeN, sizeof(sizeN), 1, packfile);
362 err = got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
367 type = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
368 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
369 size = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
371 size_t shift = 4 + 7 * (i - 1);
372 size |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
375 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
377 if (type == GOT_OBJ_TYPE_OFFSET_DELTA)
378 printf("object type OFFSET_DELTA not yet implemented\n");
379 else if (type == GOT_OBJ_TYPE_REF_DELTA)
380 printf("object type REF_DELTA not yet implemented\n");
381 else if (type == GOT_OBJ_TYPE_TAG)
382 printf("object type TAG not yet implemented\n");
384 type_tag = got_object_get_type_tag(type);
385 if (type_tag == NULL) {
386 err = got_error(GOT_ERR_BAD_OBJ_HDR);
390 fprintf(*f, "%s %llu", type_tag, size);
395 size_t len = MIN(size, sizeof(data));
397 n = fread(data, len, 1, packfile);
399 err = got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
403 n = fwrite(data, len, 1, *f);
405 err = got_ferror(*f, GOT_ERR_BAD_PACKIDX);
412 printf("object type is %d\n", type);
419 static const struct got_error *
420 extract_object(FILE **f, const char *path_packdir,
421 struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
423 const struct got_error *err = NULL;
424 int idx = get_object_idx(packidx, id);
428 char hex[SHA1_DIGEST_STRING_LENGTH];
432 if (idx == -1) /* object not found in pack index */
435 offset = get_object_offset(packidx, idx);
436 if (offset == (uint64_t)-1)
437 return got_error(GOT_ERR_BAD_PACKIDX);
439 sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
442 return got_error(GOT_ERR_PACKIDX_CSUM);
444 if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
445 GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
446 return got_error(GOT_ERR_NO_MEM);
448 packfile = fopen(path_packfile, "rb");
449 if (packfile == NULL) {
450 err = got_error_from_errno();
454 err = read_packfile_hdr(packfile, packidx);
458 printf("Dumping object at offset %llu\n", offset);
459 err = dump_packed_object(f, packfile, offset);
465 if (packfile && fclose(packfile) == -1 && err == 0)
466 err = got_error_from_errno();
470 const struct got_error *
471 got_packfile_extract_object(FILE **f, struct got_object_id *id,
472 struct got_repository *repo)
474 const struct got_error *err = NULL;
477 char *path_packdir = got_repo_get_path_objects_pack(repo);
479 if (path_packdir == NULL) {
480 err = got_error(GOT_ERR_NO_MEM);
484 packdir = opendir(path_packdir);
485 if (packdir == NULL) {
486 err = got_error_from_errno();
490 while ((dent = readdir(packdir)) != NULL) {
491 struct got_packidx_v2_hdr *packidx;
492 char *path_packidx, *path_object;
494 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
497 if (asprintf(&path_packidx, "%s/%s", path_packdir,
498 dent->d_name) == -1) {
499 err = got_error(GOT_ERR_NO_MEM);
503 err = got_packidx_open(&packidx, path_packidx);
508 err = extract_object(f, path_packdir, packidx, id);
517 if (packdir && closedir(packdir) != 0 && err == 0)
518 err = got_error_from_errno();