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_ratelimit.h"
55 #include "got_lib_pack_create.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_lockfile.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;
157 FILE *delta_cache = NULL;
158 struct got_ratelimit rl;
163 got_ratelimit_init(&rl, 0, 500);
165 if (asprintf(&path, "%s/%s/packing.pack",
166 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
167 err = got_error_from_errno("asprintf");
170 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
174 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
175 err = got_error_from_errno2("fchmod", tmpfile_path);
179 delta_cache = got_opentemp();
180 if (delta_cache == NULL) {
181 err = got_error_from_errno("got_opentemp");
185 err = get_reflist_object_ids(&ours, &nours,
186 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
187 include_refs, repo, cancel_cb, cancel_arg);
192 err = got_error(GOT_ERR_CANNOT_PACK);
196 if (!TAILQ_EMPTY(exclude_refs)) {
197 err = get_reflist_object_ids(&theirs, &ntheirs,
198 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
200 cancel_cb, cancel_arg);
205 *pack_hash = calloc(1, sizeof(**pack_hash));
206 if (*pack_hash == NULL) {
207 err = got_error_from_errno("calloc");
211 err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
212 theirs, ntheirs, ours, nours, repo, loose_obj_only, 0,
213 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
217 err = got_object_id_str(&sha1_str, *pack_hash);
220 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
221 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
223 err = got_error_from_errno("asprintf");
227 if (lseek(packfd, 0L, SEEK_SET) == -1) {
228 err = got_error_from_errno("lseek");
231 if (rename(tmpfile_path, packfile_path) == -1) {
232 err = got_error_from_errno3("rename", tmpfile_path,
239 *packfile = fdopen(packfd, "w");
240 if (*packfile == NULL) {
241 err = got_error_from_errno2("fdopen", tmpfile_path);
246 for (i = 0; i < nours; i++)
249 for (i = 0; i < ntheirs; i++)
252 if (packfd != -1 && close(packfd) == -1 && err == NULL)
253 err = got_error_from_errno2("close", packfile_path);
254 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
255 err = got_error_from_errno("fclose");
256 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
257 err = got_error_from_errno2("unlink", tmpfile_path);
272 const struct got_error *
273 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
274 struct got_repository *repo,
275 got_pack_index_progress_cb progress_cb, void *progress_arg,
276 got_cancel_cb cancel_cb, void *cancel_arg)
281 int npackfd = -1, idxfd = -1, nidxfd = -1;
283 int idxstatus, done = 0;
284 const struct got_error *err;
285 struct imsgbuf idxibuf;
287 char *tmpidxpath = NULL;
288 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
289 const char *repo_path = got_repo_get_path_git_dir(repo);
292 for (i = 0; i < nitems(tmpfds); i++)
295 if (asprintf(&path, "%s/%s/indexing.idx",
296 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
297 err = got_error_from_errno("asprintf");
300 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
304 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
305 err = got_error_from_errno2("fchmod", tmpidxpath);
311 err = got_error_from_errno("dup");
315 for (i = 0; i < nitems(tmpfds); i++) {
316 tmpfds[i] = got_opentempfd();
317 if (tmpfds[i] == -1) {
318 err = got_error_from_errno("got_opentempfd");
323 err = got_object_id_str(&id_str, pack_hash);
327 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
328 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
329 err = got_error_from_errno("asprintf");
333 if (fstat(fileno(packfile), &sb) == -1) {
334 err = got_error_from_errno2("fstat", packfile_path);
338 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
339 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
340 err = got_error_from_errno("asprintf");
344 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
345 err = got_error_from_errno("socketpair");
350 err= got_error_from_errno("fork");
352 } else if (idxpid == 0)
353 got_privsep_exec_child(imsg_idxfds,
354 GOT_PATH_PROG_INDEX_PACK, packfile_path);
355 if (close(imsg_idxfds[1]) == -1) {
356 err = got_error_from_errno("close");
359 imsg_init(&idxibuf, imsg_idxfds[0]);
361 npackfd = dup(fileno(packfile));
363 err = got_error_from_errno("dup");
366 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
371 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
375 for (i = 0; i < nitems(tmpfds); i++) {
376 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
383 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
386 err = cancel_cb(cancel_arg);
391 err = got_privsep_recv_index_progress(&done, &nobj_total,
392 &nobj_indexed, &nobj_loose, &nobj_resolved,
396 if (nobj_indexed != 0) {
397 err = progress_cb(progress_arg, sb.st_size,
398 nobj_total, nobj_indexed, nobj_loose,
404 if (close(imsg_idxfds[0]) == -1) {
405 err = got_error_from_errno("close");
408 if (waitpid(idxpid, &idxstatus, 0) == -1) {
409 err = got_error_from_errno("waitpid");
413 if (rename(tmpidxpath, idxpath) == -1) {
414 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
421 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
422 err = got_error_from_errno2("unlink", tmpidxpath);
423 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
424 err = got_error_from_errno("close");
425 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
426 err = got_error_from_errno("close");
427 for (i = 0; i < nitems(tmpfds); i++) {
428 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
429 err = got_error_from_errno("close");
437 const struct got_error *
438 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
439 struct got_repository *repo, const char *packfile_path)
441 const struct got_error *err = NULL;
442 const char *packdir_path = NULL;
443 char *packfile_name = NULL, *p, *dot;
444 struct got_object_id id;
450 packdir_path = got_repo_get_path_objects_pack(repo);
451 if (packdir_path == NULL)
452 return got_error_from_errno("got_repo_get_path_objects_pack");
454 if (!got_path_is_child(packfile_path, packdir_path,
455 strlen(packdir_path))) {
456 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
461 err = got_path_basename(&packfile_name, packfile_path);
466 if (strncmp(p, "pack-", 5) != 0) {
467 err = got_error_fmt(GOT_ERR_BAD_PATH,
468 "'%s' is not a valid pack file name",
473 dot = strchr(p, '.');
475 err = got_error_fmt(GOT_ERR_BAD_PATH,
476 "'%s' is not a valid pack file name",
480 if (strcmp(dot + 1, "pack") != 0) {
481 err = got_error_fmt(GOT_ERR_BAD_PATH,
482 "'%s' is not a valid pack file name",
487 if (!got_parse_sha1_digest(id.sha1, p)) {
488 err = got_error_fmt(GOT_ERR_BAD_PATH,
489 "'%s' is not a valid pack file name",
494 *pack_hash = got_object_id_dup(&id);
495 if (*pack_hash == NULL) {
496 err = got_error_from_errno("got_object_id_dup");
500 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
502 err = got_error_from_errno2("open", packfile_path);
506 *packfile = fdopen(packfd, "r");
507 if (*packfile == NULL) {
508 err = got_error_from_errno2("fdopen", packfile_path);
513 if (packfd != -1 && close(packfd) == -1 && err == NULL)
514 err = got_error_from_errno2("close", packfile_path);
523 const struct got_error *
524 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
525 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
526 got_cancel_cb cancel_cb, void *cancel_arg)
528 const struct got_error *err = NULL;
529 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
530 struct got_packidx *packidx = NULL;
531 struct got_pack *pack = NULL;
534 err = got_object_id_str(&id_str, pack_hash);
538 if (asprintf(&packpath, "%s/pack-%s.pack",
539 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
540 err = got_error_from_errno("asprintf");
543 if (asprintf(&idxpath, "%s/pack-%s.idx",
544 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
545 err = got_error_from_errno("asprintf");
549 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
553 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
557 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
558 for (i = 0; i < nobj; i++) {
559 struct got_packidx_object_id *oid;
560 struct got_object_id id, base_id;
561 off_t offset, base_offset = 0;
567 err = cancel_cb(cancel_arg);
571 oid = &packidx->hdr.sorted_ids[i];
572 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
574 offset = got_packidx_get_object_offset(packidx, i);
576 err = got_error(GOT_ERR_BAD_PACKIDX);
580 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
586 case GOT_OBJ_TYPE_OFFSET_DELTA:
587 err = got_pack_parse_offset_delta(&base_offset, &len,
588 pack, offset, tslen);
592 case GOT_OBJ_TYPE_REF_DELTA:
593 err = got_pack_parse_ref_delta(&base_id,
594 pack, offset, tslen);
599 err = (*list_cb)(list_arg, &id, type, offset, size,
600 base_offset, &base_id);
610 got_packidx_close(packidx);
614 static const struct got_error *
615 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
616 void *progress_arg, struct got_ratelimit *rl,
617 int nloose, int ncommits, int npurged)
619 const struct got_error *err;
622 if (progress_cb == NULL)
625 err = got_ratelimit_check(&elapsed, rl);
629 return progress_cb(progress_arg, nloose, ncommits, npurged);
632 static const struct got_error *
633 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
634 got_cleanup_progress_cb progress_cb, void *progress_arg,
635 struct got_ratelimit *rl, struct got_repository *repo)
637 const struct got_error *err = NULL;
638 char *path_objects = NULL, *path = NULL;
640 struct got_object *obj = NULL;
641 struct got_object_id id;
646 *loose_ids = got_object_idset_alloc();
647 if (*loose_ids == NULL)
648 return got_error_from_errno("got_object_idset_alloc");
650 path_objects = got_repo_get_path_objects(repo);
651 if (path_objects == NULL) {
652 err = got_error_from_errno("got_repo_get_path_objects");
656 for (i = 0; i <= 0xff; i++) {
659 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
660 err = got_error_from_errno("asprintf");
666 if (errno == ENOENT) {
670 err = got_error_from_errno2("opendir", path);
674 while ((dent = readdir(dir)) != NULL) {
677 if (strcmp(dent->d_name, ".") == 0 ||
678 strcmp(dent->d_name, "..") == 0)
681 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
682 err = got_error_from_errno("asprintf");
686 memset(&id, 0, sizeof(id));
687 if (!got_parse_sha1_digest(id.sha1, id_str)) {
693 err = got_object_open_loose_fd(&fd, &id, repo);
696 if (fstat(fd, &sb) == -1) {
697 err = got_error_from_errno("fstat");
700 err = got_object_read_header_privsep(&obj, &id, repo,
704 fd = -1; /* already closed */
707 case GOT_OBJ_TYPE_COMMIT:
708 case GOT_OBJ_TYPE_TREE:
709 case GOT_OBJ_TYPE_BLOB:
710 case GOT_OBJ_TYPE_TAG:
713 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
717 got_object_close(obj);
719 (*ondisk_size) += sb.st_size;
720 err = got_object_idset_add(*loose_ids, &id, NULL);
723 err = report_cleanup_progress(progress_cb,
725 got_object_idset_num_elements(*loose_ids), -1, -1);
730 if (closedir(dir) != 0) {
731 err = got_error_from_errno("closedir");
740 if (dir && closedir(dir) != 0 && err == NULL)
741 err = got_error_from_errno("closedir");
742 if (fd != -1 && close(fd) == -1 && err == NULL)
743 err = got_error_from_errno("close");
745 got_object_idset_free(*loose_ids);
749 got_object_close(obj);
755 static const struct got_error *
756 preserve_loose_object(struct got_object_idset *loose_ids,
757 struct got_object_id *id, struct got_repository *repo, int *npacked)
759 const struct got_error *err = NULL;
760 struct got_object *obj;
762 if (!got_object_idset_contains(loose_ids, id))
766 * Try to open this object from a pack file. This ensures that
767 * we do in fact have a valid packed copy of the object. Otherwise
768 * we should not delete the loose representation of this object.
770 err = got_object_open_packed(&obj, id, repo);
772 got_object_close(obj);
774 * The object is referenced and packed.
775 * We can purge the redundantly stored loose object.
779 } else if (err->code != GOT_ERR_NO_OBJ)
783 * This object is referenced and not packed.
784 * Remove it from our purge set.
786 return got_object_idset_remove(NULL, loose_ids, id);
789 static const struct got_error *
790 load_tree_entries(struct got_object_id_queue *ids,
791 struct got_object_idset *loose_ids,
792 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
793 const char *dpath, struct got_repository *repo, int *npacked,
794 got_cancel_cb cancel_cb, void *cancel_arg)
796 const struct got_error *err;
797 struct got_tree_object *tree;
801 err = got_object_open_as_tree(&tree, repo, tree_id);
805 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
806 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
807 struct got_object_id *id = got_tree_entry_get_id(e);
808 mode_t mode = got_tree_entry_get_mode(e);
811 err = (*cancel_cb)(cancel_arg);
816 if (got_object_tree_entry_is_symlink(e) ||
817 got_object_tree_entry_is_submodule(e) ||
818 got_object_idset_contains(traversed_ids, id))
821 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
822 got_tree_entry_get_name(e)) == -1) {
823 err = got_error_from_errno("asprintf");
828 struct got_object_qid *qid;
829 err = got_object_qid_alloc(&qid, id);
832 STAILQ_INSERT_TAIL(ids, qid, entry);
833 } else if (S_ISREG(mode)) {
834 /* This blob is referenced. */
835 err = preserve_loose_object(loose_ids, id, repo,
839 err = got_object_idset_add(traversed_ids, id, NULL);
847 got_object_tree_close(tree);
852 static const struct got_error *
853 load_tree(struct got_object_idset *loose_ids,
854 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
855 const char *dpath, struct got_repository *repo, int *npacked,
856 got_cancel_cb cancel_cb, void *cancel_arg)
858 const struct got_error *err = NULL;
859 struct got_object_id_queue tree_ids;
860 struct got_object_qid *qid;
862 err = got_object_qid_alloc(&qid, tree_id);
866 STAILQ_INIT(&tree_ids);
867 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
869 while (!STAILQ_EMPTY(&tree_ids)) {
871 err = (*cancel_cb)(cancel_arg);
876 qid = STAILQ_FIRST(&tree_ids);
877 STAILQ_REMOVE_HEAD(&tree_ids, entry);
879 if (got_object_idset_contains(traversed_ids, &qid->id)) {
880 got_object_qid_free(qid);
884 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
886 got_object_qid_free(qid);
890 /* This tree is referenced. */
891 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
895 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
896 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
897 got_object_qid_free(qid);
902 got_object_id_queue_free(&tree_ids);
906 static const struct got_error *
907 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
908 int *npacked, struct got_object_idset *traversed_ids,
909 struct got_object_id *id, struct got_repository *repo,
910 got_cleanup_progress_cb progress_cb, void *progress_arg,
911 struct got_ratelimit *rl, int nloose,
912 got_cancel_cb cancel_cb, void *cancel_arg)
914 const struct got_error *err;
915 struct got_commit_object *commit = NULL;
916 struct got_tag_object *tag = NULL;
917 struct got_object_id *tree_id = NULL;
918 struct got_object_id_queue ids;
919 struct got_object_qid *qid;
922 err = got_object_qid_alloc(&qid, id);
927 STAILQ_INSERT_TAIL(&ids, qid, entry);
929 while (!STAILQ_EMPTY(&ids)) {
931 err = (*cancel_cb)(cancel_arg);
936 qid = STAILQ_FIRST(&ids);
937 STAILQ_REMOVE_HEAD(&ids, entry);
939 if (got_object_idset_contains(traversed_ids, &qid->id)) {
940 got_object_qid_free(qid);
945 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
949 /* This commit or tag is referenced. */
950 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
954 err = got_object_get_type(&obj_type, repo, &qid->id);
958 case GOT_OBJ_TYPE_COMMIT:
959 err = got_object_open_as_commit(&commit, repo,
964 case GOT_OBJ_TYPE_TAG:
965 err = got_object_open_as_tag(&tag, repo, &qid->id);
970 /* should not happen */
971 err = got_error(GOT_ERR_OBJ_TYPE);
975 /* Find a tree object to scan. */
977 tree_id = got_object_commit_get_tree_id(commit);
979 obj_type = got_object_tag_get_object_type(tag);
981 case GOT_OBJ_TYPE_COMMIT:
982 err = got_object_open_as_commit(&commit, repo,
983 got_object_tag_get_object_id(tag));
986 tree_id = got_object_commit_get_tree_id(commit);
988 case GOT_OBJ_TYPE_TREE:
989 tree_id = got_object_tag_get_object_id(tag);
993 * Tag points at something other than a
994 * commit or tree. Leave this weird tag object
995 * and the object it points to on disk.
997 err = got_object_idset_remove(NULL, loose_ids,
999 if (err && err->code != GOT_ERR_NO_OBJ)
1001 err = got_object_idset_remove(NULL, loose_ids,
1002 got_object_tag_get_object_id(tag));
1003 if (err && err->code != GOT_ERR_NO_OBJ)
1011 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1012 repo, npacked, cancel_cb, cancel_arg);
1018 (*ncommits)++; /* scanned tags are counted as commits */
1020 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1021 nloose, *ncommits, -1);
1026 /* Find parent commits to scan. */
1027 const struct got_object_id_queue *parent_ids;
1028 parent_ids = got_object_commit_get_parent_ids(commit);
1029 err = got_object_id_queue_copy(parent_ids, &ids);
1032 got_object_commit_close(commit);
1036 got_object_tag_close(tag);
1039 got_object_qid_free(qid);
1044 got_object_qid_free(qid);
1046 got_object_commit_close(commit);
1048 got_object_tag_close(tag);
1049 got_object_id_queue_free(&ids);
1053 struct purge_loose_object_arg {
1054 struct got_repository *repo;
1055 got_cleanup_progress_cb progress_cb;
1057 struct got_ratelimit *rl;
1067 static const struct got_error *
1068 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1070 struct purge_loose_object_arg *a = arg;
1071 const struct got_error *err, *unlock_err = NULL;
1075 struct got_lockfile *lf = NULL;
1077 err = got_object_get_path(&path, id, a->repo);
1081 err = got_object_open_loose_fd(&fd, id, a->repo);
1085 if (fstat(fd, &sb) == -1) {
1086 err = got_error_from_errno("fstat");
1091 * Do not delete objects which are younger than our maximum
1092 * modification time threshold. This prevents a race where
1093 * new objects which are being added to the repository
1094 * concurrently would be deleted.
1096 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1098 err = got_lockfile_lock(&lf, path, -1);
1101 if (unlink(path) == -1) {
1102 err = got_error_from_errno2("unlink", path);
1108 a->size_purged += sb.st_size;
1109 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1110 a->rl, a->nloose, a->ncommits, a->npurged);
1115 if (fd != -1 && close(fd) == -1 && err == NULL)
1116 err = got_error_from_errno("close");
1119 unlock_err = got_lockfile_unlock(lf, -1);
1120 return err ? err : unlock_err;
1123 const struct got_error *
1124 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1125 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1126 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1127 got_cancel_cb cancel_cb, void *cancel_arg)
1129 const struct got_error *err;
1130 struct got_object_idset *loose_ids;
1131 struct got_object_idset *traversed_ids;
1132 struct got_object_id **referenced_ids;
1133 int i, nreferenced, nloose, ncommits = 0;
1134 struct got_reflist_head refs;
1135 struct got_reflist_entry *re;
1136 struct purge_loose_object_arg arg;
1137 time_t max_mtime = 0;
1138 struct got_ratelimit rl;
1141 got_ratelimit_init(&rl, 0, 500);
1147 err = get_loose_object_ids(&loose_ids, size_before,
1148 progress_cb, progress_arg, &rl, repo);
1151 nloose = got_object_idset_num_elements(loose_ids);
1153 got_object_idset_free(loose_ids);
1155 err = progress_cb(progress_arg, 0, 0, 0);
1162 traversed_ids = got_object_idset_alloc();
1163 if (traversed_ids == NULL) {
1164 err = got_error_from_errno("got_object_idset_alloc");
1168 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1171 if (!ignore_mtime) {
1172 TAILQ_FOREACH(re, &refs, entry) {
1173 time_t mtime = got_ref_get_mtime(re->ref);
1174 if (mtime > max_mtime)
1178 * For safety, keep objects created within 10 minutes
1179 * before the youngest reference was created.
1181 if (max_mtime >= 600)
1185 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1186 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1187 &refs, repo, cancel_cb, cancel_arg);
1191 for (i = 0; i < nreferenced; i++) {
1192 struct got_object_id *id = referenced_ids[i];
1193 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1194 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1195 nloose, cancel_cb, cancel_arg);
1200 /* Any remaining loose objects are unreferenced and can be purged. */
1202 arg.progress_arg = progress_arg;
1203 arg.progress_cb = progress_cb;
1205 arg.nloose = nloose;
1207 arg.size_purged = 0;
1208 arg.ncommits = ncommits;
1209 arg.dry_run = dry_run;
1210 arg.max_mtime = max_mtime;
1211 arg.ignore_mtime = ignore_mtime;
1212 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1215 *size_after = *size_before - arg.size_purged;
1217 /* Produce a final progress report. */
1219 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1224 got_object_idset_free(loose_ids);
1225 got_object_idset_free(traversed_ids);
1229 static const struct got_error *
1230 remove_packidx(int dir_fd, const char *relpath)
1232 const struct got_error *err, *unlock_err;
1233 struct got_lockfile *lf;
1235 err = got_lockfile_lock(&lf, relpath, dir_fd);
1238 if (unlinkat(dir_fd, relpath, 0) == -1)
1239 err = got_error_from_errno("unlinkat");
1240 unlock_err = got_lockfile_unlock(lf, dir_fd);
1241 return err ? err : unlock_err;
1244 const struct got_error *
1245 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1246 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1247 got_cancel_cb cancel_cb, void *cancel_arg)
1249 const struct got_error *err = NULL;
1250 DIR *packdir = NULL;
1251 struct dirent *dent;
1252 char *pack_relpath = NULL;
1256 packdir_fd = openat(got_repo_get_fd(repo),
1257 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1258 if (packdir_fd == -1) {
1259 if (errno == ENOENT)
1261 return got_error_from_errno_fmt("openat: %s/%s",
1262 got_repo_get_path_git_dir(repo),
1263 GOT_OBJECTS_PACK_DIR);
1266 packdir = fdopendir(packdir_fd);
1267 if (packdir == NULL) {
1268 err = got_error_from_errno("fdopendir");
1272 while ((dent = readdir(packdir)) != NULL) {
1274 err = cancel_cb(cancel_arg);
1279 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1282 err = got_packidx_get_packfile_path(&pack_relpath,
1287 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1289 pack_relpath = NULL;
1292 if (errno != ENOENT) {
1293 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1294 got_repo_get_path_git_dir(repo),
1295 GOT_OBJECTS_PACK_DIR,
1301 err = remove_packidx(packdir_fd, dent->d_name);
1307 if (asprintf(&path, "%s/%s/%s",
1308 got_repo_get_path_git_dir(repo),
1309 GOT_OBJECTS_PACK_DIR,
1310 dent->d_name) == -1) {
1311 err = got_error_from_errno("asprintf");
1314 err = progress_cb(progress_arg, path);
1320 pack_relpath = NULL;
1323 if (packdir && closedir(packdir) != 0 && err == NULL)
1324 err = got_error_from_errno("closedir");