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)
756 close(repo->gitdir_fd);
758 if (repo->gotconfig)
759 got_gotconfig_free(repo->gotconfig);
760 free(repo->gitconfig_author_name);
761 free(repo->gitconfig_author_email);
762 for (i = 0; i < repo->ngitconfig_remotes; i++)
763 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
764 free(repo->gitconfig_remotes);
765 for (i = 0; i < repo->nextensions; i++)
766 free(repo->extensions[i]);
767 free(repo->extensions);
768 free(repo);
770 return err;
773 void
774 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
776 int i;
778 free(repo->name);
779 repo->name = NULL;
780 free(repo->url);
781 repo->url = NULL;
782 for (i = 0; i < repo->nbranches; i++)
783 free(repo->branches[i]);
784 free(repo->branches);
785 repo->branches = NULL;
786 repo->nbranches = 0;
789 const struct got_error *
790 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
791 const char *input_path)
793 const struct got_error *err = NULL;
794 const char *repo_abspath = NULL;
795 size_t repolen, len;
796 char *canonpath, *path = NULL;
798 *in_repo_path = NULL;
800 canonpath = strdup(input_path);
801 if (canonpath == NULL) {
802 err = got_error_from_errno("strdup");
803 goto done;
805 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
806 if (err)
807 goto done;
809 repo_abspath = got_repo_get_path(repo);
811 if (canonpath[0] == '\0') {
812 path = strdup(canonpath);
813 if (path == NULL) {
814 err = got_error_from_errno("strdup");
815 goto done;
817 } else {
818 path = realpath(canonpath, NULL);
819 if (path == NULL) {
820 if (errno != ENOENT) {
821 err = got_error_from_errno2("realpath",
822 canonpath);
823 goto done;
825 /*
826 * Path is not on disk.
827 * Assume it is already relative to repository root.
828 */
829 path = strdup(canonpath);
830 if (path == NULL) {
831 err = got_error_from_errno("strdup");
832 goto done;
836 repolen = strlen(repo_abspath);
837 len = strlen(path);
840 if (strcmp(path, repo_abspath) == 0) {
841 free(path);
842 path = strdup("");
843 if (path == NULL) {
844 err = got_error_from_errno("strdup");
845 goto done;
847 } else if (len > repolen &&
848 got_path_is_child(path, repo_abspath, repolen)) {
849 /* Matched an on-disk path inside repository. */
850 if (got_repo_is_bare(repo)) {
851 /*
852 * Matched an on-disk path inside repository
853 * database. Treat input as repository-relative.
854 */
855 free(path);
856 path = canonpath;
857 canonpath = NULL;
858 } else {
859 char *child;
860 /* Strip common prefix with repository path. */
861 err = got_path_skip_common_ancestor(&child,
862 repo_abspath, path);
863 if (err)
864 goto done;
865 free(path);
866 path = child;
868 } else {
869 /*
870 * Matched unrelated on-disk path.
871 * Treat input as repository-relative.
872 */
873 free(path);
874 path = canonpath;
875 canonpath = NULL;
879 /* Make in-repository path absolute */
880 if (path[0] != '/') {
881 char *abspath;
882 if (asprintf(&abspath, "/%s", path) == -1) {
883 err = got_error_from_errno("asprintf");
884 goto done;
886 free(path);
887 path = abspath;
890 done:
891 free(canonpath);
892 if (err)
893 free(path);
894 else
895 *in_repo_path = path;
896 return err;
899 static const struct got_error *
900 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
901 const char *path_packidx)
903 const struct got_error *err = NULL;
904 size_t i;
906 for (i = 0; i < repo->pack_cache_size; i++) {
907 if (repo->packidx_cache[i] == NULL)
908 break;
909 if (strcmp(repo->packidx_cache[i]->path_packidx,
910 path_packidx) == 0) {
911 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
914 if (i == repo->pack_cache_size) {
915 err = got_packidx_close(repo->packidx_cache[i - 1]);
916 if (err)
917 return err;
920 /*
921 * Insert the new pack index at the front so it will
922 * be searched first in the future.
923 */
924 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
925 sizeof(repo->packidx_cache) -
926 sizeof(repo->packidx_cache[0]));
927 repo->packidx_cache[0] = packidx;
929 return NULL;
932 static int
933 is_packidx_filename(const char *name, size_t len)
935 if (len != GOT_PACKIDX_NAMELEN)
936 return 0;
938 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
939 return 0;
941 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
942 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
943 return 0;
945 return 1;
948 const struct got_error *
949 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
950 struct got_repository *repo, struct got_object_id *id)
952 const struct got_error *err;
953 DIR *packdir = NULL;
954 struct dirent *dent;
955 char *path_packidx;
956 size_t i;
957 int packdir_fd;
959 /* Search pack index cache. */
960 for (i = 0; i < repo->pack_cache_size; i++) {
961 if (repo->packidx_cache[i] == NULL)
962 break;
963 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
964 if (*idx != -1) {
965 *packidx = repo->packidx_cache[i];
966 /*
967 * Move this cache entry to the front. Repeatedly
968 * searching a wrong pack index can be expensive.
969 */
970 if (i > 0) {
971 struct got_packidx *p;
972 p = repo->packidx_cache[0];
973 repo->packidx_cache[0] = *packidx;
974 repo->packidx_cache[i] = p;
976 return NULL;
979 /* No luck. Search the filesystem. */
981 packdir_fd = openat(got_repo_get_fd(repo),
982 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
983 if (packdir_fd == -1) {
984 if (errno == ENOENT)
985 err = got_error_no_obj(id);
986 else
987 err = got_error_from_errno_fmt("openat: %s/%s",
988 got_repo_get_path_git_dir(repo),
989 GOT_OBJECTS_PACK_DIR);
990 goto done;
993 packdir = fdopendir(packdir_fd);
994 if (packdir == NULL) {
995 err = got_error_from_errno("fdopendir");
996 goto done;
999 while ((dent = readdir(packdir)) != NULL) {
1000 int is_cached = 0;
1002 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1003 continue;
1005 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1006 dent->d_name) == -1) {
1007 err = got_error_from_errno("asprintf");
1008 goto done;
1011 for (i = 0; i < repo->pack_cache_size; i++) {
1012 if (repo->packidx_cache[i] == NULL)
1013 break;
1014 if (strcmp(repo->packidx_cache[i]->path_packidx,
1015 path_packidx) == 0) {
1016 is_cached = 1;
1017 break;
1020 if (is_cached) {
1021 free(path_packidx);
1022 continue; /* already searched */
1025 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1026 path_packidx, 0);
1027 if (err) {
1028 free(path_packidx);
1029 goto done;
1032 err = cache_packidx(repo, *packidx, path_packidx);
1033 free(path_packidx);
1034 if (err)
1035 goto done;
1037 *idx = got_packidx_get_object_idx(*packidx, id);
1038 if (*idx != -1) {
1039 err = NULL; /* found the object */
1040 goto done;
1044 err = got_error_no_obj(id);
1045 done:
1046 if (packdir && closedir(packdir) != 0 && err == NULL)
1047 err = got_error_from_errno("closedir");
1048 return err;
1051 static const struct got_error *
1052 read_packfile_hdr(int fd, struct got_packidx *packidx)
1054 const struct got_error *err = NULL;
1055 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1056 struct got_packfile_hdr hdr;
1057 ssize_t n;
1059 n = read(fd, &hdr, sizeof(hdr));
1060 if (n < 0)
1061 return got_error_from_errno("read");
1062 if (n != sizeof(hdr))
1063 return got_error(GOT_ERR_BAD_PACKFILE);
1065 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1066 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1067 be32toh(hdr.nobjects) != totobj)
1068 err = got_error(GOT_ERR_BAD_PACKFILE);
1070 return err;
1073 static const struct got_error *
1074 open_packfile(int *fd, struct got_repository *repo,
1075 const char *relpath, struct got_packidx *packidx)
1077 const struct got_error *err = NULL;
1079 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1080 if (*fd == -1)
1081 return got_error_from_errno_fmt("openat: %s/%s",
1082 got_repo_get_path_git_dir(repo), relpath);
1084 if (packidx) {
1085 err = read_packfile_hdr(*fd, packidx);
1086 if (err) {
1087 close(*fd);
1088 *fd = -1;
1092 return err;
1095 const struct got_error *
1096 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1097 const char *path_packfile, struct got_packidx *packidx)
1099 const struct got_error *err = NULL;
1100 struct got_pack *pack = NULL;
1101 struct stat sb;
1102 size_t i;
1104 if (packp)
1105 *packp = NULL;
1107 for (i = 0; i < repo->pack_cache_size; i++) {
1108 pack = &repo->packs[i];
1109 if (pack->path_packfile == NULL)
1110 break;
1111 if (strcmp(pack->path_packfile, path_packfile) == 0)
1112 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1115 if (i == repo->pack_cache_size) {
1116 err = got_pack_close(&repo->packs[i - 1]);
1117 if (err)
1118 return err;
1119 memmove(&repo->packs[1], &repo->packs[0],
1120 sizeof(repo->packs) - sizeof(repo->packs[0]));
1121 i = 0;
1124 pack = &repo->packs[i];
1126 pack->path_packfile = strdup(path_packfile);
1127 if (pack->path_packfile == NULL) {
1128 err = got_error_from_errno("strdup");
1129 goto done;
1132 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1133 if (err)
1134 goto done;
1136 if (fstat(pack->fd, &sb) != 0) {
1137 err = got_error_from_errno("fstat");
1138 goto done;
1140 pack->filesize = sb.st_size;
1142 pack->privsep_child = NULL;
1144 #ifndef GOT_PACK_NO_MMAP
1145 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1146 pack->fd, 0);
1147 if (pack->map == MAP_FAILED) {
1148 if (errno != ENOMEM) {
1149 err = got_error_from_errno("mmap");
1150 goto done;
1152 pack->map = NULL; /* fall back to read(2) */
1154 #endif
1155 done:
1156 if (err) {
1157 if (pack) {
1158 free(pack->path_packfile);
1159 memset(pack, 0, sizeof(*pack));
1161 } else if (packp)
1162 *packp = pack;
1163 return err;
1166 struct got_pack *
1167 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1169 struct got_pack *pack = NULL;
1170 size_t i;
1172 for (i = 0; i < repo->pack_cache_size; i++) {
1173 pack = &repo->packs[i];
1174 if (pack->path_packfile == NULL)
1175 break;
1176 if (strcmp(pack->path_packfile, path_packfile) == 0)
1177 return pack;
1180 return NULL;
1183 const struct got_error *
1184 got_repo_init(const char *repo_path)
1186 const struct got_error *err = NULL;
1187 const char *dirnames[] = {
1188 GOT_OBJECTS_DIR,
1189 GOT_OBJECTS_PACK_DIR,
1190 GOT_REFS_DIR,
1192 const char *description_str = "Unnamed repository; "
1193 "edit this file 'description' to name the repository.";
1194 const char *headref_str = "ref: refs/heads/main";
1195 const char *gitconfig_str = "[core]\n"
1196 "\trepositoryformatversion = 0\n"
1197 "\tfilemode = true\n"
1198 "\tbare = true\n";
1199 char *path;
1200 size_t i;
1202 if (!got_path_dir_is_empty(repo_path))
1203 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1205 for (i = 0; i < nitems(dirnames); i++) {
1206 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1207 return got_error_from_errno("asprintf");
1209 err = got_path_mkdir(path);
1210 free(path);
1211 if (err)
1212 return err;
1215 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1216 return got_error_from_errno("asprintf");
1217 err = got_path_create_file(path, description_str);
1218 free(path);
1219 if (err)
1220 return err;
1222 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1223 return got_error_from_errno("asprintf");
1224 err = got_path_create_file(path, headref_str);
1225 free(path);
1226 if (err)
1227 return err;
1229 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1230 return got_error_from_errno("asprintf");
1231 err = got_path_create_file(path, gitconfig_str);
1232 free(path);
1233 if (err)
1234 return err;
1236 return NULL;
1239 static const struct got_error *
1240 match_packed_object(struct got_object_id **unique_id,
1241 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1243 const struct got_error *err = NULL;
1244 DIR *packdir = NULL;
1245 struct dirent *dent;
1246 char *path_packidx;
1247 struct got_object_id_queue matched_ids;
1248 int packdir_fd;
1250 SIMPLEQ_INIT(&matched_ids);
1252 packdir_fd = openat(got_repo_get_fd(repo),
1253 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1254 if (packdir_fd == -1) {
1255 if (errno != ENOENT)
1256 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1257 goto done;
1260 packdir = fdopendir(packdir_fd);
1261 if (packdir == NULL) {
1262 err = got_error_from_errno("fdopendir");
1263 goto done;
1266 while ((dent = readdir(packdir)) != NULL) {
1267 struct got_packidx *packidx;
1268 struct got_object_qid *qid;
1270 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1271 continue;
1273 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1274 dent->d_name) == -1) {
1275 err = got_error_from_errno("strdup");
1276 break;
1279 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1280 path_packidx, 0);
1281 free(path_packidx);
1282 if (err)
1283 break;
1285 err = got_packidx_match_id_str_prefix(&matched_ids,
1286 packidx, id_str_prefix);
1287 if (err) {
1288 got_packidx_close(packidx);
1289 break;
1291 err = got_packidx_close(packidx);
1292 if (err)
1293 break;
1295 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1296 if (obj_type != GOT_OBJ_TYPE_ANY) {
1297 int matched_type;
1298 err = got_object_get_type(&matched_type, repo,
1299 qid->id);
1300 if (err)
1301 goto done;
1302 if (matched_type != obj_type)
1303 continue;
1305 if (*unique_id == NULL) {
1306 *unique_id = got_object_id_dup(qid->id);
1307 if (*unique_id == NULL) {
1308 err = got_error_from_errno("malloc");
1309 goto done;
1311 } else {
1312 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1313 continue; /* packed multiple times */
1314 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1315 goto done;
1319 done:
1320 got_object_id_queue_free(&matched_ids);
1321 if (packdir && closedir(packdir) != 0 && err == NULL)
1322 err = got_error_from_errno("closedir");
1323 if (err) {
1324 free(*unique_id);
1325 *unique_id = NULL;
1327 return err;
1330 static const struct got_error *
1331 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1332 const char *object_dir, const char *id_str_prefix, int obj_type,
1333 struct got_repository *repo)
1335 const struct got_error *err = NULL;
1336 char *path;
1337 DIR *dir = NULL;
1338 struct dirent *dent;
1339 struct got_object_id id;
1341 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1342 err = got_error_from_errno("asprintf");
1343 goto done;
1346 dir = opendir(path);
1347 if (dir == NULL) {
1348 if (errno == ENOENT) {
1349 err = NULL;
1350 goto done;
1352 err = got_error_from_errno2("opendir", path);
1353 goto done;
1355 while ((dent = readdir(dir)) != NULL) {
1356 char *id_str;
1357 int cmp;
1359 if (strcmp(dent->d_name, ".") == 0 ||
1360 strcmp(dent->d_name, "..") == 0)
1361 continue;
1363 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1364 err = got_error_from_errno("asprintf");
1365 goto done;
1368 if (!got_parse_sha1_digest(id.sha1, id_str))
1369 continue;
1372 * Directory entries do not necessarily appear in
1373 * sorted order, so we must iterate over all of them.
1375 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1376 if (cmp != 0) {
1377 free(id_str);
1378 continue;
1381 if (*unique_id == NULL) {
1382 if (obj_type != GOT_OBJ_TYPE_ANY) {
1383 int matched_type;
1384 err = got_object_get_type(&matched_type, repo,
1385 &id);
1386 if (err)
1387 goto done;
1388 if (matched_type != obj_type)
1389 continue;
1391 *unique_id = got_object_id_dup(&id);
1392 if (*unique_id == NULL) {
1393 err = got_error_from_errno("got_object_id_dup");
1394 free(id_str);
1395 goto done;
1397 } else {
1398 if (got_object_id_cmp(*unique_id, &id) == 0)
1399 continue; /* both packed and loose */
1400 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1401 free(id_str);
1402 goto done;
1405 done:
1406 if (dir && closedir(dir) != 0 && err == NULL)
1407 err = got_error_from_errno("closedir");
1408 if (err) {
1409 free(*unique_id);
1410 *unique_id = NULL;
1412 free(path);
1413 return err;
1416 const struct got_error *
1417 got_repo_match_object_id_prefix(struct got_object_id **id,
1418 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1420 const struct got_error *err = NULL;
1421 char *path_objects = got_repo_get_path_objects(repo);
1422 char *object_dir = NULL;
1423 size_t len;
1424 int i;
1426 *id = NULL;
1428 for (i = 0; i < strlen(id_str_prefix); i++) {
1429 if (isxdigit((unsigned char)id_str_prefix[i]))
1430 continue;
1431 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1434 len = strlen(id_str_prefix);
1435 if (len >= 2) {
1436 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1437 if (err)
1438 goto done;
1439 object_dir = strndup(id_str_prefix, 2);
1440 if (object_dir == NULL) {
1441 err = got_error_from_errno("strdup");
1442 goto done;
1444 err = match_loose_object(id, path_objects, object_dir,
1445 id_str_prefix, obj_type, repo);
1446 } else if (len == 1) {
1447 int i;
1448 for (i = 0; i < 0xf; i++) {
1449 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1450 == -1) {
1451 err = got_error_from_errno("asprintf");
1452 goto done;
1454 err = match_packed_object(id, repo, object_dir,
1455 obj_type);
1456 if (err)
1457 goto done;
1458 err = match_loose_object(id, path_objects, object_dir,
1459 id_str_prefix, obj_type, repo);
1460 if (err)
1461 goto done;
1463 } else {
1464 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1465 goto done;
1467 done:
1468 free(object_dir);
1469 if (err) {
1470 free(*id);
1471 *id = NULL;
1472 } else if (*id == NULL)
1473 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1475 return err;
1478 const struct got_error *
1479 got_repo_match_object_id(struct got_object_id **id, char **label,
1480 const char *id_str, int obj_type, struct got_reflist_head *refs,
1481 struct got_repository *repo)
1483 const struct got_error *err;
1484 struct got_tag_object *tag;
1485 struct got_reference *ref = NULL;
1487 *id = NULL;
1488 if (label)
1489 *label = NULL;
1491 if (refs) {
1492 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1493 refs, repo);
1494 if (err == NULL) {
1495 *id = got_object_id_dup(
1496 got_object_tag_get_object_id(tag));
1497 if (*id == NULL)
1498 err = got_error_from_errno("got_object_id_dup");
1499 else if (label && asprintf(label, "refs/tags/%s",
1500 got_object_tag_get_name(tag)) == -1) {
1501 err = got_error_from_errno("asprintf");
1502 free(*id);
1503 *id = NULL;
1505 got_object_tag_close(tag);
1506 return err;
1507 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1508 err->code != GOT_ERR_NO_OBJ)
1509 return err;
1512 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1513 if (err) {
1514 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1515 return err;
1516 err = got_ref_open(&ref, repo, id_str, 0);
1517 if (err != NULL)
1518 goto done;
1519 if (label) {
1520 *label = strdup(got_ref_get_name(ref));
1521 if (*label == NULL) {
1522 err = got_error_from_errno("strdup");
1523 goto done;
1526 err = got_ref_resolve(id, repo, ref);
1527 } else if (label) {
1528 err = got_object_id_str(label, *id);
1529 if (*label == NULL) {
1530 err = got_error_from_errno("strdup");
1531 goto done;
1534 done:
1535 if (ref)
1536 got_ref_close(ref);
1537 return err;
1540 const struct got_error *
1541 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1542 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1544 const struct got_error *err = NULL;
1545 struct got_reflist_entry *re;
1546 struct got_object_id *tag_id;
1547 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1549 *tag = NULL;
1551 TAILQ_FOREACH(re, refs, entry) {
1552 const char *refname;
1553 refname = got_ref_get_name(re->ref);
1554 if (got_ref_is_symbolic(re->ref))
1555 continue;
1556 if (strncmp(refname, "refs/tags/", 10) != 0)
1557 continue;
1558 if (!name_is_absolute)
1559 refname += strlen("refs/tags/");
1560 if (strcmp(refname, name) != 0)
1561 continue;
1562 err = got_ref_resolve(&tag_id, repo, re->ref);
1563 if (err)
1564 break;
1565 err = got_object_open_as_tag(tag, repo, tag_id);
1566 free(tag_id);
1567 if (err)
1568 break;
1569 if (obj_type == GOT_OBJ_TYPE_ANY ||
1570 got_object_tag_get_object_type(*tag) == obj_type)
1571 break;
1572 got_object_tag_close(*tag);
1573 *tag = NULL;
1576 if (err == NULL && *tag == NULL)
1577 err = got_error_path(name, GOT_ERR_NO_OBJ);
1578 return err;
1581 static const struct got_error *
1582 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1583 const char *name, mode_t mode, struct got_object_id *blob_id)
1585 const struct got_error *err = NULL;
1587 *new_te = NULL;
1589 *new_te = calloc(1, sizeof(**new_te));
1590 if (*new_te == NULL)
1591 return got_error_from_errno("calloc");
1593 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1594 sizeof((*new_te)->name)) {
1595 err = got_error(GOT_ERR_NO_SPACE);
1596 goto done;
1599 if (S_ISLNK(mode)) {
1600 (*new_te)->mode = S_IFLNK;
1601 } else {
1602 (*new_te)->mode = S_IFREG;
1603 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1605 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1606 done:
1607 if (err && *new_te) {
1608 free(*new_te);
1609 *new_te = NULL;
1611 return err;
1614 static const struct got_error *
1615 import_file(struct got_tree_entry **new_te, struct dirent *de,
1616 const char *path, struct got_repository *repo)
1618 const struct got_error *err;
1619 struct got_object_id *blob_id = NULL;
1620 char *filepath;
1621 struct stat sb;
1623 if (asprintf(&filepath, "%s%s%s", path,
1624 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1625 return got_error_from_errno("asprintf");
1627 if (lstat(filepath, &sb) != 0) {
1628 err = got_error_from_errno2("lstat", path);
1629 goto done;
1632 err = got_object_blob_create(&blob_id, filepath, repo);
1633 if (err)
1634 goto done;
1636 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1637 blob_id);
1638 done:
1639 free(filepath);
1640 if (err)
1641 free(blob_id);
1642 return err;
1645 static const struct got_error *
1646 insert_tree_entry(struct got_tree_entry *new_te,
1647 struct got_pathlist_head *paths)
1649 const struct got_error *err = NULL;
1650 struct got_pathlist_entry *new_pe;
1652 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1653 if (err)
1654 return err;
1655 if (new_pe == NULL)
1656 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1657 return NULL;
1660 static const struct got_error *write_tree(struct got_object_id **,
1661 const char *, struct got_pathlist_head *, struct got_repository *,
1662 got_repo_import_cb progress_cb, void *progress_arg);
1664 static const struct got_error *
1665 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1666 const char *path, struct got_pathlist_head *ignores,
1667 struct got_repository *repo,
1668 got_repo_import_cb progress_cb, void *progress_arg)
1670 const struct got_error *err;
1671 struct got_object_id *id = NULL;
1672 char *subdirpath;
1674 if (asprintf(&subdirpath, "%s%s%s", path,
1675 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1676 return got_error_from_errno("asprintf");
1678 (*new_te) = calloc(1, sizeof(**new_te));
1679 if (*new_te == NULL)
1680 return got_error_from_errno("calloc");
1681 (*new_te)->mode = S_IFDIR;
1682 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1683 sizeof((*new_te)->name)) {
1684 err = got_error(GOT_ERR_NO_SPACE);
1685 goto done;
1687 err = write_tree(&id, subdirpath, ignores, repo,
1688 progress_cb, progress_arg);
1689 if (err)
1690 goto done;
1691 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1693 done:
1694 free(id);
1695 free(subdirpath);
1696 if (err) {
1697 free(*new_te);
1698 *new_te = NULL;
1700 return err;
1703 static const struct got_error *
1704 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1705 struct got_pathlist_head *ignores, struct got_repository *repo,
1706 got_repo_import_cb progress_cb, void *progress_arg)
1708 const struct got_error *err = NULL;
1709 DIR *dir;
1710 struct dirent *de;
1711 int nentries;
1712 struct got_tree_entry *new_te = NULL;
1713 struct got_pathlist_head paths;
1714 struct got_pathlist_entry *pe;
1716 *new_tree_id = NULL;
1718 TAILQ_INIT(&paths);
1720 dir = opendir(path_dir);
1721 if (dir == NULL) {
1722 err = got_error_from_errno2("opendir", path_dir);
1723 goto done;
1726 nentries = 0;
1727 while ((de = readdir(dir)) != NULL) {
1728 int ignore = 0;
1729 int type;
1731 if (strcmp(de->d_name, ".") == 0 ||
1732 strcmp(de->d_name, "..") == 0)
1733 continue;
1735 TAILQ_FOREACH(pe, ignores, entry) {
1736 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1737 ignore = 1;
1738 break;
1741 if (ignore)
1742 continue;
1744 err = got_path_dirent_type(&type, path_dir, de);
1745 if (err)
1746 goto done;
1748 if (type == DT_DIR) {
1749 err = import_subdir(&new_te, de, path_dir,
1750 ignores, repo, progress_cb, progress_arg);
1751 if (err) {
1752 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1753 goto done;
1754 err = NULL;
1755 continue;
1757 } else if (type == DT_REG || type == DT_LNK) {
1758 err = import_file(&new_te, de, path_dir, repo);
1759 if (err)
1760 goto done;
1761 } else
1762 continue;
1764 err = insert_tree_entry(new_te, &paths);
1765 if (err)
1766 goto done;
1767 nentries++;
1770 if (TAILQ_EMPTY(&paths)) {
1771 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1772 "cannot create tree without any entries");
1773 goto done;
1776 TAILQ_FOREACH(pe, &paths, entry) {
1777 struct got_tree_entry *te = pe->data;
1778 char *path;
1779 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1780 continue;
1781 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1782 err = got_error_from_errno("asprintf");
1783 goto done;
1785 err = (*progress_cb)(progress_arg, path);
1786 free(path);
1787 if (err)
1788 goto done;
1791 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1792 done:
1793 if (dir)
1794 closedir(dir);
1795 got_pathlist_free(&paths);
1796 return err;
1799 const struct got_error *
1800 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1801 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1802 struct got_repository *repo, got_repo_import_cb progress_cb,
1803 void *progress_arg)
1805 const struct got_error *err;
1806 struct got_object_id *new_tree_id;
1808 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1809 progress_cb, progress_arg);
1810 if (err)
1811 return err;
1813 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1814 author, time(NULL), author, time(NULL), logmsg, repo);
1815 free(new_tree_id);
1816 return err;
1819 const struct got_error *
1820 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1821 struct got_repository *repo)
1823 const struct got_error *err = NULL;
1824 char *path_objects = NULL, *path = NULL;
1825 DIR *dir = NULL;
1826 struct got_object_id id;
1827 int i;
1829 *nobjects = 0;
1830 *ondisk_size = 0;
1832 path_objects = got_repo_get_path_objects(repo);
1833 if (path_objects == NULL)
1834 return got_error_from_errno("got_repo_get_path_objects");
1836 for (i = 0; i <= 0xff; i++) {
1837 struct dirent *dent;
1839 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1840 err = got_error_from_errno("asprintf");
1841 break;
1844 dir = opendir(path);
1845 if (dir == NULL) {
1846 if (errno == ENOENT) {
1847 err = NULL;
1848 continue;
1850 err = got_error_from_errno2("opendir", path);
1851 break;
1854 while ((dent = readdir(dir)) != NULL) {
1855 char *id_str;
1856 int fd;
1857 struct stat sb;
1859 if (strcmp(dent->d_name, ".") == 0 ||
1860 strcmp(dent->d_name, "..") == 0)
1861 continue;
1863 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1864 err = got_error_from_errno("asprintf");
1865 goto done;
1868 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1869 free(id_str);
1870 continue;
1872 free(id_str);
1874 err = got_object_open_loose_fd(&fd, &id, repo);
1875 if (err)
1876 goto done;
1878 if (fstat(fd, &sb) == -1) {
1879 err = got_error_from_errno("fstat");
1880 close(fd);
1881 goto done;
1883 (*nobjects)++;
1884 (*ondisk_size) += sb.st_size;
1886 if (close(fd) == -1) {
1887 err = got_error_from_errno("close");
1888 goto done;
1892 if (closedir(dir) != 0) {
1893 err = got_error_from_errno("closedir");
1894 goto done;
1896 dir = NULL;
1898 free(path);
1899 path = NULL;
1901 done:
1902 if (dir && closedir(dir) != 0 && err == NULL)
1903 err = got_error_from_errno("closedir");
1905 if (err) {
1906 *nobjects = 0;
1907 *ondisk_size = 0;
1909 free(path_objects);
1910 free(path);
1911 return err;
1914 const struct got_error *
1915 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1916 off_t *total_packsize, struct got_repository *repo)
1918 const struct got_error *err = NULL;
1919 DIR *packdir = NULL;
1920 struct dirent *dent;
1921 struct got_packidx *packidx = NULL;
1922 char *path_packidx;
1923 char *path_packfile;
1924 int packdir_fd;
1925 struct stat sb;
1927 *npackfiles = 0;
1928 *nobjects = 0;
1929 *total_packsize = 0;
1931 packdir_fd = openat(got_repo_get_fd(repo),
1932 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1933 if (packdir_fd == -1) {
1934 return got_error_from_errno_fmt("openat: %s/%s",
1935 got_repo_get_path_git_dir(repo),
1936 GOT_OBJECTS_PACK_DIR);
1939 packdir = fdopendir(packdir_fd);
1940 if (packdir == NULL) {
1941 err = got_error_from_errno("fdopendir");
1942 goto done;
1945 while ((dent = readdir(packdir)) != NULL) {
1946 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1947 continue;
1949 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1950 dent->d_name) == -1) {
1951 err = got_error_from_errno("asprintf");
1952 goto done;
1955 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1956 path_packidx, 0);
1957 free(path_packidx);
1958 if (err)
1959 goto done;
1961 if (fstat(packidx->fd, &sb) == -1)
1962 goto done;
1963 *total_packsize += sb.st_size;
1965 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1966 if (err)
1967 goto done;
1969 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1970 0) == -1) {
1971 free(path_packfile);
1972 goto done;
1974 free(path_packfile);
1975 *total_packsize += sb.st_size;
1977 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
1979 (*npackfiles)++;
1981 got_packidx_close(packidx);
1982 packidx = NULL;
1984 done:
1985 if (packidx)
1986 got_packidx_close(packidx);
1987 if (packdir && closedir(packdir) != 0 && err == NULL)
1988 err = got_error_from_errno("closedir");
1989 if (err) {
1990 *npackfiles = 0;
1991 *nobjects = 0;
1992 *total_packsize = 0;
1994 return err;