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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <endian.h>
27 #include <fcntl.h>
28 #include <fnmatch.h>
29 #include <limits.h>
30 #include <dirent.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sha1.h>
34 #include <string.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <zlib.h>
38 #include <errno.h>
39 #include <libgen.h>
40 #include <stdint.h>
41 #include <imsg.h>
42 #include <uuid.h>
44 #include "bloom.h"
46 #include "got_error.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_path.h"
50 #include "got_cancel.h"
51 #include "got_object.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
63 #include "got_lib_gotconfig.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
67 #endif
69 const char *
70 got_repo_get_path(struct got_repository *repo)
71 {
72 return repo->path;
73 }
75 const char *
76 got_repo_get_path_git_dir(struct got_repository *repo)
77 {
78 return repo->path_git_dir;
79 }
81 int
82 got_repo_get_fd(struct got_repository *repo)
83 {
84 return repo->gitdir_fd;
85 }
87 const char *
88 got_repo_get_gitconfig_author_name(struct got_repository *repo)
89 {
90 return repo->gitconfig_author_name;
91 }
93 const char *
94 got_repo_get_gitconfig_author_email(struct got_repository *repo)
95 {
96 return repo->gitconfig_author_email;
97 }
99 const char *
100 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
102 return repo->global_gitconfig_author_name;
105 const char *
106 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
108 return repo->global_gitconfig_author_email;
111 const char *
112 got_repo_get_gitconfig_owner(struct got_repository *repo)
114 return repo->gitconfig_owner;
117 void
118 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
119 struct got_repository *repo)
121 *extensions = repo->extensions;
122 *nextensions = repo->nextensions;
125 int
126 got_repo_is_bare(struct got_repository *repo)
128 return (strcmp(repo->path, repo->path_git_dir) == 0);
131 static char *
132 get_path_git_child(struct got_repository *repo, const char *basename)
134 char *path_child;
136 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
137 basename) == -1)
138 return NULL;
140 return path_child;
143 char *
144 got_repo_get_path_objects(struct got_repository *repo)
146 return get_path_git_child(repo, GOT_OBJECTS_DIR);
149 char *
150 got_repo_get_path_objects_pack(struct got_repository *repo)
152 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
155 char *
156 got_repo_get_path_refs(struct got_repository *repo)
158 return get_path_git_child(repo, GOT_REFS_DIR);
161 char *
162 got_repo_get_path_packed_refs(struct got_repository *repo)
164 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
167 static char *
168 get_path_head(struct got_repository *repo)
170 return get_path_git_child(repo, GOT_HEAD_FILE);
173 char *
174 got_repo_get_path_gitconfig(struct got_repository *repo)
176 return get_path_git_child(repo, GOT_GITCONFIG);
179 char *
180 got_repo_get_path_gotconfig(struct got_repository *repo)
182 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
185 const struct got_gotconfig *
186 got_repo_get_gotconfig(struct got_repository *repo)
188 return repo->gotconfig;
191 void
192 got_repo_get_gitconfig_remotes(int *nremotes,
193 const struct got_remote_repo **remotes, struct got_repository *repo)
195 *nremotes = repo->ngitconfig_remotes;
196 *remotes = repo->gitconfig_remotes;
199 static int
200 is_git_repo(struct got_repository *repo)
202 const char *path_git = got_repo_get_path_git_dir(repo);
203 char *path_objects = got_repo_get_path_objects(repo);
204 char *path_refs = got_repo_get_path_refs(repo);
205 char *path_head = get_path_head(repo);
206 int ret = 0;
207 struct stat sb;
208 struct got_reference *head_ref;
210 if (lstat(path_git, &sb) == -1)
211 goto done;
212 if (!S_ISDIR(sb.st_mode))
213 goto done;
215 if (lstat(path_objects, &sb) == -1)
216 goto done;
217 if (!S_ISDIR(sb.st_mode))
218 goto done;
220 if (lstat(path_refs, &sb) == -1)
221 goto done;
222 if (!S_ISDIR(sb.st_mode))
223 goto done;
225 if (lstat(path_head, &sb) == -1)
226 goto done;
227 if (!S_ISREG(sb.st_mode))
228 goto done;
230 /* Check if the HEAD reference can be opened. */
231 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
232 goto done;
233 got_ref_close(head_ref);
235 ret = 1;
236 done:
237 free(path_objects);
238 free(path_refs);
239 free(path_head);
240 return ret;
244 const struct got_error *
245 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
246 struct got_object *obj)
248 #ifndef GOT_NO_OBJ_CACHE
249 const struct got_error *err = NULL;
250 err = got_object_cache_add(&repo->objcache, id, obj);
251 if (err) {
252 if (err->code == GOT_ERR_OBJ_EXISTS ||
253 err->code == GOT_ERR_OBJ_TOO_LARGE)
254 err = NULL;
255 return err;
257 obj->refcnt++;
258 #endif
259 return NULL;
262 struct got_object *
263 got_repo_get_cached_object(struct got_repository *repo,
264 struct got_object_id *id)
266 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
269 const struct got_error *
270 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
271 struct got_tree_object *tree)
273 #ifndef GOT_NO_OBJ_CACHE
274 const struct got_error *err = NULL;
275 err = got_object_cache_add(&repo->treecache, id, tree);
276 if (err) {
277 if (err->code == GOT_ERR_OBJ_EXISTS ||
278 err->code == GOT_ERR_OBJ_TOO_LARGE)
279 err = NULL;
280 return err;
282 tree->refcnt++;
283 #endif
284 return NULL;
287 struct got_tree_object *
288 got_repo_get_cached_tree(struct got_repository *repo,
289 struct got_object_id *id)
291 return (struct got_tree_object *)got_object_cache_get(
292 &repo->treecache, id);
295 const struct got_error *
296 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
297 struct got_commit_object *commit)
299 #ifndef GOT_NO_OBJ_CACHE
300 const struct got_error *err = NULL;
301 err = got_object_cache_add(&repo->commitcache, id, commit);
302 if (err) {
303 if (err->code == GOT_ERR_OBJ_EXISTS ||
304 err->code == GOT_ERR_OBJ_TOO_LARGE)
305 err = NULL;
306 return err;
308 commit->refcnt++;
309 #endif
310 return NULL;
313 struct got_commit_object *
314 got_repo_get_cached_commit(struct got_repository *repo,
315 struct got_object_id *id)
317 return (struct got_commit_object *)got_object_cache_get(
318 &repo->commitcache, id);
321 const struct got_error *
322 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
323 struct got_tag_object *tag)
325 #ifndef GOT_NO_OBJ_CACHE
326 const struct got_error *err = NULL;
327 err = got_object_cache_add(&repo->tagcache, id, tag);
328 if (err) {
329 if (err->code == GOT_ERR_OBJ_EXISTS ||
330 err->code == GOT_ERR_OBJ_TOO_LARGE)
331 err = NULL;
332 return err;
334 tag->refcnt++;
335 #endif
336 return NULL;
339 struct got_tag_object *
340 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
342 return (struct got_tag_object *)got_object_cache_get(
343 &repo->tagcache, id);
346 static const struct got_error *
347 open_repo(struct got_repository *repo, const char *path)
349 const struct got_error *err = NULL;
351 repo->gitdir_fd = -1;
353 /* bare git repository? */
354 repo->path_git_dir = strdup(path);
355 if (repo->path_git_dir == NULL)
356 return got_error_from_errno("strdup");
357 if (is_git_repo(repo)) {
358 repo->path = strdup(repo->path_git_dir);
359 if (repo->path == NULL) {
360 err = got_error_from_errno("strdup");
361 goto done;
363 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
364 if (repo->gitdir_fd == -1) {
365 err = got_error_from_errno2("open",
366 repo->path_git_dir);
367 goto done;
369 return NULL;
372 /* git repository with working tree? */
373 free(repo->path_git_dir);
374 repo->path_git_dir = NULL;
375 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
376 err = got_error_from_errno("asprintf");
377 goto done;
379 if (is_git_repo(repo)) {
380 repo->path = strdup(path);
381 if (repo->path == NULL) {
382 err = got_error_from_errno("strdup");
383 goto done;
385 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
386 if (repo->gitdir_fd == -1) {
387 err = got_error_from_errno2("open",
388 repo->path_git_dir);
389 goto done;
391 return NULL;
394 err = got_error(GOT_ERR_NOT_GIT_REPO);
395 done:
396 if (err) {
397 free(repo->path);
398 repo->path = NULL;
399 free(repo->path_git_dir);
400 repo->path_git_dir = NULL;
401 if (repo->gitdir_fd != -1)
402 close(repo->gitdir_fd);
403 repo->gitdir_fd = -1;
406 return err;
409 static const struct got_error *
410 parse_gitconfig_file(int *gitconfig_repository_format_version,
411 char **gitconfig_author_name, char **gitconfig_author_email,
412 struct got_remote_repo **remotes, int *nremotes,
413 char **gitconfig_owner, char ***extensions, int *nextensions,
414 const char *gitconfig_path)
416 const struct got_error *err = NULL, *child_err = NULL;
417 int fd = -1;
418 int imsg_fds[2] = { -1, -1 };
419 pid_t pid;
420 struct imsgbuf *ibuf;
422 *gitconfig_repository_format_version = 0;
423 if (extensions)
424 *extensions = NULL;
425 if (nextensions)
426 *nextensions = 0;
427 *gitconfig_author_name = NULL;
428 *gitconfig_author_email = NULL;
429 if (remotes)
430 *remotes = NULL;
431 if (nremotes)
432 *nremotes = 0;
433 if (gitconfig_owner)
434 *gitconfig_owner = NULL;
436 fd = open(gitconfig_path, O_RDONLY);
437 if (fd == -1) {
438 if (errno == ENOENT)
439 return NULL;
440 return got_error_from_errno2("open", gitconfig_path);
443 ibuf = calloc(1, sizeof(*ibuf));
444 if (ibuf == NULL) {
445 err = got_error_from_errno("calloc");
446 goto done;
449 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
450 err = got_error_from_errno("socketpair");
451 goto done;
454 pid = fork();
455 if (pid == -1) {
456 err = got_error_from_errno("fork");
457 goto done;
458 } else if (pid == 0) {
459 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
460 gitconfig_path);
461 /* not reached */
464 if (close(imsg_fds[1]) == -1) {
465 err = got_error_from_errno("close");
466 goto done;
468 imsg_fds[1] = -1;
469 imsg_init(ibuf, imsg_fds[0]);
471 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
472 if (err)
473 goto done;
474 fd = -1;
476 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
477 if (err)
478 goto done;
480 err = got_privsep_recv_gitconfig_int(
481 gitconfig_repository_format_version, ibuf);
482 if (err)
483 goto done;
485 if (extensions && nextensions) {
486 err = got_privsep_send_gitconfig_repository_extensions_req(
487 ibuf);
488 if (err)
489 goto done;
490 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
491 if (err)
492 goto done;
493 if (*nextensions > 0) {
494 int i;
495 *extensions = calloc(*nextensions, sizeof(char *));
496 if (*extensions == NULL) {
497 err = got_error_from_errno("calloc");
498 goto done;
500 for (i = 0; i < *nextensions; i++) {
501 char *ext;
502 err = got_privsep_recv_gitconfig_str(&ext,
503 ibuf);
504 if (err)
505 goto done;
506 (*extensions)[i] = ext;
511 err = got_privsep_send_gitconfig_author_name_req(ibuf);
512 if (err)
513 goto done;
515 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
516 if (err)
517 goto done;
519 err = got_privsep_send_gitconfig_author_email_req(ibuf);
520 if (err)
521 goto done;
523 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
524 if (err)
525 goto done;
527 if (remotes && nremotes) {
528 err = got_privsep_send_gitconfig_remotes_req(ibuf);
529 if (err)
530 goto done;
532 err = got_privsep_recv_gitconfig_remotes(remotes,
533 nremotes, ibuf);
534 if (err)
535 goto done;
538 if (gitconfig_owner) {
539 err = got_privsep_send_gitconfig_owner_req(ibuf);
540 if (err)
541 goto done;
542 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
543 if (err)
544 goto done;
547 imsg_clear(ibuf);
548 err = got_privsep_send_stop(imsg_fds[0]);
549 child_err = got_privsep_wait_for_child(pid);
550 if (child_err && err == NULL)
551 err = child_err;
552 done:
553 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
554 err = got_error_from_errno("close");
555 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
556 err = got_error_from_errno("close");
557 if (fd != -1 && close(fd) == -1 && err == NULL)
558 err = got_error_from_errno2("close", gitconfig_path);
559 free(ibuf);
560 return err;
563 static const struct got_error *
564 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
566 const struct got_error *err = NULL;
567 char *repo_gitconfig_path = NULL;
569 if (global_gitconfig_path) {
570 /* Read settings from ~/.gitconfig. */
571 int dummy_repo_version;
572 err = parse_gitconfig_file(&dummy_repo_version,
573 &repo->global_gitconfig_author_name,
574 &repo->global_gitconfig_author_email,
575 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
576 if (err)
577 return err;
580 /* Read repository's .git/config file. */
581 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
582 if (repo_gitconfig_path == NULL)
583 return got_error_from_errno("got_repo_get_path_gitconfig");
585 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
586 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
587 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
588 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
589 repo_gitconfig_path);
590 if (err)
591 goto done;
592 done:
593 free(repo_gitconfig_path);
594 return err;
597 static const struct got_error *
598 read_gotconfig(struct got_repository *repo)
600 const struct got_error *err = NULL;
601 char *gotconfig_path;
603 gotconfig_path = got_repo_get_path_gotconfig(repo);
604 if (gotconfig_path == NULL)
605 return got_error_from_errno("got_repo_get_path_gotconfig");
607 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
608 free(gotconfig_path);
609 return err;
612 /* Supported repository format extensions. */
613 static const char *repo_extensions[] = {
614 "noop", /* Got supports repository format version 1. */
615 "preciousObjects", /* Supported by gotadmin cleanup. */
616 "worktreeConfig", /* Got does not care about Git work trees. */
617 };
619 const struct got_error *
620 got_repo_open(struct got_repository **repop, const char *path,
621 const char *global_gitconfig_path)
623 struct got_repository *repo = NULL;
624 const struct got_error *err = NULL;
625 char *repo_path = NULL;
626 size_t i;
627 struct rlimit rl;
629 *repop = NULL;
631 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
632 return got_error_from_errno("getrlimit");
634 repo = calloc(1, sizeof(*repo));
635 if (repo == NULL) {
636 err = got_error_from_errno("calloc");
637 goto done;
640 STAILQ_INIT(&repo->packidx_bloom_filters);
642 for (i = 0; i < nitems(repo->privsep_children); i++) {
643 memset(&repo->privsep_children[i], 0,
644 sizeof(repo->privsep_children[0]));
645 repo->privsep_children[i].imsg_fd = -1;
648 err = got_object_cache_init(&repo->objcache,
649 GOT_OBJECT_CACHE_TYPE_OBJ);
650 if (err)
651 goto done;
652 err = got_object_cache_init(&repo->treecache,
653 GOT_OBJECT_CACHE_TYPE_TREE);
654 if (err)
655 goto done;
656 err = got_object_cache_init(&repo->commitcache,
657 GOT_OBJECT_CACHE_TYPE_COMMIT);
658 if (err)
659 goto done;
660 err = got_object_cache_init(&repo->tagcache,
661 GOT_OBJECT_CACHE_TYPE_TAG);
662 if (err)
663 goto done;
665 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
666 if (repo->pack_cache_size > rl.rlim_cur / 8)
667 repo->pack_cache_size = rl.rlim_cur / 8;
669 repo_path = realpath(path, NULL);
670 if (repo_path == NULL) {
671 err = got_error_from_errno2("realpath", path);
672 goto done;
675 for (;;) {
676 char *parent_path;
678 err = open_repo(repo, repo_path);
679 if (err == NULL)
680 break;
681 if (err->code != GOT_ERR_NOT_GIT_REPO)
682 goto done;
683 if (repo_path[0] == '/' && repo_path[1] == '\0') {
684 err = got_error(GOT_ERR_NOT_GIT_REPO);
685 goto done;
687 err = got_path_dirname(&parent_path, repo_path);
688 if (err)
689 goto done;
690 free(repo_path);
691 repo_path = parent_path;
694 err = read_gotconfig(repo);
695 if (err)
696 goto done;
698 err = read_gitconfig(repo, global_gitconfig_path);
699 if (err)
700 goto done;
701 if (repo->gitconfig_repository_format_version != 0)
702 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
703 for (i = 0; i < repo->nextensions; i++) {
704 char *ext = repo->extensions[i];
705 int j, supported = 0;
706 for (j = 0; j < nitems(repo_extensions); j++) {
707 if (strcmp(ext, repo_extensions[j]) == 0) {
708 supported = 1;
709 break;
712 if (!supported) {
713 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
714 goto done;
717 done:
718 if (err)
719 got_repo_close(repo);
720 else
721 *repop = repo;
722 free(repo_path);
723 return err;
726 const struct got_error *
727 got_repo_close(struct got_repository *repo)
729 const struct got_error *err = NULL, *child_err;
730 size_t i;
732 for (i = 0; i < repo->pack_cache_size; i++) {
733 if (repo->packidx_cache[i] == NULL)
734 break;
735 got_packidx_close(repo->packidx_cache[i]);
738 while (!STAILQ_EMPTY(&repo->packidx_bloom_filters)) {
739 struct got_packidx_bloom_filter *bf;
740 bf = STAILQ_FIRST(&repo->packidx_bloom_filters);
741 STAILQ_REMOVE_HEAD(&repo->packidx_bloom_filters, entry);
742 free(bf->bloom);
743 free(bf);
746 for (i = 0; i < repo->pack_cache_size; i++) {
747 if (repo->packs[i].path_packfile == NULL)
748 break;
749 got_pack_close(&repo->packs[i]);
752 free(repo->path);
753 free(repo->path_git_dir);
755 got_object_cache_close(&repo->objcache);
756 got_object_cache_close(&repo->treecache);
757 got_object_cache_close(&repo->commitcache);
758 got_object_cache_close(&repo->tagcache);
760 for (i = 0; i < nitems(repo->privsep_children); i++) {
761 if (repo->privsep_children[i].imsg_fd == -1)
762 continue;
763 imsg_clear(repo->privsep_children[i].ibuf);
764 free(repo->privsep_children[i].ibuf);
765 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
766 child_err = got_privsep_wait_for_child(
767 repo->privsep_children[i].pid);
768 if (child_err && err == NULL)
769 err = child_err;
770 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
771 err == NULL)
772 err = got_error_from_errno("close");
775 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
776 err == NULL)
777 err = got_error_from_errno("close");
779 if (repo->gotconfig)
780 got_gotconfig_free(repo->gotconfig);
781 free(repo->gitconfig_author_name);
782 free(repo->gitconfig_author_email);
783 for (i = 0; i < repo->ngitconfig_remotes; i++)
784 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
785 free(repo->gitconfig_remotes);
786 for (i = 0; i < repo->nextensions; i++)
787 free(repo->extensions[i]);
788 free(repo->extensions);
789 free(repo);
791 return err;
794 void
795 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
797 int i;
799 free(repo->name);
800 repo->name = NULL;
801 free(repo->fetch_url);
802 repo->fetch_url = NULL;
803 free(repo->send_url);
804 repo->send_url = NULL;
805 for (i = 0; i < repo->nfetch_branches; i++)
806 free(repo->fetch_branches[i]);
807 free(repo->fetch_branches);
808 repo->fetch_branches = NULL;
809 repo->nfetch_branches = 0;
810 for (i = 0; i < repo->nsend_branches; i++)
811 free(repo->send_branches[i]);
812 free(repo->send_branches);
813 repo->send_branches = NULL;
814 repo->nsend_branches = 0;
817 const struct got_error *
818 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
819 const char *input_path)
821 const struct got_error *err = NULL;
822 const char *repo_abspath = NULL;
823 size_t repolen, len;
824 char *canonpath, *path = NULL;
826 *in_repo_path = NULL;
828 canonpath = strdup(input_path);
829 if (canonpath == NULL) {
830 err = got_error_from_errno("strdup");
831 goto done;
833 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
834 if (err)
835 goto done;
837 repo_abspath = got_repo_get_path(repo);
839 if (canonpath[0] == '\0') {
840 path = strdup(canonpath);
841 if (path == NULL) {
842 err = got_error_from_errno("strdup");
843 goto done;
845 } else {
846 path = realpath(canonpath, NULL);
847 if (path == NULL) {
848 if (errno != ENOENT) {
849 err = got_error_from_errno2("realpath",
850 canonpath);
851 goto done;
853 /*
854 * Path is not on disk.
855 * Assume it is already relative to repository root.
856 */
857 path = strdup(canonpath);
858 if (path == NULL) {
859 err = got_error_from_errno("strdup");
860 goto done;
864 repolen = strlen(repo_abspath);
865 len = strlen(path);
868 if (strcmp(path, repo_abspath) == 0) {
869 free(path);
870 path = strdup("");
871 if (path == NULL) {
872 err = got_error_from_errno("strdup");
873 goto done;
875 } else if (len > repolen &&
876 got_path_is_child(path, repo_abspath, repolen)) {
877 /* Matched an on-disk path inside repository. */
878 if (got_repo_is_bare(repo)) {
879 /*
880 * Matched an on-disk path inside repository
881 * database. Treat input as repository-relative.
882 */
883 free(path);
884 path = canonpath;
885 canonpath = NULL;
886 } else {
887 char *child;
888 /* Strip common prefix with repository path. */
889 err = got_path_skip_common_ancestor(&child,
890 repo_abspath, path);
891 if (err)
892 goto done;
893 free(path);
894 path = child;
896 } else {
897 /*
898 * Matched unrelated on-disk path.
899 * Treat input as repository-relative.
900 */
901 free(path);
902 path = canonpath;
903 canonpath = NULL;
907 /* Make in-repository path absolute */
908 if (path[0] != '/') {
909 char *abspath;
910 if (asprintf(&abspath, "/%s", path) == -1) {
911 err = got_error_from_errno("asprintf");
912 goto done;
914 free(path);
915 path = abspath;
918 done:
919 free(canonpath);
920 if (err)
921 free(path);
922 else
923 *in_repo_path = path;
924 return err;
927 static const struct got_error *
928 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
929 const char *path_packidx)
931 const struct got_error *err = NULL;
932 size_t i;
934 for (i = 0; i < repo->pack_cache_size; i++) {
935 if (repo->packidx_cache[i] == NULL)
936 break;
937 if (strcmp(repo->packidx_cache[i]->path_packidx,
938 path_packidx) == 0) {
939 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
942 if (i == repo->pack_cache_size) {
943 i = repo->pack_cache_size - 1;
944 err = got_packidx_close(repo->packidx_cache[i]);
945 if (err)
946 return err;
949 repo->packidx_cache[i] = packidx;
951 return NULL;
954 int
955 got_repo_is_packidx_filename(const char *name, size_t len)
957 if (len != GOT_PACKIDX_NAMELEN)
958 return 0;
960 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
961 return 0;
963 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
964 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
965 return 0;
967 return 1;
970 static int
971 check_packidx_bloom_filter(struct got_repository *repo,
972 const char *path_packidx, struct got_object_id *id)
974 struct got_packidx_bloom_filter *bf;
976 STAILQ_FOREACH(bf, &repo->packidx_bloom_filters, entry) {
977 if (got_path_cmp(bf->path_packidx, path_packidx,
978 bf->path_packidx_len, strlen(path_packidx)) == 0) {
979 return bloom_check(bf->bloom, id->sha1,
980 sizeof(id->sha1));
984 /* No bloom filter means this pack index must be searched. */
985 return 1;
988 static const struct got_error *
989 add_packidx_bloom_filter(struct got_repository *repo,
990 struct got_packidx *packidx, const char *path_packidx)
992 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
993 struct got_packidx_bloom_filter *bf;
994 size_t len;
996 /*
997 * Don't use bloom filters for very large pack index files.
998 * Large pack files will contain a relatively large fraction
999 * of our objects so we will likely need to visit them anyway.
1000 * The more objects a pack file contains the higher the probability
1001 * of a false-positive match from the bloom filter. And reading
1002 * all object IDs from a large pack index file can be expensive.
1004 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1005 return NULL;
1007 /* Do we already have a filter for this pack index? */
1008 STAILQ_FOREACH(bf, &repo->packidx_bloom_filters, entry) {
1009 if (got_path_cmp(bf->path_packidx, path_packidx,
1010 bf->path_packidx_len, strlen(path_packidx)) == 0)
1011 return NULL;
1014 bf = calloc(1, sizeof(*bf));
1015 if (bf == NULL)
1016 return got_error_from_errno("calloc");
1017 bf->bloom = calloc(1, sizeof(*bf->bloom));
1018 if (bf->bloom == NULL) {
1019 free(bf);
1020 return got_error_from_errno("calloc");
1024 len = strlcpy(bf->path_packidx, path_packidx, sizeof(bf->path_packidx));
1025 if (len >= sizeof(bf->path_packidx)) {
1026 free(bf->bloom);
1027 free(bf);
1028 return got_error(GOT_ERR_NO_SPACE);
1030 bf->path_packidx_len = len;
1032 /* Minimum size supported by our bloom filter is 1000 entries. */
1033 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1034 for (i = 0; i < nobjects; i++) {
1035 struct got_packidx_object_id *id;
1036 id = &packidx->hdr.sorted_ids[i];
1037 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1040 STAILQ_INSERT_TAIL(&repo->packidx_bloom_filters, bf, entry);
1041 return NULL;
1044 const struct got_error *
1045 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1046 struct got_repository *repo, struct got_object_id *id)
1048 const struct got_error *err;
1049 DIR *packdir = NULL;
1050 struct dirent *dent;
1051 char *path_packidx;
1052 size_t i;
1053 int packdir_fd;
1055 /* Search pack index cache. */
1056 for (i = 0; i < repo->pack_cache_size; i++) {
1057 if (repo->packidx_cache[i] == NULL)
1058 break;
1059 if (!check_packidx_bloom_filter(repo,
1060 repo->packidx_cache[i]->path_packidx, id))
1061 continue; /* object will not be found in this index */
1062 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1063 if (*idx != -1) {
1064 *packidx = repo->packidx_cache[i];
1066 * Move this cache entry to the front. Repeatedly
1067 * searching a wrong pack index can be expensive.
1069 if (i > 0) {
1070 memmove(&repo->packidx_cache[1],
1071 &repo->packidx_cache[0],
1072 i * sizeof(repo->packidx_cache[0]));
1073 repo->packidx_cache[0] = *packidx;
1075 return NULL;
1078 /* No luck. Search the filesystem. */
1080 packdir_fd = openat(got_repo_get_fd(repo),
1081 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1082 if (packdir_fd == -1) {
1083 if (errno == ENOENT)
1084 err = got_error_no_obj(id);
1085 else
1086 err = got_error_from_errno_fmt("openat: %s/%s",
1087 got_repo_get_path_git_dir(repo),
1088 GOT_OBJECTS_PACK_DIR);
1089 goto done;
1092 packdir = fdopendir(packdir_fd);
1093 if (packdir == NULL) {
1094 err = got_error_from_errno("fdopendir");
1095 goto done;
1098 while ((dent = readdir(packdir)) != NULL) {
1099 int is_cached = 0;
1101 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1102 continue;
1104 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1105 dent->d_name) == -1) {
1106 err = got_error_from_errno("asprintf");
1107 goto done;
1110 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1111 free(path_packidx);
1112 continue; /* object will not be found in this index */
1115 for (i = 0; i < repo->pack_cache_size; i++) {
1116 if (repo->packidx_cache[i] == NULL)
1117 break;
1118 if (strcmp(repo->packidx_cache[i]->path_packidx,
1119 path_packidx) == 0) {
1120 is_cached = 1;
1121 break;
1124 if (is_cached) {
1125 free(path_packidx);
1126 continue; /* already searched */
1129 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1130 path_packidx, 0);
1131 if (err) {
1132 free(path_packidx);
1133 goto done;
1136 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1137 if (err) {
1138 free(path_packidx);
1139 goto done;
1142 err = cache_packidx(repo, *packidx, path_packidx);
1143 free(path_packidx);
1144 if (err)
1145 goto done;
1147 *idx = got_packidx_get_object_idx(*packidx, id);
1148 if (*idx != -1) {
1149 err = NULL; /* found the object */
1150 goto done;
1154 err = got_error_no_obj(id);
1155 done:
1156 if (packdir && closedir(packdir) != 0 && err == NULL)
1157 err = got_error_from_errno("closedir");
1158 return err;
1161 static const struct got_error *
1162 read_packfile_hdr(int fd, struct got_packidx *packidx)
1164 const struct got_error *err = NULL;
1165 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1166 struct got_packfile_hdr hdr;
1167 ssize_t n;
1169 n = read(fd, &hdr, sizeof(hdr));
1170 if (n < 0)
1171 return got_error_from_errno("read");
1172 if (n != sizeof(hdr))
1173 return got_error(GOT_ERR_BAD_PACKFILE);
1175 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1176 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1177 be32toh(hdr.nobjects) != totobj)
1178 err = got_error(GOT_ERR_BAD_PACKFILE);
1180 return err;
1183 static const struct got_error *
1184 open_packfile(int *fd, struct got_repository *repo,
1185 const char *relpath, struct got_packidx *packidx)
1187 const struct got_error *err = NULL;
1189 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1190 if (*fd == -1)
1191 return got_error_from_errno_fmt("openat: %s/%s",
1192 got_repo_get_path_git_dir(repo), relpath);
1194 if (packidx) {
1195 err = read_packfile_hdr(*fd, packidx);
1196 if (err) {
1197 close(*fd);
1198 *fd = -1;
1202 return err;
1205 const struct got_error *
1206 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1207 const char *path_packfile, struct got_packidx *packidx)
1209 const struct got_error *err = NULL;
1210 struct got_pack *pack = NULL;
1211 struct stat sb;
1212 size_t i;
1214 if (packp)
1215 *packp = NULL;
1217 for (i = 0; i < repo->pack_cache_size; i++) {
1218 pack = &repo->packs[i];
1219 if (pack->path_packfile == NULL)
1220 break;
1221 if (strcmp(pack->path_packfile, path_packfile) == 0)
1222 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1225 if (i == repo->pack_cache_size) {
1226 err = got_pack_close(&repo->packs[i - 1]);
1227 if (err)
1228 return err;
1229 memmove(&repo->packs[1], &repo->packs[0],
1230 sizeof(repo->packs) - sizeof(repo->packs[0]));
1231 i = 0;
1234 pack = &repo->packs[i];
1236 pack->path_packfile = strdup(path_packfile);
1237 if (pack->path_packfile == NULL) {
1238 err = got_error_from_errno("strdup");
1239 goto done;
1242 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1243 if (err)
1244 goto done;
1246 if (fstat(pack->fd, &sb) != 0) {
1247 err = got_error_from_errno("fstat");
1248 goto done;
1250 pack->filesize = sb.st_size;
1252 pack->privsep_child = NULL;
1254 #ifndef GOT_PACK_NO_MMAP
1255 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1256 pack->fd, 0);
1257 if (pack->map == MAP_FAILED) {
1258 if (errno != ENOMEM) {
1259 err = got_error_from_errno("mmap");
1260 goto done;
1262 pack->map = NULL; /* fall back to read(2) */
1264 #endif
1265 done:
1266 if (err) {
1267 if (pack) {
1268 free(pack->path_packfile);
1269 memset(pack, 0, sizeof(*pack));
1271 } else if (packp)
1272 *packp = pack;
1273 return err;
1276 struct got_pack *
1277 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1279 struct got_pack *pack = NULL;
1280 size_t i;
1282 for (i = 0; i < repo->pack_cache_size; i++) {
1283 pack = &repo->packs[i];
1284 if (pack->path_packfile == NULL)
1285 break;
1286 if (strcmp(pack->path_packfile, path_packfile) == 0)
1287 return pack;
1290 return NULL;
1293 const struct got_error *
1294 got_repo_init(const char *repo_path)
1296 const struct got_error *err = NULL;
1297 const char *dirnames[] = {
1298 GOT_OBJECTS_DIR,
1299 GOT_OBJECTS_PACK_DIR,
1300 GOT_REFS_DIR,
1302 const char *description_str = "Unnamed repository; "
1303 "edit this file 'description' to name the repository.";
1304 const char *headref_str = "ref: refs/heads/main";
1305 const char *gitconfig_str = "[core]\n"
1306 "\trepositoryformatversion = 0\n"
1307 "\tfilemode = true\n"
1308 "\tbare = true\n";
1309 char *path;
1310 size_t i;
1312 if (!got_path_dir_is_empty(repo_path))
1313 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1315 for (i = 0; i < nitems(dirnames); i++) {
1316 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1317 return got_error_from_errno("asprintf");
1319 err = got_path_mkdir(path);
1320 free(path);
1321 if (err)
1322 return err;
1325 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1326 return got_error_from_errno("asprintf");
1327 err = got_path_create_file(path, description_str);
1328 free(path);
1329 if (err)
1330 return err;
1332 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1333 return got_error_from_errno("asprintf");
1334 err = got_path_create_file(path, headref_str);
1335 free(path);
1336 if (err)
1337 return err;
1339 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1340 return got_error_from_errno("asprintf");
1341 err = got_path_create_file(path, gitconfig_str);
1342 free(path);
1343 if (err)
1344 return err;
1346 return NULL;
1349 static const struct got_error *
1350 match_packed_object(struct got_object_id **unique_id,
1351 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1353 const struct got_error *err = NULL;
1354 DIR *packdir = NULL;
1355 struct dirent *dent;
1356 char *path_packidx;
1357 struct got_object_id_queue matched_ids;
1358 int packdir_fd;
1360 STAILQ_INIT(&matched_ids);
1362 packdir_fd = openat(got_repo_get_fd(repo),
1363 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1364 if (packdir_fd == -1) {
1365 if (errno != ENOENT)
1366 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1367 goto done;
1370 packdir = fdopendir(packdir_fd);
1371 if (packdir == NULL) {
1372 err = got_error_from_errno("fdopendir");
1373 goto done;
1376 while ((dent = readdir(packdir)) != NULL) {
1377 struct got_packidx *packidx;
1378 struct got_object_qid *qid;
1380 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1381 continue;
1383 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1384 dent->d_name) == -1) {
1385 err = got_error_from_errno("strdup");
1386 break;
1389 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1390 path_packidx, 0);
1391 free(path_packidx);
1392 if (err)
1393 break;
1395 err = got_packidx_match_id_str_prefix(&matched_ids,
1396 packidx, id_str_prefix);
1397 if (err) {
1398 got_packidx_close(packidx);
1399 break;
1401 err = got_packidx_close(packidx);
1402 if (err)
1403 break;
1405 STAILQ_FOREACH(qid, &matched_ids, entry) {
1406 if (obj_type != GOT_OBJ_TYPE_ANY) {
1407 int matched_type;
1408 err = got_object_get_type(&matched_type, repo,
1409 qid->id);
1410 if (err)
1411 goto done;
1412 if (matched_type != obj_type)
1413 continue;
1415 if (*unique_id == NULL) {
1416 *unique_id = got_object_id_dup(qid->id);
1417 if (*unique_id == NULL) {
1418 err = got_error_from_errno("malloc");
1419 goto done;
1421 } else {
1422 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1423 continue; /* packed multiple times */
1424 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1425 goto done;
1429 done:
1430 got_object_id_queue_free(&matched_ids);
1431 if (packdir && closedir(packdir) != 0 && err == NULL)
1432 err = got_error_from_errno("closedir");
1433 if (err) {
1434 free(*unique_id);
1435 *unique_id = NULL;
1437 return err;
1440 static const struct got_error *
1441 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1442 const char *object_dir, const char *id_str_prefix, int obj_type,
1443 struct got_repository *repo)
1445 const struct got_error *err = NULL;
1446 char *path;
1447 DIR *dir = NULL;
1448 struct dirent *dent;
1449 struct got_object_id id;
1451 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1452 err = got_error_from_errno("asprintf");
1453 goto done;
1456 dir = opendir(path);
1457 if (dir == NULL) {
1458 if (errno == ENOENT) {
1459 err = NULL;
1460 goto done;
1462 err = got_error_from_errno2("opendir", path);
1463 goto done;
1465 while ((dent = readdir(dir)) != NULL) {
1466 char *id_str;
1467 int cmp;
1469 if (strcmp(dent->d_name, ".") == 0 ||
1470 strcmp(dent->d_name, "..") == 0)
1471 continue;
1473 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1474 err = got_error_from_errno("asprintf");
1475 goto done;
1478 if (!got_parse_sha1_digest(id.sha1, id_str))
1479 continue;
1482 * Directory entries do not necessarily appear in
1483 * sorted order, so we must iterate over all of them.
1485 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1486 if (cmp != 0) {
1487 free(id_str);
1488 continue;
1491 if (*unique_id == NULL) {
1492 if (obj_type != GOT_OBJ_TYPE_ANY) {
1493 int matched_type;
1494 err = got_object_get_type(&matched_type, repo,
1495 &id);
1496 if (err)
1497 goto done;
1498 if (matched_type != obj_type)
1499 continue;
1501 *unique_id = got_object_id_dup(&id);
1502 if (*unique_id == NULL) {
1503 err = got_error_from_errno("got_object_id_dup");
1504 free(id_str);
1505 goto done;
1507 } else {
1508 if (got_object_id_cmp(*unique_id, &id) == 0)
1509 continue; /* both packed and loose */
1510 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1511 free(id_str);
1512 goto done;
1515 done:
1516 if (dir && closedir(dir) != 0 && err == NULL)
1517 err = got_error_from_errno("closedir");
1518 if (err) {
1519 free(*unique_id);
1520 *unique_id = NULL;
1522 free(path);
1523 return err;
1526 const struct got_error *
1527 got_repo_match_object_id_prefix(struct got_object_id **id,
1528 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1530 const struct got_error *err = NULL;
1531 char *path_objects = got_repo_get_path_objects(repo);
1532 char *object_dir = NULL;
1533 size_t len;
1534 int i;
1536 *id = NULL;
1538 for (i = 0; i < strlen(id_str_prefix); i++) {
1539 if (isxdigit((unsigned char)id_str_prefix[i]))
1540 continue;
1541 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1544 len = strlen(id_str_prefix);
1545 if (len >= 2) {
1546 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1547 if (err)
1548 goto done;
1549 object_dir = strndup(id_str_prefix, 2);
1550 if (object_dir == NULL) {
1551 err = got_error_from_errno("strdup");
1552 goto done;
1554 err = match_loose_object(id, path_objects, object_dir,
1555 id_str_prefix, obj_type, repo);
1556 } else if (len == 1) {
1557 int i;
1558 for (i = 0; i < 0xf; i++) {
1559 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1560 == -1) {
1561 err = got_error_from_errno("asprintf");
1562 goto done;
1564 err = match_packed_object(id, repo, object_dir,
1565 obj_type);
1566 if (err)
1567 goto done;
1568 err = match_loose_object(id, path_objects, object_dir,
1569 id_str_prefix, obj_type, repo);
1570 if (err)
1571 goto done;
1573 } else {
1574 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1575 goto done;
1577 done:
1578 free(object_dir);
1579 if (err) {
1580 free(*id);
1581 *id = NULL;
1582 } else if (*id == NULL) {
1583 switch (obj_type) {
1584 case GOT_OBJ_TYPE_BLOB:
1585 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1586 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1587 break;
1588 case GOT_OBJ_TYPE_TREE:
1589 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1590 GOT_OBJ_LABEL_TREE, id_str_prefix);
1591 break;
1592 case GOT_OBJ_TYPE_COMMIT:
1593 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1594 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1595 break;
1596 case GOT_OBJ_TYPE_TAG:
1597 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1598 GOT_OBJ_LABEL_TAG, id_str_prefix);
1599 break;
1600 default:
1601 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1602 break;
1606 return err;
1609 const struct got_error *
1610 got_repo_match_object_id(struct got_object_id **id, char **label,
1611 const char *id_str, int obj_type, struct got_reflist_head *refs,
1612 struct got_repository *repo)
1614 const struct got_error *err;
1615 struct got_tag_object *tag;
1616 struct got_reference *ref = NULL;
1618 *id = NULL;
1619 if (label)
1620 *label = NULL;
1622 if (refs) {
1623 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1624 refs, repo);
1625 if (err == NULL) {
1626 *id = got_object_id_dup(
1627 got_object_tag_get_object_id(tag));
1628 if (*id == NULL)
1629 err = got_error_from_errno("got_object_id_dup");
1630 else if (label && asprintf(label, "refs/tags/%s",
1631 got_object_tag_get_name(tag)) == -1) {
1632 err = got_error_from_errno("asprintf");
1633 free(*id);
1634 *id = NULL;
1636 got_object_tag_close(tag);
1637 return err;
1638 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1639 err->code != GOT_ERR_NO_OBJ)
1640 return err;
1643 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1644 if (err) {
1645 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1646 return err;
1647 err = got_ref_open(&ref, repo, id_str, 0);
1648 if (err != NULL)
1649 goto done;
1650 if (label) {
1651 *label = strdup(got_ref_get_name(ref));
1652 if (*label == NULL) {
1653 err = got_error_from_errno("strdup");
1654 goto done;
1657 err = got_ref_resolve(id, repo, ref);
1658 } else if (label) {
1659 err = got_object_id_str(label, *id);
1660 if (*label == NULL) {
1661 err = got_error_from_errno("strdup");
1662 goto done;
1665 done:
1666 if (ref)
1667 got_ref_close(ref);
1668 return err;
1671 const struct got_error *
1672 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1673 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1675 const struct got_error *err = NULL;
1676 struct got_reflist_entry *re;
1677 struct got_object_id *tag_id;
1678 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1680 *tag = NULL;
1682 TAILQ_FOREACH(re, refs, entry) {
1683 const char *refname;
1684 refname = got_ref_get_name(re->ref);
1685 if (got_ref_is_symbolic(re->ref))
1686 continue;
1687 if (strncmp(refname, "refs/tags/", 10) != 0)
1688 continue;
1689 if (!name_is_absolute)
1690 refname += strlen("refs/tags/");
1691 if (strcmp(refname, name) != 0)
1692 continue;
1693 err = got_ref_resolve(&tag_id, repo, re->ref);
1694 if (err)
1695 break;
1696 err = got_object_open_as_tag(tag, repo, tag_id);
1697 free(tag_id);
1698 if (err)
1699 break;
1700 if (obj_type == GOT_OBJ_TYPE_ANY ||
1701 got_object_tag_get_object_type(*tag) == obj_type)
1702 break;
1703 got_object_tag_close(*tag);
1704 *tag = NULL;
1707 if (err == NULL && *tag == NULL)
1708 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1709 GOT_OBJ_LABEL_TAG, name);
1710 return err;
1713 static const struct got_error *
1714 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1715 const char *name, mode_t mode, struct got_object_id *blob_id)
1717 const struct got_error *err = NULL;
1719 *new_te = NULL;
1721 *new_te = calloc(1, sizeof(**new_te));
1722 if (*new_te == NULL)
1723 return got_error_from_errno("calloc");
1725 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1726 sizeof((*new_te)->name)) {
1727 err = got_error(GOT_ERR_NO_SPACE);
1728 goto done;
1731 if (S_ISLNK(mode)) {
1732 (*new_te)->mode = S_IFLNK;
1733 } else {
1734 (*new_te)->mode = S_IFREG;
1735 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1737 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1738 done:
1739 if (err && *new_te) {
1740 free(*new_te);
1741 *new_te = NULL;
1743 return err;
1746 static const struct got_error *
1747 import_file(struct got_tree_entry **new_te, struct dirent *de,
1748 const char *path, struct got_repository *repo)
1750 const struct got_error *err;
1751 struct got_object_id *blob_id = NULL;
1752 char *filepath;
1753 struct stat sb;
1755 if (asprintf(&filepath, "%s%s%s", path,
1756 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1757 return got_error_from_errno("asprintf");
1759 if (lstat(filepath, &sb) != 0) {
1760 err = got_error_from_errno2("lstat", path);
1761 goto done;
1764 err = got_object_blob_create(&blob_id, filepath, repo);
1765 if (err)
1766 goto done;
1768 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1769 blob_id);
1770 done:
1771 free(filepath);
1772 if (err)
1773 free(blob_id);
1774 return err;
1777 static const struct got_error *
1778 insert_tree_entry(struct got_tree_entry *new_te,
1779 struct got_pathlist_head *paths)
1781 const struct got_error *err = NULL;
1782 struct got_pathlist_entry *new_pe;
1784 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1785 if (err)
1786 return err;
1787 if (new_pe == NULL)
1788 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1789 return NULL;
1792 static const struct got_error *write_tree(struct got_object_id **,
1793 const char *, struct got_pathlist_head *, struct got_repository *,
1794 got_repo_import_cb progress_cb, void *progress_arg);
1796 static const struct got_error *
1797 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1798 const char *path, struct got_pathlist_head *ignores,
1799 struct got_repository *repo,
1800 got_repo_import_cb progress_cb, void *progress_arg)
1802 const struct got_error *err;
1803 struct got_object_id *id = NULL;
1804 char *subdirpath;
1806 if (asprintf(&subdirpath, "%s%s%s", path,
1807 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1808 return got_error_from_errno("asprintf");
1810 (*new_te) = calloc(1, sizeof(**new_te));
1811 if (*new_te == NULL)
1812 return got_error_from_errno("calloc");
1813 (*new_te)->mode = S_IFDIR;
1814 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1815 sizeof((*new_te)->name)) {
1816 err = got_error(GOT_ERR_NO_SPACE);
1817 goto done;
1819 err = write_tree(&id, subdirpath, ignores, repo,
1820 progress_cb, progress_arg);
1821 if (err)
1822 goto done;
1823 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1825 done:
1826 free(id);
1827 free(subdirpath);
1828 if (err) {
1829 free(*new_te);
1830 *new_te = NULL;
1832 return err;
1835 static const struct got_error *
1836 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1837 struct got_pathlist_head *ignores, struct got_repository *repo,
1838 got_repo_import_cb progress_cb, void *progress_arg)
1840 const struct got_error *err = NULL;
1841 DIR *dir;
1842 struct dirent *de;
1843 int nentries;
1844 struct got_tree_entry *new_te = NULL;
1845 struct got_pathlist_head paths;
1846 struct got_pathlist_entry *pe;
1848 *new_tree_id = NULL;
1850 TAILQ_INIT(&paths);
1852 dir = opendir(path_dir);
1853 if (dir == NULL) {
1854 err = got_error_from_errno2("opendir", path_dir);
1855 goto done;
1858 nentries = 0;
1859 while ((de = readdir(dir)) != NULL) {
1860 int ignore = 0;
1861 int type;
1863 if (strcmp(de->d_name, ".") == 0 ||
1864 strcmp(de->d_name, "..") == 0)
1865 continue;
1867 TAILQ_FOREACH(pe, ignores, entry) {
1868 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1869 ignore = 1;
1870 break;
1873 if (ignore)
1874 continue;
1876 err = got_path_dirent_type(&type, path_dir, de);
1877 if (err)
1878 goto done;
1880 if (type == DT_DIR) {
1881 err = import_subdir(&new_te, de, path_dir,
1882 ignores, repo, progress_cb, progress_arg);
1883 if (err) {
1884 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1885 goto done;
1886 err = NULL;
1887 continue;
1889 } else if (type == DT_REG || type == DT_LNK) {
1890 err = import_file(&new_te, de, path_dir, repo);
1891 if (err)
1892 goto done;
1893 } else
1894 continue;
1896 err = insert_tree_entry(new_te, &paths);
1897 if (err)
1898 goto done;
1899 nentries++;
1902 if (TAILQ_EMPTY(&paths)) {
1903 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1904 "cannot create tree without any entries");
1905 goto done;
1908 TAILQ_FOREACH(pe, &paths, entry) {
1909 struct got_tree_entry *te = pe->data;
1910 char *path;
1911 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1912 continue;
1913 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1914 err = got_error_from_errno("asprintf");
1915 goto done;
1917 err = (*progress_cb)(progress_arg, path);
1918 free(path);
1919 if (err)
1920 goto done;
1923 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1924 done:
1925 if (dir)
1926 closedir(dir);
1927 got_pathlist_free(&paths);
1928 return err;
1931 const struct got_error *
1932 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1933 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1934 struct got_repository *repo, got_repo_import_cb progress_cb,
1935 void *progress_arg)
1937 const struct got_error *err;
1938 struct got_object_id *new_tree_id;
1940 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1941 progress_cb, progress_arg);
1942 if (err)
1943 return err;
1945 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1946 author, time(NULL), author, time(NULL), logmsg, repo);
1947 free(new_tree_id);
1948 return err;
1951 const struct got_error *
1952 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1953 struct got_repository *repo)
1955 const struct got_error *err = NULL;
1956 char *path_objects = NULL, *path = NULL;
1957 DIR *dir = NULL;
1958 struct got_object_id id;
1959 int i;
1961 *nobjects = 0;
1962 *ondisk_size = 0;
1964 path_objects = got_repo_get_path_objects(repo);
1965 if (path_objects == NULL)
1966 return got_error_from_errno("got_repo_get_path_objects");
1968 for (i = 0; i <= 0xff; i++) {
1969 struct dirent *dent;
1971 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1972 err = got_error_from_errno("asprintf");
1973 break;
1976 dir = opendir(path);
1977 if (dir == NULL) {
1978 if (errno == ENOENT) {
1979 err = NULL;
1980 continue;
1982 err = got_error_from_errno2("opendir", path);
1983 break;
1986 while ((dent = readdir(dir)) != NULL) {
1987 char *id_str;
1988 int fd;
1989 struct stat sb;
1991 if (strcmp(dent->d_name, ".") == 0 ||
1992 strcmp(dent->d_name, "..") == 0)
1993 continue;
1995 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1996 err = got_error_from_errno("asprintf");
1997 goto done;
2000 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2001 free(id_str);
2002 continue;
2004 free(id_str);
2006 err = got_object_open_loose_fd(&fd, &id, repo);
2007 if (err)
2008 goto done;
2010 if (fstat(fd, &sb) == -1) {
2011 err = got_error_from_errno("fstat");
2012 close(fd);
2013 goto done;
2015 (*nobjects)++;
2016 (*ondisk_size) += sb.st_size;
2018 if (close(fd) == -1) {
2019 err = got_error_from_errno("close");
2020 goto done;
2024 if (closedir(dir) != 0) {
2025 err = got_error_from_errno("closedir");
2026 goto done;
2028 dir = NULL;
2030 free(path);
2031 path = NULL;
2033 done:
2034 if (dir && closedir(dir) != 0 && err == NULL)
2035 err = got_error_from_errno("closedir");
2037 if (err) {
2038 *nobjects = 0;
2039 *ondisk_size = 0;
2041 free(path_objects);
2042 free(path);
2043 return err;
2046 const struct got_error *
2047 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2048 off_t *total_packsize, struct got_repository *repo)
2050 const struct got_error *err = NULL;
2051 DIR *packdir = NULL;
2052 struct dirent *dent;
2053 struct got_packidx *packidx = NULL;
2054 char *path_packidx;
2055 char *path_packfile;
2056 int packdir_fd;
2057 struct stat sb;
2059 *npackfiles = 0;
2060 *nobjects = 0;
2061 *total_packsize = 0;
2063 packdir_fd = openat(got_repo_get_fd(repo),
2064 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2065 if (packdir_fd == -1) {
2066 return got_error_from_errno_fmt("openat: %s/%s",
2067 got_repo_get_path_git_dir(repo),
2068 GOT_OBJECTS_PACK_DIR);
2071 packdir = fdopendir(packdir_fd);
2072 if (packdir == NULL) {
2073 err = got_error_from_errno("fdopendir");
2074 goto done;
2077 while ((dent = readdir(packdir)) != NULL) {
2078 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2079 continue;
2081 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2082 dent->d_name) == -1) {
2083 err = got_error_from_errno("asprintf");
2084 goto done;
2087 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2088 path_packidx, 0);
2089 free(path_packidx);
2090 if (err)
2091 goto done;
2093 if (fstat(packidx->fd, &sb) == -1)
2094 goto done;
2095 *total_packsize += sb.st_size;
2097 err = got_packidx_get_packfile_path(&path_packfile,
2098 packidx->path_packidx);
2099 if (err)
2100 goto done;
2102 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2103 0) == -1) {
2104 free(path_packfile);
2105 goto done;
2107 free(path_packfile);
2108 *total_packsize += sb.st_size;
2110 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2112 (*npackfiles)++;
2114 got_packidx_close(packidx);
2115 packidx = NULL;
2117 done:
2118 if (packidx)
2119 got_packidx_close(packidx);
2120 if (packdir && closedir(packdir) != 0 && err == NULL)
2121 err = got_error_from_errno("closedir");
2122 if (err) {
2123 *npackfiles = 0;
2124 *nobjects = 0;
2125 *total_packsize = 0;
2127 return err;