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 char *objects_pack_dir = NULL;
1629 struct got_object_id_queue matched_ids;
1630 struct got_pathlist_entry *pe;
1631 struct stat sb;
1633 STAILQ_INIT(&matched_ids);
1635 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1636 if (objects_pack_dir == NULL)
1637 return got_error_from_errno("got_repo_get_path_objects_pack");
1639 if (stat(objects_pack_dir, &sb) == -1) {
1640 if (errno != ENOENT) {
1641 err = got_error_from_errno2("stat", objects_pack_dir);
1642 goto done;
1644 } else if (sb.st_mtime != repo->pack_path_mtime) {
1645 purge_packidx_paths(&repo->packidx_paths);
1646 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1647 if (err)
1648 goto done;
1651 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1652 const char *path_packidx = pe->path;
1653 struct got_packidx *packidx;
1654 struct got_object_qid *qid;
1656 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1657 path_packidx, 0);
1658 if (err)
1659 break;
1661 err = got_packidx_match_id_str_prefix(&matched_ids,
1662 packidx, id_str_prefix);
1663 if (err) {
1664 got_packidx_close(packidx);
1665 break;
1667 err = got_packidx_close(packidx);
1668 if (err)
1669 break;
1671 STAILQ_FOREACH(qid, &matched_ids, entry) {
1672 if (obj_type != GOT_OBJ_TYPE_ANY) {
1673 int matched_type;
1674 err = got_object_get_type(&matched_type, repo,
1675 &qid->id);
1676 if (err)
1677 goto done;
1678 if (matched_type != obj_type)
1679 continue;
1681 if (*unique_id == NULL) {
1682 *unique_id = got_object_id_dup(&qid->id);
1683 if (*unique_id == NULL) {
1684 err = got_error_from_errno("malloc");
1685 goto done;
1687 } else {
1688 if (got_object_id_cmp(*unique_id,
1689 &qid->id) == 0)
1690 continue; /* packed multiple times */
1691 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1692 goto done;
1696 done:
1697 free(objects_pack_dir);
1698 got_object_id_queue_free(&matched_ids);
1699 if (err) {
1700 free(*unique_id);
1701 *unique_id = NULL;
1703 return err;
1706 static const struct got_error *
1707 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1708 const char *object_dir, const char *id_str_prefix, int obj_type,
1709 struct got_repository *repo)
1711 const struct got_error *err = NULL;
1712 char *path, *id_str = NULL;
1713 DIR *dir = NULL;
1714 struct dirent *dent;
1715 struct got_object_id id;
1717 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1718 err = got_error_from_errno("asprintf");
1719 goto done;
1722 dir = opendir(path);
1723 if (dir == NULL) {
1724 if (errno == ENOENT) {
1725 err = NULL;
1726 goto done;
1728 err = got_error_from_errno2("opendir", path);
1729 goto done;
1731 while ((dent = readdir(dir)) != NULL) {
1732 int cmp;
1734 free(id_str);
1735 id_str = NULL;
1737 if (strcmp(dent->d_name, ".") == 0 ||
1738 strcmp(dent->d_name, "..") == 0)
1739 continue;
1741 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1742 err = got_error_from_errno("asprintf");
1743 goto done;
1746 if (!got_parse_sha1_digest(id.sha1, id_str))
1747 continue;
1750 * Directory entries do not necessarily appear in
1751 * sorted order, so we must iterate over all of them.
1753 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1754 if (cmp != 0)
1755 continue;
1757 if (*unique_id == NULL) {
1758 if (obj_type != GOT_OBJ_TYPE_ANY) {
1759 int matched_type;
1760 err = got_object_get_type(&matched_type, repo,
1761 &id);
1762 if (err)
1763 goto done;
1764 if (matched_type != obj_type)
1765 continue;
1767 *unique_id = got_object_id_dup(&id);
1768 if (*unique_id == NULL) {
1769 err = got_error_from_errno("got_object_id_dup");
1770 goto done;
1772 } else {
1773 if (got_object_id_cmp(*unique_id, &id) == 0)
1774 continue; /* both packed and loose */
1775 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1776 goto done;
1779 done:
1780 if (dir && closedir(dir) != 0 && err == NULL)
1781 err = got_error_from_errno("closedir");
1782 if (err) {
1783 free(*unique_id);
1784 *unique_id = NULL;
1786 free(id_str);
1787 free(path);
1788 return err;
1791 const struct got_error *
1792 got_repo_match_object_id_prefix(struct got_object_id **id,
1793 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1795 const struct got_error *err = NULL;
1796 char *path_objects = NULL, *object_dir = NULL;
1797 size_t len;
1798 int i;
1800 *id = NULL;
1802 path_objects = got_repo_get_path_objects(repo);
1804 len = strlen(id_str_prefix);
1805 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1806 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1807 goto done;
1810 for (i = 0; i < len; i++) {
1811 if (isxdigit((unsigned char)id_str_prefix[i]))
1812 continue;
1813 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1814 goto done;
1817 if (len >= 2) {
1818 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1819 if (err)
1820 goto done;
1821 object_dir = strndup(id_str_prefix, 2);
1822 if (object_dir == NULL) {
1823 err = got_error_from_errno("strdup");
1824 goto done;
1826 err = match_loose_object(id, path_objects, object_dir,
1827 id_str_prefix, obj_type, repo);
1828 } else if (len == 1) {
1829 int i;
1830 for (i = 0; i < 0xf; i++) {
1831 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1832 == -1) {
1833 err = got_error_from_errno("asprintf");
1834 goto done;
1836 err = match_packed_object(id, repo, object_dir,
1837 obj_type);
1838 if (err)
1839 goto done;
1840 err = match_loose_object(id, path_objects, object_dir,
1841 id_str_prefix, obj_type, repo);
1842 if (err)
1843 goto done;
1845 } else {
1846 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1847 goto done;
1849 done:
1850 free(path_objects);
1851 free(object_dir);
1852 if (err) {
1853 free(*id);
1854 *id = NULL;
1855 } else if (*id == NULL) {
1856 switch (obj_type) {
1857 case GOT_OBJ_TYPE_BLOB:
1858 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1859 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1860 break;
1861 case GOT_OBJ_TYPE_TREE:
1862 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1863 GOT_OBJ_LABEL_TREE, id_str_prefix);
1864 break;
1865 case GOT_OBJ_TYPE_COMMIT:
1866 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1867 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1868 break;
1869 case GOT_OBJ_TYPE_TAG:
1870 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1871 GOT_OBJ_LABEL_TAG, id_str_prefix);
1872 break;
1873 default:
1874 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1875 break;
1879 return err;
1882 const struct got_error *
1883 got_repo_match_object_id(struct got_object_id **id, char **label,
1884 const char *id_str, int obj_type, struct got_reflist_head *refs,
1885 struct got_repository *repo)
1887 const struct got_error *err;
1888 struct got_tag_object *tag;
1889 struct got_reference *ref = NULL;
1891 *id = NULL;
1892 if (label)
1893 *label = NULL;
1895 if (refs) {
1896 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1897 refs, repo);
1898 if (err == NULL) {
1899 *id = got_object_id_dup(
1900 got_object_tag_get_object_id(tag));
1901 if (*id == NULL)
1902 err = got_error_from_errno("got_object_id_dup");
1903 else if (label && asprintf(label, "refs/tags/%s",
1904 got_object_tag_get_name(tag)) == -1) {
1905 err = got_error_from_errno("asprintf");
1906 free(*id);
1907 *id = NULL;
1909 got_object_tag_close(tag);
1910 return err;
1911 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1912 err->code != GOT_ERR_NO_OBJ)
1913 return err;
1916 err = got_ref_open(&ref, repo, id_str, 0);
1917 if (err == NULL) {
1918 err = got_ref_resolve(id, repo, ref);
1919 if (err)
1920 goto done;
1921 if (label) {
1922 *label = strdup(got_ref_get_name(ref));
1923 if (*label == NULL) {
1924 err = got_error_from_errno("strdup");
1925 goto done;
1928 } else {
1929 if (err->code != GOT_ERR_NOT_REF &&
1930 err->code != GOT_ERR_BAD_REF_NAME)
1931 goto done;
1932 err = got_repo_match_object_id_prefix(id, id_str,
1933 obj_type, repo);
1934 if (err) {
1935 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1936 err = got_error_not_ref(id_str);
1937 goto done;
1939 if (label) {
1940 err = got_object_id_str(label, *id);
1941 if (*label == NULL) {
1942 err = got_error_from_errno("strdup");
1943 goto done;
1947 done:
1948 if (ref)
1949 got_ref_close(ref);
1950 return err;
1953 const struct got_error *
1954 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1955 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1957 const struct got_error *err = NULL;
1958 struct got_reflist_entry *re;
1959 struct got_object_id *tag_id;
1960 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1962 *tag = NULL;
1964 TAILQ_FOREACH(re, refs, entry) {
1965 const char *refname;
1966 refname = got_ref_get_name(re->ref);
1967 if (got_ref_is_symbolic(re->ref))
1968 continue;
1969 if (strncmp(refname, "refs/tags/", 10) != 0)
1970 continue;
1971 if (!name_is_absolute)
1972 refname += strlen("refs/tags/");
1973 if (strcmp(refname, name) != 0)
1974 continue;
1975 err = got_ref_resolve(&tag_id, repo, re->ref);
1976 if (err)
1977 break;
1978 err = got_object_open_as_tag(tag, repo, tag_id);
1979 free(tag_id);
1980 if (err)
1981 break;
1982 if (obj_type == GOT_OBJ_TYPE_ANY ||
1983 got_object_tag_get_object_type(*tag) == obj_type)
1984 break;
1985 got_object_tag_close(*tag);
1986 *tag = NULL;
1989 if (err == NULL && *tag == NULL)
1990 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1991 GOT_OBJ_LABEL_TAG, name);
1992 return err;
1995 static const struct got_error *
1996 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1997 const char *name, mode_t mode, struct got_object_id *blob_id)
1999 const struct got_error *err = NULL;
2001 *new_te = NULL;
2003 *new_te = calloc(1, sizeof(**new_te));
2004 if (*new_te == NULL)
2005 return got_error_from_errno("calloc");
2007 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2008 sizeof((*new_te)->name)) {
2009 err = got_error(GOT_ERR_NO_SPACE);
2010 goto done;
2013 if (S_ISLNK(mode)) {
2014 (*new_te)->mode = S_IFLNK;
2015 } else {
2016 (*new_te)->mode = S_IFREG;
2017 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2019 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2020 done:
2021 if (err && *new_te) {
2022 free(*new_te);
2023 *new_te = NULL;
2025 return err;
2028 static const struct got_error *
2029 import_file(struct got_tree_entry **new_te, struct dirent *de,
2030 const char *path, struct got_repository *repo)
2032 const struct got_error *err;
2033 struct got_object_id *blob_id = NULL;
2034 char *filepath;
2035 struct stat sb;
2037 if (asprintf(&filepath, "%s%s%s", path,
2038 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2039 return got_error_from_errno("asprintf");
2041 if (lstat(filepath, &sb) != 0) {
2042 err = got_error_from_errno2("lstat", path);
2043 goto done;
2046 err = got_object_blob_create(&blob_id, filepath, repo);
2047 if (err)
2048 goto done;
2050 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2051 blob_id);
2052 done:
2053 free(filepath);
2054 if (err)
2055 free(blob_id);
2056 return err;
2059 static const struct got_error *
2060 insert_tree_entry(struct got_tree_entry *new_te,
2061 struct got_pathlist_head *paths)
2063 const struct got_error *err = NULL;
2064 struct got_pathlist_entry *new_pe;
2066 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2067 if (err)
2068 return err;
2069 if (new_pe == NULL)
2070 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2071 return NULL;
2074 static const struct got_error *write_tree(struct got_object_id **,
2075 const char *, struct got_pathlist_head *, struct got_repository *,
2076 got_repo_import_cb progress_cb, void *progress_arg);
2078 static const struct got_error *
2079 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2080 const char *path, struct got_pathlist_head *ignores,
2081 struct got_repository *repo,
2082 got_repo_import_cb progress_cb, void *progress_arg)
2084 const struct got_error *err;
2085 struct got_object_id *id = NULL;
2086 char *subdirpath;
2088 if (asprintf(&subdirpath, "%s%s%s", path,
2089 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2090 return got_error_from_errno("asprintf");
2092 (*new_te) = calloc(1, sizeof(**new_te));
2093 if (*new_te == NULL)
2094 return got_error_from_errno("calloc");
2095 (*new_te)->mode = S_IFDIR;
2096 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2097 sizeof((*new_te)->name)) {
2098 err = got_error(GOT_ERR_NO_SPACE);
2099 goto done;
2101 err = write_tree(&id, subdirpath, ignores, repo,
2102 progress_cb, progress_arg);
2103 if (err)
2104 goto done;
2105 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2107 done:
2108 free(id);
2109 free(subdirpath);
2110 if (err) {
2111 free(*new_te);
2112 *new_te = NULL;
2114 return err;
2117 static const struct got_error *
2118 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2119 struct got_pathlist_head *ignores, struct got_repository *repo,
2120 got_repo_import_cb progress_cb, void *progress_arg)
2122 const struct got_error *err = NULL;
2123 DIR *dir;
2124 struct dirent *de;
2125 int nentries;
2126 struct got_tree_entry *new_te = NULL;
2127 struct got_pathlist_head paths;
2128 struct got_pathlist_entry *pe;
2130 *new_tree_id = NULL;
2132 TAILQ_INIT(&paths);
2134 dir = opendir(path_dir);
2135 if (dir == NULL) {
2136 err = got_error_from_errno2("opendir", path_dir);
2137 goto done;
2140 nentries = 0;
2141 while ((de = readdir(dir)) != NULL) {
2142 int ignore = 0;
2143 int type;
2145 if (strcmp(de->d_name, ".") == 0 ||
2146 strcmp(de->d_name, "..") == 0)
2147 continue;
2149 TAILQ_FOREACH(pe, ignores, entry) {
2150 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2151 ignore = 1;
2152 break;
2155 if (ignore)
2156 continue;
2158 err = got_path_dirent_type(&type, path_dir, de);
2159 if (err)
2160 goto done;
2162 if (type == DT_DIR) {
2163 err = import_subdir(&new_te, de, path_dir,
2164 ignores, repo, progress_cb, progress_arg);
2165 if (err) {
2166 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2167 goto done;
2168 err = NULL;
2169 continue;
2171 } else if (type == DT_REG || type == DT_LNK) {
2172 err = import_file(&new_te, de, path_dir, repo);
2173 if (err)
2174 goto done;
2175 } else
2176 continue;
2178 err = insert_tree_entry(new_te, &paths);
2179 if (err)
2180 goto done;
2181 nentries++;
2184 if (TAILQ_EMPTY(&paths)) {
2185 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2186 "cannot create tree without any entries");
2187 goto done;
2190 TAILQ_FOREACH(pe, &paths, entry) {
2191 struct got_tree_entry *te = pe->data;
2192 char *path;
2193 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2194 continue;
2195 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2196 err = got_error_from_errno("asprintf");
2197 goto done;
2199 err = (*progress_cb)(progress_arg, path);
2200 free(path);
2201 if (err)
2202 goto done;
2205 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2206 done:
2207 if (dir)
2208 closedir(dir);
2209 got_pathlist_free(&paths);
2210 return err;
2213 const struct got_error *
2214 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2215 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2216 struct got_repository *repo, got_repo_import_cb progress_cb,
2217 void *progress_arg)
2219 const struct got_error *err;
2220 struct got_object_id *new_tree_id;
2222 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2223 progress_cb, progress_arg);
2224 if (err)
2225 return err;
2227 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2228 author, time(NULL), author, time(NULL), logmsg, repo);
2229 free(new_tree_id);
2230 return err;
2233 const struct got_error *
2234 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2235 struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 char *path_objects = NULL, *path = NULL;
2239 DIR *dir = NULL;
2240 struct got_object_id id;
2241 int i;
2243 *nobjects = 0;
2244 *ondisk_size = 0;
2246 path_objects = got_repo_get_path_objects(repo);
2247 if (path_objects == NULL)
2248 return got_error_from_errno("got_repo_get_path_objects");
2250 for (i = 0; i <= 0xff; i++) {
2251 struct dirent *dent;
2253 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2254 err = got_error_from_errno("asprintf");
2255 break;
2258 dir = opendir(path);
2259 if (dir == NULL) {
2260 if (errno == ENOENT) {
2261 err = NULL;
2262 continue;
2264 err = got_error_from_errno2("opendir", path);
2265 break;
2268 while ((dent = readdir(dir)) != NULL) {
2269 char *id_str;
2270 int fd;
2271 struct stat sb;
2273 if (strcmp(dent->d_name, ".") == 0 ||
2274 strcmp(dent->d_name, "..") == 0)
2275 continue;
2277 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2278 err = got_error_from_errno("asprintf");
2279 goto done;
2282 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2283 free(id_str);
2284 continue;
2286 free(id_str);
2288 err = got_object_open_loose_fd(&fd, &id, repo);
2289 if (err)
2290 goto done;
2292 if (fstat(fd, &sb) == -1) {
2293 err = got_error_from_errno("fstat");
2294 close(fd);
2295 goto done;
2297 (*nobjects)++;
2298 (*ondisk_size) += sb.st_size;
2300 if (close(fd) == -1) {
2301 err = got_error_from_errno("close");
2302 goto done;
2306 if (closedir(dir) != 0) {
2307 err = got_error_from_errno("closedir");
2308 goto done;
2310 dir = NULL;
2312 free(path);
2313 path = NULL;
2315 done:
2316 if (dir && closedir(dir) != 0 && err == NULL)
2317 err = got_error_from_errno("closedir");
2319 if (err) {
2320 *nobjects = 0;
2321 *ondisk_size = 0;
2323 free(path_objects);
2324 free(path);
2325 return err;
2328 const struct got_error *
2329 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2330 off_t *total_packsize, struct got_repository *repo)
2332 const struct got_error *err = NULL;
2333 DIR *packdir = NULL;
2334 struct dirent *dent;
2335 struct got_packidx *packidx = NULL;
2336 char *path_packidx;
2337 char *path_packfile;
2338 int packdir_fd;
2339 struct stat sb;
2341 *npackfiles = 0;
2342 *nobjects = 0;
2343 *total_packsize = 0;
2345 packdir_fd = openat(got_repo_get_fd(repo),
2346 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2347 if (packdir_fd == -1) {
2348 return got_error_from_errno_fmt("openat: %s/%s",
2349 got_repo_get_path_git_dir(repo),
2350 GOT_OBJECTS_PACK_DIR);
2353 packdir = fdopendir(packdir_fd);
2354 if (packdir == NULL) {
2355 err = got_error_from_errno("fdopendir");
2356 goto done;
2359 while ((dent = readdir(packdir)) != NULL) {
2360 if (!got_repo_is_packidx_filename(dent->d_name,
2361 strlen(dent->d_name)))
2362 continue;
2364 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2365 dent->d_name) == -1) {
2366 err = got_error_from_errno("asprintf");
2367 goto done;
2370 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2371 path_packidx, 0);
2372 free(path_packidx);
2373 if (err)
2374 goto done;
2376 if (fstat(packidx->fd, &sb) == -1)
2377 goto done;
2378 *total_packsize += sb.st_size;
2380 err = got_packidx_get_packfile_path(&path_packfile,
2381 packidx->path_packidx);
2382 if (err)
2383 goto done;
2385 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2386 0) == -1) {
2387 free(path_packfile);
2388 goto done;
2390 free(path_packfile);
2391 *total_packsize += sb.st_size;
2393 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2395 (*npackfiles)++;
2397 got_packidx_close(packidx);
2398 packidx = NULL;
2400 done:
2401 if (packidx)
2402 got_packidx_close(packidx);
2403 if (packdir && closedir(packdir) != 0 && err == NULL)
2404 err = got_error_from_errno("closedir");
2405 if (err) {
2406 *npackfiles = 0;
2407 *nobjects = 0;
2408 *total_packsize = 0;
2410 return err;
2413 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2414 got_packidx_bloom_filter_cmp);