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 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->gotconfig)
756 got_gotconfig_free(repo->gotconfig);
757 free(repo->gitconfig_author_name);
758 free(repo->gitconfig_author_email);
759 for (i = 0; i < repo->ngitconfig_remotes; i++)
760 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
761 free(repo->gitconfig_remotes);
762 for (i = 0; i < repo->nextensions; i++)
763 free(repo->extensions[i]);
764 free(repo->extensions);
765 free(repo);
767 return err;
770 void
771 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
773 int i;
775 free(repo->name);
776 repo->name = NULL;
777 free(repo->url);
778 repo->url = NULL;
779 for (i = 0; i < repo->nbranches; i++)
780 free(repo->branches[i]);
781 free(repo->branches);
782 repo->branches = NULL;
783 repo->nbranches = 0;
786 const struct got_error *
787 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
788 const char *input_path)
790 const struct got_error *err = NULL;
791 const char *repo_abspath = NULL;
792 size_t repolen, len;
793 char *canonpath, *path = NULL;
795 *in_repo_path = NULL;
797 canonpath = strdup(input_path);
798 if (canonpath == NULL) {
799 err = got_error_from_errno("strdup");
800 goto done;
802 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
803 if (err)
804 goto done;
806 repo_abspath = got_repo_get_path(repo);
808 if (canonpath[0] == '\0') {
809 path = strdup(canonpath);
810 if (path == NULL) {
811 err = got_error_from_errno("strdup");
812 goto done;
814 } else {
815 path = realpath(canonpath, NULL);
816 if (path == NULL) {
817 if (errno != ENOENT) {
818 err = got_error_from_errno2("realpath",
819 canonpath);
820 goto done;
822 /*
823 * Path is not on disk.
824 * Assume it is already relative to repository root.
825 */
826 path = strdup(canonpath);
827 if (path == NULL) {
828 err = got_error_from_errno("strdup");
829 goto done;
833 repolen = strlen(repo_abspath);
834 len = strlen(path);
837 if (strcmp(path, repo_abspath) == 0) {
838 free(path);
839 path = strdup("");
840 if (path == NULL) {
841 err = got_error_from_errno("strdup");
842 goto done;
844 } else if (len > repolen &&
845 got_path_is_child(path, repo_abspath, repolen)) {
846 /* Matched an on-disk path inside repository. */
847 if (got_repo_is_bare(repo)) {
848 /*
849 * Matched an on-disk path inside repository
850 * database. Treat input as repository-relative.
851 */
852 free(path);
853 path = canonpath;
854 canonpath = NULL;
855 } else {
856 char *child;
857 /* Strip common prefix with repository path. */
858 err = got_path_skip_common_ancestor(&child,
859 repo_abspath, path);
860 if (err)
861 goto done;
862 free(path);
863 path = child;
865 } else {
866 /*
867 * Matched unrelated on-disk path.
868 * Treat input as repository-relative.
869 */
870 free(path);
871 path = canonpath;
872 canonpath = NULL;
876 /* Make in-repository path absolute */
877 if (path[0] != '/') {
878 char *abspath;
879 if (asprintf(&abspath, "/%s", path) == -1) {
880 err = got_error_from_errno("asprintf");
881 goto done;
883 free(path);
884 path = abspath;
887 done:
888 free(canonpath);
889 if (err)
890 free(path);
891 else
892 *in_repo_path = path;
893 return err;
896 static const struct got_error *
897 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
898 const char *path_packidx)
900 const struct got_error *err = NULL;
901 size_t i;
903 for (i = 0; i < repo->pack_cache_size; i++) {
904 if (repo->packidx_cache[i] == NULL)
905 break;
906 if (strcmp(repo->packidx_cache[i]->path_packidx,
907 path_packidx) == 0) {
908 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
911 if (i == repo->pack_cache_size) {
912 err = got_packidx_close(repo->packidx_cache[i - 1]);
913 if (err)
914 return err;
917 /*
918 * Insert the new pack index at the front so it will
919 * be searched first in the future.
920 */
921 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
922 sizeof(repo->packidx_cache) -
923 sizeof(repo->packidx_cache[0]));
924 repo->packidx_cache[0] = packidx;
926 return NULL;
929 static int
930 is_packidx_filename(const char *name, size_t len)
932 if (len != GOT_PACKIDX_NAMELEN)
933 return 0;
935 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
936 return 0;
938 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
939 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
940 return 0;
942 return 1;
945 const struct got_error *
946 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
947 struct got_repository *repo, struct got_object_id *id)
949 const struct got_error *err;
950 DIR *packdir = NULL;
951 struct dirent *dent;
952 char *path_packidx;
953 size_t i;
954 int packdir_fd;
956 /* Search pack index cache. */
957 for (i = 0; i < repo->pack_cache_size; i++) {
958 if (repo->packidx_cache[i] == NULL)
959 break;
960 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
961 if (*idx != -1) {
962 *packidx = repo->packidx_cache[i];
963 /*
964 * Move this cache entry to the front. Repeatedly
965 * searching a wrong pack index can be expensive.
966 */
967 if (i > 0) {
968 struct got_packidx *p;
969 p = repo->packidx_cache[0];
970 repo->packidx_cache[0] = *packidx;
971 repo->packidx_cache[i] = p;
973 return NULL;
976 /* No luck. Search the filesystem. */
978 packdir_fd = openat(got_repo_get_fd(repo),
979 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
980 if (packdir_fd == -1) {
981 if (errno == ENOENT)
982 err = got_error_no_obj(id);
983 else
984 err = got_error_from_errno_fmt("openat: %s/%s",
985 got_repo_get_path_git_dir(repo),
986 GOT_OBJECTS_PACK_DIR);
987 goto done;
990 packdir = fdopendir(packdir_fd);
991 if (packdir == NULL) {
992 err = got_error_from_errno("fdopendir");
993 goto done;
996 while ((dent = readdir(packdir)) != NULL) {
997 int is_cached = 0;
999 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1000 continue;
1002 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1003 dent->d_name) == -1) {
1004 err = got_error_from_errno("asprintf");
1005 goto done;
1008 for (i = 0; i < repo->pack_cache_size; i++) {
1009 if (repo->packidx_cache[i] == NULL)
1010 break;
1011 if (strcmp(repo->packidx_cache[i]->path_packidx,
1012 path_packidx) == 0) {
1013 is_cached = 1;
1014 break;
1017 if (is_cached) {
1018 free(path_packidx);
1019 continue; /* already searched */
1022 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1023 path_packidx, 0);
1024 if (err) {
1025 free(path_packidx);
1026 goto done;
1029 err = cache_packidx(repo, *packidx, path_packidx);
1030 free(path_packidx);
1031 if (err)
1032 goto done;
1034 *idx = got_packidx_get_object_idx(*packidx, id);
1035 if (*idx != -1) {
1036 err = NULL; /* found the object */
1037 goto done;
1041 err = got_error_no_obj(id);
1042 done:
1043 if (packdir && closedir(packdir) != 0 && err == NULL)
1044 err = got_error_from_errno("closedir");
1045 return err;
1048 static const struct got_error *
1049 read_packfile_hdr(int fd, struct got_packidx *packidx)
1051 const struct got_error *err = NULL;
1052 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1053 struct got_packfile_hdr hdr;
1054 ssize_t n;
1056 n = read(fd, &hdr, sizeof(hdr));
1057 if (n < 0)
1058 return got_error_from_errno("read");
1059 if (n != sizeof(hdr))
1060 return got_error(GOT_ERR_BAD_PACKFILE);
1062 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1063 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1064 be32toh(hdr.nobjects) != totobj)
1065 err = got_error(GOT_ERR_BAD_PACKFILE);
1067 return err;
1070 static const struct got_error *
1071 open_packfile(int *fd, struct got_repository *repo,
1072 const char *relpath, struct got_packidx *packidx)
1074 const struct got_error *err = NULL;
1076 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1077 if (*fd == -1)
1078 return got_error_from_errno_fmt("openat: %s/%s",
1079 got_repo_get_path_git_dir(repo), relpath);
1081 if (packidx) {
1082 err = read_packfile_hdr(*fd, packidx);
1083 if (err) {
1084 close(*fd);
1085 *fd = -1;
1089 return err;
1092 const struct got_error *
1093 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1094 const char *path_packfile, struct got_packidx *packidx)
1096 const struct got_error *err = NULL;
1097 struct got_pack *pack = NULL;
1098 struct stat sb;
1099 size_t i;
1101 if (packp)
1102 *packp = NULL;
1104 for (i = 0; i < repo->pack_cache_size; i++) {
1105 pack = &repo->packs[i];
1106 if (pack->path_packfile == NULL)
1107 break;
1108 if (strcmp(pack->path_packfile, path_packfile) == 0)
1109 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1112 if (i == repo->pack_cache_size) {
1113 err = got_pack_close(&repo->packs[i - 1]);
1114 if (err)
1115 return err;
1116 memmove(&repo->packs[1], &repo->packs[0],
1117 sizeof(repo->packs) - sizeof(repo->packs[0]));
1118 i = 0;
1121 pack = &repo->packs[i];
1123 pack->path_packfile = strdup(path_packfile);
1124 if (pack->path_packfile == NULL) {
1125 err = got_error_from_errno("strdup");
1126 goto done;
1129 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1130 if (err)
1131 goto done;
1133 if (fstat(pack->fd, &sb) != 0) {
1134 err = got_error_from_errno("fstat");
1135 goto done;
1137 pack->filesize = sb.st_size;
1139 pack->privsep_child = NULL;
1141 #ifndef GOT_PACK_NO_MMAP
1142 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1143 pack->fd, 0);
1144 if (pack->map == MAP_FAILED) {
1145 if (errno != ENOMEM) {
1146 err = got_error_from_errno("mmap");
1147 goto done;
1149 pack->map = NULL; /* fall back to read(2) */
1151 #endif
1152 done:
1153 if (err) {
1154 if (pack) {
1155 free(pack->path_packfile);
1156 memset(pack, 0, sizeof(*pack));
1158 } else if (packp)
1159 *packp = pack;
1160 return err;
1163 struct got_pack *
1164 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1166 struct got_pack *pack = NULL;
1167 size_t i;
1169 for (i = 0; i < repo->pack_cache_size; i++) {
1170 pack = &repo->packs[i];
1171 if (pack->path_packfile == NULL)
1172 break;
1173 if (strcmp(pack->path_packfile, path_packfile) == 0)
1174 return pack;
1177 return NULL;
1180 const struct got_error *
1181 got_repo_init(const char *repo_path)
1183 const struct got_error *err = NULL;
1184 const char *dirnames[] = {
1185 GOT_OBJECTS_DIR,
1186 GOT_OBJECTS_PACK_DIR,
1187 GOT_REFS_DIR,
1189 const char *description_str = "Unnamed repository; "
1190 "edit this file 'description' to name the repository.";
1191 const char *headref_str = "ref: refs/heads/main";
1192 const char *gitconfig_str = "[core]\n"
1193 "\trepositoryformatversion = 0\n"
1194 "\tfilemode = true\n"
1195 "\tbare = true\n";
1196 char *path;
1197 size_t i;
1199 if (!got_path_dir_is_empty(repo_path))
1200 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1202 for (i = 0; i < nitems(dirnames); i++) {
1203 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1204 return got_error_from_errno("asprintf");
1206 err = got_path_mkdir(path);
1207 free(path);
1208 if (err)
1209 return err;
1212 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1213 return got_error_from_errno("asprintf");
1214 err = got_path_create_file(path, description_str);
1215 free(path);
1216 if (err)
1217 return err;
1219 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1220 return got_error_from_errno("asprintf");
1221 err = got_path_create_file(path, headref_str);
1222 free(path);
1223 if (err)
1224 return err;
1226 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1227 return got_error_from_errno("asprintf");
1228 err = got_path_create_file(path, gitconfig_str);
1229 free(path);
1230 if (err)
1231 return err;
1233 return NULL;
1236 static const struct got_error *
1237 match_packed_object(struct got_object_id **unique_id,
1238 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1240 const struct got_error *err = NULL;
1241 DIR *packdir = NULL;
1242 struct dirent *dent;
1243 char *path_packidx;
1244 struct got_object_id_queue matched_ids;
1245 int packdir_fd;
1247 SIMPLEQ_INIT(&matched_ids);
1249 packdir_fd = openat(got_repo_get_fd(repo),
1250 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1251 if (packdir_fd == -1) {
1252 if (errno != ENOENT)
1253 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1254 goto done;
1257 packdir = fdopendir(packdir_fd);
1258 if (packdir == NULL) {
1259 err = got_error_from_errno("fdopendir");
1260 goto done;
1263 while ((dent = readdir(packdir)) != NULL) {
1264 struct got_packidx *packidx;
1265 struct got_object_qid *qid;
1267 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1268 continue;
1270 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1271 dent->d_name) == -1) {
1272 err = got_error_from_errno("strdup");
1273 break;
1276 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1277 path_packidx, 0);
1278 free(path_packidx);
1279 if (err)
1280 break;
1282 err = got_packidx_match_id_str_prefix(&matched_ids,
1283 packidx, id_str_prefix);
1284 if (err) {
1285 got_packidx_close(packidx);
1286 break;
1288 err = got_packidx_close(packidx);
1289 if (err)
1290 break;
1292 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1293 if (obj_type != GOT_OBJ_TYPE_ANY) {
1294 int matched_type;
1295 err = got_object_get_type(&matched_type, repo,
1296 qid->id);
1297 if (err)
1298 goto done;
1299 if (matched_type != obj_type)
1300 continue;
1302 if (*unique_id == NULL) {
1303 *unique_id = got_object_id_dup(qid->id);
1304 if (*unique_id == NULL) {
1305 err = got_error_from_errno("malloc");
1306 goto done;
1308 } else {
1309 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1310 continue; /* packed multiple times */
1311 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1312 goto done;
1316 done:
1317 got_object_id_queue_free(&matched_ids);
1318 if (packdir && closedir(packdir) != 0 && err == NULL)
1319 err = got_error_from_errno("closedir");
1320 if (err) {
1321 free(*unique_id);
1322 *unique_id = NULL;
1324 return err;
1327 static const struct got_error *
1328 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1329 const char *object_dir, const char *id_str_prefix, int obj_type,
1330 struct got_repository *repo)
1332 const struct got_error *err = NULL;
1333 char *path;
1334 DIR *dir = NULL;
1335 struct dirent *dent;
1336 struct got_object_id id;
1338 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1339 err = got_error_from_errno("asprintf");
1340 goto done;
1343 dir = opendir(path);
1344 if (dir == NULL) {
1345 if (errno == ENOENT) {
1346 err = NULL;
1347 goto done;
1349 err = got_error_from_errno2("opendir", path);
1350 goto done;
1352 while ((dent = readdir(dir)) != NULL) {
1353 char *id_str;
1354 int cmp;
1356 if (strcmp(dent->d_name, ".") == 0 ||
1357 strcmp(dent->d_name, "..") == 0)
1358 continue;
1360 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1361 err = got_error_from_errno("asprintf");
1362 goto done;
1365 if (!got_parse_sha1_digest(id.sha1, id_str))
1366 continue;
1369 * Directory entries do not necessarily appear in
1370 * sorted order, so we must iterate over all of them.
1372 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1373 if (cmp != 0) {
1374 free(id_str);
1375 continue;
1378 if (*unique_id == NULL) {
1379 if (obj_type != GOT_OBJ_TYPE_ANY) {
1380 int matched_type;
1381 err = got_object_get_type(&matched_type, repo,
1382 &id);
1383 if (err)
1384 goto done;
1385 if (matched_type != obj_type)
1386 continue;
1388 *unique_id = got_object_id_dup(&id);
1389 if (*unique_id == NULL) {
1390 err = got_error_from_errno("got_object_id_dup");
1391 free(id_str);
1392 goto done;
1394 } else {
1395 if (got_object_id_cmp(*unique_id, &id) == 0)
1396 continue; /* both packed and loose */
1397 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1398 free(id_str);
1399 goto done;
1402 done:
1403 if (dir && closedir(dir) != 0 && err == NULL)
1404 err = got_error_from_errno("closedir");
1405 if (err) {
1406 free(*unique_id);
1407 *unique_id = NULL;
1409 free(path);
1410 return err;
1413 const struct got_error *
1414 got_repo_match_object_id_prefix(struct got_object_id **id,
1415 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1417 const struct got_error *err = NULL;
1418 char *path_objects = got_repo_get_path_objects(repo);
1419 char *object_dir = NULL;
1420 size_t len;
1421 int i;
1423 *id = NULL;
1425 for (i = 0; i < strlen(id_str_prefix); i++) {
1426 if (isxdigit((unsigned char)id_str_prefix[i]))
1427 continue;
1428 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1431 len = strlen(id_str_prefix);
1432 if (len >= 2) {
1433 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1434 if (err)
1435 goto done;
1436 object_dir = strndup(id_str_prefix, 2);
1437 if (object_dir == NULL) {
1438 err = got_error_from_errno("strdup");
1439 goto done;
1441 err = match_loose_object(id, path_objects, object_dir,
1442 id_str_prefix, obj_type, repo);
1443 } else if (len == 1) {
1444 int i;
1445 for (i = 0; i < 0xf; i++) {
1446 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1447 == -1) {
1448 err = got_error_from_errno("asprintf");
1449 goto done;
1451 err = match_packed_object(id, repo, object_dir,
1452 obj_type);
1453 if (err)
1454 goto done;
1455 err = match_loose_object(id, path_objects, object_dir,
1456 id_str_prefix, obj_type, repo);
1457 if (err)
1458 goto done;
1460 } else {
1461 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1462 goto done;
1464 done:
1465 free(object_dir);
1466 if (err) {
1467 free(*id);
1468 *id = NULL;
1469 } else if (*id == NULL)
1470 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1472 return err;
1475 const struct got_error *
1476 got_repo_match_object_id(struct got_object_id **id, char **label,
1477 const char *id_str, int obj_type, struct got_reflist_head *refs,
1478 struct got_repository *repo)
1480 const struct got_error *err;
1481 struct got_tag_object *tag;
1482 struct got_reference *ref = NULL;
1484 *id = NULL;
1485 if (label)
1486 *label = NULL;
1488 if (refs) {
1489 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1490 refs, repo);
1491 if (err == NULL) {
1492 *id = got_object_id_dup(
1493 got_object_tag_get_object_id(tag));
1494 if (*id == NULL)
1495 err = got_error_from_errno("got_object_id_dup");
1496 else if (label && asprintf(label, "refs/tags/%s",
1497 got_object_tag_get_name(tag)) == -1) {
1498 err = got_error_from_errno("asprintf");
1499 free(*id);
1500 *id = NULL;
1502 got_object_tag_close(tag);
1503 return err;
1504 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1505 err->code != GOT_ERR_NO_OBJ)
1506 return err;
1509 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1510 if (err) {
1511 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1512 return err;
1513 err = got_ref_open(&ref, repo, id_str, 0);
1514 if (err != NULL)
1515 goto done;
1516 if (label) {
1517 *label = strdup(got_ref_get_name(ref));
1518 if (*label == NULL) {
1519 err = got_error_from_errno("strdup");
1520 goto done;
1523 err = got_ref_resolve(id, repo, ref);
1524 } else if (label) {
1525 err = got_object_id_str(label, *id);
1526 if (*label == NULL) {
1527 err = got_error_from_errno("strdup");
1528 goto done;
1531 done:
1532 if (ref)
1533 got_ref_close(ref);
1534 return err;
1537 const struct got_error *
1538 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1539 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1541 const struct got_error *err = NULL;
1542 struct got_reflist_entry *re;
1543 struct got_object_id *tag_id;
1544 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1546 *tag = NULL;
1548 TAILQ_FOREACH(re, refs, entry) {
1549 const char *refname;
1550 refname = got_ref_get_name(re->ref);
1551 if (got_ref_is_symbolic(re->ref))
1552 continue;
1553 if (strncmp(refname, "refs/tags/", 10) != 0)
1554 continue;
1555 if (!name_is_absolute)
1556 refname += strlen("refs/tags/");
1557 if (strcmp(refname, name) != 0)
1558 continue;
1559 err = got_ref_resolve(&tag_id, repo, re->ref);
1560 if (err)
1561 break;
1562 err = got_object_open_as_tag(tag, repo, tag_id);
1563 free(tag_id);
1564 if (err)
1565 break;
1566 if (obj_type == GOT_OBJ_TYPE_ANY ||
1567 got_object_tag_get_object_type(*tag) == obj_type)
1568 break;
1569 got_object_tag_close(*tag);
1570 *tag = NULL;
1573 if (err == NULL && *tag == NULL)
1574 err = got_error_path(name, GOT_ERR_NO_OBJ);
1575 return err;
1578 static const struct got_error *
1579 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1580 const char *name, mode_t mode, struct got_object_id *blob_id)
1582 const struct got_error *err = NULL;
1584 *new_te = NULL;
1586 *new_te = calloc(1, sizeof(**new_te));
1587 if (*new_te == NULL)
1588 return got_error_from_errno("calloc");
1590 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1591 sizeof((*new_te)->name)) {
1592 err = got_error(GOT_ERR_NO_SPACE);
1593 goto done;
1596 if (S_ISLNK(mode)) {
1597 (*new_te)->mode = S_IFLNK;
1598 } else {
1599 (*new_te)->mode = S_IFREG;
1600 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1602 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1603 done:
1604 if (err && *new_te) {
1605 free(*new_te);
1606 *new_te = NULL;
1608 return err;
1611 static const struct got_error *
1612 import_file(struct got_tree_entry **new_te, struct dirent *de,
1613 const char *path, struct got_repository *repo)
1615 const struct got_error *err;
1616 struct got_object_id *blob_id = NULL;
1617 char *filepath;
1618 struct stat sb;
1620 if (asprintf(&filepath, "%s%s%s", path,
1621 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1622 return got_error_from_errno("asprintf");
1624 if (lstat(filepath, &sb) != 0) {
1625 err = got_error_from_errno2("lstat", path);
1626 goto done;
1629 err = got_object_blob_create(&blob_id, filepath, repo);
1630 if (err)
1631 goto done;
1633 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1634 blob_id);
1635 done:
1636 free(filepath);
1637 if (err)
1638 free(blob_id);
1639 return err;
1642 static const struct got_error *
1643 insert_tree_entry(struct got_tree_entry *new_te,
1644 struct got_pathlist_head *paths)
1646 const struct got_error *err = NULL;
1647 struct got_pathlist_entry *new_pe;
1649 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1650 if (err)
1651 return err;
1652 if (new_pe == NULL)
1653 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1654 return NULL;
1657 static const struct got_error *write_tree(struct got_object_id **,
1658 const char *, struct got_pathlist_head *, struct got_repository *,
1659 got_repo_import_cb progress_cb, void *progress_arg);
1661 static const struct got_error *
1662 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1663 const char *path, struct got_pathlist_head *ignores,
1664 struct got_repository *repo,
1665 got_repo_import_cb progress_cb, void *progress_arg)
1667 const struct got_error *err;
1668 struct got_object_id *id = NULL;
1669 char *subdirpath;
1671 if (asprintf(&subdirpath, "%s%s%s", path,
1672 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1673 return got_error_from_errno("asprintf");
1675 (*new_te) = calloc(1, sizeof(**new_te));
1676 if (*new_te == NULL)
1677 return got_error_from_errno("calloc");
1678 (*new_te)->mode = S_IFDIR;
1679 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1680 sizeof((*new_te)->name)) {
1681 err = got_error(GOT_ERR_NO_SPACE);
1682 goto done;
1684 err = write_tree(&id, subdirpath, ignores, repo,
1685 progress_cb, progress_arg);
1686 if (err)
1687 goto done;
1688 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1690 done:
1691 free(id);
1692 free(subdirpath);
1693 if (err) {
1694 free(*new_te);
1695 *new_te = NULL;
1697 return err;
1700 static const struct got_error *
1701 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1702 struct got_pathlist_head *ignores, struct got_repository *repo,
1703 got_repo_import_cb progress_cb, void *progress_arg)
1705 const struct got_error *err = NULL;
1706 DIR *dir;
1707 struct dirent *de;
1708 int nentries;
1709 struct got_tree_entry *new_te = NULL;
1710 struct got_pathlist_head paths;
1711 struct got_pathlist_entry *pe;
1713 *new_tree_id = NULL;
1715 TAILQ_INIT(&paths);
1717 dir = opendir(path_dir);
1718 if (dir == NULL) {
1719 err = got_error_from_errno2("opendir", path_dir);
1720 goto done;
1723 nentries = 0;
1724 while ((de = readdir(dir)) != NULL) {
1725 int ignore = 0;
1726 int type;
1728 if (strcmp(de->d_name, ".") == 0 ||
1729 strcmp(de->d_name, "..") == 0)
1730 continue;
1732 TAILQ_FOREACH(pe, ignores, entry) {
1733 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1734 ignore = 1;
1735 break;
1738 if (ignore)
1739 continue;
1741 err = got_path_dirent_type(&type, path_dir, de);
1742 if (err)
1743 goto done;
1745 if (type == DT_DIR) {
1746 err = import_subdir(&new_te, de, path_dir,
1747 ignores, repo, progress_cb, progress_arg);
1748 if (err) {
1749 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1750 goto done;
1751 err = NULL;
1752 continue;
1754 } else if (type == DT_REG || type == DT_LNK) {
1755 err = import_file(&new_te, de, path_dir, repo);
1756 if (err)
1757 goto done;
1758 } else
1759 continue;
1761 err = insert_tree_entry(new_te, &paths);
1762 if (err)
1763 goto done;
1764 nentries++;
1767 if (TAILQ_EMPTY(&paths)) {
1768 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1769 "cannot create tree without any entries");
1770 goto done;
1773 TAILQ_FOREACH(pe, &paths, entry) {
1774 struct got_tree_entry *te = pe->data;
1775 char *path;
1776 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1777 continue;
1778 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1779 err = got_error_from_errno("asprintf");
1780 goto done;
1782 err = (*progress_cb)(progress_arg, path);
1783 free(path);
1784 if (err)
1785 goto done;
1788 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1789 done:
1790 if (dir)
1791 closedir(dir);
1792 got_pathlist_free(&paths);
1793 return err;
1796 const struct got_error *
1797 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1798 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1799 struct got_repository *repo, got_repo_import_cb progress_cb,
1800 void *progress_arg)
1802 const struct got_error *err;
1803 struct got_object_id *new_tree_id;
1805 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1806 progress_cb, progress_arg);
1807 if (err)
1808 return err;
1810 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1811 author, time(NULL), author, time(NULL), logmsg, repo);
1812 free(new_tree_id);
1813 return err;
1816 const struct got_error *
1817 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1818 struct got_repository *repo)
1820 const struct got_error *err = NULL;
1821 char *path_objects = NULL, *path = NULL;
1822 DIR *dir = NULL;
1823 struct got_object_id id;
1824 int i;
1826 *nobjects = 0;
1827 *ondisk_size = 0;
1829 path_objects = got_repo_get_path_objects(repo);
1830 if (path_objects == NULL)
1831 return got_error_from_errno("got_repo_get_path_objects");
1833 for (i = 0; i <= 0xff; i++) {
1834 struct dirent *dent;
1836 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1837 err = got_error_from_errno("asprintf");
1838 break;
1841 dir = opendir(path);
1842 if (dir == NULL) {
1843 if (errno == ENOENT) {
1844 err = NULL;
1845 continue;
1847 err = got_error_from_errno2("opendir", path);
1848 break;
1851 while ((dent = readdir(dir)) != NULL) {
1852 char *id_str;
1853 int fd;
1854 struct stat sb;
1856 if (strcmp(dent->d_name, ".") == 0 ||
1857 strcmp(dent->d_name, "..") == 0)
1858 continue;
1860 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1861 err = got_error_from_errno("asprintf");
1862 goto done;
1865 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1866 free(id_str);
1867 continue;
1869 free(id_str);
1871 err = got_object_open_loose_fd(&fd, &id, repo);
1872 if (err)
1873 goto done;
1875 if (fstat(fd, &sb) == -1) {
1876 err = got_error_from_errno("fstat");
1877 close(fd);
1878 goto done;
1880 (*nobjects)++;
1881 (*ondisk_size) += sb.st_size;
1883 if (close(fd) == -1) {
1884 err = got_error_from_errno("close");
1885 goto done;
1889 if (closedir(dir) != 0) {
1890 err = got_error_from_errno("closedir");
1891 goto done;
1893 dir = NULL;
1895 free(path);
1896 path = NULL;
1898 done:
1899 if (dir && closedir(dir) != 0 && err == NULL)
1900 err = got_error_from_errno("closedir");
1902 if (err) {
1903 *nobjects = 0;
1904 *ondisk_size = 0;
1906 free(path_objects);
1907 free(path);
1908 return err;
1911 const struct got_error *
1912 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1913 off_t *total_packsize, struct got_repository *repo)
1915 const struct got_error *err;
1916 DIR *packdir = NULL;
1917 struct dirent *dent;
1918 struct got_packidx *packidx = NULL;
1919 char *path_packidx;
1920 char *path_packfile;
1921 int packdir_fd;
1922 struct stat sb;
1924 *npackfiles = 0;
1925 *nobjects = 0;
1926 *total_packsize = 0;
1928 packdir_fd = openat(got_repo_get_fd(repo),
1929 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1930 if (packdir_fd == -1) {
1931 return got_error_from_errno_fmt("openat: %s/%s",
1932 got_repo_get_path_git_dir(repo),
1933 GOT_OBJECTS_PACK_DIR);
1936 packdir = fdopendir(packdir_fd);
1937 if (packdir == NULL) {
1938 err = got_error_from_errno("fdopendir");
1939 goto done;
1942 while ((dent = readdir(packdir)) != NULL) {
1943 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1944 continue;
1946 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1947 dent->d_name) == -1) {
1948 err = got_error_from_errno("asprintf");
1949 goto done;
1952 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1953 path_packidx, 0);
1954 free(path_packidx);
1955 if (err)
1956 goto done;
1958 if (fstat(packidx->fd, &sb) == -1)
1959 goto done;
1960 *total_packsize += sb.st_size;
1962 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1963 if (err)
1964 goto done;
1966 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1967 0) == -1) {
1968 free(path_packfile);
1969 goto done;
1971 free(path_packfile);
1972 *total_packsize += sb.st_size;
1974 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
1976 (*npackfiles)++;
1978 got_packidx_close(packidx);
1979 packidx = NULL;
1981 done:
1982 if (packidx)
1983 got_packidx_close(packidx);
1984 if (packdir && closedir(packdir) != 0 && err == NULL)
1985 err = got_error_from_errno("closedir");
1986 if (err) {
1987 *npackfiles = 0;
1988 *nobjects = 0;
1989 *total_packsize = 0;
1991 return err;