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,
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)->hash, packfd, delta_cache,
213 theirs, ntheirs, ours, nours, repo, loose_obj_only, 0,
214 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
218 err = got_object_id_str(&sha1_str, *pack_hash);
221 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
222 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
224 err = got_error_from_errno("asprintf");
228 if (lseek(packfd, 0L, SEEK_SET) == -1) {
229 err = got_error_from_errno("lseek");
232 if (rename(tmpfile_path, packfile_path) == -1) {
233 err = got_error_from_errno3("rename", tmpfile_path,
240 *packfile = fdopen(packfd, "w");
241 if (*packfile == NULL) {
242 err = got_error_from_errno2("fdopen", tmpfile_path);
247 for (i = 0; i < nours; i++)
250 for (i = 0; i < ntheirs; i++)
253 if (packfd != -1 && close(packfd) == -1 && err == NULL)
254 err = got_error_from_errno2("close", packfile_path);
255 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
256 err = got_error_from_errno("fclose");
257 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
258 err = got_error_from_errno2("unlink", tmpfile_path);
273 const struct got_error *
274 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
275 struct got_repository *repo,
276 got_pack_index_progress_cb progress_cb, void *progress_arg,
277 got_cancel_cb cancel_cb, void *cancel_arg)
282 int npackfd = -1, idxfd = -1, nidxfd = -1;
284 int idxstatus, done = 0;
285 const struct got_error *err;
286 struct imsgbuf idxibuf;
288 char *tmpidxpath = NULL;
289 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
290 const char *repo_path = got_repo_get_path_git_dir(repo);
293 for (i = 0; i < nitems(tmpfds); i++)
296 if (asprintf(&path, "%s/%s/indexing.idx",
297 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
298 err = got_error_from_errno("asprintf");
301 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
305 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
306 err = got_error_from_errno2("fchmod", tmpidxpath);
312 err = got_error_from_errno("dup");
316 for (i = 0; i < nitems(tmpfds); i++) {
317 tmpfds[i] = got_opentempfd();
318 if (tmpfds[i] == -1) {
319 err = got_error_from_errno("got_opentempfd");
324 err = got_object_id_str(&id_str, pack_hash);
328 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
329 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
330 err = got_error_from_errno("asprintf");
334 if (fstat(fileno(packfile), &sb) == -1) {
335 err = got_error_from_errno2("fstat", packfile_path);
339 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
340 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
341 err = got_error_from_errno("asprintf");
345 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
346 err = got_error_from_errno("socketpair");
351 err= got_error_from_errno("fork");
353 } else if (idxpid == 0)
354 got_privsep_exec_child(imsg_idxfds,
355 GOT_PATH_PROG_INDEX_PACK, packfile_path);
356 if (close(imsg_idxfds[1]) == -1) {
357 err = got_error_from_errno("close");
360 imsg_init(&idxibuf, imsg_idxfds[0]);
362 npackfd = dup(fileno(packfile));
364 err = got_error_from_errno("dup");
367 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->hash,
372 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
376 for (i = 0; i < nitems(tmpfds); i++) {
377 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
384 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
387 err = cancel_cb(cancel_arg);
392 err = got_privsep_recv_index_progress(&done, &nobj_total,
393 &nobj_indexed, &nobj_loose, &nobj_resolved,
397 if (nobj_indexed != 0) {
398 err = progress_cb(progress_arg, sb.st_size,
399 nobj_total, nobj_indexed, nobj_loose,
405 if (close(imsg_idxfds[0]) == -1) {
406 err = got_error_from_errno("close");
409 if (waitpid(idxpid, &idxstatus, 0) == -1) {
410 err = got_error_from_errno("waitpid");
414 if (rename(tmpidxpath, idxpath) == -1) {
415 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
422 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
423 err = got_error_from_errno2("unlink", tmpidxpath);
424 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
425 err = got_error_from_errno("close");
426 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
427 err = got_error_from_errno("close");
428 for (i = 0; i < nitems(tmpfds); i++) {
429 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
430 err = got_error_from_errno("close");
438 const struct got_error *
439 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
440 struct got_repository *repo, const char *packfile_path)
442 const struct got_error *err = NULL;
443 const char *packdir_path = NULL;
444 char *packfile_name = NULL, *p, *dot;
445 struct got_object_id id;
451 packdir_path = got_repo_get_path_objects_pack(repo);
452 if (packdir_path == NULL)
453 return got_error_from_errno("got_repo_get_path_objects_pack");
455 if (!got_path_is_child(packfile_path, packdir_path,
456 strlen(packdir_path))) {
457 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
462 err = got_path_basename(&packfile_name, packfile_path);
467 if (strncmp(p, "pack-", 5) != 0) {
468 err = got_error_fmt(GOT_ERR_BAD_PATH,
469 "'%s' is not a valid pack file name",
474 dot = strchr(p, '.');
476 err = got_error_fmt(GOT_ERR_BAD_PATH,
477 "'%s' is not a valid pack file name",
481 if (strcmp(dot + 1, "pack") != 0) {
482 err = got_error_fmt(GOT_ERR_BAD_PATH,
483 "'%s' is not a valid pack file name",
489 memset(&id, 0, sizeof(id));
490 id.algo = repo->algo;
491 if (!got_parse_hash_digest(id.hash, p, repo->algo)) {
492 err = got_error_fmt(GOT_ERR_BAD_PATH,
493 "'%s' is not a valid pack file name",
498 *pack_hash = got_object_id_dup(&id);
499 if (*pack_hash == NULL) {
500 err = got_error_from_errno("got_object_id_dup");
504 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
506 err = got_error_from_errno2("open", packfile_path);
510 *packfile = fdopen(packfd, "r");
511 if (*packfile == NULL) {
512 err = got_error_from_errno2("fdopen", packfile_path);
517 if (packfd != -1 && close(packfd) == -1 && err == NULL)
518 err = got_error_from_errno2("close", packfile_path);
527 const struct got_error *
528 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
529 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
530 got_cancel_cb cancel_cb, void *cancel_arg)
532 const struct got_error *err = NULL;
533 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
534 struct got_packidx *packidx = NULL;
535 struct got_pack *pack = NULL;
539 idlen = got_hash_digest_length(repo->algo);
541 err = got_object_id_str(&id_str, pack_hash);
545 if (asprintf(&packpath, "%s/pack-%s.pack",
546 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
547 err = got_error_from_errno("asprintf");
550 if (asprintf(&idxpath, "%s/pack-%s.idx",
551 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
552 err = got_error_from_errno("asprintf");
556 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1,
561 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
565 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
566 for (i = 0; i < nobj; i++) {
567 struct got_packidx_object_id *oid;
568 struct got_object_id id, base_id;
569 off_t offset, base_offset = 0;
575 err = cancel_cb(cancel_arg);
579 oid = packidx->hdr.sorted_ids + i * idlen;
580 memset(&id, 0, sizeof(id));
581 id.algo = repo->algo;
582 memcpy(id.hash, oid->hash, idlen);
584 offset = got_packidx_get_object_offset(packidx, i);
586 err = got_error(GOT_ERR_BAD_PACKIDX);
590 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
596 case GOT_OBJ_TYPE_OFFSET_DELTA:
597 err = got_pack_parse_offset_delta(&base_offset, &len,
598 pack, offset, tslen);
602 case GOT_OBJ_TYPE_REF_DELTA:
603 err = got_pack_parse_ref_delta(&base_id,
604 pack, offset, tslen);
609 err = (*list_cb)(list_arg, &id, type, offset, size,
610 base_offset, &base_id);
620 got_packidx_close(packidx);
624 static const struct got_error *
625 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
626 void *progress_arg, struct got_ratelimit *rl,
627 int nloose, int ncommits, int npurged)
629 const struct got_error *err;
632 if (progress_cb == NULL)
635 err = got_ratelimit_check(&elapsed, rl);
639 return progress_cb(progress_arg, nloose, ncommits, npurged);
642 static const struct got_error *
643 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
644 got_cleanup_progress_cb progress_cb, void *progress_arg,
645 struct got_ratelimit *rl, struct got_repository *repo)
647 const struct got_error *err = NULL;
648 char *path_objects = NULL, *path = NULL;
650 struct got_object *obj = NULL;
651 struct got_object_id id;
656 *loose_ids = got_object_idset_alloc();
657 if (*loose_ids == NULL)
658 return got_error_from_errno("got_object_idset_alloc");
660 path_objects = got_repo_get_path_objects(repo);
661 if (path_objects == NULL) {
662 err = got_error_from_errno("got_repo_get_path_objects");
666 for (i = 0; i <= 0xff; i++) {
669 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
670 err = got_error_from_errno("asprintf");
676 if (errno == ENOENT) {
680 err = got_error_from_errno2("opendir", path);
684 while ((dent = readdir(dir)) != NULL) {
687 if (strcmp(dent->d_name, ".") == 0 ||
688 strcmp(dent->d_name, "..") == 0)
691 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
692 err = got_error_from_errno("asprintf");
696 memset(&id, 0, sizeof(id));
697 id.algo = repo->algo;
698 if (!got_parse_hash_digest(id.hash, id_str,
705 err = got_object_open_loose_fd(&fd, &id, repo);
708 if (fstat(fd, &sb) == -1) {
709 err = got_error_from_errno("fstat");
712 err = got_object_read_header_privsep(&obj, &id, repo,
716 fd = -1; /* already closed */
719 case GOT_OBJ_TYPE_COMMIT:
720 case GOT_OBJ_TYPE_TREE:
721 case GOT_OBJ_TYPE_BLOB:
722 case GOT_OBJ_TYPE_TAG:
725 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
729 got_object_close(obj);
731 (*ondisk_size) += sb.st_size;
732 err = got_object_idset_add(*loose_ids, &id, NULL);
735 err = report_cleanup_progress(progress_cb,
737 got_object_idset_num_elements(*loose_ids), -1, -1);
742 if (closedir(dir) != 0) {
743 err = got_error_from_errno("closedir");
752 if (dir && closedir(dir) != 0 && err == NULL)
753 err = got_error_from_errno("closedir");
754 if (fd != -1 && close(fd) == -1 && err == NULL)
755 err = got_error_from_errno("close");
757 got_object_idset_free(*loose_ids);
761 got_object_close(obj);
767 static const struct got_error *
768 preserve_loose_object(struct got_object_idset *loose_ids,
769 struct got_object_id *id, struct got_repository *repo, int *npacked)
771 const struct got_error *err = NULL;
772 struct got_object *obj;
774 if (!got_object_idset_contains(loose_ids, id))
778 * Try to open this object from a pack file. This ensures that
779 * we do in fact have a valid packed copy of the object. Otherwise
780 * we should not delete the loose representation of this object.
782 err = got_object_open_packed(&obj, id, repo);
784 got_object_close(obj);
786 * The object is referenced and packed.
787 * We can purge the redundantly stored loose object.
791 } else if (err->code != GOT_ERR_NO_OBJ)
795 * This object is referenced and not packed.
796 * Remove it from our purge set.
798 return got_object_idset_remove(NULL, loose_ids, id);
801 static const struct got_error *
802 load_tree_entries(struct got_object_id_queue *ids,
803 struct got_object_idset *loose_ids,
804 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
805 const char *dpath, struct got_repository *repo, int *npacked,
806 got_cancel_cb cancel_cb, void *cancel_arg)
808 const struct got_error *err;
809 struct got_tree_object *tree;
813 err = got_object_open_as_tree(&tree, repo, tree_id);
817 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
818 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
819 struct got_object_id *id = got_tree_entry_get_id(e);
820 mode_t mode = got_tree_entry_get_mode(e);
823 err = (*cancel_cb)(cancel_arg);
828 if (got_object_tree_entry_is_symlink(e) ||
829 got_object_tree_entry_is_submodule(e) ||
830 got_object_idset_contains(traversed_ids, id))
833 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
834 got_tree_entry_get_name(e)) == -1) {
835 err = got_error_from_errno("asprintf");
840 struct got_object_qid *qid;
841 err = got_object_qid_alloc(&qid, id);
844 STAILQ_INSERT_TAIL(ids, qid, entry);
845 } else if (S_ISREG(mode)) {
846 /* This blob is referenced. */
847 err = preserve_loose_object(loose_ids, id, repo,
851 err = got_object_idset_add(traversed_ids, id, NULL);
859 got_object_tree_close(tree);
864 static const struct got_error *
865 load_tree(struct got_object_idset *loose_ids,
866 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
867 const char *dpath, struct got_repository *repo, int *npacked,
868 got_cancel_cb cancel_cb, void *cancel_arg)
870 const struct got_error *err = NULL;
871 struct got_object_id_queue tree_ids;
872 struct got_object_qid *qid;
874 err = got_object_qid_alloc(&qid, tree_id);
878 STAILQ_INIT(&tree_ids);
879 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
881 while (!STAILQ_EMPTY(&tree_ids)) {
883 err = (*cancel_cb)(cancel_arg);
888 qid = STAILQ_FIRST(&tree_ids);
889 STAILQ_REMOVE_HEAD(&tree_ids, entry);
891 if (got_object_idset_contains(traversed_ids, &qid->id)) {
892 got_object_qid_free(qid);
896 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
898 got_object_qid_free(qid);
902 /* This tree is referenced. */
903 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
907 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
908 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
909 got_object_qid_free(qid);
914 got_object_id_queue_free(&tree_ids);
918 static const struct got_error *
919 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
920 int *npacked, struct got_object_idset *traversed_ids,
921 struct got_object_id *id, struct got_repository *repo,
922 got_cleanup_progress_cb progress_cb, void *progress_arg,
923 struct got_ratelimit *rl, int nloose,
924 got_cancel_cb cancel_cb, void *cancel_arg)
926 const struct got_error *err;
927 struct got_commit_object *commit = NULL;
928 struct got_tag_object *tag = NULL;
929 struct got_object_id *tree_id = NULL;
930 struct got_object_id_queue ids;
931 struct got_object_qid *qid;
934 err = got_object_qid_alloc(&qid, id);
939 STAILQ_INSERT_TAIL(&ids, qid, entry);
941 while (!STAILQ_EMPTY(&ids)) {
943 err = (*cancel_cb)(cancel_arg);
948 qid = STAILQ_FIRST(&ids);
949 STAILQ_REMOVE_HEAD(&ids, entry);
951 if (got_object_idset_contains(traversed_ids, &qid->id)) {
952 got_object_qid_free(qid);
957 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
961 /* This commit or tag is referenced. */
962 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
966 err = got_object_get_type(&obj_type, repo, &qid->id);
970 case GOT_OBJ_TYPE_COMMIT:
971 err = got_object_open_as_commit(&commit, repo,
976 case GOT_OBJ_TYPE_TAG:
977 err = got_object_open_as_tag(&tag, repo, &qid->id);
982 /* should not happen */
983 err = got_error(GOT_ERR_OBJ_TYPE);
987 /* Find a tree object to scan. */
989 tree_id = got_object_commit_get_tree_id(commit);
991 obj_type = got_object_tag_get_object_type(tag);
993 case GOT_OBJ_TYPE_COMMIT:
994 err = got_object_open_as_commit(&commit, repo,
995 got_object_tag_get_object_id(tag));
998 tree_id = got_object_commit_get_tree_id(commit);
1000 case GOT_OBJ_TYPE_TREE:
1001 tree_id = got_object_tag_get_object_id(tag);
1005 * Tag points at something other than a
1006 * commit or tree. Leave this weird tag object
1007 * and the object it points to on disk.
1009 err = got_object_idset_remove(NULL, loose_ids,
1011 if (err && err->code != GOT_ERR_NO_OBJ)
1013 err = got_object_idset_remove(NULL, loose_ids,
1014 got_object_tag_get_object_id(tag));
1015 if (err && err->code != GOT_ERR_NO_OBJ)
1023 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1024 repo, npacked, cancel_cb, cancel_arg);
1030 (*ncommits)++; /* scanned tags are counted as commits */
1032 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1033 nloose, *ncommits, -1);
1038 /* Find parent commits to scan. */
1039 const struct got_object_id_queue *parent_ids;
1040 parent_ids = got_object_commit_get_parent_ids(commit);
1041 err = got_object_id_queue_copy(parent_ids, &ids);
1044 got_object_commit_close(commit);
1048 got_object_tag_close(tag);
1051 got_object_qid_free(qid);
1056 got_object_qid_free(qid);
1058 got_object_commit_close(commit);
1060 got_object_tag_close(tag);
1061 got_object_id_queue_free(&ids);
1065 struct purge_loose_object_arg {
1066 struct got_repository *repo;
1067 got_cleanup_progress_cb progress_cb;
1069 struct got_ratelimit *rl;
1079 static const struct got_error *
1080 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1082 struct purge_loose_object_arg *a = arg;
1083 const struct got_error *err, *unlock_err = NULL;
1087 struct got_lockfile *lf = NULL;
1089 err = got_object_get_path(&path, id, a->repo);
1093 err = got_object_open_loose_fd(&fd, id, a->repo);
1097 if (fstat(fd, &sb) == -1) {
1098 err = got_error_from_errno("fstat");
1103 * Do not delete objects which are younger than our maximum
1104 * modification time threshold. This prevents a race where
1105 * new objects which are being added to the repository
1106 * concurrently would be deleted.
1108 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1110 err = got_lockfile_lock(&lf, path, -1);
1113 if (unlink(path) == -1) {
1114 err = got_error_from_errno2("unlink", path);
1120 a->size_purged += sb.st_size;
1121 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1122 a->rl, a->nloose, a->ncommits, a->npurged);
1127 if (fd != -1 && close(fd) == -1 && err == NULL)
1128 err = got_error_from_errno("close");
1131 unlock_err = got_lockfile_unlock(lf, -1);
1132 return err ? err : unlock_err;
1135 const struct got_error *
1136 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1137 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1138 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1139 got_cancel_cb cancel_cb, void *cancel_arg)
1141 const struct got_error *err;
1142 struct got_object_idset *loose_ids;
1143 struct got_object_idset *traversed_ids;
1144 struct got_object_id **referenced_ids;
1145 int i, nreferenced, nloose, ncommits = 0;
1146 struct got_reflist_head refs;
1147 struct got_reflist_entry *re;
1148 struct purge_loose_object_arg arg;
1149 time_t max_mtime = 0;
1150 struct got_ratelimit rl;
1153 got_ratelimit_init(&rl, 0, 500);
1159 err = get_loose_object_ids(&loose_ids, size_before,
1160 progress_cb, progress_arg, &rl, repo);
1163 nloose = got_object_idset_num_elements(loose_ids);
1165 got_object_idset_free(loose_ids);
1167 err = progress_cb(progress_arg, 0, 0, 0);
1174 traversed_ids = got_object_idset_alloc();
1175 if (traversed_ids == NULL) {
1176 err = got_error_from_errno("got_object_idset_alloc");
1180 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1183 if (!ignore_mtime) {
1184 TAILQ_FOREACH(re, &refs, entry) {
1185 time_t mtime = got_ref_get_mtime(re->ref);
1186 if (mtime > max_mtime)
1190 * For safety, keep objects created within 10 minutes
1191 * before the youngest reference was created.
1193 if (max_mtime >= 600)
1197 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1198 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1199 &refs, repo, cancel_cb, cancel_arg);
1203 for (i = 0; i < nreferenced; i++) {
1204 struct got_object_id *id = referenced_ids[i];
1205 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1206 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1207 nloose, cancel_cb, cancel_arg);
1212 /* Any remaining loose objects are unreferenced and can be purged. */
1214 arg.progress_arg = progress_arg;
1215 arg.progress_cb = progress_cb;
1217 arg.nloose = nloose;
1219 arg.size_purged = 0;
1220 arg.ncommits = ncommits;
1221 arg.dry_run = dry_run;
1222 arg.max_mtime = max_mtime;
1223 arg.ignore_mtime = ignore_mtime;
1224 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1227 *size_after = *size_before - arg.size_purged;
1229 /* Produce a final progress report. */
1231 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1236 got_object_idset_free(loose_ids);
1237 got_object_idset_free(traversed_ids);
1241 static const struct got_error *
1242 remove_packidx(int dir_fd, const char *relpath)
1244 const struct got_error *err, *unlock_err;
1245 struct got_lockfile *lf;
1247 err = got_lockfile_lock(&lf, relpath, dir_fd);
1250 if (unlinkat(dir_fd, relpath, 0) == -1)
1251 err = got_error_from_errno("unlinkat");
1252 unlock_err = got_lockfile_unlock(lf, dir_fd);
1253 return err ? err : unlock_err;
1256 const struct got_error *
1257 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1258 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1259 got_cancel_cb cancel_cb, void *cancel_arg)
1261 const struct got_error *err = NULL;
1262 DIR *packdir = NULL;
1263 struct dirent *dent;
1264 char *pack_relpath = NULL;
1268 packdir_fd = openat(got_repo_get_fd(repo),
1269 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1270 if (packdir_fd == -1) {
1271 if (errno == ENOENT)
1273 return got_error_from_errno_fmt("openat: %s/%s",
1274 got_repo_get_path_git_dir(repo),
1275 GOT_OBJECTS_PACK_DIR);
1278 packdir = fdopendir(packdir_fd);
1279 if (packdir == NULL) {
1280 err = got_error_from_errno("fdopendir");
1284 while ((dent = readdir(packdir)) != NULL) {
1286 err = cancel_cb(cancel_arg);
1291 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen,
1292 got_repo_get_object_format(repo)))
1295 err = got_packidx_get_packfile_path(&pack_relpath,
1300 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1302 pack_relpath = NULL;
1305 if (errno != ENOENT) {
1306 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1307 got_repo_get_path_git_dir(repo),
1308 GOT_OBJECTS_PACK_DIR,
1314 err = remove_packidx(packdir_fd, dent->d_name);
1320 if (asprintf(&path, "%s/%s/%s",
1321 got_repo_get_path_git_dir(repo),
1322 GOT_OBJECTS_PACK_DIR,
1323 dent->d_name) == -1) {
1324 err = got_error_from_errno("asprintf");
1327 err = progress_cb(progress_arg, path);
1333 pack_relpath = NULL;
1336 if (packdir && closedir(packdir) != 0 && err == NULL)
1337 err = got_error_from_errno("closedir");