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, const char *head_name)
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 = "ref: refs/heads/";
1566 const char *gitconfig_str = "[core]\n"
1567 "\trepositoryformatversion = 0\n"
1568 "\tfilemode = true\n"
1569 "\tbare = true\n";
1570 char *headref_str, *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 if (asprintf(&headref_str, "%s%s", headref,
1596 head_name ? head_name : "main") == -1) {
1597 free(path);
1598 return got_error_from_errno("asprintf");
1600 err = got_path_create_file(path, headref_str);
1601 free(headref_str);
1602 free(path);
1603 if (err)
1604 return err;
1606 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1607 return got_error_from_errno("asprintf");
1608 err = got_path_create_file(path, gitconfig_str);
1609 free(path);
1610 if (err)
1611 return err;
1613 return NULL;
1616 static void
1617 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1619 struct got_pathlist_entry *pe;
1621 while (!TAILQ_EMPTY(packidx_paths)) {
1622 pe = TAILQ_FIRST(packidx_paths);
1623 TAILQ_REMOVE(packidx_paths, pe, entry);
1624 free((char *)pe->path);
1625 free(pe);
1629 static const struct got_error *
1630 match_packed_object(struct got_object_id **unique_id,
1631 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1633 const struct got_error *err = NULL;
1634 char *objects_pack_dir = NULL;
1635 struct got_object_id_queue matched_ids;
1636 struct got_pathlist_entry *pe;
1637 struct stat sb;
1639 STAILQ_INIT(&matched_ids);
1641 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1642 if (objects_pack_dir == NULL)
1643 return got_error_from_errno("got_repo_get_path_objects_pack");
1645 if (stat(objects_pack_dir, &sb) == -1) {
1646 if (errno != ENOENT) {
1647 err = got_error_from_errno2("stat", objects_pack_dir);
1648 goto done;
1650 } else if (sb.st_mtime != repo->pack_path_mtime) {
1651 purge_packidx_paths(&repo->packidx_paths);
1652 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1653 if (err)
1654 goto done;
1657 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1658 const char *path_packidx = pe->path;
1659 struct got_packidx *packidx;
1660 struct got_object_qid *qid;
1662 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1663 path_packidx, 0);
1664 if (err)
1665 break;
1667 err = got_packidx_match_id_str_prefix(&matched_ids,
1668 packidx, id_str_prefix);
1669 if (err) {
1670 got_packidx_close(packidx);
1671 break;
1673 err = got_packidx_close(packidx);
1674 if (err)
1675 break;
1677 STAILQ_FOREACH(qid, &matched_ids, entry) {
1678 if (obj_type != GOT_OBJ_TYPE_ANY) {
1679 int matched_type;
1680 err = got_object_get_type(&matched_type, repo,
1681 &qid->id);
1682 if (err)
1683 goto done;
1684 if (matched_type != obj_type)
1685 continue;
1687 if (*unique_id == NULL) {
1688 *unique_id = got_object_id_dup(&qid->id);
1689 if (*unique_id == NULL) {
1690 err = got_error_from_errno("malloc");
1691 goto done;
1693 } else {
1694 if (got_object_id_cmp(*unique_id,
1695 &qid->id) == 0)
1696 continue; /* packed multiple times */
1697 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1698 goto done;
1702 done:
1703 free(objects_pack_dir);
1704 got_object_id_queue_free(&matched_ids);
1705 if (err) {
1706 free(*unique_id);
1707 *unique_id = NULL;
1709 return err;
1712 static const struct got_error *
1713 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1714 const char *object_dir, const char *id_str_prefix, int obj_type,
1715 struct got_repository *repo)
1717 const struct got_error *err = NULL;
1718 char *path, *id_str = NULL;
1719 DIR *dir = NULL;
1720 struct dirent *dent;
1721 struct got_object_id id;
1723 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1724 err = got_error_from_errno("asprintf");
1725 goto done;
1728 dir = opendir(path);
1729 if (dir == NULL) {
1730 if (errno == ENOENT) {
1731 err = NULL;
1732 goto done;
1734 err = got_error_from_errno2("opendir", path);
1735 goto done;
1737 while ((dent = readdir(dir)) != NULL) {
1738 int cmp;
1740 free(id_str);
1741 id_str = NULL;
1743 if (strcmp(dent->d_name, ".") == 0 ||
1744 strcmp(dent->d_name, "..") == 0)
1745 continue;
1747 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1748 err = got_error_from_errno("asprintf");
1749 goto done;
1752 if (!got_parse_sha1_digest(id.sha1, id_str))
1753 continue;
1756 * Directory entries do not necessarily appear in
1757 * sorted order, so we must iterate over all of them.
1759 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1760 if (cmp != 0)
1761 continue;
1763 if (*unique_id == NULL) {
1764 if (obj_type != GOT_OBJ_TYPE_ANY) {
1765 int matched_type;
1766 err = got_object_get_type(&matched_type, repo,
1767 &id);
1768 if (err)
1769 goto done;
1770 if (matched_type != obj_type)
1771 continue;
1773 *unique_id = got_object_id_dup(&id);
1774 if (*unique_id == NULL) {
1775 err = got_error_from_errno("got_object_id_dup");
1776 goto done;
1778 } else {
1779 if (got_object_id_cmp(*unique_id, &id) == 0)
1780 continue; /* both packed and loose */
1781 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1782 goto done;
1785 done:
1786 if (dir && closedir(dir) != 0 && err == NULL)
1787 err = got_error_from_errno("closedir");
1788 if (err) {
1789 free(*unique_id);
1790 *unique_id = NULL;
1792 free(id_str);
1793 free(path);
1794 return err;
1797 const struct got_error *
1798 got_repo_match_object_id_prefix(struct got_object_id **id,
1799 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1801 const struct got_error *err = NULL;
1802 char *path_objects = NULL, *object_dir = NULL;
1803 size_t len;
1804 int i;
1806 *id = NULL;
1808 path_objects = got_repo_get_path_objects(repo);
1810 len = strlen(id_str_prefix);
1811 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1812 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1813 goto done;
1816 for (i = 0; i < len; i++) {
1817 if (isxdigit((unsigned char)id_str_prefix[i]))
1818 continue;
1819 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1820 goto done;
1823 if (len >= 2) {
1824 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1825 if (err)
1826 goto done;
1827 object_dir = strndup(id_str_prefix, 2);
1828 if (object_dir == NULL) {
1829 err = got_error_from_errno("strdup");
1830 goto done;
1832 err = match_loose_object(id, path_objects, object_dir,
1833 id_str_prefix, obj_type, repo);
1834 } else if (len == 1) {
1835 int i;
1836 for (i = 0; i < 0xf; i++) {
1837 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1838 == -1) {
1839 err = got_error_from_errno("asprintf");
1840 goto done;
1842 err = match_packed_object(id, repo, object_dir,
1843 obj_type);
1844 if (err)
1845 goto done;
1846 err = match_loose_object(id, path_objects, object_dir,
1847 id_str_prefix, obj_type, repo);
1848 if (err)
1849 goto done;
1851 } else {
1852 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1853 goto done;
1855 done:
1856 free(path_objects);
1857 free(object_dir);
1858 if (err) {
1859 free(*id);
1860 *id = NULL;
1861 } else if (*id == NULL) {
1862 switch (obj_type) {
1863 case GOT_OBJ_TYPE_BLOB:
1864 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1865 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1866 break;
1867 case GOT_OBJ_TYPE_TREE:
1868 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1869 GOT_OBJ_LABEL_TREE, id_str_prefix);
1870 break;
1871 case GOT_OBJ_TYPE_COMMIT:
1872 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1873 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1874 break;
1875 case GOT_OBJ_TYPE_TAG:
1876 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1877 GOT_OBJ_LABEL_TAG, id_str_prefix);
1878 break;
1879 default:
1880 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1881 break;
1885 return err;
1888 const struct got_error *
1889 got_repo_match_object_id(struct got_object_id **id, char **label,
1890 const char *id_str, int obj_type, struct got_reflist_head *refs,
1891 struct got_repository *repo)
1893 const struct got_error *err;
1894 struct got_tag_object *tag;
1895 struct got_reference *ref = NULL;
1897 *id = NULL;
1898 if (label)
1899 *label = NULL;
1901 if (refs) {
1902 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1903 refs, repo);
1904 if (err == NULL) {
1905 *id = got_object_id_dup(
1906 got_object_tag_get_object_id(tag));
1907 if (*id == NULL)
1908 err = got_error_from_errno("got_object_id_dup");
1909 else if (label && asprintf(label, "refs/tags/%s",
1910 got_object_tag_get_name(tag)) == -1) {
1911 err = got_error_from_errno("asprintf");
1912 free(*id);
1913 *id = NULL;
1915 got_object_tag_close(tag);
1916 return err;
1917 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1918 err->code != GOT_ERR_NO_OBJ)
1919 return err;
1922 err = got_ref_open(&ref, repo, id_str, 0);
1923 if (err == NULL) {
1924 err = got_ref_resolve(id, repo, ref);
1925 if (err)
1926 goto done;
1927 if (label) {
1928 *label = strdup(got_ref_get_name(ref));
1929 if (*label == NULL) {
1930 err = got_error_from_errno("strdup");
1931 goto done;
1934 } else {
1935 if (err->code != GOT_ERR_NOT_REF &&
1936 err->code != GOT_ERR_BAD_REF_NAME)
1937 goto done;
1938 err = got_repo_match_object_id_prefix(id, id_str,
1939 obj_type, repo);
1940 if (err) {
1941 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1942 err = got_error_not_ref(id_str);
1943 goto done;
1945 if (label) {
1946 err = got_object_id_str(label, *id);
1947 if (*label == NULL) {
1948 err = got_error_from_errno("strdup");
1949 goto done;
1953 done:
1954 if (ref)
1955 got_ref_close(ref);
1956 return err;
1959 const struct got_error *
1960 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1961 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1963 const struct got_error *err = NULL;
1964 struct got_reflist_entry *re;
1965 struct got_object_id *tag_id;
1966 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1968 *tag = NULL;
1970 TAILQ_FOREACH(re, refs, entry) {
1971 const char *refname;
1972 refname = got_ref_get_name(re->ref);
1973 if (got_ref_is_symbolic(re->ref))
1974 continue;
1975 if (strncmp(refname, "refs/tags/", 10) != 0)
1976 continue;
1977 if (!name_is_absolute)
1978 refname += strlen("refs/tags/");
1979 if (strcmp(refname, name) != 0)
1980 continue;
1981 err = got_ref_resolve(&tag_id, repo, re->ref);
1982 if (err)
1983 break;
1984 err = got_object_open_as_tag(tag, repo, tag_id);
1985 free(tag_id);
1986 if (err)
1987 break;
1988 if (obj_type == GOT_OBJ_TYPE_ANY ||
1989 got_object_tag_get_object_type(*tag) == obj_type)
1990 break;
1991 got_object_tag_close(*tag);
1992 *tag = NULL;
1995 if (err == NULL && *tag == NULL)
1996 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1997 GOT_OBJ_LABEL_TAG, name);
1998 return err;
2001 static const struct got_error *
2002 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2003 const char *name, mode_t mode, struct got_object_id *blob_id)
2005 const struct got_error *err = NULL;
2007 *new_te = NULL;
2009 *new_te = calloc(1, sizeof(**new_te));
2010 if (*new_te == NULL)
2011 return got_error_from_errno("calloc");
2013 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2014 sizeof((*new_te)->name)) {
2015 err = got_error(GOT_ERR_NO_SPACE);
2016 goto done;
2019 if (S_ISLNK(mode)) {
2020 (*new_te)->mode = S_IFLNK;
2021 } else {
2022 (*new_te)->mode = S_IFREG;
2023 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2025 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2026 done:
2027 if (err && *new_te) {
2028 free(*new_te);
2029 *new_te = NULL;
2031 return err;
2034 static const struct got_error *
2035 import_file(struct got_tree_entry **new_te, struct dirent *de,
2036 const char *path, struct got_repository *repo)
2038 const struct got_error *err;
2039 struct got_object_id *blob_id = NULL;
2040 char *filepath;
2041 struct stat sb;
2043 if (asprintf(&filepath, "%s%s%s", path,
2044 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2045 return got_error_from_errno("asprintf");
2047 if (lstat(filepath, &sb) != 0) {
2048 err = got_error_from_errno2("lstat", path);
2049 goto done;
2052 err = got_object_blob_create(&blob_id, filepath, repo);
2053 if (err)
2054 goto done;
2056 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2057 blob_id);
2058 done:
2059 free(filepath);
2060 if (err)
2061 free(blob_id);
2062 return err;
2065 static const struct got_error *
2066 insert_tree_entry(struct got_tree_entry *new_te,
2067 struct got_pathlist_head *paths)
2069 const struct got_error *err = NULL;
2070 struct got_pathlist_entry *new_pe;
2072 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2073 if (err)
2074 return err;
2075 if (new_pe == NULL)
2076 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2077 return NULL;
2080 static const struct got_error *write_tree(struct got_object_id **,
2081 const char *, struct got_pathlist_head *, struct got_repository *,
2082 got_repo_import_cb progress_cb, void *progress_arg);
2084 static const struct got_error *
2085 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2086 const char *path, struct got_pathlist_head *ignores,
2087 struct got_repository *repo,
2088 got_repo_import_cb progress_cb, void *progress_arg)
2090 const struct got_error *err;
2091 struct got_object_id *id = NULL;
2092 char *subdirpath;
2094 if (asprintf(&subdirpath, "%s%s%s", path,
2095 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2096 return got_error_from_errno("asprintf");
2098 (*new_te) = calloc(1, sizeof(**new_te));
2099 if (*new_te == NULL)
2100 return got_error_from_errno("calloc");
2101 (*new_te)->mode = S_IFDIR;
2102 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2103 sizeof((*new_te)->name)) {
2104 err = got_error(GOT_ERR_NO_SPACE);
2105 goto done;
2107 err = write_tree(&id, subdirpath, ignores, repo,
2108 progress_cb, progress_arg);
2109 if (err)
2110 goto done;
2111 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2113 done:
2114 free(id);
2115 free(subdirpath);
2116 if (err) {
2117 free(*new_te);
2118 *new_te = NULL;
2120 return err;
2123 static const struct got_error *
2124 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2125 struct got_pathlist_head *ignores, struct got_repository *repo,
2126 got_repo_import_cb progress_cb, void *progress_arg)
2128 const struct got_error *err = NULL;
2129 DIR *dir;
2130 struct dirent *de;
2131 int nentries;
2132 struct got_tree_entry *new_te = NULL;
2133 struct got_pathlist_head paths;
2134 struct got_pathlist_entry *pe;
2136 *new_tree_id = NULL;
2138 TAILQ_INIT(&paths);
2140 dir = opendir(path_dir);
2141 if (dir == NULL) {
2142 err = got_error_from_errno2("opendir", path_dir);
2143 goto done;
2146 nentries = 0;
2147 while ((de = readdir(dir)) != NULL) {
2148 int ignore = 0;
2149 int type;
2151 if (strcmp(de->d_name, ".") == 0 ||
2152 strcmp(de->d_name, "..") == 0)
2153 continue;
2155 TAILQ_FOREACH(pe, ignores, entry) {
2156 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2157 ignore = 1;
2158 break;
2161 if (ignore)
2162 continue;
2164 err = got_path_dirent_type(&type, path_dir, de);
2165 if (err)
2166 goto done;
2168 if (type == DT_DIR) {
2169 err = import_subdir(&new_te, de, path_dir,
2170 ignores, repo, progress_cb, progress_arg);
2171 if (err) {
2172 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2173 goto done;
2174 err = NULL;
2175 continue;
2177 } else if (type == DT_REG || type == DT_LNK) {
2178 err = import_file(&new_te, de, path_dir, repo);
2179 if (err)
2180 goto done;
2181 } else
2182 continue;
2184 err = insert_tree_entry(new_te, &paths);
2185 if (err)
2186 goto done;
2187 nentries++;
2190 if (TAILQ_EMPTY(&paths)) {
2191 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2192 "cannot create tree without any entries");
2193 goto done;
2196 TAILQ_FOREACH(pe, &paths, entry) {
2197 struct got_tree_entry *te = pe->data;
2198 char *path;
2199 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2200 continue;
2201 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2202 err = got_error_from_errno("asprintf");
2203 goto done;
2205 err = (*progress_cb)(progress_arg, path);
2206 free(path);
2207 if (err)
2208 goto done;
2211 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2212 done:
2213 if (dir)
2214 closedir(dir);
2215 got_pathlist_free(&paths);
2216 return err;
2219 const struct got_error *
2220 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2221 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2222 struct got_repository *repo, got_repo_import_cb progress_cb,
2223 void *progress_arg)
2225 const struct got_error *err;
2226 struct got_object_id *new_tree_id;
2228 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2229 progress_cb, progress_arg);
2230 if (err)
2231 return err;
2233 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2234 author, time(NULL), author, time(NULL), logmsg, repo);
2235 free(new_tree_id);
2236 return err;
2239 const struct got_error *
2240 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2241 struct got_repository *repo)
2243 const struct got_error *err = NULL;
2244 char *path_objects = NULL, *path = NULL;
2245 DIR *dir = NULL;
2246 struct got_object_id id;
2247 int i;
2249 *nobjects = 0;
2250 *ondisk_size = 0;
2252 path_objects = got_repo_get_path_objects(repo);
2253 if (path_objects == NULL)
2254 return got_error_from_errno("got_repo_get_path_objects");
2256 for (i = 0; i <= 0xff; i++) {
2257 struct dirent *dent;
2259 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2260 err = got_error_from_errno("asprintf");
2261 break;
2264 dir = opendir(path);
2265 if (dir == NULL) {
2266 if (errno == ENOENT) {
2267 err = NULL;
2268 continue;
2270 err = got_error_from_errno2("opendir", path);
2271 break;
2274 while ((dent = readdir(dir)) != NULL) {
2275 char *id_str;
2276 int fd;
2277 struct stat sb;
2279 if (strcmp(dent->d_name, ".") == 0 ||
2280 strcmp(dent->d_name, "..") == 0)
2281 continue;
2283 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2284 err = got_error_from_errno("asprintf");
2285 goto done;
2288 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2289 free(id_str);
2290 continue;
2292 free(id_str);
2294 err = got_object_open_loose_fd(&fd, &id, repo);
2295 if (err)
2296 goto done;
2298 if (fstat(fd, &sb) == -1) {
2299 err = got_error_from_errno("fstat");
2300 close(fd);
2301 goto done;
2303 (*nobjects)++;
2304 (*ondisk_size) += sb.st_size;
2306 if (close(fd) == -1) {
2307 err = got_error_from_errno("close");
2308 goto done;
2312 if (closedir(dir) != 0) {
2313 err = got_error_from_errno("closedir");
2314 goto done;
2316 dir = NULL;
2318 free(path);
2319 path = NULL;
2321 done:
2322 if (dir && closedir(dir) != 0 && err == NULL)
2323 err = got_error_from_errno("closedir");
2325 if (err) {
2326 *nobjects = 0;
2327 *ondisk_size = 0;
2329 free(path_objects);
2330 free(path);
2331 return err;
2334 const struct got_error *
2335 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2336 off_t *total_packsize, struct got_repository *repo)
2338 const struct got_error *err = NULL;
2339 DIR *packdir = NULL;
2340 struct dirent *dent;
2341 struct got_packidx *packidx = NULL;
2342 char *path_packidx;
2343 char *path_packfile;
2344 int packdir_fd;
2345 struct stat sb;
2347 *npackfiles = 0;
2348 *nobjects = 0;
2349 *total_packsize = 0;
2351 packdir_fd = openat(got_repo_get_fd(repo),
2352 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2353 if (packdir_fd == -1) {
2354 return got_error_from_errno_fmt("openat: %s/%s",
2355 got_repo_get_path_git_dir(repo),
2356 GOT_OBJECTS_PACK_DIR);
2359 packdir = fdopendir(packdir_fd);
2360 if (packdir == NULL) {
2361 err = got_error_from_errno("fdopendir");
2362 goto done;
2365 while ((dent = readdir(packdir)) != NULL) {
2366 if (!got_repo_is_packidx_filename(dent->d_name,
2367 strlen(dent->d_name)))
2368 continue;
2370 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2371 dent->d_name) == -1) {
2372 err = got_error_from_errno("asprintf");
2373 goto done;
2376 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2377 path_packidx, 0);
2378 free(path_packidx);
2379 if (err)
2380 goto done;
2382 if (fstat(packidx->fd, &sb) == -1)
2383 goto done;
2384 *total_packsize += sb.st_size;
2386 err = got_packidx_get_packfile_path(&path_packfile,
2387 packidx->path_packidx);
2388 if (err)
2389 goto done;
2391 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2392 0) == -1) {
2393 free(path_packfile);
2394 goto done;
2396 free(path_packfile);
2397 *total_packsize += sb.st_size;
2399 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2401 (*npackfiles)++;
2403 got_packidx_close(packidx);
2404 packidx = NULL;
2406 done:
2407 if (packidx)
2408 got_packidx_close(packidx);
2409 if (packdir && closedir(packdir) != 0 && err == NULL)
2410 err = got_error_from_errno("closedir");
2411 if (err) {
2412 *npackfiles = 0;
2413 *nobjects = 0;
2414 *total_packsize = 0;
2416 return err;
2419 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2420 got_packidx_bloom_filter_cmp);