Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/uio.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/resource.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <fnmatch.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <errno.h>
36 #include <libgen.h>
37 #include <stdint.h>
38 #include <uuid.h>
40 #include "got_compat.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
63 #endif
65 const char *
66 got_repo_get_path(struct got_repository *repo)
67 {
68 return repo->path;
69 }
71 const char *
72 got_repo_get_path_git_dir(struct got_repository *repo)
73 {
74 return repo->path_git_dir;
75 }
77 int
78 got_repo_get_fd(struct got_repository *repo)
79 {
80 return repo->gitdir_fd;
81 }
83 const char *
84 got_repo_get_gitconfig_author_name(struct got_repository *repo)
85 {
86 return repo->gitconfig_author_name;
87 }
89 const char *
90 got_repo_get_gitconfig_author_email(struct got_repository *repo)
91 {
92 return repo->gitconfig_author_email;
93 }
95 const char *
96 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
97 {
98 return repo->global_gitconfig_author_name;
99 }
101 const char *
102 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
104 return repo->global_gitconfig_author_email;
107 const char *
108 got_repo_get_gitconfig_owner(struct got_repository *repo)
110 return repo->gitconfig_owner;
113 void
114 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
115 struct got_repository *repo)
117 *extensions = repo->extensions;
118 *nextensions = repo->nextensions;
121 int
122 got_repo_is_bare(struct got_repository *repo)
124 return (strcmp(repo->path, repo->path_git_dir) == 0);
127 static char *
128 get_path_git_child(struct got_repository *repo, const char *basename)
130 char *path_child;
132 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
133 basename) == -1)
134 return NULL;
136 return path_child;
139 char *
140 got_repo_get_path_objects(struct got_repository *repo)
142 return get_path_git_child(repo, GOT_OBJECTS_DIR);
145 char *
146 got_repo_get_path_objects_pack(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
151 char *
152 got_repo_get_path_refs(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_REFS_DIR);
157 char *
158 got_repo_get_path_packed_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
163 static char *
164 get_path_head(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_HEAD_FILE);
169 char *
170 got_repo_get_path_gitconfig(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_GITCONFIG);
175 char *
176 got_repo_get_path_gotconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
181 const struct got_gotconfig *
182 got_repo_get_gotconfig(struct got_repository *repo)
184 return repo->gotconfig;
187 void
188 got_repo_get_gitconfig_remotes(int *nremotes,
189 const struct got_remote_repo **remotes, struct got_repository *repo)
191 *nremotes = repo->ngitconfig_remotes;
192 *remotes = repo->gitconfig_remotes;
195 static int
196 is_git_repo(struct got_repository *repo)
198 const char *path_git = got_repo_get_path_git_dir(repo);
199 char *path_objects = got_repo_get_path_objects(repo);
200 char *path_refs = got_repo_get_path_refs(repo);
201 char *path_head = get_path_head(repo);
202 int ret = 0;
203 struct stat sb;
204 struct got_reference *head_ref;
206 if (lstat(path_git, &sb) == -1)
207 goto done;
208 if (!S_ISDIR(sb.st_mode))
209 goto done;
211 if (lstat(path_objects, &sb) == -1)
212 goto done;
213 if (!S_ISDIR(sb.st_mode))
214 goto done;
216 if (lstat(path_refs, &sb) == -1)
217 goto done;
218 if (!S_ISDIR(sb.st_mode))
219 goto done;
221 if (lstat(path_head, &sb) == -1)
222 goto done;
223 if (!S_ISREG(sb.st_mode))
224 goto done;
226 /* Check if the HEAD reference can be opened. */
227 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
228 goto done;
229 got_ref_close(head_ref);
231 ret = 1;
232 done:
233 free(path_objects);
234 free(path_refs);
235 free(path_head);
236 return ret;
240 const struct got_error *
241 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
242 struct got_object *obj)
244 #ifndef GOT_NO_OBJ_CACHE
245 const struct got_error *err = NULL;
246 err = got_object_cache_add(&repo->objcache, id, obj);
247 if (err) {
248 if (err->code == GOT_ERR_OBJ_EXISTS ||
249 err->code == GOT_ERR_OBJ_TOO_LARGE)
250 err = NULL;
251 return err;
253 obj->refcnt++;
254 #endif
255 return NULL;
258 struct got_object *
259 got_repo_get_cached_object(struct got_repository *repo,
260 struct got_object_id *id)
262 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
265 const struct got_error *
266 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
267 struct got_tree_object *tree)
269 #ifndef GOT_NO_OBJ_CACHE
270 const struct got_error *err = NULL;
271 err = got_object_cache_add(&repo->treecache, id, tree);
272 if (err) {
273 if (err->code == GOT_ERR_OBJ_EXISTS ||
274 err->code == GOT_ERR_OBJ_TOO_LARGE)
275 err = NULL;
276 return err;
278 tree->refcnt++;
279 #endif
280 return NULL;
283 struct got_tree_object *
284 got_repo_get_cached_tree(struct got_repository *repo,
285 struct got_object_id *id)
287 return (struct got_tree_object *)got_object_cache_get(
288 &repo->treecache, id);
291 const struct got_error *
292 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
293 struct got_commit_object *commit)
295 #ifndef GOT_NO_OBJ_CACHE
296 const struct got_error *err = NULL;
297 err = got_object_cache_add(&repo->commitcache, id, commit);
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 commit->refcnt++;
305 #endif
306 return NULL;
309 struct got_commit_object *
310 got_repo_get_cached_commit(struct got_repository *repo,
311 struct got_object_id *id)
313 return (struct got_commit_object *)got_object_cache_get(
314 &repo->commitcache, id);
317 const struct got_error *
318 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
319 struct got_tag_object *tag)
321 #ifndef GOT_NO_OBJ_CACHE
322 const struct got_error *err = NULL;
323 err = got_object_cache_add(&repo->tagcache, id, tag);
324 if (err) {
325 if (err->code == GOT_ERR_OBJ_EXISTS ||
326 err->code == GOT_ERR_OBJ_TOO_LARGE)
327 err = NULL;
328 return err;
330 tag->refcnt++;
331 #endif
332 return NULL;
335 struct got_tag_object *
336 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
338 return (struct got_tag_object *)got_object_cache_get(
339 &repo->tagcache, id);
342 static const struct got_error *
343 open_repo(struct got_repository *repo, const char *path)
345 const struct got_error *err = NULL;
347 repo->gitdir_fd = -1;
349 /* bare git repository? */
350 repo->path_git_dir = strdup(path);
351 if (repo->path_git_dir == NULL)
352 return got_error_from_errno("strdup");
353 if (is_git_repo(repo)) {
354 repo->path = strdup(repo->path_git_dir);
355 if (repo->path == NULL) {
356 err = got_error_from_errno("strdup");
357 goto done;
359 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
360 if (repo->gitdir_fd == -1) {
361 err = got_error_from_errno2("open",
362 repo->path_git_dir);
363 goto done;
365 return NULL;
368 /* git repository with working tree? */
369 free(repo->path_git_dir);
370 repo->path_git_dir = NULL;
371 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
372 err = got_error_from_errno("asprintf");
373 goto done;
375 if (is_git_repo(repo)) {
376 repo->path = strdup(path);
377 if (repo->path == NULL) {
378 err = got_error_from_errno("strdup");
379 goto done;
381 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
382 if (repo->gitdir_fd == -1) {
383 err = got_error_from_errno2("open",
384 repo->path_git_dir);
385 goto done;
387 return NULL;
390 err = got_error(GOT_ERR_NOT_GIT_REPO);
391 done:
392 if (err) {
393 free(repo->path);
394 repo->path = NULL;
395 free(repo->path_git_dir);
396 repo->path_git_dir = NULL;
397 if (repo->gitdir_fd != -1)
398 close(repo->gitdir_fd);
399 repo->gitdir_fd = -1;
402 return err;
405 static const struct got_error *
406 parse_gitconfig_file(int *gitconfig_repository_format_version,
407 char **gitconfig_author_name, char **gitconfig_author_email,
408 struct got_remote_repo **remotes, int *nremotes,
409 char **gitconfig_owner, char ***extensions, int *nextensions,
410 const char *gitconfig_path)
412 const struct got_error *err = NULL, *child_err = NULL;
413 int fd = -1;
414 int imsg_fds[2] = { -1, -1 };
415 pid_t pid;
416 struct imsgbuf *ibuf;
418 *gitconfig_repository_format_version = 0;
419 if (extensions)
420 *extensions = NULL;
421 if (nextensions)
422 *nextensions = 0;
423 *gitconfig_author_name = NULL;
424 *gitconfig_author_email = NULL;
425 if (remotes)
426 *remotes = NULL;
427 if (nremotes)
428 *nremotes = 0;
429 if (gitconfig_owner)
430 *gitconfig_owner = NULL;
432 fd = open(gitconfig_path, O_RDONLY);
433 if (fd == -1) {
434 if (errno == ENOENT)
435 return NULL;
436 return got_error_from_errno2("open", gitconfig_path);
439 ibuf = calloc(1, sizeof(*ibuf));
440 if (ibuf == NULL) {
441 err = got_error_from_errno("calloc");
442 goto done;
445 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
446 err = got_error_from_errno("socketpair");
447 goto done;
450 pid = fork();
451 if (pid == -1) {
452 err = got_error_from_errno("fork");
453 goto done;
454 } else if (pid == 0) {
455 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
456 gitconfig_path);
457 /* not reached */
460 if (close(imsg_fds[1]) == -1) {
461 err = got_error_from_errno("close");
462 goto done;
464 imsg_fds[1] = -1;
465 imsg_init(ibuf, imsg_fds[0]);
467 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
468 if (err)
469 goto done;
470 fd = -1;
472 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
473 if (err)
474 goto done;
476 err = got_privsep_recv_gitconfig_int(
477 gitconfig_repository_format_version, ibuf);
478 if (err)
479 goto done;
481 if (extensions && nextensions) {
482 err = got_privsep_send_gitconfig_repository_extensions_req(
483 ibuf);
484 if (err)
485 goto done;
486 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
487 if (err)
488 goto done;
489 if (*nextensions > 0) {
490 int i;
491 *extensions = calloc(*nextensions, sizeof(char *));
492 if (*extensions == NULL) {
493 err = got_error_from_errno("calloc");
494 goto done;
496 for (i = 0; i < *nextensions; i++) {
497 char *ext;
498 err = got_privsep_recv_gitconfig_str(&ext,
499 ibuf);
500 if (err)
501 goto done;
502 (*extensions)[i] = ext;
507 err = got_privsep_send_gitconfig_author_name_req(ibuf);
508 if (err)
509 goto done;
511 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
512 if (err)
513 goto done;
515 err = got_privsep_send_gitconfig_author_email_req(ibuf);
516 if (err)
517 goto done;
519 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
520 if (err)
521 goto done;
523 if (remotes && nremotes) {
524 err = got_privsep_send_gitconfig_remotes_req(ibuf);
525 if (err)
526 goto done;
528 err = got_privsep_recv_gitconfig_remotes(remotes,
529 nremotes, ibuf);
530 if (err)
531 goto done;
534 if (gitconfig_owner) {
535 err = got_privsep_send_gitconfig_owner_req(ibuf);
536 if (err)
537 goto done;
538 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
539 if (err)
540 goto done;
543 imsg_clear(ibuf);
544 err = got_privsep_send_stop(imsg_fds[0]);
545 child_err = got_privsep_wait_for_child(pid);
546 if (child_err && err == NULL)
547 err = child_err;
548 done:
549 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
550 err = got_error_from_errno("close");
551 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
552 err = got_error_from_errno("close");
553 if (fd != -1 && close(fd) == -1 && err == NULL)
554 err = got_error_from_errno2("close", gitconfig_path);
555 free(ibuf);
556 return err;
559 static const struct got_error *
560 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
562 const struct got_error *err = NULL;
563 char *repo_gitconfig_path = NULL;
565 if (global_gitconfig_path) {
566 /* Read settings from ~/.gitconfig. */
567 int dummy_repo_version;
568 err = parse_gitconfig_file(&dummy_repo_version,
569 &repo->global_gitconfig_author_name,
570 &repo->global_gitconfig_author_email,
571 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
572 if (err)
573 return err;
576 /* Read repository's .git/config file. */
577 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
578 if (repo_gitconfig_path == NULL)
579 return got_error_from_errno("got_repo_get_path_gitconfig");
581 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
582 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
583 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
584 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
585 repo_gitconfig_path);
586 if (err)
587 goto done;
588 done:
589 free(repo_gitconfig_path);
590 return err;
593 static const struct got_error *
594 read_gotconfig(struct got_repository *repo)
596 const struct got_error *err = NULL;
597 char *gotconfig_path;
599 gotconfig_path = got_repo_get_path_gotconfig(repo);
600 if (gotconfig_path == NULL)
601 return got_error_from_errno("got_repo_get_path_gotconfig");
603 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
604 free(gotconfig_path);
605 return err;
608 /* Supported repository format extensions. */
609 static const char *repo_extensions[] = {
610 "noop", /* Got supports repository format version 1. */
611 "preciousObjects", /* Supported by gotadmin cleanup. */
612 "worktreeConfig", /* Got does not care about Git work trees. */
613 };
615 const struct got_error *
616 got_repo_open(struct got_repository **repop, const char *path,
617 const char *global_gitconfig_path)
619 struct got_repository *repo = NULL;
620 const struct got_error *err = NULL;
621 char *repo_path = NULL;
622 size_t i;
623 struct rlimit rl;
625 *repop = NULL;
627 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
628 return got_error_from_errno("getrlimit");
630 repo = calloc(1, sizeof(*repo));
631 if (repo == NULL) {
632 err = got_error_from_errno("calloc");
633 goto done;
636 for (i = 0; i < nitems(repo->privsep_children); i++) {
637 memset(&repo->privsep_children[i], 0,
638 sizeof(repo->privsep_children[0]));
639 repo->privsep_children[i].imsg_fd = -1;
642 err = got_object_cache_init(&repo->objcache,
643 GOT_OBJECT_CACHE_TYPE_OBJ);
644 if (err)
645 goto done;
646 err = got_object_cache_init(&repo->treecache,
647 GOT_OBJECT_CACHE_TYPE_TREE);
648 if (err)
649 goto done;
650 err = got_object_cache_init(&repo->commitcache,
651 GOT_OBJECT_CACHE_TYPE_COMMIT);
652 if (err)
653 goto done;
654 err = got_object_cache_init(&repo->tagcache,
655 GOT_OBJECT_CACHE_TYPE_TAG);
656 if (err)
657 goto done;
659 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
660 if (repo->pack_cache_size > rl.rlim_cur / 8)
661 repo->pack_cache_size = rl.rlim_cur / 8;
663 repo_path = realpath(path, NULL);
664 if (repo_path == NULL) {
665 err = got_error_from_errno2("realpath", path);
666 goto done;
669 for (;;) {
670 char *parent_path;
672 err = open_repo(repo, repo_path);
673 if (err == NULL)
674 break;
675 if (err->code != GOT_ERR_NOT_GIT_REPO)
676 goto done;
677 if (repo_path[0] == '/' && repo_path[1] == '\0') {
678 err = got_error(GOT_ERR_NOT_GIT_REPO);
679 goto done;
681 err = got_path_dirname(&parent_path, repo_path);
682 if (err)
683 goto done;
684 free(repo_path);
685 repo_path = parent_path;
688 err = read_gotconfig(repo);
689 if (err)
690 goto done;
692 err = read_gitconfig(repo, global_gitconfig_path);
693 if (err)
694 goto done;
695 if (repo->gitconfig_repository_format_version != 0)
696 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
697 for (i = 0; i < repo->nextensions; i++) {
698 char *ext = repo->extensions[i];
699 int j, supported = 0;
700 for (j = 0; j < nitems(repo_extensions); j++) {
701 if (strcmp(ext, repo_extensions[j]) == 0) {
702 supported = 1;
703 break;
706 if (!supported) {
707 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
708 goto done;
711 done:
712 if (err)
713 got_repo_close(repo);
714 else
715 *repop = repo;
716 free(repo_path);
717 return err;
720 const struct got_error *
721 got_repo_close(struct got_repository *repo)
723 const struct got_error *err = NULL, *child_err;
724 size_t i;
726 for (i = 0; i < repo->pack_cache_size; i++) {
727 if (repo->packidx_cache[i] == NULL)
728 break;
729 got_packidx_close(repo->packidx_cache[i]);
732 for (i = 0; i < repo->pack_cache_size; i++) {
733 if (repo->packs[i].path_packfile == NULL)
734 break;
735 got_pack_close(&repo->packs[i]);
738 free(repo->path);
739 free(repo->path_git_dir);
741 got_object_cache_close(&repo->objcache);
742 got_object_cache_close(&repo->treecache);
743 got_object_cache_close(&repo->commitcache);
744 got_object_cache_close(&repo->tagcache);
746 for (i = 0; i < nitems(repo->privsep_children); i++) {
747 if (repo->privsep_children[i].imsg_fd == -1)
748 continue;
749 imsg_clear(repo->privsep_children[i].ibuf);
750 free(repo->privsep_children[i].ibuf);
751 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
752 child_err = got_privsep_wait_for_child(
753 repo->privsep_children[i].pid);
754 if (child_err && err == NULL)
755 err = child_err;
756 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
757 err == NULL)
758 err = got_error_from_errno("close");
761 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
762 err == NULL)
763 err = got_error_from_errno("close");
765 if (repo->gotconfig)
766 got_gotconfig_free(repo->gotconfig);
767 free(repo->gitconfig_author_name);
768 free(repo->gitconfig_author_email);
769 for (i = 0; i < repo->ngitconfig_remotes; i++)
770 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
771 free(repo->gitconfig_remotes);
772 for (i = 0; i < repo->nextensions; i++)
773 free(repo->extensions[i]);
774 free(repo->extensions);
775 free(repo);
777 return err;
780 void
781 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
783 int i;
785 free(repo->name);
786 repo->name = NULL;
787 free(repo->fetch_url);
788 repo->fetch_url = NULL;
789 free(repo->send_url);
790 repo->send_url = NULL;
791 for (i = 0; i < repo->nfetch_branches; i++)
792 free(repo->fetch_branches[i]);
793 free(repo->fetch_branches);
794 repo->fetch_branches = NULL;
795 repo->nfetch_branches = 0;
796 for (i = 0; i < repo->nsend_branches; i++)
797 free(repo->send_branches[i]);
798 free(repo->send_branches);
799 repo->send_branches = NULL;
800 repo->nsend_branches = 0;
803 const struct got_error *
804 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
805 const char *input_path)
807 const struct got_error *err = NULL;
808 const char *repo_abspath = NULL;
809 size_t repolen, len;
810 char *canonpath, *path = NULL;
812 *in_repo_path = NULL;
814 canonpath = strdup(input_path);
815 if (canonpath == NULL) {
816 err = got_error_from_errno("strdup");
817 goto done;
819 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
820 if (err)
821 goto done;
823 repo_abspath = got_repo_get_path(repo);
825 if (canonpath[0] == '\0') {
826 path = strdup(canonpath);
827 if (path == NULL) {
828 err = got_error_from_errno("strdup");
829 goto done;
831 } else {
832 path = realpath(canonpath, NULL);
833 if (path == NULL) {
834 if (errno != ENOENT) {
835 err = got_error_from_errno2("realpath",
836 canonpath);
837 goto done;
839 /*
840 * Path is not on disk.
841 * Assume it is already relative to repository root.
842 */
843 path = strdup(canonpath);
844 if (path == NULL) {
845 err = got_error_from_errno("strdup");
846 goto done;
850 repolen = strlen(repo_abspath);
851 len = strlen(path);
854 if (strcmp(path, repo_abspath) == 0) {
855 free(path);
856 path = strdup("");
857 if (path == NULL) {
858 err = got_error_from_errno("strdup");
859 goto done;
861 } else if (len > repolen &&
862 got_path_is_child(path, repo_abspath, repolen)) {
863 /* Matched an on-disk path inside repository. */
864 if (got_repo_is_bare(repo)) {
865 /*
866 * Matched an on-disk path inside repository
867 * database. Treat input as repository-relative.
868 */
869 free(path);
870 path = canonpath;
871 canonpath = NULL;
872 } else {
873 char *child;
874 /* Strip common prefix with repository path. */
875 err = got_path_skip_common_ancestor(&child,
876 repo_abspath, path);
877 if (err)
878 goto done;
879 free(path);
880 path = child;
882 } else {
883 /*
884 * Matched unrelated on-disk path.
885 * Treat input as repository-relative.
886 */
887 free(path);
888 path = canonpath;
889 canonpath = NULL;
893 /* Make in-repository path absolute */
894 if (path[0] != '/') {
895 char *abspath;
896 if (asprintf(&abspath, "/%s", path) == -1) {
897 err = got_error_from_errno("asprintf");
898 goto done;
900 free(path);
901 path = abspath;
904 done:
905 free(canonpath);
906 if (err)
907 free(path);
908 else
909 *in_repo_path = path;
910 return err;
913 static const struct got_error *
914 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
915 const char *path_packidx)
917 const struct got_error *err = NULL;
918 size_t i;
920 for (i = 0; i < repo->pack_cache_size; i++) {
921 if (repo->packidx_cache[i] == NULL)
922 break;
923 if (strcmp(repo->packidx_cache[i]->path_packidx,
924 path_packidx) == 0) {
925 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
928 if (i == repo->pack_cache_size) {
929 i = repo->pack_cache_size - 1;
930 err = got_packidx_close(repo->packidx_cache[i]);
931 if (err)
932 return err;
935 repo->packidx_cache[i] = packidx;
937 return NULL;
940 int
941 got_repo_is_packidx_filename(const char *name, size_t len)
943 if (len != GOT_PACKIDX_NAMELEN)
944 return 0;
946 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
947 return 0;
949 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
950 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
951 return 0;
953 return 1;
956 const struct got_error *
957 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
958 struct got_repository *repo, struct got_object_id *id)
960 const struct got_error *err;
961 DIR *packdir = NULL;
962 struct dirent *dent;
963 char *path_packidx;
964 size_t i;
965 int packdir_fd;
967 /* Search pack index cache. */
968 for (i = 0; i < repo->pack_cache_size; i++) {
969 if (repo->packidx_cache[i] == NULL)
970 break;
971 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
972 if (*idx != -1) {
973 *packidx = repo->packidx_cache[i];
974 /*
975 * Move this cache entry to the front. Repeatedly
976 * searching a wrong pack index can be expensive.
977 */
978 if (i > 0) {
979 memmove(&repo->packidx_cache[1],
980 &repo->packidx_cache[0],
981 i * sizeof(repo->packidx_cache[0]));
982 repo->packidx_cache[0] = *packidx;
984 return NULL;
987 /* No luck. Search the filesystem. */
989 packdir_fd = openat(got_repo_get_fd(repo),
990 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
991 if (packdir_fd == -1) {
992 if (errno == ENOENT)
993 err = got_error_no_obj(id);
994 else
995 err = got_error_from_errno_fmt("openat: %s/%s",
996 got_repo_get_path_git_dir(repo),
997 GOT_OBJECTS_PACK_DIR);
998 goto done;
1001 packdir = fdopendir(packdir_fd);
1002 if (packdir == NULL) {
1003 err = got_error_from_errno("fdopendir");
1004 goto done;
1007 while ((dent = readdir(packdir)) != NULL) {
1008 int is_cached = 0;
1010 if (!got_repo_is_packidx_filename(dent->d_name,
1011 strlen(dent->d_name)))
1012 continue;
1014 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1015 dent->d_name) == -1) {
1016 err = got_error_from_errno("asprintf");
1017 goto done;
1020 for (i = 0; i < repo->pack_cache_size; i++) {
1021 if (repo->packidx_cache[i] == NULL)
1022 break;
1023 if (strcmp(repo->packidx_cache[i]->path_packidx,
1024 path_packidx) == 0) {
1025 is_cached = 1;
1026 break;
1029 if (is_cached) {
1030 free(path_packidx);
1031 continue; /* already searched */
1034 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1035 path_packidx, 0);
1036 if (err) {
1037 free(path_packidx);
1038 goto done;
1041 err = cache_packidx(repo, *packidx, path_packidx);
1042 free(path_packidx);
1043 if (err)
1044 goto done;
1046 *idx = got_packidx_get_object_idx(*packidx, id);
1047 if (*idx != -1) {
1048 err = NULL; /* found the object */
1049 goto done;
1053 err = got_error_no_obj(id);
1054 done:
1055 if (packdir && closedir(packdir) != 0 && err == NULL)
1056 err = got_error_from_errno("closedir");
1057 return err;
1060 static const struct got_error *
1061 read_packfile_hdr(int fd, struct got_packidx *packidx)
1063 const struct got_error *err = NULL;
1064 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1065 struct got_packfile_hdr hdr;
1066 ssize_t n;
1068 n = read(fd, &hdr, sizeof(hdr));
1069 if (n < 0)
1070 return got_error_from_errno("read");
1071 if (n != sizeof(hdr))
1072 return got_error(GOT_ERR_BAD_PACKFILE);
1074 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1075 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1076 be32toh(hdr.nobjects) != totobj)
1077 err = got_error(GOT_ERR_BAD_PACKFILE);
1079 return err;
1082 static const struct got_error *
1083 open_packfile(int *fd, struct got_repository *repo,
1084 const char *relpath, struct got_packidx *packidx)
1086 const struct got_error *err = NULL;
1088 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1089 if (*fd == -1)
1090 return got_error_from_errno_fmt("openat: %s/%s",
1091 got_repo_get_path_git_dir(repo), relpath);
1093 if (packidx) {
1094 err = read_packfile_hdr(*fd, packidx);
1095 if (err) {
1096 close(*fd);
1097 *fd = -1;
1101 return err;
1104 const struct got_error *
1105 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1106 const char *path_packfile, struct got_packidx *packidx)
1108 const struct got_error *err = NULL;
1109 struct got_pack *pack = NULL;
1110 struct stat sb;
1111 size_t i;
1113 if (packp)
1114 *packp = NULL;
1116 for (i = 0; i < repo->pack_cache_size; i++) {
1117 pack = &repo->packs[i];
1118 if (pack->path_packfile == NULL)
1119 break;
1120 if (strcmp(pack->path_packfile, path_packfile) == 0)
1121 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1124 if (i == repo->pack_cache_size) {
1125 err = got_pack_close(&repo->packs[i - 1]);
1126 if (err)
1127 return err;
1128 memmove(&repo->packs[1], &repo->packs[0],
1129 sizeof(repo->packs) - sizeof(repo->packs[0]));
1130 i = 0;
1133 pack = &repo->packs[i];
1135 pack->path_packfile = strdup(path_packfile);
1136 if (pack->path_packfile == NULL) {
1137 err = got_error_from_errno("strdup");
1138 goto done;
1141 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1142 if (err)
1143 goto done;
1145 if (fstat(pack->fd, &sb) != 0) {
1146 err = got_error_from_errno("fstat");
1147 goto done;
1149 pack->filesize = sb.st_size;
1151 pack->privsep_child = NULL;
1153 #ifndef GOT_PACK_NO_MMAP
1154 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1155 pack->fd, 0);
1156 if (pack->map == MAP_FAILED) {
1157 if (errno != ENOMEM) {
1158 err = got_error_from_errno("mmap");
1159 goto done;
1161 pack->map = NULL; /* fall back to read(2) */
1163 #endif
1164 done:
1165 if (err) {
1166 if (pack) {
1167 free(pack->path_packfile);
1168 memset(pack, 0, sizeof(*pack));
1170 } else if (packp)
1171 *packp = pack;
1172 return err;
1175 struct got_pack *
1176 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1178 struct got_pack *pack = NULL;
1179 size_t i;
1181 for (i = 0; i < repo->pack_cache_size; i++) {
1182 pack = &repo->packs[i];
1183 if (pack->path_packfile == NULL)
1184 break;
1185 if (strcmp(pack->path_packfile, path_packfile) == 0)
1186 return pack;
1189 return NULL;
1192 const struct got_error *
1193 got_repo_init(const char *repo_path)
1195 const struct got_error *err = NULL;
1196 const char *dirnames[] = {
1197 GOT_OBJECTS_DIR,
1198 GOT_OBJECTS_PACK_DIR,
1199 GOT_REFS_DIR,
1201 const char *description_str = "Unnamed repository; "
1202 "edit this file 'description' to name the repository.";
1203 const char *headref_str = "ref: refs/heads/main";
1204 const char *gitconfig_str = "[core]\n"
1205 "\trepositoryformatversion = 0\n"
1206 "\tfilemode = true\n"
1207 "\tbare = true\n";
1208 char *path;
1209 size_t i;
1211 if (!got_path_dir_is_empty(repo_path))
1212 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1214 for (i = 0; i < nitems(dirnames); i++) {
1215 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1216 return got_error_from_errno("asprintf");
1218 err = got_path_mkdir(path);
1219 free(path);
1220 if (err)
1221 return err;
1224 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1225 return got_error_from_errno("asprintf");
1226 err = got_path_create_file(path, description_str);
1227 free(path);
1228 if (err)
1229 return err;
1231 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1232 return got_error_from_errno("asprintf");
1233 err = got_path_create_file(path, headref_str);
1234 free(path);
1235 if (err)
1236 return err;
1238 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1239 return got_error_from_errno("asprintf");
1240 err = got_path_create_file(path, gitconfig_str);
1241 free(path);
1242 if (err)
1243 return err;
1245 return NULL;
1248 static const struct got_error *
1249 match_packed_object(struct got_object_id **unique_id,
1250 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1252 const struct got_error *err = NULL;
1253 DIR *packdir = NULL;
1254 struct dirent *dent;
1255 char *path_packidx;
1256 struct got_object_id_queue matched_ids;
1257 int packdir_fd;
1259 STAILQ_INIT(&matched_ids);
1261 packdir_fd = openat(got_repo_get_fd(repo),
1262 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1263 if (packdir_fd == -1) {
1264 if (errno != ENOENT)
1265 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1266 goto done;
1269 packdir = fdopendir(packdir_fd);
1270 if (packdir == NULL) {
1271 err = got_error_from_errno("fdopendir");
1272 goto done;
1275 while ((dent = readdir(packdir)) != NULL) {
1276 struct got_packidx *packidx;
1277 struct got_object_qid *qid;
1279 if (!got_repo_is_packidx_filename(dent->d_name,
1280 strlen(dent->d_name)))
1281 continue;
1283 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1284 dent->d_name) == -1) {
1285 err = got_error_from_errno("strdup");
1286 break;
1289 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1290 path_packidx, 0);
1291 free(path_packidx);
1292 if (err)
1293 break;
1295 err = got_packidx_match_id_str_prefix(&matched_ids,
1296 packidx, id_str_prefix);
1297 if (err) {
1298 got_packidx_close(packidx);
1299 break;
1301 err = got_packidx_close(packidx);
1302 if (err)
1303 break;
1305 STAILQ_FOREACH(qid, &matched_ids, entry) {
1306 if (obj_type != GOT_OBJ_TYPE_ANY) {
1307 int matched_type;
1308 err = got_object_get_type(&matched_type, repo,
1309 qid->id);
1310 if (err)
1311 goto done;
1312 if (matched_type != obj_type)
1313 continue;
1315 if (*unique_id == NULL) {
1316 *unique_id = got_object_id_dup(qid->id);
1317 if (*unique_id == NULL) {
1318 err = got_error_from_errno("malloc");
1319 goto done;
1321 } else {
1322 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1323 continue; /* packed multiple times */
1324 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1325 goto done;
1329 done:
1330 got_object_id_queue_free(&matched_ids);
1331 if (packdir && closedir(packdir) != 0 && err == NULL)
1332 err = got_error_from_errno("closedir");
1333 if (err) {
1334 free(*unique_id);
1335 *unique_id = NULL;
1337 return err;
1340 static const struct got_error *
1341 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1342 const char *object_dir, const char *id_str_prefix, int obj_type,
1343 struct got_repository *repo)
1345 const struct got_error *err = NULL;
1346 char *path;
1347 DIR *dir = NULL;
1348 struct dirent *dent;
1349 struct got_object_id id;
1351 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1352 err = got_error_from_errno("asprintf");
1353 goto done;
1356 dir = opendir(path);
1357 if (dir == NULL) {
1358 if (errno == ENOENT) {
1359 err = NULL;
1360 goto done;
1362 err = got_error_from_errno2("opendir", path);
1363 goto done;
1365 while ((dent = readdir(dir)) != NULL) {
1366 char *id_str;
1367 int cmp;
1369 if (strcmp(dent->d_name, ".") == 0 ||
1370 strcmp(dent->d_name, "..") == 0)
1371 continue;
1373 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1374 err = got_error_from_errno("asprintf");
1375 goto done;
1378 if (!got_parse_sha1_digest(id.sha1, id_str))
1379 continue;
1382 * Directory entries do not necessarily appear in
1383 * sorted order, so we must iterate over all of them.
1385 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1386 if (cmp != 0) {
1387 free(id_str);
1388 continue;
1391 if (*unique_id == NULL) {
1392 if (obj_type != GOT_OBJ_TYPE_ANY) {
1393 int matched_type;
1394 err = got_object_get_type(&matched_type, repo,
1395 &id);
1396 if (err)
1397 goto done;
1398 if (matched_type != obj_type)
1399 continue;
1401 *unique_id = got_object_id_dup(&id);
1402 if (*unique_id == NULL) {
1403 err = got_error_from_errno("got_object_id_dup");
1404 free(id_str);
1405 goto done;
1407 } else {
1408 if (got_object_id_cmp(*unique_id, &id) == 0)
1409 continue; /* both packed and loose */
1410 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1411 free(id_str);
1412 goto done;
1415 done:
1416 if (dir && closedir(dir) != 0 && err == NULL)
1417 err = got_error_from_errno("closedir");
1418 if (err) {
1419 free(*unique_id);
1420 *unique_id = NULL;
1422 free(path);
1423 return err;
1426 const struct got_error *
1427 got_repo_match_object_id_prefix(struct got_object_id **id,
1428 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1430 const struct got_error *err = NULL;
1431 char *path_objects = got_repo_get_path_objects(repo);
1432 char *object_dir = NULL;
1433 size_t len;
1434 int i;
1436 *id = NULL;
1438 for (i = 0; i < strlen(id_str_prefix); i++) {
1439 if (isxdigit((unsigned char)id_str_prefix[i]))
1440 continue;
1441 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1444 len = strlen(id_str_prefix);
1445 if (len >= 2) {
1446 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1447 if (err)
1448 goto done;
1449 object_dir = strndup(id_str_prefix, 2);
1450 if (object_dir == NULL) {
1451 err = got_error_from_errno("strdup");
1452 goto done;
1454 err = match_loose_object(id, path_objects, object_dir,
1455 id_str_prefix, obj_type, repo);
1456 } else if (len == 1) {
1457 int i;
1458 for (i = 0; i < 0xf; i++) {
1459 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1460 == -1) {
1461 err = got_error_from_errno("asprintf");
1462 goto done;
1464 err = match_packed_object(id, repo, object_dir,
1465 obj_type);
1466 if (err)
1467 goto done;
1468 err = match_loose_object(id, path_objects, object_dir,
1469 id_str_prefix, obj_type, repo);
1470 if (err)
1471 goto done;
1473 } else {
1474 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1475 goto done;
1477 done:
1478 free(object_dir);
1479 if (err) {
1480 free(*id);
1481 *id = NULL;
1482 } else if (*id == NULL) {
1483 switch (obj_type) {
1484 case GOT_OBJ_TYPE_BLOB:
1485 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1486 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1487 break;
1488 case GOT_OBJ_TYPE_TREE:
1489 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1490 GOT_OBJ_LABEL_TREE, id_str_prefix);
1491 break;
1492 case GOT_OBJ_TYPE_COMMIT:
1493 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1494 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1495 break;
1496 case GOT_OBJ_TYPE_TAG:
1497 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1498 GOT_OBJ_LABEL_TAG, id_str_prefix);
1499 break;
1500 default:
1501 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1502 break;
1506 return err;
1509 const struct got_error *
1510 got_repo_match_object_id(struct got_object_id **id, char **label,
1511 const char *id_str, int obj_type, struct got_reflist_head *refs,
1512 struct got_repository *repo)
1514 const struct got_error *err;
1515 struct got_tag_object *tag;
1516 struct got_reference *ref = NULL;
1518 *id = NULL;
1519 if (label)
1520 *label = NULL;
1522 if (refs) {
1523 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1524 refs, repo);
1525 if (err == NULL) {
1526 *id = got_object_id_dup(
1527 got_object_tag_get_object_id(tag));
1528 if (*id == NULL)
1529 err = got_error_from_errno("got_object_id_dup");
1530 else if (label && asprintf(label, "refs/tags/%s",
1531 got_object_tag_get_name(tag)) == -1) {
1532 err = got_error_from_errno("asprintf");
1533 free(*id);
1534 *id = NULL;
1536 got_object_tag_close(tag);
1537 return err;
1538 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1539 err->code != GOT_ERR_NO_OBJ)
1540 return err;
1543 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1544 if (err) {
1545 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1546 return err;
1547 err = got_ref_open(&ref, repo, id_str, 0);
1548 if (err != NULL)
1549 goto done;
1550 if (label) {
1551 *label = strdup(got_ref_get_name(ref));
1552 if (*label == NULL) {
1553 err = got_error_from_errno("strdup");
1554 goto done;
1557 err = got_ref_resolve(id, repo, ref);
1558 } else if (label) {
1559 err = got_object_id_str(label, *id);
1560 if (*label == NULL) {
1561 err = got_error_from_errno("strdup");
1562 goto done;
1565 done:
1566 if (ref)
1567 got_ref_close(ref);
1568 return err;
1571 const struct got_error *
1572 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1573 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1575 const struct got_error *err = NULL;
1576 struct got_reflist_entry *re;
1577 struct got_object_id *tag_id;
1578 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1580 *tag = NULL;
1582 TAILQ_FOREACH(re, refs, entry) {
1583 const char *refname;
1584 refname = got_ref_get_name(re->ref);
1585 if (got_ref_is_symbolic(re->ref))
1586 continue;
1587 if (strncmp(refname, "refs/tags/", 10) != 0)
1588 continue;
1589 if (!name_is_absolute)
1590 refname += strlen("refs/tags/");
1591 if (strcmp(refname, name) != 0)
1592 continue;
1593 err = got_ref_resolve(&tag_id, repo, re->ref);
1594 if (err)
1595 break;
1596 err = got_object_open_as_tag(tag, repo, tag_id);
1597 free(tag_id);
1598 if (err)
1599 break;
1600 if (obj_type == GOT_OBJ_TYPE_ANY ||
1601 got_object_tag_get_object_type(*tag) == obj_type)
1602 break;
1603 got_object_tag_close(*tag);
1604 *tag = NULL;
1607 if (err == NULL && *tag == NULL)
1608 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1609 GOT_OBJ_LABEL_TAG, name);
1610 return err;
1613 static const struct got_error *
1614 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1615 const char *name, mode_t mode, struct got_object_id *blob_id)
1617 const struct got_error *err = NULL;
1619 *new_te = NULL;
1621 *new_te = calloc(1, sizeof(**new_te));
1622 if (*new_te == NULL)
1623 return got_error_from_errno("calloc");
1625 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1626 sizeof((*new_te)->name)) {
1627 err = got_error(GOT_ERR_NO_SPACE);
1628 goto done;
1631 if (S_ISLNK(mode)) {
1632 (*new_te)->mode = S_IFLNK;
1633 } else {
1634 (*new_te)->mode = S_IFREG;
1635 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1637 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1638 done:
1639 if (err && *new_te) {
1640 free(*new_te);
1641 *new_te = NULL;
1643 return err;
1646 static const struct got_error *
1647 import_file(struct got_tree_entry **new_te, struct dirent *de,
1648 const char *path, struct got_repository *repo)
1650 const struct got_error *err;
1651 struct got_object_id *blob_id = NULL;
1652 char *filepath;
1653 struct stat sb;
1655 if (asprintf(&filepath, "%s%s%s", path,
1656 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1657 return got_error_from_errno("asprintf");
1659 if (lstat(filepath, &sb) != 0) {
1660 err = got_error_from_errno2("lstat", path);
1661 goto done;
1664 err = got_object_blob_create(&blob_id, filepath, repo);
1665 if (err)
1666 goto done;
1668 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1669 blob_id);
1670 done:
1671 free(filepath);
1672 if (err)
1673 free(blob_id);
1674 return err;
1677 static const struct got_error *
1678 insert_tree_entry(struct got_tree_entry *new_te,
1679 struct got_pathlist_head *paths)
1681 const struct got_error *err = NULL;
1682 struct got_pathlist_entry *new_pe;
1684 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1685 if (err)
1686 return err;
1687 if (new_pe == NULL)
1688 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1689 return NULL;
1692 static const struct got_error *write_tree(struct got_object_id **,
1693 const char *, struct got_pathlist_head *, struct got_repository *,
1694 got_repo_import_cb progress_cb, void *progress_arg);
1696 static const struct got_error *
1697 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1698 const char *path, struct got_pathlist_head *ignores,
1699 struct got_repository *repo,
1700 got_repo_import_cb progress_cb, void *progress_arg)
1702 const struct got_error *err;
1703 struct got_object_id *id = NULL;
1704 char *subdirpath;
1706 if (asprintf(&subdirpath, "%s%s%s", path,
1707 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1708 return got_error_from_errno("asprintf");
1710 (*new_te) = calloc(1, sizeof(**new_te));
1711 if (*new_te == NULL)
1712 return got_error_from_errno("calloc");
1713 (*new_te)->mode = S_IFDIR;
1714 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1715 sizeof((*new_te)->name)) {
1716 err = got_error(GOT_ERR_NO_SPACE);
1717 goto done;
1719 err = write_tree(&id, subdirpath, ignores, repo,
1720 progress_cb, progress_arg);
1721 if (err)
1722 goto done;
1723 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1725 done:
1726 free(id);
1727 free(subdirpath);
1728 if (err) {
1729 free(*new_te);
1730 *new_te = NULL;
1732 return err;
1735 static const struct got_error *
1736 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1737 struct got_pathlist_head *ignores, struct got_repository *repo,
1738 got_repo_import_cb progress_cb, void *progress_arg)
1740 const struct got_error *err = NULL;
1741 DIR *dir;
1742 struct dirent *de;
1743 int nentries;
1744 struct got_tree_entry *new_te = NULL;
1745 struct got_pathlist_head paths;
1746 struct got_pathlist_entry *pe;
1748 *new_tree_id = NULL;
1750 TAILQ_INIT(&paths);
1752 dir = opendir(path_dir);
1753 if (dir == NULL) {
1754 err = got_error_from_errno2("opendir", path_dir);
1755 goto done;
1758 nentries = 0;
1759 while ((de = readdir(dir)) != NULL) {
1760 int ignore = 0;
1761 int type;
1763 if (strcmp(de->d_name, ".") == 0 ||
1764 strcmp(de->d_name, "..") == 0)
1765 continue;
1767 TAILQ_FOREACH(pe, ignores, entry) {
1768 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1769 ignore = 1;
1770 break;
1773 if (ignore)
1774 continue;
1776 err = got_path_dirent_type(&type, path_dir, de);
1777 if (err)
1778 goto done;
1780 if (type == DT_DIR) {
1781 err = import_subdir(&new_te, de, path_dir,
1782 ignores, repo, progress_cb, progress_arg);
1783 if (err) {
1784 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1785 goto done;
1786 err = NULL;
1787 continue;
1789 } else if (type == DT_REG || type == DT_LNK) {
1790 err = import_file(&new_te, de, path_dir, repo);
1791 if (err)
1792 goto done;
1793 } else
1794 continue;
1796 err = insert_tree_entry(new_te, &paths);
1797 if (err)
1798 goto done;
1799 nentries++;
1802 if (TAILQ_EMPTY(&paths)) {
1803 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1804 "cannot create tree without any entries");
1805 goto done;
1808 TAILQ_FOREACH(pe, &paths, entry) {
1809 struct got_tree_entry *te = pe->data;
1810 char *path;
1811 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1812 continue;
1813 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1814 err = got_error_from_errno("asprintf");
1815 goto done;
1817 err = (*progress_cb)(progress_arg, path);
1818 free(path);
1819 if (err)
1820 goto done;
1823 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1824 done:
1825 if (dir)
1826 closedir(dir);
1827 got_pathlist_free(&paths);
1828 return err;
1831 const struct got_error *
1832 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1833 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1834 struct got_repository *repo, got_repo_import_cb progress_cb,
1835 void *progress_arg)
1837 const struct got_error *err;
1838 struct got_object_id *new_tree_id;
1840 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1841 progress_cb, progress_arg);
1842 if (err)
1843 return err;
1845 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1846 author, time(NULL), author, time(NULL), logmsg, repo);
1847 free(new_tree_id);
1848 return err;
1851 const struct got_error *
1852 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1853 struct got_repository *repo)
1855 const struct got_error *err = NULL;
1856 char *path_objects = NULL, *path = NULL;
1857 DIR *dir = NULL;
1858 struct got_object_id id;
1859 int i;
1861 *nobjects = 0;
1862 *ondisk_size = 0;
1864 path_objects = got_repo_get_path_objects(repo);
1865 if (path_objects == NULL)
1866 return got_error_from_errno("got_repo_get_path_objects");
1868 for (i = 0; i <= 0xff; i++) {
1869 struct dirent *dent;
1871 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1872 err = got_error_from_errno("asprintf");
1873 break;
1876 dir = opendir(path);
1877 if (dir == NULL) {
1878 if (errno == ENOENT) {
1879 err = NULL;
1880 continue;
1882 err = got_error_from_errno2("opendir", path);
1883 break;
1886 while ((dent = readdir(dir)) != NULL) {
1887 char *id_str;
1888 int fd;
1889 struct stat sb;
1891 if (strcmp(dent->d_name, ".") == 0 ||
1892 strcmp(dent->d_name, "..") == 0)
1893 continue;
1895 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1896 err = got_error_from_errno("asprintf");
1897 goto done;
1900 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1901 free(id_str);
1902 continue;
1904 free(id_str);
1906 err = got_object_open_loose_fd(&fd, &id, repo);
1907 if (err)
1908 goto done;
1910 if (fstat(fd, &sb) == -1) {
1911 err = got_error_from_errno("fstat");
1912 close(fd);
1913 goto done;
1915 (*nobjects)++;
1916 (*ondisk_size) += sb.st_size;
1918 if (close(fd) == -1) {
1919 err = got_error_from_errno("close");
1920 goto done;
1924 if (closedir(dir) != 0) {
1925 err = got_error_from_errno("closedir");
1926 goto done;
1928 dir = NULL;
1930 free(path);
1931 path = NULL;
1933 done:
1934 if (dir && closedir(dir) != 0 && err == NULL)
1935 err = got_error_from_errno("closedir");
1937 if (err) {
1938 *nobjects = 0;
1939 *ondisk_size = 0;
1941 free(path_objects);
1942 free(path);
1943 return err;
1946 const struct got_error *
1947 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1948 off_t *total_packsize, struct got_repository *repo)
1950 const struct got_error *err = NULL;
1951 DIR *packdir = NULL;
1952 struct dirent *dent;
1953 struct got_packidx *packidx = NULL;
1954 char *path_packidx;
1955 char *path_packfile;
1956 int packdir_fd;
1957 struct stat sb;
1959 *npackfiles = 0;
1960 *nobjects = 0;
1961 *total_packsize = 0;
1963 packdir_fd = openat(got_repo_get_fd(repo),
1964 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1965 if (packdir_fd == -1) {
1966 return got_error_from_errno_fmt("openat: %s/%s",
1967 got_repo_get_path_git_dir(repo),
1968 GOT_OBJECTS_PACK_DIR);
1971 packdir = fdopendir(packdir_fd);
1972 if (packdir == NULL) {
1973 err = got_error_from_errno("fdopendir");
1974 goto done;
1977 while ((dent = readdir(packdir)) != NULL) {
1978 if (!got_repo_is_packidx_filename(dent->d_name,
1979 strlen(dent->d_name)))
1980 continue;
1982 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1983 dent->d_name) == -1) {
1984 err = got_error_from_errno("asprintf");
1985 goto done;
1988 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1989 path_packidx, 0);
1990 free(path_packidx);
1991 if (err)
1992 goto done;
1994 if (fstat(packidx->fd, &sb) == -1)
1995 goto done;
1996 *total_packsize += sb.st_size;
1998 err = got_packidx_get_packfile_path(&path_packfile,
1999 packidx->path_packidx);
2000 if (err)
2001 goto done;
2003 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2004 0) == -1) {
2005 free(path_packfile);
2006 goto done;
2008 free(path_packfile);
2009 *total_packsize += sb.st_size;
2011 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2013 (*npackfiles)++;
2015 got_packidx_close(packidx);
2016 packidx = NULL;
2018 done:
2019 if (packidx)
2020 got_packidx_close(packidx);
2021 if (packdir && closedir(packdir) != 0 && err == NULL)
2022 err = got_error_from_errno("closedir");
2023 if (err) {
2024 *npackfiles = 0;
2025 *nobjects = 0;
2026 *total_packsize = 0;
2028 return err;