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>
22 #include <sys/socket.h>
39 #include "got_error.h"
40 #include "got_cancel.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_repository_admin.h"
45 #include "got_opentemp.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_idset.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_repository.h"
55 #include "got_lib_ratelimit.h"
56 #include "got_lib_pack_create.h"
57 #include "got_lib_hash.h"
58 #include "got_lib_lockfile.h"
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 static const struct got_error *
65 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
66 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
67 struct got_repository *repo,
68 got_cancel_cb cancel_cb, void *cancel_arg)
70 const struct got_error *err = NULL;
71 const size_t alloc_chunksz = 256;
73 struct got_reflist_entry *re;
79 err = got_reflist_sort(refs,
80 got_ref_cmp_by_commit_timestamp_descending, repo);
84 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
86 return got_error_from_errno("reallocarray");
87 nalloc = alloc_chunksz;
89 TAILQ_FOREACH(re, refs, entry) {
90 struct got_object_id *id;
93 err = cancel_cb(cancel_arg);
98 err = got_ref_resolve(&id, repo, re->ref);
102 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
104 err = got_object_get_type(&obj_type, repo, id);
107 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
114 if (nalloc <= *nobjects) {
115 struct got_object_id **new;
116 new = recallocarray(*ids, nalloc,
117 nalloc + alloc_chunksz,
118 sizeof(struct got_object_id *));
120 err = got_error_from_errno(
125 nalloc += alloc_chunksz;
127 (*ids)[*nobjects] = id;
128 if ((*ids)[*nobjects] == NULL) {
129 err = got_error_from_errno("got_object_id_dup");
136 for (i = 0; i < *nobjects; i++)
145 const struct got_error *
146 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
147 struct got_reflist_head *include_refs,
148 struct got_reflist_head *exclude_refs, struct got_repository *repo,
149 int loose_obj_only, int force_refdelta,
150 got_pack_progress_cb progress_cb, void *progress_arg,
151 got_cancel_cb cancel_cb, void *cancel_arg)
153 const struct got_error *err = NULL;
154 struct got_object_id **ours = NULL, **theirs = NULL;
155 int nours = 0, ntheirs = 0, packfd = -1, i;
156 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
157 char *sha1_str = NULL;
158 FILE *delta_cache = NULL;
159 struct got_ratelimit rl;
164 got_ratelimit_init(&rl, 0, 500);
166 if (asprintf(&path, "%s/%s/packing.pack",
167 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
168 err = got_error_from_errno("asprintf");
171 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path, "");
175 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
176 err = got_error_from_errno2("fchmod", tmpfile_path);
180 delta_cache = got_opentemp();
181 if (delta_cache == NULL) {
182 err = got_error_from_errno("got_opentemp");
186 err = get_reflist_object_ids(&ours, &nours,
187 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
188 include_refs, repo, cancel_cb, cancel_arg);
193 err = got_error(GOT_ERR_CANNOT_PACK);
197 if (!TAILQ_EMPTY(exclude_refs)) {
198 err = get_reflist_object_ids(&theirs, &ntheirs,
199 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
201 cancel_cb, cancel_arg);
206 *pack_hash = calloc(1, sizeof(**pack_hash));
207 if (*pack_hash == NULL) {
208 err = got_error_from_errno("calloc");
212 err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
213 theirs, ntheirs, ours, nours, repo, loose_obj_only,
214 0, force_refdelta, progress_cb, progress_arg, &rl,
215 cancel_cb, cancel_arg);
219 err = got_object_id_str(&sha1_str, *pack_hash);
222 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
223 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
225 err = got_error_from_errno("asprintf");
229 if (lseek(packfd, 0L, SEEK_SET) == -1) {
230 err = got_error_from_errno("lseek");
233 if (rename(tmpfile_path, packfile_path) == -1) {
234 err = got_error_from_errno3("rename", tmpfile_path,
241 *packfile = fdopen(packfd, "w");
242 if (*packfile == NULL) {
243 err = got_error_from_errno2("fdopen", tmpfile_path);
248 for (i = 0; i < nours; i++)
251 for (i = 0; i < ntheirs; i++)
254 if (packfd != -1 && close(packfd) == -1 && err == NULL)
255 err = got_error_from_errno2("close", packfile_path);
256 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
257 err = got_error_from_errno("fclose");
258 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
259 err = got_error_from_errno2("unlink", tmpfile_path);
274 const struct got_error *
275 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
276 struct got_repository *repo,
277 got_pack_index_progress_cb progress_cb, void *progress_arg,
278 got_cancel_cb cancel_cb, void *cancel_arg)
283 int npackfd = -1, idxfd = -1, nidxfd = -1;
285 int idxstatus, done = 0;
286 const struct got_error *err;
287 struct imsgbuf idxibuf;
289 char *tmpidxpath = NULL;
290 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
291 const char *repo_path = got_repo_get_path_git_dir(repo);
294 for (i = 0; i < nitems(tmpfds); i++)
297 if (asprintf(&path, "%s/%s/indexing.idx",
298 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
299 err = got_error_from_errno("asprintf");
302 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
306 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
307 err = got_error_from_errno2("fchmod", tmpidxpath);
313 err = got_error_from_errno("dup");
317 for (i = 0; i < nitems(tmpfds); i++) {
318 tmpfds[i] = got_opentempfd();
319 if (tmpfds[i] == -1) {
320 err = got_error_from_errno("got_opentempfd");
325 err = got_object_id_str(&id_str, pack_hash);
329 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
330 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
331 err = got_error_from_errno("asprintf");
335 if (fstat(fileno(packfile), &sb) == -1) {
336 err = got_error_from_errno2("fstat", packfile_path);
340 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
341 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
342 err = got_error_from_errno("asprintf");
346 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
347 err = got_error_from_errno("socketpair");
352 err= got_error_from_errno("fork");
354 } else if (idxpid == 0)
355 got_privsep_exec_child(imsg_idxfds,
356 GOT_PATH_PROG_INDEX_PACK, packfile_path);
357 if (close(imsg_idxfds[1]) == -1) {
358 err = got_error_from_errno("close");
361 imsg_init(&idxibuf, imsg_idxfds[0]);
363 npackfd = dup(fileno(packfile));
365 err = got_error_from_errno("dup");
368 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
373 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
377 for (i = 0; i < nitems(tmpfds); i++) {
378 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
385 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
388 err = cancel_cb(cancel_arg);
393 err = got_privsep_recv_index_progress(&done, &nobj_total,
394 &nobj_indexed, &nobj_loose, &nobj_resolved,
398 if (nobj_indexed != 0) {
399 err = progress_cb(progress_arg, sb.st_size,
400 nobj_total, nobj_indexed, nobj_loose,
406 if (close(imsg_idxfds[0]) == -1) {
407 err = got_error_from_errno("close");
410 if (waitpid(idxpid, &idxstatus, 0) == -1) {
411 err = got_error_from_errno("waitpid");
415 if (rename(tmpidxpath, idxpath) == -1) {
416 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
423 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
424 err = got_error_from_errno2("unlink", tmpidxpath);
425 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
426 err = got_error_from_errno("close");
427 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
428 err = got_error_from_errno("close");
429 for (i = 0; i < nitems(tmpfds); i++) {
430 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
431 err = got_error_from_errno("close");
439 const struct got_error *
440 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
441 struct got_repository *repo, const char *packfile_path)
443 const struct got_error *err = NULL;
444 const char *packdir_path = NULL;
445 char *packfile_name = NULL, *p, *dot;
446 struct got_object_id id;
452 packdir_path = got_repo_get_path_objects_pack(repo);
453 if (packdir_path == NULL)
454 return got_error_from_errno("got_repo_get_path_objects_pack");
456 if (!got_path_is_child(packfile_path, packdir_path,
457 strlen(packdir_path))) {
458 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
463 err = got_path_basename(&packfile_name, packfile_path);
468 if (strncmp(p, "pack-", 5) != 0) {
469 err = got_error_fmt(GOT_ERR_BAD_PATH,
470 "'%s' is not a valid pack file name",
475 dot = strchr(p, '.');
477 err = got_error_fmt(GOT_ERR_BAD_PATH,
478 "'%s' is not a valid pack file name",
482 if (strcmp(dot + 1, "pack") != 0) {
483 err = got_error_fmt(GOT_ERR_BAD_PATH,
484 "'%s' is not a valid pack file name",
489 if (!got_parse_object_id(&id, p, repo->algo)) {
490 err = got_error_fmt(GOT_ERR_BAD_PATH,
491 "'%s' is not a valid pack file name",
496 *pack_hash = got_object_id_dup(&id);
497 if (*pack_hash == NULL) {
498 err = got_error_from_errno("got_object_id_dup");
502 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
504 err = got_error_from_errno2("open", packfile_path);
508 *packfile = fdopen(packfd, "r");
509 if (*packfile == NULL) {
510 err = got_error_from_errno2("fdopen", packfile_path);
515 if (packfd != -1 && close(packfd) == -1 && err == NULL)
516 err = got_error_from_errno2("close", packfile_path);
525 const struct got_error *
526 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
527 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
528 got_cancel_cb cancel_cb, void *cancel_arg)
530 const struct got_error *err = NULL;
531 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
532 struct got_packidx *packidx = NULL;
533 struct got_pack *pack = NULL;
536 err = got_object_id_str(&id_str, pack_hash);
540 if (asprintf(&packpath, "%s/pack-%s.pack",
541 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
542 err = got_error_from_errno("asprintf");
545 if (asprintf(&idxpath, "%s/pack-%s.idx",
546 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
547 err = got_error_from_errno("asprintf");
551 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
555 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
559 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
560 for (i = 0; i < nobj; i++) {
561 struct got_packidx_object_id *oid;
562 struct got_object_id id, base_id;
563 off_t offset, base_offset = 0;
569 err = cancel_cb(cancel_arg);
573 oid = &packidx->hdr.sorted_ids[i];
574 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
576 offset = got_packidx_get_object_offset(packidx, i);
578 err = got_error(GOT_ERR_BAD_PACKIDX);
582 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
588 case GOT_OBJ_TYPE_OFFSET_DELTA:
589 err = got_pack_parse_offset_delta(&base_offset, &len,
590 pack, offset, tslen);
594 case GOT_OBJ_TYPE_REF_DELTA:
595 err = got_pack_parse_ref_delta(&base_id,
596 pack, offset, tslen);
601 err = (*list_cb)(list_arg, &id, type, offset, size,
602 base_offset, &base_id);
612 got_packidx_close(packidx);
616 static const struct got_error *
617 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
618 void *progress_arg, struct got_ratelimit *rl,
619 int nloose, int ncommits, int npurged)
621 const struct got_error *err;
624 if (progress_cb == NULL)
627 err = got_ratelimit_check(&elapsed, rl);
631 return progress_cb(progress_arg, nloose, ncommits, npurged);
634 static const struct got_error *
635 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
636 got_cleanup_progress_cb progress_cb, void *progress_arg,
637 struct got_ratelimit *rl, struct got_repository *repo)
639 const struct got_error *err = NULL;
640 char *path_objects = NULL, *path = NULL;
642 struct got_object *obj = NULL;
643 struct got_object_id id;
648 *loose_ids = got_object_idset_alloc();
649 if (*loose_ids == NULL)
650 return got_error_from_errno("got_object_idset_alloc");
652 path_objects = got_repo_get_path_objects(repo);
653 if (path_objects == NULL) {
654 err = got_error_from_errno("got_repo_get_path_objects");
658 for (i = 0; i <= 0xff; i++) {
661 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
662 err = got_error_from_errno("asprintf");
668 if (errno == ENOENT) {
672 err = got_error_from_errno2("opendir", path);
676 while ((dent = readdir(dir)) != NULL) {
679 if (strcmp(dent->d_name, ".") == 0 ||
680 strcmp(dent->d_name, "..") == 0)
683 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
684 err = got_error_from_errno("asprintf");
688 if (!got_parse_object_id(&id, id_str, repo->algo)) {
694 err = got_object_open_loose_fd(&fd, &id, repo);
697 if (fstat(fd, &sb) == -1) {
698 err = got_error_from_errno("fstat");
701 err = got_object_read_header_privsep(&obj, &id, repo,
705 fd = -1; /* already closed */
708 case GOT_OBJ_TYPE_COMMIT:
709 case GOT_OBJ_TYPE_TREE:
710 case GOT_OBJ_TYPE_BLOB:
711 case GOT_OBJ_TYPE_TAG:
714 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
718 got_object_close(obj);
720 (*ondisk_size) += sb.st_size;
721 err = got_object_idset_add(*loose_ids, &id, NULL);
724 err = report_cleanup_progress(progress_cb,
726 got_object_idset_num_elements(*loose_ids), -1, -1);
731 if (closedir(dir) != 0) {
732 err = got_error_from_errno("closedir");
741 if (dir && closedir(dir) != 0 && err == NULL)
742 err = got_error_from_errno("closedir");
743 if (fd != -1 && close(fd) == -1 && err == NULL)
744 err = got_error_from_errno("close");
746 got_object_idset_free(*loose_ids);
750 got_object_close(obj);
756 static const struct got_error *
757 preserve_loose_object(struct got_object_idset *loose_ids,
758 struct got_object_id *id, struct got_repository *repo, int *npacked)
760 const struct got_error *err = NULL;
761 struct got_object *obj;
763 if (!got_object_idset_contains(loose_ids, id))
767 * Try to open this object from a pack file. This ensures that
768 * we do in fact have a valid packed copy of the object. Otherwise
769 * we should not delete the loose representation of this object.
771 err = got_object_open_packed(&obj, id, repo);
773 got_object_close(obj);
775 * The object is referenced and packed.
776 * We can purge the redundantly stored loose object.
780 } else if (err->code != GOT_ERR_NO_OBJ)
784 * This object is referenced and not packed.
785 * Remove it from our purge set.
787 return got_object_idset_remove(NULL, loose_ids, id);
790 static const struct got_error *
791 load_tree_entries(struct got_object_id_queue *ids,
792 struct got_object_idset *loose_ids,
793 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
794 const char *dpath, struct got_repository *repo, int *npacked,
795 got_cancel_cb cancel_cb, void *cancel_arg)
797 const struct got_error *err;
798 struct got_tree_object *tree;
802 err = got_object_open_as_tree(&tree, repo, tree_id);
806 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
807 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
808 struct got_object_id *id = got_tree_entry_get_id(e);
809 mode_t mode = got_tree_entry_get_mode(e);
812 err = (*cancel_cb)(cancel_arg);
817 if (got_object_tree_entry_is_symlink(e) ||
818 got_object_tree_entry_is_submodule(e) ||
819 got_object_idset_contains(traversed_ids, id))
822 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
823 got_tree_entry_get_name(e)) == -1) {
824 err = got_error_from_errno("asprintf");
829 struct got_object_qid *qid;
830 err = got_object_qid_alloc(&qid, id);
833 STAILQ_INSERT_TAIL(ids, qid, entry);
834 } else if (S_ISREG(mode)) {
835 /* This blob is referenced. */
836 err = preserve_loose_object(loose_ids, id, repo,
840 err = got_object_idset_add(traversed_ids, id, NULL);
848 got_object_tree_close(tree);
853 static const struct got_error *
854 load_tree(struct got_object_idset *loose_ids,
855 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
856 const char *dpath, struct got_repository *repo, int *npacked,
857 got_cancel_cb cancel_cb, void *cancel_arg)
859 const struct got_error *err = NULL;
860 struct got_object_id_queue tree_ids;
861 struct got_object_qid *qid;
863 err = got_object_qid_alloc(&qid, tree_id);
867 STAILQ_INIT(&tree_ids);
868 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
870 while (!STAILQ_EMPTY(&tree_ids)) {
872 err = (*cancel_cb)(cancel_arg);
877 qid = STAILQ_FIRST(&tree_ids);
878 STAILQ_REMOVE_HEAD(&tree_ids, entry);
880 if (got_object_idset_contains(traversed_ids, &qid->id)) {
881 got_object_qid_free(qid);
885 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
887 got_object_qid_free(qid);
891 /* This tree is referenced. */
892 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
896 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
897 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
898 got_object_qid_free(qid);
903 got_object_id_queue_free(&tree_ids);
907 static const struct got_error *
908 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
909 int *npacked, struct got_object_idset *traversed_ids,
910 struct got_object_id *id, struct got_repository *repo,
911 got_cleanup_progress_cb progress_cb, void *progress_arg,
912 struct got_ratelimit *rl, int nloose,
913 got_cancel_cb cancel_cb, void *cancel_arg)
915 const struct got_error *err;
916 struct got_commit_object *commit = NULL;
917 struct got_tag_object *tag = NULL;
918 struct got_object_id *tree_id = NULL;
919 struct got_object_id_queue ids;
920 struct got_object_qid *qid;
923 err = got_object_qid_alloc(&qid, id);
928 STAILQ_INSERT_TAIL(&ids, qid, entry);
930 while (!STAILQ_EMPTY(&ids)) {
932 err = (*cancel_cb)(cancel_arg);
937 qid = STAILQ_FIRST(&ids);
938 STAILQ_REMOVE_HEAD(&ids, entry);
940 if (got_object_idset_contains(traversed_ids, &qid->id)) {
941 got_object_qid_free(qid);
946 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
950 /* This commit or tag is referenced. */
951 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
955 err = got_object_get_type(&obj_type, repo, &qid->id);
959 case GOT_OBJ_TYPE_COMMIT:
960 err = got_object_open_as_commit(&commit, repo,
965 case GOT_OBJ_TYPE_TAG:
966 err = got_object_open_as_tag(&tag, repo, &qid->id);
971 /* should not happen */
972 err = got_error(GOT_ERR_OBJ_TYPE);
976 /* Find a tree object to scan. */
978 tree_id = got_object_commit_get_tree_id(commit);
980 obj_type = got_object_tag_get_object_type(tag);
982 case GOT_OBJ_TYPE_COMMIT:
983 err = got_object_open_as_commit(&commit, repo,
984 got_object_tag_get_object_id(tag));
987 tree_id = got_object_commit_get_tree_id(commit);
989 case GOT_OBJ_TYPE_TREE:
990 tree_id = got_object_tag_get_object_id(tag);
994 * Tag points at something other than a
995 * commit or tree. Leave this weird tag object
996 * and the object it points to on disk.
998 err = got_object_idset_remove(NULL, loose_ids,
1000 if (err && err->code != GOT_ERR_NO_OBJ)
1002 err = got_object_idset_remove(NULL, loose_ids,
1003 got_object_tag_get_object_id(tag));
1004 if (err && err->code != GOT_ERR_NO_OBJ)
1012 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1013 repo, npacked, cancel_cb, cancel_arg);
1019 (*ncommits)++; /* scanned tags are counted as commits */
1021 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1022 nloose, *ncommits, -1);
1027 /* Find parent commits to scan. */
1028 const struct got_object_id_queue *parent_ids;
1029 parent_ids = got_object_commit_get_parent_ids(commit);
1030 err = got_object_id_queue_copy(parent_ids, &ids);
1033 got_object_commit_close(commit);
1037 got_object_tag_close(tag);
1040 got_object_qid_free(qid);
1045 got_object_qid_free(qid);
1047 got_object_commit_close(commit);
1049 got_object_tag_close(tag);
1050 got_object_id_queue_free(&ids);
1054 struct purge_loose_object_arg {
1055 struct got_repository *repo;
1056 got_cleanup_progress_cb progress_cb;
1058 struct got_ratelimit *rl;
1068 static const struct got_error *
1069 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1071 struct purge_loose_object_arg *a = arg;
1072 const struct got_error *err, *unlock_err = NULL;
1076 struct got_lockfile *lf = NULL;
1078 err = got_object_get_path(&path, id, a->repo);
1082 err = got_object_open_loose_fd(&fd, id, a->repo);
1086 if (fstat(fd, &sb) == -1) {
1087 err = got_error_from_errno("fstat");
1092 * Do not delete objects which are younger than our maximum
1093 * modification time threshold. This prevents a race where
1094 * new objects which are being added to the repository
1095 * concurrently would be deleted.
1097 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1099 err = got_lockfile_lock(&lf, path, -1);
1102 if (unlink(path) == -1) {
1103 err = got_error_from_errno2("unlink", path);
1109 a->size_purged += sb.st_size;
1110 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1111 a->rl, a->nloose, a->ncommits, a->npurged);
1116 if (fd != -1 && close(fd) == -1 && err == NULL)
1117 err = got_error_from_errno("close");
1120 unlock_err = got_lockfile_unlock(lf, -1);
1121 return err ? err : unlock_err;
1124 const struct got_error *
1125 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1126 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1127 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1128 got_cancel_cb cancel_cb, void *cancel_arg)
1130 const struct got_error *err;
1131 struct got_object_idset *loose_ids;
1132 struct got_object_idset *traversed_ids;
1133 struct got_object_id **referenced_ids;
1134 int i, nreferenced, nloose, ncommits = 0;
1135 struct got_reflist_head refs;
1136 struct got_reflist_entry *re;
1137 struct purge_loose_object_arg arg;
1138 time_t max_mtime = 0;
1139 struct got_ratelimit rl;
1142 got_ratelimit_init(&rl, 0, 500);
1148 err = get_loose_object_ids(&loose_ids, size_before,
1149 progress_cb, progress_arg, &rl, repo);
1152 nloose = got_object_idset_num_elements(loose_ids);
1154 got_object_idset_free(loose_ids);
1156 err = progress_cb(progress_arg, 0, 0, 0);
1163 traversed_ids = got_object_idset_alloc();
1164 if (traversed_ids == NULL) {
1165 err = got_error_from_errno("got_object_idset_alloc");
1169 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1172 if (!ignore_mtime) {
1173 TAILQ_FOREACH(re, &refs, entry) {
1174 time_t mtime = got_ref_get_mtime(re->ref);
1175 if (mtime > max_mtime)
1179 * For safety, keep objects created within 10 minutes
1180 * before the youngest reference was created.
1182 if (max_mtime >= 600)
1186 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1187 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1188 &refs, repo, cancel_cb, cancel_arg);
1192 for (i = 0; i < nreferenced; i++) {
1193 struct got_object_id *id = referenced_ids[i];
1194 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1195 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1196 nloose, cancel_cb, cancel_arg);
1201 /* Any remaining loose objects are unreferenced and can be purged. */
1203 arg.progress_arg = progress_arg;
1204 arg.progress_cb = progress_cb;
1206 arg.nloose = nloose;
1208 arg.size_purged = 0;
1209 arg.ncommits = ncommits;
1210 arg.dry_run = dry_run;
1211 arg.max_mtime = max_mtime;
1212 arg.ignore_mtime = ignore_mtime;
1213 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1216 *size_after = *size_before - arg.size_purged;
1218 /* Produce a final progress report. */
1220 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1225 got_object_idset_free(loose_ids);
1226 got_object_idset_free(traversed_ids);
1230 static const struct got_error *
1231 remove_packidx(int dir_fd, const char *relpath)
1233 const struct got_error *err, *unlock_err;
1234 struct got_lockfile *lf;
1236 err = got_lockfile_lock(&lf, relpath, dir_fd);
1239 if (unlinkat(dir_fd, relpath, 0) == -1)
1240 err = got_error_from_errno("unlinkat");
1241 unlock_err = got_lockfile_unlock(lf, dir_fd);
1242 return err ? err : unlock_err;
1245 const struct got_error *
1246 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1247 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1248 got_cancel_cb cancel_cb, void *cancel_arg)
1250 const struct got_error *err = NULL;
1251 DIR *packdir = NULL;
1252 struct dirent *dent;
1253 char *pack_relpath = NULL;
1257 packdir_fd = openat(got_repo_get_fd(repo),
1258 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1259 if (packdir_fd == -1) {
1260 if (errno == ENOENT)
1262 return got_error_from_errno_fmt("openat: %s/%s",
1263 got_repo_get_path_git_dir(repo),
1264 GOT_OBJECTS_PACK_DIR);
1267 packdir = fdopendir(packdir_fd);
1268 if (packdir == NULL) {
1269 err = got_error_from_errno("fdopendir");
1273 while ((dent = readdir(packdir)) != NULL) {
1275 err = cancel_cb(cancel_arg);
1280 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1283 err = got_packidx_get_packfile_path(&pack_relpath,
1288 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1290 pack_relpath = NULL;
1293 if (errno != ENOENT) {
1294 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1295 got_repo_get_path_git_dir(repo),
1296 GOT_OBJECTS_PACK_DIR,
1302 err = remove_packidx(packdir_fd, dent->d_name);
1308 if (asprintf(&path, "%s/%s/%s",
1309 got_repo_get_path_git_dir(repo),
1310 GOT_OBJECTS_PACK_DIR,
1311 dent->d_name) == -1) {
1312 err = got_error_from_errno("asprintf");
1315 err = progress_cb(progress_arg, path);
1321 pack_relpath = NULL;
1324 if (packdir && closedir(packdir) != 0 && err == NULL)
1325 err = got_error_from_errno("closedir");