Blob


1 /*
2 * Copyright (c) 2018, 2019 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/syslimits.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sha1.h>
33 #include <string.h>
34 #include <time.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
39 #include <imsg.h>
40 #include <uuid.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_object.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_worktree.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 #endif
66 #define GOT_GIT_DIR ".git"
68 /* Mandatory files and directories inside the git directory. */
69 #define GOT_OBJECTS_DIR "objects"
70 #define GOT_REFS_DIR "refs"
71 #define GOT_HEAD_FILE "HEAD"
72 #define GOT_GITCONFIG "config"
74 /* Other files and directories inside the git directory. */
75 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
76 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
77 #define GOT_OBJECTS_PACK_DIR "objects/pack"
78 #define GOT_PACKED_REFS_FILE "packed-refs"
80 const char *
81 got_repo_get_path(struct got_repository *repo)
82 {
83 return repo->path;
84 }
86 const char *
87 got_repo_get_path_git_dir(struct got_repository *repo)
88 {
89 return repo->path_git_dir;
90 }
92 const char *
93 got_repo_get_gitconfig_author_name(struct got_repository *repo)
94 {
95 return repo->gitconfig_author_name;
96 }
98 const char *
99 got_repo_get_gitconfig_author_email(struct got_repository *repo)
101 return repo->gitconfig_author_email;
104 const char *
105 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
107 return repo->global_gitconfig_author_name;
110 const char *
111 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
113 return repo->global_gitconfig_author_email;
116 int
117 got_repo_is_bare(struct got_repository *repo)
119 return (strcmp(repo->path, repo->path_git_dir) == 0);
122 static char *
123 get_path_git_child(struct got_repository *repo, const char *basename)
125 char *path_child;
127 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
128 basename) == -1)
129 return NULL;
131 return path_child;
134 char *
135 got_repo_get_path_objects(struct got_repository *repo)
137 return get_path_git_child(repo, GOT_OBJECTS_DIR);
140 char *
141 got_repo_get_path_objects_pack(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
146 char *
147 got_repo_get_path_refs(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_REFS_DIR);
152 char *
153 got_repo_get_path_packed_refs(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
158 static char *
159 get_path_head(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_HEAD_FILE);
164 static const struct got_error *
165 get_path_gitconfig(char **p, struct got_repository *repo)
167 *p = get_path_git_child(repo, GOT_GITCONFIG);
168 if (*p == NULL)
169 return got_error_from_errno("asprintf");
170 return NULL;
173 static int
174 is_git_repo(struct got_repository *repo)
176 const char *path_git = got_repo_get_path_git_dir(repo);
177 char *path_objects = got_repo_get_path_objects(repo);
178 char *path_refs = got_repo_get_path_refs(repo);
179 char *path_head = get_path_head(repo);
180 int ret = 0;
181 struct stat sb;
182 struct got_reference *head_ref;
184 if (lstat(path_git, &sb) == -1)
185 goto done;
186 if (!S_ISDIR(sb.st_mode))
187 goto done;
189 if (lstat(path_objects, &sb) == -1)
190 goto done;
191 if (!S_ISDIR(sb.st_mode))
192 goto done;
194 if (lstat(path_refs, &sb) == -1)
195 goto done;
196 if (!S_ISDIR(sb.st_mode))
197 goto done;
199 if (lstat(path_head, &sb) == -1)
200 goto done;
201 if (!S_ISREG(sb.st_mode))
202 goto done;
204 /* Check if the HEAD reference can be opened. */
205 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
206 goto done;
207 got_ref_close(head_ref);
209 ret = 1;
210 done:
211 free(path_objects);
212 free(path_refs);
213 free(path_head);
214 return ret;
218 const struct got_error *
219 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
220 struct got_object *obj)
222 #ifndef GOT_NO_OBJ_CACHE
223 const struct got_error *err = NULL;
224 err = got_object_cache_add(&repo->objcache, id, obj);
225 if (err) {
226 if (err->code == GOT_ERR_OBJ_EXISTS ||
227 err->code == GOT_ERR_OBJ_TOO_LARGE)
228 err = NULL;
229 return err;
231 obj->refcnt++;
232 #endif
233 return NULL;
236 struct got_object *
237 got_repo_get_cached_object(struct got_repository *repo,
238 struct got_object_id *id)
240 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
243 const struct got_error *
244 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
245 struct got_tree_object *tree)
247 #ifndef GOT_NO_OBJ_CACHE
248 const struct got_error *err = NULL;
249 err = got_object_cache_add(&repo->treecache, id, tree);
250 if (err) {
251 if (err->code == GOT_ERR_OBJ_EXISTS ||
252 err->code == GOT_ERR_OBJ_TOO_LARGE)
253 err = NULL;
254 return err;
256 tree->refcnt++;
257 #endif
258 return NULL;
261 struct got_tree_object *
262 got_repo_get_cached_tree(struct got_repository *repo,
263 struct got_object_id *id)
265 return (struct got_tree_object *)got_object_cache_get(
266 &repo->treecache, id);
269 const struct got_error *
270 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
271 struct got_commit_object *commit)
273 #ifndef GOT_NO_OBJ_CACHE
274 const struct got_error *err = NULL;
275 err = got_object_cache_add(&repo->commitcache, id, commit);
276 if (err) {
277 if (err->code == GOT_ERR_OBJ_EXISTS ||
278 err->code == GOT_ERR_OBJ_TOO_LARGE)
279 err = NULL;
280 return err;
282 commit->refcnt++;
283 #endif
284 return NULL;
287 struct got_commit_object *
288 got_repo_get_cached_commit(struct got_repository *repo,
289 struct got_object_id *id)
291 return (struct got_commit_object *)got_object_cache_get(
292 &repo->commitcache, id);
295 const struct got_error *
296 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
297 struct got_tag_object *tag)
299 #ifndef GOT_NO_OBJ_CACHE
300 const struct got_error *err = NULL;
301 err = got_object_cache_add(&repo->tagcache, id, tag);
302 if (err) {
303 if (err->code == GOT_ERR_OBJ_EXISTS ||
304 err->code == GOT_ERR_OBJ_TOO_LARGE)
305 err = NULL;
306 return err;
308 tag->refcnt++;
309 #endif
310 return NULL;
313 struct got_tag_object *
314 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
316 return (struct got_tag_object *)got_object_cache_get(
317 &repo->tagcache, id);
320 const struct got_error *
321 open_repo(struct got_repository *repo, const char *path)
323 const struct got_error *err = NULL;
325 /* bare git repository? */
326 repo->path_git_dir = strdup(path);
327 if (repo->path_git_dir == NULL)
328 return got_error_from_errno("strdup");
329 if (is_git_repo(repo)) {
330 repo->path = strdup(repo->path_git_dir);
331 if (repo->path == NULL) {
332 err = got_error_from_errno("strdup");
333 goto done;
335 return NULL;
338 /* git repository with working tree? */
339 free(repo->path_git_dir);
340 repo->path_git_dir = NULL;
341 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
342 err = got_error_from_errno("asprintf");
343 goto done;
345 if (is_git_repo(repo)) {
346 repo->path = strdup(path);
347 if (repo->path == NULL) {
348 err = got_error_from_errno("strdup");
349 goto done;
351 return NULL;
354 err = got_error(GOT_ERR_NOT_GIT_REPO);
355 done:
356 if (err) {
357 free(repo->path);
358 repo->path = NULL;
359 free(repo->path_git_dir);
360 repo->path_git_dir = NULL;
362 return err;
365 static const struct got_error *
366 parse_gitconfig_file(int *gitconfig_repository_format_version,
367 char **gitconfig_author_name, char **gitconfig_author_email,
368 const char *gitconfig_path)
370 const struct got_error *err = NULL, *child_err = NULL;
371 int fd = -1;
372 int imsg_fds[2] = { -1, -1 };
373 pid_t pid;
374 struct imsgbuf *ibuf;
376 *gitconfig_repository_format_version = 0;
377 *gitconfig_author_name = NULL;
378 *gitconfig_author_email = NULL;
380 fd = open(gitconfig_path, O_RDONLY);
381 if (fd == -1) {
382 if (errno == ENOENT)
383 return NULL;
384 return got_error_from_errno2("open", gitconfig_path);
387 ibuf = calloc(1, sizeof(*ibuf));
388 if (ibuf == NULL) {
389 err = got_error_from_errno("calloc");
390 goto done;
393 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
394 err = got_error_from_errno("socketpair");
395 goto done;
398 pid = fork();
399 if (pid == -1) {
400 err = got_error_from_errno("fork");
401 goto done;
402 } else if (pid == 0) {
403 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
404 gitconfig_path);
405 /* not reached */
408 if (close(imsg_fds[1]) == -1) {
409 err = got_error_from_errno("close");
410 goto done;
412 imsg_fds[1] = -1;
413 imsg_init(ibuf, imsg_fds[0]);
415 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
416 if (err)
417 goto done;
418 fd = -1;
420 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
421 if (err)
422 goto done;
424 err = got_privsep_recv_gitconfig_int(
425 gitconfig_repository_format_version, ibuf);
426 if (err)
427 goto done;
429 err = got_privsep_send_gitconfig_author_name_req(ibuf);
430 if (err)
431 goto done;
433 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
434 if (err)
435 goto done;
437 err = got_privsep_send_gitconfig_author_email_req(ibuf);
438 if (err)
439 goto done;
441 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
442 if (err)
443 goto done;
445 imsg_clear(ibuf);
446 err = got_privsep_send_stop(imsg_fds[0]);
447 child_err = got_privsep_wait_for_child(pid);
448 if (child_err && err == NULL)
449 err = child_err;
450 done:
451 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
452 err = got_error_from_errno("close");
453 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
454 err = got_error_from_errno("close");
455 if (fd != -1 && close(fd) == -1 && err == NULL)
456 err = got_error_from_errno2("close", gitconfig_path);
457 free(ibuf);
458 return err;
461 static const struct got_error *
462 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
464 const struct got_error *err = NULL;
465 char *repo_gitconfig_path = NULL;
467 if (global_gitconfig_path) {
468 /* Read settings from ~/.gitconfig. */
469 int dummy_repo_version;
470 err = parse_gitconfig_file(&dummy_repo_version,
471 &repo->global_gitconfig_author_name,
472 &repo->global_gitconfig_author_email,
473 global_gitconfig_path);
474 if (err)
475 return err;
478 /* Read repository's .git/config file. */
479 err = get_path_gitconfig(&repo_gitconfig_path, repo);
480 if (err)
481 return err;
483 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
484 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
485 repo_gitconfig_path);
486 if (err)
487 goto done;
488 done:
489 free(repo_gitconfig_path);
490 return err;
493 const struct got_error *
494 got_repo_open(struct got_repository **repop, const char *path,
495 const char *global_gitconfig_path)
497 struct got_repository *repo = NULL;
498 const struct got_error *err = NULL;
499 char *abspath;
500 int i, tried_root = 0;
502 *repop = NULL;
504 if (got_path_is_absolute(path))
505 abspath = strdup(path);
506 else
507 abspath = got_path_get_absolute(path);
508 if (abspath == NULL)
509 return got_error(GOT_ERR_BAD_PATH);
511 repo = calloc(1, sizeof(*repo));
512 if (repo == NULL) {
513 err = got_error_from_errno("calloc");
514 goto done;
517 for (i = 0; i < nitems(repo->privsep_children); i++) {
518 memset(&repo->privsep_children[i], 0,
519 sizeof(repo->privsep_children[0]));
520 repo->privsep_children[i].imsg_fd = -1;
523 err = got_object_cache_init(&repo->objcache,
524 GOT_OBJECT_CACHE_TYPE_OBJ);
525 if (err)
526 goto done;
527 err = got_object_cache_init(&repo->treecache,
528 GOT_OBJECT_CACHE_TYPE_TREE);
529 if (err)
530 goto done;
531 err = got_object_cache_init(&repo->commitcache,
532 GOT_OBJECT_CACHE_TYPE_COMMIT);
533 if (err)
534 goto done;
535 err = got_object_cache_init(&repo->tagcache,
536 GOT_OBJECT_CACHE_TYPE_TAG);
537 if (err)
538 goto done;
540 path = realpath(abspath, NULL);
541 if (path == NULL) {
542 err = got_error_from_errno2("realpath", abspath);
543 goto done;
546 do {
547 err = open_repo(repo, path);
548 if (err == NULL)
549 break;
550 if (err->code != GOT_ERR_NOT_GIT_REPO)
551 break;
552 if (path[0] == '/' && path[1] == '\0') {
553 if (tried_root) {
554 err = got_error(GOT_ERR_NOT_GIT_REPO);
555 goto done;
557 tried_root = 1;
559 path = dirname(path);
560 if (path == NULL) {
561 err = got_error_from_errno2("dirname", path);
562 goto done;
564 } while (path);
566 err = read_gitconfig(repo, global_gitconfig_path);
567 if (err)
568 goto done;
569 if (repo->gitconfig_repository_format_version != 0)
570 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
571 done:
572 if (err)
573 got_repo_close(repo);
574 else
575 *repop = repo;
576 free(abspath);
577 return err;
580 const struct got_error *
581 got_repo_close(struct got_repository *repo)
583 const struct got_error *err = NULL, *child_err;
584 int i;
586 for (i = 0; i < nitems(repo->packidx_cache); i++) {
587 if (repo->packidx_cache[i] == NULL)
588 break;
589 got_packidx_close(repo->packidx_cache[i]);
592 for (i = 0; i < nitems(repo->packs); i++) {
593 if (repo->packs[i].path_packfile == NULL)
594 break;
595 got_pack_close(&repo->packs[i]);
598 free(repo->path);
599 free(repo->path_git_dir);
601 got_object_cache_close(&repo->objcache);
602 got_object_cache_close(&repo->treecache);
603 got_object_cache_close(&repo->commitcache);
604 got_object_cache_close(&repo->tagcache);
606 for (i = 0; i < nitems(repo->privsep_children); i++) {
607 if (repo->privsep_children[i].imsg_fd == -1)
608 continue;
609 imsg_clear(repo->privsep_children[i].ibuf);
610 free(repo->privsep_children[i].ibuf);
611 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
612 child_err = got_privsep_wait_for_child(
613 repo->privsep_children[i].pid);
614 if (child_err && err == NULL)
615 err = child_err;
616 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
617 err == NULL)
618 err = got_error_from_errno("close");
621 free(repo->gitconfig_author_name);
622 free(repo->gitconfig_author_email);
623 free(repo);
625 return err;
628 const struct got_error *
629 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
630 const char *input_path, int check_disk)
632 const struct got_error *err = NULL;
633 const char *repo_abspath = NULL;
634 size_t repolen, cwdlen, len;
635 char *cwd, *canonpath, *path = NULL;
637 *in_repo_path = NULL;
639 cwd = getcwd(NULL, 0);
640 if (cwd == NULL)
641 return got_error_from_errno("getcwd");
643 canonpath = strdup(input_path);
644 if (canonpath == NULL) {
645 err = got_error_from_errno("strdup");
646 goto done;
648 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
649 if (err)
650 goto done;
652 repo_abspath = got_repo_get_path(repo);
654 if (!check_disk || canonpath[0] == '\0') {
655 path = strdup(canonpath);
656 if (path == NULL) {
657 err = got_error_from_errno("strdup");
658 goto done;
660 } else {
661 int is_repo_child = 0, is_cwd_child = 0;
663 path = realpath(canonpath, NULL);
664 if (path == NULL) {
665 if (errno != ENOENT) {
666 err = got_error_from_errno2("realpath",
667 canonpath);
668 goto done;
670 /*
671 * Path is not on disk.
672 * Assume it is already relative to repository root.
673 */
674 path = strdup(canonpath);
675 if (path == NULL) {
676 err = got_error_from_errno("strdup");
677 goto done;
681 repolen = strlen(repo_abspath);
682 cwdlen = strlen(cwd);
683 len = strlen(path);
685 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
686 is_repo_child = 1;
687 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
688 is_cwd_child = 1;
690 if (strcmp(path, repo_abspath) == 0) {
691 free(path);
692 path = strdup("");
693 if (path == NULL) {
694 err = got_error_from_errno("strdup");
695 goto done;
697 } else if (is_repo_child && is_cwd_child) {
698 char *child;
699 /* Strip common prefix with repository path. */
700 err = got_path_skip_common_ancestor(&child,
701 repo_abspath, path);
702 if (err)
703 goto done;
704 free(path);
705 path = child;
706 } else if (is_repo_child) {
707 /* Matched an on-disk path inside repository. */
708 if (got_repo_is_bare(repo)) {
709 /*
710 * Matched an on-disk path inside repository
711 * database. Treat as repository-relative.
712 */
713 } else {
714 char *child;
715 /* Strip common prefix with repository path. */
716 err = got_path_skip_common_ancestor(&child,
717 repo_abspath, path);
718 if (err)
719 goto done;
720 free(path);
721 path = child;
723 } else if (is_cwd_child) {
724 char *child;
725 /* Strip common prefix with cwd. */
726 err = got_path_skip_common_ancestor(&child, cwd,
727 path);
728 if (err)
729 goto done;
730 free(path);
731 path = child;
732 } else {
733 /*
734 * Matched unrelated on-disk path.
735 * Treat it as repository-relative.
736 */
740 /* Make in-repository path absolute */
741 if (path[0] != '/') {
742 char *abspath;
743 if (asprintf(&abspath, "/%s", path) == -1) {
744 err = got_error_from_errno("asprintf");
745 goto done;
747 free(path);
748 path = abspath;
751 done:
752 free(cwd);
753 free(canonpath);
754 if (err)
755 free(path);
756 else
757 *in_repo_path = path;
758 return err;
761 const struct got_error *
762 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
764 const struct got_error *err = NULL;
765 int i;
767 for (i = 0; i < nitems(repo->packidx_cache); i++) {
768 if (repo->packidx_cache[i] == NULL)
769 break;
771 if (i == nitems(repo->packidx_cache)) {
772 err = got_packidx_close(repo->packidx_cache[i - 1]);
773 if (err)
774 return err;
777 /*
778 * Insert the new pack index at the front so it will
779 * be searched first in the future.
780 */
781 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
782 sizeof(repo->packidx_cache) -
783 sizeof(repo->packidx_cache[0]));
784 repo->packidx_cache[0] = packidx;
786 return NULL;
789 static int
790 is_packidx_filename(const char *name, size_t len)
792 if (len != GOT_PACKIDX_NAMELEN)
793 return 0;
795 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
796 return 0;
798 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
799 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
800 return 0;
802 return 1;
805 const struct got_error *
806 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
807 struct got_repository *repo, struct got_object_id *id)
809 const struct got_error *err;
810 char *path_packdir;
811 DIR *packdir;
812 struct dirent *dent;
813 char *path_packidx;
814 int i;
816 /* Search pack index cache. */
817 for (i = 0; i < nitems(repo->packidx_cache); i++) {
818 if (repo->packidx_cache[i] == NULL)
819 break;
820 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
821 if (*idx != -1) {
822 *packidx = repo->packidx_cache[i];
823 return NULL;
826 /* No luck. Search the filesystem. */
828 path_packdir = got_repo_get_path_objects_pack(repo);
829 if (path_packdir == NULL)
830 return got_error_from_errno("got_repo_get_path_objects_pack");
832 packdir = opendir(path_packdir);
833 if (packdir == NULL) {
834 if (errno == ENOENT)
835 err = got_error_no_obj(id);
836 else
837 err = got_error_from_errno2("opendir", path_packdir);
838 goto done;
841 while ((dent = readdir(packdir)) != NULL) {
842 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
843 continue;
845 if (asprintf(&path_packidx, "%s/%s", path_packdir,
846 dent->d_name) == -1) {
847 err = got_error_from_errno("asprintf");
848 goto done;
851 err = got_packidx_open(packidx, path_packidx, 0);
852 free(path_packidx);
853 if (err)
854 goto done;
856 *idx = got_packidx_get_object_idx(*packidx, id);
857 if (*idx != -1) {
858 err = NULL; /* found the object */
859 err = got_repo_cache_packidx(repo, *packidx);
860 goto done;
863 err = got_packidx_close(*packidx);
864 *packidx = NULL;
865 if (err)
866 goto done;
869 err = got_error_no_obj(id);
870 done:
871 free(path_packdir);
872 if (packdir && closedir(packdir) != 0 && err == NULL)
873 err = got_error_from_errno("closedir");
874 return err;
877 static const struct got_error *
878 read_packfile_hdr(int fd, struct got_packidx *packidx)
880 const struct got_error *err = NULL;
881 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
882 struct got_packfile_hdr hdr;
883 ssize_t n;
885 n = read(fd, &hdr, sizeof(hdr));
886 if (n < 0)
887 return got_error_from_errno("read");
888 if (n != sizeof(hdr))
889 return got_error(GOT_ERR_BAD_PACKFILE);
891 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
892 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
893 betoh32(hdr.nobjects) != totobj)
894 err = got_error(GOT_ERR_BAD_PACKFILE);
896 return err;
899 static const struct got_error *
900 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
902 const struct got_error *err = NULL;
904 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
905 if (*fd == -1)
906 return got_error_from_errno2("open", path_packfile);
908 if (packidx) {
909 err = read_packfile_hdr(*fd, packidx);
910 if (err) {
911 close(*fd);
912 *fd = -1;
916 return err;
919 const struct got_error *
920 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
921 const char *path_packfile, struct got_packidx *packidx)
923 const struct got_error *err = NULL;
924 struct got_pack *pack = NULL;
925 struct stat sb;
926 int i;
928 if (packp)
929 *packp = NULL;
931 for (i = 0; i < nitems(repo->packs); i++) {
932 pack = &repo->packs[i];
933 if (pack->path_packfile == NULL)
934 break;
935 if (strcmp(pack->path_packfile, path_packfile) == 0)
936 return NULL;
939 if (i == nitems(repo->packs) - 1) {
940 err = got_pack_close(&repo->packs[i - 1]);
941 if (err)
942 return err;
943 memmove(&repo->packs[1], &repo->packs[0],
944 sizeof(repo->packs) - sizeof(repo->packs[0]));
945 i = 0;
948 pack = &repo->packs[i];
950 pack->path_packfile = strdup(path_packfile);
951 if (pack->path_packfile == NULL) {
952 err = got_error_from_errno("strdup");
953 goto done;
956 err = open_packfile(&pack->fd, path_packfile, packidx);
957 if (err)
958 goto done;
960 if (fstat(pack->fd, &sb) != 0) {
961 err = got_error_from_errno("fstat");
962 goto done;
964 pack->filesize = sb.st_size;
966 pack->privsep_child = NULL;
968 #ifndef GOT_PACK_NO_MMAP
969 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
970 pack->fd, 0);
971 if (pack->map == MAP_FAILED) {
972 if (errno != ENOMEM) {
973 err = got_error_from_errno("mmap");
974 goto done;
976 pack->map = NULL; /* fall back to read(2) */
978 #endif
979 done:
980 if (err) {
981 if (pack) {
982 free(pack->path_packfile);
983 memset(pack, 0, sizeof(*pack));
985 } else if (packp)
986 *packp = pack;
987 return err;
990 struct got_pack *
991 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
993 struct got_pack *pack = NULL;
994 int i;
996 for (i = 0; i < nitems(repo->packs); i++) {
997 pack = &repo->packs[i];
998 if (pack->path_packfile == NULL)
999 break;
1000 if (strcmp(pack->path_packfile, path_packfile) == 0)
1001 return pack;
1004 return NULL;
1007 const struct got_error *
1008 got_repo_init(const char *repo_path)
1010 const struct got_error *err = NULL;
1011 const char *dirnames[] = {
1012 GOT_OBJECTS_DIR,
1013 GOT_OBJECTS_PACK_DIR,
1014 GOT_REFS_DIR,
1016 const char *description_str = "Unnamed repository; "
1017 "edit this file 'description' to name the repository.";
1018 const char *headref_str = "ref: refs/heads/master";
1019 const char *gitconfig_str = "[core]\n"
1020 "\trepositoryformatversion = 0\n"
1021 "\tfilemode = true\n"
1022 "\tbare = true\n";
1023 char *path;
1024 int i;
1026 if (!got_path_dir_is_empty(repo_path))
1027 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1029 for (i = 0; i < nitems(dirnames); i++) {
1030 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1031 return got_error_from_errno("asprintf");
1033 err = got_path_mkdir(path);
1034 free(path);
1035 if (err)
1036 return err;
1039 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1040 return got_error_from_errno("asprintf");
1041 err = got_path_create_file(path, description_str);
1042 free(path);
1043 if (err)
1044 return err;
1046 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1047 return got_error_from_errno("asprintf");
1048 err = got_path_create_file(path, headref_str);
1049 free(path);
1050 if (err)
1051 return err;
1053 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1054 return got_error_from_errno("asprintf");
1055 err = got_path_create_file(path, gitconfig_str);
1056 free(path);
1057 if (err)
1058 return err;
1060 return NULL;
1063 static const struct got_error *
1064 match_packed_object(struct got_object_id **unique_id,
1065 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1067 const struct got_error *err = NULL;
1068 char *path_packdir;
1069 DIR *packdir;
1070 struct dirent *dent;
1071 char *path_packidx;
1072 struct got_object_id_queue matched_ids;
1074 SIMPLEQ_INIT(&matched_ids);
1076 path_packdir = got_repo_get_path_objects_pack(repo);
1077 if (path_packdir == NULL)
1078 return got_error_from_errno("got_repo_get_path_objects_pack");
1080 packdir = opendir(path_packdir);
1081 if (packdir == NULL) {
1082 if (errno != ENOENT)
1083 err = got_error_from_errno2("opendir", path_packdir);
1084 goto done;
1087 while ((dent = readdir(packdir)) != NULL) {
1088 struct got_packidx *packidx;
1089 struct got_object_qid *qid;
1092 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1093 continue;
1095 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1096 dent->d_name) == -1) {
1097 err = got_error_from_errno("asprintf");
1098 break;
1101 err = got_packidx_open(&packidx, path_packidx, 0);
1102 free(path_packidx);
1103 if (err)
1104 break;
1106 err = got_packidx_match_id_str_prefix(&matched_ids,
1107 packidx, id_str_prefix);
1108 if (err) {
1109 got_packidx_close(packidx);
1110 break;
1112 err = got_packidx_close(packidx);
1113 if (err)
1114 break;
1116 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1117 if (obj_type != GOT_OBJ_TYPE_ANY) {
1118 int matched_type;
1119 err = got_object_get_type(&matched_type, repo,
1120 qid->id);
1121 if (err)
1122 goto done;
1123 if (matched_type != obj_type)
1124 continue;
1126 if (*unique_id == NULL) {
1127 *unique_id = got_object_id_dup(qid->id);
1128 if (*unique_id == NULL) {
1129 err = got_error_from_errno("malloc");
1130 goto done;
1132 } else {
1133 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1134 goto done;
1138 done:
1139 got_object_id_queue_free(&matched_ids);
1140 free(path_packdir);
1141 if (packdir && closedir(packdir) != 0 && err == NULL)
1142 err = got_error_from_errno("closedir");
1143 if (err) {
1144 free(*unique_id);
1145 *unique_id = NULL;
1147 return err;
1150 static const struct got_error *
1151 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1152 const char *object_dir, const char *id_str_prefix, int obj_type,
1153 struct got_repository *repo)
1155 const struct got_error *err = NULL;
1156 char *path;
1157 DIR *dir = NULL;
1158 struct dirent *dent;
1159 struct got_object_id id;
1161 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1162 err = got_error_from_errno("asprintf");
1163 goto done;
1166 dir = opendir(path);
1167 if (dir == NULL) {
1168 if (errno == ENOENT) {
1169 err = NULL;
1170 goto done;
1172 err = got_error_from_errno2("opendir", path);
1173 goto done;
1175 while ((dent = readdir(dir)) != NULL) {
1176 char *id_str;
1177 int cmp;
1179 if (strcmp(dent->d_name, ".") == 0 ||
1180 strcmp(dent->d_name, "..") == 0)
1181 continue;
1183 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1184 err = got_error_from_errno("asprintf");
1185 goto done;
1188 if (!got_parse_sha1_digest(id.sha1, id_str))
1189 continue;
1192 * Directory entries do not necessarily appear in
1193 * sorted order, so we must iterate over all of them.
1195 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1196 if (cmp != 0) {
1197 free(id_str);
1198 continue;
1201 if (*unique_id == NULL) {
1202 if (obj_type != GOT_OBJ_TYPE_ANY) {
1203 int matched_type;
1204 err = got_object_get_type(&matched_type, repo,
1205 &id);
1206 if (err)
1207 goto done;
1208 if (matched_type != obj_type)
1209 continue;
1211 *unique_id = got_object_id_dup(&id);
1212 if (*unique_id == NULL) {
1213 err = got_error_from_errno("got_object_id_dup");
1214 free(id_str);
1215 goto done;
1217 } else {
1218 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1219 free(id_str);
1220 goto done;
1223 done:
1224 if (dir && closedir(dir) != 0 && err == NULL)
1225 err = got_error_from_errno("closedir");
1226 if (err) {
1227 free(*unique_id);
1228 *unique_id = NULL;
1230 free(path);
1231 return err;
1234 const struct got_error *
1235 got_repo_match_object_id_prefix(struct got_object_id **id,
1236 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1238 const struct got_error *err = NULL;
1239 char *path_objects = got_repo_get_path_objects(repo);
1240 char *object_dir = NULL;
1241 size_t len;
1242 int i;
1244 *id = NULL;
1246 for (i = 0; i < strlen(id_str_prefix); i++) {
1247 if (isxdigit((unsigned char)id_str_prefix[i]))
1248 continue;
1249 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1252 len = strlen(id_str_prefix);
1253 if (len >= 2) {
1254 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1255 if (err)
1256 goto done;
1257 object_dir = strndup(id_str_prefix, 2);
1258 if (object_dir == NULL) {
1259 err = got_error_from_errno("strdup");
1260 goto done;
1262 err = match_loose_object(id, path_objects, object_dir,
1263 id_str_prefix, obj_type, repo);
1264 } else if (len == 1) {
1265 int i;
1266 for (i = 0; i < 0xf; i++) {
1267 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1268 == -1) {
1269 err = got_error_from_errno("asprintf");
1270 goto done;
1272 err = match_packed_object(id, repo, object_dir,
1273 obj_type);
1274 if (err)
1275 goto done;
1276 err = match_loose_object(id, path_objects, object_dir,
1277 id_str_prefix, obj_type, repo);
1278 if (err)
1279 goto done;
1281 } else {
1282 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1283 goto done;
1285 done:
1286 free(object_dir);
1287 if (err) {
1288 free(*id);
1289 *id = NULL;
1290 } else if (*id == NULL)
1291 err = got_error(GOT_ERR_NO_OBJ);
1293 return err;
1296 const struct got_error *
1297 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1298 int obj_type, struct got_repository *repo)
1300 const struct got_error *err;
1301 struct got_reflist_head refs;
1302 struct got_reflist_entry *re;
1303 struct got_object_id *tag_id;
1305 SIMPLEQ_INIT(&refs);
1306 *tag = NULL;
1308 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1309 if (err)
1310 return err;
1312 SIMPLEQ_FOREACH(re, &refs, entry) {
1313 const char *refname;
1314 refname = got_ref_get_name(re->ref);
1315 if (got_ref_is_symbolic(re->ref))
1316 continue;
1317 refname += strlen("refs/tags/");
1318 if (strcmp(refname, name) != 0)
1319 continue;
1320 err = got_ref_resolve(&tag_id, repo, re->ref);
1321 if (err)
1322 break;
1323 err = got_object_open_as_tag(tag, repo, tag_id);
1324 free(tag_id);
1325 if (err)
1326 break;
1327 if (obj_type == GOT_OBJ_TYPE_ANY ||
1328 got_object_tag_get_object_type(*tag) == obj_type)
1329 break;
1330 got_object_tag_close(*tag);
1331 *tag = NULL;
1334 got_ref_list_free(&refs);
1335 if (err == NULL && *tag == NULL)
1336 err = got_error(GOT_ERR_NO_OBJ);
1337 return err;
1340 static const struct got_error *
1341 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1342 const char *name, mode_t mode, struct got_object_id *blob_id)
1344 const struct got_error *err = NULL;
1346 *new_te = NULL;
1348 *new_te = calloc(1, sizeof(**new_te));
1349 if (*new_te == NULL)
1350 return got_error_from_errno("calloc");
1352 (*new_te)->name = strdup(name);
1353 if ((*new_te)->name == NULL) {
1354 err = got_error_from_errno("strdup");
1355 goto done;
1358 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1359 (*new_te)->id = blob_id;
1360 done:
1361 if (err && *new_te) {
1362 got_object_tree_entry_close(*new_te);
1363 *new_te = NULL;
1365 return err;
1368 static const struct got_error *
1369 import_file(struct got_tree_entry **new_te, struct dirent *de,
1370 const char *path, struct got_repository *repo)
1372 const struct got_error *err;
1373 struct got_object_id *blob_id = NULL;
1374 char *filepath;
1375 struct stat sb;
1377 if (asprintf(&filepath, "%s%s%s", path,
1378 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1379 return got_error_from_errno("asprintf");
1381 if (lstat(filepath, &sb) != 0) {
1382 err = got_error_from_errno2("lstat", path);
1383 goto done;
1386 err = got_object_blob_create(&blob_id, filepath, repo);
1387 if (err)
1388 goto done;
1390 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1391 blob_id);
1392 done:
1393 free(filepath);
1394 if (err)
1395 free(blob_id);
1396 return err;
1399 static const struct got_error *
1400 insert_tree_entry(struct got_tree_entry *new_te,
1401 struct got_pathlist_head *paths)
1403 const struct got_error *err = NULL;
1404 struct got_pathlist_entry *new_pe;
1406 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1407 if (err)
1408 return err;
1409 if (new_pe == NULL)
1410 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1411 return NULL;
1414 static const struct got_error *write_tree(struct got_object_id **,
1415 const char *, struct got_pathlist_head *, struct got_repository *,
1416 got_repo_import_cb progress_cb, void *progress_arg);
1418 static const struct got_error *
1419 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1420 const char *path, struct got_pathlist_head *ignores,
1421 struct got_repository *repo,
1422 got_repo_import_cb progress_cb, void *progress_arg)
1424 const struct got_error *err;
1425 char *subdirpath;
1427 if (asprintf(&subdirpath, "%s%s%s", path,
1428 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1429 return got_error_from_errno("asprintf");
1431 (*new_te) = calloc(1, sizeof(**new_te));
1432 if (*new_te == NULL)
1433 return got_error_from_errno("calloc");
1434 (*new_te)->mode = S_IFDIR;
1435 (*new_te)->name = strdup(de->d_name);
1436 if ((*new_te)->name == NULL) {
1437 err = got_error_from_errno("strdup");
1438 goto done;
1441 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1442 progress_cb, progress_arg);
1443 done:
1444 free(subdirpath);
1445 if (err) {
1446 got_object_tree_entry_close(*new_te);
1447 *new_te = NULL;
1449 return err;
1452 static const struct got_error *
1453 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1454 struct got_pathlist_head *ignores, struct got_repository *repo,
1455 got_repo_import_cb progress_cb, void *progress_arg)
1457 const struct got_error *err = NULL;
1458 DIR *dir;
1459 struct dirent *de;
1460 struct got_tree_entries new_tree_entries;
1461 struct got_tree_entry *new_te = NULL;
1462 struct got_pathlist_head paths;
1463 struct got_pathlist_entry *pe;
1465 *new_tree_id = NULL;
1467 TAILQ_INIT(&paths);
1468 new_tree_entries.nentries = 0;
1469 SIMPLEQ_INIT(&new_tree_entries.head);
1471 dir = opendir(path_dir);
1472 if (dir == NULL) {
1473 err = got_error_from_errno2("opendir", path_dir);
1474 goto done;
1477 while ((de = readdir(dir)) != NULL) {
1478 int ignore = 0;
1480 if (strcmp(de->d_name, ".") == 0 ||
1481 strcmp(de->d_name, "..") == 0)
1482 continue;
1484 TAILQ_FOREACH(pe, ignores, entry) {
1485 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1486 ignore = 1;
1487 break;
1490 if (ignore)
1491 continue;
1492 if (de->d_type == DT_DIR) {
1493 err = import_subdir(&new_te, de, path_dir,
1494 ignores, repo, progress_cb, progress_arg);
1495 if (err) {
1496 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1497 goto done;
1498 err = NULL;
1499 continue;
1501 } else if (de->d_type == DT_REG) {
1502 err = import_file(&new_te, de, path_dir, repo);
1503 if (err)
1504 goto done;
1505 } else
1506 continue;
1508 err = insert_tree_entry(new_te, &paths);
1509 if (err)
1510 goto done;
1513 if (TAILQ_EMPTY(&paths)) {
1514 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1515 goto done;
1518 TAILQ_FOREACH(pe, &paths, entry) {
1519 struct got_tree_entry *te = pe->data;
1520 char *path;
1521 new_tree_entries.nentries++;
1522 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1523 if (!S_ISREG(te->mode))
1524 continue;
1525 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1526 err = got_error_from_errno("asprintf");
1527 goto done;
1529 err = (*progress_cb)(progress_arg, path);
1530 free(path);
1531 if (err)
1532 goto done;
1535 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1536 done:
1537 if (dir)
1538 closedir(dir);
1539 got_object_tree_entries_close(&new_tree_entries);
1540 got_pathlist_free(&paths);
1541 return err;
1544 const struct got_error *
1545 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1546 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1547 struct got_repository *repo, got_repo_import_cb progress_cb,
1548 void *progress_arg)
1550 const struct got_error *err;
1551 struct got_object_id *new_tree_id;
1553 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1554 progress_cb, progress_arg);
1555 if (err)
1556 return err;
1558 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1559 author, time(NULL), author, time(NULL), logmsg, repo);
1560 free(new_tree_id);
1561 return err;