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 <sys/types.h>
19 #include <sys/queue.h>
35 #if defined(__FreeBSD__)
39 #include "got_error.h"
40 #include "got_cancel.h"
41 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository_admin.h"
45 #include "got_opentemp.h"
47 #include "got_lib_deltify.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_idset.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_deflate.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_pack_create.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_repository.h"
57 #include "got_lib_ratelimit.h"
58 #include "got_lib_inflate.h"
60 #include "murmurhash2.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 struct got_pack_meta
{
75 struct got_object_id id
;
81 /* The best delta we picked */
82 struct got_pack_meta
*head
;
83 struct got_pack_meta
*prev
;
84 unsigned char *delta_buf
; /* if encoded in memory (compressed) */
85 off_t delta_offset
; /* offset in delta cache file (compressed) */
86 off_t delta_len
; /* encoded delta length */
87 off_t delta_compressed_len
; /* encoded+compressed delta length */
90 off_t reused_delta_offset
; /* offset of delta in reused pack file */
91 struct got_object_id
*base_obj_id
;
93 /* Only used for delta window */
94 struct got_delta_table
*dtab
;
96 /* Only used for writing offset deltas */
100 struct got_pack_metavec
{
101 struct got_pack_meta
**meta
;
106 static const struct got_error
*
107 alloc_meta(struct got_pack_meta
**new, struct got_object_id
*id
,
108 const char *path
, int obj_type
, time_t mtime
, uint32_t seed
)
110 struct got_pack_meta
*m
;
114 m
= calloc(1, sizeof(*m
));
116 return got_error_from_errno("calloc");
118 memcpy(&m
->id
, id
, sizeof(m
->id
));
120 m
->path_hash
= murmurhash2(path
, strlen(path
), seed
);
121 m
->obj_type
= obj_type
;
128 clear_meta(struct got_pack_meta
*meta
)
133 free(meta
->delta_buf
);
134 meta
->delta_buf
= NULL
;
135 free(meta
->base_obj_id
);
136 meta
->base_obj_id
= NULL
;
137 meta
->reused_delta_offset
= 0;
141 free_nmeta(struct got_pack_meta
**meta
, int nmeta
)
145 for (i
= 0; i
< nmeta
; i
++)
151 delta_order_cmp(const void *pa
, const void *pb
)
153 struct got_pack_meta
*a
, *b
;
155 a
= *(struct got_pack_meta
**)pa
;
156 b
= *(struct got_pack_meta
**)pb
;
158 if (a
->obj_type
!= b
->obj_type
)
159 return a
->obj_type
- b
->obj_type
;
160 if (a
->path_hash
< b
->path_hash
)
162 if (a
->path_hash
> b
->path_hash
)
164 if (a
->mtime
< b
->mtime
)
166 if (a
->mtime
> b
->mtime
)
168 return got_object_id_cmp(&a
->id
, &b
->id
);
172 delta_size(struct got_delta_instruction
*deltas
, int ndeltas
)
176 for (i
= 0; i
< ndeltas
; i
++) {
178 size
+= GOT_DELTA_SIZE_SHIFT
;
180 size
+= deltas
[i
].len
+ 1;
185 static const struct got_error
*
186 append(unsigned char **p
, size_t *len
, off_t
*sz
, void *seg
, int nseg
)
190 if (*len
+ nseg
>= *sz
) {
191 while (*len
+ nseg
>= *sz
)
193 n
= realloc(*p
, *sz
);
195 return got_error_from_errno("realloc");
198 memcpy(*p
+ *len
, seg
, nseg
);
203 static const struct got_error
*
204 encode_delta_in_mem(struct got_pack_meta
*m
, struct got_raw_object
*o
,
205 struct got_delta_instruction
*deltas
, int ndeltas
,
206 off_t delta_size
, off_t base_size
)
208 const struct got_error
*err
;
209 unsigned char buf
[16], *bp
;
211 size_t len
= 0, compressed_len
;
212 off_t bufsize
= delta_size
;
214 struct got_delta_instruction
*d
;
217 delta_buf
= malloc(bufsize
);
218 if (delta_buf
== NULL
)
219 return got_error_from_errno("malloc");
221 /* base object size */
222 buf
[0] = base_size
& GOT_DELTA_SIZE_VAL_MASK
;
223 n
= base_size
>> GOT_DELTA_SIZE_SHIFT
;
224 for (i
= 1; n
> 0; i
++) {
225 buf
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
226 buf
[i
] = n
& GOT_DELTA_SIZE_VAL_MASK
;
227 n
>>= GOT_DELTA_SIZE_SHIFT
;
229 err
= append(&delta_buf
, &len
, &bufsize
, buf
, i
);
233 /* target object size */
234 buf
[0] = o
->size
& GOT_DELTA_SIZE_VAL_MASK
;
235 n
= o
->size
>> GOT_DELTA_SIZE_SHIFT
;
236 for (i
= 1; n
> 0; i
++) {
237 buf
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
238 buf
[i
] = n
& GOT_DELTA_SIZE_VAL_MASK
;
239 n
>>= GOT_DELTA_SIZE_SHIFT
;
241 err
= append(&delta_buf
, &len
, &bufsize
, buf
, i
);
245 for (j
= 0; j
< ndeltas
; j
++) {
250 buf
[0] = GOT_DELTA_BASE_COPY
;
251 for (i
= 0; i
< 4; i
++) {
252 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
261 if (n
!= GOT_DELTA_COPY_DEFAULT_LEN
) {
262 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
263 for (i
= 0; i
< 3 && n
> 0; i
++) {
264 buf
[0] |= 1 << (i
+ 4);
269 err
= append(&delta_buf
, &len
, &bufsize
,
273 } else if (o
->f
== NULL
) {
275 while (n
!= d
->len
) {
276 buf
[0] = (d
->len
- n
< 127) ? d
->len
- n
: 127;
277 err
= append(&delta_buf
, &len
, &bufsize
,
281 err
= append(&delta_buf
, &len
, &bufsize
,
282 o
->data
+ o
->hdrlen
+ d
->offset
+ n
,
291 if (fseeko(o
->f
, o
->hdrlen
+ d
->offset
, SEEK_SET
) == -1) {
292 err
= got_error_from_errno("fseeko");
296 while (n
!= d
->len
) {
297 buf
[0] = (d
->len
- n
< 127) ? d
->len
- n
: 127;
298 err
= append(&delta_buf
, &len
, &bufsize
,
302 r
= fread(content
, 1, buf
[0], o
->f
);
304 err
= got_ferror(o
->f
, GOT_ERR_IO
);
307 err
= append(&delta_buf
, &len
, &bufsize
,
316 err
= got_deflate_to_mem_mmap(&m
->delta_buf
, &compressed_len
,
317 NULL
, NULL
, delta_buf
, 0, len
);
322 m
->delta_compressed_len
= compressed_len
;
328 static const struct got_error
*
329 encode_delta(struct got_pack_meta
*m
, struct got_raw_object
*o
,
330 struct got_delta_instruction
*deltas
, int ndeltas
,
331 off_t base_size
, FILE *f
)
333 const struct got_error
*err
;
334 unsigned char buf
[16], *bp
;
337 struct got_deflate_buf zb
;
338 struct got_delta_instruction
*d
;
339 off_t delta_len
= 0, compressed_len
= 0;
341 err
= got_deflate_init(&zb
, NULL
, GOT_DEFLATE_BUFSIZE
);
345 /* base object size */
346 buf
[0] = base_size
& GOT_DELTA_SIZE_VAL_MASK
;
347 n
= base_size
>> GOT_DELTA_SIZE_SHIFT
;
348 for (i
= 1; n
> 0; i
++) {
349 buf
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
350 buf
[i
] = n
& GOT_DELTA_SIZE_VAL_MASK
;
351 n
>>= GOT_DELTA_SIZE_SHIFT
;
354 err
= got_deflate_append_to_file_mmap(&zb
, &compressed_len
,
360 /* target object size */
361 buf
[0] = o
->size
& GOT_DELTA_SIZE_VAL_MASK
;
362 n
= o
->size
>> GOT_DELTA_SIZE_SHIFT
;
363 for (i
= 1; n
> 0; i
++) {
364 buf
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
365 buf
[i
] = n
& GOT_DELTA_SIZE_VAL_MASK
;
366 n
>>= GOT_DELTA_SIZE_SHIFT
;
369 err
= got_deflate_append_to_file_mmap(&zb
, &compressed_len
,
375 for (j
= 0; j
< ndeltas
; j
++) {
380 buf
[0] = GOT_DELTA_BASE_COPY
;
381 for (i
= 0; i
< 4; i
++) {
382 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
390 if (n
!= GOT_DELTA_COPY_DEFAULT_LEN
) {
391 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
392 for (i
= 0; i
< 3 && n
> 0; i
++) {
393 buf
[0] |= 1 << (i
+ 4);
398 err
= got_deflate_append_to_file_mmap(&zb
,
399 &compressed_len
, buf
, 0, bp
- buf
, f
, NULL
);
402 delta_len
+= (bp
- buf
);
403 } else if (o
->f
== NULL
) {
405 while (n
!= d
->len
) {
406 buf
[0] = (d
->len
- n
< 127) ? d
->len
- n
: 127;
407 err
= got_deflate_append_to_file_mmap(&zb
,
408 &compressed_len
, buf
, 0, 1, f
, NULL
);
412 err
= got_deflate_append_to_file_mmap(&zb
,
414 o
->data
+ o
->hdrlen
+ d
->offset
+ n
, 0,
424 if (fseeko(o
->f
, o
->hdrlen
+ d
->offset
, SEEK_SET
) == -1) {
425 err
= got_error_from_errno("fseeko");
429 while (n
!= d
->len
) {
430 buf
[0] = (d
->len
- n
< 127) ? d
->len
- n
: 127;
431 err
= got_deflate_append_to_file_mmap(&zb
,
432 &compressed_len
, buf
, 0, 1, f
, NULL
);
436 r
= fread(content
, 1, buf
[0], o
->f
);
438 err
= got_ferror(o
->f
, GOT_ERR_IO
);
441 err
= got_deflate_append_to_file_mmap(&zb
,
442 &compressed_len
, content
, 0, buf
[0], f
,
452 err
= got_deflate_flush(&zb
, f
, NULL
, &compressed_len
);
457 if (compressed_len
!= ftello(f
) - m
->delta_offset
) {
458 err
= got_error(GOT_ERR_COMPRESSION
);
462 m
->delta_len
= delta_len
;
463 m
->delta_compressed_len
= compressed_len
;
465 got_deflate_end(&zb
);
469 static const struct got_error
*
470 report_progress(got_pack_progress_cb progress_cb
, void *progress_arg
,
471 struct got_ratelimit
*rl
, int ncolored
, int nfound
, int ntrees
,
472 off_t packfile_size
, int ncommits
, int nobj_total
, int obj_deltify
,
475 const struct got_error
*err
;
478 if (progress_cb
== NULL
)
481 err
= got_ratelimit_check(&elapsed
, rl
);
485 return progress_cb(progress_arg
, ncolored
, nfound
, ntrees
,
486 packfile_size
, ncommits
, nobj_total
, obj_deltify
, nobj_written
);
489 static const struct got_error
*
490 add_meta(struct got_pack_meta
*m
, struct got_pack_metavec
*v
)
492 if (v
->nmeta
== v
->metasz
){
493 size_t newsize
= 2 * v
->metasz
;
494 struct got_pack_meta
**new;
495 new = reallocarray(v
->meta
, newsize
, sizeof(*new));
497 return got_error_from_errno("reallocarray");
502 v
->meta
[v
->nmeta
++] = m
;
506 static const struct got_error
*
507 find_pack_for_reuse(struct got_packidx
**best_packidx
,
508 struct got_repository
*repo
)
510 const struct got_error
*err
= NULL
;
511 struct got_pathlist_entry
*pe
;
512 const char *best_packidx_path
= NULL
;
515 *best_packidx
= NULL
;
517 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
) {
518 const char *path_packidx
= pe
->path
;
519 struct got_packidx
*packidx
;
522 err
= got_repo_get_packidx(&packidx
, path_packidx
, repo
);
526 nobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
527 if (nobj
> nobj_max
) {
528 best_packidx_path
= path_packidx
;
533 if (best_packidx_path
) {
534 err
= got_repo_get_packidx(best_packidx
, best_packidx_path
,
542 struct imsgbuf
*ibuf
;
543 struct got_object_id
*ids
[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS
];
547 static const struct got_error
*
548 send_id(struct got_object_id
*id
, void *data
, void *arg
)
550 const struct got_error
*err
= NULL
;
551 struct send_id_arg
*a
= arg
;
553 a
->ids
[a
->nids
++] = id
;
555 if (a
->nids
>= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS
) {
556 err
= got_privsep_send_object_idlist(a
->ibuf
, a
->ids
, a
->nids
);
565 static const struct got_error
*
566 send_idset(struct imsgbuf
*ibuf
, struct got_object_idset
*idset
)
568 const struct got_error
*err
;
569 struct send_id_arg sia
;
571 memset(&sia
, 0, sizeof(sia
));
573 err
= got_object_idset_for_each(idset
, send_id
, &sia
);
578 err
= got_privsep_send_object_idlist(ibuf
, sia
.ids
, sia
.nids
);
583 return got_privsep_send_object_idlist_done(ibuf
);
587 static const struct got_error
*
588 recv_reused_delta(struct got_imsg_reused_delta
*delta
,
589 struct got_object_idset
*idset
, struct got_pack_metavec
*v
)
591 struct got_pack_meta
*m
, *base
;
593 if (delta
->delta_offset
+ delta
->delta_size
< delta
->delta_offset
||
594 delta
->delta_offset
+
595 delta
->delta_compressed_size
< delta
->delta_offset
)
596 return got_error(GOT_ERR_BAD_PACKFILE
);
598 m
= got_object_idset_get(idset
, &delta
->id
);
600 return got_error(GOT_ERR_NO_OBJ
);
602 base
= got_object_idset_get(idset
, &delta
->base_id
);
604 return got_error(GOT_ERR_NO_OBJ
);
606 m
->delta_len
= delta
->delta_size
;
607 m
->delta_compressed_len
= delta
->delta_compressed_size
;
608 m
->delta_offset
= delta
->delta_out_offset
;
610 m
->size
= delta
->result_size
;
611 m
->reused_delta_offset
= delta
->delta_offset
;
612 m
->base_obj_id
= got_object_id_dup(&delta
->base_id
);
613 if (m
->base_obj_id
== NULL
)
614 return got_error_from_errno("got_object_id_dup");
616 return add_meta(m
, v
);
619 static const struct got_error
*
620 cache_pack_for_packidx(struct got_pack
**pack
, struct got_packidx
*packidx
,
621 struct got_repository
*repo
)
623 const struct got_error
*err
;
624 char *path_packfile
= NULL
;
626 err
= got_packidx_get_packfile_path(&path_packfile
,
627 packidx
->path_packidx
);
631 *pack
= got_repo_get_cached_pack(repo
, path_packfile
);
633 err
= got_repo_cache_pack(pack
, repo
, path_packfile
, packidx
);
637 if ((*pack
)->privsep_child
== NULL
) {
638 err
= got_pack_start_privsep_child(*pack
, packidx
);
647 static const struct got_error
*
648 prepare_delta_reuse(struct got_pack
*pack
, struct got_packidx
*packidx
,
649 int delta_outfd
, struct got_repository
*repo
)
651 const struct got_error
*err
= NULL
;
653 if (!pack
->child_has_delta_outfd
) {
655 outfd_child
= dup(delta_outfd
);
656 if (outfd_child
== -1) {
657 err
= got_error_from_errno("dup");
660 err
= got_privsep_send_raw_delta_outfd(
661 pack
->privsep_child
->ibuf
, outfd_child
);
664 pack
->child_has_delta_outfd
= 1;
667 err
= got_privsep_send_delta_reuse_req(pack
->privsep_child
->ibuf
);
673 static const struct got_error
*
674 search_deltas(struct got_pack_metavec
*v
, struct got_object_idset
*idset
,
675 int delta_cache_fd
, int ncolored
, int nfound
, int ntrees
, int ncommits
,
676 struct got_repository
*repo
,
677 got_pack_progress_cb progress_cb
, void *progress_arg
,
678 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
680 const struct got_error
*err
= NULL
;
681 struct got_packidx
*packidx
;
682 struct got_pack
*pack
;
683 struct got_imsg_reused_delta deltas
[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS
];
686 err
= find_pack_for_reuse(&packidx
, repo
);
693 err
= cache_pack_for_packidx(&pack
, packidx
, repo
);
697 err
= prepare_delta_reuse(pack
, packidx
, delta_cache_fd
, repo
);
701 err
= send_idset(pack
->privsep_child
->ibuf
, idset
);
709 err
= (*cancel_cb
)(cancel_arg
);
714 err
= got_privsep_recv_reused_deltas(&done
, deltas
, &ndeltas
,
715 pack
->privsep_child
->ibuf
);
719 for (i
= 0; i
< ndeltas
; i
++) {
720 struct got_imsg_reused_delta
*delta
= &deltas
[i
];
721 err
= recv_reused_delta(delta
, idset
, v
);
726 err
= report_progress(progress_cb
, progress_arg
, rl
,
727 ncolored
, nfound
, ntrees
, 0L, ncommits
,
728 got_object_idset_num_elements(idset
), v
->nmeta
, 0);
736 static const struct got_error
*
737 pick_deltas(struct got_pack_meta
**meta
, int nmeta
, int ncolored
,
738 int nfound
, int ntrees
, int ncommits
, int nreused
, FILE *delta_cache
,
739 struct got_repository
*repo
,
740 got_pack_progress_cb progress_cb
, void *progress_arg
,
741 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
743 const struct got_error
*err
= NULL
;
744 struct got_pack_meta
*m
= NULL
, *base
= NULL
;
745 struct got_raw_object
*raw
= NULL
, *base_raw
= NULL
;
746 struct got_delta_instruction
*deltas
= NULL
, *best_deltas
= NULL
;
747 int i
, j
, ndeltas
, best_ndeltas
;
748 off_t size
, best_size
;
749 const int max_base_candidates
= 3;
750 size_t delta_memsize
= 0;
751 const size_t max_delta_memsize
= 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX
;
755 delta_seed
= arc4random();
757 qsort(meta
, nmeta
, sizeof(struct got_pack_meta
*), delta_order_cmp
);
758 for (i
= 0; i
< nmeta
; i
++) {
760 err
= (*cancel_cb
)(cancel_arg
);
764 err
= report_progress(progress_cb
, progress_arg
, rl
,
765 ncolored
, nfound
, ntrees
, 0L, ncommits
, nreused
+ nmeta
,
771 if (m
->obj_type
== GOT_OBJ_TYPE_COMMIT
||
772 m
->obj_type
== GOT_OBJ_TYPE_TAG
)
775 err
= got_object_raw_open(&raw
, &outfd
, repo
, &m
->id
);
780 if (raw
->f
== NULL
) {
781 err
= got_deltify_init_mem(&m
->dtab
, raw
->data
,
782 raw
->hdrlen
, raw
->size
+ raw
->hdrlen
, delta_seed
);
784 err
= got_deltify_init(&m
->dtab
, raw
->f
, raw
->hdrlen
,
785 raw
->size
+ raw
->hdrlen
, delta_seed
);
790 if (i
> max_base_candidates
) {
791 struct got_pack_meta
*n
= NULL
;
792 n
= meta
[i
- (max_base_candidates
+ 1)];
793 got_deltify_free(n
->dtab
);
797 best_size
= raw
->size
;
799 for (j
= MAX(0, i
- max_base_candidates
); j
< i
; j
++) {
801 err
= (*cancel_cb
)(cancel_arg
);
806 /* long chains make unpacking slow, avoid such bases */
807 if (base
->nchain
>= 128 ||
808 base
->obj_type
!= m
->obj_type
)
811 err
= got_object_raw_open(&base_raw
, &outfd
, repo
,
816 if (raw
->f
== NULL
&& base_raw
->f
== NULL
) {
817 err
= got_deltify_mem_mem(&deltas
, &ndeltas
,
818 raw
->data
, raw
->hdrlen
,
819 raw
->size
+ raw
->hdrlen
, delta_seed
,
820 base
->dtab
, base_raw
->data
,
822 base_raw
->size
+ base_raw
->hdrlen
);
823 } else if (raw
->f
== NULL
) {
824 err
= got_deltify_mem_file(&deltas
, &ndeltas
,
825 raw
->data
, raw
->hdrlen
,
826 raw
->size
+ raw
->hdrlen
, delta_seed
,
827 base
->dtab
, base_raw
->f
,
829 base_raw
->size
+ base_raw
->hdrlen
);
830 } else if (base_raw
->f
== NULL
) {
831 err
= got_deltify_file_mem(&deltas
, &ndeltas
,
833 raw
->size
+ raw
->hdrlen
, delta_seed
,
834 base
->dtab
, base_raw
->data
,
836 base_raw
->size
+ base_raw
->hdrlen
);
838 err
= got_deltify(&deltas
, &ndeltas
,
840 raw
->size
+ raw
->hdrlen
, delta_seed
,
841 base
->dtab
, base_raw
->f
, base_raw
->hdrlen
,
842 base_raw
->size
+ base_raw
->hdrlen
);
844 got_object_raw_close(base_raw
);
849 size
= delta_size(deltas
, ndeltas
);
850 if (size
+ 32 < best_size
){
852 * if we already picked a best delta,
857 best_deltas
= deltas
;
858 best_ndeltas
= ndeltas
;
860 m
->nchain
= base
->nchain
+ 1;
862 m
->head
= base
->head
;
872 if (best_ndeltas
> 0) {
873 if (best_size
<= GOT_DELTA_RESULT_SIZE_CACHED_MAX
&&
874 delta_memsize
+ best_size
<= max_delta_memsize
) {
875 delta_memsize
+= best_size
;
876 err
= encode_delta_in_mem(m
, raw
, best_deltas
,
877 best_ndeltas
, best_size
, m
->prev
->size
);
879 m
->delta_offset
= ftello(delta_cache
);
880 err
= encode_delta(m
, raw
, best_deltas
,
881 best_ndeltas
, m
->prev
->size
, delta_cache
);
890 got_object_raw_close(raw
);
894 for (i
= MAX(0, nmeta
- max_base_candidates
); i
< nmeta
; i
++) {
895 got_deltify_free(meta
[i
]->dtab
);
896 meta
[i
]->dtab
= NULL
;
899 got_object_raw_close(raw
);
901 got_object_raw_close(base_raw
);
902 if (outfd
!= -1 && close(outfd
) == -1 && err
== NULL
)
903 err
= got_error_from_errno("close");
909 static const struct got_error
*
910 search_packidx(int *found
, struct got_object_id
*id
,
911 struct got_repository
*repo
)
913 const struct got_error
*err
= NULL
;
914 struct got_packidx
*packidx
= NULL
;
919 err
= got_repo_search_packidx(&packidx
, &idx
, repo
, id
);
921 *found
= 1; /* object is already packed */
922 else if (err
->code
== GOT_ERR_NO_OBJ
)
927 static const struct got_error
*
928 add_object(int want_meta
, struct got_object_idset
*idset
,
929 struct got_object_id
*id
, const char *path
, int obj_type
,
930 time_t mtime
, uint32_t seed
, int loose_obj_only
,
931 struct got_repository
*repo
, int *ncolored
, int *nfound
, int *ntrees
,
932 got_pack_progress_cb progress_cb
, void *progress_arg
,
933 struct got_ratelimit
*rl
)
935 const struct got_error
*err
;
936 struct got_pack_meta
*m
= NULL
;
938 if (loose_obj_only
) {
940 err
= search_packidx(&is_packed
, id
, repo
);
943 if (is_packed
&& want_meta
)
948 err
= alloc_meta(&m
, id
, path
, obj_type
, mtime
, seed
);
953 err
= report_progress(progress_cb
, progress_arg
, rl
,
954 *ncolored
, *nfound
, *ntrees
, 0L, 0, 0, 0, 0);
962 err
= got_object_idset_add(idset
, id
, m
);
970 static const struct got_error
*
971 load_tree_entries(struct got_object_id_queue
*ids
, int want_meta
,
972 struct got_object_idset
*idset
, struct got_object_idset
*idset_exclude
,
973 struct got_tree_object
*tree
,
974 const char *dpath
, time_t mtime
, uint32_t seed
, struct got_repository
*repo
,
975 int loose_obj_only
, int *ncolored
, int *nfound
, int *ntrees
,
976 got_pack_progress_cb progress_cb
, void *progress_arg
,
977 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
979 const struct got_error
*err
;
984 err
= report_progress(progress_cb
, progress_arg
, rl
,
985 *ncolored
, *nfound
, *ntrees
, 0L, 0, 0, 0, 0);
989 for (i
= 0; i
< got_object_tree_get_nentries(tree
); i
++) {
990 struct got_tree_entry
*e
= got_object_tree_get_entry(tree
, i
);
991 struct got_object_id
*id
= got_tree_entry_get_id(e
);
992 mode_t mode
= got_tree_entry_get_mode(e
);
995 err
= (*cancel_cb
)(cancel_arg
);
1000 if (got_object_tree_entry_is_submodule(e
) ||
1001 got_object_idset_contains(idset
, id
) ||
1002 got_object_idset_contains(idset_exclude
, id
))
1006 * If got-read-pack is crawling trees for us then
1007 * we are only here to collect blob IDs.
1009 if (ids
== NULL
&& S_ISDIR(mode
))
1012 if (asprintf(&p
, "%s%s%s", dpath
,
1013 got_path_is_root_dir(dpath
) ? "" : "/",
1014 got_tree_entry_get_name(e
)) == -1) {
1015 err
= got_error_from_errno("asprintf");
1019 if (S_ISDIR(mode
)) {
1020 struct got_object_qid
*qid
;
1021 err
= got_object_qid_alloc(&qid
, id
);
1026 STAILQ_INSERT_TAIL(ids
, qid
, entry
);
1027 } else if (S_ISREG(mode
) || S_ISLNK(mode
)) {
1028 err
= add_object(want_meta
,
1029 want_meta
? idset
: idset_exclude
, id
, p
,
1030 GOT_OBJ_TYPE_BLOB
, mtime
, seed
, loose_obj_only
,
1031 repo
, ncolored
, nfound
, ntrees
,
1032 progress_cb
, progress_arg
, rl
);
1047 static const struct got_error
*
1048 load_tree(int want_meta
, struct got_object_idset
*idset
,
1049 struct got_object_idset
*idset_exclude
,
1050 struct got_object_id
*tree_id
, const char *dpath
, time_t mtime
,
1051 uint32_t seed
, struct got_repository
*repo
, int loose_obj_only
,
1052 int *ncolored
, int *nfound
, int *ntrees
,
1053 got_pack_progress_cb progress_cb
, void *progress_arg
,
1054 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
1056 const struct got_error
*err
= NULL
;
1057 struct got_object_id_queue tree_ids
;
1058 struct got_object_qid
*qid
;
1059 struct got_tree_object
*tree
= NULL
;
1061 if (got_object_idset_contains(idset
, tree_id
) ||
1062 got_object_idset_contains(idset_exclude
, tree_id
))
1065 err
= got_object_qid_alloc(&qid
, tree_id
);
1068 qid
->data
= strdup(dpath
);
1069 if (qid
->data
== NULL
) {
1070 err
= got_error_from_errno("strdup");
1071 got_object_qid_free(qid
);
1075 STAILQ_INIT(&tree_ids
);
1076 STAILQ_INSERT_TAIL(&tree_ids
, qid
, entry
);
1078 while (!STAILQ_EMPTY(&tree_ids
)) {
1081 err
= (*cancel_cb
)(cancel_arg
);
1086 qid
= STAILQ_FIRST(&tree_ids
);
1087 STAILQ_REMOVE_HEAD(&tree_ids
, entry
);
1090 if (got_object_idset_contains(idset
, &qid
->id
) ||
1091 got_object_idset_contains(idset_exclude
, &qid
->id
)) {
1093 got_object_qid_free(qid
);
1097 err
= add_object(want_meta
, want_meta
? idset
: idset_exclude
,
1098 &qid
->id
, path
, GOT_OBJ_TYPE_TREE
,
1099 mtime
, seed
, loose_obj_only
, repo
,
1100 ncolored
, nfound
, ntrees
, progress_cb
, progress_arg
, rl
);
1103 got_object_qid_free(qid
);
1107 err
= got_object_open_as_tree(&tree
, repo
, &qid
->id
);
1110 got_object_qid_free(qid
);
1114 err
= load_tree_entries(&tree_ids
, want_meta
, idset
,
1115 idset_exclude
, tree
, path
, mtime
, seed
, repo
,
1116 loose_obj_only
, ncolored
, nfound
, ntrees
,
1117 progress_cb
, progress_arg
, rl
,
1118 cancel_cb
, cancel_arg
);
1120 got_object_qid_free(qid
);
1124 got_object_tree_close(tree
);
1128 STAILQ_FOREACH(qid
, &tree_ids
, entry
)
1130 got_object_id_queue_free(&tree_ids
);
1132 got_object_tree_close(tree
);
1136 static const struct got_error
*
1137 load_commit(int want_meta
, struct got_object_idset
*idset
,
1138 struct got_object_idset
*idset_exclude
,
1139 struct got_object_id
*id
, struct got_repository
*repo
, uint32_t seed
,
1140 int loose_obj_only
, int *ncolored
, int *nfound
, int *ntrees
,
1141 got_pack_progress_cb progress_cb
, void *progress_arg
,
1142 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
1144 const struct got_error
*err
;
1145 struct got_commit_object
*commit
;
1147 if (got_object_idset_contains(idset
, id
) ||
1148 got_object_idset_contains(idset_exclude
, id
))
1151 if (loose_obj_only
) {
1153 err
= search_packidx(&is_packed
, id
, repo
);
1156 if (is_packed
&& want_meta
)
1160 err
= got_object_open_as_commit(&commit
, repo
, id
);
1164 err
= add_object(want_meta
, want_meta
? idset
: idset_exclude
,
1165 id
, "", GOT_OBJ_TYPE_COMMIT
,
1166 got_object_commit_get_committer_time(commit
), seed
,
1167 loose_obj_only
, repo
,
1168 ncolored
, nfound
, ntrees
, progress_cb
, progress_arg
, rl
);
1172 err
= load_tree(want_meta
, idset
, idset_exclude
,
1173 got_object_commit_get_tree_id(commit
),
1174 "", got_object_commit_get_committer_time(commit
), seed
,
1175 repo
, loose_obj_only
, ncolored
, nfound
, ntrees
,
1176 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1178 got_object_commit_close(commit
);
1182 static const struct got_error
*
1183 load_tag(int want_meta
, struct got_object_idset
*idset
,
1184 struct got_object_idset
*idset_exclude
,
1185 struct got_object_id
*id
, struct got_repository
*repo
, uint32_t seed
,
1186 int loose_obj_only
, int *ncolored
, int *nfound
, int *ntrees
,
1187 got_pack_progress_cb progress_cb
, void *progress_arg
,
1188 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
1190 const struct got_error
*err
;
1191 struct got_tag_object
*tag
= NULL
;
1193 if (got_object_idset_contains(idset
, id
) ||
1194 got_object_idset_contains(idset_exclude
, id
))
1197 if (loose_obj_only
) {
1199 err
= search_packidx(&is_packed
, id
, repo
);
1202 if (is_packed
&& want_meta
)
1206 err
= got_object_open_as_tag(&tag
, repo
, id
);
1210 err
= add_object(want_meta
, want_meta
? idset
: idset_exclude
,
1211 id
, "", GOT_OBJ_TYPE_TAG
,
1212 got_object_tag_get_tagger_time(tag
), seed
, loose_obj_only
, repo
,
1213 ncolored
, nfound
, ntrees
, progress_cb
, progress_arg
, rl
);
1217 switch (got_object_tag_get_object_type(tag
)) {
1218 case GOT_OBJ_TYPE_COMMIT
:
1219 err
= load_commit(want_meta
, idset
, idset_exclude
,
1220 got_object_tag_get_object_id(tag
), repo
, seed
,
1221 loose_obj_only
, ncolored
, nfound
, ntrees
,
1222 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1224 case GOT_OBJ_TYPE_TREE
:
1225 err
= load_tree(want_meta
, idset
, idset_exclude
,
1226 got_object_tag_get_object_id(tag
), "",
1227 got_object_tag_get_tagger_time(tag
), seed
, repo
,
1228 loose_obj_only
, ncolored
, nfound
, ntrees
,
1229 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
1236 got_object_tag_close(tag
);
1240 enum findtwixt_color
{
1247 static const struct got_error
*
1248 paint_commit(struct got_object_qid
*qid
, intptr_t color
)
1250 if (color
< 0 || color
>= COLOR_MAX
)
1251 return got_error(GOT_ERR_RANGE
);
1253 qid
->data
= (void *)color
;
1257 static const struct got_error
*
1258 queue_commit_id(struct got_object_id_queue
*ids
, struct got_object_id
*id
,
1259 intptr_t color
, struct got_repository
*repo
)
1261 const struct got_error
*err
;
1262 struct got_object_qid
*qid
;
1264 err
= got_object_qid_alloc(&qid
, id
);
1268 STAILQ_INSERT_TAIL(ids
, qid
, entry
);
1269 return paint_commit(qid
, color
);
1272 struct append_id_arg
{
1273 struct got_object_id
**array
;
1275 struct got_object_idset
*drop
;
1276 struct got_object_idset
*skip
;
1279 static const struct got_error
*
1280 append_id(struct got_object_id
*id
, void *data
, void *arg
)
1282 struct append_id_arg
*a
= arg
;
1284 if (got_object_idset_contains(a
->skip
, id
) ||
1285 got_object_idset_contains(a
->drop
, id
))
1288 a
->array
[++a
->idx
] = got_object_id_dup(id
);
1289 if (a
->array
[a
->idx
] == NULL
)
1290 return got_error_from_errno("got_object_id_dup");
1295 static const struct got_error
*
1296 queue_commit_or_tag_id(struct got_object_id
*id
, intptr_t color
,
1297 struct got_object_id_queue
*ids
, struct got_repository
*repo
)
1299 const struct got_error
*err
;
1300 struct got_tag_object
*tag
= NULL
;
1303 err
= got_object_get_type(&obj_type
, repo
, id
);
1307 if (obj_type
== GOT_OBJ_TYPE_TAG
) {
1308 err
= got_object_open_as_tag(&tag
, repo
, id
);
1311 obj_type
= got_object_tag_get_object_type(tag
);
1312 id
= got_object_tag_get_object_id(tag
);
1315 if (obj_type
== GOT_OBJ_TYPE_COMMIT
) {
1316 err
= queue_commit_id(ids
, id
, color
, repo
);
1322 got_object_tag_close(tag
);
1326 struct recv_painted_commit_arg
{
1330 struct got_object_id_queue
*ids
;
1331 struct got_object_idset
*keep
;
1332 struct got_object_idset
*drop
;
1333 struct got_object_idset
*skip
;
1334 got_pack_progress_cb progress_cb
;
1336 struct got_ratelimit
*rl
;
1337 got_cancel_cb cancel_cb
;
1341 static const struct got_error
*
1342 recv_painted_commit(void *arg
, struct got_object_id
*id
, intptr_t color
)
1344 const struct got_error
*err
= NULL
;
1345 struct recv_painted_commit_arg
*a
= arg
;
1346 struct got_object_qid
*qid
, *tmp
;
1349 err
= a
->cancel_cb(a
->cancel_arg
);
1356 err
= got_object_idset_add(a
->keep
, id
, NULL
);
1362 err
= got_object_idset_add(a
->drop
, id
, NULL
);
1368 err
= got_object_idset_add(a
->skip
, id
, NULL
);
1373 /* should not happen */
1374 return got_error_fmt(GOT_ERR_NOT_IMPL
,
1375 "%s invalid commit color %d", __func__
, color
);
1378 STAILQ_FOREACH_SAFE(qid
, a
->ids
, entry
, tmp
) {
1379 if (got_object_id_cmp(&qid
->id
, id
) != 0)
1381 STAILQ_REMOVE(a
->ids
, qid
, got_object_qid
, entry
);
1382 color
= (intptr_t)qid
->data
;
1383 got_object_qid_free(qid
);
1385 if (color
== COLOR_SKIP
)
1390 return report_progress(a
->progress_cb
, a
->progress_arg
, a
->rl
,
1391 *a
->ncolored
, 0, 0, 0L, 0, 0, 0, 0);
1394 static const struct got_error
*
1395 paint_packed_commits(struct got_pack
*pack
, struct got_object_id
*id
,
1396 int idx
, intptr_t color
, int *ncolored
, int *nqueued
, int *nskip
,
1397 struct got_object_id_queue
*ids
,
1398 struct got_object_idset
*keep
, struct got_object_idset
*drop
,
1399 struct got_object_idset
*skip
, struct got_repository
*repo
,
1400 got_pack_progress_cb progress_cb
, void *progress_arg
,
1401 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
1403 const struct got_error
*err
= NULL
;
1404 struct got_object_id_queue next_ids
;
1405 struct got_object_qid
*qid
, *tmp
;
1406 struct recv_painted_commit_arg arg
;
1408 STAILQ_INIT(&next_ids
);
1410 err
= got_privsep_send_painting_request(pack
->privsep_child
->ibuf
,
1415 arg
.ncolored
= ncolored
;
1416 arg
.nqueued
= nqueued
;
1422 arg
.progress_cb
= progress_cb
;
1423 arg
.progress_arg
= progress_arg
;
1425 arg
.cancel_cb
= cancel_cb
;
1426 arg
.cancel_arg
= cancel_arg
;
1427 err
= got_privsep_recv_painted_commits(&next_ids
,
1428 recv_painted_commit
, &arg
, pack
->privsep_child
->ibuf
);
1432 STAILQ_FOREACH_SAFE(qid
, &next_ids
, entry
, tmp
) {
1433 struct got_object_qid
*old_id
;
1434 intptr_t qcolor
, ocolor
;
1435 STAILQ_FOREACH(old_id
, ids
, entry
) {
1436 if (got_object_id_cmp(&qid
->id
, &old_id
->id
))
1438 qcolor
= (intptr_t)qid
->data
;
1439 ocolor
= (intptr_t)old_id
->data
;
1440 STAILQ_REMOVE(&next_ids
, qid
, got_object_qid
, entry
);
1441 got_object_qid_free(qid
);
1443 if (qcolor
!= ocolor
) {
1444 paint_commit(old_id
, qcolor
);
1445 if (ocolor
== COLOR_SKIP
)
1447 else if (qcolor
== COLOR_SKIP
)
1453 while (!STAILQ_EMPTY(&next_ids
)) {
1454 qid
= STAILQ_FIRST(&next_ids
);
1455 STAILQ_REMOVE_HEAD(&next_ids
, entry
);
1456 paint_commit(qid
, color
);
1457 STAILQ_INSERT_TAIL(ids
, qid
, entry
);
1459 if (color
== COLOR_SKIP
)
1466 static const struct got_error
*
1467 find_pack_for_commit_painting(struct got_packidx
**best_packidx
,
1468 struct got_object_id_queue
*ids
, int nids
, struct got_repository
*repo
)
1470 const struct got_error
*err
= NULL
;
1471 struct got_pathlist_entry
*pe
;
1472 const char *best_packidx_path
= NULL
;
1474 int ncommits_max
= 0;
1476 *best_packidx
= NULL
;
1479 * Find the largest pack which contains at least some of the
1480 * commits we are interested in.
1482 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
) {
1483 const char *path_packidx
= pe
->path
;
1484 struct got_packidx
*packidx
;
1485 int nobj
, idx
, ncommits
= 0;
1486 struct got_object_qid
*qid
;
1488 err
= got_repo_get_packidx(&packidx
, path_packidx
, repo
);
1492 nobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1493 if (nobj
<= nobj_max
)
1496 STAILQ_FOREACH(qid
, ids
, entry
) {
1497 idx
= got_packidx_get_object_idx(packidx
, &qid
->id
);
1501 if (ncommits
> ncommits_max
) {
1502 best_packidx_path
= path_packidx
;
1504 ncommits_max
= ncommits
;
1508 if (best_packidx_path
&& err
== NULL
) {
1509 err
= got_repo_get_packidx(best_packidx
, best_packidx_path
,
1516 static const struct got_error
*
1517 paint_commits(int *ncolored
, struct got_object_id_queue
*ids
, int nids
,
1518 struct got_object_idset
*keep
, struct got_object_idset
*drop
,
1519 struct got_object_idset
*skip
, struct got_repository
*repo
,
1520 got_pack_progress_cb progress_cb
, void *progress_arg
,
1521 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
1523 const struct got_error
*err
= NULL
;
1524 struct got_commit_object
*commit
= NULL
;
1525 struct got_packidx
*packidx
= NULL
;
1526 struct got_pack
*pack
= NULL
;
1527 const struct got_object_id_queue
*parents
;
1528 struct got_object_qid
*qid
= NULL
;
1529 int nqueued
= nids
, nskip
= 0;
1532 while (!STAILQ_EMPTY(ids
) && nskip
!= nqueued
) {
1536 err
= cancel_cb(cancel_arg
);
1541 qid
= STAILQ_FIRST(ids
);
1542 STAILQ_REMOVE_HEAD(ids
, entry
);
1544 color
= (intptr_t)qid
->data
;
1545 if (color
== COLOR_SKIP
)
1548 if (got_object_idset_contains(skip
, &qid
->id
)) {
1549 got_object_qid_free(qid
);
1553 if (color
== COLOR_KEEP
&&
1554 got_object_idset_contains(keep
, &qid
->id
)) {
1555 got_object_qid_free(qid
);
1559 if (color
== COLOR_DROP
&&
1560 got_object_idset_contains(drop
, &qid
->id
)) {
1561 got_object_qid_free(qid
);
1566 /* Pinned pack may have moved to different cache slot. */
1567 pack
= got_repo_get_pinned_pack(repo
);
1569 if (packidx
&& pack
) {
1570 idx
= got_packidx_get_object_idx(packidx
, &qid
->id
);
1572 err
= paint_packed_commits(pack
, &qid
->id
,
1573 idx
, color
, ncolored
, &nqueued
, &nskip
,
1574 ids
, keep
, drop
, skip
, repo
,
1575 progress_cb
, progress_arg
, rl
,
1576 cancel_cb
, cancel_arg
);
1579 got_object_qid_free(qid
);
1587 if (got_object_idset_contains(drop
, &qid
->id
)) {
1588 err
= paint_commit(qid
, COLOR_SKIP
);
1593 err
= got_object_idset_add(keep
, &qid
->id
, NULL
);
1598 if (got_object_idset_contains(keep
, &qid
->id
)) {
1599 err
= paint_commit(qid
, COLOR_SKIP
);
1604 err
= got_object_idset_add(drop
, &qid
->id
, NULL
);
1609 if (!got_object_idset_contains(skip
, &qid
->id
)) {
1610 err
= got_object_idset_add(skip
, &qid
->id
,
1617 /* should not happen */
1618 err
= got_error_fmt(GOT_ERR_NOT_IMPL
,
1619 "%s invalid commit color %d", __func__
, color
);
1623 err
= report_progress(progress_cb
, progress_arg
, rl
,
1624 *ncolored
, 0, 0, 0L, 0, 0, 0, 0);
1628 err
= got_object_open_as_commit(&commit
, repo
, &qid
->id
);
1632 parents
= got_object_commit_get_parent_ids(commit
);
1634 struct got_object_qid
*pid
;
1635 color
= (intptr_t)qid
->data
;
1636 STAILQ_FOREACH(pid
, parents
, entry
) {
1637 err
= queue_commit_id(ids
, &pid
->id
,
1642 if (color
== COLOR_SKIP
)
1647 if (pack
== NULL
&& (commit
->flags
& GOT_COMMIT_FLAG_PACKED
)) {
1648 if (packidx
== NULL
) {
1649 err
= find_pack_for_commit_painting(&packidx
,
1650 ids
, nqueued
, repo
);
1654 if (packidx
!= NULL
) {
1655 err
= cache_pack_for_packidx(&pack
, packidx
,
1659 err
= got_privsep_init_commit_painting(
1660 pack
->privsep_child
->ibuf
);
1663 err
= send_idset(pack
->privsep_child
->ibuf
,
1667 err
= send_idset(pack
->privsep_child
->ibuf
, drop
);
1670 err
= send_idset(pack
->privsep_child
->ibuf
, skip
);
1673 err
= got_repo_pin_pack(repo
, packidx
, pack
);
1679 got_object_commit_close(commit
);
1682 got_object_qid_free(qid
);
1687 const struct got_error
*pack_err
;
1688 pack_err
= got_privsep_send_painting_commits_done(
1689 pack
->privsep_child
->ibuf
);
1694 got_object_commit_close(commit
);
1695 got_object_qid_free(qid
);
1696 got_repo_unpin_pack(repo
);
1700 static const struct got_error
*
1701 findtwixt(struct got_object_id
***res
, int *nres
, int *ncolored
,
1702 struct got_object_id
**head
, int nhead
,
1703 struct got_object_id
**tail
, int ntail
,
1704 struct got_repository
*repo
,
1705 got_pack_progress_cb progress_cb
, void *progress_arg
,
1706 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
1708 const struct got_error
*err
= NULL
;
1709 struct got_object_id_queue ids
;
1710 struct got_object_idset
*keep
, *drop
, *skip
= NULL
;
1718 keep
= got_object_idset_alloc();
1720 return got_error_from_errno("got_object_idset_alloc");
1722 drop
= got_object_idset_alloc();
1724 err
= got_error_from_errno("got_object_idset_alloc");
1728 skip
= got_object_idset_alloc();
1730 err
= got_error_from_errno("got_object_idset_alloc");
1734 for (i
= 0; i
< nhead
; i
++) {
1735 struct got_object_id
*id
= head
[i
];
1738 err
= queue_commit_or_tag_id(id
, COLOR_KEEP
, &ids
, repo
);
1743 for (i
= 0; i
< ntail
; i
++) {
1744 struct got_object_id
*id
= tail
[i
];
1747 err
= queue_commit_or_tag_id(id
, COLOR_DROP
, &ids
, repo
);
1752 err
= paint_commits(ncolored
, &ids
, nhead
+ ntail
,
1753 keep
, drop
, skip
, repo
, progress_cb
, progress_arg
, rl
,
1754 cancel_cb
, cancel_arg
);
1758 nkeep
= got_object_idset_num_elements(keep
);
1760 struct append_id_arg arg
;
1761 arg
.array
= calloc(nkeep
, sizeof(struct got_object_id
*));
1762 if (arg
.array
== NULL
) {
1763 err
= got_error_from_errno("calloc");
1769 err
= got_object_idset_for_each(keep
, append_id
, &arg
);
1775 *nres
= arg
.idx
+ 1;
1778 got_object_idset_free(keep
);
1779 got_object_idset_free(drop
);
1781 got_object_idset_free(skip
);
1782 got_object_id_queue_free(&ids
);
1786 struct load_packed_obj_arg
{
1787 /* output parameters: */
1788 struct got_object_id
*id
;
1792 /* input parameters: */
1795 struct got_object_idset
*idset
;
1796 struct got_object_idset
*idset_exclude
;
1801 got_pack_progress_cb progress_cb
;
1803 struct got_ratelimit
*rl
;
1804 got_cancel_cb cancel_cb
;
1808 static const struct got_error
*
1809 load_packed_commit_id(void *arg
, time_t mtime
, struct got_object_id
*id
,
1810 struct got_repository
*repo
)
1812 struct load_packed_obj_arg
*a
= arg
;
1814 if (got_object_idset_contains(a
->idset
, id
) ||
1815 got_object_idset_contains(a
->idset_exclude
, id
))
1818 return add_object(a
->want_meta
,
1819 a
->want_meta
? a
->idset
: a
->idset_exclude
,
1820 id
, "", GOT_OBJ_TYPE_COMMIT
, mtime
, a
->seed
, a
->loose_obj_only
,
1821 repo
, a
->ncolored
, a
->nfound
, a
->ntrees
,
1822 a
->progress_cb
, a
->progress_arg
, a
->rl
);
1825 static const struct got_error
*
1826 load_packed_tree_ids(void *arg
, struct got_tree_object
*tree
, time_t mtime
,
1827 struct got_object_id
*id
, const char *dpath
, struct got_repository
*repo
)
1829 const struct got_error
*err
;
1830 struct load_packed_obj_arg
*a
= arg
;
1831 const char *relpath
;
1834 * When we receive a tree's ID and path but not the tree itself,
1835 * this tree object was not found in the pack file. This is the
1836 * last time we are being called for this optimized traversal.
1837 * Return from here and switch to loading objects the slow way.
1841 a
->id
= got_object_id_dup(id
);
1842 if (a
->id
== NULL
) {
1843 err
= got_error_from_errno("got_object_id_dup");
1850 a
->dpath
= strdup(dpath
);
1851 if (a
->dpath
== NULL
) {
1852 err
= got_error_from_errno("strdup");
1862 if (got_object_idset_contains(a
->idset
, id
) ||
1863 got_object_idset_contains(a
->idset_exclude
, id
))
1867 while (relpath
[0] == '/')
1870 err
= add_object(a
->want_meta
,
1871 a
->want_meta
? a
->idset
: a
->idset_exclude
,
1872 id
, relpath
, GOT_OBJ_TYPE_TREE
, mtime
, a
->seed
,
1873 a
->loose_obj_only
, repo
, a
->ncolored
, a
->nfound
, a
->ntrees
,
1874 a
->progress_cb
, a
->progress_arg
, a
->rl
);
1878 return load_tree_entries(NULL
, a
->want_meta
, a
->idset
,
1879 a
->idset_exclude
, tree
, dpath
, mtime
, a
->seed
, repo
,
1880 a
->loose_obj_only
, a
->ncolored
, a
->nfound
, a
->ntrees
,
1881 a
->progress_cb
, a
->progress_arg
, a
->rl
,
1882 a
->cancel_cb
, a
->cancel_arg
);
1885 static const struct got_error
*
1886 load_packed_object_ids(int *found_all_objects
,
1887 struct got_object_id
**ours
, int nours
,
1888 struct got_object_id
**theirs
, int ntheirs
,
1889 int want_meta
, uint32_t seed
, struct got_object_idset
*idset
,
1890 struct got_object_idset
*idset_exclude
, int loose_obj_only
,
1891 struct got_repository
*repo
, struct got_packidx
*packidx
,
1892 int *ncolored
, int *nfound
, int *ntrees
,
1893 got_pack_progress_cb progress_cb
, void *progress_arg
,
1894 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
1896 const struct got_error
*err
= NULL
;
1897 struct load_packed_obj_arg lpa
;
1899 memset(&lpa
, 0, sizeof(lpa
));
1901 lpa
.want_meta
= want_meta
;
1903 lpa
.idset_exclude
= idset_exclude
;
1904 lpa
.loose_obj_only
= loose_obj_only
;
1905 lpa
.ncolored
= ncolored
;
1906 lpa
.nfound
= nfound
;
1907 lpa
.ntrees
= ntrees
;
1908 lpa
.progress_cb
= progress_cb
;
1909 lpa
.progress_arg
= progress_arg
;
1911 lpa
.cancel_cb
= cancel_cb
;
1912 lpa
.cancel_arg
= cancel_arg
;
1914 /* Attempt to load objects via got-read-pack, as far as possible. */
1915 err
= got_object_enumerate(found_all_objects
, load_packed_commit_id
,
1916 load_packed_tree_ids
, &lpa
, ours
, nours
, theirs
, ntheirs
,
1925 * An incomplete tree hierarchy was present in the pack file
1926 * and caused loading to be aborted.
1927 * Continue loading trees the slow way.
1929 err
= load_tree(want_meta
, idset
, idset_exclude
,
1930 lpa
.id
, lpa
.dpath
, lpa
.mtime
, seed
, repo
, loose_obj_only
,
1931 ncolored
, nfound
, ntrees
, progress_cb
, progress_arg
, rl
,
1932 cancel_cb
, cancel_arg
);
1938 static const struct got_error
*
1939 find_pack_for_enumeration(struct got_packidx
**best_packidx
,
1940 struct got_object_id
**ids
, int nids
, struct got_repository
*repo
)
1942 const struct got_error
*err
= NULL
;
1943 struct got_pathlist_entry
*pe
;
1944 const char *best_packidx_path
= NULL
;
1946 int ncommits_max
= 0;
1948 *best_packidx
= NULL
;
1951 * Find the largest pack which contains at least some of the
1952 * commits and tags we are interested in.
1954 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
) {
1955 const char *path_packidx
= pe
->path
;
1956 struct got_packidx
*packidx
;
1957 int nobj
, i
, idx
, ncommits
= 0;
1959 err
= got_repo_get_packidx(&packidx
, path_packidx
, repo
);
1963 nobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1964 if (nobj
<= nobj_max
)
1967 for (i
= 0; i
< nids
; i
++) {
1968 idx
= got_packidx_get_object_idx(packidx
, ids
[i
]);
1972 if (ncommits
> ncommits_max
) {
1973 best_packidx_path
= path_packidx
;
1975 ncommits_max
= ncommits
;
1979 if (best_packidx_path
&& err
== NULL
) {
1980 err
= got_repo_get_packidx(best_packidx
, best_packidx_path
,
1987 static const struct got_error
*
1988 load_object_ids(int *ncolored
, int *nfound
, int *ntrees
,
1989 struct got_object_idset
*idset
, struct got_object_id
**theirs
, int ntheirs
,
1990 struct got_object_id
**ours
, int nours
, struct got_repository
*repo
,
1991 uint32_t seed
, int loose_obj_only
, got_pack_progress_cb progress_cb
,
1992 void *progress_arg
, struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
,
1995 const struct got_error
*err
= NULL
;
1996 struct got_object_id
**ids
= NULL
;
1997 struct got_packidx
*packidx
= NULL
;
1998 int i
, nobj
= 0, obj_type
, found_all_objects
= 0;
1999 struct got_object_idset
*idset_exclude
;
2001 idset_exclude
= got_object_idset_alloc();
2002 if (idset_exclude
== NULL
)
2003 return got_error_from_errno("got_object_idset_alloc");
2009 err
= findtwixt(&ids
, &nobj
, ncolored
, ours
, nours
, theirs
, ntheirs
,
2010 repo
, progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
2014 err
= find_pack_for_enumeration(&packidx
, theirs
, ntheirs
, repo
);
2018 err
= load_packed_object_ids(&found_all_objects
,
2019 theirs
, ntheirs
, NULL
, 0, 0, seed
, idset
, idset_exclude
,
2020 loose_obj_only
, repo
, packidx
, ncolored
, nfound
, ntrees
,
2021 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
2026 for (i
= 0; i
< ntheirs
; i
++) {
2027 struct got_object_id
*id
= theirs
[i
];
2030 err
= got_object_get_type(&obj_type
, repo
, id
);
2033 if (obj_type
== GOT_OBJ_TYPE_COMMIT
) {
2034 if (!found_all_objects
) {
2035 err
= load_commit(0, idset
, idset_exclude
,
2036 id
, repo
, seed
, loose_obj_only
,
2037 ncolored
, nfound
, ntrees
,
2038 progress_cb
, progress_arg
, rl
,
2039 cancel_cb
, cancel_arg
);
2043 } else if (obj_type
== GOT_OBJ_TYPE_TAG
) {
2044 err
= load_tag(0, idset
, idset_exclude
, id
, repo
,
2045 seed
, loose_obj_only
, ncolored
, nfound
, ntrees
,
2046 progress_cb
, progress_arg
, rl
,
2047 cancel_cb
, cancel_arg
);
2053 found_all_objects
= 0;
2054 err
= find_pack_for_enumeration(&packidx
, ids
, nobj
, repo
);
2058 err
= load_packed_object_ids(&found_all_objects
, ids
,
2059 nobj
, theirs
, ntheirs
, 1, seed
, idset
, idset_exclude
,
2060 loose_obj_only
, repo
, packidx
, ncolored
, nfound
, ntrees
,
2061 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
2066 if (!found_all_objects
) {
2067 for (i
= 0; i
< nobj
; i
++) {
2068 err
= load_commit(1, idset
, idset_exclude
, ids
[i
],
2069 repo
, seed
, loose_obj_only
, ncolored
, nfound
,
2070 ntrees
, progress_cb
, progress_arg
, rl
,
2071 cancel_cb
, cancel_arg
);
2077 for (i
= 0; i
< nours
; i
++) {
2078 struct got_object_id
*id
= ours
[i
];
2079 struct got_pack_meta
*m
;
2082 m
= got_object_idset_get(idset
, id
);
2084 err
= got_object_get_type(&obj_type
, repo
, id
);
2088 obj_type
= m
->obj_type
;
2089 if (obj_type
!= GOT_OBJ_TYPE_TAG
)
2091 err
= load_tag(1, idset
, idset_exclude
, id
, repo
,
2092 seed
, loose_obj_only
, ncolored
, nfound
, ntrees
,
2093 progress_cb
, progress_arg
, rl
, cancel_cb
, cancel_arg
);
2098 for (i
= 0; i
< nobj
; i
++) {
2102 got_object_idset_free(idset_exclude
);
2106 static const struct got_error
*
2107 hwrite(FILE *f
, const void *buf
, off_t len
, SHA1_CTX
*ctx
)
2111 SHA1Update(ctx
, buf
, len
);
2112 n
= fwrite(buf
, 1, len
, f
);
2114 return got_ferror(f
, GOT_ERR_IO
);
2118 static const struct got_error
*
2119 hcopy(FILE *fsrc
, FILE *fdst
, off_t len
, SHA1_CTX
*ctx
)
2121 unsigned char buf
[65536];
2125 while (remain
> 0) {
2126 size_t copylen
= MIN(sizeof(buf
), remain
);
2127 n
= fread(buf
, 1, copylen
, fsrc
);
2129 return got_ferror(fsrc
, GOT_ERR_IO
);
2130 SHA1Update(ctx
, buf
, copylen
);
2131 n
= fwrite(buf
, 1, copylen
, fdst
);
2133 return got_ferror(fdst
, GOT_ERR_IO
);
2140 static const struct got_error
*
2141 hcopy_mmap(uint8_t *src
, off_t src_offset
, size_t src_size
,
2142 FILE *fdst
, off_t len
, SHA1_CTX
*ctx
)
2146 if (src_offset
+ len
> src_size
)
2147 return got_error(GOT_ERR_RANGE
);
2149 SHA1Update(ctx
, src
+ src_offset
, len
);
2150 n
= fwrite(src
+ src_offset
, 1, len
, fdst
);
2152 return got_ferror(fdst
, GOT_ERR_IO
);
2158 putbe32(char *b
, uint32_t n
)
2167 write_order_cmp(const void *pa
, const void *pb
)
2169 struct got_pack_meta
*a
, *b
, *ahd
, *bhd
;
2171 a
= *(struct got_pack_meta
**)pa
;
2172 b
= *(struct got_pack_meta
**)pb
;
2173 ahd
= (a
->head
== NULL
) ? a
: a
->head
;
2174 bhd
= (b
->head
== NULL
) ? b
: b
->head
;
2175 if (bhd
->mtime
< ahd
->mtime
)
2177 if (bhd
->mtime
> ahd
->mtime
)
2183 if (a
->nchain
!= b
->nchain
)
2184 return a
->nchain
- b
->nchain
;
2185 if (a
->mtime
< b
->mtime
)
2187 if (a
->mtime
> b
->mtime
)
2189 return got_object_id_cmp(&a
->id
, &b
->id
);
2193 reuse_write_order_cmp(const void *pa
, const void *pb
)
2195 struct got_pack_meta
*a
, *b
;
2197 a
= *(struct got_pack_meta
**)pa
;
2198 b
= *(struct got_pack_meta
**)pb
;
2200 if (a
->reused_delta_offset
< b
->reused_delta_offset
)
2202 if (a
->reused_delta_offset
> b
->reused_delta_offset
)
2207 static const struct got_error
*
2208 packhdr(int *hdrlen
, char *hdr
, size_t bufsize
, int obj_type
, size_t len
)
2214 hdr
[0] = obj_type
<< 4;
2215 hdr
[0] |= len
& 0xf;
2217 for (i
= 1; len
!= 0; i
++){
2219 return got_error(GOT_ERR_NO_SPACE
);
2220 hdr
[i
- 1] |= GOT_DELTA_SIZE_MORE
;
2221 hdr
[i
] = len
& GOT_DELTA_SIZE_VAL_MASK
;
2222 len
>>= GOT_DELTA_SIZE_SHIFT
;
2230 packoff(char *hdr
, off_t off
)
2235 rbuf
[0] = off
& GOT_DELTA_SIZE_VAL_MASK
;
2236 for (i
= 1; (off
>>= GOT_DELTA_SIZE_SHIFT
) != 0; i
++) {
2237 rbuf
[i
] = (--off
& GOT_DELTA_SIZE_VAL_MASK
) |
2238 GOT_DELTA_SIZE_MORE
;
2243 hdr
[j
++] = rbuf
[--i
];
2247 static const struct got_error
*
2248 deltahdr(off_t
*packfile_size
, SHA1_CTX
*ctx
, FILE *packfile
,
2249 struct got_pack_meta
*m
)
2251 const struct got_error
*err
;
2255 if (m
->prev
->off
!= 0) {
2256 err
= packhdr(&nh
, buf
, sizeof(buf
),
2257 GOT_OBJ_TYPE_OFFSET_DELTA
, m
->delta_len
);
2260 nh
+= packoff(buf
+ nh
, m
->off
- m
->prev
->off
);
2261 err
= hwrite(packfile
, buf
, nh
, ctx
);
2264 *packfile_size
+= nh
;
2266 err
= packhdr(&nh
, buf
, sizeof(buf
),
2267 GOT_OBJ_TYPE_REF_DELTA
, m
->delta_len
);
2270 err
= hwrite(packfile
, buf
, nh
, ctx
);
2273 *packfile_size
+= nh
;
2274 err
= hwrite(packfile
, m
->prev
->id
.sha1
,
2275 sizeof(m
->prev
->id
.sha1
), ctx
);
2278 *packfile_size
+= sizeof(m
->prev
->id
.sha1
);
2284 static const struct got_error
*
2285 write_packed_object(off_t
*packfile_size
, FILE *packfile
,
2286 FILE *delta_cache
, uint8_t *delta_cache_map
, size_t delta_cache_size
,
2287 struct got_pack_meta
*m
, int *outfd
, SHA1_CTX
*ctx
,
2288 struct got_repository
*repo
)
2290 const struct got_error
*err
= NULL
;
2291 struct got_deflate_checksum csum
;
2294 struct got_raw_object
*raw
= NULL
;
2297 csum
.output_sha1
= ctx
;
2298 csum
.output_crc
= NULL
;
2300 m
->off
= ftello(packfile
);
2301 if (m
->delta_len
== 0) {
2302 err
= got_object_raw_open(&raw
, outfd
, repo
, &m
->id
);
2305 err
= packhdr(&nh
, buf
, sizeof(buf
),
2306 m
->obj_type
, raw
->size
);
2309 err
= hwrite(packfile
, buf
, nh
, ctx
);
2312 *packfile_size
+= nh
;
2313 if (raw
->f
== NULL
) {
2314 err
= got_deflate_to_file_mmap(&outlen
,
2315 raw
->data
+ raw
->hdrlen
, 0, raw
->size
,
2320 if (fseeko(raw
->f
, raw
->hdrlen
, SEEK_SET
)
2322 err
= got_error_from_errno("fseeko");
2325 err
= got_deflate_to_file(&outlen
, raw
->f
,
2326 raw
->size
, packfile
, &csum
);
2330 *packfile_size
+= outlen
;
2331 got_object_raw_close(raw
);
2333 } else if (m
->delta_buf
) {
2334 err
= deltahdr(packfile_size
, ctx
, packfile
, m
);
2337 err
= hwrite(packfile
, m
->delta_buf
,
2338 m
->delta_compressed_len
, ctx
);
2341 *packfile_size
+= m
->delta_compressed_len
;
2343 m
->delta_buf
= NULL
;
2344 } else if (delta_cache_map
) {
2345 err
= deltahdr(packfile_size
, ctx
, packfile
, m
);
2348 err
= hcopy_mmap(delta_cache_map
, m
->delta_offset
,
2349 delta_cache_size
, packfile
, m
->delta_compressed_len
,
2353 *packfile_size
+= m
->delta_compressed_len
;
2355 if (fseeko(delta_cache
, m
->delta_offset
, SEEK_SET
)
2357 err
= got_error_from_errno("fseeko");
2360 err
= deltahdr(packfile_size
, ctx
, packfile
, m
);
2363 err
= hcopy(delta_cache
, packfile
,
2364 m
->delta_compressed_len
, ctx
);
2367 *packfile_size
+= m
->delta_compressed_len
;
2371 got_object_raw_close(raw
);
2375 static const struct got_error
*
2376 genpack(uint8_t *pack_sha1
, FILE *packfile
, FILE *delta_cache
,
2377 struct got_pack_meta
**deltify
, int ndeltify
,
2378 struct got_pack_meta
**reuse
, int nreuse
,
2379 int ncolored
, int nfound
, int ntrees
, int nours
,
2380 struct got_repository
*repo
,
2381 got_pack_progress_cb progress_cb
, void *progress_arg
,
2382 struct got_ratelimit
*rl
,
2383 got_cancel_cb cancel_cb
, void *cancel_arg
)
2385 const struct got_error
*err
= NULL
;
2388 struct got_pack_meta
*m
;
2391 off_t packfile_size
= 0;
2393 int delta_cache_fd
= -1;
2394 uint8_t *delta_cache_map
= NULL
;
2395 size_t delta_cache_size
= 0;
2399 #ifndef GOT_PACK_NO_MMAP
2400 delta_cache_fd
= dup(fileno(delta_cache
));
2401 if (delta_cache_fd
!= -1) {
2403 if (fstat(delta_cache_fd
, &sb
) == -1) {
2404 err
= got_error_from_errno("fstat");
2407 if (sb
.st_size
> 0 && sb
.st_size
<= SIZE_MAX
) {
2408 delta_cache_map
= mmap(NULL
, sb
.st_size
,
2409 PROT_READ
, MAP_PRIVATE
, delta_cache_fd
, 0);
2410 if (delta_cache_map
== MAP_FAILED
) {
2411 if (errno
!= ENOMEM
) {
2412 err
= got_error_from_errno("mmap");
2415 delta_cache_map
= NULL
; /* fallback on stdio */
2417 delta_cache_size
= (size_t)sb
.st_size
;
2421 err
= hwrite(packfile
, "PACK", 4, &ctx
);
2424 putbe32(buf
, GOT_PACKFILE_VERSION
);
2425 err
= hwrite(packfile
, buf
, 4, &ctx
);
2428 putbe32(buf
, ndeltify
+ nreuse
);
2429 err
= hwrite(packfile
, buf
, 4, &ctx
);
2433 qsort(deltify
, ndeltify
, sizeof(struct got_pack_meta
*),
2435 for (i
= 0; i
< ndeltify
; i
++) {
2436 err
= report_progress(progress_cb
, progress_arg
, rl
,
2437 ncolored
, nfound
, ntrees
, packfile_size
, nours
,
2438 ndeltify
+ nreuse
, ndeltify
+ nreuse
, i
);
2442 err
= write_packed_object(&packfile_size
, packfile
,
2443 delta_cache
, delta_cache_map
, delta_cache_size
,
2444 m
, &outfd
, &ctx
, repo
);
2449 qsort(reuse
, nreuse
, sizeof(struct got_pack_meta
*),
2450 reuse_write_order_cmp
);
2451 for (i
= 0; i
< nreuse
; i
++) {
2452 err
= report_progress(progress_cb
, progress_arg
, rl
,
2453 ncolored
, nfound
, ntrees
, packfile_size
, nours
,
2454 ndeltify
+ nreuse
, ndeltify
+ nreuse
, ndeltify
+ i
);
2458 err
= write_packed_object(&packfile_size
, packfile
,
2459 delta_cache
, delta_cache_map
, delta_cache_size
,
2460 m
, &outfd
, &ctx
, repo
);
2465 SHA1Final(pack_sha1
, &ctx
);
2466 n
= fwrite(pack_sha1
, 1, SHA1_DIGEST_LENGTH
, packfile
);
2467 if (n
!= SHA1_DIGEST_LENGTH
)
2468 err
= got_ferror(packfile
, GOT_ERR_IO
);
2469 packfile_size
+= SHA1_DIGEST_LENGTH
;
2470 packfile_size
+= sizeof(struct got_packfile_hdr
);
2472 err
= progress_cb(progress_arg
, ncolored
, nfound
, ntrees
,
2473 packfile_size
, nours
, ndeltify
+ nreuse
,
2474 ndeltify
+ nreuse
, ndeltify
+ nreuse
);
2479 if (outfd
!= -1 && close(outfd
) == -1 && err
== NULL
)
2480 err
= got_error_from_errno("close");
2481 if (delta_cache_map
&& munmap(delta_cache_map
, delta_cache_size
) == -1)
2482 err
= got_error_from_errno("munmap");
2483 if (delta_cache_fd
!= -1 && close(delta_cache_fd
) == -1 && err
== NULL
)
2484 err
= got_error_from_errno("close");
2488 static const struct got_error
*
2489 add_meta_idset_cb(struct got_object_id
*id
, void *data
, void *arg
)
2491 struct got_pack_meta
*m
= data
;
2492 struct got_pack_metavec
*v
= arg
;
2494 if (m
->reused_delta_offset
!= 0)
2497 return add_meta(m
, v
);
2500 const struct got_error
*
2501 got_pack_create(uint8_t *packsha1
, FILE *packfile
,
2502 struct got_object_id
**theirs
, int ntheirs
,
2503 struct got_object_id
**ours
, int nours
,
2504 struct got_repository
*repo
, int loose_obj_only
, int allow_empty
,
2505 got_pack_progress_cb progress_cb
, void *progress_arg
,
2506 got_cancel_cb cancel_cb
, void *cancel_arg
)
2508 const struct got_error
*err
;
2509 int delta_cache_fd
= -1;
2510 FILE *delta_cache
= NULL
;
2511 struct got_object_idset
*idset
;
2512 struct got_ratelimit rl
;
2513 struct got_pack_metavec deltify
, reuse
;
2514 int ncolored
= 0, nfound
= 0, ntrees
= 0;
2518 seed
= arc4random();
2520 memset(&deltify
, 0, sizeof(deltify
));
2521 memset(&reuse
, 0, sizeof(reuse
));
2523 got_ratelimit_init(&rl
, 0, 500);
2525 idset
= got_object_idset_alloc();
2527 return got_error_from_errno("got_object_idset_alloc");
2529 err
= load_object_ids(&ncolored
, &nfound
, &ntrees
, idset
, theirs
,
2530 ntheirs
, ours
, nours
, repo
, seed
, loose_obj_only
,
2531 progress_cb
, progress_arg
, &rl
, cancel_cb
, cancel_arg
);
2536 err
= progress_cb(progress_arg
, ncolored
, nfound
, ntrees
,
2537 0L, nours
, got_object_idset_num_elements(idset
), 0, 0);
2542 if (got_object_idset_num_elements(idset
) == 0 && !allow_empty
) {
2543 err
= got_error(GOT_ERR_CANNOT_PACK
);
2547 delta_cache_fd
= got_opentempfd();
2548 if (delta_cache_fd
== -1) {
2549 err
= got_error_from_errno("got_opentemp");
2554 reuse
.meta
= calloc(reuse
.metasz
,
2555 sizeof(struct got_pack_meta
*));
2556 if (reuse
.meta
== NULL
) {
2557 err
= got_error_from_errno("calloc");
2561 err
= search_deltas(&reuse
, idset
, delta_cache_fd
, ncolored
, nfound
,
2562 ntrees
, nours
, repo
, progress_cb
, progress_arg
, &rl
,
2563 cancel_cb
, cancel_arg
);
2567 delta_cache
= fdopen(delta_cache_fd
, "a+");
2568 if (delta_cache
== NULL
) {
2569 err
= got_error_from_errno("fdopen");
2572 delta_cache_fd
= -1;
2574 if (fseeko(delta_cache
, 0L, SEEK_END
) == -1) {
2575 err
= got_error_from_errno("fseeko");
2579 ndeltify
= got_object_idset_num_elements(idset
) - reuse
.nmeta
;
2581 deltify
.meta
= calloc(ndeltify
, sizeof(struct got_pack_meta
*));
2582 if (deltify
.meta
== NULL
) {
2583 err
= got_error_from_errno("calloc");
2586 deltify
.metasz
= ndeltify
;
2588 err
= got_object_idset_for_each(idset
, add_meta_idset_cb
,
2592 if (deltify
.nmeta
> 0) {
2593 err
= pick_deltas(deltify
.meta
, deltify
.nmeta
,
2594 ncolored
, nfound
, ntrees
, nours
, reuse
.nmeta
,
2595 delta_cache
, repo
, progress_cb
, progress_arg
, &rl
,
2596 cancel_cb
, cancel_arg
);
2602 if (fflush(delta_cache
) == EOF
) {
2603 err
= got_error_from_errno("fflush");
2606 err
= genpack(packsha1
, packfile
, delta_cache
, deltify
.meta
,
2607 deltify
.nmeta
, reuse
.meta
, reuse
.nmeta
, ncolored
, nfound
, ntrees
,
2608 nours
, repo
, progress_cb
, progress_arg
, &rl
,
2609 cancel_cb
, cancel_arg
);
2613 free_nmeta(deltify
.meta
, deltify
.nmeta
);
2614 free_nmeta(reuse
.meta
, reuse
.nmeta
);
2615 got_object_idset_free(idset
);
2616 if (delta_cache_fd
!= -1 && close(delta_cache_fd
) == -1 && err
== NULL
)
2617 err
= got_error_from_errno("close");
2618 if (delta_cache
&& fclose(delta_cache
) == EOF
&& err
== NULL
)
2619 err
= got_error_from_errno("fclose");