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/uio.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/resource.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <fnmatch.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <errno.h>
36 #include <libgen.h>
37 #include <stdint.h>
39 #include "bloom.h"
41 #include "got_error.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_object.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_privsep.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_object_cache.h"
57 #include "got_lib_repository.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef nitems
61 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
62 #endif
64 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
65 got_packidx_bloom_filter_cmp);
67 const char *
68 got_repo_get_path(struct got_repository *repo)
69 {
70 return repo->path;
71 }
73 const char *
74 got_repo_get_path_git_dir(struct got_repository *repo)
75 {
76 return repo->path_git_dir;
77 }
79 int
80 got_repo_get_fd(struct got_repository *repo)
81 {
82 return repo->gitdir_fd;
83 }
85 const char *
86 got_repo_get_gitconfig_author_name(struct got_repository *repo)
87 {
88 return repo->gitconfig_author_name;
89 }
91 const char *
92 got_repo_get_gitconfig_author_email(struct got_repository *repo)
93 {
94 return repo->gitconfig_author_email;
95 }
97 const char *
98 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
99 {
100 return repo->global_gitconfig_author_name;
103 const char *
104 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
106 return repo->global_gitconfig_author_email;
109 const char *
110 got_repo_get_gitconfig_owner(struct got_repository *repo)
112 return repo->gitconfig_owner;
115 void
116 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
117 struct got_repository *repo)
119 *extensions = repo->extensions;
120 *nextensions = repo->nextensions;
123 int
124 got_repo_is_bare(struct got_repository *repo)
126 return (strcmp(repo->path, repo->path_git_dir) == 0);
129 static char *
130 get_path_git_child(struct got_repository *repo, const char *basename)
132 char *path_child;
134 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
135 basename) == -1)
136 return NULL;
138 return path_child;
141 char *
142 got_repo_get_path_objects(struct got_repository *repo)
144 return get_path_git_child(repo, GOT_OBJECTS_DIR);
147 char *
148 got_repo_get_path_objects_pack(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
153 char *
154 got_repo_get_path_refs(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_REFS_DIR);
159 char *
160 got_repo_get_path_packed_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
165 static char *
166 get_path_head(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_HEAD_FILE);
171 char *
172 got_repo_get_path_gitconfig(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_GITCONFIG);
177 char *
178 got_repo_get_path_gotconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
183 const struct got_gotconfig *
184 got_repo_get_gotconfig(struct got_repository *repo)
186 return repo->gotconfig;
189 void
190 got_repo_get_gitconfig_remotes(int *nremotes,
191 const struct got_remote_repo **remotes, struct got_repository *repo)
193 *nremotes = repo->ngitconfig_remotes;
194 *remotes = repo->gitconfig_remotes;
197 static int
198 is_git_repo(struct got_repository *repo)
200 const char *path_git = got_repo_get_path_git_dir(repo);
201 char *path_objects = got_repo_get_path_objects(repo);
202 char *path_refs = got_repo_get_path_refs(repo);
203 char *path_head = get_path_head(repo);
204 int ret = 0;
205 struct stat sb;
206 struct got_reference *head_ref;
208 if (lstat(path_git, &sb) == -1)
209 goto done;
210 if (!S_ISDIR(sb.st_mode))
211 goto done;
213 if (lstat(path_objects, &sb) == -1)
214 goto done;
215 if (!S_ISDIR(sb.st_mode))
216 goto done;
218 if (lstat(path_refs, &sb) == -1)
219 goto done;
220 if (!S_ISDIR(sb.st_mode))
221 goto done;
223 if (lstat(path_head, &sb) == -1)
224 goto done;
225 if (!S_ISREG(sb.st_mode))
226 goto done;
228 /* Check if the HEAD reference can be opened. */
229 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
230 goto done;
231 got_ref_close(head_ref);
233 ret = 1;
234 done:
235 free(path_objects);
236 free(path_refs);
237 free(path_head);
238 return ret;
242 const struct got_error *
243 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
244 struct got_object *obj)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->objcache, id, obj);
249 if (err) {
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
252 err = NULL;
253 return err;
255 obj->refcnt++;
256 #endif
257 return NULL;
260 struct got_object *
261 got_repo_get_cached_object(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
267 const struct got_error *
268 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
269 struct got_tree_object *tree)
271 #ifndef GOT_NO_OBJ_CACHE
272 const struct got_error *err = NULL;
273 err = got_object_cache_add(&repo->treecache, id, tree);
274 if (err) {
275 if (err->code == GOT_ERR_OBJ_EXISTS ||
276 err->code == GOT_ERR_OBJ_TOO_LARGE)
277 err = NULL;
278 return err;
280 tree->refcnt++;
281 #endif
282 return NULL;
285 struct got_tree_object *
286 got_repo_get_cached_tree(struct got_repository *repo,
287 struct got_object_id *id)
289 return (struct got_tree_object *)got_object_cache_get(
290 &repo->treecache, id);
293 const struct got_error *
294 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
295 struct got_commit_object *commit)
297 #ifndef GOT_NO_OBJ_CACHE
298 const struct got_error *err = NULL;
299 err = got_object_cache_add(&repo->commitcache, id, commit);
300 if (err) {
301 if (err->code == GOT_ERR_OBJ_EXISTS ||
302 err->code == GOT_ERR_OBJ_TOO_LARGE)
303 err = NULL;
304 return err;
306 commit->refcnt++;
307 #endif
308 return NULL;
311 struct got_commit_object *
312 got_repo_get_cached_commit(struct got_repository *repo,
313 struct got_object_id *id)
315 return (struct got_commit_object *)got_object_cache_get(
316 &repo->commitcache, id);
319 const struct got_error *
320 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
321 struct got_tag_object *tag)
323 #ifndef GOT_NO_OBJ_CACHE
324 const struct got_error *err = NULL;
325 err = got_object_cache_add(&repo->tagcache, id, tag);
326 if (err) {
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
329 err = NULL;
330 return err;
332 tag->refcnt++;
333 #endif
334 return NULL;
337 struct got_tag_object *
338 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
340 return (struct got_tag_object *)got_object_cache_get(
341 &repo->tagcache, id);
344 const struct got_error *
345 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
346 struct got_raw_object *raw)
348 #ifndef GOT_NO_OBJ_CACHE
349 const struct got_error *err = NULL;
350 err = got_object_cache_add(&repo->rawcache, id, raw);
351 if (err) {
352 if (err->code == GOT_ERR_OBJ_EXISTS ||
353 err->code == GOT_ERR_OBJ_TOO_LARGE)
354 err = NULL;
355 return err;
357 raw->refcnt++;
358 #endif
359 return NULL;
363 struct got_raw_object *
364 got_repo_get_cached_raw_object(struct got_repository *repo,
365 struct got_object_id *id)
367 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
371 static const struct got_error *
372 open_repo(struct got_repository *repo, const char *path)
374 const struct got_error *err = NULL;
376 repo->gitdir_fd = -1;
378 /* bare git repository? */
379 repo->path_git_dir = strdup(path);
380 if (repo->path_git_dir == NULL)
381 return got_error_from_errno("strdup");
382 if (is_git_repo(repo)) {
383 repo->path = strdup(repo->path_git_dir);
384 if (repo->path == NULL) {
385 err = got_error_from_errno("strdup");
386 goto done;
388 repo->gitdir_fd = open(repo->path_git_dir,
389 O_DIRECTORY | O_CLOEXEC);
390 if (repo->gitdir_fd == -1) {
391 err = got_error_from_errno2("open",
392 repo->path_git_dir);
393 goto done;
395 return NULL;
398 /* git repository with working tree? */
399 free(repo->path_git_dir);
400 repo->path_git_dir = NULL;
401 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
402 err = got_error_from_errno("asprintf");
403 goto done;
405 if (is_git_repo(repo)) {
406 repo->path = strdup(path);
407 if (repo->path == NULL) {
408 err = got_error_from_errno("strdup");
409 goto done;
411 repo->gitdir_fd = open(repo->path_git_dir,
412 O_DIRECTORY | O_CLOEXEC);
413 if (repo->gitdir_fd == -1) {
414 err = got_error_from_errno2("open",
415 repo->path_git_dir);
416 goto done;
418 return NULL;
421 err = got_error(GOT_ERR_NOT_GIT_REPO);
422 done:
423 if (err) {
424 free(repo->path);
425 repo->path = NULL;
426 free(repo->path_git_dir);
427 repo->path_git_dir = NULL;
428 if (repo->gitdir_fd != -1)
429 close(repo->gitdir_fd);
430 repo->gitdir_fd = -1;
433 return err;
436 static const struct got_error *
437 parse_gitconfig_file(int *gitconfig_repository_format_version,
438 char **gitconfig_author_name, char **gitconfig_author_email,
439 struct got_remote_repo **remotes, int *nremotes,
440 char **gitconfig_owner, char ***extensions, int *nextensions,
441 const char *gitconfig_path)
443 const struct got_error *err = NULL, *child_err = NULL;
444 int fd = -1;
445 int imsg_fds[2] = { -1, -1 };
446 pid_t pid;
447 struct imsgbuf *ibuf;
449 *gitconfig_repository_format_version = 0;
450 if (extensions)
451 *extensions = NULL;
452 if (nextensions)
453 *nextensions = 0;
454 *gitconfig_author_name = NULL;
455 *gitconfig_author_email = NULL;
456 if (remotes)
457 *remotes = NULL;
458 if (nremotes)
459 *nremotes = 0;
460 if (gitconfig_owner)
461 *gitconfig_owner = NULL;
463 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
464 if (fd == -1) {
465 if (errno == ENOENT)
466 return NULL;
467 return got_error_from_errno2("open", gitconfig_path);
470 ibuf = calloc(1, sizeof(*ibuf));
471 if (ibuf == NULL) {
472 err = got_error_from_errno("calloc");
473 goto done;
476 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
477 err = got_error_from_errno("socketpair");
478 goto done;
481 pid = fork();
482 if (pid == -1) {
483 err = got_error_from_errno("fork");
484 goto done;
485 } else if (pid == 0) {
486 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
487 gitconfig_path);
488 /* not reached */
491 if (close(imsg_fds[1]) == -1) {
492 err = got_error_from_errno("close");
493 goto done;
495 imsg_fds[1] = -1;
496 imsg_init(ibuf, imsg_fds[0]);
498 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
499 if (err)
500 goto done;
501 fd = -1;
503 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
504 if (err)
505 goto done;
507 err = got_privsep_recv_gitconfig_int(
508 gitconfig_repository_format_version, ibuf);
509 if (err)
510 goto done;
512 if (extensions && nextensions) {
513 err = got_privsep_send_gitconfig_repository_extensions_req(
514 ibuf);
515 if (err)
516 goto done;
517 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
518 if (err)
519 goto done;
520 if (*nextensions > 0) {
521 int i;
522 *extensions = calloc(*nextensions, sizeof(char *));
523 if (*extensions == NULL) {
524 err = got_error_from_errno("calloc");
525 goto done;
527 for (i = 0; i < *nextensions; i++) {
528 char *ext;
529 err = got_privsep_recv_gitconfig_str(&ext,
530 ibuf);
531 if (err)
532 goto done;
533 (*extensions)[i] = ext;
538 err = got_privsep_send_gitconfig_author_name_req(ibuf);
539 if (err)
540 goto done;
542 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
543 if (err)
544 goto done;
546 err = got_privsep_send_gitconfig_author_email_req(ibuf);
547 if (err)
548 goto done;
550 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
551 if (err)
552 goto done;
554 if (remotes && nremotes) {
555 err = got_privsep_send_gitconfig_remotes_req(ibuf);
556 if (err)
557 goto done;
559 err = got_privsep_recv_gitconfig_remotes(remotes,
560 nremotes, ibuf);
561 if (err)
562 goto done;
565 if (gitconfig_owner) {
566 err = got_privsep_send_gitconfig_owner_req(ibuf);
567 if (err)
568 goto done;
569 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
570 if (err)
571 goto done;
574 err = got_privsep_send_stop(imsg_fds[0]);
575 child_err = got_privsep_wait_for_child(pid);
576 if (child_err && err == NULL)
577 err = child_err;
578 done:
579 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
580 err = got_error_from_errno("close");
581 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
582 err = got_error_from_errno("close");
583 if (fd != -1 && close(fd) == -1 && err == NULL)
584 err = got_error_from_errno2("close", gitconfig_path);
585 free(ibuf);
586 return err;
589 static const struct got_error *
590 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
592 const struct got_error *err = NULL;
593 char *repo_gitconfig_path = NULL;
595 if (global_gitconfig_path) {
596 /* Read settings from ~/.gitconfig. */
597 int dummy_repo_version;
598 err = parse_gitconfig_file(&dummy_repo_version,
599 &repo->global_gitconfig_author_name,
600 &repo->global_gitconfig_author_email,
601 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
602 if (err)
603 return err;
606 /* Read repository's .git/config file. */
607 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
608 if (repo_gitconfig_path == NULL)
609 return got_error_from_errno("got_repo_get_path_gitconfig");
611 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
612 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
613 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
614 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
615 repo_gitconfig_path);
616 if (err)
617 goto done;
618 done:
619 free(repo_gitconfig_path);
620 return err;
623 static const struct got_error *
624 read_gotconfig(struct got_repository *repo)
626 const struct got_error *err = NULL;
627 char *gotconfig_path;
629 gotconfig_path = got_repo_get_path_gotconfig(repo);
630 if (gotconfig_path == NULL)
631 return got_error_from_errno("got_repo_get_path_gotconfig");
633 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
634 free(gotconfig_path);
635 return err;
638 /* Supported repository format extensions. */
639 static const char *repo_extensions[] = {
640 "noop", /* Got supports repository format version 1. */
641 "preciousObjects", /* Supported by gotadmin cleanup. */
642 "worktreeConfig", /* Got does not care about Git work trees. */
643 };
645 const struct got_error *
646 got_repo_open(struct got_repository **repop, const char *path,
647 const char *global_gitconfig_path)
649 struct got_repository *repo = NULL;
650 const struct got_error *err = NULL;
651 char *repo_path = NULL;
652 size_t i;
653 struct rlimit rl;
655 *repop = NULL;
657 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
658 return got_error_from_errno("getrlimit");
660 repo = calloc(1, sizeof(*repo));
661 if (repo == NULL) {
662 err = got_error_from_errno("calloc");
663 goto done;
666 RB_INIT(&repo->packidx_bloom_filters);
668 for (i = 0; i < nitems(repo->privsep_children); i++) {
669 memset(&repo->privsep_children[i], 0,
670 sizeof(repo->privsep_children[0]));
671 repo->privsep_children[i].imsg_fd = -1;
674 err = got_object_cache_init(&repo->objcache,
675 GOT_OBJECT_CACHE_TYPE_OBJ);
676 if (err)
677 goto done;
678 err = got_object_cache_init(&repo->treecache,
679 GOT_OBJECT_CACHE_TYPE_TREE);
680 if (err)
681 goto done;
682 err = got_object_cache_init(&repo->commitcache,
683 GOT_OBJECT_CACHE_TYPE_COMMIT);
684 if (err)
685 goto done;
686 err = got_object_cache_init(&repo->tagcache,
687 GOT_OBJECT_CACHE_TYPE_TAG);
688 if (err)
689 goto done;
690 err = got_object_cache_init(&repo->rawcache,
691 GOT_OBJECT_CACHE_TYPE_RAW);
692 if (err)
693 goto done;
695 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
696 if (repo->pack_cache_size > rl.rlim_cur / 8)
697 repo->pack_cache_size = rl.rlim_cur / 8;
699 repo_path = realpath(path, NULL);
700 if (repo_path == NULL) {
701 err = got_error_from_errno2("realpath", path);
702 goto done;
705 for (;;) {
706 char *parent_path;
708 err = open_repo(repo, repo_path);
709 if (err == NULL)
710 break;
711 if (err->code != GOT_ERR_NOT_GIT_REPO)
712 goto done;
713 if (repo_path[0] == '/' && repo_path[1] == '\0') {
714 err = got_error(GOT_ERR_NOT_GIT_REPO);
715 goto done;
717 err = got_path_dirname(&parent_path, repo_path);
718 if (err)
719 goto done;
720 free(repo_path);
721 repo_path = parent_path;
724 err = read_gotconfig(repo);
725 if (err)
726 goto done;
728 err = read_gitconfig(repo, global_gitconfig_path);
729 if (err)
730 goto done;
731 if (repo->gitconfig_repository_format_version != 0)
732 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
733 for (i = 0; i < repo->nextensions; i++) {
734 char *ext = repo->extensions[i];
735 int j, supported = 0;
736 for (j = 0; j < nitems(repo_extensions); j++) {
737 if (strcmp(ext, repo_extensions[j]) == 0) {
738 supported = 1;
739 break;
742 if (!supported) {
743 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
744 goto done;
747 done:
748 if (err)
749 got_repo_close(repo);
750 else
751 *repop = repo;
752 free(repo_path);
753 return err;
756 const struct got_error *
757 got_repo_close(struct got_repository *repo)
759 const struct got_error *err = NULL, *child_err;
760 struct got_packidx_bloom_filter *bf;
761 size_t i;
763 for (i = 0; i < repo->pack_cache_size; i++) {
764 if (repo->packidx_cache[i] == NULL)
765 break;
766 got_packidx_close(repo->packidx_cache[i]);
769 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
770 &repo->packidx_bloom_filters))) {
771 RB_REMOVE(got_packidx_bloom_filter_tree,
772 &repo->packidx_bloom_filters, bf);
773 free(bf->bloom);
774 free(bf);
777 for (i = 0; i < repo->pack_cache_size; i++) {
778 if (repo->packs[i].path_packfile == NULL)
779 break;
780 got_pack_close(&repo->packs[i]);
783 free(repo->path);
784 free(repo->path_git_dir);
786 got_object_cache_close(&repo->objcache);
787 got_object_cache_close(&repo->treecache);
788 got_object_cache_close(&repo->commitcache);
789 got_object_cache_close(&repo->tagcache);
790 got_object_cache_close(&repo->rawcache);
792 for (i = 0; i < nitems(repo->privsep_children); i++) {
793 if (repo->privsep_children[i].imsg_fd == -1)
794 continue;
795 imsg_clear(repo->privsep_children[i].ibuf);
796 free(repo->privsep_children[i].ibuf);
797 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
798 child_err = got_privsep_wait_for_child(
799 repo->privsep_children[i].pid);
800 if (child_err && err == NULL)
801 err = child_err;
802 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
803 err == NULL)
804 err = got_error_from_errno("close");
807 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
808 err == NULL)
809 err = got_error_from_errno("close");
811 if (repo->gotconfig)
812 got_gotconfig_free(repo->gotconfig);
813 free(repo->gitconfig_author_name);
814 free(repo->gitconfig_author_email);
815 for (i = 0; i < repo->ngitconfig_remotes; i++)
816 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
817 free(repo->gitconfig_remotes);
818 for (i = 0; i < repo->nextensions; i++)
819 free(repo->extensions[i]);
820 free(repo->extensions);
821 free(repo);
823 return err;
826 void
827 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
829 int i;
831 free(repo->name);
832 repo->name = NULL;
833 free(repo->fetch_url);
834 repo->fetch_url = NULL;
835 free(repo->send_url);
836 repo->send_url = NULL;
837 for (i = 0; i < repo->nfetch_branches; i++)
838 free(repo->fetch_branches[i]);
839 free(repo->fetch_branches);
840 repo->fetch_branches = NULL;
841 repo->nfetch_branches = 0;
842 for (i = 0; i < repo->nsend_branches; i++)
843 free(repo->send_branches[i]);
844 free(repo->send_branches);
845 repo->send_branches = NULL;
846 repo->nsend_branches = 0;
849 const struct got_error *
850 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
851 const char *input_path)
853 const struct got_error *err = NULL;
854 const char *repo_abspath = NULL;
855 size_t repolen, len;
856 char *canonpath, *path = NULL;
858 *in_repo_path = NULL;
860 canonpath = strdup(input_path);
861 if (canonpath == NULL) {
862 err = got_error_from_errno("strdup");
863 goto done;
865 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
866 if (err)
867 goto done;
869 repo_abspath = got_repo_get_path(repo);
871 if (canonpath[0] == '\0') {
872 path = strdup(canonpath);
873 if (path == NULL) {
874 err = got_error_from_errno("strdup");
875 goto done;
877 } else {
878 path = realpath(canonpath, NULL);
879 if (path == NULL) {
880 if (errno != ENOENT) {
881 err = got_error_from_errno2("realpath",
882 canonpath);
883 goto done;
885 /*
886 * Path is not on disk.
887 * Assume it is already relative to repository root.
888 */
889 path = strdup(canonpath);
890 if (path == NULL) {
891 err = got_error_from_errno("strdup");
892 goto done;
896 repolen = strlen(repo_abspath);
897 len = strlen(path);
900 if (strcmp(path, repo_abspath) == 0) {
901 free(path);
902 path = strdup("");
903 if (path == NULL) {
904 err = got_error_from_errno("strdup");
905 goto done;
907 } else if (len > repolen &&
908 got_path_is_child(path, repo_abspath, repolen)) {
909 /* Matched an on-disk path inside repository. */
910 if (got_repo_is_bare(repo)) {
911 /*
912 * Matched an on-disk path inside repository
913 * database. Treat input as repository-relative.
914 */
915 free(path);
916 path = canonpath;
917 canonpath = NULL;
918 } else {
919 char *child;
920 /* Strip common prefix with repository path. */
921 err = got_path_skip_common_ancestor(&child,
922 repo_abspath, path);
923 if (err)
924 goto done;
925 free(path);
926 path = child;
928 } else {
929 /*
930 * Matched unrelated on-disk path.
931 * Treat input as repository-relative.
932 */
933 free(path);
934 path = canonpath;
935 canonpath = NULL;
939 /* Make in-repository path absolute */
940 if (path[0] != '/') {
941 char *abspath;
942 if (asprintf(&abspath, "/%s", path) == -1) {
943 err = got_error_from_errno("asprintf");
944 goto done;
946 free(path);
947 path = abspath;
950 done:
951 free(canonpath);
952 if (err)
953 free(path);
954 else
955 *in_repo_path = path;
956 return err;
959 static const struct got_error *
960 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
961 const char *path_packidx)
963 const struct got_error *err = NULL;
964 size_t i;
966 for (i = 0; i < repo->pack_cache_size; i++) {
967 if (repo->packidx_cache[i] == NULL)
968 break;
969 if (strcmp(repo->packidx_cache[i]->path_packidx,
970 path_packidx) == 0) {
971 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
974 if (i == repo->pack_cache_size) {
975 i = repo->pack_cache_size - 1;
976 err = got_packidx_close(repo->packidx_cache[i]);
977 if (err)
978 return err;
981 repo->packidx_cache[i] = packidx;
983 return NULL;
986 int
987 got_repo_is_packidx_filename(const char *name, size_t len)
989 if (len != GOT_PACKIDX_NAMELEN)
990 return 0;
992 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
993 return 0;
995 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
996 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
997 return 0;
999 return 1;
1002 static struct got_packidx_bloom_filter *
1003 get_packidx_bloom_filter(struct got_repository *repo,
1004 const char *path, size_t path_len)
1006 struct got_packidx_bloom_filter key;
1008 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1009 return NULL; /* XXX */
1010 key.path_len = path_len;
1012 return RB_FIND(got_packidx_bloom_filter_tree,
1013 &repo->packidx_bloom_filters, &key);
1016 int
1017 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1018 const char *path_packidx, struct got_object_id *id)
1020 struct got_packidx_bloom_filter *bf;
1022 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1023 if (bf)
1024 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1026 /* No bloom filter means this pack index must be searched. */
1027 return 1;
1030 static const struct got_error *
1031 add_packidx_bloom_filter(struct got_repository *repo,
1032 struct got_packidx *packidx, const char *path_packidx)
1034 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1035 struct got_packidx_bloom_filter *bf;
1036 size_t len;
1039 * Don't use bloom filters for very large pack index files.
1040 * Large pack files will contain a relatively large fraction
1041 * of our objects so we will likely need to visit them anyway.
1042 * The more objects a pack file contains the higher the probability
1043 * of a false-positive match from the bloom filter. And reading
1044 * all object IDs from a large pack index file can be expensive.
1046 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1047 return NULL;
1049 /* Do we already have a filter for this pack index? */
1050 if (get_packidx_bloom_filter(repo, path_packidx,
1051 strlen(path_packidx)) != NULL)
1052 return NULL;
1054 bf = calloc(1, sizeof(*bf));
1055 if (bf == NULL)
1056 return got_error_from_errno("calloc");
1057 bf->bloom = calloc(1, sizeof(*bf->bloom));
1058 if (bf->bloom == NULL) {
1059 free(bf);
1060 return got_error_from_errno("calloc");
1063 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1064 if (len >= sizeof(bf->path)) {
1065 free(bf->bloom);
1066 free(bf);
1067 return got_error(GOT_ERR_NO_SPACE);
1069 bf->path_len = len;
1071 /* Minimum size supported by our bloom filter is 1000 entries. */
1072 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1073 for (i = 0; i < nobjects; i++) {
1074 struct got_packidx_object_id *id;
1075 id = &packidx->hdr.sorted_ids[i];
1076 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1079 RB_INSERT(got_packidx_bloom_filter_tree,
1080 &repo->packidx_bloom_filters, bf);
1081 return NULL;
1084 const struct got_error *
1085 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1086 struct got_repository *repo, struct got_object_id *id)
1088 const struct got_error *err;
1089 DIR *packdir = NULL;
1090 struct dirent *dent;
1091 char *path_packidx;
1092 size_t i;
1093 int packdir_fd;
1095 /* Search pack index cache. */
1096 for (i = 0; i < repo->pack_cache_size; i++) {
1097 if (repo->packidx_cache[i] == NULL)
1098 break;
1099 if (!got_repo_check_packidx_bloom_filter(repo,
1100 repo->packidx_cache[i]->path_packidx, id))
1101 continue; /* object will not be found in this index */
1102 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1103 if (*idx != -1) {
1104 *packidx = repo->packidx_cache[i];
1106 * Move this cache entry to the front. Repeatedly
1107 * searching a wrong pack index can be expensive.
1109 if (i > 0) {
1110 memmove(&repo->packidx_cache[1],
1111 &repo->packidx_cache[0],
1112 i * sizeof(repo->packidx_cache[0]));
1113 repo->packidx_cache[0] = *packidx;
1115 return NULL;
1118 /* No luck. Search the filesystem. */
1120 packdir_fd = openat(got_repo_get_fd(repo),
1121 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1122 if (packdir_fd == -1) {
1123 if (errno == ENOENT)
1124 err = got_error_no_obj(id);
1125 else
1126 err = got_error_from_errno_fmt("openat: %s/%s",
1127 got_repo_get_path_git_dir(repo),
1128 GOT_OBJECTS_PACK_DIR);
1129 goto done;
1132 packdir = fdopendir(packdir_fd);
1133 if (packdir == NULL) {
1134 err = got_error_from_errno("fdopendir");
1135 goto done;
1138 while ((dent = readdir(packdir)) != NULL) {
1139 int is_cached = 0;
1141 if (!got_repo_is_packidx_filename(dent->d_name,
1142 strlen(dent->d_name)))
1143 continue;
1145 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1146 dent->d_name) == -1) {
1147 err = got_error_from_errno("asprintf");
1148 goto done;
1151 if (!got_repo_check_packidx_bloom_filter(repo,
1152 path_packidx, id)) {
1153 free(path_packidx);
1154 continue; /* object will not be found in this index */
1157 for (i = 0; i < repo->pack_cache_size; i++) {
1158 if (repo->packidx_cache[i] == NULL)
1159 break;
1160 if (strcmp(repo->packidx_cache[i]->path_packidx,
1161 path_packidx) == 0) {
1162 is_cached = 1;
1163 break;
1166 if (is_cached) {
1167 free(path_packidx);
1168 continue; /* already searched */
1171 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1172 path_packidx, 0);
1173 if (err) {
1174 free(path_packidx);
1175 goto done;
1178 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1179 if (err) {
1180 free(path_packidx);
1181 goto done;
1184 err = cache_packidx(repo, *packidx, path_packidx);
1185 free(path_packidx);
1186 if (err)
1187 goto done;
1189 *idx = got_packidx_get_object_idx(*packidx, id);
1190 if (*idx != -1) {
1191 err = NULL; /* found the object */
1192 goto done;
1196 err = got_error_no_obj(id);
1197 done:
1198 if (packdir && closedir(packdir) != 0 && err == NULL)
1199 err = got_error_from_errno("closedir");
1200 return err;
1203 const struct got_error *
1204 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1205 struct got_repository *repo)
1207 const struct got_error *err = NULL;
1208 DIR *packdir = NULL;
1209 struct dirent *dent;
1210 char *path_packidx = NULL;
1211 int packdir_fd;
1213 packdir_fd = openat(got_repo_get_fd(repo),
1214 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1215 if (packdir_fd == -1) {
1216 return got_error_from_errno_fmt("openat: %s/%s",
1217 got_repo_get_path_git_dir(repo),
1218 GOT_OBJECTS_PACK_DIR);
1221 packdir = fdopendir(packdir_fd);
1222 if (packdir == NULL) {
1223 err = got_error_from_errno("fdopendir");
1224 goto done;
1227 while ((dent = readdir(packdir)) != NULL) {
1228 if (!got_repo_is_packidx_filename(dent->d_name,
1229 strlen(dent->d_name)))
1230 continue;
1232 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1233 dent->d_name) == -1) {
1234 err = got_error_from_errno("asprintf");
1235 path_packidx = NULL;
1236 break;
1239 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1240 if (err)
1241 break;
1243 done:
1244 if (err)
1245 free(path_packidx);
1246 if (packdir && closedir(packdir) != 0 && err == NULL)
1247 err = got_error_from_errno("closedir");
1248 return err;
1251 const struct got_error *
1252 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1253 struct got_repository *repo)
1255 const struct got_error *err;
1256 size_t i;
1258 *packidx = NULL;
1260 /* Search pack index cache. */
1261 for (i = 0; i < repo->pack_cache_size; i++) {
1262 if (repo->packidx_cache[i] == NULL)
1263 break;
1264 if (strcmp(repo->packidx_cache[i]->path_packidx,
1265 path_packidx) == 0) {
1266 *packidx = repo->packidx_cache[i];
1267 return NULL;
1270 /* No luck. Search the filesystem. */
1272 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1273 path_packidx, 0);
1274 if (err)
1275 return err;
1277 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1278 if (err)
1279 goto done;
1281 err = cache_packidx(repo, *packidx, path_packidx);
1282 done:
1283 if (err) {
1284 got_packidx_close(*packidx);
1285 *packidx = NULL;
1287 return err;
1290 static const struct got_error *
1291 read_packfile_hdr(int fd, struct got_packidx *packidx)
1293 const struct got_error *err = NULL;
1294 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1295 struct got_packfile_hdr hdr;
1296 ssize_t n;
1298 n = read(fd, &hdr, sizeof(hdr));
1299 if (n < 0)
1300 return got_error_from_errno("read");
1301 if (n != sizeof(hdr))
1302 return got_error(GOT_ERR_BAD_PACKFILE);
1304 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1305 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1306 be32toh(hdr.nobjects) != totobj)
1307 err = got_error(GOT_ERR_BAD_PACKFILE);
1309 return err;
1312 static const struct got_error *
1313 open_packfile(int *fd, struct got_repository *repo,
1314 const char *relpath, struct got_packidx *packidx)
1316 const struct got_error *err = NULL;
1318 *fd = openat(got_repo_get_fd(repo), relpath,
1319 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1320 if (*fd == -1)
1321 return got_error_from_errno_fmt("openat: %s/%s",
1322 got_repo_get_path_git_dir(repo), relpath);
1324 if (packidx) {
1325 err = read_packfile_hdr(*fd, packidx);
1326 if (err) {
1327 close(*fd);
1328 *fd = -1;
1332 return err;
1335 const struct got_error *
1336 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1337 const char *path_packfile, struct got_packidx *packidx)
1339 const struct got_error *err = NULL;
1340 struct got_pack *pack = NULL;
1341 struct stat sb;
1342 size_t i;
1344 if (packp)
1345 *packp = NULL;
1347 for (i = 0; i < repo->pack_cache_size; i++) {
1348 pack = &repo->packs[i];
1349 if (pack->path_packfile == NULL)
1350 break;
1351 if (strcmp(pack->path_packfile, path_packfile) == 0)
1352 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1355 if (i == repo->pack_cache_size) {
1356 err = got_pack_close(&repo->packs[i - 1]);
1357 if (err)
1358 return err;
1359 memmove(&repo->packs[1], &repo->packs[0],
1360 sizeof(repo->packs) - sizeof(repo->packs[0]));
1361 i = 0;
1364 pack = &repo->packs[i];
1366 pack->path_packfile = strdup(path_packfile);
1367 if (pack->path_packfile == NULL) {
1368 err = got_error_from_errno("strdup");
1369 goto done;
1372 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1373 if (err)
1374 goto done;
1376 if (fstat(pack->fd, &sb) != 0) {
1377 err = got_error_from_errno("fstat");
1378 goto done;
1380 pack->filesize = sb.st_size;
1382 pack->privsep_child = NULL;
1384 #ifndef GOT_PACK_NO_MMAP
1385 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1386 pack->fd, 0);
1387 if (pack->map == MAP_FAILED) {
1388 if (errno != ENOMEM) {
1389 err = got_error_from_errno("mmap");
1390 goto done;
1392 pack->map = NULL; /* fall back to read(2) */
1394 #endif
1395 done:
1396 if (err) {
1397 if (pack) {
1398 free(pack->path_packfile);
1399 memset(pack, 0, sizeof(*pack));
1401 } else if (packp)
1402 *packp = pack;
1403 return err;
1406 struct got_pack *
1407 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1409 struct got_pack *pack = NULL;
1410 size_t i;
1412 for (i = 0; i < repo->pack_cache_size; i++) {
1413 pack = &repo->packs[i];
1414 if (pack->path_packfile == NULL)
1415 break;
1416 if (strcmp(pack->path_packfile, path_packfile) == 0)
1417 return pack;
1420 return NULL;
1423 const struct got_error *
1424 got_repo_init(const char *repo_path)
1426 const struct got_error *err = NULL;
1427 const char *dirnames[] = {
1428 GOT_OBJECTS_DIR,
1429 GOT_OBJECTS_PACK_DIR,
1430 GOT_REFS_DIR,
1432 const char *description_str = "Unnamed repository; "
1433 "edit this file 'description' to name the repository.";
1434 const char *headref_str = "ref: refs/heads/main";
1435 const char *gitconfig_str = "[core]\n"
1436 "\trepositoryformatversion = 0\n"
1437 "\tfilemode = true\n"
1438 "\tbare = true\n";
1439 char *path;
1440 size_t i;
1442 if (!got_path_dir_is_empty(repo_path))
1443 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1445 for (i = 0; i < nitems(dirnames); i++) {
1446 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1447 return got_error_from_errno("asprintf");
1449 err = got_path_mkdir(path);
1450 free(path);
1451 if (err)
1452 return err;
1455 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1456 return got_error_from_errno("asprintf");
1457 err = got_path_create_file(path, description_str);
1458 free(path);
1459 if (err)
1460 return err;
1462 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1463 return got_error_from_errno("asprintf");
1464 err = got_path_create_file(path, headref_str);
1465 free(path);
1466 if (err)
1467 return err;
1469 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1470 return got_error_from_errno("asprintf");
1471 err = got_path_create_file(path, gitconfig_str);
1472 free(path);
1473 if (err)
1474 return err;
1476 return NULL;
1479 static const struct got_error *
1480 match_packed_object(struct got_object_id **unique_id,
1481 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1483 const struct got_error *err = NULL;
1484 DIR *packdir = NULL;
1485 struct dirent *dent;
1486 char *path_packidx;
1487 struct got_object_id_queue matched_ids;
1488 int packdir_fd;
1490 STAILQ_INIT(&matched_ids);
1492 packdir_fd = openat(got_repo_get_fd(repo),
1493 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1494 if (packdir_fd == -1) {
1495 if (errno != ENOENT) {
1496 err = got_error_from_errno2("openat",
1497 GOT_OBJECTS_PACK_DIR);
1499 goto done;
1502 packdir = fdopendir(packdir_fd);
1503 if (packdir == NULL) {
1504 err = got_error_from_errno("fdopendir");
1505 goto done;
1508 while ((dent = readdir(packdir)) != NULL) {
1509 struct got_packidx *packidx;
1510 struct got_object_qid *qid;
1512 if (!got_repo_is_packidx_filename(dent->d_name,
1513 strlen(dent->d_name)))
1514 continue;
1516 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1517 dent->d_name) == -1) {
1518 err = got_error_from_errno("strdup");
1519 break;
1522 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1523 path_packidx, 0);
1524 free(path_packidx);
1525 if (err)
1526 break;
1528 err = got_packidx_match_id_str_prefix(&matched_ids,
1529 packidx, id_str_prefix);
1530 if (err) {
1531 got_packidx_close(packidx);
1532 break;
1534 err = got_packidx_close(packidx);
1535 if (err)
1536 break;
1538 STAILQ_FOREACH(qid, &matched_ids, entry) {
1539 if (obj_type != GOT_OBJ_TYPE_ANY) {
1540 int matched_type;
1541 err = got_object_get_type(&matched_type, repo,
1542 qid->id);
1543 if (err)
1544 goto done;
1545 if (matched_type != obj_type)
1546 continue;
1548 if (*unique_id == NULL) {
1549 *unique_id = got_object_id_dup(qid->id);
1550 if (*unique_id == NULL) {
1551 err = got_error_from_errno("malloc");
1552 goto done;
1554 } else {
1555 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1556 continue; /* packed multiple times */
1557 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1558 goto done;
1562 done:
1563 got_object_id_queue_free(&matched_ids);
1564 if (packdir && closedir(packdir) != 0 && err == NULL)
1565 err = got_error_from_errno("closedir");
1566 if (err) {
1567 free(*unique_id);
1568 *unique_id = NULL;
1570 return err;
1573 static const struct got_error *
1574 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1575 const char *object_dir, const char *id_str_prefix, int obj_type,
1576 struct got_repository *repo)
1578 const struct got_error *err = NULL;
1579 char *path;
1580 DIR *dir = NULL;
1581 struct dirent *dent;
1582 struct got_object_id id;
1584 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1585 err = got_error_from_errno("asprintf");
1586 goto done;
1589 dir = opendir(path);
1590 if (dir == NULL) {
1591 if (errno == ENOENT) {
1592 err = NULL;
1593 goto done;
1595 err = got_error_from_errno2("opendir", path);
1596 goto done;
1598 while ((dent = readdir(dir)) != NULL) {
1599 char *id_str;
1600 int cmp;
1602 if (strcmp(dent->d_name, ".") == 0 ||
1603 strcmp(dent->d_name, "..") == 0)
1604 continue;
1606 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1607 err = got_error_from_errno("asprintf");
1608 goto done;
1611 if (!got_parse_sha1_digest(id.sha1, id_str))
1612 continue;
1615 * Directory entries do not necessarily appear in
1616 * sorted order, so we must iterate over all of them.
1618 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1619 if (cmp != 0) {
1620 free(id_str);
1621 continue;
1624 if (*unique_id == NULL) {
1625 if (obj_type != GOT_OBJ_TYPE_ANY) {
1626 int matched_type;
1627 err = got_object_get_type(&matched_type, repo,
1628 &id);
1629 if (err)
1630 goto done;
1631 if (matched_type != obj_type)
1632 continue;
1634 *unique_id = got_object_id_dup(&id);
1635 if (*unique_id == NULL) {
1636 err = got_error_from_errno("got_object_id_dup");
1637 free(id_str);
1638 goto done;
1640 } else {
1641 if (got_object_id_cmp(*unique_id, &id) == 0)
1642 continue; /* both packed and loose */
1643 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1644 free(id_str);
1645 goto done;
1648 done:
1649 if (dir && closedir(dir) != 0 && err == NULL)
1650 err = got_error_from_errno("closedir");
1651 if (err) {
1652 free(*unique_id);
1653 *unique_id = NULL;
1655 free(path);
1656 return err;
1659 const struct got_error *
1660 got_repo_match_object_id_prefix(struct got_object_id **id,
1661 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1663 const struct got_error *err = NULL;
1664 char *path_objects = got_repo_get_path_objects(repo);
1665 char *object_dir = NULL;
1666 size_t len;
1667 int i;
1669 *id = NULL;
1671 len = strlen(id_str_prefix);
1672 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1673 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1675 for (i = 0; i < len; i++) {
1676 if (isxdigit((unsigned char)id_str_prefix[i]))
1677 continue;
1678 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1681 if (len >= 2) {
1682 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1683 if (err)
1684 goto done;
1685 object_dir = strndup(id_str_prefix, 2);
1686 if (object_dir == NULL) {
1687 err = got_error_from_errno("strdup");
1688 goto done;
1690 err = match_loose_object(id, path_objects, object_dir,
1691 id_str_prefix, obj_type, repo);
1692 } else if (len == 1) {
1693 int i;
1694 for (i = 0; i < 0xf; i++) {
1695 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1696 == -1) {
1697 err = got_error_from_errno("asprintf");
1698 goto done;
1700 err = match_packed_object(id, repo, object_dir,
1701 obj_type);
1702 if (err)
1703 goto done;
1704 err = match_loose_object(id, path_objects, object_dir,
1705 id_str_prefix, obj_type, repo);
1706 if (err)
1707 goto done;
1709 } else {
1710 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1711 goto done;
1713 done:
1714 free(object_dir);
1715 if (err) {
1716 free(*id);
1717 *id = NULL;
1718 } else if (*id == NULL) {
1719 switch (obj_type) {
1720 case GOT_OBJ_TYPE_BLOB:
1721 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1722 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1723 break;
1724 case GOT_OBJ_TYPE_TREE:
1725 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1726 GOT_OBJ_LABEL_TREE, id_str_prefix);
1727 break;
1728 case GOT_OBJ_TYPE_COMMIT:
1729 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1730 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1731 break;
1732 case GOT_OBJ_TYPE_TAG:
1733 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1734 GOT_OBJ_LABEL_TAG, id_str_prefix);
1735 break;
1736 default:
1737 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1738 break;
1742 return err;
1745 const struct got_error *
1746 got_repo_match_object_id(struct got_object_id **id, char **label,
1747 const char *id_str, int obj_type, struct got_reflist_head *refs,
1748 struct got_repository *repo)
1750 const struct got_error *err;
1751 struct got_tag_object *tag;
1752 struct got_reference *ref = NULL;
1754 *id = NULL;
1755 if (label)
1756 *label = NULL;
1758 if (refs) {
1759 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1760 refs, repo);
1761 if (err == NULL) {
1762 *id = got_object_id_dup(
1763 got_object_tag_get_object_id(tag));
1764 if (*id == NULL)
1765 err = got_error_from_errno("got_object_id_dup");
1766 else if (label && asprintf(label, "refs/tags/%s",
1767 got_object_tag_get_name(tag)) == -1) {
1768 err = got_error_from_errno("asprintf");
1769 free(*id);
1770 *id = NULL;
1772 got_object_tag_close(tag);
1773 return err;
1774 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1775 err->code != GOT_ERR_NO_OBJ)
1776 return err;
1779 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1780 if (err) {
1781 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1782 return err;
1783 err = got_ref_open(&ref, repo, id_str, 0);
1784 if (err != NULL)
1785 goto done;
1786 if (label) {
1787 *label = strdup(got_ref_get_name(ref));
1788 if (*label == NULL) {
1789 err = got_error_from_errno("strdup");
1790 goto done;
1793 err = got_ref_resolve(id, repo, ref);
1794 } else if (label) {
1795 err = got_object_id_str(label, *id);
1796 if (*label == NULL) {
1797 err = got_error_from_errno("strdup");
1798 goto done;
1801 done:
1802 if (ref)
1803 got_ref_close(ref);
1804 return err;
1807 const struct got_error *
1808 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1809 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1811 const struct got_error *err = NULL;
1812 struct got_reflist_entry *re;
1813 struct got_object_id *tag_id;
1814 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1816 *tag = NULL;
1818 TAILQ_FOREACH(re, refs, entry) {
1819 const char *refname;
1820 refname = got_ref_get_name(re->ref);
1821 if (got_ref_is_symbolic(re->ref))
1822 continue;
1823 if (strncmp(refname, "refs/tags/", 10) != 0)
1824 continue;
1825 if (!name_is_absolute)
1826 refname += strlen("refs/tags/");
1827 if (strcmp(refname, name) != 0)
1828 continue;
1829 err = got_ref_resolve(&tag_id, repo, re->ref);
1830 if (err)
1831 break;
1832 err = got_object_open_as_tag(tag, repo, tag_id);
1833 free(tag_id);
1834 if (err)
1835 break;
1836 if (obj_type == GOT_OBJ_TYPE_ANY ||
1837 got_object_tag_get_object_type(*tag) == obj_type)
1838 break;
1839 got_object_tag_close(*tag);
1840 *tag = NULL;
1843 if (err == NULL && *tag == NULL)
1844 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1845 GOT_OBJ_LABEL_TAG, name);
1846 return err;
1849 static const struct got_error *
1850 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1851 const char *name, mode_t mode, struct got_object_id *blob_id)
1853 const struct got_error *err = NULL;
1855 *new_te = NULL;
1857 *new_te = calloc(1, sizeof(**new_te));
1858 if (*new_te == NULL)
1859 return got_error_from_errno("calloc");
1861 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1862 sizeof((*new_te)->name)) {
1863 err = got_error(GOT_ERR_NO_SPACE);
1864 goto done;
1867 if (S_ISLNK(mode)) {
1868 (*new_te)->mode = S_IFLNK;
1869 } else {
1870 (*new_te)->mode = S_IFREG;
1871 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1873 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1874 done:
1875 if (err && *new_te) {
1876 free(*new_te);
1877 *new_te = NULL;
1879 return err;
1882 static const struct got_error *
1883 import_file(struct got_tree_entry **new_te, struct dirent *de,
1884 const char *path, struct got_repository *repo)
1886 const struct got_error *err;
1887 struct got_object_id *blob_id = NULL;
1888 char *filepath;
1889 struct stat sb;
1891 if (asprintf(&filepath, "%s%s%s", path,
1892 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1893 return got_error_from_errno("asprintf");
1895 if (lstat(filepath, &sb) != 0) {
1896 err = got_error_from_errno2("lstat", path);
1897 goto done;
1900 err = got_object_blob_create(&blob_id, filepath, repo);
1901 if (err)
1902 goto done;
1904 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1905 blob_id);
1906 done:
1907 free(filepath);
1908 if (err)
1909 free(blob_id);
1910 return err;
1913 static const struct got_error *
1914 insert_tree_entry(struct got_tree_entry *new_te,
1915 struct got_pathlist_head *paths)
1917 const struct got_error *err = NULL;
1918 struct got_pathlist_entry *new_pe;
1920 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1921 if (err)
1922 return err;
1923 if (new_pe == NULL)
1924 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1925 return NULL;
1928 static const struct got_error *write_tree(struct got_object_id **,
1929 const char *, struct got_pathlist_head *, struct got_repository *,
1930 got_repo_import_cb progress_cb, void *progress_arg);
1932 static const struct got_error *
1933 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1934 const char *path, struct got_pathlist_head *ignores,
1935 struct got_repository *repo,
1936 got_repo_import_cb progress_cb, void *progress_arg)
1938 const struct got_error *err;
1939 struct got_object_id *id = NULL;
1940 char *subdirpath;
1942 if (asprintf(&subdirpath, "%s%s%s", path,
1943 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1944 return got_error_from_errno("asprintf");
1946 (*new_te) = calloc(1, sizeof(**new_te));
1947 if (*new_te == NULL)
1948 return got_error_from_errno("calloc");
1949 (*new_te)->mode = S_IFDIR;
1950 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1951 sizeof((*new_te)->name)) {
1952 err = got_error(GOT_ERR_NO_SPACE);
1953 goto done;
1955 err = write_tree(&id, subdirpath, ignores, repo,
1956 progress_cb, progress_arg);
1957 if (err)
1958 goto done;
1959 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1961 done:
1962 free(id);
1963 free(subdirpath);
1964 if (err) {
1965 free(*new_te);
1966 *new_te = NULL;
1968 return err;
1971 static const struct got_error *
1972 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1973 struct got_pathlist_head *ignores, struct got_repository *repo,
1974 got_repo_import_cb progress_cb, void *progress_arg)
1976 const struct got_error *err = NULL;
1977 DIR *dir;
1978 struct dirent *de;
1979 int nentries;
1980 struct got_tree_entry *new_te = NULL;
1981 struct got_pathlist_head paths;
1982 struct got_pathlist_entry *pe;
1984 *new_tree_id = NULL;
1986 TAILQ_INIT(&paths);
1988 dir = opendir(path_dir);
1989 if (dir == NULL) {
1990 err = got_error_from_errno2("opendir", path_dir);
1991 goto done;
1994 nentries = 0;
1995 while ((de = readdir(dir)) != NULL) {
1996 int ignore = 0;
1997 int type;
1999 if (strcmp(de->d_name, ".") == 0 ||
2000 strcmp(de->d_name, "..") == 0)
2001 continue;
2003 TAILQ_FOREACH(pe, ignores, entry) {
2004 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2005 ignore = 1;
2006 break;
2009 if (ignore)
2010 continue;
2012 err = got_path_dirent_type(&type, path_dir, de);
2013 if (err)
2014 goto done;
2016 if (type == DT_DIR) {
2017 err = import_subdir(&new_te, de, path_dir,
2018 ignores, repo, progress_cb, progress_arg);
2019 if (err) {
2020 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2021 goto done;
2022 err = NULL;
2023 continue;
2025 } else if (type == DT_REG || type == DT_LNK) {
2026 err = import_file(&new_te, de, path_dir, repo);
2027 if (err)
2028 goto done;
2029 } else
2030 continue;
2032 err = insert_tree_entry(new_te, &paths);
2033 if (err)
2034 goto done;
2035 nentries++;
2038 if (TAILQ_EMPTY(&paths)) {
2039 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2040 "cannot create tree without any entries");
2041 goto done;
2044 TAILQ_FOREACH(pe, &paths, entry) {
2045 struct got_tree_entry *te = pe->data;
2046 char *path;
2047 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2048 continue;
2049 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2050 err = got_error_from_errno("asprintf");
2051 goto done;
2053 err = (*progress_cb)(progress_arg, path);
2054 free(path);
2055 if (err)
2056 goto done;
2059 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2060 done:
2061 if (dir)
2062 closedir(dir);
2063 got_pathlist_free(&paths);
2064 return err;
2067 const struct got_error *
2068 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2069 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2070 struct got_repository *repo, got_repo_import_cb progress_cb,
2071 void *progress_arg)
2073 const struct got_error *err;
2074 struct got_object_id *new_tree_id;
2076 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2077 progress_cb, progress_arg);
2078 if (err)
2079 return err;
2081 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2082 author, time(NULL), author, time(NULL), logmsg, repo);
2083 free(new_tree_id);
2084 return err;
2087 const struct got_error *
2088 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2089 struct got_repository *repo)
2091 const struct got_error *err = NULL;
2092 char *path_objects = NULL, *path = NULL;
2093 DIR *dir = NULL;
2094 struct got_object_id id;
2095 int i;
2097 *nobjects = 0;
2098 *ondisk_size = 0;
2100 path_objects = got_repo_get_path_objects(repo);
2101 if (path_objects == NULL)
2102 return got_error_from_errno("got_repo_get_path_objects");
2104 for (i = 0; i <= 0xff; i++) {
2105 struct dirent *dent;
2107 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2108 err = got_error_from_errno("asprintf");
2109 break;
2112 dir = opendir(path);
2113 if (dir == NULL) {
2114 if (errno == ENOENT) {
2115 err = NULL;
2116 continue;
2118 err = got_error_from_errno2("opendir", path);
2119 break;
2122 while ((dent = readdir(dir)) != NULL) {
2123 char *id_str;
2124 int fd;
2125 struct stat sb;
2127 if (strcmp(dent->d_name, ".") == 0 ||
2128 strcmp(dent->d_name, "..") == 0)
2129 continue;
2131 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2132 err = got_error_from_errno("asprintf");
2133 goto done;
2136 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2137 free(id_str);
2138 continue;
2140 free(id_str);
2142 err = got_object_open_loose_fd(&fd, &id, repo);
2143 if (err)
2144 goto done;
2146 if (fstat(fd, &sb) == -1) {
2147 err = got_error_from_errno("fstat");
2148 close(fd);
2149 goto done;
2151 (*nobjects)++;
2152 (*ondisk_size) += sb.st_size;
2154 if (close(fd) == -1) {
2155 err = got_error_from_errno("close");
2156 goto done;
2160 if (closedir(dir) != 0) {
2161 err = got_error_from_errno("closedir");
2162 goto done;
2164 dir = NULL;
2166 free(path);
2167 path = NULL;
2169 done:
2170 if (dir && closedir(dir) != 0 && err == NULL)
2171 err = got_error_from_errno("closedir");
2173 if (err) {
2174 *nobjects = 0;
2175 *ondisk_size = 0;
2177 free(path_objects);
2178 free(path);
2179 return err;
2182 const struct got_error *
2183 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2184 off_t *total_packsize, struct got_repository *repo)
2186 const struct got_error *err = NULL;
2187 DIR *packdir = NULL;
2188 struct dirent *dent;
2189 struct got_packidx *packidx = NULL;
2190 char *path_packidx;
2191 char *path_packfile;
2192 int packdir_fd;
2193 struct stat sb;
2195 *npackfiles = 0;
2196 *nobjects = 0;
2197 *total_packsize = 0;
2199 packdir_fd = openat(got_repo_get_fd(repo),
2200 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2201 if (packdir_fd == -1) {
2202 return got_error_from_errno_fmt("openat: %s/%s",
2203 got_repo_get_path_git_dir(repo),
2204 GOT_OBJECTS_PACK_DIR);
2207 packdir = fdopendir(packdir_fd);
2208 if (packdir == NULL) {
2209 err = got_error_from_errno("fdopendir");
2210 goto done;
2213 while ((dent = readdir(packdir)) != NULL) {
2214 if (!got_repo_is_packidx_filename(dent->d_name,
2215 strlen(dent->d_name)))
2216 continue;
2218 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2219 dent->d_name) == -1) {
2220 err = got_error_from_errno("asprintf");
2221 goto done;
2224 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2225 path_packidx, 0);
2226 free(path_packidx);
2227 if (err)
2228 goto done;
2230 if (fstat(packidx->fd, &sb) == -1)
2231 goto done;
2232 *total_packsize += sb.st_size;
2234 err = got_packidx_get_packfile_path(&path_packfile,
2235 packidx->path_packidx);
2236 if (err)
2237 goto done;
2239 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2240 0) == -1) {
2241 free(path_packfile);
2242 goto done;
2244 free(path_packfile);
2245 *total_packsize += sb.st_size;
2247 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2249 (*npackfiles)++;
2251 got_packidx_close(packidx);
2252 packidx = NULL;
2254 done:
2255 if (packidx)
2256 got_packidx_close(packidx);
2257 if (packdir && closedir(packdir) != 0 && err == NULL)
2258 err = got_error_from_errno("closedir");
2259 if (err) {
2260 *npackfiles = 0;
2261 *nobjects = 0;
2262 *total_packsize = 0;
2264 return err;
2267 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2268 got_packidx_bloom_filter_cmp);