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>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
37 #include "got_error.h"
38 #include "got_object.h"
41 #include "got_lib_hash.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_delta_cache.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_qid.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
52 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 static const struct got_error
*
60 verify_fanout_table(uint32_t *fanout_table
)
64 for (i
= 0; i
< 0xff - 1; i
++) {
65 if (be32toh(fanout_table
[i
]) > be32toh(fanout_table
[i
+ 1]))
66 return got_error(GOT_ERR_BAD_PACKIDX
);
72 const struct got_error
*
73 got_packidx_init_hdr(struct got_packidx
*p
, int verify
, off_t packfile_size
)
75 const struct got_error
*err
= NULL
;
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
, digest_string_len
;
83 got_hash_init(&ctx
, p
->algo
);
84 digest_string_len
= got_hash_digest_length(p
->algo
);
90 if (remain
< sizeof(*h
->magic
)) {
91 err
= got_error(GOT_ERR_BAD_PACKIDX
);
95 h
->magic
= (uint32_t *)(p
->map
+ offset
);
97 h
->magic
= malloc(sizeof(*h
->magic
));
98 if (h
->magic
== NULL
) {
99 err
= got_error_from_errno("malloc");
102 n
= read(p
->fd
, h
->magic
, sizeof(*h
->magic
));
104 err
= got_error_from_errno("read");
106 } else if (n
!= sizeof(*h
->magic
)) {
107 err
= got_error(GOT_ERR_BAD_PACKIDX
);
111 if (*h
->magic
!= htobe32(GOT_PACKIDX_V2_MAGIC
)) {
112 err
= got_error(GOT_ERR_BAD_PACKIDX
);
115 offset
+= sizeof(*h
->magic
);
116 remain
-= sizeof(*h
->magic
);
119 got_hash_update(&ctx
, h
->magic
, sizeof(*h
->magic
));
121 if (remain
< sizeof(*h
->version
)) {
122 err
= got_error(GOT_ERR_BAD_PACKIDX
);
126 h
->version
= (uint32_t *)(p
->map
+ offset
);
128 h
->version
= malloc(sizeof(*h
->version
));
129 if (h
->version
== NULL
) {
130 err
= got_error_from_errno("malloc");
133 n
= read(p
->fd
, h
->version
, sizeof(*h
->version
));
135 err
= got_error_from_errno("read");
137 } else if (n
!= sizeof(*h
->version
)) {
138 err
= got_error(GOT_ERR_BAD_PACKIDX
);
142 if (*h
->version
!= htobe32(GOT_PACKIDX_VERSION
)) {
143 err
= got_error(GOT_ERR_BAD_PACKIDX
);
146 offset
+= sizeof(*h
->version
);
147 remain
-= sizeof(*h
->version
);
150 got_hash_update(&ctx
, h
->version
, sizeof(*h
->version
));
153 sizeof(*h
->fanout_table
) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS
;
154 if (remain
< len_fanout
) {
155 err
= got_error(GOT_ERR_BAD_PACKIDX
);
159 h
->fanout_table
= (uint32_t *)(p
->map
+ offset
);
161 h
->fanout_table
= malloc(len_fanout
);
162 if (h
->fanout_table
== NULL
) {
163 err
= got_error_from_errno("malloc");
166 n
= read(p
->fd
, h
->fanout_table
, len_fanout
);
168 err
= got_error_from_errno("read");
170 } else if (n
!= len_fanout
) {
171 err
= got_error(GOT_ERR_BAD_PACKIDX
);
175 err
= verify_fanout_table(h
->fanout_table
);
179 got_hash_update(&ctx
, h
->fanout_table
, len_fanout
);
180 offset
+= len_fanout
;
181 remain
-= len_fanout
;
183 nobj
= be32toh(h
->fanout_table
[0xff]);
184 len_ids
= nobj
* got_hash_digest_length(p
->algo
);
185 if (len_ids
<= nobj
|| len_ids
> remain
) {
186 err
= got_error(GOT_ERR_BAD_PACKIDX
);
190 h
->sorted_ids
= 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
< digest_string_len
* 2) {
305 err
= got_error(GOT_ERR_BAD_PACKIDX
);
309 memcpy(h
->trailer
.packfile_hash
, p
->map
+ offset
,
311 memcpy(h
->trailer
.packidx_hash
,
312 p
->map
+ offset
+ digest_string_len
, digest_string_len
);
314 n
= read(p
->fd
, h
->trailer
.packfile_hash
, digest_string_len
);
316 err
= got_error_from_errno("read");
317 else if (n
!= digest_string_len
) {
318 err
= got_error(GOT_ERR_BAD_PACKIDX
);
321 n
= read(p
->fd
, h
->trailer
.packidx_hash
, digest_string_len
);
323 err
= got_error_from_errno("read");
324 else if (n
!= digest_string_len
) {
325 err
= got_error(GOT_ERR_BAD_PACKIDX
);
330 got_hash_update(&ctx
, h
->trailer
.packfile_hash
,
332 got_hash_final(&ctx
, hash
);
333 if (got_hash_cmp(ctx
.algo
, hash
, h
->trailer
.packidx_hash
) != 0)
334 err
= got_error(GOT_ERR_PACKIDX_CSUM
);
340 const struct got_error
*
341 got_packidx_open(struct got_packidx
**packidx
,
342 int dir_fd
, const char *relpath
, int verify
,
343 enum got_hash_algorithm algo
)
345 const struct got_error
*err
= NULL
;
346 struct got_packidx
*p
= NULL
;
348 struct stat idx_sb
, pack_sb
;
352 err
= got_packidx_get_packfile_path(&pack_relpath
, relpath
);
357 * Ensure that a corresponding pack file exists.
358 * Some Git repositories have this problem. Git seems to ignore
359 * the existence of lonely pack index files but we do not.
361 if (fstatat(dir_fd
, pack_relpath
, &pack_sb
, 0) == -1) {
363 err
= got_error_path(relpath
, GOT_ERR_LONELY_PACKIDX
);
365 err
= got_error_from_errno2("fstatat", pack_relpath
);
369 p
= calloc(1, sizeof(*p
));
371 err
= got_error_from_errno("calloc");
377 p
->fd
= openat(dir_fd
, relpath
, O_RDONLY
| O_NOFOLLOW
| O_CLOEXEC
);
379 err
= got_error_from_errno2("openat", relpath
);
383 if (fstat(p
->fd
, &idx_sb
) != 0) {
384 err
= got_error_from_errno2("fstat", relpath
);
387 p
->len
= idx_sb
.st_size
;
388 if (p
->len
< sizeof(p
->hdr
)) {
389 err
= got_error(GOT_ERR_BAD_PACKIDX
);
393 p
->path_packidx
= strdup(relpath
);
394 if (p
->path_packidx
== NULL
) {
395 err
= got_error_from_errno("strdup");
399 #ifndef GOT_PACK_NO_MMAP
400 if (p
->len
> 0 && p
->len
<= SIZE_MAX
) {
401 p
->map
= mmap(NULL
, p
->len
, PROT_READ
, MAP_PRIVATE
, p
->fd
, 0);
402 if (p
->map
== MAP_FAILED
) {
403 if (errno
!= ENOMEM
) {
404 err
= got_error_from_errno("mmap");
407 p
->map
= NULL
; /* fall back to read(2) */
412 err
= got_packidx_init_hdr(p
, verify
, pack_sb
.st_size
);
416 got_packidx_close(p
);
423 const struct got_error
*
424 got_packidx_close(struct got_packidx
*packidx
)
426 const struct got_error
*err
= NULL
;
428 free(packidx
->path_packidx
);
430 if (munmap(packidx
->map
, packidx
->len
) == -1)
431 err
= got_error_from_errno("munmap");
433 free(packidx
->hdr
.magic
);
434 free(packidx
->hdr
.version
);
435 free(packidx
->hdr
.fanout_table
);
436 free(packidx
->hdr
.sorted_ids
);
437 free(packidx
->hdr
.crc32
);
438 free(packidx
->hdr
.offsets
);
439 free(packidx
->hdr
.large_offsets
);
441 if (close(packidx
->fd
) == -1 && err
== NULL
)
442 err
= got_error_from_errno("close");
443 free(packidx
->sorted_offsets
);
444 free(packidx
->sorted_large_offsets
);
450 const struct got_error
*
451 got_packidx_get_packfile_path(char **path_packfile
, const char *path_packidx
)
455 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
456 size
= strlen(path_packidx
) + 2;
457 if (size
< GOT_PACKFILE_NAMELEN
+ 1)
458 return got_error_path(path_packidx
, GOT_ERR_BAD_PATH
);
460 *path_packfile
= malloc(size
);
461 if (*path_packfile
== NULL
)
462 return got_error_from_errno("malloc");
464 /* Copy up to and excluding ".idx". */
465 if (strlcpy(*path_packfile
, path_packidx
,
466 size
- strlen(GOT_PACKIDX_SUFFIX
) - 1) >= size
)
467 return got_error(GOT_ERR_NO_SPACE
);
469 if (strlcat(*path_packfile
, GOT_PACKFILE_SUFFIX
, size
) >= size
)
470 return got_error(GOT_ERR_NO_SPACE
);
476 got_packidx_get_object_offset(struct got_packidx
*packidx
, int idx
)
478 uint32_t offset
= be32toh(packidx
->hdr
.offsets
[idx
]);
479 if (offset
& GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX
) {
481 idx
= offset
& GOT_PACKIDX_OFFSET_VAL_MASK
;
482 if (idx
< 0 || idx
>= packidx
->nlargeobj
||
483 packidx
->hdr
.large_offsets
== NULL
)
485 loffset
= be64toh(packidx
->hdr
.large_offsets
[idx
]);
486 return (loffset
> INT64_MAX
? -1 : (off_t
)loffset
);
488 return (off_t
)(offset
& GOT_PACKIDX_OFFSET_VAL_MASK
);
492 got_packidx_get_object_idx(struct got_packidx
*packidx
,
493 struct got_object_id
*id
)
495 u_int8_t id0
= id
->hash
[0];
496 uint32_t totobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
497 int left
= 0, right
= totobj
- 1;
498 size_t digest_len
= got_hash_digest_length(packidx
->algo
);
501 left
= be32toh(packidx
->hdr
.fanout_table
[id0
- 1]);
503 while (left
<= right
) {
507 i
= ((left
+ right
) / 2);
508 oid
= packidx
->hdr
.sorted_ids
+ i
* digest_len
;
509 cmp
= memcmp(id
->hash
, oid
, digest_len
);
522 offset_cmp(const void *pa
, const void *pb
)
524 const struct got_pack_offset_index
*a
, *b
;
526 a
= (const struct got_pack_offset_index
*)pa
;
527 b
= (const struct got_pack_offset_index
*)pb
;
529 if (a
->offset
< b
->offset
)
531 else if (a
->offset
> b
->offset
)
538 large_offset_cmp(const void *pa
, const void *pb
)
540 const struct got_pack_large_offset_index
*a
, *b
;
542 a
= (const struct got_pack_large_offset_index
*)pa
;
543 b
= (const struct got_pack_large_offset_index
*)pb
;
545 if (a
->offset
< b
->offset
)
547 else if (a
->offset
> b
->offset
)
553 static const struct got_error
*
554 build_offset_index(struct got_packidx
*p
)
556 uint32_t nobj
= be32toh(p
->hdr
.fanout_table
[0xff]);
557 unsigned int i
, j
, k
;
559 p
->sorted_offsets
= calloc(nobj
- p
->nlargeobj
,
560 sizeof(p
->sorted_offsets
[0]));
561 if (p
->sorted_offsets
== NULL
)
562 return got_error_from_errno("calloc");
564 if (p
->nlargeobj
> 0) {
565 p
->sorted_large_offsets
= calloc(p
->nlargeobj
,
566 sizeof(p
->sorted_large_offsets
[0]));
567 if (p
->sorted_large_offsets
== NULL
)
568 return got_error_from_errno("calloc");
573 for (i
= 0; i
< nobj
; i
++) {
574 uint32_t offset
= be32toh(p
->hdr
.offsets
[i
]);
575 if (offset
& GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX
) {
578 idx
= offset
& GOT_PACKIDX_OFFSET_VAL_MASK
;
579 if (idx
>= p
->nlargeobj
||
581 p
->hdr
.large_offsets
== NULL
)
582 return got_error(GOT_ERR_BAD_PACKIDX
);
583 loffset
= be64toh(p
->hdr
.large_offsets
[idx
]);
584 p
->sorted_large_offsets
[j
].offset
= loffset
;
585 p
->sorted_large_offsets
[j
].idx
= i
;
588 p
->sorted_offsets
[k
].offset
= offset
;
589 p
->sorted_offsets
[k
].idx
= i
;
593 if (j
!= p
->nlargeobj
|| k
!= nobj
- p
->nlargeobj
)
594 return got_error(GOT_ERR_BAD_PACKIDX
);
596 qsort(p
->sorted_offsets
, nobj
- p
->nlargeobj
,
597 sizeof(p
->sorted_offsets
[0]), offset_cmp
);
599 if (p
->sorted_large_offsets
)
600 qsort(p
->sorted_large_offsets
, p
->nlargeobj
,
601 sizeof(p
->sorted_large_offsets
[0]), large_offset_cmp
);
606 const struct got_error
*
607 got_packidx_get_offset_idx(int *idx
, struct got_packidx
*packidx
, off_t offset
)
609 const struct got_error
*err
;
610 uint32_t totobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
615 if (packidx
->sorted_offsets
== NULL
) {
616 err
= build_offset_index(packidx
);
621 if (offset
>= 0x7fffffff) {
623 left
= 0, right
= packidx
->nlargeobj
- 1;
624 while (left
<= right
) {
625 i
= ((left
+ right
) / 2);
626 lo
= packidx
->sorted_large_offsets
[i
].offset
;
628 *idx
= packidx
->sorted_large_offsets
[i
].idx
;
630 } else if (offset
> lo
)
632 else if (offset
< lo
)
637 left
= 0, right
= totobj
- packidx
->nlargeobj
- 1;
638 while (left
<= right
) {
639 i
= ((left
+ right
) / 2);
640 o
= packidx
->sorted_offsets
[i
].offset
;
642 *idx
= packidx
->sorted_offsets
[i
].idx
;
644 } else if (offset
> o
)
654 const struct got_error
*
655 got_packidx_get_object_id(struct got_object_id
*id
,
656 struct got_packidx
*packidx
, int idx
)
658 uint32_t totobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
660 size_t digest_len
= got_hash_digest_length(packidx
->algo
);
662 if (idx
< 0 || idx
>= totobj
)
663 return got_error(GOT_ERR_NO_OBJ
);
665 oid
= packidx
->hdr
.sorted_ids
+ idx
* digest_len
;
666 memcpy(id
->hash
, oid
, digest_len
);
667 id
->algo
= packidx
->algo
;
671 const struct got_error
*
672 got_packidx_match_id_str_prefix(struct got_object_id_queue
*matched_ids
,
673 struct got_packidx
*packidx
, const char *id_str_prefix
)
675 const struct got_error
*err
= NULL
;
677 uint32_t totobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
679 size_t prefix_len
= strlen(id_str_prefix
);
682 size_t digest_len
= got_hash_digest_length(packidx
->algo
);
685 return got_error_path(id_str_prefix
, GOT_ERR_BAD_OBJ_ID_STR
);
687 hex
[0] = id_str_prefix
[0];
688 hex
[1] = id_str_prefix
[1];
690 if (!got_parse_xdigit(&id0
, hex
))
691 return got_error_path(id_str_prefix
, GOT_ERR_BAD_OBJ_ID_STR
);
694 i
= be32toh(packidx
->hdr
.fanout_table
[id0
- 1]);
695 oid
= packidx
->hdr
.sorted_ids
+ i
* digest_len
;
696 while (i
< totobj
&& oid
[0] == id0
) {
697 char id_str
[GOT_HASH_DIGEST_STRING_MAXLEN
];
698 struct got_object_qid
*qid
;
701 if (!got_hash_digest_to_str(oid
, id_str
, sizeof(id_str
),
703 return got_error(GOT_ERR_NO_SPACE
);
705 cmp
= strncmp(id_str
, id_str_prefix
, prefix_len
);
707 oid
= packidx
->hdr
.sorted_ids
+ (++i
) * digest_len
;
712 err
= got_object_qid_alloc_partial(&qid
);
715 memcpy(qid
->id
.hash
, oid
, digest_len
);
716 qid
->id
.algo
= packidx
->algo
;
717 STAILQ_INSERT_TAIL(matched_ids
, qid
, entry
);
719 oid
= packidx
->hdr
.sorted_ids
+ (++i
) * digest_len
;
726 set_max_datasize(void)
730 if (getrlimit(RLIMIT_DATA
, &rl
) != 0)
733 rl
.rlim_cur
= rl
.rlim_max
;
734 setrlimit(RLIMIT_DATA
, &rl
);
737 const struct got_error
*
738 got_pack_start_privsep_child(struct got_pack
*pack
, struct got_packidx
*packidx
)
740 const struct got_error
*err
= NULL
;
743 struct imsgbuf
*ibuf
;
745 ibuf
= calloc(1, sizeof(*ibuf
));
747 return got_error_from_errno("calloc");
749 pack
->privsep_child
= calloc(1, sizeof(*pack
->privsep_child
));
750 if (pack
->privsep_child
== NULL
) {
751 err
= got_error_from_errno("calloc");
755 pack
->child_has_tempfiles
= 0;
756 pack
->child_has_delta_outfd
= 0;
758 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, imsg_fds
) == -1) {
759 err
= got_error_from_errno("socketpair");
765 err
= got_error_from_errno("fork");
769 } else if (pid
== 0) {
771 got_privsep_exec_child(imsg_fds
, GOT_PATH_PROG_READ_PACK
,
772 pack
->path_packfile
);
776 if (close(imsg_fds
[1]) == -1) {
777 err
= got_error_from_errno("close");
781 pack
->privsep_child
->imsg_fd
= imsg_fds
[0];
782 pack
->privsep_child
->pid
= pid
;
783 if (imsgbuf_init(ibuf
, imsg_fds
[0]) == -1) {
784 err
= got_error_from_errno("imsgbuf_init");
788 imsgbuf_allow_fdpass(ibuf
);
790 pack
->privsep_child
->ibuf
= ibuf
;
792 err
= got_privsep_init_pack_child(ibuf
, pack
, packidx
);
794 const struct got_error
*child_err
;
795 err
= got_privsep_send_stop(pack
->privsep_child
->imsg_fd
);
796 child_err
= got_privsep_wait_for_child(
797 pack
->privsep_child
->pid
);
798 if (child_err
&& err
== NULL
)
805 free(pack
->privsep_child
);
806 pack
->privsep_child
= NULL
;
811 static const struct got_error
*
812 pack_stop_privsep_child(struct got_pack
*pack
)
814 const struct got_error
*err
= NULL
, *close_err
= NULL
;
816 if (pack
->privsep_child
== NULL
)
819 err
= got_privsep_send_stop(pack
->privsep_child
->imsg_fd
);
822 if (close(pack
->privsep_child
->imsg_fd
) == -1)
823 close_err
= got_error_from_errno("close");
824 err
= got_privsep_wait_for_child(pack
->privsep_child
->pid
);
825 if (close_err
&& err
== NULL
)
827 imsgbuf_clear(pack
->privsep_child
->ibuf
);
828 free(pack
->privsep_child
->ibuf
);
829 free(pack
->privsep_child
);
830 pack
->privsep_child
= NULL
;
834 const struct got_error
*
835 got_pack_close(struct got_pack
*pack
)
837 const struct got_error
*err
= NULL
;
839 err
= pack_stop_privsep_child(pack
);
840 if (pack
->map
&& munmap(pack
->map
, pack
->filesize
) == -1 && !err
)
841 err
= got_error_from_errno("munmap");
842 if (pack
->fd
!= -1 && close(pack
->fd
) == -1 && err
== NULL
)
843 err
= got_error_from_errno("close");
845 free(pack
->path_packfile
);
846 pack
->path_packfile
= NULL
;
848 if (pack
->delta_cache
) {
849 got_delta_cache_free(pack
->delta_cache
);
850 pack
->delta_cache
= NULL
;
854 * Leave accumfd and basefd alone. They are managed by the
855 * repository layer and can be reused.
861 const struct got_error
*
862 got_pack_parse_object_type_and_size(uint8_t *type
, uint64_t *size
, size_t *len
,
863 struct got_pack
*pack
, off_t offset
)
873 if (offset
>= pack
->filesize
)
874 return got_error(GOT_ERR_PACK_OFFSET
);
877 if (offset
> SIZE_MAX
) {
878 return got_error_fmt(GOT_ERR_PACK_OFFSET
,
879 "offset %lld overflows size_t",
883 mapoff
= (size_t)offset
;
885 if (lseek(pack
->fd
, offset
, SEEK_SET
) == -1)
886 return got_error_from_errno("lseek");
890 /* We do not support size values which don't fit in 64 bit. */
892 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE
,
893 "packfile offset %lld", (long long)offset
);
896 if (mapoff
+ sizeof(sizeN
) >= pack
->filesize
)
897 return got_error(GOT_ERR_BAD_PACKFILE
);
898 sizeN
= *(pack
->map
+ mapoff
);
899 mapoff
+= sizeof(sizeN
);
901 ssize_t n
= read(pack
->fd
, &sizeN
, sizeof(sizeN
));
903 return got_error_from_errno("read");
904 if (n
!= sizeof(sizeN
))
905 return got_error(GOT_ERR_BAD_PACKFILE
);
907 *len
+= sizeof(sizeN
);
910 t
= (sizeN
& GOT_PACK_OBJ_SIZE0_TYPE_MASK
) >>
911 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT
;
912 s
= (sizeN
& GOT_PACK_OBJ_SIZE0_VAL_MASK
);
914 size_t shift
= 4 + 7 * (i
- 1);
915 s
|= ((sizeN
& GOT_PACK_OBJ_SIZE_VAL_MASK
) << shift
);
918 } while (sizeN
& GOT_PACK_OBJ_SIZE_MORE
);
925 static const struct got_error
*
926 open_plain_object(struct got_object
**obj
, struct got_object_id
*id
,
927 uint8_t type
, off_t offset
, size_t size
, int idx
)
929 *obj
= calloc(1, sizeof(**obj
));
931 return got_error_from_errno("calloc");
934 (*obj
)->flags
= GOT_OBJ_FLAG_PACKED
;
935 (*obj
)->pack_idx
= idx
;
938 memcpy(&(*obj
)->id
, id
, sizeof((*obj
)->id
));
939 (*obj
)->pack_offset
= offset
;
944 static const struct got_error
*
945 parse_negative_offset(int64_t *offset
, size_t *len
, struct got_pack
*pack
,
956 /* We do not support offset values which don't fit in 64 bit. */
958 return got_error(GOT_ERR_NO_SPACE
);
963 if (delta_offset
> SIZE_MAX
- *len
) {
964 return got_error_fmt(GOT_ERR_PACK_OFFSET
,
965 "mapoff %lld would overflow size_t",
966 (long long)delta_offset
+ *len
);
969 mapoff
= (size_t)delta_offset
+ *len
;
970 if (mapoff
+ sizeof(offN
) >= pack
->filesize
)
971 return got_error(GOT_ERR_PACK_OFFSET
);
972 offN
= *(pack
->map
+ mapoff
);
975 n
= read(pack
->fd
, &offN
, sizeof(offN
));
977 return got_error_from_errno("read");
978 if (n
!= sizeof(offN
))
979 return got_error(GOT_ERR_BAD_PACKFILE
);
981 *len
+= sizeof(offN
);
984 o
= (offN
& GOT_PACK_OBJ_DELTA_OFF_VAL_MASK
);
988 o
+= (offN
& GOT_PACK_OBJ_DELTA_OFF_VAL_MASK
);
991 } while (offN
& GOT_PACK_OBJ_DELTA_OFF_MORE
);
997 const struct got_error
*
998 got_pack_parse_offset_delta(off_t
*base_offset
, size_t *len
,
999 struct got_pack
*pack
, off_t offset
, size_t tslen
)
1001 const struct got_error
*err
;
1007 err
= parse_negative_offset(&negoffset
, &negofflen
, pack
,
1012 /* Compute the base object's offset (must be in the same pack file). */
1013 *base_offset
= (offset
- negoffset
);
1014 if (*base_offset
<= 0)
1015 return got_error(GOT_ERR_BAD_PACKFILE
);
1021 static const struct got_error
*
1022 read_delta_data(uint8_t **delta_buf
, size_t *delta_len
,
1023 size_t *delta_compressed_len
, size_t delta_data_offset
,
1024 struct got_pack
*pack
)
1026 const struct got_error
*err
= NULL
;
1027 size_t consumed
= 0;
1030 if (delta_data_offset
>= pack
->filesize
)
1031 return got_error(GOT_ERR_PACK_OFFSET
);
1032 err
= got_inflate_to_mem_mmap(delta_buf
, delta_len
,
1033 &consumed
, NULL
, pack
->map
, delta_data_offset
,
1034 pack
->filesize
- delta_data_offset
);
1038 if (lseek(pack
->fd
, delta_data_offset
, SEEK_SET
) == -1)
1039 return got_error_from_errno("lseek");
1040 err
= got_inflate_to_mem_fd(delta_buf
, delta_len
,
1041 &consumed
, NULL
, 0, pack
->fd
);
1046 if (delta_compressed_len
)
1047 *delta_compressed_len
= consumed
;
1052 static const struct got_error
*
1053 add_delta(struct got_delta_chain
*deltas
, off_t delta_offset
, size_t tslen
,
1054 int delta_type
, size_t delta_size
, off_t delta_data_offset
)
1056 struct got_delta
*delta
;
1058 delta
= got_delta_open(delta_offset
, tslen
, delta_type
, delta_size
,
1061 return got_error_from_errno("got_delta_open");
1062 /* delta is freed in got_object_close() */
1064 STAILQ_INSERT_HEAD(&deltas
->entries
, delta
, entry
);
1068 static const struct got_error
*
1069 resolve_offset_delta(struct got_delta_chain
*deltas
,
1070 struct got_packidx
*packidx
, struct got_pack
*pack
, off_t delta_offset
,
1071 size_t tslen
, int delta_type
, size_t delta_size
, unsigned int recursion
)
1073 const struct got_error
*err
;
1078 off_t delta_data_offset
;
1081 err
= got_pack_parse_offset_delta(&base_offset
, &consumed
, pack
,
1082 delta_offset
, tslen
);
1086 delta_data_offset
= delta_offset
+ tslen
+ consumed
;
1087 if (delta_data_offset
>= pack
->filesize
)
1088 return got_error(GOT_ERR_PACK_OFFSET
);
1090 if (pack
->map
== NULL
) {
1091 delta_data_offset
= lseek(pack
->fd
, 0, SEEK_CUR
);
1092 if (delta_data_offset
== -1)
1093 return got_error_from_errno("lseek");
1096 err
= add_delta(deltas
, delta_offset
, tslen
, delta_type
, delta_size
,
1101 /* An offset delta must be in the same packfile. */
1102 if (base_offset
>= pack
->filesize
)
1103 return got_error(GOT_ERR_PACK_OFFSET
);
1105 err
= got_pack_parse_object_type_and_size(&base_type
, &base_size
,
1106 &base_tslen
, pack
, base_offset
);
1110 return got_pack_resolve_delta_chain(deltas
, packidx
, pack
, base_offset
,
1111 base_tslen
, base_type
, base_size
, recursion
- 1);
1114 const struct got_error
*
1115 got_pack_parse_ref_delta(struct got_object_id
*id
,
1116 struct got_pack
*pack
, off_t delta_offset
, int tslen
)
1118 size_t digest_len
= got_hash_digest_length(pack
->algo
);
1120 memset(id
, 0, sizeof(*id
));
1121 id
->algo
= pack
->algo
;
1126 if (delta_offset
> SIZE_MAX
- tslen
) {
1127 return got_error_fmt(GOT_ERR_PACK_OFFSET
,
1128 "mapoff %lld would overflow size_t",
1129 (long long)delta_offset
+ tslen
);
1132 mapoff
= delta_offset
+ tslen
;
1133 if (mapoff
+ sizeof(*id
) >= pack
->filesize
)
1134 return got_error(GOT_ERR_PACK_OFFSET
);
1135 memcpy(id
->hash
, pack
->map
+ mapoff
, digest_len
);
1138 n
= read(pack
->fd
, id
->hash
, digest_len
);
1140 return got_error_from_errno("read");
1141 if (n
!= digest_len
)
1142 return got_error(GOT_ERR_BAD_PACKFILE
);
1148 static const struct got_error
*
1149 resolve_ref_delta(struct got_delta_chain
*deltas
, struct got_packidx
*packidx
,
1150 struct got_pack
*pack
, off_t delta_offset
, size_t tslen
, int delta_type
,
1151 size_t delta_size
, unsigned int recursion
)
1153 const struct got_error
*err
;
1154 struct got_object_id id
;
1160 off_t delta_data_offset
;
1162 if (delta_offset
+ tslen
>= pack
->filesize
)
1163 return got_error(GOT_ERR_PACK_OFFSET
);
1165 err
= got_pack_parse_ref_delta(&id
, pack
, delta_offset
, tslen
);
1169 delta_data_offset
= delta_offset
+ tslen
+
1170 got_hash_digest_length(packidx
->algo
);
1172 delta_data_offset
= lseek(pack
->fd
, 0, SEEK_CUR
);
1173 if (delta_data_offset
== -1)
1174 return got_error_from_errno("lseek");
1177 err
= add_delta(deltas
, delta_offset
, tslen
, delta_type
, delta_size
,
1182 /* Delta base must be in the same pack file. */
1183 idx
= got_packidx_get_object_idx(packidx
, &id
);
1185 return got_error(GOT_ERR_NO_OBJ
);
1187 base_offset
= got_packidx_get_object_offset(packidx
, idx
);
1188 if (base_offset
== -1)
1189 return got_error(GOT_ERR_BAD_PACKIDX
);
1191 if (base_offset
>= pack
->filesize
)
1192 return got_error(GOT_ERR_PACK_OFFSET
);
1194 err
= got_pack_parse_object_type_and_size(&base_type
, &base_size
,
1195 &base_tslen
, pack
, base_offset
);
1199 return got_pack_resolve_delta_chain(deltas
, packidx
, pack
, base_offset
,
1200 base_tslen
, base_type
, base_size
, recursion
- 1);
1203 const struct got_error
*
1204 got_pack_resolve_delta_chain(struct got_delta_chain
*deltas
,
1205 struct got_packidx
*packidx
, struct got_pack
*pack
, off_t delta_offset
,
1206 size_t tslen
, int delta_type
, size_t delta_size
, unsigned int recursion
)
1208 const struct got_error
*err
= NULL
;
1210 if (--recursion
== 0)
1211 return got_error(GOT_ERR_RECURSION
);
1213 switch (delta_type
) {
1214 case GOT_OBJ_TYPE_COMMIT
:
1215 case GOT_OBJ_TYPE_TREE
:
1216 case GOT_OBJ_TYPE_BLOB
:
1217 case GOT_OBJ_TYPE_TAG
:
1218 /* Plain types are the final delta base. Recursion ends. */
1219 err
= add_delta(deltas
, delta_offset
, tslen
, delta_type
,
1222 case GOT_OBJ_TYPE_OFFSET_DELTA
:
1223 err
= resolve_offset_delta(deltas
, packidx
, pack
,
1224 delta_offset
, tslen
, delta_type
, delta_size
, recursion
- 1);
1226 case GOT_OBJ_TYPE_REF_DELTA
:
1227 err
= resolve_ref_delta(deltas
, packidx
, pack
,
1228 delta_offset
, tslen
, delta_type
, delta_size
, recursion
- 1);
1231 return got_error(GOT_ERR_OBJ_TYPE
);
1237 static const struct got_error
*
1238 open_delta_object(struct got_object
**obj
, struct got_packidx
*packidx
,
1239 struct got_pack
*pack
, struct got_object_id
*id
, off_t offset
,
1240 size_t tslen
, int delta_type
, size_t delta_size
, int idx
)
1242 const struct got_error
*err
= NULL
;
1245 *obj
= calloc(1, sizeof(**obj
));
1247 return got_error_from_errno("calloc");
1251 (*obj
)->size
= 0; /* Not known because deltas aren't applied yet. */
1252 memcpy(&(*obj
)->id
, id
, sizeof((*obj
)->id
));
1253 (*obj
)->pack_offset
= offset
+ tslen
;
1255 STAILQ_INIT(&(*obj
)->deltas
.entries
);
1256 (*obj
)->flags
|= GOT_OBJ_FLAG_DELTIFIED
;
1257 (*obj
)->flags
|= GOT_OBJ_FLAG_PACKED
;
1258 (*obj
)->pack_idx
= idx
;
1260 err
= got_pack_resolve_delta_chain(&(*obj
)->deltas
, packidx
, pack
,
1261 offset
, tslen
, delta_type
, delta_size
,
1262 GOT_DELTA_CHAIN_RECURSION_MAX
);
1266 err
= got_delta_chain_get_base_type(&resolved_type
, &(*obj
)->deltas
);
1269 (*obj
)->type
= resolved_type
;
1272 got_object_close(*obj
);
1278 const struct got_error
*
1279 got_packfile_open_object(struct got_object
**obj
, struct got_pack
*pack
,
1280 struct got_packidx
*packidx
, int idx
, struct got_object_id
*id
)
1282 const struct got_error
*err
= NULL
;
1290 offset
= got_packidx_get_object_offset(packidx
, idx
);
1292 return got_error(GOT_ERR_BAD_PACKIDX
);
1294 err
= got_pack_parse_object_type_and_size(&type
, &size
, &tslen
,
1300 case GOT_OBJ_TYPE_COMMIT
:
1301 case GOT_OBJ_TYPE_TREE
:
1302 case GOT_OBJ_TYPE_BLOB
:
1303 case GOT_OBJ_TYPE_TAG
:
1304 err
= open_plain_object(obj
, id
, type
, offset
+ tslen
,
1307 case GOT_OBJ_TYPE_OFFSET_DELTA
:
1308 case GOT_OBJ_TYPE_REF_DELTA
:
1309 err
= open_delta_object(obj
, packidx
, pack
, id
, offset
,
1310 tslen
, type
, size
, idx
);
1313 err
= got_error(GOT_ERR_OBJ_TYPE
);
1320 const struct got_error
*
1321 got_pack_get_delta_chain_max_size(uint64_t *max_size
,
1322 struct got_delta_chain
*deltas
, struct got_pack
*pack
)
1324 struct got_delta
*delta
;
1325 uint64_t base_size
= 0, result_size
= 0;
1328 STAILQ_FOREACH(delta
, &deltas
->entries
, entry
) {
1329 /* Plain object types are the delta base. */
1330 if (delta
->type
!= GOT_OBJ_TYPE_COMMIT
&&
1331 delta
->type
!= GOT_OBJ_TYPE_TREE
&&
1332 delta
->type
!= GOT_OBJ_TYPE_BLOB
&&
1333 delta
->type
!= GOT_OBJ_TYPE_TAG
) {
1334 const struct got_error
*err
;
1335 uint8_t *delta_buf
= NULL
;
1339 if (pack
->delta_cache
) {
1340 got_delta_cache_get(&delta_buf
, &delta_len
,
1341 NULL
, NULL
, pack
->delta_cache
,
1342 delta
->data_offset
);
1344 if (delta_buf
== NULL
) {
1346 err
= read_delta_data(&delta_buf
, &delta_len
,
1347 NULL
, delta
->data_offset
, pack
);
1351 if (pack
->delta_cache
&& !cached
) {
1352 err
= got_delta_cache_add(pack
->delta_cache
,
1353 delta
->data_offset
, delta_buf
, delta_len
);
1356 else if (err
->code
!= GOT_ERR_NO_SPACE
) {
1361 err
= got_delta_get_sizes(&base_size
, &result_size
,
1362 delta_buf
, delta_len
);
1368 base_size
= delta
->size
;
1369 if (base_size
> *max_size
)
1370 *max_size
= base_size
;
1371 if (result_size
> *max_size
)
1372 *max_size
= result_size
;
1378 const struct got_error
*
1379 got_pack_get_max_delta_object_size(uint64_t *size
, struct got_object
*obj
,
1380 struct got_pack
*pack
)
1382 if ((obj
->flags
& GOT_OBJ_FLAG_DELTIFIED
) == 0)
1383 return got_error(GOT_ERR_OBJ_TYPE
);
1385 return got_pack_get_delta_chain_max_size(size
, &obj
->deltas
, pack
);
1388 const struct got_error
*
1389 got_pack_dump_delta_chain_to_file(size_t *result_size
,
1390 struct got_delta_chain
*deltas
, struct got_pack
*pack
, FILE *outfile
,
1391 FILE *base_file
, FILE *accum_file
)
1393 const struct got_error
*err
= NULL
;
1394 struct got_delta
*delta
;
1395 uint8_t *base_buf
= NULL
, *accum_buf
= NULL
;
1396 size_t base_bufsz
= 0, accum_bufsz
= 0, accum_size
= 0;
1397 /* We process small enough files entirely in memory for speed. */
1398 const size_t max_bufsize
= GOT_DELTA_RESULT_SIZE_CACHED_MAX
;
1399 uint64_t max_size
= 0;
1404 if (STAILQ_EMPTY(&deltas
->entries
))
1405 return got_error(GOT_ERR_BAD_DELTA_CHAIN
);
1407 if (pack
->delta_cache
) {
1408 uint8_t *delta_buf
= NULL
, *fulltext
= NULL
;
1409 size_t delta_len
, fulltext_len
;
1411 delta
= STAILQ_LAST(&deltas
->entries
, got_delta
, entry
);
1412 got_delta_cache_get(&delta_buf
, &delta_len
,
1413 &fulltext
, &fulltext_len
,
1414 pack
->delta_cache
, delta
->data_offset
);
1418 w
= fwrite(fulltext
, 1, fulltext_len
, outfile
);
1419 if (w
!= fulltext_len
)
1420 return got_ferror(outfile
, GOT_ERR_IO
);
1421 if (fflush(outfile
) != 0)
1422 return got_error_from_errno("fflush");
1423 *result_size
= fulltext_len
;
1428 if (fseeko(base_file
, 0L, SEEK_SET
) == -1)
1429 return got_error_from_errno("fseeko");
1430 if (fseeko(accum_file
, 0L, SEEK_SET
) == -1)
1431 return got_error_from_errno("fseeko");
1433 /* Deltas are ordered in ascending order. */
1434 STAILQ_FOREACH(delta
, &deltas
->entries
, entry
) {
1435 uint8_t *delta_buf
= NULL
, *fulltext
= NULL
;
1436 size_t delta_len
, fulltext_len
;
1437 uint64_t base_size
, result_size
= 0;
1441 off_t delta_data_offset
;
1443 /* Plain object types are the delta base. */
1444 if (delta
->type
!= GOT_OBJ_TYPE_COMMIT
&&
1445 delta
->type
!= GOT_OBJ_TYPE_TREE
&&
1446 delta
->type
!= GOT_OBJ_TYPE_BLOB
&&
1447 delta
->type
!= GOT_OBJ_TYPE_TAG
) {
1448 err
= got_error(GOT_ERR_BAD_DELTA_CHAIN
);
1452 delta_data_offset
= delta
->offset
+ delta
->tslen
;
1453 if (delta_data_offset
>= pack
->filesize
) {
1454 err
= got_error(GOT_ERR_PACK_OFFSET
);
1457 if (pack
->map
== NULL
) {
1458 if (lseek(pack
->fd
, delta_data_offset
, SEEK_SET
)
1460 err
= got_error_from_errno("lseek");
1464 if (delta
->size
> max_size
)
1465 max_size
= delta
->size
;
1466 if (max_size
> max_bufsize
) {
1468 if (delta_data_offset
> SIZE_MAX
) {
1469 return got_error_fmt(
1471 "delta offset %lld "
1477 mapoff
= delta_data_offset
;
1478 err
= got_inflate_to_file_mmap(
1479 &base_bufsz
, NULL
, NULL
, pack
->map
,
1480 mapoff
, pack
->filesize
- mapoff
,
1483 err
= got_inflate_to_file_fd(
1484 &base_bufsz
, NULL
, NULL
, pack
->fd
,
1487 accum_buf
= malloc(max_size
);
1488 if (accum_buf
== NULL
) {
1489 err
= got_error_from_errno("malloc");
1492 accum_bufsz
= max_size
;
1494 if (delta_data_offset
> SIZE_MAX
) {
1495 err
= got_error_fmt(
1497 "delta offset %lld "
1504 mapoff
= delta_data_offset
;
1505 err
= got_inflate_to_mem_mmap(&base_buf
,
1506 &base_bufsz
, NULL
, NULL
,
1508 pack
->filesize
- mapoff
);
1510 err
= got_inflate_to_mem_fd(&base_buf
,
1511 &base_bufsz
, NULL
, NULL
, max_size
,
1517 if (base_buf
== NULL
)
1519 else if (pack
->delta_cache
&& fulltext
== NULL
) {
1520 err
= got_delta_cache_add(pack
->delta_cache
,
1521 delta_data_offset
, NULL
, 0);
1523 if (err
->code
!= GOT_ERR_NO_SPACE
)
1527 err
= got_delta_cache_add_fulltext(
1530 base_buf
, base_bufsz
);
1532 err
->code
!= GOT_ERR_NO_SPACE
)
1540 if (pack
->delta_cache
) {
1541 got_delta_cache_get(&delta_buf
, &delta_len
,
1542 &fulltext
, &fulltext_len
,
1543 pack
->delta_cache
, delta
->data_offset
);
1545 if (delta_buf
== NULL
) {
1547 err
= read_delta_data(&delta_buf
, &delta_len
, NULL
,
1548 delta
->data_offset
, pack
);
1552 if (pack
->delta_cache
&& !cached
) {
1553 err
= got_delta_cache_add(pack
->delta_cache
,
1554 delta
->data_offset
, delta_buf
, delta_len
);
1557 else if (err
->code
!= GOT_ERR_NO_SPACE
) {
1563 err
= got_delta_get_sizes(&base_size
, &result_size
,
1564 delta_buf
, delta_len
);
1570 if (base_size
> max_size
)
1571 max_size
= base_size
;
1572 if (result_size
> max_size
)
1573 max_size
= result_size
;
1574 if (fulltext_len
> max_size
)
1575 max_size
= fulltext_len
;
1577 if (base_buf
&& max_size
> max_bufsize
) {
1578 /* Switch from buffers to temporary files. */
1579 size_t w
= fwrite(base_buf
, 1, base_bufsz
,
1581 if (w
!= base_bufsz
) {
1582 err
= got_ferror(outfile
, GOT_ERR_IO
);
1593 if (base_buf
&& max_size
> base_bufsz
) {
1594 uint8_t *p
= realloc(base_buf
, max_size
);
1596 err
= got_error_from_errno("realloc");
1602 base_bufsz
= max_size
;
1605 if (accum_buf
&& max_size
> accum_bufsz
) {
1606 uint8_t *p
= realloc(accum_buf
, max_size
);
1608 err
= got_error_from_errno("realloc");
1614 accum_bufsz
= max_size
;
1619 memcpy(accum_buf
, fulltext
, fulltext_len
);
1620 accum_size
= fulltext_len
;
1623 err
= got_delta_apply_in_mem(base_buf
,
1624 base_bufsz
, delta_buf
, delta_len
,
1625 accum_buf
, &accum_size
, max_size
);
1632 if (fulltext
== NULL
) {
1633 err
= got_delta_cache_add_fulltext(
1634 pack
->delta_cache
, delta
->data_offset
,
1635 accum_buf
, accum_size
);
1637 if (err
->code
!= GOT_ERR_NO_SPACE
)
1643 err
= got_delta_apply(base_file
, delta_buf
,
1645 /* Final delta application writes to output file. */
1646 ++n
< deltas
->nentries
? accum_file
: outfile
,
1654 if (n
< deltas
->nentries
) {
1655 /* Accumulated delta becomes the new base. */
1657 uint8_t *tmp
= accum_buf
;
1658 size_t tmp_size
= accum_bufsz
;
1659 accum_buf
= base_buf
;
1660 accum_bufsz
= base_bufsz
;
1662 base_bufsz
= tmp_size
;
1664 FILE *tmp
= accum_file
;
1665 accum_file
= base_file
;
1680 size_t len
= fwrite(accum_buf
, 1, accum_size
, outfile
);
1682 if (len
!= accum_size
)
1683 err
= got_ferror(outfile
, GOT_ERR_IO
);
1687 *result_size
= accum_size
;
1691 const struct got_error
*
1692 got_pack_dump_delta_chain_to_mem(uint8_t **outbuf
, size_t *outlen
,
1693 struct got_delta_chain
*deltas
, struct got_pack
*pack
)
1695 const struct got_error
*err
= NULL
;
1696 struct got_delta
*delta
;
1697 uint8_t *base_buf
= NULL
, *accum_buf
= NULL
;
1698 size_t base_bufsz
= 0, accum_bufsz
= 0, accum_size
= 0;
1699 uint64_t max_size
= 0;
1705 if (STAILQ_EMPTY(&deltas
->entries
))
1706 return got_error(GOT_ERR_BAD_DELTA_CHAIN
);
1708 if (pack
->delta_cache
) {
1709 uint8_t *delta_buf
= NULL
, *fulltext
= NULL
;
1710 size_t delta_len
, fulltext_len
;
1712 delta
= STAILQ_LAST(&deltas
->entries
, got_delta
, entry
);
1713 got_delta_cache_get(&delta_buf
, &delta_len
,
1714 &fulltext
, &fulltext_len
,
1715 pack
->delta_cache
, delta
->data_offset
);
1717 *outbuf
= malloc(fulltext_len
);
1718 if (*outbuf
== NULL
)
1719 return got_error_from_errno("malloc");
1720 memcpy(*outbuf
, fulltext
, fulltext_len
);
1721 *outlen
= fulltext_len
;
1726 /* Deltas are ordered in ascending order. */
1727 STAILQ_FOREACH(delta
, &deltas
->entries
, entry
) {
1728 uint8_t *delta_buf
= NULL
, *fulltext
= NULL
;
1729 size_t delta_len
, fulltext_len
= 0;
1730 uint64_t base_size
, result_size
= 0;
1733 off_t delta_data_offset
;
1735 /* Plain object types are the delta base. */
1736 if (delta
->type
!= GOT_OBJ_TYPE_COMMIT
&&
1737 delta
->type
!= GOT_OBJ_TYPE_TREE
&&
1738 delta
->type
!= GOT_OBJ_TYPE_BLOB
&&
1739 delta
->type
!= GOT_OBJ_TYPE_TAG
) {
1740 err
= got_error(GOT_ERR_BAD_DELTA_CHAIN
);
1744 delta_data_offset
= delta
->offset
+ delta
->tslen
;
1745 if (delta_data_offset
>= pack
->filesize
) {
1746 err
= got_error(GOT_ERR_PACK_OFFSET
);
1750 if (pack
->delta_cache
) {
1751 got_delta_cache_get(&delta_buf
, &delta_len
,
1752 &fulltext
, &fulltext_len
,
1753 pack
->delta_cache
, delta_data_offset
);
1756 if (delta
->size
> max_size
)
1757 max_size
= delta
->size
;
1758 if (delta
->size
> fulltext_len
)
1759 max_size
= fulltext_len
;
1762 base_buf
= malloc(fulltext_len
);
1763 if (base_buf
== NULL
) {
1764 err
= got_error_from_errno("malloc");
1767 memcpy(base_buf
, fulltext
, fulltext_len
);
1768 base_bufsz
= fulltext_len
;
1769 } else if (pack
->map
) {
1772 if (delta_data_offset
> SIZE_MAX
) {
1773 return got_error_fmt(GOT_ERR_RANGE
,
1774 "delta %lld offset would "
1776 (long long)delta_data_offset
);
1779 mapoff
= delta_data_offset
;
1780 err
= got_inflate_to_mem_mmap(&base_buf
,
1781 &base_bufsz
, NULL
, NULL
, pack
->map
,
1782 mapoff
, pack
->filesize
- mapoff
);
1784 if (lseek(pack
->fd
, delta_data_offset
, SEEK_SET
)
1786 err
= got_error_from_errno("lseek");
1789 err
= got_inflate_to_mem_fd(&base_buf
,
1790 &base_bufsz
, NULL
, NULL
, max_size
,
1797 if (pack
->delta_cache
&& fulltext
== NULL
) {
1798 err
= got_delta_cache_add(pack
->delta_cache
,
1799 delta_data_offset
, NULL
, 0);
1801 if (err
->code
!= GOT_ERR_NO_SPACE
)
1805 err
= got_delta_cache_add_fulltext(
1808 base_buf
, base_bufsz
);
1810 err
->code
!= GOT_ERR_NO_SPACE
)
1818 if (pack
->delta_cache
) {
1819 got_delta_cache_get(&delta_buf
, &delta_len
,
1820 &fulltext
, &fulltext_len
,
1821 pack
->delta_cache
, delta
->data_offset
);
1823 if (delta_buf
== NULL
) {
1825 err
= read_delta_data(&delta_buf
, &delta_len
, NULL
,
1826 delta
->data_offset
, pack
);
1830 if (pack
->delta_cache
&& !cached
) {
1831 err
= got_delta_cache_add(pack
->delta_cache
,
1832 delta
->data_offset
, delta_buf
, delta_len
);
1835 else if (err
->code
!= GOT_ERR_NO_SPACE
) {
1841 err
= got_delta_get_sizes(&base_size
, &result_size
,
1842 delta_buf
, delta_len
);
1848 if (base_size
> max_size
)
1849 max_size
= base_size
;
1850 if (result_size
> max_size
)
1851 max_size
= result_size
;
1852 if (fulltext_len
> max_size
)
1853 max_size
= fulltext_len
;
1855 if (max_size
> base_bufsz
) {
1856 uint8_t *p
= realloc(base_buf
, max_size
);
1858 err
= got_error_from_errno("realloc");
1864 base_bufsz
= max_size
;
1867 if (max_size
> accum_bufsz
) {
1868 uint8_t *p
= realloc(accum_buf
, max_size
);
1870 err
= got_error_from_errno("realloc");
1876 accum_bufsz
= max_size
;
1880 memcpy(accum_buf
, fulltext
, fulltext_len
);
1881 accum_size
= fulltext_len
;
1884 err
= got_delta_apply_in_mem(base_buf
, base_bufsz
,
1885 delta_buf
, delta_len
, accum_buf
,
1886 &accum_size
, max_size
);
1894 if (fulltext
== NULL
) {
1895 err
= got_delta_cache_add_fulltext(pack
->delta_cache
,
1896 delta
->data_offset
, accum_buf
, accum_size
);
1898 if (err
->code
!= GOT_ERR_NO_SPACE
)
1904 if (n
< deltas
->nentries
) {
1905 /* Accumulated delta becomes the new base. */
1906 uint8_t *tmp
= accum_buf
;
1907 size_t tmp_size
= accum_bufsz
;
1908 accum_buf
= base_buf
;
1909 accum_bufsz
= base_bufsz
;
1911 base_bufsz
= tmp_size
;
1922 *outbuf
= accum_buf
;
1923 *outlen
= accum_size
;
1928 const struct got_error
*
1929 got_packfile_extract_object(struct got_pack
*pack
, struct got_object
*obj
,
1930 FILE *outfile
, FILE *base_file
, FILE *accum_file
)
1932 const struct got_error
*err
= NULL
;
1934 if ((obj
->flags
& GOT_OBJ_FLAG_PACKED
) == 0)
1935 return got_error(GOT_ERR_OBJ_NOT_PACKED
);
1937 if ((obj
->flags
& GOT_OBJ_FLAG_DELTIFIED
) == 0) {
1938 if (obj
->pack_offset
>= pack
->filesize
)
1939 return got_error(GOT_ERR_PACK_OFFSET
);
1944 if (obj
->pack_offset
> SIZE_MAX
) {
1945 return got_error_fmt(GOT_ERR_RANGE
,
1946 "pack offset %lld would overflow size_t",
1947 (long long)obj
->pack_offset
);
1950 mapoff
= obj
->pack_offset
;
1951 err
= got_inflate_to_file_mmap(&obj
->size
, NULL
, NULL
,
1952 pack
->map
, mapoff
, pack
->filesize
- mapoff
,
1955 if (lseek(pack
->fd
, obj
->pack_offset
, SEEK_SET
) == -1)
1956 return got_error_from_errno("lseek");
1957 err
= got_inflate_to_file_fd(&obj
->size
, NULL
, NULL
,
1961 err
= got_pack_dump_delta_chain_to_file(&obj
->size
,
1962 &obj
->deltas
, pack
, outfile
, base_file
, accum_file
);
1967 const struct got_error
*
1968 got_packfile_extract_object_to_mem(uint8_t **buf
, size_t *len
,
1969 struct got_object
*obj
, struct got_pack
*pack
)
1971 const struct got_error
*err
= NULL
;
1973 if ((obj
->flags
& GOT_OBJ_FLAG_PACKED
) == 0)
1974 return got_error(GOT_ERR_OBJ_NOT_PACKED
);
1976 if ((obj
->flags
& GOT_OBJ_FLAG_DELTIFIED
) == 0) {
1977 if (obj
->pack_offset
>= pack
->filesize
)
1978 return got_error(GOT_ERR_PACK_OFFSET
);
1982 if (obj
->pack_offset
> SIZE_MAX
) {
1983 return got_error_fmt(GOT_ERR_RANGE
,
1984 "pack offset %lld would overflow size_t",
1985 (long long)obj
->pack_offset
);
1988 mapoff
= obj
->pack_offset
;
1989 err
= got_inflate_to_mem_mmap(buf
, len
, NULL
, NULL
,
1990 pack
->map
, mapoff
, pack
->filesize
- mapoff
);
1992 if (lseek(pack
->fd
, obj
->pack_offset
, SEEK_SET
) == -1)
1993 return got_error_from_errno("lseek");
1994 err
= got_inflate_to_mem_fd(buf
, len
, NULL
, NULL
,
1995 obj
->size
, pack
->fd
);
1998 err
= got_pack_dump_delta_chain_to_mem(buf
, len
, &obj
->deltas
,
2004 static const struct got_error
*
2005 read_raw_delta_data(uint8_t **delta_buf
, size_t *delta_len
,
2006 size_t *delta_len_compressed
, uint64_t *base_size
, uint64_t *result_size
,
2007 off_t delta_data_offset
, struct got_pack
*pack
, struct got_packidx
*packidx
)
2009 const struct got_error
*err
= NULL
;
2011 /* Validate decompression and obtain the decompressed size. */
2012 err
= read_delta_data(delta_buf
, delta_len
, delta_len_compressed
,
2013 delta_data_offset
, pack
);
2017 /* Read delta base/result sizes from head of delta stream. */
2018 err
= got_delta_get_sizes(base_size
, result_size
,
2019 *delta_buf
, *delta_len
);
2023 /* Discard decompressed delta and read it again in compressed form. */
2025 *delta_buf
= malloc(*delta_len_compressed
);
2026 if (*delta_buf
== NULL
) {
2027 err
= got_error_from_errno("malloc");
2031 if (delta_data_offset
>= pack
->filesize
) {
2032 err
= got_error(GOT_ERR_PACK_OFFSET
);
2035 memcpy(*delta_buf
, pack
->map
+ delta_data_offset
,
2036 *delta_len_compressed
);
2039 if (lseek(pack
->fd
, delta_data_offset
, SEEK_SET
) == -1) {
2040 err
= got_error_from_errno("lseek");
2043 n
= read(pack
->fd
, *delta_buf
, *delta_len_compressed
);
2045 err
= got_error_from_errno("read");
2047 } else if (n
!= *delta_len_compressed
) {
2048 err
= got_error(GOT_ERR_IO
);
2057 *delta_len_compressed
= 0;
2064 const struct got_error
*
2065 got_packfile_extract_raw_delta(uint8_t **delta_buf
, size_t *delta_size
,
2066 size_t *delta_compressed_size
, off_t
*delta_offset
,
2067 off_t
*delta_data_offset
, off_t
*base_offset
,
2068 struct got_object_id
*base_id
, uint64_t *base_size
, uint64_t *result_size
,
2069 struct got_pack
*pack
, struct got_packidx
*packidx
, int idx
)
2071 const struct got_error
*err
= NULL
;
2075 size_t tslen
, delta_hdrlen
;
2079 *delta_compressed_size
= 0;
2081 *delta_data_offset
= 0;
2086 offset
= got_packidx_get_object_offset(packidx
, idx
);
2088 return got_error(GOT_ERR_BAD_PACKIDX
);
2090 if (offset
>= pack
->filesize
)
2091 return got_error(GOT_ERR_PACK_OFFSET
);
2093 err
= got_pack_parse_object_type_and_size(&type
, &size
, &tslen
,
2098 if (tslen
+ size
< tslen
|| offset
+ size
< size
||
2099 tslen
+ offset
< tslen
)
2100 return got_error(GOT_ERR_PACK_OFFSET
);
2103 case GOT_OBJ_TYPE_OFFSET_DELTA
:
2104 err
= got_pack_parse_offset_delta(base_offset
, &delta_hdrlen
,
2105 pack
, offset
, tslen
);
2109 case GOT_OBJ_TYPE_REF_DELTA
:
2110 err
= got_pack_parse_ref_delta(base_id
, pack
, offset
, tslen
);
2113 delta_hdrlen
= got_hash_digest_length(pack
->algo
);
2116 return got_error_fmt(GOT_ERR_OBJ_TYPE
,
2117 "non-delta object type %d found at offset %lld",
2118 type
, (long long)offset
);
2121 if (tslen
+ delta_hdrlen
< delta_hdrlen
||
2122 offset
+ delta_hdrlen
< delta_hdrlen
)
2123 return got_error(GOT_ERR_BAD_DELTA
);
2125 *delta_data_offset
= offset
+ tslen
+ delta_hdrlen
;
2126 err
= read_raw_delta_data(delta_buf
, delta_size
, delta_compressed_size
,
2127 base_size
, result_size
, *delta_data_offset
, pack
, packidx
);
2131 if (*delta_size
!= size
) {
2132 err
= got_error(GOT_ERR_BAD_DELTA
);
2136 *delta_offset
= offset
;
2142 *delta_compressed_size
= 0;