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"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_sha1.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
64 #include "got_lib_gotconfig.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
68 #endif
70 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
71 got_packidx_bloom_filter_cmp);
73 const char *
74 got_repo_get_path(struct got_repository *repo)
75 {
76 return repo->path;
77 }
79 const char *
80 got_repo_get_path_git_dir(struct got_repository *repo)
81 {
82 return repo->path_git_dir;
83 }
85 int
86 got_repo_get_fd(struct got_repository *repo)
87 {
88 return repo->gitdir_fd;
89 }
91 const char *
92 got_repo_get_gitconfig_author_name(struct got_repository *repo)
93 {
94 return repo->gitconfig_author_name;
95 }
97 const char *
98 got_repo_get_gitconfig_author_email(struct got_repository *repo)
99 {
100 return repo->gitconfig_author_email;
103 const char *
104 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
106 return repo->global_gitconfig_author_name;
109 const char *
110 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
112 return repo->global_gitconfig_author_email;
115 const char *
116 got_repo_get_gitconfig_owner(struct got_repository *repo)
118 return repo->gitconfig_owner;
121 void
122 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
123 struct got_repository *repo)
125 *extensions = repo->extensions;
126 *nextensions = repo->nextensions;
129 int
130 got_repo_is_bare(struct got_repository *repo)
132 return (strcmp(repo->path, repo->path_git_dir) == 0);
135 static char *
136 get_path_git_child(struct got_repository *repo, const char *basename)
138 char *path_child;
140 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
141 basename) == -1)
142 return NULL;
144 return path_child;
147 char *
148 got_repo_get_path_objects(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_DIR);
153 char *
154 got_repo_get_path_objects_pack(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
159 char *
160 got_repo_get_path_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_REFS_DIR);
165 char *
166 got_repo_get_path_packed_refs(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
171 static char *
172 get_path_head(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_HEAD_FILE);
177 char *
178 got_repo_get_path_gitconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GITCONFIG);
183 char *
184 got_repo_get_path_gotconfig(struct got_repository *repo)
186 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
189 const struct got_gotconfig *
190 got_repo_get_gotconfig(struct got_repository *repo)
192 return repo->gotconfig;
195 void
196 got_repo_get_gitconfig_remotes(int *nremotes,
197 const struct got_remote_repo **remotes, struct got_repository *repo)
199 *nremotes = repo->ngitconfig_remotes;
200 *remotes = repo->gitconfig_remotes;
203 static int
204 is_git_repo(struct got_repository *repo)
206 const char *path_git = got_repo_get_path_git_dir(repo);
207 char *path_objects = got_repo_get_path_objects(repo);
208 char *path_refs = got_repo_get_path_refs(repo);
209 char *path_head = get_path_head(repo);
210 int ret = 0;
211 struct stat sb;
212 struct got_reference *head_ref;
214 if (lstat(path_git, &sb) == -1)
215 goto done;
216 if (!S_ISDIR(sb.st_mode))
217 goto done;
219 if (lstat(path_objects, &sb) == -1)
220 goto done;
221 if (!S_ISDIR(sb.st_mode))
222 goto done;
224 if (lstat(path_refs, &sb) == -1)
225 goto done;
226 if (!S_ISDIR(sb.st_mode))
227 goto done;
229 if (lstat(path_head, &sb) == -1)
230 goto done;
231 if (!S_ISREG(sb.st_mode))
232 goto done;
234 /* Check if the HEAD reference can be opened. */
235 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
236 goto done;
237 got_ref_close(head_ref);
239 ret = 1;
240 done:
241 free(path_objects);
242 free(path_refs);
243 free(path_head);
244 return ret;
248 const struct got_error *
249 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
250 struct got_object *obj)
252 #ifndef GOT_NO_OBJ_CACHE
253 const struct got_error *err = NULL;
254 err = got_object_cache_add(&repo->objcache, id, obj);
255 if (err) {
256 if (err->code == GOT_ERR_OBJ_EXISTS ||
257 err->code == GOT_ERR_OBJ_TOO_LARGE)
258 err = NULL;
259 return err;
261 obj->refcnt++;
262 #endif
263 return NULL;
266 struct got_object *
267 got_repo_get_cached_object(struct got_repository *repo,
268 struct got_object_id *id)
270 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
273 const struct got_error *
274 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
275 struct got_tree_object *tree)
277 #ifndef GOT_NO_OBJ_CACHE
278 const struct got_error *err = NULL;
279 err = got_object_cache_add(&repo->treecache, id, tree);
280 if (err) {
281 if (err->code == GOT_ERR_OBJ_EXISTS ||
282 err->code == GOT_ERR_OBJ_TOO_LARGE)
283 err = NULL;
284 return err;
286 tree->refcnt++;
287 #endif
288 return NULL;
291 struct got_tree_object *
292 got_repo_get_cached_tree(struct got_repository *repo,
293 struct got_object_id *id)
295 return (struct got_tree_object *)got_object_cache_get(
296 &repo->treecache, id);
299 const struct got_error *
300 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
301 struct got_commit_object *commit)
303 #ifndef GOT_NO_OBJ_CACHE
304 const struct got_error *err = NULL;
305 err = got_object_cache_add(&repo->commitcache, id, commit);
306 if (err) {
307 if (err->code == GOT_ERR_OBJ_EXISTS ||
308 err->code == GOT_ERR_OBJ_TOO_LARGE)
309 err = NULL;
310 return err;
312 commit->refcnt++;
313 #endif
314 return NULL;
317 struct got_commit_object *
318 got_repo_get_cached_commit(struct got_repository *repo,
319 struct got_object_id *id)
321 return (struct got_commit_object *)got_object_cache_get(
322 &repo->commitcache, id);
325 const struct got_error *
326 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
327 struct got_tag_object *tag)
329 #ifndef GOT_NO_OBJ_CACHE
330 const struct got_error *err = NULL;
331 err = got_object_cache_add(&repo->tagcache, id, tag);
332 if (err) {
333 if (err->code == GOT_ERR_OBJ_EXISTS ||
334 err->code == GOT_ERR_OBJ_TOO_LARGE)
335 err = NULL;
336 return err;
338 tag->refcnt++;
339 #endif
340 return NULL;
343 struct got_tag_object *
344 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
346 return (struct got_tag_object *)got_object_cache_get(
347 &repo->tagcache, id);
350 const struct got_error *
351 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
352 struct got_raw_object *raw)
354 #ifndef GOT_NO_OBJ_CACHE
355 const struct got_error *err = NULL;
356 err = got_object_cache_add(&repo->rawcache, id, raw);
357 if (err) {
358 if (err->code == GOT_ERR_OBJ_EXISTS ||
359 err->code == GOT_ERR_OBJ_TOO_LARGE)
360 err = NULL;
361 return err;
363 raw->refcnt++;
364 #endif
365 return NULL;
369 struct got_raw_object *
370 got_repo_get_cached_raw_object(struct got_repository *repo,
371 struct got_object_id *id)
373 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
377 static const struct got_error *
378 open_repo(struct got_repository *repo, const char *path)
380 const struct got_error *err = NULL;
382 repo->gitdir_fd = -1;
384 /* bare git repository? */
385 repo->path_git_dir = strdup(path);
386 if (repo->path_git_dir == NULL)
387 return got_error_from_errno("strdup");
388 if (is_git_repo(repo)) {
389 repo->path = strdup(repo->path_git_dir);
390 if (repo->path == NULL) {
391 err = got_error_from_errno("strdup");
392 goto done;
394 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
395 if (repo->gitdir_fd == -1) {
396 err = got_error_from_errno2("open",
397 repo->path_git_dir);
398 goto done;
400 return NULL;
403 /* git repository with working tree? */
404 free(repo->path_git_dir);
405 repo->path_git_dir = NULL;
406 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
407 err = got_error_from_errno("asprintf");
408 goto done;
410 if (is_git_repo(repo)) {
411 repo->path = strdup(path);
412 if (repo->path == NULL) {
413 err = got_error_from_errno("strdup");
414 goto done;
416 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
417 if (repo->gitdir_fd == -1) {
418 err = got_error_from_errno2("open",
419 repo->path_git_dir);
420 goto done;
422 return NULL;
425 err = got_error(GOT_ERR_NOT_GIT_REPO);
426 done:
427 if (err) {
428 free(repo->path);
429 repo->path = NULL;
430 free(repo->path_git_dir);
431 repo->path_git_dir = NULL;
432 if (repo->gitdir_fd != -1)
433 close(repo->gitdir_fd);
434 repo->gitdir_fd = -1;
437 return err;
440 static const struct got_error *
441 parse_gitconfig_file(int *gitconfig_repository_format_version,
442 char **gitconfig_author_name, char **gitconfig_author_email,
443 struct got_remote_repo **remotes, int *nremotes,
444 char **gitconfig_owner, char ***extensions, int *nextensions,
445 const char *gitconfig_path)
447 const struct got_error *err = NULL, *child_err = NULL;
448 int fd = -1;
449 int imsg_fds[2] = { -1, -1 };
450 pid_t pid;
451 struct imsgbuf *ibuf;
453 *gitconfig_repository_format_version = 0;
454 if (extensions)
455 *extensions = NULL;
456 if (nextensions)
457 *nextensions = 0;
458 *gitconfig_author_name = NULL;
459 *gitconfig_author_email = NULL;
460 if (remotes)
461 *remotes = NULL;
462 if (nremotes)
463 *nremotes = 0;
464 if (gitconfig_owner)
465 *gitconfig_owner = NULL;
467 fd = open(gitconfig_path, O_RDONLY);
468 if (fd == -1) {
469 if (errno == ENOENT)
470 return NULL;
471 return got_error_from_errno2("open", gitconfig_path);
474 ibuf = calloc(1, sizeof(*ibuf));
475 if (ibuf == NULL) {
476 err = got_error_from_errno("calloc");
477 goto done;
480 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
481 err = got_error_from_errno("socketpair");
482 goto done;
485 pid = fork();
486 if (pid == -1) {
487 err = got_error_from_errno("fork");
488 goto done;
489 } else if (pid == 0) {
490 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
491 gitconfig_path);
492 /* not reached */
495 if (close(imsg_fds[1]) == -1) {
496 err = got_error_from_errno("close");
497 goto done;
499 imsg_fds[1] = -1;
500 imsg_init(ibuf, imsg_fds[0]);
502 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
503 if (err)
504 goto done;
505 fd = -1;
507 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
508 if (err)
509 goto done;
511 err = got_privsep_recv_gitconfig_int(
512 gitconfig_repository_format_version, ibuf);
513 if (err)
514 goto done;
516 if (extensions && nextensions) {
517 err = got_privsep_send_gitconfig_repository_extensions_req(
518 ibuf);
519 if (err)
520 goto done;
521 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
522 if (err)
523 goto done;
524 if (*nextensions > 0) {
525 int i;
526 *extensions = calloc(*nextensions, sizeof(char *));
527 if (*extensions == NULL) {
528 err = got_error_from_errno("calloc");
529 goto done;
531 for (i = 0; i < *nextensions; i++) {
532 char *ext;
533 err = got_privsep_recv_gitconfig_str(&ext,
534 ibuf);
535 if (err)
536 goto done;
537 (*extensions)[i] = ext;
542 err = got_privsep_send_gitconfig_author_name_req(ibuf);
543 if (err)
544 goto done;
546 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
547 if (err)
548 goto done;
550 err = got_privsep_send_gitconfig_author_email_req(ibuf);
551 if (err)
552 goto done;
554 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
555 if (err)
556 goto done;
558 if (remotes && nremotes) {
559 err = got_privsep_send_gitconfig_remotes_req(ibuf);
560 if (err)
561 goto done;
563 err = got_privsep_recv_gitconfig_remotes(remotes,
564 nremotes, ibuf);
565 if (err)
566 goto done;
569 if (gitconfig_owner) {
570 err = got_privsep_send_gitconfig_owner_req(ibuf);
571 if (err)
572 goto done;
573 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
574 if (err)
575 goto done;
578 imsg_clear(ibuf);
579 err = got_privsep_send_stop(imsg_fds[0]);
580 child_err = got_privsep_wait_for_child(pid);
581 if (child_err && err == NULL)
582 err = child_err;
583 done:
584 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
585 err = got_error_from_errno("close");
586 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
587 err = got_error_from_errno("close");
588 if (fd != -1 && close(fd) == -1 && err == NULL)
589 err = got_error_from_errno2("close", gitconfig_path);
590 free(ibuf);
591 return err;
594 static const struct got_error *
595 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
597 const struct got_error *err = NULL;
598 char *repo_gitconfig_path = NULL;
600 if (global_gitconfig_path) {
601 /* Read settings from ~/.gitconfig. */
602 int dummy_repo_version;
603 err = parse_gitconfig_file(&dummy_repo_version,
604 &repo->global_gitconfig_author_name,
605 &repo->global_gitconfig_author_email,
606 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
607 if (err)
608 return err;
611 /* Read repository's .git/config file. */
612 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
613 if (repo_gitconfig_path == NULL)
614 return got_error_from_errno("got_repo_get_path_gitconfig");
616 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
617 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
618 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
619 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
620 repo_gitconfig_path);
621 if (err)
622 goto done;
623 done:
624 free(repo_gitconfig_path);
625 return err;
628 static const struct got_error *
629 read_gotconfig(struct got_repository *repo)
631 const struct got_error *err = NULL;
632 char *gotconfig_path;
634 gotconfig_path = got_repo_get_path_gotconfig(repo);
635 if (gotconfig_path == NULL)
636 return got_error_from_errno("got_repo_get_path_gotconfig");
638 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
639 free(gotconfig_path);
640 return err;
643 /* Supported repository format extensions. */
644 static const char *repo_extensions[] = {
645 "noop", /* Got supports repository format version 1. */
646 "preciousObjects", /* Supported by gotadmin cleanup. */
647 "worktreeConfig", /* Got does not care about Git work trees. */
648 };
650 const struct got_error *
651 got_repo_open(struct got_repository **repop, const char *path,
652 const char *global_gitconfig_path)
654 struct got_repository *repo = NULL;
655 const struct got_error *err = NULL;
656 char *repo_path = NULL;
657 size_t i;
658 struct rlimit rl;
660 *repop = NULL;
662 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
663 return got_error_from_errno("getrlimit");
665 repo = calloc(1, sizeof(*repo));
666 if (repo == NULL) {
667 err = got_error_from_errno("calloc");
668 goto done;
671 RB_INIT(&repo->packidx_bloom_filters);
673 for (i = 0; i < nitems(repo->privsep_children); i++) {
674 memset(&repo->privsep_children[i], 0,
675 sizeof(repo->privsep_children[0]));
676 repo->privsep_children[i].imsg_fd = -1;
679 err = got_object_cache_init(&repo->objcache,
680 GOT_OBJECT_CACHE_TYPE_OBJ);
681 if (err)
682 goto done;
683 err = got_object_cache_init(&repo->treecache,
684 GOT_OBJECT_CACHE_TYPE_TREE);
685 if (err)
686 goto done;
687 err = got_object_cache_init(&repo->commitcache,
688 GOT_OBJECT_CACHE_TYPE_COMMIT);
689 if (err)
690 goto done;
691 err = got_object_cache_init(&repo->tagcache,
692 GOT_OBJECT_CACHE_TYPE_TAG);
693 if (err)
694 goto done;
695 err = got_object_cache_init(&repo->rawcache,
696 GOT_OBJECT_CACHE_TYPE_RAW);
697 if (err)
698 goto done;
700 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
701 if (repo->pack_cache_size > rl.rlim_cur / 8)
702 repo->pack_cache_size = rl.rlim_cur / 8;
704 repo_path = realpath(path, NULL);
705 if (repo_path == NULL) {
706 err = got_error_from_errno2("realpath", path);
707 goto done;
710 for (;;) {
711 char *parent_path;
713 err = open_repo(repo, repo_path);
714 if (err == NULL)
715 break;
716 if (err->code != GOT_ERR_NOT_GIT_REPO)
717 goto done;
718 if (repo_path[0] == '/' && repo_path[1] == '\0') {
719 err = got_error(GOT_ERR_NOT_GIT_REPO);
720 goto done;
722 err = got_path_dirname(&parent_path, repo_path);
723 if (err)
724 goto done;
725 free(repo_path);
726 repo_path = parent_path;
729 err = read_gotconfig(repo);
730 if (err)
731 goto done;
733 err = read_gitconfig(repo, global_gitconfig_path);
734 if (err)
735 goto done;
736 if (repo->gitconfig_repository_format_version != 0)
737 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
738 for (i = 0; i < repo->nextensions; i++) {
739 char *ext = repo->extensions[i];
740 int j, supported = 0;
741 for (j = 0; j < nitems(repo_extensions); j++) {
742 if (strcmp(ext, repo_extensions[j]) == 0) {
743 supported = 1;
744 break;
747 if (!supported) {
748 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
749 goto done;
752 done:
753 if (err)
754 got_repo_close(repo);
755 else
756 *repop = repo;
757 free(repo_path);
758 return err;
761 const struct got_error *
762 got_repo_close(struct got_repository *repo)
764 const struct got_error *err = NULL, *child_err;
765 struct got_packidx_bloom_filter *bf;
766 size_t i;
768 for (i = 0; i < repo->pack_cache_size; i++) {
769 if (repo->packidx_cache[i] == NULL)
770 break;
771 got_packidx_close(repo->packidx_cache[i]);
774 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
775 &repo->packidx_bloom_filters))) {
776 RB_REMOVE(got_packidx_bloom_filter_tree,
777 &repo->packidx_bloom_filters, bf);
778 free(bf->bloom);
779 free(bf);
782 for (i = 0; i < repo->pack_cache_size; i++) {
783 if (repo->packs[i].path_packfile == NULL)
784 break;
785 got_pack_close(&repo->packs[i]);
788 free(repo->path);
789 free(repo->path_git_dir);
791 got_object_cache_close(&repo->objcache);
792 got_object_cache_close(&repo->treecache);
793 got_object_cache_close(&repo->commitcache);
794 got_object_cache_close(&repo->tagcache);
795 got_object_cache_close(&repo->rawcache);
797 for (i = 0; i < nitems(repo->privsep_children); i++) {
798 if (repo->privsep_children[i].imsg_fd == -1)
799 continue;
800 imsg_clear(repo->privsep_children[i].ibuf);
801 free(repo->privsep_children[i].ibuf);
802 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
803 child_err = got_privsep_wait_for_child(
804 repo->privsep_children[i].pid);
805 if (child_err && err == NULL)
806 err = child_err;
807 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
808 err == NULL)
809 err = got_error_from_errno("close");
812 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
813 err == NULL)
814 err = got_error_from_errno("close");
816 if (repo->gotconfig)
817 got_gotconfig_free(repo->gotconfig);
818 free(repo->gitconfig_author_name);
819 free(repo->gitconfig_author_email);
820 for (i = 0; i < repo->ngitconfig_remotes; i++)
821 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
822 free(repo->gitconfig_remotes);
823 for (i = 0; i < repo->nextensions; i++)
824 free(repo->extensions[i]);
825 free(repo->extensions);
826 free(repo);
828 return err;
831 void
832 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
834 int i;
836 free(repo->name);
837 repo->name = NULL;
838 free(repo->fetch_url);
839 repo->fetch_url = NULL;
840 free(repo->send_url);
841 repo->send_url = NULL;
842 for (i = 0; i < repo->nfetch_branches; i++)
843 free(repo->fetch_branches[i]);
844 free(repo->fetch_branches);
845 repo->fetch_branches = NULL;
846 repo->nfetch_branches = 0;
847 for (i = 0; i < repo->nsend_branches; i++)
848 free(repo->send_branches[i]);
849 free(repo->send_branches);
850 repo->send_branches = NULL;
851 repo->nsend_branches = 0;
854 const struct got_error *
855 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
856 const char *input_path)
858 const struct got_error *err = NULL;
859 const char *repo_abspath = NULL;
860 size_t repolen, len;
861 char *canonpath, *path = NULL;
863 *in_repo_path = NULL;
865 canonpath = strdup(input_path);
866 if (canonpath == NULL) {
867 err = got_error_from_errno("strdup");
868 goto done;
870 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
871 if (err)
872 goto done;
874 repo_abspath = got_repo_get_path(repo);
876 if (canonpath[0] == '\0') {
877 path = strdup(canonpath);
878 if (path == NULL) {
879 err = got_error_from_errno("strdup");
880 goto done;
882 } else {
883 path = realpath(canonpath, NULL);
884 if (path == NULL) {
885 if (errno != ENOENT) {
886 err = got_error_from_errno2("realpath",
887 canonpath);
888 goto done;
890 /*
891 * Path is not on disk.
892 * Assume it is already relative to repository root.
893 */
894 path = strdup(canonpath);
895 if (path == NULL) {
896 err = got_error_from_errno("strdup");
897 goto done;
901 repolen = strlen(repo_abspath);
902 len = strlen(path);
905 if (strcmp(path, repo_abspath) == 0) {
906 free(path);
907 path = strdup("");
908 if (path == NULL) {
909 err = got_error_from_errno("strdup");
910 goto done;
912 } else if (len > repolen &&
913 got_path_is_child(path, repo_abspath, repolen)) {
914 /* Matched an on-disk path inside repository. */
915 if (got_repo_is_bare(repo)) {
916 /*
917 * Matched an on-disk path inside repository
918 * database. Treat input as repository-relative.
919 */
920 free(path);
921 path = canonpath;
922 canonpath = NULL;
923 } else {
924 char *child;
925 /* Strip common prefix with repository path. */
926 err = got_path_skip_common_ancestor(&child,
927 repo_abspath, path);
928 if (err)
929 goto done;
930 free(path);
931 path = child;
933 } else {
934 /*
935 * Matched unrelated on-disk path.
936 * Treat input as repository-relative.
937 */
938 free(path);
939 path = canonpath;
940 canonpath = NULL;
944 /* Make in-repository path absolute */
945 if (path[0] != '/') {
946 char *abspath;
947 if (asprintf(&abspath, "/%s", path) == -1) {
948 err = got_error_from_errno("asprintf");
949 goto done;
951 free(path);
952 path = abspath;
955 done:
956 free(canonpath);
957 if (err)
958 free(path);
959 else
960 *in_repo_path = path;
961 return err;
964 static const struct got_error *
965 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
966 const char *path_packidx)
968 const struct got_error *err = NULL;
969 size_t i;
971 for (i = 0; i < repo->pack_cache_size; i++) {
972 if (repo->packidx_cache[i] == NULL)
973 break;
974 if (strcmp(repo->packidx_cache[i]->path_packidx,
975 path_packidx) == 0) {
976 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
979 if (i == repo->pack_cache_size) {
980 i = repo->pack_cache_size - 1;
981 err = got_packidx_close(repo->packidx_cache[i]);
982 if (err)
983 return err;
986 repo->packidx_cache[i] = packidx;
988 return NULL;
991 int
992 got_repo_is_packidx_filename(const char *name, size_t len)
994 if (len != GOT_PACKIDX_NAMELEN)
995 return 0;
997 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
998 return 0;
1000 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1001 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1002 return 0;
1004 return 1;
1007 static struct got_packidx_bloom_filter *
1008 get_packidx_bloom_filter(struct got_repository *repo,
1009 const char *path, size_t path_len)
1011 struct got_packidx_bloom_filter key;
1013 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1014 return NULL; /* XXX */
1015 key.path_len = path_len;
1017 return RB_FIND(got_packidx_bloom_filter_tree,
1018 &repo->packidx_bloom_filters, &key);
1021 static int
1022 check_packidx_bloom_filter(struct got_repository *repo,
1023 const char *path_packidx, struct got_object_id *id)
1025 struct got_packidx_bloom_filter *bf;
1027 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1028 if (bf)
1029 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1031 /* No bloom filter means this pack index must be searched. */
1032 return 1;
1035 static const struct got_error *
1036 add_packidx_bloom_filter(struct got_repository *repo,
1037 struct got_packidx *packidx, const char *path_packidx)
1039 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1040 struct got_packidx_bloom_filter *bf;
1041 size_t len;
1044 * Don't use bloom filters for very large pack index files.
1045 * Large pack files will contain a relatively large fraction
1046 * of our objects so we will likely need to visit them anyway.
1047 * The more objects a pack file contains the higher the probability
1048 * of a false-positive match from the bloom filter. And reading
1049 * all object IDs from a large pack index file can be expensive.
1051 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1052 return NULL;
1054 /* Do we already have a filter for this pack index? */
1055 if (get_packidx_bloom_filter(repo, path_packidx,
1056 strlen(path_packidx)) != NULL)
1057 return NULL;
1059 bf = calloc(1, sizeof(*bf));
1060 if (bf == NULL)
1061 return got_error_from_errno("calloc");
1062 bf->bloom = calloc(1, sizeof(*bf->bloom));
1063 if (bf->bloom == NULL) {
1064 free(bf);
1065 return got_error_from_errno("calloc");
1068 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1069 if (len >= sizeof(bf->path)) {
1070 free(bf->bloom);
1071 free(bf);
1072 return got_error(GOT_ERR_NO_SPACE);
1074 bf->path_len = len;
1076 /* Minimum size supported by our bloom filter is 1000 entries. */
1077 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1078 for (i = 0; i < nobjects; i++) {
1079 struct got_packidx_object_id *id;
1080 id = &packidx->hdr.sorted_ids[i];
1081 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1084 RB_INSERT(got_packidx_bloom_filter_tree,
1085 &repo->packidx_bloom_filters, bf);
1086 return NULL;
1089 const struct got_error *
1090 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1091 struct got_repository *repo, struct got_object_id *id)
1093 const struct got_error *err;
1094 DIR *packdir = NULL;
1095 struct dirent *dent;
1096 char *path_packidx;
1097 size_t i;
1098 int packdir_fd;
1100 /* Search pack index cache. */
1101 for (i = 0; i < repo->pack_cache_size; i++) {
1102 if (repo->packidx_cache[i] == NULL)
1103 break;
1104 if (!check_packidx_bloom_filter(repo,
1105 repo->packidx_cache[i]->path_packidx, id))
1106 continue; /* object will not be found in this index */
1107 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1108 if (*idx != -1) {
1109 *packidx = repo->packidx_cache[i];
1111 * Move this cache entry to the front. Repeatedly
1112 * searching a wrong pack index can be expensive.
1114 if (i > 0) {
1115 memmove(&repo->packidx_cache[1],
1116 &repo->packidx_cache[0],
1117 i * sizeof(repo->packidx_cache[0]));
1118 repo->packidx_cache[0] = *packidx;
1120 return NULL;
1123 /* No luck. Search the filesystem. */
1125 packdir_fd = openat(got_repo_get_fd(repo),
1126 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1127 if (packdir_fd == -1) {
1128 if (errno == ENOENT)
1129 err = got_error_no_obj(id);
1130 else
1131 err = got_error_from_errno_fmt("openat: %s/%s",
1132 got_repo_get_path_git_dir(repo),
1133 GOT_OBJECTS_PACK_DIR);
1134 goto done;
1137 packdir = fdopendir(packdir_fd);
1138 if (packdir == NULL) {
1139 err = got_error_from_errno("fdopendir");
1140 goto done;
1143 while ((dent = readdir(packdir)) != NULL) {
1144 int is_cached = 0;
1146 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1147 continue;
1149 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1150 dent->d_name) == -1) {
1151 err = got_error_from_errno("asprintf");
1152 goto done;
1155 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1156 free(path_packidx);
1157 continue; /* object will not be found in this index */
1160 for (i = 0; i < repo->pack_cache_size; i++) {
1161 if (repo->packidx_cache[i] == NULL)
1162 break;
1163 if (strcmp(repo->packidx_cache[i]->path_packidx,
1164 path_packidx) == 0) {
1165 is_cached = 1;
1166 break;
1169 if (is_cached) {
1170 free(path_packidx);
1171 continue; /* already searched */
1174 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1175 path_packidx, 0);
1176 if (err) {
1177 free(path_packidx);
1178 goto done;
1181 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1182 if (err) {
1183 free(path_packidx);
1184 goto done;
1187 err = cache_packidx(repo, *packidx, path_packidx);
1188 free(path_packidx);
1189 if (err)
1190 goto done;
1192 *idx = got_packidx_get_object_idx(*packidx, id);
1193 if (*idx != -1) {
1194 err = NULL; /* found the object */
1195 goto done;
1199 err = got_error_no_obj(id);
1200 done:
1201 if (packdir && closedir(packdir) != 0 && err == NULL)
1202 err = got_error_from_errno("closedir");
1203 return err;
1206 static const struct got_error *
1207 read_packfile_hdr(int fd, struct got_packidx *packidx)
1209 const struct got_error *err = NULL;
1210 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1211 struct got_packfile_hdr hdr;
1212 ssize_t n;
1214 n = read(fd, &hdr, sizeof(hdr));
1215 if (n < 0)
1216 return got_error_from_errno("read");
1217 if (n != sizeof(hdr))
1218 return got_error(GOT_ERR_BAD_PACKFILE);
1220 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1221 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1222 be32toh(hdr.nobjects) != totobj)
1223 err = got_error(GOT_ERR_BAD_PACKFILE);
1225 return err;
1228 static const struct got_error *
1229 open_packfile(int *fd, struct got_repository *repo,
1230 const char *relpath, struct got_packidx *packidx)
1232 const struct got_error *err = NULL;
1234 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1235 if (*fd == -1)
1236 return got_error_from_errno_fmt("openat: %s/%s",
1237 got_repo_get_path_git_dir(repo), relpath);
1239 if (packidx) {
1240 err = read_packfile_hdr(*fd, packidx);
1241 if (err) {
1242 close(*fd);
1243 *fd = -1;
1247 return err;
1250 const struct got_error *
1251 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1252 const char *path_packfile, struct got_packidx *packidx)
1254 const struct got_error *err = NULL;
1255 struct got_pack *pack = NULL;
1256 struct stat sb;
1257 size_t i;
1259 if (packp)
1260 *packp = NULL;
1262 for (i = 0; i < repo->pack_cache_size; i++) {
1263 pack = &repo->packs[i];
1264 if (pack->path_packfile == NULL)
1265 break;
1266 if (strcmp(pack->path_packfile, path_packfile) == 0)
1267 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1270 if (i == repo->pack_cache_size) {
1271 err = got_pack_close(&repo->packs[i - 1]);
1272 if (err)
1273 return err;
1274 memmove(&repo->packs[1], &repo->packs[0],
1275 sizeof(repo->packs) - sizeof(repo->packs[0]));
1276 i = 0;
1279 pack = &repo->packs[i];
1281 pack->path_packfile = strdup(path_packfile);
1282 if (pack->path_packfile == NULL) {
1283 err = got_error_from_errno("strdup");
1284 goto done;
1287 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1288 if (err)
1289 goto done;
1291 if (fstat(pack->fd, &sb) != 0) {
1292 err = got_error_from_errno("fstat");
1293 goto done;
1295 pack->filesize = sb.st_size;
1297 pack->privsep_child = NULL;
1299 #ifndef GOT_PACK_NO_MMAP
1300 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1301 pack->fd, 0);
1302 if (pack->map == MAP_FAILED) {
1303 if (errno != ENOMEM) {
1304 err = got_error_from_errno("mmap");
1305 goto done;
1307 pack->map = NULL; /* fall back to read(2) */
1309 #endif
1310 done:
1311 if (err) {
1312 if (pack) {
1313 free(pack->path_packfile);
1314 memset(pack, 0, sizeof(*pack));
1316 } else if (packp)
1317 *packp = pack;
1318 return err;
1321 struct got_pack *
1322 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1324 struct got_pack *pack = NULL;
1325 size_t i;
1327 for (i = 0; i < repo->pack_cache_size; i++) {
1328 pack = &repo->packs[i];
1329 if (pack->path_packfile == NULL)
1330 break;
1331 if (strcmp(pack->path_packfile, path_packfile) == 0)
1332 return pack;
1335 return NULL;
1338 const struct got_error *
1339 got_repo_init(const char *repo_path)
1341 const struct got_error *err = NULL;
1342 const char *dirnames[] = {
1343 GOT_OBJECTS_DIR,
1344 GOT_OBJECTS_PACK_DIR,
1345 GOT_REFS_DIR,
1347 const char *description_str = "Unnamed repository; "
1348 "edit this file 'description' to name the repository.";
1349 const char *headref_str = "ref: refs/heads/main";
1350 const char *gitconfig_str = "[core]\n"
1351 "\trepositoryformatversion = 0\n"
1352 "\tfilemode = true\n"
1353 "\tbare = true\n";
1354 char *path;
1355 size_t i;
1357 if (!got_path_dir_is_empty(repo_path))
1358 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1360 for (i = 0; i < nitems(dirnames); i++) {
1361 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1362 return got_error_from_errno("asprintf");
1364 err = got_path_mkdir(path);
1365 free(path);
1366 if (err)
1367 return err;
1370 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1371 return got_error_from_errno("asprintf");
1372 err = got_path_create_file(path, description_str);
1373 free(path);
1374 if (err)
1375 return err;
1377 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1378 return got_error_from_errno("asprintf");
1379 err = got_path_create_file(path, headref_str);
1380 free(path);
1381 if (err)
1382 return err;
1384 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1385 return got_error_from_errno("asprintf");
1386 err = got_path_create_file(path, gitconfig_str);
1387 free(path);
1388 if (err)
1389 return err;
1391 return NULL;
1394 static const struct got_error *
1395 match_packed_object(struct got_object_id **unique_id,
1396 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1398 const struct got_error *err = NULL;
1399 DIR *packdir = NULL;
1400 struct dirent *dent;
1401 char *path_packidx;
1402 struct got_object_id_queue matched_ids;
1403 int packdir_fd;
1405 STAILQ_INIT(&matched_ids);
1407 packdir_fd = openat(got_repo_get_fd(repo),
1408 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1409 if (packdir_fd == -1) {
1410 if (errno != ENOENT)
1411 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1412 goto done;
1415 packdir = fdopendir(packdir_fd);
1416 if (packdir == NULL) {
1417 err = got_error_from_errno("fdopendir");
1418 goto done;
1421 while ((dent = readdir(packdir)) != NULL) {
1422 struct got_packidx *packidx;
1423 struct got_object_qid *qid;
1425 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1426 continue;
1428 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1429 dent->d_name) == -1) {
1430 err = got_error_from_errno("strdup");
1431 break;
1434 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1435 path_packidx, 0);
1436 free(path_packidx);
1437 if (err)
1438 break;
1440 err = got_packidx_match_id_str_prefix(&matched_ids,
1441 packidx, id_str_prefix);
1442 if (err) {
1443 got_packidx_close(packidx);
1444 break;
1446 err = got_packidx_close(packidx);
1447 if (err)
1448 break;
1450 STAILQ_FOREACH(qid, &matched_ids, entry) {
1451 if (obj_type != GOT_OBJ_TYPE_ANY) {
1452 int matched_type;
1453 err = got_object_get_type(&matched_type, repo,
1454 qid->id);
1455 if (err)
1456 goto done;
1457 if (matched_type != obj_type)
1458 continue;
1460 if (*unique_id == NULL) {
1461 *unique_id = got_object_id_dup(qid->id);
1462 if (*unique_id == NULL) {
1463 err = got_error_from_errno("malloc");
1464 goto done;
1466 } else {
1467 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1468 continue; /* packed multiple times */
1469 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1470 goto done;
1474 done:
1475 got_object_id_queue_free(&matched_ids);
1476 if (packdir && closedir(packdir) != 0 && err == NULL)
1477 err = got_error_from_errno("closedir");
1478 if (err) {
1479 free(*unique_id);
1480 *unique_id = NULL;
1482 return err;
1485 static const struct got_error *
1486 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1487 const char *object_dir, const char *id_str_prefix, int obj_type,
1488 struct got_repository *repo)
1490 const struct got_error *err = NULL;
1491 char *path;
1492 DIR *dir = NULL;
1493 struct dirent *dent;
1494 struct got_object_id id;
1496 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1497 err = got_error_from_errno("asprintf");
1498 goto done;
1501 dir = opendir(path);
1502 if (dir == NULL) {
1503 if (errno == ENOENT) {
1504 err = NULL;
1505 goto done;
1507 err = got_error_from_errno2("opendir", path);
1508 goto done;
1510 while ((dent = readdir(dir)) != NULL) {
1511 char *id_str;
1512 int cmp;
1514 if (strcmp(dent->d_name, ".") == 0 ||
1515 strcmp(dent->d_name, "..") == 0)
1516 continue;
1518 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1519 err = got_error_from_errno("asprintf");
1520 goto done;
1523 if (!got_parse_sha1_digest(id.sha1, id_str))
1524 continue;
1527 * Directory entries do not necessarily appear in
1528 * sorted order, so we must iterate over all of them.
1530 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1531 if (cmp != 0) {
1532 free(id_str);
1533 continue;
1536 if (*unique_id == NULL) {
1537 if (obj_type != GOT_OBJ_TYPE_ANY) {
1538 int matched_type;
1539 err = got_object_get_type(&matched_type, repo,
1540 &id);
1541 if (err)
1542 goto done;
1543 if (matched_type != obj_type)
1544 continue;
1546 *unique_id = got_object_id_dup(&id);
1547 if (*unique_id == NULL) {
1548 err = got_error_from_errno("got_object_id_dup");
1549 free(id_str);
1550 goto done;
1552 } else {
1553 if (got_object_id_cmp(*unique_id, &id) == 0)
1554 continue; /* both packed and loose */
1555 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1556 free(id_str);
1557 goto done;
1560 done:
1561 if (dir && closedir(dir) != 0 && err == NULL)
1562 err = got_error_from_errno("closedir");
1563 if (err) {
1564 free(*unique_id);
1565 *unique_id = NULL;
1567 free(path);
1568 return err;
1571 const struct got_error *
1572 got_repo_match_object_id_prefix(struct got_object_id **id,
1573 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1575 const struct got_error *err = NULL;
1576 char *path_objects = got_repo_get_path_objects(repo);
1577 char *object_dir = NULL;
1578 size_t len;
1579 int i;
1581 *id = NULL;
1583 for (i = 0; i < strlen(id_str_prefix); i++) {
1584 if (isxdigit((unsigned char)id_str_prefix[i]))
1585 continue;
1586 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1589 len = strlen(id_str_prefix);
1590 if (len >= 2) {
1591 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1592 if (err)
1593 goto done;
1594 object_dir = strndup(id_str_prefix, 2);
1595 if (object_dir == NULL) {
1596 err = got_error_from_errno("strdup");
1597 goto done;
1599 err = match_loose_object(id, path_objects, object_dir,
1600 id_str_prefix, obj_type, repo);
1601 } else if (len == 1) {
1602 int i;
1603 for (i = 0; i < 0xf; i++) {
1604 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1605 == -1) {
1606 err = got_error_from_errno("asprintf");
1607 goto done;
1609 err = match_packed_object(id, repo, object_dir,
1610 obj_type);
1611 if (err)
1612 goto done;
1613 err = match_loose_object(id, path_objects, object_dir,
1614 id_str_prefix, obj_type, repo);
1615 if (err)
1616 goto done;
1618 } else {
1619 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1620 goto done;
1622 done:
1623 free(object_dir);
1624 if (err) {
1625 free(*id);
1626 *id = NULL;
1627 } else if (*id == NULL) {
1628 switch (obj_type) {
1629 case GOT_OBJ_TYPE_BLOB:
1630 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1631 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1632 break;
1633 case GOT_OBJ_TYPE_TREE:
1634 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1635 GOT_OBJ_LABEL_TREE, id_str_prefix);
1636 break;
1637 case GOT_OBJ_TYPE_COMMIT:
1638 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1639 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1640 break;
1641 case GOT_OBJ_TYPE_TAG:
1642 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1643 GOT_OBJ_LABEL_TAG, id_str_prefix);
1644 break;
1645 default:
1646 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1647 break;
1651 return err;
1654 const struct got_error *
1655 got_repo_match_object_id(struct got_object_id **id, char **label,
1656 const char *id_str, int obj_type, struct got_reflist_head *refs,
1657 struct got_repository *repo)
1659 const struct got_error *err;
1660 struct got_tag_object *tag;
1661 struct got_reference *ref = NULL;
1663 *id = NULL;
1664 if (label)
1665 *label = NULL;
1667 if (refs) {
1668 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1669 refs, repo);
1670 if (err == NULL) {
1671 *id = got_object_id_dup(
1672 got_object_tag_get_object_id(tag));
1673 if (*id == NULL)
1674 err = got_error_from_errno("got_object_id_dup");
1675 else if (label && asprintf(label, "refs/tags/%s",
1676 got_object_tag_get_name(tag)) == -1) {
1677 err = got_error_from_errno("asprintf");
1678 free(*id);
1679 *id = NULL;
1681 got_object_tag_close(tag);
1682 return err;
1683 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1684 err->code != GOT_ERR_NO_OBJ)
1685 return err;
1688 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1689 if (err) {
1690 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1691 return err;
1692 err = got_ref_open(&ref, repo, id_str, 0);
1693 if (err != NULL)
1694 goto done;
1695 if (label) {
1696 *label = strdup(got_ref_get_name(ref));
1697 if (*label == NULL) {
1698 err = got_error_from_errno("strdup");
1699 goto done;
1702 err = got_ref_resolve(id, repo, ref);
1703 } else if (label) {
1704 err = got_object_id_str(label, *id);
1705 if (*label == NULL) {
1706 err = got_error_from_errno("strdup");
1707 goto done;
1710 done:
1711 if (ref)
1712 got_ref_close(ref);
1713 return err;
1716 const struct got_error *
1717 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1718 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1720 const struct got_error *err = NULL;
1721 struct got_reflist_entry *re;
1722 struct got_object_id *tag_id;
1723 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1725 *tag = NULL;
1727 TAILQ_FOREACH(re, refs, entry) {
1728 const char *refname;
1729 refname = got_ref_get_name(re->ref);
1730 if (got_ref_is_symbolic(re->ref))
1731 continue;
1732 if (strncmp(refname, "refs/tags/", 10) != 0)
1733 continue;
1734 if (!name_is_absolute)
1735 refname += strlen("refs/tags/");
1736 if (strcmp(refname, name) != 0)
1737 continue;
1738 err = got_ref_resolve(&tag_id, repo, re->ref);
1739 if (err)
1740 break;
1741 err = got_object_open_as_tag(tag, repo, tag_id);
1742 free(tag_id);
1743 if (err)
1744 break;
1745 if (obj_type == GOT_OBJ_TYPE_ANY ||
1746 got_object_tag_get_object_type(*tag) == obj_type)
1747 break;
1748 got_object_tag_close(*tag);
1749 *tag = NULL;
1752 if (err == NULL && *tag == NULL)
1753 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1754 GOT_OBJ_LABEL_TAG, name);
1755 return err;
1758 static const struct got_error *
1759 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1760 const char *name, mode_t mode, struct got_object_id *blob_id)
1762 const struct got_error *err = NULL;
1764 *new_te = NULL;
1766 *new_te = calloc(1, sizeof(**new_te));
1767 if (*new_te == NULL)
1768 return got_error_from_errno("calloc");
1770 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1771 sizeof((*new_te)->name)) {
1772 err = got_error(GOT_ERR_NO_SPACE);
1773 goto done;
1776 if (S_ISLNK(mode)) {
1777 (*new_te)->mode = S_IFLNK;
1778 } else {
1779 (*new_te)->mode = S_IFREG;
1780 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1782 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1783 done:
1784 if (err && *new_te) {
1785 free(*new_te);
1786 *new_te = NULL;
1788 return err;
1791 static const struct got_error *
1792 import_file(struct got_tree_entry **new_te, struct dirent *de,
1793 const char *path, struct got_repository *repo)
1795 const struct got_error *err;
1796 struct got_object_id *blob_id = NULL;
1797 char *filepath;
1798 struct stat sb;
1800 if (asprintf(&filepath, "%s%s%s", path,
1801 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1802 return got_error_from_errno("asprintf");
1804 if (lstat(filepath, &sb) != 0) {
1805 err = got_error_from_errno2("lstat", path);
1806 goto done;
1809 err = got_object_blob_create(&blob_id, filepath, repo);
1810 if (err)
1811 goto done;
1813 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1814 blob_id);
1815 done:
1816 free(filepath);
1817 if (err)
1818 free(blob_id);
1819 return err;
1822 static const struct got_error *
1823 insert_tree_entry(struct got_tree_entry *new_te,
1824 struct got_pathlist_head *paths)
1826 const struct got_error *err = NULL;
1827 struct got_pathlist_entry *new_pe;
1829 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1830 if (err)
1831 return err;
1832 if (new_pe == NULL)
1833 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1834 return NULL;
1837 static const struct got_error *write_tree(struct got_object_id **,
1838 const char *, struct got_pathlist_head *, struct got_repository *,
1839 got_repo_import_cb progress_cb, void *progress_arg);
1841 static const struct got_error *
1842 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1843 const char *path, struct got_pathlist_head *ignores,
1844 struct got_repository *repo,
1845 got_repo_import_cb progress_cb, void *progress_arg)
1847 const struct got_error *err;
1848 struct got_object_id *id = NULL;
1849 char *subdirpath;
1851 if (asprintf(&subdirpath, "%s%s%s", path,
1852 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1853 return got_error_from_errno("asprintf");
1855 (*new_te) = calloc(1, sizeof(**new_te));
1856 if (*new_te == NULL)
1857 return got_error_from_errno("calloc");
1858 (*new_te)->mode = S_IFDIR;
1859 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1860 sizeof((*new_te)->name)) {
1861 err = got_error(GOT_ERR_NO_SPACE);
1862 goto done;
1864 err = write_tree(&id, subdirpath, ignores, repo,
1865 progress_cb, progress_arg);
1866 if (err)
1867 goto done;
1868 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1870 done:
1871 free(id);
1872 free(subdirpath);
1873 if (err) {
1874 free(*new_te);
1875 *new_te = NULL;
1877 return err;
1880 static const struct got_error *
1881 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1882 struct got_pathlist_head *ignores, struct got_repository *repo,
1883 got_repo_import_cb progress_cb, void *progress_arg)
1885 const struct got_error *err = NULL;
1886 DIR *dir;
1887 struct dirent *de;
1888 int nentries;
1889 struct got_tree_entry *new_te = NULL;
1890 struct got_pathlist_head paths;
1891 struct got_pathlist_entry *pe;
1893 *new_tree_id = NULL;
1895 TAILQ_INIT(&paths);
1897 dir = opendir(path_dir);
1898 if (dir == NULL) {
1899 err = got_error_from_errno2("opendir", path_dir);
1900 goto done;
1903 nentries = 0;
1904 while ((de = readdir(dir)) != NULL) {
1905 int ignore = 0;
1906 int type;
1908 if (strcmp(de->d_name, ".") == 0 ||
1909 strcmp(de->d_name, "..") == 0)
1910 continue;
1912 TAILQ_FOREACH(pe, ignores, entry) {
1913 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1914 ignore = 1;
1915 break;
1918 if (ignore)
1919 continue;
1921 err = got_path_dirent_type(&type, path_dir, de);
1922 if (err)
1923 goto done;
1925 if (type == DT_DIR) {
1926 err = import_subdir(&new_te, de, path_dir,
1927 ignores, repo, progress_cb, progress_arg);
1928 if (err) {
1929 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1930 goto done;
1931 err = NULL;
1932 continue;
1934 } else if (type == DT_REG || type == DT_LNK) {
1935 err = import_file(&new_te, de, path_dir, repo);
1936 if (err)
1937 goto done;
1938 } else
1939 continue;
1941 err = insert_tree_entry(new_te, &paths);
1942 if (err)
1943 goto done;
1944 nentries++;
1947 if (TAILQ_EMPTY(&paths)) {
1948 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1949 "cannot create tree without any entries");
1950 goto done;
1953 TAILQ_FOREACH(pe, &paths, entry) {
1954 struct got_tree_entry *te = pe->data;
1955 char *path;
1956 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1957 continue;
1958 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1959 err = got_error_from_errno("asprintf");
1960 goto done;
1962 err = (*progress_cb)(progress_arg, path);
1963 free(path);
1964 if (err)
1965 goto done;
1968 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1969 done:
1970 if (dir)
1971 closedir(dir);
1972 got_pathlist_free(&paths);
1973 return err;
1976 const struct got_error *
1977 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1978 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1979 struct got_repository *repo, got_repo_import_cb progress_cb,
1980 void *progress_arg)
1982 const struct got_error *err;
1983 struct got_object_id *new_tree_id;
1985 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1986 progress_cb, progress_arg);
1987 if (err)
1988 return err;
1990 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1991 author, time(NULL), author, time(NULL), logmsg, repo);
1992 free(new_tree_id);
1993 return err;
1996 const struct got_error *
1997 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1998 struct got_repository *repo)
2000 const struct got_error *err = NULL;
2001 char *path_objects = NULL, *path = NULL;
2002 DIR *dir = NULL;
2003 struct got_object_id id;
2004 int i;
2006 *nobjects = 0;
2007 *ondisk_size = 0;
2009 path_objects = got_repo_get_path_objects(repo);
2010 if (path_objects == NULL)
2011 return got_error_from_errno("got_repo_get_path_objects");
2013 for (i = 0; i <= 0xff; i++) {
2014 struct dirent *dent;
2016 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2017 err = got_error_from_errno("asprintf");
2018 break;
2021 dir = opendir(path);
2022 if (dir == NULL) {
2023 if (errno == ENOENT) {
2024 err = NULL;
2025 continue;
2027 err = got_error_from_errno2("opendir", path);
2028 break;
2031 while ((dent = readdir(dir)) != NULL) {
2032 char *id_str;
2033 int fd;
2034 struct stat sb;
2036 if (strcmp(dent->d_name, ".") == 0 ||
2037 strcmp(dent->d_name, "..") == 0)
2038 continue;
2040 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2041 err = got_error_from_errno("asprintf");
2042 goto done;
2045 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2046 free(id_str);
2047 continue;
2049 free(id_str);
2051 err = got_object_open_loose_fd(&fd, &id, repo);
2052 if (err)
2053 goto done;
2055 if (fstat(fd, &sb) == -1) {
2056 err = got_error_from_errno("fstat");
2057 close(fd);
2058 goto done;
2060 (*nobjects)++;
2061 (*ondisk_size) += sb.st_size;
2063 if (close(fd) == -1) {
2064 err = got_error_from_errno("close");
2065 goto done;
2069 if (closedir(dir) != 0) {
2070 err = got_error_from_errno("closedir");
2071 goto done;
2073 dir = NULL;
2075 free(path);
2076 path = NULL;
2078 done:
2079 if (dir && closedir(dir) != 0 && err == NULL)
2080 err = got_error_from_errno("closedir");
2082 if (err) {
2083 *nobjects = 0;
2084 *ondisk_size = 0;
2086 free(path_objects);
2087 free(path);
2088 return err;
2091 const struct got_error *
2092 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2093 off_t *total_packsize, struct got_repository *repo)
2095 const struct got_error *err = NULL;
2096 DIR *packdir = NULL;
2097 struct dirent *dent;
2098 struct got_packidx *packidx = NULL;
2099 char *path_packidx;
2100 char *path_packfile;
2101 int packdir_fd;
2102 struct stat sb;
2104 *npackfiles = 0;
2105 *nobjects = 0;
2106 *total_packsize = 0;
2108 packdir_fd = openat(got_repo_get_fd(repo),
2109 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2110 if (packdir_fd == -1) {
2111 return got_error_from_errno_fmt("openat: %s/%s",
2112 got_repo_get_path_git_dir(repo),
2113 GOT_OBJECTS_PACK_DIR);
2116 packdir = fdopendir(packdir_fd);
2117 if (packdir == NULL) {
2118 err = got_error_from_errno("fdopendir");
2119 goto done;
2122 while ((dent = readdir(packdir)) != NULL) {
2123 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2124 continue;
2126 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2127 dent->d_name) == -1) {
2128 err = got_error_from_errno("asprintf");
2129 goto done;
2132 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2133 path_packidx, 0);
2134 free(path_packidx);
2135 if (err)
2136 goto done;
2138 if (fstat(packidx->fd, &sb) == -1)
2139 goto done;
2140 *total_packsize += sb.st_size;
2142 err = got_packidx_get_packfile_path(&path_packfile,
2143 packidx->path_packidx);
2144 if (err)
2145 goto done;
2147 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2148 0) == -1) {
2149 free(path_packfile);
2150 goto done;
2152 free(path_packfile);
2153 *total_packsize += sb.st_size;
2155 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2157 (*npackfiles)++;
2159 got_packidx_close(packidx);
2160 packidx = NULL;
2162 done:
2163 if (packidx)
2164 got_packidx_close(packidx);
2165 if (packdir && closedir(packdir) != 0 && err == NULL)
2166 err = got_error_from_errno("closedir");
2167 if (err) {
2168 *npackfiles = 0;
2169 *nobjects = 0;
2170 *total_packsize = 0;
2172 return err;
2175 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2176 got_packidx_bloom_filter_cmp);