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>
37 #include "got_error.h"
38 #include "got_cancel.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_repository_admin.h"
43 #include "got_opentemp.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_idset.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_pack_create.h"
54 #include "got_lib_sha1.h"
55 #include "got_lib_lockfile.h"
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 static const struct got_error *
62 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
63 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
64 struct got_repository *repo,
65 got_cancel_cb cancel_cb, void *cancel_arg)
67 const struct got_error *err = NULL;
68 const size_t alloc_chunksz = 256;
70 struct got_reflist_entry *re;
76 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
78 return got_error_from_errno("reallocarray");
79 nalloc = alloc_chunksz;
81 TAILQ_FOREACH(re, refs, entry) {
82 struct got_object_id *id;
85 err = cancel_cb(cancel_arg);
90 err = got_ref_resolve(&id, repo, re->ref);
94 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
96 err = got_object_get_type(&obj_type, repo, id);
99 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
106 if (nalloc <= *nobjects) {
107 struct got_object_id **new;
108 new = recallocarray(*ids, nalloc,
109 nalloc + alloc_chunksz,
110 sizeof(struct got_object_id *));
112 err = got_error_from_errno(
117 nalloc += alloc_chunksz;
119 (*ids)[*nobjects] = id;
120 if ((*ids)[*nobjects] == NULL) {
121 err = got_error_from_errno("got_object_id_dup");
128 for (i = 0; i < *nobjects; i++)
137 const struct got_error *
138 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
139 struct got_reflist_head *include_refs,
140 struct got_reflist_head *exclude_refs, struct got_repository *repo,
141 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
142 got_cancel_cb cancel_cb, void *cancel_arg)
144 const struct got_error *err = NULL;
145 struct got_object_id **ours = NULL, **theirs = NULL;
146 int nours = 0, ntheirs = 0, packfd = -1, i;
147 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
148 char *sha1_str = NULL;
153 if (asprintf(&path, "%s/%s/packing.pack",
154 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
155 err = got_error_from_errno("asprintf");
158 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
162 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
163 err = got_error_from_errno2("fchmod", tmpfile_path);
167 *packfile = fdopen(packfd, "w");
168 if (*packfile == NULL) {
169 err = got_error_from_errno2("fdopen", tmpfile_path);
174 err = get_reflist_object_ids(&ours, &nours,
175 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
176 include_refs, repo, cancel_cb, cancel_arg);
181 err = got_error(GOT_ERR_CANNOT_PACK);
185 if (!TAILQ_EMPTY(exclude_refs)) {
186 err = get_reflist_object_ids(&theirs, &ntheirs,
187 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
189 cancel_cb, cancel_arg);
194 *pack_hash = calloc(1, sizeof(**pack_hash));
195 if (*pack_hash == NULL) {
196 err = got_error_from_errno("calloc");
200 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
201 ours, nours, repo, loose_obj_only, progress_cb, progress_arg,
202 cancel_cb, cancel_arg);
206 err = got_object_id_str(&sha1_str, *pack_hash);
209 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
210 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
212 err = got_error_from_errno("asprintf");
216 if (fflush(*packfile) == -1) {
217 err = got_error_from_errno("fflush");
220 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
221 err = got_error_from_errno("fseek");
224 if (rename(tmpfile_path, packfile_path) == -1) {
225 err = got_error_from_errno3("rename", tmpfile_path,
232 for (i = 0; i < nours; i++)
235 for (i = 0; i < ntheirs; i++)
238 if (packfd != -1 && close(packfd) == -1 && err == NULL)
239 err = got_error_from_errno2("close", packfile_path);
240 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
241 err = got_error_from_errno2("unlink", tmpfile_path);
256 const struct got_error *
257 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
258 struct got_repository *repo,
259 got_pack_index_progress_cb progress_cb, void *progress_arg,
260 got_cancel_cb cancel_cb, void *cancel_arg)
265 int npackfd = -1, idxfd = -1, nidxfd = -1;
267 int idxstatus, done = 0;
268 const struct got_error *err;
269 struct imsgbuf idxibuf;
271 char *tmpidxpath = NULL;
272 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
273 const char *repo_path = got_repo_get_path_git_dir(repo);
276 for (i = 0; i < nitems(tmpfds); i++)
279 if (asprintf(&path, "%s/%s/indexing.idx",
280 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
281 err = got_error_from_errno("asprintf");
284 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
288 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
289 err = got_error_from_errno2("fchmod", tmpidxpath);
295 err = got_error_from_errno("dup");
299 for (i = 0; i < nitems(tmpfds); i++) {
300 tmpfds[i] = got_opentempfd();
301 if (tmpfds[i] == -1) {
302 err = got_error_from_errno("got_opentempfd");
307 err = got_object_id_str(&id_str, pack_hash);
311 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
312 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
313 err = got_error_from_errno("asprintf");
317 if (fstat(fileno(packfile), &sb) == -1) {
318 err = got_error_from_errno2("fstat", packfile_path);
322 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
323 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
324 err = got_error_from_errno("asprintf");
328 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
329 err = got_error_from_errno("socketpair");
334 err= got_error_from_errno("fork");
336 } else if (idxpid == 0)
337 got_privsep_exec_child(imsg_idxfds,
338 GOT_PATH_PROG_INDEX_PACK, packfile_path);
339 if (close(imsg_idxfds[1]) == -1) {
340 err = got_error_from_errno("close");
343 imsg_init(&idxibuf, imsg_idxfds[0]);
345 npackfd = dup(fileno(packfile));
347 err = got_error_from_errno("dup");
350 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
355 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
359 for (i = 0; i < nitems(tmpfds); i++) {
360 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
367 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
370 err = cancel_cb(cancel_arg);
375 err = got_privsep_recv_index_progress(&done, &nobj_total,
376 &nobj_indexed, &nobj_loose, &nobj_resolved,
380 if (nobj_indexed != 0) {
381 err = progress_cb(progress_arg, sb.st_size,
382 nobj_total, nobj_indexed, nobj_loose,
387 imsg_clear(&idxibuf);
389 if (close(imsg_idxfds[0]) == -1) {
390 err = got_error_from_errno("close");
393 if (waitpid(idxpid, &idxstatus, 0) == -1) {
394 err = got_error_from_errno("waitpid");
398 if (rename(tmpidxpath, idxpath) == -1) {
399 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
406 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
407 err = got_error_from_errno2("unlink", tmpidxpath);
408 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
409 err = got_error_from_errno("close");
410 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
411 err = got_error_from_errno("close");
412 for (i = 0; i < nitems(tmpfds); i++) {
413 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
414 err = got_error_from_errno("close");
422 const struct got_error *
423 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
424 struct got_repository *repo, const char *packfile_path)
426 const struct got_error *err = NULL;
427 const char *packdir_path = NULL;
428 char *packfile_name = NULL, *p, *dot;
429 struct got_object_id id;
435 packdir_path = got_repo_get_path_objects_pack(repo);
436 if (packdir_path == NULL)
437 return got_error_from_errno("got_repo_get_path_objects_pack");
439 if (!got_path_is_child(packfile_path, packdir_path,
440 strlen(packdir_path))) {
441 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
446 err = got_path_basename(&packfile_name, packfile_path);
451 if (strncmp(p, "pack-", 5) != 0) {
452 err = got_error_fmt(GOT_ERR_BAD_PATH,
453 "'%s' is not a valid pack file name",
458 dot = strchr(p, '.');
460 err = got_error_fmt(GOT_ERR_BAD_PATH,
461 "'%s' is not a valid pack file name",
465 if (strcmp(dot + 1, "pack") != 0) {
466 err = got_error_fmt(GOT_ERR_BAD_PATH,
467 "'%s' is not a valid pack file name",
472 if (!got_parse_sha1_digest(id.sha1, p)) {
473 err = got_error_fmt(GOT_ERR_BAD_PATH,
474 "'%s' is not a valid pack file name",
479 *pack_hash = got_object_id_dup(&id);
480 if (*pack_hash == NULL) {
481 err = got_error_from_errno("got_object_id_dup");
485 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW);
487 err = got_error_from_errno2("open", packfile_path);
491 *packfile = fdopen(packfd, "r");
492 if (*packfile == NULL) {
493 err = got_error_from_errno2("fdopen", packfile_path);
498 if (packfd != -1 && close(packfd) == -1 && err == NULL)
499 err = got_error_from_errno2("close", packfile_path);
508 const struct got_error *
509 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
510 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
511 got_cancel_cb cancel_cb, void *cancel_arg)
513 const struct got_error *err = NULL;
514 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
515 struct got_packidx *packidx = NULL;
516 struct got_pack *pack = NULL;
519 err = got_object_id_str(&id_str, pack_hash);
523 if (asprintf(&packpath, "%s/pack-%s.pack",
524 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
525 err = got_error_from_errno("asprintf");
528 if (asprintf(&idxpath, "%s/pack-%s.idx",
529 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
530 err = got_error_from_errno("asprintf");
534 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
538 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
542 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
543 for (i = 0; i < nobj; i++) {
544 struct got_packidx_object_id *oid;
545 struct got_object_id id, base_id;
546 off_t offset, base_offset = 0;
552 err = cancel_cb(cancel_arg);
556 oid = &packidx->hdr.sorted_ids[i];
557 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
559 offset = got_packidx_get_object_offset(packidx, i);
561 err = got_error(GOT_ERR_BAD_PACKIDX);
565 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
571 case GOT_OBJ_TYPE_OFFSET_DELTA:
572 err = got_pack_parse_offset_delta(&base_offset, &len,
573 pack, offset, tslen);
577 case GOT_OBJ_TYPE_REF_DELTA:
578 err = got_pack_parse_ref_delta(&base_id,
579 pack, offset, tslen);
584 err = (*list_cb)(list_arg, &id, type, offset, size,
585 base_offset, &base_id);
595 got_packidx_close(packidx);
599 static const struct got_error *
600 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
601 got_cleanup_progress_cb progress_cb, void *progress_arg,
602 struct got_repository *repo)
604 const struct got_error *err = NULL;
605 char *path_objects = NULL, *path = NULL;
607 struct got_object *obj = NULL;
608 struct got_object_id id;
613 *loose_ids = got_object_idset_alloc();
614 if (*loose_ids == NULL)
615 return got_error_from_errno("got_object_idset_alloc");
617 path_objects = got_repo_get_path_objects(repo);
618 if (path_objects == NULL) {
619 err = got_error_from_errno("got_repo_get_path_objects");
623 for (i = 0; i <= 0xff; i++) {
626 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
627 err = got_error_from_errno("asprintf");
633 if (errno == ENOENT) {
637 err = got_error_from_errno2("opendir", path);
641 while ((dent = readdir(dir)) != NULL) {
644 if (strcmp(dent->d_name, ".") == 0 ||
645 strcmp(dent->d_name, "..") == 0)
648 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
649 err = got_error_from_errno("asprintf");
653 memset(&id, 0, sizeof(id));
654 if (!got_parse_sha1_digest(id.sha1, id_str)) {
660 err = got_object_open_loose_fd(&fd, &id, repo);
663 if (fstat(fd, &sb) == -1) {
664 err = got_error_from_errno("fstat");
667 err = got_object_read_header_privsep(&obj, repo, fd);
670 fd = -1; /* already closed */
673 case GOT_OBJ_TYPE_COMMIT:
674 case GOT_OBJ_TYPE_TREE:
675 case GOT_OBJ_TYPE_BLOB:
676 case GOT_OBJ_TYPE_TAG:
679 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
683 got_object_close(obj);
685 (*ondisk_size) += sb.st_size;
686 err = got_object_idset_add(*loose_ids, &id, NULL);
690 err = progress_cb(progress_arg,
691 got_object_idset_num_elements(*loose_ids),
698 if (closedir(dir) != 0) {
699 err = got_error_from_errno("closedir");
708 if (dir && closedir(dir) != 0 && err == NULL)
709 err = got_error_from_errno("closedir");
710 if (fd != -1 && close(fd) == -1 && err == NULL)
711 err = got_error_from_errno("close");
713 got_object_idset_free(*loose_ids);
717 got_object_close(obj);
723 static const struct got_error *
724 preserve_loose_object(struct got_object_idset *loose_ids,
725 struct got_object_id *id, struct got_repository *repo, int *npacked)
727 const struct got_error *err = NULL;
728 struct got_object *obj;
730 if (!got_object_idset_contains(loose_ids, id))
734 * Try to open this object from a pack file. This ensures that
735 * we do in fact have a valid packed copy of the object. Otherwise
736 * we should not delete the loose representation of this object.
738 err = got_object_open_packed(&obj, id, repo);
740 got_object_close(obj);
742 * The object is referenced and packed.
743 * We can purge the redundantly stored loose object.
747 } else if (err->code != GOT_ERR_NO_OBJ)
751 * This object is referenced and not packed.
752 * Remove it from our purge set.
754 return got_object_idset_remove(NULL, loose_ids, id);
757 static const struct got_error *
758 load_tree_entries(struct got_object_id_queue *ids,
759 struct got_object_idset *loose_ids,
760 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
761 const char *dpath, struct got_repository *repo, int *npacked,
762 got_cancel_cb cancel_cb, void *cancel_arg)
764 const struct got_error *err;
765 struct got_tree_object *tree;
769 err = got_object_open_as_tree(&tree, repo, tree_id);
773 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
774 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
775 struct got_object_id *id = got_tree_entry_get_id(e);
776 mode_t mode = got_tree_entry_get_mode(e);
779 err = (*cancel_cb)(cancel_arg);
784 if (got_object_tree_entry_is_symlink(e) ||
785 got_object_tree_entry_is_submodule(e) ||
786 got_object_idset_contains(traversed_ids, id))
789 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
790 got_tree_entry_get_name(e)) == -1) {
791 err = got_error_from_errno("asprintf");
796 struct got_object_qid *qid;
797 err = got_object_qid_alloc(&qid, id);
800 STAILQ_INSERT_TAIL(ids, qid, entry);
801 } else if (S_ISREG(mode)) {
802 /* This blob is referenced. */
803 err = preserve_loose_object(loose_ids, id, repo,
807 err = got_object_idset_add(traversed_ids, id, NULL);
816 got_object_tree_close(tree);
821 static const struct got_error *
822 load_tree(struct got_object_idset *loose_ids,
823 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
824 const char *dpath, struct got_repository *repo, int *npacked,
825 got_cancel_cb cancel_cb, void *cancel_arg)
827 const struct got_error *err = NULL;
828 struct got_object_id_queue tree_ids;
829 struct got_object_qid *qid;
831 err = got_object_qid_alloc(&qid, tree_id);
835 STAILQ_INIT(&tree_ids);
836 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
838 while (!STAILQ_EMPTY(&tree_ids)) {
840 err = (*cancel_cb)(cancel_arg);
845 qid = STAILQ_FIRST(&tree_ids);
846 STAILQ_REMOVE_HEAD(&tree_ids, entry);
848 if (got_object_idset_contains(traversed_ids, qid->id)) {
849 got_object_qid_free(qid);
853 err = got_object_idset_add(traversed_ids, qid->id, NULL);
855 got_object_qid_free(qid);
859 /* This tree is referenced. */
860 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
864 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
865 qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
866 got_object_qid_free(qid);
871 got_object_id_queue_free(&tree_ids);
875 static const struct got_error *
876 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
877 int *npacked, struct got_object_idset *traversed_ids,
878 struct got_object_id *id, struct got_repository *repo,
879 got_cleanup_progress_cb progress_cb, void *progress_arg, int nloose,
880 got_cancel_cb cancel_cb, void *cancel_arg)
882 const struct got_error *err;
883 struct got_commit_object *commit = NULL;
884 struct got_tag_object *tag = NULL;
885 struct got_object_id *tree_id = NULL;
886 struct got_object_id_queue ids;
887 struct got_object_qid *qid;
890 err = got_object_qid_alloc(&qid, id);
895 STAILQ_INSERT_TAIL(&ids, qid, entry);
897 while (!STAILQ_EMPTY(&ids)) {
899 err = (*cancel_cb)(cancel_arg);
904 qid = STAILQ_FIRST(&ids);
905 STAILQ_REMOVE_HEAD(&ids, entry);
907 if (got_object_idset_contains(traversed_ids, qid->id)) {
908 got_object_qid_free(qid);
913 err = got_object_idset_add(traversed_ids, qid->id, NULL);
917 /* This commit or tag is referenced. */
918 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
922 err = got_object_get_type(&obj_type, repo, qid->id);
926 case GOT_OBJ_TYPE_COMMIT:
927 err = got_object_open_as_commit(&commit, repo, qid->id);
931 case GOT_OBJ_TYPE_TAG:
932 err = got_object_open_as_tag(&tag, repo, qid->id);
937 /* should not happen */
938 err = got_error(GOT_ERR_OBJ_TYPE);
942 /* Find a tree object to scan. */
944 tree_id = got_object_commit_get_tree_id(commit);
946 obj_type = got_object_tag_get_object_type(tag);
948 case GOT_OBJ_TYPE_COMMIT:
949 err = got_object_open_as_commit(&commit, repo,
950 got_object_tag_get_object_id(tag));
953 tree_id = got_object_commit_get_tree_id(commit);
955 case GOT_OBJ_TYPE_TREE:
956 tree_id = got_object_tag_get_object_id(tag);
960 * Tag points at something other than a
961 * commit or tree. Leave this weird tag object
962 * and the object it points to on disk.
964 err = got_object_idset_remove(NULL, loose_ids,
966 if (err && err->code != GOT_ERR_NO_OBJ)
968 err = got_object_idset_remove(NULL, loose_ids,
969 got_object_tag_get_object_id(tag));
970 if (err && err->code != GOT_ERR_NO_OBJ)
978 err = load_tree(loose_ids, traversed_ids, tree_id, "",
979 repo, npacked, cancel_cb, cancel_arg);
985 (*ncommits)++; /* scanned tags are counted as commits */
988 err = progress_cb(progress_arg, nloose, *ncommits, -1);
994 /* Find parent commits to scan. */
995 const struct got_object_id_queue *parent_ids;
996 parent_ids = got_object_commit_get_parent_ids(commit);
997 err = got_object_id_queue_copy(parent_ids, &ids);
1000 got_object_commit_close(commit);
1004 got_object_tag_close(tag);
1007 got_object_qid_free(qid);
1012 got_object_qid_free(qid);
1014 got_object_commit_close(commit);
1016 got_object_tag_close(tag);
1017 got_object_id_queue_free(&ids);
1021 struct purge_loose_object_arg {
1022 struct got_repository *repo;
1023 got_cleanup_progress_cb progress_cb;
1032 static const struct got_error *
1033 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1035 struct purge_loose_object_arg *a = arg;
1036 const struct got_error *err, *unlock_err = NULL;
1040 struct got_lockfile *lf = NULL;
1042 err = got_object_get_path(&path, id, a->repo);
1046 err = got_object_open_loose_fd(&fd, id, a->repo);
1050 if (fstat(fd, &sb) == -1) {
1051 err = got_error_from_errno("fstat");
1056 err = got_lockfile_lock(&lf, path, -1);
1059 if (unlink(path) == -1) {
1060 err = got_error_from_errno2("unlink", path);
1066 a->size_purged += sb.st_size;
1067 if (a->progress_cb) {
1068 err = a->progress_cb(a->progress_arg, a->nloose,
1069 a->ncommits, a->npurged);
1072 if (fd != -1 && close(fd) == -1 && err == NULL)
1073 err = got_error_from_errno("close");
1076 unlock_err = got_lockfile_unlock(lf, -1);
1077 return err ? err : unlock_err;
1080 const struct got_error *
1081 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1082 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1083 got_cleanup_progress_cb progress_cb, void *progress_arg,
1084 got_cancel_cb cancel_cb, void *cancel_arg)
1086 const struct got_error *err;
1087 struct got_object_idset *loose_ids;
1088 struct got_object_idset *traversed_ids;
1089 struct got_object_id **referenced_ids;
1090 int i, nreferenced, nloose, ncommits = 0;
1091 struct got_reflist_head refs;
1092 struct purge_loose_object_arg arg;
1100 err = get_loose_object_ids(&loose_ids, size_before,
1101 progress_cb, progress_arg, repo);
1104 nloose = got_object_idset_num_elements(loose_ids);
1106 got_object_idset_free(loose_ids);
1110 traversed_ids = got_object_idset_alloc();
1111 if (traversed_ids == NULL) {
1112 err = got_error_from_errno("got_object_idset_alloc");
1116 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1120 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1121 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1122 &refs, repo, cancel_cb, cancel_arg);
1126 for (i = 0; i < nreferenced; i++) {
1127 struct got_object_id *id = referenced_ids[i];
1128 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1129 traversed_ids, id, repo, progress_cb, progress_arg, nloose,
1130 cancel_cb, cancel_arg);
1135 /* Produce a final progress report in case no objects can be purged. */
1136 if (got_object_idset_num_elements(loose_ids) == 0 && progress_cb) {
1137 err = progress_cb(progress_arg, nloose, ncommits, 0);
1142 /* Any remaining loose objects are unreferenced and can be purged. */
1144 arg.progress_arg = progress_arg;
1145 arg.progress_cb = progress_cb;
1146 arg.nloose = nloose;
1148 arg.size_purged = 0;
1149 arg.ncommits = ncommits;
1150 arg.dry_run = dry_run;
1151 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1154 *size_after = *size_before - arg.size_purged;
1156 got_object_idset_free(loose_ids);
1157 got_object_idset_free(traversed_ids);
1161 static const struct got_error *
1162 remove_packidx(int dir_fd, const char *relpath)
1164 const struct got_error *err, *unlock_err;
1165 struct got_lockfile *lf;
1167 err = got_lockfile_lock(&lf, relpath, dir_fd);
1170 if (unlinkat(dir_fd, relpath, 0) == -1)
1171 err = got_error_from_errno("unlinkat");
1172 unlock_err = got_lockfile_unlock(lf, dir_fd);
1173 return err ? err : unlock_err;
1176 const struct got_error *
1177 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1178 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1179 got_cancel_cb cancel_cb, void *cancel_arg)
1181 const struct got_error *err;
1182 DIR *packdir = NULL;
1183 struct dirent *dent;
1184 char *pack_relpath = NULL;
1188 packdir_fd = openat(got_repo_get_fd(repo),
1189 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1190 if (packdir_fd == -1) {
1191 if (errno == ENOENT)
1193 return got_error_from_errno_fmt("openat: %s/%s",
1194 got_repo_get_path_git_dir(repo),
1195 GOT_OBJECTS_PACK_DIR);
1198 packdir = fdopendir(packdir_fd);
1199 if (packdir == NULL) {
1200 err = got_error_from_errno("fdopendir");
1204 while ((dent = readdir(packdir)) != NULL) {
1206 err = cancel_cb(cancel_arg);
1211 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1214 err = got_packidx_get_packfile_path(&pack_relpath,
1219 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1221 pack_relpath = NULL;
1224 if (errno != ENOENT) {
1225 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1226 got_repo_get_path_git_dir(repo),
1227 GOT_OBJECTS_PACK_DIR,
1233 err = remove_packidx(packdir_fd, dent->d_name);
1239 if (asprintf(&path, "%s/%s/%s",
1240 got_repo_get_path_git_dir(repo),
1241 GOT_OBJECTS_PACK_DIR,
1242 dent->d_name) == -1) {
1243 err = got_error_from_errno("asprintf");
1246 err = progress_cb(progress_arg, path);
1252 pack_relpath = NULL;
1255 if (packdir && closedir(packdir) != 0 && err == NULL)
1256 err = got_error_from_errno("closedir");