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 *const 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 return got_error_from_errno("calloc");
664 RB_INIT(&repo->packidx_bloom_filters);
665 TAILQ_INIT(&repo->packidx_paths);
667 for (i = 0; i < nitems(repo->privsep_children); i++) {
668 memset(&repo->privsep_children[i], 0,
669 sizeof(repo->privsep_children[0]));
670 repo->privsep_children[i].imsg_fd = -1;
673 err = got_object_cache_init(&repo->objcache,
674 GOT_OBJECT_CACHE_TYPE_OBJ);
675 if (err)
676 goto done;
677 err = got_object_cache_init(&repo->treecache,
678 GOT_OBJECT_CACHE_TYPE_TREE);
679 if (err)
680 goto done;
681 err = got_object_cache_init(&repo->commitcache,
682 GOT_OBJECT_CACHE_TYPE_COMMIT);
683 if (err)
684 goto done;
685 err = got_object_cache_init(&repo->tagcache,
686 GOT_OBJECT_CACHE_TYPE_TAG);
687 if (err)
688 goto done;
689 err = got_object_cache_init(&repo->rawcache,
690 GOT_OBJECT_CACHE_TYPE_RAW);
691 if (err)
692 goto done;
694 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
695 if (repo->pack_cache_size > rl.rlim_cur / 8)
696 repo->pack_cache_size = rl.rlim_cur / 8;
698 repo_path = realpath(path, NULL);
699 if (repo_path == NULL) {
700 err = got_error_from_errno2("realpath", path);
701 goto done;
704 for (;;) {
705 char *parent_path;
707 err = open_repo(repo, repo_path);
708 if (err == NULL)
709 break;
710 if (err->code != GOT_ERR_NOT_GIT_REPO)
711 goto done;
712 if (repo_path[0] == '/' && repo_path[1] == '\0') {
713 err = got_error(GOT_ERR_NOT_GIT_REPO);
714 goto done;
716 err = got_path_dirname(&parent_path, repo_path);
717 if (err)
718 goto done;
719 free(repo_path);
720 repo_path = parent_path;
723 err = read_gotconfig(repo);
724 if (err)
725 goto done;
727 err = read_gitconfig(repo, global_gitconfig_path);
728 if (err)
729 goto done;
730 if (repo->gitconfig_repository_format_version != 0)
731 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
732 for (i = 0; i < repo->nextensions; i++) {
733 char *ext = repo->extensions[i];
734 int j, supported = 0;
735 for (j = 0; j < nitems(repo_extensions); j++) {
736 if (strcmp(ext, repo_extensions[j]) == 0) {
737 supported = 1;
738 break;
741 if (!supported) {
742 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
743 goto done;
747 err = got_repo_list_packidx(&repo->packidx_paths, repo);
748 done:
749 if (err)
750 got_repo_close(repo);
751 else
752 *repop = repo;
753 free(repo_path);
754 return err;
757 const struct got_error *
758 got_repo_close(struct got_repository *repo)
760 const struct got_error *err = NULL, *child_err;
761 struct got_packidx_bloom_filter *bf;
762 struct got_pathlist_entry *pe;
763 size_t i;
765 for (i = 0; i < repo->pack_cache_size; i++) {
766 if (repo->packidx_cache[i] == NULL)
767 break;
768 got_packidx_close(repo->packidx_cache[i]);
771 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
772 &repo->packidx_bloom_filters))) {
773 RB_REMOVE(got_packidx_bloom_filter_tree,
774 &repo->packidx_bloom_filters, bf);
775 free(bf->bloom);
776 free(bf);
779 for (i = 0; i < repo->pack_cache_size; i++) {
780 if (repo->packs[i].path_packfile == NULL)
781 break;
782 got_pack_close(&repo->packs[i]);
785 free(repo->path);
786 free(repo->path_git_dir);
788 got_object_cache_close(&repo->objcache);
789 got_object_cache_close(&repo->treecache);
790 got_object_cache_close(&repo->commitcache);
791 got_object_cache_close(&repo->tagcache);
792 got_object_cache_close(&repo->rawcache);
794 for (i = 0; i < nitems(repo->privsep_children); i++) {
795 if (repo->privsep_children[i].imsg_fd == -1)
796 continue;
797 imsg_clear(repo->privsep_children[i].ibuf);
798 free(repo->privsep_children[i].ibuf);
799 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
800 child_err = got_privsep_wait_for_child(
801 repo->privsep_children[i].pid);
802 if (child_err && err == NULL)
803 err = child_err;
804 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
805 err == NULL)
806 err = got_error_from_errno("close");
809 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
810 err == NULL)
811 err = got_error_from_errno("close");
813 if (repo->gotconfig)
814 got_gotconfig_free(repo->gotconfig);
815 free(repo->gitconfig_author_name);
816 free(repo->gitconfig_author_email);
817 for (i = 0; i < repo->ngitconfig_remotes; i++)
818 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
819 free(repo->gitconfig_remotes);
820 for (i = 0; i < repo->nextensions; i++)
821 free(repo->extensions[i]);
822 free(repo->extensions);
824 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
825 free((void *)pe->path);
826 got_pathlist_free(&repo->packidx_paths);
827 free(repo);
829 return err;
832 void
833 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
835 int i;
837 free(repo->name);
838 repo->name = NULL;
839 free(repo->fetch_url);
840 repo->fetch_url = NULL;
841 free(repo->send_url);
842 repo->send_url = NULL;
843 for (i = 0; i < repo->nfetch_branches; i++)
844 free(repo->fetch_branches[i]);
845 free(repo->fetch_branches);
846 repo->fetch_branches = NULL;
847 repo->nfetch_branches = 0;
848 for (i = 0; i < repo->nsend_branches; i++)
849 free(repo->send_branches[i]);
850 free(repo->send_branches);
851 repo->send_branches = NULL;
852 repo->nsend_branches = 0;
855 const struct got_error *
856 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
857 const char *input_path)
859 const struct got_error *err = NULL;
860 const char *repo_abspath = NULL;
861 size_t repolen, len;
862 char *canonpath, *path = NULL;
864 *in_repo_path = NULL;
866 canonpath = strdup(input_path);
867 if (canonpath == NULL) {
868 err = got_error_from_errno("strdup");
869 goto done;
871 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
872 if (err)
873 goto done;
875 repo_abspath = got_repo_get_path(repo);
877 if (canonpath[0] == '\0') {
878 path = strdup(canonpath);
879 if (path == NULL) {
880 err = got_error_from_errno("strdup");
881 goto done;
883 } else {
884 path = realpath(canonpath, NULL);
885 if (path == NULL) {
886 if (errno != ENOENT) {
887 err = got_error_from_errno2("realpath",
888 canonpath);
889 goto done;
891 /*
892 * Path is not on disk.
893 * Assume it is already relative to repository root.
894 */
895 path = strdup(canonpath);
896 if (path == NULL) {
897 err = got_error_from_errno("strdup");
898 goto done;
902 repolen = strlen(repo_abspath);
903 len = strlen(path);
906 if (strcmp(path, repo_abspath) == 0) {
907 free(path);
908 path = strdup("");
909 if (path == NULL) {
910 err = got_error_from_errno("strdup");
911 goto done;
913 } else if (len > repolen &&
914 got_path_is_child(path, repo_abspath, repolen)) {
915 /* Matched an on-disk path inside repository. */
916 if (got_repo_is_bare(repo)) {
917 /*
918 * Matched an on-disk path inside repository
919 * database. Treat input as repository-relative.
920 */
921 free(path);
922 path = canonpath;
923 canonpath = NULL;
924 } else {
925 char *child;
926 /* Strip common prefix with repository path. */
927 err = got_path_skip_common_ancestor(&child,
928 repo_abspath, path);
929 if (err)
930 goto done;
931 free(path);
932 path = child;
934 } else {
935 /*
936 * Matched unrelated on-disk path.
937 * Treat input as repository-relative.
938 */
939 free(path);
940 path = canonpath;
941 canonpath = NULL;
945 /* Make in-repository path absolute */
946 if (path[0] != '/') {
947 char *abspath;
948 if (asprintf(&abspath, "/%s", path) == -1) {
949 err = got_error_from_errno("asprintf");
950 goto done;
952 free(path);
953 path = abspath;
956 done:
957 free(canonpath);
958 if (err)
959 free(path);
960 else
961 *in_repo_path = path;
962 return err;
965 static const struct got_error *
966 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
967 const char *path_packidx)
969 const struct got_error *err = NULL;
970 size_t i;
972 for (i = 0; i < repo->pack_cache_size; i++) {
973 if (repo->packidx_cache[i] == NULL)
974 break;
975 if (strcmp(repo->packidx_cache[i]->path_packidx,
976 path_packidx) == 0) {
977 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
980 if (i == repo->pack_cache_size) {
981 i = repo->pack_cache_size - 1;
982 err = got_packidx_close(repo->packidx_cache[i]);
983 if (err)
984 return err;
987 repo->packidx_cache[i] = packidx;
989 return NULL;
992 int
993 got_repo_is_packidx_filename(const char *name, size_t len)
995 if (len != GOT_PACKIDX_NAMELEN)
996 return 0;
998 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
999 return 0;
1001 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1002 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1003 return 0;
1005 return 1;
1008 static struct got_packidx_bloom_filter *
1009 get_packidx_bloom_filter(struct got_repository *repo,
1010 const char *path, size_t path_len)
1012 struct got_packidx_bloom_filter key;
1014 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1015 return NULL; /* XXX */
1016 key.path_len = path_len;
1018 return RB_FIND(got_packidx_bloom_filter_tree,
1019 &repo->packidx_bloom_filters, &key);
1022 int
1023 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1024 const char *path_packidx, struct got_object_id *id)
1026 struct got_packidx_bloom_filter *bf;
1028 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1029 if (bf)
1030 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1032 /* No bloom filter means this pack index must be searched. */
1033 return 1;
1036 static const struct got_error *
1037 add_packidx_bloom_filter(struct got_repository *repo,
1038 struct got_packidx *packidx, const char *path_packidx)
1040 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1041 struct got_packidx_bloom_filter *bf;
1042 size_t len;
1045 * Don't use bloom filters for very large pack index files.
1046 * Large pack files will contain a relatively large fraction
1047 * of our objects so we will likely need to visit them anyway.
1048 * The more objects a pack file contains the higher the probability
1049 * of a false-positive match from the bloom filter. And reading
1050 * all object IDs from a large pack index file can be expensive.
1052 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1053 return NULL;
1055 /* Do we already have a filter for this pack index? */
1056 if (get_packidx_bloom_filter(repo, path_packidx,
1057 strlen(path_packidx)) != NULL)
1058 return NULL;
1060 bf = calloc(1, sizeof(*bf));
1061 if (bf == NULL)
1062 return got_error_from_errno("calloc");
1063 bf->bloom = calloc(1, sizeof(*bf->bloom));
1064 if (bf->bloom == NULL) {
1065 free(bf);
1066 return got_error_from_errno("calloc");
1069 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1070 if (len >= sizeof(bf->path)) {
1071 free(bf->bloom);
1072 free(bf);
1073 return got_error(GOT_ERR_NO_SPACE);
1075 bf->path_len = len;
1077 /* Minimum size supported by our bloom filter is 1000 entries. */
1078 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1079 for (i = 0; i < nobjects; i++) {
1080 struct got_packidx_object_id *id;
1081 id = &packidx->hdr.sorted_ids[i];
1082 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1085 RB_INSERT(got_packidx_bloom_filter_tree,
1086 &repo->packidx_bloom_filters, bf);
1087 return NULL;
1090 const struct got_error *
1091 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1092 struct got_repository *repo, struct got_object_id *id)
1094 const struct got_error *err;
1095 struct got_pathlist_entry *pe;
1096 size_t i;
1098 /* Search pack index cache. */
1099 for (i = 0; i < repo->pack_cache_size; i++) {
1100 if (repo->packidx_cache[i] == NULL)
1101 break;
1102 if (!got_repo_check_packidx_bloom_filter(repo,
1103 repo->packidx_cache[i]->path_packidx, id))
1104 continue; /* object will not be found in this index */
1105 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1106 if (*idx != -1) {
1107 *packidx = repo->packidx_cache[i];
1109 * Move this cache entry to the front. Repeatedly
1110 * searching a wrong pack index can be expensive.
1112 if (i > 0) {
1113 memmove(&repo->packidx_cache[1],
1114 &repo->packidx_cache[0],
1115 i * sizeof(repo->packidx_cache[0]));
1116 repo->packidx_cache[0] = *packidx;
1118 return NULL;
1121 /* No luck. Search the filesystem. */
1123 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1124 const char *path_packidx = pe->path;
1125 int is_cached = 0;
1127 if (!got_repo_check_packidx_bloom_filter(repo,
1128 pe->path, id))
1129 continue; /* object will not be found in this index */
1131 for (i = 0; i < repo->pack_cache_size; i++) {
1132 if (repo->packidx_cache[i] == NULL)
1133 break;
1134 if (strcmp(repo->packidx_cache[i]->path_packidx,
1135 path_packidx) == 0) {
1136 is_cached = 1;
1137 break;
1140 if (is_cached)
1141 continue; /* already searched */
1143 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1144 path_packidx, 0);
1145 if (err)
1146 goto done;
1148 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1149 if (err)
1150 goto done;
1152 err = cache_packidx(repo, *packidx, path_packidx);
1153 if (err)
1154 goto done;
1156 *idx = got_packidx_get_object_idx(*packidx, id);
1157 if (*idx != -1) {
1158 err = NULL; /* found the object */
1159 goto done;
1163 err = got_error_no_obj(id);
1164 done:
1165 return err;
1168 const struct got_error *
1169 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1170 struct got_repository *repo)
1172 const struct got_error *err = NULL;
1173 DIR *packdir = NULL;
1174 struct dirent *dent;
1175 char *path_packidx = NULL;
1176 int packdir_fd;
1178 packdir_fd = openat(got_repo_get_fd(repo),
1179 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1180 if (packdir_fd == -1) {
1181 return got_error_from_errno_fmt("openat: %s/%s",
1182 got_repo_get_path_git_dir(repo),
1183 GOT_OBJECTS_PACK_DIR);
1186 packdir = fdopendir(packdir_fd);
1187 if (packdir == NULL) {
1188 err = got_error_from_errno("fdopendir");
1189 goto done;
1192 while ((dent = readdir(packdir)) != NULL) {
1193 if (!got_repo_is_packidx_filename(dent->d_name,
1194 strlen(dent->d_name)))
1195 continue;
1197 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1198 dent->d_name) == -1) {
1199 err = got_error_from_errno("asprintf");
1200 path_packidx = NULL;
1201 break;
1204 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1205 if (err)
1206 break;
1208 done:
1209 if (err)
1210 free(path_packidx);
1211 if (packdir && closedir(packdir) != 0 && err == NULL)
1212 err = got_error_from_errno("closedir");
1213 return err;
1216 const struct got_error *
1217 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1218 struct got_repository *repo)
1220 const struct got_error *err;
1221 size_t i;
1223 *packidx = NULL;
1225 /* Search pack index cache. */
1226 for (i = 0; i < repo->pack_cache_size; i++) {
1227 if (repo->packidx_cache[i] == NULL)
1228 break;
1229 if (strcmp(repo->packidx_cache[i]->path_packidx,
1230 path_packidx) == 0) {
1231 *packidx = repo->packidx_cache[i];
1232 return NULL;
1235 /* No luck. Search the filesystem. */
1237 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1238 path_packidx, 0);
1239 if (err)
1240 return err;
1242 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1243 if (err)
1244 goto done;
1246 err = cache_packidx(repo, *packidx, path_packidx);
1247 done:
1248 if (err) {
1249 got_packidx_close(*packidx);
1250 *packidx = NULL;
1252 return err;
1255 static const struct got_error *
1256 read_packfile_hdr(int fd, struct got_packidx *packidx)
1258 const struct got_error *err = NULL;
1259 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1260 struct got_packfile_hdr hdr;
1261 ssize_t n;
1263 n = read(fd, &hdr, sizeof(hdr));
1264 if (n < 0)
1265 return got_error_from_errno("read");
1266 if (n != sizeof(hdr))
1267 return got_error(GOT_ERR_BAD_PACKFILE);
1269 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1270 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1271 be32toh(hdr.nobjects) != totobj)
1272 err = got_error(GOT_ERR_BAD_PACKFILE);
1274 return err;
1277 static const struct got_error *
1278 open_packfile(int *fd, struct got_repository *repo,
1279 const char *relpath, struct got_packidx *packidx)
1281 const struct got_error *err = NULL;
1283 *fd = openat(got_repo_get_fd(repo), relpath,
1284 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1285 if (*fd == -1)
1286 return got_error_from_errno_fmt("openat: %s/%s",
1287 got_repo_get_path_git_dir(repo), relpath);
1289 if (packidx) {
1290 err = read_packfile_hdr(*fd, packidx);
1291 if (err) {
1292 close(*fd);
1293 *fd = -1;
1297 return err;
1300 const struct got_error *
1301 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1302 const char *path_packfile, struct got_packidx *packidx)
1304 const struct got_error *err = NULL;
1305 struct got_pack *pack = NULL;
1306 struct stat sb;
1307 size_t i;
1309 if (packp)
1310 *packp = NULL;
1312 for (i = 0; i < repo->pack_cache_size; i++) {
1313 pack = &repo->packs[i];
1314 if (pack->path_packfile == NULL)
1315 break;
1316 if (strcmp(pack->path_packfile, path_packfile) == 0)
1317 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1320 if (i == repo->pack_cache_size) {
1321 err = got_pack_close(&repo->packs[i - 1]);
1322 if (err)
1323 return err;
1324 memmove(&repo->packs[1], &repo->packs[0],
1325 sizeof(repo->packs) - sizeof(repo->packs[0]));
1326 i = 0;
1329 pack = &repo->packs[i];
1331 pack->path_packfile = strdup(path_packfile);
1332 if (pack->path_packfile == NULL) {
1333 err = got_error_from_errno("strdup");
1334 goto done;
1337 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1338 if (err)
1339 goto done;
1341 if (fstat(pack->fd, &sb) != 0) {
1342 err = got_error_from_errno("fstat");
1343 goto done;
1345 pack->filesize = sb.st_size;
1347 pack->privsep_child = NULL;
1349 #ifndef GOT_PACK_NO_MMAP
1350 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1351 pack->fd, 0);
1352 if (pack->map == MAP_FAILED) {
1353 if (errno != ENOMEM) {
1354 err = got_error_from_errno("mmap");
1355 goto done;
1357 pack->map = NULL; /* fall back to read(2) */
1359 #endif
1360 done:
1361 if (err) {
1362 if (pack) {
1363 free(pack->path_packfile);
1364 memset(pack, 0, sizeof(*pack));
1366 } else if (packp)
1367 *packp = pack;
1368 return err;
1371 struct got_pack *
1372 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1374 struct got_pack *pack = NULL;
1375 size_t i;
1377 for (i = 0; i < repo->pack_cache_size; i++) {
1378 pack = &repo->packs[i];
1379 if (pack->path_packfile == NULL)
1380 break;
1381 if (strcmp(pack->path_packfile, path_packfile) == 0)
1382 return pack;
1385 return NULL;
1388 const struct got_error *
1389 got_repo_init(const char *repo_path)
1391 const struct got_error *err = NULL;
1392 const char *dirnames[] = {
1393 GOT_OBJECTS_DIR,
1394 GOT_OBJECTS_PACK_DIR,
1395 GOT_REFS_DIR,
1397 const char *description_str = "Unnamed repository; "
1398 "edit this file 'description' to name the repository.";
1399 const char *headref_str = "ref: refs/heads/main";
1400 const char *gitconfig_str = "[core]\n"
1401 "\trepositoryformatversion = 0\n"
1402 "\tfilemode = true\n"
1403 "\tbare = true\n";
1404 char *path;
1405 size_t i;
1407 if (!got_path_dir_is_empty(repo_path))
1408 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1410 for (i = 0; i < nitems(dirnames); i++) {
1411 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1412 return got_error_from_errno("asprintf");
1414 err = got_path_mkdir(path);
1415 free(path);
1416 if (err)
1417 return err;
1420 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1421 return got_error_from_errno("asprintf");
1422 err = got_path_create_file(path, description_str);
1423 free(path);
1424 if (err)
1425 return err;
1427 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1428 return got_error_from_errno("asprintf");
1429 err = got_path_create_file(path, headref_str);
1430 free(path);
1431 if (err)
1432 return err;
1434 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1435 return got_error_from_errno("asprintf");
1436 err = got_path_create_file(path, gitconfig_str);
1437 free(path);
1438 if (err)
1439 return err;
1441 return NULL;
1444 static const struct got_error *
1445 match_packed_object(struct got_object_id **unique_id,
1446 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1448 const struct got_error *err = NULL;
1449 struct got_object_id_queue matched_ids;
1450 struct got_pathlist_entry *pe;
1452 STAILQ_INIT(&matched_ids);
1454 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1455 const char *path_packidx = pe->path;
1456 struct got_packidx *packidx;
1457 struct got_object_qid *qid;
1459 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1460 path_packidx, 0);
1461 if (err)
1462 break;
1464 err = got_packidx_match_id_str_prefix(&matched_ids,
1465 packidx, id_str_prefix);
1466 if (err) {
1467 got_packidx_close(packidx);
1468 break;
1470 err = got_packidx_close(packidx);
1471 if (err)
1472 break;
1474 STAILQ_FOREACH(qid, &matched_ids, entry) {
1475 if (obj_type != GOT_OBJ_TYPE_ANY) {
1476 int matched_type;
1477 err = got_object_get_type(&matched_type, repo,
1478 &qid->id);
1479 if (err)
1480 goto done;
1481 if (matched_type != obj_type)
1482 continue;
1484 if (*unique_id == NULL) {
1485 *unique_id = got_object_id_dup(&qid->id);
1486 if (*unique_id == NULL) {
1487 err = got_error_from_errno("malloc");
1488 goto done;
1490 } else {
1491 if (got_object_id_cmp(*unique_id,
1492 &qid->id) == 0)
1493 continue; /* packed multiple times */
1494 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1495 goto done;
1499 done:
1500 got_object_id_queue_free(&matched_ids);
1501 if (err) {
1502 free(*unique_id);
1503 *unique_id = NULL;
1505 return err;
1508 static const struct got_error *
1509 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1510 const char *object_dir, const char *id_str_prefix, int obj_type,
1511 struct got_repository *repo)
1513 const struct got_error *err = NULL;
1514 char *path;
1515 DIR *dir = NULL;
1516 struct dirent *dent;
1517 struct got_object_id id;
1519 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1520 err = got_error_from_errno("asprintf");
1521 goto done;
1524 dir = opendir(path);
1525 if (dir == NULL) {
1526 if (errno == ENOENT) {
1527 err = NULL;
1528 goto done;
1530 err = got_error_from_errno2("opendir", path);
1531 goto done;
1533 while ((dent = readdir(dir)) != NULL) {
1534 char *id_str;
1535 int cmp;
1537 if (strcmp(dent->d_name, ".") == 0 ||
1538 strcmp(dent->d_name, "..") == 0)
1539 continue;
1541 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1542 err = got_error_from_errno("asprintf");
1543 goto done;
1546 if (!got_parse_sha1_digest(id.sha1, id_str))
1547 continue;
1550 * Directory entries do not necessarily appear in
1551 * sorted order, so we must iterate over all of them.
1553 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1554 if (cmp != 0) {
1555 free(id_str);
1556 continue;
1559 if (*unique_id == NULL) {
1560 if (obj_type != GOT_OBJ_TYPE_ANY) {
1561 int matched_type;
1562 err = got_object_get_type(&matched_type, repo,
1563 &id);
1564 if (err)
1565 goto done;
1566 if (matched_type != obj_type)
1567 continue;
1569 *unique_id = got_object_id_dup(&id);
1570 if (*unique_id == NULL) {
1571 err = got_error_from_errno("got_object_id_dup");
1572 free(id_str);
1573 goto done;
1575 } else {
1576 if (got_object_id_cmp(*unique_id, &id) == 0)
1577 continue; /* both packed and loose */
1578 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1579 free(id_str);
1580 goto done;
1583 done:
1584 if (dir && closedir(dir) != 0 && err == NULL)
1585 err = got_error_from_errno("closedir");
1586 if (err) {
1587 free(*unique_id);
1588 *unique_id = NULL;
1590 free(path);
1591 return err;
1594 const struct got_error *
1595 got_repo_match_object_id_prefix(struct got_object_id **id,
1596 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1598 const struct got_error *err = NULL;
1599 char *path_objects = got_repo_get_path_objects(repo);
1600 char *object_dir = NULL;
1601 size_t len;
1602 int i;
1604 *id = NULL;
1606 len = strlen(id_str_prefix);
1607 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1608 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1610 for (i = 0; i < len; i++) {
1611 if (isxdigit((unsigned char)id_str_prefix[i]))
1612 continue;
1613 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1616 if (len >= 2) {
1617 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1618 if (err)
1619 goto done;
1620 object_dir = strndup(id_str_prefix, 2);
1621 if (object_dir == NULL) {
1622 err = got_error_from_errno("strdup");
1623 goto done;
1625 err = match_loose_object(id, path_objects, object_dir,
1626 id_str_prefix, obj_type, repo);
1627 } else if (len == 1) {
1628 int i;
1629 for (i = 0; i < 0xf; i++) {
1630 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1631 == -1) {
1632 err = got_error_from_errno("asprintf");
1633 goto done;
1635 err = match_packed_object(id, repo, object_dir,
1636 obj_type);
1637 if (err)
1638 goto done;
1639 err = match_loose_object(id, path_objects, object_dir,
1640 id_str_prefix, obj_type, repo);
1641 if (err)
1642 goto done;
1644 } else {
1645 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1646 goto done;
1648 done:
1649 free(object_dir);
1650 if (err) {
1651 free(*id);
1652 *id = NULL;
1653 } else if (*id == NULL) {
1654 switch (obj_type) {
1655 case GOT_OBJ_TYPE_BLOB:
1656 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1657 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1658 break;
1659 case GOT_OBJ_TYPE_TREE:
1660 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1661 GOT_OBJ_LABEL_TREE, id_str_prefix);
1662 break;
1663 case GOT_OBJ_TYPE_COMMIT:
1664 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1665 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1666 break;
1667 case GOT_OBJ_TYPE_TAG:
1668 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1669 GOT_OBJ_LABEL_TAG, id_str_prefix);
1670 break;
1671 default:
1672 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1673 break;
1677 return err;
1680 const struct got_error *
1681 got_repo_match_object_id(struct got_object_id **id, char **label,
1682 const char *id_str, int obj_type, struct got_reflist_head *refs,
1683 struct got_repository *repo)
1685 const struct got_error *err;
1686 struct got_tag_object *tag;
1687 struct got_reference *ref = NULL;
1689 *id = NULL;
1690 if (label)
1691 *label = NULL;
1693 if (refs) {
1694 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1695 refs, repo);
1696 if (err == NULL) {
1697 *id = got_object_id_dup(
1698 got_object_tag_get_object_id(tag));
1699 if (*id == NULL)
1700 err = got_error_from_errno("got_object_id_dup");
1701 else if (label && asprintf(label, "refs/tags/%s",
1702 got_object_tag_get_name(tag)) == -1) {
1703 err = got_error_from_errno("asprintf");
1704 free(*id);
1705 *id = NULL;
1707 got_object_tag_close(tag);
1708 return err;
1709 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1710 err->code != GOT_ERR_NO_OBJ)
1711 return err;
1714 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1715 if (err) {
1716 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1717 return err;
1718 err = got_ref_open(&ref, repo, id_str, 0);
1719 if (err != NULL)
1720 goto done;
1721 if (label) {
1722 *label = strdup(got_ref_get_name(ref));
1723 if (*label == NULL) {
1724 err = got_error_from_errno("strdup");
1725 goto done;
1728 err = got_ref_resolve(id, repo, ref);
1729 } else if (label) {
1730 err = got_object_id_str(label, *id);
1731 if (*label == NULL) {
1732 err = got_error_from_errno("strdup");
1733 goto done;
1736 done:
1737 if (ref)
1738 got_ref_close(ref);
1739 return err;
1742 const struct got_error *
1743 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1744 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1746 const struct got_error *err = NULL;
1747 struct got_reflist_entry *re;
1748 struct got_object_id *tag_id;
1749 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1751 *tag = NULL;
1753 TAILQ_FOREACH(re, refs, entry) {
1754 const char *refname;
1755 refname = got_ref_get_name(re->ref);
1756 if (got_ref_is_symbolic(re->ref))
1757 continue;
1758 if (strncmp(refname, "refs/tags/", 10) != 0)
1759 continue;
1760 if (!name_is_absolute)
1761 refname += strlen("refs/tags/");
1762 if (strcmp(refname, name) != 0)
1763 continue;
1764 err = got_ref_resolve(&tag_id, repo, re->ref);
1765 if (err)
1766 break;
1767 err = got_object_open_as_tag(tag, repo, tag_id);
1768 free(tag_id);
1769 if (err)
1770 break;
1771 if (obj_type == GOT_OBJ_TYPE_ANY ||
1772 got_object_tag_get_object_type(*tag) == obj_type)
1773 break;
1774 got_object_tag_close(*tag);
1775 *tag = NULL;
1778 if (err == NULL && *tag == NULL)
1779 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1780 GOT_OBJ_LABEL_TAG, name);
1781 return err;
1784 static const struct got_error *
1785 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1786 const char *name, mode_t mode, struct got_object_id *blob_id)
1788 const struct got_error *err = NULL;
1790 *new_te = NULL;
1792 *new_te = calloc(1, sizeof(**new_te));
1793 if (*new_te == NULL)
1794 return got_error_from_errno("calloc");
1796 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1797 sizeof((*new_te)->name)) {
1798 err = got_error(GOT_ERR_NO_SPACE);
1799 goto done;
1802 if (S_ISLNK(mode)) {
1803 (*new_te)->mode = S_IFLNK;
1804 } else {
1805 (*new_te)->mode = S_IFREG;
1806 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1808 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1809 done:
1810 if (err && *new_te) {
1811 free(*new_te);
1812 *new_te = NULL;
1814 return err;
1817 static const struct got_error *
1818 import_file(struct got_tree_entry **new_te, struct dirent *de,
1819 const char *path, struct got_repository *repo)
1821 const struct got_error *err;
1822 struct got_object_id *blob_id = NULL;
1823 char *filepath;
1824 struct stat sb;
1826 if (asprintf(&filepath, "%s%s%s", path,
1827 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1828 return got_error_from_errno("asprintf");
1830 if (lstat(filepath, &sb) != 0) {
1831 err = got_error_from_errno2("lstat", path);
1832 goto done;
1835 err = got_object_blob_create(&blob_id, filepath, repo);
1836 if (err)
1837 goto done;
1839 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1840 blob_id);
1841 done:
1842 free(filepath);
1843 if (err)
1844 free(blob_id);
1845 return err;
1848 static const struct got_error *
1849 insert_tree_entry(struct got_tree_entry *new_te,
1850 struct got_pathlist_head *paths)
1852 const struct got_error *err = NULL;
1853 struct got_pathlist_entry *new_pe;
1855 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1856 if (err)
1857 return err;
1858 if (new_pe == NULL)
1859 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1860 return NULL;
1863 static const struct got_error *write_tree(struct got_object_id **,
1864 const char *, struct got_pathlist_head *, struct got_repository *,
1865 got_repo_import_cb progress_cb, void *progress_arg);
1867 static const struct got_error *
1868 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1869 const char *path, struct got_pathlist_head *ignores,
1870 struct got_repository *repo,
1871 got_repo_import_cb progress_cb, void *progress_arg)
1873 const struct got_error *err;
1874 struct got_object_id *id = NULL;
1875 char *subdirpath;
1877 if (asprintf(&subdirpath, "%s%s%s", path,
1878 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1879 return got_error_from_errno("asprintf");
1881 (*new_te) = calloc(1, sizeof(**new_te));
1882 if (*new_te == NULL)
1883 return got_error_from_errno("calloc");
1884 (*new_te)->mode = S_IFDIR;
1885 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1886 sizeof((*new_te)->name)) {
1887 err = got_error(GOT_ERR_NO_SPACE);
1888 goto done;
1890 err = write_tree(&id, subdirpath, ignores, repo,
1891 progress_cb, progress_arg);
1892 if (err)
1893 goto done;
1894 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1896 done:
1897 free(id);
1898 free(subdirpath);
1899 if (err) {
1900 free(*new_te);
1901 *new_te = NULL;
1903 return err;
1906 static const struct got_error *
1907 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1908 struct got_pathlist_head *ignores, struct got_repository *repo,
1909 got_repo_import_cb progress_cb, void *progress_arg)
1911 const struct got_error *err = NULL;
1912 DIR *dir;
1913 struct dirent *de;
1914 int nentries;
1915 struct got_tree_entry *new_te = NULL;
1916 struct got_pathlist_head paths;
1917 struct got_pathlist_entry *pe;
1919 *new_tree_id = NULL;
1921 TAILQ_INIT(&paths);
1923 dir = opendir(path_dir);
1924 if (dir == NULL) {
1925 err = got_error_from_errno2("opendir", path_dir);
1926 goto done;
1929 nentries = 0;
1930 while ((de = readdir(dir)) != NULL) {
1931 int ignore = 0;
1932 int type;
1934 if (strcmp(de->d_name, ".") == 0 ||
1935 strcmp(de->d_name, "..") == 0)
1936 continue;
1938 TAILQ_FOREACH(pe, ignores, entry) {
1939 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1940 ignore = 1;
1941 break;
1944 if (ignore)
1945 continue;
1947 err = got_path_dirent_type(&type, path_dir, de);
1948 if (err)
1949 goto done;
1951 if (type == DT_DIR) {
1952 err = import_subdir(&new_te, de, path_dir,
1953 ignores, repo, progress_cb, progress_arg);
1954 if (err) {
1955 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1956 goto done;
1957 err = NULL;
1958 continue;
1960 } else if (type == DT_REG || type == DT_LNK) {
1961 err = import_file(&new_te, de, path_dir, repo);
1962 if (err)
1963 goto done;
1964 } else
1965 continue;
1967 err = insert_tree_entry(new_te, &paths);
1968 if (err)
1969 goto done;
1970 nentries++;
1973 if (TAILQ_EMPTY(&paths)) {
1974 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1975 "cannot create tree without any entries");
1976 goto done;
1979 TAILQ_FOREACH(pe, &paths, entry) {
1980 struct got_tree_entry *te = pe->data;
1981 char *path;
1982 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1983 continue;
1984 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1985 err = got_error_from_errno("asprintf");
1986 goto done;
1988 err = (*progress_cb)(progress_arg, path);
1989 free(path);
1990 if (err)
1991 goto done;
1994 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1995 done:
1996 if (dir)
1997 closedir(dir);
1998 got_pathlist_free(&paths);
1999 return err;
2002 const struct got_error *
2003 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2004 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2005 struct got_repository *repo, got_repo_import_cb progress_cb,
2006 void *progress_arg)
2008 const struct got_error *err;
2009 struct got_object_id *new_tree_id;
2011 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2012 progress_cb, progress_arg);
2013 if (err)
2014 return err;
2016 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2017 author, time(NULL), author, time(NULL), logmsg, repo);
2018 free(new_tree_id);
2019 return err;
2022 const struct got_error *
2023 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2024 struct got_repository *repo)
2026 const struct got_error *err = NULL;
2027 char *path_objects = NULL, *path = NULL;
2028 DIR *dir = NULL;
2029 struct got_object_id id;
2030 int i;
2032 *nobjects = 0;
2033 *ondisk_size = 0;
2035 path_objects = got_repo_get_path_objects(repo);
2036 if (path_objects == NULL)
2037 return got_error_from_errno("got_repo_get_path_objects");
2039 for (i = 0; i <= 0xff; i++) {
2040 struct dirent *dent;
2042 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2043 err = got_error_from_errno("asprintf");
2044 break;
2047 dir = opendir(path);
2048 if (dir == NULL) {
2049 if (errno == ENOENT) {
2050 err = NULL;
2051 continue;
2053 err = got_error_from_errno2("opendir", path);
2054 break;
2057 while ((dent = readdir(dir)) != NULL) {
2058 char *id_str;
2059 int fd;
2060 struct stat sb;
2062 if (strcmp(dent->d_name, ".") == 0 ||
2063 strcmp(dent->d_name, "..") == 0)
2064 continue;
2066 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2067 err = got_error_from_errno("asprintf");
2068 goto done;
2071 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2072 free(id_str);
2073 continue;
2075 free(id_str);
2077 err = got_object_open_loose_fd(&fd, &id, repo);
2078 if (err)
2079 goto done;
2081 if (fstat(fd, &sb) == -1) {
2082 err = got_error_from_errno("fstat");
2083 close(fd);
2084 goto done;
2086 (*nobjects)++;
2087 (*ondisk_size) += sb.st_size;
2089 if (close(fd) == -1) {
2090 err = got_error_from_errno("close");
2091 goto done;
2095 if (closedir(dir) != 0) {
2096 err = got_error_from_errno("closedir");
2097 goto done;
2099 dir = NULL;
2101 free(path);
2102 path = NULL;
2104 done:
2105 if (dir && closedir(dir) != 0 && err == NULL)
2106 err = got_error_from_errno("closedir");
2108 if (err) {
2109 *nobjects = 0;
2110 *ondisk_size = 0;
2112 free(path_objects);
2113 free(path);
2114 return err;
2117 const struct got_error *
2118 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2119 off_t *total_packsize, struct got_repository *repo)
2121 const struct got_error *err = NULL;
2122 DIR *packdir = NULL;
2123 struct dirent *dent;
2124 struct got_packidx *packidx = NULL;
2125 char *path_packidx;
2126 char *path_packfile;
2127 int packdir_fd;
2128 struct stat sb;
2130 *npackfiles = 0;
2131 *nobjects = 0;
2132 *total_packsize = 0;
2134 packdir_fd = openat(got_repo_get_fd(repo),
2135 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2136 if (packdir_fd == -1) {
2137 return got_error_from_errno_fmt("openat: %s/%s",
2138 got_repo_get_path_git_dir(repo),
2139 GOT_OBJECTS_PACK_DIR);
2142 packdir = fdopendir(packdir_fd);
2143 if (packdir == NULL) {
2144 err = got_error_from_errno("fdopendir");
2145 goto done;
2148 while ((dent = readdir(packdir)) != NULL) {
2149 if (!got_repo_is_packidx_filename(dent->d_name,
2150 strlen(dent->d_name)))
2151 continue;
2153 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2154 dent->d_name) == -1) {
2155 err = got_error_from_errno("asprintf");
2156 goto done;
2159 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2160 path_packidx, 0);
2161 free(path_packidx);
2162 if (err)
2163 goto done;
2165 if (fstat(packidx->fd, &sb) == -1)
2166 goto done;
2167 *total_packsize += sb.st_size;
2169 err = got_packidx_get_packfile_path(&path_packfile,
2170 packidx->path_packidx);
2171 if (err)
2172 goto done;
2174 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2175 0) == -1) {
2176 free(path_packfile);
2177 goto done;
2179 free(path_packfile);
2180 *total_packsize += sb.st_size;
2182 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2184 (*npackfiles)++;
2186 got_packidx_close(packidx);
2187 packidx = NULL;
2189 done:
2190 if (packidx)
2191 got_packidx_close(packidx);
2192 if (packdir && closedir(packdir) != 0 && err == NULL)
2193 err = got_error_from_errno("closedir");
2194 if (err) {
2195 *npackfiles = 0;
2196 *nobjects = 0;
2197 *total_packsize = 0;
2199 return err;
2202 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2203 got_packidx_bloom_filter_cmp);