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, *pack_fds_tmp;
257 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
258 if (pack_fds_tmp == NULL)
259 return got_error_from_errno("calloc");
260 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
261 if (*pack_fds == NULL) {
262 free(pack_fds_tmp);
263 return got_error_from_errno("calloc");
266 /*
267 * got_repo_pack_fds_close will try to close all of the
268 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
269 * a value from got_opentempfd(), which would result in a close(0) if
270 * we do not initialize to -1 here.
271 */
272 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
273 pack_fds_tmp[i] = -1;
275 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
276 pack_fds_tmp[i] = got_opentempfd();
277 if (pack_fds_tmp[i] == -1) {
278 err = got_error_from_errno("got_opentempfd");
279 got_repo_pack_fds_close(pack_fds_tmp);
280 return err;
283 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
284 return err;
287 const struct got_error *
288 got_repo_pack_fds_close(int *pack_fds)
290 const struct got_error *err = NULL;
291 int i;
293 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
294 if (pack_fds[i] == -1)
295 continue;
296 if (close(pack_fds[i]) == -1) {
297 err = got_error_from_errno("close");
298 break;
301 free(pack_fds);
302 return err;
305 const struct got_error *
306 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
307 struct got_object *obj)
309 #ifndef GOT_NO_OBJ_CACHE
310 const struct got_error *err = NULL;
311 err = got_object_cache_add(&repo->objcache, id, obj);
312 if (err) {
313 if (err->code == GOT_ERR_OBJ_EXISTS ||
314 err->code == GOT_ERR_OBJ_TOO_LARGE)
315 err = NULL;
316 return err;
318 obj->refcnt++;
319 #endif
320 return NULL;
323 struct got_object *
324 got_repo_get_cached_object(struct got_repository *repo,
325 struct got_object_id *id)
327 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
330 const struct got_error *
331 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
332 struct got_tree_object *tree)
334 #ifndef GOT_NO_OBJ_CACHE
335 const struct got_error *err = NULL;
336 err = got_object_cache_add(&repo->treecache, id, tree);
337 if (err) {
338 if (err->code == GOT_ERR_OBJ_EXISTS ||
339 err->code == GOT_ERR_OBJ_TOO_LARGE)
340 err = NULL;
341 return err;
343 tree->refcnt++;
344 #endif
345 return NULL;
348 struct got_tree_object *
349 got_repo_get_cached_tree(struct got_repository *repo,
350 struct got_object_id *id)
352 return (struct got_tree_object *)got_object_cache_get(
353 &repo->treecache, id);
356 const struct got_error *
357 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
358 struct got_commit_object *commit)
360 #ifndef GOT_NO_OBJ_CACHE
361 const struct got_error *err = NULL;
362 err = got_object_cache_add(&repo->commitcache, id, commit);
363 if (err) {
364 if (err->code == GOT_ERR_OBJ_EXISTS ||
365 err->code == GOT_ERR_OBJ_TOO_LARGE)
366 err = NULL;
367 return err;
369 commit->refcnt++;
370 #endif
371 return NULL;
374 struct got_commit_object *
375 got_repo_get_cached_commit(struct got_repository *repo,
376 struct got_object_id *id)
378 return (struct got_commit_object *)got_object_cache_get(
379 &repo->commitcache, id);
382 const struct got_error *
383 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
384 struct got_tag_object *tag)
386 #ifndef GOT_NO_OBJ_CACHE
387 const struct got_error *err = NULL;
388 err = got_object_cache_add(&repo->tagcache, id, tag);
389 if (err) {
390 if (err->code == GOT_ERR_OBJ_EXISTS ||
391 err->code == GOT_ERR_OBJ_TOO_LARGE)
392 err = NULL;
393 return err;
395 tag->refcnt++;
396 #endif
397 return NULL;
400 struct got_tag_object *
401 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
403 return (struct got_tag_object *)got_object_cache_get(
404 &repo->tagcache, id);
407 const struct got_error *
408 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
409 struct got_raw_object *raw)
411 #ifndef GOT_NO_OBJ_CACHE
412 const struct got_error *err = NULL;
413 err = got_object_cache_add(&repo->rawcache, id, raw);
414 if (err) {
415 if (err->code == GOT_ERR_OBJ_EXISTS ||
416 err->code == GOT_ERR_OBJ_TOO_LARGE)
417 err = NULL;
418 return err;
420 raw->refcnt++;
421 #endif
422 return NULL;
426 struct got_raw_object *
427 got_repo_get_cached_raw_object(struct got_repository *repo,
428 struct got_object_id *id)
430 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
434 static const struct got_error *
435 open_repo(struct got_repository *repo, const char *path)
437 const struct got_error *err = NULL;
439 repo->gitdir_fd = -1;
441 /* bare git repository? */
442 repo->path_git_dir = strdup(path);
443 if (repo->path_git_dir == NULL)
444 return got_error_from_errno("strdup");
445 if (is_git_repo(repo)) {
446 repo->path = strdup(repo->path_git_dir);
447 if (repo->path == NULL) {
448 err = got_error_from_errno("strdup");
449 goto done;
451 repo->gitdir_fd = open(repo->path_git_dir,
452 O_DIRECTORY | O_CLOEXEC);
453 if (repo->gitdir_fd == -1) {
454 err = got_error_from_errno2("open",
455 repo->path_git_dir);
456 goto done;
458 return NULL;
461 /* git repository with working tree? */
462 free(repo->path_git_dir);
463 repo->path_git_dir = NULL;
464 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
465 err = got_error_from_errno("asprintf");
466 goto done;
468 if (is_git_repo(repo)) {
469 repo->path = strdup(path);
470 if (repo->path == NULL) {
471 err = got_error_from_errno("strdup");
472 goto done;
474 repo->gitdir_fd = open(repo->path_git_dir,
475 O_DIRECTORY | O_CLOEXEC);
476 if (repo->gitdir_fd == -1) {
477 err = got_error_from_errno2("open",
478 repo->path_git_dir);
479 goto done;
481 return NULL;
484 err = got_error(GOT_ERR_NOT_GIT_REPO);
485 done:
486 if (err) {
487 free(repo->path);
488 repo->path = NULL;
489 free(repo->path_git_dir);
490 repo->path_git_dir = NULL;
491 if (repo->gitdir_fd != -1)
492 close(repo->gitdir_fd);
493 repo->gitdir_fd = -1;
496 return err;
499 static const struct got_error *
500 parse_gitconfig_file(int *gitconfig_repository_format_version,
501 char **gitconfig_author_name, char **gitconfig_author_email,
502 struct got_remote_repo **remotes, int *nremotes,
503 char **gitconfig_owner, char ***extensions, int *nextensions,
504 const char *gitconfig_path)
506 const struct got_error *err = NULL, *child_err = NULL;
507 int fd = -1;
508 int imsg_fds[2] = { -1, -1 };
509 pid_t pid;
510 struct imsgbuf *ibuf;
512 *gitconfig_repository_format_version = 0;
513 if (extensions)
514 *extensions = NULL;
515 if (nextensions)
516 *nextensions = 0;
517 *gitconfig_author_name = NULL;
518 *gitconfig_author_email = NULL;
519 if (remotes)
520 *remotes = NULL;
521 if (nremotes)
522 *nremotes = 0;
523 if (gitconfig_owner)
524 *gitconfig_owner = NULL;
526 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
527 if (fd == -1) {
528 if (errno == ENOENT)
529 return NULL;
530 return got_error_from_errno2("open", gitconfig_path);
533 ibuf = calloc(1, sizeof(*ibuf));
534 if (ibuf == NULL) {
535 err = got_error_from_errno("calloc");
536 goto done;
539 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
540 err = got_error_from_errno("socketpair");
541 goto done;
544 pid = fork();
545 if (pid == -1) {
546 err = got_error_from_errno("fork");
547 goto done;
548 } else if (pid == 0) {
549 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
550 gitconfig_path);
551 /* not reached */
554 if (close(imsg_fds[1]) == -1) {
555 err = got_error_from_errno("close");
556 goto done;
558 imsg_fds[1] = -1;
559 imsg_init(ibuf, imsg_fds[0]);
561 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
562 if (err)
563 goto done;
564 fd = -1;
566 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
567 if (err)
568 goto done;
570 err = got_privsep_recv_gitconfig_int(
571 gitconfig_repository_format_version, ibuf);
572 if (err)
573 goto done;
575 if (extensions && nextensions) {
576 err = got_privsep_send_gitconfig_repository_extensions_req(
577 ibuf);
578 if (err)
579 goto done;
580 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
581 if (err)
582 goto done;
583 if (*nextensions > 0) {
584 int i;
585 *extensions = calloc(*nextensions, sizeof(char *));
586 if (*extensions == NULL) {
587 err = got_error_from_errno("calloc");
588 goto done;
590 for (i = 0; i < *nextensions; i++) {
591 char *ext;
592 err = got_privsep_recv_gitconfig_str(&ext,
593 ibuf);
594 if (err)
595 goto done;
596 (*extensions)[i] = ext;
601 err = got_privsep_send_gitconfig_author_name_req(ibuf);
602 if (err)
603 goto done;
605 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
606 if (err)
607 goto done;
609 err = got_privsep_send_gitconfig_author_email_req(ibuf);
610 if (err)
611 goto done;
613 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
614 if (err)
615 goto done;
617 if (remotes && nremotes) {
618 err = got_privsep_send_gitconfig_remotes_req(ibuf);
619 if (err)
620 goto done;
622 err = got_privsep_recv_gitconfig_remotes(remotes,
623 nremotes, ibuf);
624 if (err)
625 goto done;
628 if (gitconfig_owner) {
629 err = got_privsep_send_gitconfig_owner_req(ibuf);
630 if (err)
631 goto done;
632 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
633 if (err)
634 goto done;
637 err = got_privsep_send_stop(imsg_fds[0]);
638 child_err = got_privsep_wait_for_child(pid);
639 if (child_err && err == NULL)
640 err = child_err;
641 done:
642 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
643 err = got_error_from_errno("close");
644 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
645 err = got_error_from_errno("close");
646 if (fd != -1 && close(fd) == -1 && err == NULL)
647 err = got_error_from_errno2("close", gitconfig_path);
648 free(ibuf);
649 return err;
652 static const struct got_error *
653 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
655 const struct got_error *err = NULL;
656 char *repo_gitconfig_path = NULL;
658 if (global_gitconfig_path) {
659 /* Read settings from ~/.gitconfig. */
660 int dummy_repo_version;
661 err = parse_gitconfig_file(&dummy_repo_version,
662 &repo->global_gitconfig_author_name,
663 &repo->global_gitconfig_author_email,
664 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
665 if (err)
666 return err;
669 /* Read repository's .git/config file. */
670 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
671 if (repo_gitconfig_path == NULL)
672 return got_error_from_errno("got_repo_get_path_gitconfig");
674 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
675 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
676 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
677 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
678 repo_gitconfig_path);
679 if (err)
680 goto done;
682 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
683 int i;
685 for (i = 0; i < repo->ngitconfig_remotes; i++) {
686 got_repo_free_remote_repo_data(
687 &repo->gitconfig_remotes[i]);
689 free(repo->gitconfig_remotes);
690 repo->gitconfig_remotes = NULL;
691 repo->ngitconfig_remotes = 0;
693 free(repo->gitconfig_author_name);
694 repo->gitconfig_author_name = NULL;
695 free(repo->gitconfig_author_email);
696 repo->gitconfig_author_email = NULL;
698 free(repo->global_gitconfig_author_name);
699 repo->global_gitconfig_author_name = NULL;
700 free(repo->global_gitconfig_author_email);
701 repo->global_gitconfig_author_email = NULL;
704 done:
705 free(repo_gitconfig_path);
706 return err;
709 static const struct got_error *
710 read_gotconfig(struct got_repository *repo)
712 const struct got_error *err = NULL;
713 char *gotconfig_path;
715 gotconfig_path = got_repo_get_path_gotconfig(repo);
716 if (gotconfig_path == NULL)
717 return got_error_from_errno("got_repo_get_path_gotconfig");
719 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
720 free(gotconfig_path);
721 return err;
724 /* Supported repository format extensions. */
725 static const char *const repo_extensions[] = {
726 "noop", /* Got supports repository format version 1. */
727 "preciousObjects", /* Supported by gotadmin cleanup. */
728 "worktreeConfig", /* Got does not care about Git work trees. */
729 };
731 const struct got_error *
732 got_repo_open(struct got_repository **repop, const char *path,
733 const char *global_gitconfig_path, int *pack_fds)
735 struct got_repository *repo = NULL;
736 const struct got_error *err = NULL;
737 char *repo_path = NULL;
738 size_t i, j = 0;
739 struct rlimit rl;
741 *repop = NULL;
743 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
744 return got_error_from_errno("getrlimit");
746 repo = calloc(1, sizeof(*repo));
747 if (repo == NULL)
748 return got_error_from_errno("calloc");
750 RB_INIT(&repo->packidx_bloom_filters);
751 TAILQ_INIT(&repo->packidx_paths);
753 for (i = 0; i < nitems(repo->privsep_children); i++) {
754 memset(&repo->privsep_children[i], 0,
755 sizeof(repo->privsep_children[0]));
756 repo->privsep_children[i].imsg_fd = -1;
759 err = got_object_cache_init(&repo->objcache,
760 GOT_OBJECT_CACHE_TYPE_OBJ);
761 if (err)
762 goto done;
763 err = got_object_cache_init(&repo->treecache,
764 GOT_OBJECT_CACHE_TYPE_TREE);
765 if (err)
766 goto done;
767 err = got_object_cache_init(&repo->commitcache,
768 GOT_OBJECT_CACHE_TYPE_COMMIT);
769 if (err)
770 goto done;
771 err = got_object_cache_init(&repo->tagcache,
772 GOT_OBJECT_CACHE_TYPE_TAG);
773 if (err)
774 goto done;
775 err = got_object_cache_init(&repo->rawcache,
776 GOT_OBJECT_CACHE_TYPE_RAW);
777 if (err)
778 goto done;
780 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
781 if (repo->pack_cache_size > rl.rlim_cur / 8)
782 repo->pack_cache_size = rl.rlim_cur / 8;
783 for (i = 0; i < nitems(repo->packs); i++) {
784 if (i < repo->pack_cache_size) {
785 repo->packs[i].basefd = pack_fds[j++];
786 repo->packs[i].accumfd = pack_fds[j++];
787 } else {
788 repo->packs[i].basefd = -1;
789 repo->packs[i].accumfd = -1;
792 repo->pinned_pack = -1;
793 repo->pinned_packidx = -1;
794 repo->pinned_pid = 0;
796 repo_path = realpath(path, NULL);
797 if (repo_path == NULL) {
798 err = got_error_from_errno2("realpath", path);
799 goto done;
802 for (;;) {
803 char *parent_path;
805 err = open_repo(repo, repo_path);
806 if (err == NULL)
807 break;
808 if (err->code != GOT_ERR_NOT_GIT_REPO)
809 goto done;
810 if (repo_path[0] == '/' && repo_path[1] == '\0') {
811 err = got_error(GOT_ERR_NOT_GIT_REPO);
812 goto done;
814 err = got_path_dirname(&parent_path, repo_path);
815 if (err)
816 goto done;
817 free(repo_path);
818 repo_path = parent_path;
821 err = read_gotconfig(repo);
822 if (err)
823 goto done;
825 err = read_gitconfig(repo, global_gitconfig_path);
826 if (err)
827 goto done;
828 if (repo->gitconfig_repository_format_version != 0) {
829 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
830 goto done;
832 for (i = 0; i < repo->nextensions; i++) {
833 char *ext = repo->extensions[i];
834 int j, supported = 0;
835 for (j = 0; j < nitems(repo_extensions); j++) {
836 if (strcmp(ext, repo_extensions[j]) == 0) {
837 supported = 1;
838 break;
841 if (!supported) {
842 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
843 goto done;
847 err = got_repo_list_packidx(&repo->packidx_paths, repo);
848 done:
849 if (err)
850 got_repo_close(repo);
851 else
852 *repop = repo;
853 free(repo_path);
854 return err;
857 const struct got_error *
858 got_repo_close(struct got_repository *repo)
860 const struct got_error *err = NULL, *child_err;
861 struct got_packidx_bloom_filter *bf;
862 struct got_pathlist_entry *pe;
863 size_t i;
865 for (i = 0; i < repo->pack_cache_size; i++) {
866 if (repo->packidx_cache[i] == NULL)
867 break;
868 got_packidx_close(repo->packidx_cache[i]);
871 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
872 &repo->packidx_bloom_filters))) {
873 RB_REMOVE(got_packidx_bloom_filter_tree,
874 &repo->packidx_bloom_filters, bf);
875 bloom_free(bf->bloom);
876 free(bf->bloom);
877 free(bf);
880 for (i = 0; i < repo->pack_cache_size; i++)
881 if (repo->packs[i].path_packfile)
882 if (repo->packs[i].path_packfile)
883 got_pack_close(&repo->packs[i]);
885 free(repo->path);
886 free(repo->path_git_dir);
888 got_object_cache_close(&repo->objcache);
889 got_object_cache_close(&repo->treecache);
890 got_object_cache_close(&repo->commitcache);
891 got_object_cache_close(&repo->tagcache);
892 got_object_cache_close(&repo->rawcache);
894 for (i = 0; i < nitems(repo->privsep_children); i++) {
895 if (repo->privsep_children[i].imsg_fd == -1)
896 continue;
897 imsg_clear(repo->privsep_children[i].ibuf);
898 free(repo->privsep_children[i].ibuf);
899 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
900 child_err = got_privsep_wait_for_child(
901 repo->privsep_children[i].pid);
902 if (child_err && err == NULL)
903 err = child_err;
904 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
905 err == NULL)
906 err = got_error_from_errno("close");
909 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
910 err == NULL)
911 err = got_error_from_errno("close");
913 if (repo->gotconfig)
914 got_gotconfig_free(repo->gotconfig);
915 free(repo->gitconfig_author_name);
916 free(repo->gitconfig_author_email);
917 for (i = 0; i < repo->ngitconfig_remotes; i++)
918 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
919 free(repo->gitconfig_remotes);
920 for (i = 0; i < repo->nextensions; i++)
921 free(repo->extensions[i]);
922 free(repo->extensions);
924 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
925 free((void *)pe->path);
926 got_pathlist_free(&repo->packidx_paths);
927 free(repo);
929 return err;
932 void
933 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
935 int i;
937 free(repo->name);
938 repo->name = NULL;
939 free(repo->fetch_url);
940 repo->fetch_url = NULL;
941 free(repo->send_url);
942 repo->send_url = NULL;
943 for (i = 0; i < repo->nfetch_branches; i++)
944 free(repo->fetch_branches[i]);
945 free(repo->fetch_branches);
946 repo->fetch_branches = NULL;
947 repo->nfetch_branches = 0;
948 for (i = 0; i < repo->nsend_branches; i++)
949 free(repo->send_branches[i]);
950 free(repo->send_branches);
951 repo->send_branches = NULL;
952 repo->nsend_branches = 0;
955 const struct got_error *
956 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
957 const char *input_path)
959 const struct got_error *err = NULL;
960 const char *repo_abspath = NULL;
961 size_t repolen, len;
962 char *canonpath, *path = NULL;
964 *in_repo_path = NULL;
966 canonpath = strdup(input_path);
967 if (canonpath == NULL) {
968 err = got_error_from_errno("strdup");
969 goto done;
971 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
972 if (err)
973 goto done;
975 repo_abspath = got_repo_get_path(repo);
977 if (canonpath[0] == '\0') {
978 path = strdup(canonpath);
979 if (path == NULL) {
980 err = got_error_from_errno("strdup");
981 goto done;
983 } else {
984 path = realpath(canonpath, NULL);
985 if (path == NULL) {
986 if (errno != ENOENT) {
987 err = got_error_from_errno2("realpath",
988 canonpath);
989 goto done;
991 /*
992 * Path is not on disk.
993 * Assume it is already relative to repository root.
994 */
995 path = strdup(canonpath);
996 if (path == NULL) {
997 err = got_error_from_errno("strdup");
998 goto done;
1002 repolen = strlen(repo_abspath);
1003 len = strlen(path);
1006 if (strcmp(path, repo_abspath) == 0) {
1007 free(path);
1008 path = strdup("");
1009 if (path == NULL) {
1010 err = got_error_from_errno("strdup");
1011 goto done;
1013 } else if (len > repolen &&
1014 got_path_is_child(path, repo_abspath, repolen)) {
1015 /* Matched an on-disk path inside repository. */
1016 if (got_repo_is_bare(repo)) {
1018 * Matched an on-disk path inside repository
1019 * database. Treat input as repository-relative.
1021 free(path);
1022 path = canonpath;
1023 canonpath = NULL;
1024 } else {
1025 char *child;
1026 /* Strip common prefix with repository path. */
1027 err = got_path_skip_common_ancestor(&child,
1028 repo_abspath, path);
1029 if (err)
1030 goto done;
1031 free(path);
1032 path = child;
1034 } else {
1036 * Matched unrelated on-disk path.
1037 * Treat input as repository-relative.
1039 free(path);
1040 path = canonpath;
1041 canonpath = NULL;
1045 /* Make in-repository path absolute */
1046 if (path[0] != '/') {
1047 char *abspath;
1048 if (asprintf(&abspath, "/%s", path) == -1) {
1049 err = got_error_from_errno("asprintf");
1050 goto done;
1052 free(path);
1053 path = abspath;
1056 done:
1057 free(canonpath);
1058 if (err)
1059 free(path);
1060 else
1061 *in_repo_path = path;
1062 return err;
1065 static const struct got_error *
1066 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1067 const char *path_packidx)
1069 const struct got_error *err = NULL;
1070 size_t i;
1072 for (i = 0; i < repo->pack_cache_size; i++) {
1073 if (repo->packidx_cache[i] == NULL)
1074 break;
1075 if (strcmp(repo->packidx_cache[i]->path_packidx,
1076 path_packidx) == 0) {
1077 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1080 if (i == repo->pack_cache_size) {
1081 do {
1082 i--;
1083 } while (i > 0 && repo->pinned_packidx >= 0 &&
1084 i == repo->pinned_packidx);
1085 err = got_packidx_close(repo->packidx_cache[i]);
1086 if (err)
1087 return err;
1090 repo->packidx_cache[i] = packidx;
1092 return NULL;
1095 int
1096 got_repo_is_packidx_filename(const char *name, size_t len)
1098 if (len != GOT_PACKIDX_NAMELEN)
1099 return 0;
1101 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1102 return 0;
1104 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1105 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1106 return 0;
1108 return 1;
1111 static struct got_packidx_bloom_filter *
1112 get_packidx_bloom_filter(struct got_repository *repo,
1113 const char *path, size_t path_len)
1115 struct got_packidx_bloom_filter key;
1117 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1118 return NULL; /* XXX */
1119 key.path_len = path_len;
1121 return RB_FIND(got_packidx_bloom_filter_tree,
1122 &repo->packidx_bloom_filters, &key);
1125 int
1126 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1127 const char *path_packidx, struct got_object_id *id)
1129 struct got_packidx_bloom_filter *bf;
1131 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1132 if (bf)
1133 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1135 /* No bloom filter means this pack index must be searched. */
1136 return 1;
1139 static const struct got_error *
1140 add_packidx_bloom_filter(struct got_repository *repo,
1141 struct got_packidx *packidx, const char *path_packidx)
1143 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1144 struct got_packidx_bloom_filter *bf;
1145 size_t len;
1148 * Don't use bloom filters for very large pack index files.
1149 * Large pack files will contain a relatively large fraction
1150 * of our objects so we will likely need to visit them anyway.
1151 * The more objects a pack file contains the higher the probability
1152 * of a false-positive match from the bloom filter. And reading
1153 * all object IDs from a large pack index file can be expensive.
1155 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1156 return NULL;
1158 /* Do we already have a filter for this pack index? */
1159 if (get_packidx_bloom_filter(repo, path_packidx,
1160 strlen(path_packidx)) != NULL)
1161 return NULL;
1163 bf = calloc(1, sizeof(*bf));
1164 if (bf == NULL)
1165 return got_error_from_errno("calloc");
1166 bf->bloom = calloc(1, sizeof(*bf->bloom));
1167 if (bf->bloom == NULL) {
1168 free(bf);
1169 return got_error_from_errno("calloc");
1172 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1173 if (len >= sizeof(bf->path)) {
1174 free(bf->bloom);
1175 free(bf);
1176 return got_error(GOT_ERR_NO_SPACE);
1178 bf->path_len = len;
1180 /* Minimum size supported by our bloom filter is 1000 entries. */
1181 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1182 for (i = 0; i < nobjects; i++) {
1183 struct got_packidx_object_id *id;
1184 id = &packidx->hdr.sorted_ids[i];
1185 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1188 RB_INSERT(got_packidx_bloom_filter_tree,
1189 &repo->packidx_bloom_filters, bf);
1190 return NULL;
1193 const struct got_error *
1194 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1195 struct got_repository *repo, struct got_object_id *id)
1197 const struct got_error *err;
1198 struct got_pathlist_entry *pe;
1199 size_t i;
1201 /* Search pack index cache. */
1202 for (i = 0; i < repo->pack_cache_size; i++) {
1203 if (repo->packidx_cache[i] == NULL)
1204 break;
1205 if (!got_repo_check_packidx_bloom_filter(repo,
1206 repo->packidx_cache[i]->path_packidx, id))
1207 continue; /* object will not be found in this index */
1208 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1209 if (*idx != -1) {
1210 *packidx = repo->packidx_cache[i];
1212 * Move this cache entry to the front. Repeatedly
1213 * searching a wrong pack index can be expensive.
1215 if (i > 0) {
1216 memmove(&repo->packidx_cache[1],
1217 &repo->packidx_cache[0],
1218 i * sizeof(repo->packidx_cache[0]));
1219 repo->packidx_cache[0] = *packidx;
1220 if (repo->pinned_packidx >= 0 &&
1221 repo->pinned_packidx < i)
1222 repo->pinned_packidx++;
1223 else if (repo->pinned_packidx == i)
1224 repo->pinned_packidx = 0;
1226 return NULL;
1229 /* No luck. Search the filesystem. */
1231 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1232 const char *path_packidx = pe->path;
1233 int is_cached = 0;
1235 if (!got_repo_check_packidx_bloom_filter(repo,
1236 pe->path, id))
1237 continue; /* object will not be found in this index */
1239 for (i = 0; i < repo->pack_cache_size; i++) {
1240 if (repo->packidx_cache[i] == NULL)
1241 break;
1242 if (strcmp(repo->packidx_cache[i]->path_packidx,
1243 path_packidx) == 0) {
1244 is_cached = 1;
1245 break;
1248 if (is_cached)
1249 continue; /* already searched */
1251 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1252 path_packidx, 0);
1253 if (err)
1254 goto done;
1256 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1257 if (err)
1258 goto done;
1260 err = cache_packidx(repo, *packidx, path_packidx);
1261 if (err)
1262 goto done;
1264 *idx = got_packidx_get_object_idx(*packidx, id);
1265 if (*idx != -1) {
1266 err = NULL; /* found the object */
1267 goto done;
1271 err = got_error_no_obj(id);
1272 done:
1273 return err;
1276 const struct got_error *
1277 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1278 struct got_repository *repo)
1280 const struct got_error *err = NULL;
1281 DIR *packdir = NULL;
1282 struct dirent *dent;
1283 char *path_packidx = NULL;
1284 int packdir_fd;
1285 struct stat sb;
1287 packdir_fd = openat(got_repo_get_fd(repo),
1288 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1289 if (packdir_fd == -1) {
1290 return got_error_from_errno_fmt("openat: %s/%s",
1291 got_repo_get_path_git_dir(repo),
1292 GOT_OBJECTS_PACK_DIR);
1295 packdir = fdopendir(packdir_fd);
1296 if (packdir == NULL) {
1297 err = got_error_from_errno("fdopendir");
1298 goto done;
1301 if (fstat(packdir_fd, &sb) == -1) {
1302 err = got_error_from_errno("fstat");
1303 goto done;
1305 repo->pack_path_mtime = sb.st_mtime;
1307 while ((dent = readdir(packdir)) != NULL) {
1308 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1309 continue;
1311 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1312 dent->d_name) == -1) {
1313 err = got_error_from_errno("asprintf");
1314 path_packidx = NULL;
1315 break;
1318 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1319 if (err)
1320 break;
1322 done:
1323 if (err)
1324 free(path_packidx);
1325 if (packdir && closedir(packdir) != 0 && err == NULL)
1326 err = got_error_from_errno("closedir");
1327 return err;
1330 const struct got_error *
1331 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1332 struct got_repository *repo)
1334 const struct got_error *err;
1335 size_t i;
1337 *packidx = NULL;
1339 /* Search pack index cache. */
1340 for (i = 0; i < repo->pack_cache_size; i++) {
1341 if (repo->packidx_cache[i] == NULL)
1342 break;
1343 if (strcmp(repo->packidx_cache[i]->path_packidx,
1344 path_packidx) == 0) {
1345 *packidx = repo->packidx_cache[i];
1346 return NULL;
1349 /* No luck. Search the filesystem. */
1351 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1352 path_packidx, 0);
1353 if (err)
1354 return err;
1356 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1357 if (err)
1358 goto done;
1360 err = cache_packidx(repo, *packidx, path_packidx);
1361 done:
1362 if (err) {
1363 got_packidx_close(*packidx);
1364 *packidx = NULL;
1366 return err;
1369 static const struct got_error *
1370 read_packfile_hdr(int fd, struct got_packidx *packidx)
1372 const struct got_error *err = NULL;
1373 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1374 struct got_packfile_hdr hdr;
1375 ssize_t n;
1377 n = read(fd, &hdr, sizeof(hdr));
1378 if (n < 0)
1379 return got_error_from_errno("read");
1380 if (n != sizeof(hdr))
1381 return got_error(GOT_ERR_BAD_PACKFILE);
1383 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1384 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1385 be32toh(hdr.nobjects) != totobj)
1386 err = got_error(GOT_ERR_BAD_PACKFILE);
1388 return err;
1391 static const struct got_error *
1392 open_packfile(int *fd, struct got_repository *repo,
1393 const char *relpath, struct got_packidx *packidx)
1395 const struct got_error *err = NULL;
1397 *fd = openat(got_repo_get_fd(repo), relpath,
1398 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1399 if (*fd == -1)
1400 return got_error_from_errno_fmt("openat: %s/%s",
1401 got_repo_get_path_git_dir(repo), relpath);
1403 if (packidx) {
1404 err = read_packfile_hdr(*fd, packidx);
1405 if (err) {
1406 close(*fd);
1407 *fd = -1;
1411 return err;
1414 const struct got_error *
1415 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1416 const char *path_packfile, struct got_packidx *packidx)
1418 const struct got_error *err = NULL;
1419 struct got_pack *pack = NULL;
1420 struct stat sb;
1421 size_t i;
1423 if (packp)
1424 *packp = NULL;
1426 for (i = 0; i < repo->pack_cache_size; i++) {
1427 pack = &repo->packs[i];
1428 if (pack->path_packfile == NULL)
1429 break;
1430 if (strcmp(pack->path_packfile, path_packfile) == 0)
1431 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1434 if (i == repo->pack_cache_size) {
1435 struct got_pack tmp;
1436 do {
1437 i--;
1438 } while (i > 0 && repo->pinned_pack >= 0 &&
1439 i == repo->pinned_pack);
1440 err = got_pack_close(&repo->packs[i]);
1441 if (err)
1442 return err;
1443 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1444 return got_error_from_errno("ftruncate");
1445 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1446 return got_error_from_errno("ftruncate");
1447 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1448 memcpy(&repo->packs[i], &repo->packs[0],
1449 sizeof(repo->packs[i]));
1450 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1451 if (repo->pinned_pack == 0)
1452 repo->pinned_pack = i;
1453 else if (repo->pinned_pack == i)
1454 repo->pinned_pack = 0;
1455 i = 0;
1458 pack = &repo->packs[i];
1460 pack->path_packfile = strdup(path_packfile);
1461 if (pack->path_packfile == NULL) {
1462 err = got_error_from_errno("strdup");
1463 goto done;
1466 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1467 if (err)
1468 goto done;
1470 if (fstat(pack->fd, &sb) != 0) {
1471 err = got_error_from_errno("fstat");
1472 goto done;
1474 pack->filesize = sb.st_size;
1476 pack->privsep_child = NULL;
1478 #ifndef GOT_PACK_NO_MMAP
1479 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1480 pack->fd, 0);
1481 if (pack->map == MAP_FAILED) {
1482 if (errno != ENOMEM) {
1483 err = got_error_from_errno("mmap");
1484 goto done;
1486 pack->map = NULL; /* fall back to read(2) */
1488 #endif
1489 done:
1490 if (err) {
1491 if (pack) {
1492 free(pack->path_packfile);
1493 memset(pack, 0, sizeof(*pack));
1495 } else if (packp)
1496 *packp = pack;
1497 return err;
1500 struct got_pack *
1501 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1503 struct got_pack *pack = NULL;
1504 size_t i;
1506 for (i = 0; i < repo->pack_cache_size; i++) {
1507 pack = &repo->packs[i];
1508 if (pack->path_packfile == NULL)
1509 break;
1510 if (strcmp(pack->path_packfile, path_packfile) == 0)
1511 return pack;
1514 return NULL;
1517 const struct got_error *
1518 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1519 struct got_pack *pack)
1521 size_t i;
1522 int pinned_pack = -1, pinned_packidx = -1;
1524 for (i = 0; i < repo->pack_cache_size; i++) {
1525 if (repo->packidx_cache[i] &&
1526 strcmp(repo->packidx_cache[i]->path_packidx,
1527 packidx->path_packidx) == 0)
1528 pinned_packidx = i;
1529 if (repo->packs[i].path_packfile &&
1530 strcmp(repo->packs[i].path_packfile,
1531 pack->path_packfile) == 0)
1532 pinned_pack = i;
1535 if (pinned_packidx == -1 || pinned_pack == -1)
1536 return got_error(GOT_ERR_PIN_PACK);
1538 repo->pinned_pack = pinned_pack;
1539 repo->pinned_packidx = pinned_packidx;
1540 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1541 return NULL;
1544 struct got_pack *
1545 got_repo_get_pinned_pack(struct got_repository *repo)
1547 if (repo->pinned_pack >= 0 &&
1548 repo->pinned_pack < repo->pack_cache_size)
1549 return &repo->packs[repo->pinned_pack];
1551 return NULL;
1554 void
1555 got_repo_unpin_pack(struct got_repository *repo)
1557 repo->pinned_packidx = -1;
1558 repo->pinned_pack = -1;
1559 repo->pinned_pid = 0;
1562 const struct got_error *
1563 got_repo_init(const char *repo_path)
1565 const struct got_error *err = NULL;
1566 const char *dirnames[] = {
1567 GOT_OBJECTS_DIR,
1568 GOT_OBJECTS_PACK_DIR,
1569 GOT_REFS_DIR,
1571 const char *description_str = "Unnamed repository; "
1572 "edit this file 'description' to name the repository.";
1573 const char *headref_str = "ref: refs/heads/main";
1574 const char *gitconfig_str = "[core]\n"
1575 "\trepositoryformatversion = 0\n"
1576 "\tfilemode = true\n"
1577 "\tbare = true\n";
1578 char *path;
1579 size_t i;
1581 if (!got_path_dir_is_empty(repo_path))
1582 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1584 for (i = 0; i < nitems(dirnames); i++) {
1585 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1586 return got_error_from_errno("asprintf");
1588 err = got_path_mkdir(path);
1589 free(path);
1590 if (err)
1591 return err;
1594 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1595 return got_error_from_errno("asprintf");
1596 err = got_path_create_file(path, description_str);
1597 free(path);
1598 if (err)
1599 return err;
1601 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1602 return got_error_from_errno("asprintf");
1603 err = got_path_create_file(path, headref_str);
1604 free(path);
1605 if (err)
1606 return err;
1608 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1609 return got_error_from_errno("asprintf");
1610 err = got_path_create_file(path, gitconfig_str);
1611 free(path);
1612 if (err)
1613 return err;
1615 return NULL;
1618 static void
1619 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1621 struct got_pathlist_entry *pe;
1623 while (!TAILQ_EMPTY(packidx_paths)) {
1624 pe = TAILQ_FIRST(packidx_paths);
1625 TAILQ_REMOVE(packidx_paths, pe, entry);
1626 free((char *)pe->path);
1627 free(pe);
1631 static const struct got_error *
1632 match_packed_object(struct got_object_id **unique_id,
1633 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1635 const struct got_error *err = NULL;
1636 struct got_object_id_queue matched_ids;
1637 struct got_pathlist_entry *pe;
1638 struct stat sb;
1640 STAILQ_INIT(&matched_ids);
1642 if (stat(got_repo_get_path_objects_pack(repo), &sb) == -1) {
1643 if (errno != ENOENT)
1644 return got_error_from_errno2("stat",
1645 got_repo_get_path_objects_pack(repo));
1646 } else if (sb.st_mtime != repo->pack_path_mtime) {
1647 purge_packidx_paths(&repo->packidx_paths);
1648 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1649 if (err)
1650 return err;
1653 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1654 const char *path_packidx = pe->path;
1655 struct got_packidx *packidx;
1656 struct got_object_qid *qid;
1658 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1659 path_packidx, 0);
1660 if (err)
1661 break;
1663 err = got_packidx_match_id_str_prefix(&matched_ids,
1664 packidx, id_str_prefix);
1665 if (err) {
1666 got_packidx_close(packidx);
1667 break;
1669 err = got_packidx_close(packidx);
1670 if (err)
1671 break;
1673 STAILQ_FOREACH(qid, &matched_ids, entry) {
1674 if (obj_type != GOT_OBJ_TYPE_ANY) {
1675 int matched_type;
1676 err = got_object_get_type(&matched_type, repo,
1677 &qid->id);
1678 if (err)
1679 goto done;
1680 if (matched_type != obj_type)
1681 continue;
1683 if (*unique_id == NULL) {
1684 *unique_id = got_object_id_dup(&qid->id);
1685 if (*unique_id == NULL) {
1686 err = got_error_from_errno("malloc");
1687 goto done;
1689 } else {
1690 if (got_object_id_cmp(*unique_id,
1691 &qid->id) == 0)
1692 continue; /* packed multiple times */
1693 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1694 goto done;
1698 done:
1699 got_object_id_queue_free(&matched_ids);
1700 if (err) {
1701 free(*unique_id);
1702 *unique_id = NULL;
1704 return err;
1707 static const struct got_error *
1708 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1709 const char *object_dir, const char *id_str_prefix, int obj_type,
1710 struct got_repository *repo)
1712 const struct got_error *err = NULL;
1713 char *path, *id_str = NULL;
1714 DIR *dir = NULL;
1715 struct dirent *dent;
1716 struct got_object_id id;
1718 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1719 err = got_error_from_errno("asprintf");
1720 goto done;
1723 dir = opendir(path);
1724 if (dir == NULL) {
1725 if (errno == ENOENT) {
1726 err = NULL;
1727 goto done;
1729 err = got_error_from_errno2("opendir", path);
1730 goto done;
1732 while ((dent = readdir(dir)) != NULL) {
1733 int cmp;
1735 free(id_str);
1736 id_str = NULL;
1738 if (strcmp(dent->d_name, ".") == 0 ||
1739 strcmp(dent->d_name, "..") == 0)
1740 continue;
1742 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1743 err = got_error_from_errno("asprintf");
1744 goto done;
1747 if (!got_parse_sha1_digest(id.sha1, id_str))
1748 continue;
1751 * Directory entries do not necessarily appear in
1752 * sorted order, so we must iterate over all of them.
1754 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1755 if (cmp != 0)
1756 continue;
1758 if (*unique_id == NULL) {
1759 if (obj_type != GOT_OBJ_TYPE_ANY) {
1760 int matched_type;
1761 err = got_object_get_type(&matched_type, repo,
1762 &id);
1763 if (err)
1764 goto done;
1765 if (matched_type != obj_type)
1766 continue;
1768 *unique_id = got_object_id_dup(&id);
1769 if (*unique_id == NULL) {
1770 err = got_error_from_errno("got_object_id_dup");
1771 goto done;
1773 } else {
1774 if (got_object_id_cmp(*unique_id, &id) == 0)
1775 continue; /* both packed and loose */
1776 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1777 goto done;
1780 done:
1781 if (dir && closedir(dir) != 0 && err == NULL)
1782 err = got_error_from_errno("closedir");
1783 if (err) {
1784 free(*unique_id);
1785 *unique_id = NULL;
1787 free(id_str);
1788 free(path);
1789 return err;
1792 const struct got_error *
1793 got_repo_match_object_id_prefix(struct got_object_id **id,
1794 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1796 const struct got_error *err = NULL;
1797 char *path_objects = got_repo_get_path_objects(repo);
1798 char *object_dir = NULL;
1799 size_t len;
1800 int i;
1802 *id = NULL;
1804 len = strlen(id_str_prefix);
1805 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1806 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1808 for (i = 0; i < len; i++) {
1809 if (isxdigit((unsigned char)id_str_prefix[i]))
1810 continue;
1811 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1814 if (len >= 2) {
1815 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1816 if (err)
1817 goto done;
1818 object_dir = strndup(id_str_prefix, 2);
1819 if (object_dir == NULL) {
1820 err = got_error_from_errno("strdup");
1821 goto done;
1823 err = match_loose_object(id, path_objects, object_dir,
1824 id_str_prefix, obj_type, repo);
1825 } else if (len == 1) {
1826 int i;
1827 for (i = 0; i < 0xf; i++) {
1828 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1829 == -1) {
1830 err = got_error_from_errno("asprintf");
1831 goto done;
1833 err = match_packed_object(id, repo, object_dir,
1834 obj_type);
1835 if (err)
1836 goto done;
1837 err = match_loose_object(id, path_objects, object_dir,
1838 id_str_prefix, obj_type, repo);
1839 if (err)
1840 goto done;
1842 } else {
1843 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1844 goto done;
1846 done:
1847 free(object_dir);
1848 if (err) {
1849 free(*id);
1850 *id = NULL;
1851 } else if (*id == NULL) {
1852 switch (obj_type) {
1853 case GOT_OBJ_TYPE_BLOB:
1854 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1855 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1856 break;
1857 case GOT_OBJ_TYPE_TREE:
1858 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1859 GOT_OBJ_LABEL_TREE, id_str_prefix);
1860 break;
1861 case GOT_OBJ_TYPE_COMMIT:
1862 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1863 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1864 break;
1865 case GOT_OBJ_TYPE_TAG:
1866 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1867 GOT_OBJ_LABEL_TAG, id_str_prefix);
1868 break;
1869 default:
1870 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1871 break;
1875 return err;
1878 const struct got_error *
1879 got_repo_match_object_id(struct got_object_id **id, char **label,
1880 const char *id_str, int obj_type, struct got_reflist_head *refs,
1881 struct got_repository *repo)
1883 const struct got_error *err;
1884 struct got_tag_object *tag;
1885 struct got_reference *ref = NULL;
1887 *id = NULL;
1888 if (label)
1889 *label = NULL;
1891 if (refs) {
1892 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1893 refs, repo);
1894 if (err == NULL) {
1895 *id = got_object_id_dup(
1896 got_object_tag_get_object_id(tag));
1897 if (*id == NULL)
1898 err = got_error_from_errno("got_object_id_dup");
1899 else if (label && asprintf(label, "refs/tags/%s",
1900 got_object_tag_get_name(tag)) == -1) {
1901 err = got_error_from_errno("asprintf");
1902 free(*id);
1903 *id = NULL;
1905 got_object_tag_close(tag);
1906 return err;
1907 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1908 err->code != GOT_ERR_NO_OBJ)
1909 return err;
1912 err = got_ref_open(&ref, repo, id_str, 0);
1913 if (err == NULL) {
1914 err = got_ref_resolve(id, repo, ref);
1915 if (err)
1916 goto done;
1917 if (label) {
1918 *label = strdup(got_ref_get_name(ref));
1919 if (*label == NULL) {
1920 err = got_error_from_errno("strdup");
1921 goto done;
1924 } else {
1925 if (err->code != GOT_ERR_NOT_REF &&
1926 err->code != GOT_ERR_BAD_REF_NAME)
1927 goto done;
1928 err = got_repo_match_object_id_prefix(id, id_str,
1929 obj_type, repo);
1930 if (err) {
1931 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1932 err = got_error_not_ref(id_str);
1933 goto done;
1935 if (label) {
1936 err = got_object_id_str(label, *id);
1937 if (*label == NULL) {
1938 err = got_error_from_errno("strdup");
1939 goto done;
1943 done:
1944 if (ref)
1945 got_ref_close(ref);
1946 return err;
1949 const struct got_error *
1950 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1951 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1953 const struct got_error *err = NULL;
1954 struct got_reflist_entry *re;
1955 struct got_object_id *tag_id;
1956 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1958 *tag = NULL;
1960 TAILQ_FOREACH(re, refs, entry) {
1961 const char *refname;
1962 refname = got_ref_get_name(re->ref);
1963 if (got_ref_is_symbolic(re->ref))
1964 continue;
1965 if (strncmp(refname, "refs/tags/", 10) != 0)
1966 continue;
1967 if (!name_is_absolute)
1968 refname += strlen("refs/tags/");
1969 if (strcmp(refname, name) != 0)
1970 continue;
1971 err = got_ref_resolve(&tag_id, repo, re->ref);
1972 if (err)
1973 break;
1974 err = got_object_open_as_tag(tag, repo, tag_id);
1975 free(tag_id);
1976 if (err)
1977 break;
1978 if (obj_type == GOT_OBJ_TYPE_ANY ||
1979 got_object_tag_get_object_type(*tag) == obj_type)
1980 break;
1981 got_object_tag_close(*tag);
1982 *tag = NULL;
1985 if (err == NULL && *tag == NULL)
1986 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1987 GOT_OBJ_LABEL_TAG, name);
1988 return err;
1991 static const struct got_error *
1992 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1993 const char *name, mode_t mode, struct got_object_id *blob_id)
1995 const struct got_error *err = NULL;
1997 *new_te = NULL;
1999 *new_te = calloc(1, sizeof(**new_te));
2000 if (*new_te == NULL)
2001 return got_error_from_errno("calloc");
2003 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2004 sizeof((*new_te)->name)) {
2005 err = got_error(GOT_ERR_NO_SPACE);
2006 goto done;
2009 if (S_ISLNK(mode)) {
2010 (*new_te)->mode = S_IFLNK;
2011 } else {
2012 (*new_te)->mode = S_IFREG;
2013 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2015 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2016 done:
2017 if (err && *new_te) {
2018 free(*new_te);
2019 *new_te = NULL;
2021 return err;
2024 static const struct got_error *
2025 import_file(struct got_tree_entry **new_te, struct dirent *de,
2026 const char *path, struct got_repository *repo)
2028 const struct got_error *err;
2029 struct got_object_id *blob_id = NULL;
2030 char *filepath;
2031 struct stat sb;
2033 if (asprintf(&filepath, "%s%s%s", path,
2034 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2035 return got_error_from_errno("asprintf");
2037 if (lstat(filepath, &sb) != 0) {
2038 err = got_error_from_errno2("lstat", path);
2039 goto done;
2042 err = got_object_blob_create(&blob_id, filepath, repo);
2043 if (err)
2044 goto done;
2046 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2047 blob_id);
2048 done:
2049 free(filepath);
2050 if (err)
2051 free(blob_id);
2052 return err;
2055 static const struct got_error *
2056 insert_tree_entry(struct got_tree_entry *new_te,
2057 struct got_pathlist_head *paths)
2059 const struct got_error *err = NULL;
2060 struct got_pathlist_entry *new_pe;
2062 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2063 if (err)
2064 return err;
2065 if (new_pe == NULL)
2066 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2067 return NULL;
2070 static const struct got_error *write_tree(struct got_object_id **,
2071 const char *, struct got_pathlist_head *, struct got_repository *,
2072 got_repo_import_cb progress_cb, void *progress_arg);
2074 static const struct got_error *
2075 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2076 const char *path, struct got_pathlist_head *ignores,
2077 struct got_repository *repo,
2078 got_repo_import_cb progress_cb, void *progress_arg)
2080 const struct got_error *err;
2081 struct got_object_id *id = NULL;
2082 char *subdirpath;
2084 if (asprintf(&subdirpath, "%s%s%s", path,
2085 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2086 return got_error_from_errno("asprintf");
2088 (*new_te) = calloc(1, sizeof(**new_te));
2089 if (*new_te == NULL)
2090 return got_error_from_errno("calloc");
2091 (*new_te)->mode = S_IFDIR;
2092 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2093 sizeof((*new_te)->name)) {
2094 err = got_error(GOT_ERR_NO_SPACE);
2095 goto done;
2097 err = write_tree(&id, subdirpath, ignores, repo,
2098 progress_cb, progress_arg);
2099 if (err)
2100 goto done;
2101 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2103 done:
2104 free(id);
2105 free(subdirpath);
2106 if (err) {
2107 free(*new_te);
2108 *new_te = NULL;
2110 return err;
2113 static const struct got_error *
2114 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2115 struct got_pathlist_head *ignores, struct got_repository *repo,
2116 got_repo_import_cb progress_cb, void *progress_arg)
2118 const struct got_error *err = NULL;
2119 DIR *dir;
2120 struct dirent *de;
2121 int nentries;
2122 struct got_tree_entry *new_te = NULL;
2123 struct got_pathlist_head paths;
2124 struct got_pathlist_entry *pe;
2126 *new_tree_id = NULL;
2128 TAILQ_INIT(&paths);
2130 dir = opendir(path_dir);
2131 if (dir == NULL) {
2132 err = got_error_from_errno2("opendir", path_dir);
2133 goto done;
2136 nentries = 0;
2137 while ((de = readdir(dir)) != NULL) {
2138 int ignore = 0;
2139 int type;
2141 if (strcmp(de->d_name, ".") == 0 ||
2142 strcmp(de->d_name, "..") == 0)
2143 continue;
2145 TAILQ_FOREACH(pe, ignores, entry) {
2146 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2147 ignore = 1;
2148 break;
2151 if (ignore)
2152 continue;
2154 err = got_path_dirent_type(&type, path_dir, de);
2155 if (err)
2156 goto done;
2158 if (type == DT_DIR) {
2159 err = import_subdir(&new_te, de, path_dir,
2160 ignores, repo, progress_cb, progress_arg);
2161 if (err) {
2162 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2163 goto done;
2164 err = NULL;
2165 continue;
2167 } else if (type == DT_REG || type == DT_LNK) {
2168 err = import_file(&new_te, de, path_dir, repo);
2169 if (err)
2170 goto done;
2171 } else
2172 continue;
2174 err = insert_tree_entry(new_te, &paths);
2175 if (err)
2176 goto done;
2177 nentries++;
2180 if (TAILQ_EMPTY(&paths)) {
2181 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2182 "cannot create tree without any entries");
2183 goto done;
2186 TAILQ_FOREACH(pe, &paths, entry) {
2187 struct got_tree_entry *te = pe->data;
2188 char *path;
2189 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2190 continue;
2191 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2192 err = got_error_from_errno("asprintf");
2193 goto done;
2195 err = (*progress_cb)(progress_arg, path);
2196 free(path);
2197 if (err)
2198 goto done;
2201 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2202 done:
2203 if (dir)
2204 closedir(dir);
2205 got_pathlist_free(&paths);
2206 return err;
2209 const struct got_error *
2210 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2211 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2212 struct got_repository *repo, got_repo_import_cb progress_cb,
2213 void *progress_arg)
2215 const struct got_error *err;
2216 struct got_object_id *new_tree_id;
2218 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2219 progress_cb, progress_arg);
2220 if (err)
2221 return err;
2223 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2224 author, time(NULL), author, time(NULL), logmsg, repo);
2225 free(new_tree_id);
2226 return err;
2229 const struct got_error *
2230 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2231 struct got_repository *repo)
2233 const struct got_error *err = NULL;
2234 char *path_objects = NULL, *path = NULL;
2235 DIR *dir = NULL;
2236 struct got_object_id id;
2237 int i;
2239 *nobjects = 0;
2240 *ondisk_size = 0;
2242 path_objects = got_repo_get_path_objects(repo);
2243 if (path_objects == NULL)
2244 return got_error_from_errno("got_repo_get_path_objects");
2246 for (i = 0; i <= 0xff; i++) {
2247 struct dirent *dent;
2249 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2250 err = got_error_from_errno("asprintf");
2251 break;
2254 dir = opendir(path);
2255 if (dir == NULL) {
2256 if (errno == ENOENT) {
2257 err = NULL;
2258 continue;
2260 err = got_error_from_errno2("opendir", path);
2261 break;
2264 while ((dent = readdir(dir)) != NULL) {
2265 char *id_str;
2266 int fd;
2267 struct stat sb;
2269 if (strcmp(dent->d_name, ".") == 0 ||
2270 strcmp(dent->d_name, "..") == 0)
2271 continue;
2273 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2274 err = got_error_from_errno("asprintf");
2275 goto done;
2278 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2279 free(id_str);
2280 continue;
2282 free(id_str);
2284 err = got_object_open_loose_fd(&fd, &id, repo);
2285 if (err)
2286 goto done;
2288 if (fstat(fd, &sb) == -1) {
2289 err = got_error_from_errno("fstat");
2290 close(fd);
2291 goto done;
2293 (*nobjects)++;
2294 (*ondisk_size) += sb.st_size;
2296 if (close(fd) == -1) {
2297 err = got_error_from_errno("close");
2298 goto done;
2302 if (closedir(dir) != 0) {
2303 err = got_error_from_errno("closedir");
2304 goto done;
2306 dir = NULL;
2308 free(path);
2309 path = NULL;
2311 done:
2312 if (dir && closedir(dir) != 0 && err == NULL)
2313 err = got_error_from_errno("closedir");
2315 if (err) {
2316 *nobjects = 0;
2317 *ondisk_size = 0;
2319 free(path_objects);
2320 free(path);
2321 return err;
2324 const struct got_error *
2325 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2326 off_t *total_packsize, struct got_repository *repo)
2328 const struct got_error *err = NULL;
2329 DIR *packdir = NULL;
2330 struct dirent *dent;
2331 struct got_packidx *packidx = NULL;
2332 char *path_packidx;
2333 char *path_packfile;
2334 int packdir_fd;
2335 struct stat sb;
2337 *npackfiles = 0;
2338 *nobjects = 0;
2339 *total_packsize = 0;
2341 packdir_fd = openat(got_repo_get_fd(repo),
2342 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2343 if (packdir_fd == -1) {
2344 return got_error_from_errno_fmt("openat: %s/%s",
2345 got_repo_get_path_git_dir(repo),
2346 GOT_OBJECTS_PACK_DIR);
2349 packdir = fdopendir(packdir_fd);
2350 if (packdir == NULL) {
2351 err = got_error_from_errno("fdopendir");
2352 goto done;
2355 while ((dent = readdir(packdir)) != NULL) {
2356 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2357 continue;
2359 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2360 dent->d_name) == -1) {
2361 err = got_error_from_errno("asprintf");
2362 goto done;
2365 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2366 path_packidx, 0);
2367 free(path_packidx);
2368 if (err)
2369 goto done;
2371 if (fstat(packidx->fd, &sb) == -1)
2372 goto done;
2373 *total_packsize += sb.st_size;
2375 err = got_packidx_get_packfile_path(&path_packfile,
2376 packidx->path_packidx);
2377 if (err)
2378 goto done;
2380 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2381 0) == -1) {
2382 free(path_packfile);
2383 goto done;
2385 free(path_packfile);
2386 *total_packsize += sb.st_size;
2388 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2390 (*npackfiles)++;
2392 got_packidx_close(packidx);
2393 packidx = NULL;
2395 done:
2396 if (packidx)
2397 got_packidx_close(packidx);
2398 if (packdir && closedir(packdir) != 0 && err == NULL)
2399 err = got_error_from_errno("closedir");
2400 if (err) {
2401 *npackfiles = 0;
2402 *nobjects = 0;
2403 *total_packsize = 0;
2405 return err;
2408 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2409 got_packidx_bloom_filter_cmp);