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 "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_worktree.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 int
118 got_repo_is_bare(struct got_repository *repo)
120 return (strcmp(repo->path, repo->path_git_dir) == 0);
123 static char *
124 get_path_git_child(struct got_repository *repo, const char *basename)
126 char *path_child;
128 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
129 basename) == -1)
130 return NULL;
132 return path_child;
135 char *
136 got_repo_get_path_objects(struct got_repository *repo)
138 return get_path_git_child(repo, GOT_OBJECTS_DIR);
141 char *
142 got_repo_get_path_objects_pack(struct got_repository *repo)
144 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
147 char *
148 got_repo_get_path_refs(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_REFS_DIR);
153 char *
154 got_repo_get_path_packed_refs(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
159 static char *
160 get_path_head(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_HEAD_FILE);
165 char *
166 got_repo_get_path_gitconfig(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_GITCONFIG);
171 char *
172 got_repo_get_path_gotconfig(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
177 const struct got_gotconfig *
178 got_repo_get_gotconfig(struct got_repository *repo)
180 return repo->gotconfig;
183 void
184 got_repo_get_gitconfig_remotes(int *nremotes,
185 const struct got_remote_repo **remotes, struct got_repository *repo)
187 *nremotes = repo->ngitconfig_remotes;
188 *remotes = repo->gitconfig_remotes;
191 static int
192 is_git_repo(struct got_repository *repo)
194 const char *path_git = got_repo_get_path_git_dir(repo);
195 char *path_objects = got_repo_get_path_objects(repo);
196 char *path_refs = got_repo_get_path_refs(repo);
197 char *path_head = get_path_head(repo);
198 int ret = 0;
199 struct stat sb;
200 struct got_reference *head_ref;
202 if (lstat(path_git, &sb) == -1)
203 goto done;
204 if (!S_ISDIR(sb.st_mode))
205 goto done;
207 if (lstat(path_objects, &sb) == -1)
208 goto done;
209 if (!S_ISDIR(sb.st_mode))
210 goto done;
212 if (lstat(path_refs, &sb) == -1)
213 goto done;
214 if (!S_ISDIR(sb.st_mode))
215 goto done;
217 if (lstat(path_head, &sb) == -1)
218 goto done;
219 if (!S_ISREG(sb.st_mode))
220 goto done;
222 /* Check if the HEAD reference can be opened. */
223 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
224 goto done;
225 got_ref_close(head_ref);
227 ret = 1;
228 done:
229 free(path_objects);
230 free(path_refs);
231 free(path_head);
232 return ret;
236 const struct got_error *
237 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
238 struct got_object *obj)
240 #ifndef GOT_NO_OBJ_CACHE
241 const struct got_error *err = NULL;
242 err = got_object_cache_add(&repo->objcache, id, obj);
243 if (err) {
244 if (err->code == GOT_ERR_OBJ_EXISTS ||
245 err->code == GOT_ERR_OBJ_TOO_LARGE)
246 err = NULL;
247 return err;
249 obj->refcnt++;
250 #endif
251 return NULL;
254 struct got_object *
255 got_repo_get_cached_object(struct got_repository *repo,
256 struct got_object_id *id)
258 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
261 const struct got_error *
262 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
263 struct got_tree_object *tree)
265 #ifndef GOT_NO_OBJ_CACHE
266 const struct got_error *err = NULL;
267 err = got_object_cache_add(&repo->treecache, id, tree);
268 if (err) {
269 if (err->code == GOT_ERR_OBJ_EXISTS ||
270 err->code == GOT_ERR_OBJ_TOO_LARGE)
271 err = NULL;
272 return err;
274 tree->refcnt++;
275 #endif
276 return NULL;
279 struct got_tree_object *
280 got_repo_get_cached_tree(struct got_repository *repo,
281 struct got_object_id *id)
283 return (struct got_tree_object *)got_object_cache_get(
284 &repo->treecache, id);
287 const struct got_error *
288 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
289 struct got_commit_object *commit)
291 #ifndef GOT_NO_OBJ_CACHE
292 const struct got_error *err = NULL;
293 err = got_object_cache_add(&repo->commitcache, id, commit);
294 if (err) {
295 if (err->code == GOT_ERR_OBJ_EXISTS ||
296 err->code == GOT_ERR_OBJ_TOO_LARGE)
297 err = NULL;
298 return err;
300 commit->refcnt++;
301 #endif
302 return NULL;
305 struct got_commit_object *
306 got_repo_get_cached_commit(struct got_repository *repo,
307 struct got_object_id *id)
309 return (struct got_commit_object *)got_object_cache_get(
310 &repo->commitcache, id);
313 const struct got_error *
314 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
315 struct got_tag_object *tag)
317 #ifndef GOT_NO_OBJ_CACHE
318 const struct got_error *err = NULL;
319 err = got_object_cache_add(&repo->tagcache, id, tag);
320 if (err) {
321 if (err->code == GOT_ERR_OBJ_EXISTS ||
322 err->code == GOT_ERR_OBJ_TOO_LARGE)
323 err = NULL;
324 return err;
326 tag->refcnt++;
327 #endif
328 return NULL;
331 struct got_tag_object *
332 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
334 return (struct got_tag_object *)got_object_cache_get(
335 &repo->tagcache, id);
338 const struct got_error *
339 open_repo(struct got_repository *repo, const char *path)
341 const struct got_error *err = NULL;
343 repo->gitdir_fd = -1;
345 /* bare git repository? */
346 repo->path_git_dir = strdup(path);
347 if (repo->path_git_dir == NULL)
348 return got_error_from_errno("strdup");
349 if (is_git_repo(repo)) {
350 repo->path = strdup(repo->path_git_dir);
351 if (repo->path == NULL) {
352 err = got_error_from_errno("strdup");
353 goto done;
355 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
356 if (repo->gitdir_fd == -1) {
357 err = got_error_from_errno2("open",
358 repo->path_git_dir);
359 goto done;
361 return NULL;
364 /* git repository with working tree? */
365 free(repo->path_git_dir);
366 repo->path_git_dir = NULL;
367 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
368 err = got_error_from_errno("asprintf");
369 goto done;
371 if (is_git_repo(repo)) {
372 repo->path = strdup(path);
373 if (repo->path == NULL) {
374 err = got_error_from_errno("strdup");
375 goto done;
377 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
378 if (repo->gitdir_fd == -1) {
379 err = got_error_from_errno2("open",
380 repo->path_git_dir);
381 goto done;
383 return NULL;
386 err = got_error(GOT_ERR_NOT_GIT_REPO);
387 done:
388 if (err) {
389 free(repo->path);
390 repo->path = NULL;
391 free(repo->path_git_dir);
392 repo->path_git_dir = NULL;
393 if (repo->gitdir_fd != -1)
394 close(repo->gitdir_fd);
395 repo->gitdir_fd = -1;
398 return err;
401 static const struct got_error *
402 parse_gitconfig_file(int *gitconfig_repository_format_version,
403 char **gitconfig_author_name, char **gitconfig_author_email,
404 struct got_remote_repo **remotes, int *nremotes,
405 char **gitconfig_owner, char ***extensions, int *nextensions,
406 const char *gitconfig_path)
408 const struct got_error *err = NULL, *child_err = NULL;
409 int fd = -1;
410 int imsg_fds[2] = { -1, -1 };
411 pid_t pid;
412 struct imsgbuf *ibuf;
414 *gitconfig_repository_format_version = 0;
415 if (extensions)
416 *extensions = NULL;
417 if (nextensions)
418 *nextensions = 0;
419 *gitconfig_author_name = NULL;
420 *gitconfig_author_email = NULL;
421 if (remotes)
422 *remotes = NULL;
423 if (nremotes)
424 *nremotes = 0;
425 if (gitconfig_owner)
426 *gitconfig_owner = NULL;
428 fd = open(gitconfig_path, O_RDONLY);
429 if (fd == -1) {
430 if (errno == ENOENT)
431 return NULL;
432 return got_error_from_errno2("open", gitconfig_path);
435 ibuf = calloc(1, sizeof(*ibuf));
436 if (ibuf == NULL) {
437 err = got_error_from_errno("calloc");
438 goto done;
441 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
442 err = got_error_from_errno("socketpair");
443 goto done;
446 pid = fork();
447 if (pid == -1) {
448 err = got_error_from_errno("fork");
449 goto done;
450 } else if (pid == 0) {
451 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
452 gitconfig_path);
453 /* not reached */
456 if (close(imsg_fds[1]) == -1) {
457 err = got_error_from_errno("close");
458 goto done;
460 imsg_fds[1] = -1;
461 imsg_init(ibuf, imsg_fds[0]);
463 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
464 if (err)
465 goto done;
466 fd = -1;
468 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
469 if (err)
470 goto done;
472 err = got_privsep_recv_gitconfig_int(
473 gitconfig_repository_format_version, ibuf);
474 if (err)
475 goto done;
477 if (extensions && nextensions) {
478 err = got_privsep_send_gitconfig_repository_extensions_req(
479 ibuf);
480 if (err)
481 goto done;
482 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
483 if (err)
484 goto done;
485 if (*nextensions > 0) {
486 int i;
487 *extensions = calloc(*nextensions, sizeof(char *));
488 if (*extensions == NULL) {
489 err = got_error_from_errno("calloc");
490 goto done;
492 for (i = 0; i < *nextensions; i++) {
493 char *ext;
494 err = got_privsep_recv_gitconfig_str(&ext,
495 ibuf);
496 if (err)
497 goto done;
498 (*extensions)[i] = ext;
503 err = got_privsep_send_gitconfig_author_name_req(ibuf);
504 if (err)
505 goto done;
507 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
508 if (err)
509 goto done;
511 err = got_privsep_send_gitconfig_author_email_req(ibuf);
512 if (err)
513 goto done;
515 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
516 if (err)
517 goto done;
519 if (remotes && nremotes) {
520 err = got_privsep_send_gitconfig_remotes_req(ibuf);
521 if (err)
522 goto done;
524 err = got_privsep_recv_gitconfig_remotes(remotes,
525 nremotes, ibuf);
526 if (err)
527 goto done;
530 if (gitconfig_owner) {
531 err = got_privsep_send_gitconfig_owner_req(ibuf);
532 if (err)
533 goto done;
534 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
535 if (err)
536 goto done;
539 imsg_clear(ibuf);
540 err = got_privsep_send_stop(imsg_fds[0]);
541 child_err = got_privsep_wait_for_child(pid);
542 if (child_err && err == NULL)
543 err = child_err;
544 done:
545 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
546 err = got_error_from_errno("close");
547 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
548 err = got_error_from_errno("close");
549 if (fd != -1 && close(fd) == -1 && err == NULL)
550 err = got_error_from_errno2("close", gitconfig_path);
551 free(ibuf);
552 return err;
555 static const struct got_error *
556 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
558 const struct got_error *err = NULL;
559 char *repo_gitconfig_path = NULL;
561 if (global_gitconfig_path) {
562 /* Read settings from ~/.gitconfig. */
563 int dummy_repo_version;
564 err = parse_gitconfig_file(&dummy_repo_version,
565 &repo->global_gitconfig_author_name,
566 &repo->global_gitconfig_author_email,
567 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
568 if (err)
569 return err;
572 /* Read repository's .git/config file. */
573 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
574 if (repo_gitconfig_path == NULL)
575 return got_error_from_errno("got_repo_get_path_gitconfig");
577 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
578 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
579 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
580 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
581 repo_gitconfig_path);
582 if (err)
583 goto done;
584 done:
585 free(repo_gitconfig_path);
586 return err;
589 static const struct got_error *
590 read_gotconfig(struct got_repository *repo)
592 const struct got_error *err = NULL;
593 char *gotconfig_path;
595 gotconfig_path = got_repo_get_path_gotconfig(repo);
596 if (gotconfig_path == NULL)
597 return got_error_from_errno("got_repo_get_path_gotconfig");
599 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
600 free(gotconfig_path);
601 return err;
604 /* Supported repository format extensions. */
605 static const char *repo_extensions[] = {
606 "noop", /* Got supports repository format version 1. */
607 "preciousObjects", /* Got has no garbage collection yet. */
608 "worktreeConfig", /* Got does not care about Git work trees. */
609 };
611 const struct got_error *
612 got_repo_open(struct got_repository **repop, const char *path,
613 const char *global_gitconfig_path)
615 struct got_repository *repo = NULL;
616 const struct got_error *err = NULL;
617 char *repo_path = NULL;
618 size_t i;
619 struct rlimit rl;
621 *repop = NULL;
623 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
624 return got_error_from_errno("getrlimit");
626 repo = calloc(1, sizeof(*repo));
627 if (repo == NULL) {
628 err = got_error_from_errno("calloc");
629 goto done;
632 for (i = 0; i < nitems(repo->privsep_children); i++) {
633 memset(&repo->privsep_children[i], 0,
634 sizeof(repo->privsep_children[0]));
635 repo->privsep_children[i].imsg_fd = -1;
638 err = got_object_cache_init(&repo->objcache,
639 GOT_OBJECT_CACHE_TYPE_OBJ);
640 if (err)
641 goto done;
642 err = got_object_cache_init(&repo->treecache,
643 GOT_OBJECT_CACHE_TYPE_TREE);
644 if (err)
645 goto done;
646 err = got_object_cache_init(&repo->commitcache,
647 GOT_OBJECT_CACHE_TYPE_COMMIT);
648 if (err)
649 goto done;
650 err = got_object_cache_init(&repo->tagcache,
651 GOT_OBJECT_CACHE_TYPE_TAG);
652 if (err)
653 goto done;
655 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
656 if (repo->pack_cache_size > rl.rlim_cur / 8)
657 repo->pack_cache_size = rl.rlim_cur / 8;
659 repo_path = realpath(path, NULL);
660 if (repo_path == NULL) {
661 err = got_error_from_errno2("realpath", path);
662 goto done;
665 for (;;) {
666 char *parent_path;
668 err = open_repo(repo, repo_path);
669 if (err == NULL)
670 break;
671 if (err->code != GOT_ERR_NOT_GIT_REPO)
672 goto done;
673 if (repo_path[0] == '/' && repo_path[1] == '\0') {
674 err = got_error(GOT_ERR_NOT_GIT_REPO);
675 goto done;
677 err = got_path_dirname(&parent_path, repo_path);
678 if (err)
679 goto done;
680 free(repo_path);
681 repo_path = parent_path;
684 err = read_gotconfig(repo);
685 if (err)
686 goto done;
688 err = read_gitconfig(repo, global_gitconfig_path);
689 if (err)
690 goto done;
691 if (repo->gitconfig_repository_format_version != 0)
692 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
693 for (i = 0; i < repo->nextensions; i++) {
694 char *ext = repo->extensions[i];
695 int j, supported = 0;
696 for (j = 0; j < nitems(repo_extensions); j++) {
697 if (strcmp(ext, repo_extensions[j]) == 0) {
698 supported = 1;
699 break;
702 if (!supported) {
703 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
704 goto done;
707 done:
708 if (err)
709 got_repo_close(repo);
710 else
711 *repop = repo;
712 free(repo_path);
713 return err;
716 const struct got_error *
717 got_repo_close(struct got_repository *repo)
719 const struct got_error *err = NULL, *child_err;
720 size_t i;
722 for (i = 0; i < repo->pack_cache_size; i++) {
723 if (repo->packidx_cache[i] == NULL)
724 break;
725 got_packidx_close(repo->packidx_cache[i]);
728 for (i = 0; i < repo->pack_cache_size; i++) {
729 if (repo->packs[i].path_packfile == NULL)
730 break;
731 got_pack_close(&repo->packs[i]);
734 free(repo->path);
735 free(repo->path_git_dir);
737 got_object_cache_close(&repo->objcache);
738 got_object_cache_close(&repo->treecache);
739 got_object_cache_close(&repo->commitcache);
740 got_object_cache_close(&repo->tagcache);
742 for (i = 0; i < nitems(repo->privsep_children); i++) {
743 if (repo->privsep_children[i].imsg_fd == -1)
744 continue;
745 imsg_clear(repo->privsep_children[i].ibuf);
746 free(repo->privsep_children[i].ibuf);
747 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
748 child_err = got_privsep_wait_for_child(
749 repo->privsep_children[i].pid);
750 if (child_err && err == NULL)
751 err = child_err;
752 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
753 err == NULL)
754 err = got_error_from_errno("close");
757 if (repo->gotconfig)
758 got_gotconfig_free(repo->gotconfig);
759 free(repo->gitconfig_author_name);
760 free(repo->gitconfig_author_email);
761 for (i = 0; i < repo->ngitconfig_remotes; i++)
762 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
763 free(repo->gitconfig_remotes);
764 for (i = 0; i < repo->nextensions; i++)
765 free(repo->extensions[i]);
766 free(repo->extensions);
767 free(repo);
769 return err;
772 void
773 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
775 int i;
777 free(repo->name);
778 repo->name = NULL;
779 free(repo->url);
780 repo->url = NULL;
781 for (i = 0; i < repo->nbranches; i++)
782 free(repo->branches[i]);
783 free(repo->branches);
784 repo->branches = NULL;
785 repo->nbranches = 0;
788 const struct got_error *
789 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
790 const char *input_path)
792 const struct got_error *err = NULL;
793 const char *repo_abspath = NULL;
794 size_t repolen, len;
795 char *canonpath, *path = NULL;
797 *in_repo_path = NULL;
799 canonpath = strdup(input_path);
800 if (canonpath == NULL) {
801 err = got_error_from_errno("strdup");
802 goto done;
804 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
805 if (err)
806 goto done;
808 repo_abspath = got_repo_get_path(repo);
810 if (canonpath[0] == '\0') {
811 path = strdup(canonpath);
812 if (path == NULL) {
813 err = got_error_from_errno("strdup");
814 goto done;
816 } else {
817 path = realpath(canonpath, NULL);
818 if (path == NULL) {
819 if (errno != ENOENT) {
820 err = got_error_from_errno2("realpath",
821 canonpath);
822 goto done;
824 /*
825 * Path is not on disk.
826 * Assume it is already relative to repository root.
827 */
828 path = strdup(canonpath);
829 if (path == NULL) {
830 err = got_error_from_errno("strdup");
831 goto done;
835 repolen = strlen(repo_abspath);
836 len = strlen(path);
839 if (strcmp(path, repo_abspath) == 0) {
840 free(path);
841 path = strdup("");
842 if (path == NULL) {
843 err = got_error_from_errno("strdup");
844 goto done;
846 } else if (len > repolen &&
847 got_path_is_child(path, repo_abspath, repolen)) {
848 /* Matched an on-disk path inside repository. */
849 if (got_repo_is_bare(repo)) {
850 /*
851 * Matched an on-disk path inside repository
852 * database. Treat input as repository-relative.
853 */
854 free(path);
855 path = canonpath;
856 canonpath = NULL;
857 } else {
858 char *child;
859 /* Strip common prefix with repository path. */
860 err = got_path_skip_common_ancestor(&child,
861 repo_abspath, path);
862 if (err)
863 goto done;
864 free(path);
865 path = child;
867 } else {
868 /*
869 * Matched unrelated on-disk path.
870 * Treat input as repository-relative.
871 */
872 free(path);
873 path = canonpath;
874 canonpath = NULL;
878 /* Make in-repository path absolute */
879 if (path[0] != '/') {
880 char *abspath;
881 if (asprintf(&abspath, "/%s", path) == -1) {
882 err = got_error_from_errno("asprintf");
883 goto done;
885 free(path);
886 path = abspath;
889 done:
890 free(canonpath);
891 if (err)
892 free(path);
893 else
894 *in_repo_path = path;
895 return err;
898 static const struct got_error *
899 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
900 const char *path_packidx)
902 const struct got_error *err = NULL;
903 size_t i;
905 for (i = 0; i < repo->pack_cache_size; i++) {
906 if (repo->packidx_cache[i] == NULL)
907 break;
908 if (strcmp(repo->packidx_cache[i]->path_packidx,
909 path_packidx) == 0) {
910 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
913 if (i == repo->pack_cache_size) {
914 err = got_packidx_close(repo->packidx_cache[i - 1]);
915 if (err)
916 return err;
919 /*
920 * Insert the new pack index at the front so it will
921 * be searched first in the future.
922 */
923 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
924 sizeof(repo->packidx_cache) -
925 sizeof(repo->packidx_cache[0]));
926 repo->packidx_cache[0] = packidx;
928 return NULL;
931 static int
932 is_packidx_filename(const char *name, size_t len)
934 if (len != GOT_PACKIDX_NAMELEN)
935 return 0;
937 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
938 return 0;
940 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
941 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
942 return 0;
944 return 1;
947 const struct got_error *
948 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
949 struct got_repository *repo, struct got_object_id *id)
951 const struct got_error *err;
952 DIR *packdir = NULL;
953 struct dirent *dent;
954 char *path_packidx;
955 size_t i;
956 int packdir_fd;
958 /* Search pack index cache. */
959 for (i = 0; i < repo->pack_cache_size; i++) {
960 if (repo->packidx_cache[i] == NULL)
961 break;
962 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
963 if (*idx != -1) {
964 *packidx = repo->packidx_cache[i];
965 /*
966 * Move this cache entry to the front. Repeatedly
967 * searching a wrong pack index can be expensive.
968 */
969 if (i > 0) {
970 struct got_packidx *p;
971 p = repo->packidx_cache[0];
972 repo->packidx_cache[0] = *packidx;
973 repo->packidx_cache[i] = p;
975 return NULL;
978 /* No luck. Search the filesystem. */
980 packdir_fd = openat(got_repo_get_fd(repo),
981 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
982 if (packdir_fd == -1) {
983 if (errno == ENOENT)
984 err = got_error_no_obj(id);
985 else
986 err = got_error_from_errno_fmt("openat: %s/%s",
987 got_repo_get_path_git_dir(repo),
988 GOT_OBJECTS_PACK_DIR);
989 goto done;
992 packdir = fdopendir(packdir_fd);
993 if (packdir == NULL) {
994 err = got_error_from_errno("fdopendir");
995 goto done;
998 while ((dent = readdir(packdir)) != NULL) {
999 int is_cached = 0;
1001 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1002 continue;
1004 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1005 dent->d_name) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 goto done;
1010 for (i = 0; i < repo->pack_cache_size; i++) {
1011 if (repo->packidx_cache[i] == NULL)
1012 break;
1013 if (strcmp(repo->packidx_cache[i]->path_packidx,
1014 path_packidx) == 0) {
1015 is_cached = 1;
1016 break;
1019 if (is_cached) {
1020 free(path_packidx);
1021 continue; /* already searched */
1024 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1025 path_packidx, 0);
1026 if (err) {
1027 free(path_packidx);
1028 goto done;
1031 err = cache_packidx(repo, *packidx, path_packidx);
1032 free(path_packidx);
1033 if (err)
1034 goto done;
1036 *idx = got_packidx_get_object_idx(*packidx, id);
1037 if (*idx != -1) {
1038 err = NULL; /* found the object */
1039 goto done;
1043 err = got_error_no_obj(id);
1044 done:
1045 if (packdir && closedir(packdir) != 0 && err == NULL)
1046 err = got_error_from_errno("closedir");
1047 return err;
1050 static const struct got_error *
1051 read_packfile_hdr(int fd, struct got_packidx *packidx)
1053 const struct got_error *err = NULL;
1054 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1055 struct got_packfile_hdr hdr;
1056 ssize_t n;
1058 n = read(fd, &hdr, sizeof(hdr));
1059 if (n < 0)
1060 return got_error_from_errno("read");
1061 if (n != sizeof(hdr))
1062 return got_error(GOT_ERR_BAD_PACKFILE);
1064 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1065 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1066 be32toh(hdr.nobjects) != totobj)
1067 err = got_error(GOT_ERR_BAD_PACKFILE);
1069 return err;
1072 static const struct got_error *
1073 open_packfile(int *fd, struct got_repository *repo,
1074 const char *relpath, struct got_packidx *packidx)
1076 const struct got_error *err = NULL;
1078 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1079 if (*fd == -1)
1080 return got_error_from_errno_fmt("openat: %s/%s",
1081 got_repo_get_path_git_dir(repo), relpath);
1083 if (packidx) {
1084 err = read_packfile_hdr(*fd, packidx);
1085 if (err) {
1086 close(*fd);
1087 *fd = -1;
1091 return err;
1094 const struct got_error *
1095 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1096 const char *path_packfile, struct got_packidx *packidx)
1098 const struct got_error *err = NULL;
1099 struct got_pack *pack = NULL;
1100 struct stat sb;
1101 size_t i;
1103 if (packp)
1104 *packp = NULL;
1106 for (i = 0; i < repo->pack_cache_size; i++) {
1107 pack = &repo->packs[i];
1108 if (pack->path_packfile == NULL)
1109 break;
1110 if (strcmp(pack->path_packfile, path_packfile) == 0)
1111 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1114 if (i == repo->pack_cache_size) {
1115 err = got_pack_close(&repo->packs[i - 1]);
1116 if (err)
1117 return err;
1118 memmove(&repo->packs[1], &repo->packs[0],
1119 sizeof(repo->packs) - sizeof(repo->packs[0]));
1120 i = 0;
1123 pack = &repo->packs[i];
1125 pack->path_packfile = strdup(path_packfile);
1126 if (pack->path_packfile == NULL) {
1127 err = got_error_from_errno("strdup");
1128 goto done;
1131 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1132 if (err)
1133 goto done;
1135 if (fstat(pack->fd, &sb) != 0) {
1136 err = got_error_from_errno("fstat");
1137 goto done;
1139 pack->filesize = sb.st_size;
1141 pack->privsep_child = NULL;
1143 #ifndef GOT_PACK_NO_MMAP
1144 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1145 pack->fd, 0);
1146 if (pack->map == MAP_FAILED) {
1147 if (errno != ENOMEM) {
1148 err = got_error_from_errno("mmap");
1149 goto done;
1151 pack->map = NULL; /* fall back to read(2) */
1153 #endif
1154 done:
1155 if (err) {
1156 if (pack) {
1157 free(pack->path_packfile);
1158 memset(pack, 0, sizeof(*pack));
1160 } else if (packp)
1161 *packp = pack;
1162 return err;
1165 struct got_pack *
1166 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1168 struct got_pack *pack = NULL;
1169 size_t i;
1171 for (i = 0; i < repo->pack_cache_size; i++) {
1172 pack = &repo->packs[i];
1173 if (pack->path_packfile == NULL)
1174 break;
1175 if (strcmp(pack->path_packfile, path_packfile) == 0)
1176 return pack;
1179 return NULL;
1182 const struct got_error *
1183 got_repo_init(const char *repo_path)
1185 const struct got_error *err = NULL;
1186 const char *dirnames[] = {
1187 GOT_OBJECTS_DIR,
1188 GOT_OBJECTS_PACK_DIR,
1189 GOT_REFS_DIR,
1191 const char *description_str = "Unnamed repository; "
1192 "edit this file 'description' to name the repository.";
1193 const char *headref_str = "ref: refs/heads/main";
1194 const char *gitconfig_str = "[core]\n"
1195 "\trepositoryformatversion = 0\n"
1196 "\tfilemode = true\n"
1197 "\tbare = true\n";
1198 char *path;
1199 size_t i;
1201 if (!got_path_dir_is_empty(repo_path))
1202 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1204 for (i = 0; i < nitems(dirnames); i++) {
1205 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1206 return got_error_from_errno("asprintf");
1208 err = got_path_mkdir(path);
1209 free(path);
1210 if (err)
1211 return err;
1214 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1215 return got_error_from_errno("asprintf");
1216 err = got_path_create_file(path, description_str);
1217 free(path);
1218 if (err)
1219 return err;
1221 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1222 return got_error_from_errno("asprintf");
1223 err = got_path_create_file(path, headref_str);
1224 free(path);
1225 if (err)
1226 return err;
1228 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1229 return got_error_from_errno("asprintf");
1230 err = got_path_create_file(path, gitconfig_str);
1231 free(path);
1232 if (err)
1233 return err;
1235 return NULL;
1238 static const struct got_error *
1239 match_packed_object(struct got_object_id **unique_id,
1240 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1242 const struct got_error *err = NULL;
1243 DIR *packdir = NULL;
1244 struct dirent *dent;
1245 char *path_packidx;
1246 struct got_object_id_queue matched_ids;
1247 int packdir_fd;
1249 SIMPLEQ_INIT(&matched_ids);
1251 packdir_fd = openat(got_repo_get_fd(repo),
1252 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1253 if (packdir_fd == -1) {
1254 if (errno != ENOENT)
1255 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1256 goto done;
1259 packdir = fdopendir(packdir_fd);
1260 if (packdir == NULL) {
1261 err = got_error_from_errno("fdopendir");
1262 goto done;
1265 while ((dent = readdir(packdir)) != NULL) {
1266 struct got_packidx *packidx;
1267 struct got_object_qid *qid;
1269 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1270 continue;
1272 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1273 dent->d_name) == -1) {
1274 err = got_error_from_errno("strdup");
1275 break;
1278 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1279 path_packidx, 0);
1280 free(path_packidx);
1281 if (err)
1282 break;
1284 err = got_packidx_match_id_str_prefix(&matched_ids,
1285 packidx, id_str_prefix);
1286 if (err) {
1287 got_packidx_close(packidx);
1288 break;
1290 err = got_packidx_close(packidx);
1291 if (err)
1292 break;
1294 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1295 if (obj_type != GOT_OBJ_TYPE_ANY) {
1296 int matched_type;
1297 err = got_object_get_type(&matched_type, repo,
1298 qid->id);
1299 if (err)
1300 goto done;
1301 if (matched_type != obj_type)
1302 continue;
1304 if (*unique_id == NULL) {
1305 *unique_id = got_object_id_dup(qid->id);
1306 if (*unique_id == NULL) {
1307 err = got_error_from_errno("malloc");
1308 goto done;
1310 } else {
1311 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1312 continue; /* packed multiple times */
1313 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1314 goto done;
1318 done:
1319 got_object_id_queue_free(&matched_ids);
1320 if (packdir && closedir(packdir) != 0 && err == NULL)
1321 err = got_error_from_errno("closedir");
1322 if (err) {
1323 free(*unique_id);
1324 *unique_id = NULL;
1326 return err;
1329 static const struct got_error *
1330 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1331 const char *object_dir, const char *id_str_prefix, int obj_type,
1332 struct got_repository *repo)
1334 const struct got_error *err = NULL;
1335 char *path;
1336 DIR *dir = NULL;
1337 struct dirent *dent;
1338 struct got_object_id id;
1340 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1341 err = got_error_from_errno("asprintf");
1342 goto done;
1345 dir = opendir(path);
1346 if (dir == NULL) {
1347 if (errno == ENOENT) {
1348 err = NULL;
1349 goto done;
1351 err = got_error_from_errno2("opendir", path);
1352 goto done;
1354 while ((dent = readdir(dir)) != NULL) {
1355 char *id_str;
1356 int cmp;
1358 if (strcmp(dent->d_name, ".") == 0 ||
1359 strcmp(dent->d_name, "..") == 0)
1360 continue;
1362 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1363 err = got_error_from_errno("asprintf");
1364 goto done;
1367 if (!got_parse_sha1_digest(id.sha1, id_str))
1368 continue;
1371 * Directory entries do not necessarily appear in
1372 * sorted order, so we must iterate over all of them.
1374 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1375 if (cmp != 0) {
1376 free(id_str);
1377 continue;
1380 if (*unique_id == NULL) {
1381 if (obj_type != GOT_OBJ_TYPE_ANY) {
1382 int matched_type;
1383 err = got_object_get_type(&matched_type, repo,
1384 &id);
1385 if (err)
1386 goto done;
1387 if (matched_type != obj_type)
1388 continue;
1390 *unique_id = got_object_id_dup(&id);
1391 if (*unique_id == NULL) {
1392 err = got_error_from_errno("got_object_id_dup");
1393 free(id_str);
1394 goto done;
1396 } else {
1397 if (got_object_id_cmp(*unique_id, &id) == 0)
1398 continue; /* both packed and loose */
1399 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1400 free(id_str);
1401 goto done;
1404 done:
1405 if (dir && closedir(dir) != 0 && err == NULL)
1406 err = got_error_from_errno("closedir");
1407 if (err) {
1408 free(*unique_id);
1409 *unique_id = NULL;
1411 free(path);
1412 return err;
1415 const struct got_error *
1416 got_repo_match_object_id_prefix(struct got_object_id **id,
1417 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1419 const struct got_error *err = NULL;
1420 char *path_objects = got_repo_get_path_objects(repo);
1421 char *object_dir = NULL;
1422 size_t len;
1423 int i;
1425 *id = NULL;
1427 for (i = 0; i < strlen(id_str_prefix); i++) {
1428 if (isxdigit((unsigned char)id_str_prefix[i]))
1429 continue;
1430 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1433 len = strlen(id_str_prefix);
1434 if (len >= 2) {
1435 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1436 if (err)
1437 goto done;
1438 object_dir = strndup(id_str_prefix, 2);
1439 if (object_dir == NULL) {
1440 err = got_error_from_errno("strdup");
1441 goto done;
1443 err = match_loose_object(id, path_objects, object_dir,
1444 id_str_prefix, obj_type, repo);
1445 } else if (len == 1) {
1446 int i;
1447 for (i = 0; i < 0xf; i++) {
1448 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1449 == -1) {
1450 err = got_error_from_errno("asprintf");
1451 goto done;
1453 err = match_packed_object(id, repo, object_dir,
1454 obj_type);
1455 if (err)
1456 goto done;
1457 err = match_loose_object(id, path_objects, object_dir,
1458 id_str_prefix, obj_type, repo);
1459 if (err)
1460 goto done;
1462 } else {
1463 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1464 goto done;
1466 done:
1467 free(object_dir);
1468 if (err) {
1469 free(*id);
1470 *id = NULL;
1471 } else if (*id == NULL)
1472 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1474 return err;
1477 const struct got_error *
1478 got_repo_match_object_id(struct got_object_id **id, char **label,
1479 const char *id_str, int obj_type, struct got_reflist_head *refs,
1480 struct got_repository *repo)
1482 const struct got_error *err;
1483 struct got_tag_object *tag;
1484 struct got_reference *ref = NULL;
1486 *id = NULL;
1487 if (label)
1488 *label = NULL;
1490 if (refs) {
1491 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1492 refs, repo);
1493 if (err == NULL) {
1494 *id = got_object_id_dup(
1495 got_object_tag_get_object_id(tag));
1496 if (*id == NULL)
1497 err = got_error_from_errno("got_object_id_dup");
1498 else if (label && asprintf(label, "refs/tags/%s",
1499 got_object_tag_get_name(tag)) == -1) {
1500 err = got_error_from_errno("asprintf");
1501 free(*id);
1502 *id = NULL;
1504 got_object_tag_close(tag);
1505 return err;
1506 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1507 err->code != GOT_ERR_NO_OBJ)
1508 return err;
1511 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1512 if (err) {
1513 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1514 return err;
1515 err = got_ref_open(&ref, repo, id_str, 0);
1516 if (err != NULL)
1517 goto done;
1518 if (label) {
1519 *label = strdup(got_ref_get_name(ref));
1520 if (*label == NULL) {
1521 err = got_error_from_errno("strdup");
1522 goto done;
1525 err = got_ref_resolve(id, repo, ref);
1526 } else if (label) {
1527 err = got_object_id_str(label, *id);
1528 if (*label == NULL) {
1529 err = got_error_from_errno("strdup");
1530 goto done;
1533 done:
1534 if (ref)
1535 got_ref_close(ref);
1536 return err;
1539 const struct got_error *
1540 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1541 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1543 const struct got_error *err = NULL;
1544 struct got_reflist_entry *re;
1545 struct got_object_id *tag_id;
1546 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1548 *tag = NULL;
1550 TAILQ_FOREACH(re, refs, entry) {
1551 const char *refname;
1552 refname = got_ref_get_name(re->ref);
1553 if (got_ref_is_symbolic(re->ref))
1554 continue;
1555 if (strncmp(refname, "refs/tags/", 10) != 0)
1556 continue;
1557 if (!name_is_absolute)
1558 refname += strlen("refs/tags/");
1559 if (strcmp(refname, name) != 0)
1560 continue;
1561 err = got_ref_resolve(&tag_id, repo, re->ref);
1562 if (err)
1563 break;
1564 err = got_object_open_as_tag(tag, repo, tag_id);
1565 free(tag_id);
1566 if (err)
1567 break;
1568 if (obj_type == GOT_OBJ_TYPE_ANY ||
1569 got_object_tag_get_object_type(*tag) == obj_type)
1570 break;
1571 got_object_tag_close(*tag);
1572 *tag = NULL;
1575 if (err == NULL && *tag == NULL)
1576 err = got_error_path(name, GOT_ERR_NO_OBJ);
1577 return err;
1580 static const struct got_error *
1581 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1582 const char *name, mode_t mode, struct got_object_id *blob_id)
1584 const struct got_error *err = NULL;
1586 *new_te = NULL;
1588 *new_te = calloc(1, sizeof(**new_te));
1589 if (*new_te == NULL)
1590 return got_error_from_errno("calloc");
1592 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1593 sizeof((*new_te)->name)) {
1594 err = got_error(GOT_ERR_NO_SPACE);
1595 goto done;
1598 if (S_ISLNK(mode)) {
1599 (*new_te)->mode = S_IFLNK;
1600 } else {
1601 (*new_te)->mode = S_IFREG;
1602 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1604 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1605 done:
1606 if (err && *new_te) {
1607 free(*new_te);
1608 *new_te = NULL;
1610 return err;
1613 static const struct got_error *
1614 import_file(struct got_tree_entry **new_te, struct dirent *de,
1615 const char *path, struct got_repository *repo)
1617 const struct got_error *err;
1618 struct got_object_id *blob_id = NULL;
1619 char *filepath;
1620 struct stat sb;
1622 if (asprintf(&filepath, "%s%s%s", path,
1623 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1624 return got_error_from_errno("asprintf");
1626 if (lstat(filepath, &sb) != 0) {
1627 err = got_error_from_errno2("lstat", path);
1628 goto done;
1631 err = got_object_blob_create(&blob_id, filepath, repo);
1632 if (err)
1633 goto done;
1635 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1636 blob_id);
1637 done:
1638 free(filepath);
1639 if (err)
1640 free(blob_id);
1641 return err;
1644 static const struct got_error *
1645 insert_tree_entry(struct got_tree_entry *new_te,
1646 struct got_pathlist_head *paths)
1648 const struct got_error *err = NULL;
1649 struct got_pathlist_entry *new_pe;
1651 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1652 if (err)
1653 return err;
1654 if (new_pe == NULL)
1655 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1656 return NULL;
1659 static const struct got_error *write_tree(struct got_object_id **,
1660 const char *, struct got_pathlist_head *, struct got_repository *,
1661 got_repo_import_cb progress_cb, void *progress_arg);
1663 static const struct got_error *
1664 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1665 const char *path, struct got_pathlist_head *ignores,
1666 struct got_repository *repo,
1667 got_repo_import_cb progress_cb, void *progress_arg)
1669 const struct got_error *err;
1670 struct got_object_id *id = NULL;
1671 char *subdirpath;
1673 if (asprintf(&subdirpath, "%s%s%s", path,
1674 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1675 return got_error_from_errno("asprintf");
1677 (*new_te) = calloc(1, sizeof(**new_te));
1678 if (*new_te == NULL)
1679 return got_error_from_errno("calloc");
1680 (*new_te)->mode = S_IFDIR;
1681 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1682 sizeof((*new_te)->name)) {
1683 err = got_error(GOT_ERR_NO_SPACE);
1684 goto done;
1686 err = write_tree(&id, subdirpath, ignores, repo,
1687 progress_cb, progress_arg);
1688 if (err)
1689 goto done;
1690 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1692 done:
1693 free(id);
1694 free(subdirpath);
1695 if (err) {
1696 free(*new_te);
1697 *new_te = NULL;
1699 return err;
1702 static const struct got_error *
1703 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1704 struct got_pathlist_head *ignores, struct got_repository *repo,
1705 got_repo_import_cb progress_cb, void *progress_arg)
1707 const struct got_error *err = NULL;
1708 DIR *dir;
1709 struct dirent *de;
1710 int nentries;
1711 struct got_tree_entry *new_te = NULL;
1712 struct got_pathlist_head paths;
1713 struct got_pathlist_entry *pe;
1715 *new_tree_id = NULL;
1717 TAILQ_INIT(&paths);
1719 dir = opendir(path_dir);
1720 if (dir == NULL) {
1721 err = got_error_from_errno2("opendir", path_dir);
1722 goto done;
1725 nentries = 0;
1726 while ((de = readdir(dir)) != NULL) {
1727 int ignore = 0;
1728 int type;
1730 if (strcmp(de->d_name, ".") == 0 ||
1731 strcmp(de->d_name, "..") == 0)
1732 continue;
1734 TAILQ_FOREACH(pe, ignores, entry) {
1735 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1736 ignore = 1;
1737 break;
1740 if (ignore)
1741 continue;
1743 err = got_path_dirent_type(&type, path_dir, de);
1744 if (err)
1745 goto done;
1747 if (type == DT_DIR) {
1748 err = import_subdir(&new_te, de, path_dir,
1749 ignores, repo, progress_cb, progress_arg);
1750 if (err) {
1751 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1752 goto done;
1753 err = NULL;
1754 continue;
1756 } else if (type == DT_REG || type == DT_LNK) {
1757 err = import_file(&new_te, de, path_dir, repo);
1758 if (err)
1759 goto done;
1760 } else
1761 continue;
1763 err = insert_tree_entry(new_te, &paths);
1764 if (err)
1765 goto done;
1766 nentries++;
1769 if (TAILQ_EMPTY(&paths)) {
1770 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1771 "cannot create tree without any entries");
1772 goto done;
1775 TAILQ_FOREACH(pe, &paths, entry) {
1776 struct got_tree_entry *te = pe->data;
1777 char *path;
1778 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1779 continue;
1780 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1781 err = got_error_from_errno("asprintf");
1782 goto done;
1784 err = (*progress_cb)(progress_arg, path);
1785 free(path);
1786 if (err)
1787 goto done;
1790 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1791 done:
1792 if (dir)
1793 closedir(dir);
1794 got_pathlist_free(&paths);
1795 return err;
1798 const struct got_error *
1799 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1800 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1801 struct got_repository *repo, got_repo_import_cb progress_cb,
1802 void *progress_arg)
1804 const struct got_error *err;
1805 struct got_object_id *new_tree_id;
1807 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1808 progress_cb, progress_arg);
1809 if (err)
1810 return err;
1812 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1813 author, time(NULL), author, time(NULL), logmsg, repo);
1814 free(new_tree_id);
1815 return err;
1818 const struct got_error *
1819 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1820 struct got_repository *repo)
1822 const struct got_error *err = NULL;
1823 char *path_objects = NULL, *path = NULL;
1824 DIR *dir = NULL;
1825 struct got_object_id id;
1826 int i;
1828 *nobjects = 0;
1829 *ondisk_size = 0;
1831 path_objects = got_repo_get_path_objects(repo);
1832 if (path_objects == NULL)
1833 return got_error_from_errno("got_repo_get_path_objects");
1835 for (i = 0; i <= 0xff; i++) {
1836 struct dirent *dent;
1838 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1839 err = got_error_from_errno("asprintf");
1840 break;
1843 dir = opendir(path);
1844 if (dir == NULL) {
1845 if (errno == ENOENT) {
1846 err = NULL;
1847 continue;
1849 err = got_error_from_errno2("opendir", path);
1850 break;
1853 while ((dent = readdir(dir)) != NULL) {
1854 char *id_str;
1855 int fd;
1856 struct stat sb;
1858 if (strcmp(dent->d_name, ".") == 0 ||
1859 strcmp(dent->d_name, "..") == 0)
1860 continue;
1862 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1863 err = got_error_from_errno("asprintf");
1864 goto done;
1867 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1868 free(id_str);
1869 continue;
1871 free(id_str);
1873 err = got_object_open_loose_fd(&fd, &id, repo);
1874 if (err)
1875 goto done;
1877 if (fstat(fd, &sb) == -1) {
1878 err = got_error_from_errno("fstat");
1879 close(fd);
1880 goto done;
1882 (*nobjects)++;
1883 (*ondisk_size) += sb.st_size;
1885 if (close(fd) == -1) {
1886 err = got_error_from_errno("close");
1887 goto done;
1891 if (closedir(dir) != 0) {
1892 err = got_error_from_errno("closedir");
1893 goto done;
1895 dir = NULL;
1897 free(path);
1898 path = NULL;
1900 done:
1901 if (dir && closedir(dir) != 0 && err == NULL)
1902 err = got_error_from_errno("closedir");
1904 if (err) {
1905 *nobjects = 0;
1906 *ondisk_size = 0;
1908 free(path_objects);
1909 free(path);
1910 return err;
1913 const struct got_error *
1914 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1915 off_t *total_packsize, struct got_repository *repo)
1917 const struct got_error *err;
1918 DIR *packdir = NULL;
1919 struct dirent *dent;
1920 struct got_packidx *packidx = NULL;
1921 char *path_packidx;
1922 char *path_packfile;
1923 int packdir_fd;
1924 struct stat sb;
1926 *npackfiles = 0;
1927 *nobjects = 0;
1928 *total_packsize = 0;
1930 packdir_fd = openat(got_repo_get_fd(repo),
1931 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1932 if (packdir_fd == -1) {
1933 return got_error_from_errno_fmt("openat: %s/%s",
1934 got_repo_get_path_git_dir(repo),
1935 GOT_OBJECTS_PACK_DIR);
1938 packdir = fdopendir(packdir_fd);
1939 if (packdir == NULL) {
1940 err = got_error_from_errno("fdopendir");
1941 goto done;
1944 while ((dent = readdir(packdir)) != NULL) {
1945 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1946 continue;
1948 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1949 dent->d_name) == -1) {
1950 err = got_error_from_errno("asprintf");
1951 goto done;
1954 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1955 path_packidx, 0);
1956 free(path_packidx);
1957 if (err)
1958 goto done;
1960 if (fstat(packidx->fd, &sb) == -1)
1961 goto done;
1962 *total_packsize += sb.st_size;
1964 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1965 if (err)
1966 goto done;
1968 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1969 0) == -1) {
1970 free(path_packfile);
1971 goto done;
1973 free(path_packfile);
1974 *total_packsize += sb.st_size;
1976 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
1978 (*npackfiles)++;
1980 got_packidx_close(packidx);
1981 packidx = NULL;
1983 done:
1984 if (packidx)
1985 got_packidx_close(packidx);
1986 if (packdir && closedir(packdir) != 0 && err == NULL)
1987 err = got_error_from_errno("closedir");
1988 if (err) {
1989 *npackfiles = 0;
1990 *nobjects = 0;
1991 *total_packsize = 0;
1993 return err;