2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "got_compat.h"
20 #include <sys/types.h>
21 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "got_cancel.h"
41 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_repository_admin.h"
47 #include "got_lib_deltify.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_hash.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_idset.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_deflate.h"
54 #include "got_lib_ratelimit.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_pack_create.h"
57 #include "got_lib_repository.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_poll.h"
61 #include "murmurhash2.h"
64 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
68 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 static const struct got_error
*
76 alloc_meta(struct got_pack_meta
**new, struct got_object_id
*id
,
77 const char *path
, int obj_type
, time_t mtime
, uint32_t seed
)
79 struct got_pack_meta
*m
;
83 m
= calloc(1, sizeof(*m
));
85 return got_error_from_errno("calloc");
87 memcpy(&m
->id
, id
, sizeof(m
->id
));
89 m
->path_hash
= murmurhash2(path
, strlen(path
), seed
);
90 m
->obj_type
= obj_type
;
97 clear_meta(struct got_pack_meta
*meta
)
102 free(meta
->delta_buf
);
103 meta
->delta_buf
= NULL
;
104 free(meta
->base_obj_id
);
105 meta
->base_obj_id
= NULL
;
106 meta
->reused_delta_offset
= 0;
107 got_deltify_free(meta
->dtab
);
112 free_nmeta(struct got_pack_meta
**meta
, int nmeta
)
116 for (i
= 0; i
< nmeta
; i
++)
123 delta_order_cmp(const void *pa
, const void *pb
)
125 struct got_pack_meta
*a
, *b
;
127 a
= *(struct got_pack_meta
**)pa
;
128 b
= *(struct got_pack_meta
**)pb
;
130 if (a
->obj_type
!= b
->obj_type
)
131 return a
->obj_type
- b
->obj_type
;
132 if (a
->path_hash
< b
->path_hash
)
134 if (a
->path_hash
> b
->path_hash
)
136 if (a
->mtime
< b
->mtime
)
138 if (a
->mtime
> b
->mtime
)
140 return got_object_id_cmp(&a
->id
, &b
->id
);
144 delta_size(struct got_delta_instruction
*deltas
, int ndeltas
)
148 for (i
= 0; i
< ndeltas
; i
++) {
150 size
+= GOT_DELTA_SIZE_SHIFT
;
152 size
+= deltas
[i
].len
+ 1;
157 static const struct got_error
*
158 append(unsigned char **p
, size_t *len
, off_t
*sz
, void *seg
, int nseg
)
162 if (*len
+ nseg
>= *sz
) {
163 while (*len
+ nseg
>= *sz
)
165 n
= realloc(*p
, *sz
);
167 return got_error_from_errno("realloc");
170 memcpy(*p
+ *len
, seg
, nseg
);
175 static const struct got_error
*
176 encode_delta_in_mem(struct got_pack_meta
*m
, struct got_raw_object
*o
,
177 struct got_delta_instruction
*deltas
, int ndeltas
,
178 off_t delta_size
, off_t base_size
)
180 const struct got_error
*err
;
181 unsigned char buf
[16], *bp
;
183 size_t len
= 0, compressed_len
;
184 off_t bufsize
= delta_size
;
186 struct got_delta_instruction
*d
;
189 delta_buf
= malloc(bufsize
);
190 if (delta_buf
== NULL
)
191 return got_error_from_errno("malloc");
193 /* base object size */
194 buf
[0] = base_size
& GOT_DELTA_SIZE_VAL_MASK
;
195 n
= base_size
>> GOT_DELTA_SIZE_SHIFT
;
196 for (i
= 1; n
> 0; i
++) {
197 buf
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
198 buf
[i
] = n
& GOT_DELTA_SIZE_VAL_MASK
;
199 n
>>= GOT_DELTA_SIZE_SHIFT
;
201 err
= append(&delta_buf
, &len
, &bufsize
, buf
, i
);
205 /* target object size */
206 buf
[0] = o
->size
& GOT_DELTA_SIZE_VAL_MASK
;
207 n
= o
->size
>> GOT_DELTA_SIZE_SHIFT
;
208 for (i
= 1; n
> 0; i
++) {
209 buf
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
210 buf
[i
] = n
& GOT_DELTA_SIZE_VAL_MASK
;
211 n
>>= GOT_DELTA_SIZE_SHIFT
;
213 err
= append(&delta_buf
, &len
, &bufsize
, buf
, i
);
217 for (j
= 0; j
< ndeltas
; j
++) {
222 buf
[0] = GOT_DELTA_BASE_COPY
;
223 for (i
= 0; i
< 4; i
++) {
224 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
233 if (n
!= GOT_DELTA_COPY_DEFAULT_LEN
) {
234 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
235 for (i
= 0; i
< 3 && n
> 0; i
++) {
236 buf
[0] |= 1 << (i
+ 4);
241 err
= append(&delta_buf
, &len
, &bufsize
,
245 } else if (o
->f
== NULL
) {
247 while (n
!= d
->len
) {
248 buf
[0] = (d
->len
- n
< 127) ? d
->len
- n
: 127;
249 err
= append(&delta_buf
, &len
, &bufsize
,
253 err
= append(&delta_buf
, &len
, &bufsize
,
254 o
->data
+ o
->hdrlen
+ d
->offset
+ n
,
263 if (fseeko(o
->f
, o
->hdrlen
+ d
->offset
, SEEK_SET
) == -1) {
264 err
= got_error_from_errno("fseeko");
268 while (n
!= d
->len
) {
269 buf
[0] = (d
->len
- n
< 127) ? d
->len
- n
: 127;
270 err
= append(&delta_buf
, &len
, &bufsize
,
274 r
= fread(content
, 1, buf
[0], o
->f
);
276 err
= got_ferror(o
->f
, GOT_ERR_IO
);
279 err
= append(&delta_buf
, &len
, &bufsize
,
288 err
= got_deflate_to_mem_mmap(&m
->delta_buf
, &compressed_len
,
289 NULL
, NULL
, delta_buf
, 0, len
);
294 m
->delta_compressed_len
= compressed_len
;
300 static const struct got_error
*
301 encode_delta(struct got_pack_meta
*m
, struct got_raw_object
*o
,
302 struct got_delta_instruction
*deltas
, int ndeltas
,
303 off_t base_size
, FILE *f
)
305 const struct got_error
*err
;
306 unsigned char buf
[16], *bp
;
309 struct got_deflate_buf zb
;
310 struct got_delta_instruction
*d
;
311 off_t delta_len
= 0, compressed_len
= 0;
313 err
= got_deflate_init(&zb
, NULL
, GOT_DEFLATE_BUFSIZE
);
317 /* base object size */
318 buf
[0] = base_size
& GOT_DELTA_SIZE_VAL_MASK
;
319 n
= base_size
>> GOT_DELTA_SIZE_SHIFT
;
320 for (i
= 1; n
> 0; i
++) {
321 buf
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
322 buf
[i
] = n
& GOT_DELTA_SIZE_VAL_MASK
;
323 n
>>= GOT_DELTA_SIZE_SHIFT
;
326 err
= got_deflate_append_to_file_mmap(&zb
, &compressed_len
,
332 /* target object size */
333 buf
[0] = o
->size
& GOT_DELTA_SIZE_VAL_MASK
;
334 n
= o
->size
>> GOT_DELTA_SIZE_SHIFT
;
335 for (i
= 1; n
> 0; i
++) {
336 buf
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
337 buf
[i
] = n
& GOT_DELTA_SIZE_VAL_MASK
;
338 n
>>= GOT_DELTA_SIZE_SHIFT
;
341 err
= got_deflate_append_to_file_mmap(&zb
, &compressed_len
,
347 for (j
= 0; j
< ndeltas
; j
++) {
352 buf
[0] = GOT_DELTA_BASE_COPY
;
353 for (i
= 0; i
< 4; i
++) {
354 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
362 if (n
!= GOT_DELTA_COPY_DEFAULT_LEN
) {
363 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
364 for (i
= 0; i
< 3 && n
> 0; i
++) {
365 buf
[0] |= 1 << (i
+ 4);
370 err
= got_deflate_append_to_file_mmap(&zb
,
371 &compressed_len
, buf
, 0, bp
- buf
, f
, NULL
);
374 delta_len
+= (bp
- buf
);
375 } else if (o
->f
== NULL
) {
377 while (n
!= d
->len
) {
378 buf
[0] = (d
->len
- n
< 127) ? d
->len
- n
: 127;
379 err
= got_deflate_append_to_file_mmap(&zb
,
380 &compressed_len
, buf
, 0, 1, f
, NULL
);
384 err
= got_deflate_append_to_file_mmap(&zb
,
386 o
->data
+ o
->hdrlen
+ d
->offset
+ n
, 0,
396 if (fseeko(o
->f
, o
->hdrlen
+ d
->offset
, SEEK_SET
) == -1) {
397 err
= got_error_from_errno("fseeko");
401 while (n
!= d
->len
) {
402 buf
[0] = (d
->len
- n
< 127) ? d
->len
- n
: 127;
403 err
= got_deflate_append_to_file_mmap(&zb
,
404 &compressed_len
, buf
, 0, 1, f
, NULL
);
408 r
= fread(content
, 1, buf
[0], o
->f
);
410 err
= got_ferror(o
->f
, GOT_ERR_IO
);
413 err
= got_deflate_append_to_file_mmap(&zb
,
414 &compressed_len
, content
, 0, buf
[0], f
,
424 err
= got_deflate_flush(&zb
, f
, NULL
, &compressed_len
);
429 if (compressed_len
!= ftello(f
) - m
->delta_offset
) {
430 err
= got_error(GOT_ERR_COMPRESSION
);
434 m
->delta_len
= delta_len
;
435 m
->delta_compressed_len
= compressed_len
;
437 got_deflate_end(&zb
);
441 const struct got_error
*
442 got_pack_report_progress(got_pack_progress_cb progress_cb
, void *progress_arg
,
443 struct got_ratelimit
*rl
, int ncolored
, int nfound
, int ntrees
,
444 off_t packfile_size
, int ncommits
, int nobj_total
, int obj_deltify
,
447 const struct got_error
*err
;
450 if (progress_cb
== NULL
)
453 err
= got_ratelimit_check(&elapsed
, rl
);
457 return progress_cb(progress_arg
, ncolored
, nfound
, ntrees
,
458 packfile_size
, ncommits
, nobj_total
, obj_deltify
, nobj_written
);
461 const struct got_error
*
462 got_pack_add_meta(struct got_pack_meta
*m
, struct got_pack_metavec
*v
)
464 if (v
->nmeta
== v
->metasz
){
465 size_t newsize
= 2 * v
->metasz
;
466 struct got_pack_meta
**new;
467 new = reallocarray(v
->meta
, newsize
, sizeof(*new));
469 return got_error_from_errno("reallocarray");
474 v
->meta
[v
->nmeta
++] = m
;
478 const struct got_error
*
479 got_pack_find_pack_for_reuse(struct got_packidx
**best_packidx
,
480 struct got_repository
*repo
)
482 const struct got_error
*err
= NULL
;
483 struct got_pathlist_entry
*pe
;
484 const char *best_packidx_path
= NULL
;
487 *best_packidx
= NULL
;
489 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
) {
490 const char *path_packidx
= pe
->path
;
491 struct got_packidx
*packidx
;
494 err
= got_repo_get_packidx(&packidx
, path_packidx
, repo
);
498 nobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
499 if (nobj
> nobj_max
) {
500 best_packidx_path
= path_packidx
;
505 if (best_packidx_path
) {
506 err
= got_repo_get_packidx(best_packidx
, best_packidx_path
,
513 const struct got_error
*
514 got_pack_cache_pack_for_packidx(struct got_pack
**pack
,
515 struct got_packidx
*packidx
, struct got_repository
*repo
)
517 const struct got_error
*err
;
518 char *path_packfile
= NULL
;
520 err
= got_packidx_get_packfile_path(&path_packfile
,
521 packidx
->path_packidx
);
525 *pack
= got_repo_get_cached_pack(repo
, path_packfile
);
527 err
= got_repo_cache_pack(pack
, repo
, path_packfile
, packidx
);
536 static const struct got_error
*
537 pick_deltas(struct got_pack_meta
**meta
, int nmeta
, int ncolored
,
538 int nfound
, int ntrees
, int ncommits
, int nreused
, FILE *delta_cache
,
539 struct got_repository
*repo
,
540 got_pack_progress_cb progress_cb
, void *progress_arg
,
541 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
543 const struct got_error
*err
= NULL
;
544 struct got_pack_meta
*m
= NULL
, *base
= NULL
;
545 struct got_raw_object
*raw
= NULL
, *base_raw
= NULL
;
546 struct got_delta_instruction
*deltas
= NULL
, *best_deltas
= NULL
;
547 int i
, j
, ndeltas
, best_ndeltas
;
548 off_t size
, best_size
;
549 const int max_base_candidates
= 3;
550 size_t delta_memsize
= 0;
551 const size_t max_delta_memsize
= 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX
;
555 delta_seed
= arc4random();
557 qsort(meta
, nmeta
, sizeof(struct got_pack_meta
*), delta_order_cmp
);
558 for (i
= 0; i
< nmeta
; i
++) {
560 err
= (*cancel_cb
)(cancel_arg
);
564 err
= got_pack_report_progress(progress_cb
, progress_arg
, rl
,
565 ncolored
, nfound
, ntrees
, 0L, ncommits
, nreused
+ nmeta
,
571 if (m
->obj_type
== GOT_OBJ_TYPE_COMMIT
||
572 m
->obj_type
== GOT_OBJ_TYPE_TAG
)
575 err
= got_object_raw_open(&raw
, &outfd
, repo
, &m
->id
);
580 if (raw
->f
== NULL
) {
581 err
= got_deltify_init_mem(&m
->dtab
, raw
->data
,
582 raw
->hdrlen
, raw
->size
+ raw
->hdrlen
, delta_seed
);
584 err
= got_deltify_init(&m
->dtab
, raw
->f
, raw
->hdrlen
,
585 raw
->size
+ raw
->hdrlen
, delta_seed
);
590 if (i
> max_base_candidates
) {
591 struct got_pack_meta
*n
= NULL
;
592 n
= meta
[i
- (max_base_candidates
+ 1)];
593 got_deltify_free(n
->dtab
);
597 best_size
= raw
->size
;
599 for (j
= MAX(0, i
- max_base_candidates
); j
< i
; j
++) {
601 err
= (*cancel_cb
)(cancel_arg
);
606 /* long chains make unpacking slow, avoid such bases */
607 if (base
->nchain
>= 128 ||
608 base
->obj_type
!= m
->obj_type
)
611 err
= got_object_raw_open(&base_raw
, &outfd
, repo
,
616 if (raw
->f
== NULL
&& base_raw
->f
== NULL
) {
617 err
= got_deltify_mem_mem(&deltas
, &ndeltas
,
618 raw
->data
, raw
->hdrlen
,
619 raw
->size
+ raw
->hdrlen
, delta_seed
,
620 base
->dtab
, base_raw
->data
,
622 base_raw
->size
+ base_raw
->hdrlen
);
623 } else if (raw
->f
== NULL
) {
624 err
= got_deltify_mem_file(&deltas
, &ndeltas
,
625 raw
->data
, raw
->hdrlen
,
626 raw
->size
+ raw
->hdrlen
, delta_seed
,
627 base
->dtab
, base_raw
->f
,
629 base_raw
->size
+ base_raw
->hdrlen
);
630 } else if (base_raw
->f
== NULL
) {
631 err
= got_deltify_file_mem(&deltas
, &ndeltas
,
633 raw
->size
+ raw
->hdrlen
, delta_seed
,
634 base
->dtab
, base_raw
->data
,
636 base_raw
->size
+ base_raw
->hdrlen
);
638 err
= got_deltify(&deltas
, &ndeltas
,
640 raw
->size
+ raw
->hdrlen
, delta_seed
,
641 base
->dtab
, base_raw
->f
, base_raw
->hdrlen
,
642 base_raw
->size
+ base_raw
->hdrlen
);
644 got_object_raw_close(base_raw
);
649 size
= delta_size(deltas
, ndeltas
);
650 if (size
+ 32 < best_size
){
652 * if we already picked a best delta,
657 best_deltas
= deltas
;
658 best_ndeltas
= ndeltas
;
660 m
->nchain
= base
->nchain
+ 1;
662 m
->head
= base
->head
;
672 if (best_ndeltas
> 0) {
673 if (best_size
<= GOT_DELTA_RESULT_SIZE_CACHED_MAX
&&
674 delta_memsize
+ best_size
<= max_delta_memsize
) {
675 delta_memsize
+= best_size
;
676 err
= encode_delta_in_mem(m
, raw
, best_deltas
,
677 best_ndeltas
, best_size
, m
->prev
->size
);
679 m
->delta_offset
= ftello(delta_cache
);
680 err
= encode_delta(m
, raw
, best_deltas
,
681 best_ndeltas
, m
->prev
->size
, delta_cache
);
690 got_object_raw_close(raw
);
695 got_object_raw_close(raw
);
697 got_object_raw_close(base_raw
);
698 if (outfd
!= -1 && close(outfd
) == -1 && err
== NULL
)
699 err
= got_error_from_errno("close");
705 static const struct got_error
*
706 search_packidx(int *found
, struct got_object_id
*id
,
707 struct got_repository
*repo
)
709 const struct got_error
*err
= NULL
;
710 struct got_packidx
*packidx
= NULL
;
715 err
= got_repo_search_packidx(&packidx
, &idx
, repo
, id
);
717 *found
= 1; /* object is already packed */
718 else if (err
->code
== GOT_ERR_NO_OBJ
)
723 const struct got_error
*
724 got_pack_add_object(int want_meta
, struct got_object_idset
*idset
,
725 struct got_object_id
*id
, const char *path
, int obj_type
,
726 time_t mtime
, uint32_t seed
, int loose_obj_only
,
727 struct got_repository
*repo
, int *ncolored
, int *nfound
, int *ntrees
,
728 got_pack_progress_cb progress_cb
, void *progress_arg
,
729 struct got_ratelimit
*rl
)
731 const struct got_error
*err
;
732 struct got_pack_meta
*m
= NULL
;
734 if (loose_obj_only
) {
736 err
= search_packidx(&is_packed
, id
, repo
);
739 if (is_packed
&& want_meta
)
744 err
= alloc_meta(&m
, id
, path
, obj_type
, mtime
, seed
);
749 err
= got_pack_report_progress(progress_cb
, progress_arg
, rl
,
750 *ncolored
, *nfound
, *ntrees
, 0L, 0, 0, 0, 0);
758 err
= got_object_idset_add(idset
, id
, m
);
766 const struct got_error
*
767 got_pack_load_tree_entries(struct got_object_id_queue
*ids
, int want_meta
,
768 struct got_object_idset
*idset
, struct got_object_idset
*idset_exclude
,
769 struct got_tree_object
*tree
,
770 const char *dpath
, time_t mtime
, uint32_t seed
, struct got_repository
*repo
,
771 int loose_obj_only
, int *ncolored
, int *nfound
, int *ntrees
,
772 got_pack_progress_cb progress_cb
, void *progress_arg
,
773 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
775 const struct got_error
*err
;
780 err
= got_pack_report_progress(progress_cb
, progress_arg
, rl
,
781 *ncolored
, *nfound
, *ntrees
, 0L, 0, 0, 0, 0);
785 for (i
= 0; i
< got_object_tree_get_nentries(tree
); i
++) {
786 struct got_tree_entry
*e
= got_object_tree_get_entry(tree
, i
);
787 struct got_object_id
*id
= got_tree_entry_get_id(e
);
788 mode_t mode
= got_tree_entry_get_mode(e
);
791 err
= (*cancel_cb
)(cancel_arg
);
796 if (got_object_tree_entry_is_submodule(e
) ||
797 got_object_idset_contains(idset
, id
) ||
798 got_object_idset_contains(idset_exclude
, id
))
802 * If got-read-pack is crawling trees for us then
803 * we are only here to collect blob IDs.
805 if (ids
== NULL
&& S_ISDIR(mode
))
808 if (asprintf(&p
, "%s%s%s", dpath
,
809 got_path_is_root_dir(dpath
) ? "" : "/",
810 got_tree_entry_get_name(e
)) == -1) {
811 err
= got_error_from_errno("asprintf");
816 struct got_object_qid
*qid
;
817 err
= got_object_qid_alloc(&qid
, id
);
822 STAILQ_INSERT_TAIL(ids
, qid
, entry
);
823 } else if (S_ISREG(mode
) || S_ISLNK(mode
)) {
824 err
= got_pack_add_object(want_meta
,
825 want_meta
? idset
: idset_exclude
, id
, p
,
826 GOT_OBJ_TYPE_BLOB
, mtime
, seed
, loose_obj_only
,
827 repo
, ncolored
, nfound
, ntrees
,
828 progress_cb
, progress_arg
, rl
);
843 const struct got_error
*
844 got_pack_load_tree(int want_meta
, struct got_object_idset
*idset
,
845 struct got_object_idset
*idset_exclude
,
846 struct got_object_id
*tree_id
, const char *dpath
, time_t mtime
,
847 uint32_t seed
, struct got_repository
*repo
, int loose_obj_only
,
848 int *ncolored
, int *nfound
, int *ntrees
,
849 got_pack_progress_cb progress_cb
, void *progress_arg
,
850 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
852 const struct got_error
*err
= NULL
;
853 struct got_object_id_queue tree_ids
;
854 struct got_object_qid
*qid
;
855 struct got_tree_object
*tree
= NULL
;
857 if (got_object_idset_contains(idset
, tree_id
) ||
858 got_object_idset_contains(idset_exclude
, tree_id
))
861 err
= got_object_qid_alloc(&qid
, tree_id
);
864 qid
->data
= strdup(dpath
);
865 if (qid
->data
== NULL
) {
866 err
= got_error_from_errno("strdup");
867 got_object_qid_free(qid
);
871 STAILQ_INIT(&tree_ids
);
872 STAILQ_INSERT_TAIL(&tree_ids
, qid
, entry
);
874 while (!STAILQ_EMPTY(&tree_ids
)) {
877 err
= (*cancel_cb
)(cancel_arg
);
882 qid
= STAILQ_FIRST(&tree_ids
);
883 STAILQ_REMOVE_HEAD(&tree_ids
, entry
);
886 if (got_object_idset_contains(idset
, &qid
->id
) ||
887 got_object_idset_contains(idset_exclude
, &qid
->id
)) {
889 got_object_qid_free(qid
);
893 err
= got_pack_add_object(want_meta
,
894 want_meta
? idset
: idset_exclude
,
895 &qid
->id
, path
, GOT_OBJ_TYPE_TREE
,
896 mtime
, seed
, loose_obj_only
, repo
,
897 ncolored
, nfound
, ntrees
, progress_cb
, progress_arg
, rl
);
900 got_object_qid_free(qid
);
904 err
= got_object_open_as_tree(&tree
, repo
, &qid
->id
);
907 got_object_qid_free(qid
);
911 err
= got_pack_load_tree_entries(&tree_ids
, want_meta
, idset
,
912 idset_exclude
, tree
, path
, mtime
, seed
, repo
,
913 loose_obj_only
, ncolored
, nfound
, ntrees
,
914 progress_cb
, progress_arg
, rl
,
915 cancel_cb
, cancel_arg
);
917 got_object_qid_free(qid
);
921 got_object_tree_close(tree
);
925 STAILQ_FOREACH(qid
, &tree_ids
, entry
)
927 got_object_id_queue_free(&tree_ids
);
929 got_object_tree_close(tree
);
933 static const struct got_error
*
934 load_commit(int want_meta
, struct got_object_idset
*idset
,
935 struct got_object_idset
*idset_exclude
,
936 struct got_object_id
*id
, struct got_repository
*repo
, uint32_t seed
,
937 int loose_obj_only
, int *ncolored
, int *nfound
, int *ntrees
,
938 got_pack_progress_cb progress_cb
, void *progress_arg
,
939 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
941 const struct got_error
*err
;
942 struct got_commit_object
*commit
;
944 if (got_object_idset_contains(idset
, id
) ||
945 got_object_idset_contains(idset_exclude
, id
))
948 if (loose_obj_only
) {
950 err
= search_packidx(&is_packed
, id
, repo
);
953 if (is_packed
&& want_meta
)
957 err
= got_object_open_as_commit(&commit
, repo
, id
);
961 err
= got_pack_add_object(want_meta
,
962 want_meta
? idset
: idset_exclude
, id
, "", GOT_OBJ_TYPE_COMMIT
,
963 got_object_commit_get_committer_time(commit
), seed
,
964 loose_obj_only
, repo
,
965 ncolored
, nfound
, ntrees
, progress_cb
, progress_arg
, rl
);
969 err
= got_pack_load_tree(want_meta
, idset
, idset_exclude
,
970 got_object_commit_get_tree_id(commit
),
971 "", got_object_commit_get_committer_time(commit
), seed
,
972 repo
, loose_obj_only
, ncolored
, nfound
, ntrees
,
973 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
975 got_object_commit_close(commit
);
979 static const struct got_error
*
980 load_tag(int want_meta
, struct got_object_idset
*idset
,
981 struct got_object_idset
*idset_exclude
,
982 struct got_object_id
*id
, struct got_repository
*repo
, uint32_t seed
,
983 int loose_obj_only
, int *ncolored
, int *nfound
, int *ntrees
,
984 got_pack_progress_cb progress_cb
, void *progress_arg
,
985 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
987 const struct got_error
*err
;
988 struct got_tag_object
*tag
= NULL
;
990 if (got_object_idset_contains(idset
, id
) ||
991 got_object_idset_contains(idset_exclude
, id
))
994 if (loose_obj_only
) {
996 err
= search_packidx(&is_packed
, id
, repo
);
999 if (is_packed
&& want_meta
)
1003 err
= got_object_open_as_tag(&tag
, repo
, id
);
1007 err
= got_pack_add_object(want_meta
,
1008 want_meta
? idset
: idset_exclude
, id
, "", GOT_OBJ_TYPE_TAG
,
1009 got_object_tag_get_tagger_time(tag
), seed
, loose_obj_only
, repo
,
1010 ncolored
, nfound
, ntrees
, progress_cb
, progress_arg
, rl
);
1014 switch (got_object_tag_get_object_type(tag
)) {
1015 case GOT_OBJ_TYPE_COMMIT
:
1016 err
= load_commit(want_meta
, idset
, idset_exclude
,
1017 got_object_tag_get_object_id(tag
), repo
, seed
,
1018 loose_obj_only
, ncolored
, nfound
, ntrees
,
1019 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1021 case GOT_OBJ_TYPE_TREE
:
1022 err
= got_pack_load_tree(want_meta
, idset
, idset_exclude
,
1023 got_object_tag_get_object_id(tag
), "",
1024 got_object_tag_get_tagger_time(tag
), seed
, repo
,
1025 loose_obj_only
, ncolored
, nfound
, ntrees
,
1026 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1033 got_object_tag_close(tag
);
1037 const struct got_error
*
1038 got_pack_paint_commit(struct got_object_qid
*qid
, intptr_t color
)
1040 if (color
< 0 || color
>= COLOR_MAX
)
1041 return got_error(GOT_ERR_RANGE
);
1043 qid
->data
= (void *)color
;
1047 const struct got_error
*
1048 got_pack_queue_commit_id(struct got_object_id_queue
*ids
,
1049 struct got_object_id
*id
, intptr_t color
, struct got_repository
*repo
)
1051 const struct got_error
*err
;
1052 struct got_object_qid
*qid
;
1054 err
= got_object_qid_alloc(&qid
, id
);
1058 STAILQ_INSERT_TAIL(ids
, qid
, entry
);
1059 return got_pack_paint_commit(qid
, color
);
1062 struct append_id_arg
{
1063 struct got_object_id
**array
;
1065 struct got_object_idset
*drop
;
1066 struct got_object_idset
*skip
;
1069 static const struct got_error
*
1070 append_id(struct got_object_id
*id
, void *data
, void *arg
)
1072 struct append_id_arg
*a
= arg
;
1074 if (got_object_idset_contains(a
->skip
, id
) ||
1075 got_object_idset_contains(a
->drop
, id
))
1078 a
->array
[++a
->idx
] = got_object_id_dup(id
);
1079 if (a
->array
[a
->idx
] == NULL
)
1080 return got_error_from_errno("got_object_id_dup");
1085 static const struct got_error
*
1086 free_meta(struct got_object_id
*id
, void *data
, void *arg
)
1088 struct got_pack_meta
*meta
= data
;
1095 static const struct got_error
*
1096 queue_commit_or_tag_id(struct got_object_id
*id
, intptr_t color
,
1097 struct got_object_id_queue
*ids
, struct got_repository
*repo
)
1099 const struct got_error
*err
;
1100 struct got_tag_object
*tag
= NULL
;
1103 err
= got_object_get_type(&obj_type
, repo
, id
);
1107 if (obj_type
== GOT_OBJ_TYPE_TAG
) {
1108 err
= got_object_open_as_tag(&tag
, repo
, id
);
1111 obj_type
= got_object_tag_get_object_type(tag
);
1112 id
= got_object_tag_get_object_id(tag
);
1115 if (obj_type
== GOT_OBJ_TYPE_COMMIT
) {
1116 err
= got_pack_queue_commit_id(ids
, id
, color
, repo
);
1122 got_object_tag_close(tag
);
1126 const struct got_error
*
1127 got_pack_find_pack_for_commit_painting(struct got_packidx
**best_packidx
,
1128 struct got_object_id_queue
*ids
, int nids
, struct got_repository
*repo
)
1130 const struct got_error
*err
= NULL
;
1131 struct got_pathlist_entry
*pe
;
1132 const char *best_packidx_path
= NULL
;
1134 int ncommits_max
= 0;
1136 *best_packidx
= NULL
;
1139 * Find the largest pack which contains at least some of the
1140 * commits we are interested in.
1142 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
) {
1143 const char *path_packidx
= pe
->path
;
1144 struct got_packidx
*packidx
;
1145 int nobj
, idx
, ncommits
= 0;
1146 struct got_object_qid
*qid
;
1148 err
= got_repo_get_packidx(&packidx
, path_packidx
, repo
);
1152 nobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1153 if (nobj
<= nobj_max
)
1156 STAILQ_FOREACH(qid
, ids
, entry
) {
1157 idx
= got_packidx_get_object_idx(packidx
, &qid
->id
);
1161 if (ncommits
> ncommits_max
) {
1162 best_packidx_path
= path_packidx
;
1164 ncommits_max
= ncommits
;
1168 if (best_packidx_path
&& err
== NULL
) {
1169 err
= got_repo_get_packidx(best_packidx
, best_packidx_path
,
1176 static const struct got_error
*
1177 findtwixt(struct got_object_id
***res
, int *nres
, int *ncolored
,
1178 struct got_object_id
**head
, int nhead
,
1179 struct got_object_id
**tail
, int ntail
,
1180 struct got_repository
*repo
,
1181 got_pack_progress_cb progress_cb
, void *progress_arg
,
1182 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
1184 const struct got_error
*err
= NULL
;
1185 struct got_object_id_queue ids
;
1186 struct got_object_idset
*keep
, *drop
, *skip
= NULL
;
1194 keep
= got_object_idset_alloc();
1196 return got_error_from_errno("got_object_idset_alloc");
1198 drop
= got_object_idset_alloc();
1200 err
= got_error_from_errno("got_object_idset_alloc");
1204 skip
= got_object_idset_alloc();
1206 err
= got_error_from_errno("got_object_idset_alloc");
1210 for (i
= 0; i
< nhead
; i
++) {
1211 struct got_object_id
*id
= head
[i
];
1214 err
= queue_commit_or_tag_id(id
, COLOR_KEEP
, &ids
, repo
);
1219 for (i
= 0; i
< ntail
; i
++) {
1220 struct got_object_id
*id
= tail
[i
];
1223 err
= queue_commit_or_tag_id(id
, COLOR_DROP
, &ids
, repo
);
1228 err
= got_pack_paint_commits(ncolored
, &ids
, nhead
+ ntail
,
1229 keep
, drop
, skip
, repo
, progress_cb
, progress_arg
, rl
,
1230 cancel_cb
, cancel_arg
);
1234 nkeep
= got_object_idset_num_elements(keep
);
1236 struct append_id_arg arg
;
1237 arg
.array
= calloc(nkeep
, sizeof(struct got_object_id
*));
1238 if (arg
.array
== NULL
) {
1239 err
= got_error_from_errno("calloc");
1245 err
= got_object_idset_for_each(keep
, append_id
, &arg
);
1251 *nres
= arg
.idx
+ 1;
1254 got_object_idset_free(keep
);
1255 got_object_idset_free(drop
);
1257 got_object_idset_free(skip
);
1258 got_object_id_queue_free(&ids
);
1262 static const struct got_error
*
1263 find_pack_for_enumeration(struct got_packidx
**best_packidx
,
1264 struct got_object_id
**ids
, int nids
, struct got_repository
*repo
)
1266 const struct got_error
*err
= NULL
;
1267 struct got_pathlist_entry
*pe
;
1268 const char *best_packidx_path
= NULL
;
1270 int ncommits_max
= 0;
1272 *best_packidx
= NULL
;
1275 * Find the largest pack which contains at least some of the
1276 * commits and tags we are interested in.
1278 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
) {
1279 const char *path_packidx
= pe
->path
;
1280 struct got_packidx
*packidx
;
1281 int nobj
, i
, idx
, ncommits
= 0;
1283 err
= got_repo_get_packidx(&packidx
, path_packidx
, repo
);
1287 nobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1288 if (nobj
<= nobj_max
)
1291 for (i
= 0; i
< nids
; i
++) {
1292 idx
= got_packidx_get_object_idx(packidx
, ids
[i
]);
1296 if (ncommits
> ncommits_max
) {
1297 best_packidx_path
= path_packidx
;
1299 ncommits_max
= ncommits
;
1303 if (best_packidx_path
&& err
== NULL
) {
1304 err
= got_repo_get_packidx(best_packidx
, best_packidx_path
,
1311 static const struct got_error
*
1312 load_object_ids(int *ncolored
, int *nfound
, int *ntrees
,
1313 struct got_object_idset
*idset
, struct got_object_id
**theirs
, int ntheirs
,
1314 struct got_object_id
**ours
, int nours
, struct got_repository
*repo
,
1315 uint32_t seed
, int loose_obj_only
, got_pack_progress_cb progress_cb
,
1316 void *progress_arg
, struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
,
1319 const struct got_error
*err
= NULL
;
1320 struct got_object_id
**ids
= NULL
;
1321 struct got_packidx
*packidx
= NULL
;
1322 int i
, nobj
= 0, obj_type
, found_all_objects
= 0;
1323 struct got_object_idset
*idset_exclude
;
1325 idset_exclude
= got_object_idset_alloc();
1326 if (idset_exclude
== NULL
)
1327 return got_error_from_errno("got_object_idset_alloc");
1333 err
= findtwixt(&ids
, &nobj
, ncolored
, ours
, nours
, theirs
, ntheirs
,
1334 repo
, progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1338 err
= find_pack_for_enumeration(&packidx
, theirs
, ntheirs
, repo
);
1342 err
= got_pack_load_packed_object_ids(&found_all_objects
,
1343 theirs
, ntheirs
, NULL
, 0, 0, seed
, idset
, idset_exclude
,
1344 loose_obj_only
, repo
, packidx
, ncolored
, nfound
, ntrees
,
1345 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1350 for (i
= 0; i
< ntheirs
; i
++) {
1351 struct got_object_id
*id
= theirs
[i
];
1354 err
= got_object_get_type(&obj_type
, repo
, id
);
1357 if (obj_type
== GOT_OBJ_TYPE_COMMIT
) {
1358 if (!found_all_objects
) {
1359 err
= load_commit(0, idset
, idset_exclude
,
1360 id
, repo
, seed
, loose_obj_only
,
1361 ncolored
, nfound
, ntrees
,
1362 progress_cb
, progress_arg
, rl
,
1363 cancel_cb
, cancel_arg
);
1367 } else if (obj_type
== GOT_OBJ_TYPE_TAG
) {
1368 err
= load_tag(0, idset
, idset_exclude
, id
, repo
,
1369 seed
, loose_obj_only
, ncolored
, nfound
, ntrees
,
1370 progress_cb
, progress_arg
, rl
,
1371 cancel_cb
, cancel_arg
);
1377 found_all_objects
= 0;
1378 err
= find_pack_for_enumeration(&packidx
, ids
, nobj
, repo
);
1382 err
= got_pack_load_packed_object_ids(&found_all_objects
, ids
,
1383 nobj
, theirs
, ntheirs
, 1, seed
, idset
, idset_exclude
,
1384 loose_obj_only
, repo
, packidx
, ncolored
, nfound
, ntrees
,
1385 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1390 if (!found_all_objects
) {
1391 for (i
= 0; i
< nobj
; i
++) {
1392 err
= load_commit(1, idset
, idset_exclude
, ids
[i
],
1393 repo
, seed
, loose_obj_only
, ncolored
, nfound
,
1394 ntrees
, progress_cb
, progress_arg
, rl
,
1395 cancel_cb
, cancel_arg
);
1401 for (i
= 0; i
< nours
; i
++) {
1402 struct got_object_id
*id
= ours
[i
];
1403 struct got_pack_meta
*m
;
1406 m
= got_object_idset_get(idset
, id
);
1408 err
= got_object_get_type(&obj_type
, repo
, id
);
1412 obj_type
= m
->obj_type
;
1413 if (obj_type
!= GOT_OBJ_TYPE_TAG
)
1415 err
= load_tag(1, idset
, idset_exclude
, id
, repo
,
1416 seed
, loose_obj_only
, ncolored
, nfound
, ntrees
,
1417 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1422 for (i
= 0; i
< nobj
; i
++) {
1426 got_object_idset_free(idset_exclude
);
1430 static const struct got_error
*
1431 hwrite(int fd
, const void *buf
, off_t len
, struct got_hash
*ctx
)
1433 got_hash_update(ctx
, buf
, len
);
1434 return got_poll_write_full(fd
, buf
, len
);
1437 static const struct got_error
*
1438 hcopy(FILE *fsrc
, int fd_dst
, off_t len
, struct got_hash
*ctx
)
1440 const struct got_error
*err
;
1441 unsigned char buf
[65536];
1445 while (remain
> 0) {
1446 size_t copylen
= MIN(sizeof(buf
), remain
);
1447 n
= fread(buf
, 1, copylen
, fsrc
);
1449 return got_ferror(fsrc
, GOT_ERR_IO
);
1450 got_hash_update(ctx
, buf
, copylen
);
1451 err
= got_poll_write_full(fd_dst
, buf
, copylen
);
1460 static const struct got_error
*
1461 hcopy_mmap(uint8_t *src
, off_t src_offset
, size_t src_size
,
1462 int fd
, off_t len
, struct got_hash
*ctx
)
1464 if (src_offset
+ len
> src_size
)
1465 return got_error(GOT_ERR_RANGE
);
1467 got_hash_update(ctx
, src
+ src_offset
, len
);
1468 return got_poll_write_full(fd
, src
+ src_offset
, len
);
1472 putbe32(char *b
, uint32_t n
)
1481 write_order_cmp(const void *pa
, const void *pb
)
1483 struct got_pack_meta
*a
, *b
, *ahd
, *bhd
;
1485 a
= *(struct got_pack_meta
**)pa
;
1486 b
= *(struct got_pack_meta
**)pb
;
1487 ahd
= (a
->head
== NULL
) ? a
: a
->head
;
1488 bhd
= (b
->head
== NULL
) ? b
: b
->head
;
1489 if (bhd
->mtime
< ahd
->mtime
)
1491 if (bhd
->mtime
> ahd
->mtime
)
1497 if (a
->nchain
!= b
->nchain
)
1498 return a
->nchain
- b
->nchain
;
1499 if (a
->mtime
< b
->mtime
)
1501 if (a
->mtime
> b
->mtime
)
1503 return got_object_id_cmp(&a
->id
, &b
->id
);
1507 reuse_write_order_cmp(const void *pa
, const void *pb
)
1509 struct got_pack_meta
*a
, *b
;
1511 a
= *(struct got_pack_meta
**)pa
;
1512 b
= *(struct got_pack_meta
**)pb
;
1514 if (a
->reused_delta_offset
< b
->reused_delta_offset
)
1516 if (a
->reused_delta_offset
> b
->reused_delta_offset
)
1521 static const struct got_error
*
1522 packhdr(int *hdrlen
, char *hdr
, size_t bufsize
, int obj_type
, size_t len
)
1528 hdr
[0] = obj_type
<< 4;
1529 hdr
[0] |= len
& 0xf;
1531 for (i
= 1; len
!= 0; i
++){
1533 return got_error(GOT_ERR_NO_SPACE
);
1534 hdr
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
1535 hdr
[i
] = len
& GOT_DELTA_SIZE_VAL_MASK
;
1536 len
>>= GOT_DELTA_SIZE_SHIFT
;
1544 packoff(char *hdr
, off_t off
)
1549 rbuf
[0] = off
& GOT_DELTA_SIZE_VAL_MASK
;
1550 for (i
= 1; (off
>>= GOT_DELTA_SIZE_SHIFT
) != 0; i
++) {
1551 rbuf
[i
] = (--off
& GOT_DELTA_SIZE_VAL_MASK
) |
1552 GOT_DELTA_SIZE_MORE
;
1557 hdr
[j
++] = rbuf
[--i
];
1561 static const struct got_error
*
1562 deltahdr(off_t
*packfile_size
, struct got_hash
*ctx
, int packfd
,
1563 int force_refdelta
, struct got_pack_meta
*m
)
1565 const struct got_error
*err
;
1568 size_t digest_len
= got_hash_digest_length(m
->prev
->id
.algo
);
1570 if (m
->prev
->off
!= 0 && !force_refdelta
) {
1571 err
= packhdr(&nh
, buf
, sizeof(buf
),
1572 GOT_OBJ_TYPE_OFFSET_DELTA
, m
->delta_len
);
1575 nh
+= packoff(buf
+ nh
, m
->off
- m
->prev
->off
);
1576 err
= hwrite(packfd
, buf
, nh
, ctx
);
1579 *packfile_size
+= nh
;
1581 err
= packhdr(&nh
, buf
, sizeof(buf
),
1582 GOT_OBJ_TYPE_REF_DELTA
, m
->delta_len
);
1585 err
= hwrite(packfd
, buf
, nh
, ctx
);
1588 *packfile_size
+= nh
;
1589 err
= hwrite(packfd
, m
->prev
->id
.hash
, digest_len
, ctx
);
1592 *packfile_size
+= digest_len
;
1598 static const struct got_error
*
1599 write_packed_object(off_t
*packfile_size
, int packfd
,
1600 FILE *delta_cache
, uint8_t *delta_cache_map
, size_t delta_cache_size
,
1601 struct got_pack_meta
*m
, int *outfd
, struct got_hash
*ctx
,
1602 struct got_repository
*repo
, int force_refdelta
)
1604 const struct got_error
*err
= NULL
;
1605 struct got_deflate_checksum csum
;
1608 struct got_raw_object
*raw
= NULL
;
1609 off_t outlen
, delta_offset
;
1611 memset(&csum
, 0, sizeof(csum
));
1612 csum
.output_ctx
= ctx
;
1614 if (m
->reused_delta_offset
)
1615 delta_offset
= m
->reused_delta_offset
;
1617 delta_offset
= m
->delta_offset
;
1619 m
->off
= *packfile_size
;
1620 if (m
->delta_len
== 0) {
1621 err
= got_object_raw_open(&raw
, outfd
, repo
, &m
->id
);
1624 err
= packhdr(&nh
, buf
, sizeof(buf
),
1625 m
->obj_type
, raw
->size
);
1628 err
= hwrite(packfd
, buf
, nh
, ctx
);
1631 *packfile_size
+= nh
;
1632 if (raw
->f
== NULL
) {
1633 err
= got_deflate_to_fd_mmap(&outlen
,
1634 raw
->data
+ raw
->hdrlen
, 0, raw
->size
,
1639 if (fseeko(raw
->f
, raw
->hdrlen
, SEEK_SET
)
1641 err
= got_error_from_errno("fseeko");
1644 err
= got_deflate_to_fd(&outlen
, raw
->f
,
1645 raw
->size
, packfd
, &csum
);
1649 *packfile_size
+= outlen
;
1650 got_object_raw_close(raw
);
1652 } else if (m
->delta_buf
) {
1653 err
= deltahdr(packfile_size
, ctx
, packfd
, force_refdelta
, m
);
1656 err
= hwrite(packfd
, m
->delta_buf
,
1657 m
->delta_compressed_len
, ctx
);
1660 *packfile_size
+= m
->delta_compressed_len
;
1662 m
->delta_buf
= NULL
;
1663 } else if (delta_cache_map
) {
1664 err
= deltahdr(packfile_size
, ctx
, packfd
, force_refdelta
, m
);
1667 err
= hcopy_mmap(delta_cache_map
, delta_offset
,
1668 delta_cache_size
, packfd
, m
->delta_compressed_len
,
1672 *packfile_size
+= m
->delta_compressed_len
;
1674 if (fseeko(delta_cache
, delta_offset
, SEEK_SET
) == -1) {
1675 err
= got_error_from_errno("fseeko");
1678 err
= deltahdr(packfile_size
, ctx
, packfd
, force_refdelta
, m
);
1681 err
= hcopy(delta_cache
, packfd
,
1682 m
->delta_compressed_len
, ctx
);
1685 *packfile_size
+= m
->delta_compressed_len
;
1689 got_object_raw_close(raw
);
1693 static const struct got_error
*
1694 genpack(struct got_object_id
*pack_hash
, int packfd
,
1695 struct got_pack
*reuse_pack
, FILE *delta_cache
,
1696 struct got_pack_meta
**deltify
, int ndeltify
,
1697 struct got_pack_meta
**reuse
, int nreuse
,
1698 int ncolored
, int nfound
, int ntrees
, int nours
,
1699 struct got_repository
*repo
, int force_refdelta
,
1700 got_pack_progress_cb progress_cb
, void *progress_arg
,
1701 struct got_ratelimit
*rl
,
1702 got_cancel_cb cancel_cb
, void *cancel_arg
)
1704 const struct got_error
*err
= NULL
;
1706 struct got_hash ctx
;
1707 struct got_pack_meta
*m
;
1709 off_t packfile_size
= 0;
1711 int delta_cache_fd
= -1;
1712 uint8_t *delta_cache_map
= NULL
;
1713 size_t delta_cache_size
= 0;
1714 FILE *packfile
= NULL
;
1715 enum got_hash_algorithm algo
;
1718 algo
= got_repo_get_object_format(repo
);
1719 digest_len
= got_hash_digest_length(algo
);
1720 got_hash_init(&ctx
, algo
);
1722 memset(pack_hash
, 0, sizeof(*pack_hash
));
1723 pack_hash
->algo
= algo
;
1725 #ifndef GOT_PACK_NO_MMAP
1726 delta_cache_fd
= dup(fileno(delta_cache
));
1727 if (delta_cache_fd
!= -1) {
1729 if (fstat(delta_cache_fd
, &sb
) == -1) {
1730 err
= got_error_from_errno("fstat");
1733 if (sb
.st_size
> 0 && sb
.st_size
<= SIZE_MAX
) {
1734 delta_cache_map
= mmap(NULL
, sb
.st_size
,
1735 PROT_READ
, MAP_PRIVATE
, delta_cache_fd
, 0);
1736 if (delta_cache_map
== MAP_FAILED
) {
1737 if (errno
!= ENOMEM
) {
1738 err
= got_error_from_errno("mmap");
1741 delta_cache_map
= NULL
; /* fallback on stdio */
1743 delta_cache_size
= (size_t)sb
.st_size
;
1747 err
= hwrite(packfd
, "PACK", 4, &ctx
);
1750 putbe32(buf
, GOT_PACKFILE_VERSION
);
1751 err
= hwrite(packfd
, buf
, 4, &ctx
);
1754 putbe32(buf
, ndeltify
+ nreuse
);
1755 err
= hwrite(packfd
, buf
, 4, &ctx
);
1759 qsort(deltify
, ndeltify
, sizeof(struct got_pack_meta
*),
1761 for (i
= 0; i
< ndeltify
; i
++) {
1762 err
= got_pack_report_progress(progress_cb
, progress_arg
, rl
,
1763 ncolored
, nfound
, ntrees
, packfile_size
, nours
,
1764 ndeltify
+ nreuse
, ndeltify
+ nreuse
, i
);
1768 err
= write_packed_object(&packfile_size
, packfd
,
1769 delta_cache
, delta_cache_map
, delta_cache_size
,
1770 m
, &outfd
, &ctx
, repo
, force_refdelta
);
1775 qsort(reuse
, nreuse
, sizeof(struct got_pack_meta
*),
1776 reuse_write_order_cmp
);
1777 if (nreuse
> 0 && reuse_pack
->map
== NULL
) {
1778 int fd
= dup(reuse_pack
->fd
);
1780 err
= got_error_from_errno("dup");
1783 packfile
= fdopen(fd
, "r");
1784 if (packfile
== NULL
) {
1785 err
= got_error_from_errno("fdopen");
1790 for (i
= 0; i
< nreuse
; i
++) {
1791 err
= got_pack_report_progress(progress_cb
, progress_arg
, rl
,
1792 ncolored
, nfound
, ntrees
, packfile_size
, nours
,
1793 ndeltify
+ nreuse
, ndeltify
+ nreuse
, ndeltify
+ i
);
1797 err
= write_packed_object(&packfile_size
, packfd
,
1798 packfile
, reuse_pack
->map
, reuse_pack
->filesize
,
1799 m
, &outfd
, &ctx
, repo
, force_refdelta
);
1804 got_hash_final_object_id(&ctx
, pack_hash
);
1805 err
= got_poll_write_full(packfd
, pack_hash
->hash
, digest_len
);
1808 packfile_size
+= digest_len
;
1809 packfile_size
+= sizeof(struct got_packfile_hdr
);
1811 err
= progress_cb(progress_arg
, ncolored
, nfound
, ntrees
,
1812 packfile_size
, nours
, ndeltify
+ nreuse
,
1813 ndeltify
+ nreuse
, ndeltify
+ nreuse
);
1818 if (outfd
!= -1 && close(outfd
) == -1 && err
== NULL
)
1819 err
= got_error_from_errno("close");
1820 if (delta_cache_map
&& munmap(delta_cache_map
, delta_cache_size
) == -1)
1821 err
= got_error_from_errno("munmap");
1822 if (delta_cache_fd
!= -1 && close(delta_cache_fd
) == -1 && err
== NULL
)
1823 err
= got_error_from_errno("close");
1824 if (packfile
&& fclose(packfile
) == EOF
&& err
== NULL
)
1825 err
= got_error_from_errno("fclose");
1829 static const struct got_error
*
1830 add_meta_idset_cb(struct got_object_id
*id
, void *data
, void *arg
)
1832 struct got_pack_meta
*m
= data
;
1833 struct got_pack_metavec
*v
= arg
;
1835 if (m
->reused_delta_offset
!= 0)
1838 return got_pack_add_meta(m
, v
);
1841 const struct got_error
*
1842 got_pack_create(struct got_object_id
*packhash
, int packfd
, FILE *delta_cache
,
1843 struct got_object_id
**theirs
, int ntheirs
,
1844 struct got_object_id
**ours
, int nours
,
1845 struct got_repository
*repo
, int loose_obj_only
, int allow_empty
,
1846 int force_refdelta
, got_pack_progress_cb progress_cb
, void *progress_arg
,
1847 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
1849 const struct got_error
*err
;
1850 struct got_object_idset
*idset
;
1851 struct got_packidx
*reuse_packidx
= NULL
;
1852 struct got_pack
*reuse_pack
= NULL
;
1853 struct got_pack_metavec deltify
, reuse
;
1854 int ncolored
= 0, nfound
= 0, ntrees
= 0;
1858 seed
= arc4random();
1860 memset(&deltify
, 0, sizeof(deltify
));
1861 memset(&reuse
, 0, sizeof(reuse
));
1863 idset
= got_object_idset_alloc();
1865 return got_error_from_errno("got_object_idset_alloc");
1867 err
= load_object_ids(&ncolored
, &nfound
, &ntrees
, idset
, theirs
,
1868 ntheirs
, ours
, nours
, repo
, seed
, loose_obj_only
,
1869 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1874 err
= progress_cb(progress_arg
, ncolored
, nfound
, ntrees
,
1875 0L, nours
, got_object_idset_num_elements(idset
), 0, 0);
1880 if (got_object_idset_num_elements(idset
) == 0 && !allow_empty
) {
1881 err
= got_error(GOT_ERR_CANNOT_PACK
);
1886 reuse
.meta
= calloc(reuse
.metasz
,
1887 sizeof(struct got_pack_meta
*));
1888 if (reuse
.meta
== NULL
) {
1889 err
= got_error_from_errno("calloc");
1893 err
= got_pack_search_deltas(&reuse_packidx
, &reuse_pack
,
1894 &reuse
, idset
, ncolored
, nfound
, ntrees
, nours
,
1895 repo
, progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1899 if (reuse_packidx
&& reuse_pack
) {
1900 err
= got_repo_pin_pack(repo
, reuse_packidx
, reuse_pack
);
1905 if (fseeko(delta_cache
, 0L, SEEK_END
) == -1) {
1906 err
= got_error_from_errno("fseeko");
1910 ndeltify
= got_object_idset_num_elements(idset
) - reuse
.nmeta
;
1912 deltify
.meta
= calloc(ndeltify
, sizeof(struct got_pack_meta
*));
1913 if (deltify
.meta
== NULL
) {
1914 err
= got_error_from_errno("calloc");
1917 deltify
.metasz
= ndeltify
;
1919 err
= got_object_idset_for_each(idset
, add_meta_idset_cb
,
1923 if (deltify
.nmeta
> 0) {
1924 err
= pick_deltas(deltify
.meta
, deltify
.nmeta
,
1925 ncolored
, nfound
, ntrees
, nours
, reuse
.nmeta
,
1926 delta_cache
, repo
, progress_cb
, progress_arg
, rl
,
1927 cancel_cb
, cancel_arg
);
1933 if (fflush(delta_cache
) == EOF
) {
1934 err
= got_error_from_errno("fflush");
1940 * Report a 1-byte packfile write to indicate we are about
1941 * to start sending packfile data. gotd(8) needs this.
1943 err
= progress_cb(progress_arg
, ncolored
, nfound
, ntrees
,
1944 1 /* packfile_size */, nours
,
1945 got_object_idset_num_elements(idset
),
1946 deltify
.nmeta
+ reuse
.nmeta
, 0);
1951 /* Pinned pack may have moved to different cache slot. */
1952 reuse_pack
= got_repo_get_pinned_pack(repo
);
1954 err
= genpack(packhash
, packfd
, reuse_pack
, delta_cache
, deltify
.meta
,
1955 deltify
.nmeta
, reuse
.meta
, reuse
.nmeta
, ncolored
, nfound
, ntrees
,
1956 nours
, repo
, force_refdelta
, progress_cb
, progress_arg
, rl
,
1957 cancel_cb
, cancel_arg
);
1961 free_nmeta(deltify
.meta
, deltify
.nmeta
);
1962 free_nmeta(reuse
.meta
, reuse
.nmeta
);
1963 got_object_idset_for_each(idset
, free_meta
, NULL
);
1964 got_object_idset_free(idset
);
1965 got_repo_unpin_pack(repo
);