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"
35 #include "got_sha1.h"
36 #include "pack.h"
37 #include "path.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))
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 static const struct got_error *
54 verify_fanout_table(uint32_t *fanout_table)
55 {
56 int i;
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);
61 }
63 return NULL;
64 }
66 static const struct got_error *
67 get_packfile_size(size_t *size, const char *path_idx)
68 {
69 struct stat sb;
70 char *path_pack;
71 char base_path[PATH_MAX];
72 char *dot;
74 if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
75 return got_error(GOT_ERR_NO_SPACE);
77 dot = strrchr(base_path, '.');
78 if (dot == NULL)
79 return got_error(GOT_ERR_BAD_PATH);
80 *dot = '\0';
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) {
85 free(path_pack);
86 return got_error(GOT_ERR_IO);
88 }
90 free(path_pack);
91 *size = sb.st_size;
92 return 0;
93 }
95 const struct got_error *
96 got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
97 {
98 struct got_packidx_v2_hdr *p;
99 FILE *f;
100 const struct got_error *err = NULL;
101 size_t n, nobj, packfile_size;
102 SHA1_CTX ctx;
103 uint8_t sha1[SHA1_DIGEST_LENGTH];
105 SHA1Init(&ctx);
107 f = fopen(path, "rb");
108 if (f == NULL)
109 return got_error(GOT_ERR_BAD_PATH);
111 err = get_packfile_size(&packfile_size, path);
112 if (err)
113 return err;
115 p = calloc(1, sizeof(*p));
116 if (p == NULL) {
117 err = got_error(GOT_ERR_NO_MEM);
118 goto done;
121 n = fread(&p->magic, sizeof(p->magic), 1, f);
122 if (n != 1) {
123 err = got_error(ferror(f) ? GOT_ERR_IO : GOT_ERR_BAD_PACKIDX);
124 goto done;
127 if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
128 err = got_error(GOT_ERR_BAD_PACKIDX);
129 goto done;
132 SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
134 n = fread(&p->version, sizeof(p->version), 1, f);
135 if (n != 1) {
136 err = got_error(ferror(f) ? GOT_ERR_IO : GOT_ERR_BAD_PACKIDX);
137 goto done;
140 if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
141 err = got_error(GOT_ERR_BAD_PACKIDX);
142 goto done;
145 SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
147 n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
148 if (n != 1) {
149 err = got_error(ferror(f) ? GOT_ERR_IO : GOT_ERR_BAD_PACKIDX);
150 goto done;
153 err = verify_fanout_table(p->fanout_table);
154 if (err)
155 goto done;
157 SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
159 nobj = betoh32(p->fanout_table[0xff]);
161 p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
162 if (p->sorted_ids == NULL) {
163 err = got_error(GOT_ERR_NO_MEM);
164 goto done;
167 n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
168 if (n != nobj) {
169 err = got_error(ferror(f) ? GOT_ERR_IO : GOT_ERR_BAD_PACKIDX);
170 goto done;
173 SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
174 nobj * sizeof(*p->sorted_ids));
176 p->crc32 = calloc(nobj, sizeof(*p->crc32));
177 if (p->crc32 == NULL) {
178 err = got_error(GOT_ERR_NO_MEM);
179 goto done;
182 n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
183 if (n != nobj) {
184 err = got_error(ferror(f) ? GOT_ERR_IO : GOT_ERR_BAD_PACKIDX);
185 goto done;
188 SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
190 p->offsets = calloc(nobj, sizeof(*p->offsets));
191 if (p->offsets == NULL) {
192 err = got_error(GOT_ERR_NO_MEM);
193 goto done;
196 n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
197 if (n != nobj) {
198 err = got_error(ferror(f) ? GOT_ERR_IO : GOT_ERR_BAD_PACKIDX);
199 goto done;
202 SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
204 /* Large file offsets are contained only in files > 2GB. */
205 if (packfile_size <= 0x80000000)
206 goto checksum;
208 p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
209 if (p->large_offsets == NULL) {
210 err = got_error(GOT_ERR_NO_MEM);
211 goto done;
214 n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
215 if (n != nobj) {
216 err = got_error(ferror(f) ? GOT_ERR_IO : GOT_ERR_BAD_PACKIDX);
217 goto done;
220 SHA1Update(&ctx, (uint8_t*)p->large_offsets,
221 nobj * sizeof(*p->large_offsets));
223 checksum:
224 n = fread(&p->trailer, sizeof(p->trailer), 1, f);
225 if (n != 1) {
226 err = got_error(ferror(f) ? GOT_ERR_IO : GOT_ERR_BAD_PACKIDX);
227 goto done;
230 SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
231 SHA1Final(sha1, &ctx);
232 if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
233 err = got_error(GOT_ERR_PACKIDX_CSUM);
234 done:
235 fclose(f);
236 if (err)
237 got_packidx_close(p);
238 else
239 *packidx = p;
240 return err;
243 void
244 got_packidx_close(struct got_packidx_v2_hdr *packidx)
246 free(packidx->sorted_ids);
247 free(packidx->offsets);
248 free(packidx->crc32);
249 free(packidx->large_offsets);
250 free(packidx);
253 static int
254 is_packidx_filename(const char *name, size_t len)
256 if (len != GOT_PACKIDX_NAMELEN)
257 return 0;
259 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
260 return 0;
262 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
263 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
264 return 0;
266 return 1;
269 static off_t
270 get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
272 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
273 uint32_t offset = betoh32(packidx->offsets[idx]);
274 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
275 uint64_t loffset;
276 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
277 if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
278 return -1;
279 loffset = betoh64(packidx->large_offsets[idx]);
280 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
282 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
285 static int
286 get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
288 u_int8_t id0 = id->sha1[0];
289 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
290 int i = 0;
292 if (id0 > 0)
293 i = betoh32(packidx->fanout_table[id0 - 1]);
295 while (i < totobj) {
296 struct got_object_id *oid = &packidx->sorted_ids[i++];
297 uint32_t offset;
299 if (got_object_id_cmp(id, oid) < 0)
300 continue;
301 if (got_object_id_cmp(id, oid) > 0)
302 break;
304 return i;
307 return -1;
310 const struct got_error *
311 read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
313 const struct got_error *err = NULL;
314 uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
315 struct got_packfile_hdr hdr;
316 size_t n;
318 n = fread(&hdr, sizeof(hdr), 1, f);
319 if (n != 1)
320 return got_error(ferror(f) ? GOT_ERR_IO : GOT_ERR_BAD_PACKFILE);
322 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
323 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
324 betoh32(hdr.nobjects) != totobj)
325 err = got_error(GOT_ERR_BAD_PACKFILE);
327 return err;
330 static const struct got_error *
331 dump_packed_object(FILE **f, FILE *packfile, off_t offset)
333 const struct got_error *err = NULL;
334 const char *template = "/tmp/got.XXXXXXXXXX";
335 uint64_t size = 0;
336 uint8_t type = 0;
337 uint8_t sizeN;
338 int i;
339 size_t n;
340 const char *type_tag;
342 *f = got_opentemp();
343 if (*f == NULL) {
344 err = got_error(GOT_ERR_FILE_OPEN);
345 goto done;
348 if (fseeko(packfile, offset, SEEK_SET) != 0) {
349 err = got_error(errno == EIO ? GOT_ERR_IO : GOT_ERR_BAD_PATH);
350 goto done;
353 i = 0;
354 do {
355 /* We do not support size values which don't fit in 64 bit. */
356 if (i > 9) {
357 err = got_error(GOT_ERR_NO_SPACE);
358 goto done;
361 n = fread(&sizeN, sizeof(sizeN), 1, packfile);
362 if (n != 1) {
363 err = got_error(ferror(packfile) ?
364 GOT_ERR_IO : GOT_ERR_BAD_PACKFILE);
365 goto done;
367 if (i == 0) {
368 type = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
369 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
370 size = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
371 } else {
372 size_t shift = 4 + 7 * (i - 1);
373 size |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
375 i++;
376 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
378 if (type == GOT_OBJ_TYPE_OFFSET_DELTA)
379 printf("object type OFFSET_DELTA not yet implemented\n");
380 else if (type == GOT_OBJ_TYPE_REF_DELTA)
381 printf("object type REF_DELTA not yet implemented\n");
382 else if (type == GOT_OBJ_TYPE_TAG)
383 printf("object type TAG not yet implemented\n");
385 type_tag = got_object_get_type_tag(type);
386 if (type_tag == NULL) {
387 err = got_error(GOT_ERR_BAD_OBJ_HDR);
388 goto done;
391 fprintf(*f, "%s %llu", type_tag, size);
392 fputc('\0', *f);
394 while (size > 0) {
395 uint8_t data[2048];
396 size_t len = MIN(size, sizeof(data));
398 n = fread(data, len, 1, packfile);
399 if (n != 1) {
400 err = got_error(ferror(packfile) ?
401 GOT_ERR_IO : GOT_ERR_BAD_PACKFILE);
402 goto done;
405 n = fwrite(data, len, 1, *f);
406 if (n != 1) {
407 err = got_error(ferror(*f) ?
408 GOT_ERR_IO : GOT_ERR_BAD_PACKFILE);
409 goto done;
412 size -= len;
415 printf("object type is %d\n", type);
416 rewind(*f);
417 done:
418 if (err && *f)
419 fclose(*f);
420 return err;
422 static const struct got_error *
423 extract_object(FILE **f, const char *path_packdir,
424 struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
426 const struct got_error *err = NULL;
427 int idx = get_object_idx(packidx, id);
428 off_t offset;
429 char *path_packfile;
430 FILE *packfile;
431 char hex[SHA1_DIGEST_STRING_LENGTH];
432 char *sha1str;
434 *f = NULL;
435 if (idx == -1) /* object not found in pack index */
436 return NULL;
438 offset = get_object_offset(packidx, idx);
439 if (offset == (uint64_t)-1)
440 return got_error(GOT_ERR_BAD_PACKIDX);
442 sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
443 hex, sizeof(hex));
444 if (sha1str == NULL)
445 return got_error(GOT_ERR_PACKIDX_CSUM);
447 if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
448 GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
449 return got_error(GOT_ERR_NO_MEM);
451 packfile = fopen(path_packfile, "rb");
452 if (packfile == NULL) {
453 err = got_error(errno == EIO ? GOT_ERR_IO : GOT_ERR_BAD_PATH);
454 goto done;
457 err = read_packfile_hdr(packfile, packidx);
458 if (err)
459 goto done;
461 printf("Dumping object at offset %llu\n", offset);
462 err = dump_packed_object(f, packfile, offset);
463 if (err)
464 goto done;
466 done:
467 free(path_packfile);
468 if (packfile && fclose(packfile) == -1 && errno == EIO && err == 0)
469 err = got_error(GOT_ERR_IO);
470 return err;
473 const struct got_error *
474 got_packfile_extract_object(FILE **f, struct got_object_id *id,
475 struct got_repository *repo)
477 const struct got_error *err = NULL;
478 DIR *packdir = NULL;
479 struct dirent *dent;
480 char *path_packdir = got_repo_get_path_objects_pack(repo);
482 if (path_packdir == NULL) {
483 err = got_error(GOT_ERR_NO_MEM);
484 goto done;
487 packdir = opendir(path_packdir);
488 if (packdir == NULL) {
489 err = got_error(errno == EIO ? GOT_ERR_IO : GOT_ERR_BAD_PATH);
490 goto done;
493 while ((dent = readdir(packdir)) != NULL) {
494 struct got_packidx_v2_hdr *packidx;
495 char *path_packidx, *path_object;
497 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
498 continue;
500 if (asprintf(&path_packidx, "%s/%s", path_packdir,
501 dent->d_name) == -1) {
502 err = got_error(GOT_ERR_NO_MEM);
503 goto done;
506 err = got_packidx_open(&packidx, path_packidx);
507 free(path_packidx);
508 if (err)
509 goto done;
511 err = extract_object(f, path_packdir, packidx, id);
512 if (err)
513 goto done;
514 if (*f != NULL)
515 break;
518 done:
519 free(path_packdir);
520 if (packdir && closedir(packdir) != 0 && errno == EIO && err == 0)
521 err = got_error(GOT_ERR_IO);
522 return err;