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_sha1.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,
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, 0,
209 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
213 err = got_object_id_str(&sha1_str, *pack_hash);
216 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
217 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
219 err = got_error_from_errno("asprintf");
223 if (lseek(packfd, 0L, SEEK_SET) == -1) {
224 err = got_error_from_errno("lseek");
227 if (rename(tmpfile_path, packfile_path) == -1) {
228 err = got_error_from_errno3("rename", tmpfile_path,
235 *packfile = fdopen(packfd, "w");
236 if (*packfile == NULL) {
237 err = got_error_from_errno2("fdopen", tmpfile_path);
242 for (i = 0; i < nours; i++)
245 for (i = 0; i < ntheirs; i++)
248 if (packfd != -1 && close(packfd) == -1 && err == NULL)
249 err = got_error_from_errno2("close", packfile_path);
250 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
251 err = got_error_from_errno("fclose");
252 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
253 err = got_error_from_errno2("unlink", tmpfile_path);
268 const struct got_error *
269 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
270 struct got_repository *repo,
271 got_pack_index_progress_cb progress_cb, void *progress_arg,
272 got_cancel_cb cancel_cb, void *cancel_arg)
277 int npackfd = -1, idxfd = -1, nidxfd = -1;
279 int idxstatus, done = 0;
280 const struct got_error *err;
281 struct imsgbuf idxibuf;
283 char *tmpidxpath = NULL;
284 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
285 const char *repo_path = got_repo_get_path_git_dir(repo);
288 for (i = 0; i < nitems(tmpfds); i++)
291 if (asprintf(&path, "%s/%s/indexing.idx",
292 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
293 err = got_error_from_errno("asprintf");
296 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
300 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
301 err = got_error_from_errno2("fchmod", tmpidxpath);
307 err = got_error_from_errno("dup");
311 for (i = 0; i < nitems(tmpfds); i++) {
312 tmpfds[i] = got_opentempfd();
313 if (tmpfds[i] == -1) {
314 err = got_error_from_errno("got_opentempfd");
319 err = got_object_id_str(&id_str, pack_hash);
323 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
324 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
325 err = got_error_from_errno("asprintf");
329 if (fstat(fileno(packfile), &sb) == -1) {
330 err = got_error_from_errno2("fstat", packfile_path);
334 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
335 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
336 err = got_error_from_errno("asprintf");
340 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
341 err = got_error_from_errno("socketpair");
346 err= got_error_from_errno("fork");
348 } else if (idxpid == 0)
349 got_privsep_exec_child(imsg_idxfds,
350 GOT_PATH_PROG_INDEX_PACK, packfile_path);
351 if (close(imsg_idxfds[1]) == -1) {
352 err = got_error_from_errno("close");
355 imsg_init(&idxibuf, imsg_idxfds[0]);
357 npackfd = dup(fileno(packfile));
359 err = got_error_from_errno("dup");
362 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
367 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
371 for (i = 0; i < nitems(tmpfds); i++) {
372 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
379 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
382 err = cancel_cb(cancel_arg);
387 err = got_privsep_recv_index_progress(&done, &nobj_total,
388 &nobj_indexed, &nobj_loose, &nobj_resolved,
392 if (nobj_indexed != 0) {
393 err = progress_cb(progress_arg, sb.st_size,
394 nobj_total, nobj_indexed, nobj_loose,
400 if (close(imsg_idxfds[0]) == -1) {
401 err = got_error_from_errno("close");
404 if (waitpid(idxpid, &idxstatus, 0) == -1) {
405 err = got_error_from_errno("waitpid");
409 if (rename(tmpidxpath, idxpath) == -1) {
410 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
417 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
418 err = got_error_from_errno2("unlink", tmpidxpath);
419 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
420 err = got_error_from_errno("close");
421 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
422 err = got_error_from_errno("close");
423 for (i = 0; i < nitems(tmpfds); i++) {
424 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
425 err = got_error_from_errno("close");
433 const struct got_error *
434 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
435 struct got_repository *repo, const char *packfile_path)
437 const struct got_error *err = NULL;
438 const char *packdir_path = NULL;
439 char *packfile_name = NULL, *p, *dot;
440 struct got_object_id id;
446 packdir_path = got_repo_get_path_objects_pack(repo);
447 if (packdir_path == NULL)
448 return got_error_from_errno("got_repo_get_path_objects_pack");
450 if (!got_path_is_child(packfile_path, packdir_path,
451 strlen(packdir_path))) {
452 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
457 err = got_path_basename(&packfile_name, packfile_path);
462 if (strncmp(p, "pack-", 5) != 0) {
463 err = got_error_fmt(GOT_ERR_BAD_PATH,
464 "'%s' is not a valid pack file name",
469 dot = strchr(p, '.');
471 err = got_error_fmt(GOT_ERR_BAD_PATH,
472 "'%s' is not a valid pack file name",
476 if (strcmp(dot + 1, "pack") != 0) {
477 err = got_error_fmt(GOT_ERR_BAD_PATH,
478 "'%s' is not a valid pack file name",
483 if (!got_parse_sha1_digest(id.sha1, p)) {
484 err = got_error_fmt(GOT_ERR_BAD_PATH,
485 "'%s' is not a valid pack file name",
490 *pack_hash = got_object_id_dup(&id);
491 if (*pack_hash == NULL) {
492 err = got_error_from_errno("got_object_id_dup");
496 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
498 err = got_error_from_errno2("open", packfile_path);
502 *packfile = fdopen(packfd, "r");
503 if (*packfile == NULL) {
504 err = got_error_from_errno2("fdopen", packfile_path);
509 if (packfd != -1 && close(packfd) == -1 && err == NULL)
510 err = got_error_from_errno2("close", packfile_path);
519 const struct got_error *
520 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
521 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
522 got_cancel_cb cancel_cb, void *cancel_arg)
524 const struct got_error *err = NULL;
525 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
526 struct got_packidx *packidx = NULL;
527 struct got_pack *pack = NULL;
530 err = got_object_id_str(&id_str, pack_hash);
534 if (asprintf(&packpath, "%s/pack-%s.pack",
535 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
536 err = got_error_from_errno("asprintf");
539 if (asprintf(&idxpath, "%s/pack-%s.idx",
540 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
541 err = got_error_from_errno("asprintf");
545 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
549 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
553 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
554 for (i = 0; i < nobj; i++) {
555 struct got_packidx_object_id *oid;
556 struct got_object_id id, base_id;
557 off_t offset, base_offset = 0;
563 err = cancel_cb(cancel_arg);
567 oid = &packidx->hdr.sorted_ids[i];
568 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
570 offset = got_packidx_get_object_offset(packidx, i);
572 err = got_error(GOT_ERR_BAD_PACKIDX);
576 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
582 case GOT_OBJ_TYPE_OFFSET_DELTA:
583 err = got_pack_parse_offset_delta(&base_offset, &len,
584 pack, offset, tslen);
588 case GOT_OBJ_TYPE_REF_DELTA:
589 err = got_pack_parse_ref_delta(&base_id,
590 pack, offset, tslen);
595 err = (*list_cb)(list_arg, &id, type, offset, size,
596 base_offset, &base_id);
606 got_packidx_close(packidx);
610 static const struct got_error *
611 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
612 void *progress_arg, struct got_ratelimit *rl,
613 int nloose, int ncommits, int npurged)
615 const struct got_error *err;
618 if (progress_cb == NULL)
621 err = got_ratelimit_check(&elapsed, rl);
625 return progress_cb(progress_arg, nloose, ncommits, npurged);
628 static const struct got_error *
629 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
630 got_cleanup_progress_cb progress_cb, void *progress_arg,
631 struct got_ratelimit *rl, struct got_repository *repo)
633 const struct got_error *err = NULL;
634 char *path_objects = NULL, *path = NULL;
636 struct got_object *obj = NULL;
637 struct got_object_id id;
642 *loose_ids = got_object_idset_alloc();
643 if (*loose_ids == NULL)
644 return got_error_from_errno("got_object_idset_alloc");
646 path_objects = got_repo_get_path_objects(repo);
647 if (path_objects == NULL) {
648 err = got_error_from_errno("got_repo_get_path_objects");
652 for (i = 0; i <= 0xff; i++) {
655 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
656 err = got_error_from_errno("asprintf");
662 if (errno == ENOENT) {
666 err = got_error_from_errno2("opendir", path);
670 while ((dent = readdir(dir)) != NULL) {
673 if (strcmp(dent->d_name, ".") == 0 ||
674 strcmp(dent->d_name, "..") == 0)
677 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
678 err = got_error_from_errno("asprintf");
682 memset(&id, 0, sizeof(id));
683 if (!got_parse_sha1_digest(id.sha1, id_str)) {
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");