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 "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 #include <sys/stat.h>
23 #include <sys/socket.h>
24 #include <sys/wait.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <unistd.h>
36 #include "got_error.h"
37 #include "got_cancel.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_repository_admin.h"
42 #include "got_opentemp.h"
43 #include "got_path.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_idset.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
53 #include "got_lib_pack_create.h"
54 #include "got_lib_hash.h"
55 #include "got_lib_lockfile.h"
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
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)
66 {
67 const struct got_error *err = NULL;
68 const size_t alloc_chunksz = 256;
69 size_t nalloc;
70 struct got_reflist_entry *re;
71 int i;
73 *ids = NULL;
74 *nobjects = 0;
76 err = got_reflist_sort(refs,
77 got_ref_cmp_by_commit_timestamp_descending, repo);
78 if (err)
79 return err;
81 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
82 if (*ids == NULL)
83 return got_error_from_errno("reallocarray");
84 nalloc = alloc_chunksz;
86 TAILQ_FOREACH(re, refs, entry) {
87 struct got_object_id *id;
89 if (cancel_cb) {
90 err = cancel_cb(cancel_arg);
91 if (err)
92 goto done;
93 }
95 err = got_ref_resolve(&id, repo, re->ref);
96 if (err)
97 goto done;
99 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
100 int obj_type;
101 err = got_object_get_type(&obj_type, repo, id);
102 if (err)
103 goto done;
104 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
105 free(id);
106 id = NULL;
107 continue;
111 if (nalloc <= *nobjects) {
112 struct got_object_id **new;
113 new = recallocarray(*ids, nalloc,
114 nalloc + alloc_chunksz,
115 sizeof(struct got_object_id *));
116 if (new == NULL) {
117 err = got_error_from_errno(
118 "recallocarray");
119 goto done;
121 *ids = new;
122 nalloc += alloc_chunksz;
124 (*ids)[*nobjects] = id;
125 if ((*ids)[*nobjects] == NULL) {
126 err = got_error_from_errno("got_object_id_dup");
127 goto done;
129 (*nobjects)++;
131 done:
132 if (err) {
133 for (i = 0; i < *nobjects; i++)
134 free((*ids)[i]);
135 free(*ids);
136 *ids = NULL;
137 *nobjects = 0;
139 return err;
142 const struct got_error *
143 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
144 struct got_reflist_head *include_refs,
145 struct got_reflist_head *exclude_refs, struct got_repository *repo,
146 int loose_obj_only, int force_refdelta,
147 got_pack_progress_cb progress_cb, void *progress_arg,
148 got_cancel_cb cancel_cb, void *cancel_arg)
150 const struct got_error *err = NULL;
151 struct got_object_id **ours = NULL, **theirs = NULL;
152 int nours = 0, ntheirs = 0, packfd = -1, i;
153 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
154 char *sha1_str = NULL;
155 FILE *delta_cache = NULL;
156 struct got_ratelimit rl;
158 *packfile = NULL;
159 *pack_hash = NULL;
161 got_ratelimit_init(&rl, 0, 500);
163 if (asprintf(&path, "%s/%s/packing.pack",
164 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
165 err = got_error_from_errno("asprintf");
166 goto done;
168 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path, "");
169 if (err)
170 goto done;
172 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
173 err = got_error_from_errno2("fchmod", tmpfile_path);
174 goto done;
177 delta_cache = got_opentemp();
178 if (delta_cache == NULL) {
179 err = got_error_from_errno("got_opentemp");
180 goto done;
183 err = get_reflist_object_ids(&ours, &nours,
184 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
185 include_refs, repo, cancel_cb, cancel_arg);
186 if (err)
187 goto done;
189 if (nours == 0) {
190 err = got_error(GOT_ERR_CANNOT_PACK);
191 goto done;
194 if (!TAILQ_EMPTY(exclude_refs)) {
195 err = get_reflist_object_ids(&theirs, &ntheirs,
196 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
197 exclude_refs, repo,
198 cancel_cb, cancel_arg);
199 if (err)
200 goto done;
203 *pack_hash = calloc(1, sizeof(**pack_hash));
204 if (*pack_hash == NULL) {
205 err = got_error_from_errno("calloc");
206 goto done;
209 err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
210 theirs, ntheirs, ours, nours, repo, loose_obj_only,
211 0, force_refdelta, progress_cb, progress_arg, &rl,
212 cancel_cb, cancel_arg);
213 if (err)
214 goto done;
216 err = got_object_id_str(&sha1_str, *pack_hash);
217 if (err)
218 goto done;
219 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
220 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
221 sha1_str) == -1) {
222 err = got_error_from_errno("asprintf");
223 goto done;
226 if (lseek(packfd, 0L, SEEK_SET) == -1) {
227 err = got_error_from_errno("lseek");
228 goto done;
230 if (rename(tmpfile_path, packfile_path) == -1) {
231 err = got_error_from_errno3("rename", tmpfile_path,
232 packfile_path);
233 goto done;
235 free(tmpfile_path);
236 tmpfile_path = NULL;
238 *packfile = fdopen(packfd, "w");
239 if (*packfile == NULL) {
240 err = got_error_from_errno2("fdopen", tmpfile_path);
241 goto done;
243 packfd = -1;
244 done:
245 for (i = 0; i < nours; i++)
246 free(ours[i]);
247 free(ours);
248 for (i = 0; i < ntheirs; i++)
249 free(theirs[i]);
250 free(theirs);
251 if (packfd != -1 && close(packfd) == -1 && err == NULL)
252 err = got_error_from_errno2("close", packfile_path);
253 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
254 err = got_error_from_errno("fclose");
255 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
256 err = got_error_from_errno2("unlink", tmpfile_path);
257 free(tmpfile_path);
258 free(packfile_path);
259 free(sha1_str);
260 free(path);
261 if (err) {
262 free(*pack_hash);
263 *pack_hash = NULL;
264 if (*packfile)
265 fclose(*packfile);
266 *packfile = NULL;
268 return err;
271 const struct got_error *
272 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
273 struct got_repository *repo,
274 got_pack_index_progress_cb progress_cb, void *progress_arg,
275 got_cancel_cb cancel_cb, void *cancel_arg)
277 size_t i;
278 char *path;
279 int imsg_idxfds[2];
280 int npackfd = -1, idxfd = -1, nidxfd = -1;
281 int tmpfds[3];
282 int idxstatus, done = 0;
283 const struct got_error *err;
284 struct imsgbuf idxibuf;
285 pid_t idxpid;
286 char *tmpidxpath = NULL;
287 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
288 const char *repo_path = got_repo_get_path_git_dir(repo);
289 struct stat sb;
291 for (i = 0; i < nitems(tmpfds); i++)
292 tmpfds[i] = -1;
294 if (asprintf(&path, "%s/%s/indexing.idx",
295 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
296 err = got_error_from_errno("asprintf");
297 goto done;
299 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
300 free(path);
301 if (err)
302 goto done;
303 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
304 err = got_error_from_errno2("fchmod", tmpidxpath);
305 goto done;
308 nidxfd = dup(idxfd);
309 if (nidxfd == -1) {
310 err = got_error_from_errno("dup");
311 goto done;
314 for (i = 0; i < nitems(tmpfds); i++) {
315 tmpfds[i] = got_opentempfd();
316 if (tmpfds[i] == -1) {
317 err = got_error_from_errno("got_opentempfd");
318 goto done;
322 err = got_object_id_str(&id_str, pack_hash);
323 if (err)
324 goto done;
326 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
327 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
328 err = got_error_from_errno("asprintf");
329 goto done;
332 if (fstat(fileno(packfile), &sb) == -1) {
333 err = got_error_from_errno2("fstat", packfile_path);
334 goto done;
337 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
338 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
339 err = got_error_from_errno("asprintf");
340 goto done;
343 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
344 err = got_error_from_errno("socketpair");
345 goto done;
347 idxpid = fork();
348 if (idxpid == -1) {
349 err= got_error_from_errno("fork");
350 goto done;
351 } else if (idxpid == 0)
352 got_privsep_exec_child(imsg_idxfds,
353 GOT_PATH_PROG_INDEX_PACK, packfile_path);
354 if (close(imsg_idxfds[1]) == -1) {
355 err = got_error_from_errno("close");
356 goto done;
358 imsg_init(&idxibuf, imsg_idxfds[0]);
360 npackfd = dup(fileno(packfile));
361 if (npackfd == -1) {
362 err = got_error_from_errno("dup");
363 goto done;
365 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
366 npackfd);
367 if (err != NULL)
368 goto done;
369 npackfd = -1;
370 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
371 if (err != NULL)
372 goto done;
373 nidxfd = -1;
374 for (i = 0; i < nitems(tmpfds); i++) {
375 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
376 if (err != NULL)
377 goto done;
378 tmpfds[i] = -1;
380 done = 0;
381 while (!done) {
382 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
384 if (cancel_cb) {
385 err = cancel_cb(cancel_arg);
386 if (err)
387 goto done;
390 err = got_privsep_recv_index_progress(&done, &nobj_total,
391 &nobj_indexed, &nobj_loose, &nobj_resolved,
392 &idxibuf);
393 if (err != NULL)
394 goto done;
395 if (nobj_indexed != 0) {
396 err = progress_cb(progress_arg, sb.st_size,
397 nobj_total, nobj_indexed, nobj_loose,
398 nobj_resolved);
399 if (err)
400 break;
403 if (close(imsg_idxfds[0]) == -1) {
404 err = got_error_from_errno("close");
405 goto done;
407 if (waitpid(idxpid, &idxstatus, 0) == -1) {
408 err = got_error_from_errno("waitpid");
409 goto done;
412 if (rename(tmpidxpath, idxpath) == -1) {
413 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
414 goto done;
416 free(tmpidxpath);
417 tmpidxpath = NULL;
419 done:
420 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
421 err = got_error_from_errno2("unlink", tmpidxpath);
422 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
423 err = got_error_from_errno("close");
424 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
425 err = got_error_from_errno("close");
426 for (i = 0; i < nitems(tmpfds); i++) {
427 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
428 err = got_error_from_errno("close");
430 free(tmpidxpath);
431 free(idxpath);
432 free(packfile_path);
433 return err;
436 const struct got_error *
437 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
438 struct got_repository *repo, const char *packfile_path)
440 const struct got_error *err = NULL;
441 const char *packdir_path = NULL;
442 char *packfile_name = NULL, *p, *dot;
443 struct got_object_id id;
444 int packfd = -1;
446 *packfile = NULL;
447 *pack_hash = NULL;
449 packdir_path = got_repo_get_path_objects_pack(repo);
450 if (packdir_path == NULL)
451 return got_error_from_errno("got_repo_get_path_objects_pack");
453 if (!got_path_is_child(packfile_path, packdir_path,
454 strlen(packdir_path))) {
455 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
456 goto done;
460 err = got_path_basename(&packfile_name, packfile_path);
461 if (err)
462 goto done;
463 p = packfile_name;
465 if (strncmp(p, "pack-", 5) != 0) {
466 err = got_error_fmt(GOT_ERR_BAD_PATH,
467 "'%s' is not a valid pack file name",
468 packfile_name);
469 goto done;
471 p += 5;
472 dot = strchr(p, '.');
473 if (dot == NULL) {
474 err = got_error_fmt(GOT_ERR_BAD_PATH,
475 "'%s' is not a valid pack file name",
476 packfile_name);
477 goto done;
479 if (strcmp(dot + 1, "pack") != 0) {
480 err = got_error_fmt(GOT_ERR_BAD_PATH,
481 "'%s' is not a valid pack file name",
482 packfile_name);
483 goto done;
485 *dot = '\0';
486 if (!got_parse_object_id(&id, p, repo->algo)) {
487 err = got_error_fmt(GOT_ERR_BAD_PATH,
488 "'%s' is not a valid pack file name",
489 packfile_name);
490 goto done;
493 *pack_hash = got_object_id_dup(&id);
494 if (*pack_hash == NULL) {
495 err = got_error_from_errno("got_object_id_dup");
496 goto done;
499 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
500 if (packfd == -1) {
501 err = got_error_from_errno2("open", packfile_path);
502 goto done;
505 *packfile = fdopen(packfd, "r");
506 if (*packfile == NULL) {
507 err = got_error_from_errno2("fdopen", packfile_path);
508 goto done;
510 packfd = -1;
511 done:
512 if (packfd != -1 && close(packfd) == -1 && err == NULL)
513 err = got_error_from_errno2("close", packfile_path);
514 free(packfile_name);
515 if (err) {
516 free(*pack_hash);
517 *pack_hash = NULL;
519 return err;
522 const struct got_error *
523 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
524 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
525 got_cancel_cb cancel_cb, void *cancel_arg)
527 const struct got_error *err = NULL;
528 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
529 struct got_packidx *packidx = NULL;
530 struct got_pack *pack = NULL;
531 uint32_t nobj, i;
533 err = got_object_id_str(&id_str, pack_hash);
534 if (err)
535 goto done;
537 if (asprintf(&packpath, "%s/pack-%s.pack",
538 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
539 err = got_error_from_errno("asprintf");
540 goto done;
542 if (asprintf(&idxpath, "%s/pack-%s.idx",
543 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
544 err = got_error_from_errno("asprintf");
545 goto done;
548 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
549 if (err)
550 goto done;
552 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
553 if (err)
554 goto done;
556 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
557 for (i = 0; i < nobj; i++) {
558 struct got_packidx_object_id *oid;
559 struct got_object_id id, base_id;
560 off_t offset, base_offset = 0;
561 uint8_t type;
562 uint64_t size;
563 size_t tslen, len;
565 if (cancel_cb) {
566 err = cancel_cb(cancel_arg);
567 if (err)
568 break;
570 oid = &packidx->hdr.sorted_ids[i];
571 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
573 offset = got_packidx_get_object_offset(packidx, i);
574 if (offset == -1) {
575 err = got_error(GOT_ERR_BAD_PACKIDX);
576 goto done;
579 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
580 pack, offset);
581 if (err)
582 goto done;
584 switch (type) {
585 case GOT_OBJ_TYPE_OFFSET_DELTA:
586 err = got_pack_parse_offset_delta(&base_offset, &len,
587 pack, offset, tslen);
588 if (err)
589 goto done;
590 break;
591 case GOT_OBJ_TYPE_REF_DELTA:
592 err = got_pack_parse_ref_delta(&base_id,
593 pack, offset, tslen);
594 if (err)
595 goto done;
596 break;
598 err = (*list_cb)(list_arg, &id, type, offset, size,
599 base_offset, &base_id);
600 if (err)
601 goto done;
604 done:
605 free(id_str);
606 free(idxpath);
607 free(packpath);
608 if (packidx)
609 got_packidx_close(packidx);
610 return err;
613 const struct got_error *
614 got_repo_cleanup_prepare(struct got_repository *repo,
615 struct got_lockfile **lk)
617 const struct got_error *err;
618 char myname[_POSIX_HOST_NAME_MAX + 1];
620 if (gethostname(myname, sizeof(myname)) == -1)
621 return got_error_from_errno("gethostname");
623 err = got_lockfile_lock(lk, "gc.pid", got_repo_get_fd(repo));
624 if (err)
625 return err;
627 /*
628 * Git uses these info to provide some verbiage when finds a
629 * lock during `git gc --force' so don't try too hard to avoid
630 * short writes and don't care if a race happens between the
631 * lockfile creation and the write itself.
632 */
633 if (dprintf((*lk)->fd, "%d %s", getpid(), myname) < 0)
634 return got_error_from_errno("dprintf");
636 return NULL;
639 const struct got_error *
640 got_repo_cleanup_complete(struct got_repository *repo,
641 struct got_lockfile *lk)
643 if (lk == NULL)
644 return NULL;
646 return got_lockfile_unlock(lk, got_repo_get_fd(repo));
649 static const struct got_error *
650 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
651 void *progress_arg, struct got_ratelimit *rl,
652 int nloose, int ncommits, int npurged, int nredundant)
654 const struct got_error *err;
655 int elapsed;
657 if (progress_cb == NULL)
658 return NULL;
660 err = got_ratelimit_check(&elapsed, rl);
661 if (err || !elapsed)
662 return err;
664 return progress_cb(progress_arg, nloose, ncommits, npurged, nredundant);
667 static const struct got_error *
668 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
669 got_cleanup_progress_cb progress_cb, void *progress_arg,
670 struct got_ratelimit *rl, struct got_repository *repo)
672 const struct got_error *err = NULL;
673 char *path_objects = NULL, *path = NULL;
674 DIR *dir = NULL;
675 struct got_object *obj = NULL;
676 struct got_object_id id;
677 int i, fd = -1;
678 struct stat sb;
680 *ondisk_size = 0;
681 *loose_ids = got_object_idset_alloc();
682 if (*loose_ids == NULL)
683 return got_error_from_errno("got_object_idset_alloc");
685 path_objects = got_repo_get_path_objects(repo);
686 if (path_objects == NULL) {
687 err = got_error_from_errno("got_repo_get_path_objects");
688 goto done;
691 for (i = 0; i <= 0xff; i++) {
692 struct dirent *dent;
694 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
695 err = got_error_from_errno("asprintf");
696 break;
699 dir = opendir(path);
700 if (dir == NULL) {
701 if (errno == ENOENT) {
702 err = NULL;
703 continue;
705 err = got_error_from_errno2("opendir", path);
706 break;
709 while ((dent = readdir(dir)) != NULL) {
710 char *id_str;
712 if (strcmp(dent->d_name, ".") == 0 ||
713 strcmp(dent->d_name, "..") == 0)
714 continue;
716 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
717 err = got_error_from_errno("asprintf");
718 goto done;
721 if (!got_parse_object_id(&id, id_str, repo->algo)) {
722 free(id_str);
723 continue;
725 free(id_str);
727 err = got_object_open_loose_fd(&fd, &id, repo);
728 if (err)
729 goto done;
730 if (fstat(fd, &sb) == -1) {
731 err = got_error_from_errno("fstat");
732 goto done;
734 err = got_object_read_header_privsep(&obj, &id, repo,
735 fd);
736 if (err)
737 goto done;
738 fd = -1; /* already closed */
740 switch (obj->type) {
741 case GOT_OBJ_TYPE_COMMIT:
742 case GOT_OBJ_TYPE_TREE:
743 case GOT_OBJ_TYPE_BLOB:
744 case GOT_OBJ_TYPE_TAG:
745 break;
746 default:
747 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
748 "%d", obj->type);
749 goto done;
751 got_object_close(obj);
752 obj = NULL;
753 (*ondisk_size) += sb.st_size;
754 err = got_object_idset_add(*loose_ids, &id, NULL);
755 if (err)
756 goto done;
757 err = report_cleanup_progress(progress_cb,
758 progress_arg, rl,
759 got_object_idset_num_elements(*loose_ids),
760 -1, -1, -1);
761 if (err)
762 goto done;
765 if (closedir(dir) != 0) {
766 err = got_error_from_errno("closedir");
767 goto done;
769 dir = NULL;
771 free(path);
772 path = NULL;
774 done:
775 if (dir && closedir(dir) != 0 && err == NULL)
776 err = got_error_from_errno("closedir");
777 if (fd != -1 && close(fd) == -1 && err == NULL)
778 err = got_error_from_errno("close");
779 if (err) {
780 got_object_idset_free(*loose_ids);
781 *loose_ids = NULL;
783 if (obj)
784 got_object_close(obj);
785 free(path_objects);
786 free(path);
787 return err;
790 static const struct got_error *
791 preserve_loose_object(struct got_object_idset *loose_ids,
792 struct got_object_id *id, struct got_repository *repo, int *npacked)
794 const struct got_error *err = NULL;
795 struct got_object *obj;
797 if (!got_object_idset_contains(loose_ids, id))
798 return NULL;
800 /*
801 * Try to open this object from a pack file. This ensures that
802 * we do in fact have a valid packed copy of the object. Otherwise
803 * we should not delete the loose representation of this object.
804 */
805 err = got_object_open_packed(&obj, id, repo);
806 if (err == NULL) {
807 got_object_close(obj);
808 /*
809 * The object is referenced and packed.
810 * We can purge the redundantly stored loose object.
811 */
812 (*npacked)++;
813 return NULL;
814 } else if (err->code != GOT_ERR_NO_OBJ)
815 return err;
817 /*
818 * This object is referenced and not packed.
819 * Remove it from our purge set.
820 */
821 return got_object_idset_remove(NULL, loose_ids, id);
824 static const struct got_error *
825 load_tree_entries(struct got_object_id_queue *ids,
826 struct got_object_idset *loose_ids,
827 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
828 const char *dpath, struct got_repository *repo, int *npacked,
829 got_cancel_cb cancel_cb, void *cancel_arg)
831 const struct got_error *err;
832 struct got_tree_object *tree;
833 char *p = NULL;
834 int i;
836 err = got_object_open_as_tree(&tree, repo, tree_id);
837 if (err)
838 return err;
840 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
841 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
842 struct got_object_id *id = got_tree_entry_get_id(e);
843 mode_t mode = got_tree_entry_get_mode(e);
845 if (cancel_cb) {
846 err = (*cancel_cb)(cancel_arg);
847 if (err)
848 break;
851 if (got_object_tree_entry_is_symlink(e) ||
852 got_object_tree_entry_is_submodule(e) ||
853 got_object_idset_contains(traversed_ids, id))
854 continue;
856 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
857 got_tree_entry_get_name(e)) == -1) {
858 err = got_error_from_errno("asprintf");
859 break;
862 if (S_ISDIR(mode)) {
863 struct got_object_qid *qid;
864 err = got_object_qid_alloc(&qid, id);
865 if (err)
866 break;
867 STAILQ_INSERT_TAIL(ids, qid, entry);
868 } else if (S_ISREG(mode)) {
869 /* This blob is referenced. */
870 err = preserve_loose_object(loose_ids, id, repo,
871 npacked);
872 if (err)
873 break;
874 err = got_object_idset_add(traversed_ids, id, NULL);
875 if (err)
876 break;
878 free(p);
879 p = NULL;
882 got_object_tree_close(tree);
883 free(p);
884 return err;
887 static const struct got_error *
888 load_tree(struct got_object_idset *loose_ids,
889 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
890 const char *dpath, struct got_repository *repo, int *npacked,
891 got_cancel_cb cancel_cb, void *cancel_arg)
893 const struct got_error *err = NULL;
894 struct got_object_id_queue tree_ids;
895 struct got_object_qid *qid;
897 err = got_object_qid_alloc(&qid, tree_id);
898 if (err)
899 return err;
901 STAILQ_INIT(&tree_ids);
902 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
904 while (!STAILQ_EMPTY(&tree_ids)) {
905 if (cancel_cb) {
906 err = (*cancel_cb)(cancel_arg);
907 if (err)
908 break;
911 qid = STAILQ_FIRST(&tree_ids);
912 STAILQ_REMOVE_HEAD(&tree_ids, entry);
914 if (got_object_idset_contains(traversed_ids, &qid->id)) {
915 got_object_qid_free(qid);
916 continue;
919 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
920 if (err) {
921 got_object_qid_free(qid);
922 break;
925 /* This tree is referenced. */
926 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
927 if (err)
928 break;
930 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
931 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
932 got_object_qid_free(qid);
933 if (err)
934 break;
937 got_object_id_queue_free(&tree_ids);
938 return err;
941 static const struct got_error *
942 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
943 int *npacked, struct got_object_idset *traversed_ids,
944 struct got_object_id *id, struct got_repository *repo,
945 got_cleanup_progress_cb progress_cb, void *progress_arg,
946 struct got_ratelimit *rl, int nloose,
947 got_cancel_cb cancel_cb, void *cancel_arg)
949 const struct got_error *err;
950 struct got_commit_object *commit = NULL;
951 struct got_tag_object *tag = NULL;
952 struct got_object_id *tree_id = NULL;
953 struct got_object_id_queue ids;
954 struct got_object_qid *qid;
955 int obj_type;
957 err = got_object_qid_alloc(&qid, id);
958 if (err)
959 return err;
961 STAILQ_INIT(&ids);
962 STAILQ_INSERT_TAIL(&ids, qid, entry);
964 while (!STAILQ_EMPTY(&ids)) {
965 if (cancel_cb) {
966 err = (*cancel_cb)(cancel_arg);
967 if (err)
968 break;
971 qid = STAILQ_FIRST(&ids);
972 STAILQ_REMOVE_HEAD(&ids, entry);
974 if (got_object_idset_contains(traversed_ids, &qid->id)) {
975 got_object_qid_free(qid);
976 qid = NULL;
977 continue;
980 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
981 if (err)
982 break;
984 /* This commit or tag is referenced. */
985 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
986 if (err)
987 break;
989 err = got_object_get_type(&obj_type, repo, &qid->id);
990 if (err)
991 break;
992 switch (obj_type) {
993 case GOT_OBJ_TYPE_COMMIT:
994 err = got_object_open_as_commit(&commit, repo,
995 &qid->id);
996 if (err)
997 goto done;
998 break;
999 case GOT_OBJ_TYPE_TAG:
1000 err = got_object_open_as_tag(&tag, repo, &qid->id);
1001 if (err)
1002 goto done;
1003 break;
1004 default:
1005 /* should not happen */
1006 err = got_error(GOT_ERR_OBJ_TYPE);
1007 goto done;
1010 /* Find a tree object to scan. */
1011 if (commit) {
1012 tree_id = got_object_commit_get_tree_id(commit);
1013 } else if (tag) {
1014 obj_type = got_object_tag_get_object_type(tag);
1015 switch (obj_type) {
1016 case GOT_OBJ_TYPE_COMMIT:
1017 err = got_object_open_as_commit(&commit, repo,
1018 got_object_tag_get_object_id(tag));
1019 if (err)
1020 goto done;
1021 tree_id = got_object_commit_get_tree_id(commit);
1022 break;
1023 case GOT_OBJ_TYPE_TREE:
1024 tree_id = got_object_tag_get_object_id(tag);
1025 break;
1026 default:
1028 * Tag points at something other than a
1029 * commit or tree. Leave this weird tag object
1030 * and the object it points to on disk.
1032 err = got_object_idset_remove(NULL, loose_ids,
1033 &qid->id);
1034 if (err && err->code != GOT_ERR_NO_OBJ)
1035 goto done;
1036 err = got_object_idset_remove(NULL, loose_ids,
1037 got_object_tag_get_object_id(tag));
1038 if (err && err->code != GOT_ERR_NO_OBJ)
1039 goto done;
1040 err = NULL;
1041 break;
1045 if (tree_id) {
1046 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1047 repo, npacked, cancel_cb, cancel_arg);
1048 if (err)
1049 break;
1052 if (commit || tag)
1053 (*ncommits)++; /* scanned tags are counted as commits */
1055 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1056 nloose, *ncommits, -1, -1);
1057 if (err)
1058 break;
1060 if (commit) {
1061 /* Find parent commits to scan. */
1062 const struct got_object_id_queue *parent_ids;
1063 parent_ids = got_object_commit_get_parent_ids(commit);
1064 err = got_object_id_queue_copy(parent_ids, &ids);
1065 if (err)
1066 break;
1067 got_object_commit_close(commit);
1068 commit = NULL;
1070 if (tag) {
1071 got_object_tag_close(tag);
1072 tag = NULL;
1074 got_object_qid_free(qid);
1075 qid = NULL;
1077 done:
1078 if (qid)
1079 got_object_qid_free(qid);
1080 if (commit)
1081 got_object_commit_close(commit);
1082 if (tag)
1083 got_object_tag_close(tag);
1084 got_object_id_queue_free(&ids);
1085 return err;
1088 struct purge_loose_object_arg {
1089 struct got_repository *repo;
1090 got_cleanup_progress_cb progress_cb;
1091 void *progress_arg;
1092 struct got_ratelimit *rl;
1093 int nloose;
1094 int ncommits;
1095 int npurged;
1096 off_t size_purged;
1097 int dry_run;
1098 time_t max_mtime;
1099 int ignore_mtime;
1102 static const struct got_error *
1103 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1105 struct purge_loose_object_arg *a = arg;
1106 const struct got_error *err, *unlock_err = NULL;
1107 char *path = NULL;
1108 int fd = -1;
1109 struct stat sb;
1110 struct got_lockfile *lf = NULL;
1112 err = got_object_get_path(&path, id, a->repo);
1113 if (err)
1114 return err;
1116 err = got_object_open_loose_fd(&fd, id, a->repo);
1117 if (err)
1118 goto done;
1120 if (fstat(fd, &sb) == -1) {
1121 err = got_error_from_errno("fstat");
1122 goto done;
1126 * Do not delete objects which are younger than our maximum
1127 * modification time threshold. This prevents a race where
1128 * new objects which are being added to the repository
1129 * concurrently would be deleted.
1131 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1132 if (!a->dry_run) {
1133 err = got_lockfile_lock(&lf, path, -1);
1134 if (err)
1135 goto done;
1136 if (unlink(path) == -1) {
1137 err = got_error_from_errno2("unlink", path);
1138 goto done;
1142 a->npurged++;
1143 a->size_purged += sb.st_size;
1144 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1145 a->rl, a->nloose, a->ncommits, a->npurged, -1);
1146 if (err)
1147 goto done;
1149 done:
1150 if (fd != -1 && close(fd) == -1 && err == NULL)
1151 err = got_error_from_errno("close");
1152 free(path);
1153 if (lf)
1154 unlock_err = got_lockfile_unlock(lf, -1);
1155 return err ? err : unlock_err;
1158 const struct got_error *
1159 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1160 off_t *size_before, off_t *size_after, int *ncommits, int *nloose,
1161 int *npacked, int dry_run, int ignore_mtime,
1162 got_cleanup_progress_cb progress_cb, void *progress_arg,
1163 got_cancel_cb cancel_cb, void *cancel_arg)
1165 const struct got_error *err;
1166 struct got_object_idset *loose_ids;
1167 struct got_object_idset *traversed_ids;
1168 struct got_object_id **referenced_ids;
1169 int i, nreferenced;
1170 struct got_reflist_head refs;
1171 struct got_reflist_entry *re;
1172 struct purge_loose_object_arg arg;
1173 time_t max_mtime = 0;
1174 struct got_ratelimit rl;
1176 TAILQ_INIT(&refs);
1177 got_ratelimit_init(&rl, 0, 500);
1179 *size_before = 0;
1180 *size_after = 0;
1181 *npacked = 0;
1183 err = get_loose_object_ids(&loose_ids, size_before,
1184 progress_cb, progress_arg, &rl, repo);
1185 if (err)
1186 return err;
1187 *nloose = got_object_idset_num_elements(loose_ids);
1188 if (*nloose == 0) {
1189 got_object_idset_free(loose_ids);
1190 if (progress_cb) {
1191 err = progress_cb(progress_arg, 0, 0, 0, -1);
1192 if (err)
1193 return err;
1195 return NULL;
1198 traversed_ids = got_object_idset_alloc();
1199 if (traversed_ids == NULL) {
1200 err = got_error_from_errno("got_object_idset_alloc");
1201 goto done;
1204 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1205 if (err)
1206 goto done;
1207 if (!ignore_mtime) {
1208 TAILQ_FOREACH(re, &refs, entry) {
1209 time_t mtime = got_ref_get_mtime(re->ref);
1210 if (mtime > max_mtime)
1211 max_mtime = mtime;
1214 * For safety, keep objects created within 10 minutes
1215 * before the youngest reference was created.
1217 if (max_mtime >= 600)
1218 max_mtime -= 600;
1221 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1222 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1223 &refs, repo, cancel_cb, cancel_arg);
1224 if (err)
1225 goto done;
1227 for (i = 0; i < nreferenced; i++) {
1228 struct got_object_id *id = referenced_ids[i];
1229 err = load_commit_or_tag(loose_ids, ncommits, npacked,
1230 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1231 *nloose, cancel_cb, cancel_arg);
1232 if (err)
1233 goto done;
1236 /* Any remaining loose objects are unreferenced and can be purged. */
1237 arg.repo = repo;
1238 arg.progress_arg = progress_arg;
1239 arg.progress_cb = progress_cb;
1240 arg.rl = &rl;
1241 arg.nloose = *nloose;
1242 arg.npurged = 0;
1243 arg.size_purged = 0;
1244 arg.ncommits = *ncommits;
1245 arg.dry_run = dry_run;
1246 arg.max_mtime = max_mtime;
1247 arg.ignore_mtime = ignore_mtime;
1248 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1249 if (err)
1250 goto done;
1251 *size_after = *size_before - arg.size_purged;
1253 /* Produce a final progress report. */
1254 if (progress_cb) {
1255 err = progress_cb(progress_arg, *nloose, *ncommits, arg.npurged,
1256 -1);
1257 if (err)
1258 goto done;
1260 done:
1261 got_object_idset_free(loose_ids);
1262 got_object_idset_free(traversed_ids);
1263 return err;
1266 static const struct got_error *
1267 purge_redundant_pack(struct got_repository *repo, const char *packidx_path,
1268 int dry_run, int *remove, off_t *size_before, off_t *size_after)
1270 static const char *ext[] = {".idx", ".pack", ".rev", ".bitmap",
1271 ".promisor", ".mtimes"};
1272 struct stat sb;
1273 char *dot, path[PATH_MAX];
1274 size_t i;
1276 if (strlcpy(path, packidx_path, sizeof(path)) >=
1277 sizeof(path))
1278 return got_error(GOT_ERR_NO_SPACE);
1281 * For compatibility with Git, if a matching .keep file exist
1282 * don't delete the packfile.
1284 dot = strrchr(path, '.');
1285 *dot = '\0';
1286 if (strlcat(path, ".keep", sizeof(path)) >= sizeof(path))
1287 return got_error(GOT_ERR_NO_SPACE);
1288 if (faccessat(got_repo_get_fd(repo), path, F_OK, 0) == 0)
1289 *remove = 0;
1291 for (i = 0; i < nitems(ext); ++i) {
1292 *dot = '\0';
1294 if (strlcat(path, ext[i], sizeof(path)) >=
1295 sizeof(path))
1296 return got_error(GOT_ERR_NO_SPACE);
1298 if (fstatat(got_repo_get_fd(repo), path, &sb, 0) ==
1299 -1) {
1300 if (errno == ENOENT &&
1301 strcmp(ext[i], ".pack") != 0 &&
1302 strcmp(ext[i], ".idx") != 0)
1303 continue;
1304 return got_error_from_errno2("fstatat", path);
1307 *size_before += sb.st_size;
1308 if (!*remove) {
1309 *size_after += sb.st_size;
1310 continue;
1313 if (dry_run)
1314 continue;
1316 if (unlinkat(got_repo_get_fd(repo), path, 0) == -1) {
1317 if (errno == ENOENT)
1318 continue;
1319 return got_error_from_errno2("unlinkat",
1320 path);
1324 return NULL;
1327 static const struct got_error *
1328 pack_is_redundant(int *redundant, struct got_repository *repo,
1329 const char *packidx_path, struct got_object_idset *idset)
1331 const struct got_error *err;
1332 struct got_packidx *packidx;
1333 struct got_packidx_object_id *pid;
1334 struct got_object_id id;
1335 size_t i, nobjects;
1337 *redundant = 1;
1339 err = got_repo_get_packidx(&packidx, packidx_path, repo);
1340 if (err)
1341 return err;
1343 nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1344 for (i = 0; i < nobjects; ++i) {
1345 pid = &packidx->hdr.sorted_ids[i];
1347 memset(&id, 0, sizeof(id));
1348 memcpy(&id.sha1, pid->sha1, sizeof(id.sha1));
1350 if (got_object_idset_contains(idset, &id))
1351 continue;
1353 *redundant = 0;
1354 err = got_object_idset_add(idset, &id, NULL);
1355 if (err)
1356 return err;
1359 return NULL;
1362 struct pack_info {
1363 const char *path;
1364 size_t nobjects;
1367 static int
1368 pack_info_cmp(const void *a, const void *b)
1370 const struct pack_info *pa, *pb;
1372 pa = a;
1373 pb = b;
1374 if (pa->nobjects == pb->nobjects)
1375 return strcmp(pa->path, pb->path);
1376 if (pa->nobjects > pb->nobjects)
1377 return -1;
1378 return 1;
1381 const struct got_error *
1382 got_repo_purge_redundant_packfiles(struct got_repository *repo,
1383 off_t *size_before, off_t *size_after, int dry_run,
1384 int nloose, int ncommits, int npurged,
1385 got_cleanup_progress_cb progress_cb, void *progress_arg,
1386 got_cancel_cb cancel_cb, void *cancel_arg)
1388 const struct got_error *err;
1389 struct pack_info *pinfo, *sorted = NULL;
1390 struct got_packidx *packidx;
1391 struct got_object_idset *idset = NULL;
1392 struct got_pathlist_entry *pe;
1393 size_t i, npacks;
1394 int remove, redundant_packs = 0;
1395 struct got_ratelimit rl;
1397 got_ratelimit_init(&rl, 0, 500);
1399 *size_before = 0;
1400 *size_after = 0;
1402 npacks = 0;
1403 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
1404 npacks++;
1406 if (npacks == 0)
1407 return NULL;
1409 sorted = calloc(npacks, sizeof(*sorted));
1410 if (sorted == NULL)
1411 return got_error_from_errno("calloc");
1413 i = 0;
1414 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1415 err = got_repo_get_packidx(&packidx, pe->path, repo);
1416 if (err)
1417 goto done;
1419 pinfo = &sorted[i++];
1420 pinfo->path = pe->path;
1421 pinfo->nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1423 qsort(sorted, npacks, sizeof(*sorted), pack_info_cmp);
1425 idset = got_object_idset_alloc();
1426 if (idset == NULL) {
1427 err = got_error_from_errno("got_object_idset_alloc");
1428 goto done;
1431 for (i = 0; i < npacks; ++i) {
1432 if (cancel_cb) {
1433 err = (*cancel_cb)(cancel_arg);
1434 if (err)
1435 break;
1438 err = pack_is_redundant(&remove, repo, sorted[i].path, idset);
1439 if (err)
1440 goto done;
1441 err = purge_redundant_pack(repo, sorted[i].path, dry_run,
1442 &remove, size_before, size_after);
1443 if (err)
1444 goto done;
1445 if (!remove)
1446 continue;
1447 err = report_cleanup_progress(progress_cb, progress_arg,
1448 &rl, nloose, ncommits, npurged, ++redundant_packs);
1449 if (err)
1450 goto done;
1453 /* Produce a final progress report. */
1454 if (progress_cb) {
1455 err = progress_cb(progress_arg, nloose, ncommits, npurged,
1456 redundant_packs);
1457 if (err)
1458 goto done;
1460 done:
1461 free(sorted);
1462 if (idset)
1463 got_object_idset_free(idset);
1464 return err;
1467 const struct got_error *
1468 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1469 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1470 got_cancel_cb cancel_cb, void *cancel_arg)
1472 const struct got_error *err = NULL;
1473 DIR *packdir = NULL;
1474 struct dirent *dent;
1475 char *pack_relpath = NULL;
1476 int packdir_fd;
1477 struct stat sb;
1479 packdir_fd = openat(got_repo_get_fd(repo),
1480 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1481 if (packdir_fd == -1) {
1482 if (errno == ENOENT)
1483 return NULL;
1484 return got_error_from_errno_fmt("openat: %s/%s",
1485 got_repo_get_path_git_dir(repo),
1486 GOT_OBJECTS_PACK_DIR);
1489 packdir = fdopendir(packdir_fd);
1490 if (packdir == NULL) {
1491 err = got_error_from_errno("fdopendir");
1492 goto done;
1495 while ((dent = readdir(packdir)) != NULL) {
1496 if (cancel_cb) {
1497 err = cancel_cb(cancel_arg);
1498 if (err)
1499 goto done;
1502 if (!got_repo_is_packidx_filename(dent->d_name,
1503 strlen(dent->d_name)))
1504 continue;
1506 err = got_packidx_get_packfile_path(&pack_relpath,
1507 dent->d_name);
1508 if (err)
1509 goto done;
1511 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1512 free(pack_relpath);
1513 pack_relpath = NULL;
1514 continue;
1516 if (errno != ENOENT) {
1517 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1518 got_repo_get_path_git_dir(repo),
1519 GOT_OBJECTS_PACK_DIR,
1520 pack_relpath);
1521 goto done;
1524 if (!dry_run) {
1525 if (unlinkat(packdir_fd, dent->d_name, 0) == -1) {
1526 err = got_error_from_errno("unlinkat");
1527 goto done;
1530 if (progress_cb) {
1531 char *path;
1532 if (asprintf(&path, "%s/%s/%s",
1533 got_repo_get_path_git_dir(repo),
1534 GOT_OBJECTS_PACK_DIR,
1535 dent->d_name) == -1) {
1536 err = got_error_from_errno("asprintf");
1537 goto done;
1539 err = progress_cb(progress_arg, path);
1540 free(path);
1541 if (err)
1542 goto done;
1544 free(pack_relpath);
1545 pack_relpath = NULL;
1547 done:
1548 if (packdir && closedir(packdir) != 0 && err == NULL)
1549 err = got_error_from_errno("closedir");
1550 free(pack_relpath);
1551 return err;