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 <endian.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sha1.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <errno.h>
38 #include <libgen.h>
39 #include <stdint.h>
40 #include <uuid.h>
42 #include "got_compat.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_object.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
63 #ifndef nitems
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 #endif
67 const char *
68 got_repo_get_path(struct got_repository *repo)
69 {
70 return repo->path;
71 }
73 const char *
74 got_repo_get_path_git_dir(struct got_repository *repo)
75 {
76 return repo->path_git_dir;
77 }
79 int
80 got_repo_get_fd(struct got_repository *repo)
81 {
82 return repo->gitdir_fd;
83 }
85 const char *
86 got_repo_get_gitconfig_author_name(struct got_repository *repo)
87 {
88 return repo->gitconfig_author_name;
89 }
91 const char *
92 got_repo_get_gitconfig_author_email(struct got_repository *repo)
93 {
94 return repo->gitconfig_author_email;
95 }
97 const char *
98 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
99 {
100 return repo->global_gitconfig_author_name;
103 const char *
104 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
106 return repo->global_gitconfig_author_email;
109 const char *
110 got_repo_get_gitconfig_owner(struct got_repository *repo)
112 return repo->gitconfig_owner;
115 void
116 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
117 struct got_repository *repo)
119 *extensions = repo->extensions;
120 *nextensions = repo->nextensions;
123 int
124 got_repo_is_bare(struct got_repository *repo)
126 return (strcmp(repo->path, repo->path_git_dir) == 0);
129 static char *
130 get_path_git_child(struct got_repository *repo, const char *basename)
132 char *path_child;
134 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
135 basename) == -1)
136 return NULL;
138 return path_child;
141 char *
142 got_repo_get_path_objects(struct got_repository *repo)
144 return get_path_git_child(repo, GOT_OBJECTS_DIR);
147 char *
148 got_repo_get_path_objects_pack(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
153 char *
154 got_repo_get_path_refs(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_REFS_DIR);
159 char *
160 got_repo_get_path_packed_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
165 static char *
166 get_path_head(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_HEAD_FILE);
171 char *
172 got_repo_get_path_gitconfig(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_GITCONFIG);
177 char *
178 got_repo_get_path_gotconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
183 const struct got_gotconfig *
184 got_repo_get_gotconfig(struct got_repository *repo)
186 return repo->gotconfig;
189 void
190 got_repo_get_gitconfig_remotes(int *nremotes,
191 const struct got_remote_repo **remotes, struct got_repository *repo)
193 *nremotes = repo->ngitconfig_remotes;
194 *remotes = repo->gitconfig_remotes;
197 static int
198 is_git_repo(struct got_repository *repo)
200 const char *path_git = got_repo_get_path_git_dir(repo);
201 char *path_objects = got_repo_get_path_objects(repo);
202 char *path_refs = got_repo_get_path_refs(repo);
203 char *path_head = get_path_head(repo);
204 int ret = 0;
205 struct stat sb;
206 struct got_reference *head_ref;
208 if (lstat(path_git, &sb) == -1)
209 goto done;
210 if (!S_ISDIR(sb.st_mode))
211 goto done;
213 if (lstat(path_objects, &sb) == -1)
214 goto done;
215 if (!S_ISDIR(sb.st_mode))
216 goto done;
218 if (lstat(path_refs, &sb) == -1)
219 goto done;
220 if (!S_ISDIR(sb.st_mode))
221 goto done;
223 if (lstat(path_head, &sb) == -1)
224 goto done;
225 if (!S_ISREG(sb.st_mode))
226 goto done;
228 /* Check if the HEAD reference can be opened. */
229 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
230 goto done;
231 got_ref_close(head_ref);
233 ret = 1;
234 done:
235 free(path_objects);
236 free(path_refs);
237 free(path_head);
238 return ret;
242 const struct got_error *
243 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
244 struct got_object *obj)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->objcache, id, obj);
249 if (err) {
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
252 err = NULL;
253 return err;
255 obj->refcnt++;
256 #endif
257 return NULL;
260 struct got_object *
261 got_repo_get_cached_object(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
267 const struct got_error *
268 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
269 struct got_tree_object *tree)
271 #ifndef GOT_NO_OBJ_CACHE
272 const struct got_error *err = NULL;
273 err = got_object_cache_add(&repo->treecache, id, tree);
274 if (err) {
275 if (err->code == GOT_ERR_OBJ_EXISTS ||
276 err->code == GOT_ERR_OBJ_TOO_LARGE)
277 err = NULL;
278 return err;
280 tree->refcnt++;
281 #endif
282 return NULL;
285 struct got_tree_object *
286 got_repo_get_cached_tree(struct got_repository *repo,
287 struct got_object_id *id)
289 return (struct got_tree_object *)got_object_cache_get(
290 &repo->treecache, id);
293 const struct got_error *
294 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
295 struct got_commit_object *commit)
297 #ifndef GOT_NO_OBJ_CACHE
298 const struct got_error *err = NULL;
299 err = got_object_cache_add(&repo->commitcache, id, commit);
300 if (err) {
301 if (err->code == GOT_ERR_OBJ_EXISTS ||
302 err->code == GOT_ERR_OBJ_TOO_LARGE)
303 err = NULL;
304 return err;
306 commit->refcnt++;
307 #endif
308 return NULL;
311 struct got_commit_object *
312 got_repo_get_cached_commit(struct got_repository *repo,
313 struct got_object_id *id)
315 return (struct got_commit_object *)got_object_cache_get(
316 &repo->commitcache, id);
319 const struct got_error *
320 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
321 struct got_tag_object *tag)
323 #ifndef GOT_NO_OBJ_CACHE
324 const struct got_error *err = NULL;
325 err = got_object_cache_add(&repo->tagcache, id, tag);
326 if (err) {
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
329 err = NULL;
330 return err;
332 tag->refcnt++;
333 #endif
334 return NULL;
337 struct got_tag_object *
338 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
340 return (struct got_tag_object *)got_object_cache_get(
341 &repo->tagcache, id);
344 static const struct got_error *
345 open_repo(struct got_repository *repo, const char *path)
347 const struct got_error *err = NULL;
349 repo->gitdir_fd = -1;
351 /* bare git repository? */
352 repo->path_git_dir = strdup(path);
353 if (repo->path_git_dir == NULL)
354 return got_error_from_errno("strdup");
355 if (is_git_repo(repo)) {
356 repo->path = strdup(repo->path_git_dir);
357 if (repo->path == NULL) {
358 err = got_error_from_errno("strdup");
359 goto done;
361 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
362 if (repo->gitdir_fd == -1) {
363 err = got_error_from_errno2("open",
364 repo->path_git_dir);
365 goto done;
367 return NULL;
370 /* git repository with working tree? */
371 free(repo->path_git_dir);
372 repo->path_git_dir = NULL;
373 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
374 err = got_error_from_errno("asprintf");
375 goto done;
377 if (is_git_repo(repo)) {
378 repo->path = strdup(path);
379 if (repo->path == NULL) {
380 err = got_error_from_errno("strdup");
381 goto done;
383 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
384 if (repo->gitdir_fd == -1) {
385 err = got_error_from_errno2("open",
386 repo->path_git_dir);
387 goto done;
389 return NULL;
392 err = got_error(GOT_ERR_NOT_GIT_REPO);
393 done:
394 if (err) {
395 free(repo->path);
396 repo->path = NULL;
397 free(repo->path_git_dir);
398 repo->path_git_dir = NULL;
399 if (repo->gitdir_fd != -1)
400 close(repo->gitdir_fd);
401 repo->gitdir_fd = -1;
404 return err;
407 static const struct got_error *
408 parse_gitconfig_file(int *gitconfig_repository_format_version,
409 char **gitconfig_author_name, char **gitconfig_author_email,
410 struct got_remote_repo **remotes, int *nremotes,
411 char **gitconfig_owner, char ***extensions, int *nextensions,
412 const char *gitconfig_path)
414 const struct got_error *err = NULL, *child_err = NULL;
415 int fd = -1;
416 int imsg_fds[2] = { -1, -1 };
417 pid_t pid;
418 struct imsgbuf *ibuf;
420 *gitconfig_repository_format_version = 0;
421 if (extensions)
422 *extensions = NULL;
423 if (nextensions)
424 *nextensions = 0;
425 *gitconfig_author_name = NULL;
426 *gitconfig_author_email = NULL;
427 if (remotes)
428 *remotes = NULL;
429 if (nremotes)
430 *nremotes = 0;
431 if (gitconfig_owner)
432 *gitconfig_owner = NULL;
434 fd = open(gitconfig_path, O_RDONLY);
435 if (fd == -1) {
436 if (errno == ENOENT)
437 return NULL;
438 return got_error_from_errno2("open", gitconfig_path);
441 ibuf = calloc(1, sizeof(*ibuf));
442 if (ibuf == NULL) {
443 err = got_error_from_errno("calloc");
444 goto done;
447 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
448 err = got_error_from_errno("socketpair");
449 goto done;
452 pid = fork();
453 if (pid == -1) {
454 err = got_error_from_errno("fork");
455 goto done;
456 } else if (pid == 0) {
457 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
458 gitconfig_path);
459 /* not reached */
462 if (close(imsg_fds[1]) == -1) {
463 err = got_error_from_errno("close");
464 goto done;
466 imsg_fds[1] = -1;
467 imsg_init(ibuf, imsg_fds[0]);
469 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
470 if (err)
471 goto done;
472 fd = -1;
474 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
475 if (err)
476 goto done;
478 err = got_privsep_recv_gitconfig_int(
479 gitconfig_repository_format_version, ibuf);
480 if (err)
481 goto done;
483 if (extensions && nextensions) {
484 err = got_privsep_send_gitconfig_repository_extensions_req(
485 ibuf);
486 if (err)
487 goto done;
488 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
489 if (err)
490 goto done;
491 if (*nextensions > 0) {
492 int i;
493 *extensions = calloc(*nextensions, sizeof(char *));
494 if (*extensions == NULL) {
495 err = got_error_from_errno("calloc");
496 goto done;
498 for (i = 0; i < *nextensions; i++) {
499 char *ext;
500 err = got_privsep_recv_gitconfig_str(&ext,
501 ibuf);
502 if (err)
503 goto done;
504 (*extensions)[i] = ext;
509 err = got_privsep_send_gitconfig_author_name_req(ibuf);
510 if (err)
511 goto done;
513 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
514 if (err)
515 goto done;
517 err = got_privsep_send_gitconfig_author_email_req(ibuf);
518 if (err)
519 goto done;
521 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
522 if (err)
523 goto done;
525 if (remotes && nremotes) {
526 err = got_privsep_send_gitconfig_remotes_req(ibuf);
527 if (err)
528 goto done;
530 err = got_privsep_recv_gitconfig_remotes(remotes,
531 nremotes, ibuf);
532 if (err)
533 goto done;
536 if (gitconfig_owner) {
537 err = got_privsep_send_gitconfig_owner_req(ibuf);
538 if (err)
539 goto done;
540 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
541 if (err)
542 goto done;
545 imsg_clear(ibuf);
546 err = got_privsep_send_stop(imsg_fds[0]);
547 child_err = got_privsep_wait_for_child(pid);
548 if (child_err && err == NULL)
549 err = child_err;
550 done:
551 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
552 err = got_error_from_errno("close");
553 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
554 err = got_error_from_errno("close");
555 if (fd != -1 && close(fd) == -1 && err == NULL)
556 err = got_error_from_errno2("close", gitconfig_path);
557 free(ibuf);
558 return err;
561 static const struct got_error *
562 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
564 const struct got_error *err = NULL;
565 char *repo_gitconfig_path = NULL;
567 if (global_gitconfig_path) {
568 /* Read settings from ~/.gitconfig. */
569 int dummy_repo_version;
570 err = parse_gitconfig_file(&dummy_repo_version,
571 &repo->global_gitconfig_author_name,
572 &repo->global_gitconfig_author_email,
573 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
574 if (err)
575 return err;
578 /* Read repository's .git/config file. */
579 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
580 if (repo_gitconfig_path == NULL)
581 return got_error_from_errno("got_repo_get_path_gitconfig");
583 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
584 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
585 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
586 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
587 repo_gitconfig_path);
588 if (err)
589 goto done;
590 done:
591 free(repo_gitconfig_path);
592 return err;
595 static const struct got_error *
596 read_gotconfig(struct got_repository *repo)
598 const struct got_error *err = NULL;
599 char *gotconfig_path;
601 gotconfig_path = got_repo_get_path_gotconfig(repo);
602 if (gotconfig_path == NULL)
603 return got_error_from_errno("got_repo_get_path_gotconfig");
605 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
606 free(gotconfig_path);
607 return err;
610 /* Supported repository format extensions. */
611 static const char *repo_extensions[] = {
612 "noop", /* Got supports repository format version 1. */
613 "preciousObjects", /* Supported by gotadmin cleanup. */
614 "worktreeConfig", /* Got does not care about Git work trees. */
615 };
617 const struct got_error *
618 got_repo_open(struct got_repository **repop, const char *path,
619 const char *global_gitconfig_path)
621 struct got_repository *repo = NULL;
622 const struct got_error *err = NULL;
623 char *repo_path = NULL;
624 size_t i;
625 struct rlimit rl;
627 *repop = NULL;
629 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
630 return got_error_from_errno("getrlimit");
632 repo = calloc(1, sizeof(*repo));
633 if (repo == NULL) {
634 err = got_error_from_errno("calloc");
635 goto done;
638 for (i = 0; i < nitems(repo->privsep_children); i++) {
639 memset(&repo->privsep_children[i], 0,
640 sizeof(repo->privsep_children[0]));
641 repo->privsep_children[i].imsg_fd = -1;
644 err = got_object_cache_init(&repo->objcache,
645 GOT_OBJECT_CACHE_TYPE_OBJ);
646 if (err)
647 goto done;
648 err = got_object_cache_init(&repo->treecache,
649 GOT_OBJECT_CACHE_TYPE_TREE);
650 if (err)
651 goto done;
652 err = got_object_cache_init(&repo->commitcache,
653 GOT_OBJECT_CACHE_TYPE_COMMIT);
654 if (err)
655 goto done;
656 err = got_object_cache_init(&repo->tagcache,
657 GOT_OBJECT_CACHE_TYPE_TAG);
658 if (err)
659 goto done;
661 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
662 if (repo->pack_cache_size > rl.rlim_cur / 8)
663 repo->pack_cache_size = rl.rlim_cur / 8;
665 repo_path = realpath(path, NULL);
666 if (repo_path == NULL) {
667 err = got_error_from_errno2("realpath", path);
668 goto done;
671 for (;;) {
672 char *parent_path;
674 err = open_repo(repo, repo_path);
675 if (err == NULL)
676 break;
677 if (err->code != GOT_ERR_NOT_GIT_REPO)
678 goto done;
679 if (repo_path[0] == '/' && repo_path[1] == '\0') {
680 err = got_error(GOT_ERR_NOT_GIT_REPO);
681 goto done;
683 err = got_path_dirname(&parent_path, repo_path);
684 if (err)
685 goto done;
686 free(repo_path);
687 repo_path = parent_path;
690 err = read_gotconfig(repo);
691 if (err)
692 goto done;
694 err = read_gitconfig(repo, global_gitconfig_path);
695 if (err)
696 goto done;
697 if (repo->gitconfig_repository_format_version != 0)
698 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
699 for (i = 0; i < repo->nextensions; i++) {
700 char *ext = repo->extensions[i];
701 int j, supported = 0;
702 for (j = 0; j < nitems(repo_extensions); j++) {
703 if (strcmp(ext, repo_extensions[j]) == 0) {
704 supported = 1;
705 break;
708 if (!supported) {
709 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
710 goto done;
713 done:
714 if (err)
715 got_repo_close(repo);
716 else
717 *repop = repo;
718 free(repo_path);
719 return err;
722 const struct got_error *
723 got_repo_close(struct got_repository *repo)
725 const struct got_error *err = NULL, *child_err;
726 size_t i;
728 for (i = 0; i < repo->pack_cache_size; i++) {
729 if (repo->packidx_cache[i] == NULL)
730 break;
731 got_packidx_close(repo->packidx_cache[i]);
734 for (i = 0; i < repo->pack_cache_size; i++) {
735 if (repo->packs[i].path_packfile == NULL)
736 break;
737 got_pack_close(&repo->packs[i]);
740 free(repo->path);
741 free(repo->path_git_dir);
743 got_object_cache_close(&repo->objcache);
744 got_object_cache_close(&repo->treecache);
745 got_object_cache_close(&repo->commitcache);
746 got_object_cache_close(&repo->tagcache);
748 for (i = 0; i < nitems(repo->privsep_children); i++) {
749 if (repo->privsep_children[i].imsg_fd == -1)
750 continue;
751 imsg_clear(repo->privsep_children[i].ibuf);
752 free(repo->privsep_children[i].ibuf);
753 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
754 child_err = got_privsep_wait_for_child(
755 repo->privsep_children[i].pid);
756 if (child_err && err == NULL)
757 err = child_err;
758 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
759 err == NULL)
760 err = got_error_from_errno("close");
763 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
764 err == NULL)
765 err = got_error_from_errno("close");
767 if (repo->gotconfig)
768 got_gotconfig_free(repo->gotconfig);
769 free(repo->gitconfig_author_name);
770 free(repo->gitconfig_author_email);
771 for (i = 0; i < repo->ngitconfig_remotes; i++)
772 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
773 free(repo->gitconfig_remotes);
774 for (i = 0; i < repo->nextensions; i++)
775 free(repo->extensions[i]);
776 free(repo->extensions);
777 free(repo);
779 return err;
782 void
783 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
785 int i;
787 free(repo->name);
788 repo->name = NULL;
789 free(repo->fetch_url);
790 repo->fetch_url = NULL;
791 free(repo->send_url);
792 repo->send_url = NULL;
793 for (i = 0; i < repo->nfetch_branches; i++)
794 free(repo->fetch_branches[i]);
795 free(repo->fetch_branches);
796 repo->fetch_branches = NULL;
797 repo->nfetch_branches = 0;
798 for (i = 0; i < repo->nsend_branches; i++)
799 free(repo->send_branches[i]);
800 free(repo->send_branches);
801 repo->send_branches = NULL;
802 repo->nsend_branches = 0;
805 const struct got_error *
806 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
807 const char *input_path)
809 const struct got_error *err = NULL;
810 const char *repo_abspath = NULL;
811 size_t repolen, len;
812 char *canonpath, *path = NULL;
814 *in_repo_path = NULL;
816 canonpath = strdup(input_path);
817 if (canonpath == NULL) {
818 err = got_error_from_errno("strdup");
819 goto done;
821 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
822 if (err)
823 goto done;
825 repo_abspath = got_repo_get_path(repo);
827 if (canonpath[0] == '\0') {
828 path = strdup(canonpath);
829 if (path == NULL) {
830 err = got_error_from_errno("strdup");
831 goto done;
833 } else {
834 path = realpath(canonpath, NULL);
835 if (path == NULL) {
836 if (errno != ENOENT) {
837 err = got_error_from_errno2("realpath",
838 canonpath);
839 goto done;
841 /*
842 * Path is not on disk.
843 * Assume it is already relative to repository root.
844 */
845 path = strdup(canonpath);
846 if (path == NULL) {
847 err = got_error_from_errno("strdup");
848 goto done;
852 repolen = strlen(repo_abspath);
853 len = strlen(path);
856 if (strcmp(path, repo_abspath) == 0) {
857 free(path);
858 path = strdup("");
859 if (path == NULL) {
860 err = got_error_from_errno("strdup");
861 goto done;
863 } else if (len > repolen &&
864 got_path_is_child(path, repo_abspath, repolen)) {
865 /* Matched an on-disk path inside repository. */
866 if (got_repo_is_bare(repo)) {
867 /*
868 * Matched an on-disk path inside repository
869 * database. Treat input as repository-relative.
870 */
871 free(path);
872 path = canonpath;
873 canonpath = NULL;
874 } else {
875 char *child;
876 /* Strip common prefix with repository path. */
877 err = got_path_skip_common_ancestor(&child,
878 repo_abspath, path);
879 if (err)
880 goto done;
881 free(path);
882 path = child;
884 } else {
885 /*
886 * Matched unrelated on-disk path.
887 * Treat input as repository-relative.
888 */
889 free(path);
890 path = canonpath;
891 canonpath = NULL;
895 /* Make in-repository path absolute */
896 if (path[0] != '/') {
897 char *abspath;
898 if (asprintf(&abspath, "/%s", path) == -1) {
899 err = got_error_from_errno("asprintf");
900 goto done;
902 free(path);
903 path = abspath;
906 done:
907 free(canonpath);
908 if (err)
909 free(path);
910 else
911 *in_repo_path = path;
912 return err;
915 static const struct got_error *
916 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
917 const char *path_packidx)
919 const struct got_error *err = NULL;
920 size_t i;
922 for (i = 0; i < repo->pack_cache_size; i++) {
923 if (repo->packidx_cache[i] == NULL)
924 break;
925 if (strcmp(repo->packidx_cache[i]->path_packidx,
926 path_packidx) == 0) {
927 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
930 if (i == repo->pack_cache_size) {
931 err = got_packidx_close(repo->packidx_cache[i - 1]);
932 if (err)
933 return err;
936 /*
937 * Insert the new pack index at the front so it will
938 * be searched first in the future.
939 */
940 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
941 sizeof(repo->packidx_cache) -
942 sizeof(repo->packidx_cache[0]));
943 repo->packidx_cache[0] = packidx;
945 return NULL;
948 int
949 got_repo_is_packidx_filename(const char *name, size_t len)
951 if (len != GOT_PACKIDX_NAMELEN)
952 return 0;
954 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
955 return 0;
957 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
958 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
959 return 0;
961 return 1;
964 const struct got_error *
965 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
966 struct got_repository *repo, struct got_object_id *id)
968 const struct got_error *err;
969 DIR *packdir = NULL;
970 struct dirent *dent;
971 char *path_packidx;
972 size_t i;
973 int packdir_fd;
975 /* Search pack index cache. */
976 for (i = 0; i < repo->pack_cache_size; i++) {
977 if (repo->packidx_cache[i] == NULL)
978 break;
979 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
980 if (*idx != -1) {
981 *packidx = repo->packidx_cache[i];
982 /*
983 * Move this cache entry to the front. Repeatedly
984 * searching a wrong pack index can be expensive.
985 */
986 if (i > 0) {
987 struct got_packidx *p;
988 p = repo->packidx_cache[0];
989 repo->packidx_cache[0] = *packidx;
990 repo->packidx_cache[i] = p;
992 return NULL;
995 /* No luck. Search the filesystem. */
997 packdir_fd = openat(got_repo_get_fd(repo),
998 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
999 if (packdir_fd == -1) {
1000 if (errno == ENOENT)
1001 err = got_error_no_obj(id);
1002 else
1003 err = got_error_from_errno_fmt("openat: %s/%s",
1004 got_repo_get_path_git_dir(repo),
1005 GOT_OBJECTS_PACK_DIR);
1006 goto done;
1009 packdir = fdopendir(packdir_fd);
1010 if (packdir == NULL) {
1011 err = got_error_from_errno("fdopendir");
1012 goto done;
1015 while ((dent = readdir(packdir)) != NULL) {
1016 int is_cached = 0;
1018 if (!got_repo_is_packidx_filename(dent->d_name,
1019 strlen(dent->d_name)))
1020 continue;
1022 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1023 dent->d_name) == -1) {
1024 err = got_error_from_errno("asprintf");
1025 goto done;
1028 for (i = 0; i < repo->pack_cache_size; i++) {
1029 if (repo->packidx_cache[i] == NULL)
1030 break;
1031 if (strcmp(repo->packidx_cache[i]->path_packidx,
1032 path_packidx) == 0) {
1033 is_cached = 1;
1034 break;
1037 if (is_cached) {
1038 free(path_packidx);
1039 continue; /* already searched */
1042 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1043 path_packidx, 0);
1044 if (err) {
1045 free(path_packidx);
1046 goto done;
1049 err = cache_packidx(repo, *packidx, path_packidx);
1050 free(path_packidx);
1051 if (err)
1052 goto done;
1054 *idx = got_packidx_get_object_idx(*packidx, id);
1055 if (*idx != -1) {
1056 err = NULL; /* found the object */
1057 goto done;
1061 err = got_error_no_obj(id);
1062 done:
1063 if (packdir && closedir(packdir) != 0 && err == NULL)
1064 err = got_error_from_errno("closedir");
1065 return err;
1068 static const struct got_error *
1069 read_packfile_hdr(int fd, struct got_packidx *packidx)
1071 const struct got_error *err = NULL;
1072 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1073 struct got_packfile_hdr hdr;
1074 ssize_t n;
1076 n = read(fd, &hdr, sizeof(hdr));
1077 if (n < 0)
1078 return got_error_from_errno("read");
1079 if (n != sizeof(hdr))
1080 return got_error(GOT_ERR_BAD_PACKFILE);
1082 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1083 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1084 be32toh(hdr.nobjects) != totobj)
1085 err = got_error(GOT_ERR_BAD_PACKFILE);
1087 return err;
1090 static const struct got_error *
1091 open_packfile(int *fd, struct got_repository *repo,
1092 const char *relpath, struct got_packidx *packidx)
1094 const struct got_error *err = NULL;
1096 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1097 if (*fd == -1)
1098 return got_error_from_errno_fmt("openat: %s/%s",
1099 got_repo_get_path_git_dir(repo), relpath);
1101 if (packidx) {
1102 err = read_packfile_hdr(*fd, packidx);
1103 if (err) {
1104 close(*fd);
1105 *fd = -1;
1109 return err;
1112 const struct got_error *
1113 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1114 const char *path_packfile, struct got_packidx *packidx)
1116 const struct got_error *err = NULL;
1117 struct got_pack *pack = NULL;
1118 struct stat sb;
1119 size_t i;
1121 if (packp)
1122 *packp = NULL;
1124 for (i = 0; i < repo->pack_cache_size; i++) {
1125 pack = &repo->packs[i];
1126 if (pack->path_packfile == NULL)
1127 break;
1128 if (strcmp(pack->path_packfile, path_packfile) == 0)
1129 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1132 if (i == repo->pack_cache_size) {
1133 err = got_pack_close(&repo->packs[i - 1]);
1134 if (err)
1135 return err;
1136 memmove(&repo->packs[1], &repo->packs[0],
1137 sizeof(repo->packs) - sizeof(repo->packs[0]));
1138 i = 0;
1141 pack = &repo->packs[i];
1143 pack->path_packfile = strdup(path_packfile);
1144 if (pack->path_packfile == NULL) {
1145 err = got_error_from_errno("strdup");
1146 goto done;
1149 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1150 if (err)
1151 goto done;
1153 if (fstat(pack->fd, &sb) != 0) {
1154 err = got_error_from_errno("fstat");
1155 goto done;
1157 pack->filesize = sb.st_size;
1159 pack->privsep_child = NULL;
1161 #ifndef GOT_PACK_NO_MMAP
1162 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1163 pack->fd, 0);
1164 if (pack->map == MAP_FAILED) {
1165 if (errno != ENOMEM) {
1166 err = got_error_from_errno("mmap");
1167 goto done;
1169 pack->map = NULL; /* fall back to read(2) */
1171 #endif
1172 done:
1173 if (err) {
1174 if (pack) {
1175 free(pack->path_packfile);
1176 memset(pack, 0, sizeof(*pack));
1178 } else if (packp)
1179 *packp = pack;
1180 return err;
1183 struct got_pack *
1184 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1186 struct got_pack *pack = NULL;
1187 size_t i;
1189 for (i = 0; i < repo->pack_cache_size; i++) {
1190 pack = &repo->packs[i];
1191 if (pack->path_packfile == NULL)
1192 break;
1193 if (strcmp(pack->path_packfile, path_packfile) == 0)
1194 return pack;
1197 return NULL;
1200 const struct got_error *
1201 got_repo_init(const char *repo_path)
1203 const struct got_error *err = NULL;
1204 const char *dirnames[] = {
1205 GOT_OBJECTS_DIR,
1206 GOT_OBJECTS_PACK_DIR,
1207 GOT_REFS_DIR,
1209 const char *description_str = "Unnamed repository; "
1210 "edit this file 'description' to name the repository.";
1211 const char *headref_str = "ref: refs/heads/main";
1212 const char *gitconfig_str = "[core]\n"
1213 "\trepositoryformatversion = 0\n"
1214 "\tfilemode = true\n"
1215 "\tbare = true\n";
1216 char *path;
1217 size_t i;
1219 if (!got_path_dir_is_empty(repo_path))
1220 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1222 for (i = 0; i < nitems(dirnames); i++) {
1223 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1224 return got_error_from_errno("asprintf");
1226 err = got_path_mkdir(path);
1227 free(path);
1228 if (err)
1229 return err;
1232 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1233 return got_error_from_errno("asprintf");
1234 err = got_path_create_file(path, description_str);
1235 free(path);
1236 if (err)
1237 return err;
1239 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1240 return got_error_from_errno("asprintf");
1241 err = got_path_create_file(path, headref_str);
1242 free(path);
1243 if (err)
1244 return err;
1246 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1247 return got_error_from_errno("asprintf");
1248 err = got_path_create_file(path, gitconfig_str);
1249 free(path);
1250 if (err)
1251 return err;
1253 return NULL;
1256 static const struct got_error *
1257 match_packed_object(struct got_object_id **unique_id,
1258 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1260 const struct got_error *err = NULL;
1261 DIR *packdir = NULL;
1262 struct dirent *dent;
1263 char *path_packidx;
1264 struct got_object_id_queue matched_ids;
1265 int packdir_fd;
1267 STAILQ_INIT(&matched_ids);
1269 packdir_fd = openat(got_repo_get_fd(repo),
1270 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1271 if (packdir_fd == -1) {
1272 if (errno != ENOENT)
1273 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1274 goto done;
1277 packdir = fdopendir(packdir_fd);
1278 if (packdir == NULL) {
1279 err = got_error_from_errno("fdopendir");
1280 goto done;
1283 while ((dent = readdir(packdir)) != NULL) {
1284 struct got_packidx *packidx;
1285 struct got_object_qid *qid;
1287 if (!got_repo_is_packidx_filename(dent->d_name,
1288 strlen(dent->d_name)))
1289 continue;
1291 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1292 dent->d_name) == -1) {
1293 err = got_error_from_errno("strdup");
1294 break;
1297 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1298 path_packidx, 0);
1299 free(path_packidx);
1300 if (err)
1301 break;
1303 err = got_packidx_match_id_str_prefix(&matched_ids,
1304 packidx, id_str_prefix);
1305 if (err) {
1306 got_packidx_close(packidx);
1307 break;
1309 err = got_packidx_close(packidx);
1310 if (err)
1311 break;
1313 STAILQ_FOREACH(qid, &matched_ids, entry) {
1314 if (obj_type != GOT_OBJ_TYPE_ANY) {
1315 int matched_type;
1316 err = got_object_get_type(&matched_type, repo,
1317 qid->id);
1318 if (err)
1319 goto done;
1320 if (matched_type != obj_type)
1321 continue;
1323 if (*unique_id == NULL) {
1324 *unique_id = got_object_id_dup(qid->id);
1325 if (*unique_id == NULL) {
1326 err = got_error_from_errno("malloc");
1327 goto done;
1329 } else {
1330 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1331 continue; /* packed multiple times */
1332 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1333 goto done;
1337 done:
1338 got_object_id_queue_free(&matched_ids);
1339 if (packdir && closedir(packdir) != 0 && err == NULL)
1340 err = got_error_from_errno("closedir");
1341 if (err) {
1342 free(*unique_id);
1343 *unique_id = NULL;
1345 return err;
1348 static const struct got_error *
1349 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1350 const char *object_dir, const char *id_str_prefix, int obj_type,
1351 struct got_repository *repo)
1353 const struct got_error *err = NULL;
1354 char *path;
1355 DIR *dir = NULL;
1356 struct dirent *dent;
1357 struct got_object_id id;
1359 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1360 err = got_error_from_errno("asprintf");
1361 goto done;
1364 dir = opendir(path);
1365 if (dir == NULL) {
1366 if (errno == ENOENT) {
1367 err = NULL;
1368 goto done;
1370 err = got_error_from_errno2("opendir", path);
1371 goto done;
1373 while ((dent = readdir(dir)) != NULL) {
1374 char *id_str;
1375 int cmp;
1377 if (strcmp(dent->d_name, ".") == 0 ||
1378 strcmp(dent->d_name, "..") == 0)
1379 continue;
1381 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1382 err = got_error_from_errno("asprintf");
1383 goto done;
1386 if (!got_parse_sha1_digest(id.sha1, id_str))
1387 continue;
1390 * Directory entries do not necessarily appear in
1391 * sorted order, so we must iterate over all of them.
1393 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1394 if (cmp != 0) {
1395 free(id_str);
1396 continue;
1399 if (*unique_id == NULL) {
1400 if (obj_type != GOT_OBJ_TYPE_ANY) {
1401 int matched_type;
1402 err = got_object_get_type(&matched_type, repo,
1403 &id);
1404 if (err)
1405 goto done;
1406 if (matched_type != obj_type)
1407 continue;
1409 *unique_id = got_object_id_dup(&id);
1410 if (*unique_id == NULL) {
1411 err = got_error_from_errno("got_object_id_dup");
1412 free(id_str);
1413 goto done;
1415 } else {
1416 if (got_object_id_cmp(*unique_id, &id) == 0)
1417 continue; /* both packed and loose */
1418 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1419 free(id_str);
1420 goto done;
1423 done:
1424 if (dir && closedir(dir) != 0 && err == NULL)
1425 err = got_error_from_errno("closedir");
1426 if (err) {
1427 free(*unique_id);
1428 *unique_id = NULL;
1430 free(path);
1431 return err;
1434 const struct got_error *
1435 got_repo_match_object_id_prefix(struct got_object_id **id,
1436 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1438 const struct got_error *err = NULL;
1439 char *path_objects = got_repo_get_path_objects(repo);
1440 char *object_dir = NULL;
1441 size_t len;
1442 int i;
1444 *id = NULL;
1446 for (i = 0; i < strlen(id_str_prefix); i++) {
1447 if (isxdigit((unsigned char)id_str_prefix[i]))
1448 continue;
1449 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1452 len = strlen(id_str_prefix);
1453 if (len >= 2) {
1454 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1455 if (err)
1456 goto done;
1457 object_dir = strndup(id_str_prefix, 2);
1458 if (object_dir == NULL) {
1459 err = got_error_from_errno("strdup");
1460 goto done;
1462 err = match_loose_object(id, path_objects, object_dir,
1463 id_str_prefix, obj_type, repo);
1464 } else if (len == 1) {
1465 int i;
1466 for (i = 0; i < 0xf; i++) {
1467 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1468 == -1) {
1469 err = got_error_from_errno("asprintf");
1470 goto done;
1472 err = match_packed_object(id, repo, object_dir,
1473 obj_type);
1474 if (err)
1475 goto done;
1476 err = match_loose_object(id, path_objects, object_dir,
1477 id_str_prefix, obj_type, repo);
1478 if (err)
1479 goto done;
1481 } else {
1482 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1483 goto done;
1485 done:
1486 free(object_dir);
1487 if (err) {
1488 free(*id);
1489 *id = NULL;
1490 } else if (*id == NULL)
1491 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1493 return err;
1496 const struct got_error *
1497 got_repo_match_object_id(struct got_object_id **id, char **label,
1498 const char *id_str, int obj_type, struct got_reflist_head *refs,
1499 struct got_repository *repo)
1501 const struct got_error *err;
1502 struct got_tag_object *tag;
1503 struct got_reference *ref = NULL;
1505 *id = NULL;
1506 if (label)
1507 *label = NULL;
1509 if (refs) {
1510 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1511 refs, repo);
1512 if (err == NULL) {
1513 *id = got_object_id_dup(
1514 got_object_tag_get_object_id(tag));
1515 if (*id == NULL)
1516 err = got_error_from_errno("got_object_id_dup");
1517 else if (label && asprintf(label, "refs/tags/%s",
1518 got_object_tag_get_name(tag)) == -1) {
1519 err = got_error_from_errno("asprintf");
1520 free(*id);
1521 *id = NULL;
1523 got_object_tag_close(tag);
1524 return err;
1525 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1526 err->code != GOT_ERR_NO_OBJ)
1527 return err;
1530 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1531 if (err) {
1532 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1533 return err;
1534 err = got_ref_open(&ref, repo, id_str, 0);
1535 if (err != NULL)
1536 goto done;
1537 if (label) {
1538 *label = strdup(got_ref_get_name(ref));
1539 if (*label == NULL) {
1540 err = got_error_from_errno("strdup");
1541 goto done;
1544 err = got_ref_resolve(id, repo, ref);
1545 } else if (label) {
1546 err = got_object_id_str(label, *id);
1547 if (*label == NULL) {
1548 err = got_error_from_errno("strdup");
1549 goto done;
1552 done:
1553 if (ref)
1554 got_ref_close(ref);
1555 return err;
1558 const struct got_error *
1559 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1560 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1562 const struct got_error *err = NULL;
1563 struct got_reflist_entry *re;
1564 struct got_object_id *tag_id;
1565 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1567 *tag = NULL;
1569 TAILQ_FOREACH(re, refs, entry) {
1570 const char *refname;
1571 refname = got_ref_get_name(re->ref);
1572 if (got_ref_is_symbolic(re->ref))
1573 continue;
1574 if (strncmp(refname, "refs/tags/", 10) != 0)
1575 continue;
1576 if (!name_is_absolute)
1577 refname += strlen("refs/tags/");
1578 if (strcmp(refname, name) != 0)
1579 continue;
1580 err = got_ref_resolve(&tag_id, repo, re->ref);
1581 if (err)
1582 break;
1583 err = got_object_open_as_tag(tag, repo, tag_id);
1584 free(tag_id);
1585 if (err)
1586 break;
1587 if (obj_type == GOT_OBJ_TYPE_ANY ||
1588 got_object_tag_get_object_type(*tag) == obj_type)
1589 break;
1590 got_object_tag_close(*tag);
1591 *tag = NULL;
1594 if (err == NULL && *tag == NULL)
1595 err = got_error_path(name, GOT_ERR_NO_OBJ);
1596 return err;
1599 static const struct got_error *
1600 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1601 const char *name, mode_t mode, struct got_object_id *blob_id)
1603 const struct got_error *err = NULL;
1605 *new_te = NULL;
1607 *new_te = calloc(1, sizeof(**new_te));
1608 if (*new_te == NULL)
1609 return got_error_from_errno("calloc");
1611 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1612 sizeof((*new_te)->name)) {
1613 err = got_error(GOT_ERR_NO_SPACE);
1614 goto done;
1617 if (S_ISLNK(mode)) {
1618 (*new_te)->mode = S_IFLNK;
1619 } else {
1620 (*new_te)->mode = S_IFREG;
1621 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1623 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1624 done:
1625 if (err && *new_te) {
1626 free(*new_te);
1627 *new_te = NULL;
1629 return err;
1632 static const struct got_error *
1633 import_file(struct got_tree_entry **new_te, struct dirent *de,
1634 const char *path, struct got_repository *repo)
1636 const struct got_error *err;
1637 struct got_object_id *blob_id = NULL;
1638 char *filepath;
1639 struct stat sb;
1641 if (asprintf(&filepath, "%s%s%s", path,
1642 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1643 return got_error_from_errno("asprintf");
1645 if (lstat(filepath, &sb) != 0) {
1646 err = got_error_from_errno2("lstat", path);
1647 goto done;
1650 err = got_object_blob_create(&blob_id, filepath, repo);
1651 if (err)
1652 goto done;
1654 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1655 blob_id);
1656 done:
1657 free(filepath);
1658 if (err)
1659 free(blob_id);
1660 return err;
1663 static const struct got_error *
1664 insert_tree_entry(struct got_tree_entry *new_te,
1665 struct got_pathlist_head *paths)
1667 const struct got_error *err = NULL;
1668 struct got_pathlist_entry *new_pe;
1670 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1671 if (err)
1672 return err;
1673 if (new_pe == NULL)
1674 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1675 return NULL;
1678 static const struct got_error *write_tree(struct got_object_id **,
1679 const char *, struct got_pathlist_head *, struct got_repository *,
1680 got_repo_import_cb progress_cb, void *progress_arg);
1682 static const struct got_error *
1683 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1684 const char *path, struct got_pathlist_head *ignores,
1685 struct got_repository *repo,
1686 got_repo_import_cb progress_cb, void *progress_arg)
1688 const struct got_error *err;
1689 struct got_object_id *id = NULL;
1690 char *subdirpath;
1692 if (asprintf(&subdirpath, "%s%s%s", path,
1693 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1694 return got_error_from_errno("asprintf");
1696 (*new_te) = calloc(1, sizeof(**new_te));
1697 if (*new_te == NULL)
1698 return got_error_from_errno("calloc");
1699 (*new_te)->mode = S_IFDIR;
1700 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1701 sizeof((*new_te)->name)) {
1702 err = got_error(GOT_ERR_NO_SPACE);
1703 goto done;
1705 err = write_tree(&id, subdirpath, ignores, repo,
1706 progress_cb, progress_arg);
1707 if (err)
1708 goto done;
1709 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1711 done:
1712 free(id);
1713 free(subdirpath);
1714 if (err) {
1715 free(*new_te);
1716 *new_te = NULL;
1718 return err;
1721 static const struct got_error *
1722 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1723 struct got_pathlist_head *ignores, struct got_repository *repo,
1724 got_repo_import_cb progress_cb, void *progress_arg)
1726 const struct got_error *err = NULL;
1727 DIR *dir;
1728 struct dirent *de;
1729 int nentries;
1730 struct got_tree_entry *new_te = NULL;
1731 struct got_pathlist_head paths;
1732 struct got_pathlist_entry *pe;
1734 *new_tree_id = NULL;
1736 TAILQ_INIT(&paths);
1738 dir = opendir(path_dir);
1739 if (dir == NULL) {
1740 err = got_error_from_errno2("opendir", path_dir);
1741 goto done;
1744 nentries = 0;
1745 while ((de = readdir(dir)) != NULL) {
1746 int ignore = 0;
1747 int type;
1749 if (strcmp(de->d_name, ".") == 0 ||
1750 strcmp(de->d_name, "..") == 0)
1751 continue;
1753 TAILQ_FOREACH(pe, ignores, entry) {
1754 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1755 ignore = 1;
1756 break;
1759 if (ignore)
1760 continue;
1762 err = got_path_dirent_type(&type, path_dir, de);
1763 if (err)
1764 goto done;
1766 if (type == DT_DIR) {
1767 err = import_subdir(&new_te, de, path_dir,
1768 ignores, repo, progress_cb, progress_arg);
1769 if (err) {
1770 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1771 goto done;
1772 err = NULL;
1773 continue;
1775 } else if (type == DT_REG || type == DT_LNK) {
1776 err = import_file(&new_te, de, path_dir, repo);
1777 if (err)
1778 goto done;
1779 } else
1780 continue;
1782 err = insert_tree_entry(new_te, &paths);
1783 if (err)
1784 goto done;
1785 nentries++;
1788 if (TAILQ_EMPTY(&paths)) {
1789 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1790 "cannot create tree without any entries");
1791 goto done;
1794 TAILQ_FOREACH(pe, &paths, entry) {
1795 struct got_tree_entry *te = pe->data;
1796 char *path;
1797 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1798 continue;
1799 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1800 err = got_error_from_errno("asprintf");
1801 goto done;
1803 err = (*progress_cb)(progress_arg, path);
1804 free(path);
1805 if (err)
1806 goto done;
1809 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1810 done:
1811 if (dir)
1812 closedir(dir);
1813 got_pathlist_free(&paths);
1814 return err;
1817 const struct got_error *
1818 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1819 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1820 struct got_repository *repo, got_repo_import_cb progress_cb,
1821 void *progress_arg)
1823 const struct got_error *err;
1824 struct got_object_id *new_tree_id;
1826 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1827 progress_cb, progress_arg);
1828 if (err)
1829 return err;
1831 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1832 author, time(NULL), author, time(NULL), logmsg, repo);
1833 free(new_tree_id);
1834 return err;
1837 const struct got_error *
1838 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1839 struct got_repository *repo)
1841 const struct got_error *err = NULL;
1842 char *path_objects = NULL, *path = NULL;
1843 DIR *dir = NULL;
1844 struct got_object_id id;
1845 int i;
1847 *nobjects = 0;
1848 *ondisk_size = 0;
1850 path_objects = got_repo_get_path_objects(repo);
1851 if (path_objects == NULL)
1852 return got_error_from_errno("got_repo_get_path_objects");
1854 for (i = 0; i <= 0xff; i++) {
1855 struct dirent *dent;
1857 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1858 err = got_error_from_errno("asprintf");
1859 break;
1862 dir = opendir(path);
1863 if (dir == NULL) {
1864 if (errno == ENOENT) {
1865 err = NULL;
1866 continue;
1868 err = got_error_from_errno2("opendir", path);
1869 break;
1872 while ((dent = readdir(dir)) != NULL) {
1873 char *id_str;
1874 int fd;
1875 struct stat sb;
1877 if (strcmp(dent->d_name, ".") == 0 ||
1878 strcmp(dent->d_name, "..") == 0)
1879 continue;
1881 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1882 err = got_error_from_errno("asprintf");
1883 goto done;
1886 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1887 free(id_str);
1888 continue;
1890 free(id_str);
1892 err = got_object_open_loose_fd(&fd, &id, repo);
1893 if (err)
1894 goto done;
1896 if (fstat(fd, &sb) == -1) {
1897 err = got_error_from_errno("fstat");
1898 close(fd);
1899 goto done;
1901 (*nobjects)++;
1902 (*ondisk_size) += sb.st_size;
1904 if (close(fd) == -1) {
1905 err = got_error_from_errno("close");
1906 goto done;
1910 if (closedir(dir) != 0) {
1911 err = got_error_from_errno("closedir");
1912 goto done;
1914 dir = NULL;
1916 free(path);
1917 path = NULL;
1919 done:
1920 if (dir && closedir(dir) != 0 && err == NULL)
1921 err = got_error_from_errno("closedir");
1923 if (err) {
1924 *nobjects = 0;
1925 *ondisk_size = 0;
1927 free(path_objects);
1928 free(path);
1929 return err;
1932 const struct got_error *
1933 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1934 off_t *total_packsize, struct got_repository *repo)
1936 const struct got_error *err = NULL;
1937 DIR *packdir = NULL;
1938 struct dirent *dent;
1939 struct got_packidx *packidx = NULL;
1940 char *path_packidx;
1941 char *path_packfile;
1942 int packdir_fd;
1943 struct stat sb;
1945 *npackfiles = 0;
1946 *nobjects = 0;
1947 *total_packsize = 0;
1949 packdir_fd = openat(got_repo_get_fd(repo),
1950 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1951 if (packdir_fd == -1) {
1952 return got_error_from_errno_fmt("openat: %s/%s",
1953 got_repo_get_path_git_dir(repo),
1954 GOT_OBJECTS_PACK_DIR);
1957 packdir = fdopendir(packdir_fd);
1958 if (packdir == NULL) {
1959 err = got_error_from_errno("fdopendir");
1960 goto done;
1963 while ((dent = readdir(packdir)) != NULL) {
1964 if (!got_repo_is_packidx_filename(dent->d_name,
1965 strlen(dent->d_name)))
1966 continue;
1968 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1969 dent->d_name) == -1) {
1970 err = got_error_from_errno("asprintf");
1971 goto done;
1974 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1975 path_packidx, 0);
1976 free(path_packidx);
1977 if (err)
1978 goto done;
1980 if (fstat(packidx->fd, &sb) == -1)
1981 goto done;
1982 *total_packsize += sb.st_size;
1984 err = got_packidx_get_packfile_path(&path_packfile,
1985 packidx->path_packidx);
1986 if (err)
1987 goto done;
1989 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1990 0) == -1) {
1991 free(path_packfile);
1992 goto done;
1994 free(path_packfile);
1995 *total_packsize += sb.st_size;
1997 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
1999 (*npackfiles)++;
2001 got_packidx_close(packidx);
2002 packidx = NULL;
2004 done:
2005 if (packidx)
2006 got_packidx_close(packidx);
2007 if (packdir && closedir(packdir) != 0 && err == NULL)
2008 err = got_error_from_errno("closedir");
2009 if (err) {
2010 *npackfiles = 0;
2011 *nobjects = 0;
2012 *total_packsize = 0;
2014 return err;