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_object.h"
47 #include "got_lib_object_idset.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
53 #include "got_lib_pack_create.h"
54 #include "got_lib_hash.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 *sha1_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");
209 err
= got_pack_create((*pack_hash
)->sha1
, packfd
, delta_cache
,
210 theirs
, ntheirs
, ours
, nours
, repo
, loose_obj_only
,
211 0, force_refdelta
, progress_cb
, progress_arg
, &rl
,
212 cancel_cb
, cancel_arg
);
216 err
= got_object_id_str(&sha1_str
, *pack_hash
);
219 if (asprintf(&packfile_path
, "%s/%s/pack-%s.pack",
220 got_repo_get_path_git_dir(repo
), GOT_OBJECTS_PACK_DIR
,
222 err
= got_error_from_errno("asprintf");
226 if (lseek(packfd
, 0L, SEEK_SET
) == -1) {
227 err
= got_error_from_errno("lseek");
230 if (rename(tmpfile_path
, packfile_path
) == -1) {
231 err
= got_error_from_errno3("rename", tmpfile_path
,
238 *packfile
= fdopen(packfd
, "w");
239 if (*packfile
== NULL
) {
240 err
= got_error_from_errno2("fdopen", tmpfile_path
);
245 for (i
= 0; i
< nours
; i
++)
248 for (i
= 0; i
< ntheirs
; i
++)
251 if (packfd
!= -1 && close(packfd
) == -1 && err
== NULL
)
252 err
= got_error_from_errno2("close", packfile_path
);
253 if (delta_cache
&& fclose(delta_cache
) == EOF
&& err
== NULL
)
254 err
= got_error_from_errno("fclose");
255 if (tmpfile_path
&& unlink(tmpfile_path
) == -1 && err
== NULL
)
256 err
= got_error_from_errno2("unlink", tmpfile_path
);
271 const struct got_error
*
272 got_repo_index_pack(FILE *packfile
, struct got_object_id
*pack_hash
,
273 struct got_repository
*repo
,
274 got_pack_index_progress_cb progress_cb
, void *progress_arg
,
275 got_cancel_cb cancel_cb
, void *cancel_arg
)
280 int npackfd
= -1, idxfd
= -1, nidxfd
= -1;
282 int idxstatus
, done
= 0;
283 const struct got_error
*err
;
284 struct imsgbuf idxibuf
;
286 char *tmpidxpath
= NULL
;
287 char *packfile_path
= NULL
, *idxpath
= NULL
, *id_str
= NULL
;
288 const char *repo_path
= got_repo_get_path_git_dir(repo
);
291 for (i
= 0; i
< nitems(tmpfds
); i
++)
294 if (asprintf(&path
, "%s/%s/indexing.idx",
295 repo_path
, GOT_OBJECTS_PACK_DIR
) == -1) {
296 err
= got_error_from_errno("asprintf");
299 err
= got_opentemp_named_fd(&tmpidxpath
, &idxfd
, path
, "");
303 if (fchmod(idxfd
, GOT_DEFAULT_PACK_MODE
) == -1) {
304 err
= got_error_from_errno2("fchmod", tmpidxpath
);
310 err
= got_error_from_errno("dup");
314 for (i
= 0; i
< nitems(tmpfds
); i
++) {
315 tmpfds
[i
] = got_opentempfd();
316 if (tmpfds
[i
] == -1) {
317 err
= got_error_from_errno("got_opentempfd");
322 err
= got_object_id_str(&id_str
, pack_hash
);
326 if (asprintf(&packfile_path
, "%s/%s/pack-%s.pack",
327 repo_path
, GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
328 err
= got_error_from_errno("asprintf");
332 if (fstat(fileno(packfile
), &sb
) == -1) {
333 err
= got_error_from_errno2("fstat", packfile_path
);
337 if (asprintf(&idxpath
, "%s/%s/pack-%s.idx",
338 repo_path
, GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
339 err
= got_error_from_errno("asprintf");
343 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, imsg_idxfds
) == -1) {
344 err
= got_error_from_errno("socketpair");
349 err
= got_error_from_errno("fork");
351 } else if (idxpid
== 0)
352 got_privsep_exec_child(imsg_idxfds
,
353 GOT_PATH_PROG_INDEX_PACK
, packfile_path
);
354 if (close(imsg_idxfds
[1]) == -1) {
355 err
= got_error_from_errno("close");
358 imsg_init(&idxibuf
, imsg_idxfds
[0]);
360 npackfd
= dup(fileno(packfile
));
362 err
= got_error_from_errno("dup");
365 err
= got_privsep_send_index_pack_req(&idxibuf
, pack_hash
->sha1
,
370 err
= got_privsep_send_index_pack_outfd(&idxibuf
, nidxfd
);
374 for (i
= 0; i
< nitems(tmpfds
); i
++) {
375 err
= got_privsep_send_tmpfd(&idxibuf
, tmpfds
[i
]);
382 int nobj_total
, nobj_indexed
, nobj_loose
, nobj_resolved
;
385 err
= cancel_cb(cancel_arg
);
390 err
= got_privsep_recv_index_progress(&done
, &nobj_total
,
391 &nobj_indexed
, &nobj_loose
, &nobj_resolved
,
395 if (nobj_indexed
!= 0) {
396 err
= progress_cb(progress_arg
, sb
.st_size
,
397 nobj_total
, nobj_indexed
, nobj_loose
,
403 if (close(imsg_idxfds
[0]) == -1) {
404 err
= got_error_from_errno("close");
407 if (waitpid(idxpid
, &idxstatus
, 0) == -1) {
408 err
= got_error_from_errno("waitpid");
412 if (rename(tmpidxpath
, idxpath
) == -1) {
413 err
= got_error_from_errno3("rename", tmpidxpath
, idxpath
);
420 if (tmpidxpath
&& unlink(tmpidxpath
) == -1 && err
== NULL
)
421 err
= got_error_from_errno2("unlink", tmpidxpath
);
422 if (npackfd
!= -1 && close(npackfd
) == -1 && err
== NULL
)
423 err
= got_error_from_errno("close");
424 if (idxfd
!= -1 && close(idxfd
) == -1 && err
== NULL
)
425 err
= got_error_from_errno("close");
426 for (i
= 0; i
< nitems(tmpfds
); i
++) {
427 if (tmpfds
[i
] != -1 && close(tmpfds
[i
]) == -1 && err
== NULL
)
428 err
= got_error_from_errno("close");
436 const struct got_error
*
437 got_repo_find_pack(FILE **packfile
, struct got_object_id
**pack_hash
,
438 struct got_repository
*repo
, const char *packfile_path
)
440 const struct got_error
*err
= NULL
;
441 const char *packdir_path
= NULL
;
442 char *packfile_name
= NULL
, *p
, *dot
;
443 struct got_object_id id
;
449 packdir_path
= got_repo_get_path_objects_pack(repo
);
450 if (packdir_path
== NULL
)
451 return got_error_from_errno("got_repo_get_path_objects_pack");
453 if (!got_path_is_child(packfile_path
, packdir_path
,
454 strlen(packdir_path
))) {
455 err
= got_error_path(packfile_path
, GOT_ERR_BAD_PATH
);
460 err
= got_path_basename(&packfile_name
, packfile_path
);
465 if (strncmp(p
, "pack-", 5) != 0) {
466 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
467 "'%s' is not a valid pack file name",
472 dot
= strchr(p
, '.');
474 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
475 "'%s' is not a valid pack file name",
479 if (strcmp(dot
+ 1, "pack") != 0) {
480 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
481 "'%s' is not a valid pack file name",
486 if (!got_parse_object_id(&id
, p
, repo
->algo
)) {
487 err
= got_error_fmt(GOT_ERR_BAD_PATH
,
488 "'%s' is not a valid pack file name",
493 *pack_hash
= got_object_id_dup(&id
);
494 if (*pack_hash
== NULL
) {
495 err
= got_error_from_errno("got_object_id_dup");
499 packfd
= open(packfile_path
, O_RDONLY
| O_NOFOLLOW
| O_CLOEXEC
);
501 err
= got_error_from_errno2("open", packfile_path
);
505 *packfile
= fdopen(packfd
, "r");
506 if (*packfile
== NULL
) {
507 err
= got_error_from_errno2("fdopen", packfile_path
);
512 if (packfd
!= -1 && close(packfd
) == -1 && err
== NULL
)
513 err
= got_error_from_errno2("close", packfile_path
);
522 const struct got_error
*
523 got_repo_list_pack(FILE *packfile
, struct got_object_id
*pack_hash
,
524 struct got_repository
*repo
, got_pack_list_cb list_cb
, void *list_arg
,
525 got_cancel_cb cancel_cb
, void *cancel_arg
)
527 const struct got_error
*err
= NULL
;
528 char *id_str
= NULL
, *idxpath
= NULL
, *packpath
= NULL
;
529 struct got_packidx
*packidx
= NULL
;
530 struct got_pack
*pack
= NULL
;
533 err
= got_object_id_str(&id_str
, pack_hash
);
537 if (asprintf(&packpath
, "%s/pack-%s.pack",
538 GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
539 err
= got_error_from_errno("asprintf");
542 if (asprintf(&idxpath
, "%s/pack-%s.idx",
543 GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
544 err
= got_error_from_errno("asprintf");
548 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
++) {
558 struct got_packidx_object_id
*oid
;
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
];
571 memcpy(id
.sha1
, oid
->sha1
, SHA1_DIGEST_LENGTH
);
573 offset
= got_packidx_get_object_offset(packidx
, i
);
575 err
= got_error(GOT_ERR_BAD_PACKIDX
);
579 err
= got_pack_parse_object_type_and_size(&type
, &size
, &tslen
,
585 case GOT_OBJ_TYPE_OFFSET_DELTA
:
586 err
= got_pack_parse_offset_delta(&base_offset
, &len
,
587 pack
, offset
, tslen
);
591 case GOT_OBJ_TYPE_REF_DELTA
:
592 err
= got_pack_parse_ref_delta(&base_id
,
593 pack
, offset
, tslen
);
598 err
= (*list_cb
)(list_arg
, &id
, type
, offset
, size
,
599 base_offset
, &base_id
);
609 got_packidx_close(packidx
);
613 static const struct got_error
*
614 repo_cleanup_lock(struct got_repository
*repo
, struct got_lockfile
**lk
)
616 const struct got_error
*err
;
617 char myname
[_POSIX_HOST_NAME_MAX
+ 1];
619 if (gethostname(myname
, sizeof(myname
)) == -1)
620 return got_error_from_errno("gethostname");
622 err
= got_lockfile_lock(lk
, "gc.pid", got_repo_get_fd(repo
));
627 * Git uses these info to provide some verbiage when finds a
628 * lock during `git gc --force' so don't try too hard to avoid
629 * short writes and don't care if a race happens between the
630 * lockfile creation and the write itself.
632 if (dprintf((*lk
)->fd
, "%d %s", getpid(), myname
) < 0)
633 return got_error_from_errno("dprintf");
638 static const struct got_error
*
639 report_cleanup_progress(got_cleanup_progress_cb progress_cb
,
640 void *progress_arg
, struct got_ratelimit
*rl
,
641 int ncommits
, int nloose
, int npurged
, int nredundant
)
643 const struct got_error
*err
;
646 if (progress_cb
== NULL
)
649 err
= got_ratelimit_check(&elapsed
, rl
);
653 return progress_cb(progress_arg
, ncommits
, nloose
, npurged
,
657 static const struct got_error
*
658 get_loose_object_ids(struct got_object_idset
**loose_ids
,
659 off_t
*ondisk_size
, int ncommits
,
660 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
661 struct got_ratelimit
*rl
, struct got_repository
*repo
)
663 const struct got_error
*err
= NULL
;
664 char *path_objects
= NULL
, *path
= NULL
;
666 struct got_object
*obj
= NULL
;
667 struct got_object_id id
;
672 *loose_ids
= got_object_idset_alloc();
673 if (*loose_ids
== NULL
)
674 return got_error_from_errno("got_object_idset_alloc");
676 path_objects
= got_repo_get_path_objects(repo
);
677 if (path_objects
== NULL
) {
678 err
= got_error_from_errno("got_repo_get_path_objects");
682 for (i
= 0; i
<= 0xff; i
++) {
685 if (asprintf(&path
, "%s/%.2x", path_objects
, i
) == -1) {
686 err
= got_error_from_errno("asprintf");
692 if (errno
== ENOENT
) {
696 err
= got_error_from_errno2("opendir", path
);
700 while ((dent
= readdir(dir
)) != NULL
) {
703 if (strcmp(dent
->d_name
, ".") == 0 ||
704 strcmp(dent
->d_name
, "..") == 0)
707 if (asprintf(&id_str
, "%.2x%s", i
, dent
->d_name
) == -1) {
708 err
= got_error_from_errno("asprintf");
712 if (!got_parse_object_id(&id
, id_str
, repo
->algo
)) {
718 err
= got_object_open_loose_fd(&fd
, &id
, repo
);
721 if (fstat(fd
, &sb
) == -1) {
722 err
= got_error_from_errno("fstat");
725 err
= got_object_read_header_privsep(&obj
, &id
, repo
,
729 fd
= -1; /* already closed */
732 case GOT_OBJ_TYPE_COMMIT
:
733 case GOT_OBJ_TYPE_TREE
:
734 case GOT_OBJ_TYPE_BLOB
:
735 case GOT_OBJ_TYPE_TAG
:
738 err
= got_error_fmt(GOT_ERR_OBJ_TYPE
,
742 got_object_close(obj
);
744 (*ondisk_size
) += sb
.st_size
;
745 err
= got_object_idset_add(*loose_ids
, &id
, NULL
);
748 err
= report_cleanup_progress(progress_cb
,
749 progress_arg
, rl
, ncommits
,
750 got_object_idset_num_elements(*loose_ids
),
756 if (closedir(dir
) != 0) {
757 err
= got_error_from_errno("closedir");
766 if (dir
&& closedir(dir
) != 0 && err
== NULL
)
767 err
= got_error_from_errno("closedir");
768 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
769 err
= got_error_from_errno("close");
771 got_object_idset_free(*loose_ids
);
775 got_object_close(obj
);
781 static const struct got_error
*
782 load_tree_entries(struct got_object_id_queue
*ids
,
783 struct got_object_idset
*traversed_ids
, struct got_object_id
*tree_id
,
784 const char *dpath
, struct got_repository
*repo
,
785 got_cancel_cb cancel_cb
, void *cancel_arg
)
787 const struct got_error
*err
;
788 struct got_tree_object
*tree
;
792 err
= got_object_open_as_tree(&tree
, repo
, tree_id
);
796 for (i
= 0; i
< got_object_tree_get_nentries(tree
); i
++) {
797 struct got_tree_entry
*e
= got_object_tree_get_entry(tree
, i
);
798 struct got_object_id
*id
= got_tree_entry_get_id(e
);
799 mode_t mode
= got_tree_entry_get_mode(e
);
802 err
= (*cancel_cb
)(cancel_arg
);
807 if (got_object_tree_entry_is_symlink(e
) ||
808 got_object_tree_entry_is_submodule(e
) ||
809 got_object_idset_contains(traversed_ids
, id
))
812 if (asprintf(&p
, "%s%s%s", dpath
, dpath
[0] != '\0' ? "/" : "",
813 got_tree_entry_get_name(e
)) == -1) {
814 err
= got_error_from_errno("asprintf");
819 struct got_object_qid
*qid
;
820 err
= got_object_qid_alloc(&qid
, id
);
823 STAILQ_INSERT_TAIL(ids
, qid
, entry
);
824 } else if (S_ISREG(mode
)) {
825 /* This blob is referenced. */
826 err
= got_object_idset_add(traversed_ids
, id
, NULL
);
834 got_object_tree_close(tree
);
839 static const struct got_error
*
840 load_tree(struct got_object_idset
*traversed_ids
,
841 struct got_object_id
*tree_id
,
842 const char *dpath
, struct got_repository
*repo
,
843 got_cancel_cb cancel_cb
, void *cancel_arg
)
845 const struct got_error
*err
= NULL
;
846 struct got_object_id_queue tree_ids
;
847 struct got_object_qid
*qid
;
849 err
= got_object_qid_alloc(&qid
, tree_id
);
853 STAILQ_INIT(&tree_ids
);
854 STAILQ_INSERT_TAIL(&tree_ids
, qid
, entry
);
856 while (!STAILQ_EMPTY(&tree_ids
)) {
858 err
= (*cancel_cb
)(cancel_arg
);
863 qid
= STAILQ_FIRST(&tree_ids
);
864 STAILQ_REMOVE_HEAD(&tree_ids
, entry
);
866 if (got_object_idset_contains(traversed_ids
, &qid
->id
)) {
867 got_object_qid_free(qid
);
871 err
= got_object_idset_add(traversed_ids
, &qid
->id
, NULL
);
873 got_object_qid_free(qid
);
877 err
= load_tree_entries(&tree_ids
, traversed_ids
,
878 &qid
->id
, dpath
, repo
, cancel_cb
, cancel_arg
);
879 got_object_qid_free(qid
);
884 got_object_id_queue_free(&tree_ids
);
888 static const struct got_error
*
889 load_commit_or_tag(int *ncommits
, struct got_object_idset
*traversed_ids
,
890 struct got_object_id
*id
, struct got_repository
*repo
,
891 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
892 struct got_ratelimit
*rl
, got_cancel_cb cancel_cb
, void *cancel_arg
)
894 const struct got_error
*err
;
895 struct got_commit_object
*commit
= NULL
;
896 struct got_tag_object
*tag
= NULL
;
897 struct got_object_id
*tree_id
= NULL
;
898 struct got_object_id_queue ids
;
899 struct got_object_qid
*qid
;
902 err
= got_object_qid_alloc(&qid
, id
);
907 STAILQ_INSERT_TAIL(&ids
, qid
, entry
);
909 while (!STAILQ_EMPTY(&ids
)) {
911 err
= (*cancel_cb
)(cancel_arg
);
916 qid
= STAILQ_FIRST(&ids
);
917 STAILQ_REMOVE_HEAD(&ids
, entry
);
919 if (got_object_idset_contains(traversed_ids
, &qid
->id
)) {
920 got_object_qid_free(qid
);
925 err
= got_object_idset_add(traversed_ids
, &qid
->id
, NULL
);
929 err
= got_object_get_type(&obj_type
, repo
, &qid
->id
);
933 case GOT_OBJ_TYPE_COMMIT
:
934 err
= got_object_open_as_commit(&commit
, repo
,
939 case GOT_OBJ_TYPE_TAG
:
940 err
= got_object_open_as_tag(&tag
, repo
, &qid
->id
);
945 /* should not happen */
946 err
= got_error(GOT_ERR_OBJ_TYPE
);
950 /* Find a tree object to scan. */
952 tree_id
= got_object_commit_get_tree_id(commit
);
954 obj_type
= got_object_tag_get_object_type(tag
);
956 case GOT_OBJ_TYPE_COMMIT
:
957 err
= got_object_open_as_commit(&commit
, repo
,
958 got_object_tag_get_object_id(tag
));
961 tree_id
= got_object_commit_get_tree_id(commit
);
963 case GOT_OBJ_TYPE_TREE
:
964 tree_id
= got_object_tag_get_object_id(tag
);
968 * Tag points at something other than a
969 * commit or tree. Leave this weird tag object
970 * and the object it points to.
972 if (got_object_idset_contains(traversed_ids
,
973 got_object_tag_get_object_id(tag
)))
975 err
= got_object_idset_add(traversed_ids
,
976 got_object_tag_get_object_id(tag
), NULL
);
984 err
= load_tree(traversed_ids
, tree_id
, "",
985 repo
, cancel_cb
, cancel_arg
);
991 (*ncommits
)++; /* scanned tags are counted as commits */
993 err
= report_cleanup_progress(progress_cb
, progress_arg
, rl
,
994 *ncommits
, -1, -1, -1);
999 /* Find parent commits to scan. */
1000 const struct got_object_id_queue
*parent_ids
;
1001 parent_ids
= got_object_commit_get_parent_ids(commit
);
1002 err
= got_object_id_queue_copy(parent_ids
, &ids
);
1005 got_object_commit_close(commit
);
1009 got_object_tag_close(tag
);
1012 got_object_qid_free(qid
);
1017 got_object_qid_free(qid
);
1019 got_object_commit_close(commit
);
1021 got_object_tag_close(tag
);
1022 got_object_id_queue_free(&ids
);
1026 static const struct got_error
*
1027 is_object_packed(int *packed
, struct got_repository
*repo
,
1028 struct got_object_id
*id
)
1030 const struct got_error
*err
;
1031 struct got_object
*obj
;
1035 err
= got_object_open_packed(&obj
, id
, repo
);
1037 if (err
->code
== GOT_ERR_NO_OBJ
)
1041 got_object_close(obj
);
1046 struct purge_loose_object_arg
{
1047 struct got_repository
*repo
;
1048 got_cleanup_progress_cb progress_cb
;
1050 struct got_ratelimit
*rl
;
1051 struct got_object_idset
*traversed_ids
;
1062 static const struct got_error
*
1063 purge_loose_object(struct got_object_id
*id
, void *data
, void *arg
)
1065 struct purge_loose_object_arg
*a
= arg
;
1066 const struct got_error
*err
, *unlock_err
= NULL
;
1068 int packed
, fd
= -1;
1070 struct got_lockfile
*lf
= NULL
;
1072 err
= is_object_packed(&packed
, a
->repo
, id
);
1076 if (!packed
&& got_object_idset_contains(a
->traversed_ids
, id
))
1082 err
= got_object_get_path(&path
, id
, a
->repo
);
1086 err
= got_object_open_loose_fd(&fd
, id
, a
->repo
);
1090 if (fstat(fd
, &sb
) == -1) {
1091 err
= got_error_from_errno("fstat");
1096 * Do not delete objects which are younger than our maximum
1097 * modification time threshold. This prevents a race where
1098 * new objects which are being added to the repository
1099 * concurrently would be deleted.
1101 if (a
->ignore_mtime
|| sb
.st_mtime
<= a
->max_mtime
) {
1103 err
= got_lockfile_lock(&lf
, path
, -1);
1106 if (unlink(path
) == -1) {
1107 err
= got_error_from_errno2("unlink", path
);
1113 a
->size_purged
+= sb
.st_size
;
1114 err
= report_cleanup_progress(a
->progress_cb
, a
->progress_arg
,
1115 a
->rl
, a
->ncommits
, a
->nloose
, a
->npurged
, -1);
1120 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
1121 err
= got_error_from_errno("close");
1124 unlock_err
= got_lockfile_unlock(lf
, -1);
1125 return err
? err
: unlock_err
;
1128 static const struct got_error
*
1129 repo_purge_unreferenced_loose_objects(struct got_repository
*repo
,
1130 struct got_object_idset
*traversed_ids
,
1131 off_t
*size_before
, off_t
*size_after
, int ncommits
, int *nloose
,
1132 int *npacked
, int *npurged
, int dry_run
, int ignore_mtime
,
1133 time_t max_mtime
, struct got_ratelimit
*rl
,
1134 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
1135 got_cancel_cb cancel_cb
, void *cancel_arg
)
1137 const struct got_error
*err
;
1138 struct got_object_idset
*loose_ids
;
1139 struct purge_loose_object_arg arg
;
1141 err
= get_loose_object_ids(&loose_ids
, size_before
, ncommits
,
1142 progress_cb
, progress_arg
, rl
, repo
);
1145 *nloose
= got_object_idset_num_elements(loose_ids
);
1147 got_object_idset_free(loose_ids
);
1149 err
= progress_cb(progress_arg
, 0, 0, 0, -1);
1156 memset(&arg
, 0, sizeof(arg
));
1158 arg
.progress_arg
= progress_arg
;
1159 arg
.progress_cb
= progress_cb
;
1161 arg
.traversed_ids
= traversed_ids
;
1162 arg
.nloose
= *nloose
;
1165 arg
.size_purged
= 0;
1166 arg
.dry_run
= dry_run
;
1167 arg
.max_mtime
= max_mtime
;
1168 arg
.ignore_mtime
= ignore_mtime
;
1169 err
= got_object_idset_for_each(loose_ids
, purge_loose_object
, &arg
);
1173 *size_after
= *size_before
- arg
.size_purged
;
1174 *npacked
= arg
.npacked
;
1175 *npurged
= arg
.npurged
;
1177 /* Produce a final progress report. */
1179 err
= progress_cb(progress_arg
, ncommits
, *nloose
,
1185 got_object_idset_free(loose_ids
);
1189 static const struct got_error
*
1190 purge_redundant_pack(struct got_repository
*repo
, const char *packidx_path
,
1191 int dry_run
, int ignore_mtime
, time_t max_mtime
,
1192 int *remove
, off_t
*size_before
, off_t
*size_after
)
1194 static const char *ext
[] = {".idx", ".pack", ".rev", ".bitmap",
1195 ".promisor", ".mtimes"};
1197 char *dot
, path
[PATH_MAX
];
1200 if (strlcpy(path
, packidx_path
, sizeof(path
)) >= sizeof(path
))
1201 return got_error(GOT_ERR_NO_SPACE
);
1204 * Do not delete pack files which are younger than our maximum
1205 * modification time threshold. This prevents a race where a
1206 * new pack file which is being added to the repository
1207 * concurrently would be deleted.
1209 if (fstatat(got_repo_get_fd(repo
), path
, &sb
, 0) == -1) {
1210 if (errno
== ENOENT
)
1212 return got_error_from_errno2("fstatat", path
);
1214 if (!ignore_mtime
&& sb
.st_mtime
> max_mtime
)
1218 * For compatibility with Git, if a matching .keep file exist
1219 * don't delete the packfile.
1221 dot
= strrchr(path
, '.');
1223 if (strlcat(path
, ".keep", sizeof(path
)) >= sizeof(path
))
1224 return got_error(GOT_ERR_NO_SPACE
);
1225 if (faccessat(got_repo_get_fd(repo
), path
, F_OK
, 0) == 0)
1228 for (i
= 0; i
< nitems(ext
); ++i
) {
1231 if (strlcat(path
, ext
[i
], sizeof(path
)) >=
1233 return got_error(GOT_ERR_NO_SPACE
);
1235 if (fstatat(got_repo_get_fd(repo
), path
, &sb
, 0) ==
1237 if (errno
== ENOENT
)
1239 return got_error_from_errno2("fstatat", path
);
1242 *size_before
+= sb
.st_size
;
1244 *size_after
+= sb
.st_size
;
1251 if (unlinkat(got_repo_get_fd(repo
), path
, 0) == -1) {
1252 if (errno
== ENOENT
)
1254 return got_error_from_errno2("unlinkat",
1262 static const struct got_error
*
1263 pack_is_redundant(int *redundant
, struct got_repository
*repo
,
1264 struct got_object_idset
*traversed_ids
,
1265 const char *packidx_path
, struct got_object_idset
*idset
)
1267 const struct got_error
*err
;
1268 struct got_packidx
*packidx
;
1269 struct got_packidx_object_id
*pid
;
1270 struct got_object_id id
;
1275 err
= got_repo_get_packidx(&packidx
, packidx_path
, repo
);
1279 nobjects
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1280 for (i
= 0; i
< nobjects
; ++i
) {
1281 pid
= &packidx
->hdr
.sorted_ids
[i
];
1283 memset(&id
, 0, sizeof(id
));
1284 memcpy(&id
.sha1
, pid
->sha1
, sizeof(id
.sha1
));
1286 if (got_object_idset_contains(idset
, &id
))
1289 if (!got_object_idset_contains(traversed_ids
, &id
))
1293 err
= got_object_idset_add(idset
, &id
, NULL
);
1307 pack_info_cmp(const void *a
, const void *b
)
1309 const struct pack_info
*pa
, *pb
;
1313 if (pa
->nobjects
== pb
->nobjects
)
1314 return strcmp(pa
->path
, pb
->path
);
1315 if (pa
->nobjects
> pb
->nobjects
)
1320 static const struct got_error
*
1321 repo_purge_redundant_packfiles(struct got_repository
*repo
,
1322 struct got_object_idset
*traversed_ids
,
1323 off_t
*size_before
, off_t
*size_after
, int dry_run
, int ignore_mtime
,
1324 time_t max_mtime
, int nloose
, int ncommits
, int npurged
,
1325 struct got_ratelimit
*rl
,
1326 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
1327 got_cancel_cb cancel_cb
, void *cancel_arg
)
1329 const struct got_error
*err
;
1330 struct pack_info
*pinfo
, *sorted
= NULL
;
1331 struct got_packidx
*packidx
;
1332 struct got_object_idset
*idset
= NULL
;
1333 struct got_pathlist_entry
*pe
;
1335 int remove
, redundant_packs
= 0;
1338 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
)
1344 sorted
= calloc(npacks
, sizeof(*sorted
));
1346 return got_error_from_errno("calloc");
1349 TAILQ_FOREACH(pe
, &repo
->packidx_paths
, entry
) {
1350 err
= got_repo_get_packidx(&packidx
, pe
->path
, repo
);
1354 pinfo
= &sorted
[i
++];
1355 pinfo
->path
= pe
->path
;
1356 pinfo
->nobjects
= be32toh(packidx
->hdr
.fanout_table
[0xff]);
1358 qsort(sorted
, npacks
, sizeof(*sorted
), pack_info_cmp
);
1360 idset
= got_object_idset_alloc();
1361 if (idset
== NULL
) {
1362 err
= got_error_from_errno("got_object_idset_alloc");
1366 for (i
= 0; i
< npacks
; ++i
) {
1368 err
= (*cancel_cb
)(cancel_arg
);
1373 err
= pack_is_redundant(&remove
, repo
, traversed_ids
,
1374 sorted
[i
].path
, idset
);
1377 err
= purge_redundant_pack(repo
, sorted
[i
].path
, dry_run
,
1378 ignore_mtime
, max_mtime
, &remove
, size_before
, size_after
);
1383 err
= report_cleanup_progress(progress_cb
, progress_arg
,
1384 rl
, ncommits
, nloose
, npurged
, ++redundant_packs
);
1389 /* Produce a final progress report. */
1391 err
= progress_cb(progress_arg
, ncommits
, nloose
, npurged
,
1399 got_object_idset_free(idset
);
1403 const struct got_error
*
1404 got_repo_cleanup(struct got_repository
*repo
,
1405 off_t
*loose_before
, off_t
*loose_after
,
1406 off_t
*pack_before
, off_t
*pack_after
,
1407 int *ncommits
, int *nloose
, int *npacked
, int dry_run
, int ignore_mtime
,
1408 got_cleanup_progress_cb progress_cb
, void *progress_arg
,
1409 got_cancel_cb cancel_cb
, void *cancel_arg
)
1411 const struct got_error
*unlock_err
, *err
= NULL
;
1412 struct got_lockfile
*lk
= NULL
;
1413 struct got_ratelimit rl
;
1414 struct got_reflist_head refs
;
1415 struct got_object_idset
*traversed_ids
= NULL
;
1416 struct got_reflist_entry
*re
;
1417 struct got_object_id
**referenced_ids
;
1420 time_t max_mtime
= 0;
1423 got_ratelimit_init(&rl
, 0, 500);
1433 err
= repo_cleanup_lock(repo
, &lk
);
1437 traversed_ids
= got_object_idset_alloc();
1438 if (traversed_ids
== NULL
) {
1439 err
= got_error_from_errno("got_object_idset_alloc");
1443 err
= got_ref_list(&refs
, repo
, "", got_ref_cmp_by_name
, NULL
);
1446 if (!ignore_mtime
) {
1447 TAILQ_FOREACH(re
, &refs
, entry
) {
1448 time_t mtime
= got_ref_get_mtime(re
->ref
);
1449 if (mtime
> max_mtime
)
1453 * For safety, keep objects created within 10 minutes
1454 * before the youngest reference was created.
1456 if (max_mtime
>= 600)
1460 err
= get_reflist_object_ids(&referenced_ids
, &nreferenced
,
1461 (1 << GOT_OBJ_TYPE_COMMIT
) | (1 << GOT_OBJ_TYPE_TAG
),
1462 &refs
, repo
, cancel_cb
, cancel_arg
);
1466 for (i
= 0; i
< nreferenced
; i
++) {
1467 struct got_object_id
*id
= referenced_ids
[i
];
1468 err
= load_commit_or_tag(ncommits
, traversed_ids
,
1469 id
, repo
, progress_cb
, progress_arg
, &rl
,
1470 cancel_cb
, cancel_arg
);
1475 err
= repo_purge_unreferenced_loose_objects(repo
, traversed_ids
,
1476 loose_before
, loose_after
, *ncommits
, nloose
, npacked
, &npurged
,
1477 dry_run
, ignore_mtime
, max_mtime
, &rl
, progress_cb
, progress_arg
,
1478 cancel_cb
, cancel_arg
);
1482 err
= repo_purge_redundant_packfiles(repo
, traversed_ids
,
1483 pack_before
, pack_after
, dry_run
, ignore_mtime
, max_mtime
,
1484 *nloose
, *ncommits
, npurged
, &rl
, progress_cb
, progress_arg
,
1485 cancel_cb
, cancel_arg
);
1491 unlock_err
= got_lockfile_unlock(lk
, got_repo_get_fd(repo
));
1496 got_object_idset_free(traversed_ids
);
1500 const struct got_error
*
1501 got_repo_remove_lonely_packidx(struct got_repository
*repo
, int dry_run
,
1502 got_lonely_packidx_progress_cb progress_cb
, void *progress_arg
,
1503 got_cancel_cb cancel_cb
, void *cancel_arg
)
1505 const struct got_error
*err
= NULL
;
1506 DIR *packdir
= NULL
;
1507 struct dirent
*dent
;
1508 char *pack_relpath
= NULL
;
1512 packdir_fd
= openat(got_repo_get_fd(repo
),
1513 GOT_OBJECTS_PACK_DIR
, O_DIRECTORY
| O_CLOEXEC
);
1514 if (packdir_fd
== -1) {
1515 if (errno
== ENOENT
)
1517 return got_error_from_errno_fmt("openat: %s/%s",
1518 got_repo_get_path_git_dir(repo
),
1519 GOT_OBJECTS_PACK_DIR
);
1522 packdir
= fdopendir(packdir_fd
);
1523 if (packdir
== NULL
) {
1524 err
= got_error_from_errno("fdopendir");
1529 while ((dent
= readdir(packdir
)) != NULL
) {
1531 err
= cancel_cb(cancel_arg
);
1536 if (!got_repo_is_packidx_filename(dent
->d_name
,
1537 strlen(dent
->d_name
)))
1540 err
= got_packidx_get_packfile_path(&pack_relpath
,
1545 if (fstatat(packdir_fd
, pack_relpath
, &sb
, 0) != -1) {
1547 pack_relpath
= NULL
;
1550 if (errno
!= ENOENT
) {
1551 err
= got_error_from_errno_fmt("fstatat: %s/%s/%s",
1552 got_repo_get_path_git_dir(repo
),
1553 GOT_OBJECTS_PACK_DIR
,
1559 if (unlinkat(packdir_fd
, dent
->d_name
, 0) == -1) {
1560 err
= got_error_from_errno("unlinkat");
1566 if (asprintf(&path
, "%s/%s/%s",
1567 got_repo_get_path_git_dir(repo
),
1568 GOT_OBJECTS_PACK_DIR
,
1569 dent
->d_name
) == -1) {
1570 err
= got_error_from_errno("asprintf");
1573 err
= progress_cb(progress_arg
, path
);
1579 pack_relpath
= NULL
;
1582 if (packdir
&& closedir(packdir
) != 0 && err
== NULL
)
1583 err
= got_error_from_errno("closedir");