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, qid->id) == 0)
1492 continue; /* packed multiple times */
1493 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1494 goto done;
1498 done:
1499 got_object_id_queue_free(&matched_ids);
1500 if (err) {
1501 free(*unique_id);
1502 *unique_id = NULL;
1504 return err;
1507 static const struct got_error *
1508 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1509 const char *object_dir, const char *id_str_prefix, int obj_type,
1510 struct got_repository *repo)
1512 const struct got_error *err = NULL;
1513 char *path;
1514 DIR *dir = NULL;
1515 struct dirent *dent;
1516 struct got_object_id id;
1518 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1519 err = got_error_from_errno("asprintf");
1520 goto done;
1523 dir = opendir(path);
1524 if (dir == NULL) {
1525 if (errno == ENOENT) {
1526 err = NULL;
1527 goto done;
1529 err = got_error_from_errno2("opendir", path);
1530 goto done;
1532 while ((dent = readdir(dir)) != NULL) {
1533 char *id_str;
1534 int cmp;
1536 if (strcmp(dent->d_name, ".") == 0 ||
1537 strcmp(dent->d_name, "..") == 0)
1538 continue;
1540 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1541 err = got_error_from_errno("asprintf");
1542 goto done;
1545 if (!got_parse_sha1_digest(id.sha1, id_str))
1546 continue;
1549 * Directory entries do not necessarily appear in
1550 * sorted order, so we must iterate over all of them.
1552 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1553 if (cmp != 0) {
1554 free(id_str);
1555 continue;
1558 if (*unique_id == NULL) {
1559 if (obj_type != GOT_OBJ_TYPE_ANY) {
1560 int matched_type;
1561 err = got_object_get_type(&matched_type, repo,
1562 &id);
1563 if (err)
1564 goto done;
1565 if (matched_type != obj_type)
1566 continue;
1568 *unique_id = got_object_id_dup(&id);
1569 if (*unique_id == NULL) {
1570 err = got_error_from_errno("got_object_id_dup");
1571 free(id_str);
1572 goto done;
1574 } else {
1575 if (got_object_id_cmp(*unique_id, &id) == 0)
1576 continue; /* both packed and loose */
1577 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1578 free(id_str);
1579 goto done;
1582 done:
1583 if (dir && closedir(dir) != 0 && err == NULL)
1584 err = got_error_from_errno("closedir");
1585 if (err) {
1586 free(*unique_id);
1587 *unique_id = NULL;
1589 free(path);
1590 return err;
1593 const struct got_error *
1594 got_repo_match_object_id_prefix(struct got_object_id **id,
1595 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1597 const struct got_error *err = NULL;
1598 char *path_objects = got_repo_get_path_objects(repo);
1599 char *object_dir = NULL;
1600 size_t len;
1601 int i;
1603 *id = NULL;
1605 len = strlen(id_str_prefix);
1606 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1607 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1609 for (i = 0; i < len; i++) {
1610 if (isxdigit((unsigned char)id_str_prefix[i]))
1611 continue;
1612 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1615 if (len >= 2) {
1616 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1617 if (err)
1618 goto done;
1619 object_dir = strndup(id_str_prefix, 2);
1620 if (object_dir == NULL) {
1621 err = got_error_from_errno("strdup");
1622 goto done;
1624 err = match_loose_object(id, path_objects, object_dir,
1625 id_str_prefix, obj_type, repo);
1626 } else if (len == 1) {
1627 int i;
1628 for (i = 0; i < 0xf; i++) {
1629 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1630 == -1) {
1631 err = got_error_from_errno("asprintf");
1632 goto done;
1634 err = match_packed_object(id, repo, object_dir,
1635 obj_type);
1636 if (err)
1637 goto done;
1638 err = match_loose_object(id, path_objects, object_dir,
1639 id_str_prefix, obj_type, repo);
1640 if (err)
1641 goto done;
1643 } else {
1644 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1645 goto done;
1647 done:
1648 free(object_dir);
1649 if (err) {
1650 free(*id);
1651 *id = NULL;
1652 } else if (*id == NULL) {
1653 switch (obj_type) {
1654 case GOT_OBJ_TYPE_BLOB:
1655 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1656 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1657 break;
1658 case GOT_OBJ_TYPE_TREE:
1659 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1660 GOT_OBJ_LABEL_TREE, id_str_prefix);
1661 break;
1662 case GOT_OBJ_TYPE_COMMIT:
1663 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1664 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1665 break;
1666 case GOT_OBJ_TYPE_TAG:
1667 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1668 GOT_OBJ_LABEL_TAG, id_str_prefix);
1669 break;
1670 default:
1671 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1672 break;
1676 return err;
1679 const struct got_error *
1680 got_repo_match_object_id(struct got_object_id **id, char **label,
1681 const char *id_str, int obj_type, struct got_reflist_head *refs,
1682 struct got_repository *repo)
1684 const struct got_error *err;
1685 struct got_tag_object *tag;
1686 struct got_reference *ref = NULL;
1688 *id = NULL;
1689 if (label)
1690 *label = NULL;
1692 if (refs) {
1693 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1694 refs, repo);
1695 if (err == NULL) {
1696 *id = got_object_id_dup(
1697 got_object_tag_get_object_id(tag));
1698 if (*id == NULL)
1699 err = got_error_from_errno("got_object_id_dup");
1700 else if (label && asprintf(label, "refs/tags/%s",
1701 got_object_tag_get_name(tag)) == -1) {
1702 err = got_error_from_errno("asprintf");
1703 free(*id);
1704 *id = NULL;
1706 got_object_tag_close(tag);
1707 return err;
1708 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1709 err->code != GOT_ERR_NO_OBJ)
1710 return err;
1713 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1714 if (err) {
1715 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1716 return err;
1717 err = got_ref_open(&ref, repo, id_str, 0);
1718 if (err != NULL)
1719 goto done;
1720 if (label) {
1721 *label = strdup(got_ref_get_name(ref));
1722 if (*label == NULL) {
1723 err = got_error_from_errno("strdup");
1724 goto done;
1727 err = got_ref_resolve(id, repo, ref);
1728 } else if (label) {
1729 err = got_object_id_str(label, *id);
1730 if (*label == NULL) {
1731 err = got_error_from_errno("strdup");
1732 goto done;
1735 done:
1736 if (ref)
1737 got_ref_close(ref);
1738 return err;
1741 const struct got_error *
1742 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1743 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1745 const struct got_error *err = NULL;
1746 struct got_reflist_entry *re;
1747 struct got_object_id *tag_id;
1748 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1750 *tag = NULL;
1752 TAILQ_FOREACH(re, refs, entry) {
1753 const char *refname;
1754 refname = got_ref_get_name(re->ref);
1755 if (got_ref_is_symbolic(re->ref))
1756 continue;
1757 if (strncmp(refname, "refs/tags/", 10) != 0)
1758 continue;
1759 if (!name_is_absolute)
1760 refname += strlen("refs/tags/");
1761 if (strcmp(refname, name) != 0)
1762 continue;
1763 err = got_ref_resolve(&tag_id, repo, re->ref);
1764 if (err)
1765 break;
1766 err = got_object_open_as_tag(tag, repo, tag_id);
1767 free(tag_id);
1768 if (err)
1769 break;
1770 if (obj_type == GOT_OBJ_TYPE_ANY ||
1771 got_object_tag_get_object_type(*tag) == obj_type)
1772 break;
1773 got_object_tag_close(*tag);
1774 *tag = NULL;
1777 if (err == NULL && *tag == NULL)
1778 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1779 GOT_OBJ_LABEL_TAG, name);
1780 return err;
1783 static const struct got_error *
1784 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1785 const char *name, mode_t mode, struct got_object_id *blob_id)
1787 const struct got_error *err = NULL;
1789 *new_te = NULL;
1791 *new_te = calloc(1, sizeof(**new_te));
1792 if (*new_te == NULL)
1793 return got_error_from_errno("calloc");
1795 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1796 sizeof((*new_te)->name)) {
1797 err = got_error(GOT_ERR_NO_SPACE);
1798 goto done;
1801 if (S_ISLNK(mode)) {
1802 (*new_te)->mode = S_IFLNK;
1803 } else {
1804 (*new_te)->mode = S_IFREG;
1805 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1807 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1808 done:
1809 if (err && *new_te) {
1810 free(*new_te);
1811 *new_te = NULL;
1813 return err;
1816 static const struct got_error *
1817 import_file(struct got_tree_entry **new_te, struct dirent *de,
1818 const char *path, struct got_repository *repo)
1820 const struct got_error *err;
1821 struct got_object_id *blob_id = NULL;
1822 char *filepath;
1823 struct stat sb;
1825 if (asprintf(&filepath, "%s%s%s", path,
1826 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1827 return got_error_from_errno("asprintf");
1829 if (lstat(filepath, &sb) != 0) {
1830 err = got_error_from_errno2("lstat", path);
1831 goto done;
1834 err = got_object_blob_create(&blob_id, filepath, repo);
1835 if (err)
1836 goto done;
1838 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1839 blob_id);
1840 done:
1841 free(filepath);
1842 if (err)
1843 free(blob_id);
1844 return err;
1847 static const struct got_error *
1848 insert_tree_entry(struct got_tree_entry *new_te,
1849 struct got_pathlist_head *paths)
1851 const struct got_error *err = NULL;
1852 struct got_pathlist_entry *new_pe;
1854 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1855 if (err)
1856 return err;
1857 if (new_pe == NULL)
1858 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1859 return NULL;
1862 static const struct got_error *write_tree(struct got_object_id **,
1863 const char *, struct got_pathlist_head *, struct got_repository *,
1864 got_repo_import_cb progress_cb, void *progress_arg);
1866 static const struct got_error *
1867 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1868 const char *path, struct got_pathlist_head *ignores,
1869 struct got_repository *repo,
1870 got_repo_import_cb progress_cb, void *progress_arg)
1872 const struct got_error *err;
1873 struct got_object_id *id = NULL;
1874 char *subdirpath;
1876 if (asprintf(&subdirpath, "%s%s%s", path,
1877 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1878 return got_error_from_errno("asprintf");
1880 (*new_te) = calloc(1, sizeof(**new_te));
1881 if (*new_te == NULL)
1882 return got_error_from_errno("calloc");
1883 (*new_te)->mode = S_IFDIR;
1884 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1885 sizeof((*new_te)->name)) {
1886 err = got_error(GOT_ERR_NO_SPACE);
1887 goto done;
1889 err = write_tree(&id, subdirpath, ignores, repo,
1890 progress_cb, progress_arg);
1891 if (err)
1892 goto done;
1893 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1895 done:
1896 free(id);
1897 free(subdirpath);
1898 if (err) {
1899 free(*new_te);
1900 *new_te = NULL;
1902 return err;
1905 static const struct got_error *
1906 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1907 struct got_pathlist_head *ignores, struct got_repository *repo,
1908 got_repo_import_cb progress_cb, void *progress_arg)
1910 const struct got_error *err = NULL;
1911 DIR *dir;
1912 struct dirent *de;
1913 int nentries;
1914 struct got_tree_entry *new_te = NULL;
1915 struct got_pathlist_head paths;
1916 struct got_pathlist_entry *pe;
1918 *new_tree_id = NULL;
1920 TAILQ_INIT(&paths);
1922 dir = opendir(path_dir);
1923 if (dir == NULL) {
1924 err = got_error_from_errno2("opendir", path_dir);
1925 goto done;
1928 nentries = 0;
1929 while ((de = readdir(dir)) != NULL) {
1930 int ignore = 0;
1931 int type;
1933 if (strcmp(de->d_name, ".") == 0 ||
1934 strcmp(de->d_name, "..") == 0)
1935 continue;
1937 TAILQ_FOREACH(pe, ignores, entry) {
1938 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1939 ignore = 1;
1940 break;
1943 if (ignore)
1944 continue;
1946 err = got_path_dirent_type(&type, path_dir, de);
1947 if (err)
1948 goto done;
1950 if (type == DT_DIR) {
1951 err = import_subdir(&new_te, de, path_dir,
1952 ignores, repo, progress_cb, progress_arg);
1953 if (err) {
1954 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1955 goto done;
1956 err = NULL;
1957 continue;
1959 } else if (type == DT_REG || type == DT_LNK) {
1960 err = import_file(&new_te, de, path_dir, repo);
1961 if (err)
1962 goto done;
1963 } else
1964 continue;
1966 err = insert_tree_entry(new_te, &paths);
1967 if (err)
1968 goto done;
1969 nentries++;
1972 if (TAILQ_EMPTY(&paths)) {
1973 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1974 "cannot create tree without any entries");
1975 goto done;
1978 TAILQ_FOREACH(pe, &paths, entry) {
1979 struct got_tree_entry *te = pe->data;
1980 char *path;
1981 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1982 continue;
1983 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1984 err = got_error_from_errno("asprintf");
1985 goto done;
1987 err = (*progress_cb)(progress_arg, path);
1988 free(path);
1989 if (err)
1990 goto done;
1993 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1994 done:
1995 if (dir)
1996 closedir(dir);
1997 got_pathlist_free(&paths);
1998 return err;
2001 const struct got_error *
2002 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2003 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2004 struct got_repository *repo, got_repo_import_cb progress_cb,
2005 void *progress_arg)
2007 const struct got_error *err;
2008 struct got_object_id *new_tree_id;
2010 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2011 progress_cb, progress_arg);
2012 if (err)
2013 return err;
2015 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2016 author, time(NULL), author, time(NULL), logmsg, repo);
2017 free(new_tree_id);
2018 return err;
2021 const struct got_error *
2022 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2023 struct got_repository *repo)
2025 const struct got_error *err = NULL;
2026 char *path_objects = NULL, *path = NULL;
2027 DIR *dir = NULL;
2028 struct got_object_id id;
2029 int i;
2031 *nobjects = 0;
2032 *ondisk_size = 0;
2034 path_objects = got_repo_get_path_objects(repo);
2035 if (path_objects == NULL)
2036 return got_error_from_errno("got_repo_get_path_objects");
2038 for (i = 0; i <= 0xff; i++) {
2039 struct dirent *dent;
2041 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2042 err = got_error_from_errno("asprintf");
2043 break;
2046 dir = opendir(path);
2047 if (dir == NULL) {
2048 if (errno == ENOENT) {
2049 err = NULL;
2050 continue;
2052 err = got_error_from_errno2("opendir", path);
2053 break;
2056 while ((dent = readdir(dir)) != NULL) {
2057 char *id_str;
2058 int fd;
2059 struct stat sb;
2061 if (strcmp(dent->d_name, ".") == 0 ||
2062 strcmp(dent->d_name, "..") == 0)
2063 continue;
2065 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2066 err = got_error_from_errno("asprintf");
2067 goto done;
2070 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2071 free(id_str);
2072 continue;
2074 free(id_str);
2076 err = got_object_open_loose_fd(&fd, &id, repo);
2077 if (err)
2078 goto done;
2080 if (fstat(fd, &sb) == -1) {
2081 err = got_error_from_errno("fstat");
2082 close(fd);
2083 goto done;
2085 (*nobjects)++;
2086 (*ondisk_size) += sb.st_size;
2088 if (close(fd) == -1) {
2089 err = got_error_from_errno("close");
2090 goto done;
2094 if (closedir(dir) != 0) {
2095 err = got_error_from_errno("closedir");
2096 goto done;
2098 dir = NULL;
2100 free(path);
2101 path = NULL;
2103 done:
2104 if (dir && closedir(dir) != 0 && err == NULL)
2105 err = got_error_from_errno("closedir");
2107 if (err) {
2108 *nobjects = 0;
2109 *ondisk_size = 0;
2111 free(path_objects);
2112 free(path);
2113 return err;
2116 const struct got_error *
2117 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2118 off_t *total_packsize, struct got_repository *repo)
2120 const struct got_error *err = NULL;
2121 DIR *packdir = NULL;
2122 struct dirent *dent;
2123 struct got_packidx *packidx = NULL;
2124 char *path_packidx;
2125 char *path_packfile;
2126 int packdir_fd;
2127 struct stat sb;
2129 *npackfiles = 0;
2130 *nobjects = 0;
2131 *total_packsize = 0;
2133 packdir_fd = openat(got_repo_get_fd(repo),
2134 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2135 if (packdir_fd == -1) {
2136 return got_error_from_errno_fmt("openat: %s/%s",
2137 got_repo_get_path_git_dir(repo),
2138 GOT_OBJECTS_PACK_DIR);
2141 packdir = fdopendir(packdir_fd);
2142 if (packdir == NULL) {
2143 err = got_error_from_errno("fdopendir");
2144 goto done;
2147 while ((dent = readdir(packdir)) != NULL) {
2148 if (!got_repo_is_packidx_filename(dent->d_name,
2149 strlen(dent->d_name)))
2150 continue;
2152 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2153 dent->d_name) == -1) {
2154 err = got_error_from_errno("asprintf");
2155 goto done;
2158 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2159 path_packidx, 0);
2160 free(path_packidx);
2161 if (err)
2162 goto done;
2164 if (fstat(packidx->fd, &sb) == -1)
2165 goto done;
2166 *total_packsize += sb.st_size;
2168 err = got_packidx_get_packfile_path(&path_packfile,
2169 packidx->path_packidx);
2170 if (err)
2171 goto done;
2173 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2174 0) == -1) {
2175 free(path_packfile);
2176 goto done;
2178 free(path_packfile);
2179 *total_packsize += sb.st_size;
2181 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2183 (*npackfiles)++;
2185 got_packidx_close(packidx);
2186 packidx = NULL;
2188 done:
2189 if (packidx)
2190 got_packidx_close(packidx);
2191 if (packdir && closedir(packdir) != 0 && err == NULL)
2192 err = got_error_from_errno("closedir");
2193 if (err) {
2194 *npackfiles = 0;
2195 *nobjects = 0;
2196 *total_packsize = 0;
2198 return err;
2201 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2202 got_packidx_bloom_filter_cmp);