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 imsg_init(&idxibuf
, imsg_idxfds
[0]);
359 npackfd
= dup(fileno(packfile
));
361 err
= got_error_from_errno("dup");
364 err
= got_privsep_send_index_pack_req(&idxibuf
, pack_hash
, npackfd
);
368 err
= got_privsep_send_index_pack_outfd(&idxibuf
, nidxfd
);
372 for (i
= 0; i
< nitems(tmpfds
); i
++) {
373 err
= got_privsep_send_tmpfd(&idxibuf
, tmpfds
[i
]);
380 int nobj_total
, nobj_indexed
, nobj_loose
, nobj_resolved
;
383 err
= cancel_cb(cancel_arg
);
388 err
= got_privsep_recv_index_progress(&done
, &nobj_total
,
389 &nobj_indexed
, &nobj_loose
, &nobj_resolved
,
393 if (nobj_indexed
!= 0) {
394 err
= progress_cb(progress_arg
, sb
.st_size
,
395 nobj_total
, nobj_indexed
, nobj_loose
,
401 if (close(imsg_idxfds
[0]) == -1) {
402 err
= got_error_from_errno("close");
405 if (waitpid(idxpid
, &idxstatus
, 0) == -1) {
406 err
= got_error_from_errno("waitpid");
410 if (rename(tmpidxpath
, idxpath
) == -1) {
411 err
= got_error_from_errno3("rename", tmpidxpath
, idxpath
);
418 if (tmpidxpath
&& unlink(tmpidxpath
) == -1 && err
== NULL
)
419 err
= got_error_from_errno2("unlink", tmpidxpath
);
420 if (npackfd
!= -1 && close(npackfd
) == -1 && err
== NULL
)
421 err
= got_error_from_errno("close");
422 if (idxfd
!= -1 && close(idxfd
) == -1 && err
== NULL
)
423 err
= got_error_from_errno("close");
424 for (i
= 0; i
< nitems(tmpfds
); i
++) {
425 if (tmpfds
[i
] != -1 && close(tmpfds
[i
]) == -1 && err
== NULL
)
426 err
= got_error_from_errno("close");
434 const struct got_error
*
435 got_repo_find_pack(FILE **packfile
, struct got_object_id
**pack_hash
,
436 struct got_repository
*repo
, const char *packfile_path
)
438 const struct got_error
*err
= NULL
;
439 const char *packdir_path
= NULL
;
440 char *packfile_name
= NULL
, *p
, *dot
;
441 struct got_object_id id
;
447 packdir_path
= got_repo_get_path_objects_pack(repo
);
448 if (packdir_path
== NULL
)
449 return got_error_from_errno("got_repo_get_path_objects_pack");
451 if (!got_path_is_child(packfile_path
, packdir_path
,
452 strlen(packdir_path
))) {
453 err
= got_error_path(packfile_path
, GOT_ERR_BAD_PATH
);
458 err
= got_path_basename(&packfile_name
, packfile_path
);
463 if (strncmp(p
, "pack-", 5) != 0) {
464 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
465 "'%s' is not a valid pack file name",
470 dot
= strchr(p
, '.');
472 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
473 "'%s' is not a valid pack file name",
477 if (strcmp(dot
+ 1, "pack") != 0) {
478 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
479 "'%s' is not a valid pack file name",
484 if (!got_parse_object_id(&id
, p
, repo
->algo
)) {
485 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
486 "'%s' is not a valid pack file name",
491 *pack_hash
= got_object_id_dup(&id
);
492 if (*pack_hash
== NULL
) {
493 err
= got_error_from_errno("got_object_id_dup");
497 packfd
= open(packfile_path
, O_RDONLY
| O_NOFOLLOW
| O_CLOEXEC
);
499 err
= got_error_from_errno2("open", packfile_path
);
503 *packfile
= fdopen(packfd
, "r");
504 if (*packfile
== NULL
) {
505 err
= got_error_from_errno2("fdopen", packfile_path
);
510 if (packfd
!= -1 && close(packfd
) == -1 && err
== NULL
)
511 err
= got_error_from_errno2("close", packfile_path
);
520 const struct got_error
*
521 got_repo_list_pack(FILE *packfile
, struct got_object_id
*pack_hash
,
522 struct got_repository
*repo
, got_pack_list_cb list_cb
, void *list_arg
,
523 got_cancel_cb cancel_cb
, void *cancel_arg
)
525 const struct got_error
*err
= NULL
;
526 char *id_str
= NULL
, *idxpath
= NULL
, *packpath
= NULL
;
527 struct got_packidx
*packidx
= NULL
;
528 struct got_pack
*pack
= NULL
;
530 size_t digest_len
= got_hash_digest_length(repo
->algo
);
532 err
= got_object_id_str(&id_str
, pack_hash
);
536 if (asprintf(&packpath
, "%s/pack-%s.pack",
537 GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
538 err
= got_error_from_errno("asprintf");
541 if (asprintf(&idxpath
, "%s/pack-%s.idx",
542 GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
543 err
= got_error_from_errno("asprintf");
547 err
= got_packidx_open(&packidx
, got_repo_get_fd(repo
), idxpath
, 1,
552 err
= got_repo_cache_pack(&pack
, repo
, packpath
, packidx
);
556 nobj
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
557 for (i
= 0; i
< nobj
; i
++) {
559 struct got_object_id id
, base_id
;
560 off_t offset
, base_offset
= 0;
566 err
= cancel_cb(cancel_arg
);
570 oid
= packidx
->hdr
.sorted_ids
+ i
* digest_len
;
571 id
.algo
= repo
->algo
;
572 memcpy(id
.hash
, oid
, digest_len
);
574 offset
= got_packidx_get_object_offset(packidx
, i
);
576 err
= got_error(GOT_ERR_BAD_PACKIDX
);
580 err
= got_pack_parse_object_type_and_size(&type
, &size
, &tslen
,
586 case GOT_OBJ_TYPE_OFFSET_DELTA
:
587 err
= got_pack_parse_offset_delta(&base_offset
, &len
,
588 pack
, offset
, tslen
);
592 case GOT_OBJ_TYPE_REF_DELTA
:
593 err
= got_pack_parse_ref_delta(&base_id
,
594 pack
, offset
, tslen
);
599 err
= (*list_cb
)(list_arg
, &id
, type
, offset
, size
,
600 base_offset
, &base_id
);
610 got_packidx_close(packidx
);
614 static const struct got_error
*
615 repo_cleanup_lock(struct got_repository
*repo
, struct got_lockfile
**lk
)
617 const struct got_error
*err
;
618 char myname
[_POSIX_HOST_NAME_MAX
+ 1];
620 if (gethostname(myname
, sizeof(myname
)) == -1)
621 return got_error_from_errno("gethostname");
623 err
= got_lockfile_lock(lk
, "gc.pid", got_repo_get_fd(repo
));
628 * Git uses these info to provide some verbiage when finds a
629 * lock during `git gc --force' so don't try too hard to avoid
630 * short writes and don't care if a race happens between the
631 * lockfile creation and the write itself.
633 if (dprintf((*lk
)->fd
, "%d %s", getpid(), myname
) < 0)
634 return got_error_from_errno("dprintf");
639 static const struct got_error
*
640 report_cleanup_progress(got_cleanup_progress_cb progress_cb
,
641 void *progress_arg
, struct got_ratelimit
*rl
,
642 int ncommits
, int nloose
, int npurged
, int nredundant
)
644 const struct got_error
*err
;
647 if (progress_cb
== NULL
)
650 err
= got_ratelimit_check(&elapsed
, rl
);
654 return progress_cb(progress_arg
, ncommits
, nloose
, npurged
,
658 static const struct got_error
*
659 get_loose_object_ids(struct got_object_idset
**loose_ids
,
660 off_t
*ondisk_size
, int ncommits
,
661 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
662 struct got_ratelimit
*rl
, struct got_repository
*repo
)
664 const struct got_error
*err
= NULL
;
665 char *path_objects
= NULL
, *path
= NULL
;
667 struct got_object
*obj
= NULL
;
668 struct got_object_id id
;
673 *loose_ids
= got_object_idset_alloc();
674 if (*loose_ids
== NULL
)
675 return got_error_from_errno("got_object_idset_alloc");
677 path_objects
= got_repo_get_path_objects(repo
);
678 if (path_objects
== NULL
) {
679 err
= got_error_from_errno("got_repo_get_path_objects");
683 for (i
= 0; i
<= 0xff; i
++) {
686 if (asprintf(&path
, "%s/%.2x", path_objects
, i
) == -1) {
687 err
= got_error_from_errno("asprintf");
693 if (errno
== ENOENT
) {
697 err
= got_error_from_errno2("opendir", path
);
701 while ((dent
= readdir(dir
)) != NULL
) {
704 if (strcmp(dent
->d_name
, ".") == 0 ||
705 strcmp(dent
->d_name
, "..") == 0)
708 if (asprintf(&id_str
, "%.2x%s", i
, dent
->d_name
) == -1) {
709 err
= got_error_from_errno("asprintf");
713 if (!got_parse_object_id(&id
, id_str
, repo
->algo
)) {
719 err
= got_object_open_loose_fd(&fd
, &id
, repo
);
722 if (fstat(fd
, &sb
) == -1) {
723 err
= got_error_from_errno("fstat");
726 err
= got_object_read_header_privsep(&obj
, &id
, repo
,
730 fd
= -1; /* already closed */
733 case GOT_OBJ_TYPE_COMMIT
:
734 case GOT_OBJ_TYPE_TREE
:
735 case GOT_OBJ_TYPE_BLOB
:
736 case GOT_OBJ_TYPE_TAG
:
739 err
= got_error_fmt(GOT_ERR_OBJ_TYPE
,
743 got_object_close(obj
);
745 (*ondisk_size
) += sb
.st_size
;
746 err
= got_object_idset_add(*loose_ids
, &id
, NULL
);
749 err
= report_cleanup_progress(progress_cb
,
750 progress_arg
, rl
, ncommits
,
751 got_object_idset_num_elements(*loose_ids
),
757 if (closedir(dir
) != 0) {
758 err
= got_error_from_errno("closedir");
767 if (dir
&& closedir(dir
) != 0 && err
== NULL
)
768 err
= got_error_from_errno("closedir");
769 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
770 err
= got_error_from_errno("close");
772 got_object_idset_free(*loose_ids
);
776 got_object_close(obj
);
782 static const struct got_error
*
783 load_tree_entries(struct got_object_id_queue
*ids
,
784 struct got_object_idset
*traversed_ids
, struct got_object_id
*tree_id
,
785 const char *dpath
, struct got_repository
*repo
,
786 got_cancel_cb cancel_cb
, void *cancel_arg
)
788 const struct got_error
*err
;
789 struct got_tree_object
*tree
;
793 err
= got_object_open_as_tree(&tree
, repo
, tree_id
);
797 for (i
= 0; i
< got_object_tree_get_nentries(tree
); i
++) {
798 struct got_tree_entry
*e
= got_object_tree_get_entry(tree
, i
);
799 struct got_object_id
*id
= got_tree_entry_get_id(e
);
800 mode_t mode
= got_tree_entry_get_mode(e
);
803 err
= (*cancel_cb
)(cancel_arg
);
808 if (got_object_tree_entry_is_symlink(e
) ||
809 got_object_tree_entry_is_submodule(e
) ||
810 got_object_idset_contains(traversed_ids
, id
))
813 if (asprintf(&p
, "%s%s%s", dpath
, dpath
[0] != '\0' ? "/" : "",
814 got_tree_entry_get_name(e
)) == -1) {
815 err
= got_error_from_errno("asprintf");
820 struct got_object_qid
*qid
;
821 err
= got_object_qid_alloc(&qid
, id
);
824 STAILQ_INSERT_TAIL(ids
, qid
, entry
);
825 } else if (S_ISREG(mode
)) {
826 /* This blob is referenced. */
827 err
= got_object_idset_add(traversed_ids
, id
, NULL
);
835 got_object_tree_close(tree
);
840 static const struct got_error
*
841 load_tree(struct got_object_idset
*traversed_ids
,
842 struct got_object_id
*tree_id
,
843 const char *dpath
, struct got_repository
*repo
,
844 got_cancel_cb cancel_cb
, void *cancel_arg
)
846 const struct got_error
*err
= NULL
;
847 struct got_object_id_queue tree_ids
;
848 struct got_object_qid
*qid
;
850 err
= got_object_qid_alloc(&qid
, tree_id
);
854 STAILQ_INIT(&tree_ids
);
855 STAILQ_INSERT_TAIL(&tree_ids
, qid
, entry
);
857 while (!STAILQ_EMPTY(&tree_ids
)) {
859 err
= (*cancel_cb
)(cancel_arg
);
864 qid
= STAILQ_FIRST(&tree_ids
);
865 STAILQ_REMOVE_HEAD(&tree_ids
, entry
);
867 if (got_object_idset_contains(traversed_ids
, &qid
->id
)) {
868 got_object_qid_free(qid
);
872 err
= got_object_idset_add(traversed_ids
, &qid
->id
, NULL
);
874 got_object_qid_free(qid
);
878 err
= load_tree_entries(&tree_ids
, traversed_ids
,
879 &qid
->id
, dpath
, repo
, cancel_cb
, cancel_arg
);
880 got_object_qid_free(qid
);
885 got_object_id_queue_free(&tree_ids
);
889 static const struct got_error
*
890 load_commit_or_tag(int *ncommits
, struct got_object_idset
*traversed_ids
,
891 struct got_object_id
*id
, struct got_repository
*repo
,
892 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
893 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
895 const struct got_error
*err
;
896 struct got_commit_object
*commit
= NULL
;
897 struct got_tag_object
*tag
= NULL
;
898 struct got_object_id
*tree_id
= NULL
;
899 struct got_object_id_queue ids
;
900 struct got_object_qid
*qid
;
903 err
= got_object_qid_alloc(&qid
, id
);
908 STAILQ_INSERT_TAIL(&ids
, qid
, entry
);
910 while (!STAILQ_EMPTY(&ids
)) {
912 err
= (*cancel_cb
)(cancel_arg
);
917 qid
= STAILQ_FIRST(&ids
);
918 STAILQ_REMOVE_HEAD(&ids
, entry
);
920 if (got_object_idset_contains(traversed_ids
, &qid
->id
)) {
921 got_object_qid_free(qid
);
926 err
= got_object_idset_add(traversed_ids
, &qid
->id
, NULL
);
930 err
= got_object_get_type(&obj_type
, repo
, &qid
->id
);
934 case GOT_OBJ_TYPE_COMMIT
:
935 err
= got_object_open_as_commit(&commit
, repo
,
940 case GOT_OBJ_TYPE_TAG
:
941 err
= got_object_open_as_tag(&tag
, repo
, &qid
->id
);
946 /* should not happen */
947 err
= got_error(GOT_ERR_OBJ_TYPE
);
951 /* Find a tree object to scan. */
953 tree_id
= got_object_commit_get_tree_id(commit
);
955 obj_type
= got_object_tag_get_object_type(tag
);
957 case GOT_OBJ_TYPE_COMMIT
:
958 err
= got_object_open_as_commit(&commit
, repo
,
959 got_object_tag_get_object_id(tag
));
962 tree_id
= got_object_commit_get_tree_id(commit
);
964 case GOT_OBJ_TYPE_TREE
:
965 tree_id
= got_object_tag_get_object_id(tag
);
969 * Tag points at something other than a
970 * commit or tree. Leave this weird tag object
971 * and the object it points to.
973 if (got_object_idset_contains(traversed_ids
,
974 got_object_tag_get_object_id(tag
)))
976 err
= got_object_idset_add(traversed_ids
,
977 got_object_tag_get_object_id(tag
), NULL
);
985 err
= load_tree(traversed_ids
, tree_id
, "",
986 repo
, cancel_cb
, cancel_arg
);
992 (*ncommits
)++; /* scanned tags are counted as commits */
994 err
= report_cleanup_progress(progress_cb
, progress_arg
, rl
,
995 *ncommits
, -1, -1, -1);
1000 /* Find parent commits to scan. */
1001 const struct got_object_id_queue
*parent_ids
;
1002 parent_ids
= got_object_commit_get_parent_ids(commit
);
1003 err
= got_object_id_queue_copy(parent_ids
, &ids
);
1006 got_object_commit_close(commit
);
1010 got_object_tag_close(tag
);
1013 got_object_qid_free(qid
);
1018 got_object_qid_free(qid
);
1020 got_object_commit_close(commit
);
1022 got_object_tag_close(tag
);
1023 got_object_id_queue_free(&ids
);
1027 static const struct got_error
*
1028 is_object_packed(int *packed
, struct got_repository
*repo
,
1029 struct got_object_id
*id
)
1031 const struct got_error
*err
;
1032 struct got_object
*obj
;
1036 err
= got_object_open_packed(&obj
, id
, repo
);
1038 if (err
->code
== GOT_ERR_NO_OBJ
)
1042 got_object_close(obj
);
1047 struct purge_loose_object_arg
{
1048 struct got_repository
*repo
;
1049 got_cleanup_progress_cb progress_cb
;
1051 struct got_ratelimit
*rl
;
1052 struct got_object_idset
*traversed_ids
;
1063 static const struct got_error
*
1064 purge_loose_object(struct got_object_id
*id
, void *data
, void *arg
)
1066 struct purge_loose_object_arg
*a
= arg
;
1067 const struct got_error
*err
, *unlock_err
= NULL
;
1069 int packed
, fd
= -1;
1071 struct got_lockfile
*lf
= NULL
;
1073 err
= is_object_packed(&packed
, a
->repo
, id
);
1077 if (!packed
&& got_object_idset_contains(a
->traversed_ids
, id
))
1083 err
= got_object_get_path(&path
, id
, a
->repo
);
1087 err
= got_object_open_loose_fd(&fd
, id
, a
->repo
);
1091 if (fstat(fd
, &sb
) == -1) {
1092 err
= got_error_from_errno("fstat");
1097 * Do not delete objects which are younger than our maximum
1098 * modification time threshold. This prevents a race where
1099 * new objects which are being added to the repository
1100 * concurrently would be deleted.
1102 if (a
->ignore_mtime
|| sb
.st_mtime
<= a
->max_mtime
) {
1104 err
= got_lockfile_lock(&lf
, path
, -1);
1107 if (unlink(path
) == -1) {
1108 err
= got_error_from_errno2("unlink", path
);
1114 a
->size_purged
+= sb
.st_size
;
1115 err
= report_cleanup_progress(a
->progress_cb
, a
->progress_arg
,
1116 a
->rl
, a
->ncommits
, a
->nloose
, a
->npurged
, -1);
1121 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
1122 err
= got_error_from_errno("close");
1125 unlock_err
= got_lockfile_unlock(lf
, -1);
1126 return err
? err
: unlock_err
;
1129 static const struct got_error
*
1130 repo_purge_unreferenced_loose_objects(struct got_repository
*repo
,
1131 struct got_object_idset
*traversed_ids
,
1132 off_t
*size_before
, off_t
*size_after
, int ncommits
, int *nloose
,
1133 int *npacked
, int *npurged
, int dry_run
, int ignore_mtime
,
1134 time_t max_mtime
, struct got_ratelimit
*rl
,
1135 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
1136 got_cancel_cb cancel_cb
, void *cancel_arg
)
1138 const struct got_error
*err
;
1139 struct got_object_idset
*loose_ids
;
1140 struct purge_loose_object_arg arg
;
1142 err
= get_loose_object_ids(&loose_ids
, size_before
, ncommits
,
1143 progress_cb
, progress_arg
, rl
, repo
);
1146 *nloose
= got_object_idset_num_elements(loose_ids
);
1148 got_object_idset_free(loose_ids
);
1150 err
= progress_cb(progress_arg
, 0, 0, 0, -1);
1157 memset(&arg
, 0, sizeof(arg
));
1159 arg
.progress_arg
= progress_arg
;
1160 arg
.progress_cb
= progress_cb
;
1162 arg
.traversed_ids
= traversed_ids
;
1163 arg
.nloose
= *nloose
;
1166 arg
.size_purged
= 0;
1167 arg
.dry_run
= dry_run
;
1168 arg
.max_mtime
= max_mtime
;
1169 arg
.ignore_mtime
= ignore_mtime
;
1170 err
= got_object_idset_for_each(loose_ids
, purge_loose_object
, &arg
);
1174 *size_after
= *size_before
- arg
.size_purged
;
1175 *npacked
= arg
.npacked
;
1176 *npurged
= arg
.npurged
;
1178 /* Produce a final progress report. */
1180 err
= progress_cb(progress_arg
, ncommits
, *nloose
,
1186 got_object_idset_free(loose_ids
);
1190 static const struct got_error
*
1191 purge_redundant_pack(struct got_repository
*repo
, const char *packidx_path
,
1192 int dry_run
, int ignore_mtime
, time_t max_mtime
,
1193 int *remove
, off_t
*size_before
, off_t
*size_after
)
1195 static const char *ext
[] = {".idx", ".pack", ".rev", ".bitmap",
1196 ".promisor", ".mtimes"};
1198 char *dot
, path
[PATH_MAX
];
1201 if (strlcpy(path
, packidx_path
, sizeof(path
)) >= sizeof(path
))
1202 return got_error(GOT_ERR_NO_SPACE
);
1205 * Do not delete pack files which are younger than our maximum
1206 * modification time threshold. This prevents a race where a
1207 * new pack file which is being added to the repository
1208 * concurrently would be deleted.
1210 if (fstatat(got_repo_get_fd(repo
), path
, &sb
, 0) == -1) {
1211 if (errno
== ENOENT
)
1213 return got_error_from_errno2("fstatat", path
);
1215 if (!ignore_mtime
&& sb
.st_mtime
> max_mtime
)
1219 * For compatibility with Git, if a matching .keep file exist
1220 * don't delete the packfile.
1222 dot
= strrchr(path
, '.');
1224 if (strlcat(path
, ".keep", sizeof(path
)) >= sizeof(path
))
1225 return got_error(GOT_ERR_NO_SPACE
);
1226 if (faccessat(got_repo_get_fd(repo
), path
, F_OK
, 0) == 0)
1229 for (i
= 0; i
< nitems(ext
); ++i
) {
1232 if (strlcat(path
, ext
[i
], sizeof(path
)) >=
1234 return got_error(GOT_ERR_NO_SPACE
);
1236 if (fstatat(got_repo_get_fd(repo
), path
, &sb
, 0) ==
1238 if (errno
== ENOENT
)
1240 return got_error_from_errno2("fstatat", path
);
1243 *size_before
+= sb
.st_size
;
1245 *size_after
+= sb
.st_size
;
1252 if (unlinkat(got_repo_get_fd(repo
), path
, 0) == -1) {
1253 if (errno
== ENOENT
)
1255 return got_error_from_errno2("unlinkat",
1263 static const struct got_error
*
1264 pack_is_redundant(int *redundant
, struct got_repository
*repo
,
1265 struct got_object_idset
*traversed_ids
,
1266 const char *packidx_path
, struct got_object_idset
*idset
)
1268 const struct got_error
*err
;
1269 struct got_packidx
*packidx
;
1271 struct got_object_id id
;
1273 size_t digest_len
= got_hash_digest_length(repo
->algo
);
1277 err
= got_repo_get_packidx(&packidx
, packidx_path
, repo
);
1281 nobjects
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1282 for (i
= 0; i
< nobjects
; ++i
) {
1283 pid
= packidx
->hdr
.sorted_ids
+ i
* digest_len
;
1285 memset(&id
, 0, sizeof(id
));
1286 memcpy(&id
.hash
, pid
, digest_len
);
1287 id
.algo
= repo
->algo
;
1289 if (got_object_idset_contains(idset
, &id
))
1292 if (!got_object_idset_contains(traversed_ids
, &id
))
1296 err
= got_object_idset_add(idset
, &id
, NULL
);
1310 pack_info_cmp(const void *a
, const void *b
)
1312 const struct pack_info
*pa
, *pb
;
1316 if (pa
->nobjects
== pb
->nobjects
)
1317 return strcmp(pa
->path
, pb
->path
);
1318 if (pa
->nobjects
> pb
->nobjects
)
1323 static const struct got_error
*
1324 repo_purge_redundant_packfiles(struct got_repository
*repo
,
1325 struct got_object_idset
*traversed_ids
,
1326 off_t
*size_before
, off_t
*size_after
, int dry_run
, int ignore_mtime
,
1327 time_t max_mtime
, int nloose
, int ncommits
, int npurged
,
1328 struct got_ratelimit
*rl
,
1329 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
1330 got_cancel_cb cancel_cb
, void *cancel_arg
)
1332 const struct got_error
*err
;
1333 struct pack_info
*pinfo
, *sorted
= NULL
;
1334 struct got_packidx
*packidx
;
1335 struct got_object_idset
*idset
= NULL
;
1336 struct got_pathlist_entry
*pe
;
1338 int remove
, redundant_packs
= 0;
1341 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
)
1347 sorted
= calloc(npacks
, sizeof(*sorted
));
1349 return got_error_from_errno("calloc");
1352 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
) {
1353 err
= got_repo_get_packidx(&packidx
, pe
->path
, repo
);
1357 pinfo
= &sorted
[i
++];
1358 pinfo
->path
= pe
->path
;
1359 pinfo
->nobjects
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1361 qsort(sorted
, npacks
, sizeof(*sorted
), pack_info_cmp
);
1363 idset
= got_object_idset_alloc();
1364 if (idset
== NULL
) {
1365 err
= got_error_from_errno("got_object_idset_alloc");
1369 for (i
= 0; i
< npacks
; ++i
) {
1371 err
= (*cancel_cb
)(cancel_arg
);
1376 err
= pack_is_redundant(&remove
, repo
, traversed_ids
,
1377 sorted
[i
].path
, idset
);
1380 err
= purge_redundant_pack(repo
, sorted
[i
].path
, dry_run
,
1381 ignore_mtime
, max_mtime
, &remove
, size_before
, size_after
);
1386 err
= report_cleanup_progress(progress_cb
, progress_arg
,
1387 rl
, ncommits
, nloose
, npurged
, ++redundant_packs
);
1392 /* Produce a final progress report. */
1394 err
= progress_cb(progress_arg
, ncommits
, nloose
, npurged
,
1402 got_object_idset_free(idset
);
1406 const struct got_error
*
1407 got_repo_cleanup(struct got_repository
*repo
,
1408 off_t
*loose_before
, off_t
*loose_after
,
1409 off_t
*pack_before
, off_t
*pack_after
,
1410 int *ncommits
, int *nloose
, int *npacked
, int dry_run
, int ignore_mtime
,
1411 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
1412 got_cancel_cb cancel_cb
, void *cancel_arg
)
1414 const struct got_error
*unlock_err
, *err
= NULL
;
1415 struct got_lockfile
*lk
= NULL
;
1416 struct got_ratelimit rl
;
1417 struct got_reflist_head refs
;
1418 struct got_object_idset
*traversed_ids
= NULL
;
1419 struct got_reflist_entry
*re
;
1420 struct got_object_id
**referenced_ids
;
1423 time_t max_mtime
= 0;
1426 got_ratelimit_init(&rl
, 0, 500);
1436 err
= repo_cleanup_lock(repo
, &lk
);
1440 traversed_ids
= got_object_idset_alloc();
1441 if (traversed_ids
== NULL
) {
1442 err
= got_error_from_errno("got_object_idset_alloc");
1446 err
= got_ref_list(&refs
, repo
, "", got_ref_cmp_by_name
, NULL
);
1449 if (!ignore_mtime
) {
1450 TAILQ_FOREACH(re
, &refs
, entry
) {
1451 time_t mtime
= got_ref_get_mtime(re
->ref
);
1452 if (mtime
> max_mtime
)
1456 * For safety, keep objects created within 10 minutes
1457 * before the youngest reference was created.
1459 if (max_mtime
>= 600)
1463 err
= get_reflist_object_ids(&referenced_ids
, &nreferenced
,
1464 (1 << GOT_OBJ_TYPE_COMMIT
) | (1 << GOT_OBJ_TYPE_TAG
),
1465 &refs
, repo
, cancel_cb
, cancel_arg
);
1469 for (i
= 0; i
< nreferenced
; i
++) {
1470 struct got_object_id
*id
= referenced_ids
[i
];
1471 err
= load_commit_or_tag(ncommits
, traversed_ids
,
1472 id
, repo
, progress_cb
, progress_arg
, &rl
,
1473 cancel_cb
, cancel_arg
);
1478 err
= repo_purge_unreferenced_loose_objects(repo
, traversed_ids
,
1479 loose_before
, loose_after
, *ncommits
, nloose
, npacked
, &npurged
,
1480 dry_run
, ignore_mtime
, max_mtime
, &rl
, progress_cb
, progress_arg
,
1481 cancel_cb
, cancel_arg
);
1485 err
= repo_purge_redundant_packfiles(repo
, traversed_ids
,
1486 pack_before
, pack_after
, dry_run
, ignore_mtime
, max_mtime
,
1487 *nloose
, *ncommits
, npurged
, &rl
, progress_cb
, progress_arg
,
1488 cancel_cb
, cancel_arg
);
1494 unlock_err
= got_lockfile_unlock(lk
, got_repo_get_fd(repo
));
1499 got_object_idset_free(traversed_ids
);
1503 const struct got_error
*
1504 got_repo_remove_lonely_packidx(struct got_repository
*repo
, int dry_run
,
1505 got_lonely_packidx_progress_cb progress_cb
, void *progress_arg
,
1506 got_cancel_cb cancel_cb
, void *cancel_arg
)
1508 const struct got_error
*err
= NULL
;
1509 DIR *packdir
= NULL
;
1510 struct dirent
*dent
;
1511 char *pack_relpath
= NULL
;
1515 packdir_fd
= openat(got_repo_get_fd(repo
),
1516 GOT_OBJECTS_PACK_DIR
, O_DIRECTORY
| O_CLOEXEC
);
1517 if (packdir_fd
== -1) {
1518 if (errno
== ENOENT
)
1520 return got_error_from_errno_fmt("openat: %s/%s",
1521 got_repo_get_path_git_dir(repo
),
1522 GOT_OBJECTS_PACK_DIR
);
1525 packdir
= fdopendir(packdir_fd
);
1526 if (packdir
== NULL
) {
1527 err
= got_error_from_errno("fdopendir");
1532 while ((dent
= readdir(packdir
)) != NULL
) {
1534 err
= cancel_cb(cancel_arg
);
1539 if (!got_repo_is_packidx_filename(dent
->d_name
,
1540 strlen(dent
->d_name
),
1541 got_repo_get_object_format(repo
)))
1544 err
= got_packidx_get_packfile_path(&pack_relpath
,
1549 if (fstatat(packdir_fd
, pack_relpath
, &sb
, 0) != -1) {
1551 pack_relpath
= NULL
;
1554 if (errno
!= ENOENT
) {
1555 err
= got_error_from_errno_fmt("fstatat: %s/%s/%s",
1556 got_repo_get_path_git_dir(repo
),
1557 GOT_OBJECTS_PACK_DIR
,
1563 if (unlinkat(packdir_fd
, dent
->d_name
, 0) == -1) {
1564 err
= got_error_from_errno("unlinkat");
1570 if (asprintf(&path
, "%s/%s/%s",
1571 got_repo_get_path_git_dir(repo
),
1572 GOT_OBJECTS_PACK_DIR
,
1573 dent
->d_name
) == -1) {
1574 err
= got_error_from_errno("asprintf");
1577 err
= progress_cb(progress_arg
, path
);
1583 pack_relpath
= NULL
;
1586 if (packdir
&& closedir(packdir
) != 0 && err
== NULL
)
1587 err
= got_error_from_errno("closedir");