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_pack_create.h"
51 #include "got_lib_sha1.h"
52 #include "got_lib_lockfile.h"
53 #include "got_lib_ratelimit.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;
157 if (asprintf(&path, "%s/%s/packing.pack",
158 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
159 err = got_error_from_errno("asprintf");
162 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
166 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
167 err = got_error_from_errno2("fchmod", tmpfile_path);
171 *packfile = fdopen(packfd, "w");
172 if (*packfile == NULL) {
173 err = got_error_from_errno2("fdopen", tmpfile_path);
178 err = get_reflist_object_ids(&ours, &nours,
179 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
180 include_refs, repo, cancel_cb, cancel_arg);
185 err = got_error(GOT_ERR_CANNOT_PACK);
189 if (!TAILQ_EMPTY(exclude_refs)) {
190 err = get_reflist_object_ids(&theirs, &ntheirs,
191 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
193 cancel_cb, cancel_arg);
198 *pack_hash = calloc(1, sizeof(**pack_hash));
199 if (*pack_hash == NULL) {
200 err = got_error_from_errno("calloc");
204 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
205 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
206 cancel_cb, cancel_arg);
210 err = got_object_id_str(&sha1_str, *pack_hash);
213 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
214 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
216 err = got_error_from_errno("asprintf");
220 if (fflush(*packfile) == -1) {
221 err = got_error_from_errno("fflush");
224 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
225 err = got_error_from_errno("fseek");
228 if (rename(tmpfile_path, packfile_path) == -1) {
229 err = got_error_from_errno3("rename", tmpfile_path,
236 for (i = 0; i < nours; i++)
239 for (i = 0; i < ntheirs; i++)
242 if (packfd != -1 && close(packfd) == -1 && err == NULL)
243 err = got_error_from_errno2("close", packfile_path);
244 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
245 err = got_error_from_errno2("unlink", tmpfile_path);
260 const struct got_error *
261 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
262 struct got_repository *repo,
263 got_pack_index_progress_cb progress_cb, void *progress_arg,
264 got_cancel_cb cancel_cb, void *cancel_arg)
269 int npackfd = -1, idxfd = -1, nidxfd = -1;
271 int idxstatus, done = 0;
272 const struct got_error *err;
273 struct imsgbuf idxibuf;
275 char *tmpidxpath = NULL;
276 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
277 const char *repo_path = got_repo_get_path_git_dir(repo);
280 for (i = 0; i < nitems(tmpfds); i++)
283 if (asprintf(&path, "%s/%s/indexing.idx",
284 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
285 err = got_error_from_errno("asprintf");
288 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
292 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
293 err = got_error_from_errno2("fchmod", tmpidxpath);
299 err = got_error_from_errno("dup");
303 for (i = 0; i < nitems(tmpfds); i++) {
304 tmpfds[i] = got_opentempfd();
305 if (tmpfds[i] == -1) {
306 err = got_error_from_errno("got_opentempfd");
311 err = got_object_id_str(&id_str, pack_hash);
315 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
316 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
317 err = got_error_from_errno("asprintf");
321 if (fstat(fileno(packfile), &sb) == -1) {
322 err = got_error_from_errno2("fstat", packfile_path);
326 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
327 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
328 err = got_error_from_errno("asprintf");
332 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
333 err = got_error_from_errno("socketpair");
338 err= got_error_from_errno("fork");
340 } else if (idxpid == 0)
341 got_privsep_exec_child(imsg_idxfds,
342 GOT_PATH_PROG_INDEX_PACK, packfile_path);
343 if (close(imsg_idxfds[1]) == -1) {
344 err = got_error_from_errno("close");
347 imsg_init(&idxibuf, imsg_idxfds[0]);
349 npackfd = dup(fileno(packfile));
351 err = got_error_from_errno("dup");
354 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
359 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
363 for (i = 0; i < nitems(tmpfds); i++) {
364 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
371 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
374 err = cancel_cb(cancel_arg);
379 err = got_privsep_recv_index_progress(&done, &nobj_total,
380 &nobj_indexed, &nobj_loose, &nobj_resolved,
384 if (nobj_indexed != 0) {
385 err = progress_cb(progress_arg, sb.st_size,
386 nobj_total, nobj_indexed, nobj_loose,
392 if (close(imsg_idxfds[0]) == -1) {
393 err = got_error_from_errno("close");
396 if (waitpid(idxpid, &idxstatus, 0) == -1) {
397 err = got_error_from_errno("waitpid");
401 if (rename(tmpidxpath, idxpath) == -1) {
402 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
409 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
410 err = got_error_from_errno2("unlink", tmpidxpath);
411 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
412 err = got_error_from_errno("close");
413 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
414 err = got_error_from_errno("close");
415 for (i = 0; i < nitems(tmpfds); i++) {
416 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
417 err = got_error_from_errno("close");
425 const struct got_error *
426 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
427 struct got_repository *repo, const char *packfile_path)
429 const struct got_error *err = NULL;
430 const char *packdir_path = NULL;
431 char *packfile_name = NULL, *p, *dot;
432 struct got_object_id id;
438 packdir_path = got_repo_get_path_objects_pack(repo);
439 if (packdir_path == NULL)
440 return got_error_from_errno("got_repo_get_path_objects_pack");
442 if (!got_path_is_child(packfile_path, packdir_path,
443 strlen(packdir_path))) {
444 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
449 err = got_path_basename(&packfile_name, packfile_path);
454 if (strncmp(p, "pack-", 5) != 0) {
455 err = got_error_fmt(GOT_ERR_BAD_PATH,
456 "'%s' is not a valid pack file name",
461 dot = strchr(p, '.');
463 err = got_error_fmt(GOT_ERR_BAD_PATH,
464 "'%s' is not a valid pack file name",
468 if (strcmp(dot + 1, "pack") != 0) {
469 err = got_error_fmt(GOT_ERR_BAD_PATH,
470 "'%s' is not a valid pack file name",
475 if (!got_parse_sha1_digest(id.sha1, p)) {
476 err = got_error_fmt(GOT_ERR_BAD_PATH,
477 "'%s' is not a valid pack file name",
482 *pack_hash = got_object_id_dup(&id);
483 if (*pack_hash == NULL) {
484 err = got_error_from_errno("got_object_id_dup");
488 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
490 err = got_error_from_errno2("open", packfile_path);
494 *packfile = fdopen(packfd, "r");
495 if (*packfile == NULL) {
496 err = got_error_from_errno2("fdopen", packfile_path);
501 if (packfd != -1 && close(packfd) == -1 && err == NULL)
502 err = got_error_from_errno2("close", packfile_path);
511 const struct got_error *
512 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
513 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
514 got_cancel_cb cancel_cb, void *cancel_arg)
516 const struct got_error *err = NULL;
517 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
518 struct got_packidx *packidx = NULL;
519 struct got_pack *pack = NULL;
522 err = got_object_id_str(&id_str, pack_hash);
526 if (asprintf(&packpath, "%s/pack-%s.pack",
527 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
528 err = got_error_from_errno("asprintf");
531 if (asprintf(&idxpath, "%s/pack-%s.idx",
532 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
533 err = got_error_from_errno("asprintf");
537 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
541 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
545 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
546 for (i = 0; i < nobj; i++) {
547 struct got_packidx_object_id *oid;
548 struct got_object_id id, base_id;
549 off_t offset, base_offset = 0;
555 err = cancel_cb(cancel_arg);
559 oid = &packidx->hdr.sorted_ids[i];
560 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
562 offset = got_packidx_get_object_offset(packidx, i);
564 err = got_error(GOT_ERR_BAD_PACKIDX);
568 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
574 case GOT_OBJ_TYPE_OFFSET_DELTA:
575 err = got_pack_parse_offset_delta(&base_offset, &len,
576 pack, offset, tslen);
580 case GOT_OBJ_TYPE_REF_DELTA:
581 err = got_pack_parse_ref_delta(&base_id,
582 pack, offset, tslen);
587 err = (*list_cb)(list_arg, &id, type, offset, size,
588 base_offset, &base_id);
598 got_packidx_close(packidx);
602 static const struct got_error *
603 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
604 void *progress_arg, struct got_ratelimit *rl,
605 int nloose, int ncommits, int npurged)
607 const struct got_error *err;
610 if (progress_cb == NULL)
613 err = got_ratelimit_check(&elapsed, rl);
617 return progress_cb(progress_arg, nloose, ncommits, npurged);
620 static const struct got_error *
621 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
622 got_cleanup_progress_cb progress_cb, void *progress_arg,
623 struct got_ratelimit *rl, struct got_repository *repo)
625 const struct got_error *err = NULL;
626 char *path_objects = NULL, *path = NULL;
628 struct got_object *obj = NULL;
629 struct got_object_id id;
634 *loose_ids = got_object_idset_alloc();
635 if (*loose_ids == NULL)
636 return got_error_from_errno("got_object_idset_alloc");
638 path_objects = got_repo_get_path_objects(repo);
639 if (path_objects == NULL) {
640 err = got_error_from_errno("got_repo_get_path_objects");
644 for (i = 0; i <= 0xff; i++) {
647 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
648 err = got_error_from_errno("asprintf");
654 if (errno == ENOENT) {
658 err = got_error_from_errno2("opendir", path);
662 while ((dent = readdir(dir)) != NULL) {
665 if (strcmp(dent->d_name, ".") == 0 ||
666 strcmp(dent->d_name, "..") == 0)
669 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
670 err = got_error_from_errno("asprintf");
674 memset(&id, 0, sizeof(id));
675 if (!got_parse_sha1_digest(id.sha1, id_str)) {
681 err = got_object_open_loose_fd(&fd, &id, repo);
684 if (fstat(fd, &sb) == -1) {
685 err = got_error_from_errno("fstat");
688 err = got_object_read_header_privsep(&obj, &id, repo,
692 fd = -1; /* already closed */
695 case GOT_OBJ_TYPE_COMMIT:
696 case GOT_OBJ_TYPE_TREE:
697 case GOT_OBJ_TYPE_BLOB:
698 case GOT_OBJ_TYPE_TAG:
701 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
705 got_object_close(obj);
707 (*ondisk_size) += sb.st_size;
708 err = got_object_idset_add(*loose_ids, &id, NULL);
711 err = report_cleanup_progress(progress_cb,
713 got_object_idset_num_elements(*loose_ids), -1, -1);
718 if (closedir(dir) != 0) {
719 err = got_error_from_errno("closedir");
728 if (dir && closedir(dir) != 0 && err == NULL)
729 err = got_error_from_errno("closedir");
730 if (fd != -1 && close(fd) == -1 && err == NULL)
731 err = got_error_from_errno("close");
733 got_object_idset_free(*loose_ids);
737 got_object_close(obj);
743 static const struct got_error *
744 preserve_loose_object(struct got_object_idset *loose_ids,
745 struct got_object_id *id, struct got_repository *repo, int *npacked)
747 const struct got_error *err = NULL;
748 struct got_object *obj;
750 if (!got_object_idset_contains(loose_ids, id))
754 * Try to open this object from a pack file. This ensures that
755 * we do in fact have a valid packed copy of the object. Otherwise
756 * we should not delete the loose representation of this object.
758 err = got_object_open_packed(&obj, id, repo);
760 got_object_close(obj);
762 * The object is referenced and packed.
763 * We can purge the redundantly stored loose object.
767 } else if (err->code != GOT_ERR_NO_OBJ)
771 * This object is referenced and not packed.
772 * Remove it from our purge set.
774 return got_object_idset_remove(NULL, loose_ids, id);
777 static const struct got_error *
778 load_tree_entries(struct got_object_id_queue *ids,
779 struct got_object_idset *loose_ids,
780 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
781 const char *dpath, struct got_repository *repo, int *npacked,
782 got_cancel_cb cancel_cb, void *cancel_arg)
784 const struct got_error *err;
785 struct got_tree_object *tree;
789 err = got_object_open_as_tree(&tree, repo, tree_id);
793 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
794 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
795 struct got_object_id *id = got_tree_entry_get_id(e);
796 mode_t mode = got_tree_entry_get_mode(e);
799 err = (*cancel_cb)(cancel_arg);
804 if (got_object_tree_entry_is_symlink(e) ||
805 got_object_tree_entry_is_submodule(e) ||
806 got_object_idset_contains(traversed_ids, id))
809 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
810 got_tree_entry_get_name(e)) == -1) {
811 err = got_error_from_errno("asprintf");
816 struct got_object_qid *qid;
817 err = got_object_qid_alloc(&qid, id);
820 STAILQ_INSERT_TAIL(ids, qid, entry);
821 } else if (S_ISREG(mode)) {
822 /* This blob is referenced. */
823 err = preserve_loose_object(loose_ids, id, repo,
827 err = got_object_idset_add(traversed_ids, id, NULL);
836 got_object_tree_close(tree);
841 static const struct got_error *
842 load_tree(struct got_object_idset *loose_ids,
843 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
844 const char *dpath, struct got_repository *repo, int *npacked,
845 got_cancel_cb cancel_cb, void *cancel_arg)
847 const struct got_error *err = NULL;
848 struct got_object_id_queue tree_ids;
849 struct got_object_qid *qid;
851 err = got_object_qid_alloc(&qid, tree_id);
855 STAILQ_INIT(&tree_ids);
856 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
858 while (!STAILQ_EMPTY(&tree_ids)) {
860 err = (*cancel_cb)(cancel_arg);
865 qid = STAILQ_FIRST(&tree_ids);
866 STAILQ_REMOVE_HEAD(&tree_ids, entry);
868 if (got_object_idset_contains(traversed_ids, &qid->id)) {
869 got_object_qid_free(qid);
873 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
875 got_object_qid_free(qid);
879 /* This tree is referenced. */
880 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
884 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
885 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
886 got_object_qid_free(qid);
891 got_object_id_queue_free(&tree_ids);
895 static const struct got_error *
896 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
897 int *npacked, struct got_object_idset *traversed_ids,
898 struct got_object_id *id, struct got_repository *repo,
899 got_cleanup_progress_cb progress_cb, void *progress_arg,
900 struct got_ratelimit *rl, int nloose,
901 got_cancel_cb cancel_cb, void *cancel_arg)
903 const struct got_error *err;
904 struct got_commit_object *commit = NULL;
905 struct got_tag_object *tag = NULL;
906 struct got_object_id *tree_id = NULL;
907 struct got_object_id_queue ids;
908 struct got_object_qid *qid;
911 err = got_object_qid_alloc(&qid, id);
916 STAILQ_INSERT_TAIL(&ids, qid, entry);
918 while (!STAILQ_EMPTY(&ids)) {
920 err = (*cancel_cb)(cancel_arg);
925 qid = STAILQ_FIRST(&ids);
926 STAILQ_REMOVE_HEAD(&ids, entry);
928 if (got_object_idset_contains(traversed_ids, &qid->id)) {
929 got_object_qid_free(qid);
934 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
938 /* This commit or tag is referenced. */
939 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
943 err = got_object_get_type(&obj_type, repo, &qid->id);
947 case GOT_OBJ_TYPE_COMMIT:
948 err = got_object_open_as_commit(&commit, repo,
953 case GOT_OBJ_TYPE_TAG:
954 err = got_object_open_as_tag(&tag, repo, &qid->id);
959 /* should not happen */
960 err = got_error(GOT_ERR_OBJ_TYPE);
964 /* Find a tree object to scan. */
966 tree_id = got_object_commit_get_tree_id(commit);
968 obj_type = got_object_tag_get_object_type(tag);
970 case GOT_OBJ_TYPE_COMMIT:
971 err = got_object_open_as_commit(&commit, repo,
972 got_object_tag_get_object_id(tag));
975 tree_id = got_object_commit_get_tree_id(commit);
977 case GOT_OBJ_TYPE_TREE:
978 tree_id = got_object_tag_get_object_id(tag);
982 * Tag points at something other than a
983 * commit or tree. Leave this weird tag object
984 * and the object it points to on disk.
986 err = got_object_idset_remove(NULL, loose_ids,
988 if (err && err->code != GOT_ERR_NO_OBJ)
990 err = got_object_idset_remove(NULL, loose_ids,
991 got_object_tag_get_object_id(tag));
992 if (err && err->code != GOT_ERR_NO_OBJ)
1000 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1001 repo, npacked, cancel_cb, cancel_arg);
1007 (*ncommits)++; /* scanned tags are counted as commits */
1009 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1010 nloose, *ncommits, -1);
1015 /* Find parent commits to scan. */
1016 const struct got_object_id_queue *parent_ids;
1017 parent_ids = got_object_commit_get_parent_ids(commit);
1018 err = got_object_id_queue_copy(parent_ids, &ids);
1021 got_object_commit_close(commit);
1025 got_object_tag_close(tag);
1028 got_object_qid_free(qid);
1033 got_object_qid_free(qid);
1035 got_object_commit_close(commit);
1037 got_object_tag_close(tag);
1038 got_object_id_queue_free(&ids);
1042 struct purge_loose_object_arg {
1043 struct got_repository *repo;
1044 got_cleanup_progress_cb progress_cb;
1046 struct got_ratelimit *rl;
1056 static const struct got_error *
1057 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1059 struct purge_loose_object_arg *a = arg;
1060 const struct got_error *err, *unlock_err = NULL;
1064 struct got_lockfile *lf = NULL;
1066 err = got_object_get_path(&path, id, a->repo);
1070 err = got_object_open_loose_fd(&fd, id, a->repo);
1074 if (fstat(fd, &sb) == -1) {
1075 err = got_error_from_errno("fstat");
1080 * Do not delete objects which are younger than our maximum
1081 * modification time threshold. This prevents a race where
1082 * new objects which are being added to the repository
1083 * concurrently would be deleted.
1085 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1087 err = got_lockfile_lock(&lf, path, -1);
1090 if (unlink(path) == -1) {
1091 err = got_error_from_errno2("unlink", path);
1097 a->size_purged += sb.st_size;
1098 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1099 a->rl, a->nloose, a->ncommits, a->npurged);
1104 if (fd != -1 && close(fd) == -1 && err == NULL)
1105 err = got_error_from_errno("close");
1108 unlock_err = got_lockfile_unlock(lf, -1);
1109 return err ? err : unlock_err;
1112 const struct got_error *
1113 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1114 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1115 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1116 got_cancel_cb cancel_cb, void *cancel_arg)
1118 const struct got_error *err;
1119 struct got_object_idset *loose_ids;
1120 struct got_object_idset *traversed_ids;
1121 struct got_object_id **referenced_ids;
1122 int i, nreferenced, nloose, ncommits = 0;
1123 struct got_reflist_head refs;
1124 struct got_reflist_entry *re;
1125 struct purge_loose_object_arg arg;
1126 time_t max_mtime = 0;
1127 struct got_ratelimit rl;
1130 got_ratelimit_init(&rl, 0, 500);
1136 err = get_loose_object_ids(&loose_ids, size_before,
1137 progress_cb, progress_arg, &rl, repo);
1140 nloose = got_object_idset_num_elements(loose_ids);
1142 got_object_idset_free(loose_ids);
1144 err = progress_cb(progress_arg, 0, 0, 0);
1151 traversed_ids = got_object_idset_alloc();
1152 if (traversed_ids == NULL) {
1153 err = got_error_from_errno("got_object_idset_alloc");
1157 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1160 if (!ignore_mtime) {
1161 TAILQ_FOREACH(re, &refs, entry) {
1162 time_t mtime = got_ref_get_mtime(re->ref);
1163 if (mtime > max_mtime)
1167 * For safety, keep objects created within 10 minutes
1168 * before the youngest reference was created.
1170 if (max_mtime >= 600)
1174 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1175 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1176 &refs, repo, cancel_cb, cancel_arg);
1180 for (i = 0; i < nreferenced; i++) {
1181 struct got_object_id *id = referenced_ids[i];
1182 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1183 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1184 nloose, cancel_cb, cancel_arg);
1189 /* Any remaining loose objects are unreferenced and can be purged. */
1191 arg.progress_arg = progress_arg;
1192 arg.progress_cb = progress_cb;
1194 arg.nloose = nloose;
1196 arg.size_purged = 0;
1197 arg.ncommits = ncommits;
1198 arg.dry_run = dry_run;
1199 arg.max_mtime = max_mtime;
1200 arg.ignore_mtime = ignore_mtime;
1201 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1204 *size_after = *size_before - arg.size_purged;
1206 /* Produce a final progress report. */
1208 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1213 got_object_idset_free(loose_ids);
1214 got_object_idset_free(traversed_ids);
1218 static const struct got_error *
1219 remove_packidx(int dir_fd, const char *relpath)
1221 const struct got_error *err, *unlock_err;
1222 struct got_lockfile *lf;
1224 err = got_lockfile_lock(&lf, relpath, dir_fd);
1227 if (unlinkat(dir_fd, relpath, 0) == -1)
1228 err = got_error_from_errno("unlinkat");
1229 unlock_err = got_lockfile_unlock(lf, dir_fd);
1230 return err ? err : unlock_err;
1233 const struct got_error *
1234 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1235 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1236 got_cancel_cb cancel_cb, void *cancel_arg)
1238 const struct got_error *err = NULL;
1239 DIR *packdir = NULL;
1240 struct dirent *dent;
1241 char *pack_relpath = NULL;
1245 packdir_fd = openat(got_repo_get_fd(repo),
1246 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1247 if (packdir_fd == -1) {
1248 if (errno == ENOENT)
1250 return got_error_from_errno_fmt("openat: %s/%s",
1251 got_repo_get_path_git_dir(repo),
1252 GOT_OBJECTS_PACK_DIR);
1255 packdir = fdopendir(packdir_fd);
1256 if (packdir == NULL) {
1257 err = got_error_from_errno("fdopendir");
1261 while ((dent = readdir(packdir)) != NULL) {
1263 err = cancel_cb(cancel_arg);
1268 if (!got_repo_is_packidx_filename(dent->d_name,
1269 strlen(dent->d_name)))
1272 err = got_packidx_get_packfile_path(&pack_relpath,
1277 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1279 pack_relpath = NULL;
1282 if (errno != ENOENT) {
1283 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1284 got_repo_get_path_git_dir(repo),
1285 GOT_OBJECTS_PACK_DIR,
1291 err = remove_packidx(packdir_fd, dent->d_name);
1297 if (asprintf(&path, "%s/%s/%s",
1298 got_repo_get_path_git_dir(repo),
1299 GOT_OBJECTS_PACK_DIR,
1300 dent->d_name) == -1) {
1301 err = got_error_from_errno("asprintf");
1304 err = progress_cb(progress_arg, path);
1310 pack_relpath = NULL;
1313 if (packdir && closedir(packdir) != 0 && err == NULL)
1314 err = got_error_from_errno("closedir");