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(), resulting in a close(0).
270 */
271 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
272 pack_fds_tmp[i] = -1;
274 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
275 pack_fds_tmp[i] = got_opentempfd();
276 if (pack_fds_tmp[i] == -1) {
277 err = got_error_from_errno("got_opentempfd");
278 got_repo_pack_fds_close(pack_fds_tmp);
279 return err;
282 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
283 return err;
286 const struct got_error *
287 got_repo_pack_fds_close(int *pack_fds)
289 const struct got_error *err = NULL;
290 int i;
292 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
293 if (pack_fds[i] == -1)
294 continue;
295 if (close(pack_fds[i]) == -1) {
296 err = got_error_from_errno("close");
297 break;
300 free(pack_fds);
301 return err;
304 const struct got_error *
305 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
306 struct got_object *obj)
308 #ifndef GOT_NO_OBJ_CACHE
309 const struct got_error *err = NULL;
310 err = got_object_cache_add(&repo->objcache, id, obj);
311 if (err) {
312 if (err->code == GOT_ERR_OBJ_EXISTS ||
313 err->code == GOT_ERR_OBJ_TOO_LARGE)
314 err = NULL;
315 return err;
317 obj->refcnt++;
318 #endif
319 return NULL;
322 struct got_object *
323 got_repo_get_cached_object(struct got_repository *repo,
324 struct got_object_id *id)
326 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
329 const struct got_error *
330 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
331 struct got_tree_object *tree)
333 #ifndef GOT_NO_OBJ_CACHE
334 const struct got_error *err = NULL;
335 err = got_object_cache_add(&repo->treecache, id, tree);
336 if (err) {
337 if (err->code == GOT_ERR_OBJ_EXISTS ||
338 err->code == GOT_ERR_OBJ_TOO_LARGE)
339 err = NULL;
340 return err;
342 tree->refcnt++;
343 #endif
344 return NULL;
347 struct got_tree_object *
348 got_repo_get_cached_tree(struct got_repository *repo,
349 struct got_object_id *id)
351 return (struct got_tree_object *)got_object_cache_get(
352 &repo->treecache, id);
355 const struct got_error *
356 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
357 struct got_commit_object *commit)
359 #ifndef GOT_NO_OBJ_CACHE
360 const struct got_error *err = NULL;
361 err = got_object_cache_add(&repo->commitcache, id, commit);
362 if (err) {
363 if (err->code == GOT_ERR_OBJ_EXISTS ||
364 err->code == GOT_ERR_OBJ_TOO_LARGE)
365 err = NULL;
366 return err;
368 commit->refcnt++;
369 #endif
370 return NULL;
373 struct got_commit_object *
374 got_repo_get_cached_commit(struct got_repository *repo,
375 struct got_object_id *id)
377 return (struct got_commit_object *)got_object_cache_get(
378 &repo->commitcache, id);
381 const struct got_error *
382 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
383 struct got_tag_object *tag)
385 #ifndef GOT_NO_OBJ_CACHE
386 const struct got_error *err = NULL;
387 err = got_object_cache_add(&repo->tagcache, id, tag);
388 if (err) {
389 if (err->code == GOT_ERR_OBJ_EXISTS ||
390 err->code == GOT_ERR_OBJ_TOO_LARGE)
391 err = NULL;
392 return err;
394 tag->refcnt++;
395 #endif
396 return NULL;
399 struct got_tag_object *
400 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
402 return (struct got_tag_object *)got_object_cache_get(
403 &repo->tagcache, id);
406 const struct got_error *
407 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
408 struct got_raw_object *raw)
410 #ifndef GOT_NO_OBJ_CACHE
411 const struct got_error *err = NULL;
412 err = got_object_cache_add(&repo->rawcache, id, raw);
413 if (err) {
414 if (err->code == GOT_ERR_OBJ_EXISTS ||
415 err->code == GOT_ERR_OBJ_TOO_LARGE)
416 err = NULL;
417 return err;
419 raw->refcnt++;
420 #endif
421 return NULL;
425 struct got_raw_object *
426 got_repo_get_cached_raw_object(struct got_repository *repo,
427 struct got_object_id *id)
429 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
433 static const struct got_error *
434 open_repo(struct got_repository *repo, const char *path)
436 const struct got_error *err = NULL;
438 repo->gitdir_fd = -1;
440 /* bare git repository? */
441 repo->path_git_dir = strdup(path);
442 if (repo->path_git_dir == NULL)
443 return got_error_from_errno("strdup");
444 if (is_git_repo(repo)) {
445 repo->path = strdup(repo->path_git_dir);
446 if (repo->path == NULL) {
447 err = got_error_from_errno("strdup");
448 goto done;
450 repo->gitdir_fd = open(repo->path_git_dir,
451 O_DIRECTORY | O_CLOEXEC);
452 if (repo->gitdir_fd == -1) {
453 err = got_error_from_errno2("open",
454 repo->path_git_dir);
455 goto done;
457 return NULL;
460 /* git repository with working tree? */
461 free(repo->path_git_dir);
462 repo->path_git_dir = NULL;
463 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
464 err = got_error_from_errno("asprintf");
465 goto done;
467 if (is_git_repo(repo)) {
468 repo->path = strdup(path);
469 if (repo->path == NULL) {
470 err = got_error_from_errno("strdup");
471 goto done;
473 repo->gitdir_fd = open(repo->path_git_dir,
474 O_DIRECTORY | O_CLOEXEC);
475 if (repo->gitdir_fd == -1) {
476 err = got_error_from_errno2("open",
477 repo->path_git_dir);
478 goto done;
480 return NULL;
483 err = got_error(GOT_ERR_NOT_GIT_REPO);
484 done:
485 if (err) {
486 free(repo->path);
487 repo->path = NULL;
488 free(repo->path_git_dir);
489 repo->path_git_dir = NULL;
490 if (repo->gitdir_fd != -1)
491 close(repo->gitdir_fd);
492 repo->gitdir_fd = -1;
495 return err;
498 static const struct got_error *
499 parse_gitconfig_file(int *gitconfig_repository_format_version,
500 char **gitconfig_author_name, char **gitconfig_author_email,
501 struct got_remote_repo **remotes, int *nremotes,
502 char **gitconfig_owner, char ***extensions, int *nextensions,
503 const char *gitconfig_path)
505 const struct got_error *err = NULL, *child_err = NULL;
506 int fd = -1;
507 int imsg_fds[2] = { -1, -1 };
508 pid_t pid;
509 struct imsgbuf *ibuf;
511 *gitconfig_repository_format_version = 0;
512 if (extensions)
513 *extensions = NULL;
514 if (nextensions)
515 *nextensions = 0;
516 *gitconfig_author_name = NULL;
517 *gitconfig_author_email = NULL;
518 if (remotes)
519 *remotes = NULL;
520 if (nremotes)
521 *nremotes = 0;
522 if (gitconfig_owner)
523 *gitconfig_owner = NULL;
525 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
526 if (fd == -1) {
527 if (errno == ENOENT)
528 return NULL;
529 return got_error_from_errno2("open", gitconfig_path);
532 ibuf = calloc(1, sizeof(*ibuf));
533 if (ibuf == NULL) {
534 err = got_error_from_errno("calloc");
535 goto done;
538 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
539 err = got_error_from_errno("socketpair");
540 goto done;
543 pid = fork();
544 if (pid == -1) {
545 err = got_error_from_errno("fork");
546 goto done;
547 } else if (pid == 0) {
548 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
549 gitconfig_path);
550 /* not reached */
553 if (close(imsg_fds[1]) == -1) {
554 err = got_error_from_errno("close");
555 goto done;
557 imsg_fds[1] = -1;
558 imsg_init(ibuf, imsg_fds[0]);
560 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
561 if (err)
562 goto done;
563 fd = -1;
565 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
566 if (err)
567 goto done;
569 err = got_privsep_recv_gitconfig_int(
570 gitconfig_repository_format_version, ibuf);
571 if (err)
572 goto done;
574 if (extensions && nextensions) {
575 err = got_privsep_send_gitconfig_repository_extensions_req(
576 ibuf);
577 if (err)
578 goto done;
579 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
580 if (err)
581 goto done;
582 if (*nextensions > 0) {
583 int i;
584 *extensions = calloc(*nextensions, sizeof(char *));
585 if (*extensions == NULL) {
586 err = got_error_from_errno("calloc");
587 goto done;
589 for (i = 0; i < *nextensions; i++) {
590 char *ext;
591 err = got_privsep_recv_gitconfig_str(&ext,
592 ibuf);
593 if (err)
594 goto done;
595 (*extensions)[i] = ext;
600 err = got_privsep_send_gitconfig_author_name_req(ibuf);
601 if (err)
602 goto done;
604 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
605 if (err)
606 goto done;
608 err = got_privsep_send_gitconfig_author_email_req(ibuf);
609 if (err)
610 goto done;
612 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
613 if (err)
614 goto done;
616 if (remotes && nremotes) {
617 err = got_privsep_send_gitconfig_remotes_req(ibuf);
618 if (err)
619 goto done;
621 err = got_privsep_recv_gitconfig_remotes(remotes,
622 nremotes, ibuf);
623 if (err)
624 goto done;
627 if (gitconfig_owner) {
628 err = got_privsep_send_gitconfig_owner_req(ibuf);
629 if (err)
630 goto done;
631 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
632 if (err)
633 goto done;
636 err = got_privsep_send_stop(imsg_fds[0]);
637 child_err = got_privsep_wait_for_child(pid);
638 if (child_err && err == NULL)
639 err = child_err;
640 done:
641 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
642 err = got_error_from_errno("close");
643 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
644 err = got_error_from_errno("close");
645 if (fd != -1 && close(fd) == -1 && err == NULL)
646 err = got_error_from_errno2("close", gitconfig_path);
647 free(ibuf);
648 return err;
651 static const struct got_error *
652 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
654 const struct got_error *err = NULL;
655 char *repo_gitconfig_path = NULL;
657 if (global_gitconfig_path) {
658 /* Read settings from ~/.gitconfig. */
659 int dummy_repo_version;
660 err = parse_gitconfig_file(&dummy_repo_version,
661 &repo->global_gitconfig_author_name,
662 &repo->global_gitconfig_author_email,
663 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
664 if (err)
665 return err;
668 /* Read repository's .git/config file. */
669 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
670 if (repo_gitconfig_path == NULL)
671 return got_error_from_errno("got_repo_get_path_gitconfig");
673 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
674 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
675 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
676 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
677 repo_gitconfig_path);
678 if (err)
679 goto done;
680 done:
681 free(repo_gitconfig_path);
682 return err;
685 static const struct got_error *
686 read_gotconfig(struct got_repository *repo)
688 const struct got_error *err = NULL;
689 char *gotconfig_path;
691 gotconfig_path = got_repo_get_path_gotconfig(repo);
692 if (gotconfig_path == NULL)
693 return got_error_from_errno("got_repo_get_path_gotconfig");
695 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
696 free(gotconfig_path);
697 return err;
700 /* Supported repository format extensions. */
701 static const char *const repo_extensions[] = {
702 "noop", /* Got supports repository format version 1. */
703 "preciousObjects", /* Supported by gotadmin cleanup. */
704 "worktreeConfig", /* Got does not care about Git work trees. */
705 };
707 const struct got_error *
708 got_repo_open(struct got_repository **repop, const char *path,
709 const char *global_gitconfig_path, int *pack_fds)
711 struct got_repository *repo = NULL;
712 const struct got_error *err = NULL;
713 char *repo_path = NULL;
714 size_t i, j = 0;
715 struct rlimit rl;
717 *repop = NULL;
719 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
720 return got_error_from_errno("getrlimit");
722 repo = calloc(1, sizeof(*repo));
723 if (repo == NULL)
724 return got_error_from_errno("calloc");
726 RB_INIT(&repo->packidx_bloom_filters);
727 TAILQ_INIT(&repo->packidx_paths);
729 for (i = 0; i < nitems(repo->privsep_children); i++) {
730 memset(&repo->privsep_children[i], 0,
731 sizeof(repo->privsep_children[0]));
732 repo->privsep_children[i].imsg_fd = -1;
735 err = got_object_cache_init(&repo->objcache,
736 GOT_OBJECT_CACHE_TYPE_OBJ);
737 if (err)
738 goto done;
739 err = got_object_cache_init(&repo->treecache,
740 GOT_OBJECT_CACHE_TYPE_TREE);
741 if (err)
742 goto done;
743 err = got_object_cache_init(&repo->commitcache,
744 GOT_OBJECT_CACHE_TYPE_COMMIT);
745 if (err)
746 goto done;
747 err = got_object_cache_init(&repo->tagcache,
748 GOT_OBJECT_CACHE_TYPE_TAG);
749 if (err)
750 goto done;
751 err = got_object_cache_init(&repo->rawcache,
752 GOT_OBJECT_CACHE_TYPE_RAW);
753 if (err)
754 goto done;
756 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
757 if (repo->pack_cache_size > rl.rlim_cur / 8)
758 repo->pack_cache_size = rl.rlim_cur / 8;
759 for (i = 0; i < nitems(repo->packs); i++) {
760 if (i < repo->pack_cache_size) {
761 repo->packs[i].basefd = pack_fds[j++];
762 repo->packs[i].accumfd = pack_fds[j++];
763 } else {
764 repo->packs[i].basefd = -1;
765 repo->packs[i].accumfd = -1;
768 repo->pinned_pack = -1;
769 repo->pinned_packidx = -1;
770 repo->pinned_pid = 0;
772 repo_path = realpath(path, NULL);
773 if (repo_path == NULL) {
774 err = got_error_from_errno2("realpath", path);
775 goto done;
778 for (;;) {
779 char *parent_path;
781 err = open_repo(repo, repo_path);
782 if (err == NULL)
783 break;
784 if (err->code != GOT_ERR_NOT_GIT_REPO)
785 goto done;
786 if (repo_path[0] == '/' && repo_path[1] == '\0') {
787 err = got_error(GOT_ERR_NOT_GIT_REPO);
788 goto done;
790 err = got_path_dirname(&parent_path, repo_path);
791 if (err)
792 goto done;
793 free(repo_path);
794 repo_path = parent_path;
797 err = read_gotconfig(repo);
798 if (err)
799 goto done;
801 err = read_gitconfig(repo, global_gitconfig_path);
802 if (err)
803 goto done;
804 if (repo->gitconfig_repository_format_version != 0) {
805 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
806 goto done;
808 for (i = 0; i < repo->nextensions; i++) {
809 char *ext = repo->extensions[i];
810 int j, supported = 0;
811 for (j = 0; j < nitems(repo_extensions); j++) {
812 if (strcmp(ext, repo_extensions[j]) == 0) {
813 supported = 1;
814 break;
817 if (!supported) {
818 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
819 goto done;
823 err = got_repo_list_packidx(&repo->packidx_paths, repo);
824 done:
825 if (err)
826 got_repo_close(repo);
827 else
828 *repop = repo;
829 free(repo_path);
830 return err;
833 const struct got_error *
834 got_repo_close(struct got_repository *repo)
836 const struct got_error *err = NULL, *child_err;
837 struct got_packidx_bloom_filter *bf;
838 struct got_pathlist_entry *pe;
839 size_t i;
841 for (i = 0; i < repo->pack_cache_size; i++) {
842 if (repo->packidx_cache[i] == NULL)
843 break;
844 got_packidx_close(repo->packidx_cache[i]);
847 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
848 &repo->packidx_bloom_filters))) {
849 RB_REMOVE(got_packidx_bloom_filter_tree,
850 &repo->packidx_bloom_filters, bf);
851 free(bf->bloom);
852 free(bf);
855 for (i = 0; i < repo->pack_cache_size; i++)
856 if (repo->packs[i].path_packfile)
857 if (repo->packs[i].path_packfile)
858 got_pack_close(&repo->packs[i]);
860 free(repo->path);
861 free(repo->path_git_dir);
863 got_object_cache_close(&repo->objcache);
864 got_object_cache_close(&repo->treecache);
865 got_object_cache_close(&repo->commitcache);
866 got_object_cache_close(&repo->tagcache);
867 got_object_cache_close(&repo->rawcache);
869 for (i = 0; i < nitems(repo->privsep_children); i++) {
870 if (repo->privsep_children[i].imsg_fd == -1)
871 continue;
872 imsg_clear(repo->privsep_children[i].ibuf);
873 free(repo->privsep_children[i].ibuf);
874 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
875 child_err = got_privsep_wait_for_child(
876 repo->privsep_children[i].pid);
877 if (child_err && err == NULL)
878 err = child_err;
879 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
880 err == NULL)
881 err = got_error_from_errno("close");
884 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
885 err == NULL)
886 err = got_error_from_errno("close");
888 if (repo->gotconfig)
889 got_gotconfig_free(repo->gotconfig);
890 free(repo->gitconfig_author_name);
891 free(repo->gitconfig_author_email);
892 for (i = 0; i < repo->ngitconfig_remotes; i++)
893 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
894 free(repo->gitconfig_remotes);
895 for (i = 0; i < repo->nextensions; i++)
896 free(repo->extensions[i]);
897 free(repo->extensions);
899 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
900 free((void *)pe->path);
901 got_pathlist_free(&repo->packidx_paths);
902 free(repo);
904 return err;
907 void
908 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
910 int i;
912 free(repo->name);
913 repo->name = NULL;
914 free(repo->fetch_url);
915 repo->fetch_url = NULL;
916 free(repo->send_url);
917 repo->send_url = NULL;
918 for (i = 0; i < repo->nfetch_branches; i++)
919 free(repo->fetch_branches[i]);
920 free(repo->fetch_branches);
921 repo->fetch_branches = NULL;
922 repo->nfetch_branches = 0;
923 for (i = 0; i < repo->nsend_branches; i++)
924 free(repo->send_branches[i]);
925 free(repo->send_branches);
926 repo->send_branches = NULL;
927 repo->nsend_branches = 0;
930 const struct got_error *
931 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
932 const char *input_path)
934 const struct got_error *err = NULL;
935 const char *repo_abspath = NULL;
936 size_t repolen, len;
937 char *canonpath, *path = NULL;
939 *in_repo_path = NULL;
941 canonpath = strdup(input_path);
942 if (canonpath == NULL) {
943 err = got_error_from_errno("strdup");
944 goto done;
946 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
947 if (err)
948 goto done;
950 repo_abspath = got_repo_get_path(repo);
952 if (canonpath[0] == '\0') {
953 path = strdup(canonpath);
954 if (path == NULL) {
955 err = got_error_from_errno("strdup");
956 goto done;
958 } else {
959 path = realpath(canonpath, NULL);
960 if (path == NULL) {
961 if (errno != ENOENT) {
962 err = got_error_from_errno2("realpath",
963 canonpath);
964 goto done;
966 /*
967 * Path is not on disk.
968 * Assume it is already relative to repository root.
969 */
970 path = strdup(canonpath);
971 if (path == NULL) {
972 err = got_error_from_errno("strdup");
973 goto done;
977 repolen = strlen(repo_abspath);
978 len = strlen(path);
981 if (strcmp(path, repo_abspath) == 0) {
982 free(path);
983 path = strdup("");
984 if (path == NULL) {
985 err = got_error_from_errno("strdup");
986 goto done;
988 } else if (len > repolen &&
989 got_path_is_child(path, repo_abspath, repolen)) {
990 /* Matched an on-disk path inside repository. */
991 if (got_repo_is_bare(repo)) {
992 /*
993 * Matched an on-disk path inside repository
994 * database. Treat input as repository-relative.
995 */
996 free(path);
997 path = canonpath;
998 canonpath = NULL;
999 } else {
1000 char *child;
1001 /* Strip common prefix with repository path. */
1002 err = got_path_skip_common_ancestor(&child,
1003 repo_abspath, path);
1004 if (err)
1005 goto done;
1006 free(path);
1007 path = child;
1009 } else {
1011 * Matched unrelated on-disk path.
1012 * Treat input as repository-relative.
1014 free(path);
1015 path = canonpath;
1016 canonpath = NULL;
1020 /* Make in-repository path absolute */
1021 if (path[0] != '/') {
1022 char *abspath;
1023 if (asprintf(&abspath, "/%s", path) == -1) {
1024 err = got_error_from_errno("asprintf");
1025 goto done;
1027 free(path);
1028 path = abspath;
1031 done:
1032 free(canonpath);
1033 if (err)
1034 free(path);
1035 else
1036 *in_repo_path = path;
1037 return err;
1040 static const struct got_error *
1041 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1042 const char *path_packidx)
1044 const struct got_error *err = NULL;
1045 size_t i;
1047 for (i = 0; i < repo->pack_cache_size; i++) {
1048 if (repo->packidx_cache[i] == NULL)
1049 break;
1050 if (strcmp(repo->packidx_cache[i]->path_packidx,
1051 path_packidx) == 0) {
1052 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1055 if (i == repo->pack_cache_size) {
1056 do {
1057 i--;
1058 } while (i > 0 && repo->pinned_packidx >= 0 &&
1059 i == repo->pinned_packidx);
1060 err = got_packidx_close(repo->packidx_cache[i]);
1061 if (err)
1062 return err;
1065 repo->packidx_cache[i] = packidx;
1067 return NULL;
1070 int
1071 got_repo_is_packidx_filename(const char *name, size_t len)
1073 if (len != GOT_PACKIDX_NAMELEN)
1074 return 0;
1076 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1077 return 0;
1079 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1080 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1081 return 0;
1083 return 1;
1086 static struct got_packidx_bloom_filter *
1087 get_packidx_bloom_filter(struct got_repository *repo,
1088 const char *path, size_t path_len)
1090 struct got_packidx_bloom_filter key;
1092 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1093 return NULL; /* XXX */
1094 key.path_len = path_len;
1096 return RB_FIND(got_packidx_bloom_filter_tree,
1097 &repo->packidx_bloom_filters, &key);
1100 int
1101 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1102 const char *path_packidx, struct got_object_id *id)
1104 struct got_packidx_bloom_filter *bf;
1106 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1107 if (bf)
1108 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1110 /* No bloom filter means this pack index must be searched. */
1111 return 1;
1114 static const struct got_error *
1115 add_packidx_bloom_filter(struct got_repository *repo,
1116 struct got_packidx *packidx, const char *path_packidx)
1118 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1119 struct got_packidx_bloom_filter *bf;
1120 size_t len;
1123 * Don't use bloom filters for very large pack index files.
1124 * Large pack files will contain a relatively large fraction
1125 * of our objects so we will likely need to visit them anyway.
1126 * The more objects a pack file contains the higher the probability
1127 * of a false-positive match from the bloom filter. And reading
1128 * all object IDs from a large pack index file can be expensive.
1130 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1131 return NULL;
1133 /* Do we already have a filter for this pack index? */
1134 if (get_packidx_bloom_filter(repo, path_packidx,
1135 strlen(path_packidx)) != NULL)
1136 return NULL;
1138 bf = calloc(1, sizeof(*bf));
1139 if (bf == NULL)
1140 return got_error_from_errno("calloc");
1141 bf->bloom = calloc(1, sizeof(*bf->bloom));
1142 if (bf->bloom == NULL) {
1143 free(bf);
1144 return got_error_from_errno("calloc");
1147 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1148 if (len >= sizeof(bf->path)) {
1149 free(bf->bloom);
1150 free(bf);
1151 return got_error(GOT_ERR_NO_SPACE);
1153 bf->path_len = len;
1155 /* Minimum size supported by our bloom filter is 1000 entries. */
1156 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1157 for (i = 0; i < nobjects; i++) {
1158 struct got_packidx_object_id *id;
1159 id = &packidx->hdr.sorted_ids[i];
1160 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1163 RB_INSERT(got_packidx_bloom_filter_tree,
1164 &repo->packidx_bloom_filters, bf);
1165 return NULL;
1168 const struct got_error *
1169 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1170 struct got_repository *repo, struct got_object_id *id)
1172 const struct got_error *err;
1173 struct got_pathlist_entry *pe;
1174 size_t i;
1176 /* Search pack index cache. */
1177 for (i = 0; i < repo->pack_cache_size; i++) {
1178 if (repo->packidx_cache[i] == NULL)
1179 break;
1180 if (!got_repo_check_packidx_bloom_filter(repo,
1181 repo->packidx_cache[i]->path_packidx, id))
1182 continue; /* object will not be found in this index */
1183 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1184 if (*idx != -1) {
1185 *packidx = repo->packidx_cache[i];
1187 * Move this cache entry to the front. Repeatedly
1188 * searching a wrong pack index can be expensive.
1190 if (i > 0) {
1191 memmove(&repo->packidx_cache[1],
1192 &repo->packidx_cache[0],
1193 i * sizeof(repo->packidx_cache[0]));
1194 repo->packidx_cache[0] = *packidx;
1195 if (repo->pinned_packidx >= 0 &&
1196 repo->pinned_packidx < i)
1197 repo->pinned_packidx++;
1198 else if (repo->pinned_packidx == i)
1199 repo->pinned_packidx = 0;
1201 return NULL;
1204 /* No luck. Search the filesystem. */
1206 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1207 const char *path_packidx = pe->path;
1208 int is_cached = 0;
1210 if (!got_repo_check_packidx_bloom_filter(repo,
1211 pe->path, id))
1212 continue; /* object will not be found in this index */
1214 for (i = 0; i < repo->pack_cache_size; i++) {
1215 if (repo->packidx_cache[i] == NULL)
1216 break;
1217 if (strcmp(repo->packidx_cache[i]->path_packidx,
1218 path_packidx) == 0) {
1219 is_cached = 1;
1220 break;
1223 if (is_cached)
1224 continue; /* already searched */
1226 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1227 path_packidx, 0);
1228 if (err)
1229 goto done;
1231 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1232 if (err)
1233 goto done;
1235 err = cache_packidx(repo, *packidx, path_packidx);
1236 if (err)
1237 goto done;
1239 *idx = got_packidx_get_object_idx(*packidx, id);
1240 if (*idx != -1) {
1241 err = NULL; /* found the object */
1242 goto done;
1246 err = got_error_no_obj(id);
1247 done:
1248 return err;
1251 const struct got_error *
1252 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1253 struct got_repository *repo)
1255 const struct got_error *err = NULL;
1256 DIR *packdir = NULL;
1257 struct dirent *dent;
1258 char *path_packidx = NULL;
1259 int packdir_fd;
1261 packdir_fd = openat(got_repo_get_fd(repo),
1262 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1263 if (packdir_fd == -1) {
1264 return got_error_from_errno_fmt("openat: %s/%s",
1265 got_repo_get_path_git_dir(repo),
1266 GOT_OBJECTS_PACK_DIR);
1269 packdir = fdopendir(packdir_fd);
1270 if (packdir == NULL) {
1271 err = got_error_from_errno("fdopendir");
1272 goto done;
1275 while ((dent = readdir(packdir)) != NULL) {
1276 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1277 continue;
1279 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1280 dent->d_name) == -1) {
1281 err = got_error_from_errno("asprintf");
1282 path_packidx = NULL;
1283 break;
1286 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1287 if (err)
1288 break;
1290 done:
1291 if (err)
1292 free(path_packidx);
1293 if (packdir && closedir(packdir) != 0 && err == NULL)
1294 err = got_error_from_errno("closedir");
1295 return err;
1298 const struct got_error *
1299 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1300 struct got_repository *repo)
1302 const struct got_error *err;
1303 size_t i;
1305 *packidx = NULL;
1307 /* Search pack index cache. */
1308 for (i = 0; i < repo->pack_cache_size; i++) {
1309 if (repo->packidx_cache[i] == NULL)
1310 break;
1311 if (strcmp(repo->packidx_cache[i]->path_packidx,
1312 path_packidx) == 0) {
1313 *packidx = repo->packidx_cache[i];
1314 return NULL;
1317 /* No luck. Search the filesystem. */
1319 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1320 path_packidx, 0);
1321 if (err)
1322 return err;
1324 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1325 if (err)
1326 goto done;
1328 err = cache_packidx(repo, *packidx, path_packidx);
1329 done:
1330 if (err) {
1331 got_packidx_close(*packidx);
1332 *packidx = NULL;
1334 return err;
1337 static const struct got_error *
1338 read_packfile_hdr(int fd, struct got_packidx *packidx)
1340 const struct got_error *err = NULL;
1341 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1342 struct got_packfile_hdr hdr;
1343 ssize_t n;
1345 n = read(fd, &hdr, sizeof(hdr));
1346 if (n < 0)
1347 return got_error_from_errno("read");
1348 if (n != sizeof(hdr))
1349 return got_error(GOT_ERR_BAD_PACKFILE);
1351 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1352 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1353 be32toh(hdr.nobjects) != totobj)
1354 err = got_error(GOT_ERR_BAD_PACKFILE);
1356 return err;
1359 static const struct got_error *
1360 open_packfile(int *fd, struct got_repository *repo,
1361 const char *relpath, struct got_packidx *packidx)
1363 const struct got_error *err = NULL;
1365 *fd = openat(got_repo_get_fd(repo), relpath,
1366 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1367 if (*fd == -1)
1368 return got_error_from_errno_fmt("openat: %s/%s",
1369 got_repo_get_path_git_dir(repo), relpath);
1371 if (packidx) {
1372 err = read_packfile_hdr(*fd, packidx);
1373 if (err) {
1374 close(*fd);
1375 *fd = -1;
1379 return err;
1382 const struct got_error *
1383 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1384 const char *path_packfile, struct got_packidx *packidx)
1386 const struct got_error *err = NULL;
1387 struct got_pack *pack = NULL;
1388 struct stat sb;
1389 size_t i;
1391 if (packp)
1392 *packp = NULL;
1394 for (i = 0; i < repo->pack_cache_size; i++) {
1395 pack = &repo->packs[i];
1396 if (pack->path_packfile == NULL)
1397 break;
1398 if (strcmp(pack->path_packfile, path_packfile) == 0)
1399 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1402 if (i == repo->pack_cache_size) {
1403 struct got_pack tmp;
1404 do {
1405 i--;
1406 } while (i > 0 && repo->pinned_pack >= 0 &&
1407 i == repo->pinned_pack);
1408 err = got_pack_close(&repo->packs[i]);
1409 if (err)
1410 return err;
1411 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1412 return got_error_from_errno("ftruncate");
1413 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1414 return got_error_from_errno("ftruncate");
1415 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1416 memcpy(&repo->packs[i], &repo->packs[0],
1417 sizeof(repo->packs[i]));
1418 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1419 if (repo->pinned_pack == 0)
1420 repo->pinned_pack = i;
1421 else if (repo->pinned_pack == i)
1422 repo->pinned_pack = 0;
1423 i = 0;
1426 pack = &repo->packs[i];
1428 pack->path_packfile = strdup(path_packfile);
1429 if (pack->path_packfile == NULL) {
1430 err = got_error_from_errno("strdup");
1431 goto done;
1434 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1435 if (err)
1436 goto done;
1438 if (fstat(pack->fd, &sb) != 0) {
1439 err = got_error_from_errno("fstat");
1440 goto done;
1442 pack->filesize = sb.st_size;
1444 pack->privsep_child = NULL;
1446 #ifndef GOT_PACK_NO_MMAP
1447 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1448 pack->fd, 0);
1449 if (pack->map == MAP_FAILED) {
1450 if (errno != ENOMEM) {
1451 err = got_error_from_errno("mmap");
1452 goto done;
1454 pack->map = NULL; /* fall back to read(2) */
1456 #endif
1457 done:
1458 if (err) {
1459 if (pack) {
1460 free(pack->path_packfile);
1461 memset(pack, 0, sizeof(*pack));
1463 } else if (packp)
1464 *packp = pack;
1465 return err;
1468 struct got_pack *
1469 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1471 struct got_pack *pack = NULL;
1472 size_t i;
1474 for (i = 0; i < repo->pack_cache_size; i++) {
1475 pack = &repo->packs[i];
1476 if (pack->path_packfile == NULL)
1477 break;
1478 if (strcmp(pack->path_packfile, path_packfile) == 0)
1479 return pack;
1482 return NULL;
1485 const struct got_error *
1486 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1487 struct got_pack *pack)
1489 size_t i;
1490 int pinned_pack = -1, pinned_packidx = -1;
1492 for (i = 0; i < repo->pack_cache_size; i++) {
1493 if (repo->packidx_cache[i] &&
1494 strcmp(repo->packidx_cache[i]->path_packidx,
1495 packidx->path_packidx) == 0)
1496 pinned_packidx = i;
1497 if (repo->packs[i].path_packfile &&
1498 strcmp(repo->packs[i].path_packfile,
1499 pack->path_packfile) == 0)
1500 pinned_pack = i;
1503 if (pinned_packidx == -1 || pinned_pack == -1)
1504 return got_error(GOT_ERR_PIN_PACK);
1506 repo->pinned_pack = pinned_pack;
1507 repo->pinned_packidx = pinned_packidx;
1508 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1509 return NULL;
1512 struct got_pack *
1513 got_repo_get_pinned_pack(struct got_repository *repo)
1515 if (repo->pinned_pack >= 0 &&
1516 repo->pinned_pack < repo->pack_cache_size)
1517 return &repo->packs[repo->pinned_pack];
1519 return NULL;
1522 void
1523 got_repo_unpin_pack(struct got_repository *repo)
1525 repo->pinned_packidx = -1;
1526 repo->pinned_pack = -1;
1527 repo->pinned_pid = 0;
1530 const struct got_error *
1531 got_repo_init(const char *repo_path)
1533 const struct got_error *err = NULL;
1534 const char *dirnames[] = {
1535 GOT_OBJECTS_DIR,
1536 GOT_OBJECTS_PACK_DIR,
1537 GOT_REFS_DIR,
1539 const char *description_str = "Unnamed repository; "
1540 "edit this file 'description' to name the repository.";
1541 const char *headref_str = "ref: refs/heads/main";
1542 const char *gitconfig_str = "[core]\n"
1543 "\trepositoryformatversion = 0\n"
1544 "\tfilemode = true\n"
1545 "\tbare = true\n";
1546 char *path;
1547 size_t i;
1549 if (!got_path_dir_is_empty(repo_path))
1550 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1552 for (i = 0; i < nitems(dirnames); i++) {
1553 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1554 return got_error_from_errno("asprintf");
1556 err = got_path_mkdir(path);
1557 free(path);
1558 if (err)
1559 return err;
1562 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1563 return got_error_from_errno("asprintf");
1564 err = got_path_create_file(path, description_str);
1565 free(path);
1566 if (err)
1567 return err;
1569 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1570 return got_error_from_errno("asprintf");
1571 err = got_path_create_file(path, headref_str);
1572 free(path);
1573 if (err)
1574 return err;
1576 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1577 return got_error_from_errno("asprintf");
1578 err = got_path_create_file(path, gitconfig_str);
1579 free(path);
1580 if (err)
1581 return err;
1583 return NULL;
1586 static const struct got_error *
1587 match_packed_object(struct got_object_id **unique_id,
1588 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1590 const struct got_error *err = NULL;
1591 struct got_object_id_queue matched_ids;
1592 struct got_pathlist_entry *pe;
1594 STAILQ_INIT(&matched_ids);
1596 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1597 const char *path_packidx = pe->path;
1598 struct got_packidx *packidx;
1599 struct got_object_qid *qid;
1601 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1602 path_packidx, 0);
1603 if (err)
1604 break;
1606 err = got_packidx_match_id_str_prefix(&matched_ids,
1607 packidx, id_str_prefix);
1608 if (err) {
1609 got_packidx_close(packidx);
1610 break;
1612 err = got_packidx_close(packidx);
1613 if (err)
1614 break;
1616 STAILQ_FOREACH(qid, &matched_ids, entry) {
1617 if (obj_type != GOT_OBJ_TYPE_ANY) {
1618 int matched_type;
1619 err = got_object_get_type(&matched_type, repo,
1620 &qid->id);
1621 if (err)
1622 goto done;
1623 if (matched_type != obj_type)
1624 continue;
1626 if (*unique_id == NULL) {
1627 *unique_id = got_object_id_dup(&qid->id);
1628 if (*unique_id == NULL) {
1629 err = got_error_from_errno("malloc");
1630 goto done;
1632 } else {
1633 if (got_object_id_cmp(*unique_id,
1634 &qid->id) == 0)
1635 continue; /* packed multiple times */
1636 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1637 goto done;
1641 done:
1642 got_object_id_queue_free(&matched_ids);
1643 if (err) {
1644 free(*unique_id);
1645 *unique_id = NULL;
1647 return err;
1650 static const struct got_error *
1651 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1652 const char *object_dir, const char *id_str_prefix, int obj_type,
1653 struct got_repository *repo)
1655 const struct got_error *err = NULL;
1656 char *path;
1657 DIR *dir = NULL;
1658 struct dirent *dent;
1659 struct got_object_id id;
1661 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1662 err = got_error_from_errno("asprintf");
1663 goto done;
1666 dir = opendir(path);
1667 if (dir == NULL) {
1668 if (errno == ENOENT) {
1669 err = NULL;
1670 goto done;
1672 err = got_error_from_errno2("opendir", path);
1673 goto done;
1675 while ((dent = readdir(dir)) != NULL) {
1676 char *id_str;
1677 int cmp;
1679 if (strcmp(dent->d_name, ".") == 0 ||
1680 strcmp(dent->d_name, "..") == 0)
1681 continue;
1683 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1684 err = got_error_from_errno("asprintf");
1685 goto done;
1688 if (!got_parse_sha1_digest(id.sha1, id_str))
1689 continue;
1692 * Directory entries do not necessarily appear in
1693 * sorted order, so we must iterate over all of them.
1695 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1696 if (cmp != 0) {
1697 free(id_str);
1698 continue;
1701 if (*unique_id == NULL) {
1702 if (obj_type != GOT_OBJ_TYPE_ANY) {
1703 int matched_type;
1704 err = got_object_get_type(&matched_type, repo,
1705 &id);
1706 if (err)
1707 goto done;
1708 if (matched_type != obj_type)
1709 continue;
1711 *unique_id = got_object_id_dup(&id);
1712 if (*unique_id == NULL) {
1713 err = got_error_from_errno("got_object_id_dup");
1714 free(id_str);
1715 goto done;
1717 } else {
1718 if (got_object_id_cmp(*unique_id, &id) == 0)
1719 continue; /* both packed and loose */
1720 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1721 free(id_str);
1722 goto done;
1725 done:
1726 if (dir && closedir(dir) != 0 && err == NULL)
1727 err = got_error_from_errno("closedir");
1728 if (err) {
1729 free(*unique_id);
1730 *unique_id = NULL;
1732 free(path);
1733 return err;
1736 const struct got_error *
1737 got_repo_match_object_id_prefix(struct got_object_id **id,
1738 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1740 const struct got_error *err = NULL;
1741 char *path_objects = got_repo_get_path_objects(repo);
1742 char *object_dir = NULL;
1743 size_t len;
1744 int i;
1746 *id = NULL;
1748 len = strlen(id_str_prefix);
1749 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1750 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1752 for (i = 0; i < len; i++) {
1753 if (isxdigit((unsigned char)id_str_prefix[i]))
1754 continue;
1755 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1758 if (len >= 2) {
1759 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1760 if (err)
1761 goto done;
1762 object_dir = strndup(id_str_prefix, 2);
1763 if (object_dir == NULL) {
1764 err = got_error_from_errno("strdup");
1765 goto done;
1767 err = match_loose_object(id, path_objects, object_dir,
1768 id_str_prefix, obj_type, repo);
1769 } else if (len == 1) {
1770 int i;
1771 for (i = 0; i < 0xf; i++) {
1772 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1773 == -1) {
1774 err = got_error_from_errno("asprintf");
1775 goto done;
1777 err = match_packed_object(id, repo, object_dir,
1778 obj_type);
1779 if (err)
1780 goto done;
1781 err = match_loose_object(id, path_objects, object_dir,
1782 id_str_prefix, obj_type, repo);
1783 if (err)
1784 goto done;
1786 } else {
1787 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1788 goto done;
1790 done:
1791 free(object_dir);
1792 if (err) {
1793 free(*id);
1794 *id = NULL;
1795 } else if (*id == NULL) {
1796 switch (obj_type) {
1797 case GOT_OBJ_TYPE_BLOB:
1798 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1799 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1800 break;
1801 case GOT_OBJ_TYPE_TREE:
1802 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1803 GOT_OBJ_LABEL_TREE, id_str_prefix);
1804 break;
1805 case GOT_OBJ_TYPE_COMMIT:
1806 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1807 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1808 break;
1809 case GOT_OBJ_TYPE_TAG:
1810 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1811 GOT_OBJ_LABEL_TAG, id_str_prefix);
1812 break;
1813 default:
1814 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1815 break;
1819 return err;
1822 const struct got_error *
1823 got_repo_match_object_id(struct got_object_id **id, char **label,
1824 const char *id_str, int obj_type, struct got_reflist_head *refs,
1825 struct got_repository *repo)
1827 const struct got_error *err;
1828 struct got_tag_object *tag;
1829 struct got_reference *ref = NULL;
1831 *id = NULL;
1832 if (label)
1833 *label = NULL;
1835 if (refs) {
1836 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1837 refs, repo);
1838 if (err == NULL) {
1839 *id = got_object_id_dup(
1840 got_object_tag_get_object_id(tag));
1841 if (*id == NULL)
1842 err = got_error_from_errno("got_object_id_dup");
1843 else if (label && asprintf(label, "refs/tags/%s",
1844 got_object_tag_get_name(tag)) == -1) {
1845 err = got_error_from_errno("asprintf");
1846 free(*id);
1847 *id = NULL;
1849 got_object_tag_close(tag);
1850 return err;
1851 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1852 err->code != GOT_ERR_NO_OBJ)
1853 return err;
1856 err = got_ref_open(&ref, repo, id_str, 0);
1857 if (err == NULL) {
1858 err = got_ref_resolve(id, repo, ref);
1859 if (err)
1860 goto done;
1861 if (label) {
1862 *label = strdup(got_ref_get_name(ref));
1863 if (*label == NULL) {
1864 err = got_error_from_errno("strdup");
1865 goto done;
1868 } else {
1869 if (err->code != GOT_ERR_NOT_REF &&
1870 err->code != GOT_ERR_BAD_REF_NAME)
1871 goto done;
1872 err = got_repo_match_object_id_prefix(id, id_str,
1873 obj_type, repo);
1874 if (err) {
1875 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1876 err = got_error_not_ref(id_str);
1877 goto done;
1879 if (label) {
1880 err = got_object_id_str(label, *id);
1881 if (*label == NULL) {
1882 err = got_error_from_errno("strdup");
1883 goto done;
1887 done:
1888 if (ref)
1889 got_ref_close(ref);
1890 return err;
1893 const struct got_error *
1894 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1895 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1897 const struct got_error *err = NULL;
1898 struct got_reflist_entry *re;
1899 struct got_object_id *tag_id;
1900 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1902 *tag = NULL;
1904 TAILQ_FOREACH(re, refs, entry) {
1905 const char *refname;
1906 refname = got_ref_get_name(re->ref);
1907 if (got_ref_is_symbolic(re->ref))
1908 continue;
1909 if (strncmp(refname, "refs/tags/", 10) != 0)
1910 continue;
1911 if (!name_is_absolute)
1912 refname += strlen("refs/tags/");
1913 if (strcmp(refname, name) != 0)
1914 continue;
1915 err = got_ref_resolve(&tag_id, repo, re->ref);
1916 if (err)
1917 break;
1918 err = got_object_open_as_tag(tag, repo, tag_id);
1919 free(tag_id);
1920 if (err)
1921 break;
1922 if (obj_type == GOT_OBJ_TYPE_ANY ||
1923 got_object_tag_get_object_type(*tag) == obj_type)
1924 break;
1925 got_object_tag_close(*tag);
1926 *tag = NULL;
1929 if (err == NULL && *tag == NULL)
1930 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1931 GOT_OBJ_LABEL_TAG, name);
1932 return err;
1935 static const struct got_error *
1936 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1937 const char *name, mode_t mode, struct got_object_id *blob_id)
1939 const struct got_error *err = NULL;
1941 *new_te = NULL;
1943 *new_te = calloc(1, sizeof(**new_te));
1944 if (*new_te == NULL)
1945 return got_error_from_errno("calloc");
1947 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1948 sizeof((*new_te)->name)) {
1949 err = got_error(GOT_ERR_NO_SPACE);
1950 goto done;
1953 if (S_ISLNK(mode)) {
1954 (*new_te)->mode = S_IFLNK;
1955 } else {
1956 (*new_te)->mode = S_IFREG;
1957 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1959 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1960 done:
1961 if (err && *new_te) {
1962 free(*new_te);
1963 *new_te = NULL;
1965 return err;
1968 static const struct got_error *
1969 import_file(struct got_tree_entry **new_te, struct dirent *de,
1970 const char *path, struct got_repository *repo)
1972 const struct got_error *err;
1973 struct got_object_id *blob_id = NULL;
1974 char *filepath;
1975 struct stat sb;
1977 if (asprintf(&filepath, "%s%s%s", path,
1978 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1979 return got_error_from_errno("asprintf");
1981 if (lstat(filepath, &sb) != 0) {
1982 err = got_error_from_errno2("lstat", path);
1983 goto done;
1986 err = got_object_blob_create(&blob_id, filepath, repo);
1987 if (err)
1988 goto done;
1990 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1991 blob_id);
1992 done:
1993 free(filepath);
1994 if (err)
1995 free(blob_id);
1996 return err;
1999 static const struct got_error *
2000 insert_tree_entry(struct got_tree_entry *new_te,
2001 struct got_pathlist_head *paths)
2003 const struct got_error *err = NULL;
2004 struct got_pathlist_entry *new_pe;
2006 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2007 if (err)
2008 return err;
2009 if (new_pe == NULL)
2010 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2011 return NULL;
2014 static const struct got_error *write_tree(struct got_object_id **,
2015 const char *, struct got_pathlist_head *, struct got_repository *,
2016 got_repo_import_cb progress_cb, void *progress_arg);
2018 static const struct got_error *
2019 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2020 const char *path, struct got_pathlist_head *ignores,
2021 struct got_repository *repo,
2022 got_repo_import_cb progress_cb, void *progress_arg)
2024 const struct got_error *err;
2025 struct got_object_id *id = NULL;
2026 char *subdirpath;
2028 if (asprintf(&subdirpath, "%s%s%s", path,
2029 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2030 return got_error_from_errno("asprintf");
2032 (*new_te) = calloc(1, sizeof(**new_te));
2033 if (*new_te == NULL)
2034 return got_error_from_errno("calloc");
2035 (*new_te)->mode = S_IFDIR;
2036 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2037 sizeof((*new_te)->name)) {
2038 err = got_error(GOT_ERR_NO_SPACE);
2039 goto done;
2041 err = write_tree(&id, subdirpath, ignores, repo,
2042 progress_cb, progress_arg);
2043 if (err)
2044 goto done;
2045 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2047 done:
2048 free(id);
2049 free(subdirpath);
2050 if (err) {
2051 free(*new_te);
2052 *new_te = NULL;
2054 return err;
2057 static const struct got_error *
2058 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2059 struct got_pathlist_head *ignores, struct got_repository *repo,
2060 got_repo_import_cb progress_cb, void *progress_arg)
2062 const struct got_error *err = NULL;
2063 DIR *dir;
2064 struct dirent *de;
2065 int nentries;
2066 struct got_tree_entry *new_te = NULL;
2067 struct got_pathlist_head paths;
2068 struct got_pathlist_entry *pe;
2070 *new_tree_id = NULL;
2072 TAILQ_INIT(&paths);
2074 dir = opendir(path_dir);
2075 if (dir == NULL) {
2076 err = got_error_from_errno2("opendir", path_dir);
2077 goto done;
2080 nentries = 0;
2081 while ((de = readdir(dir)) != NULL) {
2082 int ignore = 0;
2083 int type;
2085 if (strcmp(de->d_name, ".") == 0 ||
2086 strcmp(de->d_name, "..") == 0)
2087 continue;
2089 TAILQ_FOREACH(pe, ignores, entry) {
2090 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2091 ignore = 1;
2092 break;
2095 if (ignore)
2096 continue;
2098 err = got_path_dirent_type(&type, path_dir, de);
2099 if (err)
2100 goto done;
2102 if (type == DT_DIR) {
2103 err = import_subdir(&new_te, de, path_dir,
2104 ignores, repo, progress_cb, progress_arg);
2105 if (err) {
2106 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2107 goto done;
2108 err = NULL;
2109 continue;
2111 } else if (type == DT_REG || type == DT_LNK) {
2112 err = import_file(&new_te, de, path_dir, repo);
2113 if (err)
2114 goto done;
2115 } else
2116 continue;
2118 err = insert_tree_entry(new_te, &paths);
2119 if (err)
2120 goto done;
2121 nentries++;
2124 if (TAILQ_EMPTY(&paths)) {
2125 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2126 "cannot create tree without any entries");
2127 goto done;
2130 TAILQ_FOREACH(pe, &paths, entry) {
2131 struct got_tree_entry *te = pe->data;
2132 char *path;
2133 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2134 continue;
2135 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2136 err = got_error_from_errno("asprintf");
2137 goto done;
2139 err = (*progress_cb)(progress_arg, path);
2140 free(path);
2141 if (err)
2142 goto done;
2145 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2146 done:
2147 if (dir)
2148 closedir(dir);
2149 got_pathlist_free(&paths);
2150 return err;
2153 const struct got_error *
2154 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2155 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2156 struct got_repository *repo, got_repo_import_cb progress_cb,
2157 void *progress_arg)
2159 const struct got_error *err;
2160 struct got_object_id *new_tree_id;
2162 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2163 progress_cb, progress_arg);
2164 if (err)
2165 return err;
2167 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2168 author, time(NULL), author, time(NULL), logmsg, repo);
2169 free(new_tree_id);
2170 return err;
2173 const struct got_error *
2174 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2175 struct got_repository *repo)
2177 const struct got_error *err = NULL;
2178 char *path_objects = NULL, *path = NULL;
2179 DIR *dir = NULL;
2180 struct got_object_id id;
2181 int i;
2183 *nobjects = 0;
2184 *ondisk_size = 0;
2186 path_objects = got_repo_get_path_objects(repo);
2187 if (path_objects == NULL)
2188 return got_error_from_errno("got_repo_get_path_objects");
2190 for (i = 0; i <= 0xff; i++) {
2191 struct dirent *dent;
2193 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2194 err = got_error_from_errno("asprintf");
2195 break;
2198 dir = opendir(path);
2199 if (dir == NULL) {
2200 if (errno == ENOENT) {
2201 err = NULL;
2202 continue;
2204 err = got_error_from_errno2("opendir", path);
2205 break;
2208 while ((dent = readdir(dir)) != NULL) {
2209 char *id_str;
2210 int fd;
2211 struct stat sb;
2213 if (strcmp(dent->d_name, ".") == 0 ||
2214 strcmp(dent->d_name, "..") == 0)
2215 continue;
2217 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2218 err = got_error_from_errno("asprintf");
2219 goto done;
2222 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2223 free(id_str);
2224 continue;
2226 free(id_str);
2228 err = got_object_open_loose_fd(&fd, &id, repo);
2229 if (err)
2230 goto done;
2232 if (fstat(fd, &sb) == -1) {
2233 err = got_error_from_errno("fstat");
2234 close(fd);
2235 goto done;
2237 (*nobjects)++;
2238 (*ondisk_size) += sb.st_size;
2240 if (close(fd) == -1) {
2241 err = got_error_from_errno("close");
2242 goto done;
2246 if (closedir(dir) != 0) {
2247 err = got_error_from_errno("closedir");
2248 goto done;
2250 dir = NULL;
2252 free(path);
2253 path = NULL;
2255 done:
2256 if (dir && closedir(dir) != 0 && err == NULL)
2257 err = got_error_from_errno("closedir");
2259 if (err) {
2260 *nobjects = 0;
2261 *ondisk_size = 0;
2263 free(path_objects);
2264 free(path);
2265 return err;
2268 const struct got_error *
2269 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2270 off_t *total_packsize, struct got_repository *repo)
2272 const struct got_error *err = NULL;
2273 DIR *packdir = NULL;
2274 struct dirent *dent;
2275 struct got_packidx *packidx = NULL;
2276 char *path_packidx;
2277 char *path_packfile;
2278 int packdir_fd;
2279 struct stat sb;
2281 *npackfiles = 0;
2282 *nobjects = 0;
2283 *total_packsize = 0;
2285 packdir_fd = openat(got_repo_get_fd(repo),
2286 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2287 if (packdir_fd == -1) {
2288 return got_error_from_errno_fmt("openat: %s/%s",
2289 got_repo_get_path_git_dir(repo),
2290 GOT_OBJECTS_PACK_DIR);
2293 packdir = fdopendir(packdir_fd);
2294 if (packdir == NULL) {
2295 err = got_error_from_errno("fdopendir");
2296 goto done;
2299 while ((dent = readdir(packdir)) != NULL) {
2300 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2301 continue;
2303 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2304 dent->d_name) == -1) {
2305 err = got_error_from_errno("asprintf");
2306 goto done;
2309 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2310 path_packidx, 0);
2311 free(path_packidx);
2312 if (err)
2313 goto done;
2315 if (fstat(packidx->fd, &sb) == -1)
2316 goto done;
2317 *total_packsize += sb.st_size;
2319 err = got_packidx_get_packfile_path(&path_packfile,
2320 packidx->path_packidx);
2321 if (err)
2322 goto done;
2324 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2325 0) == -1) {
2326 free(path_packfile);
2327 goto done;
2329 free(path_packfile);
2330 *total_packsize += sb.st_size;
2332 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2334 (*npackfiles)++;
2336 got_packidx_close(packidx);
2337 packidx = NULL;
2339 done:
2340 if (packidx)
2341 got_packidx_close(packidx);
2342 if (packdir && closedir(packdir) != 0 && err == NULL)
2343 err = got_error_from_errno("closedir");
2344 if (err) {
2345 *npackfiles = 0;
2346 *nobjects = 0;
2347 *total_packsize = 0;
2349 return err;
2352 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2353 got_packidx_bloom_filter_cmp);