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 <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/socket.h>
34 #include "got_error.h"
35 #include "got_cancel.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository.h"
39 #include "got_repository_admin.h"
40 #include "got_opentemp.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_idset.h"
46 #include "got_lib_object_cache.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_repository.h"
50 #include "got_lib_ratelimit.h"
51 #include "got_lib_pack_create.h"
52 #include "got_lib_hash.h"
53 #include "got_lib_lockfile.h"
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 static const struct got_error *
60 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
61 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
62 struct got_repository *repo,
63 got_cancel_cb cancel_cb, void *cancel_arg)
65 const struct got_error *err = NULL;
66 const size_t alloc_chunksz = 256;
68 struct got_reflist_entry *re;
74 err = got_reflist_sort(refs,
75 got_ref_cmp_by_commit_timestamp_descending, repo);
79 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
81 return got_error_from_errno("reallocarray");
82 nalloc = alloc_chunksz;
84 TAILQ_FOREACH(re, refs, entry) {
85 struct got_object_id *id;
88 err = cancel_cb(cancel_arg);
93 err = got_ref_resolve(&id, repo, re->ref);
97 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
99 err = got_object_get_type(&obj_type, repo, id);
102 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
109 if (nalloc <= *nobjects) {
110 struct got_object_id **new;
111 new = recallocarray(*ids, nalloc,
112 nalloc + alloc_chunksz,
113 sizeof(struct got_object_id *));
115 err = got_error_from_errno(
120 nalloc += alloc_chunksz;
122 (*ids)[*nobjects] = id;
123 if ((*ids)[*nobjects] == NULL) {
124 err = got_error_from_errno("got_object_id_dup");
131 for (i = 0; i < *nobjects; i++)
140 const struct got_error *
141 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
142 struct got_reflist_head *include_refs,
143 struct got_reflist_head *exclude_refs, struct got_repository *repo,
144 int loose_obj_only, int force_refdelta,
145 got_pack_progress_cb progress_cb, void *progress_arg,
146 got_cancel_cb cancel_cb, void *cancel_arg)
148 const struct got_error *err = NULL;
149 struct got_object_id **ours = NULL, **theirs = NULL;
150 int nours = 0, ntheirs = 0, packfd = -1, i;
151 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
152 char *sha1_str = NULL;
153 FILE *delta_cache = NULL;
154 struct got_ratelimit rl;
159 got_ratelimit_init(&rl, 0, 500);
161 if (asprintf(&path, "%s/%s/packing.pack",
162 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
163 err = got_error_from_errno("asprintf");
166 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path, "");
170 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
171 err = got_error_from_errno2("fchmod", tmpfile_path);
175 delta_cache = got_opentemp();
176 if (delta_cache == NULL) {
177 err = got_error_from_errno("got_opentemp");
181 err = get_reflist_object_ids(&ours, &nours,
182 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
183 include_refs, repo, cancel_cb, cancel_arg);
188 err = got_error(GOT_ERR_CANNOT_PACK);
192 if (!TAILQ_EMPTY(exclude_refs)) {
193 err = get_reflist_object_ids(&theirs, &ntheirs,
194 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
196 cancel_cb, cancel_arg);
201 *pack_hash = calloc(1, sizeof(**pack_hash));
202 if (*pack_hash == NULL) {
203 err = got_error_from_errno("calloc");
207 err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
208 theirs, ntheirs, ours, nours, repo, loose_obj_only,
209 0, force_refdelta, progress_cb, progress_arg, &rl,
210 cancel_cb, cancel_arg);
214 err = got_object_id_str(&sha1_str, *pack_hash);
217 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
218 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
220 err = got_error_from_errno("asprintf");
224 if (lseek(packfd, 0L, SEEK_SET) == -1) {
225 err = got_error_from_errno("lseek");
228 if (rename(tmpfile_path, packfile_path) == -1) {
229 err = got_error_from_errno3("rename", tmpfile_path,
236 *packfile = fdopen(packfd, "w");
237 if (*packfile == NULL) {
238 err = got_error_from_errno2("fdopen", tmpfile_path);
243 for (i = 0; i < nours; i++)
246 for (i = 0; i < ntheirs; i++)
249 if (packfd != -1 && close(packfd) == -1 && err == NULL)
250 err = got_error_from_errno2("close", packfile_path);
251 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
252 err = got_error_from_errno("fclose");
253 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
254 err = got_error_from_errno2("unlink", tmpfile_path);
269 const struct got_error *
270 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
271 struct got_repository *repo,
272 got_pack_index_progress_cb progress_cb, void *progress_arg,
273 got_cancel_cb cancel_cb, void *cancel_arg)
278 int npackfd = -1, idxfd = -1, nidxfd = -1;
280 int idxstatus, done = 0;
281 const struct got_error *err;
282 struct imsgbuf idxibuf;
284 char *tmpidxpath = NULL;
285 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
286 const char *repo_path = got_repo_get_path_git_dir(repo);
289 for (i = 0; i < nitems(tmpfds); i++)
292 if (asprintf(&path, "%s/%s/indexing.idx",
293 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
294 err = got_error_from_errno("asprintf");
297 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
301 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
302 err = got_error_from_errno2("fchmod", tmpidxpath);
308 err = got_error_from_errno("dup");
312 for (i = 0; i < nitems(tmpfds); i++) {
313 tmpfds[i] = got_opentempfd();
314 if (tmpfds[i] == -1) {
315 err = got_error_from_errno("got_opentempfd");
320 err = got_object_id_str(&id_str, pack_hash);
324 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
325 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
326 err = got_error_from_errno("asprintf");
330 if (fstat(fileno(packfile), &sb) == -1) {
331 err = got_error_from_errno2("fstat", packfile_path);
335 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
336 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
337 err = got_error_from_errno("asprintf");
341 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
342 err = got_error_from_errno("socketpair");
347 err= got_error_from_errno("fork");
349 } else if (idxpid == 0)
350 got_privsep_exec_child(imsg_idxfds,
351 GOT_PATH_PROG_INDEX_PACK, packfile_path);
352 if (close(imsg_idxfds[1]) == -1) {
353 err = got_error_from_errno("close");
356 imsg_init(&idxibuf, imsg_idxfds[0]);
358 npackfd = dup(fileno(packfile));
360 err = got_error_from_errno("dup");
363 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
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;
531 err = got_object_id_str(&id_str, pack_hash);
535 if (asprintf(&packpath, "%s/pack-%s.pack",
536 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
537 err = got_error_from_errno("asprintf");
540 if (asprintf(&idxpath, "%s/pack-%s.idx",
541 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
542 err = got_error_from_errno("asprintf");
546 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
550 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
554 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
555 for (i = 0; i < nobj; i++) {
556 struct got_packidx_object_id *oid;
557 struct got_object_id id, base_id;
558 off_t offset, base_offset = 0;
564 err = cancel_cb(cancel_arg);
568 oid = &packidx->hdr.sorted_ids[i];
569 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
571 offset = got_packidx_get_object_offset(packidx, i);
573 err = got_error(GOT_ERR_BAD_PACKIDX);
577 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
583 case GOT_OBJ_TYPE_OFFSET_DELTA:
584 err = got_pack_parse_offset_delta(&base_offset, &len,
585 pack, offset, tslen);
589 case GOT_OBJ_TYPE_REF_DELTA:
590 err = got_pack_parse_ref_delta(&base_id,
591 pack, offset, tslen);
596 err = (*list_cb)(list_arg, &id, type, offset, size,
597 base_offset, &base_id);
607 got_packidx_close(packidx);
611 static const struct got_error *
612 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
613 void *progress_arg, struct got_ratelimit *rl,
614 int nloose, int ncommits, int npurged)
616 const struct got_error *err;
619 if (progress_cb == NULL)
622 err = got_ratelimit_check(&elapsed, rl);
626 return progress_cb(progress_arg, nloose, ncommits, npurged);
629 static const struct got_error *
630 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
631 got_cleanup_progress_cb progress_cb, void *progress_arg,
632 struct got_ratelimit *rl, struct got_repository *repo)
634 const struct got_error *err = NULL;
635 char *path_objects = NULL, *path = NULL;
637 struct got_object *obj = NULL;
638 struct got_object_id id;
643 *loose_ids = got_object_idset_alloc();
644 if (*loose_ids == NULL)
645 return got_error_from_errno("got_object_idset_alloc");
647 path_objects = got_repo_get_path_objects(repo);
648 if (path_objects == NULL) {
649 err = got_error_from_errno("got_repo_get_path_objects");
653 for (i = 0; i <= 0xff; i++) {
656 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
657 err = got_error_from_errno("asprintf");
663 if (errno == ENOENT) {
667 err = got_error_from_errno2("opendir", path);
671 while ((dent = readdir(dir)) != NULL) {
674 if (strcmp(dent->d_name, ".") == 0 ||
675 strcmp(dent->d_name, "..") == 0)
678 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
679 err = got_error_from_errno("asprintf");
683 if (!got_parse_object_id(&id, id_str, repo->algo)) {
689 err = got_object_open_loose_fd(&fd, &id, repo);
692 if (fstat(fd, &sb) == -1) {
693 err = got_error_from_errno("fstat");
696 err = got_object_read_header_privsep(&obj, &id, repo,
700 fd = -1; /* already closed */
703 case GOT_OBJ_TYPE_COMMIT:
704 case GOT_OBJ_TYPE_TREE:
705 case GOT_OBJ_TYPE_BLOB:
706 case GOT_OBJ_TYPE_TAG:
709 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
713 got_object_close(obj);
715 (*ondisk_size) += sb.st_size;
716 err = got_object_idset_add(*loose_ids, &id, NULL);
719 err = report_cleanup_progress(progress_cb,
721 got_object_idset_num_elements(*loose_ids), -1, -1);
726 if (closedir(dir) != 0) {
727 err = got_error_from_errno("closedir");
736 if (dir && closedir(dir) != 0 && err == NULL)
737 err = got_error_from_errno("closedir");
738 if (fd != -1 && close(fd) == -1 && err == NULL)
739 err = got_error_from_errno("close");
741 got_object_idset_free(*loose_ids);
745 got_object_close(obj);
751 static const struct got_error *
752 preserve_loose_object(struct got_object_idset *loose_ids,
753 struct got_object_id *id, struct got_repository *repo, int *npacked)
755 const struct got_error *err = NULL;
756 struct got_object *obj;
758 if (!got_object_idset_contains(loose_ids, id))
762 * Try to open this object from a pack file. This ensures that
763 * we do in fact have a valid packed copy of the object. Otherwise
764 * we should not delete the loose representation of this object.
766 err = got_object_open_packed(&obj, id, repo);
768 got_object_close(obj);
770 * The object is referenced and packed.
771 * We can purge the redundantly stored loose object.
775 } else if (err->code != GOT_ERR_NO_OBJ)
779 * This object is referenced and not packed.
780 * Remove it from our purge set.
782 return got_object_idset_remove(NULL, loose_ids, id);
785 static const struct got_error *
786 load_tree_entries(struct got_object_id_queue *ids,
787 struct got_object_idset *loose_ids,
788 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
789 const char *dpath, struct got_repository *repo, int *npacked,
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 = preserve_loose_object(loose_ids, id, repo,
835 err = got_object_idset_add(traversed_ids, id, NULL);
843 got_object_tree_close(tree);
848 static const struct got_error *
849 load_tree(struct got_object_idset *loose_ids,
850 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
851 const char *dpath, struct got_repository *repo, int *npacked,
852 got_cancel_cb cancel_cb, void *cancel_arg)
854 const struct got_error *err = NULL;
855 struct got_object_id_queue tree_ids;
856 struct got_object_qid *qid;
858 err = got_object_qid_alloc(&qid, tree_id);
862 STAILQ_INIT(&tree_ids);
863 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
865 while (!STAILQ_EMPTY(&tree_ids)) {
867 err = (*cancel_cb)(cancel_arg);
872 qid = STAILQ_FIRST(&tree_ids);
873 STAILQ_REMOVE_HEAD(&tree_ids, entry);
875 if (got_object_idset_contains(traversed_ids, &qid->id)) {
876 got_object_qid_free(qid);
880 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
882 got_object_qid_free(qid);
886 /* This tree is referenced. */
887 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
891 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
892 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
893 got_object_qid_free(qid);
898 got_object_id_queue_free(&tree_ids);
902 static const struct got_error *
903 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
904 int *npacked, struct got_object_idset *traversed_ids,
905 struct got_object_id *id, struct got_repository *repo,
906 got_cleanup_progress_cb progress_cb, void *progress_arg,
907 struct got_ratelimit *rl, int nloose,
908 got_cancel_cb cancel_cb, void *cancel_arg)
910 const struct got_error *err;
911 struct got_commit_object *commit = NULL;
912 struct got_tag_object *tag = NULL;
913 struct got_object_id *tree_id = NULL;
914 struct got_object_id_queue ids;
915 struct got_object_qid *qid;
918 err = got_object_qid_alloc(&qid, id);
923 STAILQ_INSERT_TAIL(&ids, qid, entry);
925 while (!STAILQ_EMPTY(&ids)) {
927 err = (*cancel_cb)(cancel_arg);
932 qid = STAILQ_FIRST(&ids);
933 STAILQ_REMOVE_HEAD(&ids, entry);
935 if (got_object_idset_contains(traversed_ids, &qid->id)) {
936 got_object_qid_free(qid);
941 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
945 /* This commit or tag is referenced. */
946 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
950 err = got_object_get_type(&obj_type, repo, &qid->id);
954 case GOT_OBJ_TYPE_COMMIT:
955 err = got_object_open_as_commit(&commit, repo,
960 case GOT_OBJ_TYPE_TAG:
961 err = got_object_open_as_tag(&tag, repo, &qid->id);
966 /* should not happen */
967 err = got_error(GOT_ERR_OBJ_TYPE);
971 /* Find a tree object to scan. */
973 tree_id = got_object_commit_get_tree_id(commit);
975 obj_type = got_object_tag_get_object_type(tag);
977 case GOT_OBJ_TYPE_COMMIT:
978 err = got_object_open_as_commit(&commit, repo,
979 got_object_tag_get_object_id(tag));
982 tree_id = got_object_commit_get_tree_id(commit);
984 case GOT_OBJ_TYPE_TREE:
985 tree_id = got_object_tag_get_object_id(tag);
989 * Tag points at something other than a
990 * commit or tree. Leave this weird tag object
991 * and the object it points to on disk.
993 err = got_object_idset_remove(NULL, loose_ids,
995 if (err && err->code != GOT_ERR_NO_OBJ)
997 err = got_object_idset_remove(NULL, loose_ids,
998 got_object_tag_get_object_id(tag));
999 if (err && err->code != GOT_ERR_NO_OBJ)
1007 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1008 repo, npacked, cancel_cb, cancel_arg);
1014 (*ncommits)++; /* scanned tags are counted as commits */
1016 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1017 nloose, *ncommits, -1);
1022 /* Find parent commits to scan. */
1023 const struct got_object_id_queue *parent_ids;
1024 parent_ids = got_object_commit_get_parent_ids(commit);
1025 err = got_object_id_queue_copy(parent_ids, &ids);
1028 got_object_commit_close(commit);
1032 got_object_tag_close(tag);
1035 got_object_qid_free(qid);
1040 got_object_qid_free(qid);
1042 got_object_commit_close(commit);
1044 got_object_tag_close(tag);
1045 got_object_id_queue_free(&ids);
1049 struct purge_loose_object_arg {
1050 struct got_repository *repo;
1051 got_cleanup_progress_cb progress_cb;
1053 struct got_ratelimit *rl;
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;
1071 struct got_lockfile *lf = NULL;
1073 err = got_object_get_path(&path, id, a->repo);
1077 err = got_object_open_loose_fd(&fd, id, a->repo);
1081 if (fstat(fd, &sb) == -1) {
1082 err = got_error_from_errno("fstat");
1087 * Do not delete objects which are younger than our maximum
1088 * modification time threshold. This prevents a race where
1089 * new objects which are being added to the repository
1090 * concurrently would be deleted.
1092 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1094 err = got_lockfile_lock(&lf, path, -1);
1097 if (unlink(path) == -1) {
1098 err = got_error_from_errno2("unlink", path);
1104 a->size_purged += sb.st_size;
1105 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1106 a->rl, a->nloose, a->ncommits, a->npurged);
1111 if (fd != -1 && close(fd) == -1 && err == NULL)
1112 err = got_error_from_errno("close");
1115 unlock_err = got_lockfile_unlock(lf, -1);
1116 return err ? err : unlock_err;
1119 const struct got_error *
1120 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1121 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1122 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1123 got_cancel_cb cancel_cb, void *cancel_arg)
1125 const struct got_error *err;
1126 struct got_object_idset *loose_ids;
1127 struct got_object_idset *traversed_ids;
1128 struct got_object_id **referenced_ids;
1129 int i, nreferenced, nloose, ncommits = 0;
1130 struct got_reflist_head refs;
1131 struct got_reflist_entry *re;
1132 struct purge_loose_object_arg arg;
1133 time_t max_mtime = 0;
1134 struct got_ratelimit rl;
1137 got_ratelimit_init(&rl, 0, 500);
1143 err = get_loose_object_ids(&loose_ids, size_before,
1144 progress_cb, progress_arg, &rl, repo);
1147 nloose = got_object_idset_num_elements(loose_ids);
1149 got_object_idset_free(loose_ids);
1151 err = progress_cb(progress_arg, 0, 0, 0);
1158 traversed_ids = got_object_idset_alloc();
1159 if (traversed_ids == NULL) {
1160 err = got_error_from_errno("got_object_idset_alloc");
1164 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1167 if (!ignore_mtime) {
1168 TAILQ_FOREACH(re, &refs, entry) {
1169 time_t mtime = got_ref_get_mtime(re->ref);
1170 if (mtime > max_mtime)
1174 * For safety, keep objects created within 10 minutes
1175 * before the youngest reference was created.
1177 if (max_mtime >= 600)
1181 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1182 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1183 &refs, repo, cancel_cb, cancel_arg);
1187 for (i = 0; i < nreferenced; i++) {
1188 struct got_object_id *id = referenced_ids[i];
1189 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1190 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1191 nloose, cancel_cb, cancel_arg);
1196 /* Any remaining loose objects are unreferenced and can be purged. */
1198 arg.progress_arg = progress_arg;
1199 arg.progress_cb = progress_cb;
1201 arg.nloose = nloose;
1203 arg.size_purged = 0;
1204 arg.ncommits = ncommits;
1205 arg.dry_run = dry_run;
1206 arg.max_mtime = max_mtime;
1207 arg.ignore_mtime = ignore_mtime;
1208 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1211 *size_after = *size_before - arg.size_purged;
1213 /* Produce a final progress report. */
1215 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1220 got_object_idset_free(loose_ids);
1221 got_object_idset_free(traversed_ids);
1225 static const struct got_error *
1226 remove_packidx(int dir_fd, const char *relpath)
1228 const struct got_error *err, *unlock_err;
1229 struct got_lockfile *lf;
1231 err = got_lockfile_lock(&lf, relpath, dir_fd);
1234 if (unlinkat(dir_fd, relpath, 0) == -1)
1235 err = got_error_from_errno("unlinkat");
1236 unlock_err = got_lockfile_unlock(lf, dir_fd);
1237 return err ? err : unlock_err;
1240 const struct got_error *
1241 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1242 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1243 got_cancel_cb cancel_cb, void *cancel_arg)
1245 const struct got_error *err = NULL;
1246 DIR *packdir = NULL;
1247 struct dirent *dent;
1248 char *pack_relpath = NULL;
1252 packdir_fd = openat(got_repo_get_fd(repo),
1253 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1254 if (packdir_fd == -1) {
1255 if (errno == ENOENT)
1257 return got_error_from_errno_fmt("openat: %s/%s",
1258 got_repo_get_path_git_dir(repo),
1259 GOT_OBJECTS_PACK_DIR);
1262 packdir = fdopendir(packdir_fd);
1263 if (packdir == NULL) {
1264 err = got_error_from_errno("fdopendir");
1268 while ((dent = readdir(packdir)) != NULL) {
1270 err = cancel_cb(cancel_arg);
1275 if (!got_repo_is_packidx_filename(dent->d_name,
1276 strlen(dent->d_name)))
1279 err = got_packidx_get_packfile_path(&pack_relpath,
1284 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1286 pack_relpath = NULL;
1289 if (errno != ENOENT) {
1290 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1291 got_repo_get_path_git_dir(repo),
1292 GOT_OBJECTS_PACK_DIR,
1298 err = remove_packidx(packdir_fd, dent->d_name);
1304 if (asprintf(&path, "%s/%s/%s",
1305 got_repo_get_path_git_dir(repo),
1306 GOT_OBJECTS_PACK_DIR,
1307 dent->d_name) == -1) {
1308 err = got_error_from_errno("asprintf");
1311 err = progress_cb(progress_arg, path);
1317 pack_relpath = NULL;
1320 if (packdir && closedir(packdir) != 0 && err == NULL)
1321 err = got_error_from_errno("closedir");