Blob


1 /*
2 * Copyright (c) 2018, 2019, 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/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <sha2.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <zlib.h>
40 #include <errno.h>
41 #include <libgen.h>
42 #include <stdint.h>
43 #include <imsg.h>
44 #include <uuid.h>
46 #include "bloom.h"
48 #include "got_error.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_path.h"
52 #include "got_cancel.h"
53 #include "got_object.h"
54 #include "got_opentemp.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_delta_cache.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_object.h"
60 #include "got_lib_object_parse.h"
61 #include "got_lib_object_create.h"
62 #include "got_lib_pack.h"
63 #include "got_lib_privsep.h"
64 #include "got_lib_hash.h"
65 #include "got_lib_object_cache.h"
66 #include "got_lib_repository.h"
67 #include "got_lib_gotconfig.h"
69 #ifndef nitems
70 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
71 #endif
73 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
75 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
76 got_packidx_bloom_filter_cmp);
78 const char *
79 got_repo_get_path(struct got_repository *repo)
80 {
81 return repo->path;
82 }
84 const char *
85 got_repo_get_path_git_dir(struct got_repository *repo)
86 {
87 return repo->path_git_dir;
88 }
90 int
91 got_repo_get_fd(struct got_repository *repo)
92 {
93 return repo->gitdir_fd;
94 }
96 const char *
97 got_repo_get_gitconfig_author_name(struct got_repository *repo)
98 {
99 return repo->gitconfig_author_name;
102 const char *
103 got_repo_get_gitconfig_author_email(struct got_repository *repo)
105 return repo->gitconfig_author_email;
108 const char *
109 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
111 return repo->global_gitconfig_author_name;
114 const char *
115 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
117 return repo->global_gitconfig_author_email;
120 const char *
121 got_repo_get_gitconfig_owner(struct got_repository *repo)
123 return repo->gitconfig_owner;
126 void
127 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
128 struct got_repository *repo)
130 *extensions = repo->extensions;
131 *nextensions = repo->nextensions;
134 int
135 got_repo_is_bare(struct got_repository *repo)
137 return (strcmp(repo->path, repo->path_git_dir) == 0);
140 static char *
141 get_path_git_child(struct got_repository *repo, const char *basename)
143 char *path_child;
145 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
146 basename) == -1)
147 return NULL;
149 return path_child;
152 char *
153 got_repo_get_path_objects(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_OBJECTS_DIR);
158 char *
159 got_repo_get_path_objects_pack(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
164 char *
165 got_repo_get_path_refs(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_REFS_DIR);
170 char *
171 got_repo_get_path_packed_refs(struct got_repository *repo)
173 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
176 static char *
177 get_path_head(struct got_repository *repo)
179 return get_path_git_child(repo, GOT_HEAD_FILE);
182 char *
183 got_repo_get_path_gitconfig(struct got_repository *repo)
185 return get_path_git_child(repo, GOT_GITCONFIG);
188 char *
189 got_repo_get_path_gotconfig(struct got_repository *repo)
191 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
194 const struct got_gotconfig *
195 got_repo_get_gotconfig(struct got_repository *repo)
197 return repo->gotconfig;
200 void
201 got_repo_get_gitconfig_remotes(int *nremotes,
202 const struct got_remote_repo **remotes, struct got_repository *repo)
204 *nremotes = repo->ngitconfig_remotes;
205 *remotes = repo->gitconfig_remotes;
208 static int
209 is_git_repo(struct got_repository *repo)
211 const char *path_git = got_repo_get_path_git_dir(repo);
212 char *path_objects = got_repo_get_path_objects(repo);
213 char *path_refs = got_repo_get_path_refs(repo);
214 char *path_head = get_path_head(repo);
215 int ret = 0;
216 struct stat sb;
217 struct got_reference *head_ref;
219 if (lstat(path_git, &sb) == -1)
220 goto done;
221 if (!S_ISDIR(sb.st_mode))
222 goto done;
224 if (lstat(path_objects, &sb) == -1)
225 goto done;
226 if (!S_ISDIR(sb.st_mode))
227 goto done;
229 if (lstat(path_refs, &sb) == -1)
230 goto done;
231 if (!S_ISDIR(sb.st_mode))
232 goto done;
234 if (lstat(path_head, &sb) == -1)
235 goto done;
236 if (!S_ISREG(sb.st_mode))
237 goto done;
239 /* Check if the HEAD reference can be opened. */
240 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
241 goto done;
242 got_ref_close(head_ref);
244 ret = 1;
245 done:
246 free(path_objects);
247 free(path_refs);
248 free(path_head);
249 return ret;
253 static const struct got_error *
254 close_tempfiles(int *fds, size_t nfds)
256 const struct got_error *err = NULL;
257 int i;
259 for (i = 0; i < nfds; i++) {
260 if (fds[i] == -1)
261 continue;
262 if (close(fds[i]) == -1) {
263 err = got_error_from_errno("close");
264 break;
267 free(fds);
268 return err;
271 static const struct got_error *
272 open_tempfiles(int **fds, size_t array_size, size_t nfds)
274 const struct got_error *err = NULL;
275 int i;
277 *fds = calloc(array_size, sizeof(**fds));
278 if (*fds == NULL)
279 return got_error_from_errno("calloc");
281 for (i = 0; i < array_size; i++)
282 (*fds)[i] = -1;
284 for (i = 0; i < nfds; i++) {
285 (*fds)[i] = got_opentempfd();
286 if ((*fds)[i] == -1) {
287 err = got_error_from_errno("got_opentempfd");
288 close_tempfiles(*fds, nfds);
289 *fds = NULL;
290 return err;
294 return NULL;
297 static const struct got_error *
298 get_pack_cache_size(int *pack_cache_size)
300 struct rlimit rl;
302 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
303 return got_error_from_errno("getrlimit");
305 *pack_cache_size = GOT_PACK_CACHE_SIZE;
306 if (*pack_cache_size > rl.rlim_cur / 8)
307 *pack_cache_size = rl.rlim_cur / 8;
309 return NULL;
312 const struct got_error *
313 got_repo_pack_fds_open(int **pack_fds)
315 const struct got_error *err;
316 int nfds;
318 err = get_pack_cache_size(&nfds);
319 if (err)
320 return err;
322 /*
323 * We need one basefd and one accumfd per cached pack.
324 * Our constants should be set up in a way such that
325 * this error never triggers.
326 */
327 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
328 return got_error(GOT_ERR_NO_SPACE);
330 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
333 const struct got_error *
334 got_repo_pack_fds_close(int *pack_fds)
336 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
339 const struct got_error *
340 got_repo_temp_fds_open(int **temp_fds)
342 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
343 GOT_REPO_NUM_TEMPFILES);
346 void
347 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
349 int i;
351 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
352 repo->tempfiles[i] = temp_fds[i];
355 const struct got_error *
356 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
358 int i;
360 *fd = -1;
361 *idx = -1;
363 for (i = 0; i < nitems(repo->tempfiles); i++) {
364 if (repo->tempfile_use_mask & (1 << i))
365 continue;
366 if (repo->tempfiles[i] != -1) {
367 if (ftruncate(repo->tempfiles[i], 0L) == -1)
368 return got_error_from_errno("ftruncate");
369 *fd = repo->tempfiles[i];
370 *idx = i;
371 repo->tempfile_use_mask |= (1 << i);
372 return NULL;
376 return got_error(GOT_ERR_REPO_TEMPFILE);
379 void
380 got_repo_temp_fds_put(int idx, struct got_repository *repo)
382 repo->tempfile_use_mask &= ~(1 << idx);
385 const struct got_error *
386 got_repo_temp_fds_close(int *temp_fds)
388 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
391 const struct got_error *
392 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
393 struct got_object *obj)
395 #ifndef GOT_NO_OBJ_CACHE
396 const struct got_error *err = NULL;
397 err = got_object_cache_add(&repo->objcache, id, obj);
398 if (err) {
399 if (err->code == GOT_ERR_OBJ_EXISTS ||
400 err->code == GOT_ERR_OBJ_TOO_LARGE)
401 err = NULL;
402 return err;
404 obj->refcnt++;
405 #endif
406 return NULL;
409 struct got_object *
410 got_repo_get_cached_object(struct got_repository *repo,
411 struct got_object_id *id)
413 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
416 const struct got_error *
417 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
418 struct got_tree_object *tree)
420 #ifndef GOT_NO_OBJ_CACHE
421 const struct got_error *err = NULL;
422 err = got_object_cache_add(&repo->treecache, id, tree);
423 if (err) {
424 if (err->code == GOT_ERR_OBJ_EXISTS ||
425 err->code == GOT_ERR_OBJ_TOO_LARGE)
426 err = NULL;
427 return err;
429 tree->refcnt++;
430 #endif
431 return NULL;
434 struct got_tree_object *
435 got_repo_get_cached_tree(struct got_repository *repo,
436 struct got_object_id *id)
438 return (struct got_tree_object *)got_object_cache_get(
439 &repo->treecache, id);
442 const struct got_error *
443 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
444 struct got_commit_object *commit)
446 #ifndef GOT_NO_OBJ_CACHE
447 const struct got_error *err = NULL;
448 err = got_object_cache_add(&repo->commitcache, id, commit);
449 if (err) {
450 if (err->code == GOT_ERR_OBJ_EXISTS ||
451 err->code == GOT_ERR_OBJ_TOO_LARGE)
452 err = NULL;
453 return err;
455 commit->refcnt++;
456 #endif
457 return NULL;
460 struct got_commit_object *
461 got_repo_get_cached_commit(struct got_repository *repo,
462 struct got_object_id *id)
464 return (struct got_commit_object *)got_object_cache_get(
465 &repo->commitcache, id);
468 const struct got_error *
469 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
470 struct got_tag_object *tag)
472 #ifndef GOT_NO_OBJ_CACHE
473 const struct got_error *err = NULL;
474 err = got_object_cache_add(&repo->tagcache, id, tag);
475 if (err) {
476 if (err->code == GOT_ERR_OBJ_EXISTS ||
477 err->code == GOT_ERR_OBJ_TOO_LARGE)
478 err = NULL;
479 return err;
481 tag->refcnt++;
482 #endif
483 return NULL;
486 struct got_tag_object *
487 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
489 return (struct got_tag_object *)got_object_cache_get(
490 &repo->tagcache, id);
493 const struct got_error *
494 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
495 struct got_raw_object *raw)
497 #ifndef GOT_NO_OBJ_CACHE
498 const struct got_error *err = NULL;
499 err = got_object_cache_add(&repo->rawcache, id, raw);
500 if (err) {
501 if (err->code == GOT_ERR_OBJ_EXISTS ||
502 err->code == GOT_ERR_OBJ_TOO_LARGE)
503 err = NULL;
504 return err;
506 raw->refcnt++;
507 #endif
508 return NULL;
512 struct got_raw_object *
513 got_repo_get_cached_raw_object(struct got_repository *repo,
514 struct got_object_id *id)
516 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
520 static const struct got_error *
521 open_repo(struct got_repository *repo, const char *path)
523 const struct got_error *err = NULL;
525 repo->gitdir_fd = -1;
527 /* bare git repository? */
528 repo->path_git_dir = strdup(path);
529 if (repo->path_git_dir == NULL)
530 return got_error_from_errno("strdup");
531 if (is_git_repo(repo)) {
532 repo->path = strdup(repo->path_git_dir);
533 if (repo->path == NULL) {
534 err = got_error_from_errno("strdup");
535 goto done;
537 repo->gitdir_fd = open(repo->path_git_dir,
538 O_DIRECTORY | O_CLOEXEC);
539 if (repo->gitdir_fd == -1) {
540 err = got_error_from_errno2("open",
541 repo->path_git_dir);
542 goto done;
544 return NULL;
547 /* git repository with working tree? */
548 free(repo->path_git_dir);
549 repo->path_git_dir = NULL;
550 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
551 err = got_error_from_errno("asprintf");
552 goto done;
554 if (is_git_repo(repo)) {
555 repo->path = strdup(path);
556 if (repo->path == NULL) {
557 err = got_error_from_errno("strdup");
558 goto done;
560 repo->gitdir_fd = open(repo->path_git_dir,
561 O_DIRECTORY | O_CLOEXEC);
562 if (repo->gitdir_fd == -1) {
563 err = got_error_from_errno2("open",
564 repo->path_git_dir);
565 goto done;
567 return NULL;
570 err = got_error(GOT_ERR_NOT_GIT_REPO);
571 done:
572 if (err) {
573 free(repo->path);
574 repo->path = NULL;
575 free(repo->path_git_dir);
576 repo->path_git_dir = NULL;
577 if (repo->gitdir_fd != -1)
578 close(repo->gitdir_fd);
579 repo->gitdir_fd = -1;
582 return err;
585 static const struct got_error *
586 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
588 const struct got_error *err = NULL;
589 char *repo_gitconfig_path = NULL;
590 char *object_format = NULL;
592 if (global_gitconfig_path) {
593 /* Read settings from ~/.gitconfig. */
594 int dummy_repo_version;
595 err = got_repo_read_gitconfig(&dummy_repo_version,
596 &repo->global_gitconfig_author_name,
597 &repo->global_gitconfig_author_email,
598 NULL, NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
599 if (err)
600 return err;
603 /* Read repository's .git/config file. */
604 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
605 if (repo_gitconfig_path == NULL)
606 return got_error_from_errno("got_repo_get_path_gitconfig");
608 err = got_repo_read_gitconfig(
609 &repo->gitconfig_repository_format_version,
610 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
611 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
612 &repo->gitconfig_owner, &object_format,
613 &repo->extensions, &repo->nextensions,
614 repo_gitconfig_path);
615 if (err)
616 goto done;
618 if (!object_format)
619 repo->algo = GOT_HASH_SHA1;
620 else {
621 if (!strcmp(object_format, "sha256"))
622 repo->algo = GOT_HASH_SHA256;
623 else {
624 err = got_error_msg(GOT_ERR_OBJECT_FORMAT,
625 object_format);
626 goto done;
630 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
631 int i;
633 for (i = 0; i < repo->ngitconfig_remotes; i++) {
634 got_repo_free_remote_repo_data(
635 &repo->gitconfig_remotes[i]);
637 free(repo->gitconfig_remotes);
638 repo->gitconfig_remotes = NULL;
639 repo->ngitconfig_remotes = 0;
641 free(repo->gitconfig_author_name);
642 repo->gitconfig_author_name = NULL;
643 free(repo->gitconfig_author_email);
644 repo->gitconfig_author_email = NULL;
646 free(repo->global_gitconfig_author_name);
647 repo->global_gitconfig_author_name = NULL;
648 free(repo->global_gitconfig_author_email);
649 repo->global_gitconfig_author_email = NULL;
652 done:
653 free(object_format);
654 free(repo_gitconfig_path);
655 return err;
658 static const struct got_error *
659 read_gotconfig(struct got_repository *repo)
661 const struct got_error *err = NULL;
662 char *gotconfig_path;
664 gotconfig_path = got_repo_get_path_gotconfig(repo);
665 if (gotconfig_path == NULL)
666 return got_error_from_errno("got_repo_get_path_gotconfig");
668 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
669 free(gotconfig_path);
670 return err;
673 /* Supported repository format extensions. */
674 static const char *const repo_extensions[] = {
675 "noop", /* Got supports repository format version 1. */
676 "preciousObjects", /* Supported by gotadmin cleanup. */
677 "worktreeConfig", /* Got does not care about Git work trees. */
678 };
680 const struct got_error *
681 got_repo_open(struct got_repository **repop, const char *path,
682 const char *global_gitconfig_path, int *pack_fds)
684 struct got_repository *repo = NULL;
685 const struct got_error *err = NULL;
686 char *repo_path = NULL;
687 size_t i, j = 0;
689 *repop = NULL;
691 repo = calloc(1, sizeof(*repo));
692 if (repo == NULL)
693 return got_error_from_errno("calloc");
695 RB_INIT(&repo->packidx_bloom_filters);
696 TAILQ_INIT(&repo->packidx_paths);
698 for (i = 0; i < nitems(repo->privsep_children); i++) {
699 memset(&repo->privsep_children[i], 0,
700 sizeof(repo->privsep_children[0]));
701 repo->privsep_children[i].imsg_fd = -1;
704 err = got_object_cache_init(&repo->objcache,
705 GOT_OBJECT_CACHE_TYPE_OBJ);
706 if (err)
707 goto done;
708 err = got_object_cache_init(&repo->treecache,
709 GOT_OBJECT_CACHE_TYPE_TREE);
710 if (err)
711 goto done;
712 err = got_object_cache_init(&repo->commitcache,
713 GOT_OBJECT_CACHE_TYPE_COMMIT);
714 if (err)
715 goto done;
716 err = got_object_cache_init(&repo->tagcache,
717 GOT_OBJECT_CACHE_TYPE_TAG);
718 if (err)
719 goto done;
720 err = got_object_cache_init(&repo->rawcache,
721 GOT_OBJECT_CACHE_TYPE_RAW);
722 if (err)
723 goto done;
725 err = get_pack_cache_size(&repo->pack_cache_size);
726 if (err)
727 goto done;
728 for (i = 0; i < nitems(repo->packs); i++) {
729 if (pack_fds != NULL && i < repo->pack_cache_size) {
730 repo->packs[i].basefd = pack_fds[j++];
731 repo->packs[i].accumfd = pack_fds[j++];
732 } else {
733 repo->packs[i].basefd = -1;
734 repo->packs[i].accumfd = -1;
737 for (i = 0; i < nitems(repo->tempfiles); i++)
738 repo->tempfiles[i] = -1;
739 repo->pinned_pack = -1;
740 repo->pinned_packidx = -1;
741 repo->pinned_pid = 0;
743 repo_path = realpath(path, NULL);
744 if (repo_path == NULL) {
745 err = got_error_from_errno2("realpath", path);
746 goto done;
749 for (;;) {
750 char *parent_path;
752 err = open_repo(repo, repo_path);
753 if (err == NULL)
754 break;
755 if (err->code != GOT_ERR_NOT_GIT_REPO)
756 goto done;
757 if (repo_path[0] == '/' && repo_path[1] == '\0') {
758 err = got_error(GOT_ERR_NOT_GIT_REPO);
759 goto done;
761 err = got_path_dirname(&parent_path, repo_path);
762 if (err)
763 goto done;
764 free(repo_path);
765 repo_path = parent_path;
768 err = read_gotconfig(repo);
769 if (err)
770 goto done;
772 err = read_gitconfig(repo, global_gitconfig_path);
773 if (err)
774 goto done;
775 if (repo->gitconfig_repository_format_version > 1) {
776 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
777 goto done;
779 if (repo->gitconfig_repository_format_version == 0 &&
780 repo->algo != GOT_HASH_SHA1) {
781 err = got_error(GOT_ERR_OBJECT_FORMAT);
782 goto done;
784 for (i = 0; i < repo->nextensions; i++) {
785 char *ext = repo->extensions[i];
786 int j, supported = 0;
787 for (j = 0; j < nitems(repo_extensions); j++) {
788 if (strcmp(ext, repo_extensions[j]) == 0) {
789 supported = 1;
790 break;
793 if (!supported) {
794 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
795 goto done;
799 err = got_repo_list_packidx(&repo->packidx_paths, repo);
800 done:
801 if (err)
802 got_repo_close(repo);
803 else
804 *repop = repo;
805 free(repo_path);
806 return err;
809 const struct got_error *
810 got_repo_close(struct got_repository *repo)
812 const struct got_error *err = NULL, *child_err;
813 struct got_packidx_bloom_filter *bf;
814 size_t i;
816 for (i = 0; i < repo->pack_cache_size; i++) {
817 if (repo->packidx_cache[i] == NULL)
818 break;
819 got_packidx_close(repo->packidx_cache[i]);
822 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
823 &repo->packidx_bloom_filters))) {
824 RB_REMOVE(got_packidx_bloom_filter_tree,
825 &repo->packidx_bloom_filters, bf);
826 bloom_free(bf->bloom);
827 free(bf->bloom);
828 free(bf);
831 for (i = 0; i < repo->pack_cache_size; i++)
832 if (repo->packs[i].path_packfile)
833 if (repo->packs[i].path_packfile)
834 got_pack_close(&repo->packs[i]);
836 free(repo->path);
837 free(repo->path_git_dir);
839 got_object_cache_close(&repo->objcache);
840 got_object_cache_close(&repo->treecache);
841 got_object_cache_close(&repo->commitcache);
842 got_object_cache_close(&repo->tagcache);
843 got_object_cache_close(&repo->rawcache);
845 for (i = 0; i < nitems(repo->privsep_children); i++) {
846 if (repo->privsep_children[i].imsg_fd == -1)
847 continue;
848 imsg_clear(repo->privsep_children[i].ibuf);
849 free(repo->privsep_children[i].ibuf);
850 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
851 child_err = got_privsep_wait_for_child(
852 repo->privsep_children[i].pid);
853 if (child_err && err == NULL)
854 err = child_err;
855 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
856 err == NULL)
857 err = got_error_from_errno("close");
860 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
861 err == NULL)
862 err = got_error_from_errno("close");
864 if (repo->gotconfig)
865 got_gotconfig_free(repo->gotconfig);
866 free(repo->gitconfig_author_name);
867 free(repo->gitconfig_author_email);
868 for (i = 0; i < repo->ngitconfig_remotes; i++)
869 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
870 free(repo->gitconfig_remotes);
871 for (i = 0; i < repo->nextensions; i++)
872 free(repo->extensions[i]);
873 free(repo->extensions);
875 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
876 free(repo);
878 return err;
881 void
882 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
884 int i;
886 free(repo->name);
887 repo->name = NULL;
888 free(repo->fetch_url);
889 repo->fetch_url = NULL;
890 free(repo->send_url);
891 repo->send_url = NULL;
892 for (i = 0; i < repo->nfetch_branches; i++)
893 free(repo->fetch_branches[i]);
894 free(repo->fetch_branches);
895 repo->fetch_branches = NULL;
896 repo->nfetch_branches = 0;
897 for (i = 0; i < repo->nsend_branches; i++)
898 free(repo->send_branches[i]);
899 free(repo->send_branches);
900 repo->send_branches = NULL;
901 repo->nsend_branches = 0;
904 const struct got_error *
905 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
906 const char *input_path)
908 const struct got_error *err = NULL;
909 const char *repo_abspath = NULL;
910 size_t repolen, len;
911 char *canonpath, *path = NULL;
913 *in_repo_path = NULL;
915 canonpath = strdup(input_path);
916 if (canonpath == NULL) {
917 err = got_error_from_errno("strdup");
918 goto done;
920 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
921 if (err)
922 goto done;
924 repo_abspath = got_repo_get_path(repo);
926 if (canonpath[0] == '\0') {
927 path = strdup(canonpath);
928 if (path == NULL) {
929 err = got_error_from_errno("strdup");
930 goto done;
932 } else {
933 path = realpath(canonpath, NULL);
934 if (path == NULL) {
935 if (errno != ENOENT) {
936 err = got_error_from_errno2("realpath",
937 canonpath);
938 goto done;
940 /*
941 * Path is not on disk.
942 * Assume it is already relative to repository root.
943 */
944 path = strdup(canonpath);
945 if (path == NULL) {
946 err = got_error_from_errno("strdup");
947 goto done;
951 repolen = strlen(repo_abspath);
952 len = strlen(path);
955 if (strcmp(path, repo_abspath) == 0) {
956 free(path);
957 path = strdup("");
958 if (path == NULL) {
959 err = got_error_from_errno("strdup");
960 goto done;
962 } else if (len > repolen &&
963 got_path_is_child(path, repo_abspath, repolen)) {
964 /* Matched an on-disk path inside repository. */
965 if (got_repo_is_bare(repo)) {
966 /*
967 * Matched an on-disk path inside repository
968 * database. Treat input as repository-relative.
969 */
970 free(path);
971 path = canonpath;
972 canonpath = NULL;
973 } else {
974 char *child;
975 /* Strip common prefix with repository path. */
976 err = got_path_skip_common_ancestor(&child,
977 repo_abspath, path);
978 if (err)
979 goto done;
980 free(path);
981 path = child;
983 } else {
984 /*
985 * Matched unrelated on-disk path.
986 * Treat input as repository-relative.
987 */
988 free(path);
989 path = canonpath;
990 canonpath = NULL;
994 /* Make in-repository path absolute */
995 if (path[0] != '/') {
996 char *abspath;
997 if (asprintf(&abspath, "/%s", path) == -1) {
998 err = got_error_from_errno("asprintf");
999 goto done;
1001 free(path);
1002 path = abspath;
1005 done:
1006 free(canonpath);
1007 if (err)
1008 free(path);
1009 else
1010 *in_repo_path = path;
1011 return err;
1014 static const struct got_error *
1015 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1016 const char *path_packidx)
1018 const struct got_error *err = NULL;
1019 size_t i;
1021 for (i = 0; i < repo->pack_cache_size; i++) {
1022 if (repo->packidx_cache[i] == NULL)
1023 break;
1024 if (strcmp(repo->packidx_cache[i]->path_packidx,
1025 path_packidx) == 0) {
1026 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1029 if (i == repo->pack_cache_size) {
1030 do {
1031 i--;
1032 } while (i > 0 && repo->pinned_packidx >= 0 &&
1033 i == repo->pinned_packidx);
1034 err = got_packidx_close(repo->packidx_cache[i]);
1035 if (err)
1036 return err;
1039 repo->packidx_cache[i] = packidx;
1041 return NULL;
1044 int
1045 got_repo_is_packidx_filename(const char *name, size_t len)
1047 if (len != GOT_PACKIDX_NAMELEN)
1048 return 0;
1050 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1051 return 0;
1053 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1054 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1055 return 0;
1057 return 1;
1060 static struct got_packidx_bloom_filter *
1061 get_packidx_bloom_filter(struct got_repository *repo,
1062 const char *path, size_t path_len)
1064 struct got_packidx_bloom_filter key;
1066 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1067 return NULL; /* XXX */
1068 key.path_len = path_len;
1070 return RB_FIND(got_packidx_bloom_filter_tree,
1071 &repo->packidx_bloom_filters, &key);
1074 int
1075 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1076 const char *path_packidx, struct got_object_id *id)
1078 struct got_packidx_bloom_filter *bf;
1080 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1081 if (bf)
1082 return bloom_check(bf->bloom, id->hash, sizeof(id->hash));
1084 /* No bloom filter means this pack index must be searched. */
1085 return 1;
1088 static const struct got_error *
1089 add_packidx_bloom_filter(struct got_repository *repo,
1090 struct got_packidx *packidx, const char *path_packidx)
1092 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1093 struct got_packidx_bloom_filter *bf;
1094 size_t len;
1097 * Don't use bloom filters for very large pack index files.
1098 * Large pack files will contain a relatively large fraction
1099 * of our objects so we will likely need to visit them anyway.
1100 * The more objects a pack file contains the higher the probability
1101 * of a false-positive match from the bloom filter. And reading
1102 * all object IDs from a large pack index file can be expensive.
1104 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1105 return NULL;
1107 /* Do we already have a filter for this pack index? */
1108 if (get_packidx_bloom_filter(repo, path_packidx,
1109 strlen(path_packidx)) != NULL)
1110 return NULL;
1112 bf = calloc(1, sizeof(*bf));
1113 if (bf == NULL)
1114 return got_error_from_errno("calloc");
1115 bf->bloom = calloc(1, sizeof(*bf->bloom));
1116 if (bf->bloom == NULL) {
1117 free(bf);
1118 return got_error_from_errno("calloc");
1121 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1122 if (len >= sizeof(bf->path)) {
1123 free(bf->bloom);
1124 free(bf);
1125 return got_error(GOT_ERR_NO_SPACE);
1127 bf->path_len = len;
1129 /* Minimum size supported by our bloom filter is 1000 entries. */
1130 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1131 for (i = 0; i < nobjects; i++) {
1132 struct got_packidx_object_id *id;
1133 id = &packidx->hdr.sorted_ids[i];
1134 bloom_add(bf->bloom, id->hash, sizeof(id->hash));
1137 RB_INSERT(got_packidx_bloom_filter_tree,
1138 &repo->packidx_bloom_filters, bf);
1139 return NULL;
1142 static void
1143 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1145 struct got_pathlist_entry *pe;
1147 while (!TAILQ_EMPTY(packidx_paths)) {
1148 pe = TAILQ_FIRST(packidx_paths);
1149 TAILQ_REMOVE(packidx_paths, pe, entry);
1150 free((char *)pe->path);
1151 free(pe);
1155 static const struct got_error *
1156 refresh_packidx_paths(struct got_repository *repo)
1158 const struct got_error *err = NULL;
1159 char *objects_pack_dir = NULL;
1160 struct stat sb;
1162 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1163 if (objects_pack_dir == NULL)
1164 return got_error_from_errno("got_repo_get_path_objects_pack");
1166 if (stat(objects_pack_dir, &sb) == -1) {
1167 if (errno != ENOENT) {
1168 err = got_error_from_errno2("stat", objects_pack_dir);
1169 goto done;
1171 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1172 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1173 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1174 purge_packidx_paths(&repo->packidx_paths);
1175 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1176 if (err)
1177 goto done;
1179 done:
1180 free(objects_pack_dir);
1181 return err;
1184 const struct got_error *
1185 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1186 struct got_repository *repo, struct got_object_id *id)
1188 const struct got_error *err;
1189 struct got_pathlist_entry *pe;
1190 size_t i;
1192 /* Search pack index cache. */
1193 for (i = 0; i < repo->pack_cache_size; i++) {
1194 if (repo->packidx_cache[i] == NULL)
1195 break;
1196 if (!got_repo_check_packidx_bloom_filter(repo,
1197 repo->packidx_cache[i]->path_packidx, id))
1198 continue; /* object will not be found in this index */
1199 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1200 if (*idx != -1) {
1201 *packidx = repo->packidx_cache[i];
1203 * Move this cache entry to the front. Repeatedly
1204 * searching a wrong pack index can be expensive.
1206 if (i > 0) {
1207 memmove(&repo->packidx_cache[1],
1208 &repo->packidx_cache[0],
1209 i * sizeof(repo->packidx_cache[0]));
1210 repo->packidx_cache[0] = *packidx;
1211 if (repo->pinned_packidx >= 0 &&
1212 repo->pinned_packidx < i)
1213 repo->pinned_packidx++;
1214 else if (repo->pinned_packidx == i)
1215 repo->pinned_packidx = 0;
1217 return NULL;
1220 /* No luck. Search the filesystem. */
1222 err = refresh_packidx_paths(repo);
1223 if (err)
1224 return err;
1226 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1227 const char *path_packidx = pe->path;
1228 int is_cached = 0;
1230 if (!got_repo_check_packidx_bloom_filter(repo,
1231 pe->path, id))
1232 continue; /* object will not be found in this index */
1234 for (i = 0; i < repo->pack_cache_size; i++) {
1235 if (repo->packidx_cache[i] == NULL)
1236 break;
1237 if (strcmp(repo->packidx_cache[i]->path_packidx,
1238 path_packidx) == 0) {
1239 is_cached = 1;
1240 break;
1243 if (is_cached)
1244 continue; /* already searched */
1246 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1247 path_packidx, 0);
1248 if (err)
1249 goto done;
1251 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1252 if (err)
1253 goto done;
1255 err = cache_packidx(repo, *packidx, path_packidx);
1256 if (err)
1257 goto done;
1259 *idx = got_packidx_get_object_idx(*packidx, id);
1260 if (*idx != -1) {
1261 err = NULL; /* found the object */
1262 goto done;
1266 err = got_error_no_obj(id);
1267 done:
1268 return err;
1271 const struct got_error *
1272 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1273 struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 DIR *packdir = NULL;
1277 struct dirent *dent;
1278 char *path_packidx = NULL;
1279 int packdir_fd;
1280 struct stat sb;
1282 packdir_fd = openat(got_repo_get_fd(repo),
1283 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1284 if (packdir_fd == -1) {
1285 return got_error_from_errno_fmt("openat: %s/%s",
1286 got_repo_get_path_git_dir(repo),
1287 GOT_OBJECTS_PACK_DIR);
1290 packdir = fdopendir(packdir_fd);
1291 if (packdir == NULL) {
1292 err = got_error_from_errno("fdopendir");
1293 goto done;
1296 if (fstat(packdir_fd, &sb) == -1) {
1297 err = got_error_from_errno("fstat");
1298 goto done;
1300 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1301 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1303 while ((dent = readdir(packdir)) != NULL) {
1304 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1305 continue;
1307 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1308 dent->d_name) == -1) {
1309 err = got_error_from_errno("asprintf");
1310 path_packidx = NULL;
1311 break;
1314 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1315 if (err)
1316 break;
1318 done:
1319 if (err)
1320 free(path_packidx);
1321 if (packdir && closedir(packdir) != 0 && err == NULL)
1322 err = got_error_from_errno("closedir");
1323 return err;
1326 const struct got_error *
1327 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1328 struct got_repository *repo)
1330 const struct got_error *err;
1331 size_t i;
1333 *packidx = NULL;
1335 /* Search pack index cache. */
1336 for (i = 0; i < repo->pack_cache_size; i++) {
1337 if (repo->packidx_cache[i] == NULL)
1338 break;
1339 if (strcmp(repo->packidx_cache[i]->path_packidx,
1340 path_packidx) == 0) {
1341 *packidx = repo->packidx_cache[i];
1342 return NULL;
1345 /* No luck. Search the filesystem. */
1347 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1348 path_packidx, 0);
1349 if (err)
1350 return err;
1352 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1353 if (err)
1354 goto done;
1356 err = cache_packidx(repo, *packidx, path_packidx);
1357 done:
1358 if (err) {
1359 got_packidx_close(*packidx);
1360 *packidx = NULL;
1362 return err;
1365 static const struct got_error *
1366 read_packfile_hdr(int fd, struct got_packidx *packidx)
1368 const struct got_error *err = NULL;
1369 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1370 struct got_packfile_hdr hdr;
1371 ssize_t n;
1373 n = read(fd, &hdr, sizeof(hdr));
1374 if (n < 0)
1375 return got_error_from_errno("read");
1376 if (n != sizeof(hdr))
1377 return got_error(GOT_ERR_BAD_PACKFILE);
1379 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1380 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1381 be32toh(hdr.nobjects) != totobj)
1382 err = got_error(GOT_ERR_BAD_PACKFILE);
1384 return err;
1387 static const struct got_error *
1388 open_packfile(int *fd, struct got_repository *repo,
1389 const char *relpath, struct got_packidx *packidx)
1391 const struct got_error *err = NULL;
1393 *fd = openat(got_repo_get_fd(repo), relpath,
1394 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1395 if (*fd == -1)
1396 return got_error_from_errno_fmt("openat: %s/%s",
1397 got_repo_get_path_git_dir(repo), relpath);
1399 if (packidx) {
1400 err = read_packfile_hdr(*fd, packidx);
1401 if (err) {
1402 close(*fd);
1403 *fd = -1;
1407 return err;
1410 const struct got_error *
1411 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1412 const char *path_packfile, struct got_packidx *packidx)
1414 const struct got_error *err = NULL;
1415 struct got_pack *pack = NULL;
1416 struct stat sb;
1417 size_t i;
1419 if (packp)
1420 *packp = NULL;
1422 for (i = 0; i < repo->pack_cache_size; i++) {
1423 pack = &repo->packs[i];
1424 if (pack->path_packfile == NULL)
1425 break;
1426 if (strcmp(pack->path_packfile, path_packfile) == 0)
1427 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1430 if (i == repo->pack_cache_size) {
1431 struct got_pack tmp;
1432 do {
1433 i--;
1434 } while (i > 0 && repo->pinned_pack >= 0 &&
1435 i == repo->pinned_pack);
1436 err = got_pack_close(&repo->packs[i]);
1437 if (err)
1438 return err;
1439 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1440 return got_error_from_errno("ftruncate");
1441 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1442 return got_error_from_errno("ftruncate");
1443 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1444 memcpy(&repo->packs[i], &repo->packs[0],
1445 sizeof(repo->packs[i]));
1446 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1447 if (repo->pinned_pack == 0)
1448 repo->pinned_pack = i;
1449 else if (repo->pinned_pack == i)
1450 repo->pinned_pack = 0;
1451 i = 0;
1454 pack = &repo->packs[i];
1456 pack->path_packfile = strdup(path_packfile);
1457 if (pack->path_packfile == NULL) {
1458 err = got_error_from_errno("strdup");
1459 goto done;
1462 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1463 if (err)
1464 goto done;
1466 if (fstat(pack->fd, &sb) != 0) {
1467 err = got_error_from_errno("fstat");
1468 goto done;
1470 pack->filesize = sb.st_size;
1472 pack->privsep_child = NULL;
1474 err = got_delta_cache_alloc(&pack->delta_cache);
1475 if (err)
1476 goto done;
1478 #ifndef GOT_PACK_NO_MMAP
1479 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1480 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1481 pack->fd, 0);
1482 if (pack->map == MAP_FAILED) {
1483 if (errno != ENOMEM) {
1484 err = got_error_from_errno("mmap");
1485 goto done;
1487 pack->map = NULL; /* fall back to read(2) */
1490 #endif
1491 done:
1492 if (err) {
1493 if (pack)
1494 got_pack_close(pack);
1495 } else if (packp)
1496 *packp = pack;
1497 return err;
1500 struct got_pack *
1501 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1503 struct got_pack *pack = NULL;
1504 size_t i;
1506 for (i = 0; i < repo->pack_cache_size; i++) {
1507 pack = &repo->packs[i];
1508 if (pack->path_packfile == NULL)
1509 break;
1510 if (strcmp(pack->path_packfile, path_packfile) == 0)
1511 return pack;
1514 return NULL;
1517 const struct got_error *
1518 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1519 struct got_pack *pack)
1521 size_t i;
1522 int pinned_pack = -1, pinned_packidx = -1;
1524 for (i = 0; i < repo->pack_cache_size; i++) {
1525 if (repo->packidx_cache[i] &&
1526 strcmp(repo->packidx_cache[i]->path_packidx,
1527 packidx->path_packidx) == 0)
1528 pinned_packidx = i;
1529 if (repo->packs[i].path_packfile &&
1530 strcmp(repo->packs[i].path_packfile,
1531 pack->path_packfile) == 0)
1532 pinned_pack = i;
1535 if (pinned_packidx == -1 || pinned_pack == -1)
1536 return got_error(GOT_ERR_PIN_PACK);
1538 repo->pinned_pack = pinned_pack;
1539 repo->pinned_packidx = pinned_packidx;
1540 if (repo->packs[pinned_pack].privsep_child)
1541 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1542 return NULL;
1545 struct got_pack *
1546 got_repo_get_pinned_pack(struct got_repository *repo)
1548 if (repo->pinned_pack >= 0 &&
1549 repo->pinned_pack < repo->pack_cache_size)
1550 return &repo->packs[repo->pinned_pack];
1552 return NULL;
1555 void
1556 got_repo_unpin_pack(struct got_repository *repo)
1558 repo->pinned_packidx = -1;
1559 repo->pinned_pack = -1;
1560 repo->pinned_pid = 0;
1563 const struct got_error *
1564 got_repo_init(const char *repo_path, const char *head_name)
1566 const struct got_error *err = NULL;
1567 const char *dirnames[] = {
1568 GOT_OBJECTS_DIR,
1569 GOT_OBJECTS_PACK_DIR,
1570 GOT_REFS_DIR,
1572 const char *description_str = "Unnamed repository; "
1573 "edit this file 'description' to name the repository.";
1574 const char *headref = "ref: refs/heads/";
1575 const char *gitconfig_str = "[core]\n"
1576 "\trepositoryformatversion = 0\n"
1577 "\tfilemode = true\n"
1578 "\tbare = true\n";
1579 char *headref_str, *path;
1580 size_t i;
1582 if (!got_path_dir_is_empty(repo_path))
1583 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1585 for (i = 0; i < nitems(dirnames); i++) {
1586 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1587 return got_error_from_errno("asprintf");
1589 err = got_path_mkdir(path);
1590 free(path);
1591 if (err)
1592 return err;
1595 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1596 return got_error_from_errno("asprintf");
1597 err = got_path_create_file(path, description_str);
1598 free(path);
1599 if (err)
1600 return err;
1602 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1603 return got_error_from_errno("asprintf");
1604 if (asprintf(&headref_str, "%s%s", headref,
1605 head_name ? head_name : "main") == -1) {
1606 free(path);
1607 return got_error_from_errno("asprintf");
1609 err = got_path_create_file(path, headref_str);
1610 free(headref_str);
1611 free(path);
1612 if (err)
1613 return err;
1615 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1616 return got_error_from_errno("asprintf");
1617 err = got_path_create_file(path, gitconfig_str);
1618 free(path);
1619 if (err)
1620 return err;
1622 return NULL;
1625 static const struct got_error *
1626 match_packed_object(struct got_object_id **unique_id,
1627 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1629 const struct got_error *err = NULL;
1630 struct got_object_id_queue matched_ids;
1631 struct got_pathlist_entry *pe;
1633 STAILQ_INIT(&matched_ids);
1635 err = refresh_packidx_paths(repo);
1636 if (err)
1637 return err;
1639 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1640 const char *path_packidx = pe->path;
1641 struct got_packidx *packidx;
1642 struct got_object_qid *qid;
1644 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1645 path_packidx, 0);
1646 if (err)
1647 break;
1649 err = got_packidx_match_id_str_prefix(&matched_ids,
1650 packidx, id_str_prefix);
1651 if (err) {
1652 got_packidx_close(packidx);
1653 break;
1655 err = got_packidx_close(packidx);
1656 if (err)
1657 break;
1659 STAILQ_FOREACH(qid, &matched_ids, entry) {
1660 if (obj_type != GOT_OBJ_TYPE_ANY) {
1661 int matched_type;
1662 err = got_object_get_type(&matched_type, repo,
1663 &qid->id);
1664 if (err)
1665 goto done;
1666 if (matched_type != obj_type)
1667 continue;
1669 if (*unique_id == NULL) {
1670 *unique_id = got_object_id_dup(&qid->id);
1671 if (*unique_id == NULL) {
1672 err = got_error_from_errno("malloc");
1673 goto done;
1675 } else {
1676 if (got_object_id_cmp(*unique_id,
1677 &qid->id) == 0)
1678 continue; /* packed multiple times */
1679 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1680 goto done;
1684 done:
1685 got_object_id_queue_free(&matched_ids);
1686 if (err) {
1687 free(*unique_id);
1688 *unique_id = NULL;
1690 return err;
1693 static const struct got_error *
1694 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1695 const char *object_dir, const char *id_str_prefix, int obj_type,
1696 struct got_repository *repo)
1698 const struct got_error *err = NULL;
1699 char *path, *id_str = NULL;
1700 DIR *dir = NULL;
1701 struct dirent *dent;
1702 struct got_object_id id;
1704 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1705 err = got_error_from_errno("asprintf");
1706 goto done;
1709 dir = opendir(path);
1710 if (dir == NULL) {
1711 if (errno == ENOENT) {
1712 err = NULL;
1713 goto done;
1715 err = got_error_from_errno2("opendir", path);
1716 goto done;
1718 while ((dent = readdir(dir)) != NULL) {
1719 int cmp;
1721 free(id_str);
1722 id_str = NULL;
1724 if (strcmp(dent->d_name, ".") == 0 ||
1725 strcmp(dent->d_name, "..") == 0)
1726 continue;
1728 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1729 err = got_error_from_errno("asprintf");
1730 goto done;
1733 if (!got_parse_sha1_digest(id.hash, id_str))
1734 continue;
1737 * Directory entries do not necessarily appear in
1738 * sorted order, so we must iterate over all of them.
1740 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1741 if (cmp != 0)
1742 continue;
1744 if (*unique_id == NULL) {
1745 if (obj_type != GOT_OBJ_TYPE_ANY) {
1746 int matched_type;
1747 err = got_object_get_type(&matched_type, repo,
1748 &id);
1749 if (err)
1750 goto done;
1751 if (matched_type != obj_type)
1752 continue;
1754 *unique_id = got_object_id_dup(&id);
1755 if (*unique_id == NULL) {
1756 err = got_error_from_errno("got_object_id_dup");
1757 goto done;
1759 } else {
1760 if (got_object_id_cmp(*unique_id, &id) == 0)
1761 continue; /* both packed and loose */
1762 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1763 goto done;
1766 done:
1767 if (dir && closedir(dir) != 0 && err == NULL)
1768 err = got_error_from_errno("closedir");
1769 if (err) {
1770 free(*unique_id);
1771 *unique_id = NULL;
1773 free(id_str);
1774 free(path);
1775 return err;
1778 const struct got_error *
1779 got_repo_match_object_id_prefix(struct got_object_id **id,
1780 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1782 const struct got_error *err = NULL;
1783 char *path_objects = NULL, *object_dir = NULL;
1784 size_t len;
1785 int i;
1787 *id = NULL;
1789 path_objects = got_repo_get_path_objects(repo);
1791 len = strlen(id_str_prefix);
1792 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1793 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1794 goto done;
1797 for (i = 0; i < len; i++) {
1798 if (isxdigit((unsigned char)id_str_prefix[i]))
1799 continue;
1800 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1801 goto done;
1804 if (len >= 2) {
1805 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1806 if (err)
1807 goto done;
1808 object_dir = strndup(id_str_prefix, 2);
1809 if (object_dir == NULL) {
1810 err = got_error_from_errno("strdup");
1811 goto done;
1813 err = match_loose_object(id, path_objects, object_dir,
1814 id_str_prefix, obj_type, repo);
1815 } else if (len == 1) {
1816 int i;
1817 for (i = 0; i < 0xf; i++) {
1818 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1819 == -1) {
1820 err = got_error_from_errno("asprintf");
1821 goto done;
1823 err = match_packed_object(id, repo, object_dir,
1824 obj_type);
1825 if (err)
1826 goto done;
1827 err = match_loose_object(id, path_objects, object_dir,
1828 id_str_prefix, obj_type, repo);
1829 if (err)
1830 goto done;
1832 } else {
1833 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1834 goto done;
1836 done:
1837 free(path_objects);
1838 free(object_dir);
1839 if (err) {
1840 free(*id);
1841 *id = NULL;
1842 } else if (*id == NULL) {
1843 switch (obj_type) {
1844 case GOT_OBJ_TYPE_BLOB:
1845 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1846 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1847 break;
1848 case GOT_OBJ_TYPE_TREE:
1849 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1850 GOT_OBJ_LABEL_TREE, id_str_prefix);
1851 break;
1852 case GOT_OBJ_TYPE_COMMIT:
1853 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1854 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1855 break;
1856 case GOT_OBJ_TYPE_TAG:
1857 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1858 GOT_OBJ_LABEL_TAG, id_str_prefix);
1859 break;
1860 default:
1861 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1862 break;
1866 return err;
1869 const struct got_error *
1870 got_repo_match_object_id(struct got_object_id **id, char **label,
1871 const char *id_str, int obj_type, struct got_reflist_head *refs,
1872 struct got_repository *repo)
1874 const struct got_error *err;
1875 struct got_tag_object *tag;
1876 struct got_reference *ref = NULL;
1878 *id = NULL;
1879 if (label)
1880 *label = NULL;
1882 if (refs) {
1883 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1884 refs, repo);
1885 if (err == NULL) {
1886 *id = got_object_id_dup(
1887 got_object_tag_get_object_id(tag));
1888 if (*id == NULL)
1889 err = got_error_from_errno("got_object_id_dup");
1890 else if (label && asprintf(label, "refs/tags/%s",
1891 got_object_tag_get_name(tag)) == -1) {
1892 err = got_error_from_errno("asprintf");
1893 free(*id);
1894 *id = NULL;
1896 got_object_tag_close(tag);
1897 return err;
1898 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1899 err->code != GOT_ERR_NO_OBJ)
1900 return err;
1903 err = got_ref_open(&ref, repo, id_str, 0);
1904 if (err == NULL) {
1905 err = got_ref_resolve(id, repo, ref);
1906 if (err)
1907 goto done;
1908 if (label) {
1909 *label = strdup(got_ref_get_name(ref));
1910 if (*label == NULL) {
1911 err = got_error_from_errno("strdup");
1912 goto done;
1915 } else {
1916 if (err->code != GOT_ERR_NOT_REF &&
1917 err->code != GOT_ERR_BAD_REF_NAME)
1918 goto done;
1919 err = got_repo_match_object_id_prefix(id, id_str,
1920 obj_type, repo);
1921 if (err) {
1922 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1923 err = got_error_not_ref(id_str);
1924 goto done;
1926 if (label) {
1927 err = got_object_id_str(label, *id);
1928 if (*label == NULL) {
1929 err = got_error_from_errno("strdup");
1930 goto done;
1934 done:
1935 if (ref)
1936 got_ref_close(ref);
1937 return err;
1940 const struct got_error *
1941 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1942 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1944 const struct got_error *err = NULL;
1945 struct got_reflist_entry *re;
1946 struct got_object_id *tag_id;
1947 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1949 *tag = NULL;
1951 TAILQ_FOREACH(re, refs, entry) {
1952 const char *refname;
1953 refname = got_ref_get_name(re->ref);
1954 if (got_ref_is_symbolic(re->ref))
1955 continue;
1956 if (strncmp(refname, "refs/tags/", 10) != 0)
1957 continue;
1958 if (!name_is_absolute)
1959 refname += strlen("refs/tags/");
1960 if (strcmp(refname, name) != 0)
1961 continue;
1962 err = got_ref_resolve(&tag_id, repo, re->ref);
1963 if (err)
1964 break;
1965 err = got_object_open_as_tag(tag, repo, tag_id);
1966 free(tag_id);
1967 if (err)
1968 break;
1969 if (obj_type == GOT_OBJ_TYPE_ANY ||
1970 got_object_tag_get_object_type(*tag) == obj_type)
1971 break;
1972 got_object_tag_close(*tag);
1973 *tag = NULL;
1976 if (err == NULL && *tag == NULL)
1977 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1978 GOT_OBJ_LABEL_TAG, name);
1979 return err;
1982 static const struct got_error *
1983 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1984 const char *name, mode_t mode, struct got_object_id *blob_id)
1986 const struct got_error *err = NULL;
1988 *new_te = NULL;
1990 *new_te = calloc(1, sizeof(**new_te));
1991 if (*new_te == NULL)
1992 return got_error_from_errno("calloc");
1994 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1995 sizeof((*new_te)->name)) {
1996 err = got_error(GOT_ERR_NO_SPACE);
1997 goto done;
2000 if (S_ISLNK(mode)) {
2001 (*new_te)->mode = S_IFLNK;
2002 } else {
2003 (*new_te)->mode = S_IFREG;
2004 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2006 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2007 done:
2008 if (err && *new_te) {
2009 free(*new_te);
2010 *new_te = NULL;
2012 return err;
2015 static const struct got_error *
2016 import_file(struct got_tree_entry **new_te, struct dirent *de,
2017 const char *path, struct got_repository *repo)
2019 const struct got_error *err;
2020 struct got_object_id *blob_id = NULL;
2021 char *filepath;
2022 struct stat sb;
2024 if (asprintf(&filepath, "%s%s%s", path,
2025 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2026 return got_error_from_errno("asprintf");
2028 if (lstat(filepath, &sb) != 0) {
2029 err = got_error_from_errno2("lstat", path);
2030 goto done;
2033 err = got_object_blob_create(&blob_id, filepath, repo);
2034 if (err)
2035 goto done;
2037 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2038 blob_id);
2039 done:
2040 free(filepath);
2041 if (err)
2042 free(blob_id);
2043 return err;
2046 static const struct got_error *
2047 insert_tree_entry(struct got_tree_entry *new_te,
2048 struct got_pathlist_head *paths)
2050 const struct got_error *err = NULL;
2051 struct got_pathlist_entry *new_pe;
2053 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2054 if (err)
2055 return err;
2056 if (new_pe == NULL)
2057 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2058 return NULL;
2061 static const struct got_error *write_tree(struct got_object_id **,
2062 const char *, struct got_pathlist_head *, struct got_repository *,
2063 got_repo_import_cb progress_cb, void *progress_arg);
2065 static const struct got_error *
2066 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2067 const char *path, struct got_pathlist_head *ignores,
2068 struct got_repository *repo,
2069 got_repo_import_cb progress_cb, void *progress_arg)
2071 const struct got_error *err;
2072 struct got_object_id *id = NULL;
2073 char *subdirpath;
2075 if (asprintf(&subdirpath, "%s%s%s", path,
2076 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2077 return got_error_from_errno("asprintf");
2079 (*new_te) = calloc(1, sizeof(**new_te));
2080 if (*new_te == NULL)
2081 return got_error_from_errno("calloc");
2082 (*new_te)->mode = S_IFDIR;
2083 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2084 sizeof((*new_te)->name)) {
2085 err = got_error(GOT_ERR_NO_SPACE);
2086 goto done;
2088 err = write_tree(&id, subdirpath, ignores, repo,
2089 progress_cb, progress_arg);
2090 if (err)
2091 goto done;
2092 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2094 done:
2095 free(id);
2096 free(subdirpath);
2097 if (err) {
2098 free(*new_te);
2099 *new_te = NULL;
2101 return err;
2104 static const struct got_error *
2105 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2106 struct got_pathlist_head *ignores, struct got_repository *repo,
2107 got_repo_import_cb progress_cb, void *progress_arg)
2109 const struct got_error *err = NULL;
2110 DIR *dir;
2111 struct dirent *de;
2112 int nentries;
2113 struct got_tree_entry *new_te = NULL;
2114 struct got_pathlist_head paths;
2115 struct got_pathlist_entry *pe;
2117 *new_tree_id = NULL;
2119 TAILQ_INIT(&paths);
2121 dir = opendir(path_dir);
2122 if (dir == NULL) {
2123 err = got_error_from_errno2("opendir", path_dir);
2124 goto done;
2127 nentries = 0;
2128 while ((de = readdir(dir)) != NULL) {
2129 int ignore = 0;
2130 int type;
2132 if (strcmp(de->d_name, ".") == 0 ||
2133 strcmp(de->d_name, "..") == 0)
2134 continue;
2136 TAILQ_FOREACH(pe, ignores, entry) {
2137 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2138 ignore = 1;
2139 break;
2142 if (ignore)
2143 continue;
2145 err = got_path_dirent_type(&type, path_dir, de);
2146 if (err)
2147 goto done;
2149 if (type == DT_DIR) {
2150 err = import_subdir(&new_te, de, path_dir,
2151 ignores, repo, progress_cb, progress_arg);
2152 if (err) {
2153 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2154 goto done;
2155 err = NULL;
2156 continue;
2158 } else if (type == DT_REG || type == DT_LNK) {
2159 err = import_file(&new_te, de, path_dir, repo);
2160 if (err)
2161 goto done;
2162 } else
2163 continue;
2165 err = insert_tree_entry(new_te, &paths);
2166 if (err)
2167 goto done;
2168 nentries++;
2171 if (TAILQ_EMPTY(&paths)) {
2172 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2173 "cannot create tree without any entries");
2174 goto done;
2177 TAILQ_FOREACH(pe, &paths, entry) {
2178 struct got_tree_entry *te = pe->data;
2179 char *path;
2180 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2181 continue;
2182 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2183 err = got_error_from_errno("asprintf");
2184 goto done;
2186 err = (*progress_cb)(progress_arg, path);
2187 free(path);
2188 if (err)
2189 goto done;
2192 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2193 done:
2194 if (dir)
2195 closedir(dir);
2196 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2197 return err;
2200 const struct got_error *
2201 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2202 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2203 struct got_repository *repo, got_repo_import_cb progress_cb,
2204 void *progress_arg)
2206 const struct got_error *err;
2207 struct got_object_id *new_tree_id;
2209 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2210 progress_cb, progress_arg);
2211 if (err)
2212 return err;
2214 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2215 author, time(NULL), author, time(NULL), logmsg, repo);
2216 free(new_tree_id);
2217 return err;
2220 const struct got_error *
2221 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2222 struct got_repository *repo)
2224 const struct got_error *err = NULL;
2225 char *path_objects = NULL, *path = NULL;
2226 DIR *dir = NULL;
2227 struct got_object_id id;
2228 int i;
2230 *nobjects = 0;
2231 *ondisk_size = 0;
2233 path_objects = got_repo_get_path_objects(repo);
2234 if (path_objects == NULL)
2235 return got_error_from_errno("got_repo_get_path_objects");
2237 for (i = 0; i <= 0xff; i++) {
2238 struct dirent *dent;
2240 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2241 err = got_error_from_errno("asprintf");
2242 break;
2245 dir = opendir(path);
2246 if (dir == NULL) {
2247 if (errno == ENOENT) {
2248 err = NULL;
2249 continue;
2251 err = got_error_from_errno2("opendir", path);
2252 break;
2255 while ((dent = readdir(dir)) != NULL) {
2256 char *id_str;
2257 int fd;
2258 struct stat sb;
2260 if (strcmp(dent->d_name, ".") == 0 ||
2261 strcmp(dent->d_name, "..") == 0)
2262 continue;
2264 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2265 err = got_error_from_errno("asprintf");
2266 goto done;
2269 if (!got_parse_sha1_digest(id.hash, id_str)) {
2270 free(id_str);
2271 continue;
2273 free(id_str);
2275 err = got_object_open_loose_fd(&fd, &id, repo);
2276 if (err)
2277 goto done;
2279 if (fstat(fd, &sb) == -1) {
2280 err = got_error_from_errno("fstat");
2281 close(fd);
2282 goto done;
2284 (*nobjects)++;
2285 (*ondisk_size) += sb.st_size;
2287 if (close(fd) == -1) {
2288 err = got_error_from_errno("close");
2289 goto done;
2293 if (closedir(dir) != 0) {
2294 err = got_error_from_errno("closedir");
2295 goto done;
2297 dir = NULL;
2299 free(path);
2300 path = NULL;
2302 done:
2303 if (dir && closedir(dir) != 0 && err == NULL)
2304 err = got_error_from_errno("closedir");
2306 if (err) {
2307 *nobjects = 0;
2308 *ondisk_size = 0;
2310 free(path_objects);
2311 free(path);
2312 return err;
2315 const struct got_error *
2316 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2317 off_t *total_packsize, struct got_repository *repo)
2319 const struct got_error *err = NULL;
2320 DIR *packdir = NULL;
2321 struct dirent *dent;
2322 struct got_packidx *packidx = NULL;
2323 char *path_packidx;
2324 char *path_packfile;
2325 int packdir_fd;
2326 struct stat sb;
2328 *npackfiles = 0;
2329 *nobjects = 0;
2330 *total_packsize = 0;
2332 packdir_fd = openat(got_repo_get_fd(repo),
2333 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2334 if (packdir_fd == -1) {
2335 return got_error_from_errno_fmt("openat: %s/%s",
2336 got_repo_get_path_git_dir(repo),
2337 GOT_OBJECTS_PACK_DIR);
2340 packdir = fdopendir(packdir_fd);
2341 if (packdir == NULL) {
2342 err = got_error_from_errno("fdopendir");
2343 goto done;
2346 while ((dent = readdir(packdir)) != NULL) {
2347 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2348 continue;
2350 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2351 dent->d_name) == -1) {
2352 err = got_error_from_errno("asprintf");
2353 goto done;
2356 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2357 path_packidx, 0);
2358 free(path_packidx);
2359 if (err)
2360 goto done;
2362 if (fstat(packidx->fd, &sb) == -1)
2363 goto done;
2364 *total_packsize += sb.st_size;
2366 err = got_packidx_get_packfile_path(&path_packfile,
2367 packidx->path_packidx);
2368 if (err)
2369 goto done;
2371 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2372 0) == -1) {
2373 free(path_packfile);
2374 goto done;
2376 free(path_packfile);
2377 *total_packsize += sb.st_size;
2379 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2381 (*npackfiles)++;
2383 got_packidx_close(packidx);
2384 packidx = NULL;
2386 done:
2387 if (packidx)
2388 got_packidx_close(packidx);
2389 if (packdir && closedir(packdir) != 0 && err == NULL)
2390 err = got_error_from_errno("closedir");
2391 if (err) {
2392 *npackfiles = 0;
2393 *nobjects = 0;
2394 *total_packsize = 0;
2396 return err;
2399 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2400 got_packidx_bloom_filter_cmp);