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 const struct got_error *
617 got_repo_cleanup_prepare(struct got_repository *repo,
618 struct got_lockfile **lk)
620 const struct got_error *err;
621 char myname[_POSIX_HOST_NAME_MAX + 1];
623 if (gethostname(myname, sizeof(myname)) == -1)
624 return got_error_from_errno("gethostname");
626 err = got_lockfile_lock(lk, "gc.pid", got_repo_get_fd(repo));
627 if (err)
628 return err;
630 /*
631 * Git uses these info to provide some verbiage when finds a
632 * lock during `git gc --force' so don't try too hard to avoid
633 * short writes and don't care if a race happens between the
634 * lockfile creation and the write itself.
635 */
636 if (dprintf((*lk)->fd, "%d %s", getpid(), myname) < 0)
637 return got_error_from_errno("dprintf");
639 return NULL;
642 const struct got_error *
643 got_repo_cleanup_complete(struct got_repository *repo,
644 struct got_lockfile *lk)
646 if (lk == NULL)
647 return NULL;
649 return got_lockfile_unlock(lk, got_repo_get_fd(repo));
652 static const struct got_error *
653 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
654 void *progress_arg, struct got_ratelimit *rl,
655 int nloose, int ncommits, int npurged, int nredundant)
657 const struct got_error *err;
658 int elapsed;
660 if (progress_cb == NULL)
661 return NULL;
663 err = got_ratelimit_check(&elapsed, rl);
664 if (err || !elapsed)
665 return err;
667 return progress_cb(progress_arg, nloose, ncommits, npurged, nredundant);
670 static const struct got_error *
671 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
672 got_cleanup_progress_cb progress_cb, void *progress_arg,
673 struct got_ratelimit *rl, struct got_repository *repo)
675 const struct got_error *err = NULL;
676 char *path_objects = NULL, *path = NULL;
677 DIR *dir = NULL;
678 struct got_object *obj = NULL;
679 struct got_object_id id;
680 int i, fd = -1;
681 struct stat sb;
683 *ondisk_size = 0;
684 *loose_ids = got_object_idset_alloc();
685 if (*loose_ids == NULL)
686 return got_error_from_errno("got_object_idset_alloc");
688 path_objects = got_repo_get_path_objects(repo);
689 if (path_objects == NULL) {
690 err = got_error_from_errno("got_repo_get_path_objects");
691 goto done;
694 for (i = 0; i <= 0xff; i++) {
695 struct dirent *dent;
697 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
698 err = got_error_from_errno("asprintf");
699 break;
702 dir = opendir(path);
703 if (dir == NULL) {
704 if (errno == ENOENT) {
705 err = NULL;
706 continue;
708 err = got_error_from_errno2("opendir", path);
709 break;
712 while ((dent = readdir(dir)) != NULL) {
713 char *id_str;
715 if (strcmp(dent->d_name, ".") == 0 ||
716 strcmp(dent->d_name, "..") == 0)
717 continue;
719 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
720 err = got_error_from_errno("asprintf");
721 goto done;
724 if (!got_parse_object_id(&id, id_str, repo->algo)) {
725 free(id_str);
726 continue;
728 free(id_str);
730 err = got_object_open_loose_fd(&fd, &id, repo);
731 if (err)
732 goto done;
733 if (fstat(fd, &sb) == -1) {
734 err = got_error_from_errno("fstat");
735 goto done;
737 err = got_object_read_header_privsep(&obj, &id, repo,
738 fd);
739 if (err)
740 goto done;
741 fd = -1; /* already closed */
743 switch (obj->type) {
744 case GOT_OBJ_TYPE_COMMIT:
745 case GOT_OBJ_TYPE_TREE:
746 case GOT_OBJ_TYPE_BLOB:
747 case GOT_OBJ_TYPE_TAG:
748 break;
749 default:
750 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
751 "%d", obj->type);
752 goto done;
754 got_object_close(obj);
755 obj = NULL;
756 (*ondisk_size) += sb.st_size;
757 err = got_object_idset_add(*loose_ids, &id, NULL);
758 if (err)
759 goto done;
760 err = report_cleanup_progress(progress_cb,
761 progress_arg, rl,
762 got_object_idset_num_elements(*loose_ids),
763 -1, -1, -1);
764 if (err)
765 goto done;
768 if (closedir(dir) != 0) {
769 err = got_error_from_errno("closedir");
770 goto done;
772 dir = NULL;
774 free(path);
775 path = NULL;
777 done:
778 if (dir && closedir(dir) != 0 && err == NULL)
779 err = got_error_from_errno("closedir");
780 if (fd != -1 && close(fd) == -1 && err == NULL)
781 err = got_error_from_errno("close");
782 if (err) {
783 got_object_idset_free(*loose_ids);
784 *loose_ids = NULL;
786 if (obj)
787 got_object_close(obj);
788 free(path_objects);
789 free(path);
790 return err;
793 static const struct got_error *
794 preserve_loose_object(struct got_object_idset *loose_ids,
795 struct got_object_id *id, struct got_repository *repo, int *npacked)
797 const struct got_error *err = NULL;
798 struct got_object *obj;
800 if (!got_object_idset_contains(loose_ids, id))
801 return NULL;
803 /*
804 * Try to open this object from a pack file. This ensures that
805 * we do in fact have a valid packed copy of the object. Otherwise
806 * we should not delete the loose representation of this object.
807 */
808 err = got_object_open_packed(&obj, id, repo);
809 if (err == NULL) {
810 got_object_close(obj);
811 /*
812 * The object is referenced and packed.
813 * We can purge the redundantly stored loose object.
814 */
815 (*npacked)++;
816 return NULL;
817 } else if (err->code != GOT_ERR_NO_OBJ)
818 return err;
820 /*
821 * This object is referenced and not packed.
822 * Remove it from our purge set.
823 */
824 return got_object_idset_remove(NULL, loose_ids, id);
827 static const struct got_error *
828 load_tree_entries(struct got_object_id_queue *ids,
829 struct got_object_idset *loose_ids,
830 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
831 const char *dpath, struct got_repository *repo, int *npacked,
832 got_cancel_cb cancel_cb, void *cancel_arg)
834 const struct got_error *err;
835 struct got_tree_object *tree;
836 char *p = NULL;
837 int i;
839 err = got_object_open_as_tree(&tree, repo, tree_id);
840 if (err)
841 return err;
843 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
844 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
845 struct got_object_id *id = got_tree_entry_get_id(e);
846 mode_t mode = got_tree_entry_get_mode(e);
848 if (cancel_cb) {
849 err = (*cancel_cb)(cancel_arg);
850 if (err)
851 break;
854 if (got_object_tree_entry_is_symlink(e) ||
855 got_object_tree_entry_is_submodule(e) ||
856 got_object_idset_contains(traversed_ids, id))
857 continue;
859 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
860 got_tree_entry_get_name(e)) == -1) {
861 err = got_error_from_errno("asprintf");
862 break;
865 if (S_ISDIR(mode)) {
866 struct got_object_qid *qid;
867 err = got_object_qid_alloc(&qid, id);
868 if (err)
869 break;
870 STAILQ_INSERT_TAIL(ids, qid, entry);
871 } else if (S_ISREG(mode)) {
872 /* This blob is referenced. */
873 err = preserve_loose_object(loose_ids, id, repo,
874 npacked);
875 if (err)
876 break;
877 err = got_object_idset_add(traversed_ids, id, NULL);
878 if (err)
879 break;
881 free(p);
882 p = NULL;
885 got_object_tree_close(tree);
886 free(p);
887 return err;
890 static const struct got_error *
891 load_tree(struct got_object_idset *loose_ids,
892 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
893 const char *dpath, struct got_repository *repo, int *npacked,
894 got_cancel_cb cancel_cb, void *cancel_arg)
896 const struct got_error *err = NULL;
897 struct got_object_id_queue tree_ids;
898 struct got_object_qid *qid;
900 err = got_object_qid_alloc(&qid, tree_id);
901 if (err)
902 return err;
904 STAILQ_INIT(&tree_ids);
905 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
907 while (!STAILQ_EMPTY(&tree_ids)) {
908 if (cancel_cb) {
909 err = (*cancel_cb)(cancel_arg);
910 if (err)
911 break;
914 qid = STAILQ_FIRST(&tree_ids);
915 STAILQ_REMOVE_HEAD(&tree_ids, entry);
917 if (got_object_idset_contains(traversed_ids, &qid->id)) {
918 got_object_qid_free(qid);
919 continue;
922 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
923 if (err) {
924 got_object_qid_free(qid);
925 break;
928 /* This tree is referenced. */
929 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
930 if (err)
931 break;
933 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
934 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
935 got_object_qid_free(qid);
936 if (err)
937 break;
940 got_object_id_queue_free(&tree_ids);
941 return err;
944 static const struct got_error *
945 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
946 int *npacked, struct got_object_idset *traversed_ids,
947 struct got_object_id *id, struct got_repository *repo,
948 got_cleanup_progress_cb progress_cb, void *progress_arg,
949 struct got_ratelimit *rl, int nloose,
950 got_cancel_cb cancel_cb, void *cancel_arg)
952 const struct got_error *err;
953 struct got_commit_object *commit = NULL;
954 struct got_tag_object *tag = NULL;
955 struct got_object_id *tree_id = NULL;
956 struct got_object_id_queue ids;
957 struct got_object_qid *qid;
958 int obj_type;
960 err = got_object_qid_alloc(&qid, id);
961 if (err)
962 return err;
964 STAILQ_INIT(&ids);
965 STAILQ_INSERT_TAIL(&ids, qid, entry);
967 while (!STAILQ_EMPTY(&ids)) {
968 if (cancel_cb) {
969 err = (*cancel_cb)(cancel_arg);
970 if (err)
971 break;
974 qid = STAILQ_FIRST(&ids);
975 STAILQ_REMOVE_HEAD(&ids, entry);
977 if (got_object_idset_contains(traversed_ids, &qid->id)) {
978 got_object_qid_free(qid);
979 qid = NULL;
980 continue;
983 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
984 if (err)
985 break;
987 /* This commit or tag is referenced. */
988 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
989 if (err)
990 break;
992 err = got_object_get_type(&obj_type, repo, &qid->id);
993 if (err)
994 break;
995 switch (obj_type) {
996 case GOT_OBJ_TYPE_COMMIT:
997 err = got_object_open_as_commit(&commit, repo,
998 &qid->id);
999 if (err)
1000 goto done;
1001 break;
1002 case GOT_OBJ_TYPE_TAG:
1003 err = got_object_open_as_tag(&tag, repo, &qid->id);
1004 if (err)
1005 goto done;
1006 break;
1007 default:
1008 /* should not happen */
1009 err = got_error(GOT_ERR_OBJ_TYPE);
1010 goto done;
1013 /* Find a tree object to scan. */
1014 if (commit) {
1015 tree_id = got_object_commit_get_tree_id(commit);
1016 } else if (tag) {
1017 obj_type = got_object_tag_get_object_type(tag);
1018 switch (obj_type) {
1019 case GOT_OBJ_TYPE_COMMIT:
1020 err = got_object_open_as_commit(&commit, repo,
1021 got_object_tag_get_object_id(tag));
1022 if (err)
1023 goto done;
1024 tree_id = got_object_commit_get_tree_id(commit);
1025 break;
1026 case GOT_OBJ_TYPE_TREE:
1027 tree_id = got_object_tag_get_object_id(tag);
1028 break;
1029 default:
1031 * Tag points at something other than a
1032 * commit or tree. Leave this weird tag object
1033 * and the object it points to on disk.
1035 err = got_object_idset_remove(NULL, loose_ids,
1036 &qid->id);
1037 if (err && err->code != GOT_ERR_NO_OBJ)
1038 goto done;
1039 err = got_object_idset_remove(NULL, loose_ids,
1040 got_object_tag_get_object_id(tag));
1041 if (err && err->code != GOT_ERR_NO_OBJ)
1042 goto done;
1043 err = NULL;
1044 break;
1048 if (tree_id) {
1049 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1050 repo, npacked, cancel_cb, cancel_arg);
1051 if (err)
1052 break;
1055 if (commit || tag)
1056 (*ncommits)++; /* scanned tags are counted as commits */
1058 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1059 nloose, *ncommits, -1, -1);
1060 if (err)
1061 break;
1063 if (commit) {
1064 /* Find parent commits to scan. */
1065 const struct got_object_id_queue *parent_ids;
1066 parent_ids = got_object_commit_get_parent_ids(commit);
1067 err = got_object_id_queue_copy(parent_ids, &ids);
1068 if (err)
1069 break;
1070 got_object_commit_close(commit);
1071 commit = NULL;
1073 if (tag) {
1074 got_object_tag_close(tag);
1075 tag = NULL;
1077 got_object_qid_free(qid);
1078 qid = NULL;
1080 done:
1081 if (qid)
1082 got_object_qid_free(qid);
1083 if (commit)
1084 got_object_commit_close(commit);
1085 if (tag)
1086 got_object_tag_close(tag);
1087 got_object_id_queue_free(&ids);
1088 return err;
1091 struct purge_loose_object_arg {
1092 struct got_repository *repo;
1093 got_cleanup_progress_cb progress_cb;
1094 void *progress_arg;
1095 struct got_ratelimit *rl;
1096 int nloose;
1097 int ncommits;
1098 int npurged;
1099 off_t size_purged;
1100 int dry_run;
1101 time_t max_mtime;
1102 int ignore_mtime;
1105 static const struct got_error *
1106 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1108 struct purge_loose_object_arg *a = arg;
1109 const struct got_error *err, *unlock_err = NULL;
1110 char *path = NULL;
1111 int fd = -1;
1112 struct stat sb;
1113 struct got_lockfile *lf = NULL;
1115 err = got_object_get_path(&path, id, a->repo);
1116 if (err)
1117 return err;
1119 err = got_object_open_loose_fd(&fd, id, a->repo);
1120 if (err)
1121 goto done;
1123 if (fstat(fd, &sb) == -1) {
1124 err = got_error_from_errno("fstat");
1125 goto done;
1129 * Do not delete objects which are younger than our maximum
1130 * modification time threshold. This prevents a race where
1131 * new objects which are being added to the repository
1132 * concurrently would be deleted.
1134 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1135 if (!a->dry_run) {
1136 err = got_lockfile_lock(&lf, path, -1);
1137 if (err)
1138 goto done;
1139 if (unlink(path) == -1) {
1140 err = got_error_from_errno2("unlink", path);
1141 goto done;
1145 a->npurged++;
1146 a->size_purged += sb.st_size;
1147 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1148 a->rl, a->nloose, a->ncommits, a->npurged, -1);
1149 if (err)
1150 goto done;
1152 done:
1153 if (fd != -1 && close(fd) == -1 && err == NULL)
1154 err = got_error_from_errno("close");
1155 free(path);
1156 if (lf)
1157 unlock_err = got_lockfile_unlock(lf, -1);
1158 return err ? err : unlock_err;
1161 const struct got_error *
1162 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1163 off_t *size_before, off_t *size_after, int *ncommits, int *nloose,
1164 int *npacked, int dry_run, int ignore_mtime,
1165 got_cleanup_progress_cb progress_cb, void *progress_arg,
1166 got_cancel_cb cancel_cb, void *cancel_arg)
1168 const struct got_error *err;
1169 struct got_object_idset *loose_ids;
1170 struct got_object_idset *traversed_ids;
1171 struct got_object_id **referenced_ids;
1172 int i, nreferenced;
1173 struct got_reflist_head refs;
1174 struct got_reflist_entry *re;
1175 struct purge_loose_object_arg arg;
1176 time_t max_mtime = 0;
1177 struct got_ratelimit rl;
1179 TAILQ_INIT(&refs);
1180 got_ratelimit_init(&rl, 0, 500);
1182 *size_before = 0;
1183 *size_after = 0;
1184 *npacked = 0;
1186 err = get_loose_object_ids(&loose_ids, size_before,
1187 progress_cb, progress_arg, &rl, repo);
1188 if (err)
1189 return err;
1190 *nloose = got_object_idset_num_elements(loose_ids);
1191 if (*nloose == 0) {
1192 got_object_idset_free(loose_ids);
1193 if (progress_cb) {
1194 err = progress_cb(progress_arg, 0, 0, 0, -1);
1195 if (err)
1196 return err;
1198 return NULL;
1201 traversed_ids = got_object_idset_alloc();
1202 if (traversed_ids == NULL) {
1203 err = got_error_from_errno("got_object_idset_alloc");
1204 goto done;
1207 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1208 if (err)
1209 goto done;
1210 if (!ignore_mtime) {
1211 TAILQ_FOREACH(re, &refs, entry) {
1212 time_t mtime = got_ref_get_mtime(re->ref);
1213 if (mtime > max_mtime)
1214 max_mtime = mtime;
1217 * For safety, keep objects created within 10 minutes
1218 * before the youngest reference was created.
1220 if (max_mtime >= 600)
1221 max_mtime -= 600;
1224 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1225 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1226 &refs, repo, cancel_cb, cancel_arg);
1227 if (err)
1228 goto done;
1230 for (i = 0; i < nreferenced; i++) {
1231 struct got_object_id *id = referenced_ids[i];
1232 err = load_commit_or_tag(loose_ids, ncommits, npacked,
1233 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1234 *nloose, cancel_cb, cancel_arg);
1235 if (err)
1236 goto done;
1239 /* Any remaining loose objects are unreferenced and can be purged. */
1240 arg.repo = repo;
1241 arg.progress_arg = progress_arg;
1242 arg.progress_cb = progress_cb;
1243 arg.rl = &rl;
1244 arg.nloose = *nloose;
1245 arg.npurged = 0;
1246 arg.size_purged = 0;
1247 arg.ncommits = *ncommits;
1248 arg.dry_run = dry_run;
1249 arg.max_mtime = max_mtime;
1250 arg.ignore_mtime = ignore_mtime;
1251 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1252 if (err)
1253 goto done;
1254 *size_after = *size_before - arg.size_purged;
1256 /* Produce a final progress report. */
1257 if (progress_cb) {
1258 err = progress_cb(progress_arg, *nloose, *ncommits, arg.npurged,
1259 -1);
1260 if (err)
1261 goto done;
1263 done:
1264 got_object_idset_free(loose_ids);
1265 got_object_idset_free(traversed_ids);
1266 return err;
1269 static const struct got_error *
1270 purge_redundant_pack(struct got_repository *repo, const char *packidx_path,
1271 int dry_run, int *remove, off_t *size_before, off_t *size_after)
1273 static const char *ext[] = {".idx", ".pack", ".rev", ".bitmap",
1274 ".promisor", ".mtimes"};
1275 struct stat sb;
1276 char *dot, path[PATH_MAX];
1277 size_t i;
1279 if (strlcpy(path, packidx_path, sizeof(path)) >=
1280 sizeof(path))
1281 return got_error(GOT_ERR_NO_SPACE);
1284 * For compatibility with Git, if a matching .keep file exist
1285 * don't delete the packfile.
1287 dot = strrchr(path, '.');
1288 *dot = '\0';
1289 if (strlcat(path, ".keep", sizeof(path)) >= sizeof(path))
1290 return got_error(GOT_ERR_NO_SPACE);
1291 if (faccessat(got_repo_get_fd(repo), path, F_OK, 0) == 0)
1292 *remove = 0;
1294 for (i = 0; i < nitems(ext); ++i) {
1295 *dot = '\0';
1297 if (strlcat(path, ext[i], sizeof(path)) >=
1298 sizeof(path))
1299 return got_error(GOT_ERR_NO_SPACE);
1301 if (fstatat(got_repo_get_fd(repo), path, &sb, 0) ==
1302 -1) {
1303 if (errno == ENOENT &&
1304 strcmp(ext[i], ".pack") != 0 &&
1305 strcmp(ext[i], ".idx") != 0)
1306 continue;
1307 return got_error_from_errno2("fstatat", path);
1310 *size_before += sb.st_size;
1311 if (!*remove) {
1312 *size_after += sb.st_size;
1313 continue;
1316 if (dry_run)
1317 continue;
1319 if (unlinkat(got_repo_get_fd(repo), path, 0) == -1) {
1320 if (errno == ENOENT)
1321 continue;
1322 return got_error_from_errno2("unlinkat",
1323 path);
1327 return NULL;
1330 static const struct got_error *
1331 pack_is_redundant(int *redundant, struct got_repository *repo,
1332 const char *packidx_path, struct got_object_idset *idset)
1334 const struct got_error *err;
1335 struct got_packidx *packidx;
1336 struct got_packidx_object_id *pid;
1337 struct got_object_id id;
1338 size_t i, nobjects;
1340 *redundant = 1;
1342 err = got_repo_get_packidx(&packidx, packidx_path, repo);
1343 if (err)
1344 return err;
1346 nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1347 for (i = 0; i < nobjects; ++i) {
1348 pid = &packidx->hdr.sorted_ids[i];
1350 memset(&id, 0, sizeof(id));
1351 memcpy(&id.sha1, pid->sha1, sizeof(id.sha1));
1353 if (got_object_idset_contains(idset, &id))
1354 continue;
1356 *redundant = 0;
1357 err = got_object_idset_add(idset, &id, NULL);
1358 if (err)
1359 return err;
1362 return NULL;
1365 struct pack_info {
1366 const char *path;
1367 size_t nobjects;
1370 static int
1371 pack_info_cmp(const void *a, const void *b)
1373 const struct pack_info *pa, *pb;
1375 pa = a;
1376 pb = b;
1377 if (pa->nobjects == pb->nobjects)
1378 return strcmp(pa->path, pb->path);
1379 if (pa->nobjects > pb->nobjects)
1380 return -1;
1381 return 1;
1384 const struct got_error *
1385 got_repo_purge_redundant_packfiles(struct got_repository *repo,
1386 off_t *size_before, off_t *size_after, int dry_run,
1387 int nloose, int ncommits, int npurged,
1388 got_cleanup_progress_cb progress_cb, void *progress_arg,
1389 got_cancel_cb cancel_cb, void *cancel_arg)
1391 const struct got_error *err;
1392 struct pack_info *pinfo, *sorted = NULL;
1393 struct got_packidx *packidx;
1394 struct got_object_idset *idset = NULL;
1395 struct got_pathlist_entry *pe;
1396 size_t i, npacks;
1397 int remove, redundant_packs = 0;
1398 struct got_ratelimit rl;
1400 got_ratelimit_init(&rl, 0, 500);
1402 *size_before = 0;
1403 *size_after = 0;
1405 npacks = 0;
1406 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
1407 npacks++;
1409 if (npacks == 0)
1410 return NULL;
1412 sorted = calloc(npacks, sizeof(*sorted));
1413 if (sorted == NULL)
1414 return got_error_from_errno("calloc");
1416 i = 0;
1417 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1418 err = got_repo_get_packidx(&packidx, pe->path, repo);
1419 if (err)
1420 goto done;
1422 pinfo = &sorted[i++];
1423 pinfo->path = pe->path;
1424 pinfo->nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1426 qsort(sorted, npacks, sizeof(*sorted), pack_info_cmp);
1428 idset = got_object_idset_alloc();
1429 if (idset == NULL) {
1430 err = got_error_from_errno("got_object_idset_alloc");
1431 goto done;
1434 for (i = 0; i < npacks; ++i) {
1435 if (cancel_cb) {
1436 err = (*cancel_cb)(cancel_arg);
1437 if (err)
1438 break;
1441 err = pack_is_redundant(&remove, repo, sorted[i].path, idset);
1442 if (err)
1443 goto done;
1444 err = purge_redundant_pack(repo, sorted[i].path, dry_run,
1445 &remove, size_before, size_after);
1446 if (err)
1447 goto done;
1448 if (!remove)
1449 continue;
1450 err = report_cleanup_progress(progress_cb, progress_arg,
1451 &rl, nloose, ncommits, npurged, ++redundant_packs);
1452 if (err)
1453 goto done;
1456 /* Produce a final progress report. */
1457 if (progress_cb) {
1458 err = progress_cb(progress_arg, nloose, ncommits, npurged,
1459 redundant_packs);
1460 if (err)
1461 goto done;
1463 done:
1464 free(sorted);
1465 if (idset)
1466 got_object_idset_free(idset);
1467 return err;
1470 const struct got_error *
1471 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1472 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1473 got_cancel_cb cancel_cb, void *cancel_arg)
1475 const struct got_error *err = NULL;
1476 DIR *packdir = NULL;
1477 struct dirent *dent;
1478 char *pack_relpath = NULL;
1479 int packdir_fd;
1480 struct stat sb;
1482 packdir_fd = openat(got_repo_get_fd(repo),
1483 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1484 if (packdir_fd == -1) {
1485 if (errno == ENOENT)
1486 return NULL;
1487 return got_error_from_errno_fmt("openat: %s/%s",
1488 got_repo_get_path_git_dir(repo),
1489 GOT_OBJECTS_PACK_DIR);
1492 packdir = fdopendir(packdir_fd);
1493 if (packdir == NULL) {
1494 err = got_error_from_errno("fdopendir");
1495 goto done;
1498 while ((dent = readdir(packdir)) != NULL) {
1499 if (cancel_cb) {
1500 err = cancel_cb(cancel_arg);
1501 if (err)
1502 goto done;
1505 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1506 continue;
1508 err = got_packidx_get_packfile_path(&pack_relpath,
1509 dent->d_name);
1510 if (err)
1511 goto done;
1513 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1514 free(pack_relpath);
1515 pack_relpath = NULL;
1516 continue;
1518 if (errno != ENOENT) {
1519 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1520 got_repo_get_path_git_dir(repo),
1521 GOT_OBJECTS_PACK_DIR,
1522 pack_relpath);
1523 goto done;
1526 if (!dry_run) {
1527 if (unlinkat(packdir_fd, dent->d_name, 0) == -1) {
1528 err = got_error_from_errno("unlinkat");
1529 goto done;
1532 if (progress_cb) {
1533 char *path;
1534 if (asprintf(&path, "%s/%s/%s",
1535 got_repo_get_path_git_dir(repo),
1536 GOT_OBJECTS_PACK_DIR,
1537 dent->d_name) == -1) {
1538 err = got_error_from_errno("asprintf");
1539 goto done;
1541 err = progress_cb(progress_arg, path);
1542 free(path);
1543 if (err)
1544 goto done;
1546 free(pack_relpath);
1547 pack_relpath = NULL;
1549 done:
1550 if (packdir && closedir(packdir) != 0 && err == NULL)
1551 err = got_error_from_errno("closedir");
1552 free(pack_relpath);
1553 return err;