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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 #endif
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
71 const char *
72 got_repo_get_path(struct got_repository *repo)
73 {
74 return repo->path;
75 }
77 const char *
78 got_repo_get_path_git_dir(struct got_repository *repo)
79 {
80 return repo->path_git_dir;
81 }
83 int
84 got_repo_get_fd(struct got_repository *repo)
85 {
86 return repo->gitdir_fd;
87 }
89 const char *
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
91 {
92 return repo->gitconfig_author_name;
93 }
95 const char *
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
97 {
98 return repo->gitconfig_author_email;
99 }
101 const char *
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
107 const char *
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
113 const char *
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
119 void
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
127 int
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
133 static char *
134 get_path_git_child(struct got_repository *repo, const char *basename)
136 char *path_child;
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
139 basename) == -1)
140 return NULL;
142 return path_child;
145 char *
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
151 char *
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
157 char *
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
163 char *
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
169 static char *
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
175 char *
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
181 char *
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
193 void
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
201 static int
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
208 int ret = 0;
209 struct stat sb;
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
213 goto done;
214 if (!S_ISDIR(sb.st_mode))
215 goto done;
217 if (lstat(path_objects, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_refs, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_head, &sb) == -1)
228 goto done;
229 if (!S_ISREG(sb.st_mode))
230 goto done;
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
234 goto done;
235 got_ref_close(head_ref);
237 ret = 1;
238 done:
239 free(path_objects);
240 free(path_refs);
241 free(path_head);
242 return ret;
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
250 int i;
252 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
253 if (*pack_fds == NULL)
254 return got_error_from_errno("calloc");
256 /*
257 * got_repo_pack_fds_close will try to close all of the
258 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
259 * a value from got_opentempfd(), which would result in a close(0) if
260 * we do not initialize to -1 here.
261 */
262 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
263 (*pack_fds)[i] = -1;
265 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
266 (*pack_fds)[i] = got_opentempfd();
267 if ((*pack_fds)[i] == -1) {
268 err = got_error_from_errno("got_opentempfd");
269 got_repo_pack_fds_close(*pack_fds);
270 *pack_fds = NULL;
271 return err;
275 return NULL;
278 const struct got_error *
279 got_repo_pack_fds_close(int *pack_fds)
281 const struct got_error *err = NULL;
282 int i;
284 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
285 if (pack_fds[i] == -1)
286 continue;
287 if (close(pack_fds[i]) == -1) {
288 err = got_error_from_errno("close");
289 break;
292 free(pack_fds);
293 return err;
296 const struct got_error *
297 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
298 struct got_object *obj)
300 #ifndef GOT_NO_OBJ_CACHE
301 const struct got_error *err = NULL;
302 err = got_object_cache_add(&repo->objcache, id, obj);
303 if (err) {
304 if (err->code == GOT_ERR_OBJ_EXISTS ||
305 err->code == GOT_ERR_OBJ_TOO_LARGE)
306 err = NULL;
307 return err;
309 obj->refcnt++;
310 #endif
311 return NULL;
314 struct got_object *
315 got_repo_get_cached_object(struct got_repository *repo,
316 struct got_object_id *id)
318 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
321 const struct got_error *
322 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
323 struct got_tree_object *tree)
325 #ifndef GOT_NO_OBJ_CACHE
326 const struct got_error *err = NULL;
327 err = got_object_cache_add(&repo->treecache, id, tree);
328 if (err) {
329 if (err->code == GOT_ERR_OBJ_EXISTS ||
330 err->code == GOT_ERR_OBJ_TOO_LARGE)
331 err = NULL;
332 return err;
334 tree->refcnt++;
335 #endif
336 return NULL;
339 struct got_tree_object *
340 got_repo_get_cached_tree(struct got_repository *repo,
341 struct got_object_id *id)
343 return (struct got_tree_object *)got_object_cache_get(
344 &repo->treecache, id);
347 const struct got_error *
348 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
349 struct got_commit_object *commit)
351 #ifndef GOT_NO_OBJ_CACHE
352 const struct got_error *err = NULL;
353 err = got_object_cache_add(&repo->commitcache, id, commit);
354 if (err) {
355 if (err->code == GOT_ERR_OBJ_EXISTS ||
356 err->code == GOT_ERR_OBJ_TOO_LARGE)
357 err = NULL;
358 return err;
360 commit->refcnt++;
361 #endif
362 return NULL;
365 struct got_commit_object *
366 got_repo_get_cached_commit(struct got_repository *repo,
367 struct got_object_id *id)
369 return (struct got_commit_object *)got_object_cache_get(
370 &repo->commitcache, id);
373 const struct got_error *
374 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
375 struct got_tag_object *tag)
377 #ifndef GOT_NO_OBJ_CACHE
378 const struct got_error *err = NULL;
379 err = got_object_cache_add(&repo->tagcache, id, tag);
380 if (err) {
381 if (err->code == GOT_ERR_OBJ_EXISTS ||
382 err->code == GOT_ERR_OBJ_TOO_LARGE)
383 err = NULL;
384 return err;
386 tag->refcnt++;
387 #endif
388 return NULL;
391 struct got_tag_object *
392 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
394 return (struct got_tag_object *)got_object_cache_get(
395 &repo->tagcache, id);
398 const struct got_error *
399 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
400 struct got_raw_object *raw)
402 #ifndef GOT_NO_OBJ_CACHE
403 const struct got_error *err = NULL;
404 err = got_object_cache_add(&repo->rawcache, id, raw);
405 if (err) {
406 if (err->code == GOT_ERR_OBJ_EXISTS ||
407 err->code == GOT_ERR_OBJ_TOO_LARGE)
408 err = NULL;
409 return err;
411 raw->refcnt++;
412 #endif
413 return NULL;
417 struct got_raw_object *
418 got_repo_get_cached_raw_object(struct got_repository *repo,
419 struct got_object_id *id)
421 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
425 static const struct got_error *
426 open_repo(struct got_repository *repo, const char *path)
428 const struct got_error *err = NULL;
430 repo->gitdir_fd = -1;
432 /* bare git repository? */
433 repo->path_git_dir = strdup(path);
434 if (repo->path_git_dir == NULL)
435 return got_error_from_errno("strdup");
436 if (is_git_repo(repo)) {
437 repo->path = strdup(repo->path_git_dir);
438 if (repo->path == NULL) {
439 err = got_error_from_errno("strdup");
440 goto done;
442 repo->gitdir_fd = open(repo->path_git_dir,
443 O_DIRECTORY | O_CLOEXEC);
444 if (repo->gitdir_fd == -1) {
445 err = got_error_from_errno2("open",
446 repo->path_git_dir);
447 goto done;
449 return NULL;
452 /* git repository with working tree? */
453 free(repo->path_git_dir);
454 repo->path_git_dir = NULL;
455 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
456 err = got_error_from_errno("asprintf");
457 goto done;
459 if (is_git_repo(repo)) {
460 repo->path = strdup(path);
461 if (repo->path == NULL) {
462 err = got_error_from_errno("strdup");
463 goto done;
465 repo->gitdir_fd = open(repo->path_git_dir,
466 O_DIRECTORY | O_CLOEXEC);
467 if (repo->gitdir_fd == -1) {
468 err = got_error_from_errno2("open",
469 repo->path_git_dir);
470 goto done;
472 return NULL;
475 err = got_error(GOT_ERR_NOT_GIT_REPO);
476 done:
477 if (err) {
478 free(repo->path);
479 repo->path = NULL;
480 free(repo->path_git_dir);
481 repo->path_git_dir = NULL;
482 if (repo->gitdir_fd != -1)
483 close(repo->gitdir_fd);
484 repo->gitdir_fd = -1;
487 return err;
490 static const struct got_error *
491 parse_gitconfig_file(int *gitconfig_repository_format_version,
492 char **gitconfig_author_name, char **gitconfig_author_email,
493 struct got_remote_repo **remotes, int *nremotes,
494 char **gitconfig_owner, char ***extensions, int *nextensions,
495 const char *gitconfig_path)
497 const struct got_error *err = NULL, *child_err = NULL;
498 int fd = -1;
499 int imsg_fds[2] = { -1, -1 };
500 pid_t pid;
501 struct imsgbuf *ibuf;
503 *gitconfig_repository_format_version = 0;
504 if (extensions)
505 *extensions = NULL;
506 if (nextensions)
507 *nextensions = 0;
508 *gitconfig_author_name = NULL;
509 *gitconfig_author_email = NULL;
510 if (remotes)
511 *remotes = NULL;
512 if (nremotes)
513 *nremotes = 0;
514 if (gitconfig_owner)
515 *gitconfig_owner = NULL;
517 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
518 if (fd == -1) {
519 if (errno == ENOENT)
520 return NULL;
521 return got_error_from_errno2("open", gitconfig_path);
524 ibuf = calloc(1, sizeof(*ibuf));
525 if (ibuf == NULL) {
526 err = got_error_from_errno("calloc");
527 goto done;
530 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
531 err = got_error_from_errno("socketpair");
532 goto done;
535 pid = fork();
536 if (pid == -1) {
537 err = got_error_from_errno("fork");
538 goto done;
539 } else if (pid == 0) {
540 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
541 gitconfig_path);
542 /* not reached */
545 if (close(imsg_fds[1]) == -1) {
546 err = got_error_from_errno("close");
547 goto done;
549 imsg_fds[1] = -1;
550 imsg_init(ibuf, imsg_fds[0]);
552 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
553 if (err)
554 goto done;
555 fd = -1;
557 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
558 if (err)
559 goto done;
561 err = got_privsep_recv_gitconfig_int(
562 gitconfig_repository_format_version, ibuf);
563 if (err)
564 goto done;
566 if (extensions && nextensions) {
567 err = got_privsep_send_gitconfig_repository_extensions_req(
568 ibuf);
569 if (err)
570 goto done;
571 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
572 if (err)
573 goto done;
574 if (*nextensions > 0) {
575 int i;
576 *extensions = calloc(*nextensions, sizeof(char *));
577 if (*extensions == NULL) {
578 err = got_error_from_errno("calloc");
579 goto done;
581 for (i = 0; i < *nextensions; i++) {
582 char *ext;
583 err = got_privsep_recv_gitconfig_str(&ext,
584 ibuf);
585 if (err)
586 goto done;
587 (*extensions)[i] = ext;
592 err = got_privsep_send_gitconfig_author_name_req(ibuf);
593 if (err)
594 goto done;
596 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
597 if (err)
598 goto done;
600 err = got_privsep_send_gitconfig_author_email_req(ibuf);
601 if (err)
602 goto done;
604 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
605 if (err)
606 goto done;
608 if (remotes && nremotes) {
609 err = got_privsep_send_gitconfig_remotes_req(ibuf);
610 if (err)
611 goto done;
613 err = got_privsep_recv_gitconfig_remotes(remotes,
614 nremotes, ibuf);
615 if (err)
616 goto done;
619 if (gitconfig_owner) {
620 err = got_privsep_send_gitconfig_owner_req(ibuf);
621 if (err)
622 goto done;
623 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
624 if (err)
625 goto done;
628 err = got_privsep_send_stop(imsg_fds[0]);
629 child_err = got_privsep_wait_for_child(pid);
630 if (child_err && err == NULL)
631 err = child_err;
632 done:
633 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
634 err = got_error_from_errno("close");
635 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
636 err = got_error_from_errno("close");
637 if (fd != -1 && close(fd) == -1 && err == NULL)
638 err = got_error_from_errno2("close", gitconfig_path);
639 free(ibuf);
640 return err;
643 static const struct got_error *
644 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
646 const struct got_error *err = NULL;
647 char *repo_gitconfig_path = NULL;
649 if (global_gitconfig_path) {
650 /* Read settings from ~/.gitconfig. */
651 int dummy_repo_version;
652 err = parse_gitconfig_file(&dummy_repo_version,
653 &repo->global_gitconfig_author_name,
654 &repo->global_gitconfig_author_email,
655 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
656 if (err)
657 return err;
660 /* Read repository's .git/config file. */
661 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
662 if (repo_gitconfig_path == NULL)
663 return got_error_from_errno("got_repo_get_path_gitconfig");
665 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
666 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
667 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
668 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
669 repo_gitconfig_path);
670 if (err)
671 goto done;
673 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
674 int i;
676 for (i = 0; i < repo->ngitconfig_remotes; i++) {
677 got_repo_free_remote_repo_data(
678 &repo->gitconfig_remotes[i]);
680 free(repo->gitconfig_remotes);
681 repo->gitconfig_remotes = NULL;
682 repo->ngitconfig_remotes = 0;
684 free(repo->gitconfig_author_name);
685 repo->gitconfig_author_name = NULL;
686 free(repo->gitconfig_author_email);
687 repo->gitconfig_author_email = NULL;
689 free(repo->global_gitconfig_author_name);
690 repo->global_gitconfig_author_name = NULL;
691 free(repo->global_gitconfig_author_email);
692 repo->global_gitconfig_author_email = NULL;
695 done:
696 free(repo_gitconfig_path);
697 return err;
700 static const struct got_error *
701 read_gotconfig(struct got_repository *repo)
703 const struct got_error *err = NULL;
704 char *gotconfig_path;
706 gotconfig_path = got_repo_get_path_gotconfig(repo);
707 if (gotconfig_path == NULL)
708 return got_error_from_errno("got_repo_get_path_gotconfig");
710 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
711 free(gotconfig_path);
712 return err;
715 /* Supported repository format extensions. */
716 static const char *const repo_extensions[] = {
717 "noop", /* Got supports repository format version 1. */
718 "preciousObjects", /* Supported by gotadmin cleanup. */
719 "worktreeConfig", /* Got does not care about Git work trees. */
720 };
722 const struct got_error *
723 got_repo_open(struct got_repository **repop, const char *path,
724 const char *global_gitconfig_path, int *pack_fds)
726 struct got_repository *repo = NULL;
727 const struct got_error *err = NULL;
728 char *repo_path = NULL;
729 size_t i, j = 0;
730 struct rlimit rl;
732 *repop = NULL;
734 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
735 return got_error_from_errno("getrlimit");
737 repo = calloc(1, sizeof(*repo));
738 if (repo == NULL)
739 return got_error_from_errno("calloc");
741 RB_INIT(&repo->packidx_bloom_filters);
742 TAILQ_INIT(&repo->packidx_paths);
744 for (i = 0; i < nitems(repo->privsep_children); i++) {
745 memset(&repo->privsep_children[i], 0,
746 sizeof(repo->privsep_children[0]));
747 repo->privsep_children[i].imsg_fd = -1;
750 err = got_object_cache_init(&repo->objcache,
751 GOT_OBJECT_CACHE_TYPE_OBJ);
752 if (err)
753 goto done;
754 err = got_object_cache_init(&repo->treecache,
755 GOT_OBJECT_CACHE_TYPE_TREE);
756 if (err)
757 goto done;
758 err = got_object_cache_init(&repo->commitcache,
759 GOT_OBJECT_CACHE_TYPE_COMMIT);
760 if (err)
761 goto done;
762 err = got_object_cache_init(&repo->tagcache,
763 GOT_OBJECT_CACHE_TYPE_TAG);
764 if (err)
765 goto done;
766 err = got_object_cache_init(&repo->rawcache,
767 GOT_OBJECT_CACHE_TYPE_RAW);
768 if (err)
769 goto done;
771 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
772 if (repo->pack_cache_size > rl.rlim_cur / 8)
773 repo->pack_cache_size = rl.rlim_cur / 8;
774 for (i = 0; i < nitems(repo->packs); i++) {
775 if (i < repo->pack_cache_size) {
776 repo->packs[i].basefd = pack_fds[j++];
777 repo->packs[i].accumfd = pack_fds[j++];
778 } else {
779 repo->packs[i].basefd = -1;
780 repo->packs[i].accumfd = -1;
783 repo->pinned_pack = -1;
784 repo->pinned_packidx = -1;
785 repo->pinned_pid = 0;
787 repo_path = realpath(path, NULL);
788 if (repo_path == NULL) {
789 err = got_error_from_errno2("realpath", path);
790 goto done;
793 for (;;) {
794 char *parent_path;
796 err = open_repo(repo, repo_path);
797 if (err == NULL)
798 break;
799 if (err->code != GOT_ERR_NOT_GIT_REPO)
800 goto done;
801 if (repo_path[0] == '/' && repo_path[1] == '\0') {
802 err = got_error(GOT_ERR_NOT_GIT_REPO);
803 goto done;
805 err = got_path_dirname(&parent_path, repo_path);
806 if (err)
807 goto done;
808 free(repo_path);
809 repo_path = parent_path;
812 err = read_gotconfig(repo);
813 if (err)
814 goto done;
816 err = read_gitconfig(repo, global_gitconfig_path);
817 if (err)
818 goto done;
819 if (repo->gitconfig_repository_format_version != 0) {
820 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
821 goto done;
823 for (i = 0; i < repo->nextensions; i++) {
824 char *ext = repo->extensions[i];
825 int j, supported = 0;
826 for (j = 0; j < nitems(repo_extensions); j++) {
827 if (strcmp(ext, repo_extensions[j]) == 0) {
828 supported = 1;
829 break;
832 if (!supported) {
833 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
834 goto done;
838 err = got_repo_list_packidx(&repo->packidx_paths, repo);
839 done:
840 if (err)
841 got_repo_close(repo);
842 else
843 *repop = repo;
844 free(repo_path);
845 return err;
848 const struct got_error *
849 got_repo_close(struct got_repository *repo)
851 const struct got_error *err = NULL, *child_err;
852 struct got_packidx_bloom_filter *bf;
853 struct got_pathlist_entry *pe;
854 size_t i;
856 for (i = 0; i < repo->pack_cache_size; i++) {
857 if (repo->packidx_cache[i] == NULL)
858 break;
859 got_packidx_close(repo->packidx_cache[i]);
862 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
863 &repo->packidx_bloom_filters))) {
864 RB_REMOVE(got_packidx_bloom_filter_tree,
865 &repo->packidx_bloom_filters, bf);
866 bloom_free(bf->bloom);
867 free(bf->bloom);
868 free(bf);
871 for (i = 0; i < repo->pack_cache_size; i++)
872 if (repo->packs[i].path_packfile)
873 if (repo->packs[i].path_packfile)
874 got_pack_close(&repo->packs[i]);
876 free(repo->path);
877 free(repo->path_git_dir);
879 got_object_cache_close(&repo->objcache);
880 got_object_cache_close(&repo->treecache);
881 got_object_cache_close(&repo->commitcache);
882 got_object_cache_close(&repo->tagcache);
883 got_object_cache_close(&repo->rawcache);
885 for (i = 0; i < nitems(repo->privsep_children); i++) {
886 if (repo->privsep_children[i].imsg_fd == -1)
887 continue;
888 imsg_clear(repo->privsep_children[i].ibuf);
889 free(repo->privsep_children[i].ibuf);
890 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
891 child_err = got_privsep_wait_for_child(
892 repo->privsep_children[i].pid);
893 if (child_err && err == NULL)
894 err = child_err;
895 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
896 err == NULL)
897 err = got_error_from_errno("close");
900 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
901 err == NULL)
902 err = got_error_from_errno("close");
904 if (repo->gotconfig)
905 got_gotconfig_free(repo->gotconfig);
906 free(repo->gitconfig_author_name);
907 free(repo->gitconfig_author_email);
908 for (i = 0; i < repo->ngitconfig_remotes; i++)
909 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
910 free(repo->gitconfig_remotes);
911 for (i = 0; i < repo->nextensions; i++)
912 free(repo->extensions[i]);
913 free(repo->extensions);
915 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
916 free((void *)pe->path);
917 got_pathlist_free(&repo->packidx_paths);
918 free(repo);
920 return err;
923 void
924 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
926 int i;
928 free(repo->name);
929 repo->name = NULL;
930 free(repo->fetch_url);
931 repo->fetch_url = NULL;
932 free(repo->send_url);
933 repo->send_url = NULL;
934 for (i = 0; i < repo->nfetch_branches; i++)
935 free(repo->fetch_branches[i]);
936 free(repo->fetch_branches);
937 repo->fetch_branches = NULL;
938 repo->nfetch_branches = 0;
939 for (i = 0; i < repo->nsend_branches; i++)
940 free(repo->send_branches[i]);
941 free(repo->send_branches);
942 repo->send_branches = NULL;
943 repo->nsend_branches = 0;
946 const struct got_error *
947 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
948 const char *input_path)
950 const struct got_error *err = NULL;
951 const char *repo_abspath = NULL;
952 size_t repolen, len;
953 char *canonpath, *path = NULL;
955 *in_repo_path = NULL;
957 canonpath = strdup(input_path);
958 if (canonpath == NULL) {
959 err = got_error_from_errno("strdup");
960 goto done;
962 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
963 if (err)
964 goto done;
966 repo_abspath = got_repo_get_path(repo);
968 if (canonpath[0] == '\0') {
969 path = strdup(canonpath);
970 if (path == NULL) {
971 err = got_error_from_errno("strdup");
972 goto done;
974 } else {
975 path = realpath(canonpath, NULL);
976 if (path == NULL) {
977 if (errno != ENOENT) {
978 err = got_error_from_errno2("realpath",
979 canonpath);
980 goto done;
982 /*
983 * Path is not on disk.
984 * Assume it is already relative to repository root.
985 */
986 path = strdup(canonpath);
987 if (path == NULL) {
988 err = got_error_from_errno("strdup");
989 goto done;
993 repolen = strlen(repo_abspath);
994 len = strlen(path);
997 if (strcmp(path, repo_abspath) == 0) {
998 free(path);
999 path = strdup("");
1000 if (path == NULL) {
1001 err = got_error_from_errno("strdup");
1002 goto done;
1004 } else if (len > repolen &&
1005 got_path_is_child(path, repo_abspath, repolen)) {
1006 /* Matched an on-disk path inside repository. */
1007 if (got_repo_is_bare(repo)) {
1009 * Matched an on-disk path inside repository
1010 * database. Treat input as repository-relative.
1012 free(path);
1013 path = canonpath;
1014 canonpath = NULL;
1015 } else {
1016 char *child;
1017 /* Strip common prefix with repository path. */
1018 err = got_path_skip_common_ancestor(&child,
1019 repo_abspath, path);
1020 if (err)
1021 goto done;
1022 free(path);
1023 path = child;
1025 } else {
1027 * Matched unrelated on-disk path.
1028 * Treat input as repository-relative.
1030 free(path);
1031 path = canonpath;
1032 canonpath = NULL;
1036 /* Make in-repository path absolute */
1037 if (path[0] != '/') {
1038 char *abspath;
1039 if (asprintf(&abspath, "/%s", path) == -1) {
1040 err = got_error_from_errno("asprintf");
1041 goto done;
1043 free(path);
1044 path = abspath;
1047 done:
1048 free(canonpath);
1049 if (err)
1050 free(path);
1051 else
1052 *in_repo_path = path;
1053 return err;
1056 static const struct got_error *
1057 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1058 const char *path_packidx)
1060 const struct got_error *err = NULL;
1061 size_t i;
1063 for (i = 0; i < repo->pack_cache_size; i++) {
1064 if (repo->packidx_cache[i] == NULL)
1065 break;
1066 if (strcmp(repo->packidx_cache[i]->path_packidx,
1067 path_packidx) == 0) {
1068 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1071 if (i == repo->pack_cache_size) {
1072 do {
1073 i--;
1074 } while (i > 0 && repo->pinned_packidx >= 0 &&
1075 i == repo->pinned_packidx);
1076 err = got_packidx_close(repo->packidx_cache[i]);
1077 if (err)
1078 return err;
1081 repo->packidx_cache[i] = packidx;
1083 return NULL;
1086 int
1087 got_repo_is_packidx_filename(const char *name, size_t len)
1089 if (len != GOT_PACKIDX_NAMELEN)
1090 return 0;
1092 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1093 return 0;
1095 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1096 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1097 return 0;
1099 return 1;
1102 static struct got_packidx_bloom_filter *
1103 get_packidx_bloom_filter(struct got_repository *repo,
1104 const char *path, size_t path_len)
1106 struct got_packidx_bloom_filter key;
1108 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1109 return NULL; /* XXX */
1110 key.path_len = path_len;
1112 return RB_FIND(got_packidx_bloom_filter_tree,
1113 &repo->packidx_bloom_filters, &key);
1116 int
1117 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1118 const char *path_packidx, struct got_object_id *id)
1120 struct got_packidx_bloom_filter *bf;
1122 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1123 if (bf)
1124 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1126 /* No bloom filter means this pack index must be searched. */
1127 return 1;
1130 static const struct got_error *
1131 add_packidx_bloom_filter(struct got_repository *repo,
1132 struct got_packidx *packidx, const char *path_packidx)
1134 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1135 struct got_packidx_bloom_filter *bf;
1136 size_t len;
1139 * Don't use bloom filters for very large pack index files.
1140 * Large pack files will contain a relatively large fraction
1141 * of our objects so we will likely need to visit them anyway.
1142 * The more objects a pack file contains the higher the probability
1143 * of a false-positive match from the bloom filter. And reading
1144 * all object IDs from a large pack index file can be expensive.
1146 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1147 return NULL;
1149 /* Do we already have a filter for this pack index? */
1150 if (get_packidx_bloom_filter(repo, path_packidx,
1151 strlen(path_packidx)) != NULL)
1152 return NULL;
1154 bf = calloc(1, sizeof(*bf));
1155 if (bf == NULL)
1156 return got_error_from_errno("calloc");
1157 bf->bloom = calloc(1, sizeof(*bf->bloom));
1158 if (bf->bloom == NULL) {
1159 free(bf);
1160 return got_error_from_errno("calloc");
1163 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1164 if (len >= sizeof(bf->path)) {
1165 free(bf->bloom);
1166 free(bf);
1167 return got_error(GOT_ERR_NO_SPACE);
1169 bf->path_len = len;
1171 /* Minimum size supported by our bloom filter is 1000 entries. */
1172 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1173 for (i = 0; i < nobjects; i++) {
1174 struct got_packidx_object_id *id;
1175 id = &packidx->hdr.sorted_ids[i];
1176 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1179 RB_INSERT(got_packidx_bloom_filter_tree,
1180 &repo->packidx_bloom_filters, bf);
1181 return NULL;
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 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1223 const char *path_packidx = pe->path;
1224 int is_cached = 0;
1226 if (!got_repo_check_packidx_bloom_filter(repo,
1227 pe->path, id))
1228 continue; /* object will not be found in this index */
1230 for (i = 0; i < repo->pack_cache_size; i++) {
1231 if (repo->packidx_cache[i] == NULL)
1232 break;
1233 if (strcmp(repo->packidx_cache[i]->path_packidx,
1234 path_packidx) == 0) {
1235 is_cached = 1;
1236 break;
1239 if (is_cached)
1240 continue; /* already searched */
1242 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1243 path_packidx, 0);
1244 if (err)
1245 goto done;
1247 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1248 if (err)
1249 goto done;
1251 err = cache_packidx(repo, *packidx, path_packidx);
1252 if (err)
1253 goto done;
1255 *idx = got_packidx_get_object_idx(*packidx, id);
1256 if (*idx != -1) {
1257 err = NULL; /* found the object */
1258 goto done;
1262 err = got_error_no_obj(id);
1263 done:
1264 return err;
1267 const struct got_error *
1268 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1269 struct got_repository *repo)
1271 const struct got_error *err = NULL;
1272 DIR *packdir = NULL;
1273 struct dirent *dent;
1274 char *path_packidx = NULL;
1275 int packdir_fd;
1276 struct stat sb;
1278 packdir_fd = openat(got_repo_get_fd(repo),
1279 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1280 if (packdir_fd == -1) {
1281 return got_error_from_errno_fmt("openat: %s/%s",
1282 got_repo_get_path_git_dir(repo),
1283 GOT_OBJECTS_PACK_DIR);
1286 packdir = fdopendir(packdir_fd);
1287 if (packdir == NULL) {
1288 err = got_error_from_errno("fdopendir");
1289 goto done;
1292 if (fstat(packdir_fd, &sb) == -1) {
1293 err = got_error_from_errno("fstat");
1294 goto done;
1296 repo->pack_path_mtime = sb.st_mtime;
1298 while ((dent = readdir(packdir)) != NULL) {
1299 if (!got_repo_is_packidx_filename(dent->d_name,
1300 strlen(dent->d_name)))
1301 continue;
1303 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1304 dent->d_name) == -1) {
1305 err = got_error_from_errno("asprintf");
1306 path_packidx = NULL;
1307 break;
1310 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1311 if (err)
1312 break;
1314 done:
1315 if (err)
1316 free(path_packidx);
1317 if (packdir && closedir(packdir) != 0 && err == NULL)
1318 err = got_error_from_errno("closedir");
1319 return err;
1322 const struct got_error *
1323 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1324 struct got_repository *repo)
1326 const struct got_error *err;
1327 size_t i;
1329 *packidx = NULL;
1331 /* Search pack index cache. */
1332 for (i = 0; i < repo->pack_cache_size; i++) {
1333 if (repo->packidx_cache[i] == NULL)
1334 break;
1335 if (strcmp(repo->packidx_cache[i]->path_packidx,
1336 path_packidx) == 0) {
1337 *packidx = repo->packidx_cache[i];
1338 return NULL;
1341 /* No luck. Search the filesystem. */
1343 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1344 path_packidx, 0);
1345 if (err)
1346 return err;
1348 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1349 if (err)
1350 goto done;
1352 err = cache_packidx(repo, *packidx, path_packidx);
1353 done:
1354 if (err) {
1355 got_packidx_close(*packidx);
1356 *packidx = NULL;
1358 return err;
1361 static const struct got_error *
1362 read_packfile_hdr(int fd, struct got_packidx *packidx)
1364 const struct got_error *err = NULL;
1365 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1366 struct got_packfile_hdr hdr;
1367 ssize_t n;
1369 n = read(fd, &hdr, sizeof(hdr));
1370 if (n < 0)
1371 return got_error_from_errno("read");
1372 if (n != sizeof(hdr))
1373 return got_error(GOT_ERR_BAD_PACKFILE);
1375 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1376 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1377 be32toh(hdr.nobjects) != totobj)
1378 err = got_error(GOT_ERR_BAD_PACKFILE);
1380 return err;
1383 static const struct got_error *
1384 open_packfile(int *fd, struct got_repository *repo,
1385 const char *relpath, struct got_packidx *packidx)
1387 const struct got_error *err = NULL;
1389 *fd = openat(got_repo_get_fd(repo), relpath,
1390 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1391 if (*fd == -1)
1392 return got_error_from_errno_fmt("openat: %s/%s",
1393 got_repo_get_path_git_dir(repo), relpath);
1395 if (packidx) {
1396 err = read_packfile_hdr(*fd, packidx);
1397 if (err) {
1398 close(*fd);
1399 *fd = -1;
1403 return err;
1406 const struct got_error *
1407 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1408 const char *path_packfile, struct got_packidx *packidx)
1410 const struct got_error *err = NULL;
1411 struct got_pack *pack = NULL;
1412 struct stat sb;
1413 size_t i;
1415 if (packp)
1416 *packp = NULL;
1418 for (i = 0; i < repo->pack_cache_size; i++) {
1419 pack = &repo->packs[i];
1420 if (pack->path_packfile == NULL)
1421 break;
1422 if (strcmp(pack->path_packfile, path_packfile) == 0)
1423 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1426 if (i == repo->pack_cache_size) {
1427 struct got_pack tmp;
1428 do {
1429 i--;
1430 } while (i > 0 && repo->pinned_pack >= 0 &&
1431 i == repo->pinned_pack);
1432 err = got_pack_close(&repo->packs[i]);
1433 if (err)
1434 return err;
1435 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1436 return got_error_from_errno("ftruncate");
1437 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1438 return got_error_from_errno("ftruncate");
1439 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1440 memcpy(&repo->packs[i], &repo->packs[0],
1441 sizeof(repo->packs[i]));
1442 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1443 if (repo->pinned_pack == 0)
1444 repo->pinned_pack = i;
1445 else if (repo->pinned_pack == i)
1446 repo->pinned_pack = 0;
1447 i = 0;
1450 pack = &repo->packs[i];
1452 pack->path_packfile = strdup(path_packfile);
1453 if (pack->path_packfile == NULL) {
1454 err = got_error_from_errno("strdup");
1455 goto done;
1458 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1459 if (err)
1460 goto done;
1462 if (fstat(pack->fd, &sb) != 0) {
1463 err = got_error_from_errno("fstat");
1464 goto done;
1466 pack->filesize = sb.st_size;
1468 pack->privsep_child = NULL;
1470 #ifndef GOT_PACK_NO_MMAP
1471 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1472 pack->fd, 0);
1473 if (pack->map == MAP_FAILED) {
1474 if (errno != ENOMEM) {
1475 err = got_error_from_errno("mmap");
1476 goto done;
1478 pack->map = NULL; /* fall back to read(2) */
1480 #endif
1481 done:
1482 if (err) {
1483 if (pack) {
1484 free(pack->path_packfile);
1485 memset(pack, 0, sizeof(*pack));
1487 } else if (packp)
1488 *packp = pack;
1489 return err;
1492 struct got_pack *
1493 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1495 struct got_pack *pack = NULL;
1496 size_t i;
1498 for (i = 0; i < repo->pack_cache_size; i++) {
1499 pack = &repo->packs[i];
1500 if (pack->path_packfile == NULL)
1501 break;
1502 if (strcmp(pack->path_packfile, path_packfile) == 0)
1503 return pack;
1506 return NULL;
1509 const struct got_error *
1510 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1511 struct got_pack *pack)
1513 size_t i;
1514 int pinned_pack = -1, pinned_packidx = -1;
1516 for (i = 0; i < repo->pack_cache_size; i++) {
1517 if (repo->packidx_cache[i] &&
1518 strcmp(repo->packidx_cache[i]->path_packidx,
1519 packidx->path_packidx) == 0)
1520 pinned_packidx = i;
1521 if (repo->packs[i].path_packfile &&
1522 strcmp(repo->packs[i].path_packfile,
1523 pack->path_packfile) == 0)
1524 pinned_pack = i;
1527 if (pinned_packidx == -1 || pinned_pack == -1)
1528 return got_error(GOT_ERR_PIN_PACK);
1530 repo->pinned_pack = pinned_pack;
1531 repo->pinned_packidx = pinned_packidx;
1532 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1533 return NULL;
1536 struct got_pack *
1537 got_repo_get_pinned_pack(struct got_repository *repo)
1539 if (repo->pinned_pack >= 0 &&
1540 repo->pinned_pack < repo->pack_cache_size)
1541 return &repo->packs[repo->pinned_pack];
1543 return NULL;
1546 void
1547 got_repo_unpin_pack(struct got_repository *repo)
1549 repo->pinned_packidx = -1;
1550 repo->pinned_pack = -1;
1551 repo->pinned_pid = 0;
1554 const struct got_error *
1555 got_repo_init(const char *repo_path)
1557 const struct got_error *err = NULL;
1558 const char *dirnames[] = {
1559 GOT_OBJECTS_DIR,
1560 GOT_OBJECTS_PACK_DIR,
1561 GOT_REFS_DIR,
1563 const char *description_str = "Unnamed repository; "
1564 "edit this file 'description' to name the repository.";
1565 const char *headref_str = "ref: refs/heads/main";
1566 const char *gitconfig_str = "[core]\n"
1567 "\trepositoryformatversion = 0\n"
1568 "\tfilemode = true\n"
1569 "\tbare = true\n";
1570 char *path;
1571 size_t i;
1573 if (!got_path_dir_is_empty(repo_path))
1574 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1576 for (i = 0; i < nitems(dirnames); i++) {
1577 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1578 return got_error_from_errno("asprintf");
1580 err = got_path_mkdir(path);
1581 free(path);
1582 if (err)
1583 return err;
1586 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1587 return got_error_from_errno("asprintf");
1588 err = got_path_create_file(path, description_str);
1589 free(path);
1590 if (err)
1591 return err;
1593 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1594 return got_error_from_errno("asprintf");
1595 err = got_path_create_file(path, headref_str);
1596 free(path);
1597 if (err)
1598 return err;
1600 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1601 return got_error_from_errno("asprintf");
1602 err = got_path_create_file(path, gitconfig_str);
1603 free(path);
1604 if (err)
1605 return err;
1607 return NULL;
1610 static void
1611 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1613 struct got_pathlist_entry *pe;
1615 while (!TAILQ_EMPTY(packidx_paths)) {
1616 pe = TAILQ_FIRST(packidx_paths);
1617 TAILQ_REMOVE(packidx_paths, pe, entry);
1618 free((char *)pe->path);
1619 free(pe);
1623 static const struct got_error *
1624 match_packed_object(struct got_object_id **unique_id,
1625 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1627 const struct got_error *err = NULL;
1628 struct got_object_id_queue matched_ids;
1629 struct got_pathlist_entry *pe;
1630 struct stat sb;
1632 STAILQ_INIT(&matched_ids);
1634 if (stat(got_repo_get_path_objects_pack(repo), &sb) == -1) {
1635 if (errno != ENOENT)
1636 return got_error_from_errno2("stat",
1637 got_repo_get_path_objects_pack(repo));
1638 } else if (sb.st_mtime != repo->pack_path_mtime) {
1639 purge_packidx_paths(&repo->packidx_paths);
1640 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1641 if (err)
1642 return err;
1645 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1646 const char *path_packidx = pe->path;
1647 struct got_packidx *packidx;
1648 struct got_object_qid *qid;
1650 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1651 path_packidx, 0);
1652 if (err)
1653 break;
1655 err = got_packidx_match_id_str_prefix(&matched_ids,
1656 packidx, id_str_prefix);
1657 if (err) {
1658 got_packidx_close(packidx);
1659 break;
1661 err = got_packidx_close(packidx);
1662 if (err)
1663 break;
1665 STAILQ_FOREACH(qid, &matched_ids, entry) {
1666 if (obj_type != GOT_OBJ_TYPE_ANY) {
1667 int matched_type;
1668 err = got_object_get_type(&matched_type, repo,
1669 &qid->id);
1670 if (err)
1671 goto done;
1672 if (matched_type != obj_type)
1673 continue;
1675 if (*unique_id == NULL) {
1676 *unique_id = got_object_id_dup(&qid->id);
1677 if (*unique_id == NULL) {
1678 err = got_error_from_errno("malloc");
1679 goto done;
1681 } else {
1682 if (got_object_id_cmp(*unique_id,
1683 &qid->id) == 0)
1684 continue; /* packed multiple times */
1685 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1686 goto done;
1690 done:
1691 got_object_id_queue_free(&matched_ids);
1692 if (err) {
1693 free(*unique_id);
1694 *unique_id = NULL;
1696 return err;
1699 static const struct got_error *
1700 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1701 const char *object_dir, const char *id_str_prefix, int obj_type,
1702 struct got_repository *repo)
1704 const struct got_error *err = NULL;
1705 char *path, *id_str = NULL;
1706 DIR *dir = NULL;
1707 struct dirent *dent;
1708 struct got_object_id id;
1710 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1711 err = got_error_from_errno("asprintf");
1712 goto done;
1715 dir = opendir(path);
1716 if (dir == NULL) {
1717 if (errno == ENOENT) {
1718 err = NULL;
1719 goto done;
1721 err = got_error_from_errno2("opendir", path);
1722 goto done;
1724 while ((dent = readdir(dir)) != NULL) {
1725 int cmp;
1727 free(id_str);
1728 id_str = NULL;
1730 if (strcmp(dent->d_name, ".") == 0 ||
1731 strcmp(dent->d_name, "..") == 0)
1732 continue;
1734 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1735 err = got_error_from_errno("asprintf");
1736 goto done;
1739 if (!got_parse_sha1_digest(id.sha1, id_str))
1740 continue;
1743 * Directory entries do not necessarily appear in
1744 * sorted order, so we must iterate over all of them.
1746 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1747 if (cmp != 0)
1748 continue;
1750 if (*unique_id == NULL) {
1751 if (obj_type != GOT_OBJ_TYPE_ANY) {
1752 int matched_type;
1753 err = got_object_get_type(&matched_type, repo,
1754 &id);
1755 if (err)
1756 goto done;
1757 if (matched_type != obj_type)
1758 continue;
1760 *unique_id = got_object_id_dup(&id);
1761 if (*unique_id == NULL) {
1762 err = got_error_from_errno("got_object_id_dup");
1763 goto done;
1765 } else {
1766 if (got_object_id_cmp(*unique_id, &id) == 0)
1767 continue; /* both packed and loose */
1768 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1769 goto done;
1772 done:
1773 if (dir && closedir(dir) != 0 && err == NULL)
1774 err = got_error_from_errno("closedir");
1775 if (err) {
1776 free(*unique_id);
1777 *unique_id = NULL;
1779 free(id_str);
1780 free(path);
1781 return err;
1784 const struct got_error *
1785 got_repo_match_object_id_prefix(struct got_object_id **id,
1786 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1788 const struct got_error *err = NULL;
1789 char *path_objects = got_repo_get_path_objects(repo);
1790 char *object_dir = NULL;
1791 size_t len;
1792 int i;
1794 *id = NULL;
1796 len = strlen(id_str_prefix);
1797 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1798 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1800 for (i = 0; i < len; i++) {
1801 if (isxdigit((unsigned char)id_str_prefix[i]))
1802 continue;
1803 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1806 if (len >= 2) {
1807 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1808 if (err)
1809 goto done;
1810 object_dir = strndup(id_str_prefix, 2);
1811 if (object_dir == NULL) {
1812 err = got_error_from_errno("strdup");
1813 goto done;
1815 err = match_loose_object(id, path_objects, object_dir,
1816 id_str_prefix, obj_type, repo);
1817 } else if (len == 1) {
1818 int i;
1819 for (i = 0; i < 0xf; i++) {
1820 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1821 == -1) {
1822 err = got_error_from_errno("asprintf");
1823 goto done;
1825 err = match_packed_object(id, repo, object_dir,
1826 obj_type);
1827 if (err)
1828 goto done;
1829 err = match_loose_object(id, path_objects, object_dir,
1830 id_str_prefix, obj_type, repo);
1831 if (err)
1832 goto done;
1834 } else {
1835 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1836 goto done;
1838 done:
1839 free(object_dir);
1840 if (err) {
1841 free(*id);
1842 *id = NULL;
1843 } else if (*id == NULL) {
1844 switch (obj_type) {
1845 case GOT_OBJ_TYPE_BLOB:
1846 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1847 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1848 break;
1849 case GOT_OBJ_TYPE_TREE:
1850 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1851 GOT_OBJ_LABEL_TREE, id_str_prefix);
1852 break;
1853 case GOT_OBJ_TYPE_COMMIT:
1854 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1855 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1856 break;
1857 case GOT_OBJ_TYPE_TAG:
1858 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1859 GOT_OBJ_LABEL_TAG, id_str_prefix);
1860 break;
1861 default:
1862 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1863 break;
1867 return err;
1870 const struct got_error *
1871 got_repo_match_object_id(struct got_object_id **id, char **label,
1872 const char *id_str, int obj_type, struct got_reflist_head *refs,
1873 struct got_repository *repo)
1875 const struct got_error *err;
1876 struct got_tag_object *tag;
1877 struct got_reference *ref = NULL;
1879 *id = NULL;
1880 if (label)
1881 *label = NULL;
1883 if (refs) {
1884 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1885 refs, repo);
1886 if (err == NULL) {
1887 *id = got_object_id_dup(
1888 got_object_tag_get_object_id(tag));
1889 if (*id == NULL)
1890 err = got_error_from_errno("got_object_id_dup");
1891 else if (label && asprintf(label, "refs/tags/%s",
1892 got_object_tag_get_name(tag)) == -1) {
1893 err = got_error_from_errno("asprintf");
1894 free(*id);
1895 *id = NULL;
1897 got_object_tag_close(tag);
1898 return err;
1899 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1900 err->code != GOT_ERR_NO_OBJ)
1901 return err;
1904 err = got_ref_open(&ref, repo, id_str, 0);
1905 if (err == NULL) {
1906 err = got_ref_resolve(id, repo, ref);
1907 if (err)
1908 goto done;
1909 if (label) {
1910 *label = strdup(got_ref_get_name(ref));
1911 if (*label == NULL) {
1912 err = got_error_from_errno("strdup");
1913 goto done;
1916 } else {
1917 if (err->code != GOT_ERR_NOT_REF &&
1918 err->code != GOT_ERR_BAD_REF_NAME)
1919 goto done;
1920 err = got_repo_match_object_id_prefix(id, id_str,
1921 obj_type, repo);
1922 if (err) {
1923 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1924 err = got_error_not_ref(id_str);
1925 goto done;
1927 if (label) {
1928 err = got_object_id_str(label, *id);
1929 if (*label == NULL) {
1930 err = got_error_from_errno("strdup");
1931 goto done;
1935 done:
1936 if (ref)
1937 got_ref_close(ref);
1938 return err;
1941 const struct got_error *
1942 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1943 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1945 const struct got_error *err = NULL;
1946 struct got_reflist_entry *re;
1947 struct got_object_id *tag_id;
1948 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1950 *tag = NULL;
1952 TAILQ_FOREACH(re, refs, entry) {
1953 const char *refname;
1954 refname = got_ref_get_name(re->ref);
1955 if (got_ref_is_symbolic(re->ref))
1956 continue;
1957 if (strncmp(refname, "refs/tags/", 10) != 0)
1958 continue;
1959 if (!name_is_absolute)
1960 refname += strlen("refs/tags/");
1961 if (strcmp(refname, name) != 0)
1962 continue;
1963 err = got_ref_resolve(&tag_id, repo, re->ref);
1964 if (err)
1965 break;
1966 err = got_object_open_as_tag(tag, repo, tag_id);
1967 free(tag_id);
1968 if (err)
1969 break;
1970 if (obj_type == GOT_OBJ_TYPE_ANY ||
1971 got_object_tag_get_object_type(*tag) == obj_type)
1972 break;
1973 got_object_tag_close(*tag);
1974 *tag = NULL;
1977 if (err == NULL && *tag == NULL)
1978 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1979 GOT_OBJ_LABEL_TAG, name);
1980 return err;
1983 static const struct got_error *
1984 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1985 const char *name, mode_t mode, struct got_object_id *blob_id)
1987 const struct got_error *err = NULL;
1989 *new_te = NULL;
1991 *new_te = calloc(1, sizeof(**new_te));
1992 if (*new_te == NULL)
1993 return got_error_from_errno("calloc");
1995 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1996 sizeof((*new_te)->name)) {
1997 err = got_error(GOT_ERR_NO_SPACE);
1998 goto done;
2001 if (S_ISLNK(mode)) {
2002 (*new_te)->mode = S_IFLNK;
2003 } else {
2004 (*new_te)->mode = S_IFREG;
2005 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2007 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2008 done:
2009 if (err && *new_te) {
2010 free(*new_te);
2011 *new_te = NULL;
2013 return err;
2016 static const struct got_error *
2017 import_file(struct got_tree_entry **new_te, struct dirent *de,
2018 const char *path, struct got_repository *repo)
2020 const struct got_error *err;
2021 struct got_object_id *blob_id = NULL;
2022 char *filepath;
2023 struct stat sb;
2025 if (asprintf(&filepath, "%s%s%s", path,
2026 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2027 return got_error_from_errno("asprintf");
2029 if (lstat(filepath, &sb) != 0) {
2030 err = got_error_from_errno2("lstat", path);
2031 goto done;
2034 err = got_object_blob_create(&blob_id, filepath, repo);
2035 if (err)
2036 goto done;
2038 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2039 blob_id);
2040 done:
2041 free(filepath);
2042 if (err)
2043 free(blob_id);
2044 return err;
2047 static const struct got_error *
2048 insert_tree_entry(struct got_tree_entry *new_te,
2049 struct got_pathlist_head *paths)
2051 const struct got_error *err = NULL;
2052 struct got_pathlist_entry *new_pe;
2054 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2055 if (err)
2056 return err;
2057 if (new_pe == NULL)
2058 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2059 return NULL;
2062 static const struct got_error *write_tree(struct got_object_id **,
2063 const char *, struct got_pathlist_head *, struct got_repository *,
2064 got_repo_import_cb progress_cb, void *progress_arg);
2066 static const struct got_error *
2067 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2068 const char *path, struct got_pathlist_head *ignores,
2069 struct got_repository *repo,
2070 got_repo_import_cb progress_cb, void *progress_arg)
2072 const struct got_error *err;
2073 struct got_object_id *id = NULL;
2074 char *subdirpath;
2076 if (asprintf(&subdirpath, "%s%s%s", path,
2077 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2078 return got_error_from_errno("asprintf");
2080 (*new_te) = calloc(1, sizeof(**new_te));
2081 if (*new_te == NULL)
2082 return got_error_from_errno("calloc");
2083 (*new_te)->mode = S_IFDIR;
2084 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2085 sizeof((*new_te)->name)) {
2086 err = got_error(GOT_ERR_NO_SPACE);
2087 goto done;
2089 err = write_tree(&id, subdirpath, ignores, repo,
2090 progress_cb, progress_arg);
2091 if (err)
2092 goto done;
2093 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2095 done:
2096 free(id);
2097 free(subdirpath);
2098 if (err) {
2099 free(*new_te);
2100 *new_te = NULL;
2102 return err;
2105 static const struct got_error *
2106 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2107 struct got_pathlist_head *ignores, struct got_repository *repo,
2108 got_repo_import_cb progress_cb, void *progress_arg)
2110 const struct got_error *err = NULL;
2111 DIR *dir;
2112 struct dirent *de;
2113 int nentries;
2114 struct got_tree_entry *new_te = NULL;
2115 struct got_pathlist_head paths;
2116 struct got_pathlist_entry *pe;
2118 *new_tree_id = NULL;
2120 TAILQ_INIT(&paths);
2122 dir = opendir(path_dir);
2123 if (dir == NULL) {
2124 err = got_error_from_errno2("opendir", path_dir);
2125 goto done;
2128 nentries = 0;
2129 while ((de = readdir(dir)) != NULL) {
2130 int ignore = 0;
2131 int type;
2133 if (strcmp(de->d_name, ".") == 0 ||
2134 strcmp(de->d_name, "..") == 0)
2135 continue;
2137 TAILQ_FOREACH(pe, ignores, entry) {
2138 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2139 ignore = 1;
2140 break;
2143 if (ignore)
2144 continue;
2146 err = got_path_dirent_type(&type, path_dir, de);
2147 if (err)
2148 goto done;
2150 if (type == DT_DIR) {
2151 err = import_subdir(&new_te, de, path_dir,
2152 ignores, repo, progress_cb, progress_arg);
2153 if (err) {
2154 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2155 goto done;
2156 err = NULL;
2157 continue;
2159 } else if (type == DT_REG || type == DT_LNK) {
2160 err = import_file(&new_te, de, path_dir, repo);
2161 if (err)
2162 goto done;
2163 } else
2164 continue;
2166 err = insert_tree_entry(new_te, &paths);
2167 if (err)
2168 goto done;
2169 nentries++;
2172 if (TAILQ_EMPTY(&paths)) {
2173 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2174 "cannot create tree without any entries");
2175 goto done;
2178 TAILQ_FOREACH(pe, &paths, entry) {
2179 struct got_tree_entry *te = pe->data;
2180 char *path;
2181 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2182 continue;
2183 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2184 err = got_error_from_errno("asprintf");
2185 goto done;
2187 err = (*progress_cb)(progress_arg, path);
2188 free(path);
2189 if (err)
2190 goto done;
2193 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2194 done:
2195 if (dir)
2196 closedir(dir);
2197 got_pathlist_free(&paths);
2198 return err;
2201 const struct got_error *
2202 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2203 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2204 struct got_repository *repo, got_repo_import_cb progress_cb,
2205 void *progress_arg)
2207 const struct got_error *err;
2208 struct got_object_id *new_tree_id;
2210 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2211 progress_cb, progress_arg);
2212 if (err)
2213 return err;
2215 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2216 author, time(NULL), author, time(NULL), logmsg, repo);
2217 free(new_tree_id);
2218 return err;
2221 const struct got_error *
2222 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2223 struct got_repository *repo)
2225 const struct got_error *err = NULL;
2226 char *path_objects = NULL, *path = NULL;
2227 DIR *dir = NULL;
2228 struct got_object_id id;
2229 int i;
2231 *nobjects = 0;
2232 *ondisk_size = 0;
2234 path_objects = got_repo_get_path_objects(repo);
2235 if (path_objects == NULL)
2236 return got_error_from_errno("got_repo_get_path_objects");
2238 for (i = 0; i <= 0xff; i++) {
2239 struct dirent *dent;
2241 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2242 err = got_error_from_errno("asprintf");
2243 break;
2246 dir = opendir(path);
2247 if (dir == NULL) {
2248 if (errno == ENOENT) {
2249 err = NULL;
2250 continue;
2252 err = got_error_from_errno2("opendir", path);
2253 break;
2256 while ((dent = readdir(dir)) != NULL) {
2257 char *id_str;
2258 int fd;
2259 struct stat sb;
2261 if (strcmp(dent->d_name, ".") == 0 ||
2262 strcmp(dent->d_name, "..") == 0)
2263 continue;
2265 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2266 err = got_error_from_errno("asprintf");
2267 goto done;
2270 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2271 free(id_str);
2272 continue;
2274 free(id_str);
2276 err = got_object_open_loose_fd(&fd, &id, repo);
2277 if (err)
2278 goto done;
2280 if (fstat(fd, &sb) == -1) {
2281 err = got_error_from_errno("fstat");
2282 close(fd);
2283 goto done;
2285 (*nobjects)++;
2286 (*ondisk_size) += sb.st_size;
2288 if (close(fd) == -1) {
2289 err = got_error_from_errno("close");
2290 goto done;
2294 if (closedir(dir) != 0) {
2295 err = got_error_from_errno("closedir");
2296 goto done;
2298 dir = NULL;
2300 free(path);
2301 path = NULL;
2303 done:
2304 if (dir && closedir(dir) != 0 && err == NULL)
2305 err = got_error_from_errno("closedir");
2307 if (err) {
2308 *nobjects = 0;
2309 *ondisk_size = 0;
2311 free(path_objects);
2312 free(path);
2313 return err;
2316 const struct got_error *
2317 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2318 off_t *total_packsize, struct got_repository *repo)
2320 const struct got_error *err = NULL;
2321 DIR *packdir = NULL;
2322 struct dirent *dent;
2323 struct got_packidx *packidx = NULL;
2324 char *path_packidx;
2325 char *path_packfile;
2326 int packdir_fd;
2327 struct stat sb;
2329 *npackfiles = 0;
2330 *nobjects = 0;
2331 *total_packsize = 0;
2333 packdir_fd = openat(got_repo_get_fd(repo),
2334 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2335 if (packdir_fd == -1) {
2336 return got_error_from_errno_fmt("openat: %s/%s",
2337 got_repo_get_path_git_dir(repo),
2338 GOT_OBJECTS_PACK_DIR);
2341 packdir = fdopendir(packdir_fd);
2342 if (packdir == NULL) {
2343 err = got_error_from_errno("fdopendir");
2344 goto done;
2347 while ((dent = readdir(packdir)) != NULL) {
2348 if (!got_repo_is_packidx_filename(dent->d_name,
2349 strlen(dent->d_name)))
2350 continue;
2352 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2353 dent->d_name) == -1) {
2354 err = got_error_from_errno("asprintf");
2355 goto done;
2358 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2359 path_packidx, 0);
2360 free(path_packidx);
2361 if (err)
2362 goto done;
2364 if (fstat(packidx->fd, &sb) == -1)
2365 goto done;
2366 *total_packsize += sb.st_size;
2368 err = got_packidx_get_packfile_path(&path_packfile,
2369 packidx->path_packidx);
2370 if (err)
2371 goto done;
2373 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2374 0) == -1) {
2375 free(path_packfile);
2376 goto done;
2378 free(path_packfile);
2379 *total_packsize += sb.st_size;
2381 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2383 (*npackfiles)++;
2385 got_packidx_close(packidx);
2386 packidx = NULL;
2388 done:
2389 if (packidx)
2390 got_packidx_close(packidx);
2391 if (packdir && closedir(packdir) != 0 && err == NULL)
2392 err = got_error_from_errno("closedir");
2393 if (err) {
2394 *npackfiles = 0;
2395 *nobjects = 0;
2396 *total_packsize = 0;
2398 return err;
2401 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2402 got_packidx_bloom_filter_cmp);