Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 #endif
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
76 const char *
77 got_repo_get_path(struct got_repository *repo)
78 {
79 return repo->path;
80 }
82 const char *
83 got_repo_get_path_git_dir(struct got_repository *repo)
84 {
85 return repo->path_git_dir;
86 }
88 int
89 got_repo_get_fd(struct got_repository *repo)
90 {
91 return repo->gitdir_fd;
92 }
94 const char *
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_name;
98 }
100 const char *
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
106 const char *
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
112 const char *
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
118 const char *
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
124 void
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
132 int
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
138 static char *
139 get_path_git_child(struct got_repository *repo, const char *basename)
141 char *path_child;
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
144 basename) == -1)
145 return NULL;
147 return path_child;
150 char *
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
156 char *
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
162 char *
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
168 char *
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
174 static char *
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
180 char *
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
186 char *
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
198 void
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
206 static int
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
213 int ret = 0;
214 struct stat sb;
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_objects, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_refs, &sb) == -1)
228 goto done;
229 if (!S_ISDIR(sb.st_mode))
230 goto done;
232 if (lstat(path_head, &sb) == -1)
233 goto done;
234 if (!S_ISREG(sb.st_mode))
235 goto done;
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
239 goto done;
240 got_ref_close(head_ref);
242 ret = 1;
243 done:
244 free(path_objects);
245 free(path_refs);
246 free(path_head);
247 return ret;
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i;
257 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
258 if (*pack_fds == NULL)
259 return got_error_from_errno("calloc");
261 /*
262 * got_repo_pack_fds_close will try to close all of the
263 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
264 * a value from got_opentempfd(), which would result in a close(0) if
265 * we do not initialize to -1 here.
266 */
267 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
268 (*pack_fds)[i] = -1;
270 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
271 (*pack_fds)[i] = got_opentempfd();
272 if ((*pack_fds)[i] == -1) {
273 err = got_error_from_errno("got_opentempfd");
274 got_repo_pack_fds_close(*pack_fds);
275 *pack_fds = NULL;
276 return err;
280 return NULL;
283 const struct got_error *
284 got_repo_pack_fds_close(int *pack_fds)
286 const struct got_error *err = NULL;
287 int i;
289 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
290 if (pack_fds[i] == -1)
291 continue;
292 if (close(pack_fds[i]) == -1) {
293 err = got_error_from_errno("close");
294 break;
297 free(pack_fds);
298 return err;
301 const struct got_error *
302 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
303 struct got_object *obj)
305 #ifndef GOT_NO_OBJ_CACHE
306 const struct got_error *err = NULL;
307 err = got_object_cache_add(&repo->objcache, id, obj);
308 if (err) {
309 if (err->code == GOT_ERR_OBJ_EXISTS ||
310 err->code == GOT_ERR_OBJ_TOO_LARGE)
311 err = NULL;
312 return err;
314 obj->refcnt++;
315 #endif
316 return NULL;
319 struct got_object *
320 got_repo_get_cached_object(struct got_repository *repo,
321 struct got_object_id *id)
323 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
326 const struct got_error *
327 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
328 struct got_tree_object *tree)
330 #ifndef GOT_NO_OBJ_CACHE
331 const struct got_error *err = NULL;
332 err = got_object_cache_add(&repo->treecache, id, tree);
333 if (err) {
334 if (err->code == GOT_ERR_OBJ_EXISTS ||
335 err->code == GOT_ERR_OBJ_TOO_LARGE)
336 err = NULL;
337 return err;
339 tree->refcnt++;
340 #endif
341 return NULL;
344 struct got_tree_object *
345 got_repo_get_cached_tree(struct got_repository *repo,
346 struct got_object_id *id)
348 return (struct got_tree_object *)got_object_cache_get(
349 &repo->treecache, id);
352 const struct got_error *
353 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
354 struct got_commit_object *commit)
356 #ifndef GOT_NO_OBJ_CACHE
357 const struct got_error *err = NULL;
358 err = got_object_cache_add(&repo->commitcache, id, commit);
359 if (err) {
360 if (err->code == GOT_ERR_OBJ_EXISTS ||
361 err->code == GOT_ERR_OBJ_TOO_LARGE)
362 err = NULL;
363 return err;
365 commit->refcnt++;
366 #endif
367 return NULL;
370 struct got_commit_object *
371 got_repo_get_cached_commit(struct got_repository *repo,
372 struct got_object_id *id)
374 return (struct got_commit_object *)got_object_cache_get(
375 &repo->commitcache, id);
378 const struct got_error *
379 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
380 struct got_tag_object *tag)
382 #ifndef GOT_NO_OBJ_CACHE
383 const struct got_error *err = NULL;
384 err = got_object_cache_add(&repo->tagcache, id, tag);
385 if (err) {
386 if (err->code == GOT_ERR_OBJ_EXISTS ||
387 err->code == GOT_ERR_OBJ_TOO_LARGE)
388 err = NULL;
389 return err;
391 tag->refcnt++;
392 #endif
393 return NULL;
396 struct got_tag_object *
397 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
399 return (struct got_tag_object *)got_object_cache_get(
400 &repo->tagcache, id);
403 const struct got_error *
404 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
405 struct got_raw_object *raw)
407 #ifndef GOT_NO_OBJ_CACHE
408 const struct got_error *err = NULL;
409 err = got_object_cache_add(&repo->rawcache, id, raw);
410 if (err) {
411 if (err->code == GOT_ERR_OBJ_EXISTS ||
412 err->code == GOT_ERR_OBJ_TOO_LARGE)
413 err = NULL;
414 return err;
416 raw->refcnt++;
417 #endif
418 return NULL;
422 struct got_raw_object *
423 got_repo_get_cached_raw_object(struct got_repository *repo,
424 struct got_object_id *id)
426 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
430 static const struct got_error *
431 open_repo(struct got_repository *repo, const char *path)
433 const struct got_error *err = NULL;
435 repo->gitdir_fd = -1;
437 /* bare git repository? */
438 repo->path_git_dir = strdup(path);
439 if (repo->path_git_dir == NULL)
440 return got_error_from_errno("strdup");
441 if (is_git_repo(repo)) {
442 repo->path = strdup(repo->path_git_dir);
443 if (repo->path == NULL) {
444 err = got_error_from_errno("strdup");
445 goto done;
447 repo->gitdir_fd = open(repo->path_git_dir,
448 O_DIRECTORY | O_CLOEXEC);
449 if (repo->gitdir_fd == -1) {
450 err = got_error_from_errno2("open",
451 repo->path_git_dir);
452 goto done;
454 return NULL;
457 /* git repository with working tree? */
458 free(repo->path_git_dir);
459 repo->path_git_dir = NULL;
460 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
461 err = got_error_from_errno("asprintf");
462 goto done;
464 if (is_git_repo(repo)) {
465 repo->path = strdup(path);
466 if (repo->path == NULL) {
467 err = got_error_from_errno("strdup");
468 goto done;
470 repo->gitdir_fd = open(repo->path_git_dir,
471 O_DIRECTORY | O_CLOEXEC);
472 if (repo->gitdir_fd == -1) {
473 err = got_error_from_errno2("open",
474 repo->path_git_dir);
475 goto done;
477 return NULL;
480 err = got_error(GOT_ERR_NOT_GIT_REPO);
481 done:
482 if (err) {
483 free(repo->path);
484 repo->path = NULL;
485 free(repo->path_git_dir);
486 repo->path_git_dir = NULL;
487 if (repo->gitdir_fd != -1)
488 close(repo->gitdir_fd);
489 repo->gitdir_fd = -1;
492 return err;
495 static const struct got_error *
496 parse_gitconfig_file(int *gitconfig_repository_format_version,
497 char **gitconfig_author_name, char **gitconfig_author_email,
498 struct got_remote_repo **remotes, int *nremotes,
499 char **gitconfig_owner, char ***extensions, int *nextensions,
500 const char *gitconfig_path)
502 const struct got_error *err = NULL, *child_err = NULL;
503 int fd = -1;
504 int imsg_fds[2] = { -1, -1 };
505 pid_t pid;
506 struct imsgbuf *ibuf;
508 *gitconfig_repository_format_version = 0;
509 if (extensions)
510 *extensions = NULL;
511 if (nextensions)
512 *nextensions = 0;
513 *gitconfig_author_name = NULL;
514 *gitconfig_author_email = NULL;
515 if (remotes)
516 *remotes = NULL;
517 if (nremotes)
518 *nremotes = 0;
519 if (gitconfig_owner)
520 *gitconfig_owner = NULL;
522 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
523 if (fd == -1) {
524 if (errno == ENOENT)
525 return NULL;
526 return got_error_from_errno2("open", gitconfig_path);
529 ibuf = calloc(1, sizeof(*ibuf));
530 if (ibuf == NULL) {
531 err = got_error_from_errno("calloc");
532 goto done;
535 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
536 err = got_error_from_errno("socketpair");
537 goto done;
540 pid = fork();
541 if (pid == -1) {
542 err = got_error_from_errno("fork");
543 goto done;
544 } else if (pid == 0) {
545 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
546 gitconfig_path);
547 /* not reached */
550 if (close(imsg_fds[1]) == -1) {
551 err = got_error_from_errno("close");
552 goto done;
554 imsg_fds[1] = -1;
555 imsg_init(ibuf, imsg_fds[0]);
557 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
558 if (err)
559 goto done;
560 fd = -1;
562 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
563 if (err)
564 goto done;
566 err = got_privsep_recv_gitconfig_int(
567 gitconfig_repository_format_version, ibuf);
568 if (err)
569 goto done;
571 if (extensions && nextensions) {
572 err = got_privsep_send_gitconfig_repository_extensions_req(
573 ibuf);
574 if (err)
575 goto done;
576 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
577 if (err)
578 goto done;
579 if (*nextensions > 0) {
580 int i;
581 *extensions = calloc(*nextensions, sizeof(char *));
582 if (*extensions == NULL) {
583 err = got_error_from_errno("calloc");
584 goto done;
586 for (i = 0; i < *nextensions; i++) {
587 char *ext;
588 err = got_privsep_recv_gitconfig_str(&ext,
589 ibuf);
590 if (err)
591 goto done;
592 (*extensions)[i] = ext;
597 err = got_privsep_send_gitconfig_author_name_req(ibuf);
598 if (err)
599 goto done;
601 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
602 if (err)
603 goto done;
605 err = got_privsep_send_gitconfig_author_email_req(ibuf);
606 if (err)
607 goto done;
609 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
610 if (err)
611 goto done;
613 if (remotes && nremotes) {
614 err = got_privsep_send_gitconfig_remotes_req(ibuf);
615 if (err)
616 goto done;
618 err = got_privsep_recv_gitconfig_remotes(remotes,
619 nremotes, ibuf);
620 if (err)
621 goto done;
624 if (gitconfig_owner) {
625 err = got_privsep_send_gitconfig_owner_req(ibuf);
626 if (err)
627 goto done;
628 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
629 if (err)
630 goto done;
633 err = got_privsep_send_stop(imsg_fds[0]);
634 child_err = got_privsep_wait_for_child(pid);
635 if (child_err && err == NULL)
636 err = child_err;
637 done:
638 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
639 err = got_error_from_errno("close");
640 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
641 err = got_error_from_errno("close");
642 if (fd != -1 && close(fd) == -1 && err == NULL)
643 err = got_error_from_errno2("close", gitconfig_path);
644 free(ibuf);
645 return err;
648 static const struct got_error *
649 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
651 const struct got_error *err = NULL;
652 char *repo_gitconfig_path = NULL;
654 if (global_gitconfig_path) {
655 /* Read settings from ~/.gitconfig. */
656 int dummy_repo_version;
657 err = parse_gitconfig_file(&dummy_repo_version,
658 &repo->global_gitconfig_author_name,
659 &repo->global_gitconfig_author_email,
660 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
661 if (err)
662 return err;
665 /* Read repository's .git/config file. */
666 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
667 if (repo_gitconfig_path == NULL)
668 return got_error_from_errno("got_repo_get_path_gitconfig");
670 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
671 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
672 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
673 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
674 repo_gitconfig_path);
675 if (err)
676 goto done;
678 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
679 int i;
681 for (i = 0; i < repo->ngitconfig_remotes; i++) {
682 got_repo_free_remote_repo_data(
683 &repo->gitconfig_remotes[i]);
685 free(repo->gitconfig_remotes);
686 repo->gitconfig_remotes = NULL;
687 repo->ngitconfig_remotes = 0;
689 free(repo->gitconfig_author_name);
690 repo->gitconfig_author_name = NULL;
691 free(repo->gitconfig_author_email);
692 repo->gitconfig_author_email = NULL;
694 free(repo->global_gitconfig_author_name);
695 repo->global_gitconfig_author_name = NULL;
696 free(repo->global_gitconfig_author_email);
697 repo->global_gitconfig_author_email = NULL;
700 done:
701 free(repo_gitconfig_path);
702 return err;
705 static const struct got_error *
706 read_gotconfig(struct got_repository *repo)
708 const struct got_error *err = NULL;
709 char *gotconfig_path;
711 gotconfig_path = got_repo_get_path_gotconfig(repo);
712 if (gotconfig_path == NULL)
713 return got_error_from_errno("got_repo_get_path_gotconfig");
715 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
716 free(gotconfig_path);
717 return err;
720 /* Supported repository format extensions. */
721 static const char *const repo_extensions[] = {
722 "noop", /* Got supports repository format version 1. */
723 "preciousObjects", /* Supported by gotadmin cleanup. */
724 "worktreeConfig", /* Got does not care about Git work trees. */
725 };
727 const struct got_error *
728 got_repo_open(struct got_repository **repop, const char *path,
729 const char *global_gitconfig_path, int *pack_fds)
731 struct got_repository *repo = NULL;
732 const struct got_error *err = NULL;
733 char *repo_path = NULL;
734 size_t i, j = 0;
735 struct rlimit rl;
737 *repop = NULL;
739 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
740 return got_error_from_errno("getrlimit");
742 repo = calloc(1, sizeof(*repo));
743 if (repo == NULL)
744 return got_error_from_errno("calloc");
746 RB_INIT(&repo->packidx_bloom_filters);
747 TAILQ_INIT(&repo->packidx_paths);
749 for (i = 0; i < nitems(repo->privsep_children); i++) {
750 memset(&repo->privsep_children[i], 0,
751 sizeof(repo->privsep_children[0]));
752 repo->privsep_children[i].imsg_fd = -1;
755 err = got_object_cache_init(&repo->objcache,
756 GOT_OBJECT_CACHE_TYPE_OBJ);
757 if (err)
758 goto done;
759 err = got_object_cache_init(&repo->treecache,
760 GOT_OBJECT_CACHE_TYPE_TREE);
761 if (err)
762 goto done;
763 err = got_object_cache_init(&repo->commitcache,
764 GOT_OBJECT_CACHE_TYPE_COMMIT);
765 if (err)
766 goto done;
767 err = got_object_cache_init(&repo->tagcache,
768 GOT_OBJECT_CACHE_TYPE_TAG);
769 if (err)
770 goto done;
771 err = got_object_cache_init(&repo->rawcache,
772 GOT_OBJECT_CACHE_TYPE_RAW);
773 if (err)
774 goto done;
776 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
777 if (repo->pack_cache_size > rl.rlim_cur / 8)
778 repo->pack_cache_size = rl.rlim_cur / 8;
779 for (i = 0; i < nitems(repo->packs); i++) {
780 if (i < repo->pack_cache_size) {
781 repo->packs[i].basefd = pack_fds[j++];
782 repo->packs[i].accumfd = pack_fds[j++];
783 } else {
784 repo->packs[i].basefd = -1;
785 repo->packs[i].accumfd = -1;
788 repo->pinned_pack = -1;
789 repo->pinned_packidx = -1;
790 repo->pinned_pid = 0;
792 repo_path = realpath(path, NULL);
793 if (repo_path == NULL) {
794 err = got_error_from_errno2("realpath", path);
795 goto done;
798 for (;;) {
799 char *parent_path;
801 err = open_repo(repo, repo_path);
802 if (err == NULL)
803 break;
804 if (err->code != GOT_ERR_NOT_GIT_REPO)
805 goto done;
806 if (repo_path[0] == '/' && repo_path[1] == '\0') {
807 err = got_error(GOT_ERR_NOT_GIT_REPO);
808 goto done;
810 err = got_path_dirname(&parent_path, repo_path);
811 if (err)
812 goto done;
813 free(repo_path);
814 repo_path = parent_path;
817 err = read_gotconfig(repo);
818 if (err)
819 goto done;
821 err = read_gitconfig(repo, global_gitconfig_path);
822 if (err)
823 goto done;
824 if (repo->gitconfig_repository_format_version != 0) {
825 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
826 goto done;
828 for (i = 0; i < repo->nextensions; i++) {
829 char *ext = repo->extensions[i];
830 int j, supported = 0;
831 for (j = 0; j < nitems(repo_extensions); j++) {
832 if (strcmp(ext, repo_extensions[j]) == 0) {
833 supported = 1;
834 break;
837 if (!supported) {
838 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
839 goto done;
843 err = got_repo_list_packidx(&repo->packidx_paths, repo);
844 done:
845 if (err)
846 got_repo_close(repo);
847 else
848 *repop = repo;
849 free(repo_path);
850 return err;
853 const struct got_error *
854 got_repo_close(struct got_repository *repo)
856 const struct got_error *err = NULL, *child_err;
857 struct got_packidx_bloom_filter *bf;
858 struct got_pathlist_entry *pe;
859 size_t i;
861 for (i = 0; i < repo->pack_cache_size; i++) {
862 if (repo->packidx_cache[i] == NULL)
863 break;
864 got_packidx_close(repo->packidx_cache[i]);
867 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
868 &repo->packidx_bloom_filters))) {
869 RB_REMOVE(got_packidx_bloom_filter_tree,
870 &repo->packidx_bloom_filters, bf);
871 bloom_free(bf->bloom);
872 free(bf->bloom);
873 free(bf);
876 for (i = 0; i < repo->pack_cache_size; i++)
877 if (repo->packs[i].path_packfile)
878 if (repo->packs[i].path_packfile)
879 got_pack_close(&repo->packs[i]);
881 free(repo->path);
882 free(repo->path_git_dir);
884 got_object_cache_close(&repo->objcache);
885 got_object_cache_close(&repo->treecache);
886 got_object_cache_close(&repo->commitcache);
887 got_object_cache_close(&repo->tagcache);
888 got_object_cache_close(&repo->rawcache);
890 for (i = 0; i < nitems(repo->privsep_children); i++) {
891 if (repo->privsep_children[i].imsg_fd == -1)
892 continue;
893 imsg_clear(repo->privsep_children[i].ibuf);
894 free(repo->privsep_children[i].ibuf);
895 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
896 child_err = got_privsep_wait_for_child(
897 repo->privsep_children[i].pid);
898 if (child_err && err == NULL)
899 err = child_err;
900 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
901 err == NULL)
902 err = got_error_from_errno("close");
905 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
906 err == NULL)
907 err = got_error_from_errno("close");
909 if (repo->gotconfig)
910 got_gotconfig_free(repo->gotconfig);
911 free(repo->gitconfig_author_name);
912 free(repo->gitconfig_author_email);
913 for (i = 0; i < repo->ngitconfig_remotes; i++)
914 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
915 free(repo->gitconfig_remotes);
916 for (i = 0; i < repo->nextensions; i++)
917 free(repo->extensions[i]);
918 free(repo->extensions);
920 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
921 free((void *)pe->path);
922 got_pathlist_free(&repo->packidx_paths);
923 free(repo);
925 return err;
928 void
929 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
931 int i;
933 free(repo->name);
934 repo->name = NULL;
935 free(repo->fetch_url);
936 repo->fetch_url = NULL;
937 free(repo->send_url);
938 repo->send_url = NULL;
939 for (i = 0; i < repo->nfetch_branches; i++)
940 free(repo->fetch_branches[i]);
941 free(repo->fetch_branches);
942 repo->fetch_branches = NULL;
943 repo->nfetch_branches = 0;
944 for (i = 0; i < repo->nsend_branches; i++)
945 free(repo->send_branches[i]);
946 free(repo->send_branches);
947 repo->send_branches = NULL;
948 repo->nsend_branches = 0;
951 const struct got_error *
952 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
953 const char *input_path)
955 const struct got_error *err = NULL;
956 const char *repo_abspath = NULL;
957 size_t repolen, len;
958 char *canonpath, *path = NULL;
960 *in_repo_path = NULL;
962 canonpath = strdup(input_path);
963 if (canonpath == NULL) {
964 err = got_error_from_errno("strdup");
965 goto done;
967 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
968 if (err)
969 goto done;
971 repo_abspath = got_repo_get_path(repo);
973 if (canonpath[0] == '\0') {
974 path = strdup(canonpath);
975 if (path == NULL) {
976 err = got_error_from_errno("strdup");
977 goto done;
979 } else {
980 path = realpath(canonpath, NULL);
981 if (path == NULL) {
982 if (errno != ENOENT) {
983 err = got_error_from_errno2("realpath",
984 canonpath);
985 goto done;
987 /*
988 * Path is not on disk.
989 * Assume it is already relative to repository root.
990 */
991 path = strdup(canonpath);
992 if (path == NULL) {
993 err = got_error_from_errno("strdup");
994 goto done;
998 repolen = strlen(repo_abspath);
999 len = strlen(path);
1002 if (strcmp(path, repo_abspath) == 0) {
1003 free(path);
1004 path = strdup("");
1005 if (path == NULL) {
1006 err = got_error_from_errno("strdup");
1007 goto done;
1009 } else if (len > repolen &&
1010 got_path_is_child(path, repo_abspath, repolen)) {
1011 /* Matched an on-disk path inside repository. */
1012 if (got_repo_is_bare(repo)) {
1014 * Matched an on-disk path inside repository
1015 * database. Treat input as repository-relative.
1017 free(path);
1018 path = canonpath;
1019 canonpath = NULL;
1020 } else {
1021 char *child;
1022 /* Strip common prefix with repository path. */
1023 err = got_path_skip_common_ancestor(&child,
1024 repo_abspath, path);
1025 if (err)
1026 goto done;
1027 free(path);
1028 path = child;
1030 } else {
1032 * Matched unrelated on-disk path.
1033 * Treat input as repository-relative.
1035 free(path);
1036 path = canonpath;
1037 canonpath = NULL;
1041 /* Make in-repository path absolute */
1042 if (path[0] != '/') {
1043 char *abspath;
1044 if (asprintf(&abspath, "/%s", path) == -1) {
1045 err = got_error_from_errno("asprintf");
1046 goto done;
1048 free(path);
1049 path = abspath;
1052 done:
1053 free(canonpath);
1054 if (err)
1055 free(path);
1056 else
1057 *in_repo_path = path;
1058 return err;
1061 static const struct got_error *
1062 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1063 const char *path_packidx)
1065 const struct got_error *err = NULL;
1066 size_t i;
1068 for (i = 0; i < repo->pack_cache_size; i++) {
1069 if (repo->packidx_cache[i] == NULL)
1070 break;
1071 if (strcmp(repo->packidx_cache[i]->path_packidx,
1072 path_packidx) == 0) {
1073 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1076 if (i == repo->pack_cache_size) {
1077 do {
1078 i--;
1079 } while (i > 0 && repo->pinned_packidx >= 0 &&
1080 i == repo->pinned_packidx);
1081 err = got_packidx_close(repo->packidx_cache[i]);
1082 if (err)
1083 return err;
1086 repo->packidx_cache[i] = packidx;
1088 return NULL;
1091 int
1092 got_repo_is_packidx_filename(const char *name, size_t len)
1094 if (len != GOT_PACKIDX_NAMELEN)
1095 return 0;
1097 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1098 return 0;
1100 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1101 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1102 return 0;
1104 return 1;
1107 static struct got_packidx_bloom_filter *
1108 get_packidx_bloom_filter(struct got_repository *repo,
1109 const char *path, size_t path_len)
1111 struct got_packidx_bloom_filter key;
1113 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1114 return NULL; /* XXX */
1115 key.path_len = path_len;
1117 return RB_FIND(got_packidx_bloom_filter_tree,
1118 &repo->packidx_bloom_filters, &key);
1121 int
1122 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1123 const char *path_packidx, struct got_object_id *id)
1125 struct got_packidx_bloom_filter *bf;
1127 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1128 if (bf)
1129 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1131 /* No bloom filter means this pack index must be searched. */
1132 return 1;
1135 static const struct got_error *
1136 add_packidx_bloom_filter(struct got_repository *repo,
1137 struct got_packidx *packidx, const char *path_packidx)
1139 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1140 struct got_packidx_bloom_filter *bf;
1141 size_t len;
1144 * Don't use bloom filters for very large pack index files.
1145 * Large pack files will contain a relatively large fraction
1146 * of our objects so we will likely need to visit them anyway.
1147 * The more objects a pack file contains the higher the probability
1148 * of a false-positive match from the bloom filter. And reading
1149 * all object IDs from a large pack index file can be expensive.
1151 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1152 return NULL;
1154 /* Do we already have a filter for this pack index? */
1155 if (get_packidx_bloom_filter(repo, path_packidx,
1156 strlen(path_packidx)) != NULL)
1157 return NULL;
1159 bf = calloc(1, sizeof(*bf));
1160 if (bf == NULL)
1161 return got_error_from_errno("calloc");
1162 bf->bloom = calloc(1, sizeof(*bf->bloom));
1163 if (bf->bloom == NULL) {
1164 free(bf);
1165 return got_error_from_errno("calloc");
1168 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1169 if (len >= sizeof(bf->path)) {
1170 free(bf->bloom);
1171 free(bf);
1172 return got_error(GOT_ERR_NO_SPACE);
1174 bf->path_len = len;
1176 /* Minimum size supported by our bloom filter is 1000 entries. */
1177 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1178 for (i = 0; i < nobjects; i++) {
1179 struct got_packidx_object_id *id;
1180 id = &packidx->hdr.sorted_ids[i];
1181 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1184 RB_INSERT(got_packidx_bloom_filter_tree,
1185 &repo->packidx_bloom_filters, bf);
1186 return NULL;
1189 const struct got_error *
1190 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1191 struct got_repository *repo, struct got_object_id *id)
1193 const struct got_error *err;
1194 struct got_pathlist_entry *pe;
1195 size_t i;
1197 /* Search pack index cache. */
1198 for (i = 0; i < repo->pack_cache_size; i++) {
1199 if (repo->packidx_cache[i] == NULL)
1200 break;
1201 if (!got_repo_check_packidx_bloom_filter(repo,
1202 repo->packidx_cache[i]->path_packidx, id))
1203 continue; /* object will not be found in this index */
1204 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1205 if (*idx != -1) {
1206 *packidx = repo->packidx_cache[i];
1208 * Move this cache entry to the front. Repeatedly
1209 * searching a wrong pack index can be expensive.
1211 if (i > 0) {
1212 memmove(&repo->packidx_cache[1],
1213 &repo->packidx_cache[0],
1214 i * sizeof(repo->packidx_cache[0]));
1215 repo->packidx_cache[0] = *packidx;
1216 if (repo->pinned_packidx >= 0 &&
1217 repo->pinned_packidx < i)
1218 repo->pinned_packidx++;
1219 else if (repo->pinned_packidx == i)
1220 repo->pinned_packidx = 0;
1222 return NULL;
1225 /* No luck. Search the filesystem. */
1227 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1228 const char *path_packidx = pe->path;
1229 int is_cached = 0;
1231 if (!got_repo_check_packidx_bloom_filter(repo,
1232 pe->path, id))
1233 continue; /* object will not be found in this index */
1235 for (i = 0; i < repo->pack_cache_size; i++) {
1236 if (repo->packidx_cache[i] == NULL)
1237 break;
1238 if (strcmp(repo->packidx_cache[i]->path_packidx,
1239 path_packidx) == 0) {
1240 is_cached = 1;
1241 break;
1244 if (is_cached)
1245 continue; /* already searched */
1247 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1248 path_packidx, 0);
1249 if (err)
1250 goto done;
1252 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1253 if (err)
1254 goto done;
1256 err = cache_packidx(repo, *packidx, path_packidx);
1257 if (err)
1258 goto done;
1260 *idx = got_packidx_get_object_idx(*packidx, id);
1261 if (*idx != -1) {
1262 err = NULL; /* found the object */
1263 goto done;
1267 err = got_error_no_obj(id);
1268 done:
1269 return err;
1272 const struct got_error *
1273 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1274 struct got_repository *repo)
1276 const struct got_error *err = NULL;
1277 DIR *packdir = NULL;
1278 struct dirent *dent;
1279 char *path_packidx = NULL;
1280 int packdir_fd;
1281 struct stat sb;
1283 packdir_fd = openat(got_repo_get_fd(repo),
1284 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1285 if (packdir_fd == -1) {
1286 return got_error_from_errno_fmt("openat: %s/%s",
1287 got_repo_get_path_git_dir(repo),
1288 GOT_OBJECTS_PACK_DIR);
1291 packdir = fdopendir(packdir_fd);
1292 if (packdir == NULL) {
1293 err = got_error_from_errno("fdopendir");
1294 goto done;
1297 if (fstat(packdir_fd, &sb) == -1) {
1298 err = got_error_from_errno("fstat");
1299 goto done;
1301 repo->pack_path_mtime = sb.st_mtime;
1303 while ((dent = readdir(packdir)) != NULL) {
1304 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1305 continue;
1307 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1308 dent->d_name) == -1) {
1309 err = got_error_from_errno("asprintf");
1310 path_packidx = NULL;
1311 break;
1314 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1315 if (err)
1316 break;
1318 done:
1319 if (err)
1320 free(path_packidx);
1321 if (packdir && closedir(packdir) != 0 && err == NULL)
1322 err = got_error_from_errno("closedir");
1323 return err;
1326 const struct got_error *
1327 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1328 struct got_repository *repo)
1330 const struct got_error *err;
1331 size_t i;
1333 *packidx = NULL;
1335 /* Search pack index cache. */
1336 for (i = 0; i < repo->pack_cache_size; i++) {
1337 if (repo->packidx_cache[i] == NULL)
1338 break;
1339 if (strcmp(repo->packidx_cache[i]->path_packidx,
1340 path_packidx) == 0) {
1341 *packidx = repo->packidx_cache[i];
1342 return NULL;
1345 /* No luck. Search the filesystem. */
1347 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1348 path_packidx, 0);
1349 if (err)
1350 return err;
1352 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1353 if (err)
1354 goto done;
1356 err = cache_packidx(repo, *packidx, path_packidx);
1357 done:
1358 if (err) {
1359 got_packidx_close(*packidx);
1360 *packidx = NULL;
1362 return err;
1365 static const struct got_error *
1366 read_packfile_hdr(int fd, struct got_packidx *packidx)
1368 const struct got_error *err = NULL;
1369 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1370 struct got_packfile_hdr hdr;
1371 ssize_t n;
1373 n = read(fd, &hdr, sizeof(hdr));
1374 if (n < 0)
1375 return got_error_from_errno("read");
1376 if (n != sizeof(hdr))
1377 return got_error(GOT_ERR_BAD_PACKFILE);
1379 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1380 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1381 be32toh(hdr.nobjects) != totobj)
1382 err = got_error(GOT_ERR_BAD_PACKFILE);
1384 return err;
1387 static const struct got_error *
1388 open_packfile(int *fd, struct got_repository *repo,
1389 const char *relpath, struct got_packidx *packidx)
1391 const struct got_error *err = NULL;
1393 *fd = openat(got_repo_get_fd(repo), relpath,
1394 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1395 if (*fd == -1)
1396 return got_error_from_errno_fmt("openat: %s/%s",
1397 got_repo_get_path_git_dir(repo), relpath);
1399 if (packidx) {
1400 err = read_packfile_hdr(*fd, packidx);
1401 if (err) {
1402 close(*fd);
1403 *fd = -1;
1407 return err;
1410 const struct got_error *
1411 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1412 const char *path_packfile, struct got_packidx *packidx)
1414 const struct got_error *err = NULL;
1415 struct got_pack *pack = NULL;
1416 struct stat sb;
1417 size_t i;
1419 if (packp)
1420 *packp = NULL;
1422 for (i = 0; i < repo->pack_cache_size; i++) {
1423 pack = &repo->packs[i];
1424 if (pack->path_packfile == NULL)
1425 break;
1426 if (strcmp(pack->path_packfile, path_packfile) == 0)
1427 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1430 if (i == repo->pack_cache_size) {
1431 struct got_pack tmp;
1432 do {
1433 i--;
1434 } while (i > 0 && repo->pinned_pack >= 0 &&
1435 i == repo->pinned_pack);
1436 err = got_pack_close(&repo->packs[i]);
1437 if (err)
1438 return err;
1439 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1440 return got_error_from_errno("ftruncate");
1441 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1442 return got_error_from_errno("ftruncate");
1443 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1444 memcpy(&repo->packs[i], &repo->packs[0],
1445 sizeof(repo->packs[i]));
1446 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1447 if (repo->pinned_pack == 0)
1448 repo->pinned_pack = i;
1449 else if (repo->pinned_pack == i)
1450 repo->pinned_pack = 0;
1451 i = 0;
1454 pack = &repo->packs[i];
1456 pack->path_packfile = strdup(path_packfile);
1457 if (pack->path_packfile == NULL) {
1458 err = got_error_from_errno("strdup");
1459 goto done;
1462 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1463 if (err)
1464 goto done;
1466 if (fstat(pack->fd, &sb) != 0) {
1467 err = got_error_from_errno("fstat");
1468 goto done;
1470 pack->filesize = sb.st_size;
1472 pack->privsep_child = NULL;
1474 #ifndef GOT_PACK_NO_MMAP
1475 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1476 pack->fd, 0);
1477 if (pack->map == MAP_FAILED) {
1478 if (errno != ENOMEM) {
1479 err = got_error_from_errno("mmap");
1480 goto done;
1482 pack->map = NULL; /* fall back to read(2) */
1484 #endif
1485 done:
1486 if (err) {
1487 if (pack) {
1488 free(pack->path_packfile);
1489 memset(pack, 0, sizeof(*pack));
1491 } else if (packp)
1492 *packp = pack;
1493 return err;
1496 struct got_pack *
1497 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1499 struct got_pack *pack = NULL;
1500 size_t i;
1502 for (i = 0; i < repo->pack_cache_size; i++) {
1503 pack = &repo->packs[i];
1504 if (pack->path_packfile == NULL)
1505 break;
1506 if (strcmp(pack->path_packfile, path_packfile) == 0)
1507 return pack;
1510 return NULL;
1513 const struct got_error *
1514 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1515 struct got_pack *pack)
1517 size_t i;
1518 int pinned_pack = -1, pinned_packidx = -1;
1520 for (i = 0; i < repo->pack_cache_size; i++) {
1521 if (repo->packidx_cache[i] &&
1522 strcmp(repo->packidx_cache[i]->path_packidx,
1523 packidx->path_packidx) == 0)
1524 pinned_packidx = i;
1525 if (repo->packs[i].path_packfile &&
1526 strcmp(repo->packs[i].path_packfile,
1527 pack->path_packfile) == 0)
1528 pinned_pack = i;
1531 if (pinned_packidx == -1 || pinned_pack == -1)
1532 return got_error(GOT_ERR_PIN_PACK);
1534 repo->pinned_pack = pinned_pack;
1535 repo->pinned_packidx = pinned_packidx;
1536 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1537 return NULL;
1540 struct got_pack *
1541 got_repo_get_pinned_pack(struct got_repository *repo)
1543 if (repo->pinned_pack >= 0 &&
1544 repo->pinned_pack < repo->pack_cache_size)
1545 return &repo->packs[repo->pinned_pack];
1547 return NULL;
1550 void
1551 got_repo_unpin_pack(struct got_repository *repo)
1553 repo->pinned_packidx = -1;
1554 repo->pinned_pack = -1;
1555 repo->pinned_pid = 0;
1558 const struct got_error *
1559 got_repo_init(const char *repo_path)
1561 const struct got_error *err = NULL;
1562 const char *dirnames[] = {
1563 GOT_OBJECTS_DIR,
1564 GOT_OBJECTS_PACK_DIR,
1565 GOT_REFS_DIR,
1567 const char *description_str = "Unnamed repository; "
1568 "edit this file 'description' to name the repository.";
1569 const char *headref_str = "ref: refs/heads/main";
1570 const char *gitconfig_str = "[core]\n"
1571 "\trepositoryformatversion = 0\n"
1572 "\tfilemode = true\n"
1573 "\tbare = true\n";
1574 char *path;
1575 size_t i;
1577 if (!got_path_dir_is_empty(repo_path))
1578 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1580 for (i = 0; i < nitems(dirnames); i++) {
1581 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1582 return got_error_from_errno("asprintf");
1584 err = got_path_mkdir(path);
1585 free(path);
1586 if (err)
1587 return err;
1590 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1591 return got_error_from_errno("asprintf");
1592 err = got_path_create_file(path, description_str);
1593 free(path);
1594 if (err)
1595 return err;
1597 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1598 return got_error_from_errno("asprintf");
1599 err = got_path_create_file(path, headref_str);
1600 free(path);
1601 if (err)
1602 return err;
1604 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1605 return got_error_from_errno("asprintf");
1606 err = got_path_create_file(path, gitconfig_str);
1607 free(path);
1608 if (err)
1609 return err;
1611 return NULL;
1614 static void
1615 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1617 struct got_pathlist_entry *pe;
1619 while (!TAILQ_EMPTY(packidx_paths)) {
1620 pe = TAILQ_FIRST(packidx_paths);
1621 TAILQ_REMOVE(packidx_paths, pe, entry);
1622 free((char *)pe->path);
1623 free(pe);
1627 static const struct got_error *
1628 match_packed_object(struct got_object_id **unique_id,
1629 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1631 const struct got_error *err = NULL;
1632 struct got_object_id_queue matched_ids;
1633 struct got_pathlist_entry *pe;
1634 struct stat sb;
1636 STAILQ_INIT(&matched_ids);
1638 if (stat(got_repo_get_path_objects_pack(repo), &sb) == -1) {
1639 if (errno != ENOENT)
1640 return got_error_from_errno2("stat",
1641 got_repo_get_path_objects_pack(repo));
1642 } else if (sb.st_mtime != repo->pack_path_mtime) {
1643 purge_packidx_paths(&repo->packidx_paths);
1644 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1645 if (err)
1646 return err;
1649 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1650 const char *path_packidx = pe->path;
1651 struct got_packidx *packidx;
1652 struct got_object_qid *qid;
1654 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1655 path_packidx, 0);
1656 if (err)
1657 break;
1659 err = got_packidx_match_id_str_prefix(&matched_ids,
1660 packidx, id_str_prefix);
1661 if (err) {
1662 got_packidx_close(packidx);
1663 break;
1665 err = got_packidx_close(packidx);
1666 if (err)
1667 break;
1669 STAILQ_FOREACH(qid, &matched_ids, entry) {
1670 if (obj_type != GOT_OBJ_TYPE_ANY) {
1671 int matched_type;
1672 err = got_object_get_type(&matched_type, repo,
1673 &qid->id);
1674 if (err)
1675 goto done;
1676 if (matched_type != obj_type)
1677 continue;
1679 if (*unique_id == NULL) {
1680 *unique_id = got_object_id_dup(&qid->id);
1681 if (*unique_id == NULL) {
1682 err = got_error_from_errno("malloc");
1683 goto done;
1685 } else {
1686 if (got_object_id_cmp(*unique_id,
1687 &qid->id) == 0)
1688 continue; /* packed multiple times */
1689 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1690 goto done;
1694 done:
1695 got_object_id_queue_free(&matched_ids);
1696 if (err) {
1697 free(*unique_id);
1698 *unique_id = NULL;
1700 return err;
1703 static const struct got_error *
1704 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1705 const char *object_dir, const char *id_str_prefix, int obj_type,
1706 struct got_repository *repo)
1708 const struct got_error *err = NULL;
1709 char *path, *id_str = NULL;
1710 DIR *dir = NULL;
1711 struct dirent *dent;
1712 struct got_object_id id;
1714 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1715 err = got_error_from_errno("asprintf");
1716 goto done;
1719 dir = opendir(path);
1720 if (dir == NULL) {
1721 if (errno == ENOENT) {
1722 err = NULL;
1723 goto done;
1725 err = got_error_from_errno2("opendir", path);
1726 goto done;
1728 while ((dent = readdir(dir)) != NULL) {
1729 int cmp;
1731 free(id_str);
1732 id_str = NULL;
1734 if (strcmp(dent->d_name, ".") == 0 ||
1735 strcmp(dent->d_name, "..") == 0)
1736 continue;
1738 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1739 err = got_error_from_errno("asprintf");
1740 goto done;
1743 if (!got_parse_sha1_digest(id.sha1, id_str))
1744 continue;
1747 * Directory entries do not necessarily appear in
1748 * sorted order, so we must iterate over all of them.
1750 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1751 if (cmp != 0)
1752 continue;
1754 if (*unique_id == NULL) {
1755 if (obj_type != GOT_OBJ_TYPE_ANY) {
1756 int matched_type;
1757 err = got_object_get_type(&matched_type, repo,
1758 &id);
1759 if (err)
1760 goto done;
1761 if (matched_type != obj_type)
1762 continue;
1764 *unique_id = got_object_id_dup(&id);
1765 if (*unique_id == NULL) {
1766 err = got_error_from_errno("got_object_id_dup");
1767 goto done;
1769 } else {
1770 if (got_object_id_cmp(*unique_id, &id) == 0)
1771 continue; /* both packed and loose */
1772 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1773 goto done;
1776 done:
1777 if (dir && closedir(dir) != 0 && err == NULL)
1778 err = got_error_from_errno("closedir");
1779 if (err) {
1780 free(*unique_id);
1781 *unique_id = NULL;
1783 free(id_str);
1784 free(path);
1785 return err;
1788 const struct got_error *
1789 got_repo_match_object_id_prefix(struct got_object_id **id,
1790 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1792 const struct got_error *err = NULL;
1793 char *path_objects = got_repo_get_path_objects(repo);
1794 char *object_dir = NULL;
1795 size_t len;
1796 int i;
1798 *id = NULL;
1800 len = strlen(id_str_prefix);
1801 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1802 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1804 for (i = 0; i < len; i++) {
1805 if (isxdigit((unsigned char)id_str_prefix[i]))
1806 continue;
1807 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1810 if (len >= 2) {
1811 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1812 if (err)
1813 goto done;
1814 object_dir = strndup(id_str_prefix, 2);
1815 if (object_dir == NULL) {
1816 err = got_error_from_errno("strdup");
1817 goto done;
1819 err = match_loose_object(id, path_objects, object_dir,
1820 id_str_prefix, obj_type, repo);
1821 } else if (len == 1) {
1822 int i;
1823 for (i = 0; i < 0xf; i++) {
1824 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1825 == -1) {
1826 err = got_error_from_errno("asprintf");
1827 goto done;
1829 err = match_packed_object(id, repo, object_dir,
1830 obj_type);
1831 if (err)
1832 goto done;
1833 err = match_loose_object(id, path_objects, object_dir,
1834 id_str_prefix, obj_type, repo);
1835 if (err)
1836 goto done;
1838 } else {
1839 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1840 goto done;
1842 done:
1843 free(object_dir);
1844 if (err) {
1845 free(*id);
1846 *id = NULL;
1847 } else if (*id == NULL) {
1848 switch (obj_type) {
1849 case GOT_OBJ_TYPE_BLOB:
1850 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1851 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1852 break;
1853 case GOT_OBJ_TYPE_TREE:
1854 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1855 GOT_OBJ_LABEL_TREE, id_str_prefix);
1856 break;
1857 case GOT_OBJ_TYPE_COMMIT:
1858 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1859 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1860 break;
1861 case GOT_OBJ_TYPE_TAG:
1862 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1863 GOT_OBJ_LABEL_TAG, id_str_prefix);
1864 break;
1865 default:
1866 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1867 break;
1871 return err;
1874 const struct got_error *
1875 got_repo_match_object_id(struct got_object_id **id, char **label,
1876 const char *id_str, int obj_type, struct got_reflist_head *refs,
1877 struct got_repository *repo)
1879 const struct got_error *err;
1880 struct got_tag_object *tag;
1881 struct got_reference *ref = NULL;
1883 *id = NULL;
1884 if (label)
1885 *label = NULL;
1887 if (refs) {
1888 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1889 refs, repo);
1890 if (err == NULL) {
1891 *id = got_object_id_dup(
1892 got_object_tag_get_object_id(tag));
1893 if (*id == NULL)
1894 err = got_error_from_errno("got_object_id_dup");
1895 else if (label && asprintf(label, "refs/tags/%s",
1896 got_object_tag_get_name(tag)) == -1) {
1897 err = got_error_from_errno("asprintf");
1898 free(*id);
1899 *id = NULL;
1901 got_object_tag_close(tag);
1902 return err;
1903 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1904 err->code != GOT_ERR_NO_OBJ)
1905 return err;
1908 err = got_ref_open(&ref, repo, id_str, 0);
1909 if (err == NULL) {
1910 err = got_ref_resolve(id, repo, ref);
1911 if (err)
1912 goto done;
1913 if (label) {
1914 *label = strdup(got_ref_get_name(ref));
1915 if (*label == NULL) {
1916 err = got_error_from_errno("strdup");
1917 goto done;
1920 } else {
1921 if (err->code != GOT_ERR_NOT_REF &&
1922 err->code != GOT_ERR_BAD_REF_NAME)
1923 goto done;
1924 err = got_repo_match_object_id_prefix(id, id_str,
1925 obj_type, repo);
1926 if (err) {
1927 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1928 err = got_error_not_ref(id_str);
1929 goto done;
1931 if (label) {
1932 err = got_object_id_str(label, *id);
1933 if (*label == NULL) {
1934 err = got_error_from_errno("strdup");
1935 goto done;
1939 done:
1940 if (ref)
1941 got_ref_close(ref);
1942 return err;
1945 const struct got_error *
1946 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1947 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1949 const struct got_error *err = NULL;
1950 struct got_reflist_entry *re;
1951 struct got_object_id *tag_id;
1952 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1954 *tag = NULL;
1956 TAILQ_FOREACH(re, refs, entry) {
1957 const char *refname;
1958 refname = got_ref_get_name(re->ref);
1959 if (got_ref_is_symbolic(re->ref))
1960 continue;
1961 if (strncmp(refname, "refs/tags/", 10) != 0)
1962 continue;
1963 if (!name_is_absolute)
1964 refname += strlen("refs/tags/");
1965 if (strcmp(refname, name) != 0)
1966 continue;
1967 err = got_ref_resolve(&tag_id, repo, re->ref);
1968 if (err)
1969 break;
1970 err = got_object_open_as_tag(tag, repo, tag_id);
1971 free(tag_id);
1972 if (err)
1973 break;
1974 if (obj_type == GOT_OBJ_TYPE_ANY ||
1975 got_object_tag_get_object_type(*tag) == obj_type)
1976 break;
1977 got_object_tag_close(*tag);
1978 *tag = NULL;
1981 if (err == NULL && *tag == NULL)
1982 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1983 GOT_OBJ_LABEL_TAG, name);
1984 return err;
1987 static const struct got_error *
1988 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1989 const char *name, mode_t mode, struct got_object_id *blob_id)
1991 const struct got_error *err = NULL;
1993 *new_te = NULL;
1995 *new_te = calloc(1, sizeof(**new_te));
1996 if (*new_te == NULL)
1997 return got_error_from_errno("calloc");
1999 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2000 sizeof((*new_te)->name)) {
2001 err = got_error(GOT_ERR_NO_SPACE);
2002 goto done;
2005 if (S_ISLNK(mode)) {
2006 (*new_te)->mode = S_IFLNK;
2007 } else {
2008 (*new_te)->mode = S_IFREG;
2009 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2011 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2012 done:
2013 if (err && *new_te) {
2014 free(*new_te);
2015 *new_te = NULL;
2017 return err;
2020 static const struct got_error *
2021 import_file(struct got_tree_entry **new_te, struct dirent *de,
2022 const char *path, struct got_repository *repo)
2024 const struct got_error *err;
2025 struct got_object_id *blob_id = NULL;
2026 char *filepath;
2027 struct stat sb;
2029 if (asprintf(&filepath, "%s%s%s", path,
2030 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2031 return got_error_from_errno("asprintf");
2033 if (lstat(filepath, &sb) != 0) {
2034 err = got_error_from_errno2("lstat", path);
2035 goto done;
2038 err = got_object_blob_create(&blob_id, filepath, repo);
2039 if (err)
2040 goto done;
2042 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2043 blob_id);
2044 done:
2045 free(filepath);
2046 if (err)
2047 free(blob_id);
2048 return err;
2051 static const struct got_error *
2052 insert_tree_entry(struct got_tree_entry *new_te,
2053 struct got_pathlist_head *paths)
2055 const struct got_error *err = NULL;
2056 struct got_pathlist_entry *new_pe;
2058 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2059 if (err)
2060 return err;
2061 if (new_pe == NULL)
2062 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2063 return NULL;
2066 static const struct got_error *write_tree(struct got_object_id **,
2067 const char *, struct got_pathlist_head *, struct got_repository *,
2068 got_repo_import_cb progress_cb, void *progress_arg);
2070 static const struct got_error *
2071 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2072 const char *path, struct got_pathlist_head *ignores,
2073 struct got_repository *repo,
2074 got_repo_import_cb progress_cb, void *progress_arg)
2076 const struct got_error *err;
2077 struct got_object_id *id = NULL;
2078 char *subdirpath;
2080 if (asprintf(&subdirpath, "%s%s%s", path,
2081 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2082 return got_error_from_errno("asprintf");
2084 (*new_te) = calloc(1, sizeof(**new_te));
2085 if (*new_te == NULL)
2086 return got_error_from_errno("calloc");
2087 (*new_te)->mode = S_IFDIR;
2088 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2089 sizeof((*new_te)->name)) {
2090 err = got_error(GOT_ERR_NO_SPACE);
2091 goto done;
2093 err = write_tree(&id, subdirpath, ignores, repo,
2094 progress_cb, progress_arg);
2095 if (err)
2096 goto done;
2097 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2099 done:
2100 free(id);
2101 free(subdirpath);
2102 if (err) {
2103 free(*new_te);
2104 *new_te = NULL;
2106 return err;
2109 static const struct got_error *
2110 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2111 struct got_pathlist_head *ignores, struct got_repository *repo,
2112 got_repo_import_cb progress_cb, void *progress_arg)
2114 const struct got_error *err = NULL;
2115 DIR *dir;
2116 struct dirent *de;
2117 int nentries;
2118 struct got_tree_entry *new_te = NULL;
2119 struct got_pathlist_head paths;
2120 struct got_pathlist_entry *pe;
2122 *new_tree_id = NULL;
2124 TAILQ_INIT(&paths);
2126 dir = opendir(path_dir);
2127 if (dir == NULL) {
2128 err = got_error_from_errno2("opendir", path_dir);
2129 goto done;
2132 nentries = 0;
2133 while ((de = readdir(dir)) != NULL) {
2134 int ignore = 0;
2135 int type;
2137 if (strcmp(de->d_name, ".") == 0 ||
2138 strcmp(de->d_name, "..") == 0)
2139 continue;
2141 TAILQ_FOREACH(pe, ignores, entry) {
2142 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2143 ignore = 1;
2144 break;
2147 if (ignore)
2148 continue;
2150 err = got_path_dirent_type(&type, path_dir, de);
2151 if (err)
2152 goto done;
2154 if (type == DT_DIR) {
2155 err = import_subdir(&new_te, de, path_dir,
2156 ignores, repo, progress_cb, progress_arg);
2157 if (err) {
2158 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2159 goto done;
2160 err = NULL;
2161 continue;
2163 } else if (type == DT_REG || type == DT_LNK) {
2164 err = import_file(&new_te, de, path_dir, repo);
2165 if (err)
2166 goto done;
2167 } else
2168 continue;
2170 err = insert_tree_entry(new_te, &paths);
2171 if (err)
2172 goto done;
2173 nentries++;
2176 if (TAILQ_EMPTY(&paths)) {
2177 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2178 "cannot create tree without any entries");
2179 goto done;
2182 TAILQ_FOREACH(pe, &paths, entry) {
2183 struct got_tree_entry *te = pe->data;
2184 char *path;
2185 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2186 continue;
2187 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2188 err = got_error_from_errno("asprintf");
2189 goto done;
2191 err = (*progress_cb)(progress_arg, path);
2192 free(path);
2193 if (err)
2194 goto done;
2197 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2198 done:
2199 if (dir)
2200 closedir(dir);
2201 got_pathlist_free(&paths);
2202 return err;
2205 const struct got_error *
2206 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2207 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2208 struct got_repository *repo, got_repo_import_cb progress_cb,
2209 void *progress_arg)
2211 const struct got_error *err;
2212 struct got_object_id *new_tree_id;
2214 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2215 progress_cb, progress_arg);
2216 if (err)
2217 return err;
2219 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2220 author, time(NULL), author, time(NULL), logmsg, repo);
2221 free(new_tree_id);
2222 return err;
2225 const struct got_error *
2226 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2227 struct got_repository *repo)
2229 const struct got_error *err = NULL;
2230 char *path_objects = NULL, *path = NULL;
2231 DIR *dir = NULL;
2232 struct got_object_id id;
2233 int i;
2235 *nobjects = 0;
2236 *ondisk_size = 0;
2238 path_objects = got_repo_get_path_objects(repo);
2239 if (path_objects == NULL)
2240 return got_error_from_errno("got_repo_get_path_objects");
2242 for (i = 0; i <= 0xff; i++) {
2243 struct dirent *dent;
2245 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2246 err = got_error_from_errno("asprintf");
2247 break;
2250 dir = opendir(path);
2251 if (dir == NULL) {
2252 if (errno == ENOENT) {
2253 err = NULL;
2254 continue;
2256 err = got_error_from_errno2("opendir", path);
2257 break;
2260 while ((dent = readdir(dir)) != NULL) {
2261 char *id_str;
2262 int fd;
2263 struct stat sb;
2265 if (strcmp(dent->d_name, ".") == 0 ||
2266 strcmp(dent->d_name, "..") == 0)
2267 continue;
2269 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2270 err = got_error_from_errno("asprintf");
2271 goto done;
2274 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2275 free(id_str);
2276 continue;
2278 free(id_str);
2280 err = got_object_open_loose_fd(&fd, &id, repo);
2281 if (err)
2282 goto done;
2284 if (fstat(fd, &sb) == -1) {
2285 err = got_error_from_errno("fstat");
2286 close(fd);
2287 goto done;
2289 (*nobjects)++;
2290 (*ondisk_size) += sb.st_size;
2292 if (close(fd) == -1) {
2293 err = got_error_from_errno("close");
2294 goto done;
2298 if (closedir(dir) != 0) {
2299 err = got_error_from_errno("closedir");
2300 goto done;
2302 dir = NULL;
2304 free(path);
2305 path = NULL;
2307 done:
2308 if (dir && closedir(dir) != 0 && err == NULL)
2309 err = got_error_from_errno("closedir");
2311 if (err) {
2312 *nobjects = 0;
2313 *ondisk_size = 0;
2315 free(path_objects);
2316 free(path);
2317 return err;
2320 const struct got_error *
2321 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2322 off_t *total_packsize, struct got_repository *repo)
2324 const struct got_error *err = NULL;
2325 DIR *packdir = NULL;
2326 struct dirent *dent;
2327 struct got_packidx *packidx = NULL;
2328 char *path_packidx;
2329 char *path_packfile;
2330 int packdir_fd;
2331 struct stat sb;
2333 *npackfiles = 0;
2334 *nobjects = 0;
2335 *total_packsize = 0;
2337 packdir_fd = openat(got_repo_get_fd(repo),
2338 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2339 if (packdir_fd == -1) {
2340 return got_error_from_errno_fmt("openat: %s/%s",
2341 got_repo_get_path_git_dir(repo),
2342 GOT_OBJECTS_PACK_DIR);
2345 packdir = fdopendir(packdir_fd);
2346 if (packdir == NULL) {
2347 err = got_error_from_errno("fdopendir");
2348 goto done;
2351 while ((dent = readdir(packdir)) != NULL) {
2352 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2353 continue;
2355 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2356 dent->d_name) == -1) {
2357 err = got_error_from_errno("asprintf");
2358 goto done;
2361 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2362 path_packidx, 0);
2363 free(path_packidx);
2364 if (err)
2365 goto done;
2367 if (fstat(packidx->fd, &sb) == -1)
2368 goto done;
2369 *total_packsize += sb.st_size;
2371 err = got_packidx_get_packfile_path(&path_packfile,
2372 packidx->path_packidx);
2373 if (err)
2374 goto done;
2376 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2377 0) == -1) {
2378 free(path_packfile);
2379 goto done;
2381 free(path_packfile);
2382 *total_packsize += sb.st_size;
2384 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2386 (*npackfiles)++;
2388 got_packidx_close(packidx);
2389 packidx = NULL;
2391 done:
2392 if (packidx)
2393 got_packidx_close(packidx);
2394 if (packdir && closedir(packdir) != 0 && err == NULL)
2395 err = got_error_from_errno("closedir");
2396 if (err) {
2397 *npackfiles = 0;
2398 *nobjects = 0;
2399 *total_packsize = 0;
2401 return err;
2404 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2405 got_packidx_bloom_filter_cmp);