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 err = got_reflist_sort(refs,
74 got_ref_cmp_by_commit_timestamp_descending, repo);
78 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
80 return got_error_from_errno("reallocarray");
81 nalloc = alloc_chunksz;
83 TAILQ_FOREACH(re, refs, entry) {
84 struct got_object_id *id;
87 err = cancel_cb(cancel_arg);
92 err = got_ref_resolve(&id, repo, re->ref);
96 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
98 err = got_object_get_type(&obj_type, repo, id);
101 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
108 if (nalloc <= *nobjects) {
109 struct got_object_id **new;
110 new = recallocarray(*ids, nalloc,
111 nalloc + alloc_chunksz,
112 sizeof(struct got_object_id *));
114 err = got_error_from_errno(
119 nalloc += alloc_chunksz;
121 (*ids)[*nobjects] = id;
122 if ((*ids)[*nobjects] == NULL) {
123 err = got_error_from_errno("got_object_id_dup");
130 for (i = 0; i < *nobjects; i++)
139 const struct got_error *
140 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
141 struct got_reflist_head *include_refs,
142 struct got_reflist_head *exclude_refs, struct got_repository *repo,
144 got_pack_progress_cb progress_cb, void *progress_arg,
145 got_cancel_cb cancel_cb, void *cancel_arg)
147 const struct got_error *err = NULL;
148 struct got_object_id **ours = NULL, **theirs = NULL;
149 int nours = 0, ntheirs = 0, packfd = -1, i;
150 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
151 char *sha1_str = NULL;
156 if (asprintf(&path, "%s/%s/packing.pack",
157 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
158 err = got_error_from_errno("asprintf");
161 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
165 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
166 err = got_error_from_errno2("fchmod", tmpfile_path);
170 *packfile = fdopen(packfd, "w");
171 if (*packfile == NULL) {
172 err = got_error_from_errno2("fdopen", tmpfile_path);
177 err = get_reflist_object_ids(&ours, &nours,
178 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
179 include_refs, repo, cancel_cb, cancel_arg);
184 err = got_error(GOT_ERR_CANNOT_PACK);
188 if (!TAILQ_EMPTY(exclude_refs)) {
189 err = get_reflist_object_ids(&theirs, &ntheirs,
190 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
192 cancel_cb, cancel_arg);
197 *pack_hash = calloc(1, sizeof(**pack_hash));
198 if (*pack_hash == NULL) {
199 err = got_error_from_errno("calloc");
203 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
204 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
205 cancel_cb, cancel_arg);
209 err = got_object_id_str(&sha1_str, *pack_hash);
212 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
213 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
215 err = got_error_from_errno("asprintf");
219 if (fflush(*packfile) == -1) {
220 err = got_error_from_errno("fflush");
223 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
224 err = got_error_from_errno("fseek");
227 if (rename(tmpfile_path, packfile_path) == -1) {
228 err = got_error_from_errno3("rename", tmpfile_path,
235 for (i = 0; i < nours; i++)
238 for (i = 0; i < ntheirs; i++)
241 if (packfd != -1 && close(packfd) == -1 && err == NULL)
242 err = got_error_from_errno2("close", packfile_path);
243 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
244 err = got_error_from_errno2("unlink", tmpfile_path);
259 const struct got_error *
260 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
261 struct got_repository *repo,
262 got_pack_index_progress_cb progress_cb, void *progress_arg,
263 got_cancel_cb cancel_cb, void *cancel_arg)
268 int npackfd = -1, idxfd = -1, nidxfd = -1;
270 int idxstatus, done = 0;
271 const struct got_error *err;
272 struct imsgbuf idxibuf;
274 char *tmpidxpath = NULL;
275 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
276 const char *repo_path = got_repo_get_path_git_dir(repo);
279 for (i = 0; i < nitems(tmpfds); i++)
282 if (asprintf(&path, "%s/%s/indexing.idx",
283 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
284 err = got_error_from_errno("asprintf");
287 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
291 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
292 err = got_error_from_errno2("fchmod", tmpidxpath);
298 err = got_error_from_errno("dup");
302 for (i = 0; i < nitems(tmpfds); i++) {
303 tmpfds[i] = got_opentempfd();
304 if (tmpfds[i] == -1) {
305 err = got_error_from_errno("got_opentempfd");
310 err = got_object_id_str(&id_str, pack_hash);
314 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
315 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
316 err = got_error_from_errno("asprintf");
320 if (fstat(fileno(packfile), &sb) == -1) {
321 err = got_error_from_errno2("fstat", packfile_path);
325 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
326 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
327 err = got_error_from_errno("asprintf");
331 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
332 err = got_error_from_errno("socketpair");
337 err= got_error_from_errno("fork");
339 } else if (idxpid == 0)
340 got_privsep_exec_child(imsg_idxfds,
341 GOT_PATH_PROG_INDEX_PACK, packfile_path);
342 if (close(imsg_idxfds[1]) == -1) {
343 err = got_error_from_errno("close");
346 imsg_init(&idxibuf, imsg_idxfds[0]);
348 npackfd = dup(fileno(packfile));
350 err = got_error_from_errno("dup");
353 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
358 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
362 for (i = 0; i < nitems(tmpfds); i++) {
363 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
370 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
373 err = cancel_cb(cancel_arg);
378 err = got_privsep_recv_index_progress(&done, &nobj_total,
379 &nobj_indexed, &nobj_loose, &nobj_resolved,
383 if (nobj_indexed != 0) {
384 err = progress_cb(progress_arg, sb.st_size,
385 nobj_total, nobj_indexed, nobj_loose,
391 if (close(imsg_idxfds[0]) == -1) {
392 err = got_error_from_errno("close");
395 if (waitpid(idxpid, &idxstatus, 0) == -1) {
396 err = got_error_from_errno("waitpid");
400 if (rename(tmpidxpath, idxpath) == -1) {
401 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
408 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
409 err = got_error_from_errno2("unlink", tmpidxpath);
410 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
411 err = got_error_from_errno("close");
412 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
413 err = got_error_from_errno("close");
414 for (i = 0; i < nitems(tmpfds); i++) {
415 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
416 err = got_error_from_errno("close");
424 const struct got_error *
425 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
426 struct got_repository *repo, const char *packfile_path)
428 const struct got_error *err = NULL;
429 const char *packdir_path = NULL;
430 char *packfile_name = NULL, *p, *dot;
431 struct got_object_id id;
437 packdir_path = got_repo_get_path_objects_pack(repo);
438 if (packdir_path == NULL)
439 return got_error_from_errno("got_repo_get_path_objects_pack");
441 if (!got_path_is_child(packfile_path, packdir_path,
442 strlen(packdir_path))) {
443 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
448 err = got_path_basename(&packfile_name, packfile_path);
453 if (strncmp(p, "pack-", 5) != 0) {
454 err = got_error_fmt(GOT_ERR_BAD_PATH,
455 "'%s' is not a valid pack file name",
460 dot = strchr(p, '.');
462 err = got_error_fmt(GOT_ERR_BAD_PATH,
463 "'%s' is not a valid pack file name",
467 if (strcmp(dot + 1, "pack") != 0) {
468 err = got_error_fmt(GOT_ERR_BAD_PATH,
469 "'%s' is not a valid pack file name",
474 if (!got_parse_sha1_digest(id.sha1, p)) {
475 err = got_error_fmt(GOT_ERR_BAD_PATH,
476 "'%s' is not a valid pack file name",
481 *pack_hash = got_object_id_dup(&id);
482 if (*pack_hash == NULL) {
483 err = got_error_from_errno("got_object_id_dup");
487 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
489 err = got_error_from_errno2("open", packfile_path);
493 *packfile = fdopen(packfd, "r");
494 if (*packfile == NULL) {
495 err = got_error_from_errno2("fdopen", packfile_path);
500 if (packfd != -1 && close(packfd) == -1 && err == NULL)
501 err = got_error_from_errno2("close", packfile_path);
510 const struct got_error *
511 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
512 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
513 got_cancel_cb cancel_cb, void *cancel_arg)
515 const struct got_error *err = NULL;
516 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
517 struct got_packidx *packidx = NULL;
518 struct got_pack *pack = NULL;
521 err = got_object_id_str(&id_str, pack_hash);
525 if (asprintf(&packpath, "%s/pack-%s.pack",
526 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
527 err = got_error_from_errno("asprintf");
530 if (asprintf(&idxpath, "%s/pack-%s.idx",
531 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
532 err = got_error_from_errno("asprintf");
536 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
540 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
544 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
545 for (i = 0; i < nobj; i++) {
546 struct got_packidx_object_id *oid;
547 struct got_object_id id, base_id;
548 off_t offset, base_offset = 0;
554 err = cancel_cb(cancel_arg);
558 oid = &packidx->hdr.sorted_ids[i];
559 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
561 offset = got_packidx_get_object_offset(packidx, i);
563 err = got_error(GOT_ERR_BAD_PACKIDX);
567 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
573 case GOT_OBJ_TYPE_OFFSET_DELTA:
574 err = got_pack_parse_offset_delta(&base_offset, &len,
575 pack, offset, tslen);
579 case GOT_OBJ_TYPE_REF_DELTA:
580 err = got_pack_parse_ref_delta(&base_id,
581 pack, offset, tslen);
586 err = (*list_cb)(list_arg, &id, type, offset, size,
587 base_offset, &base_id);
597 got_packidx_close(packidx);
601 static const struct got_error *
602 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
603 void *progress_arg, struct got_ratelimit *rl,
604 int nloose, int ncommits, int npurged)
606 const struct got_error *err;
609 if (progress_cb == NULL)
612 err = got_ratelimit_check(&elapsed, rl);
616 return progress_cb(progress_arg, nloose, ncommits, npurged);
619 static const struct got_error *
620 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
621 got_cleanup_progress_cb progress_cb, void *progress_arg,
622 struct got_ratelimit *rl, struct got_repository *repo)
624 const struct got_error *err = NULL;
625 char *path_objects = NULL, *path = NULL;
627 struct got_object *obj = NULL;
628 struct got_object_id id;
633 *loose_ids = got_object_idset_alloc();
634 if (*loose_ids == NULL)
635 return got_error_from_errno("got_object_idset_alloc");
637 path_objects = got_repo_get_path_objects(repo);
638 if (path_objects == NULL) {
639 err = got_error_from_errno("got_repo_get_path_objects");
643 for (i = 0; i <= 0xff; i++) {
646 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
647 err = got_error_from_errno("asprintf");
653 if (errno == ENOENT) {
657 err = got_error_from_errno2("opendir", path);
661 while ((dent = readdir(dir)) != NULL) {
664 if (strcmp(dent->d_name, ".") == 0 ||
665 strcmp(dent->d_name, "..") == 0)
668 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
669 err = got_error_from_errno("asprintf");
673 memset(&id, 0, sizeof(id));
674 if (!got_parse_sha1_digest(id.sha1, id_str)) {
680 err = got_object_open_loose_fd(&fd, &id, repo);
683 if (fstat(fd, &sb) == -1) {
684 err = got_error_from_errno("fstat");
687 err = got_object_read_header_privsep(&obj, &id, repo,
691 fd = -1; /* already closed */
694 case GOT_OBJ_TYPE_COMMIT:
695 case GOT_OBJ_TYPE_TREE:
696 case GOT_OBJ_TYPE_BLOB:
697 case GOT_OBJ_TYPE_TAG:
700 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
704 got_object_close(obj);
706 (*ondisk_size) += sb.st_size;
707 err = got_object_idset_add(*loose_ids, &id, NULL);
710 err = report_cleanup_progress(progress_cb,
712 got_object_idset_num_elements(*loose_ids), -1, -1);
717 if (closedir(dir) != 0) {
718 err = got_error_from_errno("closedir");
727 if (dir && closedir(dir) != 0 && err == NULL)
728 err = got_error_from_errno("closedir");
729 if (fd != -1 && close(fd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
732 got_object_idset_free(*loose_ids);
736 got_object_close(obj);
742 static const struct got_error *
743 preserve_loose_object(struct got_object_idset *loose_ids,
744 struct got_object_id *id, struct got_repository *repo, int *npacked)
746 const struct got_error *err = NULL;
747 struct got_object *obj;
749 if (!got_object_idset_contains(loose_ids, id))
753 * Try to open this object from a pack file. This ensures that
754 * we do in fact have a valid packed copy of the object. Otherwise
755 * we should not delete the loose representation of this object.
757 err = got_object_open_packed(&obj, id, repo);
759 got_object_close(obj);
761 * The object is referenced and packed.
762 * We can purge the redundantly stored loose object.
766 } else if (err->code != GOT_ERR_NO_OBJ)
770 * This object is referenced and not packed.
771 * Remove it from our purge set.
773 return got_object_idset_remove(NULL, loose_ids, id);
776 static const struct got_error *
777 load_tree_entries(struct got_object_id_queue *ids,
778 struct got_object_idset *loose_ids,
779 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
780 const char *dpath, struct got_repository *repo, int *npacked,
781 got_cancel_cb cancel_cb, void *cancel_arg)
783 const struct got_error *err;
784 struct got_tree_object *tree;
788 err = got_object_open_as_tree(&tree, repo, tree_id);
792 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
793 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
794 struct got_object_id *id = got_tree_entry_get_id(e);
795 mode_t mode = got_tree_entry_get_mode(e);
798 err = (*cancel_cb)(cancel_arg);
803 if (got_object_tree_entry_is_symlink(e) ||
804 got_object_tree_entry_is_submodule(e) ||
805 got_object_idset_contains(traversed_ids, id))
808 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
809 got_tree_entry_get_name(e)) == -1) {
810 err = got_error_from_errno("asprintf");
815 struct got_object_qid *qid;
816 err = got_object_qid_alloc(&qid, id);
819 STAILQ_INSERT_TAIL(ids, qid, entry);
820 } else if (S_ISREG(mode)) {
821 /* This blob is referenced. */
822 err = preserve_loose_object(loose_ids, id, repo,
826 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 *loose_ids,
842 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
843 const char *dpath, struct got_repository *repo, int *npacked,
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 /* This tree is referenced. */
879 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
883 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
884 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
885 got_object_qid_free(qid);
890 got_object_id_queue_free(&tree_ids);
894 static const struct got_error *
895 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
896 int *npacked, struct got_object_idset *traversed_ids,
897 struct got_object_id *id, struct got_repository *repo,
898 got_cleanup_progress_cb progress_cb, void *progress_arg,
899 struct got_ratelimit *rl, int nloose,
900 got_cancel_cb cancel_cb, void *cancel_arg)
902 const struct got_error *err;
903 struct got_commit_object *commit = NULL;
904 struct got_tag_object *tag = NULL;
905 struct got_object_id *tree_id = NULL;
906 struct got_object_id_queue ids;
907 struct got_object_qid *qid;
910 err = got_object_qid_alloc(&qid, id);
915 STAILQ_INSERT_TAIL(&ids, qid, entry);
917 while (!STAILQ_EMPTY(&ids)) {
919 err = (*cancel_cb)(cancel_arg);
924 qid = STAILQ_FIRST(&ids);
925 STAILQ_REMOVE_HEAD(&ids, entry);
927 if (got_object_idset_contains(traversed_ids, &qid->id)) {
928 got_object_qid_free(qid);
933 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
937 /* This commit or tag is referenced. */
938 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
942 err = got_object_get_type(&obj_type, repo, &qid->id);
946 case GOT_OBJ_TYPE_COMMIT:
947 err = got_object_open_as_commit(&commit, repo,
952 case GOT_OBJ_TYPE_TAG:
953 err = got_object_open_as_tag(&tag, repo, &qid->id);
958 /* should not happen */
959 err = got_error(GOT_ERR_OBJ_TYPE);
963 /* Find a tree object to scan. */
965 tree_id = got_object_commit_get_tree_id(commit);
967 obj_type = got_object_tag_get_object_type(tag);
969 case GOT_OBJ_TYPE_COMMIT:
970 err = got_object_open_as_commit(&commit, repo,
971 got_object_tag_get_object_id(tag));
974 tree_id = got_object_commit_get_tree_id(commit);
976 case GOT_OBJ_TYPE_TREE:
977 tree_id = got_object_tag_get_object_id(tag);
981 * Tag points at something other than a
982 * commit or tree. Leave this weird tag object
983 * and the object it points to on disk.
985 err = got_object_idset_remove(NULL, loose_ids,
987 if (err && err->code != GOT_ERR_NO_OBJ)
989 err = got_object_idset_remove(NULL, loose_ids,
990 got_object_tag_get_object_id(tag));
991 if (err && err->code != GOT_ERR_NO_OBJ)
999 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1000 repo, npacked, cancel_cb, cancel_arg);
1006 (*ncommits)++; /* scanned tags are counted as commits */
1008 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1009 nloose, *ncommits, -1);
1014 /* Find parent commits to scan. */
1015 const struct got_object_id_queue *parent_ids;
1016 parent_ids = got_object_commit_get_parent_ids(commit);
1017 err = got_object_id_queue_copy(parent_ids, &ids);
1020 got_object_commit_close(commit);
1024 got_object_tag_close(tag);
1027 got_object_qid_free(qid);
1032 got_object_qid_free(qid);
1034 got_object_commit_close(commit);
1036 got_object_tag_close(tag);
1037 got_object_id_queue_free(&ids);
1041 struct purge_loose_object_arg {
1042 struct got_repository *repo;
1043 got_cleanup_progress_cb progress_cb;
1045 struct got_ratelimit *rl;
1055 static const struct got_error *
1056 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1058 struct purge_loose_object_arg *a = arg;
1059 const struct got_error *err, *unlock_err = NULL;
1063 struct got_lockfile *lf = NULL;
1065 err = got_object_get_path(&path, id, a->repo);
1069 err = got_object_open_loose_fd(&fd, id, a->repo);
1073 if (fstat(fd, &sb) == -1) {
1074 err = got_error_from_errno("fstat");
1079 * Do not delete objects which are younger than our maximum
1080 * modification time threshold. This prevents a race where
1081 * new objects which are being added to the repository
1082 * concurrently would be deleted.
1084 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1086 err = got_lockfile_lock(&lf, path, -1);
1089 if (unlink(path) == -1) {
1090 err = got_error_from_errno2("unlink", path);
1096 a->size_purged += sb.st_size;
1097 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1098 a->rl, a->nloose, a->ncommits, a->npurged);
1103 if (fd != -1 && close(fd) == -1 && err == NULL)
1104 err = got_error_from_errno("close");
1107 unlock_err = got_lockfile_unlock(lf, -1);
1108 return err ? err : unlock_err;
1111 const struct got_error *
1112 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1113 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1114 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1115 got_cancel_cb cancel_cb, void *cancel_arg)
1117 const struct got_error *err;
1118 struct got_object_idset *loose_ids;
1119 struct got_object_idset *traversed_ids;
1120 struct got_object_id **referenced_ids;
1121 int i, nreferenced, nloose, ncommits = 0;
1122 struct got_reflist_head refs;
1123 struct got_reflist_entry *re;
1124 struct purge_loose_object_arg arg;
1125 time_t max_mtime = 0;
1126 struct got_ratelimit rl;
1129 got_ratelimit_init(&rl, 0, 500);
1135 err = get_loose_object_ids(&loose_ids, size_before,
1136 progress_cb, progress_arg, &rl, repo);
1139 nloose = got_object_idset_num_elements(loose_ids);
1141 got_object_idset_free(loose_ids);
1143 err = progress_cb(progress_arg, 0, 0, 0);
1150 traversed_ids = got_object_idset_alloc();
1151 if (traversed_ids == NULL) {
1152 err = got_error_from_errno("got_object_idset_alloc");
1156 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1159 if (!ignore_mtime) {
1160 TAILQ_FOREACH(re, &refs, entry) {
1161 time_t mtime = got_ref_get_mtime(re->ref);
1162 if (mtime > max_mtime)
1166 * For safety, keep objects created within 10 minutes
1167 * before the youngest reference was created.
1169 if (max_mtime >= 600)
1173 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1174 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1175 &refs, repo, cancel_cb, cancel_arg);
1179 for (i = 0; i < nreferenced; i++) {
1180 struct got_object_id *id = referenced_ids[i];
1181 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1182 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1183 nloose, cancel_cb, cancel_arg);
1188 /* Any remaining loose objects are unreferenced and can be purged. */
1190 arg.progress_arg = progress_arg;
1191 arg.progress_cb = progress_cb;
1193 arg.nloose = nloose;
1195 arg.size_purged = 0;
1196 arg.ncommits = ncommits;
1197 arg.dry_run = dry_run;
1198 arg.max_mtime = max_mtime;
1199 arg.ignore_mtime = ignore_mtime;
1200 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1203 *size_after = *size_before - arg.size_purged;
1205 /* Produce a final progress report. */
1207 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1212 got_object_idset_free(loose_ids);
1213 got_object_idset_free(traversed_ids);
1217 static const struct got_error *
1218 remove_packidx(int dir_fd, const char *relpath)
1220 const struct got_error *err, *unlock_err;
1221 struct got_lockfile *lf;
1223 err = got_lockfile_lock(&lf, relpath, dir_fd);
1226 if (unlinkat(dir_fd, relpath, 0) == -1)
1227 err = got_error_from_errno("unlinkat");
1228 unlock_err = got_lockfile_unlock(lf, dir_fd);
1229 return err ? err : unlock_err;
1232 const struct got_error *
1233 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1234 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1235 got_cancel_cb cancel_cb, void *cancel_arg)
1237 const struct got_error *err;
1238 DIR *packdir = NULL;
1239 struct dirent *dent;
1240 char *pack_relpath = NULL;
1244 packdir_fd = openat(got_repo_get_fd(repo),
1245 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1246 if (packdir_fd == -1) {
1247 if (errno == ENOENT)
1249 return got_error_from_errno_fmt("openat: %s/%s",
1250 got_repo_get_path_git_dir(repo),
1251 GOT_OBJECTS_PACK_DIR);
1254 packdir = fdopendir(packdir_fd);
1255 if (packdir == NULL) {
1256 err = got_error_from_errno("fdopendir");
1260 while ((dent = readdir(packdir)) != NULL) {
1262 err = cancel_cb(cancel_arg);
1267 if (!got_repo_is_packidx_filename(dent->d_name,
1268 strlen(dent->d_name)))
1271 err = got_packidx_get_packfile_path(&pack_relpath,
1276 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1278 pack_relpath = NULL;
1281 if (errno != ENOENT) {
1282 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1283 got_repo_get_path_git_dir(repo),
1284 GOT_OBJECTS_PACK_DIR,
1290 err = remove_packidx(packdir_fd, dent->d_name);
1296 if (asprintf(&path, "%s/%s/%s",
1297 got_repo_get_path_git_dir(repo),
1298 GOT_OBJECTS_PACK_DIR,
1299 dent->d_name) == -1) {
1300 err = got_error_from_errno("asprintf");
1303 err = progress_cb(progress_arg, path);
1309 pack_relpath = NULL;
1312 if (packdir && closedir(packdir) != 0 && err == NULL)
1313 err = got_error_from_errno("closedir");