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"
47 #include "got_opentemp.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
63 #endif
65 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
67 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
68 got_packidx_bloom_filter_cmp);
70 const char *
71 got_repo_get_path(struct got_repository *repo)
72 {
73 return repo->path;
74 }
76 const char *
77 got_repo_get_path_git_dir(struct got_repository *repo)
78 {
79 return repo->path_git_dir;
80 }
82 int
83 got_repo_get_fd(struct got_repository *repo)
84 {
85 return repo->gitdir_fd;
86 }
88 const char *
89 got_repo_get_gitconfig_author_name(struct got_repository *repo)
90 {
91 return repo->gitconfig_author_name;
92 }
94 const char *
95 got_repo_get_gitconfig_author_email(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_email;
98 }
100 const char *
101 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
103 return repo->global_gitconfig_author_name;
106 const char *
107 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
109 return repo->global_gitconfig_author_email;
112 const char *
113 got_repo_get_gitconfig_owner(struct got_repository *repo)
115 return repo->gitconfig_owner;
118 void
119 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
120 struct got_repository *repo)
122 *extensions = repo->extensions;
123 *nextensions = repo->nextensions;
126 int
127 got_repo_is_bare(struct got_repository *repo)
129 return (strcmp(repo->path, repo->path_git_dir) == 0);
132 static char *
133 get_path_git_child(struct got_repository *repo, const char *basename)
135 char *path_child;
137 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
138 basename) == -1)
139 return NULL;
141 return path_child;
144 char *
145 got_repo_get_path_objects(struct got_repository *repo)
147 return get_path_git_child(repo, GOT_OBJECTS_DIR);
150 char *
151 got_repo_get_path_objects_pack(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
156 char *
157 got_repo_get_path_refs(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_REFS_DIR);
162 char *
163 got_repo_get_path_packed_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
168 static char *
169 get_path_head(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_HEAD_FILE);
174 char *
175 got_repo_get_path_gitconfig(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_GITCONFIG);
180 char *
181 got_repo_get_path_gotconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
186 const struct got_gotconfig *
187 got_repo_get_gotconfig(struct got_repository *repo)
189 return repo->gotconfig;
192 void
193 got_repo_get_gitconfig_remotes(int *nremotes,
194 const struct got_remote_repo **remotes, struct got_repository *repo)
196 *nremotes = repo->ngitconfig_remotes;
197 *remotes = repo->gitconfig_remotes;
200 static int
201 is_git_repo(struct got_repository *repo)
203 const char *path_git = got_repo_get_path_git_dir(repo);
204 char *path_objects = got_repo_get_path_objects(repo);
205 char *path_refs = got_repo_get_path_refs(repo);
206 char *path_head = get_path_head(repo);
207 int ret = 0;
208 struct stat sb;
209 struct got_reference *head_ref;
211 if (lstat(path_git, &sb) == -1)
212 goto done;
213 if (!S_ISDIR(sb.st_mode))
214 goto done;
216 if (lstat(path_objects, &sb) == -1)
217 goto done;
218 if (!S_ISDIR(sb.st_mode))
219 goto done;
221 if (lstat(path_refs, &sb) == -1)
222 goto done;
223 if (!S_ISDIR(sb.st_mode))
224 goto done;
226 if (lstat(path_head, &sb) == -1)
227 goto done;
228 if (!S_ISREG(sb.st_mode))
229 goto done;
231 /* Check if the HEAD reference can be opened. */
232 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
233 goto done;
234 got_ref_close(head_ref);
236 ret = 1;
237 done:
238 free(path_objects);
239 free(path_refs);
240 free(path_head);
241 return ret;
245 const struct got_error *
246 got_repo_pack_fds_open(int **pack_fds)
248 const struct got_error *err = NULL;
249 int i, pack_fds_tmp[GOT_PACK_NUM_TEMPFILES];
251 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
252 if (*pack_fds == NULL)
253 return got_error_from_errno("calloc");
255 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
256 pack_fds_tmp[i] = got_opentempfd();
257 if (pack_fds_tmp[i] == -1) {
258 err = got_error_from_errno("got_opentempfd");
259 got_repo_pack_fds_close(pack_fds_tmp);
260 return err;
263 memcpy(*pack_fds, pack_fds_tmp, sizeof(pack_fds_tmp));
264 return err;
267 const struct got_error *
268 got_repo_pack_fds_close(int *pack_fds)
270 const struct got_error *err = NULL;
271 int i;
273 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
274 if (pack_fds[i] == -1)
275 continue;
276 if (close(pack_fds[i]) == -1) {
277 err = got_error_from_errno("close");
278 break;
281 free(pack_fds);
282 return err;
285 const struct got_error *
286 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
287 struct got_object *obj)
289 #ifndef GOT_NO_OBJ_CACHE
290 const struct got_error *err = NULL;
291 err = got_object_cache_add(&repo->objcache, id, obj);
292 if (err) {
293 if (err->code == GOT_ERR_OBJ_EXISTS ||
294 err->code == GOT_ERR_OBJ_TOO_LARGE)
295 err = NULL;
296 return err;
298 obj->refcnt++;
299 #endif
300 return NULL;
303 struct got_object *
304 got_repo_get_cached_object(struct got_repository *repo,
305 struct got_object_id *id)
307 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
310 const struct got_error *
311 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
312 struct got_tree_object *tree)
314 #ifndef GOT_NO_OBJ_CACHE
315 const struct got_error *err = NULL;
316 err = got_object_cache_add(&repo->treecache, id, tree);
317 if (err) {
318 if (err->code == GOT_ERR_OBJ_EXISTS ||
319 err->code == GOT_ERR_OBJ_TOO_LARGE)
320 err = NULL;
321 return err;
323 tree->refcnt++;
324 #endif
325 return NULL;
328 struct got_tree_object *
329 got_repo_get_cached_tree(struct got_repository *repo,
330 struct got_object_id *id)
332 return (struct got_tree_object *)got_object_cache_get(
333 &repo->treecache, id);
336 const struct got_error *
337 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
338 struct got_commit_object *commit)
340 #ifndef GOT_NO_OBJ_CACHE
341 const struct got_error *err = NULL;
342 err = got_object_cache_add(&repo->commitcache, id, commit);
343 if (err) {
344 if (err->code == GOT_ERR_OBJ_EXISTS ||
345 err->code == GOT_ERR_OBJ_TOO_LARGE)
346 err = NULL;
347 return err;
349 commit->refcnt++;
350 #endif
351 return NULL;
354 struct got_commit_object *
355 got_repo_get_cached_commit(struct got_repository *repo,
356 struct got_object_id *id)
358 return (struct got_commit_object *)got_object_cache_get(
359 &repo->commitcache, id);
362 const struct got_error *
363 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
364 struct got_tag_object *tag)
366 #ifndef GOT_NO_OBJ_CACHE
367 const struct got_error *err = NULL;
368 err = got_object_cache_add(&repo->tagcache, id, tag);
369 if (err) {
370 if (err->code == GOT_ERR_OBJ_EXISTS ||
371 err->code == GOT_ERR_OBJ_TOO_LARGE)
372 err = NULL;
373 return err;
375 tag->refcnt++;
376 #endif
377 return NULL;
380 struct got_tag_object *
381 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
383 return (struct got_tag_object *)got_object_cache_get(
384 &repo->tagcache, id);
387 const struct got_error *
388 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
389 struct got_raw_object *raw)
391 #ifndef GOT_NO_OBJ_CACHE
392 const struct got_error *err = NULL;
393 err = got_object_cache_add(&repo->rawcache, id, raw);
394 if (err) {
395 if (err->code == GOT_ERR_OBJ_EXISTS ||
396 err->code == GOT_ERR_OBJ_TOO_LARGE)
397 err = NULL;
398 return err;
400 raw->refcnt++;
401 #endif
402 return NULL;
406 struct got_raw_object *
407 got_repo_get_cached_raw_object(struct got_repository *repo,
408 struct got_object_id *id)
410 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
414 static const struct got_error *
415 open_repo(struct got_repository *repo, const char *path)
417 const struct got_error *err = NULL;
419 repo->gitdir_fd = -1;
421 /* bare git repository? */
422 repo->path_git_dir = strdup(path);
423 if (repo->path_git_dir == NULL)
424 return got_error_from_errno("strdup");
425 if (is_git_repo(repo)) {
426 repo->path = strdup(repo->path_git_dir);
427 if (repo->path == NULL) {
428 err = got_error_from_errno("strdup");
429 goto done;
431 repo->gitdir_fd = open(repo->path_git_dir,
432 O_DIRECTORY | O_CLOEXEC);
433 if (repo->gitdir_fd == -1) {
434 err = got_error_from_errno2("open",
435 repo->path_git_dir);
436 goto done;
438 return NULL;
441 /* git repository with working tree? */
442 free(repo->path_git_dir);
443 repo->path_git_dir = NULL;
444 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
445 err = got_error_from_errno("asprintf");
446 goto done;
448 if (is_git_repo(repo)) {
449 repo->path = strdup(path);
450 if (repo->path == NULL) {
451 err = got_error_from_errno("strdup");
452 goto done;
454 repo->gitdir_fd = open(repo->path_git_dir,
455 O_DIRECTORY | O_CLOEXEC);
456 if (repo->gitdir_fd == -1) {
457 err = got_error_from_errno2("open",
458 repo->path_git_dir);
459 goto done;
461 return NULL;
464 err = got_error(GOT_ERR_NOT_GIT_REPO);
465 done:
466 if (err) {
467 free(repo->path);
468 repo->path = NULL;
469 free(repo->path_git_dir);
470 repo->path_git_dir = NULL;
471 if (repo->gitdir_fd != -1)
472 close(repo->gitdir_fd);
473 repo->gitdir_fd = -1;
476 return err;
479 static const struct got_error *
480 parse_gitconfig_file(int *gitconfig_repository_format_version,
481 char **gitconfig_author_name, char **gitconfig_author_email,
482 struct got_remote_repo **remotes, int *nremotes,
483 char **gitconfig_owner, char ***extensions, int *nextensions,
484 const char *gitconfig_path)
486 const struct got_error *err = NULL, *child_err = NULL;
487 int fd = -1;
488 int imsg_fds[2] = { -1, -1 };
489 pid_t pid;
490 struct imsgbuf *ibuf;
492 *gitconfig_repository_format_version = 0;
493 if (extensions)
494 *extensions = NULL;
495 if (nextensions)
496 *nextensions = 0;
497 *gitconfig_author_name = NULL;
498 *gitconfig_author_email = NULL;
499 if (remotes)
500 *remotes = NULL;
501 if (nremotes)
502 *nremotes = 0;
503 if (gitconfig_owner)
504 *gitconfig_owner = NULL;
506 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
507 if (fd == -1) {
508 if (errno == ENOENT)
509 return NULL;
510 return got_error_from_errno2("open", gitconfig_path);
513 ibuf = calloc(1, sizeof(*ibuf));
514 if (ibuf == NULL) {
515 err = got_error_from_errno("calloc");
516 goto done;
519 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
520 err = got_error_from_errno("socketpair");
521 goto done;
524 pid = fork();
525 if (pid == -1) {
526 err = got_error_from_errno("fork");
527 goto done;
528 } else if (pid == 0) {
529 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
530 gitconfig_path);
531 /* not reached */
534 if (close(imsg_fds[1]) == -1) {
535 err = got_error_from_errno("close");
536 goto done;
538 imsg_fds[1] = -1;
539 imsg_init(ibuf, imsg_fds[0]);
541 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
542 if (err)
543 goto done;
544 fd = -1;
546 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
547 if (err)
548 goto done;
550 err = got_privsep_recv_gitconfig_int(
551 gitconfig_repository_format_version, ibuf);
552 if (err)
553 goto done;
555 if (extensions && nextensions) {
556 err = got_privsep_send_gitconfig_repository_extensions_req(
557 ibuf);
558 if (err)
559 goto done;
560 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
561 if (err)
562 goto done;
563 if (*nextensions > 0) {
564 int i;
565 *extensions = calloc(*nextensions, sizeof(char *));
566 if (*extensions == NULL) {
567 err = got_error_from_errno("calloc");
568 goto done;
570 for (i = 0; i < *nextensions; i++) {
571 char *ext;
572 err = got_privsep_recv_gitconfig_str(&ext,
573 ibuf);
574 if (err)
575 goto done;
576 (*extensions)[i] = ext;
581 err = got_privsep_send_gitconfig_author_name_req(ibuf);
582 if (err)
583 goto done;
585 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
586 if (err)
587 goto done;
589 err = got_privsep_send_gitconfig_author_email_req(ibuf);
590 if (err)
591 goto done;
593 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
594 if (err)
595 goto done;
597 if (remotes && nremotes) {
598 err = got_privsep_send_gitconfig_remotes_req(ibuf);
599 if (err)
600 goto done;
602 err = got_privsep_recv_gitconfig_remotes(remotes,
603 nremotes, ibuf);
604 if (err)
605 goto done;
608 if (gitconfig_owner) {
609 err = got_privsep_send_gitconfig_owner_req(ibuf);
610 if (err)
611 goto done;
612 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
613 if (err)
614 goto done;
617 err = got_privsep_send_stop(imsg_fds[0]);
618 child_err = got_privsep_wait_for_child(pid);
619 if (child_err && err == NULL)
620 err = child_err;
621 done:
622 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
623 err = got_error_from_errno("close");
624 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
625 err = got_error_from_errno("close");
626 if (fd != -1 && close(fd) == -1 && err == NULL)
627 err = got_error_from_errno2("close", gitconfig_path);
628 free(ibuf);
629 return err;
632 static const struct got_error *
633 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
635 const struct got_error *err = NULL;
636 char *repo_gitconfig_path = NULL;
638 if (global_gitconfig_path) {
639 /* Read settings from ~/.gitconfig. */
640 int dummy_repo_version;
641 err = parse_gitconfig_file(&dummy_repo_version,
642 &repo->global_gitconfig_author_name,
643 &repo->global_gitconfig_author_email,
644 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
645 if (err)
646 return err;
649 /* Read repository's .git/config file. */
650 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
651 if (repo_gitconfig_path == NULL)
652 return got_error_from_errno("got_repo_get_path_gitconfig");
654 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
655 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
656 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
657 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
658 repo_gitconfig_path);
659 if (err)
660 goto done;
661 done:
662 free(repo_gitconfig_path);
663 return err;
666 static const struct got_error *
667 read_gotconfig(struct got_repository *repo)
669 const struct got_error *err = NULL;
670 char *gotconfig_path;
672 gotconfig_path = got_repo_get_path_gotconfig(repo);
673 if (gotconfig_path == NULL)
674 return got_error_from_errno("got_repo_get_path_gotconfig");
676 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
677 free(gotconfig_path);
678 return err;
681 /* Supported repository format extensions. */
682 static const char *const repo_extensions[] = {
683 "noop", /* Got supports repository format version 1. */
684 "preciousObjects", /* Supported by gotadmin cleanup. */
685 "worktreeConfig", /* Got does not care about Git work trees. */
686 };
688 const struct got_error *
689 got_repo_open(struct got_repository **repop, const char *path,
690 const char *global_gitconfig_path, int *pack_fds)
692 struct got_repository *repo = NULL;
693 const struct got_error *err = NULL;
694 char *repo_path = NULL;
695 size_t i, j = 0;
696 struct rlimit rl;
698 *repop = NULL;
700 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
701 return got_error_from_errno("getrlimit");
703 repo = calloc(1, sizeof(*repo));
704 if (repo == NULL)
705 return got_error_from_errno("calloc");
707 RB_INIT(&repo->packidx_bloom_filters);
708 TAILQ_INIT(&repo->packidx_paths);
710 for (i = 0; i < nitems(repo->privsep_children); i++) {
711 memset(&repo->privsep_children[i], 0,
712 sizeof(repo->privsep_children[0]));
713 repo->privsep_children[i].imsg_fd = -1;
716 err = got_object_cache_init(&repo->objcache,
717 GOT_OBJECT_CACHE_TYPE_OBJ);
718 if (err)
719 goto done;
720 err = got_object_cache_init(&repo->treecache,
721 GOT_OBJECT_CACHE_TYPE_TREE);
722 if (err)
723 goto done;
724 err = got_object_cache_init(&repo->commitcache,
725 GOT_OBJECT_CACHE_TYPE_COMMIT);
726 if (err)
727 goto done;
728 err = got_object_cache_init(&repo->tagcache,
729 GOT_OBJECT_CACHE_TYPE_TAG);
730 if (err)
731 goto done;
732 err = got_object_cache_init(&repo->rawcache,
733 GOT_OBJECT_CACHE_TYPE_RAW);
734 if (err)
735 goto done;
737 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
738 if (repo->pack_cache_size > rl.rlim_cur / 8)
739 repo->pack_cache_size = rl.rlim_cur / 8;
740 for (i = 0; i < nitems(repo->packs); i++) {
741 if (i < repo->pack_cache_size) {
742 repo->packs[i].basefd = pack_fds[j++];
743 repo->packs[i].accumfd = pack_fds[j++];
744 } else {
745 repo->packs[i].basefd = -1;
746 repo->packs[i].accumfd = -1;
749 repo->pinned_pack = -1;
750 repo->pinned_packidx = -1;
751 repo->pinned_pid = 0;
753 repo_path = realpath(path, NULL);
754 if (repo_path == NULL) {
755 err = got_error_from_errno2("realpath", path);
756 goto done;
759 for (;;) {
760 char *parent_path;
762 err = open_repo(repo, repo_path);
763 if (err == NULL)
764 break;
765 if (err->code != GOT_ERR_NOT_GIT_REPO)
766 goto done;
767 if (repo_path[0] == '/' && repo_path[1] == '\0') {
768 err = got_error(GOT_ERR_NOT_GIT_REPO);
769 goto done;
771 err = got_path_dirname(&parent_path, repo_path);
772 if (err)
773 goto done;
774 free(repo_path);
775 repo_path = parent_path;
778 err = read_gotconfig(repo);
779 if (err)
780 goto done;
782 err = read_gitconfig(repo, global_gitconfig_path);
783 if (err)
784 goto done;
785 if (repo->gitconfig_repository_format_version != 0)
786 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
787 for (i = 0; i < repo->nextensions; i++) {
788 char *ext = repo->extensions[i];
789 int j, supported = 0;
790 for (j = 0; j < nitems(repo_extensions); j++) {
791 if (strcmp(ext, repo_extensions[j]) == 0) {
792 supported = 1;
793 break;
796 if (!supported) {
797 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
798 goto done;
802 err = got_repo_list_packidx(&repo->packidx_paths, repo);
803 done:
804 if (err)
805 got_repo_close(repo);
806 else
807 *repop = repo;
808 free(repo_path);
809 return err;
812 const struct got_error *
813 got_repo_close(struct got_repository *repo)
815 const struct got_error *err = NULL, *child_err;
816 struct got_packidx_bloom_filter *bf;
817 struct got_pathlist_entry *pe;
818 size_t i;
820 for (i = 0; i < repo->pack_cache_size; i++) {
821 if (repo->packidx_cache[i] == NULL)
822 break;
823 got_packidx_close(repo->packidx_cache[i]);
826 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
827 &repo->packidx_bloom_filters))) {
828 RB_REMOVE(got_packidx_bloom_filter_tree,
829 &repo->packidx_bloom_filters, bf);
830 free(bf->bloom);
831 free(bf);
834 for (i = 0; i < repo->pack_cache_size; i++)
835 if (repo->packs[i].path_packfile)
836 if (repo->packs[i].path_packfile)
837 got_pack_close(&repo->packs[i]);
839 free(repo->path);
840 free(repo->path_git_dir);
842 got_object_cache_close(&repo->objcache);
843 got_object_cache_close(&repo->treecache);
844 got_object_cache_close(&repo->commitcache);
845 got_object_cache_close(&repo->tagcache);
846 got_object_cache_close(&repo->rawcache);
848 for (i = 0; i < nitems(repo->privsep_children); i++) {
849 if (repo->privsep_children[i].imsg_fd == -1)
850 continue;
851 imsg_clear(repo->privsep_children[i].ibuf);
852 free(repo->privsep_children[i].ibuf);
853 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
854 child_err = got_privsep_wait_for_child(
855 repo->privsep_children[i].pid);
856 if (child_err && err == NULL)
857 err = child_err;
858 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
859 err == NULL)
860 err = got_error_from_errno("close");
863 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
864 err == NULL)
865 err = got_error_from_errno("close");
867 if (repo->gotconfig)
868 got_gotconfig_free(repo->gotconfig);
869 free(repo->gitconfig_author_name);
870 free(repo->gitconfig_author_email);
871 for (i = 0; i < repo->ngitconfig_remotes; i++)
872 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
873 free(repo->gitconfig_remotes);
874 for (i = 0; i < repo->nextensions; i++)
875 free(repo->extensions[i]);
876 free(repo->extensions);
878 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
879 free((void *)pe->path);
880 got_pathlist_free(&repo->packidx_paths);
881 free(repo);
883 return err;
886 void
887 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
889 int i;
891 free(repo->name);
892 repo->name = NULL;
893 free(repo->fetch_url);
894 repo->fetch_url = NULL;
895 free(repo->send_url);
896 repo->send_url = NULL;
897 for (i = 0; i < repo->nfetch_branches; i++)
898 free(repo->fetch_branches[i]);
899 free(repo->fetch_branches);
900 repo->fetch_branches = NULL;
901 repo->nfetch_branches = 0;
902 for (i = 0; i < repo->nsend_branches; i++)
903 free(repo->send_branches[i]);
904 free(repo->send_branches);
905 repo->send_branches = NULL;
906 repo->nsend_branches = 0;
909 const struct got_error *
910 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
911 const char *input_path)
913 const struct got_error *err = NULL;
914 const char *repo_abspath = NULL;
915 size_t repolen, len;
916 char *canonpath, *path = NULL;
918 *in_repo_path = NULL;
920 canonpath = strdup(input_path);
921 if (canonpath == NULL) {
922 err = got_error_from_errno("strdup");
923 goto done;
925 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
926 if (err)
927 goto done;
929 repo_abspath = got_repo_get_path(repo);
931 if (canonpath[0] == '\0') {
932 path = strdup(canonpath);
933 if (path == NULL) {
934 err = got_error_from_errno("strdup");
935 goto done;
937 } else {
938 path = realpath(canonpath, NULL);
939 if (path == NULL) {
940 if (errno != ENOENT) {
941 err = got_error_from_errno2("realpath",
942 canonpath);
943 goto done;
945 /*
946 * Path is not on disk.
947 * Assume it is already relative to repository root.
948 */
949 path = strdup(canonpath);
950 if (path == NULL) {
951 err = got_error_from_errno("strdup");
952 goto done;
956 repolen = strlen(repo_abspath);
957 len = strlen(path);
960 if (strcmp(path, repo_abspath) == 0) {
961 free(path);
962 path = strdup("");
963 if (path == NULL) {
964 err = got_error_from_errno("strdup");
965 goto done;
967 } else if (len > repolen &&
968 got_path_is_child(path, repo_abspath, repolen)) {
969 /* Matched an on-disk path inside repository. */
970 if (got_repo_is_bare(repo)) {
971 /*
972 * Matched an on-disk path inside repository
973 * database. Treat input as repository-relative.
974 */
975 free(path);
976 path = canonpath;
977 canonpath = NULL;
978 } else {
979 char *child;
980 /* Strip common prefix with repository path. */
981 err = got_path_skip_common_ancestor(&child,
982 repo_abspath, path);
983 if (err)
984 goto done;
985 free(path);
986 path = child;
988 } else {
989 /*
990 * Matched unrelated on-disk path.
991 * Treat input as repository-relative.
992 */
993 free(path);
994 path = canonpath;
995 canonpath = NULL;
999 /* Make in-repository path absolute */
1000 if (path[0] != '/') {
1001 char *abspath;
1002 if (asprintf(&abspath, "/%s", path) == -1) {
1003 err = got_error_from_errno("asprintf");
1004 goto done;
1006 free(path);
1007 path = abspath;
1010 done:
1011 free(canonpath);
1012 if (err)
1013 free(path);
1014 else
1015 *in_repo_path = path;
1016 return err;
1019 static const struct got_error *
1020 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1021 const char *path_packidx)
1023 const struct got_error *err = NULL;
1024 size_t i;
1026 for (i = 0; i < repo->pack_cache_size; i++) {
1027 if (repo->packidx_cache[i] == NULL)
1028 break;
1029 if (strcmp(repo->packidx_cache[i]->path_packidx,
1030 path_packidx) == 0) {
1031 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1034 if (i == repo->pack_cache_size) {
1035 do {
1036 i--;
1037 } while (i > 0 && repo->pinned_packidx >= 0 &&
1038 i == repo->pinned_packidx);
1039 err = got_packidx_close(repo->packidx_cache[i]);
1040 if (err)
1041 return err;
1044 repo->packidx_cache[i] = packidx;
1046 return NULL;
1049 int
1050 got_repo_is_packidx_filename(const char *name, size_t len)
1052 if (len != GOT_PACKIDX_NAMELEN)
1053 return 0;
1055 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1056 return 0;
1058 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1059 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1060 return 0;
1062 return 1;
1065 static struct got_packidx_bloom_filter *
1066 get_packidx_bloom_filter(struct got_repository *repo,
1067 const char *path, size_t path_len)
1069 struct got_packidx_bloom_filter key;
1071 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1072 return NULL; /* XXX */
1073 key.path_len = path_len;
1075 return RB_FIND(got_packidx_bloom_filter_tree,
1076 &repo->packidx_bloom_filters, &key);
1079 int
1080 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1081 const char *path_packidx, struct got_object_id *id)
1083 struct got_packidx_bloom_filter *bf;
1085 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1086 if (bf)
1087 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1089 /* No bloom filter means this pack index must be searched. */
1090 return 1;
1093 static const struct got_error *
1094 add_packidx_bloom_filter(struct got_repository *repo,
1095 struct got_packidx *packidx, const char *path_packidx)
1097 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1098 struct got_packidx_bloom_filter *bf;
1099 size_t len;
1102 * Don't use bloom filters for very large pack index files.
1103 * Large pack files will contain a relatively large fraction
1104 * of our objects so we will likely need to visit them anyway.
1105 * The more objects a pack file contains the higher the probability
1106 * of a false-positive match from the bloom filter. And reading
1107 * all object IDs from a large pack index file can be expensive.
1109 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1110 return NULL;
1112 /* Do we already have a filter for this pack index? */
1113 if (get_packidx_bloom_filter(repo, path_packidx,
1114 strlen(path_packidx)) != NULL)
1115 return NULL;
1117 bf = calloc(1, sizeof(*bf));
1118 if (bf == NULL)
1119 return got_error_from_errno("calloc");
1120 bf->bloom = calloc(1, sizeof(*bf->bloom));
1121 if (bf->bloom == NULL) {
1122 free(bf);
1123 return got_error_from_errno("calloc");
1126 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1127 if (len >= sizeof(bf->path)) {
1128 free(bf->bloom);
1129 free(bf);
1130 return got_error(GOT_ERR_NO_SPACE);
1132 bf->path_len = len;
1134 /* Minimum size supported by our bloom filter is 1000 entries. */
1135 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1136 for (i = 0; i < nobjects; i++) {
1137 struct got_packidx_object_id *id;
1138 id = &packidx->hdr.sorted_ids[i];
1139 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1142 RB_INSERT(got_packidx_bloom_filter_tree,
1143 &repo->packidx_bloom_filters, bf);
1144 return NULL;
1147 const struct got_error *
1148 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1149 struct got_repository *repo, struct got_object_id *id)
1151 const struct got_error *err;
1152 struct got_pathlist_entry *pe;
1153 size_t i;
1155 /* Search pack index cache. */
1156 for (i = 0; i < repo->pack_cache_size; i++) {
1157 if (repo->packidx_cache[i] == NULL)
1158 break;
1159 if (!got_repo_check_packidx_bloom_filter(repo,
1160 repo->packidx_cache[i]->path_packidx, id))
1161 continue; /* object will not be found in this index */
1162 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1163 if (*idx != -1) {
1164 *packidx = repo->packidx_cache[i];
1166 * Move this cache entry to the front. Repeatedly
1167 * searching a wrong pack index can be expensive.
1169 if (i > 0) {
1170 memmove(&repo->packidx_cache[1],
1171 &repo->packidx_cache[0],
1172 i * sizeof(repo->packidx_cache[0]));
1173 repo->packidx_cache[0] = *packidx;
1174 if (repo->pinned_packidx >= 0 &&
1175 repo->pinned_packidx < i)
1176 repo->pinned_packidx++;
1177 else if (repo->pinned_packidx == i)
1178 repo->pinned_packidx = 0;
1180 return NULL;
1183 /* No luck. Search the filesystem. */
1185 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1186 const char *path_packidx = pe->path;
1187 int is_cached = 0;
1189 if (!got_repo_check_packidx_bloom_filter(repo,
1190 pe->path, id))
1191 continue; /* object will not be found in this index */
1193 for (i = 0; i < repo->pack_cache_size; i++) {
1194 if (repo->packidx_cache[i] == NULL)
1195 break;
1196 if (strcmp(repo->packidx_cache[i]->path_packidx,
1197 path_packidx) == 0) {
1198 is_cached = 1;
1199 break;
1202 if (is_cached)
1203 continue; /* already searched */
1205 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1206 path_packidx, 0);
1207 if (err)
1208 goto done;
1210 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1211 if (err)
1212 goto done;
1214 err = cache_packidx(repo, *packidx, path_packidx);
1215 if (err)
1216 goto done;
1218 *idx = got_packidx_get_object_idx(*packidx, id);
1219 if (*idx != -1) {
1220 err = NULL; /* found the object */
1221 goto done;
1225 err = got_error_no_obj(id);
1226 done:
1227 return err;
1230 const struct got_error *
1231 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1232 struct got_repository *repo)
1234 const struct got_error *err = NULL;
1235 DIR *packdir = NULL;
1236 struct dirent *dent;
1237 char *path_packidx = NULL;
1238 int packdir_fd;
1240 packdir_fd = openat(got_repo_get_fd(repo),
1241 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1242 if (packdir_fd == -1) {
1243 return got_error_from_errno_fmt("openat: %s/%s",
1244 got_repo_get_path_git_dir(repo),
1245 GOT_OBJECTS_PACK_DIR);
1248 packdir = fdopendir(packdir_fd);
1249 if (packdir == NULL) {
1250 err = got_error_from_errno("fdopendir");
1251 goto done;
1254 while ((dent = readdir(packdir)) != NULL) {
1255 if (!got_repo_is_packidx_filename(dent->d_name,
1256 strlen(dent->d_name)))
1257 continue;
1259 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1260 dent->d_name) == -1) {
1261 err = got_error_from_errno("asprintf");
1262 path_packidx = NULL;
1263 break;
1266 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1267 if (err)
1268 break;
1270 done:
1271 if (err)
1272 free(path_packidx);
1273 if (packdir && closedir(packdir) != 0 && err == NULL)
1274 err = got_error_from_errno("closedir");
1275 return err;
1278 const struct got_error *
1279 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1280 struct got_repository *repo)
1282 const struct got_error *err;
1283 size_t i;
1285 *packidx = NULL;
1287 /* Search pack index cache. */
1288 for (i = 0; i < repo->pack_cache_size; i++) {
1289 if (repo->packidx_cache[i] == NULL)
1290 break;
1291 if (strcmp(repo->packidx_cache[i]->path_packidx,
1292 path_packidx) == 0) {
1293 *packidx = repo->packidx_cache[i];
1294 return NULL;
1297 /* No luck. Search the filesystem. */
1299 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1300 path_packidx, 0);
1301 if (err)
1302 return err;
1304 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1305 if (err)
1306 goto done;
1308 err = cache_packidx(repo, *packidx, path_packidx);
1309 done:
1310 if (err) {
1311 got_packidx_close(*packidx);
1312 *packidx = NULL;
1314 return err;
1317 static const struct got_error *
1318 read_packfile_hdr(int fd, struct got_packidx *packidx)
1320 const struct got_error *err = NULL;
1321 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1322 struct got_packfile_hdr hdr;
1323 ssize_t n;
1325 n = read(fd, &hdr, sizeof(hdr));
1326 if (n < 0)
1327 return got_error_from_errno("read");
1328 if (n != sizeof(hdr))
1329 return got_error(GOT_ERR_BAD_PACKFILE);
1331 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1332 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1333 be32toh(hdr.nobjects) != totobj)
1334 err = got_error(GOT_ERR_BAD_PACKFILE);
1336 return err;
1339 static const struct got_error *
1340 open_packfile(int *fd, struct got_repository *repo,
1341 const char *relpath, struct got_packidx *packidx)
1343 const struct got_error *err = NULL;
1345 *fd = openat(got_repo_get_fd(repo), relpath,
1346 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1347 if (*fd == -1)
1348 return got_error_from_errno_fmt("openat: %s/%s",
1349 got_repo_get_path_git_dir(repo), relpath);
1351 if (packidx) {
1352 err = read_packfile_hdr(*fd, packidx);
1353 if (err) {
1354 close(*fd);
1355 *fd = -1;
1359 return err;
1362 const struct got_error *
1363 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1364 const char *path_packfile, struct got_packidx *packidx)
1366 const struct got_error *err = NULL;
1367 struct got_pack *pack = NULL;
1368 struct stat sb;
1369 size_t i;
1371 if (packp)
1372 *packp = NULL;
1374 for (i = 0; i < repo->pack_cache_size; i++) {
1375 pack = &repo->packs[i];
1376 if (pack->path_packfile == NULL)
1377 break;
1378 if (strcmp(pack->path_packfile, path_packfile) == 0)
1379 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1382 if (i == repo->pack_cache_size) {
1383 struct got_pack tmp;
1384 do {
1385 i--;
1386 } while (i > 0 && repo->pinned_pack >= 0 &&
1387 i == repo->pinned_pack);
1388 err = got_pack_close(&repo->packs[i]);
1389 if (err)
1390 return err;
1391 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1392 return got_error_from_errno("ftruncate");
1393 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1394 return got_error_from_errno("ftruncate");
1395 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1396 memcpy(&repo->packs[i], &repo->packs[0],
1397 sizeof(repo->packs[i]));
1398 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1399 if (repo->pinned_pack == 0)
1400 repo->pinned_pack = i;
1401 else if (repo->pinned_pack == i)
1402 repo->pinned_pack = 0;
1403 i = 0;
1406 pack = &repo->packs[i];
1408 pack->path_packfile = strdup(path_packfile);
1409 if (pack->path_packfile == NULL) {
1410 err = got_error_from_errno("strdup");
1411 goto done;
1414 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1415 if (err)
1416 goto done;
1418 if (fstat(pack->fd, &sb) != 0) {
1419 err = got_error_from_errno("fstat");
1420 goto done;
1422 pack->filesize = sb.st_size;
1424 pack->privsep_child = NULL;
1426 #ifndef GOT_PACK_NO_MMAP
1427 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1428 pack->fd, 0);
1429 if (pack->map == MAP_FAILED) {
1430 if (errno != ENOMEM) {
1431 err = got_error_from_errno("mmap");
1432 goto done;
1434 pack->map = NULL; /* fall back to read(2) */
1436 #endif
1437 done:
1438 if (err) {
1439 if (pack) {
1440 free(pack->path_packfile);
1441 memset(pack, 0, sizeof(*pack));
1443 } else if (packp)
1444 *packp = pack;
1445 return err;
1448 struct got_pack *
1449 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1451 struct got_pack *pack = NULL;
1452 size_t i;
1454 for (i = 0; i < repo->pack_cache_size; i++) {
1455 pack = &repo->packs[i];
1456 if (pack->path_packfile == NULL)
1457 break;
1458 if (strcmp(pack->path_packfile, path_packfile) == 0)
1459 return pack;
1462 return NULL;
1465 const struct got_error *
1466 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1467 struct got_pack *pack)
1469 size_t i;
1470 int pinned_pack = -1, pinned_packidx = -1;
1472 for (i = 0; i < repo->pack_cache_size; i++) {
1473 if (repo->packidx_cache[i] &&
1474 strcmp(repo->packidx_cache[i]->path_packidx,
1475 packidx->path_packidx) == 0)
1476 pinned_packidx = i;
1477 if (repo->packs[i].path_packfile &&
1478 strcmp(repo->packs[i].path_packfile,
1479 pack->path_packfile) == 0)
1480 pinned_pack = i;
1483 if (pinned_packidx == -1 || pinned_pack == -1)
1484 return got_error(GOT_ERR_PIN_PACK);
1486 repo->pinned_pack = pinned_pack;
1487 repo->pinned_packidx = pinned_packidx;
1488 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1489 return NULL;
1492 struct got_pack *
1493 got_repo_get_pinned_pack(struct got_repository *repo)
1495 if (repo->pinned_pack >= 0 &&
1496 repo->pinned_pack < repo->pack_cache_size)
1497 return &repo->packs[repo->pinned_pack];
1499 return NULL;
1502 void
1503 got_repo_unpin_pack(struct got_repository *repo)
1505 repo->pinned_packidx = -1;
1506 repo->pinned_pack = -1;
1507 repo->pinned_pid = 0;
1510 const struct got_error *
1511 got_repo_init(const char *repo_path)
1513 const struct got_error *err = NULL;
1514 const char *dirnames[] = {
1515 GOT_OBJECTS_DIR,
1516 GOT_OBJECTS_PACK_DIR,
1517 GOT_REFS_DIR,
1519 const char *description_str = "Unnamed repository; "
1520 "edit this file 'description' to name the repository.";
1521 const char *headref_str = "ref: refs/heads/main";
1522 const char *gitconfig_str = "[core]\n"
1523 "\trepositoryformatversion = 0\n"
1524 "\tfilemode = true\n"
1525 "\tbare = true\n";
1526 char *path;
1527 size_t i;
1529 if (!got_path_dir_is_empty(repo_path))
1530 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1532 for (i = 0; i < nitems(dirnames); i++) {
1533 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1534 return got_error_from_errno("asprintf");
1536 err = got_path_mkdir(path);
1537 free(path);
1538 if (err)
1539 return err;
1542 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1543 return got_error_from_errno("asprintf");
1544 err = got_path_create_file(path, description_str);
1545 free(path);
1546 if (err)
1547 return err;
1549 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1550 return got_error_from_errno("asprintf");
1551 err = got_path_create_file(path, headref_str);
1552 free(path);
1553 if (err)
1554 return err;
1556 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1557 return got_error_from_errno("asprintf");
1558 err = got_path_create_file(path, gitconfig_str);
1559 free(path);
1560 if (err)
1561 return err;
1563 return NULL;
1566 static const struct got_error *
1567 match_packed_object(struct got_object_id **unique_id,
1568 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1570 const struct got_error *err = NULL;
1571 struct got_object_id_queue matched_ids;
1572 struct got_pathlist_entry *pe;
1574 STAILQ_INIT(&matched_ids);
1576 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1577 const char *path_packidx = pe->path;
1578 struct got_packidx *packidx;
1579 struct got_object_qid *qid;
1581 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1582 path_packidx, 0);
1583 if (err)
1584 break;
1586 err = got_packidx_match_id_str_prefix(&matched_ids,
1587 packidx, id_str_prefix);
1588 if (err) {
1589 got_packidx_close(packidx);
1590 break;
1592 err = got_packidx_close(packidx);
1593 if (err)
1594 break;
1596 STAILQ_FOREACH(qid, &matched_ids, entry) {
1597 if (obj_type != GOT_OBJ_TYPE_ANY) {
1598 int matched_type;
1599 err = got_object_get_type(&matched_type, repo,
1600 &qid->id);
1601 if (err)
1602 goto done;
1603 if (matched_type != obj_type)
1604 continue;
1606 if (*unique_id == NULL) {
1607 *unique_id = got_object_id_dup(&qid->id);
1608 if (*unique_id == NULL) {
1609 err = got_error_from_errno("malloc");
1610 goto done;
1612 } else {
1613 if (got_object_id_cmp(*unique_id,
1614 &qid->id) == 0)
1615 continue; /* packed multiple times */
1616 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1617 goto done;
1621 done:
1622 got_object_id_queue_free(&matched_ids);
1623 if (err) {
1624 free(*unique_id);
1625 *unique_id = NULL;
1627 return err;
1630 static const struct got_error *
1631 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1632 const char *object_dir, const char *id_str_prefix, int obj_type,
1633 struct got_repository *repo)
1635 const struct got_error *err = NULL;
1636 char *path;
1637 DIR *dir = NULL;
1638 struct dirent *dent;
1639 struct got_object_id id;
1641 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1642 err = got_error_from_errno("asprintf");
1643 goto done;
1646 dir = opendir(path);
1647 if (dir == NULL) {
1648 if (errno == ENOENT) {
1649 err = NULL;
1650 goto done;
1652 err = got_error_from_errno2("opendir", path);
1653 goto done;
1655 while ((dent = readdir(dir)) != NULL) {
1656 char *id_str;
1657 int cmp;
1659 if (strcmp(dent->d_name, ".") == 0 ||
1660 strcmp(dent->d_name, "..") == 0)
1661 continue;
1663 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1664 err = got_error_from_errno("asprintf");
1665 goto done;
1668 if (!got_parse_sha1_digest(id.sha1, id_str))
1669 continue;
1672 * Directory entries do not necessarily appear in
1673 * sorted order, so we must iterate over all of them.
1675 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1676 if (cmp != 0) {
1677 free(id_str);
1678 continue;
1681 if (*unique_id == NULL) {
1682 if (obj_type != GOT_OBJ_TYPE_ANY) {
1683 int matched_type;
1684 err = got_object_get_type(&matched_type, repo,
1685 &id);
1686 if (err)
1687 goto done;
1688 if (matched_type != obj_type)
1689 continue;
1691 *unique_id = got_object_id_dup(&id);
1692 if (*unique_id == NULL) {
1693 err = got_error_from_errno("got_object_id_dup");
1694 free(id_str);
1695 goto done;
1697 } else {
1698 if (got_object_id_cmp(*unique_id, &id) == 0)
1699 continue; /* both packed and loose */
1700 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1701 free(id_str);
1702 goto done;
1705 done:
1706 if (dir && closedir(dir) != 0 && err == NULL)
1707 err = got_error_from_errno("closedir");
1708 if (err) {
1709 free(*unique_id);
1710 *unique_id = NULL;
1712 free(path);
1713 return err;
1716 const struct got_error *
1717 got_repo_match_object_id_prefix(struct got_object_id **id,
1718 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1720 const struct got_error *err = NULL;
1721 char *path_objects = got_repo_get_path_objects(repo);
1722 char *object_dir = NULL;
1723 size_t len;
1724 int i;
1726 *id = NULL;
1728 len = strlen(id_str_prefix);
1729 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1730 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1732 for (i = 0; i < len; i++) {
1733 if (isxdigit((unsigned char)id_str_prefix[i]))
1734 continue;
1735 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1738 if (len >= 2) {
1739 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1740 if (err)
1741 goto done;
1742 object_dir = strndup(id_str_prefix, 2);
1743 if (object_dir == NULL) {
1744 err = got_error_from_errno("strdup");
1745 goto done;
1747 err = match_loose_object(id, path_objects, object_dir,
1748 id_str_prefix, obj_type, repo);
1749 } else if (len == 1) {
1750 int i;
1751 for (i = 0; i < 0xf; i++) {
1752 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1753 == -1) {
1754 err = got_error_from_errno("asprintf");
1755 goto done;
1757 err = match_packed_object(id, repo, object_dir,
1758 obj_type);
1759 if (err)
1760 goto done;
1761 err = match_loose_object(id, path_objects, object_dir,
1762 id_str_prefix, obj_type, repo);
1763 if (err)
1764 goto done;
1766 } else {
1767 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1768 goto done;
1770 done:
1771 free(object_dir);
1772 if (err) {
1773 free(*id);
1774 *id = NULL;
1775 } else if (*id == NULL) {
1776 switch (obj_type) {
1777 case GOT_OBJ_TYPE_BLOB:
1778 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1779 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1780 break;
1781 case GOT_OBJ_TYPE_TREE:
1782 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1783 GOT_OBJ_LABEL_TREE, id_str_prefix);
1784 break;
1785 case GOT_OBJ_TYPE_COMMIT:
1786 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1787 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1788 break;
1789 case GOT_OBJ_TYPE_TAG:
1790 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1791 GOT_OBJ_LABEL_TAG, id_str_prefix);
1792 break;
1793 default:
1794 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1795 break;
1799 return err;
1802 const struct got_error *
1803 got_repo_match_object_id(struct got_object_id **id, char **label,
1804 const char *id_str, int obj_type, struct got_reflist_head *refs,
1805 struct got_repository *repo)
1807 const struct got_error *err;
1808 struct got_tag_object *tag;
1809 struct got_reference *ref = NULL;
1811 *id = NULL;
1812 if (label)
1813 *label = NULL;
1815 if (refs) {
1816 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1817 refs, repo);
1818 if (err == NULL) {
1819 *id = got_object_id_dup(
1820 got_object_tag_get_object_id(tag));
1821 if (*id == NULL)
1822 err = got_error_from_errno("got_object_id_dup");
1823 else if (label && asprintf(label, "refs/tags/%s",
1824 got_object_tag_get_name(tag)) == -1) {
1825 err = got_error_from_errno("asprintf");
1826 free(*id);
1827 *id = NULL;
1829 got_object_tag_close(tag);
1830 return err;
1831 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1832 err->code != GOT_ERR_NO_OBJ)
1833 return err;
1836 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1837 if (err) {
1838 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1839 return err;
1840 err = got_ref_open(&ref, repo, id_str, 0);
1841 if (err != NULL)
1842 goto done;
1843 if (label) {
1844 *label = strdup(got_ref_get_name(ref));
1845 if (*label == NULL) {
1846 err = got_error_from_errno("strdup");
1847 goto done;
1850 err = got_ref_resolve(id, repo, ref);
1851 } else if (label) {
1852 err = got_object_id_str(label, *id);
1853 if (*label == NULL) {
1854 err = got_error_from_errno("strdup");
1855 goto done;
1858 done:
1859 if (ref)
1860 got_ref_close(ref);
1861 return err;
1864 const struct got_error *
1865 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1866 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1868 const struct got_error *err = NULL;
1869 struct got_reflist_entry *re;
1870 struct got_object_id *tag_id;
1871 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1873 *tag = NULL;
1875 TAILQ_FOREACH(re, refs, entry) {
1876 const char *refname;
1877 refname = got_ref_get_name(re->ref);
1878 if (got_ref_is_symbolic(re->ref))
1879 continue;
1880 if (strncmp(refname, "refs/tags/", 10) != 0)
1881 continue;
1882 if (!name_is_absolute)
1883 refname += strlen("refs/tags/");
1884 if (strcmp(refname, name) != 0)
1885 continue;
1886 err = got_ref_resolve(&tag_id, repo, re->ref);
1887 if (err)
1888 break;
1889 err = got_object_open_as_tag(tag, repo, tag_id);
1890 free(tag_id);
1891 if (err)
1892 break;
1893 if (obj_type == GOT_OBJ_TYPE_ANY ||
1894 got_object_tag_get_object_type(*tag) == obj_type)
1895 break;
1896 got_object_tag_close(*tag);
1897 *tag = NULL;
1900 if (err == NULL && *tag == NULL)
1901 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1902 GOT_OBJ_LABEL_TAG, name);
1903 return err;
1906 static const struct got_error *
1907 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1908 const char *name, mode_t mode, struct got_object_id *blob_id)
1910 const struct got_error *err = NULL;
1912 *new_te = NULL;
1914 *new_te = calloc(1, sizeof(**new_te));
1915 if (*new_te == NULL)
1916 return got_error_from_errno("calloc");
1918 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1919 sizeof((*new_te)->name)) {
1920 err = got_error(GOT_ERR_NO_SPACE);
1921 goto done;
1924 if (S_ISLNK(mode)) {
1925 (*new_te)->mode = S_IFLNK;
1926 } else {
1927 (*new_te)->mode = S_IFREG;
1928 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1930 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1931 done:
1932 if (err && *new_te) {
1933 free(*new_te);
1934 *new_te = NULL;
1936 return err;
1939 static const struct got_error *
1940 import_file(struct got_tree_entry **new_te, struct dirent *de,
1941 const char *path, struct got_repository *repo)
1943 const struct got_error *err;
1944 struct got_object_id *blob_id = NULL;
1945 char *filepath;
1946 struct stat sb;
1948 if (asprintf(&filepath, "%s%s%s", path,
1949 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1950 return got_error_from_errno("asprintf");
1952 if (lstat(filepath, &sb) != 0) {
1953 err = got_error_from_errno2("lstat", path);
1954 goto done;
1957 err = got_object_blob_create(&blob_id, filepath, repo);
1958 if (err)
1959 goto done;
1961 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1962 blob_id);
1963 done:
1964 free(filepath);
1965 if (err)
1966 free(blob_id);
1967 return err;
1970 static const struct got_error *
1971 insert_tree_entry(struct got_tree_entry *new_te,
1972 struct got_pathlist_head *paths)
1974 const struct got_error *err = NULL;
1975 struct got_pathlist_entry *new_pe;
1977 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1978 if (err)
1979 return err;
1980 if (new_pe == NULL)
1981 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1982 return NULL;
1985 static const struct got_error *write_tree(struct got_object_id **,
1986 const char *, struct got_pathlist_head *, struct got_repository *,
1987 got_repo_import_cb progress_cb, void *progress_arg);
1989 static const struct got_error *
1990 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1991 const char *path, struct got_pathlist_head *ignores,
1992 struct got_repository *repo,
1993 got_repo_import_cb progress_cb, void *progress_arg)
1995 const struct got_error *err;
1996 struct got_object_id *id = NULL;
1997 char *subdirpath;
1999 if (asprintf(&subdirpath, "%s%s%s", path,
2000 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2001 return got_error_from_errno("asprintf");
2003 (*new_te) = calloc(1, sizeof(**new_te));
2004 if (*new_te == NULL)
2005 return got_error_from_errno("calloc");
2006 (*new_te)->mode = S_IFDIR;
2007 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2008 sizeof((*new_te)->name)) {
2009 err = got_error(GOT_ERR_NO_SPACE);
2010 goto done;
2012 err = write_tree(&id, subdirpath, ignores, repo,
2013 progress_cb, progress_arg);
2014 if (err)
2015 goto done;
2016 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2018 done:
2019 free(id);
2020 free(subdirpath);
2021 if (err) {
2022 free(*new_te);
2023 *new_te = NULL;
2025 return err;
2028 static const struct got_error *
2029 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2030 struct got_pathlist_head *ignores, struct got_repository *repo,
2031 got_repo_import_cb progress_cb, void *progress_arg)
2033 const struct got_error *err = NULL;
2034 DIR *dir;
2035 struct dirent *de;
2036 int nentries;
2037 struct got_tree_entry *new_te = NULL;
2038 struct got_pathlist_head paths;
2039 struct got_pathlist_entry *pe;
2041 *new_tree_id = NULL;
2043 TAILQ_INIT(&paths);
2045 dir = opendir(path_dir);
2046 if (dir == NULL) {
2047 err = got_error_from_errno2("opendir", path_dir);
2048 goto done;
2051 nentries = 0;
2052 while ((de = readdir(dir)) != NULL) {
2053 int ignore = 0;
2054 int type;
2056 if (strcmp(de->d_name, ".") == 0 ||
2057 strcmp(de->d_name, "..") == 0)
2058 continue;
2060 TAILQ_FOREACH(pe, ignores, entry) {
2061 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2062 ignore = 1;
2063 break;
2066 if (ignore)
2067 continue;
2069 err = got_path_dirent_type(&type, path_dir, de);
2070 if (err)
2071 goto done;
2073 if (type == DT_DIR) {
2074 err = import_subdir(&new_te, de, path_dir,
2075 ignores, repo, progress_cb, progress_arg);
2076 if (err) {
2077 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2078 goto done;
2079 err = NULL;
2080 continue;
2082 } else if (type == DT_REG || type == DT_LNK) {
2083 err = import_file(&new_te, de, path_dir, repo);
2084 if (err)
2085 goto done;
2086 } else
2087 continue;
2089 err = insert_tree_entry(new_te, &paths);
2090 if (err)
2091 goto done;
2092 nentries++;
2095 if (TAILQ_EMPTY(&paths)) {
2096 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2097 "cannot create tree without any entries");
2098 goto done;
2101 TAILQ_FOREACH(pe, &paths, entry) {
2102 struct got_tree_entry *te = pe->data;
2103 char *path;
2104 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2105 continue;
2106 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2107 err = got_error_from_errno("asprintf");
2108 goto done;
2110 err = (*progress_cb)(progress_arg, path);
2111 free(path);
2112 if (err)
2113 goto done;
2116 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2117 done:
2118 if (dir)
2119 closedir(dir);
2120 got_pathlist_free(&paths);
2121 return err;
2124 const struct got_error *
2125 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2126 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2127 struct got_repository *repo, got_repo_import_cb progress_cb,
2128 void *progress_arg)
2130 const struct got_error *err;
2131 struct got_object_id *new_tree_id;
2133 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2134 progress_cb, progress_arg);
2135 if (err)
2136 return err;
2138 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2139 author, time(NULL), author, time(NULL), logmsg, repo);
2140 free(new_tree_id);
2141 return err;
2144 const struct got_error *
2145 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2146 struct got_repository *repo)
2148 const struct got_error *err = NULL;
2149 char *path_objects = NULL, *path = NULL;
2150 DIR *dir = NULL;
2151 struct got_object_id id;
2152 int i;
2154 *nobjects = 0;
2155 *ondisk_size = 0;
2157 path_objects = got_repo_get_path_objects(repo);
2158 if (path_objects == NULL)
2159 return got_error_from_errno("got_repo_get_path_objects");
2161 for (i = 0; i <= 0xff; i++) {
2162 struct dirent *dent;
2164 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2165 err = got_error_from_errno("asprintf");
2166 break;
2169 dir = opendir(path);
2170 if (dir == NULL) {
2171 if (errno == ENOENT) {
2172 err = NULL;
2173 continue;
2175 err = got_error_from_errno2("opendir", path);
2176 break;
2179 while ((dent = readdir(dir)) != NULL) {
2180 char *id_str;
2181 int fd;
2182 struct stat sb;
2184 if (strcmp(dent->d_name, ".") == 0 ||
2185 strcmp(dent->d_name, "..") == 0)
2186 continue;
2188 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2189 err = got_error_from_errno("asprintf");
2190 goto done;
2193 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2194 free(id_str);
2195 continue;
2197 free(id_str);
2199 err = got_object_open_loose_fd(&fd, &id, repo);
2200 if (err)
2201 goto done;
2203 if (fstat(fd, &sb) == -1) {
2204 err = got_error_from_errno("fstat");
2205 close(fd);
2206 goto done;
2208 (*nobjects)++;
2209 (*ondisk_size) += sb.st_size;
2211 if (close(fd) == -1) {
2212 err = got_error_from_errno("close");
2213 goto done;
2217 if (closedir(dir) != 0) {
2218 err = got_error_from_errno("closedir");
2219 goto done;
2221 dir = NULL;
2223 free(path);
2224 path = NULL;
2226 done:
2227 if (dir && closedir(dir) != 0 && err == NULL)
2228 err = got_error_from_errno("closedir");
2230 if (err) {
2231 *nobjects = 0;
2232 *ondisk_size = 0;
2234 free(path_objects);
2235 free(path);
2236 return err;
2239 const struct got_error *
2240 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2241 off_t *total_packsize, struct got_repository *repo)
2243 const struct got_error *err = NULL;
2244 DIR *packdir = NULL;
2245 struct dirent *dent;
2246 struct got_packidx *packidx = NULL;
2247 char *path_packidx;
2248 char *path_packfile;
2249 int packdir_fd;
2250 struct stat sb;
2252 *npackfiles = 0;
2253 *nobjects = 0;
2254 *total_packsize = 0;
2256 packdir_fd = openat(got_repo_get_fd(repo),
2257 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2258 if (packdir_fd == -1) {
2259 return got_error_from_errno_fmt("openat: %s/%s",
2260 got_repo_get_path_git_dir(repo),
2261 GOT_OBJECTS_PACK_DIR);
2264 packdir = fdopendir(packdir_fd);
2265 if (packdir == NULL) {
2266 err = got_error_from_errno("fdopendir");
2267 goto done;
2270 while ((dent = readdir(packdir)) != NULL) {
2271 if (!got_repo_is_packidx_filename(dent->d_name,
2272 strlen(dent->d_name)))
2273 continue;
2275 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2276 dent->d_name) == -1) {
2277 err = got_error_from_errno("asprintf");
2278 goto done;
2281 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2282 path_packidx, 0);
2283 free(path_packidx);
2284 if (err)
2285 goto done;
2287 if (fstat(packidx->fd, &sb) == -1)
2288 goto done;
2289 *total_packsize += sb.st_size;
2291 err = got_packidx_get_packfile_path(&path_packfile,
2292 packidx->path_packidx);
2293 if (err)
2294 goto done;
2296 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2297 0) == -1) {
2298 free(path_packfile);
2299 goto done;
2301 free(path_packfile);
2302 *total_packsize += sb.st_size;
2304 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2306 (*npackfiles)++;
2308 got_packidx_close(packidx);
2309 packidx = NULL;
2311 done:
2312 if (packidx)
2313 got_packidx_close(packidx);
2314 if (packdir && closedir(packdir) != 0 && err == NULL)
2315 err = got_error_from_errno("closedir");
2316 if (err) {
2317 *npackfiles = 0;
2318 *nobjects = 0;
2319 *total_packsize = 0;
2321 return err;
2324 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2325 got_packidx_bloom_filter_cmp);