Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 #endif
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
76 const char *
77 got_repo_get_path(struct got_repository *repo)
78 {
79 return repo->path;
80 }
82 const char *
83 got_repo_get_path_git_dir(struct got_repository *repo)
84 {
85 return repo->path_git_dir;
86 }
88 int
89 got_repo_get_fd(struct got_repository *repo)
90 {
91 return repo->gitdir_fd;
92 }
94 const char *
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_name;
98 }
100 const char *
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
106 const char *
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
112 const char *
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
118 const char *
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
124 void
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
132 int
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
138 static char *
139 get_path_git_child(struct got_repository *repo, const char *basename)
141 char *path_child;
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
144 basename) == -1)
145 return NULL;
147 return path_child;
150 char *
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
156 char *
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
162 char *
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
168 char *
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
174 static char *
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
180 char *
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
186 char *
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
198 void
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
206 static int
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
213 int ret = 0;
214 struct stat sb;
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_objects, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_refs, &sb) == -1)
228 goto done;
229 if (!S_ISDIR(sb.st_mode))
230 goto done;
232 if (lstat(path_head, &sb) == -1)
233 goto done;
234 if (!S_ISREG(sb.st_mode))
235 goto done;
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
239 goto done;
240 got_ref_close(head_ref);
242 ret = 1;
243 done:
244 free(path_objects);
245 free(path_refs);
246 free(path_head);
247 return ret;
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i, pack_fds_tmp[GOT_PACK_NUM_TEMPFILES];
257 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
258 if (*pack_fds == NULL)
259 return got_error_from_errno("calloc");
261 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
262 pack_fds_tmp[i] = got_opentempfd();
263 if (pack_fds_tmp[i] == -1) {
264 err = got_error_from_errno("got_opentempfd");
265 got_repo_pack_fds_close(pack_fds_tmp);
266 return err;
269 memcpy(*pack_fds, pack_fds_tmp, sizeof(pack_fds_tmp));
270 return err;
273 const struct got_error *
274 got_repo_pack_fds_close(int *pack_fds)
276 const struct got_error *err = NULL;
277 int i;
279 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
280 if (pack_fds[i] == -1)
281 continue;
282 if (close(pack_fds[i]) == -1) {
283 err = got_error_from_errno("close");
284 break;
287 free(pack_fds);
288 return err;
291 const struct got_error *
292 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
293 struct got_object *obj)
295 #ifndef GOT_NO_OBJ_CACHE
296 const struct got_error *err = NULL;
297 err = got_object_cache_add(&repo->objcache, id, obj);
298 if (err) {
299 if (err->code == GOT_ERR_OBJ_EXISTS ||
300 err->code == GOT_ERR_OBJ_TOO_LARGE)
301 err = NULL;
302 return err;
304 obj->refcnt++;
305 #endif
306 return NULL;
309 struct got_object *
310 got_repo_get_cached_object(struct got_repository *repo,
311 struct got_object_id *id)
313 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
316 const struct got_error *
317 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
318 struct got_tree_object *tree)
320 #ifndef GOT_NO_OBJ_CACHE
321 const struct got_error *err = NULL;
322 err = got_object_cache_add(&repo->treecache, id, tree);
323 if (err) {
324 if (err->code == GOT_ERR_OBJ_EXISTS ||
325 err->code == GOT_ERR_OBJ_TOO_LARGE)
326 err = NULL;
327 return err;
329 tree->refcnt++;
330 #endif
331 return NULL;
334 struct got_tree_object *
335 got_repo_get_cached_tree(struct got_repository *repo,
336 struct got_object_id *id)
338 return (struct got_tree_object *)got_object_cache_get(
339 &repo->treecache, id);
342 const struct got_error *
343 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
344 struct got_commit_object *commit)
346 #ifndef GOT_NO_OBJ_CACHE
347 const struct got_error *err = NULL;
348 err = got_object_cache_add(&repo->commitcache, id, commit);
349 if (err) {
350 if (err->code == GOT_ERR_OBJ_EXISTS ||
351 err->code == GOT_ERR_OBJ_TOO_LARGE)
352 err = NULL;
353 return err;
355 commit->refcnt++;
356 #endif
357 return NULL;
360 struct got_commit_object *
361 got_repo_get_cached_commit(struct got_repository *repo,
362 struct got_object_id *id)
364 return (struct got_commit_object *)got_object_cache_get(
365 &repo->commitcache, id);
368 const struct got_error *
369 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
370 struct got_tag_object *tag)
372 #ifndef GOT_NO_OBJ_CACHE
373 const struct got_error *err = NULL;
374 err = got_object_cache_add(&repo->tagcache, id, tag);
375 if (err) {
376 if (err->code == GOT_ERR_OBJ_EXISTS ||
377 err->code == GOT_ERR_OBJ_TOO_LARGE)
378 err = NULL;
379 return err;
381 tag->refcnt++;
382 #endif
383 return NULL;
386 struct got_tag_object *
387 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
389 return (struct got_tag_object *)got_object_cache_get(
390 &repo->tagcache, id);
393 const struct got_error *
394 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
395 struct got_raw_object *raw)
397 #ifndef GOT_NO_OBJ_CACHE
398 const struct got_error *err = NULL;
399 err = got_object_cache_add(&repo->rawcache, id, raw);
400 if (err) {
401 if (err->code == GOT_ERR_OBJ_EXISTS ||
402 err->code == GOT_ERR_OBJ_TOO_LARGE)
403 err = NULL;
404 return err;
406 raw->refcnt++;
407 #endif
408 return NULL;
412 struct got_raw_object *
413 got_repo_get_cached_raw_object(struct got_repository *repo,
414 struct got_object_id *id)
416 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
420 static const struct got_error *
421 open_repo(struct got_repository *repo, const char *path)
423 const struct got_error *err = NULL;
425 repo->gitdir_fd = -1;
427 /* bare git repository? */
428 repo->path_git_dir = strdup(path);
429 if (repo->path_git_dir == NULL)
430 return got_error_from_errno("strdup");
431 if (is_git_repo(repo)) {
432 repo->path = strdup(repo->path_git_dir);
433 if (repo->path == NULL) {
434 err = got_error_from_errno("strdup");
435 goto done;
437 repo->gitdir_fd = open(repo->path_git_dir,
438 O_DIRECTORY | O_CLOEXEC);
439 if (repo->gitdir_fd == -1) {
440 err = got_error_from_errno2("open",
441 repo->path_git_dir);
442 goto done;
444 return NULL;
447 /* git repository with working tree? */
448 free(repo->path_git_dir);
449 repo->path_git_dir = NULL;
450 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
451 err = got_error_from_errno("asprintf");
452 goto done;
454 if (is_git_repo(repo)) {
455 repo->path = strdup(path);
456 if (repo->path == NULL) {
457 err = got_error_from_errno("strdup");
458 goto done;
460 repo->gitdir_fd = open(repo->path_git_dir,
461 O_DIRECTORY | O_CLOEXEC);
462 if (repo->gitdir_fd == -1) {
463 err = got_error_from_errno2("open",
464 repo->path_git_dir);
465 goto done;
467 return NULL;
470 err = got_error(GOT_ERR_NOT_GIT_REPO);
471 done:
472 if (err) {
473 free(repo->path);
474 repo->path = NULL;
475 free(repo->path_git_dir);
476 repo->path_git_dir = NULL;
477 if (repo->gitdir_fd != -1)
478 close(repo->gitdir_fd);
479 repo->gitdir_fd = -1;
482 return err;
485 static const struct got_error *
486 parse_gitconfig_file(int *gitconfig_repository_format_version,
487 char **gitconfig_author_name, char **gitconfig_author_email,
488 struct got_remote_repo **remotes, int *nremotes,
489 char **gitconfig_owner, char ***extensions, int *nextensions,
490 const char *gitconfig_path)
492 const struct got_error *err = NULL, *child_err = NULL;
493 int fd = -1;
494 int imsg_fds[2] = { -1, -1 };
495 pid_t pid;
496 struct imsgbuf *ibuf;
498 *gitconfig_repository_format_version = 0;
499 if (extensions)
500 *extensions = NULL;
501 if (nextensions)
502 *nextensions = 0;
503 *gitconfig_author_name = NULL;
504 *gitconfig_author_email = NULL;
505 if (remotes)
506 *remotes = NULL;
507 if (nremotes)
508 *nremotes = 0;
509 if (gitconfig_owner)
510 *gitconfig_owner = NULL;
512 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
513 if (fd == -1) {
514 if (errno == ENOENT)
515 return NULL;
516 return got_error_from_errno2("open", gitconfig_path);
519 ibuf = calloc(1, sizeof(*ibuf));
520 if (ibuf == NULL) {
521 err = got_error_from_errno("calloc");
522 goto done;
525 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
526 err = got_error_from_errno("socketpair");
527 goto done;
530 pid = fork();
531 if (pid == -1) {
532 err = got_error_from_errno("fork");
533 goto done;
534 } else if (pid == 0) {
535 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
536 gitconfig_path);
537 /* not reached */
540 if (close(imsg_fds[1]) == -1) {
541 err = got_error_from_errno("close");
542 goto done;
544 imsg_fds[1] = -1;
545 imsg_init(ibuf, imsg_fds[0]);
547 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
548 if (err)
549 goto done;
550 fd = -1;
552 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
553 if (err)
554 goto done;
556 err = got_privsep_recv_gitconfig_int(
557 gitconfig_repository_format_version, ibuf);
558 if (err)
559 goto done;
561 if (extensions && nextensions) {
562 err = got_privsep_send_gitconfig_repository_extensions_req(
563 ibuf);
564 if (err)
565 goto done;
566 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
567 if (err)
568 goto done;
569 if (*nextensions > 0) {
570 int i;
571 *extensions = calloc(*nextensions, sizeof(char *));
572 if (*extensions == NULL) {
573 err = got_error_from_errno("calloc");
574 goto done;
576 for (i = 0; i < *nextensions; i++) {
577 char *ext;
578 err = got_privsep_recv_gitconfig_str(&ext,
579 ibuf);
580 if (err)
581 goto done;
582 (*extensions)[i] = ext;
587 err = got_privsep_send_gitconfig_author_name_req(ibuf);
588 if (err)
589 goto done;
591 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
592 if (err)
593 goto done;
595 err = got_privsep_send_gitconfig_author_email_req(ibuf);
596 if (err)
597 goto done;
599 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
600 if (err)
601 goto done;
603 if (remotes && nremotes) {
604 err = got_privsep_send_gitconfig_remotes_req(ibuf);
605 if (err)
606 goto done;
608 err = got_privsep_recv_gitconfig_remotes(remotes,
609 nremotes, ibuf);
610 if (err)
611 goto done;
614 if (gitconfig_owner) {
615 err = got_privsep_send_gitconfig_owner_req(ibuf);
616 if (err)
617 goto done;
618 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
619 if (err)
620 goto done;
623 err = got_privsep_send_stop(imsg_fds[0]);
624 child_err = got_privsep_wait_for_child(pid);
625 if (child_err && err == NULL)
626 err = child_err;
627 done:
628 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
629 err = got_error_from_errno("close");
630 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
631 err = got_error_from_errno("close");
632 if (fd != -1 && close(fd) == -1 && err == NULL)
633 err = got_error_from_errno2("close", gitconfig_path);
634 free(ibuf);
635 return err;
638 static const struct got_error *
639 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
641 const struct got_error *err = NULL;
642 char *repo_gitconfig_path = NULL;
644 if (global_gitconfig_path) {
645 /* Read settings from ~/.gitconfig. */
646 int dummy_repo_version;
647 err = parse_gitconfig_file(&dummy_repo_version,
648 &repo->global_gitconfig_author_name,
649 &repo->global_gitconfig_author_email,
650 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
651 if (err)
652 return err;
655 /* Read repository's .git/config file. */
656 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
657 if (repo_gitconfig_path == NULL)
658 return got_error_from_errno("got_repo_get_path_gitconfig");
660 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
661 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
662 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
663 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
664 repo_gitconfig_path);
665 if (err)
666 goto done;
667 done:
668 free(repo_gitconfig_path);
669 return err;
672 static const struct got_error *
673 read_gotconfig(struct got_repository *repo)
675 const struct got_error *err = NULL;
676 char *gotconfig_path;
678 gotconfig_path = got_repo_get_path_gotconfig(repo);
679 if (gotconfig_path == NULL)
680 return got_error_from_errno("got_repo_get_path_gotconfig");
682 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
683 free(gotconfig_path);
684 return err;
687 /* Supported repository format extensions. */
688 static const char *const repo_extensions[] = {
689 "noop", /* Got supports repository format version 1. */
690 "preciousObjects", /* Supported by gotadmin cleanup. */
691 "worktreeConfig", /* Got does not care about Git work trees. */
692 };
694 const struct got_error *
695 got_repo_open(struct got_repository **repop, const char *path,
696 const char *global_gitconfig_path, int *pack_fds)
698 struct got_repository *repo = NULL;
699 const struct got_error *err = NULL;
700 char *repo_path = NULL;
701 size_t i, j = 0;
702 struct rlimit rl;
704 *repop = NULL;
706 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
707 return got_error_from_errno("getrlimit");
709 repo = calloc(1, sizeof(*repo));
710 if (repo == NULL)
711 return got_error_from_errno("calloc");
713 RB_INIT(&repo->packidx_bloom_filters);
714 TAILQ_INIT(&repo->packidx_paths);
716 for (i = 0; i < nitems(repo->privsep_children); i++) {
717 memset(&repo->privsep_children[i], 0,
718 sizeof(repo->privsep_children[0]));
719 repo->privsep_children[i].imsg_fd = -1;
722 err = got_object_cache_init(&repo->objcache,
723 GOT_OBJECT_CACHE_TYPE_OBJ);
724 if (err)
725 goto done;
726 err = got_object_cache_init(&repo->treecache,
727 GOT_OBJECT_CACHE_TYPE_TREE);
728 if (err)
729 goto done;
730 err = got_object_cache_init(&repo->commitcache,
731 GOT_OBJECT_CACHE_TYPE_COMMIT);
732 if (err)
733 goto done;
734 err = got_object_cache_init(&repo->tagcache,
735 GOT_OBJECT_CACHE_TYPE_TAG);
736 if (err)
737 goto done;
738 err = got_object_cache_init(&repo->rawcache,
739 GOT_OBJECT_CACHE_TYPE_RAW);
740 if (err)
741 goto done;
743 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
744 if (repo->pack_cache_size > rl.rlim_cur / 8)
745 repo->pack_cache_size = rl.rlim_cur / 8;
746 for (i = 0; i < nitems(repo->packs); i++) {
747 if (i < repo->pack_cache_size) {
748 repo->packs[i].basefd = pack_fds[j++];
749 repo->packs[i].accumfd = pack_fds[j++];
750 } else {
751 repo->packs[i].basefd = -1;
752 repo->packs[i].accumfd = -1;
756 repo_path = realpath(path, NULL);
757 if (repo_path == NULL) {
758 err = got_error_from_errno2("realpath", path);
759 goto done;
762 for (;;) {
763 char *parent_path;
765 err = open_repo(repo, repo_path);
766 if (err == NULL)
767 break;
768 if (err->code != GOT_ERR_NOT_GIT_REPO)
769 goto done;
770 if (repo_path[0] == '/' && repo_path[1] == '\0') {
771 err = got_error(GOT_ERR_NOT_GIT_REPO);
772 goto done;
774 err = got_path_dirname(&parent_path, repo_path);
775 if (err)
776 goto done;
777 free(repo_path);
778 repo_path = parent_path;
781 err = read_gotconfig(repo);
782 if (err)
783 goto done;
785 err = read_gitconfig(repo, global_gitconfig_path);
786 if (err)
787 goto done;
788 if (repo->gitconfig_repository_format_version != 0)
789 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
790 for (i = 0; i < repo->nextensions; i++) {
791 char *ext = repo->extensions[i];
792 int j, supported = 0;
793 for (j = 0; j < nitems(repo_extensions); j++) {
794 if (strcmp(ext, repo_extensions[j]) == 0) {
795 supported = 1;
796 break;
799 if (!supported) {
800 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
801 goto done;
805 err = got_repo_list_packidx(&repo->packidx_paths, repo);
806 done:
807 if (err)
808 got_repo_close(repo);
809 else
810 *repop = repo;
811 free(repo_path);
812 return err;
815 const struct got_error *
816 got_repo_close(struct got_repository *repo)
818 const struct got_error *err = NULL, *child_err;
819 struct got_packidx_bloom_filter *bf;
820 struct got_pathlist_entry *pe;
821 size_t i;
823 for (i = 0; i < repo->pack_cache_size; i++) {
824 if (repo->packidx_cache[i] == NULL)
825 break;
826 got_packidx_close(repo->packidx_cache[i]);
829 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
830 &repo->packidx_bloom_filters))) {
831 RB_REMOVE(got_packidx_bloom_filter_tree,
832 &repo->packidx_bloom_filters, bf);
833 free(bf->bloom);
834 free(bf);
837 for (i = 0; i < repo->pack_cache_size; i++)
838 if (repo->packs[i].path_packfile)
839 if (repo->packs[i].path_packfile)
840 got_pack_close(&repo->packs[i]);
842 free(repo->path);
843 free(repo->path_git_dir);
845 got_object_cache_close(&repo->objcache);
846 got_object_cache_close(&repo->treecache);
847 got_object_cache_close(&repo->commitcache);
848 got_object_cache_close(&repo->tagcache);
849 got_object_cache_close(&repo->rawcache);
851 for (i = 0; i < nitems(repo->privsep_children); i++) {
852 if (repo->privsep_children[i].imsg_fd == -1)
853 continue;
854 imsg_clear(repo->privsep_children[i].ibuf);
855 free(repo->privsep_children[i].ibuf);
856 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
857 child_err = got_privsep_wait_for_child(
858 repo->privsep_children[i].pid);
859 if (child_err && err == NULL)
860 err = child_err;
861 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
862 err == NULL)
863 err = got_error_from_errno("close");
866 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
867 err == NULL)
868 err = got_error_from_errno("close");
870 if (repo->gotconfig)
871 got_gotconfig_free(repo->gotconfig);
872 free(repo->gitconfig_author_name);
873 free(repo->gitconfig_author_email);
874 for (i = 0; i < repo->ngitconfig_remotes; i++)
875 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
876 free(repo->gitconfig_remotes);
877 for (i = 0; i < repo->nextensions; i++)
878 free(repo->extensions[i]);
879 free(repo->extensions);
881 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
882 free((void *)pe->path);
883 got_pathlist_free(&repo->packidx_paths);
884 free(repo);
886 return err;
889 void
890 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
892 int i;
894 free(repo->name);
895 repo->name = NULL;
896 free(repo->fetch_url);
897 repo->fetch_url = NULL;
898 free(repo->send_url);
899 repo->send_url = NULL;
900 for (i = 0; i < repo->nfetch_branches; i++)
901 free(repo->fetch_branches[i]);
902 free(repo->fetch_branches);
903 repo->fetch_branches = NULL;
904 repo->nfetch_branches = 0;
905 for (i = 0; i < repo->nsend_branches; i++)
906 free(repo->send_branches[i]);
907 free(repo->send_branches);
908 repo->send_branches = NULL;
909 repo->nsend_branches = 0;
912 const struct got_error *
913 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
914 const char *input_path)
916 const struct got_error *err = NULL;
917 const char *repo_abspath = NULL;
918 size_t repolen, len;
919 char *canonpath, *path = NULL;
921 *in_repo_path = NULL;
923 canonpath = strdup(input_path);
924 if (canonpath == NULL) {
925 err = got_error_from_errno("strdup");
926 goto done;
928 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
929 if (err)
930 goto done;
932 repo_abspath = got_repo_get_path(repo);
934 if (canonpath[0] == '\0') {
935 path = strdup(canonpath);
936 if (path == NULL) {
937 err = got_error_from_errno("strdup");
938 goto done;
940 } else {
941 path = realpath(canonpath, NULL);
942 if (path == NULL) {
943 if (errno != ENOENT) {
944 err = got_error_from_errno2("realpath",
945 canonpath);
946 goto done;
948 /*
949 * Path is not on disk.
950 * Assume it is already relative to repository root.
951 */
952 path = strdup(canonpath);
953 if (path == NULL) {
954 err = got_error_from_errno("strdup");
955 goto done;
959 repolen = strlen(repo_abspath);
960 len = strlen(path);
963 if (strcmp(path, repo_abspath) == 0) {
964 free(path);
965 path = strdup("");
966 if (path == NULL) {
967 err = got_error_from_errno("strdup");
968 goto done;
970 } else if (len > repolen &&
971 got_path_is_child(path, repo_abspath, repolen)) {
972 /* Matched an on-disk path inside repository. */
973 if (got_repo_is_bare(repo)) {
974 /*
975 * Matched an on-disk path inside repository
976 * database. Treat input as repository-relative.
977 */
978 free(path);
979 path = canonpath;
980 canonpath = NULL;
981 } else {
982 char *child;
983 /* Strip common prefix with repository path. */
984 err = got_path_skip_common_ancestor(&child,
985 repo_abspath, path);
986 if (err)
987 goto done;
988 free(path);
989 path = child;
991 } else {
992 /*
993 * Matched unrelated on-disk path.
994 * Treat input as repository-relative.
995 */
996 free(path);
997 path = canonpath;
998 canonpath = NULL;
1002 /* Make in-repository path absolute */
1003 if (path[0] != '/') {
1004 char *abspath;
1005 if (asprintf(&abspath, "/%s", path) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 goto done;
1009 free(path);
1010 path = abspath;
1013 done:
1014 free(canonpath);
1015 if (err)
1016 free(path);
1017 else
1018 *in_repo_path = path;
1019 return err;
1022 static const struct got_error *
1023 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1024 const char *path_packidx)
1026 const struct got_error *err = NULL;
1027 size_t i;
1029 for (i = 0; i < repo->pack_cache_size; i++) {
1030 if (repo->packidx_cache[i] == NULL)
1031 break;
1032 if (strcmp(repo->packidx_cache[i]->path_packidx,
1033 path_packidx) == 0) {
1034 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1037 if (i == repo->pack_cache_size) {
1038 i = repo->pack_cache_size - 1;
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;
1175 return NULL;
1178 /* No luck. Search the filesystem. */
1180 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1181 const char *path_packidx = pe->path;
1182 int is_cached = 0;
1184 if (!got_repo_check_packidx_bloom_filter(repo,
1185 pe->path, id))
1186 continue; /* object will not be found in this index */
1188 for (i = 0; i < repo->pack_cache_size; i++) {
1189 if (repo->packidx_cache[i] == NULL)
1190 break;
1191 if (strcmp(repo->packidx_cache[i]->path_packidx,
1192 path_packidx) == 0) {
1193 is_cached = 1;
1194 break;
1197 if (is_cached)
1198 continue; /* already searched */
1200 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1201 path_packidx, 0);
1202 if (err)
1203 goto done;
1205 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1206 if (err)
1207 goto done;
1209 err = cache_packidx(repo, *packidx, path_packidx);
1210 if (err)
1211 goto done;
1213 *idx = got_packidx_get_object_idx(*packidx, id);
1214 if (*idx != -1) {
1215 err = NULL; /* found the object */
1216 goto done;
1220 err = got_error_no_obj(id);
1221 done:
1222 return err;
1225 const struct got_error *
1226 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1227 struct got_repository *repo)
1229 const struct got_error *err = NULL;
1230 DIR *packdir = NULL;
1231 struct dirent *dent;
1232 char *path_packidx = NULL;
1233 int packdir_fd;
1235 packdir_fd = openat(got_repo_get_fd(repo),
1236 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1237 if (packdir_fd == -1) {
1238 return got_error_from_errno_fmt("openat: %s/%s",
1239 got_repo_get_path_git_dir(repo),
1240 GOT_OBJECTS_PACK_DIR);
1243 packdir = fdopendir(packdir_fd);
1244 if (packdir == NULL) {
1245 err = got_error_from_errno("fdopendir");
1246 goto done;
1249 while ((dent = readdir(packdir)) != NULL) {
1250 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1251 continue;
1253 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1254 dent->d_name) == -1) {
1255 err = got_error_from_errno("asprintf");
1256 path_packidx = NULL;
1257 break;
1260 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1261 if (err)
1262 break;
1264 done:
1265 if (err)
1266 free(path_packidx);
1267 if (packdir && closedir(packdir) != 0 && err == NULL)
1268 err = got_error_from_errno("closedir");
1269 return err;
1272 const struct got_error *
1273 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1274 struct got_repository *repo)
1276 const struct got_error *err;
1277 size_t i;
1279 *packidx = NULL;
1281 /* Search pack index cache. */
1282 for (i = 0; i < repo->pack_cache_size; i++) {
1283 if (repo->packidx_cache[i] == NULL)
1284 break;
1285 if (strcmp(repo->packidx_cache[i]->path_packidx,
1286 path_packidx) == 0) {
1287 *packidx = repo->packidx_cache[i];
1288 return NULL;
1291 /* No luck. Search the filesystem. */
1293 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1294 path_packidx, 0);
1295 if (err)
1296 return err;
1298 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1299 if (err)
1300 goto done;
1302 err = cache_packidx(repo, *packidx, path_packidx);
1303 done:
1304 if (err) {
1305 got_packidx_close(*packidx);
1306 *packidx = NULL;
1308 return err;
1311 static const struct got_error *
1312 read_packfile_hdr(int fd, struct got_packidx *packidx)
1314 const struct got_error *err = NULL;
1315 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1316 struct got_packfile_hdr hdr;
1317 ssize_t n;
1319 n = read(fd, &hdr, sizeof(hdr));
1320 if (n < 0)
1321 return got_error_from_errno("read");
1322 if (n != sizeof(hdr))
1323 return got_error(GOT_ERR_BAD_PACKFILE);
1325 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1326 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1327 be32toh(hdr.nobjects) != totobj)
1328 err = got_error(GOT_ERR_BAD_PACKFILE);
1330 return err;
1333 static const struct got_error *
1334 open_packfile(int *fd, struct got_repository *repo,
1335 const char *relpath, struct got_packidx *packidx)
1337 const struct got_error *err = NULL;
1339 *fd = openat(got_repo_get_fd(repo), relpath,
1340 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1341 if (*fd == -1)
1342 return got_error_from_errno_fmt("openat: %s/%s",
1343 got_repo_get_path_git_dir(repo), relpath);
1345 if (packidx) {
1346 err = read_packfile_hdr(*fd, packidx);
1347 if (err) {
1348 close(*fd);
1349 *fd = -1;
1353 return err;
1356 const struct got_error *
1357 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1358 const char *path_packfile, struct got_packidx *packidx)
1360 const struct got_error *err = NULL;
1361 struct got_pack *pack = NULL;
1362 struct stat sb;
1363 size_t i;
1365 if (packp)
1366 *packp = NULL;
1368 for (i = 0; i < repo->pack_cache_size; i++) {
1369 pack = &repo->packs[i];
1370 if (pack->path_packfile == NULL)
1371 break;
1372 if (strcmp(pack->path_packfile, path_packfile) == 0)
1373 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1376 if (i == repo->pack_cache_size) {
1377 struct got_pack tmp;
1378 err = got_pack_close(&repo->packs[i - 1]);
1379 if (err)
1380 return err;
1381 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1382 return got_error_from_errno("ftruncate");
1383 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1384 return got_error_from_errno("ftruncate");
1385 memcpy(&tmp, &repo->packs[i - 1], sizeof(tmp));
1386 memcpy(&repo->packs[i - 1], &repo->packs[0],
1387 sizeof(repo->packs[i - 1]));
1388 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1389 i = 0;
1392 pack = &repo->packs[i];
1394 pack->path_packfile = strdup(path_packfile);
1395 if (pack->path_packfile == NULL) {
1396 err = got_error_from_errno("strdup");
1397 goto done;
1400 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1401 if (err)
1402 goto done;
1404 if (fstat(pack->fd, &sb) != 0) {
1405 err = got_error_from_errno("fstat");
1406 goto done;
1408 pack->filesize = sb.st_size;
1410 pack->privsep_child = NULL;
1412 #ifndef GOT_PACK_NO_MMAP
1413 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1414 pack->fd, 0);
1415 if (pack->map == MAP_FAILED) {
1416 if (errno != ENOMEM) {
1417 err = got_error_from_errno("mmap");
1418 goto done;
1420 pack->map = NULL; /* fall back to read(2) */
1422 #endif
1423 done:
1424 if (err) {
1425 if (pack) {
1426 free(pack->path_packfile);
1427 memset(pack, 0, sizeof(*pack));
1429 } else if (packp)
1430 *packp = pack;
1431 return err;
1434 struct got_pack *
1435 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1437 struct got_pack *pack = NULL;
1438 size_t i;
1440 for (i = 0; i < repo->pack_cache_size; i++) {
1441 pack = &repo->packs[i];
1442 if (pack->path_packfile == NULL)
1443 break;
1444 if (strcmp(pack->path_packfile, path_packfile) == 0)
1445 return pack;
1448 return NULL;
1451 const struct got_error *
1452 got_repo_init(const char *repo_path)
1454 const struct got_error *err = NULL;
1455 const char *dirnames[] = {
1456 GOT_OBJECTS_DIR,
1457 GOT_OBJECTS_PACK_DIR,
1458 GOT_REFS_DIR,
1460 const char *description_str = "Unnamed repository; "
1461 "edit this file 'description' to name the repository.";
1462 const char *headref_str = "ref: refs/heads/main";
1463 const char *gitconfig_str = "[core]\n"
1464 "\trepositoryformatversion = 0\n"
1465 "\tfilemode = true\n"
1466 "\tbare = true\n";
1467 char *path;
1468 size_t i;
1470 if (!got_path_dir_is_empty(repo_path))
1471 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1473 for (i = 0; i < nitems(dirnames); i++) {
1474 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1475 return got_error_from_errno("asprintf");
1477 err = got_path_mkdir(path);
1478 free(path);
1479 if (err)
1480 return err;
1483 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1484 return got_error_from_errno("asprintf");
1485 err = got_path_create_file(path, description_str);
1486 free(path);
1487 if (err)
1488 return err;
1490 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1491 return got_error_from_errno("asprintf");
1492 err = got_path_create_file(path, headref_str);
1493 free(path);
1494 if (err)
1495 return err;
1497 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1498 return got_error_from_errno("asprintf");
1499 err = got_path_create_file(path, gitconfig_str);
1500 free(path);
1501 if (err)
1502 return err;
1504 return NULL;
1507 static const struct got_error *
1508 match_packed_object(struct got_object_id **unique_id,
1509 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1511 const struct got_error *err = NULL;
1512 struct got_object_id_queue matched_ids;
1513 struct got_pathlist_entry *pe;
1515 STAILQ_INIT(&matched_ids);
1517 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1518 const char *path_packidx = pe->path;
1519 struct got_packidx *packidx;
1520 struct got_object_qid *qid;
1522 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1523 path_packidx, 0);
1524 if (err)
1525 break;
1527 err = got_packidx_match_id_str_prefix(&matched_ids,
1528 packidx, id_str_prefix);
1529 if (err) {
1530 got_packidx_close(packidx);
1531 break;
1533 err = got_packidx_close(packidx);
1534 if (err)
1535 break;
1537 STAILQ_FOREACH(qid, &matched_ids, entry) {
1538 if (obj_type != GOT_OBJ_TYPE_ANY) {
1539 int matched_type;
1540 err = got_object_get_type(&matched_type, repo,
1541 &qid->id);
1542 if (err)
1543 goto done;
1544 if (matched_type != obj_type)
1545 continue;
1547 if (*unique_id == NULL) {
1548 *unique_id = got_object_id_dup(&qid->id);
1549 if (*unique_id == NULL) {
1550 err = got_error_from_errno("malloc");
1551 goto done;
1553 } else {
1554 if (got_object_id_cmp(*unique_id,
1555 &qid->id) == 0)
1556 continue; /* packed multiple times */
1557 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1558 goto done;
1562 done:
1563 got_object_id_queue_free(&matched_ids);
1564 if (err) {
1565 free(*unique_id);
1566 *unique_id = NULL;
1568 return err;
1571 static const struct got_error *
1572 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1573 const char *object_dir, const char *id_str_prefix, int obj_type,
1574 struct got_repository *repo)
1576 const struct got_error *err = NULL;
1577 char *path;
1578 DIR *dir = NULL;
1579 struct dirent *dent;
1580 struct got_object_id id;
1582 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1583 err = got_error_from_errno("asprintf");
1584 goto done;
1587 dir = opendir(path);
1588 if (dir == NULL) {
1589 if (errno == ENOENT) {
1590 err = NULL;
1591 goto done;
1593 err = got_error_from_errno2("opendir", path);
1594 goto done;
1596 while ((dent = readdir(dir)) != NULL) {
1597 char *id_str;
1598 int cmp;
1600 if (strcmp(dent->d_name, ".") == 0 ||
1601 strcmp(dent->d_name, "..") == 0)
1602 continue;
1604 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1605 err = got_error_from_errno("asprintf");
1606 goto done;
1609 if (!got_parse_sha1_digest(id.sha1, id_str))
1610 continue;
1613 * Directory entries do not necessarily appear in
1614 * sorted order, so we must iterate over all of them.
1616 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1617 if (cmp != 0) {
1618 free(id_str);
1619 continue;
1622 if (*unique_id == NULL) {
1623 if (obj_type != GOT_OBJ_TYPE_ANY) {
1624 int matched_type;
1625 err = got_object_get_type(&matched_type, repo,
1626 &id);
1627 if (err)
1628 goto done;
1629 if (matched_type != obj_type)
1630 continue;
1632 *unique_id = got_object_id_dup(&id);
1633 if (*unique_id == NULL) {
1634 err = got_error_from_errno("got_object_id_dup");
1635 free(id_str);
1636 goto done;
1638 } else {
1639 if (got_object_id_cmp(*unique_id, &id) == 0)
1640 continue; /* both packed and loose */
1641 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1642 free(id_str);
1643 goto done;
1646 done:
1647 if (dir && closedir(dir) != 0 && err == NULL)
1648 err = got_error_from_errno("closedir");
1649 if (err) {
1650 free(*unique_id);
1651 *unique_id = NULL;
1653 free(path);
1654 return err;
1657 const struct got_error *
1658 got_repo_match_object_id_prefix(struct got_object_id **id,
1659 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1661 const struct got_error *err = NULL;
1662 char *path_objects = got_repo_get_path_objects(repo);
1663 char *object_dir = NULL;
1664 size_t len;
1665 int i;
1667 *id = NULL;
1669 len = strlen(id_str_prefix);
1670 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1671 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1673 for (i = 0; i < len; i++) {
1674 if (isxdigit((unsigned char)id_str_prefix[i]))
1675 continue;
1676 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1679 if (len >= 2) {
1680 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1681 if (err)
1682 goto done;
1683 object_dir = strndup(id_str_prefix, 2);
1684 if (object_dir == NULL) {
1685 err = got_error_from_errno("strdup");
1686 goto done;
1688 err = match_loose_object(id, path_objects, object_dir,
1689 id_str_prefix, obj_type, repo);
1690 } else if (len == 1) {
1691 int i;
1692 for (i = 0; i < 0xf; i++) {
1693 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1694 == -1) {
1695 err = got_error_from_errno("asprintf");
1696 goto done;
1698 err = match_packed_object(id, repo, object_dir,
1699 obj_type);
1700 if (err)
1701 goto done;
1702 err = match_loose_object(id, path_objects, object_dir,
1703 id_str_prefix, obj_type, repo);
1704 if (err)
1705 goto done;
1707 } else {
1708 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1709 goto done;
1711 done:
1712 free(object_dir);
1713 if (err) {
1714 free(*id);
1715 *id = NULL;
1716 } else if (*id == NULL) {
1717 switch (obj_type) {
1718 case GOT_OBJ_TYPE_BLOB:
1719 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1720 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1721 break;
1722 case GOT_OBJ_TYPE_TREE:
1723 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1724 GOT_OBJ_LABEL_TREE, id_str_prefix);
1725 break;
1726 case GOT_OBJ_TYPE_COMMIT:
1727 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1728 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1729 break;
1730 case GOT_OBJ_TYPE_TAG:
1731 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1732 GOT_OBJ_LABEL_TAG, id_str_prefix);
1733 break;
1734 default:
1735 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1736 break;
1740 return err;
1743 const struct got_error *
1744 got_repo_match_object_id(struct got_object_id **id, char **label,
1745 const char *id_str, int obj_type, struct got_reflist_head *refs,
1746 struct got_repository *repo)
1748 const struct got_error *err;
1749 struct got_tag_object *tag;
1750 struct got_reference *ref = NULL;
1752 *id = NULL;
1753 if (label)
1754 *label = NULL;
1756 if (refs) {
1757 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1758 refs, repo);
1759 if (err == NULL) {
1760 *id = got_object_id_dup(
1761 got_object_tag_get_object_id(tag));
1762 if (*id == NULL)
1763 err = got_error_from_errno("got_object_id_dup");
1764 else if (label && asprintf(label, "refs/tags/%s",
1765 got_object_tag_get_name(tag)) == -1) {
1766 err = got_error_from_errno("asprintf");
1767 free(*id);
1768 *id = NULL;
1770 got_object_tag_close(tag);
1771 return err;
1772 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1773 err->code != GOT_ERR_NO_OBJ)
1774 return err;
1777 err = got_ref_open(&ref, repo, id_str, 0);
1778 if (err == NULL) {
1779 err = got_ref_resolve(id, repo, ref);
1780 if (err)
1781 goto done;
1782 if (label) {
1783 *label = strdup(got_ref_get_name(ref));
1784 if (*label == NULL) {
1785 err = got_error_from_errno("strdup");
1786 goto done;
1789 } else {
1790 if (err->code != GOT_ERR_NOT_REF &&
1791 err->code != GOT_ERR_BAD_REF_NAME)
1792 goto done;
1793 err = got_repo_match_object_id_prefix(id, id_str,
1794 obj_type, repo);
1795 if (err) {
1796 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1797 err = got_error_not_ref(id_str);
1798 goto done;
1800 if (label) {
1801 err = got_object_id_str(label, *id);
1802 if (*label == NULL) {
1803 err = got_error_from_errno("strdup");
1804 goto done;
1808 done:
1809 if (ref)
1810 got_ref_close(ref);
1811 return err;
1814 const struct got_error *
1815 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1816 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1818 const struct got_error *err = NULL;
1819 struct got_reflist_entry *re;
1820 struct got_object_id *tag_id;
1821 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1823 *tag = NULL;
1825 TAILQ_FOREACH(re, refs, entry) {
1826 const char *refname;
1827 refname = got_ref_get_name(re->ref);
1828 if (got_ref_is_symbolic(re->ref))
1829 continue;
1830 if (strncmp(refname, "refs/tags/", 10) != 0)
1831 continue;
1832 if (!name_is_absolute)
1833 refname += strlen("refs/tags/");
1834 if (strcmp(refname, name) != 0)
1835 continue;
1836 err = got_ref_resolve(&tag_id, repo, re->ref);
1837 if (err)
1838 break;
1839 err = got_object_open_as_tag(tag, repo, tag_id);
1840 free(tag_id);
1841 if (err)
1842 break;
1843 if (obj_type == GOT_OBJ_TYPE_ANY ||
1844 got_object_tag_get_object_type(*tag) == obj_type)
1845 break;
1846 got_object_tag_close(*tag);
1847 *tag = NULL;
1850 if (err == NULL && *tag == NULL)
1851 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1852 GOT_OBJ_LABEL_TAG, name);
1853 return err;
1856 static const struct got_error *
1857 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1858 const char *name, mode_t mode, struct got_object_id *blob_id)
1860 const struct got_error *err = NULL;
1862 *new_te = NULL;
1864 *new_te = calloc(1, sizeof(**new_te));
1865 if (*new_te == NULL)
1866 return got_error_from_errno("calloc");
1868 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1869 sizeof((*new_te)->name)) {
1870 err = got_error(GOT_ERR_NO_SPACE);
1871 goto done;
1874 if (S_ISLNK(mode)) {
1875 (*new_te)->mode = S_IFLNK;
1876 } else {
1877 (*new_te)->mode = S_IFREG;
1878 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1880 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1881 done:
1882 if (err && *new_te) {
1883 free(*new_te);
1884 *new_te = NULL;
1886 return err;
1889 static const struct got_error *
1890 import_file(struct got_tree_entry **new_te, struct dirent *de,
1891 const char *path, struct got_repository *repo)
1893 const struct got_error *err;
1894 struct got_object_id *blob_id = NULL;
1895 char *filepath;
1896 struct stat sb;
1898 if (asprintf(&filepath, "%s%s%s", path,
1899 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1900 return got_error_from_errno("asprintf");
1902 if (lstat(filepath, &sb) != 0) {
1903 err = got_error_from_errno2("lstat", path);
1904 goto done;
1907 err = got_object_blob_create(&blob_id, filepath, repo);
1908 if (err)
1909 goto done;
1911 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1912 blob_id);
1913 done:
1914 free(filepath);
1915 if (err)
1916 free(blob_id);
1917 return err;
1920 static const struct got_error *
1921 insert_tree_entry(struct got_tree_entry *new_te,
1922 struct got_pathlist_head *paths)
1924 const struct got_error *err = NULL;
1925 struct got_pathlist_entry *new_pe;
1927 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1928 if (err)
1929 return err;
1930 if (new_pe == NULL)
1931 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1932 return NULL;
1935 static const struct got_error *write_tree(struct got_object_id **,
1936 const char *, struct got_pathlist_head *, struct got_repository *,
1937 got_repo_import_cb progress_cb, void *progress_arg);
1939 static const struct got_error *
1940 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1941 const char *path, struct got_pathlist_head *ignores,
1942 struct got_repository *repo,
1943 got_repo_import_cb progress_cb, void *progress_arg)
1945 const struct got_error *err;
1946 struct got_object_id *id = NULL;
1947 char *subdirpath;
1949 if (asprintf(&subdirpath, "%s%s%s", path,
1950 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1951 return got_error_from_errno("asprintf");
1953 (*new_te) = calloc(1, sizeof(**new_te));
1954 if (*new_te == NULL)
1955 return got_error_from_errno("calloc");
1956 (*new_te)->mode = S_IFDIR;
1957 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1958 sizeof((*new_te)->name)) {
1959 err = got_error(GOT_ERR_NO_SPACE);
1960 goto done;
1962 err = write_tree(&id, subdirpath, ignores, repo,
1963 progress_cb, progress_arg);
1964 if (err)
1965 goto done;
1966 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1968 done:
1969 free(id);
1970 free(subdirpath);
1971 if (err) {
1972 free(*new_te);
1973 *new_te = NULL;
1975 return err;
1978 static const struct got_error *
1979 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1980 struct got_pathlist_head *ignores, struct got_repository *repo,
1981 got_repo_import_cb progress_cb, void *progress_arg)
1983 const struct got_error *err = NULL;
1984 DIR *dir;
1985 struct dirent *de;
1986 int nentries;
1987 struct got_tree_entry *new_te = NULL;
1988 struct got_pathlist_head paths;
1989 struct got_pathlist_entry *pe;
1991 *new_tree_id = NULL;
1993 TAILQ_INIT(&paths);
1995 dir = opendir(path_dir);
1996 if (dir == NULL) {
1997 err = got_error_from_errno2("opendir", path_dir);
1998 goto done;
2001 nentries = 0;
2002 while ((de = readdir(dir)) != NULL) {
2003 int ignore = 0;
2004 int type;
2006 if (strcmp(de->d_name, ".") == 0 ||
2007 strcmp(de->d_name, "..") == 0)
2008 continue;
2010 TAILQ_FOREACH(pe, ignores, entry) {
2011 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2012 ignore = 1;
2013 break;
2016 if (ignore)
2017 continue;
2019 err = got_path_dirent_type(&type, path_dir, de);
2020 if (err)
2021 goto done;
2023 if (type == DT_DIR) {
2024 err = import_subdir(&new_te, de, path_dir,
2025 ignores, repo, progress_cb, progress_arg);
2026 if (err) {
2027 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2028 goto done;
2029 err = NULL;
2030 continue;
2032 } else if (type == DT_REG || type == DT_LNK) {
2033 err = import_file(&new_te, de, path_dir, repo);
2034 if (err)
2035 goto done;
2036 } else
2037 continue;
2039 err = insert_tree_entry(new_te, &paths);
2040 if (err)
2041 goto done;
2042 nentries++;
2045 if (TAILQ_EMPTY(&paths)) {
2046 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2047 "cannot create tree without any entries");
2048 goto done;
2051 TAILQ_FOREACH(pe, &paths, entry) {
2052 struct got_tree_entry *te = pe->data;
2053 char *path;
2054 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2055 continue;
2056 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2057 err = got_error_from_errno("asprintf");
2058 goto done;
2060 err = (*progress_cb)(progress_arg, path);
2061 free(path);
2062 if (err)
2063 goto done;
2066 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2067 done:
2068 if (dir)
2069 closedir(dir);
2070 got_pathlist_free(&paths);
2071 return err;
2074 const struct got_error *
2075 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2076 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2077 struct got_repository *repo, got_repo_import_cb progress_cb,
2078 void *progress_arg)
2080 const struct got_error *err;
2081 struct got_object_id *new_tree_id;
2083 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2084 progress_cb, progress_arg);
2085 if (err)
2086 return err;
2088 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2089 author, time(NULL), author, time(NULL), logmsg, repo);
2090 free(new_tree_id);
2091 return err;
2094 const struct got_error *
2095 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2096 struct got_repository *repo)
2098 const struct got_error *err = NULL;
2099 char *path_objects = NULL, *path = NULL;
2100 DIR *dir = NULL;
2101 struct got_object_id id;
2102 int i;
2104 *nobjects = 0;
2105 *ondisk_size = 0;
2107 path_objects = got_repo_get_path_objects(repo);
2108 if (path_objects == NULL)
2109 return got_error_from_errno("got_repo_get_path_objects");
2111 for (i = 0; i <= 0xff; i++) {
2112 struct dirent *dent;
2114 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2115 err = got_error_from_errno("asprintf");
2116 break;
2119 dir = opendir(path);
2120 if (dir == NULL) {
2121 if (errno == ENOENT) {
2122 err = NULL;
2123 continue;
2125 err = got_error_from_errno2("opendir", path);
2126 break;
2129 while ((dent = readdir(dir)) != NULL) {
2130 char *id_str;
2131 int fd;
2132 struct stat sb;
2134 if (strcmp(dent->d_name, ".") == 0 ||
2135 strcmp(dent->d_name, "..") == 0)
2136 continue;
2138 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2139 err = got_error_from_errno("asprintf");
2140 goto done;
2143 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2144 free(id_str);
2145 continue;
2147 free(id_str);
2149 err = got_object_open_loose_fd(&fd, &id, repo);
2150 if (err)
2151 goto done;
2153 if (fstat(fd, &sb) == -1) {
2154 err = got_error_from_errno("fstat");
2155 close(fd);
2156 goto done;
2158 (*nobjects)++;
2159 (*ondisk_size) += sb.st_size;
2161 if (close(fd) == -1) {
2162 err = got_error_from_errno("close");
2163 goto done;
2167 if (closedir(dir) != 0) {
2168 err = got_error_from_errno("closedir");
2169 goto done;
2171 dir = NULL;
2173 free(path);
2174 path = NULL;
2176 done:
2177 if (dir && closedir(dir) != 0 && err == NULL)
2178 err = got_error_from_errno("closedir");
2180 if (err) {
2181 *nobjects = 0;
2182 *ondisk_size = 0;
2184 free(path_objects);
2185 free(path);
2186 return err;
2189 const struct got_error *
2190 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2191 off_t *total_packsize, struct got_repository *repo)
2193 const struct got_error *err = NULL;
2194 DIR *packdir = NULL;
2195 struct dirent *dent;
2196 struct got_packidx *packidx = NULL;
2197 char *path_packidx;
2198 char *path_packfile;
2199 int packdir_fd;
2200 struct stat sb;
2202 *npackfiles = 0;
2203 *nobjects = 0;
2204 *total_packsize = 0;
2206 packdir_fd = openat(got_repo_get_fd(repo),
2207 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2208 if (packdir_fd == -1) {
2209 return got_error_from_errno_fmt("openat: %s/%s",
2210 got_repo_get_path_git_dir(repo),
2211 GOT_OBJECTS_PACK_DIR);
2214 packdir = fdopendir(packdir_fd);
2215 if (packdir == NULL) {
2216 err = got_error_from_errno("fdopendir");
2217 goto done;
2220 while ((dent = readdir(packdir)) != NULL) {
2221 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2222 continue;
2224 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2225 dent->d_name) == -1) {
2226 err = got_error_from_errno("asprintf");
2227 goto done;
2230 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2231 path_packidx, 0);
2232 free(path_packidx);
2233 if (err)
2234 goto done;
2236 if (fstat(packidx->fd, &sb) == -1)
2237 goto done;
2238 *total_packsize += sb.st_size;
2240 err = got_packidx_get_packfile_path(&path_packfile,
2241 packidx->path_packidx);
2242 if (err)
2243 goto done;
2245 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2246 0) == -1) {
2247 free(path_packfile);
2248 goto done;
2250 free(path_packfile);
2251 *total_packsize += sb.st_size;
2253 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2255 (*npackfiles)++;
2257 got_packidx_close(packidx);
2258 packidx = NULL;
2260 done:
2261 if (packidx)
2262 got_packidx_close(packidx);
2263 if (packdir && closedir(packdir) != 0 && err == NULL)
2264 err = got_error_from_errno("closedir");
2265 if (err) {
2266 *npackfiles = 0;
2267 *nobjects = 0;
2268 *total_packsize = 0;
2270 return err;
2273 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2274 got_packidx_bloom_filter_cmp);