Blob


1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
25 #include <dirent.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <sha2.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <unistd.h>
37 #include <imsg.h>
39 #include "got_error.h"
40 #include "got_cancel.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_repository_admin.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_idset.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_repository.h"
55 #include "got_lib_ratelimit.h"
56 #include "got_lib_pack_create.h"
57 #include "got_lib_hash.h"
58 #include "got_lib_lockfile.h"
60 #ifndef nitems
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #endif
64 static const struct got_error *
65 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
66 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
67 struct got_repository *repo,
68 got_cancel_cb cancel_cb, void *cancel_arg)
69 {
70 const struct got_error *err = NULL;
71 const size_t alloc_chunksz = 256;
72 size_t nalloc;
73 struct got_reflist_entry *re;
74 int i;
76 *ids = NULL;
77 *nobjects = 0;
79 err = got_reflist_sort(refs,
80 got_ref_cmp_by_commit_timestamp_descending, repo);
81 if (err)
82 return err;
84 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
85 if (*ids == NULL)
86 return got_error_from_errno("reallocarray");
87 nalloc = alloc_chunksz;
89 TAILQ_FOREACH(re, refs, entry) {
90 struct got_object_id *id;
92 if (cancel_cb) {
93 err = cancel_cb(cancel_arg);
94 if (err)
95 goto done;
96 }
98 err = got_ref_resolve(&id, repo, re->ref);
99 if (err)
100 goto done;
102 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
103 int obj_type;
104 err = got_object_get_type(&obj_type, repo, id);
105 if (err)
106 goto done;
107 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
108 free(id);
109 id = NULL;
110 continue;
114 if (nalloc <= *nobjects) {
115 struct got_object_id **new;
116 new = recallocarray(*ids, nalloc,
117 nalloc + alloc_chunksz,
118 sizeof(struct got_object_id *));
119 if (new == NULL) {
120 err = got_error_from_errno(
121 "recallocarray");
122 goto done;
124 *ids = new;
125 nalloc += alloc_chunksz;
127 (*ids)[*nobjects] = id;
128 if ((*ids)[*nobjects] == NULL) {
129 err = got_error_from_errno("got_object_id_dup");
130 goto done;
132 (*nobjects)++;
134 done:
135 if (err) {
136 for (i = 0; i < *nobjects; i++)
137 free((*ids)[i]);
138 free(*ids);
139 *ids = NULL;
140 *nobjects = 0;
142 return err;
145 const struct got_error *
146 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
147 struct got_reflist_head *include_refs,
148 struct got_reflist_head *exclude_refs, struct got_repository *repo,
149 int loose_obj_only, int force_refdelta,
150 got_pack_progress_cb progress_cb, void *progress_arg,
151 got_cancel_cb cancel_cb, void *cancel_arg)
153 const struct got_error *err = NULL;
154 struct got_object_id **ours = NULL, **theirs = NULL;
155 int nours = 0, ntheirs = 0, packfd = -1, i;
156 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
157 char *sha1_str = NULL;
158 FILE *delta_cache = NULL;
159 struct got_ratelimit rl;
161 *packfile = NULL;
162 *pack_hash = NULL;
164 got_ratelimit_init(&rl, 0, 500);
166 if (asprintf(&path, "%s/%s/packing.pack",
167 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
168 err = got_error_from_errno("asprintf");
169 goto done;
171 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path, "");
172 if (err)
173 goto done;
175 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
176 err = got_error_from_errno2("fchmod", tmpfile_path);
177 goto done;
180 delta_cache = got_opentemp();
181 if (delta_cache == NULL) {
182 err = got_error_from_errno("got_opentemp");
183 goto done;
186 err = get_reflist_object_ids(&ours, &nours,
187 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
188 include_refs, repo, cancel_cb, cancel_arg);
189 if (err)
190 goto done;
192 if (nours == 0) {
193 err = got_error(GOT_ERR_CANNOT_PACK);
194 goto done;
197 if (!TAILQ_EMPTY(exclude_refs)) {
198 err = get_reflist_object_ids(&theirs, &ntheirs,
199 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
200 exclude_refs, repo,
201 cancel_cb, cancel_arg);
202 if (err)
203 goto done;
206 *pack_hash = calloc(1, sizeof(**pack_hash));
207 if (*pack_hash == NULL) {
208 err = got_error_from_errno("calloc");
209 goto done;
212 err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
213 theirs, ntheirs, ours, nours, repo, loose_obj_only,
214 0, force_refdelta, progress_cb, progress_arg, &rl,
215 cancel_cb, cancel_arg);
216 if (err)
217 goto done;
219 err = got_object_id_str(&sha1_str, *pack_hash);
220 if (err)
221 goto done;
222 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
223 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
224 sha1_str) == -1) {
225 err = got_error_from_errno("asprintf");
226 goto done;
229 if (lseek(packfd, 0L, SEEK_SET) == -1) {
230 err = got_error_from_errno("lseek");
231 goto done;
233 if (rename(tmpfile_path, packfile_path) == -1) {
234 err = got_error_from_errno3("rename", tmpfile_path,
235 packfile_path);
236 goto done;
238 free(tmpfile_path);
239 tmpfile_path = NULL;
241 *packfile = fdopen(packfd, "w");
242 if (*packfile == NULL) {
243 err = got_error_from_errno2("fdopen", tmpfile_path);
244 goto done;
246 packfd = -1;
247 done:
248 for (i = 0; i < nours; i++)
249 free(ours[i]);
250 free(ours);
251 for (i = 0; i < ntheirs; i++)
252 free(theirs[i]);
253 free(theirs);
254 if (packfd != -1 && close(packfd) == -1 && err == NULL)
255 err = got_error_from_errno2("close", packfile_path);
256 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
257 err = got_error_from_errno("fclose");
258 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
259 err = got_error_from_errno2("unlink", tmpfile_path);
260 free(tmpfile_path);
261 free(packfile_path);
262 free(sha1_str);
263 free(path);
264 if (err) {
265 free(*pack_hash);
266 *pack_hash = NULL;
267 if (*packfile)
268 fclose(*packfile);
269 *packfile = NULL;
271 return err;
274 const struct got_error *
275 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
276 struct got_repository *repo,
277 got_pack_index_progress_cb progress_cb, void *progress_arg,
278 got_cancel_cb cancel_cb, void *cancel_arg)
280 size_t i;
281 char *path;
282 int imsg_idxfds[2];
283 int npackfd = -1, idxfd = -1, nidxfd = -1;
284 int tmpfds[3];
285 int idxstatus, done = 0;
286 const struct got_error *err;
287 struct imsgbuf idxibuf;
288 pid_t idxpid;
289 char *tmpidxpath = NULL;
290 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
291 const char *repo_path = got_repo_get_path_git_dir(repo);
292 struct stat sb;
294 for (i = 0; i < nitems(tmpfds); i++)
295 tmpfds[i] = -1;
297 if (asprintf(&path, "%s/%s/indexing.idx",
298 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
299 err = got_error_from_errno("asprintf");
300 goto done;
302 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
303 free(path);
304 if (err)
305 goto done;
306 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
307 err = got_error_from_errno2("fchmod", tmpidxpath);
308 goto done;
311 nidxfd = dup(idxfd);
312 if (nidxfd == -1) {
313 err = got_error_from_errno("dup");
314 goto done;
317 for (i = 0; i < nitems(tmpfds); i++) {
318 tmpfds[i] = got_opentempfd();
319 if (tmpfds[i] == -1) {
320 err = got_error_from_errno("got_opentempfd");
321 goto done;
325 err = got_object_id_str(&id_str, pack_hash);
326 if (err)
327 goto done;
329 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
330 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
331 err = got_error_from_errno("asprintf");
332 goto done;
335 if (fstat(fileno(packfile), &sb) == -1) {
336 err = got_error_from_errno2("fstat", packfile_path);
337 goto done;
340 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
341 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
342 err = got_error_from_errno("asprintf");
343 goto done;
346 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
347 err = got_error_from_errno("socketpair");
348 goto done;
350 idxpid = fork();
351 if (idxpid == -1) {
352 err= got_error_from_errno("fork");
353 goto done;
354 } else if (idxpid == 0)
355 got_privsep_exec_child(imsg_idxfds,
356 GOT_PATH_PROG_INDEX_PACK, packfile_path);
357 if (close(imsg_idxfds[1]) == -1) {
358 err = got_error_from_errno("close");
359 goto done;
361 imsg_init(&idxibuf, imsg_idxfds[0]);
363 npackfd = dup(fileno(packfile));
364 if (npackfd == -1) {
365 err = got_error_from_errno("dup");
366 goto done;
368 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
369 npackfd);
370 if (err != NULL)
371 goto done;
372 npackfd = -1;
373 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
374 if (err != NULL)
375 goto done;
376 nidxfd = -1;
377 for (i = 0; i < nitems(tmpfds); i++) {
378 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
379 if (err != NULL)
380 goto done;
381 tmpfds[i] = -1;
383 done = 0;
384 while (!done) {
385 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
387 if (cancel_cb) {
388 err = cancel_cb(cancel_arg);
389 if (err)
390 goto done;
393 err = got_privsep_recv_index_progress(&done, &nobj_total,
394 &nobj_indexed, &nobj_loose, &nobj_resolved,
395 &idxibuf);
396 if (err != NULL)
397 goto done;
398 if (nobj_indexed != 0) {
399 err = progress_cb(progress_arg, sb.st_size,
400 nobj_total, nobj_indexed, nobj_loose,
401 nobj_resolved);
402 if (err)
403 break;
406 if (close(imsg_idxfds[0]) == -1) {
407 err = got_error_from_errno("close");
408 goto done;
410 if (waitpid(idxpid, &idxstatus, 0) == -1) {
411 err = got_error_from_errno("waitpid");
412 goto done;
415 if (rename(tmpidxpath, idxpath) == -1) {
416 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
417 goto done;
419 free(tmpidxpath);
420 tmpidxpath = NULL;
422 done:
423 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
424 err = got_error_from_errno2("unlink", tmpidxpath);
425 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
426 err = got_error_from_errno("close");
427 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
428 err = got_error_from_errno("close");
429 for (i = 0; i < nitems(tmpfds); i++) {
430 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
431 err = got_error_from_errno("close");
433 free(tmpidxpath);
434 free(idxpath);
435 free(packfile_path);
436 return err;
439 const struct got_error *
440 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
441 struct got_repository *repo, const char *packfile_path)
443 const struct got_error *err = NULL;
444 const char *packdir_path = NULL;
445 char *packfile_name = NULL, *p, *dot;
446 struct got_object_id id;
447 int packfd = -1;
449 *packfile = NULL;
450 *pack_hash = NULL;
452 packdir_path = got_repo_get_path_objects_pack(repo);
453 if (packdir_path == NULL)
454 return got_error_from_errno("got_repo_get_path_objects_pack");
456 if (!got_path_is_child(packfile_path, packdir_path,
457 strlen(packdir_path))) {
458 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
459 goto done;
463 err = got_path_basename(&packfile_name, packfile_path);
464 if (err)
465 goto done;
466 p = packfile_name;
468 if (strncmp(p, "pack-", 5) != 0) {
469 err = got_error_fmt(GOT_ERR_BAD_PATH,
470 "'%s' is not a valid pack file name",
471 packfile_name);
472 goto done;
474 p += 5;
475 dot = strchr(p, '.');
476 if (dot == NULL) {
477 err = got_error_fmt(GOT_ERR_BAD_PATH,
478 "'%s' is not a valid pack file name",
479 packfile_name);
480 goto done;
482 if (strcmp(dot + 1, "pack") != 0) {
483 err = got_error_fmt(GOT_ERR_BAD_PATH,
484 "'%s' is not a valid pack file name",
485 packfile_name);
486 goto done;
488 *dot = '\0';
489 if (!got_parse_object_id(&id, p, repo->algo)) {
490 err = got_error_fmt(GOT_ERR_BAD_PATH,
491 "'%s' is not a valid pack file name",
492 packfile_name);
493 goto done;
496 *pack_hash = got_object_id_dup(&id);
497 if (*pack_hash == NULL) {
498 err = got_error_from_errno("got_object_id_dup");
499 goto done;
502 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
503 if (packfd == -1) {
504 err = got_error_from_errno2("open", packfile_path);
505 goto done;
508 *packfile = fdopen(packfd, "r");
509 if (*packfile == NULL) {
510 err = got_error_from_errno2("fdopen", packfile_path);
511 goto done;
513 packfd = -1;
514 done:
515 if (packfd != -1 && close(packfd) == -1 && err == NULL)
516 err = got_error_from_errno2("close", packfile_path);
517 free(packfile_name);
518 if (err) {
519 free(*pack_hash);
520 *pack_hash = NULL;
522 return err;
525 const struct got_error *
526 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
527 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
528 got_cancel_cb cancel_cb, void *cancel_arg)
530 const struct got_error *err = NULL;
531 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
532 struct got_packidx *packidx = NULL;
533 struct got_pack *pack = NULL;
534 uint32_t nobj, i;
536 err = got_object_id_str(&id_str, pack_hash);
537 if (err)
538 goto done;
540 if (asprintf(&packpath, "%s/pack-%s.pack",
541 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
542 err = got_error_from_errno("asprintf");
543 goto done;
545 if (asprintf(&idxpath, "%s/pack-%s.idx",
546 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
551 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
552 if (err)
553 goto done;
555 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
556 if (err)
557 goto done;
559 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
560 for (i = 0; i < nobj; i++) {
561 struct got_packidx_object_id *oid;
562 struct got_object_id id, base_id;
563 off_t offset, base_offset = 0;
564 uint8_t type;
565 uint64_t size;
566 size_t tslen, len;
568 if (cancel_cb) {
569 err = cancel_cb(cancel_arg);
570 if (err)
571 break;
573 oid = &packidx->hdr.sorted_ids[i];
574 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
576 offset = got_packidx_get_object_offset(packidx, i);
577 if (offset == -1) {
578 err = got_error(GOT_ERR_BAD_PACKIDX);
579 goto done;
582 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
583 pack, offset);
584 if (err)
585 goto done;
587 switch (type) {
588 case GOT_OBJ_TYPE_OFFSET_DELTA:
589 err = got_pack_parse_offset_delta(&base_offset, &len,
590 pack, offset, tslen);
591 if (err)
592 goto done;
593 break;
594 case GOT_OBJ_TYPE_REF_DELTA:
595 err = got_pack_parse_ref_delta(&base_id,
596 pack, offset, tslen);
597 if (err)
598 goto done;
599 break;
601 err = (*list_cb)(list_arg, &id, type, offset, size,
602 base_offset, &base_id);
603 if (err)
604 goto done;
607 done:
608 free(id_str);
609 free(idxpath);
610 free(packpath);
611 if (packidx)
612 got_packidx_close(packidx);
613 return err;
616 static const struct got_error *
617 repo_cleanup_lock(struct got_repository *repo, struct got_lockfile **lk)
619 const struct got_error *err;
620 char myname[_POSIX_HOST_NAME_MAX + 1];
622 if (gethostname(myname, sizeof(myname)) == -1)
623 return got_error_from_errno("gethostname");
625 err = got_lockfile_lock(lk, "gc.pid", got_repo_get_fd(repo));
626 if (err)
627 return err;
629 /*
630 * Git uses these info to provide some verbiage when finds a
631 * lock during `git gc --force' so don't try too hard to avoid
632 * short writes and don't care if a race happens between the
633 * lockfile creation and the write itself.
634 */
635 if (dprintf((*lk)->fd, "%d %s", getpid(), myname) < 0)
636 return got_error_from_errno("dprintf");
638 return NULL;
641 static const struct got_error *
642 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
643 void *progress_arg, struct got_ratelimit *rl,
644 int ncommits, int nloose, int npurged, int nredundant)
646 const struct got_error *err;
647 int elapsed;
649 if (progress_cb == NULL)
650 return NULL;
652 err = got_ratelimit_check(&elapsed, rl);
653 if (err || !elapsed)
654 return err;
656 return progress_cb(progress_arg, ncommits, nloose, npurged,
657 nredundant);
660 static const struct got_error *
661 get_loose_object_ids(struct got_object_idset **loose_ids,
662 off_t *ondisk_size, int ncommits,
663 got_cleanup_progress_cb progress_cb, void *progress_arg,
664 struct got_ratelimit *rl, struct got_repository *repo)
666 const struct got_error *err = NULL;
667 char *path_objects = NULL, *path = NULL;
668 DIR *dir = NULL;
669 struct got_object *obj = NULL;
670 struct got_object_id id;
671 int i, fd = -1;
672 struct stat sb;
674 *ondisk_size = 0;
675 *loose_ids = got_object_idset_alloc();
676 if (*loose_ids == NULL)
677 return got_error_from_errno("got_object_idset_alloc");
679 path_objects = got_repo_get_path_objects(repo);
680 if (path_objects == NULL) {
681 err = got_error_from_errno("got_repo_get_path_objects");
682 goto done;
685 for (i = 0; i <= 0xff; i++) {
686 struct dirent *dent;
688 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
689 err = got_error_from_errno("asprintf");
690 break;
693 dir = opendir(path);
694 if (dir == NULL) {
695 if (errno == ENOENT) {
696 err = NULL;
697 continue;
699 err = got_error_from_errno2("opendir", path);
700 break;
703 while ((dent = readdir(dir)) != NULL) {
704 char *id_str;
706 if (strcmp(dent->d_name, ".") == 0 ||
707 strcmp(dent->d_name, "..") == 0)
708 continue;
710 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
711 err = got_error_from_errno("asprintf");
712 goto done;
715 if (!got_parse_object_id(&id, id_str, repo->algo)) {
716 free(id_str);
717 continue;
719 free(id_str);
721 err = got_object_open_loose_fd(&fd, &id, repo);
722 if (err)
723 goto done;
724 if (fstat(fd, &sb) == -1) {
725 err = got_error_from_errno("fstat");
726 goto done;
728 err = got_object_read_header_privsep(&obj, &id, repo,
729 fd);
730 if (err)
731 goto done;
732 fd = -1; /* already closed */
734 switch (obj->type) {
735 case GOT_OBJ_TYPE_COMMIT:
736 case GOT_OBJ_TYPE_TREE:
737 case GOT_OBJ_TYPE_BLOB:
738 case GOT_OBJ_TYPE_TAG:
739 break;
740 default:
741 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
742 "%d", obj->type);
743 goto done;
745 got_object_close(obj);
746 obj = NULL;
747 (*ondisk_size) += sb.st_size;
748 err = got_object_idset_add(*loose_ids, &id, NULL);
749 if (err)
750 goto done;
751 err = report_cleanup_progress(progress_cb,
752 progress_arg, rl, ncommits,
753 got_object_idset_num_elements(*loose_ids),
754 -1, -1);
755 if (err)
756 goto done;
759 if (closedir(dir) != 0) {
760 err = got_error_from_errno("closedir");
761 goto done;
763 dir = NULL;
765 free(path);
766 path = NULL;
768 done:
769 if (dir && closedir(dir) != 0 && err == NULL)
770 err = got_error_from_errno("closedir");
771 if (fd != -1 && close(fd) == -1 && err == NULL)
772 err = got_error_from_errno("close");
773 if (err) {
774 got_object_idset_free(*loose_ids);
775 *loose_ids = NULL;
777 if (obj)
778 got_object_close(obj);
779 free(path_objects);
780 free(path);
781 return err;
784 static const struct got_error *
785 load_tree_entries(struct got_object_id_queue *ids,
786 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
787 const char *dpath, struct got_repository *repo,
788 got_cancel_cb cancel_cb, void *cancel_arg)
790 const struct got_error *err;
791 struct got_tree_object *tree;
792 char *p = NULL;
793 int i;
795 err = got_object_open_as_tree(&tree, repo, tree_id);
796 if (err)
797 return err;
799 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
800 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
801 struct got_object_id *id = got_tree_entry_get_id(e);
802 mode_t mode = got_tree_entry_get_mode(e);
804 if (cancel_cb) {
805 err = (*cancel_cb)(cancel_arg);
806 if (err)
807 break;
810 if (got_object_tree_entry_is_symlink(e) ||
811 got_object_tree_entry_is_submodule(e) ||
812 got_object_idset_contains(traversed_ids, id))
813 continue;
815 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
816 got_tree_entry_get_name(e)) == -1) {
817 err = got_error_from_errno("asprintf");
818 break;
821 if (S_ISDIR(mode)) {
822 struct got_object_qid *qid;
823 err = got_object_qid_alloc(&qid, id);
824 if (err)
825 break;
826 STAILQ_INSERT_TAIL(ids, qid, entry);
827 } else if (S_ISREG(mode)) {
828 /* This blob is referenced. */
829 err = got_object_idset_add(traversed_ids, id, NULL);
830 if (err)
831 break;
833 free(p);
834 p = NULL;
837 got_object_tree_close(tree);
838 free(p);
839 return err;
842 static const struct got_error *
843 load_tree(struct got_object_idset *traversed_ids,
844 struct got_object_id *tree_id,
845 const char *dpath, struct got_repository *repo,
846 got_cancel_cb cancel_cb, void *cancel_arg)
848 const struct got_error *err = NULL;
849 struct got_object_id_queue tree_ids;
850 struct got_object_qid *qid;
852 err = got_object_qid_alloc(&qid, tree_id);
853 if (err)
854 return err;
856 STAILQ_INIT(&tree_ids);
857 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
859 while (!STAILQ_EMPTY(&tree_ids)) {
860 if (cancel_cb) {
861 err = (*cancel_cb)(cancel_arg);
862 if (err)
863 break;
866 qid = STAILQ_FIRST(&tree_ids);
867 STAILQ_REMOVE_HEAD(&tree_ids, entry);
869 if (got_object_idset_contains(traversed_ids, &qid->id)) {
870 got_object_qid_free(qid);
871 continue;
874 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
875 if (err) {
876 got_object_qid_free(qid);
877 break;
880 err = load_tree_entries(&tree_ids, traversed_ids,
881 &qid->id, dpath, repo, cancel_cb, cancel_arg);
882 got_object_qid_free(qid);
883 if (err)
884 break;
887 got_object_id_queue_free(&tree_ids);
888 return err;
891 static const struct got_error *
892 load_commit_or_tag(int *ncommits, struct got_object_idset *traversed_ids,
893 struct got_object_id *id, struct got_repository *repo,
894 got_cleanup_progress_cb progress_cb, void *progress_arg,
895 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
897 const struct got_error *err;
898 struct got_commit_object *commit = NULL;
899 struct got_tag_object *tag = NULL;
900 struct got_object_id *tree_id = NULL;
901 struct got_object_id_queue ids;
902 struct got_object_qid *qid;
903 int obj_type;
905 err = got_object_qid_alloc(&qid, id);
906 if (err)
907 return err;
909 STAILQ_INIT(&ids);
910 STAILQ_INSERT_TAIL(&ids, qid, entry);
912 while (!STAILQ_EMPTY(&ids)) {
913 if (cancel_cb) {
914 err = (*cancel_cb)(cancel_arg);
915 if (err)
916 break;
919 qid = STAILQ_FIRST(&ids);
920 STAILQ_REMOVE_HEAD(&ids, entry);
922 if (got_object_idset_contains(traversed_ids, &qid->id)) {
923 got_object_qid_free(qid);
924 qid = NULL;
925 continue;
928 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
929 if (err)
930 break;
932 err = got_object_get_type(&obj_type, repo, &qid->id);
933 if (err)
934 break;
935 switch (obj_type) {
936 case GOT_OBJ_TYPE_COMMIT:
937 err = got_object_open_as_commit(&commit, repo,
938 &qid->id);
939 if (err)
940 goto done;
941 break;
942 case GOT_OBJ_TYPE_TAG:
943 err = got_object_open_as_tag(&tag, repo, &qid->id);
944 if (err)
945 goto done;
946 break;
947 default:
948 /* should not happen */
949 err = got_error(GOT_ERR_OBJ_TYPE);
950 goto done;
953 /* Find a tree object to scan. */
954 if (commit) {
955 tree_id = got_object_commit_get_tree_id(commit);
956 } else if (tag) {
957 obj_type = got_object_tag_get_object_type(tag);
958 switch (obj_type) {
959 case GOT_OBJ_TYPE_COMMIT:
960 err = got_object_open_as_commit(&commit, repo,
961 got_object_tag_get_object_id(tag));
962 if (err)
963 goto done;
964 tree_id = got_object_commit_get_tree_id(commit);
965 break;
966 case GOT_OBJ_TYPE_TREE:
967 tree_id = got_object_tag_get_object_id(tag);
968 break;
969 default:
970 /*
971 * Tag points at something other than a
972 * commit or tree. Leave this weird tag object
973 * and the object it points to.
974 */
975 if (got_object_idset_contains(traversed_ids,
976 got_object_tag_get_object_id(tag)))
977 break;
978 err = got_object_idset_add(traversed_ids,
979 got_object_tag_get_object_id(tag), NULL);
980 if (err)
981 goto done;
982 break;
986 if (tree_id) {
987 err = load_tree(traversed_ids, tree_id, "",
988 repo, cancel_cb, cancel_arg);
989 if (err)
990 break;
993 if (commit || tag)
994 (*ncommits)++; /* scanned tags are counted as commits */
996 err = report_cleanup_progress(progress_cb, progress_arg, rl,
997 *ncommits, -1, -1, -1);
998 if (err)
999 break;
1001 if (commit) {
1002 /* Find parent commits to scan. */
1003 const struct got_object_id_queue *parent_ids;
1004 parent_ids = got_object_commit_get_parent_ids(commit);
1005 err = got_object_id_queue_copy(parent_ids, &ids);
1006 if (err)
1007 break;
1008 got_object_commit_close(commit);
1009 commit = NULL;
1011 if (tag) {
1012 got_object_tag_close(tag);
1013 tag = NULL;
1015 got_object_qid_free(qid);
1016 qid = NULL;
1018 done:
1019 if (qid)
1020 got_object_qid_free(qid);
1021 if (commit)
1022 got_object_commit_close(commit);
1023 if (tag)
1024 got_object_tag_close(tag);
1025 got_object_id_queue_free(&ids);
1026 return err;
1029 static const struct got_error *
1030 is_object_packed(int *packed, struct got_repository *repo,
1031 struct got_object_id *id)
1033 const struct got_error *err;
1034 struct got_object *obj;
1036 *packed = 0;
1038 err = got_object_open_packed(&obj, id, repo);
1039 if (err) {
1040 if (err->code == GOT_ERR_NO_OBJ)
1041 err = NULL;
1042 return err;
1044 got_object_close(obj);
1045 *packed = 1;
1046 return NULL;
1049 struct purge_loose_object_arg {
1050 struct got_repository *repo;
1051 got_cleanup_progress_cb progress_cb;
1052 void *progress_arg;
1053 struct got_ratelimit *rl;
1054 struct got_object_idset *traversed_ids;
1055 int nloose;
1056 int ncommits;
1057 int npacked;
1058 int npurged;
1059 off_t size_purged;
1060 int dry_run;
1061 time_t max_mtime;
1062 int ignore_mtime;
1065 static const struct got_error *
1066 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1068 struct purge_loose_object_arg *a = arg;
1069 const struct got_error *err, *unlock_err = NULL;
1070 char *path = NULL;
1071 int packed, fd = -1;
1072 struct stat sb;
1073 struct got_lockfile *lf = NULL;
1075 err = is_object_packed(&packed, a->repo, id);
1076 if (err)
1077 return err;
1079 if (!packed && got_object_idset_contains(a->traversed_ids, id))
1080 return NULL;
1082 if (packed)
1083 a->npacked++;
1085 err = got_object_get_path(&path, id, a->repo);
1086 if (err)
1087 return err;
1089 err = got_object_open_loose_fd(&fd, id, a->repo);
1090 if (err)
1091 goto done;
1093 if (fstat(fd, &sb) == -1) {
1094 err = got_error_from_errno("fstat");
1095 goto done;
1099 * Do not delete objects which are younger than our maximum
1100 * modification time threshold. This prevents a race where
1101 * new objects which are being added to the repository
1102 * concurrently would be deleted.
1104 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1105 if (!a->dry_run) {
1106 err = got_lockfile_lock(&lf, path, -1);
1107 if (err)
1108 goto done;
1109 if (unlink(path) == -1) {
1110 err = got_error_from_errno2("unlink", path);
1111 goto done;
1115 a->npurged++;
1116 a->size_purged += sb.st_size;
1117 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1118 a->rl, a->ncommits, a->nloose, a->npurged, -1);
1119 if (err)
1120 goto done;
1122 done:
1123 if (fd != -1 && close(fd) == -1 && err == NULL)
1124 err = got_error_from_errno("close");
1125 free(path);
1126 if (lf)
1127 unlock_err = got_lockfile_unlock(lf, -1);
1128 return err ? err : unlock_err;
1131 static const struct got_error *
1132 repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1133 struct got_object_idset *traversed_ids,
1134 off_t *size_before, off_t *size_after, int ncommits, int *nloose,
1135 int *npacked, int *npurged, int dry_run, int ignore_mtime,
1136 time_t max_mtime, struct got_ratelimit *rl,
1137 got_cleanup_progress_cb progress_cb, void *progress_arg,
1138 got_cancel_cb cancel_cb, void *cancel_arg)
1140 const struct got_error *err;
1141 struct got_object_idset *loose_ids;
1142 struct purge_loose_object_arg arg;
1144 err = get_loose_object_ids(&loose_ids, size_before, ncommits,
1145 progress_cb, progress_arg, rl, repo);
1146 if (err)
1147 return err;
1148 *nloose = got_object_idset_num_elements(loose_ids);
1149 if (*nloose == 0) {
1150 got_object_idset_free(loose_ids);
1151 if (progress_cb) {
1152 err = progress_cb(progress_arg, 0, 0, 0, -1);
1153 if (err)
1154 return err;
1156 return NULL;
1159 memset(&arg, 0, sizeof(arg));
1160 arg.repo = repo;
1161 arg.progress_arg = progress_arg;
1162 arg.progress_cb = progress_cb;
1163 arg.rl = rl;
1164 arg.traversed_ids = traversed_ids;
1165 arg.nloose = *nloose;
1166 arg.npacked = 0;
1167 arg.npurged = 0;
1168 arg.size_purged = 0;
1169 arg.dry_run = dry_run;
1170 arg.max_mtime = max_mtime;
1171 arg.ignore_mtime = ignore_mtime;
1172 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1173 if (err)
1174 goto done;
1176 *size_after = *size_before - arg.size_purged;
1177 *npacked = arg.npacked;
1178 *npurged = arg.npurged;
1180 /* Produce a final progress report. */
1181 if (progress_cb) {
1182 err = progress_cb(progress_arg, ncommits, *nloose,
1183 arg.npurged, -1);
1184 if (err)
1185 goto done;
1187 done:
1188 got_object_idset_free(loose_ids);
1189 return err;
1192 static const struct got_error *
1193 purge_redundant_pack(struct got_repository *repo, const char *packidx_path,
1194 int dry_run, int ignore_mtime, time_t max_mtime,
1195 int *remove, off_t *size_before, off_t *size_after)
1197 static const char *ext[] = {".idx", ".pack", ".rev", ".bitmap",
1198 ".promisor", ".mtimes"};
1199 struct stat sb;
1200 char *dot, path[PATH_MAX];
1201 size_t i;
1203 if (strlcpy(path, packidx_path, sizeof(path)) >= sizeof(path))
1204 return got_error(GOT_ERR_NO_SPACE);
1207 * Do not delete pack files which are younger than our maximum
1208 * modification time threshold. This prevents a race where a
1209 * new pack file which is being added to the repository
1210 * concurrently would be deleted.
1212 if (fstatat(got_repo_get_fd(repo), path, &sb, 0) == -1) {
1213 if (errno == ENOENT)
1214 return NULL;
1215 return got_error_from_errno2("fstatat", path);
1217 if (!ignore_mtime && sb.st_mtime > max_mtime)
1218 *remove = 0;
1221 * For compatibility with Git, if a matching .keep file exist
1222 * don't delete the packfile.
1224 dot = strrchr(path, '.');
1225 *dot = '\0';
1226 if (strlcat(path, ".keep", sizeof(path)) >= sizeof(path))
1227 return got_error(GOT_ERR_NO_SPACE);
1228 if (faccessat(got_repo_get_fd(repo), path, F_OK, 0) == 0)
1229 *remove = 0;
1231 for (i = 0; i < nitems(ext); ++i) {
1232 *dot = '\0';
1234 if (strlcat(path, ext[i], sizeof(path)) >=
1235 sizeof(path))
1236 return got_error(GOT_ERR_NO_SPACE);
1238 if (fstatat(got_repo_get_fd(repo), path, &sb, 0) ==
1239 -1) {
1240 if (errno == ENOENT)
1241 continue;
1242 return got_error_from_errno2("fstatat", path);
1245 *size_before += sb.st_size;
1246 if (!*remove) {
1247 *size_after += sb.st_size;
1248 continue;
1251 if (dry_run)
1252 continue;
1254 if (unlinkat(got_repo_get_fd(repo), path, 0) == -1) {
1255 if (errno == ENOENT)
1256 continue;
1257 return got_error_from_errno2("unlinkat",
1258 path);
1262 return NULL;
1265 static const struct got_error *
1266 pack_is_redundant(int *redundant, struct got_repository *repo,
1267 struct got_object_idset *traversed_ids,
1268 const char *packidx_path, struct got_object_idset *idset)
1270 const struct got_error *err;
1271 struct got_packidx *packidx;
1272 struct got_packidx_object_id *pid;
1273 struct got_object_id id;
1274 size_t i, nobjects;
1276 *redundant = 1;
1278 err = got_repo_get_packidx(&packidx, packidx_path, repo);
1279 if (err)
1280 return err;
1282 nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1283 for (i = 0; i < nobjects; ++i) {
1284 pid = &packidx->hdr.sorted_ids[i];
1286 memset(&id, 0, sizeof(id));
1287 memcpy(&id.sha1, pid->sha1, sizeof(id.sha1));
1289 if (got_object_idset_contains(idset, &id))
1290 continue;
1292 if (!got_object_idset_contains(traversed_ids, &id))
1293 continue;
1295 *redundant = 0;
1296 err = got_object_idset_add(idset, &id, NULL);
1297 if (err)
1298 return err;
1301 return NULL;
1304 struct pack_info {
1305 const char *path;
1306 size_t nobjects;
1309 static int
1310 pack_info_cmp(const void *a, const void *b)
1312 const struct pack_info *pa, *pb;
1314 pa = a;
1315 pb = b;
1316 if (pa->nobjects == pb->nobjects)
1317 return strcmp(pa->path, pb->path);
1318 if (pa->nobjects > pb->nobjects)
1319 return -1;
1320 return 1;
1323 static const struct got_error *
1324 repo_purge_redundant_packfiles(struct got_repository *repo,
1325 struct got_object_idset *traversed_ids,
1326 off_t *size_before, off_t *size_after, int dry_run, int ignore_mtime,
1327 time_t max_mtime, int nloose, int ncommits, int npurged,
1328 struct got_ratelimit *rl,
1329 got_cleanup_progress_cb progress_cb, void *progress_arg,
1330 got_cancel_cb cancel_cb, void *cancel_arg)
1332 const struct got_error *err;
1333 struct pack_info *pinfo, *sorted = NULL;
1334 struct got_packidx *packidx;
1335 struct got_object_idset *idset = NULL;
1336 struct got_pathlist_entry *pe;
1337 size_t i, npacks;
1338 int remove, redundant_packs = 0;
1340 npacks = 0;
1341 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
1342 npacks++;
1344 if (npacks == 0)
1345 return NULL;
1347 sorted = calloc(npacks, sizeof(*sorted));
1348 if (sorted == NULL)
1349 return got_error_from_errno("calloc");
1351 i = 0;
1352 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1353 err = got_repo_get_packidx(&packidx, pe->path, repo);
1354 if (err)
1355 goto done;
1357 pinfo = &sorted[i++];
1358 pinfo->path = pe->path;
1359 pinfo->nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1361 qsort(sorted, npacks, sizeof(*sorted), pack_info_cmp);
1363 idset = got_object_idset_alloc();
1364 if (idset == NULL) {
1365 err = got_error_from_errno("got_object_idset_alloc");
1366 goto done;
1369 for (i = 0; i < npacks; ++i) {
1370 if (cancel_cb) {
1371 err = (*cancel_cb)(cancel_arg);
1372 if (err)
1373 break;
1376 err = pack_is_redundant(&remove, repo, traversed_ids,
1377 sorted[i].path, idset);
1378 if (err)
1379 goto done;
1380 err = purge_redundant_pack(repo, sorted[i].path, dry_run,
1381 ignore_mtime, max_mtime, &remove, size_before, size_after);
1382 if (err)
1383 goto done;
1384 if (!remove)
1385 continue;
1386 err = report_cleanup_progress(progress_cb, progress_arg,
1387 rl, ncommits, nloose, npurged, ++redundant_packs);
1388 if (err)
1389 goto done;
1392 /* Produce a final progress report. */
1393 if (progress_cb) {
1394 err = progress_cb(progress_arg, ncommits, nloose, npurged,
1395 redundant_packs);
1396 if (err)
1397 goto done;
1399 done:
1400 free(sorted);
1401 if (idset)
1402 got_object_idset_free(idset);
1403 return err;
1406 const struct got_error *
1407 got_repo_cleanup(struct got_repository *repo,
1408 off_t *loose_before, off_t *loose_after,
1409 off_t *pack_before, off_t *pack_after,
1410 int *ncommits, int *nloose, int *npacked, int dry_run, int ignore_mtime,
1411 got_cleanup_progress_cb progress_cb, void *progress_arg,
1412 got_cancel_cb cancel_cb, void *cancel_arg)
1414 const struct got_error *unlock_err, *err = NULL;
1415 struct got_lockfile *lk = NULL;
1416 struct got_ratelimit rl;
1417 struct got_reflist_head refs;
1418 struct got_object_idset *traversed_ids = NULL;
1419 struct got_reflist_entry *re;
1420 struct got_object_id **referenced_ids;
1421 int i, nreferenced;
1422 int npurged = 0;
1423 time_t max_mtime = 0;
1425 TAILQ_INIT(&refs);
1426 got_ratelimit_init(&rl, 0, 500);
1428 *loose_before = 0;
1429 *loose_after = 0;
1430 *pack_before = 0;
1431 *pack_after = 0;
1432 *ncommits = 0;
1433 *nloose = 0;
1434 *npacked = 0;
1436 err = repo_cleanup_lock(repo, &lk);
1437 if (err)
1438 return err;
1440 traversed_ids = got_object_idset_alloc();
1441 if (traversed_ids == NULL) {
1442 err = got_error_from_errno("got_object_idset_alloc");
1443 goto done;
1446 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1447 if (err)
1448 goto done;
1449 if (!ignore_mtime) {
1450 TAILQ_FOREACH(re, &refs, entry) {
1451 time_t mtime = got_ref_get_mtime(re->ref);
1452 if (mtime > max_mtime)
1453 max_mtime = mtime;
1456 * For safety, keep objects created within 10 minutes
1457 * before the youngest reference was created.
1459 if (max_mtime >= 600)
1460 max_mtime -= 600;
1463 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1464 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1465 &refs, repo, cancel_cb, cancel_arg);
1466 if (err)
1467 goto done;
1469 for (i = 0; i < nreferenced; i++) {
1470 struct got_object_id *id = referenced_ids[i];
1471 err = load_commit_or_tag(ncommits, traversed_ids,
1472 id, repo, progress_cb, progress_arg, &rl,
1473 cancel_cb, cancel_arg);
1474 if (err)
1475 goto done;
1478 err = repo_purge_unreferenced_loose_objects(repo, traversed_ids,
1479 loose_before, loose_after, *ncommits, nloose, npacked, &npurged,
1480 dry_run, ignore_mtime, max_mtime, &rl, progress_cb, progress_arg,
1481 cancel_cb, cancel_arg);
1482 if (err)
1483 goto done;
1485 err = repo_purge_redundant_packfiles(repo, traversed_ids,
1486 pack_before, pack_after, dry_run, ignore_mtime, max_mtime,
1487 *nloose, *ncommits, npurged, &rl, progress_cb, progress_arg,
1488 cancel_cb, cancel_arg);
1489 if (err)
1490 goto done;
1492 done:
1493 if (lk) {
1494 unlock_err = got_lockfile_unlock(lk, got_repo_get_fd(repo));
1495 if (err == NULL)
1496 err = unlock_err;
1498 if (traversed_ids)
1499 got_object_idset_free(traversed_ids);
1500 return err;
1503 const struct got_error *
1504 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1505 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1506 got_cancel_cb cancel_cb, void *cancel_arg)
1508 const struct got_error *err = NULL;
1509 DIR *packdir = NULL;
1510 struct dirent *dent;
1511 char *pack_relpath = NULL;
1512 int packdir_fd;
1513 struct stat sb;
1515 packdir_fd = openat(got_repo_get_fd(repo),
1516 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1517 if (packdir_fd == -1) {
1518 if (errno == ENOENT)
1519 return NULL;
1520 return got_error_from_errno_fmt("openat: %s/%s",
1521 got_repo_get_path_git_dir(repo),
1522 GOT_OBJECTS_PACK_DIR);
1525 packdir = fdopendir(packdir_fd);
1526 if (packdir == NULL) {
1527 err = got_error_from_errno("fdopendir");
1528 close(packdir_fd);
1529 goto done;
1532 while ((dent = readdir(packdir)) != NULL) {
1533 if (cancel_cb) {
1534 err = cancel_cb(cancel_arg);
1535 if (err)
1536 goto done;
1539 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1540 continue;
1542 err = got_packidx_get_packfile_path(&pack_relpath,
1543 dent->d_name);
1544 if (err)
1545 goto done;
1547 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1548 free(pack_relpath);
1549 pack_relpath = NULL;
1550 continue;
1552 if (errno != ENOENT) {
1553 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1554 got_repo_get_path_git_dir(repo),
1555 GOT_OBJECTS_PACK_DIR,
1556 pack_relpath);
1557 goto done;
1560 if (!dry_run) {
1561 if (unlinkat(packdir_fd, dent->d_name, 0) == -1) {
1562 err = got_error_from_errno("unlinkat");
1563 goto done;
1566 if (progress_cb) {
1567 char *path;
1568 if (asprintf(&path, "%s/%s/%s",
1569 got_repo_get_path_git_dir(repo),
1570 GOT_OBJECTS_PACK_DIR,
1571 dent->d_name) == -1) {
1572 err = got_error_from_errno("asprintf");
1573 goto done;
1575 err = progress_cb(progress_arg, path);
1576 free(path);
1577 if (err)
1578 goto done;
1580 free(pack_relpath);
1581 pack_relpath = NULL;
1583 done:
1584 if (packdir && closedir(packdir) != 0 && err == NULL)
1585 err = got_error_from_errno("closedir");
1586 free(pack_relpath);
1587 return err;