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"
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static const struct got_error *
63 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
64 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
65 struct got_repository *repo,
66 got_cancel_cb cancel_cb, void *cancel_arg)
68 const struct got_error *err = NULL;
69 const size_t alloc_chunksz = 256;
71 struct got_reflist_entry *re;
77 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
79 return got_error_from_errno("reallocarray");
80 nalloc = alloc_chunksz;
82 TAILQ_FOREACH(re, refs, entry) {
83 struct got_object_id *id;
86 err = cancel_cb(cancel_arg);
91 err = got_ref_resolve(&id, repo, re->ref);
95 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
97 err = got_object_get_type(&obj_type, repo, id);
100 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
107 if (nalloc <= *nobjects) {
108 struct got_object_id **new;
109 new = recallocarray(*ids, nalloc,
110 nalloc + alloc_chunksz,
111 sizeof(struct got_object_id *));
113 err = got_error_from_errno(
118 nalloc += alloc_chunksz;
120 (*ids)[*nobjects] = id;
121 if ((*ids)[*nobjects] == NULL) {
122 err = got_error_from_errno("got_object_id_dup");
129 for (i = 0; i < *nobjects; i++)
138 const struct got_error *
139 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
140 struct got_reflist_head *include_refs,
141 struct got_reflist_head *exclude_refs, struct got_repository *repo,
142 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
143 got_cancel_cb cancel_cb, void *cancel_arg)
145 const struct got_error *err = NULL;
146 struct got_object_id **ours = NULL, **theirs = NULL;
147 int nours = 0, ntheirs = 0, packfd = -1, i;
148 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
149 char *sha1_str = NULL;
154 if (asprintf(&path, "%s/%s/packing.pack",
155 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
156 err = got_error_from_errno("asprintf");
159 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
163 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
164 err = got_error_from_errno2("fchmod", tmpfile_path);
168 *packfile = fdopen(packfd, "w");
169 if (*packfile == NULL) {
170 err = got_error_from_errno2("fdopen", tmpfile_path);
175 err = get_reflist_object_ids(&ours, &nours,
176 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
177 include_refs, repo, cancel_cb, cancel_arg);
182 err = got_error(GOT_ERR_CANNOT_PACK);
186 if (!TAILQ_EMPTY(exclude_refs)) {
187 err = get_reflist_object_ids(&theirs, &ntheirs,
188 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
190 cancel_cb, cancel_arg);
195 *pack_hash = calloc(1, sizeof(**pack_hash));
196 if (*pack_hash == NULL) {
197 err = got_error_from_errno("calloc");
201 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
202 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
203 cancel_cb, cancel_arg);
207 err = got_object_id_str(&sha1_str, *pack_hash);
210 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
211 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
213 err = got_error_from_errno("asprintf");
217 if (fflush(*packfile) == -1) {
218 err = got_error_from_errno("fflush");
221 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
222 err = got_error_from_errno("fseek");
225 if (rename(tmpfile_path, packfile_path) == -1) {
226 err = got_error_from_errno3("rename", tmpfile_path,
233 for (i = 0; i < nours; i++)
236 for (i = 0; i < ntheirs; i++)
239 if (packfd != -1 && close(packfd) == -1 && err == NULL)
240 err = got_error_from_errno2("close", packfile_path);
241 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
242 err = got_error_from_errno2("unlink", tmpfile_path);
257 const struct got_error *
258 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
259 struct got_repository *repo,
260 got_pack_index_progress_cb progress_cb, void *progress_arg,
261 got_cancel_cb cancel_cb, void *cancel_arg)
266 int npackfd = -1, idxfd = -1, nidxfd = -1;
268 int idxstatus, done = 0;
269 const struct got_error *err;
270 struct imsgbuf idxibuf;
272 char *tmpidxpath = NULL;
273 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
274 const char *repo_path = got_repo_get_path_git_dir(repo);
277 for (i = 0; i < nitems(tmpfds); i++)
280 if (asprintf(&path, "%s/%s/indexing.idx",
281 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
282 err = got_error_from_errno("asprintf");
285 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
289 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
290 err = got_error_from_errno2("fchmod", tmpidxpath);
296 err = got_error_from_errno("dup");
300 for (i = 0; i < nitems(tmpfds); i++) {
301 tmpfds[i] = got_opentempfd();
302 if (tmpfds[i] == -1) {
303 err = got_error_from_errno("got_opentempfd");
308 err = got_object_id_str(&id_str, pack_hash);
312 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
313 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
314 err = got_error_from_errno("asprintf");
318 if (fstat(fileno(packfile), &sb) == -1) {
319 err = got_error_from_errno2("fstat", packfile_path);
323 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
324 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
325 err = got_error_from_errno("asprintf");
329 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
330 err = got_error_from_errno("socketpair");
335 err= got_error_from_errno("fork");
337 } else if (idxpid == 0)
338 got_privsep_exec_child(imsg_idxfds,
339 GOT_PATH_PROG_INDEX_PACK, packfile_path);
340 if (close(imsg_idxfds[1]) == -1) {
341 err = got_error_from_errno("close");
344 imsg_init(&idxibuf, imsg_idxfds[0]);
346 npackfd = dup(fileno(packfile));
348 err = got_error_from_errno("dup");
351 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
356 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
360 for (i = 0; i < nitems(tmpfds); i++) {
361 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
368 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
371 err = cancel_cb(cancel_arg);
376 err = got_privsep_recv_index_progress(&done, &nobj_total,
377 &nobj_indexed, &nobj_loose, &nobj_resolved,
381 if (nobj_indexed != 0) {
382 err = progress_cb(progress_arg, sb.st_size,
383 nobj_total, nobj_indexed, nobj_loose,
388 imsg_clear(&idxibuf);
390 if (close(imsg_idxfds[0]) == -1) {
391 err = got_error_from_errno("close");
394 if (waitpid(idxpid, &idxstatus, 0) == -1) {
395 err = got_error_from_errno("waitpid");
399 if (rename(tmpidxpath, idxpath) == -1) {
400 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
407 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
408 err = got_error_from_errno2("unlink", tmpidxpath);
409 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
410 err = got_error_from_errno("close");
411 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
412 err = got_error_from_errno("close");
413 for (i = 0; i < nitems(tmpfds); i++) {
414 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
415 err = got_error_from_errno("close");
423 const struct got_error *
424 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
425 struct got_repository *repo, const char *packfile_path)
427 const struct got_error *err = NULL;
428 const char *packdir_path = NULL;
429 char *packfile_name = NULL, *p, *dot;
430 struct got_object_id id;
436 packdir_path = got_repo_get_path_objects_pack(repo);
437 if (packdir_path == NULL)
438 return got_error_from_errno("got_repo_get_path_objects_pack");
440 if (!got_path_is_child(packfile_path, packdir_path,
441 strlen(packdir_path))) {
442 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
447 err = got_path_basename(&packfile_name, packfile_path);
452 if (strncmp(p, "pack-", 5) != 0) {
453 err = got_error_fmt(GOT_ERR_BAD_PATH,
454 "'%s' is not a valid pack file name",
459 dot = strchr(p, '.');
461 err = got_error_fmt(GOT_ERR_BAD_PATH,
462 "'%s' is not a valid pack file name",
466 if (strcmp(dot + 1, "pack") != 0) {
467 err = got_error_fmt(GOT_ERR_BAD_PATH,
468 "'%s' is not a valid pack file name",
473 if (!got_parse_sha1_digest(id.sha1, p)) {
474 err = got_error_fmt(GOT_ERR_BAD_PATH,
475 "'%s' is not a valid pack file name",
480 *pack_hash = got_object_id_dup(&id);
481 if (*pack_hash == NULL) {
482 err = got_error_from_errno("got_object_id_dup");
486 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
488 err = got_error_from_errno2("open", packfile_path);
492 *packfile = fdopen(packfd, "r");
493 if (*packfile == NULL) {
494 err = got_error_from_errno2("fdopen", packfile_path);
499 if (packfd != -1 && close(packfd) == -1 && err == NULL)
500 err = got_error_from_errno2("close", packfile_path);
509 const struct got_error *
510 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
511 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
512 got_cancel_cb cancel_cb, void *cancel_arg)
514 const struct got_error *err = NULL;
515 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
516 struct got_packidx *packidx = NULL;
517 struct got_pack *pack = NULL;
520 err = got_object_id_str(&id_str, pack_hash);
524 if (asprintf(&packpath, "%s/pack-%s.pack",
525 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
526 err = got_error_from_errno("asprintf");
529 if (asprintf(&idxpath, "%s/pack-%s.idx",
530 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
531 err = got_error_from_errno("asprintf");
535 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
539 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
543 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
544 for (i = 0; i < nobj; i++) {
545 struct got_packidx_object_id *oid;
546 struct got_object_id id, base_id;
547 off_t offset, base_offset = 0;
553 err = cancel_cb(cancel_arg);
557 oid = &packidx->hdr.sorted_ids[i];
558 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
560 offset = got_packidx_get_object_offset(packidx, i);
562 err = got_error(GOT_ERR_BAD_PACKIDX);
566 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
572 case GOT_OBJ_TYPE_OFFSET_DELTA:
573 err = got_pack_parse_offset_delta(&base_offset, &len,
574 pack, offset, tslen);
578 case GOT_OBJ_TYPE_REF_DELTA:
579 err = got_pack_parse_ref_delta(&base_id,
580 pack, offset, tslen);
585 err = (*list_cb)(list_arg, &id, type, offset, size,
586 base_offset, &base_id);
596 got_packidx_close(packidx);
600 static const struct got_error *
601 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
602 got_cleanup_progress_cb progress_cb, void *progress_arg,
603 struct got_repository *repo)
605 const struct got_error *err = NULL;
606 char *path_objects = NULL, *path = NULL;
608 struct got_object *obj = NULL;
609 struct got_object_id id;
614 *loose_ids = got_object_idset_alloc();
615 if (*loose_ids == NULL)
616 return got_error_from_errno("got_object_idset_alloc");
618 path_objects = got_repo_get_path_objects(repo);
619 if (path_objects == NULL) {
620 err = got_error_from_errno("got_repo_get_path_objects");
624 for (i = 0; i <= 0xff; i++) {
627 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
628 err = got_error_from_errno("asprintf");
634 if (errno == ENOENT) {
638 err = got_error_from_errno2("opendir", path);
642 while ((dent = readdir(dir)) != NULL) {
645 if (strcmp(dent->d_name, ".") == 0 ||
646 strcmp(dent->d_name, "..") == 0)
649 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
650 err = got_error_from_errno("asprintf");
654 memset(&id, 0, sizeof(id));
655 if (!got_parse_sha1_digest(id.sha1, id_str)) {
661 err = got_object_open_loose_fd(&fd, &id, repo);
664 if (fstat(fd, &sb) == -1) {
665 err = got_error_from_errno("fstat");
668 err = got_object_read_header_privsep(&obj, &id, repo,
672 fd = -1; /* already closed */
675 case GOT_OBJ_TYPE_COMMIT:
676 case GOT_OBJ_TYPE_TREE:
677 case GOT_OBJ_TYPE_BLOB:
678 case GOT_OBJ_TYPE_TAG:
681 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
685 got_object_close(obj);
687 (*ondisk_size) += sb.st_size;
688 err = got_object_idset_add(*loose_ids, &id, NULL);
692 err = progress_cb(progress_arg,
693 got_object_idset_num_elements(*loose_ids),
700 if (closedir(dir) != 0) {
701 err = got_error_from_errno("closedir");
710 if (dir && closedir(dir) != 0 && err == NULL)
711 err = got_error_from_errno("closedir");
712 if (fd != -1 && close(fd) == -1 && err == NULL)
713 err = got_error_from_errno("close");
715 got_object_idset_free(*loose_ids);
719 got_object_close(obj);
725 static const struct got_error *
726 preserve_loose_object(struct got_object_idset *loose_ids,
727 struct got_object_id *id, struct got_repository *repo, int *npacked)
729 const struct got_error *err = NULL;
730 struct got_object *obj;
732 if (!got_object_idset_contains(loose_ids, id))
736 * Try to open this object from a pack file. This ensures that
737 * we do in fact have a valid packed copy of the object. Otherwise
738 * we should not delete the loose representation of this object.
740 err = got_object_open_packed(&obj, id, repo);
742 got_object_close(obj);
744 * The object is referenced and packed.
745 * We can purge the redundantly stored loose object.
749 } else if (err->code != GOT_ERR_NO_OBJ)
753 * This object is referenced and not packed.
754 * Remove it from our purge set.
756 return got_object_idset_remove(NULL, loose_ids, id);
759 static const struct got_error *
760 load_tree_entries(struct got_object_id_queue *ids,
761 struct got_object_idset *loose_ids,
762 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
763 const char *dpath, struct got_repository *repo, int *npacked,
764 got_cancel_cb cancel_cb, void *cancel_arg)
766 const struct got_error *err;
767 struct got_tree_object *tree;
771 err = got_object_open_as_tree(&tree, repo, tree_id);
775 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
776 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
777 struct got_object_id *id = got_tree_entry_get_id(e);
778 mode_t mode = got_tree_entry_get_mode(e);
781 err = (*cancel_cb)(cancel_arg);
786 if (got_object_tree_entry_is_symlink(e) ||
787 got_object_tree_entry_is_submodule(e) ||
788 got_object_idset_contains(traversed_ids, id))
791 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
792 got_tree_entry_get_name(e)) == -1) {
793 err = got_error_from_errno("asprintf");
798 struct got_object_qid *qid;
799 err = got_object_qid_alloc(&qid, id);
802 STAILQ_INSERT_TAIL(ids, qid, entry);
803 } else if (S_ISREG(mode)) {
804 /* This blob is referenced. */
805 err = preserve_loose_object(loose_ids, id, repo,
809 err = got_object_idset_add(traversed_ids, id, NULL);
818 got_object_tree_close(tree);
823 static const struct got_error *
824 load_tree(struct got_object_idset *loose_ids,
825 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
826 const char *dpath, struct got_repository *repo, int *npacked,
827 got_cancel_cb cancel_cb, void *cancel_arg)
829 const struct got_error *err = NULL;
830 struct got_object_id_queue tree_ids;
831 struct got_object_qid *qid;
833 err = got_object_qid_alloc(&qid, tree_id);
837 STAILQ_INIT(&tree_ids);
838 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
840 while (!STAILQ_EMPTY(&tree_ids)) {
842 err = (*cancel_cb)(cancel_arg);
847 qid = STAILQ_FIRST(&tree_ids);
848 STAILQ_REMOVE_HEAD(&tree_ids, entry);
850 if (got_object_idset_contains(traversed_ids, qid->id)) {
851 got_object_qid_free(qid);
855 err = got_object_idset_add(traversed_ids, qid->id, NULL);
857 got_object_qid_free(qid);
861 /* This tree is referenced. */
862 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
866 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
867 qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
868 got_object_qid_free(qid);
873 got_object_id_queue_free(&tree_ids);
877 static const struct got_error *
878 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
879 int *npacked, struct got_object_idset *traversed_ids,
880 struct got_object_id *id, struct got_repository *repo,
881 got_cleanup_progress_cb progress_cb, void *progress_arg, int nloose,
882 got_cancel_cb cancel_cb, void *cancel_arg)
884 const struct got_error *err;
885 struct got_commit_object *commit = NULL;
886 struct got_tag_object *tag = NULL;
887 struct got_object_id *tree_id = NULL;
888 struct got_object_id_queue ids;
889 struct got_object_qid *qid;
892 err = got_object_qid_alloc(&qid, id);
897 STAILQ_INSERT_TAIL(&ids, qid, entry);
899 while (!STAILQ_EMPTY(&ids)) {
901 err = (*cancel_cb)(cancel_arg);
906 qid = STAILQ_FIRST(&ids);
907 STAILQ_REMOVE_HEAD(&ids, entry);
909 if (got_object_idset_contains(traversed_ids, qid->id)) {
910 got_object_qid_free(qid);
915 err = got_object_idset_add(traversed_ids, qid->id, NULL);
919 /* This commit or tag is referenced. */
920 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
924 err = got_object_get_type(&obj_type, repo, qid->id);
928 case GOT_OBJ_TYPE_COMMIT:
929 err = got_object_open_as_commit(&commit, repo, qid->id);
933 case GOT_OBJ_TYPE_TAG:
934 err = got_object_open_as_tag(&tag, repo, qid->id);
939 /* should not happen */
940 err = got_error(GOT_ERR_OBJ_TYPE);
944 /* Find a tree object to scan. */
946 tree_id = got_object_commit_get_tree_id(commit);
948 obj_type = got_object_tag_get_object_type(tag);
950 case GOT_OBJ_TYPE_COMMIT:
951 err = got_object_open_as_commit(&commit, repo,
952 got_object_tag_get_object_id(tag));
955 tree_id = got_object_commit_get_tree_id(commit);
957 case GOT_OBJ_TYPE_TREE:
958 tree_id = got_object_tag_get_object_id(tag);
962 * Tag points at something other than a
963 * commit or tree. Leave this weird tag object
964 * and the object it points to on disk.
966 err = got_object_idset_remove(NULL, loose_ids,
968 if (err && err->code != GOT_ERR_NO_OBJ)
970 err = got_object_idset_remove(NULL, loose_ids,
971 got_object_tag_get_object_id(tag));
972 if (err && err->code != GOT_ERR_NO_OBJ)
980 err = load_tree(loose_ids, traversed_ids, tree_id, "",
981 repo, npacked, cancel_cb, cancel_arg);
987 (*ncommits)++; /* scanned tags are counted as commits */
990 err = progress_cb(progress_arg, nloose, *ncommits, -1);
996 /* Find parent commits to scan. */
997 const struct got_object_id_queue *parent_ids;
998 parent_ids = got_object_commit_get_parent_ids(commit);
999 err = got_object_id_queue_copy(parent_ids, &ids);
1002 got_object_commit_close(commit);
1006 got_object_tag_close(tag);
1009 got_object_qid_free(qid);
1014 got_object_qid_free(qid);
1016 got_object_commit_close(commit);
1018 got_object_tag_close(tag);
1019 got_object_id_queue_free(&ids);
1023 struct purge_loose_object_arg {
1024 struct got_repository *repo;
1025 got_cleanup_progress_cb progress_cb;
1036 static const struct got_error *
1037 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1039 struct purge_loose_object_arg *a = arg;
1040 const struct got_error *err, *unlock_err = NULL;
1044 struct got_lockfile *lf = NULL;
1046 err = got_object_get_path(&path, id, a->repo);
1050 err = got_object_open_loose_fd(&fd, id, a->repo);
1054 if (fstat(fd, &sb) == -1) {
1055 err = got_error_from_errno("fstat");
1060 * Do not delete objects which are younger than our maximum
1061 * modification time threshold. This prevents a race where
1062 * new objects which are being added to the repository
1063 * concurrently would be deleted.
1065 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1067 err = got_lockfile_lock(&lf, path, -1);
1070 if (unlink(path) == -1) {
1071 err = got_error_from_errno2("unlink", path);
1077 a->size_purged += sb.st_size;
1078 if (a->progress_cb) {
1079 err = a->progress_cb(a->progress_arg, a->nloose,
1080 a->ncommits, a->npurged);
1084 if (fd != -1 && close(fd) == -1 && err == NULL)
1085 err = got_error_from_errno("close");
1088 unlock_err = got_lockfile_unlock(lf, -1);
1089 return err ? err : unlock_err;
1092 const struct got_error *
1093 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1094 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1095 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1096 got_cancel_cb cancel_cb, void *cancel_arg)
1098 const struct got_error *err;
1099 struct got_object_idset *loose_ids;
1100 struct got_object_idset *traversed_ids;
1101 struct got_object_id **referenced_ids;
1102 int i, nreferenced, nloose, ncommits = 0;
1103 struct got_reflist_head refs;
1104 struct got_reflist_entry *re;
1105 struct purge_loose_object_arg arg;
1106 time_t max_mtime = 0;
1114 err = get_loose_object_ids(&loose_ids, size_before,
1115 progress_cb, progress_arg, repo);
1118 nloose = got_object_idset_num_elements(loose_ids);
1120 got_object_idset_free(loose_ids);
1124 traversed_ids = got_object_idset_alloc();
1125 if (traversed_ids == NULL) {
1126 err = got_error_from_errno("got_object_idset_alloc");
1130 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1133 if (!ignore_mtime) {
1134 TAILQ_FOREACH(re, &refs, entry) {
1135 time_t mtime = got_ref_get_mtime(re->ref);
1136 if (mtime > max_mtime)
1140 * For safety, keep objects created within 10 minutes
1141 * before the youngest reference was created.
1143 if (max_mtime >= 600)
1147 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1148 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1149 &refs, repo, cancel_cb, cancel_arg);
1153 for (i = 0; i < nreferenced; i++) {
1154 struct got_object_id *id = referenced_ids[i];
1155 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1156 traversed_ids, id, repo, progress_cb, progress_arg, nloose,
1157 cancel_cb, cancel_arg);
1162 /* Produce a final progress report in case no objects can be purged. */
1163 if (got_object_idset_num_elements(loose_ids) == 0 && progress_cb) {
1164 err = progress_cb(progress_arg, nloose, ncommits, 0);
1169 /* Any remaining loose objects are unreferenced and can be purged. */
1171 arg.progress_arg = progress_arg;
1172 arg.progress_cb = progress_cb;
1173 arg.nloose = nloose;
1175 arg.size_purged = 0;
1176 arg.ncommits = ncommits;
1177 arg.dry_run = dry_run;
1178 arg.max_mtime = max_mtime;
1179 arg.ignore_mtime = ignore_mtime;
1180 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1183 *size_after = *size_before - arg.size_purged;
1185 got_object_idset_free(loose_ids);
1186 got_object_idset_free(traversed_ids);
1190 static const struct got_error *
1191 remove_packidx(int dir_fd, const char *relpath)
1193 const struct got_error *err, *unlock_err;
1194 struct got_lockfile *lf;
1196 err = got_lockfile_lock(&lf, relpath, dir_fd);
1199 if (unlinkat(dir_fd, relpath, 0) == -1)
1200 err = got_error_from_errno("unlinkat");
1201 unlock_err = got_lockfile_unlock(lf, dir_fd);
1202 return err ? err : unlock_err;
1205 const struct got_error *
1206 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1207 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1208 got_cancel_cb cancel_cb, void *cancel_arg)
1210 const struct got_error *err;
1211 DIR *packdir = NULL;
1212 struct dirent *dent;
1213 char *pack_relpath = NULL;
1217 packdir_fd = openat(got_repo_get_fd(repo),
1218 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1219 if (packdir_fd == -1) {
1220 if (errno == ENOENT)
1222 return got_error_from_errno_fmt("openat: %s/%s",
1223 got_repo_get_path_git_dir(repo),
1224 GOT_OBJECTS_PACK_DIR);
1227 packdir = fdopendir(packdir_fd);
1228 if (packdir == NULL) {
1229 err = got_error_from_errno("fdopendir");
1233 while ((dent = readdir(packdir)) != NULL) {
1235 err = cancel_cb(cancel_arg);
1240 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1243 err = got_packidx_get_packfile_path(&pack_relpath,
1248 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1250 pack_relpath = NULL;
1253 if (errno != ENOENT) {
1254 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1255 got_repo_get_path_git_dir(repo),
1256 GOT_OBJECTS_PACK_DIR,
1262 err = remove_packidx(packdir_fd, dent->d_name);
1268 if (asprintf(&path, "%s/%s/%s",
1269 got_repo_get_path_git_dir(repo),
1270 GOT_OBJECTS_PACK_DIR,
1271 dent->d_name) == -1) {
1272 err = got_error_from_errno("asprintf");
1275 err = progress_cb(progress_arg, path);
1281 pack_relpath = NULL;
1284 if (packdir && closedir(packdir) != 0 && err == NULL)
1285 err = got_error_from_errno("closedir");