2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
23 #include <sys/socket.h>
36 #include "got_error.h"
37 #include "got_cancel.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_repository_admin.h"
42 #include "got_opentemp.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_hash.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_idset.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_ratelimit.h"
54 #include "got_lib_pack_create.h"
55 #include "got_lib_lockfile.h"
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 static const struct got_error
*
62 get_reflist_object_ids(struct got_object_id
***ids
, int *nobjects
,
63 unsigned int wanted_obj_type_mask
, struct got_reflist_head
*refs
,
64 struct got_repository
*repo
,
65 got_cancel_cb cancel_cb
, void *cancel_arg
)
67 const struct got_error
*err
= NULL
;
68 const size_t alloc_chunksz
= 256;
70 struct got_reflist_entry
*re
;
76 err
= got_reflist_sort(refs
,
77 got_ref_cmp_by_commit_timestamp_descending
, repo
);
81 *ids
= reallocarray(NULL
, alloc_chunksz
, sizeof(struct got_object_id
*));
83 return got_error_from_errno("reallocarray");
84 nalloc
= alloc_chunksz
;
86 TAILQ_FOREACH(re
, refs
, entry
) {
87 struct got_object_id
*id
;
90 err
= cancel_cb(cancel_arg
);
95 err
= got_ref_resolve(&id
, repo
, re
->ref
);
99 if (wanted_obj_type_mask
!= GOT_OBJ_TYPE_ANY
) {
101 err
= got_object_get_type(&obj_type
, repo
, id
);
104 if ((wanted_obj_type_mask
& (1 << obj_type
)) == 0) {
111 if (nalloc
<= *nobjects
) {
112 struct got_object_id
**new;
113 new = recallocarray(*ids
, nalloc
,
114 nalloc
+ alloc_chunksz
,
115 sizeof(struct got_object_id
*));
117 err
= got_error_from_errno(
122 nalloc
+= alloc_chunksz
;
124 (*ids
)[*nobjects
] = id
;
125 if ((*ids
)[*nobjects
] == NULL
) {
126 err
= got_error_from_errno("got_object_id_dup");
133 for (i
= 0; i
< *nobjects
; i
++)
142 const struct got_error
*
143 got_repo_pack_objects(FILE **packfile
, struct got_object_id
**pack_hash
,
144 struct got_reflist_head
*include_refs
,
145 struct got_reflist_head
*exclude_refs
, struct got_repository
*repo
,
146 int loose_obj_only
, int force_refdelta
,
147 got_pack_progress_cb progress_cb
, void *progress_arg
,
148 got_cancel_cb cancel_cb
, void *cancel_arg
)
150 const struct got_error
*err
= NULL
;
151 struct got_object_id
**ours
= NULL
, **theirs
= NULL
;
152 int nours
= 0, ntheirs
= 0, packfd
= -1, i
;
153 char *tmpfile_path
= NULL
, *path
= NULL
, *packfile_path
= NULL
;
154 char *hash_str
= NULL
;
155 FILE *delta_cache
= NULL
;
156 struct got_ratelimit rl
;
161 got_ratelimit_init(&rl
, 0, 500);
163 if (asprintf(&path
, "%s/%s/packing.pack",
164 got_repo_get_path_git_dir(repo
), GOT_OBJECTS_PACK_DIR
) == -1) {
165 err
= got_error_from_errno("asprintf");
168 err
= got_opentemp_named_fd(&tmpfile_path
, &packfd
, path
, "");
172 if (fchmod(packfd
, GOT_DEFAULT_PACK_MODE
) == -1) {
173 err
= got_error_from_errno2("fchmod", tmpfile_path
);
177 delta_cache
= got_opentemp();
178 if (delta_cache
== NULL
) {
179 err
= got_error_from_errno("got_opentemp");
183 err
= get_reflist_object_ids(&ours
, &nours
,
184 (1 << GOT_OBJ_TYPE_COMMIT
) | (1 << GOT_OBJ_TYPE_TAG
),
185 include_refs
, repo
, cancel_cb
, cancel_arg
);
190 err
= got_error(GOT_ERR_CANNOT_PACK
);
194 if (!TAILQ_EMPTY(exclude_refs
)) {
195 err
= get_reflist_object_ids(&theirs
, &ntheirs
,
196 (1 << GOT_OBJ_TYPE_COMMIT
) | (1 << GOT_OBJ_TYPE_TAG
),
198 cancel_cb
, cancel_arg
);
203 *pack_hash
= calloc(1, sizeof(**pack_hash
));
204 if (*pack_hash
== NULL
) {
205 err
= got_error_from_errno("calloc");
208 err
= got_pack_create(*pack_hash
, packfd
, delta_cache
,
209 theirs
, ntheirs
, ours
, nours
, repo
, loose_obj_only
,
210 0, force_refdelta
, progress_cb
, progress_arg
, &rl
,
211 cancel_cb
, cancel_arg
);
215 err
= got_object_id_str(&hash_str
, *pack_hash
);
218 if (asprintf(&packfile_path
, "%s/%s/pack-%s.pack",
219 got_repo_get_path_git_dir(repo
), GOT_OBJECTS_PACK_DIR
,
221 err
= got_error_from_errno("asprintf");
225 if (lseek(packfd
, 0L, SEEK_SET
) == -1) {
226 err
= got_error_from_errno("lseek");
229 if (rename(tmpfile_path
, packfile_path
) == -1) {
230 err
= got_error_from_errno3("rename", tmpfile_path
,
237 *packfile
= fdopen(packfd
, "w");
238 if (*packfile
== NULL
) {
239 err
= got_error_from_errno2("fdopen", tmpfile_path
);
244 for (i
= 0; i
< nours
; i
++)
247 for (i
= 0; i
< ntheirs
; i
++)
250 if (packfd
!= -1 && close(packfd
) == -1 && err
== NULL
)
251 err
= got_error_from_errno2("close", packfile_path
);
252 if (delta_cache
&& fclose(delta_cache
) == EOF
&& err
== NULL
)
253 err
= got_error_from_errno("fclose");
254 if (tmpfile_path
&& unlink(tmpfile_path
) == -1 && err
== NULL
)
255 err
= got_error_from_errno2("unlink", tmpfile_path
);
270 const struct got_error
*
271 got_repo_index_pack(FILE *packfile
, struct got_object_id
*pack_hash
,
272 struct got_repository
*repo
,
273 got_pack_index_progress_cb progress_cb
, void *progress_arg
,
274 got_cancel_cb cancel_cb
, void *cancel_arg
)
279 int npackfd
= -1, idxfd
= -1, nidxfd
= -1;
281 int idxstatus
, done
= 0;
282 const struct got_error
*err
;
283 struct imsgbuf idxibuf
;
285 char *tmpidxpath
= NULL
;
286 char *packfile_path
= NULL
, *idxpath
= NULL
, *id_str
= NULL
;
287 const char *repo_path
= got_repo_get_path_git_dir(repo
);
290 for (i
= 0; i
< nitems(tmpfds
); i
++)
293 if (asprintf(&path
, "%s/%s/indexing.idx",
294 repo_path
, GOT_OBJECTS_PACK_DIR
) == -1) {
295 err
= got_error_from_errno("asprintf");
298 err
= got_opentemp_named_fd(&tmpidxpath
, &idxfd
, path
, "");
302 if (fchmod(idxfd
, GOT_DEFAULT_PACK_MODE
) == -1) {
303 err
= got_error_from_errno2("fchmod", tmpidxpath
);
309 err
= got_error_from_errno("dup");
313 for (i
= 0; i
< nitems(tmpfds
); i
++) {
314 tmpfds
[i
] = got_opentempfd();
315 if (tmpfds
[i
] == -1) {
316 err
= got_error_from_errno("got_opentempfd");
321 err
= got_object_id_str(&id_str
, pack_hash
);
325 if (asprintf(&packfile_path
, "%s/%s/pack-%s.pack",
326 repo_path
, GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
327 err
= got_error_from_errno("asprintf");
331 if (fstat(fileno(packfile
), &sb
) == -1) {
332 err
= got_error_from_errno2("fstat", packfile_path
);
336 if (asprintf(&idxpath
, "%s/%s/pack-%s.idx",
337 repo_path
, GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
338 err
= got_error_from_errno("asprintf");
342 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, imsg_idxfds
) == -1) {
343 err
= got_error_from_errno("socketpair");
348 err
= got_error_from_errno("fork");
350 } else if (idxpid
== 0)
351 got_privsep_exec_child(imsg_idxfds
,
352 GOT_PATH_PROG_INDEX_PACK
, packfile_path
);
353 if (close(imsg_idxfds
[1]) == -1) {
354 err
= got_error_from_errno("close");
357 if (imsgbuf_init(&idxibuf
, imsg_idxfds
[0]) == -1) {
358 err
= got_error_from_errno("imsgbuf_init");
361 imsgbuf_allow_fdpass(&idxibuf
);
363 npackfd
= dup(fileno(packfile
));
365 err
= got_error_from_errno("dup");
368 err
= got_privsep_send_index_pack_req(&idxibuf
, pack_hash
, npackfd
);
372 err
= got_privsep_send_index_pack_outfd(&idxibuf
, nidxfd
);
376 for (i
= 0; i
< nitems(tmpfds
); i
++) {
377 err
= got_privsep_send_tmpfd(&idxibuf
, tmpfds
[i
]);
384 int nobj_total
, nobj_indexed
, nobj_loose
, nobj_resolved
;
387 err
= cancel_cb(cancel_arg
);
392 err
= got_privsep_recv_index_progress(&done
, &nobj_total
,
393 &nobj_indexed
, &nobj_loose
, &nobj_resolved
,
397 if (nobj_indexed
!= 0) {
398 err
= progress_cb(progress_arg
, sb
.st_size
,
399 nobj_total
, nobj_indexed
, nobj_loose
,
405 if (close(imsg_idxfds
[0]) == -1) {
406 err
= got_error_from_errno("close");
409 if (waitpid(idxpid
, &idxstatus
, 0) == -1) {
410 err
= got_error_from_errno("waitpid");
414 if (rename(tmpidxpath
, idxpath
) == -1) {
415 err
= got_error_from_errno3("rename", tmpidxpath
, idxpath
);
422 if (tmpidxpath
&& unlink(tmpidxpath
) == -1 && err
== NULL
)
423 err
= got_error_from_errno2("unlink", tmpidxpath
);
424 if (npackfd
!= -1 && close(npackfd
) == -1 && err
== NULL
)
425 err
= got_error_from_errno("close");
426 if (idxfd
!= -1 && close(idxfd
) == -1 && err
== NULL
)
427 err
= got_error_from_errno("close");
428 for (i
= 0; i
< nitems(tmpfds
); i
++) {
429 if (tmpfds
[i
] != -1 && close(tmpfds
[i
]) == -1 && err
== NULL
)
430 err
= got_error_from_errno("close");
438 const struct got_error
*
439 got_repo_find_pack(FILE **packfile
, struct got_object_id
**pack_hash
,
440 struct got_repository
*repo
, const char *packfile_path
)
442 const struct got_error
*err
= NULL
;
443 const char *packdir_path
= NULL
;
444 char *packfile_name
= NULL
, *p
, *dot
;
445 struct got_object_id id
;
451 packdir_path
= got_repo_get_path_objects_pack(repo
);
452 if (packdir_path
== NULL
)
453 return got_error_from_errno("got_repo_get_path_objects_pack");
455 if (!got_path_is_child(packfile_path
, packdir_path
,
456 strlen(packdir_path
))) {
457 err
= got_error_path(packfile_path
, GOT_ERR_BAD_PATH
);
462 err
= got_path_basename(&packfile_name
, packfile_path
);
467 if (strncmp(p
, "pack-", 5) != 0) {
468 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
469 "'%s' is not a valid pack file name",
474 dot
= strchr(p
, '.');
476 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
477 "'%s' is not a valid pack file name",
481 if (strcmp(dot
+ 1, "pack") != 0) {
482 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
483 "'%s' is not a valid pack file name",
488 if (!got_parse_object_id(&id
, p
, repo
->algo
)) {
489 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
490 "'%s' is not a valid pack file name",
495 *pack_hash
= got_object_id_dup(&id
);
496 if (*pack_hash
== NULL
) {
497 err
= got_error_from_errno("got_object_id_dup");
501 packfd
= open(packfile_path
, O_RDONLY
| O_NOFOLLOW
| O_CLOEXEC
);
503 err
= got_error_from_errno2("open", packfile_path
);
507 *packfile
= fdopen(packfd
, "r");
508 if (*packfile
== NULL
) {
509 err
= got_error_from_errno2("fdopen", packfile_path
);
514 if (packfd
!= -1 && close(packfd
) == -1 && err
== NULL
)
515 err
= got_error_from_errno2("close", packfile_path
);
524 const struct got_error
*
525 got_repo_list_pack(FILE *packfile
, struct got_object_id
*pack_hash
,
526 struct got_repository
*repo
, got_pack_list_cb list_cb
, void *list_arg
,
527 got_cancel_cb cancel_cb
, void *cancel_arg
)
529 const struct got_error
*err
= NULL
;
530 char *id_str
= NULL
, *idxpath
= NULL
, *packpath
= NULL
;
531 struct got_packidx
*packidx
= NULL
;
532 struct got_pack
*pack
= NULL
;
534 size_t digest_len
= got_hash_digest_length(repo
->algo
);
536 err
= got_object_id_str(&id_str
, pack_hash
);
540 if (asprintf(&packpath
, "%s/pack-%s.pack",
541 GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
542 err
= got_error_from_errno("asprintf");
545 if (asprintf(&idxpath
, "%s/pack-%s.idx",
546 GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
547 err
= got_error_from_errno("asprintf");
551 err
= got_packidx_open(&packidx
, got_repo_get_fd(repo
), idxpath
, 1,
556 err
= got_repo_cache_pack(&pack
, repo
, packpath
, packidx
);
560 nobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
561 for (i
= 0; i
< nobj
; i
++) {
563 struct got_object_id id
, base_id
;
564 off_t offset
, base_offset
= 0;
570 err
= cancel_cb(cancel_arg
);
574 oid
= packidx
->hdr
.sorted_ids
+ i
* digest_len
;
575 id
.algo
= repo
->algo
;
576 memcpy(id
.hash
, oid
, digest_len
);
578 offset
= got_packidx_get_object_offset(packidx
, i
);
580 err
= got_error(GOT_ERR_BAD_PACKIDX
);
584 err
= got_pack_parse_object_type_and_size(&type
, &size
, &tslen
,
590 case GOT_OBJ_TYPE_OFFSET_DELTA
:
591 err
= got_pack_parse_offset_delta(&base_offset
, &len
,
592 pack
, offset
, tslen
);
596 case GOT_OBJ_TYPE_REF_DELTA
:
597 err
= got_pack_parse_ref_delta(&base_id
,
598 pack
, offset
, tslen
);
603 err
= (*list_cb
)(list_arg
, &id
, type
, offset
, size
,
604 base_offset
, &base_id
);
614 got_packidx_close(packidx
);
618 static const struct got_error
*
619 repo_cleanup_lock(struct got_repository
*repo
, struct got_lockfile
**lk
)
621 const struct got_error
*err
;
622 char myname
[_POSIX_HOST_NAME_MAX
+ 1];
624 if (gethostname(myname
, sizeof(myname
)) == -1)
625 return got_error_from_errno("gethostname");
627 err
= got_lockfile_lock(lk
, "gc.pid", got_repo_get_fd(repo
));
632 * Git uses these info to provide some verbiage when finds a
633 * lock during `git gc --force' so don't try too hard to avoid
634 * short writes and don't care if a race happens between the
635 * lockfile creation and the write itself.
637 if (dprintf((*lk
)->fd
, "%d %s", getpid(), myname
) < 0)
638 return got_error_from_errno("dprintf");
643 static const struct got_error
*
644 report_cleanup_progress(got_cleanup_progress_cb progress_cb
,
645 void *progress_arg
, struct got_ratelimit
*rl
,
646 int ncommits
, int nloose
, int npurged
, int nredundant
)
648 const struct got_error
*err
;
651 if (progress_cb
== NULL
)
654 err
= got_ratelimit_check(&elapsed
, rl
);
658 return progress_cb(progress_arg
, ncommits
, nloose
, npurged
,
662 static const struct got_error
*
663 get_loose_object_ids(struct got_object_idset
**loose_ids
,
664 off_t
*ondisk_size
, int ncommits
,
665 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
666 struct got_ratelimit
*rl
, struct got_repository
*repo
)
668 const struct got_error
*err
= NULL
;
669 char *path_objects
= NULL
, *path
= NULL
;
671 struct got_object
*obj
= NULL
;
672 struct got_object_id id
;
677 *loose_ids
= got_object_idset_alloc();
678 if (*loose_ids
== NULL
)
679 return got_error_from_errno("got_object_idset_alloc");
681 path_objects
= got_repo_get_path_objects(repo
);
682 if (path_objects
== NULL
) {
683 err
= got_error_from_errno("got_repo_get_path_objects");
687 for (i
= 0; i
<= 0xff; i
++) {
690 if (asprintf(&path
, "%s/%.2x", path_objects
, i
) == -1) {
691 err
= got_error_from_errno("asprintf");
697 if (errno
== ENOENT
) {
701 err
= got_error_from_errno2("opendir", path
);
705 while ((dent
= readdir(dir
)) != NULL
) {
708 if (strcmp(dent
->d_name
, ".") == 0 ||
709 strcmp(dent
->d_name
, "..") == 0)
712 if (asprintf(&id_str
, "%.2x%s", i
, dent
->d_name
) == -1) {
713 err
= got_error_from_errno("asprintf");
717 if (!got_parse_object_id(&id
, id_str
, repo
->algo
)) {
723 err
= got_object_open_loose_fd(&fd
, &id
, repo
);
726 if (fstat(fd
, &sb
) == -1) {
727 err
= got_error_from_errno("fstat");
730 err
= got_object_read_header_privsep(&obj
, &id
, repo
,
734 fd
= -1; /* already closed */
737 case GOT_OBJ_TYPE_COMMIT
:
738 case GOT_OBJ_TYPE_TREE
:
739 case GOT_OBJ_TYPE_BLOB
:
740 case GOT_OBJ_TYPE_TAG
:
743 err
= got_error_fmt(GOT_ERR_OBJ_TYPE
,
747 got_object_close(obj
);
749 (*ondisk_size
) += sb
.st_size
;
750 err
= got_object_idset_add(*loose_ids
, &id
, NULL
);
753 err
= report_cleanup_progress(progress_cb
,
754 progress_arg
, rl
, ncommits
,
755 got_object_idset_num_elements(*loose_ids
),
761 if (closedir(dir
) != 0) {
762 err
= got_error_from_errno("closedir");
771 if (dir
&& closedir(dir
) != 0 && err
== NULL
)
772 err
= got_error_from_errno("closedir");
773 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
774 err
= got_error_from_errno("close");
776 got_object_idset_free(*loose_ids
);
780 got_object_close(obj
);
786 static const struct got_error
*
787 load_tree_entries(struct got_object_id_queue
*ids
,
788 struct got_object_idset
*traversed_ids
, struct got_object_id
*tree_id
,
789 const char *dpath
, struct got_repository
*repo
,
790 got_cancel_cb cancel_cb
, void *cancel_arg
)
792 const struct got_error
*err
;
793 struct got_tree_object
*tree
;
797 err
= got_object_open_as_tree(&tree
, repo
, tree_id
);
801 for (i
= 0; i
< got_object_tree_get_nentries(tree
); i
++) {
802 struct got_tree_entry
*e
= got_object_tree_get_entry(tree
, i
);
803 struct got_object_id
*id
= got_tree_entry_get_id(e
);
804 mode_t mode
= got_tree_entry_get_mode(e
);
807 err
= (*cancel_cb
)(cancel_arg
);
812 if (got_object_tree_entry_is_symlink(e
) ||
813 got_object_tree_entry_is_submodule(e
) ||
814 got_object_idset_contains(traversed_ids
, id
))
817 if (asprintf(&p
, "%s%s%s", dpath
, dpath
[0] != '\0' ? "/" : "",
818 got_tree_entry_get_name(e
)) == -1) {
819 err
= got_error_from_errno("asprintf");
824 struct got_object_qid
*qid
;
825 err
= got_object_qid_alloc(&qid
, id
);
828 STAILQ_INSERT_TAIL(ids
, qid
, entry
);
829 } else if (S_ISREG(mode
)) {
830 /* This blob is referenced. */
831 err
= got_object_idset_add(traversed_ids
, id
, NULL
);
839 got_object_tree_close(tree
);
844 static const struct got_error
*
845 load_tree(struct got_object_idset
*traversed_ids
,
846 struct got_object_id
*tree_id
,
847 const char *dpath
, struct got_repository
*repo
,
848 got_cancel_cb cancel_cb
, void *cancel_arg
)
850 const struct got_error
*err
= NULL
;
851 struct got_object_id_queue tree_ids
;
852 struct got_object_qid
*qid
;
854 err
= got_object_qid_alloc(&qid
, tree_id
);
858 STAILQ_INIT(&tree_ids
);
859 STAILQ_INSERT_TAIL(&tree_ids
, qid
, entry
);
861 while (!STAILQ_EMPTY(&tree_ids
)) {
863 err
= (*cancel_cb
)(cancel_arg
);
868 qid
= STAILQ_FIRST(&tree_ids
);
869 STAILQ_REMOVE_HEAD(&tree_ids
, entry
);
871 if (got_object_idset_contains(traversed_ids
, &qid
->id
)) {
872 got_object_qid_free(qid
);
876 err
= got_object_idset_add(traversed_ids
, &qid
->id
, NULL
);
878 got_object_qid_free(qid
);
882 err
= load_tree_entries(&tree_ids
, traversed_ids
,
883 &qid
->id
, dpath
, repo
, cancel_cb
, cancel_arg
);
884 got_object_qid_free(qid
);
889 got_object_id_queue_free(&tree_ids
);
893 static const struct got_error
*
894 load_commit_or_tag(int *ncommits
, struct got_object_idset
*traversed_ids
,
895 struct got_object_id
*id
, struct got_repository
*repo
,
896 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
897 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
899 const struct got_error
*err
;
900 struct got_commit_object
*commit
= NULL
;
901 struct got_tag_object
*tag
= NULL
;
902 struct got_object_id
*tree_id
= NULL
;
903 struct got_object_id_queue ids
;
904 struct got_object_qid
*qid
;
907 err
= got_object_qid_alloc(&qid
, id
);
912 STAILQ_INSERT_TAIL(&ids
, qid
, entry
);
914 while (!STAILQ_EMPTY(&ids
)) {
916 err
= (*cancel_cb
)(cancel_arg
);
921 qid
= STAILQ_FIRST(&ids
);
922 STAILQ_REMOVE_HEAD(&ids
, entry
);
924 if (got_object_idset_contains(traversed_ids
, &qid
->id
)) {
925 got_object_qid_free(qid
);
930 err
= got_object_idset_add(traversed_ids
, &qid
->id
, NULL
);
934 err
= got_object_get_type(&obj_type
, repo
, &qid
->id
);
938 case GOT_OBJ_TYPE_COMMIT
:
939 err
= got_object_open_as_commit(&commit
, repo
,
944 case GOT_OBJ_TYPE_TAG
:
945 err
= got_object_open_as_tag(&tag
, repo
, &qid
->id
);
950 /* should not happen */
951 err
= got_error(GOT_ERR_OBJ_TYPE
);
955 /* Find a tree object to scan. */
957 tree_id
= got_object_commit_get_tree_id(commit
);
959 obj_type
= got_object_tag_get_object_type(tag
);
961 case GOT_OBJ_TYPE_COMMIT
:
962 err
= got_object_open_as_commit(&commit
, repo
,
963 got_object_tag_get_object_id(tag
));
966 tree_id
= got_object_commit_get_tree_id(commit
);
968 case GOT_OBJ_TYPE_TREE
:
969 tree_id
= got_object_tag_get_object_id(tag
);
973 * Tag points at something other than a
974 * commit or tree. Leave this weird tag object
975 * and the object it points to.
977 if (got_object_idset_contains(traversed_ids
,
978 got_object_tag_get_object_id(tag
)))
980 err
= got_object_idset_add(traversed_ids
,
981 got_object_tag_get_object_id(tag
), NULL
);
989 err
= load_tree(traversed_ids
, tree_id
, "",
990 repo
, cancel_cb
, cancel_arg
);
996 (*ncommits
)++; /* scanned tags are counted as commits */
998 err
= report_cleanup_progress(progress_cb
, progress_arg
, rl
,
999 *ncommits
, -1, -1, -1);
1004 /* Find parent commits to scan. */
1005 const struct got_object_id_queue
*parent_ids
;
1006 parent_ids
= got_object_commit_get_parent_ids(commit
);
1007 err
= got_object_id_queue_copy(parent_ids
, &ids
);
1010 got_object_commit_close(commit
);
1014 got_object_tag_close(tag
);
1017 got_object_qid_free(qid
);
1022 got_object_qid_free(qid
);
1024 got_object_commit_close(commit
);
1026 got_object_tag_close(tag
);
1027 got_object_id_queue_free(&ids
);
1031 static const struct got_error
*
1032 is_object_packed(int *packed
, struct got_repository
*repo
,
1033 struct got_object_id
*id
)
1035 const struct got_error
*err
;
1036 struct got_object
*obj
;
1040 err
= got_object_open_packed(&obj
, id
, repo
);
1042 if (err
->code
== GOT_ERR_NO_OBJ
)
1046 got_object_close(obj
);
1051 struct purge_loose_object_arg
{
1052 struct got_repository
*repo
;
1053 got_cleanup_progress_cb progress_cb
;
1055 struct got_ratelimit
*rl
;
1056 struct got_object_idset
*traversed_ids
;
1067 static const struct got_error
*
1068 purge_loose_object(struct got_object_id
*id
, void *data
, void *arg
)
1070 struct purge_loose_object_arg
*a
= arg
;
1071 const struct got_error
*err
, *unlock_err
= NULL
;
1073 int packed
, fd
= -1;
1075 struct got_lockfile
*lf
= NULL
;
1077 err
= is_object_packed(&packed
, a
->repo
, id
);
1081 if (!packed
&& got_object_idset_contains(a
->traversed_ids
, id
))
1087 err
= got_object_get_path(&path
, id
, a
->repo
);
1091 err
= got_object_open_loose_fd(&fd
, id
, a
->repo
);
1095 if (fstat(fd
, &sb
) == -1) {
1096 err
= got_error_from_errno("fstat");
1101 * Do not delete objects which are younger than our maximum
1102 * modification time threshold. This prevents a race where
1103 * new objects which are being added to the repository
1104 * concurrently would be deleted.
1106 if (a
->ignore_mtime
|| sb
.st_mtime
<= a
->max_mtime
) {
1108 err
= got_lockfile_lock(&lf
, path
, -1);
1111 if (unlink(path
) == -1) {
1112 err
= got_error_from_errno2("unlink", path
);
1118 a
->size_purged
+= sb
.st_size
;
1119 err
= report_cleanup_progress(a
->progress_cb
, a
->progress_arg
,
1120 a
->rl
, a
->ncommits
, a
->nloose
, a
->npurged
, -1);
1125 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
1126 err
= got_error_from_errno("close");
1129 unlock_err
= got_lockfile_unlock(lf
, -1);
1130 return err
? err
: unlock_err
;
1133 static const struct got_error
*
1134 repo_purge_unreferenced_loose_objects(struct got_repository
*repo
,
1135 struct got_object_idset
*traversed_ids
,
1136 off_t
*size_before
, off_t
*size_after
, int ncommits
, int *nloose
,
1137 int *npacked
, int *npurged
, int dry_run
, int ignore_mtime
,
1138 time_t max_mtime
, struct got_ratelimit
*rl
,
1139 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
1140 got_cancel_cb cancel_cb
, void *cancel_arg
)
1142 const struct got_error
*err
;
1143 struct got_object_idset
*loose_ids
;
1144 struct purge_loose_object_arg arg
;
1146 err
= get_loose_object_ids(&loose_ids
, size_before
, ncommits
,
1147 progress_cb
, progress_arg
, rl
, repo
);
1150 *nloose
= got_object_idset_num_elements(loose_ids
);
1152 got_object_idset_free(loose_ids
);
1154 err
= progress_cb(progress_arg
, 0, 0, 0, -1);
1161 memset(&arg
, 0, sizeof(arg
));
1163 arg
.progress_arg
= progress_arg
;
1164 arg
.progress_cb
= progress_cb
;
1166 arg
.traversed_ids
= traversed_ids
;
1167 arg
.nloose
= *nloose
;
1170 arg
.size_purged
= 0;
1171 arg
.dry_run
= dry_run
;
1172 arg
.max_mtime
= max_mtime
;
1173 arg
.ignore_mtime
= ignore_mtime
;
1174 err
= got_object_idset_for_each(loose_ids
, purge_loose_object
, &arg
);
1178 *size_after
= *size_before
- arg
.size_purged
;
1179 *npacked
= arg
.npacked
;
1180 *npurged
= arg
.npurged
;
1182 /* Produce a final progress report. */
1184 err
= progress_cb(progress_arg
, ncommits
, *nloose
,
1190 got_object_idset_free(loose_ids
);
1194 static const struct got_error
*
1195 purge_redundant_pack(struct got_repository
*repo
, const char *packidx_path
,
1196 int dry_run
, int ignore_mtime
, time_t max_mtime
,
1197 int *remove
, off_t
*size_before
, off_t
*size_after
)
1199 static const char *ext
[] = {".idx", ".pack", ".rev", ".bitmap",
1200 ".promisor", ".mtimes"};
1202 char *dot
, path
[PATH_MAX
];
1205 if (strlcpy(path
, packidx_path
, sizeof(path
)) >= sizeof(path
))
1206 return got_error(GOT_ERR_NO_SPACE
);
1209 * Do not delete pack files which are younger than our maximum
1210 * modification time threshold. This prevents a race where a
1211 * new pack file which is being added to the repository
1212 * concurrently would be deleted.
1214 if (fstatat(got_repo_get_fd(repo
), path
, &sb
, 0) == -1) {
1215 if (errno
== ENOENT
)
1217 return got_error_from_errno2("fstatat", path
);
1219 if (!ignore_mtime
&& sb
.st_mtime
> max_mtime
)
1223 * For compatibility with Git, if a matching .keep file exist
1224 * don't delete the packfile.
1226 dot
= strrchr(path
, '.');
1228 if (strlcat(path
, ".keep", sizeof(path
)) >= sizeof(path
))
1229 return got_error(GOT_ERR_NO_SPACE
);
1230 if (faccessat(got_repo_get_fd(repo
), path
, F_OK
, 0) == 0)
1233 for (i
= 0; i
< nitems(ext
); ++i
) {
1236 if (strlcat(path
, ext
[i
], sizeof(path
)) >=
1238 return got_error(GOT_ERR_NO_SPACE
);
1240 if (fstatat(got_repo_get_fd(repo
), path
, &sb
, 0) ==
1242 if (errno
== ENOENT
)
1244 return got_error_from_errno2("fstatat", path
);
1247 *size_before
+= sb
.st_size
;
1249 *size_after
+= sb
.st_size
;
1256 if (unlinkat(got_repo_get_fd(repo
), path
, 0) == -1) {
1257 if (errno
== ENOENT
)
1259 return got_error_from_errno2("unlinkat",
1267 static const struct got_error
*
1268 pack_is_redundant(int *redundant
, struct got_repository
*repo
,
1269 struct got_object_idset
*traversed_ids
,
1270 const char *packidx_path
, struct got_object_idset
*idset
)
1272 const struct got_error
*err
;
1273 struct got_packidx
*packidx
;
1275 struct got_object_id id
;
1277 size_t digest_len
= got_hash_digest_length(repo
->algo
);
1281 err
= got_repo_get_packidx(&packidx
, packidx_path
, repo
);
1285 nobjects
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1286 for (i
= 0; i
< nobjects
; ++i
) {
1287 pid
= packidx
->hdr
.sorted_ids
+ i
* digest_len
;
1289 memset(&id
, 0, sizeof(id
));
1290 memcpy(&id
.hash
, pid
, digest_len
);
1291 id
.algo
= repo
->algo
;
1293 if (got_object_idset_contains(idset
, &id
))
1296 if (!got_object_idset_contains(traversed_ids
, &id
))
1300 err
= got_object_idset_add(idset
, &id
, NULL
);
1314 pack_info_cmp(const void *a
, const void *b
)
1316 const struct pack_info
*pa
, *pb
;
1320 if (pa
->nobjects
== pb
->nobjects
)
1321 return strcmp(pa
->path
, pb
->path
);
1322 if (pa
->nobjects
> pb
->nobjects
)
1327 static const struct got_error
*
1328 repo_purge_redundant_packfiles(struct got_repository
*repo
,
1329 struct got_object_idset
*traversed_ids
,
1330 off_t
*size_before
, off_t
*size_after
, int dry_run
, int ignore_mtime
,
1331 time_t max_mtime
, int nloose
, int ncommits
, int npurged
,
1332 struct got_ratelimit
*rl
,
1333 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
1334 got_cancel_cb cancel_cb
, void *cancel_arg
)
1336 const struct got_error
*err
;
1337 struct pack_info
*pinfo
, *sorted
= NULL
;
1338 struct got_packidx
*packidx
;
1339 struct got_object_idset
*idset
= NULL
;
1340 struct got_pathlist_entry
*pe
;
1342 int remove
, redundant_packs
= 0;
1345 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
)
1351 sorted
= calloc(npacks
, sizeof(*sorted
));
1353 return got_error_from_errno("calloc");
1356 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
) {
1357 err
= got_repo_get_packidx(&packidx
, pe
->path
, repo
);
1361 pinfo
= &sorted
[i
++];
1362 pinfo
->path
= pe
->path
;
1363 pinfo
->nobjects
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1365 qsort(sorted
, npacks
, sizeof(*sorted
), pack_info_cmp
);
1367 idset
= got_object_idset_alloc();
1368 if (idset
== NULL
) {
1369 err
= got_error_from_errno("got_object_idset_alloc");
1373 for (i
= 0; i
< npacks
; ++i
) {
1375 err
= (*cancel_cb
)(cancel_arg
);
1380 err
= pack_is_redundant(&remove
, repo
, traversed_ids
,
1381 sorted
[i
].path
, idset
);
1384 err
= purge_redundant_pack(repo
, sorted
[i
].path
, dry_run
,
1385 ignore_mtime
, max_mtime
, &remove
, size_before
, size_after
);
1390 err
= report_cleanup_progress(progress_cb
, progress_arg
,
1391 rl
, ncommits
, nloose
, npurged
, ++redundant_packs
);
1396 /* Produce a final progress report. */
1398 err
= progress_cb(progress_arg
, ncommits
, nloose
, npurged
,
1406 got_object_idset_free(idset
);
1410 const struct got_error
*
1411 got_repo_cleanup(struct got_repository
*repo
,
1412 off_t
*loose_before
, off_t
*loose_after
,
1413 off_t
*pack_before
, off_t
*pack_after
,
1414 int *ncommits
, int *nloose
, int *npacked
, int dry_run
, int ignore_mtime
,
1415 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
1416 got_cancel_cb cancel_cb
, void *cancel_arg
)
1418 const struct got_error
*unlock_err
, *err
= NULL
;
1419 struct got_lockfile
*lk
= NULL
;
1420 struct got_ratelimit rl
;
1421 struct got_reflist_head refs
;
1422 struct got_object_idset
*traversed_ids
= NULL
;
1423 struct got_reflist_entry
*re
;
1424 struct got_object_id
**referenced_ids
;
1427 time_t max_mtime
= 0;
1430 got_ratelimit_init(&rl
, 0, 500);
1440 err
= repo_cleanup_lock(repo
, &lk
);
1444 traversed_ids
= got_object_idset_alloc();
1445 if (traversed_ids
== NULL
) {
1446 err
= got_error_from_errno("got_object_idset_alloc");
1450 err
= got_ref_list(&refs
, repo
, "", got_ref_cmp_by_name
, NULL
);
1453 if (!ignore_mtime
) {
1454 TAILQ_FOREACH(re
, &refs
, entry
) {
1455 time_t mtime
= got_ref_get_mtime(re
->ref
);
1456 if (mtime
> max_mtime
)
1460 * For safety, keep objects created within 10 minutes
1461 * before the youngest reference was created.
1463 if (max_mtime
>= 600)
1467 err
= get_reflist_object_ids(&referenced_ids
, &nreferenced
,
1468 (1 << GOT_OBJ_TYPE_COMMIT
) | (1 << GOT_OBJ_TYPE_TAG
),
1469 &refs
, repo
, cancel_cb
, cancel_arg
);
1473 for (i
= 0; i
< nreferenced
; i
++) {
1474 struct got_object_id
*id
= referenced_ids
[i
];
1475 err
= load_commit_or_tag(ncommits
, traversed_ids
,
1476 id
, repo
, progress_cb
, progress_arg
, &rl
,
1477 cancel_cb
, cancel_arg
);
1482 err
= repo_purge_unreferenced_loose_objects(repo
, traversed_ids
,
1483 loose_before
, loose_after
, *ncommits
, nloose
, npacked
, &npurged
,
1484 dry_run
, ignore_mtime
, max_mtime
, &rl
, progress_cb
, progress_arg
,
1485 cancel_cb
, cancel_arg
);
1489 err
= repo_purge_redundant_packfiles(repo
, traversed_ids
,
1490 pack_before
, pack_after
, dry_run
, ignore_mtime
, max_mtime
,
1491 *nloose
, *ncommits
, npurged
, &rl
, progress_cb
, progress_arg
,
1492 cancel_cb
, cancel_arg
);
1498 unlock_err
= got_lockfile_unlock(lk
, got_repo_get_fd(repo
));
1503 got_object_idset_free(traversed_ids
);
1507 const struct got_error
*
1508 got_repo_remove_lonely_packidx(struct got_repository
*repo
, int dry_run
,
1509 got_lonely_packidx_progress_cb progress_cb
, void *progress_arg
,
1510 got_cancel_cb cancel_cb
, void *cancel_arg
)
1512 const struct got_error
*err
= NULL
;
1513 DIR *packdir
= NULL
;
1514 struct dirent
*dent
;
1515 char *pack_relpath
= NULL
;
1519 packdir_fd
= openat(got_repo_get_fd(repo
),
1520 GOT_OBJECTS_PACK_DIR
, O_DIRECTORY
| O_CLOEXEC
);
1521 if (packdir_fd
== -1) {
1522 if (errno
== ENOENT
)
1524 return got_error_from_errno_fmt("openat: %s/%s",
1525 got_repo_get_path_git_dir(repo
),
1526 GOT_OBJECTS_PACK_DIR
);
1529 packdir
= fdopendir(packdir_fd
);
1530 if (packdir
== NULL
) {
1531 err
= got_error_from_errno("fdopendir");
1536 while ((dent
= readdir(packdir
)) != NULL
) {
1538 err
= cancel_cb(cancel_arg
);
1543 if (!got_repo_is_packidx_filename(dent
->d_name
,
1544 strlen(dent
->d_name
),
1545 got_repo_get_object_format(repo
)))
1548 err
= got_packidx_get_packfile_path(&pack_relpath
,
1553 if (fstatat(packdir_fd
, pack_relpath
, &sb
, 0) != -1) {
1555 pack_relpath
= NULL
;
1558 if (errno
!= ENOENT
) {
1559 err
= got_error_from_errno_fmt("fstatat: %s/%s/%s",
1560 got_repo_get_path_git_dir(repo
),
1561 GOT_OBJECTS_PACK_DIR
,
1567 if (unlinkat(packdir_fd
, dent
->d_name
, 0) == -1) {
1568 err
= got_error_from_errno("unlinkat");
1574 if (asprintf(&path
, "%s/%s/%s",
1575 got_repo_get_path_git_dir(repo
),
1576 GOT_OBJECTS_PACK_DIR
,
1577 dent
->d_name
) == -1) {
1578 err
= got_error_from_errno("asprintf");
1581 err
= progress_cb(progress_arg
, path
);
1587 pack_relpath
= NULL
;
1590 if (packdir
&& closedir(packdir
) != 0 && err
== NULL
)
1591 err
= got_error_from_errno("closedir");