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 void
116 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
117 struct got_repository *repo)
119 *extensions = repo->extensions;
120 *nextensions = repo->nextensions;
123 int
124 got_repo_is_bare(struct got_repository *repo)
126 return (strcmp(repo->path, repo->path_git_dir) == 0);
129 static char *
130 get_path_git_child(struct got_repository *repo, const char *basename)
132 char *path_child;
134 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
135 basename) == -1)
136 return NULL;
138 return path_child;
141 char *
142 got_repo_get_path_objects(struct got_repository *repo)
144 return get_path_git_child(repo, GOT_OBJECTS_DIR);
147 char *
148 got_repo_get_path_objects_pack(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
153 char *
154 got_repo_get_path_refs(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_REFS_DIR);
159 char *
160 got_repo_get_path_packed_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
165 static char *
166 get_path_head(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_HEAD_FILE);
171 char *
172 got_repo_get_path_gitconfig(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_GITCONFIG);
177 char *
178 got_repo_get_path_gotconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
183 const struct got_gotconfig *
184 got_repo_get_gotconfig(struct got_repository *repo)
186 return repo->gotconfig;
189 void
190 got_repo_get_gitconfig_remotes(int *nremotes,
191 const struct got_remote_repo **remotes, struct got_repository *repo)
193 *nremotes = repo->ngitconfig_remotes;
194 *remotes = repo->gitconfig_remotes;
197 static int
198 is_git_repo(struct got_repository *repo)
200 const char *path_git = got_repo_get_path_git_dir(repo);
201 char *path_objects = got_repo_get_path_objects(repo);
202 char *path_refs = got_repo_get_path_refs(repo);
203 char *path_head = get_path_head(repo);
204 int ret = 0;
205 struct stat sb;
206 struct got_reference *head_ref;
208 if (lstat(path_git, &sb) == -1)
209 goto done;
210 if (!S_ISDIR(sb.st_mode))
211 goto done;
213 if (lstat(path_objects, &sb) == -1)
214 goto done;
215 if (!S_ISDIR(sb.st_mode))
216 goto done;
218 if (lstat(path_refs, &sb) == -1)
219 goto done;
220 if (!S_ISDIR(sb.st_mode))
221 goto done;
223 if (lstat(path_head, &sb) == -1)
224 goto done;
225 if (!S_ISREG(sb.st_mode))
226 goto done;
228 /* Check if the HEAD reference can be opened. */
229 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
230 goto done;
231 got_ref_close(head_ref);
233 ret = 1;
234 done:
235 free(path_objects);
236 free(path_refs);
237 free(path_head);
238 return ret;
242 const struct got_error *
243 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
244 struct got_object *obj)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->objcache, id, obj);
249 if (err) {
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
252 err = NULL;
253 return err;
255 obj->refcnt++;
256 #endif
257 return NULL;
260 struct got_object *
261 got_repo_get_cached_object(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
267 const struct got_error *
268 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
269 struct got_tree_object *tree)
271 #ifndef GOT_NO_OBJ_CACHE
272 const struct got_error *err = NULL;
273 err = got_object_cache_add(&repo->treecache, id, tree);
274 if (err) {
275 if (err->code == GOT_ERR_OBJ_EXISTS ||
276 err->code == GOT_ERR_OBJ_TOO_LARGE)
277 err = NULL;
278 return err;
280 tree->refcnt++;
281 #endif
282 return NULL;
285 struct got_tree_object *
286 got_repo_get_cached_tree(struct got_repository *repo,
287 struct got_object_id *id)
289 return (struct got_tree_object *)got_object_cache_get(
290 &repo->treecache, id);
293 const struct got_error *
294 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
295 struct got_commit_object *commit)
297 #ifndef GOT_NO_OBJ_CACHE
298 const struct got_error *err = NULL;
299 err = got_object_cache_add(&repo->commitcache, id, commit);
300 if (err) {
301 if (err->code == GOT_ERR_OBJ_EXISTS ||
302 err->code == GOT_ERR_OBJ_TOO_LARGE)
303 err = NULL;
304 return err;
306 commit->refcnt++;
307 #endif
308 return NULL;
311 struct got_commit_object *
312 got_repo_get_cached_commit(struct got_repository *repo,
313 struct got_object_id *id)
315 return (struct got_commit_object *)got_object_cache_get(
316 &repo->commitcache, id);
319 const struct got_error *
320 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
321 struct got_tag_object *tag)
323 #ifndef GOT_NO_OBJ_CACHE
324 const struct got_error *err = NULL;
325 err = got_object_cache_add(&repo->tagcache, id, tag);
326 if (err) {
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
329 err = NULL;
330 return err;
332 tag->refcnt++;
333 #endif
334 return NULL;
337 struct got_tag_object *
338 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
340 return (struct got_tag_object *)got_object_cache_get(
341 &repo->tagcache, id);
344 static const struct got_error *
345 open_repo(struct got_repository *repo, const char *path)
347 const struct got_error *err = NULL;
349 repo->gitdir_fd = -1;
351 /* bare git repository? */
352 repo->path_git_dir = strdup(path);
353 if (repo->path_git_dir == NULL)
354 return got_error_from_errno("strdup");
355 if (is_git_repo(repo)) {
356 repo->path = strdup(repo->path_git_dir);
357 if (repo->path == NULL) {
358 err = got_error_from_errno("strdup");
359 goto done;
361 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
362 if (repo->gitdir_fd == -1) {
363 err = got_error_from_errno2("open",
364 repo->path_git_dir);
365 goto done;
367 return NULL;
370 /* git repository with working tree? */
371 free(repo->path_git_dir);
372 repo->path_git_dir = NULL;
373 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
374 err = got_error_from_errno("asprintf");
375 goto done;
377 if (is_git_repo(repo)) {
378 repo->path = strdup(path);
379 if (repo->path == NULL) {
380 err = got_error_from_errno("strdup");
381 goto done;
383 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
384 if (repo->gitdir_fd == -1) {
385 err = got_error_from_errno2("open",
386 repo->path_git_dir);
387 goto done;
389 return NULL;
392 err = got_error(GOT_ERR_NOT_GIT_REPO);
393 done:
394 if (err) {
395 free(repo->path);
396 repo->path = NULL;
397 free(repo->path_git_dir);
398 repo->path_git_dir = NULL;
399 if (repo->gitdir_fd != -1)
400 close(repo->gitdir_fd);
401 repo->gitdir_fd = -1;
404 return err;
407 static const struct got_error *
408 parse_gitconfig_file(int *gitconfig_repository_format_version,
409 char **gitconfig_author_name, char **gitconfig_author_email,
410 struct got_remote_repo **remotes, int *nremotes,
411 char **gitconfig_owner, char ***extensions, int *nextensions,
412 const char *gitconfig_path)
414 const struct got_error *err = NULL, *child_err = NULL;
415 int fd = -1;
416 int imsg_fds[2] = { -1, -1 };
417 pid_t pid;
418 struct imsgbuf *ibuf;
420 *gitconfig_repository_format_version = 0;
421 if (extensions)
422 *extensions = NULL;
423 if (nextensions)
424 *nextensions = 0;
425 *gitconfig_author_name = NULL;
426 *gitconfig_author_email = NULL;
427 if (remotes)
428 *remotes = NULL;
429 if (nremotes)
430 *nremotes = 0;
431 if (gitconfig_owner)
432 *gitconfig_owner = NULL;
434 fd = open(gitconfig_path, O_RDONLY);
435 if (fd == -1) {
436 if (errno == ENOENT)
437 return NULL;
438 return got_error_from_errno2("open", gitconfig_path);
441 ibuf = calloc(1, sizeof(*ibuf));
442 if (ibuf == NULL) {
443 err = got_error_from_errno("calloc");
444 goto done;
447 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
448 err = got_error_from_errno("socketpair");
449 goto done;
452 pid = fork();
453 if (pid == -1) {
454 err = got_error_from_errno("fork");
455 goto done;
456 } else if (pid == 0) {
457 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
458 gitconfig_path);
459 /* not reached */
462 if (close(imsg_fds[1]) == -1) {
463 err = got_error_from_errno("close");
464 goto done;
466 imsg_fds[1] = -1;
467 imsg_init(ibuf, imsg_fds[0]);
469 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
470 if (err)
471 goto done;
472 fd = -1;
474 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
475 if (err)
476 goto done;
478 err = got_privsep_recv_gitconfig_int(
479 gitconfig_repository_format_version, ibuf);
480 if (err)
481 goto done;
483 if (extensions && nextensions) {
484 err = got_privsep_send_gitconfig_repository_extensions_req(
485 ibuf);
486 if (err)
487 goto done;
488 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
489 if (err)
490 goto done;
491 if (*nextensions > 0) {
492 int i;
493 *extensions = calloc(*nextensions, sizeof(char *));
494 if (*extensions == NULL) {
495 err = got_error_from_errno("calloc");
496 goto done;
498 for (i = 0; i < *nextensions; i++) {
499 char *ext;
500 err = got_privsep_recv_gitconfig_str(&ext,
501 ibuf);
502 if (err)
503 goto done;
504 (*extensions)[i] = ext;
509 err = got_privsep_send_gitconfig_author_name_req(ibuf);
510 if (err)
511 goto done;
513 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
514 if (err)
515 goto done;
517 err = got_privsep_send_gitconfig_author_email_req(ibuf);
518 if (err)
519 goto done;
521 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
522 if (err)
523 goto done;
525 if (remotes && nremotes) {
526 err = got_privsep_send_gitconfig_remotes_req(ibuf);
527 if (err)
528 goto done;
530 err = got_privsep_recv_gitconfig_remotes(remotes,
531 nremotes, ibuf);
532 if (err)
533 goto done;
536 if (gitconfig_owner) {
537 err = got_privsep_send_gitconfig_owner_req(ibuf);
538 if (err)
539 goto done;
540 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
541 if (err)
542 goto done;
545 imsg_clear(ibuf);
546 err = got_privsep_send_stop(imsg_fds[0]);
547 child_err = got_privsep_wait_for_child(pid);
548 if (child_err && err == NULL)
549 err = child_err;
550 done:
551 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
552 err = got_error_from_errno("close");
553 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
554 err = got_error_from_errno("close");
555 if (fd != -1 && close(fd) == -1 && err == NULL)
556 err = got_error_from_errno2("close", gitconfig_path);
557 free(ibuf);
558 return err;
561 static const struct got_error *
562 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
564 const struct got_error *err = NULL;
565 char *repo_gitconfig_path = NULL;
567 if (global_gitconfig_path) {
568 /* Read settings from ~/.gitconfig. */
569 int dummy_repo_version;
570 err = parse_gitconfig_file(&dummy_repo_version,
571 &repo->global_gitconfig_author_name,
572 &repo->global_gitconfig_author_email,
573 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
574 if (err)
575 return err;
578 /* Read repository's .git/config file. */
579 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
580 if (repo_gitconfig_path == NULL)
581 return got_error_from_errno("got_repo_get_path_gitconfig");
583 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
584 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
585 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
586 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
587 repo_gitconfig_path);
588 if (err)
589 goto done;
590 done:
591 free(repo_gitconfig_path);
592 return err;
595 static const struct got_error *
596 read_gotconfig(struct got_repository *repo)
598 const struct got_error *err = NULL;
599 char *gotconfig_path;
601 gotconfig_path = got_repo_get_path_gotconfig(repo);
602 if (gotconfig_path == NULL)
603 return got_error_from_errno("got_repo_get_path_gotconfig");
605 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
606 free(gotconfig_path);
607 return err;
610 /* Supported repository format extensions. */
611 static const char *repo_extensions[] = {
612 "noop", /* Got supports repository format version 1. */
613 "preciousObjects", /* Supported by gotadmin cleanup. */
614 "worktreeConfig", /* Got does not care about Git work trees. */
615 };
617 const struct got_error *
618 got_repo_open(struct got_repository **repop, const char *path,
619 const char *global_gitconfig_path)
621 struct got_repository *repo = NULL;
622 const struct got_error *err = NULL;
623 char *repo_path = NULL;
624 size_t i;
625 struct rlimit rl;
627 *repop = NULL;
629 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
630 return got_error_from_errno("getrlimit");
632 repo = calloc(1, sizeof(*repo));
633 if (repo == NULL) {
634 err = got_error_from_errno("calloc");
635 goto done;
638 for (i = 0; i < nitems(repo->privsep_children); i++) {
639 memset(&repo->privsep_children[i], 0,
640 sizeof(repo->privsep_children[0]));
641 repo->privsep_children[i].imsg_fd = -1;
644 err = got_object_cache_init(&repo->objcache,
645 GOT_OBJECT_CACHE_TYPE_OBJ);
646 if (err)
647 goto done;
648 err = got_object_cache_init(&repo->treecache,
649 GOT_OBJECT_CACHE_TYPE_TREE);
650 if (err)
651 goto done;
652 err = got_object_cache_init(&repo->commitcache,
653 GOT_OBJECT_CACHE_TYPE_COMMIT);
654 if (err)
655 goto done;
656 err = got_object_cache_init(&repo->tagcache,
657 GOT_OBJECT_CACHE_TYPE_TAG);
658 if (err)
659 goto done;
661 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
662 if (repo->pack_cache_size > rl.rlim_cur / 8)
663 repo->pack_cache_size = rl.rlim_cur / 8;
665 repo_path = realpath(path, NULL);
666 if (repo_path == NULL) {
667 err = got_error_from_errno2("realpath", path);
668 goto done;
671 for (;;) {
672 char *parent_path;
674 err = open_repo(repo, repo_path);
675 if (err == NULL)
676 break;
677 if (err->code != GOT_ERR_NOT_GIT_REPO)
678 goto done;
679 if (repo_path[0] == '/' && repo_path[1] == '\0') {
680 err = got_error(GOT_ERR_NOT_GIT_REPO);
681 goto done;
683 err = got_path_dirname(&parent_path, repo_path);
684 if (err)
685 goto done;
686 free(repo_path);
687 repo_path = parent_path;
690 err = read_gotconfig(repo);
691 if (err)
692 goto done;
694 err = read_gitconfig(repo, global_gitconfig_path);
695 if (err)
696 goto done;
697 if (repo->gitconfig_repository_format_version != 0)
698 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
699 for (i = 0; i < repo->nextensions; i++) {
700 char *ext = repo->extensions[i];
701 int j, supported = 0;
702 for (j = 0; j < nitems(repo_extensions); j++) {
703 if (strcmp(ext, repo_extensions[j]) == 0) {
704 supported = 1;
705 break;
708 if (!supported) {
709 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
710 goto done;
713 done:
714 if (err)
715 got_repo_close(repo);
716 else
717 *repop = repo;
718 free(repo_path);
719 return err;
722 const struct got_error *
723 got_repo_close(struct got_repository *repo)
725 const struct got_error *err = NULL, *child_err;
726 size_t i;
728 for (i = 0; i < repo->pack_cache_size; i++) {
729 if (repo->packidx_cache[i] == NULL)
730 break;
731 got_packidx_close(repo->packidx_cache[i]);
734 for (i = 0; i < repo->pack_cache_size; i++) {
735 if (repo->packs[i].path_packfile == NULL)
736 break;
737 got_pack_close(&repo->packs[i]);
740 free(repo->path);
741 free(repo->path_git_dir);
743 got_object_cache_close(&repo->objcache);
744 got_object_cache_close(&repo->treecache);
745 got_object_cache_close(&repo->commitcache);
746 got_object_cache_close(&repo->tagcache);
748 for (i = 0; i < nitems(repo->privsep_children); i++) {
749 if (repo->privsep_children[i].imsg_fd == -1)
750 continue;
751 imsg_clear(repo->privsep_children[i].ibuf);
752 free(repo->privsep_children[i].ibuf);
753 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
754 child_err = got_privsep_wait_for_child(
755 repo->privsep_children[i].pid);
756 if (child_err && err == NULL)
757 err = child_err;
758 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
759 err == NULL)
760 err = got_error_from_errno("close");
763 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
764 err == NULL)
765 err = got_error_from_errno("close");
767 if (repo->gotconfig)
768 got_gotconfig_free(repo->gotconfig);
769 free(repo->gitconfig_author_name);
770 free(repo->gitconfig_author_email);
771 for (i = 0; i < repo->ngitconfig_remotes; i++)
772 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
773 free(repo->gitconfig_remotes);
774 for (i = 0; i < repo->nextensions; i++)
775 free(repo->extensions[i]);
776 free(repo->extensions);
777 free(repo);
779 return err;
782 void
783 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
785 int i;
787 free(repo->name);
788 repo->name = NULL;
789 free(repo->url);
790 repo->url = NULL;
791 for (i = 0; i < repo->nbranches; i++)
792 free(repo->branches[i]);
793 free(repo->branches);
794 repo->branches = NULL;
795 repo->nbranches = 0;
798 const struct got_error *
799 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
800 const char *input_path)
802 const struct got_error *err = NULL;
803 const char *repo_abspath = NULL;
804 size_t repolen, len;
805 char *canonpath, *path = NULL;
807 *in_repo_path = NULL;
809 canonpath = strdup(input_path);
810 if (canonpath == NULL) {
811 err = got_error_from_errno("strdup");
812 goto done;
814 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
815 if (err)
816 goto done;
818 repo_abspath = got_repo_get_path(repo);
820 if (canonpath[0] == '\0') {
821 path = strdup(canonpath);
822 if (path == NULL) {
823 err = got_error_from_errno("strdup");
824 goto done;
826 } else {
827 path = realpath(canonpath, NULL);
828 if (path == NULL) {
829 if (errno != ENOENT) {
830 err = got_error_from_errno2("realpath",
831 canonpath);
832 goto done;
834 /*
835 * Path is not on disk.
836 * Assume it is already relative to repository root.
837 */
838 path = strdup(canonpath);
839 if (path == NULL) {
840 err = got_error_from_errno("strdup");
841 goto done;
845 repolen = strlen(repo_abspath);
846 len = strlen(path);
849 if (strcmp(path, repo_abspath) == 0) {
850 free(path);
851 path = strdup("");
852 if (path == NULL) {
853 err = got_error_from_errno("strdup");
854 goto done;
856 } else if (len > repolen &&
857 got_path_is_child(path, repo_abspath, repolen)) {
858 /* Matched an on-disk path inside repository. */
859 if (got_repo_is_bare(repo)) {
860 /*
861 * Matched an on-disk path inside repository
862 * database. Treat input as repository-relative.
863 */
864 free(path);
865 path = canonpath;
866 canonpath = NULL;
867 } else {
868 char *child;
869 /* Strip common prefix with repository path. */
870 err = got_path_skip_common_ancestor(&child,
871 repo_abspath, path);
872 if (err)
873 goto done;
874 free(path);
875 path = child;
877 } else {
878 /*
879 * Matched unrelated on-disk path.
880 * Treat input as repository-relative.
881 */
882 free(path);
883 path = canonpath;
884 canonpath = NULL;
888 /* Make in-repository path absolute */
889 if (path[0] != '/') {
890 char *abspath;
891 if (asprintf(&abspath, "/%s", path) == -1) {
892 err = got_error_from_errno("asprintf");
893 goto done;
895 free(path);
896 path = abspath;
899 done:
900 free(canonpath);
901 if (err)
902 free(path);
903 else
904 *in_repo_path = path;
905 return err;
908 static const struct got_error *
909 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
910 const char *path_packidx)
912 const struct got_error *err = NULL;
913 size_t i;
915 for (i = 0; i < repo->pack_cache_size; i++) {
916 if (repo->packidx_cache[i] == NULL)
917 break;
918 if (strcmp(repo->packidx_cache[i]->path_packidx,
919 path_packidx) == 0) {
920 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
923 if (i == repo->pack_cache_size) {
924 err = got_packidx_close(repo->packidx_cache[i - 1]);
925 if (err)
926 return err;
929 /*
930 * Insert the new pack index at the front so it will
931 * be searched first in the future.
932 */
933 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
934 sizeof(repo->packidx_cache) -
935 sizeof(repo->packidx_cache[0]));
936 repo->packidx_cache[0] = packidx;
938 return NULL;
941 static int
942 is_packidx_filename(const char *name, size_t len)
944 if (len != GOT_PACKIDX_NAMELEN)
945 return 0;
947 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
948 return 0;
950 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
951 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
952 return 0;
954 return 1;
957 const struct got_error *
958 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
959 struct got_repository *repo, struct got_object_id *id)
961 const struct got_error *err;
962 DIR *packdir = NULL;
963 struct dirent *dent;
964 char *path_packidx;
965 size_t i;
966 int packdir_fd;
968 /* Search pack index cache. */
969 for (i = 0; i < repo->pack_cache_size; i++) {
970 if (repo->packidx_cache[i] == NULL)
971 break;
972 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
973 if (*idx != -1) {
974 *packidx = repo->packidx_cache[i];
975 /*
976 * Move this cache entry to the front. Repeatedly
977 * searching a wrong pack index can be expensive.
978 */
979 if (i > 0) {
980 struct got_packidx *p;
981 p = repo->packidx_cache[0];
982 repo->packidx_cache[0] = *packidx;
983 repo->packidx_cache[i] = p;
985 return NULL;
988 /* No luck. Search the filesystem. */
990 packdir_fd = openat(got_repo_get_fd(repo),
991 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
992 if (packdir_fd == -1) {
993 if (errno == ENOENT)
994 err = got_error_no_obj(id);
995 else
996 err = got_error_from_errno_fmt("openat: %s/%s",
997 got_repo_get_path_git_dir(repo),
998 GOT_OBJECTS_PACK_DIR);
999 goto done;
1002 packdir = fdopendir(packdir_fd);
1003 if (packdir == NULL) {
1004 err = got_error_from_errno("fdopendir");
1005 goto done;
1008 while ((dent = readdir(packdir)) != NULL) {
1009 int is_cached = 0;
1011 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1012 continue;
1014 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1015 dent->d_name) == -1) {
1016 err = got_error_from_errno("asprintf");
1017 goto done;
1020 for (i = 0; i < repo->pack_cache_size; i++) {
1021 if (repo->packidx_cache[i] == NULL)
1022 break;
1023 if (strcmp(repo->packidx_cache[i]->path_packidx,
1024 path_packidx) == 0) {
1025 is_cached = 1;
1026 break;
1029 if (is_cached) {
1030 free(path_packidx);
1031 continue; /* already searched */
1034 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1035 path_packidx, 0);
1036 if (err) {
1037 free(path_packidx);
1038 goto done;
1041 err = cache_packidx(repo, *packidx, path_packidx);
1042 free(path_packidx);
1043 if (err)
1044 goto done;
1046 *idx = got_packidx_get_object_idx(*packidx, id);
1047 if (*idx != -1) {
1048 err = NULL; /* found the object */
1049 goto done;
1053 err = got_error_no_obj(id);
1054 done:
1055 if (packdir && closedir(packdir) != 0 && err == NULL)
1056 err = got_error_from_errno("closedir");
1057 return err;
1060 static const struct got_error *
1061 read_packfile_hdr(int fd, struct got_packidx *packidx)
1063 const struct got_error *err = NULL;
1064 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1065 struct got_packfile_hdr hdr;
1066 ssize_t n;
1068 n = read(fd, &hdr, sizeof(hdr));
1069 if (n < 0)
1070 return got_error_from_errno("read");
1071 if (n != sizeof(hdr))
1072 return got_error(GOT_ERR_BAD_PACKFILE);
1074 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1075 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1076 be32toh(hdr.nobjects) != totobj)
1077 err = got_error(GOT_ERR_BAD_PACKFILE);
1079 return err;
1082 static const struct got_error *
1083 open_packfile(int *fd, struct got_repository *repo,
1084 const char *relpath, struct got_packidx *packidx)
1086 const struct got_error *err = NULL;
1088 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1089 if (*fd == -1)
1090 return got_error_from_errno_fmt("openat: %s/%s",
1091 got_repo_get_path_git_dir(repo), relpath);
1093 if (packidx) {
1094 err = read_packfile_hdr(*fd, packidx);
1095 if (err) {
1096 close(*fd);
1097 *fd = -1;
1101 return err;
1104 const struct got_error *
1105 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1106 const char *path_packfile, struct got_packidx *packidx)
1108 const struct got_error *err = NULL;
1109 struct got_pack *pack = NULL;
1110 struct stat sb;
1111 size_t i;
1113 if (packp)
1114 *packp = NULL;
1116 for (i = 0; i < repo->pack_cache_size; i++) {
1117 pack = &repo->packs[i];
1118 if (pack->path_packfile == NULL)
1119 break;
1120 if (strcmp(pack->path_packfile, path_packfile) == 0)
1121 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1124 if (i == repo->pack_cache_size) {
1125 err = got_pack_close(&repo->packs[i - 1]);
1126 if (err)
1127 return err;
1128 memmove(&repo->packs[1], &repo->packs[0],
1129 sizeof(repo->packs) - sizeof(repo->packs[0]));
1130 i = 0;
1133 pack = &repo->packs[i];
1135 pack->path_packfile = strdup(path_packfile);
1136 if (pack->path_packfile == NULL) {
1137 err = got_error_from_errno("strdup");
1138 goto done;
1141 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1142 if (err)
1143 goto done;
1145 if (fstat(pack->fd, &sb) != 0) {
1146 err = got_error_from_errno("fstat");
1147 goto done;
1149 pack->filesize = sb.st_size;
1151 pack->privsep_child = NULL;
1153 #ifndef GOT_PACK_NO_MMAP
1154 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1155 pack->fd, 0);
1156 if (pack->map == MAP_FAILED) {
1157 if (errno != ENOMEM) {
1158 err = got_error_from_errno("mmap");
1159 goto done;
1161 pack->map = NULL; /* fall back to read(2) */
1163 #endif
1164 done:
1165 if (err) {
1166 if (pack) {
1167 free(pack->path_packfile);
1168 memset(pack, 0, sizeof(*pack));
1170 } else if (packp)
1171 *packp = pack;
1172 return err;
1175 struct got_pack *
1176 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1178 struct got_pack *pack = NULL;
1179 size_t i;
1181 for (i = 0; i < repo->pack_cache_size; i++) {
1182 pack = &repo->packs[i];
1183 if (pack->path_packfile == NULL)
1184 break;
1185 if (strcmp(pack->path_packfile, path_packfile) == 0)
1186 return pack;
1189 return NULL;
1192 const struct got_error *
1193 got_repo_init(const char *repo_path)
1195 const struct got_error *err = NULL;
1196 const char *dirnames[] = {
1197 GOT_OBJECTS_DIR,
1198 GOT_OBJECTS_PACK_DIR,
1199 GOT_REFS_DIR,
1201 const char *description_str = "Unnamed repository; "
1202 "edit this file 'description' to name the repository.";
1203 const char *headref_str = "ref: refs/heads/main";
1204 const char *gitconfig_str = "[core]\n"
1205 "\trepositoryformatversion = 0\n"
1206 "\tfilemode = true\n"
1207 "\tbare = true\n";
1208 char *path;
1209 size_t i;
1211 if (!got_path_dir_is_empty(repo_path))
1212 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1214 for (i = 0; i < nitems(dirnames); i++) {
1215 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1216 return got_error_from_errno("asprintf");
1218 err = got_path_mkdir(path);
1219 free(path);
1220 if (err)
1221 return err;
1224 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1225 return got_error_from_errno("asprintf");
1226 err = got_path_create_file(path, description_str);
1227 free(path);
1228 if (err)
1229 return err;
1231 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1232 return got_error_from_errno("asprintf");
1233 err = got_path_create_file(path, headref_str);
1234 free(path);
1235 if (err)
1236 return err;
1238 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1239 return got_error_from_errno("asprintf");
1240 err = got_path_create_file(path, gitconfig_str);
1241 free(path);
1242 if (err)
1243 return err;
1245 return NULL;
1248 static const struct got_error *
1249 match_packed_object(struct got_object_id **unique_id,
1250 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1252 const struct got_error *err = NULL;
1253 DIR *packdir = NULL;
1254 struct dirent *dent;
1255 char *path_packidx;
1256 struct got_object_id_queue matched_ids;
1257 int packdir_fd;
1259 STAILQ_INIT(&matched_ids);
1261 packdir_fd = openat(got_repo_get_fd(repo),
1262 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1263 if (packdir_fd == -1) {
1264 if (errno != ENOENT)
1265 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1266 goto done;
1269 packdir = fdopendir(packdir_fd);
1270 if (packdir == NULL) {
1271 err = got_error_from_errno("fdopendir");
1272 goto done;
1275 while ((dent = readdir(packdir)) != NULL) {
1276 struct got_packidx *packidx;
1277 struct got_object_qid *qid;
1279 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1280 continue;
1282 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1283 dent->d_name) == -1) {
1284 err = got_error_from_errno("strdup");
1285 break;
1288 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1289 path_packidx, 0);
1290 free(path_packidx);
1291 if (err)
1292 break;
1294 err = got_packidx_match_id_str_prefix(&matched_ids,
1295 packidx, id_str_prefix);
1296 if (err) {
1297 got_packidx_close(packidx);
1298 break;
1300 err = got_packidx_close(packidx);
1301 if (err)
1302 break;
1304 STAILQ_FOREACH(qid, &matched_ids, entry) {
1305 if (obj_type != GOT_OBJ_TYPE_ANY) {
1306 int matched_type;
1307 err = got_object_get_type(&matched_type, repo,
1308 qid->id);
1309 if (err)
1310 goto done;
1311 if (matched_type != obj_type)
1312 continue;
1314 if (*unique_id == NULL) {
1315 *unique_id = got_object_id_dup(qid->id);
1316 if (*unique_id == NULL) {
1317 err = got_error_from_errno("malloc");
1318 goto done;
1320 } else {
1321 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1322 continue; /* packed multiple times */
1323 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1324 goto done;
1328 done:
1329 got_object_id_queue_free(&matched_ids);
1330 if (packdir && closedir(packdir) != 0 && err == NULL)
1331 err = got_error_from_errno("closedir");
1332 if (err) {
1333 free(*unique_id);
1334 *unique_id = NULL;
1336 return err;
1339 static const struct got_error *
1340 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1341 const char *object_dir, const char *id_str_prefix, int obj_type,
1342 struct got_repository *repo)
1344 const struct got_error *err = NULL;
1345 char *path;
1346 DIR *dir = NULL;
1347 struct dirent *dent;
1348 struct got_object_id id;
1350 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1351 err = got_error_from_errno("asprintf");
1352 goto done;
1355 dir = opendir(path);
1356 if (dir == NULL) {
1357 if (errno == ENOENT) {
1358 err = NULL;
1359 goto done;
1361 err = got_error_from_errno2("opendir", path);
1362 goto done;
1364 while ((dent = readdir(dir)) != NULL) {
1365 char *id_str;
1366 int cmp;
1368 if (strcmp(dent->d_name, ".") == 0 ||
1369 strcmp(dent->d_name, "..") == 0)
1370 continue;
1372 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1373 err = got_error_from_errno("asprintf");
1374 goto done;
1377 if (!got_parse_sha1_digest(id.sha1, id_str))
1378 continue;
1381 * Directory entries do not necessarily appear in
1382 * sorted order, so we must iterate over all of them.
1384 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1385 if (cmp != 0) {
1386 free(id_str);
1387 continue;
1390 if (*unique_id == NULL) {
1391 if (obj_type != GOT_OBJ_TYPE_ANY) {
1392 int matched_type;
1393 err = got_object_get_type(&matched_type, repo,
1394 &id);
1395 if (err)
1396 goto done;
1397 if (matched_type != obj_type)
1398 continue;
1400 *unique_id = got_object_id_dup(&id);
1401 if (*unique_id == NULL) {
1402 err = got_error_from_errno("got_object_id_dup");
1403 free(id_str);
1404 goto done;
1406 } else {
1407 if (got_object_id_cmp(*unique_id, &id) == 0)
1408 continue; /* both packed and loose */
1409 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1410 free(id_str);
1411 goto done;
1414 done:
1415 if (dir && closedir(dir) != 0 && err == NULL)
1416 err = got_error_from_errno("closedir");
1417 if (err) {
1418 free(*unique_id);
1419 *unique_id = NULL;
1421 free(path);
1422 return err;
1425 const struct got_error *
1426 got_repo_match_object_id_prefix(struct got_object_id **id,
1427 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1429 const struct got_error *err = NULL;
1430 char *path_objects = got_repo_get_path_objects(repo);
1431 char *object_dir = NULL;
1432 size_t len;
1433 int i;
1435 *id = NULL;
1437 for (i = 0; i < strlen(id_str_prefix); i++) {
1438 if (isxdigit((unsigned char)id_str_prefix[i]))
1439 continue;
1440 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1443 len = strlen(id_str_prefix);
1444 if (len >= 2) {
1445 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1446 if (err)
1447 goto done;
1448 object_dir = strndup(id_str_prefix, 2);
1449 if (object_dir == NULL) {
1450 err = got_error_from_errno("strdup");
1451 goto done;
1453 err = match_loose_object(id, path_objects, object_dir,
1454 id_str_prefix, obj_type, repo);
1455 } else if (len == 1) {
1456 int i;
1457 for (i = 0; i < 0xf; i++) {
1458 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1459 == -1) {
1460 err = got_error_from_errno("asprintf");
1461 goto done;
1463 err = match_packed_object(id, repo, object_dir,
1464 obj_type);
1465 if (err)
1466 goto done;
1467 err = match_loose_object(id, path_objects, object_dir,
1468 id_str_prefix, obj_type, repo);
1469 if (err)
1470 goto done;
1472 } else {
1473 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1474 goto done;
1476 done:
1477 free(object_dir);
1478 if (err) {
1479 free(*id);
1480 *id = NULL;
1481 } else if (*id == NULL)
1482 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1484 return err;
1487 const struct got_error *
1488 got_repo_match_object_id(struct got_object_id **id, char **label,
1489 const char *id_str, int obj_type, struct got_reflist_head *refs,
1490 struct got_repository *repo)
1492 const struct got_error *err;
1493 struct got_tag_object *tag;
1494 struct got_reference *ref = NULL;
1496 *id = NULL;
1497 if (label)
1498 *label = NULL;
1500 if (refs) {
1501 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1502 refs, repo);
1503 if (err == NULL) {
1504 *id = got_object_id_dup(
1505 got_object_tag_get_object_id(tag));
1506 if (*id == NULL)
1507 err = got_error_from_errno("got_object_id_dup");
1508 else if (label && asprintf(label, "refs/tags/%s",
1509 got_object_tag_get_name(tag)) == -1) {
1510 err = got_error_from_errno("asprintf");
1511 free(*id);
1512 *id = NULL;
1514 got_object_tag_close(tag);
1515 return err;
1516 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1517 err->code != GOT_ERR_NO_OBJ)
1518 return err;
1521 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1522 if (err) {
1523 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1524 return err;
1525 err = got_ref_open(&ref, repo, id_str, 0);
1526 if (err != NULL)
1527 goto done;
1528 if (label) {
1529 *label = strdup(got_ref_get_name(ref));
1530 if (*label == NULL) {
1531 err = got_error_from_errno("strdup");
1532 goto done;
1535 err = got_ref_resolve(id, repo, ref);
1536 } else if (label) {
1537 err = got_object_id_str(label, *id);
1538 if (*label == NULL) {
1539 err = got_error_from_errno("strdup");
1540 goto done;
1543 done:
1544 if (ref)
1545 got_ref_close(ref);
1546 return err;
1549 const struct got_error *
1550 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1551 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1553 const struct got_error *err = NULL;
1554 struct got_reflist_entry *re;
1555 struct got_object_id *tag_id;
1556 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1558 *tag = NULL;
1560 TAILQ_FOREACH(re, refs, entry) {
1561 const char *refname;
1562 refname = got_ref_get_name(re->ref);
1563 if (got_ref_is_symbolic(re->ref))
1564 continue;
1565 if (strncmp(refname, "refs/tags/", 10) != 0)
1566 continue;
1567 if (!name_is_absolute)
1568 refname += strlen("refs/tags/");
1569 if (strcmp(refname, name) != 0)
1570 continue;
1571 err = got_ref_resolve(&tag_id, repo, re->ref);
1572 if (err)
1573 break;
1574 err = got_object_open_as_tag(tag, repo, tag_id);
1575 free(tag_id);
1576 if (err)
1577 break;
1578 if (obj_type == GOT_OBJ_TYPE_ANY ||
1579 got_object_tag_get_object_type(*tag) == obj_type)
1580 break;
1581 got_object_tag_close(*tag);
1582 *tag = NULL;
1585 if (err == NULL && *tag == NULL)
1586 err = got_error_path(name, GOT_ERR_NO_OBJ);
1587 return err;
1590 static const struct got_error *
1591 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1592 const char *name, mode_t mode, struct got_object_id *blob_id)
1594 const struct got_error *err = NULL;
1596 *new_te = NULL;
1598 *new_te = calloc(1, sizeof(**new_te));
1599 if (*new_te == NULL)
1600 return got_error_from_errno("calloc");
1602 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1603 sizeof((*new_te)->name)) {
1604 err = got_error(GOT_ERR_NO_SPACE);
1605 goto done;
1608 if (S_ISLNK(mode)) {
1609 (*new_te)->mode = S_IFLNK;
1610 } else {
1611 (*new_te)->mode = S_IFREG;
1612 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1614 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1615 done:
1616 if (err && *new_te) {
1617 free(*new_te);
1618 *new_te = NULL;
1620 return err;
1623 static const struct got_error *
1624 import_file(struct got_tree_entry **new_te, struct dirent *de,
1625 const char *path, struct got_repository *repo)
1627 const struct got_error *err;
1628 struct got_object_id *blob_id = NULL;
1629 char *filepath;
1630 struct stat sb;
1632 if (asprintf(&filepath, "%s%s%s", path,
1633 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1634 return got_error_from_errno("asprintf");
1636 if (lstat(filepath, &sb) != 0) {
1637 err = got_error_from_errno2("lstat", path);
1638 goto done;
1641 err = got_object_blob_create(&blob_id, filepath, repo);
1642 if (err)
1643 goto done;
1645 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1646 blob_id);
1647 done:
1648 free(filepath);
1649 if (err)
1650 free(blob_id);
1651 return err;
1654 static const struct got_error *
1655 insert_tree_entry(struct got_tree_entry *new_te,
1656 struct got_pathlist_head *paths)
1658 const struct got_error *err = NULL;
1659 struct got_pathlist_entry *new_pe;
1661 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1662 if (err)
1663 return err;
1664 if (new_pe == NULL)
1665 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1666 return NULL;
1669 static const struct got_error *write_tree(struct got_object_id **,
1670 const char *, struct got_pathlist_head *, struct got_repository *,
1671 got_repo_import_cb progress_cb, void *progress_arg);
1673 static const struct got_error *
1674 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1675 const char *path, struct got_pathlist_head *ignores,
1676 struct got_repository *repo,
1677 got_repo_import_cb progress_cb, void *progress_arg)
1679 const struct got_error *err;
1680 struct got_object_id *id = NULL;
1681 char *subdirpath;
1683 if (asprintf(&subdirpath, "%s%s%s", path,
1684 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1685 return got_error_from_errno("asprintf");
1687 (*new_te) = calloc(1, sizeof(**new_te));
1688 if (*new_te == NULL)
1689 return got_error_from_errno("calloc");
1690 (*new_te)->mode = S_IFDIR;
1691 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1692 sizeof((*new_te)->name)) {
1693 err = got_error(GOT_ERR_NO_SPACE);
1694 goto done;
1696 err = write_tree(&id, subdirpath, ignores, repo,
1697 progress_cb, progress_arg);
1698 if (err)
1699 goto done;
1700 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1702 done:
1703 free(id);
1704 free(subdirpath);
1705 if (err) {
1706 free(*new_te);
1707 *new_te = NULL;
1709 return err;
1712 static const struct got_error *
1713 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1714 struct got_pathlist_head *ignores, struct got_repository *repo,
1715 got_repo_import_cb progress_cb, void *progress_arg)
1717 const struct got_error *err = NULL;
1718 DIR *dir;
1719 struct dirent *de;
1720 int nentries;
1721 struct got_tree_entry *new_te = NULL;
1722 struct got_pathlist_head paths;
1723 struct got_pathlist_entry *pe;
1725 *new_tree_id = NULL;
1727 TAILQ_INIT(&paths);
1729 dir = opendir(path_dir);
1730 if (dir == NULL) {
1731 err = got_error_from_errno2("opendir", path_dir);
1732 goto done;
1735 nentries = 0;
1736 while ((de = readdir(dir)) != NULL) {
1737 int ignore = 0;
1738 int type;
1740 if (strcmp(de->d_name, ".") == 0 ||
1741 strcmp(de->d_name, "..") == 0)
1742 continue;
1744 TAILQ_FOREACH(pe, ignores, entry) {
1745 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1746 ignore = 1;
1747 break;
1750 if (ignore)
1751 continue;
1753 err = got_path_dirent_type(&type, path_dir, de);
1754 if (err)
1755 goto done;
1757 if (type == DT_DIR) {
1758 err = import_subdir(&new_te, de, path_dir,
1759 ignores, repo, progress_cb, progress_arg);
1760 if (err) {
1761 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1762 goto done;
1763 err = NULL;
1764 continue;
1766 } else if (type == DT_REG || type == DT_LNK) {
1767 err = import_file(&new_te, de, path_dir, repo);
1768 if (err)
1769 goto done;
1770 } else
1771 continue;
1773 err = insert_tree_entry(new_te, &paths);
1774 if (err)
1775 goto done;
1776 nentries++;
1779 if (TAILQ_EMPTY(&paths)) {
1780 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1781 "cannot create tree without any entries");
1782 goto done;
1785 TAILQ_FOREACH(pe, &paths, entry) {
1786 struct got_tree_entry *te = pe->data;
1787 char *path;
1788 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1789 continue;
1790 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1791 err = got_error_from_errno("asprintf");
1792 goto done;
1794 err = (*progress_cb)(progress_arg, path);
1795 free(path);
1796 if (err)
1797 goto done;
1800 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1801 done:
1802 if (dir)
1803 closedir(dir);
1804 got_pathlist_free(&paths);
1805 return err;
1808 const struct got_error *
1809 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1810 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1811 struct got_repository *repo, got_repo_import_cb progress_cb,
1812 void *progress_arg)
1814 const struct got_error *err;
1815 struct got_object_id *new_tree_id;
1817 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1818 progress_cb, progress_arg);
1819 if (err)
1820 return err;
1822 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1823 author, time(NULL), author, time(NULL), logmsg, repo);
1824 free(new_tree_id);
1825 return err;
1828 const struct got_error *
1829 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1830 struct got_repository *repo)
1832 const struct got_error *err = NULL;
1833 char *path_objects = NULL, *path = NULL;
1834 DIR *dir = NULL;
1835 struct got_object_id id;
1836 int i;
1838 *nobjects = 0;
1839 *ondisk_size = 0;
1841 path_objects = got_repo_get_path_objects(repo);
1842 if (path_objects == NULL)
1843 return got_error_from_errno("got_repo_get_path_objects");
1845 for (i = 0; i <= 0xff; i++) {
1846 struct dirent *dent;
1848 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1849 err = got_error_from_errno("asprintf");
1850 break;
1853 dir = opendir(path);
1854 if (dir == NULL) {
1855 if (errno == ENOENT) {
1856 err = NULL;
1857 continue;
1859 err = got_error_from_errno2("opendir", path);
1860 break;
1863 while ((dent = readdir(dir)) != NULL) {
1864 char *id_str;
1865 int fd;
1866 struct stat sb;
1868 if (strcmp(dent->d_name, ".") == 0 ||
1869 strcmp(dent->d_name, "..") == 0)
1870 continue;
1872 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1873 err = got_error_from_errno("asprintf");
1874 goto done;
1877 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1878 free(id_str);
1879 continue;
1881 free(id_str);
1883 err = got_object_open_loose_fd(&fd, &id, repo);
1884 if (err)
1885 goto done;
1887 if (fstat(fd, &sb) == -1) {
1888 err = got_error_from_errno("fstat");
1889 close(fd);
1890 goto done;
1892 (*nobjects)++;
1893 (*ondisk_size) += sb.st_size;
1895 if (close(fd) == -1) {
1896 err = got_error_from_errno("close");
1897 goto done;
1901 if (closedir(dir) != 0) {
1902 err = got_error_from_errno("closedir");
1903 goto done;
1905 dir = NULL;
1907 free(path);
1908 path = NULL;
1910 done:
1911 if (dir && closedir(dir) != 0 && err == NULL)
1912 err = got_error_from_errno("closedir");
1914 if (err) {
1915 *nobjects = 0;
1916 *ondisk_size = 0;
1918 free(path_objects);
1919 free(path);
1920 return err;
1923 const struct got_error *
1924 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1925 off_t *total_packsize, struct got_repository *repo)
1927 const struct got_error *err = NULL;
1928 DIR *packdir = NULL;
1929 struct dirent *dent;
1930 struct got_packidx *packidx = NULL;
1931 char *path_packidx;
1932 char *path_packfile;
1933 int packdir_fd;
1934 struct stat sb;
1936 *npackfiles = 0;
1937 *nobjects = 0;
1938 *total_packsize = 0;
1940 packdir_fd = openat(got_repo_get_fd(repo),
1941 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1942 if (packdir_fd == -1) {
1943 return got_error_from_errno_fmt("openat: %s/%s",
1944 got_repo_get_path_git_dir(repo),
1945 GOT_OBJECTS_PACK_DIR);
1948 packdir = fdopendir(packdir_fd);
1949 if (packdir == NULL) {
1950 err = got_error_from_errno("fdopendir");
1951 goto done;
1954 while ((dent = readdir(packdir)) != NULL) {
1955 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1956 continue;
1958 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1959 dent->d_name) == -1) {
1960 err = got_error_from_errno("asprintf");
1961 goto done;
1964 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1965 path_packidx, 0);
1966 free(path_packidx);
1967 if (err)
1968 goto done;
1970 if (fstat(packidx->fd, &sb) == -1)
1971 goto done;
1972 *total_packsize += sb.st_size;
1974 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1975 if (err)
1976 goto done;
1978 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1979 0) == -1) {
1980 free(path_packfile);
1981 goto done;
1983 free(path_packfile);
1984 *total_packsize += sb.st_size;
1986 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
1988 (*npackfiles)++;
1990 got_packidx_close(packidx);
1991 packidx = NULL;
1993 done:
1994 if (packidx)
1995 got_packidx_close(packidx);
1996 if (packdir && closedir(packdir) != 0 && err == NULL)
1997 err = got_error_from_errno("closedir");
1998 if (err) {
1999 *npackfiles = 0;
2000 *nobjects = 0;
2001 *total_packsize = 0;
2003 return err;