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;
755 repo->pinned_pack = -1;
756 repo->pinned_packidx = -1;
757 repo->pinned_pid = 0;
759 repo_path = realpath(path, NULL);
760 if (repo_path == NULL) {
761 err = got_error_from_errno2("realpath", path);
762 goto done;
765 for (;;) {
766 char *parent_path;
768 err = open_repo(repo, repo_path);
769 if (err == NULL)
770 break;
771 if (err->code != GOT_ERR_NOT_GIT_REPO)
772 goto done;
773 if (repo_path[0] == '/' && repo_path[1] == '\0') {
774 err = got_error(GOT_ERR_NOT_GIT_REPO);
775 goto done;
777 err = got_path_dirname(&parent_path, repo_path);
778 if (err)
779 goto done;
780 free(repo_path);
781 repo_path = parent_path;
784 err = read_gotconfig(repo);
785 if (err)
786 goto done;
788 err = read_gitconfig(repo, global_gitconfig_path);
789 if (err)
790 goto done;
791 if (repo->gitconfig_repository_format_version != 0)
792 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
793 for (i = 0; i < repo->nextensions; i++) {
794 char *ext = repo->extensions[i];
795 int j, supported = 0;
796 for (j = 0; j < nitems(repo_extensions); j++) {
797 if (strcmp(ext, repo_extensions[j]) == 0) {
798 supported = 1;
799 break;
802 if (!supported) {
803 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
804 goto done;
808 err = got_repo_list_packidx(&repo->packidx_paths, repo);
809 done:
810 if (err)
811 got_repo_close(repo);
812 else
813 *repop = repo;
814 free(repo_path);
815 return err;
818 const struct got_error *
819 got_repo_close(struct got_repository *repo)
821 const struct got_error *err = NULL, *child_err;
822 struct got_packidx_bloom_filter *bf;
823 struct got_pathlist_entry *pe;
824 size_t i;
826 for (i = 0; i < repo->pack_cache_size; i++) {
827 if (repo->packidx_cache[i] == NULL)
828 break;
829 got_packidx_close(repo->packidx_cache[i]);
832 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
833 &repo->packidx_bloom_filters))) {
834 RB_REMOVE(got_packidx_bloom_filter_tree,
835 &repo->packidx_bloom_filters, bf);
836 free(bf->bloom);
837 free(bf);
840 for (i = 0; i < repo->pack_cache_size; i++)
841 if (repo->packs[i].path_packfile)
842 if (repo->packs[i].path_packfile)
843 got_pack_close(&repo->packs[i]);
845 free(repo->path);
846 free(repo->path_git_dir);
848 got_object_cache_close(&repo->objcache);
849 got_object_cache_close(&repo->treecache);
850 got_object_cache_close(&repo->commitcache);
851 got_object_cache_close(&repo->tagcache);
852 got_object_cache_close(&repo->rawcache);
854 for (i = 0; i < nitems(repo->privsep_children); i++) {
855 if (repo->privsep_children[i].imsg_fd == -1)
856 continue;
857 imsg_clear(repo->privsep_children[i].ibuf);
858 free(repo->privsep_children[i].ibuf);
859 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
860 child_err = got_privsep_wait_for_child(
861 repo->privsep_children[i].pid);
862 if (child_err && err == NULL)
863 err = child_err;
864 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
865 err == NULL)
866 err = got_error_from_errno("close");
869 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
870 err == NULL)
871 err = got_error_from_errno("close");
873 if (repo->gotconfig)
874 got_gotconfig_free(repo->gotconfig);
875 free(repo->gitconfig_author_name);
876 free(repo->gitconfig_author_email);
877 for (i = 0; i < repo->ngitconfig_remotes; i++)
878 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
879 free(repo->gitconfig_remotes);
880 for (i = 0; i < repo->nextensions; i++)
881 free(repo->extensions[i]);
882 free(repo->extensions);
884 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
885 free((void *)pe->path);
886 got_pathlist_free(&repo->packidx_paths);
887 free(repo);
889 return err;
892 void
893 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
895 int i;
897 free(repo->name);
898 repo->name = NULL;
899 free(repo->fetch_url);
900 repo->fetch_url = NULL;
901 free(repo->send_url);
902 repo->send_url = NULL;
903 for (i = 0; i < repo->nfetch_branches; i++)
904 free(repo->fetch_branches[i]);
905 free(repo->fetch_branches);
906 repo->fetch_branches = NULL;
907 repo->nfetch_branches = 0;
908 for (i = 0; i < repo->nsend_branches; i++)
909 free(repo->send_branches[i]);
910 free(repo->send_branches);
911 repo->send_branches = NULL;
912 repo->nsend_branches = 0;
915 const struct got_error *
916 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
917 const char *input_path)
919 const struct got_error *err = NULL;
920 const char *repo_abspath = NULL;
921 size_t repolen, len;
922 char *canonpath, *path = NULL;
924 *in_repo_path = NULL;
926 canonpath = strdup(input_path);
927 if (canonpath == NULL) {
928 err = got_error_from_errno("strdup");
929 goto done;
931 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
932 if (err)
933 goto done;
935 repo_abspath = got_repo_get_path(repo);
937 if (canonpath[0] == '\0') {
938 path = strdup(canonpath);
939 if (path == NULL) {
940 err = got_error_from_errno("strdup");
941 goto done;
943 } else {
944 path = realpath(canonpath, NULL);
945 if (path == NULL) {
946 if (errno != ENOENT) {
947 err = got_error_from_errno2("realpath",
948 canonpath);
949 goto done;
951 /*
952 * Path is not on disk.
953 * Assume it is already relative to repository root.
954 */
955 path = strdup(canonpath);
956 if (path == NULL) {
957 err = got_error_from_errno("strdup");
958 goto done;
962 repolen = strlen(repo_abspath);
963 len = strlen(path);
966 if (strcmp(path, repo_abspath) == 0) {
967 free(path);
968 path = strdup("");
969 if (path == NULL) {
970 err = got_error_from_errno("strdup");
971 goto done;
973 } else if (len > repolen &&
974 got_path_is_child(path, repo_abspath, repolen)) {
975 /* Matched an on-disk path inside repository. */
976 if (got_repo_is_bare(repo)) {
977 /*
978 * Matched an on-disk path inside repository
979 * database. Treat input as repository-relative.
980 */
981 free(path);
982 path = canonpath;
983 canonpath = NULL;
984 } else {
985 char *child;
986 /* Strip common prefix with repository path. */
987 err = got_path_skip_common_ancestor(&child,
988 repo_abspath, path);
989 if (err)
990 goto done;
991 free(path);
992 path = child;
994 } else {
995 /*
996 * Matched unrelated on-disk path.
997 * Treat input as repository-relative.
998 */
999 free(path);
1000 path = canonpath;
1001 canonpath = NULL;
1005 /* Make in-repository path absolute */
1006 if (path[0] != '/') {
1007 char *abspath;
1008 if (asprintf(&abspath, "/%s", path) == -1) {
1009 err = got_error_from_errno("asprintf");
1010 goto done;
1012 free(path);
1013 path = abspath;
1016 done:
1017 free(canonpath);
1018 if (err)
1019 free(path);
1020 else
1021 *in_repo_path = path;
1022 return err;
1025 static const struct got_error *
1026 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1027 const char *path_packidx)
1029 const struct got_error *err = NULL;
1030 size_t i;
1032 for (i = 0; i < repo->pack_cache_size; i++) {
1033 if (repo->packidx_cache[i] == NULL)
1034 break;
1035 if (strcmp(repo->packidx_cache[i]->path_packidx,
1036 path_packidx) == 0) {
1037 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1040 if (i == repo->pack_cache_size) {
1041 do {
1042 i--;
1043 } while (i > 0 && repo->pinned_packidx >= 0 &&
1044 i == repo->pinned_packidx);
1045 err = got_packidx_close(repo->packidx_cache[i]);
1046 if (err)
1047 return err;
1050 repo->packidx_cache[i] = packidx;
1052 return NULL;
1055 int
1056 got_repo_is_packidx_filename(const char *name, size_t len)
1058 if (len != GOT_PACKIDX_NAMELEN)
1059 return 0;
1061 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1062 return 0;
1064 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1065 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1066 return 0;
1068 return 1;
1071 static struct got_packidx_bloom_filter *
1072 get_packidx_bloom_filter(struct got_repository *repo,
1073 const char *path, size_t path_len)
1075 struct got_packidx_bloom_filter key;
1077 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1078 return NULL; /* XXX */
1079 key.path_len = path_len;
1081 return RB_FIND(got_packidx_bloom_filter_tree,
1082 &repo->packidx_bloom_filters, &key);
1085 int
1086 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1087 const char *path_packidx, struct got_object_id *id)
1089 struct got_packidx_bloom_filter *bf;
1091 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1092 if (bf)
1093 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1095 /* No bloom filter means this pack index must be searched. */
1096 return 1;
1099 static const struct got_error *
1100 add_packidx_bloom_filter(struct got_repository *repo,
1101 struct got_packidx *packidx, const char *path_packidx)
1103 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1104 struct got_packidx_bloom_filter *bf;
1105 size_t len;
1108 * Don't use bloom filters for very large pack index files.
1109 * Large pack files will contain a relatively large fraction
1110 * of our objects so we will likely need to visit them anyway.
1111 * The more objects a pack file contains the higher the probability
1112 * of a false-positive match from the bloom filter. And reading
1113 * all object IDs from a large pack index file can be expensive.
1115 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1116 return NULL;
1118 /* Do we already have a filter for this pack index? */
1119 if (get_packidx_bloom_filter(repo, path_packidx,
1120 strlen(path_packidx)) != NULL)
1121 return NULL;
1123 bf = calloc(1, sizeof(*bf));
1124 if (bf == NULL)
1125 return got_error_from_errno("calloc");
1126 bf->bloom = calloc(1, sizeof(*bf->bloom));
1127 if (bf->bloom == NULL) {
1128 free(bf);
1129 return got_error_from_errno("calloc");
1132 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1133 if (len >= sizeof(bf->path)) {
1134 free(bf->bloom);
1135 free(bf);
1136 return got_error(GOT_ERR_NO_SPACE);
1138 bf->path_len = len;
1140 /* Minimum size supported by our bloom filter is 1000 entries. */
1141 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1142 for (i = 0; i < nobjects; i++) {
1143 struct got_packidx_object_id *id;
1144 id = &packidx->hdr.sorted_ids[i];
1145 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1148 RB_INSERT(got_packidx_bloom_filter_tree,
1149 &repo->packidx_bloom_filters, bf);
1150 return NULL;
1153 const struct got_error *
1154 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1155 struct got_repository *repo, struct got_object_id *id)
1157 const struct got_error *err;
1158 struct got_pathlist_entry *pe;
1159 size_t i;
1161 /* Search pack index cache. */
1162 for (i = 0; i < repo->pack_cache_size; i++) {
1163 if (repo->packidx_cache[i] == NULL)
1164 break;
1165 if (!got_repo_check_packidx_bloom_filter(repo,
1166 repo->packidx_cache[i]->path_packidx, id))
1167 continue; /* object will not be found in this index */
1168 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1169 if (*idx != -1) {
1170 *packidx = repo->packidx_cache[i];
1172 * Move this cache entry to the front. Repeatedly
1173 * searching a wrong pack index can be expensive.
1175 if (i > 0) {
1176 memmove(&repo->packidx_cache[1],
1177 &repo->packidx_cache[0],
1178 i * sizeof(repo->packidx_cache[0]));
1179 repo->packidx_cache[0] = *packidx;
1180 if (repo->pinned_packidx >= 0 &&
1181 repo->pinned_packidx < i)
1182 repo->pinned_packidx++;
1183 else if (repo->pinned_packidx == i)
1184 repo->pinned_packidx = 0;
1186 return NULL;
1189 /* No luck. Search the filesystem. */
1191 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1192 const char *path_packidx = pe->path;
1193 int is_cached = 0;
1195 if (!got_repo_check_packidx_bloom_filter(repo,
1196 pe->path, id))
1197 continue; /* object will not be found in this index */
1199 for (i = 0; i < repo->pack_cache_size; i++) {
1200 if (repo->packidx_cache[i] == NULL)
1201 break;
1202 if (strcmp(repo->packidx_cache[i]->path_packidx,
1203 path_packidx) == 0) {
1204 is_cached = 1;
1205 break;
1208 if (is_cached)
1209 continue; /* already searched */
1211 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1212 path_packidx, 0);
1213 if (err)
1214 goto done;
1216 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1217 if (err)
1218 goto done;
1220 err = cache_packidx(repo, *packidx, path_packidx);
1221 if (err)
1222 goto done;
1224 *idx = got_packidx_get_object_idx(*packidx, id);
1225 if (*idx != -1) {
1226 err = NULL; /* found the object */
1227 goto done;
1231 err = got_error_no_obj(id);
1232 done:
1233 return err;
1236 const struct got_error *
1237 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1238 struct got_repository *repo)
1240 const struct got_error *err = NULL;
1241 DIR *packdir = NULL;
1242 struct dirent *dent;
1243 char *path_packidx = NULL;
1244 int packdir_fd;
1246 packdir_fd = openat(got_repo_get_fd(repo),
1247 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1248 if (packdir_fd == -1) {
1249 return got_error_from_errno_fmt("openat: %s/%s",
1250 got_repo_get_path_git_dir(repo),
1251 GOT_OBJECTS_PACK_DIR);
1254 packdir = fdopendir(packdir_fd);
1255 if (packdir == NULL) {
1256 err = got_error_from_errno("fdopendir");
1257 goto done;
1260 while ((dent = readdir(packdir)) != NULL) {
1261 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1262 continue;
1264 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1265 dent->d_name) == -1) {
1266 err = got_error_from_errno("asprintf");
1267 path_packidx = NULL;
1268 break;
1271 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1272 if (err)
1273 break;
1275 done:
1276 if (err)
1277 free(path_packidx);
1278 if (packdir && closedir(packdir) != 0 && err == NULL)
1279 err = got_error_from_errno("closedir");
1280 return err;
1283 const struct got_error *
1284 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1285 struct got_repository *repo)
1287 const struct got_error *err;
1288 size_t i;
1290 *packidx = NULL;
1292 /* Search pack index cache. */
1293 for (i = 0; i < repo->pack_cache_size; i++) {
1294 if (repo->packidx_cache[i] == NULL)
1295 break;
1296 if (strcmp(repo->packidx_cache[i]->path_packidx,
1297 path_packidx) == 0) {
1298 *packidx = repo->packidx_cache[i];
1299 return NULL;
1302 /* No luck. Search the filesystem. */
1304 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1305 path_packidx, 0);
1306 if (err)
1307 return err;
1309 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1310 if (err)
1311 goto done;
1313 err = cache_packidx(repo, *packidx, path_packidx);
1314 done:
1315 if (err) {
1316 got_packidx_close(*packidx);
1317 *packidx = NULL;
1319 return err;
1322 static const struct got_error *
1323 read_packfile_hdr(int fd, struct got_packidx *packidx)
1325 const struct got_error *err = NULL;
1326 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1327 struct got_packfile_hdr hdr;
1328 ssize_t n;
1330 n = read(fd, &hdr, sizeof(hdr));
1331 if (n < 0)
1332 return got_error_from_errno("read");
1333 if (n != sizeof(hdr))
1334 return got_error(GOT_ERR_BAD_PACKFILE);
1336 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1337 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1338 be32toh(hdr.nobjects) != totobj)
1339 err = got_error(GOT_ERR_BAD_PACKFILE);
1341 return err;
1344 static const struct got_error *
1345 open_packfile(int *fd, struct got_repository *repo,
1346 const char *relpath, struct got_packidx *packidx)
1348 const struct got_error *err = NULL;
1350 *fd = openat(got_repo_get_fd(repo), relpath,
1351 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1352 if (*fd == -1)
1353 return got_error_from_errno_fmt("openat: %s/%s",
1354 got_repo_get_path_git_dir(repo), relpath);
1356 if (packidx) {
1357 err = read_packfile_hdr(*fd, packidx);
1358 if (err) {
1359 close(*fd);
1360 *fd = -1;
1364 return err;
1367 const struct got_error *
1368 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1369 const char *path_packfile, struct got_packidx *packidx)
1371 const struct got_error *err = NULL;
1372 struct got_pack *pack = NULL;
1373 struct stat sb;
1374 size_t i;
1376 if (packp)
1377 *packp = NULL;
1379 for (i = 0; i < repo->pack_cache_size; i++) {
1380 pack = &repo->packs[i];
1381 if (pack->path_packfile == NULL)
1382 break;
1383 if (strcmp(pack->path_packfile, path_packfile) == 0)
1384 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1387 if (i == repo->pack_cache_size) {
1388 struct got_pack tmp;
1389 do {
1390 i--;
1391 } while (i > 0 && repo->pinned_pack >= 0 &&
1392 i == repo->pinned_pack);
1393 err = got_pack_close(&repo->packs[i]);
1394 if (err)
1395 return err;
1396 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1397 return got_error_from_errno("ftruncate");
1398 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1399 return got_error_from_errno("ftruncate");
1400 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1401 memcpy(&repo->packs[i], &repo->packs[0],
1402 sizeof(repo->packs[i]));
1403 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1404 if (repo->pinned_pack == 0)
1405 repo->pinned_pack = i;
1406 else if (repo->pinned_pack == i)
1407 repo->pinned_pack = 0;
1408 i = 0;
1411 pack = &repo->packs[i];
1413 pack->path_packfile = strdup(path_packfile);
1414 if (pack->path_packfile == NULL) {
1415 err = got_error_from_errno("strdup");
1416 goto done;
1419 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1420 if (err)
1421 goto done;
1423 if (fstat(pack->fd, &sb) != 0) {
1424 err = got_error_from_errno("fstat");
1425 goto done;
1427 pack->filesize = sb.st_size;
1429 pack->privsep_child = NULL;
1431 #ifndef GOT_PACK_NO_MMAP
1432 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1433 pack->fd, 0);
1434 if (pack->map == MAP_FAILED) {
1435 if (errno != ENOMEM) {
1436 err = got_error_from_errno("mmap");
1437 goto done;
1439 pack->map = NULL; /* fall back to read(2) */
1441 #endif
1442 done:
1443 if (err) {
1444 if (pack) {
1445 free(pack->path_packfile);
1446 memset(pack, 0, sizeof(*pack));
1448 } else if (packp)
1449 *packp = pack;
1450 return err;
1453 struct got_pack *
1454 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1456 struct got_pack *pack = NULL;
1457 size_t i;
1459 for (i = 0; i < repo->pack_cache_size; i++) {
1460 pack = &repo->packs[i];
1461 if (pack->path_packfile == NULL)
1462 break;
1463 if (strcmp(pack->path_packfile, path_packfile) == 0)
1464 return pack;
1467 return NULL;
1470 const struct got_error *
1471 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1472 struct got_pack *pack)
1474 size_t i;
1475 int pinned_pack = -1, pinned_packidx = -1;
1477 for (i = 0; i < repo->pack_cache_size; i++) {
1478 if (repo->packidx_cache[i] &&
1479 strcmp(repo->packidx_cache[i]->path_packidx,
1480 packidx->path_packidx) == 0)
1481 pinned_packidx = i;
1482 if (repo->packs[i].path_packfile &&
1483 strcmp(repo->packs[i].path_packfile,
1484 pack->path_packfile) == 0)
1485 pinned_pack = i;
1488 if (pinned_packidx == -1 || pinned_pack == -1)
1489 return got_error(GOT_ERR_PIN_PACK);
1491 repo->pinned_pack = pinned_pack;
1492 repo->pinned_packidx = pinned_packidx;
1493 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1494 return NULL;
1497 struct got_pack *
1498 got_repo_get_pinned_pack(struct got_repository *repo)
1500 if (repo->pinned_pack >= 0 &&
1501 repo->pinned_pack < repo->pack_cache_size)
1502 return &repo->packs[repo->pinned_pack];
1504 return NULL;
1507 void
1508 got_repo_unpin_pack(struct got_repository *repo)
1510 repo->pinned_packidx = -1;
1511 repo->pinned_pack = -1;
1512 repo->pinned_pid = 0;
1515 const struct got_error *
1516 got_repo_init(const char *repo_path)
1518 const struct got_error *err = NULL;
1519 const char *dirnames[] = {
1520 GOT_OBJECTS_DIR,
1521 GOT_OBJECTS_PACK_DIR,
1522 GOT_REFS_DIR,
1524 const char *description_str = "Unnamed repository; "
1525 "edit this file 'description' to name the repository.";
1526 const char *headref_str = "ref: refs/heads/main";
1527 const char *gitconfig_str = "[core]\n"
1528 "\trepositoryformatversion = 0\n"
1529 "\tfilemode = true\n"
1530 "\tbare = true\n";
1531 char *path;
1532 size_t i;
1534 if (!got_path_dir_is_empty(repo_path))
1535 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1537 for (i = 0; i < nitems(dirnames); i++) {
1538 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1539 return got_error_from_errno("asprintf");
1541 err = got_path_mkdir(path);
1542 free(path);
1543 if (err)
1544 return err;
1547 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1548 return got_error_from_errno("asprintf");
1549 err = got_path_create_file(path, description_str);
1550 free(path);
1551 if (err)
1552 return err;
1554 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1555 return got_error_from_errno("asprintf");
1556 err = got_path_create_file(path, headref_str);
1557 free(path);
1558 if (err)
1559 return err;
1561 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1562 return got_error_from_errno("asprintf");
1563 err = got_path_create_file(path, gitconfig_str);
1564 free(path);
1565 if (err)
1566 return err;
1568 return NULL;
1571 static const struct got_error *
1572 match_packed_object(struct got_object_id **unique_id,
1573 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1575 const struct got_error *err = NULL;
1576 struct got_object_id_queue matched_ids;
1577 struct got_pathlist_entry *pe;
1579 STAILQ_INIT(&matched_ids);
1581 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1582 const char *path_packidx = pe->path;
1583 struct got_packidx *packidx;
1584 struct got_object_qid *qid;
1586 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1587 path_packidx, 0);
1588 if (err)
1589 break;
1591 err = got_packidx_match_id_str_prefix(&matched_ids,
1592 packidx, id_str_prefix);
1593 if (err) {
1594 got_packidx_close(packidx);
1595 break;
1597 err = got_packidx_close(packidx);
1598 if (err)
1599 break;
1601 STAILQ_FOREACH(qid, &matched_ids, entry) {
1602 if (obj_type != GOT_OBJ_TYPE_ANY) {
1603 int matched_type;
1604 err = got_object_get_type(&matched_type, repo,
1605 &qid->id);
1606 if (err)
1607 goto done;
1608 if (matched_type != obj_type)
1609 continue;
1611 if (*unique_id == NULL) {
1612 *unique_id = got_object_id_dup(&qid->id);
1613 if (*unique_id == NULL) {
1614 err = got_error_from_errno("malloc");
1615 goto done;
1617 } else {
1618 if (got_object_id_cmp(*unique_id,
1619 &qid->id) == 0)
1620 continue; /* packed multiple times */
1621 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1622 goto done;
1626 done:
1627 got_object_id_queue_free(&matched_ids);
1628 if (err) {
1629 free(*unique_id);
1630 *unique_id = NULL;
1632 return err;
1635 static const struct got_error *
1636 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1637 const char *object_dir, const char *id_str_prefix, int obj_type,
1638 struct got_repository *repo)
1640 const struct got_error *err = NULL;
1641 char *path;
1642 DIR *dir = NULL;
1643 struct dirent *dent;
1644 struct got_object_id id;
1646 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1647 err = got_error_from_errno("asprintf");
1648 goto done;
1651 dir = opendir(path);
1652 if (dir == NULL) {
1653 if (errno == ENOENT) {
1654 err = NULL;
1655 goto done;
1657 err = got_error_from_errno2("opendir", path);
1658 goto done;
1660 while ((dent = readdir(dir)) != NULL) {
1661 char *id_str;
1662 int cmp;
1664 if (strcmp(dent->d_name, ".") == 0 ||
1665 strcmp(dent->d_name, "..") == 0)
1666 continue;
1668 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1669 err = got_error_from_errno("asprintf");
1670 goto done;
1673 if (!got_parse_sha1_digest(id.sha1, id_str))
1674 continue;
1677 * Directory entries do not necessarily appear in
1678 * sorted order, so we must iterate over all of them.
1680 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1681 if (cmp != 0) {
1682 free(id_str);
1683 continue;
1686 if (*unique_id == NULL) {
1687 if (obj_type != GOT_OBJ_TYPE_ANY) {
1688 int matched_type;
1689 err = got_object_get_type(&matched_type, repo,
1690 &id);
1691 if (err)
1692 goto done;
1693 if (matched_type != obj_type)
1694 continue;
1696 *unique_id = got_object_id_dup(&id);
1697 if (*unique_id == NULL) {
1698 err = got_error_from_errno("got_object_id_dup");
1699 free(id_str);
1700 goto done;
1702 } else {
1703 if (got_object_id_cmp(*unique_id, &id) == 0)
1704 continue; /* both packed and loose */
1705 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1706 free(id_str);
1707 goto done;
1710 done:
1711 if (dir && closedir(dir) != 0 && err == NULL)
1712 err = got_error_from_errno("closedir");
1713 if (err) {
1714 free(*unique_id);
1715 *unique_id = NULL;
1717 free(path);
1718 return err;
1721 const struct got_error *
1722 got_repo_match_object_id_prefix(struct got_object_id **id,
1723 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1725 const struct got_error *err = NULL;
1726 char *path_objects = got_repo_get_path_objects(repo);
1727 char *object_dir = NULL;
1728 size_t len;
1729 int i;
1731 *id = NULL;
1733 len = strlen(id_str_prefix);
1734 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1735 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1737 for (i = 0; i < len; i++) {
1738 if (isxdigit((unsigned char)id_str_prefix[i]))
1739 continue;
1740 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1743 if (len >= 2) {
1744 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1745 if (err)
1746 goto done;
1747 object_dir = strndup(id_str_prefix, 2);
1748 if (object_dir == NULL) {
1749 err = got_error_from_errno("strdup");
1750 goto done;
1752 err = match_loose_object(id, path_objects, object_dir,
1753 id_str_prefix, obj_type, repo);
1754 } else if (len == 1) {
1755 int i;
1756 for (i = 0; i < 0xf; i++) {
1757 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1758 == -1) {
1759 err = got_error_from_errno("asprintf");
1760 goto done;
1762 err = match_packed_object(id, repo, object_dir,
1763 obj_type);
1764 if (err)
1765 goto done;
1766 err = match_loose_object(id, path_objects, object_dir,
1767 id_str_prefix, obj_type, repo);
1768 if (err)
1769 goto done;
1771 } else {
1772 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1773 goto done;
1775 done:
1776 free(object_dir);
1777 if (err) {
1778 free(*id);
1779 *id = NULL;
1780 } else if (*id == NULL) {
1781 switch (obj_type) {
1782 case GOT_OBJ_TYPE_BLOB:
1783 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1784 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1785 break;
1786 case GOT_OBJ_TYPE_TREE:
1787 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1788 GOT_OBJ_LABEL_TREE, id_str_prefix);
1789 break;
1790 case GOT_OBJ_TYPE_COMMIT:
1791 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1792 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1793 break;
1794 case GOT_OBJ_TYPE_TAG:
1795 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1796 GOT_OBJ_LABEL_TAG, id_str_prefix);
1797 break;
1798 default:
1799 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1800 break;
1804 return err;
1807 const struct got_error *
1808 got_repo_match_object_id(struct got_object_id **id, char **label,
1809 const char *id_str, int obj_type, struct got_reflist_head *refs,
1810 struct got_repository *repo)
1812 const struct got_error *err;
1813 struct got_tag_object *tag;
1814 struct got_reference *ref = NULL;
1816 *id = NULL;
1817 if (label)
1818 *label = NULL;
1820 if (refs) {
1821 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1822 refs, repo);
1823 if (err == NULL) {
1824 *id = got_object_id_dup(
1825 got_object_tag_get_object_id(tag));
1826 if (*id == NULL)
1827 err = got_error_from_errno("got_object_id_dup");
1828 else if (label && asprintf(label, "refs/tags/%s",
1829 got_object_tag_get_name(tag)) == -1) {
1830 err = got_error_from_errno("asprintf");
1831 free(*id);
1832 *id = NULL;
1834 got_object_tag_close(tag);
1835 return err;
1836 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1837 err->code != GOT_ERR_NO_OBJ)
1838 return err;
1841 err = got_ref_open(&ref, repo, id_str, 0);
1842 if (err == NULL) {
1843 err = got_ref_resolve(id, repo, ref);
1844 if (err)
1845 goto done;
1846 if (label) {
1847 *label = strdup(got_ref_get_name(ref));
1848 if (*label == NULL) {
1849 err = got_error_from_errno("strdup");
1850 goto done;
1853 } else {
1854 if (err->code != GOT_ERR_NOT_REF &&
1855 err->code != GOT_ERR_BAD_REF_NAME)
1856 goto done;
1857 err = got_repo_match_object_id_prefix(id, id_str,
1858 obj_type, repo);
1859 if (err) {
1860 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1861 err = got_error_not_ref(id_str);
1862 goto done;
1864 if (label) {
1865 err = got_object_id_str(label, *id);
1866 if (*label == NULL) {
1867 err = got_error_from_errno("strdup");
1868 goto done;
1872 done:
1873 if (ref)
1874 got_ref_close(ref);
1875 return err;
1878 const struct got_error *
1879 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1880 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1882 const struct got_error *err = NULL;
1883 struct got_reflist_entry *re;
1884 struct got_object_id *tag_id;
1885 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1887 *tag = NULL;
1889 TAILQ_FOREACH(re, refs, entry) {
1890 const char *refname;
1891 refname = got_ref_get_name(re->ref);
1892 if (got_ref_is_symbolic(re->ref))
1893 continue;
1894 if (strncmp(refname, "refs/tags/", 10) != 0)
1895 continue;
1896 if (!name_is_absolute)
1897 refname += strlen("refs/tags/");
1898 if (strcmp(refname, name) != 0)
1899 continue;
1900 err = got_ref_resolve(&tag_id, repo, re->ref);
1901 if (err)
1902 break;
1903 err = got_object_open_as_tag(tag, repo, tag_id);
1904 free(tag_id);
1905 if (err)
1906 break;
1907 if (obj_type == GOT_OBJ_TYPE_ANY ||
1908 got_object_tag_get_object_type(*tag) == obj_type)
1909 break;
1910 got_object_tag_close(*tag);
1911 *tag = NULL;
1914 if (err == NULL && *tag == NULL)
1915 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1916 GOT_OBJ_LABEL_TAG, name);
1917 return err;
1920 static const struct got_error *
1921 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1922 const char *name, mode_t mode, struct got_object_id *blob_id)
1924 const struct got_error *err = NULL;
1926 *new_te = NULL;
1928 *new_te = calloc(1, sizeof(**new_te));
1929 if (*new_te == NULL)
1930 return got_error_from_errno("calloc");
1932 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1933 sizeof((*new_te)->name)) {
1934 err = got_error(GOT_ERR_NO_SPACE);
1935 goto done;
1938 if (S_ISLNK(mode)) {
1939 (*new_te)->mode = S_IFLNK;
1940 } else {
1941 (*new_te)->mode = S_IFREG;
1942 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1944 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1945 done:
1946 if (err && *new_te) {
1947 free(*new_te);
1948 *new_te = NULL;
1950 return err;
1953 static const struct got_error *
1954 import_file(struct got_tree_entry **new_te, struct dirent *de,
1955 const char *path, struct got_repository *repo)
1957 const struct got_error *err;
1958 struct got_object_id *blob_id = NULL;
1959 char *filepath;
1960 struct stat sb;
1962 if (asprintf(&filepath, "%s%s%s", path,
1963 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1964 return got_error_from_errno("asprintf");
1966 if (lstat(filepath, &sb) != 0) {
1967 err = got_error_from_errno2("lstat", path);
1968 goto done;
1971 err = got_object_blob_create(&blob_id, filepath, repo);
1972 if (err)
1973 goto done;
1975 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1976 blob_id);
1977 done:
1978 free(filepath);
1979 if (err)
1980 free(blob_id);
1981 return err;
1984 static const struct got_error *
1985 insert_tree_entry(struct got_tree_entry *new_te,
1986 struct got_pathlist_head *paths)
1988 const struct got_error *err = NULL;
1989 struct got_pathlist_entry *new_pe;
1991 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1992 if (err)
1993 return err;
1994 if (new_pe == NULL)
1995 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1996 return NULL;
1999 static const struct got_error *write_tree(struct got_object_id **,
2000 const char *, struct got_pathlist_head *, struct got_repository *,
2001 got_repo_import_cb progress_cb, void *progress_arg);
2003 static const struct got_error *
2004 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2005 const char *path, struct got_pathlist_head *ignores,
2006 struct got_repository *repo,
2007 got_repo_import_cb progress_cb, void *progress_arg)
2009 const struct got_error *err;
2010 struct got_object_id *id = NULL;
2011 char *subdirpath;
2013 if (asprintf(&subdirpath, "%s%s%s", path,
2014 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2015 return got_error_from_errno("asprintf");
2017 (*new_te) = calloc(1, sizeof(**new_te));
2018 if (*new_te == NULL)
2019 return got_error_from_errno("calloc");
2020 (*new_te)->mode = S_IFDIR;
2021 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2022 sizeof((*new_te)->name)) {
2023 err = got_error(GOT_ERR_NO_SPACE);
2024 goto done;
2026 err = write_tree(&id, subdirpath, ignores, repo,
2027 progress_cb, progress_arg);
2028 if (err)
2029 goto done;
2030 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2032 done:
2033 free(id);
2034 free(subdirpath);
2035 if (err) {
2036 free(*new_te);
2037 *new_te = NULL;
2039 return err;
2042 static const struct got_error *
2043 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2044 struct got_pathlist_head *ignores, struct got_repository *repo,
2045 got_repo_import_cb progress_cb, void *progress_arg)
2047 const struct got_error *err = NULL;
2048 DIR *dir;
2049 struct dirent *de;
2050 int nentries;
2051 struct got_tree_entry *new_te = NULL;
2052 struct got_pathlist_head paths;
2053 struct got_pathlist_entry *pe;
2055 *new_tree_id = NULL;
2057 TAILQ_INIT(&paths);
2059 dir = opendir(path_dir);
2060 if (dir == NULL) {
2061 err = got_error_from_errno2("opendir", path_dir);
2062 goto done;
2065 nentries = 0;
2066 while ((de = readdir(dir)) != NULL) {
2067 int ignore = 0;
2068 int type;
2070 if (strcmp(de->d_name, ".") == 0 ||
2071 strcmp(de->d_name, "..") == 0)
2072 continue;
2074 TAILQ_FOREACH(pe, ignores, entry) {
2075 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2076 ignore = 1;
2077 break;
2080 if (ignore)
2081 continue;
2083 err = got_path_dirent_type(&type, path_dir, de);
2084 if (err)
2085 goto done;
2087 if (type == DT_DIR) {
2088 err = import_subdir(&new_te, de, path_dir,
2089 ignores, repo, progress_cb, progress_arg);
2090 if (err) {
2091 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2092 goto done;
2093 err = NULL;
2094 continue;
2096 } else if (type == DT_REG || type == DT_LNK) {
2097 err = import_file(&new_te, de, path_dir, repo);
2098 if (err)
2099 goto done;
2100 } else
2101 continue;
2103 err = insert_tree_entry(new_te, &paths);
2104 if (err)
2105 goto done;
2106 nentries++;
2109 if (TAILQ_EMPTY(&paths)) {
2110 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2111 "cannot create tree without any entries");
2112 goto done;
2115 TAILQ_FOREACH(pe, &paths, entry) {
2116 struct got_tree_entry *te = pe->data;
2117 char *path;
2118 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2119 continue;
2120 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2121 err = got_error_from_errno("asprintf");
2122 goto done;
2124 err = (*progress_cb)(progress_arg, path);
2125 free(path);
2126 if (err)
2127 goto done;
2130 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2131 done:
2132 if (dir)
2133 closedir(dir);
2134 got_pathlist_free(&paths);
2135 return err;
2138 const struct got_error *
2139 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2140 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2141 struct got_repository *repo, got_repo_import_cb progress_cb,
2142 void *progress_arg)
2144 const struct got_error *err;
2145 struct got_object_id *new_tree_id;
2147 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2148 progress_cb, progress_arg);
2149 if (err)
2150 return err;
2152 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2153 author, time(NULL), author, time(NULL), logmsg, repo);
2154 free(new_tree_id);
2155 return err;
2158 const struct got_error *
2159 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2160 struct got_repository *repo)
2162 const struct got_error *err = NULL;
2163 char *path_objects = NULL, *path = NULL;
2164 DIR *dir = NULL;
2165 struct got_object_id id;
2166 int i;
2168 *nobjects = 0;
2169 *ondisk_size = 0;
2171 path_objects = got_repo_get_path_objects(repo);
2172 if (path_objects == NULL)
2173 return got_error_from_errno("got_repo_get_path_objects");
2175 for (i = 0; i <= 0xff; i++) {
2176 struct dirent *dent;
2178 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2179 err = got_error_from_errno("asprintf");
2180 break;
2183 dir = opendir(path);
2184 if (dir == NULL) {
2185 if (errno == ENOENT) {
2186 err = NULL;
2187 continue;
2189 err = got_error_from_errno2("opendir", path);
2190 break;
2193 while ((dent = readdir(dir)) != NULL) {
2194 char *id_str;
2195 int fd;
2196 struct stat sb;
2198 if (strcmp(dent->d_name, ".") == 0 ||
2199 strcmp(dent->d_name, "..") == 0)
2200 continue;
2202 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2203 err = got_error_from_errno("asprintf");
2204 goto done;
2207 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2208 free(id_str);
2209 continue;
2211 free(id_str);
2213 err = got_object_open_loose_fd(&fd, &id, repo);
2214 if (err)
2215 goto done;
2217 if (fstat(fd, &sb) == -1) {
2218 err = got_error_from_errno("fstat");
2219 close(fd);
2220 goto done;
2222 (*nobjects)++;
2223 (*ondisk_size) += sb.st_size;
2225 if (close(fd) == -1) {
2226 err = got_error_from_errno("close");
2227 goto done;
2231 if (closedir(dir) != 0) {
2232 err = got_error_from_errno("closedir");
2233 goto done;
2235 dir = NULL;
2237 free(path);
2238 path = NULL;
2240 done:
2241 if (dir && closedir(dir) != 0 && err == NULL)
2242 err = got_error_from_errno("closedir");
2244 if (err) {
2245 *nobjects = 0;
2246 *ondisk_size = 0;
2248 free(path_objects);
2249 free(path);
2250 return err;
2253 const struct got_error *
2254 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2255 off_t *total_packsize, struct got_repository *repo)
2257 const struct got_error *err = NULL;
2258 DIR *packdir = NULL;
2259 struct dirent *dent;
2260 struct got_packidx *packidx = NULL;
2261 char *path_packidx;
2262 char *path_packfile;
2263 int packdir_fd;
2264 struct stat sb;
2266 *npackfiles = 0;
2267 *nobjects = 0;
2268 *total_packsize = 0;
2270 packdir_fd = openat(got_repo_get_fd(repo),
2271 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2272 if (packdir_fd == -1) {
2273 return got_error_from_errno_fmt("openat: %s/%s",
2274 got_repo_get_path_git_dir(repo),
2275 GOT_OBJECTS_PACK_DIR);
2278 packdir = fdopendir(packdir_fd);
2279 if (packdir == NULL) {
2280 err = got_error_from_errno("fdopendir");
2281 goto done;
2284 while ((dent = readdir(packdir)) != NULL) {
2285 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2286 continue;
2288 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2289 dent->d_name) == -1) {
2290 err = got_error_from_errno("asprintf");
2291 goto done;
2294 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2295 path_packidx, 0);
2296 free(path_packidx);
2297 if (err)
2298 goto done;
2300 if (fstat(packidx->fd, &sb) == -1)
2301 goto done;
2302 *total_packsize += sb.st_size;
2304 err = got_packidx_get_packfile_path(&path_packfile,
2305 packidx->path_packidx);
2306 if (err)
2307 goto done;
2309 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2310 0) == -1) {
2311 free(path_packfile);
2312 goto done;
2314 free(path_packfile);
2315 *total_packsize += sb.st_size;
2317 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2319 (*npackfiles)++;
2321 got_packidx_close(packidx);
2322 packidx = NULL;
2324 done:
2325 if (packidx)
2326 got_packidx_close(packidx);
2327 if (packdir && closedir(packdir) != 0 && err == NULL)
2328 err = got_error_from_errno("closedir");
2329 if (err) {
2330 *npackfiles = 0;
2331 *nobjects = 0;
2332 *total_packsize = 0;
2334 return err;
2337 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2338 got_packidx_bloom_filter_cmp);