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_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 int
116 got_repo_is_bare(struct got_repository *repo)
118 return (strcmp(repo->path, repo->path_git_dir) == 0);
121 static char *
122 get_path_git_child(struct got_repository *repo, const char *basename)
124 char *path_child;
126 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
127 basename) == -1)
128 return NULL;
130 return path_child;
133 char *
134 got_repo_get_path_objects(struct got_repository *repo)
136 return get_path_git_child(repo, GOT_OBJECTS_DIR);
139 char *
140 got_repo_get_path_objects_pack(struct got_repository *repo)
142 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
145 char *
146 got_repo_get_path_refs(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_REFS_DIR);
151 char *
152 got_repo_get_path_packed_refs(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
157 static char *
158 get_path_head(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_HEAD_FILE);
163 char *
164 got_repo_get_path_gitconfig(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_GITCONFIG);
169 char *
170 got_repo_get_path_gotconfig(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
175 const struct got_gotconfig *
176 got_repo_get_gotconfig(struct got_repository *repo)
178 return repo->gotconfig;
181 void
182 got_repo_get_gitconfig_remotes(int *nremotes,
183 const struct got_remote_repo **remotes, struct got_repository *repo)
185 *nremotes = repo->ngitconfig_remotes;
186 *remotes = repo->gitconfig_remotes;
189 static int
190 is_git_repo(struct got_repository *repo)
192 const char *path_git = got_repo_get_path_git_dir(repo);
193 char *path_objects = got_repo_get_path_objects(repo);
194 char *path_refs = got_repo_get_path_refs(repo);
195 char *path_head = get_path_head(repo);
196 int ret = 0;
197 struct stat sb;
198 struct got_reference *head_ref;
200 if (lstat(path_git, &sb) == -1)
201 goto done;
202 if (!S_ISDIR(sb.st_mode))
203 goto done;
205 if (lstat(path_objects, &sb) == -1)
206 goto done;
207 if (!S_ISDIR(sb.st_mode))
208 goto done;
210 if (lstat(path_refs, &sb) == -1)
211 goto done;
212 if (!S_ISDIR(sb.st_mode))
213 goto done;
215 if (lstat(path_head, &sb) == -1)
216 goto done;
217 if (!S_ISREG(sb.st_mode))
218 goto done;
220 /* Check if the HEAD reference can be opened. */
221 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
222 goto done;
223 got_ref_close(head_ref);
225 ret = 1;
226 done:
227 free(path_objects);
228 free(path_refs);
229 free(path_head);
230 return ret;
234 const struct got_error *
235 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
236 struct got_object *obj)
238 #ifndef GOT_NO_OBJ_CACHE
239 const struct got_error *err = NULL;
240 err = got_object_cache_add(&repo->objcache, id, obj);
241 if (err) {
242 if (err->code == GOT_ERR_OBJ_EXISTS ||
243 err->code == GOT_ERR_OBJ_TOO_LARGE)
244 err = NULL;
245 return err;
247 obj->refcnt++;
248 #endif
249 return NULL;
252 struct got_object *
253 got_repo_get_cached_object(struct got_repository *repo,
254 struct got_object_id *id)
256 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
259 const struct got_error *
260 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
261 struct got_tree_object *tree)
263 #ifndef GOT_NO_OBJ_CACHE
264 const struct got_error *err = NULL;
265 err = got_object_cache_add(&repo->treecache, id, tree);
266 if (err) {
267 if (err->code == GOT_ERR_OBJ_EXISTS ||
268 err->code == GOT_ERR_OBJ_TOO_LARGE)
269 err = NULL;
270 return err;
272 tree->refcnt++;
273 #endif
274 return NULL;
277 struct got_tree_object *
278 got_repo_get_cached_tree(struct got_repository *repo,
279 struct got_object_id *id)
281 return (struct got_tree_object *)got_object_cache_get(
282 &repo->treecache, id);
285 const struct got_error *
286 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
287 struct got_commit_object *commit)
289 #ifndef GOT_NO_OBJ_CACHE
290 const struct got_error *err = NULL;
291 err = got_object_cache_add(&repo->commitcache, id, commit);
292 if (err) {
293 if (err->code == GOT_ERR_OBJ_EXISTS ||
294 err->code == GOT_ERR_OBJ_TOO_LARGE)
295 err = NULL;
296 return err;
298 commit->refcnt++;
299 #endif
300 return NULL;
303 struct got_commit_object *
304 got_repo_get_cached_commit(struct got_repository *repo,
305 struct got_object_id *id)
307 return (struct got_commit_object *)got_object_cache_get(
308 &repo->commitcache, id);
311 const struct got_error *
312 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
313 struct got_tag_object *tag)
315 #ifndef GOT_NO_OBJ_CACHE
316 const struct got_error *err = NULL;
317 err = got_object_cache_add(&repo->tagcache, id, tag);
318 if (err) {
319 if (err->code == GOT_ERR_OBJ_EXISTS ||
320 err->code == GOT_ERR_OBJ_TOO_LARGE)
321 err = NULL;
322 return err;
324 tag->refcnt++;
325 #endif
326 return NULL;
329 struct got_tag_object *
330 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
332 return (struct got_tag_object *)got_object_cache_get(
333 &repo->tagcache, id);
336 static const struct got_error *
337 open_repo(struct got_repository *repo, const char *path)
339 const struct got_error *err = NULL;
341 repo->gitdir_fd = -1;
343 /* bare git repository? */
344 repo->path_git_dir = strdup(path);
345 if (repo->path_git_dir == NULL)
346 return got_error_from_errno("strdup");
347 if (is_git_repo(repo)) {
348 repo->path = strdup(repo->path_git_dir);
349 if (repo->path == NULL) {
350 err = got_error_from_errno("strdup");
351 goto done;
353 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
354 if (repo->gitdir_fd == -1) {
355 err = got_error_from_errno2("open",
356 repo->path_git_dir);
357 goto done;
359 return NULL;
362 /* git repository with working tree? */
363 free(repo->path_git_dir);
364 repo->path_git_dir = NULL;
365 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
366 err = got_error_from_errno("asprintf");
367 goto done;
369 if (is_git_repo(repo)) {
370 repo->path = strdup(path);
371 if (repo->path == NULL) {
372 err = got_error_from_errno("strdup");
373 goto done;
375 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
376 if (repo->gitdir_fd == -1) {
377 err = got_error_from_errno2("open",
378 repo->path_git_dir);
379 goto done;
381 return NULL;
384 err = got_error(GOT_ERR_NOT_GIT_REPO);
385 done:
386 if (err) {
387 free(repo->path);
388 repo->path = NULL;
389 free(repo->path_git_dir);
390 repo->path_git_dir = NULL;
391 if (repo->gitdir_fd != -1)
392 close(repo->gitdir_fd);
393 repo->gitdir_fd = -1;
396 return err;
399 static const struct got_error *
400 parse_gitconfig_file(int *gitconfig_repository_format_version,
401 char **gitconfig_author_name, char **gitconfig_author_email,
402 struct got_remote_repo **remotes, int *nremotes,
403 char **gitconfig_owner, char ***extensions, int *nextensions,
404 const char *gitconfig_path)
406 const struct got_error *err = NULL, *child_err = NULL;
407 int fd = -1;
408 int imsg_fds[2] = { -1, -1 };
409 pid_t pid;
410 struct imsgbuf *ibuf;
412 *gitconfig_repository_format_version = 0;
413 if (extensions)
414 *extensions = NULL;
415 if (nextensions)
416 *nextensions = 0;
417 *gitconfig_author_name = NULL;
418 *gitconfig_author_email = NULL;
419 if (remotes)
420 *remotes = NULL;
421 if (nremotes)
422 *nremotes = 0;
423 if (gitconfig_owner)
424 *gitconfig_owner = NULL;
426 fd = open(gitconfig_path, O_RDONLY);
427 if (fd == -1) {
428 if (errno == ENOENT)
429 return NULL;
430 return got_error_from_errno2("open", gitconfig_path);
433 ibuf = calloc(1, sizeof(*ibuf));
434 if (ibuf == NULL) {
435 err = got_error_from_errno("calloc");
436 goto done;
439 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
440 err = got_error_from_errno("socketpair");
441 goto done;
444 pid = fork();
445 if (pid == -1) {
446 err = got_error_from_errno("fork");
447 goto done;
448 } else if (pid == 0) {
449 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
450 gitconfig_path);
451 /* not reached */
454 if (close(imsg_fds[1]) == -1) {
455 err = got_error_from_errno("close");
456 goto done;
458 imsg_fds[1] = -1;
459 imsg_init(ibuf, imsg_fds[0]);
461 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
462 if (err)
463 goto done;
464 fd = -1;
466 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
467 if (err)
468 goto done;
470 err = got_privsep_recv_gitconfig_int(
471 gitconfig_repository_format_version, ibuf);
472 if (err)
473 goto done;
475 if (extensions && nextensions) {
476 err = got_privsep_send_gitconfig_repository_extensions_req(
477 ibuf);
478 if (err)
479 goto done;
480 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
481 if (err)
482 goto done;
483 if (*nextensions > 0) {
484 int i;
485 *extensions = calloc(*nextensions, sizeof(char *));
486 if (*extensions == NULL) {
487 err = got_error_from_errno("calloc");
488 goto done;
490 for (i = 0; i < *nextensions; i++) {
491 char *ext;
492 err = got_privsep_recv_gitconfig_str(&ext,
493 ibuf);
494 if (err)
495 goto done;
496 (*extensions)[i] = ext;
501 err = got_privsep_send_gitconfig_author_name_req(ibuf);
502 if (err)
503 goto done;
505 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
506 if (err)
507 goto done;
509 err = got_privsep_send_gitconfig_author_email_req(ibuf);
510 if (err)
511 goto done;
513 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
514 if (err)
515 goto done;
517 if (remotes && nremotes) {
518 err = got_privsep_send_gitconfig_remotes_req(ibuf);
519 if (err)
520 goto done;
522 err = got_privsep_recv_gitconfig_remotes(remotes,
523 nremotes, ibuf);
524 if (err)
525 goto done;
528 if (gitconfig_owner) {
529 err = got_privsep_send_gitconfig_owner_req(ibuf);
530 if (err)
531 goto done;
532 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
533 if (err)
534 goto done;
537 imsg_clear(ibuf);
538 err = got_privsep_send_stop(imsg_fds[0]);
539 child_err = got_privsep_wait_for_child(pid);
540 if (child_err && err == NULL)
541 err = child_err;
542 done:
543 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
544 err = got_error_from_errno("close");
545 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
546 err = got_error_from_errno("close");
547 if (fd != -1 && close(fd) == -1 && err == NULL)
548 err = got_error_from_errno2("close", gitconfig_path);
549 free(ibuf);
550 return err;
553 static const struct got_error *
554 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
556 const struct got_error *err = NULL;
557 char *repo_gitconfig_path = NULL;
559 if (global_gitconfig_path) {
560 /* Read settings from ~/.gitconfig. */
561 int dummy_repo_version;
562 err = parse_gitconfig_file(&dummy_repo_version,
563 &repo->global_gitconfig_author_name,
564 &repo->global_gitconfig_author_email,
565 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
566 if (err)
567 return err;
570 /* Read repository's .git/config file. */
571 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
572 if (repo_gitconfig_path == NULL)
573 return got_error_from_errno("got_repo_get_path_gitconfig");
575 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
576 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
577 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
578 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
579 repo_gitconfig_path);
580 if (err)
581 goto done;
582 done:
583 free(repo_gitconfig_path);
584 return err;
587 static const struct got_error *
588 read_gotconfig(struct got_repository *repo)
590 const struct got_error *err = NULL;
591 char *gotconfig_path;
593 gotconfig_path = got_repo_get_path_gotconfig(repo);
594 if (gotconfig_path == NULL)
595 return got_error_from_errno("got_repo_get_path_gotconfig");
597 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
598 free(gotconfig_path);
599 return err;
602 /* Supported repository format extensions. */
603 static const char *repo_extensions[] = {
604 "noop", /* Got supports repository format version 1. */
605 "preciousObjects", /* Got has no garbage collection yet. */
606 "worktreeConfig", /* Got does not care about Git work trees. */
607 };
609 const struct got_error *
610 got_repo_open(struct got_repository **repop, const char *path,
611 const char *global_gitconfig_path)
613 struct got_repository *repo = NULL;
614 const struct got_error *err = NULL;
615 char *repo_path = NULL;
616 size_t i;
617 struct rlimit rl;
619 *repop = NULL;
621 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
622 return got_error_from_errno("getrlimit");
624 repo = calloc(1, sizeof(*repo));
625 if (repo == NULL) {
626 err = got_error_from_errno("calloc");
627 goto done;
630 for (i = 0; i < nitems(repo->privsep_children); i++) {
631 memset(&repo->privsep_children[i], 0,
632 sizeof(repo->privsep_children[0]));
633 repo->privsep_children[i].imsg_fd = -1;
636 err = got_object_cache_init(&repo->objcache,
637 GOT_OBJECT_CACHE_TYPE_OBJ);
638 if (err)
639 goto done;
640 err = got_object_cache_init(&repo->treecache,
641 GOT_OBJECT_CACHE_TYPE_TREE);
642 if (err)
643 goto done;
644 err = got_object_cache_init(&repo->commitcache,
645 GOT_OBJECT_CACHE_TYPE_COMMIT);
646 if (err)
647 goto done;
648 err = got_object_cache_init(&repo->tagcache,
649 GOT_OBJECT_CACHE_TYPE_TAG);
650 if (err)
651 goto done;
653 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
654 if (repo->pack_cache_size > rl.rlim_cur / 8)
655 repo->pack_cache_size = rl.rlim_cur / 8;
657 repo_path = realpath(path, NULL);
658 if (repo_path == NULL) {
659 err = got_error_from_errno2("realpath", path);
660 goto done;
663 for (;;) {
664 char *parent_path;
666 err = open_repo(repo, repo_path);
667 if (err == NULL)
668 break;
669 if (err->code != GOT_ERR_NOT_GIT_REPO)
670 goto done;
671 if (repo_path[0] == '/' && repo_path[1] == '\0') {
672 err = got_error(GOT_ERR_NOT_GIT_REPO);
673 goto done;
675 err = got_path_dirname(&parent_path, repo_path);
676 if (err)
677 goto done;
678 free(repo_path);
679 repo_path = parent_path;
682 err = read_gotconfig(repo);
683 if (err)
684 goto done;
686 err = read_gitconfig(repo, global_gitconfig_path);
687 if (err)
688 goto done;
689 if (repo->gitconfig_repository_format_version != 0)
690 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
691 for (i = 0; i < repo->nextensions; i++) {
692 char *ext = repo->extensions[i];
693 int j, supported = 0;
694 for (j = 0; j < nitems(repo_extensions); j++) {
695 if (strcmp(ext, repo_extensions[j]) == 0) {
696 supported = 1;
697 break;
700 if (!supported) {
701 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
702 goto done;
705 done:
706 if (err)
707 got_repo_close(repo);
708 else
709 *repop = repo;
710 free(repo_path);
711 return err;
714 const struct got_error *
715 got_repo_close(struct got_repository *repo)
717 const struct got_error *err = NULL, *child_err;
718 size_t i;
720 for (i = 0; i < repo->pack_cache_size; i++) {
721 if (repo->packidx_cache[i] == NULL)
722 break;
723 got_packidx_close(repo->packidx_cache[i]);
726 for (i = 0; i < repo->pack_cache_size; i++) {
727 if (repo->packs[i].path_packfile == NULL)
728 break;
729 got_pack_close(&repo->packs[i]);
732 free(repo->path);
733 free(repo->path_git_dir);
735 got_object_cache_close(&repo->objcache);
736 got_object_cache_close(&repo->treecache);
737 got_object_cache_close(&repo->commitcache);
738 got_object_cache_close(&repo->tagcache);
740 for (i = 0; i < nitems(repo->privsep_children); i++) {
741 if (repo->privsep_children[i].imsg_fd == -1)
742 continue;
743 imsg_clear(repo->privsep_children[i].ibuf);
744 free(repo->privsep_children[i].ibuf);
745 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
746 child_err = got_privsep_wait_for_child(
747 repo->privsep_children[i].pid);
748 if (child_err && err == NULL)
749 err = child_err;
750 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
751 err == NULL)
752 err = got_error_from_errno("close");
755 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
756 err == NULL)
757 err = got_error_from_errno("close");
759 if (repo->gotconfig)
760 got_gotconfig_free(repo->gotconfig);
761 free(repo->gitconfig_author_name);
762 free(repo->gitconfig_author_email);
763 for (i = 0; i < repo->ngitconfig_remotes; i++)
764 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
765 free(repo->gitconfig_remotes);
766 for (i = 0; i < repo->nextensions; i++)
767 free(repo->extensions[i]);
768 free(repo->extensions);
769 free(repo);
771 return err;
774 void
775 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
777 int i;
779 free(repo->name);
780 repo->name = NULL;
781 free(repo->url);
782 repo->url = NULL;
783 for (i = 0; i < repo->nbranches; i++)
784 free(repo->branches[i]);
785 free(repo->branches);
786 repo->branches = NULL;
787 repo->nbranches = 0;
790 const struct got_error *
791 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
792 const char *input_path)
794 const struct got_error *err = NULL;
795 const char *repo_abspath = NULL;
796 size_t repolen, len;
797 char *canonpath, *path = NULL;
799 *in_repo_path = NULL;
801 canonpath = strdup(input_path);
802 if (canonpath == NULL) {
803 err = got_error_from_errno("strdup");
804 goto done;
806 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
807 if (err)
808 goto done;
810 repo_abspath = got_repo_get_path(repo);
812 if (canonpath[0] == '\0') {
813 path = strdup(canonpath);
814 if (path == NULL) {
815 err = got_error_from_errno("strdup");
816 goto done;
818 } else {
819 path = realpath(canonpath, NULL);
820 if (path == NULL) {
821 if (errno != ENOENT) {
822 err = got_error_from_errno2("realpath",
823 canonpath);
824 goto done;
826 /*
827 * Path is not on disk.
828 * Assume it is already relative to repository root.
829 */
830 path = strdup(canonpath);
831 if (path == NULL) {
832 err = got_error_from_errno("strdup");
833 goto done;
837 repolen = strlen(repo_abspath);
838 len = strlen(path);
841 if (strcmp(path, repo_abspath) == 0) {
842 free(path);
843 path = strdup("");
844 if (path == NULL) {
845 err = got_error_from_errno("strdup");
846 goto done;
848 } else if (len > repolen &&
849 got_path_is_child(path, repo_abspath, repolen)) {
850 /* Matched an on-disk path inside repository. */
851 if (got_repo_is_bare(repo)) {
852 /*
853 * Matched an on-disk path inside repository
854 * database. Treat input as repository-relative.
855 */
856 free(path);
857 path = canonpath;
858 canonpath = NULL;
859 } else {
860 char *child;
861 /* Strip common prefix with repository path. */
862 err = got_path_skip_common_ancestor(&child,
863 repo_abspath, path);
864 if (err)
865 goto done;
866 free(path);
867 path = child;
869 } else {
870 /*
871 * Matched unrelated on-disk path.
872 * Treat input as repository-relative.
873 */
874 free(path);
875 path = canonpath;
876 canonpath = NULL;
880 /* Make in-repository path absolute */
881 if (path[0] != '/') {
882 char *abspath;
883 if (asprintf(&abspath, "/%s", path) == -1) {
884 err = got_error_from_errno("asprintf");
885 goto done;
887 free(path);
888 path = abspath;
891 done:
892 free(canonpath);
893 if (err)
894 free(path);
895 else
896 *in_repo_path = path;
897 return err;
900 static const struct got_error *
901 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
902 const char *path_packidx)
904 const struct got_error *err = NULL;
905 size_t i;
907 for (i = 0; i < repo->pack_cache_size; i++) {
908 if (repo->packidx_cache[i] == NULL)
909 break;
910 if (strcmp(repo->packidx_cache[i]->path_packidx,
911 path_packidx) == 0) {
912 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
915 if (i == repo->pack_cache_size) {
916 err = got_packidx_close(repo->packidx_cache[i - 1]);
917 if (err)
918 return err;
921 /*
922 * Insert the new pack index at the front so it will
923 * be searched first in the future.
924 */
925 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
926 sizeof(repo->packidx_cache) -
927 sizeof(repo->packidx_cache[0]));
928 repo->packidx_cache[0] = packidx;
930 return NULL;
933 static int
934 is_packidx_filename(const char *name, size_t len)
936 if (len != GOT_PACKIDX_NAMELEN)
937 return 0;
939 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
940 return 0;
942 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
943 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
944 return 0;
946 return 1;
949 const struct got_error *
950 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
951 struct got_repository *repo, struct got_object_id *id)
953 const struct got_error *err;
954 DIR *packdir = NULL;
955 struct dirent *dent;
956 char *path_packidx;
957 size_t i;
958 int packdir_fd;
960 /* Search pack index cache. */
961 for (i = 0; i < repo->pack_cache_size; i++) {
962 if (repo->packidx_cache[i] == NULL)
963 break;
964 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
965 if (*idx != -1) {
966 *packidx = repo->packidx_cache[i];
967 /*
968 * Move this cache entry to the front. Repeatedly
969 * searching a wrong pack index can be expensive.
970 */
971 if (i > 0) {
972 struct got_packidx *p;
973 p = repo->packidx_cache[0];
974 repo->packidx_cache[0] = *packidx;
975 repo->packidx_cache[i] = p;
977 return NULL;
980 /* No luck. Search the filesystem. */
982 packdir_fd = openat(got_repo_get_fd(repo),
983 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
984 if (packdir_fd == -1) {
985 if (errno == ENOENT)
986 err = got_error_no_obj(id);
987 else
988 err = got_error_from_errno_fmt("openat: %s/%s",
989 got_repo_get_path_git_dir(repo),
990 GOT_OBJECTS_PACK_DIR);
991 goto done;
994 packdir = fdopendir(packdir_fd);
995 if (packdir == NULL) {
996 err = got_error_from_errno("fdopendir");
997 goto done;
1000 while ((dent = readdir(packdir)) != NULL) {
1001 int is_cached = 0;
1003 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1004 continue;
1006 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1007 dent->d_name) == -1) {
1008 err = got_error_from_errno("asprintf");
1009 goto done;
1012 for (i = 0; i < repo->pack_cache_size; i++) {
1013 if (repo->packidx_cache[i] == NULL)
1014 break;
1015 if (strcmp(repo->packidx_cache[i]->path_packidx,
1016 path_packidx) == 0) {
1017 is_cached = 1;
1018 break;
1021 if (is_cached) {
1022 free(path_packidx);
1023 continue; /* already searched */
1026 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1027 path_packidx, 0);
1028 if (err) {
1029 free(path_packidx);
1030 goto done;
1033 err = cache_packidx(repo, *packidx, path_packidx);
1034 free(path_packidx);
1035 if (err)
1036 goto done;
1038 *idx = got_packidx_get_object_idx(*packidx, id);
1039 if (*idx != -1) {
1040 err = NULL; /* found the object */
1041 goto done;
1045 err = got_error_no_obj(id);
1046 done:
1047 if (packdir && closedir(packdir) != 0 && err == NULL)
1048 err = got_error_from_errno("closedir");
1049 return err;
1052 static const struct got_error *
1053 read_packfile_hdr(int fd, struct got_packidx *packidx)
1055 const struct got_error *err = NULL;
1056 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1057 struct got_packfile_hdr hdr;
1058 ssize_t n;
1060 n = read(fd, &hdr, sizeof(hdr));
1061 if (n < 0)
1062 return got_error_from_errno("read");
1063 if (n != sizeof(hdr))
1064 return got_error(GOT_ERR_BAD_PACKFILE);
1066 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1067 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1068 be32toh(hdr.nobjects) != totobj)
1069 err = got_error(GOT_ERR_BAD_PACKFILE);
1071 return err;
1074 static const struct got_error *
1075 open_packfile(int *fd, struct got_repository *repo,
1076 const char *relpath, struct got_packidx *packidx)
1078 const struct got_error *err = NULL;
1080 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1081 if (*fd == -1)
1082 return got_error_from_errno_fmt("openat: %s/%s",
1083 got_repo_get_path_git_dir(repo), relpath);
1085 if (packidx) {
1086 err = read_packfile_hdr(*fd, packidx);
1087 if (err) {
1088 close(*fd);
1089 *fd = -1;
1093 return err;
1096 const struct got_error *
1097 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1098 const char *path_packfile, struct got_packidx *packidx)
1100 const struct got_error *err = NULL;
1101 struct got_pack *pack = NULL;
1102 struct stat sb;
1103 size_t i;
1105 if (packp)
1106 *packp = NULL;
1108 for (i = 0; i < repo->pack_cache_size; i++) {
1109 pack = &repo->packs[i];
1110 if (pack->path_packfile == NULL)
1111 break;
1112 if (strcmp(pack->path_packfile, path_packfile) == 0)
1113 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1116 if (i == repo->pack_cache_size) {
1117 err = got_pack_close(&repo->packs[i - 1]);
1118 if (err)
1119 return err;
1120 memmove(&repo->packs[1], &repo->packs[0],
1121 sizeof(repo->packs) - sizeof(repo->packs[0]));
1122 i = 0;
1125 pack = &repo->packs[i];
1127 pack->path_packfile = strdup(path_packfile);
1128 if (pack->path_packfile == NULL) {
1129 err = got_error_from_errno("strdup");
1130 goto done;
1133 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1134 if (err)
1135 goto done;
1137 if (fstat(pack->fd, &sb) != 0) {
1138 err = got_error_from_errno("fstat");
1139 goto done;
1141 pack->filesize = sb.st_size;
1143 pack->privsep_child = NULL;
1145 #ifndef GOT_PACK_NO_MMAP
1146 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1147 pack->fd, 0);
1148 if (pack->map == MAP_FAILED) {
1149 if (errno != ENOMEM) {
1150 err = got_error_from_errno("mmap");
1151 goto done;
1153 pack->map = NULL; /* fall back to read(2) */
1155 #endif
1156 done:
1157 if (err) {
1158 if (pack) {
1159 free(pack->path_packfile);
1160 memset(pack, 0, sizeof(*pack));
1162 } else if (packp)
1163 *packp = pack;
1164 return err;
1167 struct got_pack *
1168 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1170 struct got_pack *pack = NULL;
1171 size_t i;
1173 for (i = 0; i < repo->pack_cache_size; i++) {
1174 pack = &repo->packs[i];
1175 if (pack->path_packfile == NULL)
1176 break;
1177 if (strcmp(pack->path_packfile, path_packfile) == 0)
1178 return pack;
1181 return NULL;
1184 const struct got_error *
1185 got_repo_init(const char *repo_path)
1187 const struct got_error *err = NULL;
1188 const char *dirnames[] = {
1189 GOT_OBJECTS_DIR,
1190 GOT_OBJECTS_PACK_DIR,
1191 GOT_REFS_DIR,
1193 const char *description_str = "Unnamed repository; "
1194 "edit this file 'description' to name the repository.";
1195 const char *headref_str = "ref: refs/heads/main";
1196 const char *gitconfig_str = "[core]\n"
1197 "\trepositoryformatversion = 0\n"
1198 "\tfilemode = true\n"
1199 "\tbare = true\n";
1200 char *path;
1201 size_t i;
1203 if (!got_path_dir_is_empty(repo_path))
1204 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1206 for (i = 0; i < nitems(dirnames); i++) {
1207 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1208 return got_error_from_errno("asprintf");
1210 err = got_path_mkdir(path);
1211 free(path);
1212 if (err)
1213 return err;
1216 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1217 return got_error_from_errno("asprintf");
1218 err = got_path_create_file(path, description_str);
1219 free(path);
1220 if (err)
1221 return err;
1223 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1224 return got_error_from_errno("asprintf");
1225 err = got_path_create_file(path, headref_str);
1226 free(path);
1227 if (err)
1228 return err;
1230 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1231 return got_error_from_errno("asprintf");
1232 err = got_path_create_file(path, gitconfig_str);
1233 free(path);
1234 if (err)
1235 return err;
1237 return NULL;
1240 static const struct got_error *
1241 match_packed_object(struct got_object_id **unique_id,
1242 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1244 const struct got_error *err = NULL;
1245 DIR *packdir = NULL;
1246 struct dirent *dent;
1247 char *path_packidx;
1248 struct got_object_id_queue matched_ids;
1249 int packdir_fd;
1251 SIMPLEQ_INIT(&matched_ids);
1253 packdir_fd = openat(got_repo_get_fd(repo),
1254 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1255 if (packdir_fd == -1) {
1256 if (errno != ENOENT)
1257 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1258 goto done;
1261 packdir = fdopendir(packdir_fd);
1262 if (packdir == NULL) {
1263 err = got_error_from_errno("fdopendir");
1264 goto done;
1267 while ((dent = readdir(packdir)) != NULL) {
1268 struct got_packidx *packidx;
1269 struct got_object_qid *qid;
1271 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1272 continue;
1274 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1275 dent->d_name) == -1) {
1276 err = got_error_from_errno("strdup");
1277 break;
1280 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1281 path_packidx, 0);
1282 free(path_packidx);
1283 if (err)
1284 break;
1286 err = got_packidx_match_id_str_prefix(&matched_ids,
1287 packidx, id_str_prefix);
1288 if (err) {
1289 got_packidx_close(packidx);
1290 break;
1292 err = got_packidx_close(packidx);
1293 if (err)
1294 break;
1296 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1297 if (obj_type != GOT_OBJ_TYPE_ANY) {
1298 int matched_type;
1299 err = got_object_get_type(&matched_type, repo,
1300 qid->id);
1301 if (err)
1302 goto done;
1303 if (matched_type != obj_type)
1304 continue;
1306 if (*unique_id == NULL) {
1307 *unique_id = got_object_id_dup(qid->id);
1308 if (*unique_id == NULL) {
1309 err = got_error_from_errno("malloc");
1310 goto done;
1312 } else {
1313 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1314 continue; /* packed multiple times */
1315 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1316 goto done;
1320 done:
1321 got_object_id_queue_free(&matched_ids);
1322 if (packdir && closedir(packdir) != 0 && err == NULL)
1323 err = got_error_from_errno("closedir");
1324 if (err) {
1325 free(*unique_id);
1326 *unique_id = NULL;
1328 return err;
1331 static const struct got_error *
1332 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1333 const char *object_dir, const char *id_str_prefix, int obj_type,
1334 struct got_repository *repo)
1336 const struct got_error *err = NULL;
1337 char *path;
1338 DIR *dir = NULL;
1339 struct dirent *dent;
1340 struct got_object_id id;
1342 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1343 err = got_error_from_errno("asprintf");
1344 goto done;
1347 dir = opendir(path);
1348 if (dir == NULL) {
1349 if (errno == ENOENT) {
1350 err = NULL;
1351 goto done;
1353 err = got_error_from_errno2("opendir", path);
1354 goto done;
1356 while ((dent = readdir(dir)) != NULL) {
1357 char *id_str;
1358 int cmp;
1360 if (strcmp(dent->d_name, ".") == 0 ||
1361 strcmp(dent->d_name, "..") == 0)
1362 continue;
1364 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1365 err = got_error_from_errno("asprintf");
1366 goto done;
1369 if (!got_parse_sha1_digest(id.sha1, id_str))
1370 continue;
1373 * Directory entries do not necessarily appear in
1374 * sorted order, so we must iterate over all of them.
1376 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1377 if (cmp != 0) {
1378 free(id_str);
1379 continue;
1382 if (*unique_id == NULL) {
1383 if (obj_type != GOT_OBJ_TYPE_ANY) {
1384 int matched_type;
1385 err = got_object_get_type(&matched_type, repo,
1386 &id);
1387 if (err)
1388 goto done;
1389 if (matched_type != obj_type)
1390 continue;
1392 *unique_id = got_object_id_dup(&id);
1393 if (*unique_id == NULL) {
1394 err = got_error_from_errno("got_object_id_dup");
1395 free(id_str);
1396 goto done;
1398 } else {
1399 if (got_object_id_cmp(*unique_id, &id) == 0)
1400 continue; /* both packed and loose */
1401 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1402 free(id_str);
1403 goto done;
1406 done:
1407 if (dir && closedir(dir) != 0 && err == NULL)
1408 err = got_error_from_errno("closedir");
1409 if (err) {
1410 free(*unique_id);
1411 *unique_id = NULL;
1413 free(path);
1414 return err;
1417 const struct got_error *
1418 got_repo_match_object_id_prefix(struct got_object_id **id,
1419 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1421 const struct got_error *err = NULL;
1422 char *path_objects = got_repo_get_path_objects(repo);
1423 char *object_dir = NULL;
1424 size_t len;
1425 int i;
1427 *id = NULL;
1429 for (i = 0; i < strlen(id_str_prefix); i++) {
1430 if (isxdigit((unsigned char)id_str_prefix[i]))
1431 continue;
1432 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1435 len = strlen(id_str_prefix);
1436 if (len >= 2) {
1437 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1438 if (err)
1439 goto done;
1440 object_dir = strndup(id_str_prefix, 2);
1441 if (object_dir == NULL) {
1442 err = got_error_from_errno("strdup");
1443 goto done;
1445 err = match_loose_object(id, path_objects, object_dir,
1446 id_str_prefix, obj_type, repo);
1447 } else if (len == 1) {
1448 int i;
1449 for (i = 0; i < 0xf; i++) {
1450 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1451 == -1) {
1452 err = got_error_from_errno("asprintf");
1453 goto done;
1455 err = match_packed_object(id, repo, object_dir,
1456 obj_type);
1457 if (err)
1458 goto done;
1459 err = match_loose_object(id, path_objects, object_dir,
1460 id_str_prefix, obj_type, repo);
1461 if (err)
1462 goto done;
1464 } else {
1465 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1466 goto done;
1468 done:
1469 free(object_dir);
1470 if (err) {
1471 free(*id);
1472 *id = NULL;
1473 } else if (*id == NULL)
1474 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1476 return err;
1479 const struct got_error *
1480 got_repo_match_object_id(struct got_object_id **id, char **label,
1481 const char *id_str, int obj_type, struct got_reflist_head *refs,
1482 struct got_repository *repo)
1484 const struct got_error *err;
1485 struct got_tag_object *tag;
1486 struct got_reference *ref = NULL;
1488 *id = NULL;
1489 if (label)
1490 *label = NULL;
1492 if (refs) {
1493 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1494 refs, repo);
1495 if (err == NULL) {
1496 *id = got_object_id_dup(
1497 got_object_tag_get_object_id(tag));
1498 if (*id == NULL)
1499 err = got_error_from_errno("got_object_id_dup");
1500 else if (label && asprintf(label, "refs/tags/%s",
1501 got_object_tag_get_name(tag)) == -1) {
1502 err = got_error_from_errno("asprintf");
1503 free(*id);
1504 *id = NULL;
1506 got_object_tag_close(tag);
1507 return err;
1508 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1509 err->code != GOT_ERR_NO_OBJ)
1510 return err;
1513 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1514 if (err) {
1515 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1516 return err;
1517 err = got_ref_open(&ref, repo, id_str, 0);
1518 if (err != NULL)
1519 goto done;
1520 if (label) {
1521 *label = strdup(got_ref_get_name(ref));
1522 if (*label == NULL) {
1523 err = got_error_from_errno("strdup");
1524 goto done;
1527 err = got_ref_resolve(id, repo, ref);
1528 } else if (label) {
1529 err = got_object_id_str(label, *id);
1530 if (*label == NULL) {
1531 err = got_error_from_errno("strdup");
1532 goto done;
1535 done:
1536 if (ref)
1537 got_ref_close(ref);
1538 return err;
1541 const struct got_error *
1542 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1543 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1545 const struct got_error *err = NULL;
1546 struct got_reflist_entry *re;
1547 struct got_object_id *tag_id;
1548 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1550 *tag = NULL;
1552 TAILQ_FOREACH(re, refs, entry) {
1553 const char *refname;
1554 refname = got_ref_get_name(re->ref);
1555 if (got_ref_is_symbolic(re->ref))
1556 continue;
1557 if (strncmp(refname, "refs/tags/", 10) != 0)
1558 continue;
1559 if (!name_is_absolute)
1560 refname += strlen("refs/tags/");
1561 if (strcmp(refname, name) != 0)
1562 continue;
1563 err = got_ref_resolve(&tag_id, repo, re->ref);
1564 if (err)
1565 break;
1566 err = got_object_open_as_tag(tag, repo, tag_id);
1567 free(tag_id);
1568 if (err)
1569 break;
1570 if (obj_type == GOT_OBJ_TYPE_ANY ||
1571 got_object_tag_get_object_type(*tag) == obj_type)
1572 break;
1573 got_object_tag_close(*tag);
1574 *tag = NULL;
1577 if (err == NULL && *tag == NULL)
1578 err = got_error_path(name, GOT_ERR_NO_OBJ);
1579 return err;
1582 static const struct got_error *
1583 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1584 const char *name, mode_t mode, struct got_object_id *blob_id)
1586 const struct got_error *err = NULL;
1588 *new_te = NULL;
1590 *new_te = calloc(1, sizeof(**new_te));
1591 if (*new_te == NULL)
1592 return got_error_from_errno("calloc");
1594 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1595 sizeof((*new_te)->name)) {
1596 err = got_error(GOT_ERR_NO_SPACE);
1597 goto done;
1600 if (S_ISLNK(mode)) {
1601 (*new_te)->mode = S_IFLNK;
1602 } else {
1603 (*new_te)->mode = S_IFREG;
1604 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1606 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1607 done:
1608 if (err && *new_te) {
1609 free(*new_te);
1610 *new_te = NULL;
1612 return err;
1615 static const struct got_error *
1616 import_file(struct got_tree_entry **new_te, struct dirent *de,
1617 const char *path, struct got_repository *repo)
1619 const struct got_error *err;
1620 struct got_object_id *blob_id = NULL;
1621 char *filepath;
1622 struct stat sb;
1624 if (asprintf(&filepath, "%s%s%s", path,
1625 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1626 return got_error_from_errno("asprintf");
1628 if (lstat(filepath, &sb) != 0) {
1629 err = got_error_from_errno2("lstat", path);
1630 goto done;
1633 err = got_object_blob_create(&blob_id, filepath, repo);
1634 if (err)
1635 goto done;
1637 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1638 blob_id);
1639 done:
1640 free(filepath);
1641 if (err)
1642 free(blob_id);
1643 return err;
1646 static const struct got_error *
1647 insert_tree_entry(struct got_tree_entry *new_te,
1648 struct got_pathlist_head *paths)
1650 const struct got_error *err = NULL;
1651 struct got_pathlist_entry *new_pe;
1653 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1654 if (err)
1655 return err;
1656 if (new_pe == NULL)
1657 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1658 return NULL;
1661 static const struct got_error *write_tree(struct got_object_id **,
1662 const char *, struct got_pathlist_head *, struct got_repository *,
1663 got_repo_import_cb progress_cb, void *progress_arg);
1665 static const struct got_error *
1666 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1667 const char *path, struct got_pathlist_head *ignores,
1668 struct got_repository *repo,
1669 got_repo_import_cb progress_cb, void *progress_arg)
1671 const struct got_error *err;
1672 struct got_object_id *id = NULL;
1673 char *subdirpath;
1675 if (asprintf(&subdirpath, "%s%s%s", path,
1676 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1677 return got_error_from_errno("asprintf");
1679 (*new_te) = calloc(1, sizeof(**new_te));
1680 if (*new_te == NULL)
1681 return got_error_from_errno("calloc");
1682 (*new_te)->mode = S_IFDIR;
1683 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1684 sizeof((*new_te)->name)) {
1685 err = got_error(GOT_ERR_NO_SPACE);
1686 goto done;
1688 err = write_tree(&id, subdirpath, ignores, repo,
1689 progress_cb, progress_arg);
1690 if (err)
1691 goto done;
1692 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1694 done:
1695 free(id);
1696 free(subdirpath);
1697 if (err) {
1698 free(*new_te);
1699 *new_te = NULL;
1701 return err;
1704 static const struct got_error *
1705 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1706 struct got_pathlist_head *ignores, struct got_repository *repo,
1707 got_repo_import_cb progress_cb, void *progress_arg)
1709 const struct got_error *err = NULL;
1710 DIR *dir;
1711 struct dirent *de;
1712 int nentries;
1713 struct got_tree_entry *new_te = NULL;
1714 struct got_pathlist_head paths;
1715 struct got_pathlist_entry *pe;
1717 *new_tree_id = NULL;
1719 TAILQ_INIT(&paths);
1721 dir = opendir(path_dir);
1722 if (dir == NULL) {
1723 err = got_error_from_errno2("opendir", path_dir);
1724 goto done;
1727 nentries = 0;
1728 while ((de = readdir(dir)) != NULL) {
1729 int ignore = 0;
1730 int type;
1732 if (strcmp(de->d_name, ".") == 0 ||
1733 strcmp(de->d_name, "..") == 0)
1734 continue;
1736 TAILQ_FOREACH(pe, ignores, entry) {
1737 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1738 ignore = 1;
1739 break;
1742 if (ignore)
1743 continue;
1745 err = got_path_dirent_type(&type, path_dir, de);
1746 if (err)
1747 goto done;
1749 if (type == DT_DIR) {
1750 err = import_subdir(&new_te, de, path_dir,
1751 ignores, repo, progress_cb, progress_arg);
1752 if (err) {
1753 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1754 goto done;
1755 err = NULL;
1756 continue;
1758 } else if (type == DT_REG || type == DT_LNK) {
1759 err = import_file(&new_te, de, path_dir, repo);
1760 if (err)
1761 goto done;
1762 } else
1763 continue;
1765 err = insert_tree_entry(new_te, &paths);
1766 if (err)
1767 goto done;
1768 nentries++;
1771 if (TAILQ_EMPTY(&paths)) {
1772 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1773 "cannot create tree without any entries");
1774 goto done;
1777 TAILQ_FOREACH(pe, &paths, entry) {
1778 struct got_tree_entry *te = pe->data;
1779 char *path;
1780 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1781 continue;
1782 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1783 err = got_error_from_errno("asprintf");
1784 goto done;
1786 err = (*progress_cb)(progress_arg, path);
1787 free(path);
1788 if (err)
1789 goto done;
1792 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1793 done:
1794 if (dir)
1795 closedir(dir);
1796 got_pathlist_free(&paths);
1797 return err;
1800 const struct got_error *
1801 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1802 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1803 struct got_repository *repo, got_repo_import_cb progress_cb,
1804 void *progress_arg)
1806 const struct got_error *err;
1807 struct got_object_id *new_tree_id;
1809 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1810 progress_cb, progress_arg);
1811 if (err)
1812 return err;
1814 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1815 author, time(NULL), author, time(NULL), logmsg, repo);
1816 free(new_tree_id);
1817 return err;
1820 const struct got_error *
1821 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1822 struct got_repository *repo)
1824 const struct got_error *err = NULL;
1825 char *path_objects = NULL, *path = NULL;
1826 DIR *dir = NULL;
1827 struct got_object_id id;
1828 int i;
1830 *nobjects = 0;
1831 *ondisk_size = 0;
1833 path_objects = got_repo_get_path_objects(repo);
1834 if (path_objects == NULL)
1835 return got_error_from_errno("got_repo_get_path_objects");
1837 for (i = 0; i <= 0xff; i++) {
1838 struct dirent *dent;
1840 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1841 err = got_error_from_errno("asprintf");
1842 break;
1845 dir = opendir(path);
1846 if (dir == NULL) {
1847 if (errno == ENOENT) {
1848 err = NULL;
1849 continue;
1851 err = got_error_from_errno2("opendir", path);
1852 break;
1855 while ((dent = readdir(dir)) != NULL) {
1856 char *id_str;
1857 int fd;
1858 struct stat sb;
1860 if (strcmp(dent->d_name, ".") == 0 ||
1861 strcmp(dent->d_name, "..") == 0)
1862 continue;
1864 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1865 err = got_error_from_errno("asprintf");
1866 goto done;
1869 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1870 free(id_str);
1871 continue;
1873 free(id_str);
1875 err = got_object_open_loose_fd(&fd, &id, repo);
1876 if (err)
1877 goto done;
1879 if (fstat(fd, &sb) == -1) {
1880 err = got_error_from_errno("fstat");
1881 close(fd);
1882 goto done;
1884 (*nobjects)++;
1885 (*ondisk_size) += sb.st_size;
1887 if (close(fd) == -1) {
1888 err = got_error_from_errno("close");
1889 goto done;
1893 if (closedir(dir) != 0) {
1894 err = got_error_from_errno("closedir");
1895 goto done;
1897 dir = NULL;
1899 free(path);
1900 path = NULL;
1902 done:
1903 if (dir && closedir(dir) != 0 && err == NULL)
1904 err = got_error_from_errno("closedir");
1906 if (err) {
1907 *nobjects = 0;
1908 *ondisk_size = 0;
1910 free(path_objects);
1911 free(path);
1912 return err;
1915 const struct got_error *
1916 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1917 off_t *total_packsize, struct got_repository *repo)
1919 const struct got_error *err = NULL;
1920 DIR *packdir = NULL;
1921 struct dirent *dent;
1922 struct got_packidx *packidx = NULL;
1923 char *path_packidx;
1924 char *path_packfile;
1925 int packdir_fd;
1926 struct stat sb;
1928 *npackfiles = 0;
1929 *nobjects = 0;
1930 *total_packsize = 0;
1932 packdir_fd = openat(got_repo_get_fd(repo),
1933 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1934 if (packdir_fd == -1) {
1935 return got_error_from_errno_fmt("openat: %s/%s",
1936 got_repo_get_path_git_dir(repo),
1937 GOT_OBJECTS_PACK_DIR);
1940 packdir = fdopendir(packdir_fd);
1941 if (packdir == NULL) {
1942 err = got_error_from_errno("fdopendir");
1943 goto done;
1946 while ((dent = readdir(packdir)) != NULL) {
1947 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1948 continue;
1950 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1951 dent->d_name) == -1) {
1952 err = got_error_from_errno("asprintf");
1953 goto done;
1956 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1957 path_packidx, 0);
1958 free(path_packidx);
1959 if (err)
1960 goto done;
1962 if (fstat(packidx->fd, &sb) == -1)
1963 goto done;
1964 *total_packsize += sb.st_size;
1966 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1967 if (err)
1968 goto done;
1970 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1971 0) == -1) {
1972 free(path_packfile);
1973 goto done;
1975 free(path_packfile);
1976 *total_packsize += sb.st_size;
1978 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
1980 (*npackfiles)++;
1982 got_packidx_close(packidx);
1983 packidx = NULL;
1985 done:
1986 if (packidx)
1987 got_packidx_close(packidx);
1988 if (packdir && closedir(packdir) != 0 && err == NULL)
1989 err = got_error_from_errno("closedir");
1990 if (err) {
1991 *npackfiles = 0;
1992 *nobjects = 0;
1993 *total_packsize = 0;
1995 return err;