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>
38 #include "got_error.h"
39 #include "got_cancel.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_repository_admin.h"
44 #include "got_opentemp.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_idset.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_repository.h"
54 #include "got_lib_pack_create.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_lockfile.h"
57 #include "got_lib_ratelimit.h"
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 static const struct got_error *
64 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
65 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
66 struct got_repository *repo,
67 got_cancel_cb cancel_cb, void *cancel_arg)
69 const struct got_error *err = NULL;
70 const size_t alloc_chunksz = 256;
72 struct got_reflist_entry *re;
78 err = got_reflist_sort(refs,
79 got_ref_cmp_by_commit_timestamp_descending, repo);
83 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
85 return got_error_from_errno("reallocarray");
86 nalloc = alloc_chunksz;
88 TAILQ_FOREACH(re, refs, entry) {
89 struct got_object_id *id;
92 err = cancel_cb(cancel_arg);
97 err = got_ref_resolve(&id, repo, re->ref);
101 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
103 err = got_object_get_type(&obj_type, repo, id);
106 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
113 if (nalloc <= *nobjects) {
114 struct got_object_id **new;
115 new = recallocarray(*ids, nalloc,
116 nalloc + alloc_chunksz,
117 sizeof(struct got_object_id *));
119 err = got_error_from_errno(
124 nalloc += alloc_chunksz;
126 (*ids)[*nobjects] = id;
127 if ((*ids)[*nobjects] == NULL) {
128 err = got_error_from_errno("got_object_id_dup");
135 for (i = 0; i < *nobjects; i++)
144 const struct got_error *
145 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
146 struct got_reflist_head *include_refs,
147 struct got_reflist_head *exclude_refs, struct got_repository *repo,
149 got_pack_progress_cb progress_cb, void *progress_arg,
150 got_cancel_cb cancel_cb, void *cancel_arg)
152 const struct got_error *err = NULL;
153 struct got_object_id **ours = NULL, **theirs = NULL;
154 int nours = 0, ntheirs = 0, packfd = -1, i;
155 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
156 char *sha1_str = NULL;
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_FILE_MODE) != 0) {
171 err = got_error_from_errno2("fchmod", tmpfile_path);
175 *packfile = fdopen(packfd, "w");
176 if (*packfile == NULL) {
177 err = got_error_from_errno2("fdopen", tmpfile_path);
182 err = get_reflist_object_ids(&ours, &nours,
183 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
184 include_refs, repo, cancel_cb, cancel_arg);
189 err = got_error(GOT_ERR_CANNOT_PACK);
193 if (!TAILQ_EMPTY(exclude_refs)) {
194 err = get_reflist_object_ids(&theirs, &ntheirs,
195 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
197 cancel_cb, cancel_arg);
202 *pack_hash = calloc(1, sizeof(**pack_hash));
203 if (*pack_hash == NULL) {
204 err = got_error_from_errno("calloc");
208 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
209 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
210 cancel_cb, cancel_arg);
214 err = got_object_id_str(&sha1_str, *pack_hash);
217 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
218 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
220 err = got_error_from_errno("asprintf");
224 if (fflush(*packfile) == -1) {
225 err = got_error_from_errno("fflush");
228 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
229 err = got_error_from_errno("fseek");
232 if (rename(tmpfile_path, packfile_path) == -1) {
233 err = got_error_from_errno3("rename", tmpfile_path,
240 for (i = 0; i < nours; i++)
243 for (i = 0; i < ntheirs; i++)
246 if (packfd != -1 && close(packfd) == -1 && err == NULL)
247 err = got_error_from_errno2("close", packfile_path);
248 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
249 err = got_error_from_errno2("unlink", tmpfile_path);
264 const struct got_error *
265 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
266 struct got_repository *repo,
267 got_pack_index_progress_cb progress_cb, void *progress_arg,
268 got_cancel_cb cancel_cb, void *cancel_arg)
273 int npackfd = -1, idxfd = -1, nidxfd = -1;
275 int idxstatus, done = 0;
276 const struct got_error *err;
277 struct imsgbuf idxibuf;
279 char *tmpidxpath = NULL;
280 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
281 const char *repo_path = got_repo_get_path_git_dir(repo);
284 for (i = 0; i < nitems(tmpfds); i++)
287 if (asprintf(&path, "%s/%s/indexing.idx",
288 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
289 err = got_error_from_errno("asprintf");
292 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
296 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
297 err = got_error_from_errno2("fchmod", tmpidxpath);
303 err = got_error_from_errno("dup");
307 for (i = 0; i < nitems(tmpfds); i++) {
308 tmpfds[i] = got_opentempfd();
309 if (tmpfds[i] == -1) {
310 err = got_error_from_errno("got_opentempfd");
315 err = got_object_id_str(&id_str, pack_hash);
319 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
320 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
321 err = got_error_from_errno("asprintf");
325 if (fstat(fileno(packfile), &sb) == -1) {
326 err = got_error_from_errno2("fstat", packfile_path);
330 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
331 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
332 err = got_error_from_errno("asprintf");
336 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
337 err = got_error_from_errno("socketpair");
342 err= got_error_from_errno("fork");
344 } else if (idxpid == 0)
345 got_privsep_exec_child(imsg_idxfds,
346 GOT_PATH_PROG_INDEX_PACK, packfile_path);
347 if (close(imsg_idxfds[1]) == -1) {
348 err = got_error_from_errno("close");
351 imsg_init(&idxibuf, imsg_idxfds[0]);
353 npackfd = dup(fileno(packfile));
355 err = got_error_from_errno("dup");
358 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
363 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
367 for (i = 0; i < nitems(tmpfds); i++) {
368 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
375 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
378 err = cancel_cb(cancel_arg);
383 err = got_privsep_recv_index_progress(&done, &nobj_total,
384 &nobj_indexed, &nobj_loose, &nobj_resolved,
388 if (nobj_indexed != 0) {
389 err = progress_cb(progress_arg, sb.st_size,
390 nobj_total, nobj_indexed, nobj_loose,
396 if (close(imsg_idxfds[0]) == -1) {
397 err = got_error_from_errno("close");
400 if (waitpid(idxpid, &idxstatus, 0) == -1) {
401 err = got_error_from_errno("waitpid");
405 if (rename(tmpidxpath, idxpath) == -1) {
406 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
413 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
414 err = got_error_from_errno2("unlink", tmpidxpath);
415 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
416 err = got_error_from_errno("close");
417 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
418 err = got_error_from_errno("close");
419 for (i = 0; i < nitems(tmpfds); i++) {
420 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
421 err = got_error_from_errno("close");
429 const struct got_error *
430 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
431 struct got_repository *repo, const char *packfile_path)
433 const struct got_error *err = NULL;
434 const char *packdir_path = NULL;
435 char *packfile_name = NULL, *p, *dot;
436 struct got_object_id id;
442 packdir_path = got_repo_get_path_objects_pack(repo);
443 if (packdir_path == NULL)
444 return got_error_from_errno("got_repo_get_path_objects_pack");
446 if (!got_path_is_child(packfile_path, packdir_path,
447 strlen(packdir_path))) {
448 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
453 err = got_path_basename(&packfile_name, packfile_path);
458 if (strncmp(p, "pack-", 5) != 0) {
459 err = got_error_fmt(GOT_ERR_BAD_PATH,
460 "'%s' is not a valid pack file name",
465 dot = strchr(p, '.');
467 err = got_error_fmt(GOT_ERR_BAD_PATH,
468 "'%s' is not a valid pack file name",
472 if (strcmp(dot + 1, "pack") != 0) {
473 err = got_error_fmt(GOT_ERR_BAD_PATH,
474 "'%s' is not a valid pack file name",
479 if (!got_parse_sha1_digest(id.sha1, p)) {
480 err = got_error_fmt(GOT_ERR_BAD_PATH,
481 "'%s' is not a valid pack file name",
486 *pack_hash = got_object_id_dup(&id);
487 if (*pack_hash == NULL) {
488 err = got_error_from_errno("got_object_id_dup");
492 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
494 err = got_error_from_errno2("open", packfile_path);
498 *packfile = fdopen(packfd, "r");
499 if (*packfile == NULL) {
500 err = got_error_from_errno2("fdopen", packfile_path);
505 if (packfd != -1 && close(packfd) == -1 && err == NULL)
506 err = got_error_from_errno2("close", packfile_path);
515 const struct got_error *
516 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
517 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
518 got_cancel_cb cancel_cb, void *cancel_arg)
520 const struct got_error *err = NULL;
521 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
522 struct got_packidx *packidx = NULL;
523 struct got_pack *pack = NULL;
526 err = got_object_id_str(&id_str, pack_hash);
530 if (asprintf(&packpath, "%s/pack-%s.pack",
531 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
532 err = got_error_from_errno("asprintf");
535 if (asprintf(&idxpath, "%s/pack-%s.idx",
536 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
537 err = got_error_from_errno("asprintf");
541 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
545 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
549 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
550 for (i = 0; i < nobj; i++) {
551 struct got_packidx_object_id *oid;
552 struct got_object_id id, base_id;
553 off_t offset, base_offset = 0;
559 err = cancel_cb(cancel_arg);
563 oid = &packidx->hdr.sorted_ids[i];
564 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
566 offset = got_packidx_get_object_offset(packidx, i);
568 err = got_error(GOT_ERR_BAD_PACKIDX);
572 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
578 case GOT_OBJ_TYPE_OFFSET_DELTA:
579 err = got_pack_parse_offset_delta(&base_offset, &len,
580 pack, offset, tslen);
584 case GOT_OBJ_TYPE_REF_DELTA:
585 err = got_pack_parse_ref_delta(&base_id,
586 pack, offset, tslen);
591 err = (*list_cb)(list_arg, &id, type, offset, size,
592 base_offset, &base_id);
602 got_packidx_close(packidx);
606 static const struct got_error *
607 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
608 void *progress_arg, struct got_ratelimit *rl,
609 int nloose, int ncommits, int npurged)
611 const struct got_error *err;
614 if (progress_cb == NULL)
617 err = got_ratelimit_check(&elapsed, rl);
621 return progress_cb(progress_arg, nloose, ncommits, npurged);
624 static const struct got_error *
625 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
626 got_cleanup_progress_cb progress_cb, void *progress_arg,
627 struct got_ratelimit *rl, struct got_repository *repo)
629 const struct got_error *err = NULL;
630 char *path_objects = NULL, *path = NULL;
632 struct got_object *obj = NULL;
633 struct got_object_id id;
638 *loose_ids = got_object_idset_alloc();
639 if (*loose_ids == NULL)
640 return got_error_from_errno("got_object_idset_alloc");
642 path_objects = got_repo_get_path_objects(repo);
643 if (path_objects == NULL) {
644 err = got_error_from_errno("got_repo_get_path_objects");
648 for (i = 0; i <= 0xff; i++) {
651 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
652 err = got_error_from_errno("asprintf");
658 if (errno == ENOENT) {
662 err = got_error_from_errno2("opendir", path);
666 while ((dent = readdir(dir)) != NULL) {
669 if (strcmp(dent->d_name, ".") == 0 ||
670 strcmp(dent->d_name, "..") == 0)
673 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
674 err = got_error_from_errno("asprintf");
678 memset(&id, 0, sizeof(id));
679 if (!got_parse_sha1_digest(id.sha1, id_str)) {
685 err = got_object_open_loose_fd(&fd, &id, repo);
688 if (fstat(fd, &sb) == -1) {
689 err = got_error_from_errno("fstat");
692 err = got_object_read_header_privsep(&obj, &id, repo,
696 fd = -1; /* already closed */
699 case GOT_OBJ_TYPE_COMMIT:
700 case GOT_OBJ_TYPE_TREE:
701 case GOT_OBJ_TYPE_BLOB:
702 case GOT_OBJ_TYPE_TAG:
705 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
709 got_object_close(obj);
711 (*ondisk_size) += sb.st_size;
712 err = got_object_idset_add(*loose_ids, &id, NULL);
715 err = report_cleanup_progress(progress_cb,
717 got_object_idset_num_elements(*loose_ids), -1, -1);
722 if (closedir(dir) != 0) {
723 err = got_error_from_errno("closedir");
732 if (dir && closedir(dir) != 0 && err == NULL)
733 err = got_error_from_errno("closedir");
734 if (fd != -1 && close(fd) == -1 && err == NULL)
735 err = got_error_from_errno("close");
737 got_object_idset_free(*loose_ids);
741 got_object_close(obj);
747 static const struct got_error *
748 preserve_loose_object(struct got_object_idset *loose_ids,
749 struct got_object_id *id, struct got_repository *repo, int *npacked)
751 const struct got_error *err = NULL;
752 struct got_object *obj;
754 if (!got_object_idset_contains(loose_ids, id))
758 * Try to open this object from a pack file. This ensures that
759 * we do in fact have a valid packed copy of the object. Otherwise
760 * we should not delete the loose representation of this object.
762 err = got_object_open_packed(&obj, id, repo);
764 got_object_close(obj);
766 * The object is referenced and packed.
767 * We can purge the redundantly stored loose object.
771 } else if (err->code != GOT_ERR_NO_OBJ)
775 * This object is referenced and not packed.
776 * Remove it from our purge set.
778 return got_object_idset_remove(NULL, loose_ids, id);
781 static const struct got_error *
782 load_tree_entries(struct got_object_id_queue *ids,
783 struct got_object_idset *loose_ids,
784 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
785 const char *dpath, struct got_repository *repo, int *npacked,
786 got_cancel_cb cancel_cb, void *cancel_arg)
788 const struct got_error *err;
789 struct got_tree_object *tree;
793 err = got_object_open_as_tree(&tree, repo, tree_id);
797 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
798 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
799 struct got_object_id *id = got_tree_entry_get_id(e);
800 mode_t mode = got_tree_entry_get_mode(e);
803 err = (*cancel_cb)(cancel_arg);
808 if (got_object_tree_entry_is_symlink(e) ||
809 got_object_tree_entry_is_submodule(e) ||
810 got_object_idset_contains(traversed_ids, id))
813 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
814 got_tree_entry_get_name(e)) == -1) {
815 err = got_error_from_errno("asprintf");
820 struct got_object_qid *qid;
821 err = got_object_qid_alloc(&qid, id);
824 STAILQ_INSERT_TAIL(ids, qid, entry);
825 } else if (S_ISREG(mode)) {
826 /* This blob is referenced. */
827 err = preserve_loose_object(loose_ids, id, repo,
831 err = got_object_idset_add(traversed_ids, id, NULL);
840 got_object_tree_close(tree);
845 static const struct got_error *
846 load_tree(struct got_object_idset *loose_ids,
847 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
848 const char *dpath, struct got_repository *repo, int *npacked,
849 got_cancel_cb cancel_cb, void *cancel_arg)
851 const struct got_error *err = NULL;
852 struct got_object_id_queue tree_ids;
853 struct got_object_qid *qid;
855 err = got_object_qid_alloc(&qid, tree_id);
859 STAILQ_INIT(&tree_ids);
860 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
862 while (!STAILQ_EMPTY(&tree_ids)) {
864 err = (*cancel_cb)(cancel_arg);
869 qid = STAILQ_FIRST(&tree_ids);
870 STAILQ_REMOVE_HEAD(&tree_ids, entry);
872 if (got_object_idset_contains(traversed_ids, &qid->id)) {
873 got_object_qid_free(qid);
877 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
879 got_object_qid_free(qid);
883 /* This tree is referenced. */
884 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
888 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
889 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
890 got_object_qid_free(qid);
895 got_object_id_queue_free(&tree_ids);
899 static const struct got_error *
900 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
901 int *npacked, struct got_object_idset *traversed_ids,
902 struct got_object_id *id, struct got_repository *repo,
903 got_cleanup_progress_cb progress_cb, void *progress_arg,
904 struct got_ratelimit *rl, int nloose,
905 got_cancel_cb cancel_cb, void *cancel_arg)
907 const struct got_error *err;
908 struct got_commit_object *commit = NULL;
909 struct got_tag_object *tag = NULL;
910 struct got_object_id *tree_id = NULL;
911 struct got_object_id_queue ids;
912 struct got_object_qid *qid;
915 err = got_object_qid_alloc(&qid, id);
920 STAILQ_INSERT_TAIL(&ids, qid, entry);
922 while (!STAILQ_EMPTY(&ids)) {
924 err = (*cancel_cb)(cancel_arg);
929 qid = STAILQ_FIRST(&ids);
930 STAILQ_REMOVE_HEAD(&ids, entry);
932 if (got_object_idset_contains(traversed_ids, &qid->id)) {
933 got_object_qid_free(qid);
938 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
942 /* This commit or tag is referenced. */
943 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
947 err = got_object_get_type(&obj_type, repo, &qid->id);
951 case GOT_OBJ_TYPE_COMMIT:
952 err = got_object_open_as_commit(&commit, repo,
957 case GOT_OBJ_TYPE_TAG:
958 err = got_object_open_as_tag(&tag, repo, &qid->id);
963 /* should not happen */
964 err = got_error(GOT_ERR_OBJ_TYPE);
968 /* Find a tree object to scan. */
970 tree_id = got_object_commit_get_tree_id(commit);
972 obj_type = got_object_tag_get_object_type(tag);
974 case GOT_OBJ_TYPE_COMMIT:
975 err = got_object_open_as_commit(&commit, repo,
976 got_object_tag_get_object_id(tag));
979 tree_id = got_object_commit_get_tree_id(commit);
981 case GOT_OBJ_TYPE_TREE:
982 tree_id = got_object_tag_get_object_id(tag);
986 * Tag points at something other than a
987 * commit or tree. Leave this weird tag object
988 * and the object it points to on disk.
990 err = got_object_idset_remove(NULL, loose_ids,
992 if (err && err->code != GOT_ERR_NO_OBJ)
994 err = got_object_idset_remove(NULL, loose_ids,
995 got_object_tag_get_object_id(tag));
996 if (err && err->code != GOT_ERR_NO_OBJ)
1004 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1005 repo, npacked, cancel_cb, cancel_arg);
1011 (*ncommits)++; /* scanned tags are counted as commits */
1013 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1014 nloose, *ncommits, -1);
1019 /* Find parent commits to scan. */
1020 const struct got_object_id_queue *parent_ids;
1021 parent_ids = got_object_commit_get_parent_ids(commit);
1022 err = got_object_id_queue_copy(parent_ids, &ids);
1025 got_object_commit_close(commit);
1029 got_object_tag_close(tag);
1032 got_object_qid_free(qid);
1037 got_object_qid_free(qid);
1039 got_object_commit_close(commit);
1041 got_object_tag_close(tag);
1042 got_object_id_queue_free(&ids);
1046 struct purge_loose_object_arg {
1047 struct got_repository *repo;
1048 got_cleanup_progress_cb progress_cb;
1050 struct got_ratelimit *rl;
1060 static const struct got_error *
1061 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1063 struct purge_loose_object_arg *a = arg;
1064 const struct got_error *err, *unlock_err = NULL;
1068 struct got_lockfile *lf = NULL;
1070 err = got_object_get_path(&path, id, a->repo);
1074 err = got_object_open_loose_fd(&fd, id, a->repo);
1078 if (fstat(fd, &sb) == -1) {
1079 err = got_error_from_errno("fstat");
1084 * Do not delete objects which are younger than our maximum
1085 * modification time threshold. This prevents a race where
1086 * new objects which are being added to the repository
1087 * concurrently would be deleted.
1089 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1091 err = got_lockfile_lock(&lf, path, -1);
1094 if (unlink(path) == -1) {
1095 err = got_error_from_errno2("unlink", path);
1101 a->size_purged += sb.st_size;
1102 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1103 a->rl, a->nloose, a->ncommits, a->npurged);
1108 if (fd != -1 && close(fd) == -1 && err == NULL)
1109 err = got_error_from_errno("close");
1112 unlock_err = got_lockfile_unlock(lf, -1);
1113 return err ? err : unlock_err;
1116 const struct got_error *
1117 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1118 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1119 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1120 got_cancel_cb cancel_cb, void *cancel_arg)
1122 const struct got_error *err;
1123 struct got_object_idset *loose_ids;
1124 struct got_object_idset *traversed_ids;
1125 struct got_object_id **referenced_ids;
1126 int i, nreferenced, nloose, ncommits = 0;
1127 struct got_reflist_head refs;
1128 struct got_reflist_entry *re;
1129 struct purge_loose_object_arg arg;
1130 time_t max_mtime = 0;
1131 struct got_ratelimit rl;
1134 got_ratelimit_init(&rl, 0, 500);
1140 err = get_loose_object_ids(&loose_ids, size_before,
1141 progress_cb, progress_arg, &rl, repo);
1144 nloose = got_object_idset_num_elements(loose_ids);
1146 got_object_idset_free(loose_ids);
1148 err = progress_cb(progress_arg, 0, 0, 0);
1155 traversed_ids = got_object_idset_alloc();
1156 if (traversed_ids == NULL) {
1157 err = got_error_from_errno("got_object_idset_alloc");
1161 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1164 if (!ignore_mtime) {
1165 TAILQ_FOREACH(re, &refs, entry) {
1166 time_t mtime = got_ref_get_mtime(re->ref);
1167 if (mtime > max_mtime)
1171 * For safety, keep objects created within 10 minutes
1172 * before the youngest reference was created.
1174 if (max_mtime >= 600)
1178 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1179 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1180 &refs, repo, cancel_cb, cancel_arg);
1184 for (i = 0; i < nreferenced; i++) {
1185 struct got_object_id *id = referenced_ids[i];
1186 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1187 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1188 nloose, cancel_cb, cancel_arg);
1193 /* Any remaining loose objects are unreferenced and can be purged. */
1195 arg.progress_arg = progress_arg;
1196 arg.progress_cb = progress_cb;
1198 arg.nloose = nloose;
1200 arg.size_purged = 0;
1201 arg.ncommits = ncommits;
1202 arg.dry_run = dry_run;
1203 arg.max_mtime = max_mtime;
1204 arg.ignore_mtime = ignore_mtime;
1205 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1208 *size_after = *size_before - arg.size_purged;
1210 /* Produce a final progress report. */
1212 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1217 got_object_idset_free(loose_ids);
1218 got_object_idset_free(traversed_ids);
1222 static const struct got_error *
1223 remove_packidx(int dir_fd, const char *relpath)
1225 const struct got_error *err, *unlock_err;
1226 struct got_lockfile *lf;
1228 err = got_lockfile_lock(&lf, relpath, dir_fd);
1231 if (unlinkat(dir_fd, relpath, 0) == -1)
1232 err = got_error_from_errno("unlinkat");
1233 unlock_err = got_lockfile_unlock(lf, dir_fd);
1234 return err ? err : unlock_err;
1237 const struct got_error *
1238 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1239 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1240 got_cancel_cb cancel_cb, void *cancel_arg)
1242 const struct got_error *err;
1243 DIR *packdir = NULL;
1244 struct dirent *dent;
1245 char *pack_relpath = NULL;
1249 packdir_fd = openat(got_repo_get_fd(repo),
1250 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1251 if (packdir_fd == -1) {
1252 if (errno == ENOENT)
1254 return got_error_from_errno_fmt("openat: %s/%s",
1255 got_repo_get_path_git_dir(repo),
1256 GOT_OBJECTS_PACK_DIR);
1259 packdir = fdopendir(packdir_fd);
1260 if (packdir == NULL) {
1261 err = got_error_from_errno("fdopendir");
1265 while ((dent = readdir(packdir)) != NULL) {
1267 err = cancel_cb(cancel_arg);
1272 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1275 err = got_packidx_get_packfile_path(&pack_relpath,
1280 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1282 pack_relpath = NULL;
1285 if (errno != ENOENT) {
1286 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1287 got_repo_get_path_git_dir(repo),
1288 GOT_OBJECTS_PACK_DIR,
1294 err = remove_packidx(packdir_fd, dent->d_name);
1300 if (asprintf(&path, "%s/%s/%s",
1301 got_repo_get_path_git_dir(repo),
1302 GOT_OBJECTS_PACK_DIR,
1303 dent->d_name) == -1) {
1304 err = got_error_from_errno("asprintf");
1307 err = progress_cb(progress_arg, path);
1313 pack_relpath = NULL;
1316 if (packdir && closedir(packdir) != 0 && err == NULL)
1317 err = got_error_from_errno("closedir");