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>
20 #include <sys/socket.h>
33 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_repository.h"
38 #include "got_repository_admin.h"
39 #include "got_opentemp.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_idset.h"
45 #include "got_lib_object_cache.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_repository.h"
49 #include "got_lib_pack_create.h"
50 #include "got_lib_sha1.h"
51 #include "got_lib_lockfile.h"
52 #include "got_lib_ratelimit.h"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 static const struct got_error *
59 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
60 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
61 struct got_repository *repo,
62 got_cancel_cb cancel_cb, void *cancel_arg)
64 const struct got_error *err = NULL;
65 const size_t alloc_chunksz = 256;
67 struct got_reflist_entry *re;
73 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
75 return got_error_from_errno("reallocarray");
76 nalloc = alloc_chunksz;
78 TAILQ_FOREACH(re, refs, entry) {
79 struct got_object_id *id;
82 err = cancel_cb(cancel_arg);
87 err = got_ref_resolve(&id, repo, re->ref);
91 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
93 err = got_object_get_type(&obj_type, repo, id);
96 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
103 if (nalloc <= *nobjects) {
104 struct got_object_id **new;
105 new = recallocarray(*ids, nalloc,
106 nalloc + alloc_chunksz,
107 sizeof(struct got_object_id *));
109 err = got_error_from_errno(
114 nalloc += alloc_chunksz;
116 (*ids)[*nobjects] = id;
117 if ((*ids)[*nobjects] == NULL) {
118 err = got_error_from_errno("got_object_id_dup");
125 for (i = 0; i < *nobjects; i++)
134 const struct got_error *
135 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
136 struct got_reflist_head *include_refs,
137 struct got_reflist_head *exclude_refs, struct got_repository *repo,
139 got_pack_progress_cb progress_cb, void *progress_arg,
140 got_cancel_cb cancel_cb, void *cancel_arg)
142 const struct got_error *err = NULL;
143 struct got_object_id **ours = NULL, **theirs = NULL;
144 int nours = 0, ntheirs = 0, packfd = -1, i;
145 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
146 char *sha1_str = NULL;
151 if (asprintf(&path, "%s/%s/packing.pack",
152 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
153 err = got_error_from_errno("asprintf");
156 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
160 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
161 err = got_error_from_errno2("fchmod", tmpfile_path);
165 *packfile = fdopen(packfd, "w");
166 if (*packfile == NULL) {
167 err = got_error_from_errno2("fdopen", tmpfile_path);
172 err = get_reflist_object_ids(&ours, &nours,
173 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
174 include_refs, repo, cancel_cb, cancel_arg);
179 err = got_error(GOT_ERR_CANNOT_PACK);
183 if (!TAILQ_EMPTY(exclude_refs)) {
184 err = get_reflist_object_ids(&theirs, &ntheirs,
185 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
187 cancel_cb, cancel_arg);
192 *pack_hash = calloc(1, sizeof(**pack_hash));
193 if (*pack_hash == NULL) {
194 err = got_error_from_errno("calloc");
198 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
199 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
200 cancel_cb, cancel_arg);
204 err = got_object_id_str(&sha1_str, *pack_hash);
207 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
208 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
210 err = got_error_from_errno("asprintf");
214 if (fflush(*packfile) == -1) {
215 err = got_error_from_errno("fflush");
218 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
219 err = got_error_from_errno("fseek");
222 if (rename(tmpfile_path, packfile_path) == -1) {
223 err = got_error_from_errno3("rename", tmpfile_path,
230 for (i = 0; i < nours; i++)
233 for (i = 0; i < ntheirs; i++)
236 if (packfd != -1 && close(packfd) == -1 && err == NULL)
237 err = got_error_from_errno2("close", packfile_path);
238 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
239 err = got_error_from_errno2("unlink", tmpfile_path);
254 const struct got_error *
255 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
256 struct got_repository *repo,
257 got_pack_index_progress_cb progress_cb, void *progress_arg,
258 got_cancel_cb cancel_cb, void *cancel_arg)
263 int npackfd = -1, idxfd = -1, nidxfd = -1;
265 int idxstatus, done = 0;
266 const struct got_error *err;
267 struct imsgbuf idxibuf;
269 char *tmpidxpath = NULL;
270 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
271 const char *repo_path = got_repo_get_path_git_dir(repo);
274 for (i = 0; i < nitems(tmpfds); i++)
277 if (asprintf(&path, "%s/%s/indexing.idx",
278 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
279 err = got_error_from_errno("asprintf");
282 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
286 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
287 err = got_error_from_errno2("fchmod", tmpidxpath);
293 err = got_error_from_errno("dup");
297 for (i = 0; i < nitems(tmpfds); i++) {
298 tmpfds[i] = got_opentempfd();
299 if (tmpfds[i] == -1) {
300 err = got_error_from_errno("got_opentempfd");
305 err = got_object_id_str(&id_str, pack_hash);
309 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
310 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
311 err = got_error_from_errno("asprintf");
315 if (fstat(fileno(packfile), &sb) == -1) {
316 err = got_error_from_errno2("fstat", packfile_path);
320 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
321 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
322 err = got_error_from_errno("asprintf");
326 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
327 err = got_error_from_errno("socketpair");
332 err= got_error_from_errno("fork");
334 } else if (idxpid == 0)
335 got_privsep_exec_child(imsg_idxfds,
336 GOT_PATH_PROG_INDEX_PACK, packfile_path);
337 if (close(imsg_idxfds[1]) == -1) {
338 err = got_error_from_errno("close");
341 imsg_init(&idxibuf, imsg_idxfds[0]);
343 npackfd = dup(fileno(packfile));
345 err = got_error_from_errno("dup");
348 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
353 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
357 for (i = 0; i < nitems(tmpfds); i++) {
358 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
365 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
368 err = cancel_cb(cancel_arg);
373 err = got_privsep_recv_index_progress(&done, &nobj_total,
374 &nobj_indexed, &nobj_loose, &nobj_resolved,
378 if (nobj_indexed != 0) {
379 err = progress_cb(progress_arg, sb.st_size,
380 nobj_total, nobj_indexed, nobj_loose,
386 if (close(imsg_idxfds[0]) == -1) {
387 err = got_error_from_errno("close");
390 if (waitpid(idxpid, &idxstatus, 0) == -1) {
391 err = got_error_from_errno("waitpid");
395 if (rename(tmpidxpath, idxpath) == -1) {
396 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
403 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
404 err = got_error_from_errno2("unlink", tmpidxpath);
405 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
406 err = got_error_from_errno("close");
407 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
408 err = got_error_from_errno("close");
409 for (i = 0; i < nitems(tmpfds); i++) {
410 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
411 err = got_error_from_errno("close");
419 const struct got_error *
420 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
421 struct got_repository *repo, const char *packfile_path)
423 const struct got_error *err = NULL;
424 const char *packdir_path = NULL;
425 char *packfile_name = NULL, *p, *dot;
426 struct got_object_id id;
432 packdir_path = got_repo_get_path_objects_pack(repo);
433 if (packdir_path == NULL)
434 return got_error_from_errno("got_repo_get_path_objects_pack");
436 if (!got_path_is_child(packfile_path, packdir_path,
437 strlen(packdir_path))) {
438 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
443 err = got_path_basename(&packfile_name, packfile_path);
448 if (strncmp(p, "pack-", 5) != 0) {
449 err = got_error_fmt(GOT_ERR_BAD_PATH,
450 "'%s' is not a valid pack file name",
455 dot = strchr(p, '.');
457 err = got_error_fmt(GOT_ERR_BAD_PATH,
458 "'%s' is not a valid pack file name",
462 if (strcmp(dot + 1, "pack") != 0) {
463 err = got_error_fmt(GOT_ERR_BAD_PATH,
464 "'%s' is not a valid pack file name",
469 if (!got_parse_sha1_digest(id.sha1, p)) {
470 err = got_error_fmt(GOT_ERR_BAD_PATH,
471 "'%s' is not a valid pack file name",
476 *pack_hash = got_object_id_dup(&id);
477 if (*pack_hash == NULL) {
478 err = got_error_from_errno("got_object_id_dup");
482 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
484 err = got_error_from_errno2("open", packfile_path);
488 *packfile = fdopen(packfd, "r");
489 if (*packfile == NULL) {
490 err = got_error_from_errno2("fdopen", packfile_path);
495 if (packfd != -1 && close(packfd) == -1 && err == NULL)
496 err = got_error_from_errno2("close", packfile_path);
505 const struct got_error *
506 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
507 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
508 got_cancel_cb cancel_cb, void *cancel_arg)
510 const struct got_error *err = NULL;
511 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
512 struct got_packidx *packidx = NULL;
513 struct got_pack *pack = NULL;
516 err = got_object_id_str(&id_str, pack_hash);
520 if (asprintf(&packpath, "%s/pack-%s.pack",
521 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
522 err = got_error_from_errno("asprintf");
525 if (asprintf(&idxpath, "%s/pack-%s.idx",
526 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
527 err = got_error_from_errno("asprintf");
531 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
535 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
539 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
540 for (i = 0; i < nobj; i++) {
541 struct got_packidx_object_id *oid;
542 struct got_object_id id, base_id;
543 off_t offset, base_offset = 0;
549 err = cancel_cb(cancel_arg);
553 oid = &packidx->hdr.sorted_ids[i];
554 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
556 offset = got_packidx_get_object_offset(packidx, i);
558 err = got_error(GOT_ERR_BAD_PACKIDX);
562 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
568 case GOT_OBJ_TYPE_OFFSET_DELTA:
569 err = got_pack_parse_offset_delta(&base_offset, &len,
570 pack, offset, tslen);
574 case GOT_OBJ_TYPE_REF_DELTA:
575 err = got_pack_parse_ref_delta(&base_id,
576 pack, offset, tslen);
581 err = (*list_cb)(list_arg, &id, type, offset, size,
582 base_offset, &base_id);
592 got_packidx_close(packidx);
596 static const struct got_error *
597 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
598 void *progress_arg, struct got_ratelimit *rl,
599 int nloose, int ncommits, int npurged)
601 const struct got_error *err;
604 if (progress_cb == NULL)
607 err = got_ratelimit_check(&elapsed, rl);
611 return progress_cb(progress_arg, nloose, ncommits, npurged);
614 static const struct got_error *
615 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
616 got_cleanup_progress_cb progress_cb, void *progress_arg,
617 struct got_ratelimit *rl, struct got_repository *repo)
619 const struct got_error *err = NULL;
620 char *path_objects = NULL, *path = NULL;
622 struct got_object *obj = NULL;
623 struct got_object_id id;
628 *loose_ids = got_object_idset_alloc();
629 if (*loose_ids == NULL)
630 return got_error_from_errno("got_object_idset_alloc");
632 path_objects = got_repo_get_path_objects(repo);
633 if (path_objects == NULL) {
634 err = got_error_from_errno("got_repo_get_path_objects");
638 for (i = 0; i <= 0xff; i++) {
641 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
642 err = got_error_from_errno("asprintf");
648 if (errno == ENOENT) {
652 err = got_error_from_errno2("opendir", path);
656 while ((dent = readdir(dir)) != NULL) {
659 if (strcmp(dent->d_name, ".") == 0 ||
660 strcmp(dent->d_name, "..") == 0)
663 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
664 err = got_error_from_errno("asprintf");
668 memset(&id, 0, sizeof(id));
669 if (!got_parse_sha1_digest(id.sha1, id_str)) {
675 err = got_object_open_loose_fd(&fd, &id, repo);
678 if (fstat(fd, &sb) == -1) {
679 err = got_error_from_errno("fstat");
682 err = got_object_read_header_privsep(&obj, &id, repo,
686 fd = -1; /* already closed */
689 case GOT_OBJ_TYPE_COMMIT:
690 case GOT_OBJ_TYPE_TREE:
691 case GOT_OBJ_TYPE_BLOB:
692 case GOT_OBJ_TYPE_TAG:
695 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
699 got_object_close(obj);
701 (*ondisk_size) += sb.st_size;
702 err = got_object_idset_add(*loose_ids, &id, NULL);
705 err = report_cleanup_progress(progress_cb,
707 got_object_idset_num_elements(*loose_ids), -1, -1);
712 if (closedir(dir) != 0) {
713 err = got_error_from_errno("closedir");
722 if (dir && closedir(dir) != 0 && err == NULL)
723 err = got_error_from_errno("closedir");
724 if (fd != -1 && close(fd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
727 got_object_idset_free(*loose_ids);
731 got_object_close(obj);
737 static const struct got_error *
738 preserve_loose_object(struct got_object_idset *loose_ids,
739 struct got_object_id *id, struct got_repository *repo, int *npacked)
741 const struct got_error *err = NULL;
742 struct got_object *obj;
744 if (!got_object_idset_contains(loose_ids, id))
748 * Try to open this object from a pack file. This ensures that
749 * we do in fact have a valid packed copy of the object. Otherwise
750 * we should not delete the loose representation of this object.
752 err = got_object_open_packed(&obj, id, repo);
754 got_object_close(obj);
756 * The object is referenced and packed.
757 * We can purge the redundantly stored loose object.
761 } else if (err->code != GOT_ERR_NO_OBJ)
765 * This object is referenced and not packed.
766 * Remove it from our purge set.
768 return got_object_idset_remove(NULL, loose_ids, id);
771 static const struct got_error *
772 load_tree_entries(struct got_object_id_queue *ids,
773 struct got_object_idset *loose_ids,
774 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
775 const char *dpath, struct got_repository *repo, int *npacked,
776 got_cancel_cb cancel_cb, void *cancel_arg)
778 const struct got_error *err;
779 struct got_tree_object *tree;
783 err = got_object_open_as_tree(&tree, repo, tree_id);
787 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
788 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
789 struct got_object_id *id = got_tree_entry_get_id(e);
790 mode_t mode = got_tree_entry_get_mode(e);
793 err = (*cancel_cb)(cancel_arg);
798 if (got_object_tree_entry_is_symlink(e) ||
799 got_object_tree_entry_is_submodule(e) ||
800 got_object_idset_contains(traversed_ids, id))
803 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
804 got_tree_entry_get_name(e)) == -1) {
805 err = got_error_from_errno("asprintf");
810 struct got_object_qid *qid;
811 err = got_object_qid_alloc(&qid, id);
814 STAILQ_INSERT_TAIL(ids, qid, entry);
815 } else if (S_ISREG(mode)) {
816 /* This blob is referenced. */
817 err = preserve_loose_object(loose_ids, id, repo,
821 err = got_object_idset_add(traversed_ids, id, NULL);
830 got_object_tree_close(tree);
835 static const struct got_error *
836 load_tree(struct got_object_idset *loose_ids,
837 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
838 const char *dpath, struct got_repository *repo, int *npacked,
839 got_cancel_cb cancel_cb, void *cancel_arg)
841 const struct got_error *err = NULL;
842 struct got_object_id_queue tree_ids;
843 struct got_object_qid *qid;
845 err = got_object_qid_alloc(&qid, tree_id);
849 STAILQ_INIT(&tree_ids);
850 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
852 while (!STAILQ_EMPTY(&tree_ids)) {
854 err = (*cancel_cb)(cancel_arg);
859 qid = STAILQ_FIRST(&tree_ids);
860 STAILQ_REMOVE_HEAD(&tree_ids, entry);
862 if (got_object_idset_contains(traversed_ids, qid->id)) {
863 got_object_qid_free(qid);
867 err = got_object_idset_add(traversed_ids, qid->id, NULL);
869 got_object_qid_free(qid);
873 /* This tree is referenced. */
874 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
878 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
879 qid->id, dpath, repo, npacked, 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(struct got_object_idset *loose_ids, int *ncommits,
891 int *npacked, struct got_object_idset *traversed_ids,
892 struct got_object_id *id, struct got_repository *repo,
893 got_cleanup_progress_cb progress_cb, void *progress_arg,
894 struct got_ratelimit *rl, int nloose,
895 got_cancel_cb cancel_cb, void *cancel_arg)
897 const struct got_error *err;
898 struct got_commit_object *commit = NULL;
899 struct got_tag_object *tag = NULL;
900 struct got_object_id *tree_id = NULL;
901 struct got_object_id_queue ids;
902 struct got_object_qid *qid;
905 err = got_object_qid_alloc(&qid, id);
910 STAILQ_INSERT_TAIL(&ids, qid, entry);
912 while (!STAILQ_EMPTY(&ids)) {
914 err = (*cancel_cb)(cancel_arg);
919 qid = STAILQ_FIRST(&ids);
920 STAILQ_REMOVE_HEAD(&ids, entry);
922 if (got_object_idset_contains(traversed_ids, qid->id)) {
923 got_object_qid_free(qid);
928 err = got_object_idset_add(traversed_ids, qid->id, NULL);
932 /* This commit or tag is referenced. */
933 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
937 err = got_object_get_type(&obj_type, repo, qid->id);
941 case GOT_OBJ_TYPE_COMMIT:
942 err = got_object_open_as_commit(&commit, repo, qid->id);
946 case GOT_OBJ_TYPE_TAG:
947 err = got_object_open_as_tag(&tag, repo, qid->id);
952 /* should not happen */
953 err = got_error(GOT_ERR_OBJ_TYPE);
957 /* Find a tree object to scan. */
959 tree_id = got_object_commit_get_tree_id(commit);
961 obj_type = got_object_tag_get_object_type(tag);
963 case GOT_OBJ_TYPE_COMMIT:
964 err = got_object_open_as_commit(&commit, repo,
965 got_object_tag_get_object_id(tag));
968 tree_id = got_object_commit_get_tree_id(commit);
970 case GOT_OBJ_TYPE_TREE:
971 tree_id = got_object_tag_get_object_id(tag);
975 * Tag points at something other than a
976 * commit or tree. Leave this weird tag object
977 * and the object it points to on disk.
979 err = got_object_idset_remove(NULL, loose_ids,
981 if (err && err->code != GOT_ERR_NO_OBJ)
983 err = got_object_idset_remove(NULL, loose_ids,
984 got_object_tag_get_object_id(tag));
985 if (err && err->code != GOT_ERR_NO_OBJ)
993 err = load_tree(loose_ids, traversed_ids, tree_id, "",
994 repo, npacked, cancel_cb, cancel_arg);
1000 (*ncommits)++; /* scanned tags are counted as commits */
1002 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1003 nloose, *ncommits, -1);
1008 /* Find parent commits to scan. */
1009 const struct got_object_id_queue *parent_ids;
1010 parent_ids = got_object_commit_get_parent_ids(commit);
1011 err = got_object_id_queue_copy(parent_ids, &ids);
1014 got_object_commit_close(commit);
1018 got_object_tag_close(tag);
1021 got_object_qid_free(qid);
1026 got_object_qid_free(qid);
1028 got_object_commit_close(commit);
1030 got_object_tag_close(tag);
1031 got_object_id_queue_free(&ids);
1035 struct purge_loose_object_arg {
1036 struct got_repository *repo;
1037 got_cleanup_progress_cb progress_cb;
1039 struct got_ratelimit *rl;
1049 static const struct got_error *
1050 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1052 struct purge_loose_object_arg *a = arg;
1053 const struct got_error *err, *unlock_err = NULL;
1057 struct got_lockfile *lf = NULL;
1059 err = got_object_get_path(&path, id, a->repo);
1063 err = got_object_open_loose_fd(&fd, id, a->repo);
1067 if (fstat(fd, &sb) == -1) {
1068 err = got_error_from_errno("fstat");
1073 * Do not delete objects which are younger than our maximum
1074 * modification time threshold. This prevents a race where
1075 * new objects which are being added to the repository
1076 * concurrently would be deleted.
1078 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1080 err = got_lockfile_lock(&lf, path, -1);
1083 if (unlink(path) == -1) {
1084 err = got_error_from_errno2("unlink", path);
1090 a->size_purged += sb.st_size;
1091 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1092 a->rl, a->nloose, a->ncommits, a->npurged);
1097 if (fd != -1 && close(fd) == -1 && err == NULL)
1098 err = got_error_from_errno("close");
1101 unlock_err = got_lockfile_unlock(lf, -1);
1102 return err ? err : unlock_err;
1105 const struct got_error *
1106 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1107 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1108 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1109 got_cancel_cb cancel_cb, void *cancel_arg)
1111 const struct got_error *err;
1112 struct got_object_idset *loose_ids;
1113 struct got_object_idset *traversed_ids;
1114 struct got_object_id **referenced_ids;
1115 int i, nreferenced, nloose, ncommits = 0;
1116 struct got_reflist_head refs;
1117 struct got_reflist_entry *re;
1118 struct purge_loose_object_arg arg;
1119 time_t max_mtime = 0;
1120 struct got_ratelimit rl;
1123 got_ratelimit_init(&rl, 0, 500);
1129 err = get_loose_object_ids(&loose_ids, size_before,
1130 progress_cb, progress_arg, &rl, repo);
1133 nloose = got_object_idset_num_elements(loose_ids);
1135 got_object_idset_free(loose_ids);
1137 err = progress_cb(progress_arg, 0, 0, 0);
1144 traversed_ids = got_object_idset_alloc();
1145 if (traversed_ids == NULL) {
1146 err = got_error_from_errno("got_object_idset_alloc");
1150 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1153 if (!ignore_mtime) {
1154 TAILQ_FOREACH(re, &refs, entry) {
1155 time_t mtime = got_ref_get_mtime(re->ref);
1156 if (mtime > max_mtime)
1160 * For safety, keep objects created within 10 minutes
1161 * before the youngest reference was created.
1163 if (max_mtime >= 600)
1167 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1168 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1169 &refs, repo, cancel_cb, cancel_arg);
1173 for (i = 0; i < nreferenced; i++) {
1174 struct got_object_id *id = referenced_ids[i];
1175 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1176 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1177 nloose, cancel_cb, cancel_arg);
1182 /* Any remaining loose objects are unreferenced and can be purged. */
1184 arg.progress_arg = progress_arg;
1185 arg.progress_cb = progress_cb;
1187 arg.nloose = nloose;
1189 arg.size_purged = 0;
1190 arg.ncommits = ncommits;
1191 arg.dry_run = dry_run;
1192 arg.max_mtime = max_mtime;
1193 arg.ignore_mtime = ignore_mtime;
1194 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1197 *size_after = *size_before - arg.size_purged;
1199 /* Produce a final progress report. */
1201 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1206 got_object_idset_free(loose_ids);
1207 got_object_idset_free(traversed_ids);
1211 static const struct got_error *
1212 remove_packidx(int dir_fd, const char *relpath)
1214 const struct got_error *err, *unlock_err;
1215 struct got_lockfile *lf;
1217 err = got_lockfile_lock(&lf, relpath, dir_fd);
1220 if (unlinkat(dir_fd, relpath, 0) == -1)
1221 err = got_error_from_errno("unlinkat");
1222 unlock_err = got_lockfile_unlock(lf, dir_fd);
1223 return err ? err : unlock_err;
1226 const struct got_error *
1227 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1228 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1229 got_cancel_cb cancel_cb, void *cancel_arg)
1231 const struct got_error *err;
1232 DIR *packdir = NULL;
1233 struct dirent *dent;
1234 char *pack_relpath = NULL;
1238 packdir_fd = openat(got_repo_get_fd(repo),
1239 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1240 if (packdir_fd == -1) {
1241 if (errno == ENOENT)
1243 return got_error_from_errno_fmt("openat: %s/%s",
1244 got_repo_get_path_git_dir(repo),
1245 GOT_OBJECTS_PACK_DIR);
1248 packdir = fdopendir(packdir_fd);
1249 if (packdir == NULL) {
1250 err = got_error_from_errno("fdopendir");
1254 while ((dent = readdir(packdir)) != NULL) {
1256 err = cancel_cb(cancel_arg);
1261 if (!got_repo_is_packidx_filename(dent->d_name,
1262 strlen(dent->d_name)))
1265 err = got_packidx_get_packfile_path(&pack_relpath,
1270 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1272 pack_relpath = NULL;
1275 if (errno != ENOENT) {
1276 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1277 got_repo_get_path_git_dir(repo),
1278 GOT_OBJECTS_PACK_DIR,
1284 err = remove_packidx(packdir_fd, dent->d_name);
1290 if (asprintf(&path, "%s/%s/%s",
1291 got_repo_get_path_git_dir(repo),
1292 GOT_OBJECTS_PACK_DIR,
1293 dent->d_name) == -1) {
1294 err = got_error_from_errno("asprintf");
1297 err = progress_cb(progress_arg, path);
1303 pack_relpath = NULL;
1306 if (packdir && closedir(packdir) != 0 && err == NULL)
1307 err = got_error_from_errno("closedir");