2 * Copyright (c) 2018, 2019, 2020 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.
16 #include "got_compat.h"
18 #include <sys/types.h>
20 #include <sys/queue.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
36 #include "got_error.h"
37 #include "got_object.h"
40 #include "got_lib_hash.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_delta_cache.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_qid.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
51 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 static const struct got_error
*
59 verify_fanout_table(uint32_t *fanout_table
)
63 for (i
= 0; i
< 0xff - 1; i
++) {
64 if (be32toh(fanout_table
[i
]) > be32toh(fanout_table
[i
+ 1]))
65 return got_error(GOT_ERR_BAD_PACKIDX
);
71 const struct got_error
*
72 got_packidx_init_hdr(struct got_packidx
*p
, int verify
, off_t packfile_size
)
74 const struct got_error
*err
= NULL
;
75 enum got_hash_algorithm algo
= GOT_HASH_SHA1
;
76 struct got_packidx_v2_hdr
*h
;
78 uint8_t hash
[GOT_HASH_DIGEST_MAXLEN
];
79 size_t nobj
, len_fanout
, len_ids
, offset
, remain
;
83 got_hash_init(&ctx
, algo
);
89 if (remain
< sizeof(*h
->magic
)) {
90 err
= got_error(GOT_ERR_BAD_PACKIDX
);
94 h
->magic
= (uint32_t *)(p
->map
+ offset
);
96 h
->magic
= malloc(sizeof(*h
->magic
));
97 if (h
->magic
== NULL
) {
98 err
= got_error_from_errno("malloc");
101 n
= read(p
->fd
, h
->magic
, sizeof(*h
->magic
));
103 err
= got_error_from_errno("read");
105 } else if (n
!= sizeof(*h
->magic
)) {
106 err
= got_error(GOT_ERR_BAD_PACKIDX
);
110 if (*h
->magic
!= htobe32(GOT_PACKIDX_V2_MAGIC
)) {
111 err
= got_error(GOT_ERR_BAD_PACKIDX
);
114 offset
+= sizeof(*h
->magic
);
115 remain
-= sizeof(*h
->magic
);
118 got_hash_update(&ctx
, h
->magic
, sizeof(*h
->magic
));
120 if (remain
< sizeof(*h
->version
)) {
121 err
= got_error(GOT_ERR_BAD_PACKIDX
);
125 h
->version
= (uint32_t *)(p
->map
+ offset
);
127 h
->version
= malloc(sizeof(*h
->version
));
128 if (h
->version
== NULL
) {
129 err
= got_error_from_errno("malloc");
132 n
= read(p
->fd
, h
->version
, sizeof(*h
->version
));
134 err
= got_error_from_errno("read");
136 } else if (n
!= sizeof(*h
->version
)) {
137 err
= got_error(GOT_ERR_BAD_PACKIDX
);
141 if (*h
->version
!= htobe32(GOT_PACKIDX_VERSION
)) {
142 err
= got_error(GOT_ERR_BAD_PACKIDX
);
145 offset
+= sizeof(*h
->version
);
146 remain
-= sizeof(*h
->version
);
149 got_hash_update(&ctx
, h
->version
, sizeof(*h
->version
));
152 sizeof(*h
->fanout_table
) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS
;
153 if (remain
< len_fanout
) {
154 err
= got_error(GOT_ERR_BAD_PACKIDX
);
158 h
->fanout_table
= (uint32_t *)(p
->map
+ offset
);
160 h
->fanout_table
= malloc(len_fanout
);
161 if (h
->fanout_table
== NULL
) {
162 err
= got_error_from_errno("malloc");
165 n
= read(p
->fd
, h
->fanout_table
, len_fanout
);
167 err
= got_error_from_errno("read");
169 } else if (n
!= len_fanout
) {
170 err
= got_error(GOT_ERR_BAD_PACKIDX
);
174 err
= verify_fanout_table(h
->fanout_table
);
178 got_hash_update(&ctx
, h
->fanout_table
, len_fanout
);
179 offset
+= len_fanout
;
180 remain
-= len_fanout
;
182 nobj
= be32toh(h
->fanout_table
[0xff]);
183 len_ids
= nobj
* sizeof(*h
->sorted_ids
);
184 if (len_ids
<= nobj
|| len_ids
> remain
) {
185 err
= got_error(GOT_ERR_BAD_PACKIDX
);
190 (struct got_packidx_object_id
*)((uint8_t*)(p
->map
+ offset
));
192 h
->sorted_ids
= malloc(len_ids
);
193 if (h
->sorted_ids
== NULL
) {
194 err
= got_error(GOT_ERR_BAD_PACKIDX
);
197 n
= read(p
->fd
, h
->sorted_ids
, len_ids
);
199 err
= got_error_from_errno("read");
200 else if (n
!= len_ids
) {
201 err
= got_error(GOT_ERR_BAD_PACKIDX
);
206 got_hash_update(&ctx
, h
->sorted_ids
, len_ids
);
210 if (remain
< nobj
* sizeof(*h
->crc32
)) {
211 err
= got_error(GOT_ERR_BAD_PACKIDX
);
215 h
->crc32
= (uint32_t *)((uint8_t*)(p
->map
+ offset
));
217 h
->crc32
= malloc(nobj
* sizeof(*h
->crc32
));
218 if (h
->crc32
== NULL
) {
219 err
= got_error_from_errno("malloc");
222 n
= read(p
->fd
, h
->crc32
, nobj
* sizeof(*h
->crc32
));
224 err
= got_error_from_errno("read");
225 else if (n
!= nobj
* sizeof(*h
->crc32
)) {
226 err
= got_error(GOT_ERR_BAD_PACKIDX
);
231 got_hash_update(&ctx
, h
->crc32
, nobj
* sizeof(*h
->crc32
));
232 remain
-= nobj
* sizeof(*h
->crc32
);
233 offset
+= nobj
* sizeof(*h
->crc32
);
235 if (remain
< nobj
* sizeof(*h
->offsets
)) {
236 err
= got_error(GOT_ERR_BAD_PACKIDX
);
240 h
->offsets
= (uint32_t *)((uint8_t*)(p
->map
+ offset
));
242 h
->offsets
= malloc(nobj
* sizeof(*h
->offsets
));
243 if (h
->offsets
== NULL
) {
244 err
= got_error_from_errno("malloc");
247 n
= read(p
->fd
, h
->offsets
, nobj
* sizeof(*h
->offsets
));
249 err
= got_error_from_errno("read");
250 else if (n
!= nobj
* sizeof(*h
->offsets
)) {
251 err
= got_error(GOT_ERR_BAD_PACKIDX
);
256 got_hash_update(&ctx
, h
->offsets
, nobj
* sizeof(*h
->offsets
));
257 remain
-= nobj
* sizeof(*h
->offsets
);
258 offset
+= nobj
* sizeof(*h
->offsets
);
260 /* Large file offsets are contained only in files > 2GB. */
261 if (verify
|| packfile_size
> 0x7fffffff) {
262 for (i
= 0; i
< nobj
; i
++) {
263 uint32_t o
= h
->offsets
[i
];
264 if (o
& htobe32(GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX
))
268 if (p
->nlargeobj
== 0)
270 else if (packfile_size
<= 0x7fffffff) {
271 err
= got_error(GOT_ERR_BAD_PACKIDX
);
275 if (remain
< p
->nlargeobj
* sizeof(*h
->large_offsets
)) {
276 err
= got_error(GOT_ERR_BAD_PACKIDX
);
280 h
->large_offsets
= (uint64_t *)((uint8_t*)(p
->map
+ offset
));
282 h
->large_offsets
= malloc(p
->nlargeobj
*
283 sizeof(*h
->large_offsets
));
284 if (h
->large_offsets
== NULL
) {
285 err
= got_error_from_errno("malloc");
288 n
= read(p
->fd
, h
->large_offsets
,
289 p
->nlargeobj
* sizeof(*h
->large_offsets
));
291 err
= got_error_from_errno("read");
292 else if (n
!= p
->nlargeobj
* sizeof(*h
->large_offsets
)) {
293 err
= got_error(GOT_ERR_BAD_PACKIDX
);
298 got_hash_update(&ctx
, h
->large_offsets
,
299 p
->nlargeobj
* sizeof(*h
->large_offsets
));
300 remain
-= p
->nlargeobj
* sizeof(*h
->large_offsets
);
301 offset
+= p
->nlargeobj
* sizeof(*h
->large_offsets
);
304 if (remain
< sizeof(*h
->trailer
)) {
305 err
= got_error(GOT_ERR_BAD_PACKIDX
);
310 (struct got_packidx_trailer
*)((uint8_t*)(p
->map
+ offset
));
312 h
->trailer
= malloc(sizeof(*h
->trailer
));
313 if (h
->trailer
== NULL
) {
314 err
= got_error_from_errno("malloc");
317 n
= read(p
->fd
, h
->trailer
, sizeof(*h
->trailer
));
319 err
= got_error_from_errno("read");
320 else if (n
!= sizeof(*h
->trailer
)) {
321 err
= got_error(GOT_ERR_BAD_PACKIDX
);
326 got_hash_update(&ctx
, h
->trailer
->packfile_sha1
,
328 got_hash_final(&ctx
, hash
);
329 if (got_hash_cmp(ctx
.algo
, hash
, h
->trailer
->packidx_sha1
) != 0)
330 err
= got_error(GOT_ERR_PACKIDX_CSUM
);
336 const struct got_error
*
337 got_packidx_open(struct got_packidx
**packidx
,
338 int dir_fd
, const char *relpath
, int verify
)
340 const struct got_error
*err
= NULL
;
341 struct got_packidx
*p
= NULL
;
343 struct stat idx_sb
, pack_sb
;
347 err
= got_packidx_get_packfile_path(&pack_relpath
, relpath
);
352 * Ensure that a corresponding pack file exists.
353 * Some Git repositories have this problem. Git seems to ignore
354 * the existence of lonely pack index files but we do not.
356 if (fstatat(dir_fd
, pack_relpath
, &pack_sb
, 0) == -1) {
357 if (errno
== ENOENT
) {
358 err
= got_error_fmt(GOT_ERR_LONELY_PACKIDX
,
361 err
= got_error_from_errno2("fstatat", pack_relpath
);
365 p
= calloc(1, sizeof(*p
));
367 err
= got_error_from_errno("calloc");
371 p
->fd
= openat(dir_fd
, relpath
, O_RDONLY
| O_NOFOLLOW
| O_CLOEXEC
);
373 err
= got_error_from_errno2("openat", relpath
);
377 if (fstat(p
->fd
, &idx_sb
) != 0) {
378 err
= got_error_from_errno2("fstat", relpath
);
381 p
->len
= idx_sb
.st_size
;
382 if (p
->len
< sizeof(p
->hdr
)) {
383 err
= got_error(GOT_ERR_BAD_PACKIDX
);
387 p
->path_packidx
= strdup(relpath
);
388 if (p
->path_packidx
== NULL
) {
389 err
= got_error_from_errno("strdup");
393 #ifndef GOT_PACK_NO_MMAP
394 if (p
->len
> 0 && p
->len
<= SIZE_MAX
) {
395 p
->map
= mmap(NULL
, p
->len
, PROT_READ
, MAP_PRIVATE
, p
->fd
, 0);
396 if (p
->map
== MAP_FAILED
) {
397 if (errno
!= ENOMEM
) {
398 err
= got_error_from_errno("mmap");
401 p
->map
= NULL
; /* fall back to read(2) */
406 err
= got_packidx_init_hdr(p
, verify
, pack_sb
.st_size
);
410 got_packidx_close(p
);
417 const struct got_error
*
418 got_packidx_close(struct got_packidx
*packidx
)
420 const struct got_error
*err
= NULL
;
422 free(packidx
->path_packidx
);
424 if (munmap(packidx
->map
, packidx
->len
) == -1)
425 err
= got_error_from_errno("munmap");
427 free(packidx
->hdr
.magic
);
428 free(packidx
->hdr
.version
);
429 free(packidx
->hdr
.fanout_table
);
430 free(packidx
->hdr
.sorted_ids
);
431 free(packidx
->hdr
.crc32
);
432 free(packidx
->hdr
.offsets
);
433 free(packidx
->hdr
.large_offsets
);
434 free(packidx
->hdr
.trailer
);
436 if (close(packidx
->fd
) == -1 && err
== NULL
)
437 err
= got_error_from_errno("close");
438 free(packidx
->sorted_offsets
);
439 free(packidx
->sorted_large_offsets
);
445 const struct got_error
*
446 got_packidx_get_packfile_path(char **path_packfile
, const char *path_packidx
)
450 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
451 size
= strlen(path_packidx
) + 2;
452 if (size
< GOT_PACKFILE_NAMELEN
+ 1)
453 return got_error_path(path_packidx
, GOT_ERR_BAD_PATH
);
455 *path_packfile
= malloc(size
);
456 if (*path_packfile
== NULL
)
457 return got_error_from_errno("malloc");
459 /* Copy up to and excluding ".idx". */
460 if (strlcpy(*path_packfile
, path_packidx
,
461 size
- strlen(GOT_PACKIDX_SUFFIX
) - 1) >= size
)
462 return got_error(GOT_ERR_NO_SPACE
);
464 if (strlcat(*path_packfile
, GOT_PACKFILE_SUFFIX
, size
) >= size
)
465 return got_error(GOT_ERR_NO_SPACE
);
471 got_packidx_get_object_offset(struct got_packidx
*packidx
, int idx
)
473 uint32_t offset
= be32toh(packidx
->hdr
.offsets
[idx
]);
474 if (offset
& GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX
) {
476 idx
= offset
& GOT_PACKIDX_OFFSET_VAL_MASK
;
477 if (idx
< 0 || idx
>= packidx
->nlargeobj
||
478 packidx
->hdr
.large_offsets
== NULL
)
480 loffset
= be64toh(packidx
->hdr
.large_offsets
[idx
]);
481 return (loffset
> INT64_MAX
? -1 : (off_t
)loffset
);
483 return (off_t
)(offset
& GOT_PACKIDX_OFFSET_VAL_MASK
);
487 got_packidx_get_object_idx(struct got_packidx
*packidx
,
488 struct got_object_id
*id
)
490 u_int8_t id0
= id
->sha1
[0];
491 uint32_t totobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
492 int left
= 0, right
= totobj
- 1;
495 left
= be32toh(packidx
->hdr
.fanout_table
[id0
- 1]);
497 while (left
<= right
) {
498 struct got_packidx_object_id
*oid
;
501 i
= ((left
+ right
) / 2);
502 oid
= &packidx
->hdr
.sorted_ids
[i
];
503 cmp
= memcmp(id
->sha1
, oid
->sha1
, SHA1_DIGEST_LENGTH
);
516 offset_cmp(const void *pa
, const void *pb
)
518 const struct got_pack_offset_index
*a
, *b
;
520 a
= (const struct got_pack_offset_index
*)pa
;
521 b
= (const struct got_pack_offset_index
*)pb
;
523 if (a
->offset
< b
->offset
)
525 else if (a
->offset
> b
->offset
)
532 large_offset_cmp(const void *pa
, const void *pb
)
534 const struct got_pack_large_offset_index
*a
, *b
;
536 a
= (const struct got_pack_large_offset_index
*)pa
;
537 b
= (const struct got_pack_large_offset_index
*)pb
;
539 if (a
->offset
< b
->offset
)
541 else if (a
->offset
> b
->offset
)
547 static const struct got_error
*
548 build_offset_index(struct got_packidx
*p
)
550 uint32_t nobj
= be32toh(p
->hdr
.fanout_table
[0xff]);
551 unsigned int i
, j
, k
;
553 p
->sorted_offsets
= calloc(nobj
- p
->nlargeobj
,
554 sizeof(p
->sorted_offsets
[0]));
555 if (p
->sorted_offsets
== NULL
)
556 return got_error_from_errno("calloc");
558 if (p
->nlargeobj
> 0) {
559 p
->sorted_large_offsets
= calloc(p
->nlargeobj
,
560 sizeof(p
->sorted_large_offsets
[0]));
561 if (p
->sorted_large_offsets
== NULL
)
562 return got_error_from_errno("calloc");
567 for (i
= 0; i
< nobj
; i
++) {
568 uint32_t offset
= be32toh(p
->hdr
.offsets
[i
]);
569 if (offset
& GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX
) {
572 idx
= offset
& GOT_PACKIDX_OFFSET_VAL_MASK
;
573 if (idx
>= p
->nlargeobj
||
575 p
->hdr
.large_offsets
== NULL
)
576 return got_error(GOT_ERR_BAD_PACKIDX
);
577 loffset
= be64toh(p
->hdr
.large_offsets
[idx
]);
578 p
->sorted_large_offsets
[j
].offset
= loffset
;
579 p
->sorted_large_offsets
[j
].idx
= i
;
582 p
->sorted_offsets
[k
].offset
= offset
;
583 p
->sorted_offsets
[k
].idx
= i
;
587 if (j
!= p
->nlargeobj
|| k
!= nobj
- p
->nlargeobj
)
588 return got_error(GOT_ERR_BAD_PACKIDX
);
590 qsort(p
->sorted_offsets
, nobj
- p
->nlargeobj
,
591 sizeof(p
->sorted_offsets
[0]), offset_cmp
);
593 if (p
->sorted_large_offsets
)
594 qsort(p
->sorted_large_offsets
, p
->nlargeobj
,
595 sizeof(p
->sorted_large_offsets
[0]), large_offset_cmp
);
600 const struct got_error
*
601 got_packidx_get_offset_idx(int *idx
, struct got_packidx
*packidx
, off_t offset
)
603 const struct got_error
*err
;
604 uint32_t totobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
609 if (packidx
->sorted_offsets
== NULL
) {
610 err
= build_offset_index(packidx
);
615 if (offset
>= 0x7fffffff) {
617 left
= 0, right
= packidx
->nlargeobj
- 1;
618 while (left
<= right
) {
619 i
= ((left
+ right
) / 2);
620 lo
= packidx
->sorted_large_offsets
[i
].offset
;
622 *idx
= packidx
->sorted_large_offsets
[i
].idx
;
624 } else if (offset
> lo
)
626 else if (offset
< lo
)
631 left
= 0, right
= totobj
- packidx
->nlargeobj
- 1;
632 while (left
<= right
) {
633 i
= ((left
+ right
) / 2);
634 o
= packidx
->sorted_offsets
[i
].offset
;
636 *idx
= packidx
->sorted_offsets
[i
].idx
;
638 } else if (offset
> o
)
648 const struct got_error
*
649 got_packidx_get_object_id(struct got_object_id
*id
,
650 struct got_packidx
*packidx
, int idx
)
652 uint32_t totobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
653 struct got_packidx_object_id
*oid
;
655 if (idx
< 0 || idx
>= totobj
)
656 return got_error(GOT_ERR_NO_OBJ
);
658 oid
= &packidx
->hdr
.sorted_ids
[idx
];
659 memcpy(id
->sha1
, oid
->sha1
, SHA1_DIGEST_LENGTH
);
663 const struct got_error
*
664 got_packidx_match_id_str_prefix(struct got_object_id_queue
*matched_ids
,
665 struct got_packidx
*packidx
, const char *id_str_prefix
)
667 const struct got_error
*err
= NULL
;
669 uint32_t totobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
671 size_t prefix_len
= strlen(id_str_prefix
);
672 struct got_packidx_object_id
*oid
;
675 STAILQ_INIT(matched_ids
);
678 return got_error_path(id_str_prefix
, GOT_ERR_BAD_OBJ_ID_STR
);
680 hex
[0] = id_str_prefix
[0];
681 hex
[1] = id_str_prefix
[1];
683 if (!got_parse_xdigit(&id0
, hex
))
684 return got_error_path(id_str_prefix
, GOT_ERR_BAD_OBJ_ID_STR
);
687 i
= be32toh(packidx
->hdr
.fanout_table
[id0
- 1]);
688 oid
= &packidx
->hdr
.sorted_ids
[i
];
689 while (i
< totobj
&& oid
->sha1
[0] == id0
) {
690 char id_str
[SHA1_DIGEST_STRING_LENGTH
];
691 struct got_object_qid
*qid
;
694 if (!got_sha1_digest_to_str(oid
->sha1
, id_str
, sizeof(id_str
)))
695 return got_error(GOT_ERR_NO_SPACE
);
697 cmp
= strncmp(id_str
, id_str_prefix
, prefix_len
);
699 oid
= &packidx
->hdr
.sorted_ids
[++i
];
704 err
= got_object_qid_alloc_partial(&qid
);
707 memcpy(qid
->id
.sha1
, oid
->sha1
, SHA1_DIGEST_LENGTH
);
708 STAILQ_INSERT_TAIL(matched_ids
, qid
, entry
);
710 oid
= &packidx
->hdr
.sorted_ids
[++i
];
714 got_object_id_queue_free(matched_ids
);
719 set_max_datasize(void)
723 if (getrlimit(RLIMIT_DATA
, &rl
) != 0)
726 rl
.rlim_cur
= rl
.rlim_max
;
727 setrlimit(RLIMIT_DATA
, &rl
);
730 const struct got_error
*
731 got_pack_start_privsep_child(struct got_pack
*pack
, struct got_packidx
*packidx
)
733 const struct got_error
*err
= NULL
;
736 struct imsgbuf
*ibuf
;
738 ibuf
= calloc(1, sizeof(*ibuf
));
740 return got_error_from_errno("calloc");
742 pack
->privsep_child
= calloc(1, sizeof(*pack
->privsep_child
));
743 if (pack
->privsep_child
== NULL
) {
744 err
= got_error_from_errno("calloc");
748 pack
->child_has_tempfiles
= 0;
749 pack
->child_has_delta_outfd
= 0;
751 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, imsg_fds
) == -1) {
752 err
= got_error_from_errno("socketpair");
758 err
= got_error_from_errno("fork");
760 } else if (pid
== 0) {
762 got_privsep_exec_child(imsg_fds
, GOT_PATH_PROG_READ_PACK
,
763 pack
->path_packfile
);
767 if (close(imsg_fds
[1]) == -1)
768 return got_error_from_errno("close");
769 pack
->privsep_child
->imsg_fd
= imsg_fds
[0];
770 pack
->privsep_child
->pid
= pid
;
771 imsg_init(ibuf
, imsg_fds
[0]);
772 pack
->privsep_child
->ibuf
= ibuf
;
774 err
= got_privsep_init_pack_child(ibuf
, pack
, packidx
);
776 const struct got_error
*child_err
;
777 err
= got_privsep_send_stop(pack
->privsep_child
->imsg_fd
);
778 child_err
= got_privsep_wait_for_child(
779 pack
->privsep_child
->pid
);
780 if (child_err
&& err
== NULL
)
786 free(pack
->privsep_child
);
787 pack
->privsep_child
= NULL
;
792 static const struct got_error
*
793 pack_stop_privsep_child(struct got_pack
*pack
)
795 const struct got_error
*err
= NULL
, *close_err
= NULL
;
797 if (pack
->privsep_child
== NULL
)
800 err
= got_privsep_send_stop(pack
->privsep_child
->imsg_fd
);
803 if (close(pack
->privsep_child
->imsg_fd
) == -1)
804 close_err
= got_error_from_errno("close");
805 err
= got_privsep_wait_for_child(pack
->privsep_child
->pid
);
806 if (close_err
&& err
== NULL
)
808 imsg_clear(pack
->privsep_child
->ibuf
);
809 free(pack
->privsep_child
->ibuf
);
810 free(pack
->privsep_child
);
811 pack
->privsep_child
= NULL
;
815 const struct got_error
*
816 got_pack_close(struct got_pack
*pack
)
818 const struct got_error
*err
= NULL
;
820 err
= pack_stop_privsep_child(pack
);
821 if (pack
->map
&& munmap(pack
->map
, pack
->filesize
) == -1 && !err
)
822 err
= got_error_from_errno("munmap");
823 if (pack
->fd
!= -1 && close(pack
->fd
) == -1 && err
== NULL
)
824 err
= got_error_from_errno("close");
826 free(pack
->path_packfile
);
827 pack
->path_packfile
= NULL
;
829 if (pack
->delta_cache
) {
830 got_delta_cache_free(pack
->delta_cache
);
831 pack
->delta_cache
= NULL
;
835 * Leave accumfd and basefd alone. They are managed by the
836 * repository layer and can be reused.
842 const struct got_error
*
843 got_pack_parse_object_type_and_size(uint8_t *type
, uint64_t *size
, size_t *len
,
844 struct got_pack
*pack
, off_t offset
)
854 if (offset
>= pack
->filesize
)
855 return got_error(GOT_ERR_PACK_OFFSET
);
858 if (offset
> SIZE_MAX
) {
859 return got_error_fmt(GOT_ERR_PACK_OFFSET
,
860 "offset %lld overflows size_t",
864 mapoff
= (size_t)offset
;
866 if (lseek(pack
->fd
, offset
, SEEK_SET
) == -1)
867 return got_error_from_errno("lseek");
871 /* We do not support size values which don't fit in 64 bit. */
873 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE
,
874 "packfile offset %lld", (long long)offset
);
877 if (mapoff
+ sizeof(sizeN
) >= pack
->filesize
)
878 return got_error(GOT_ERR_BAD_PACKFILE
);
879 sizeN
= *(pack
->map
+ mapoff
);
880 mapoff
+= sizeof(sizeN
);
882 ssize_t n
= read(pack
->fd
, &sizeN
, sizeof(sizeN
));
884 return got_error_from_errno("read");
885 if (n
!= sizeof(sizeN
))
886 return got_error(GOT_ERR_BAD_PACKFILE
);
888 *len
+= sizeof(sizeN
);
891 t
= (sizeN
& GOT_PACK_OBJ_SIZE0_TYPE_MASK
) >>
892 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT
;
893 s
= (sizeN
& GOT_PACK_OBJ_SIZE0_VAL_MASK
);
895 size_t shift
= 4 + 7 * (i
- 1);
896 s
|= ((sizeN
& GOT_PACK_OBJ_SIZE_VAL_MASK
) << shift
);
899 } while (sizeN
& GOT_PACK_OBJ_SIZE_MORE
);
906 static const struct got_error
*
907 open_plain_object(struct got_object
**obj
, struct got_object_id
*id
,
908 uint8_t type
, off_t offset
, size_t size
, int idx
)
910 *obj
= calloc(1, sizeof(**obj
));
912 return got_error_from_errno("calloc");
915 (*obj
)->flags
= GOT_OBJ_FLAG_PACKED
;
916 (*obj
)->pack_idx
= idx
;
919 memcpy(&(*obj
)->id
, id
, sizeof((*obj
)->id
));
920 (*obj
)->pack_offset
= offset
;
925 static const struct got_error
*
926 parse_negative_offset(int64_t *offset
, size_t *len
, struct got_pack
*pack
,
937 /* We do not support offset values which don't fit in 64 bit. */
939 return got_error(GOT_ERR_NO_SPACE
);
944 if (delta_offset
+ *len
> SIZE_MAX
) {
945 return got_error_fmt(GOT_ERR_PACK_OFFSET
,
946 "mapoff %lld would overflow size_t",
947 (long long)delta_offset
+ *len
);
950 mapoff
= (size_t)delta_offset
+ *len
;
951 if (mapoff
+ sizeof(offN
) >= pack
->filesize
)
952 return got_error(GOT_ERR_PACK_OFFSET
);
953 offN
= *(pack
->map
+ mapoff
);
956 n
= read(pack
->fd
, &offN
, sizeof(offN
));
958 return got_error_from_errno("read");
959 if (n
!= sizeof(offN
))
960 return got_error(GOT_ERR_BAD_PACKFILE
);
962 *len
+= sizeof(offN
);
965 o
= (offN
& GOT_PACK_OBJ_DELTA_OFF_VAL_MASK
);
969 o
+= (offN
& GOT_PACK_OBJ_DELTA_OFF_VAL_MASK
);
972 } while (offN
& GOT_PACK_OBJ_DELTA_OFF_MORE
);
978 const struct got_error
*
979 got_pack_parse_offset_delta(off_t
*base_offset
, size_t *len
,
980 struct got_pack
*pack
, off_t offset
, size_t tslen
)
982 const struct got_error
*err
;
988 err
= parse_negative_offset(&negoffset
, &negofflen
, pack
,
993 /* Compute the base object's offset (must be in the same pack file). */
994 *base_offset
= (offset
- negoffset
);
995 if (*base_offset
<= 0)
996 return got_error(GOT_ERR_BAD_PACKFILE
);
1002 static const struct got_error
*
1003 read_delta_data(uint8_t **delta_buf
, size_t *delta_len
,
1004 size_t *delta_compressed_len
, size_t delta_data_offset
,
1005 struct got_pack
*pack
)
1007 const struct got_error
*err
= NULL
;
1008 size_t consumed
= 0;
1011 if (delta_data_offset
>= pack
->filesize
)
1012 return got_error(GOT_ERR_PACK_OFFSET
);
1013 err
= got_inflate_to_mem_mmap(delta_buf
, delta_len
,
1014 &consumed
, NULL
, pack
->map
, delta_data_offset
,
1015 pack
->filesize
- delta_data_offset
);
1019 if (lseek(pack
->fd
, delta_data_offset
, SEEK_SET
) == -1)
1020 return got_error_from_errno("lseek");
1021 err
= got_inflate_to_mem_fd(delta_buf
, delta_len
,
1022 &consumed
, NULL
, 0, pack
->fd
);
1027 if (delta_compressed_len
)
1028 *delta_compressed_len
= consumed
;
1033 static const struct got_error
*
1034 add_delta(struct got_delta_chain
*deltas
, off_t delta_offset
, size_t tslen
,
1035 int delta_type
, size_t delta_size
, off_t delta_data_offset
)
1037 struct got_delta
*delta
;
1039 delta
= got_delta_open(delta_offset
, tslen
, delta_type
, delta_size
,
1042 return got_error_from_errno("got_delta_open");
1043 /* delta is freed in got_object_close() */
1045 STAILQ_INSERT_HEAD(&deltas
->entries
, delta
, entry
);
1049 static const struct got_error
*
1050 resolve_offset_delta(struct got_delta_chain
*deltas
,
1051 struct got_packidx
*packidx
, struct got_pack
*pack
, off_t delta_offset
,
1052 size_t tslen
, int delta_type
, size_t delta_size
, unsigned int recursion
)
1054 const struct got_error
*err
;
1059 off_t delta_data_offset
;
1062 err
= got_pack_parse_offset_delta(&base_offset
, &consumed
, pack
,
1063 delta_offset
, tslen
);
1067 delta_data_offset
= delta_offset
+ tslen
+ consumed
;
1068 if (delta_data_offset
>= pack
->filesize
)
1069 return got_error(GOT_ERR_PACK_OFFSET
);
1071 if (pack
->map
== NULL
) {
1072 delta_data_offset
= lseek(pack
->fd
, 0, SEEK_CUR
);
1073 if (delta_data_offset
== -1)
1074 return got_error_from_errno("lseek");
1077 err
= add_delta(deltas
, delta_offset
, tslen
, delta_type
, delta_size
,
1082 /* An offset delta must be in the same packfile. */
1083 if (base_offset
>= pack
->filesize
)
1084 return got_error(GOT_ERR_PACK_OFFSET
);
1086 err
= got_pack_parse_object_type_and_size(&base_type
, &base_size
,
1087 &base_tslen
, pack
, base_offset
);
1091 return got_pack_resolve_delta_chain(deltas
, packidx
, pack
, base_offset
,
1092 base_tslen
, base_type
, base_size
, recursion
- 1);
1095 const struct got_error
*
1096 got_pack_parse_ref_delta(struct got_object_id
*id
,
1097 struct got_pack
*pack
, off_t delta_offset
, int tslen
)
1102 if (delta_offset
+ tslen
> SIZE_MAX
) {
1103 return got_error_fmt(GOT_ERR_PACK_OFFSET
,
1104 "mapoff %lld would overflow size_t",
1105 (long long)delta_offset
+ tslen
);
1108 mapoff
= delta_offset
+ tslen
;
1109 if (mapoff
+ sizeof(*id
) >= pack
->filesize
)
1110 return got_error(GOT_ERR_PACK_OFFSET
);
1111 memcpy(id
, pack
->map
+ mapoff
, sizeof(*id
));
1114 n
= read(pack
->fd
, id
, sizeof(*id
));
1116 return got_error_from_errno("read");
1117 if (n
!= sizeof(*id
))
1118 return got_error(GOT_ERR_BAD_PACKFILE
);
1124 static const struct got_error
*
1125 resolve_ref_delta(struct got_delta_chain
*deltas
, struct got_packidx
*packidx
,
1126 struct got_pack
*pack
, off_t delta_offset
, size_t tslen
, int delta_type
,
1127 size_t delta_size
, unsigned int recursion
)
1129 const struct got_error
*err
;
1130 struct got_object_id id
;
1136 off_t delta_data_offset
;
1138 if (delta_offset
+ tslen
>= pack
->filesize
)
1139 return got_error(GOT_ERR_PACK_OFFSET
);
1141 err
= got_pack_parse_ref_delta(&id
, pack
, delta_offset
, tslen
);
1145 delta_data_offset
= delta_offset
+ tslen
+ SHA1_DIGEST_LENGTH
;
1147 delta_data_offset
= lseek(pack
->fd
, 0, SEEK_CUR
);
1148 if (delta_data_offset
== -1)
1149 return got_error_from_errno("lseek");
1152 err
= add_delta(deltas
, delta_offset
, tslen
, delta_type
, delta_size
,
1157 /* Delta base must be in the same pack file. */
1158 idx
= got_packidx_get_object_idx(packidx
, &id
);
1160 return got_error(GOT_ERR_NO_OBJ
);
1162 base_offset
= got_packidx_get_object_offset(packidx
, idx
);
1163 if (base_offset
== -1)
1164 return got_error(GOT_ERR_BAD_PACKIDX
);
1166 if (base_offset
>= pack
->filesize
)
1167 return got_error(GOT_ERR_PACK_OFFSET
);
1169 err
= got_pack_parse_object_type_and_size(&base_type
, &base_size
,
1170 &base_tslen
, pack
, base_offset
);
1174 return got_pack_resolve_delta_chain(deltas
, packidx
, pack
, base_offset
,
1175 base_tslen
, base_type
, base_size
, recursion
- 1);
1178 const struct got_error
*
1179 got_pack_resolve_delta_chain(struct got_delta_chain
*deltas
,
1180 struct got_packidx
*packidx
, struct got_pack
*pack
, off_t delta_offset
,
1181 size_t tslen
, int delta_type
, size_t delta_size
, unsigned int recursion
)
1183 const struct got_error
*err
= NULL
;
1185 if (--recursion
== 0)
1186 return got_error(GOT_ERR_RECURSION
);
1188 switch (delta_type
) {
1189 case GOT_OBJ_TYPE_COMMIT
:
1190 case GOT_OBJ_TYPE_TREE
:
1191 case GOT_OBJ_TYPE_BLOB
:
1192 case GOT_OBJ_TYPE_TAG
:
1193 /* Plain types are the final delta base. Recursion ends. */
1194 err
= add_delta(deltas
, delta_offset
, tslen
, delta_type
,
1197 case GOT_OBJ_TYPE_OFFSET_DELTA
:
1198 err
= resolve_offset_delta(deltas
, packidx
, pack
,
1199 delta_offset
, tslen
, delta_type
, delta_size
, recursion
- 1);
1201 case GOT_OBJ_TYPE_REF_DELTA
:
1202 err
= resolve_ref_delta(deltas
, packidx
, pack
,
1203 delta_offset
, tslen
, delta_type
, delta_size
, recursion
- 1);
1206 return got_error(GOT_ERR_OBJ_TYPE
);
1212 static const struct got_error
*
1213 open_delta_object(struct got_object
**obj
, struct got_packidx
*packidx
,
1214 struct got_pack
*pack
, struct got_object_id
*id
, off_t offset
,
1215 size_t tslen
, int delta_type
, size_t delta_size
, int idx
)
1217 const struct got_error
*err
= NULL
;
1220 *obj
= calloc(1, sizeof(**obj
));
1222 return got_error_from_errno("calloc");
1226 (*obj
)->size
= 0; /* Not known because deltas aren't applied yet. */
1227 memcpy(&(*obj
)->id
, id
, sizeof((*obj
)->id
));
1228 (*obj
)->pack_offset
= offset
+ tslen
;
1230 STAILQ_INIT(&(*obj
)->deltas
.entries
);
1231 (*obj
)->flags
|= GOT_OBJ_FLAG_DELTIFIED
;
1232 (*obj
)->flags
|= GOT_OBJ_FLAG_PACKED
;
1233 (*obj
)->pack_idx
= idx
;
1235 err
= got_pack_resolve_delta_chain(&(*obj
)->deltas
, packidx
, pack
,
1236 offset
, tslen
, delta_type
, delta_size
,
1237 GOT_DELTA_CHAIN_RECURSION_MAX
);
1241 err
= got_delta_chain_get_base_type(&resolved_type
, &(*obj
)->deltas
);
1244 (*obj
)->type
= resolved_type
;
1247 got_object_close(*obj
);
1253 const struct got_error
*
1254 got_packfile_open_object(struct got_object
**obj
, struct got_pack
*pack
,
1255 struct got_packidx
*packidx
, int idx
, struct got_object_id
*id
)
1257 const struct got_error
*err
= NULL
;
1265 offset
= got_packidx_get_object_offset(packidx
, idx
);
1267 return got_error(GOT_ERR_BAD_PACKIDX
);
1269 err
= got_pack_parse_object_type_and_size(&type
, &size
, &tslen
,
1275 case GOT_OBJ_TYPE_COMMIT
:
1276 case GOT_OBJ_TYPE_TREE
:
1277 case GOT_OBJ_TYPE_BLOB
:
1278 case GOT_OBJ_TYPE_TAG
:
1279 err
= open_plain_object(obj
, id
, type
, offset
+ tslen
,
1282 case GOT_OBJ_TYPE_OFFSET_DELTA
:
1283 case GOT_OBJ_TYPE_REF_DELTA
:
1284 err
= open_delta_object(obj
, packidx
, pack
, id
, offset
,
1285 tslen
, type
, size
, idx
);
1288 err
= got_error(GOT_ERR_OBJ_TYPE
);
1295 const struct got_error
*
1296 got_pack_get_delta_chain_max_size(uint64_t *max_size
,
1297 struct got_delta_chain
*deltas
, struct got_pack
*pack
)
1299 struct got_delta
*delta
;
1300 uint64_t base_size
= 0, result_size
= 0;
1303 STAILQ_FOREACH(delta
, &deltas
->entries
, entry
) {
1304 /* Plain object types are the delta base. */
1305 if (delta
->type
!= GOT_OBJ_TYPE_COMMIT
&&
1306 delta
->type
!= GOT_OBJ_TYPE_TREE
&&
1307 delta
->type
!= GOT_OBJ_TYPE_BLOB
&&
1308 delta
->type
!= GOT_OBJ_TYPE_TAG
) {
1309 const struct got_error
*err
;
1310 uint8_t *delta_buf
= NULL
;
1314 if (pack
->delta_cache
) {
1315 got_delta_cache_get(&delta_buf
, &delta_len
,
1316 NULL
, NULL
, pack
->delta_cache
,
1317 delta
->data_offset
);
1319 if (delta_buf
== NULL
) {
1321 err
= read_delta_data(&delta_buf
, &delta_len
,
1322 NULL
, delta
->data_offset
, pack
);
1326 if (pack
->delta_cache
&& !cached
) {
1327 err
= got_delta_cache_add(pack
->delta_cache
,
1328 delta
->data_offset
, delta_buf
, delta_len
);
1331 else if (err
->code
!= GOT_ERR_NO_SPACE
) {
1336 err
= got_delta_get_sizes(&base_size
, &result_size
,
1337 delta_buf
, delta_len
);
1343 base_size
= delta
->size
;
1344 if (base_size
> *max_size
)
1345 *max_size
= base_size
;
1346 if (result_size
> *max_size
)
1347 *max_size
= result_size
;
1353 const struct got_error
*
1354 got_pack_get_max_delta_object_size(uint64_t *size
, struct got_object
*obj
,
1355 struct got_pack
*pack
)
1357 if ((obj
->flags
& GOT_OBJ_FLAG_DELTIFIED
) == 0)
1358 return got_error(GOT_ERR_OBJ_TYPE
);
1360 return got_pack_get_delta_chain_max_size(size
, &obj
->deltas
, pack
);
1363 const struct got_error
*
1364 got_pack_dump_delta_chain_to_file(size_t *result_size
,
1365 struct got_delta_chain
*deltas
, struct got_pack
*pack
, FILE *outfile
,
1366 FILE *base_file
, FILE *accum_file
)
1368 const struct got_error
*err
= NULL
;
1369 struct got_delta
*delta
;
1370 uint8_t *base_buf
= NULL
, *accum_buf
= NULL
;
1371 size_t base_bufsz
= 0, accum_bufsz
= 0, accum_size
= 0;
1372 /* We process small enough files entirely in memory for speed. */
1373 const size_t max_bufsize
= GOT_DELTA_RESULT_SIZE_CACHED_MAX
;
1374 uint64_t max_size
= 0;
1379 if (STAILQ_EMPTY(&deltas
->entries
))
1380 return got_error(GOT_ERR_BAD_DELTA_CHAIN
);
1382 if (pack
->delta_cache
) {
1383 uint8_t *delta_buf
= NULL
, *fulltext
= NULL
;
1384 size_t delta_len
, fulltext_len
;
1386 delta
= STAILQ_LAST(&deltas
->entries
, got_delta
, entry
);
1387 got_delta_cache_get(&delta_buf
, &delta_len
,
1388 &fulltext
, &fulltext_len
,
1389 pack
->delta_cache
, delta
->data_offset
);
1393 w
= fwrite(fulltext
, 1, fulltext_len
, outfile
);
1394 if (w
!= fulltext_len
)
1395 return got_ferror(outfile
, GOT_ERR_IO
);
1396 *result_size
= fulltext_len
;
1401 if (fseeko(base_file
, 0L, SEEK_SET
) == -1)
1402 return got_error_from_errno("fseeko");
1403 if (fseeko(accum_file
, 0L, SEEK_SET
) == -1)
1404 return got_error_from_errno("fseeko");
1406 /* Deltas are ordered in ascending order. */
1407 STAILQ_FOREACH(delta
, &deltas
->entries
, entry
) {
1408 uint8_t *delta_buf
= NULL
, *fulltext
= NULL
;
1409 size_t delta_len
, fulltext_len
;
1410 uint64_t base_size
, result_size
= 0;
1414 off_t delta_data_offset
;
1416 /* Plain object types are the delta base. */
1417 if (delta
->type
!= GOT_OBJ_TYPE_COMMIT
&&
1418 delta
->type
!= GOT_OBJ_TYPE_TREE
&&
1419 delta
->type
!= GOT_OBJ_TYPE_BLOB
&&
1420 delta
->type
!= GOT_OBJ_TYPE_TAG
) {
1421 err
= got_error(GOT_ERR_BAD_DELTA_CHAIN
);
1425 delta_data_offset
= delta
->offset
+ delta
->tslen
;
1426 if (delta_data_offset
>= pack
->filesize
) {
1427 err
= got_error(GOT_ERR_PACK_OFFSET
);
1430 if (pack
->map
== NULL
) {
1431 if (lseek(pack
->fd
, delta_data_offset
, SEEK_SET
)
1433 err
= got_error_from_errno("lseek");
1437 if (delta
->size
> max_size
)
1438 max_size
= delta
->size
;
1439 if (max_size
> max_bufsize
) {
1441 if (delta_data_offset
> SIZE_MAX
) {
1442 return got_error_fmt(
1444 "delta offset %lld "
1450 mapoff
= delta_data_offset
;
1451 err
= got_inflate_to_file_mmap(
1452 &base_bufsz
, NULL
, NULL
, pack
->map
,
1453 mapoff
, pack
->filesize
- mapoff
,
1456 err
= got_inflate_to_file_fd(
1457 &base_bufsz
, NULL
, NULL
, pack
->fd
,
1460 accum_buf
= malloc(max_size
);
1461 if (accum_buf
== NULL
) {
1462 err
= got_error_from_errno("malloc");
1465 accum_bufsz
= max_size
;
1467 if (delta_data_offset
> SIZE_MAX
) {
1468 return got_error_fmt(
1470 "delta offset %lld "
1476 mapoff
= delta_data_offset
;
1477 err
= got_inflate_to_mem_mmap(&base_buf
,
1478 &base_bufsz
, NULL
, NULL
,
1480 pack
->filesize
- mapoff
);
1482 err
= got_inflate_to_mem_fd(&base_buf
,
1483 &base_bufsz
, NULL
, NULL
, max_size
,
1489 if (base_buf
== NULL
)
1494 if (pack
->delta_cache
) {
1495 got_delta_cache_get(&delta_buf
, &delta_len
,
1496 &fulltext
, &fulltext_len
,
1497 pack
->delta_cache
, delta
->data_offset
);
1499 if (delta_buf
== NULL
) {
1501 err
= read_delta_data(&delta_buf
, &delta_len
, NULL
,
1502 delta
->data_offset
, pack
);
1506 if (pack
->delta_cache
&& !cached
) {
1507 err
= got_delta_cache_add(pack
->delta_cache
,
1508 delta
->data_offset
, delta_buf
, delta_len
);
1511 else if (err
->code
!= GOT_ERR_NO_SPACE
) {
1517 err
= got_delta_get_sizes(&base_size
, &result_size
,
1518 delta_buf
, delta_len
);
1524 if (base_size
> max_size
)
1525 max_size
= base_size
;
1526 if (result_size
> max_size
)
1527 max_size
= result_size
;
1528 if (fulltext_len
> max_size
)
1529 max_size
= fulltext_len
;
1531 if (base_buf
&& max_size
> max_bufsize
) {
1532 /* Switch from buffers to temporary files. */
1533 size_t w
= fwrite(base_buf
, 1, base_bufsz
,
1535 if (w
!= base_bufsz
) {
1536 err
= got_ferror(outfile
, GOT_ERR_IO
);
1547 if (base_buf
&& max_size
> base_bufsz
) {
1548 uint8_t *p
= realloc(base_buf
, max_size
);
1550 err
= got_error_from_errno("realloc");
1556 base_bufsz
= max_size
;
1559 if (accum_buf
&& max_size
> accum_bufsz
) {
1560 uint8_t *p
= realloc(accum_buf
, max_size
);
1562 err
= got_error_from_errno("realloc");
1568 accum_bufsz
= max_size
;
1573 memcpy(accum_buf
, fulltext
, fulltext_len
);
1574 accum_size
= fulltext_len
;
1577 err
= got_delta_apply_in_mem(base_buf
,
1578 base_bufsz
, delta_buf
, delta_len
,
1579 accum_buf
, &accum_size
, max_size
);
1586 if (fulltext
== NULL
) {
1587 err
= got_delta_cache_add_fulltext(
1588 pack
->delta_cache
, delta
->data_offset
,
1589 accum_buf
, accum_size
);
1591 if (err
->code
!= GOT_ERR_NO_SPACE
)
1597 err
= got_delta_apply(base_file
, delta_buf
,
1599 /* Final delta application writes to output file. */
1600 ++n
< deltas
->nentries
? accum_file
: outfile
,
1608 if (n
< deltas
->nentries
) {
1609 /* Accumulated delta becomes the new base. */
1611 uint8_t *tmp
= accum_buf
;
1612 size_t tmp_size
= accum_bufsz
;
1613 accum_buf
= base_buf
;
1614 accum_bufsz
= base_bufsz
;
1616 base_bufsz
= tmp_size
;
1618 FILE *tmp
= accum_file
;
1619 accum_file
= base_file
;
1630 size_t len
= fwrite(accum_buf
, 1, accum_size
, outfile
);
1632 if (len
!= accum_size
)
1633 err
= got_ferror(outfile
, GOT_ERR_IO
);
1637 *result_size
= accum_size
;
1641 const struct got_error
*
1642 got_pack_dump_delta_chain_to_mem(uint8_t **outbuf
, size_t *outlen
,
1643 struct got_delta_chain
*deltas
, struct got_pack
*pack
)
1645 const struct got_error
*err
= NULL
;
1646 struct got_delta
*delta
;
1647 uint8_t *base_buf
= NULL
, *accum_buf
= NULL
;
1648 size_t base_bufsz
= 0, accum_bufsz
= 0, accum_size
= 0;
1649 uint64_t max_size
= 0;
1655 if (STAILQ_EMPTY(&deltas
->entries
))
1656 return got_error(GOT_ERR_BAD_DELTA_CHAIN
);
1658 if (pack
->delta_cache
) {
1659 uint8_t *delta_buf
= NULL
, *fulltext
= NULL
;
1660 size_t delta_len
, fulltext_len
;
1662 delta
= STAILQ_LAST(&deltas
->entries
, got_delta
, entry
);
1663 got_delta_cache_get(&delta_buf
, &delta_len
,
1664 &fulltext
, &fulltext_len
,
1665 pack
->delta_cache
, delta
->data_offset
);
1667 *outbuf
= malloc(fulltext_len
);
1668 if (*outbuf
== NULL
)
1669 return got_error_from_errno("malloc");
1670 memcpy(*outbuf
, fulltext
, fulltext_len
);
1671 *outlen
= fulltext_len
;
1676 /* Deltas are ordered in ascending order. */
1677 STAILQ_FOREACH(delta
, &deltas
->entries
, entry
) {
1678 uint8_t *delta_buf
= NULL
, *fulltext
= NULL
;
1679 size_t delta_len
, fulltext_len
= 0;
1680 uint64_t base_size
, result_size
= 0;
1683 off_t delta_data_offset
;
1685 /* Plain object types are the delta base. */
1686 if (delta
->type
!= GOT_OBJ_TYPE_COMMIT
&&
1687 delta
->type
!= GOT_OBJ_TYPE_TREE
&&
1688 delta
->type
!= GOT_OBJ_TYPE_BLOB
&&
1689 delta
->type
!= GOT_OBJ_TYPE_TAG
) {
1690 err
= got_error(GOT_ERR_BAD_DELTA_CHAIN
);
1694 delta_data_offset
= delta
->offset
+ delta
->tslen
;
1695 if (delta_data_offset
>= pack
->filesize
) {
1696 err
= got_error(GOT_ERR_PACK_OFFSET
);
1700 if (pack
->delta_cache
) {
1701 got_delta_cache_get(&delta_buf
, &delta_len
,
1702 &fulltext
, &fulltext_len
,
1703 pack
->delta_cache
, delta_data_offset
);
1706 if (delta
->size
> max_size
)
1707 max_size
= delta
->size
;
1708 if (delta
->size
> fulltext_len
)
1709 max_size
= fulltext_len
;
1712 base_buf
= malloc(fulltext_len
);
1713 if (base_buf
== NULL
) {
1714 err
= got_error_from_errno("malloc");
1717 memcpy(base_buf
, fulltext
, fulltext_len
);
1718 base_bufsz
= fulltext_len
;
1719 } else if (pack
->map
) {
1722 if (delta_data_offset
> SIZE_MAX
) {
1723 return got_error_fmt(GOT_ERR_RANGE
,
1724 "delta %lld offset would "
1726 (long long)delta_data_offset
);
1729 mapoff
= delta_data_offset
;
1730 err
= got_inflate_to_mem_mmap(&base_buf
,
1731 &base_bufsz
, NULL
, NULL
, pack
->map
,
1732 mapoff
, pack
->filesize
- mapoff
);
1734 if (lseek(pack
->fd
, delta_data_offset
, SEEK_SET
)
1736 err
= got_error_from_errno("lseek");
1739 err
= got_inflate_to_mem_fd(&base_buf
,
1740 &base_bufsz
, NULL
, NULL
, max_size
,
1747 if (pack
->delta_cache
&& fulltext
== NULL
) {
1748 err
= got_delta_cache_add(pack
->delta_cache
,
1749 delta_data_offset
, NULL
, 0);
1751 if (err
->code
!= GOT_ERR_NO_SPACE
)
1755 err
= got_delta_cache_add_fulltext(
1758 base_buf
, base_bufsz
);
1760 err
->code
!= GOT_ERR_NO_SPACE
)
1768 if (pack
->delta_cache
) {
1769 got_delta_cache_get(&delta_buf
, &delta_len
,
1770 &fulltext
, &fulltext_len
,
1771 pack
->delta_cache
, delta
->data_offset
);
1773 if (delta_buf
== NULL
) {
1775 err
= read_delta_data(&delta_buf
, &delta_len
, NULL
,
1776 delta
->data_offset
, pack
);
1780 if (pack
->delta_cache
&& !cached
) {
1781 err
= got_delta_cache_add(pack
->delta_cache
,
1782 delta
->data_offset
, delta_buf
, delta_len
);
1785 else if (err
->code
!= GOT_ERR_NO_SPACE
) {
1791 err
= got_delta_get_sizes(&base_size
, &result_size
,
1792 delta_buf
, delta_len
);
1798 if (base_size
> max_size
)
1799 max_size
= base_size
;
1800 if (result_size
> max_size
)
1801 max_size
= result_size
;
1802 if (fulltext_len
> max_size
)
1803 max_size
= fulltext_len
;
1805 if (max_size
> base_bufsz
) {
1806 uint8_t *p
= realloc(base_buf
, max_size
);
1808 err
= got_error_from_errno("realloc");
1814 base_bufsz
= max_size
;
1817 if (max_size
> accum_bufsz
) {
1818 uint8_t *p
= realloc(accum_buf
, max_size
);
1820 err
= got_error_from_errno("realloc");
1826 accum_bufsz
= max_size
;
1830 memcpy(accum_buf
, fulltext
, fulltext_len
);
1831 accum_size
= fulltext_len
;
1834 err
= got_delta_apply_in_mem(base_buf
, base_bufsz
,
1835 delta_buf
, delta_len
, accum_buf
,
1836 &accum_size
, max_size
);
1844 if (fulltext
== NULL
) {
1845 err
= got_delta_cache_add_fulltext(pack
->delta_cache
,
1846 delta
->data_offset
, accum_buf
, accum_size
);
1848 if (err
->code
!= GOT_ERR_NO_SPACE
)
1854 if (n
< deltas
->nentries
) {
1855 /* Accumulated delta becomes the new base. */
1856 uint8_t *tmp
= accum_buf
;
1857 size_t tmp_size
= accum_bufsz
;
1858 accum_buf
= base_buf
;
1859 accum_bufsz
= base_bufsz
;
1861 base_bufsz
= tmp_size
;
1872 *outbuf
= accum_buf
;
1873 *outlen
= accum_size
;
1878 const struct got_error
*
1879 got_packfile_extract_object(struct got_pack
*pack
, struct got_object
*obj
,
1880 FILE *outfile
, FILE *base_file
, FILE *accum_file
)
1882 const struct got_error
*err
= NULL
;
1884 if ((obj
->flags
& GOT_OBJ_FLAG_PACKED
) == 0)
1885 return got_error(GOT_ERR_OBJ_NOT_PACKED
);
1887 if ((obj
->flags
& GOT_OBJ_FLAG_DELTIFIED
) == 0) {
1888 if (obj
->pack_offset
>= pack
->filesize
)
1889 return got_error(GOT_ERR_PACK_OFFSET
);
1894 if (obj
->pack_offset
> SIZE_MAX
) {
1895 return got_error_fmt(GOT_ERR_RANGE
,
1896 "pack offset %lld would overflow size_t",
1897 (long long)obj
->pack_offset
);
1900 mapoff
= obj
->pack_offset
;
1901 err
= got_inflate_to_file_mmap(&obj
->size
, NULL
, NULL
,
1902 pack
->map
, mapoff
, pack
->filesize
- mapoff
,
1905 if (lseek(pack
->fd
, obj
->pack_offset
, SEEK_SET
) == -1)
1906 return got_error_from_errno("lseek");
1907 err
= got_inflate_to_file_fd(&obj
->size
, NULL
, NULL
,
1911 err
= got_pack_dump_delta_chain_to_file(&obj
->size
,
1912 &obj
->deltas
, pack
, outfile
, base_file
, accum_file
);
1917 const struct got_error
*
1918 got_packfile_extract_object_to_mem(uint8_t **buf
, size_t *len
,
1919 struct got_object
*obj
, struct got_pack
*pack
)
1921 const struct got_error
*err
= NULL
;
1923 if ((obj
->flags
& GOT_OBJ_FLAG_PACKED
) == 0)
1924 return got_error(GOT_ERR_OBJ_NOT_PACKED
);
1926 if ((obj
->flags
& GOT_OBJ_FLAG_DELTIFIED
) == 0) {
1927 if (obj
->pack_offset
>= pack
->filesize
)
1928 return got_error(GOT_ERR_PACK_OFFSET
);
1932 if (obj
->pack_offset
> SIZE_MAX
) {
1933 return got_error_fmt(GOT_ERR_RANGE
,
1934 "pack offset %lld would overflow size_t",
1935 (long long)obj
->pack_offset
);
1938 mapoff
= obj
->pack_offset
;
1939 err
= got_inflate_to_mem_mmap(buf
, len
, NULL
, NULL
,
1940 pack
->map
, mapoff
, pack
->filesize
- mapoff
);
1942 if (lseek(pack
->fd
, obj
->pack_offset
, SEEK_SET
) == -1)
1943 return got_error_from_errno("lseek");
1944 err
= got_inflate_to_mem_fd(buf
, len
, NULL
, NULL
,
1945 obj
->size
, pack
->fd
);
1948 err
= got_pack_dump_delta_chain_to_mem(buf
, len
, &obj
->deltas
,
1954 static const struct got_error
*
1955 read_raw_delta_data(uint8_t **delta_buf
, size_t *delta_len
,
1956 size_t *delta_len_compressed
, uint64_t *base_size
, uint64_t *result_size
,
1957 off_t delta_data_offset
, struct got_pack
*pack
, struct got_packidx
*packidx
)
1959 const struct got_error
*err
= NULL
;
1961 /* Validate decompression and obtain the decompressed size. */
1962 err
= read_delta_data(delta_buf
, delta_len
, delta_len_compressed
,
1963 delta_data_offset
, pack
);
1967 /* Read delta base/result sizes from head of delta stream. */
1968 err
= got_delta_get_sizes(base_size
, result_size
,
1969 *delta_buf
, *delta_len
);
1973 /* Discard decompressed delta and read it again in compressed form. */
1975 *delta_buf
= malloc(*delta_len_compressed
);
1976 if (*delta_buf
== NULL
) {
1977 err
= got_error_from_errno("malloc");
1981 if (delta_data_offset
>= pack
->filesize
) {
1982 err
= got_error(GOT_ERR_PACK_OFFSET
);
1985 memcpy(*delta_buf
, pack
->map
+ delta_data_offset
,
1986 *delta_len_compressed
);
1989 if (lseek(pack
->fd
, delta_data_offset
, SEEK_SET
) == -1) {
1990 err
= got_error_from_errno("lseek");
1993 n
= read(pack
->fd
, *delta_buf
, *delta_len_compressed
);
1995 err
= got_error_from_errno("read");
1997 } else if (n
!= *delta_len_compressed
) {
1998 err
= got_error(GOT_ERR_IO
);
2007 *delta_len_compressed
= 0;
2014 const struct got_error
*
2015 got_packfile_extract_raw_delta(uint8_t **delta_buf
, size_t *delta_size
,
2016 size_t *delta_compressed_size
, off_t
*delta_offset
,
2017 off_t
*delta_data_offset
, off_t
*base_offset
,
2018 struct got_object_id
*base_id
, uint64_t *base_size
, uint64_t *result_size
,
2019 struct got_pack
*pack
, struct got_packidx
*packidx
, int idx
)
2021 const struct got_error
*err
= NULL
;
2025 size_t tslen
, delta_hdrlen
;
2029 *delta_compressed_size
= 0;
2031 *delta_data_offset
= 0;
2036 offset
= got_packidx_get_object_offset(packidx
, idx
);
2038 return got_error(GOT_ERR_BAD_PACKIDX
);
2040 if (offset
>= pack
->filesize
)
2041 return got_error(GOT_ERR_PACK_OFFSET
);
2043 err
= got_pack_parse_object_type_and_size(&type
, &size
, &tslen
,
2048 if (tslen
+ size
< tslen
|| offset
+ size
< size
||
2049 tslen
+ offset
< tslen
)
2050 return got_error(GOT_ERR_PACK_OFFSET
);
2053 case GOT_OBJ_TYPE_OFFSET_DELTA
:
2054 err
= got_pack_parse_offset_delta(base_offset
, &delta_hdrlen
,
2055 pack
, offset
, tslen
);
2059 case GOT_OBJ_TYPE_REF_DELTA
:
2060 err
= got_pack_parse_ref_delta(base_id
, pack
, offset
, tslen
);
2063 delta_hdrlen
= SHA1_DIGEST_LENGTH
;
2066 return got_error_fmt(GOT_ERR_OBJ_TYPE
,
2067 "non-delta object type %d found at offset %lld",
2068 type
, (long long)offset
);
2071 if (tslen
+ delta_hdrlen
< delta_hdrlen
||
2072 offset
+ delta_hdrlen
< delta_hdrlen
)
2073 return got_error(GOT_ERR_BAD_DELTA
);
2075 *delta_data_offset
= offset
+ tslen
+ delta_hdrlen
;
2076 err
= read_raw_delta_data(delta_buf
, delta_size
, delta_compressed_size
,
2077 base_size
, result_size
, *delta_data_offset
, pack
, packidx
);
2081 if (*delta_size
!= size
) {
2082 err
= got_error(GOT_ERR_BAD_DELTA
);
2086 *delta_offset
= offset
;
2092 *delta_compressed_size
= 0;