Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 #endif
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
71 const char *
72 got_repo_get_path(struct got_repository *repo)
73 {
74 return repo->path;
75 }
77 const char *
78 got_repo_get_path_git_dir(struct got_repository *repo)
79 {
80 return repo->path_git_dir;
81 }
83 int
84 got_repo_get_fd(struct got_repository *repo)
85 {
86 return repo->gitdir_fd;
87 }
89 const char *
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
91 {
92 return repo->gitconfig_author_name;
93 }
95 const char *
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
97 {
98 return repo->gitconfig_author_email;
99 }
101 const char *
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
107 const char *
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
113 const char *
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
119 void
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
127 int
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
133 static char *
134 get_path_git_child(struct got_repository *repo, const char *basename)
136 char *path_child;
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
139 basename) == -1)
140 return NULL;
142 return path_child;
145 char *
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
151 char *
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
157 char *
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
163 char *
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
169 static char *
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
175 char *
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
181 char *
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
193 void
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
201 static int
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
208 int ret = 0;
209 struct stat sb;
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
213 goto done;
214 if (!S_ISDIR(sb.st_mode))
215 goto done;
217 if (lstat(path_objects, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_refs, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_head, &sb) == -1)
228 goto done;
229 if (!S_ISREG(sb.st_mode))
230 goto done;
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
234 goto done;
235 got_ref_close(head_ref);
237 ret = 1;
238 done:
239 free(path_objects);
240 free(path_refs);
241 free(path_head);
242 return ret;
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
250 int i, *pack_fds_tmp;
252 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
253 if (pack_fds_tmp == NULL)
254 return got_error_from_errno("calloc");
255 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
256 if (*pack_fds == NULL) {
257 free(pack_fds_tmp);
258 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_tmp[i] = -1;
270 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
271 pack_fds_tmp[i] = got_opentempfd();
272 if (pack_fds_tmp[i] == -1) {
273 err = got_error_from_errno("got_opentempfd");
274 got_repo_pack_fds_close(pack_fds_tmp);
275 return err;
278 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
279 return err;
282 const struct got_error *
283 got_repo_pack_fds_close(int *pack_fds)
285 const struct got_error *err = NULL;
286 int i;
288 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
289 if (pack_fds[i] == -1)
290 continue;
291 if (close(pack_fds[i]) == -1) {
292 err = got_error_from_errno("close");
293 break;
296 free(pack_fds);
297 return err;
300 const struct got_error *
301 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
302 struct got_object *obj)
304 #ifndef GOT_NO_OBJ_CACHE
305 const struct got_error *err = NULL;
306 err = got_object_cache_add(&repo->objcache, id, obj);
307 if (err) {
308 if (err->code == GOT_ERR_OBJ_EXISTS ||
309 err->code == GOT_ERR_OBJ_TOO_LARGE)
310 err = NULL;
311 return err;
313 obj->refcnt++;
314 #endif
315 return NULL;
318 struct got_object *
319 got_repo_get_cached_object(struct got_repository *repo,
320 struct got_object_id *id)
322 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
325 const struct got_error *
326 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
327 struct got_tree_object *tree)
329 #ifndef GOT_NO_OBJ_CACHE
330 const struct got_error *err = NULL;
331 err = got_object_cache_add(&repo->treecache, id, tree);
332 if (err) {
333 if (err->code == GOT_ERR_OBJ_EXISTS ||
334 err->code == GOT_ERR_OBJ_TOO_LARGE)
335 err = NULL;
336 return err;
338 tree->refcnt++;
339 #endif
340 return NULL;
343 struct got_tree_object *
344 got_repo_get_cached_tree(struct got_repository *repo,
345 struct got_object_id *id)
347 return (struct got_tree_object *)got_object_cache_get(
348 &repo->treecache, id);
351 const struct got_error *
352 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
353 struct got_commit_object *commit)
355 #ifndef GOT_NO_OBJ_CACHE
356 const struct got_error *err = NULL;
357 err = got_object_cache_add(&repo->commitcache, id, commit);
358 if (err) {
359 if (err->code == GOT_ERR_OBJ_EXISTS ||
360 err->code == GOT_ERR_OBJ_TOO_LARGE)
361 err = NULL;
362 return err;
364 commit->refcnt++;
365 #endif
366 return NULL;
369 struct got_commit_object *
370 got_repo_get_cached_commit(struct got_repository *repo,
371 struct got_object_id *id)
373 return (struct got_commit_object *)got_object_cache_get(
374 &repo->commitcache, id);
377 const struct got_error *
378 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
379 struct got_tag_object *tag)
381 #ifndef GOT_NO_OBJ_CACHE
382 const struct got_error *err = NULL;
383 err = got_object_cache_add(&repo->tagcache, id, tag);
384 if (err) {
385 if (err->code == GOT_ERR_OBJ_EXISTS ||
386 err->code == GOT_ERR_OBJ_TOO_LARGE)
387 err = NULL;
388 return err;
390 tag->refcnt++;
391 #endif
392 return NULL;
395 struct got_tag_object *
396 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
398 return (struct got_tag_object *)got_object_cache_get(
399 &repo->tagcache, id);
402 const struct got_error *
403 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
404 struct got_raw_object *raw)
406 #ifndef GOT_NO_OBJ_CACHE
407 const struct got_error *err = NULL;
408 err = got_object_cache_add(&repo->rawcache, id, raw);
409 if (err) {
410 if (err->code == GOT_ERR_OBJ_EXISTS ||
411 err->code == GOT_ERR_OBJ_TOO_LARGE)
412 err = NULL;
413 return err;
415 raw->refcnt++;
416 #endif
417 return NULL;
421 struct got_raw_object *
422 got_repo_get_cached_raw_object(struct got_repository *repo,
423 struct got_object_id *id)
425 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
429 static const struct got_error *
430 open_repo(struct got_repository *repo, const char *path)
432 const struct got_error *err = NULL;
434 repo->gitdir_fd = -1;
436 /* bare git repository? */
437 repo->path_git_dir = strdup(path);
438 if (repo->path_git_dir == NULL)
439 return got_error_from_errno("strdup");
440 if (is_git_repo(repo)) {
441 repo->path = strdup(repo->path_git_dir);
442 if (repo->path == NULL) {
443 err = got_error_from_errno("strdup");
444 goto done;
446 repo->gitdir_fd = open(repo->path_git_dir,
447 O_DIRECTORY | O_CLOEXEC);
448 if (repo->gitdir_fd == -1) {
449 err = got_error_from_errno2("open",
450 repo->path_git_dir);
451 goto done;
453 return NULL;
456 /* git repository with working tree? */
457 free(repo->path_git_dir);
458 repo->path_git_dir = NULL;
459 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
460 err = got_error_from_errno("asprintf");
461 goto done;
463 if (is_git_repo(repo)) {
464 repo->path = strdup(path);
465 if (repo->path == NULL) {
466 err = got_error_from_errno("strdup");
467 goto done;
469 repo->gitdir_fd = open(repo->path_git_dir,
470 O_DIRECTORY | O_CLOEXEC);
471 if (repo->gitdir_fd == -1) {
472 err = got_error_from_errno2("open",
473 repo->path_git_dir);
474 goto done;
476 return NULL;
479 err = got_error(GOT_ERR_NOT_GIT_REPO);
480 done:
481 if (err) {
482 free(repo->path);
483 repo->path = NULL;
484 free(repo->path_git_dir);
485 repo->path_git_dir = NULL;
486 if (repo->gitdir_fd != -1)
487 close(repo->gitdir_fd);
488 repo->gitdir_fd = -1;
491 return err;
494 static const struct got_error *
495 parse_gitconfig_file(int *gitconfig_repository_format_version,
496 char **gitconfig_author_name, char **gitconfig_author_email,
497 struct got_remote_repo **remotes, int *nremotes,
498 char **gitconfig_owner, char ***extensions, int *nextensions,
499 const char *gitconfig_path)
501 const struct got_error *err = NULL, *child_err = NULL;
502 int fd = -1;
503 int imsg_fds[2] = { -1, -1 };
504 pid_t pid;
505 struct imsgbuf *ibuf;
507 *gitconfig_repository_format_version = 0;
508 if (extensions)
509 *extensions = NULL;
510 if (nextensions)
511 *nextensions = 0;
512 *gitconfig_author_name = NULL;
513 *gitconfig_author_email = NULL;
514 if (remotes)
515 *remotes = NULL;
516 if (nremotes)
517 *nremotes = 0;
518 if (gitconfig_owner)
519 *gitconfig_owner = NULL;
521 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
522 if (fd == -1) {
523 if (errno == ENOENT)
524 return NULL;
525 return got_error_from_errno2("open", gitconfig_path);
528 ibuf = calloc(1, sizeof(*ibuf));
529 if (ibuf == NULL) {
530 err = got_error_from_errno("calloc");
531 goto done;
534 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
535 err = got_error_from_errno("socketpair");
536 goto done;
539 pid = fork();
540 if (pid == -1) {
541 err = got_error_from_errno("fork");
542 goto done;
543 } else if (pid == 0) {
544 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
545 gitconfig_path);
546 /* not reached */
549 if (close(imsg_fds[1]) == -1) {
550 err = got_error_from_errno("close");
551 goto done;
553 imsg_fds[1] = -1;
554 imsg_init(ibuf, imsg_fds[0]);
556 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
557 if (err)
558 goto done;
559 fd = -1;
561 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
562 if (err)
563 goto done;
565 err = got_privsep_recv_gitconfig_int(
566 gitconfig_repository_format_version, ibuf);
567 if (err)
568 goto done;
570 if (extensions && nextensions) {
571 err = got_privsep_send_gitconfig_repository_extensions_req(
572 ibuf);
573 if (err)
574 goto done;
575 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
576 if (err)
577 goto done;
578 if (*nextensions > 0) {
579 int i;
580 *extensions = calloc(*nextensions, sizeof(char *));
581 if (*extensions == NULL) {
582 err = got_error_from_errno("calloc");
583 goto done;
585 for (i = 0; i < *nextensions; i++) {
586 char *ext;
587 err = got_privsep_recv_gitconfig_str(&ext,
588 ibuf);
589 if (err)
590 goto done;
591 (*extensions)[i] = ext;
596 err = got_privsep_send_gitconfig_author_name_req(ibuf);
597 if (err)
598 goto done;
600 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
601 if (err)
602 goto done;
604 err = got_privsep_send_gitconfig_author_email_req(ibuf);
605 if (err)
606 goto done;
608 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
609 if (err)
610 goto done;
612 if (remotes && nremotes) {
613 err = got_privsep_send_gitconfig_remotes_req(ibuf);
614 if (err)
615 goto done;
617 err = got_privsep_recv_gitconfig_remotes(remotes,
618 nremotes, ibuf);
619 if (err)
620 goto done;
623 if (gitconfig_owner) {
624 err = got_privsep_send_gitconfig_owner_req(ibuf);
625 if (err)
626 goto done;
627 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
628 if (err)
629 goto done;
632 err = got_privsep_send_stop(imsg_fds[0]);
633 child_err = got_privsep_wait_for_child(pid);
634 if (child_err && err == NULL)
635 err = child_err;
636 done:
637 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
638 err = got_error_from_errno("close");
639 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
640 err = got_error_from_errno("close");
641 if (fd != -1 && close(fd) == -1 && err == NULL)
642 err = got_error_from_errno2("close", gitconfig_path);
643 free(ibuf);
644 return err;
647 static const struct got_error *
648 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
650 const struct got_error *err = NULL;
651 char *repo_gitconfig_path = NULL;
653 if (global_gitconfig_path) {
654 /* Read settings from ~/.gitconfig. */
655 int dummy_repo_version;
656 err = parse_gitconfig_file(&dummy_repo_version,
657 &repo->global_gitconfig_author_name,
658 &repo->global_gitconfig_author_email,
659 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
660 if (err)
661 return err;
664 /* Read repository's .git/config file. */
665 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
666 if (repo_gitconfig_path == NULL)
667 return got_error_from_errno("got_repo_get_path_gitconfig");
669 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
670 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
671 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
672 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
673 repo_gitconfig_path);
674 if (err)
675 goto done;
677 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
678 int i;
680 for (i = 0; i < repo->ngitconfig_remotes; i++) {
681 got_repo_free_remote_repo_data(
682 &repo->gitconfig_remotes[i]);
684 free(repo->gitconfig_remotes);
685 repo->gitconfig_remotes = NULL;
686 repo->ngitconfig_remotes = 0;
688 free(repo->gitconfig_author_name);
689 repo->gitconfig_author_name = NULL;
690 free(repo->gitconfig_author_email);
691 repo->gitconfig_author_email = NULL;
693 free(repo->global_gitconfig_author_name);
694 repo->global_gitconfig_author_name = NULL;
695 free(repo->global_gitconfig_author_email);
696 repo->global_gitconfig_author_email = NULL;
699 done:
700 free(repo_gitconfig_path);
701 return err;
704 static const struct got_error *
705 read_gotconfig(struct got_repository *repo)
707 const struct got_error *err = NULL;
708 char *gotconfig_path;
710 gotconfig_path = got_repo_get_path_gotconfig(repo);
711 if (gotconfig_path == NULL)
712 return got_error_from_errno("got_repo_get_path_gotconfig");
714 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
715 free(gotconfig_path);
716 return err;
719 /* Supported repository format extensions. */
720 static const char *const repo_extensions[] = {
721 "noop", /* Got supports repository format version 1. */
722 "preciousObjects", /* Supported by gotadmin cleanup. */
723 "worktreeConfig", /* Got does not care about Git work trees. */
724 };
726 const struct got_error *
727 got_repo_open(struct got_repository **repop, const char *path,
728 const char *global_gitconfig_path, int *pack_fds)
730 struct got_repository *repo = NULL;
731 const struct got_error *err = NULL;
732 char *repo_path = NULL;
733 size_t i, j = 0;
734 struct rlimit rl;
736 *repop = NULL;
738 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
739 return got_error_from_errno("getrlimit");
741 repo = calloc(1, sizeof(*repo));
742 if (repo == NULL)
743 return got_error_from_errno("calloc");
745 RB_INIT(&repo->packidx_bloom_filters);
746 TAILQ_INIT(&repo->packidx_paths);
748 for (i = 0; i < nitems(repo->privsep_children); i++) {
749 memset(&repo->privsep_children[i], 0,
750 sizeof(repo->privsep_children[0]));
751 repo->privsep_children[i].imsg_fd = -1;
754 err = got_object_cache_init(&repo->objcache,
755 GOT_OBJECT_CACHE_TYPE_OBJ);
756 if (err)
757 goto done;
758 err = got_object_cache_init(&repo->treecache,
759 GOT_OBJECT_CACHE_TYPE_TREE);
760 if (err)
761 goto done;
762 err = got_object_cache_init(&repo->commitcache,
763 GOT_OBJECT_CACHE_TYPE_COMMIT);
764 if (err)
765 goto done;
766 err = got_object_cache_init(&repo->tagcache,
767 GOT_OBJECT_CACHE_TYPE_TAG);
768 if (err)
769 goto done;
770 err = got_object_cache_init(&repo->rawcache,
771 GOT_OBJECT_CACHE_TYPE_RAW);
772 if (err)
773 goto done;
775 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
776 if (repo->pack_cache_size > rl.rlim_cur / 8)
777 repo->pack_cache_size = rl.rlim_cur / 8;
778 for (i = 0; i < nitems(repo->packs); i++) {
779 if (i < repo->pack_cache_size) {
780 repo->packs[i].basefd = pack_fds[j++];
781 repo->packs[i].accumfd = pack_fds[j++];
782 } else {
783 repo->packs[i].basefd = -1;
784 repo->packs[i].accumfd = -1;
787 repo->pinned_pack = -1;
788 repo->pinned_packidx = -1;
789 repo->pinned_pid = 0;
791 repo_path = realpath(path, NULL);
792 if (repo_path == NULL) {
793 err = got_error_from_errno2("realpath", path);
794 goto done;
797 for (;;) {
798 char *parent_path;
800 err = open_repo(repo, repo_path);
801 if (err == NULL)
802 break;
803 if (err->code != GOT_ERR_NOT_GIT_REPO)
804 goto done;
805 if (repo_path[0] == '/' && repo_path[1] == '\0') {
806 err = got_error(GOT_ERR_NOT_GIT_REPO);
807 goto done;
809 err = got_path_dirname(&parent_path, repo_path);
810 if (err)
811 goto done;
812 free(repo_path);
813 repo_path = parent_path;
816 err = read_gotconfig(repo);
817 if (err)
818 goto done;
820 err = read_gitconfig(repo, global_gitconfig_path);
821 if (err)
822 goto done;
823 if (repo->gitconfig_repository_format_version != 0) {
824 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
825 goto done;
827 for (i = 0; i < repo->nextensions; i++) {
828 char *ext = repo->extensions[i];
829 int j, supported = 0;
830 for (j = 0; j < nitems(repo_extensions); j++) {
831 if (strcmp(ext, repo_extensions[j]) == 0) {
832 supported = 1;
833 break;
836 if (!supported) {
837 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
838 goto done;
842 err = got_repo_list_packidx(&repo->packidx_paths, repo);
843 done:
844 if (err)
845 got_repo_close(repo);
846 else
847 *repop = repo;
848 free(repo_path);
849 return err;
852 const struct got_error *
853 got_repo_close(struct got_repository *repo)
855 const struct got_error *err = NULL, *child_err;
856 struct got_packidx_bloom_filter *bf;
857 struct got_pathlist_entry *pe;
858 size_t i;
860 for (i = 0; i < repo->pack_cache_size; i++) {
861 if (repo->packidx_cache[i] == NULL)
862 break;
863 got_packidx_close(repo->packidx_cache[i]);
866 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
867 &repo->packidx_bloom_filters))) {
868 RB_REMOVE(got_packidx_bloom_filter_tree,
869 &repo->packidx_bloom_filters, bf);
870 bloom_free(bf->bloom);
871 free(bf->bloom);
872 free(bf);
875 for (i = 0; i < repo->pack_cache_size; i++)
876 if (repo->packs[i].path_packfile)
877 if (repo->packs[i].path_packfile)
878 got_pack_close(&repo->packs[i]);
880 free(repo->path);
881 free(repo->path_git_dir);
883 got_object_cache_close(&repo->objcache);
884 got_object_cache_close(&repo->treecache);
885 got_object_cache_close(&repo->commitcache);
886 got_object_cache_close(&repo->tagcache);
887 got_object_cache_close(&repo->rawcache);
889 for (i = 0; i < nitems(repo->privsep_children); i++) {
890 if (repo->privsep_children[i].imsg_fd == -1)
891 continue;
892 imsg_clear(repo->privsep_children[i].ibuf);
893 free(repo->privsep_children[i].ibuf);
894 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
895 child_err = got_privsep_wait_for_child(
896 repo->privsep_children[i].pid);
897 if (child_err && err == NULL)
898 err = child_err;
899 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
900 err == NULL)
901 err = got_error_from_errno("close");
904 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
905 err == NULL)
906 err = got_error_from_errno("close");
908 if (repo->gotconfig)
909 got_gotconfig_free(repo->gotconfig);
910 free(repo->gitconfig_author_name);
911 free(repo->gitconfig_author_email);
912 for (i = 0; i < repo->ngitconfig_remotes; i++)
913 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
914 free(repo->gitconfig_remotes);
915 for (i = 0; i < repo->nextensions; i++)
916 free(repo->extensions[i]);
917 free(repo->extensions);
919 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
920 free((void *)pe->path);
921 got_pathlist_free(&repo->packidx_paths);
922 free(repo);
924 return err;
927 void
928 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
930 int i;
932 free(repo->name);
933 repo->name = NULL;
934 free(repo->fetch_url);
935 repo->fetch_url = NULL;
936 free(repo->send_url);
937 repo->send_url = NULL;
938 for (i = 0; i < repo->nfetch_branches; i++)
939 free(repo->fetch_branches[i]);
940 free(repo->fetch_branches);
941 repo->fetch_branches = NULL;
942 repo->nfetch_branches = 0;
943 for (i = 0; i < repo->nsend_branches; i++)
944 free(repo->send_branches[i]);
945 free(repo->send_branches);
946 repo->send_branches = NULL;
947 repo->nsend_branches = 0;
950 const struct got_error *
951 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
952 const char *input_path)
954 const struct got_error *err = NULL;
955 const char *repo_abspath = NULL;
956 size_t repolen, len;
957 char *canonpath, *path = NULL;
959 *in_repo_path = NULL;
961 canonpath = strdup(input_path);
962 if (canonpath == NULL) {
963 err = got_error_from_errno("strdup");
964 goto done;
966 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
967 if (err)
968 goto done;
970 repo_abspath = got_repo_get_path(repo);
972 if (canonpath[0] == '\0') {
973 path = strdup(canonpath);
974 if (path == NULL) {
975 err = got_error_from_errno("strdup");
976 goto done;
978 } else {
979 path = realpath(canonpath, NULL);
980 if (path == NULL) {
981 if (errno != ENOENT) {
982 err = got_error_from_errno2("realpath",
983 canonpath);
984 goto done;
986 /*
987 * Path is not on disk.
988 * Assume it is already relative to repository root.
989 */
990 path = strdup(canonpath);
991 if (path == NULL) {
992 err = got_error_from_errno("strdup");
993 goto done;
997 repolen = strlen(repo_abspath);
998 len = strlen(path);
1001 if (strcmp(path, repo_abspath) == 0) {
1002 free(path);
1003 path = strdup("");
1004 if (path == NULL) {
1005 err = got_error_from_errno("strdup");
1006 goto done;
1008 } else if (len > repolen &&
1009 got_path_is_child(path, repo_abspath, repolen)) {
1010 /* Matched an on-disk path inside repository. */
1011 if (got_repo_is_bare(repo)) {
1013 * Matched an on-disk path inside repository
1014 * database. Treat input as repository-relative.
1016 free(path);
1017 path = canonpath;
1018 canonpath = NULL;
1019 } else {
1020 char *child;
1021 /* Strip common prefix with repository path. */
1022 err = got_path_skip_common_ancestor(&child,
1023 repo_abspath, path);
1024 if (err)
1025 goto done;
1026 free(path);
1027 path = child;
1029 } else {
1031 * Matched unrelated on-disk path.
1032 * Treat input as repository-relative.
1034 free(path);
1035 path = canonpath;
1036 canonpath = NULL;
1040 /* Make in-repository path absolute */
1041 if (path[0] != '/') {
1042 char *abspath;
1043 if (asprintf(&abspath, "/%s", path) == -1) {
1044 err = got_error_from_errno("asprintf");
1045 goto done;
1047 free(path);
1048 path = abspath;
1051 done:
1052 free(canonpath);
1053 if (err)
1054 free(path);
1055 else
1056 *in_repo_path = path;
1057 return err;
1060 static const struct got_error *
1061 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1062 const char *path_packidx)
1064 const struct got_error *err = NULL;
1065 size_t i;
1067 for (i = 0; i < repo->pack_cache_size; i++) {
1068 if (repo->packidx_cache[i] == NULL)
1069 break;
1070 if (strcmp(repo->packidx_cache[i]->path_packidx,
1071 path_packidx) == 0) {
1072 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1075 if (i == repo->pack_cache_size) {
1076 do {
1077 i--;
1078 } while (i > 0 && repo->pinned_packidx >= 0 &&
1079 i == repo->pinned_packidx);
1080 err = got_packidx_close(repo->packidx_cache[i]);
1081 if (err)
1082 return err;
1085 repo->packidx_cache[i] = packidx;
1087 return NULL;
1090 int
1091 got_repo_is_packidx_filename(const char *name, size_t len)
1093 if (len != GOT_PACKIDX_NAMELEN)
1094 return 0;
1096 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1097 return 0;
1099 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1100 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1101 return 0;
1103 return 1;
1106 static struct got_packidx_bloom_filter *
1107 get_packidx_bloom_filter(struct got_repository *repo,
1108 const char *path, size_t path_len)
1110 struct got_packidx_bloom_filter key;
1112 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1113 return NULL; /* XXX */
1114 key.path_len = path_len;
1116 return RB_FIND(got_packidx_bloom_filter_tree,
1117 &repo->packidx_bloom_filters, &key);
1120 int
1121 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1122 const char *path_packidx, struct got_object_id *id)
1124 struct got_packidx_bloom_filter *bf;
1126 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1127 if (bf)
1128 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1130 /* No bloom filter means this pack index must be searched. */
1131 return 1;
1134 static const struct got_error *
1135 add_packidx_bloom_filter(struct got_repository *repo,
1136 struct got_packidx *packidx, const char *path_packidx)
1138 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1139 struct got_packidx_bloom_filter *bf;
1140 size_t len;
1143 * Don't use bloom filters for very large pack index files.
1144 * Large pack files will contain a relatively large fraction
1145 * of our objects so we will likely need to visit them anyway.
1146 * The more objects a pack file contains the higher the probability
1147 * of a false-positive match from the bloom filter. And reading
1148 * all object IDs from a large pack index file can be expensive.
1150 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1151 return NULL;
1153 /* Do we already have a filter for this pack index? */
1154 if (get_packidx_bloom_filter(repo, path_packidx,
1155 strlen(path_packidx)) != NULL)
1156 return NULL;
1158 bf = calloc(1, sizeof(*bf));
1159 if (bf == NULL)
1160 return got_error_from_errno("calloc");
1161 bf->bloom = calloc(1, sizeof(*bf->bloom));
1162 if (bf->bloom == NULL) {
1163 free(bf);
1164 return got_error_from_errno("calloc");
1167 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1168 if (len >= sizeof(bf->path)) {
1169 free(bf->bloom);
1170 free(bf);
1171 return got_error(GOT_ERR_NO_SPACE);
1173 bf->path_len = len;
1175 /* Minimum size supported by our bloom filter is 1000 entries. */
1176 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1177 for (i = 0; i < nobjects; i++) {
1178 struct got_packidx_object_id *id;
1179 id = &packidx->hdr.sorted_ids[i];
1180 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1183 RB_INSERT(got_packidx_bloom_filter_tree,
1184 &repo->packidx_bloom_filters, bf);
1185 return NULL;
1188 const struct got_error *
1189 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1190 struct got_repository *repo, struct got_object_id *id)
1192 const struct got_error *err;
1193 struct got_pathlist_entry *pe;
1194 size_t i;
1196 /* Search pack index cache. */
1197 for (i = 0; i < repo->pack_cache_size; i++) {
1198 if (repo->packidx_cache[i] == NULL)
1199 break;
1200 if (!got_repo_check_packidx_bloom_filter(repo,
1201 repo->packidx_cache[i]->path_packidx, id))
1202 continue; /* object will not be found in this index */
1203 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1204 if (*idx != -1) {
1205 *packidx = repo->packidx_cache[i];
1207 * Move this cache entry to the front. Repeatedly
1208 * searching a wrong pack index can be expensive.
1210 if (i > 0) {
1211 memmove(&repo->packidx_cache[1],
1212 &repo->packidx_cache[0],
1213 i * sizeof(repo->packidx_cache[0]));
1214 repo->packidx_cache[0] = *packidx;
1215 if (repo->pinned_packidx >= 0 &&
1216 repo->pinned_packidx < i)
1217 repo->pinned_packidx++;
1218 else if (repo->pinned_packidx == i)
1219 repo->pinned_packidx = 0;
1221 return NULL;
1224 /* No luck. Search the filesystem. */
1226 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1227 const char *path_packidx = pe->path;
1228 int is_cached = 0;
1230 if (!got_repo_check_packidx_bloom_filter(repo,
1231 pe->path, id))
1232 continue; /* object will not be found in this index */
1234 for (i = 0; i < repo->pack_cache_size; i++) {
1235 if (repo->packidx_cache[i] == NULL)
1236 break;
1237 if (strcmp(repo->packidx_cache[i]->path_packidx,
1238 path_packidx) == 0) {
1239 is_cached = 1;
1240 break;
1243 if (is_cached)
1244 continue; /* already searched */
1246 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1247 path_packidx, 0);
1248 if (err)
1249 goto done;
1251 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1252 if (err)
1253 goto done;
1255 err = cache_packidx(repo, *packidx, path_packidx);
1256 if (err)
1257 goto done;
1259 *idx = got_packidx_get_object_idx(*packidx, id);
1260 if (*idx != -1) {
1261 err = NULL; /* found the object */
1262 goto done;
1266 err = got_error_no_obj(id);
1267 done:
1268 return err;
1271 const struct got_error *
1272 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1273 struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 DIR *packdir = NULL;
1277 struct dirent *dent;
1278 char *path_packidx = NULL;
1279 int packdir_fd;
1280 struct stat sb;
1282 packdir_fd = openat(got_repo_get_fd(repo),
1283 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1284 if (packdir_fd == -1) {
1285 return got_error_from_errno_fmt("openat: %s/%s",
1286 got_repo_get_path_git_dir(repo),
1287 GOT_OBJECTS_PACK_DIR);
1290 packdir = fdopendir(packdir_fd);
1291 if (packdir == NULL) {
1292 err = got_error_from_errno("fdopendir");
1293 goto done;
1296 if (fstat(packdir_fd, &sb) == -1) {
1297 err = got_error_from_errno("fstat");
1298 goto done;
1300 repo->pack_path_mtime = sb.st_mtime;
1302 while ((dent = readdir(packdir)) != NULL) {
1303 if (!got_repo_is_packidx_filename(dent->d_name,
1304 strlen(dent->d_name)))
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;
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 char *id_str;
1730 int cmp;
1732 if (strcmp(dent->d_name, ".") == 0 ||
1733 strcmp(dent->d_name, "..") == 0)
1734 continue;
1736 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1737 err = got_error_from_errno("asprintf");
1738 goto done;
1741 if (!got_parse_sha1_digest(id.sha1, id_str))
1742 continue;
1745 * Directory entries do not necessarily appear in
1746 * sorted order, so we must iterate over all of them.
1748 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1749 if (cmp != 0) {
1750 free(id_str);
1751 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 free(id_str);
1768 goto done;
1770 } else {
1771 if (got_object_id_cmp(*unique_id, &id) == 0)
1772 continue; /* both packed and loose */
1773 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1774 free(id_str);
1775 goto done;
1778 done:
1779 if (dir && closedir(dir) != 0 && err == NULL)
1780 err = got_error_from_errno("closedir");
1781 if (err) {
1782 free(*unique_id);
1783 *unique_id = NULL;
1785 free(path);
1786 return err;
1789 const struct got_error *
1790 got_repo_match_object_id_prefix(struct got_object_id **id,
1791 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1793 const struct got_error *err = NULL;
1794 char *path_objects = got_repo_get_path_objects(repo);
1795 char *object_dir = NULL;
1796 size_t len;
1797 int i;
1799 *id = NULL;
1801 len = strlen(id_str_prefix);
1802 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1803 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1805 for (i = 0; i < len; i++) {
1806 if (isxdigit((unsigned char)id_str_prefix[i]))
1807 continue;
1808 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1811 if (len >= 2) {
1812 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1813 if (err)
1814 goto done;
1815 object_dir = strndup(id_str_prefix, 2);
1816 if (object_dir == NULL) {
1817 err = got_error_from_errno("strdup");
1818 goto done;
1820 err = match_loose_object(id, path_objects, object_dir,
1821 id_str_prefix, obj_type, repo);
1822 } else if (len == 1) {
1823 int i;
1824 for (i = 0; i < 0xf; i++) {
1825 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1826 == -1) {
1827 err = got_error_from_errno("asprintf");
1828 goto done;
1830 err = match_packed_object(id, repo, object_dir,
1831 obj_type);
1832 if (err)
1833 goto done;
1834 err = match_loose_object(id, path_objects, object_dir,
1835 id_str_prefix, obj_type, repo);
1836 if (err)
1837 goto done;
1839 } else {
1840 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1841 goto done;
1843 done:
1844 free(object_dir);
1845 if (err) {
1846 free(*id);
1847 *id = NULL;
1848 } else if (*id == NULL) {
1849 switch (obj_type) {
1850 case GOT_OBJ_TYPE_BLOB:
1851 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1852 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1853 break;
1854 case GOT_OBJ_TYPE_TREE:
1855 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1856 GOT_OBJ_LABEL_TREE, id_str_prefix);
1857 break;
1858 case GOT_OBJ_TYPE_COMMIT:
1859 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1860 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1861 break;
1862 case GOT_OBJ_TYPE_TAG:
1863 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1864 GOT_OBJ_LABEL_TAG, id_str_prefix);
1865 break;
1866 default:
1867 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1868 break;
1872 return err;
1875 const struct got_error *
1876 got_repo_match_object_id(struct got_object_id **id, char **label,
1877 const char *id_str, int obj_type, struct got_reflist_head *refs,
1878 struct got_repository *repo)
1880 const struct got_error *err;
1881 struct got_tag_object *tag;
1882 struct got_reference *ref = NULL;
1884 *id = NULL;
1885 if (label)
1886 *label = NULL;
1888 if (refs) {
1889 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1890 refs, repo);
1891 if (err == NULL) {
1892 *id = got_object_id_dup(
1893 got_object_tag_get_object_id(tag));
1894 if (*id == NULL)
1895 err = got_error_from_errno("got_object_id_dup");
1896 else if (label && asprintf(label, "refs/tags/%s",
1897 got_object_tag_get_name(tag)) == -1) {
1898 err = got_error_from_errno("asprintf");
1899 free(*id);
1900 *id = NULL;
1902 got_object_tag_close(tag);
1903 return err;
1904 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1905 err->code != GOT_ERR_NO_OBJ)
1906 return err;
1909 err = got_ref_open(&ref, repo, id_str, 0);
1910 if (err == NULL) {
1911 err = got_ref_resolve(id, repo, ref);
1912 if (err)
1913 goto done;
1914 if (label) {
1915 *label = strdup(got_ref_get_name(ref));
1916 if (*label == NULL) {
1917 err = got_error_from_errno("strdup");
1918 goto done;
1921 } else {
1922 if (err->code != GOT_ERR_NOT_REF &&
1923 err->code != GOT_ERR_BAD_REF_NAME)
1924 goto done;
1925 err = got_repo_match_object_id_prefix(id, id_str,
1926 obj_type, repo);
1927 if (err) {
1928 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1929 err = got_error_not_ref(id_str);
1930 goto done;
1932 if (label) {
1933 err = got_object_id_str(label, *id);
1934 if (*label == NULL) {
1935 err = got_error_from_errno("strdup");
1936 goto done;
1940 done:
1941 if (ref)
1942 got_ref_close(ref);
1943 return err;
1946 const struct got_error *
1947 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1948 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1950 const struct got_error *err = NULL;
1951 struct got_reflist_entry *re;
1952 struct got_object_id *tag_id;
1953 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1955 *tag = NULL;
1957 TAILQ_FOREACH(re, refs, entry) {
1958 const char *refname;
1959 refname = got_ref_get_name(re->ref);
1960 if (got_ref_is_symbolic(re->ref))
1961 continue;
1962 if (strncmp(refname, "refs/tags/", 10) != 0)
1963 continue;
1964 if (!name_is_absolute)
1965 refname += strlen("refs/tags/");
1966 if (strcmp(refname, name) != 0)
1967 continue;
1968 err = got_ref_resolve(&tag_id, repo, re->ref);
1969 if (err)
1970 break;
1971 err = got_object_open_as_tag(tag, repo, tag_id);
1972 free(tag_id);
1973 if (err)
1974 break;
1975 if (obj_type == GOT_OBJ_TYPE_ANY ||
1976 got_object_tag_get_object_type(*tag) == obj_type)
1977 break;
1978 got_object_tag_close(*tag);
1979 *tag = NULL;
1982 if (err == NULL && *tag == NULL)
1983 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1984 GOT_OBJ_LABEL_TAG, name);
1985 return err;
1988 static const struct got_error *
1989 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1990 const char *name, mode_t mode, struct got_object_id *blob_id)
1992 const struct got_error *err = NULL;
1994 *new_te = NULL;
1996 *new_te = calloc(1, sizeof(**new_te));
1997 if (*new_te == NULL)
1998 return got_error_from_errno("calloc");
2000 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2001 sizeof((*new_te)->name)) {
2002 err = got_error(GOT_ERR_NO_SPACE);
2003 goto done;
2006 if (S_ISLNK(mode)) {
2007 (*new_te)->mode = S_IFLNK;
2008 } else {
2009 (*new_te)->mode = S_IFREG;
2010 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2012 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2013 done:
2014 if (err && *new_te) {
2015 free(*new_te);
2016 *new_te = NULL;
2018 return err;
2021 static const struct got_error *
2022 import_file(struct got_tree_entry **new_te, struct dirent *de,
2023 const char *path, struct got_repository *repo)
2025 const struct got_error *err;
2026 struct got_object_id *blob_id = NULL;
2027 char *filepath;
2028 struct stat sb;
2030 if (asprintf(&filepath, "%s%s%s", path,
2031 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2032 return got_error_from_errno("asprintf");
2034 if (lstat(filepath, &sb) != 0) {
2035 err = got_error_from_errno2("lstat", path);
2036 goto done;
2039 err = got_object_blob_create(&blob_id, filepath, repo);
2040 if (err)
2041 goto done;
2043 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2044 blob_id);
2045 done:
2046 free(filepath);
2047 if (err)
2048 free(blob_id);
2049 return err;
2052 static const struct got_error *
2053 insert_tree_entry(struct got_tree_entry *new_te,
2054 struct got_pathlist_head *paths)
2056 const struct got_error *err = NULL;
2057 struct got_pathlist_entry *new_pe;
2059 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2060 if (err)
2061 return err;
2062 if (new_pe == NULL)
2063 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2064 return NULL;
2067 static const struct got_error *write_tree(struct got_object_id **,
2068 const char *, struct got_pathlist_head *, struct got_repository *,
2069 got_repo_import_cb progress_cb, void *progress_arg);
2071 static const struct got_error *
2072 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2073 const char *path, struct got_pathlist_head *ignores,
2074 struct got_repository *repo,
2075 got_repo_import_cb progress_cb, void *progress_arg)
2077 const struct got_error *err;
2078 struct got_object_id *id = NULL;
2079 char *subdirpath;
2081 if (asprintf(&subdirpath, "%s%s%s", path,
2082 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2083 return got_error_from_errno("asprintf");
2085 (*new_te) = calloc(1, sizeof(**new_te));
2086 if (*new_te == NULL)
2087 return got_error_from_errno("calloc");
2088 (*new_te)->mode = S_IFDIR;
2089 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2090 sizeof((*new_te)->name)) {
2091 err = got_error(GOT_ERR_NO_SPACE);
2092 goto done;
2094 err = write_tree(&id, subdirpath, ignores, repo,
2095 progress_cb, progress_arg);
2096 if (err)
2097 goto done;
2098 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2100 done:
2101 free(id);
2102 free(subdirpath);
2103 if (err) {
2104 free(*new_te);
2105 *new_te = NULL;
2107 return err;
2110 static const struct got_error *
2111 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2112 struct got_pathlist_head *ignores, struct got_repository *repo,
2113 got_repo_import_cb progress_cb, void *progress_arg)
2115 const struct got_error *err = NULL;
2116 DIR *dir;
2117 struct dirent *de;
2118 int nentries;
2119 struct got_tree_entry *new_te = NULL;
2120 struct got_pathlist_head paths;
2121 struct got_pathlist_entry *pe;
2123 *new_tree_id = NULL;
2125 TAILQ_INIT(&paths);
2127 dir = opendir(path_dir);
2128 if (dir == NULL) {
2129 err = got_error_from_errno2("opendir", path_dir);
2130 goto done;
2133 nentries = 0;
2134 while ((de = readdir(dir)) != NULL) {
2135 int ignore = 0;
2136 int type;
2138 if (strcmp(de->d_name, ".") == 0 ||
2139 strcmp(de->d_name, "..") == 0)
2140 continue;
2142 TAILQ_FOREACH(pe, ignores, entry) {
2143 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2144 ignore = 1;
2145 break;
2148 if (ignore)
2149 continue;
2151 err = got_path_dirent_type(&type, path_dir, de);
2152 if (err)
2153 goto done;
2155 if (type == DT_DIR) {
2156 err = import_subdir(&new_te, de, path_dir,
2157 ignores, repo, progress_cb, progress_arg);
2158 if (err) {
2159 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2160 goto done;
2161 err = NULL;
2162 continue;
2164 } else if (type == DT_REG || type == DT_LNK) {
2165 err = import_file(&new_te, de, path_dir, repo);
2166 if (err)
2167 goto done;
2168 } else
2169 continue;
2171 err = insert_tree_entry(new_te, &paths);
2172 if (err)
2173 goto done;
2174 nentries++;
2177 if (TAILQ_EMPTY(&paths)) {
2178 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2179 "cannot create tree without any entries");
2180 goto done;
2183 TAILQ_FOREACH(pe, &paths, entry) {
2184 struct got_tree_entry *te = pe->data;
2185 char *path;
2186 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2187 continue;
2188 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2189 err = got_error_from_errno("asprintf");
2190 goto done;
2192 err = (*progress_cb)(progress_arg, path);
2193 free(path);
2194 if (err)
2195 goto done;
2198 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2199 done:
2200 if (dir)
2201 closedir(dir);
2202 got_pathlist_free(&paths);
2203 return err;
2206 const struct got_error *
2207 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2208 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2209 struct got_repository *repo, got_repo_import_cb progress_cb,
2210 void *progress_arg)
2212 const struct got_error *err;
2213 struct got_object_id *new_tree_id;
2215 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2216 progress_cb, progress_arg);
2217 if (err)
2218 return err;
2220 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2221 author, time(NULL), author, time(NULL), logmsg, repo);
2222 free(new_tree_id);
2223 return err;
2226 const struct got_error *
2227 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2228 struct got_repository *repo)
2230 const struct got_error *err = NULL;
2231 char *path_objects = NULL, *path = NULL;
2232 DIR *dir = NULL;
2233 struct got_object_id id;
2234 int i;
2236 *nobjects = 0;
2237 *ondisk_size = 0;
2239 path_objects = got_repo_get_path_objects(repo);
2240 if (path_objects == NULL)
2241 return got_error_from_errno("got_repo_get_path_objects");
2243 for (i = 0; i <= 0xff; i++) {
2244 struct dirent *dent;
2246 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2247 err = got_error_from_errno("asprintf");
2248 break;
2251 dir = opendir(path);
2252 if (dir == NULL) {
2253 if (errno == ENOENT) {
2254 err = NULL;
2255 continue;
2257 err = got_error_from_errno2("opendir", path);
2258 break;
2261 while ((dent = readdir(dir)) != NULL) {
2262 char *id_str;
2263 int fd;
2264 struct stat sb;
2266 if (strcmp(dent->d_name, ".") == 0 ||
2267 strcmp(dent->d_name, "..") == 0)
2268 continue;
2270 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2271 err = got_error_from_errno("asprintf");
2272 goto done;
2275 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2276 free(id_str);
2277 continue;
2279 free(id_str);
2281 err = got_object_open_loose_fd(&fd, &id, repo);
2282 if (err)
2283 goto done;
2285 if (fstat(fd, &sb) == -1) {
2286 err = got_error_from_errno("fstat");
2287 close(fd);
2288 goto done;
2290 (*nobjects)++;
2291 (*ondisk_size) += sb.st_size;
2293 if (close(fd) == -1) {
2294 err = got_error_from_errno("close");
2295 goto done;
2299 if (closedir(dir) != 0) {
2300 err = got_error_from_errno("closedir");
2301 goto done;
2303 dir = NULL;
2305 free(path);
2306 path = NULL;
2308 done:
2309 if (dir && closedir(dir) != 0 && err == NULL)
2310 err = got_error_from_errno("closedir");
2312 if (err) {
2313 *nobjects = 0;
2314 *ondisk_size = 0;
2316 free(path_objects);
2317 free(path);
2318 return err;
2321 const struct got_error *
2322 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2323 off_t *total_packsize, struct got_repository *repo)
2325 const struct got_error *err = NULL;
2326 DIR *packdir = NULL;
2327 struct dirent *dent;
2328 struct got_packidx *packidx = NULL;
2329 char *path_packidx;
2330 char *path_packfile;
2331 int packdir_fd;
2332 struct stat sb;
2334 *npackfiles = 0;
2335 *nobjects = 0;
2336 *total_packsize = 0;
2338 packdir_fd = openat(got_repo_get_fd(repo),
2339 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2340 if (packdir_fd == -1) {
2341 return got_error_from_errno_fmt("openat: %s/%s",
2342 got_repo_get_path_git_dir(repo),
2343 GOT_OBJECTS_PACK_DIR);
2346 packdir = fdopendir(packdir_fd);
2347 if (packdir == NULL) {
2348 err = got_error_from_errno("fdopendir");
2349 goto done;
2352 while ((dent = readdir(packdir)) != NULL) {
2353 if (!got_repo_is_packidx_filename(dent->d_name,
2354 strlen(dent->d_name)))
2355 continue;
2357 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2358 dent->d_name) == -1) {
2359 err = got_error_from_errno("asprintf");
2360 goto done;
2363 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2364 path_packidx, 0);
2365 free(path_packidx);
2366 if (err)
2367 goto done;
2369 if (fstat(packidx->fd, &sb) == -1)
2370 goto done;
2371 *total_packsize += sb.st_size;
2373 err = got_packidx_get_packfile_path(&path_packfile,
2374 packidx->path_packidx);
2375 if (err)
2376 goto done;
2378 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2379 0) == -1) {
2380 free(path_packfile);
2381 goto done;
2383 free(path_packfile);
2384 *total_packsize += sb.st_size;
2386 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2388 (*npackfiles)++;
2390 got_packidx_close(packidx);
2391 packidx = NULL;
2393 done:
2394 if (packidx)
2395 got_packidx_close(packidx);
2396 if (packdir && closedir(packdir) != 0 && err == NULL)
2397 err = got_error_from_errno("closedir");
2398 if (err) {
2399 *npackfiles = 0;
2400 *nobjects = 0;
2401 *total_packsize = 0;
2403 return err;
2406 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2407 got_packidx_bloom_filter_cmp);