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/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_delta_cache.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_sha1.h"
64 #include "got_lib_object_cache.h"
65 #include "got_lib_repository.h"
66 #include "got_lib_gotconfig.h"
68 #ifndef nitems
69 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
70 #endif
72 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
74 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
75 got_packidx_bloom_filter_cmp);
77 const char *
78 got_repo_get_path(struct got_repository *repo)
79 {
80 return repo->path;
81 }
83 const char *
84 got_repo_get_path_git_dir(struct got_repository *repo)
85 {
86 return repo->path_git_dir;
87 }
89 int
90 got_repo_get_fd(struct got_repository *repo)
91 {
92 return repo->gitdir_fd;
93 }
95 const char *
96 got_repo_get_gitconfig_author_name(struct got_repository *repo)
97 {
98 return repo->gitconfig_author_name;
99 }
101 const char *
102 got_repo_get_gitconfig_author_email(struct got_repository *repo)
104 return repo->gitconfig_author_email;
107 const char *
108 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
110 return repo->global_gitconfig_author_name;
113 const char *
114 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
116 return repo->global_gitconfig_author_email;
119 const char *
120 got_repo_get_gitconfig_owner(struct got_repository *repo)
122 return repo->gitconfig_owner;
125 void
126 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
127 struct got_repository *repo)
129 *extensions = repo->extensions;
130 *nextensions = repo->nextensions;
133 int
134 got_repo_is_bare(struct got_repository *repo)
136 return (strcmp(repo->path, repo->path_git_dir) == 0);
139 static char *
140 get_path_git_child(struct got_repository *repo, const char *basename)
142 char *path_child;
144 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
145 basename) == -1)
146 return NULL;
148 return path_child;
151 char *
152 got_repo_get_path_objects(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_DIR);
157 char *
158 got_repo_get_path_objects_pack(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
163 char *
164 got_repo_get_path_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_REFS_DIR);
169 char *
170 got_repo_get_path_packed_refs(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
175 static char *
176 get_path_head(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_HEAD_FILE);
181 char *
182 got_repo_get_path_gitconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GITCONFIG);
187 char *
188 got_repo_get_path_gotconfig(struct got_repository *repo)
190 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
193 const struct got_gotconfig *
194 got_repo_get_gotconfig(struct got_repository *repo)
196 return repo->gotconfig;
199 void
200 got_repo_get_gitconfig_remotes(int *nremotes,
201 const struct got_remote_repo **remotes, struct got_repository *repo)
203 *nremotes = repo->ngitconfig_remotes;
204 *remotes = repo->gitconfig_remotes;
207 static int
208 is_git_repo(struct got_repository *repo)
210 const char *path_git = got_repo_get_path_git_dir(repo);
211 char *path_objects = got_repo_get_path_objects(repo);
212 char *path_refs = got_repo_get_path_refs(repo);
213 char *path_head = get_path_head(repo);
214 int ret = 0;
215 struct stat sb;
216 struct got_reference *head_ref;
218 if (lstat(path_git, &sb) == -1)
219 goto done;
220 if (!S_ISDIR(sb.st_mode))
221 goto done;
223 if (lstat(path_objects, &sb) == -1)
224 goto done;
225 if (!S_ISDIR(sb.st_mode))
226 goto done;
228 if (lstat(path_refs, &sb) == -1)
229 goto done;
230 if (!S_ISDIR(sb.st_mode))
231 goto done;
233 if (lstat(path_head, &sb) == -1)
234 goto done;
235 if (!S_ISREG(sb.st_mode))
236 goto done;
238 /* Check if the HEAD reference can be opened. */
239 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
240 goto done;
241 got_ref_close(head_ref);
243 ret = 1;
244 done:
245 free(path_objects);
246 free(path_refs);
247 free(path_head);
248 return ret;
252 static const struct got_error *
253 close_tempfiles(int *fds, size_t nfds)
255 const struct got_error *err = NULL;
256 int i;
258 for (i = 0; i < nfds; i++) {
259 if (fds[i] == -1)
260 continue;
261 if (close(fds[i]) == -1) {
262 err = got_error_from_errno("close");
263 break;
266 free(fds);
267 return err;
270 static const struct got_error *
271 open_tempfiles(int **fds, size_t array_size, size_t nfds)
273 const struct got_error *err = NULL;
274 int i;
276 *fds = calloc(array_size, sizeof(**fds));
277 if (*fds == NULL)
278 return got_error_from_errno("calloc");
280 for (i = 0; i < array_size; i++)
281 (*fds)[i] = -1;
283 for (i = 0; i < nfds; i++) {
284 (*fds)[i] = got_opentempfd();
285 if ((*fds)[i] == -1) {
286 err = got_error_from_errno("got_opentempfd");
287 close_tempfiles(*fds, nfds);
288 *fds = NULL;
289 return err;
293 return NULL;
296 static const struct got_error *
297 get_pack_cache_size(int *pack_cache_size)
299 struct rlimit rl;
301 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
302 return got_error_from_errno("getrlimit");
304 *pack_cache_size = GOT_PACK_CACHE_SIZE;
305 if (*pack_cache_size > rl.rlim_cur / 8)
306 *pack_cache_size = rl.rlim_cur / 8;
308 return NULL;
311 const struct got_error *
312 got_repo_pack_fds_open(int **pack_fds)
314 const struct got_error *err;
315 int nfds;
317 err = get_pack_cache_size(&nfds);
318 if (err)
319 return err;
321 /*
322 * We need one basefd and one accumfd per cached pack.
323 * Our constants should be set up in a way such that
324 * this error never triggers.
325 */
326 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
327 return got_error(GOT_ERR_NO_SPACE);
329 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
332 const struct got_error *
333 got_repo_pack_fds_close(int *pack_fds)
335 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
338 const struct got_error *
339 got_repo_temp_fds_open(int **temp_fds)
341 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
342 GOT_REPO_NUM_TEMPFILES);
345 void
346 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
348 int i;
350 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
351 repo->tempfiles[i] = temp_fds[i];
354 const struct got_error *
355 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
357 int i;
359 *fd = -1;
360 *idx = -1;
362 for (i = 0; i < nitems(repo->tempfiles); i++) {
363 if (repo->tempfile_use_mask & (1 << i))
364 continue;
365 if (repo->tempfiles[i] != -1) {
366 if (ftruncate(repo->tempfiles[i], 0L) == -1)
367 return got_error_from_errno("ftruncate");
368 *fd = repo->tempfiles[i];
369 *idx = i;
370 repo->tempfile_use_mask |= (1 << i);
371 return NULL;
375 return got_error(GOT_ERR_REPO_TEMPFILE);
378 void
379 got_repo_temp_fds_put(int idx, struct got_repository *repo)
381 repo->tempfile_use_mask &= ~(1 << idx);
384 const struct got_error *
385 got_repo_temp_fds_close(int *temp_fds)
387 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
390 const struct got_error *
391 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
392 struct got_object *obj)
394 #ifndef GOT_NO_OBJ_CACHE
395 const struct got_error *err = NULL;
396 err = got_object_cache_add(&repo->objcache, id, obj);
397 if (err) {
398 if (err->code == GOT_ERR_OBJ_EXISTS ||
399 err->code == GOT_ERR_OBJ_TOO_LARGE)
400 err = NULL;
401 return err;
403 obj->refcnt++;
404 #endif
405 return NULL;
408 struct got_object *
409 got_repo_get_cached_object(struct got_repository *repo,
410 struct got_object_id *id)
412 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
415 const struct got_error *
416 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
417 struct got_tree_object *tree)
419 #ifndef GOT_NO_OBJ_CACHE
420 const struct got_error *err = NULL;
421 err = got_object_cache_add(&repo->treecache, id, tree);
422 if (err) {
423 if (err->code == GOT_ERR_OBJ_EXISTS ||
424 err->code == GOT_ERR_OBJ_TOO_LARGE)
425 err = NULL;
426 return err;
428 tree->refcnt++;
429 #endif
430 return NULL;
433 struct got_tree_object *
434 got_repo_get_cached_tree(struct got_repository *repo,
435 struct got_object_id *id)
437 return (struct got_tree_object *)got_object_cache_get(
438 &repo->treecache, id);
441 const struct got_error *
442 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
443 struct got_commit_object *commit)
445 #ifndef GOT_NO_OBJ_CACHE
446 const struct got_error *err = NULL;
447 err = got_object_cache_add(&repo->commitcache, id, commit);
448 if (err) {
449 if (err->code == GOT_ERR_OBJ_EXISTS ||
450 err->code == GOT_ERR_OBJ_TOO_LARGE)
451 err = NULL;
452 return err;
454 commit->refcnt++;
455 #endif
456 return NULL;
459 struct got_commit_object *
460 got_repo_get_cached_commit(struct got_repository *repo,
461 struct got_object_id *id)
463 return (struct got_commit_object *)got_object_cache_get(
464 &repo->commitcache, id);
467 const struct got_error *
468 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
469 struct got_tag_object *tag)
471 #ifndef GOT_NO_OBJ_CACHE
472 const struct got_error *err = NULL;
473 err = got_object_cache_add(&repo->tagcache, id, tag);
474 if (err) {
475 if (err->code == GOT_ERR_OBJ_EXISTS ||
476 err->code == GOT_ERR_OBJ_TOO_LARGE)
477 err = NULL;
478 return err;
480 tag->refcnt++;
481 #endif
482 return NULL;
485 struct got_tag_object *
486 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
488 return (struct got_tag_object *)got_object_cache_get(
489 &repo->tagcache, id);
492 const struct got_error *
493 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
494 struct got_raw_object *raw)
496 #ifndef GOT_NO_OBJ_CACHE
497 const struct got_error *err = NULL;
498 err = got_object_cache_add(&repo->rawcache, id, raw);
499 if (err) {
500 if (err->code == GOT_ERR_OBJ_EXISTS ||
501 err->code == GOT_ERR_OBJ_TOO_LARGE)
502 err = NULL;
503 return err;
505 raw->refcnt++;
506 #endif
507 return NULL;
511 struct got_raw_object *
512 got_repo_get_cached_raw_object(struct got_repository *repo,
513 struct got_object_id *id)
515 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
519 static const struct got_error *
520 open_repo(struct got_repository *repo, const char *path)
522 const struct got_error *err = NULL;
524 repo->gitdir_fd = -1;
526 /* bare git repository? */
527 repo->path_git_dir = strdup(path);
528 if (repo->path_git_dir == NULL)
529 return got_error_from_errno("strdup");
530 if (is_git_repo(repo)) {
531 repo->path = strdup(repo->path_git_dir);
532 if (repo->path == NULL) {
533 err = got_error_from_errno("strdup");
534 goto done;
536 repo->gitdir_fd = open(repo->path_git_dir,
537 O_DIRECTORY | O_CLOEXEC);
538 if (repo->gitdir_fd == -1) {
539 err = got_error_from_errno2("open",
540 repo->path_git_dir);
541 goto done;
543 return NULL;
546 /* git repository with working tree? */
547 free(repo->path_git_dir);
548 repo->path_git_dir = NULL;
549 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
550 err = got_error_from_errno("asprintf");
551 goto done;
553 if (is_git_repo(repo)) {
554 repo->path = strdup(path);
555 if (repo->path == NULL) {
556 err = got_error_from_errno("strdup");
557 goto done;
559 repo->gitdir_fd = open(repo->path_git_dir,
560 O_DIRECTORY | O_CLOEXEC);
561 if (repo->gitdir_fd == -1) {
562 err = got_error_from_errno2("open",
563 repo->path_git_dir);
564 goto done;
566 return NULL;
569 err = got_error(GOT_ERR_NOT_GIT_REPO);
570 done:
571 if (err) {
572 free(repo->path);
573 repo->path = NULL;
574 free(repo->path_git_dir);
575 repo->path_git_dir = NULL;
576 if (repo->gitdir_fd != -1)
577 close(repo->gitdir_fd);
578 repo->gitdir_fd = -1;
581 return err;
584 static const struct got_error *
585 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
587 const struct got_error *err = NULL;
588 char *repo_gitconfig_path = NULL;
590 if (global_gitconfig_path) {
591 /* Read settings from ~/.gitconfig. */
592 int dummy_repo_version;
593 err = got_repo_read_gitconfig(&dummy_repo_version,
594 &repo->global_gitconfig_author_name,
595 &repo->global_gitconfig_author_email,
596 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
597 if (err)
598 return err;
601 /* Read repository's .git/config file. */
602 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
603 if (repo_gitconfig_path == NULL)
604 return got_error_from_errno("got_repo_get_path_gitconfig");
606 err = got_repo_read_gitconfig(
607 &repo->gitconfig_repository_format_version,
608 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
609 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
610 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
611 repo_gitconfig_path);
612 if (err)
613 goto done;
615 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
616 int i;
618 for (i = 0; i < repo->ngitconfig_remotes; i++) {
619 got_repo_free_remote_repo_data(
620 &repo->gitconfig_remotes[i]);
622 free(repo->gitconfig_remotes);
623 repo->gitconfig_remotes = NULL;
624 repo->ngitconfig_remotes = 0;
626 free(repo->gitconfig_author_name);
627 repo->gitconfig_author_name = NULL;
628 free(repo->gitconfig_author_email);
629 repo->gitconfig_author_email = NULL;
631 free(repo->global_gitconfig_author_name);
632 repo->global_gitconfig_author_name = NULL;
633 free(repo->global_gitconfig_author_email);
634 repo->global_gitconfig_author_email = NULL;
637 done:
638 free(repo_gitconfig_path);
639 return err;
642 static const struct got_error *
643 read_gotconfig(struct got_repository *repo)
645 const struct got_error *err = NULL;
646 char *gotconfig_path;
648 gotconfig_path = got_repo_get_path_gotconfig(repo);
649 if (gotconfig_path == NULL)
650 return got_error_from_errno("got_repo_get_path_gotconfig");
652 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
653 free(gotconfig_path);
654 return err;
657 /* Supported repository format extensions. */
658 static const char *const repo_extensions[] = {
659 "noop", /* Got supports repository format version 1. */
660 "preciousObjects", /* Supported by gotadmin cleanup. */
661 "worktreeConfig", /* Got does not care about Git work trees. */
662 };
664 const struct got_error *
665 got_repo_open(struct got_repository **repop, const char *path,
666 const char *global_gitconfig_path, int *pack_fds)
668 struct got_repository *repo = NULL;
669 const struct got_error *err = NULL;
670 char *repo_path = NULL;
671 size_t i, j = 0;
673 *repop = NULL;
675 repo = calloc(1, sizeof(*repo));
676 if (repo == NULL)
677 return got_error_from_errno("calloc");
679 RB_INIT(&repo->packidx_bloom_filters);
680 TAILQ_INIT(&repo->packidx_paths);
682 for (i = 0; i < nitems(repo->privsep_children); i++) {
683 memset(&repo->privsep_children[i], 0,
684 sizeof(repo->privsep_children[0]));
685 repo->privsep_children[i].imsg_fd = -1;
688 err = got_object_cache_init(&repo->objcache,
689 GOT_OBJECT_CACHE_TYPE_OBJ);
690 if (err)
691 goto done;
692 err = got_object_cache_init(&repo->treecache,
693 GOT_OBJECT_CACHE_TYPE_TREE);
694 if (err)
695 goto done;
696 err = got_object_cache_init(&repo->commitcache,
697 GOT_OBJECT_CACHE_TYPE_COMMIT);
698 if (err)
699 goto done;
700 err = got_object_cache_init(&repo->tagcache,
701 GOT_OBJECT_CACHE_TYPE_TAG);
702 if (err)
703 goto done;
704 err = got_object_cache_init(&repo->rawcache,
705 GOT_OBJECT_CACHE_TYPE_RAW);
706 if (err)
707 goto done;
709 err = get_pack_cache_size(&repo->pack_cache_size);
710 if (err)
711 goto done;
712 for (i = 0; i < nitems(repo->packs); i++) {
713 if (pack_fds != NULL && i < repo->pack_cache_size) {
714 repo->packs[i].basefd = pack_fds[j++];
715 repo->packs[i].accumfd = pack_fds[j++];
716 } else {
717 repo->packs[i].basefd = -1;
718 repo->packs[i].accumfd = -1;
721 for (i = 0; i < nitems(repo->tempfiles); i++)
722 repo->tempfiles[i] = -1;
723 repo->pinned_pack = -1;
724 repo->pinned_packidx = -1;
725 repo->pinned_pid = 0;
727 repo_path = realpath(path, NULL);
728 if (repo_path == NULL) {
729 err = got_error_from_errno2("realpath", path);
730 goto done;
733 for (;;) {
734 char *parent_path;
736 err = open_repo(repo, repo_path);
737 if (err == NULL)
738 break;
739 if (err->code != GOT_ERR_NOT_GIT_REPO)
740 goto done;
741 if (repo_path[0] == '/' && repo_path[1] == '\0') {
742 err = got_error(GOT_ERR_NOT_GIT_REPO);
743 goto done;
745 err = got_path_dirname(&parent_path, repo_path);
746 if (err)
747 goto done;
748 free(repo_path);
749 repo_path = parent_path;
752 err = read_gotconfig(repo);
753 if (err)
754 goto done;
756 err = read_gitconfig(repo, global_gitconfig_path);
757 if (err)
758 goto done;
759 if (repo->gitconfig_repository_format_version != 0) {
760 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
761 goto done;
763 for (i = 0; i < repo->nextensions; i++) {
764 char *ext = repo->extensions[i];
765 int j, supported = 0;
766 for (j = 0; j < nitems(repo_extensions); j++) {
767 if (strcmp(ext, repo_extensions[j]) == 0) {
768 supported = 1;
769 break;
772 if (!supported) {
773 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
774 goto done;
778 err = got_repo_list_packidx(&repo->packidx_paths, repo);
779 done:
780 if (err)
781 got_repo_close(repo);
782 else
783 *repop = repo;
784 free(repo_path);
785 return err;
788 const struct got_error *
789 got_repo_close(struct got_repository *repo)
791 const struct got_error *err = NULL, *child_err;
792 struct got_packidx_bloom_filter *bf;
793 struct got_pathlist_entry *pe;
794 size_t i;
796 for (i = 0; i < repo->pack_cache_size; i++) {
797 if (repo->packidx_cache[i] == NULL)
798 break;
799 got_packidx_close(repo->packidx_cache[i]);
802 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
803 &repo->packidx_bloom_filters))) {
804 RB_REMOVE(got_packidx_bloom_filter_tree,
805 &repo->packidx_bloom_filters, bf);
806 bloom_free(bf->bloom);
807 free(bf->bloom);
808 free(bf);
811 for (i = 0; i < repo->pack_cache_size; i++)
812 if (repo->packs[i].path_packfile)
813 if (repo->packs[i].path_packfile)
814 got_pack_close(&repo->packs[i]);
816 free(repo->path);
817 free(repo->path_git_dir);
819 got_object_cache_close(&repo->objcache);
820 got_object_cache_close(&repo->treecache);
821 got_object_cache_close(&repo->commitcache);
822 got_object_cache_close(&repo->tagcache);
823 got_object_cache_close(&repo->rawcache);
825 for (i = 0; i < nitems(repo->privsep_children); i++) {
826 if (repo->privsep_children[i].imsg_fd == -1)
827 continue;
828 imsg_clear(repo->privsep_children[i].ibuf);
829 free(repo->privsep_children[i].ibuf);
830 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
831 child_err = got_privsep_wait_for_child(
832 repo->privsep_children[i].pid);
833 if (child_err && err == NULL)
834 err = child_err;
835 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
836 err == NULL)
837 err = got_error_from_errno("close");
840 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
841 err == NULL)
842 err = got_error_from_errno("close");
844 if (repo->gotconfig)
845 got_gotconfig_free(repo->gotconfig);
846 free(repo->gitconfig_author_name);
847 free(repo->gitconfig_author_email);
848 for (i = 0; i < repo->ngitconfig_remotes; i++)
849 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
850 free(repo->gitconfig_remotes);
851 for (i = 0; i < repo->nextensions; i++)
852 free(repo->extensions[i]);
853 free(repo->extensions);
855 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
856 free((void *)pe->path);
857 got_pathlist_free(&repo->packidx_paths);
858 free(repo);
860 return err;
863 void
864 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
866 int i;
868 free(repo->name);
869 repo->name = NULL;
870 free(repo->fetch_url);
871 repo->fetch_url = NULL;
872 free(repo->send_url);
873 repo->send_url = NULL;
874 for (i = 0; i < repo->nfetch_branches; i++)
875 free(repo->fetch_branches[i]);
876 free(repo->fetch_branches);
877 repo->fetch_branches = NULL;
878 repo->nfetch_branches = 0;
879 for (i = 0; i < repo->nsend_branches; i++)
880 free(repo->send_branches[i]);
881 free(repo->send_branches);
882 repo->send_branches = NULL;
883 repo->nsend_branches = 0;
886 const struct got_error *
887 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
888 const char *input_path)
890 const struct got_error *err = NULL;
891 const char *repo_abspath = NULL;
892 size_t repolen, len;
893 char *canonpath, *path = NULL;
895 *in_repo_path = NULL;
897 canonpath = strdup(input_path);
898 if (canonpath == NULL) {
899 err = got_error_from_errno("strdup");
900 goto done;
902 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
903 if (err)
904 goto done;
906 repo_abspath = got_repo_get_path(repo);
908 if (canonpath[0] == '\0') {
909 path = strdup(canonpath);
910 if (path == NULL) {
911 err = got_error_from_errno("strdup");
912 goto done;
914 } else {
915 path = realpath(canonpath, NULL);
916 if (path == NULL) {
917 if (errno != ENOENT) {
918 err = got_error_from_errno2("realpath",
919 canonpath);
920 goto done;
922 /*
923 * Path is not on disk.
924 * Assume it is already relative to repository root.
925 */
926 path = strdup(canonpath);
927 if (path == NULL) {
928 err = got_error_from_errno("strdup");
929 goto done;
933 repolen = strlen(repo_abspath);
934 len = strlen(path);
937 if (strcmp(path, repo_abspath) == 0) {
938 free(path);
939 path = strdup("");
940 if (path == NULL) {
941 err = got_error_from_errno("strdup");
942 goto done;
944 } else if (len > repolen &&
945 got_path_is_child(path, repo_abspath, repolen)) {
946 /* Matched an on-disk path inside repository. */
947 if (got_repo_is_bare(repo)) {
948 /*
949 * Matched an on-disk path inside repository
950 * database. Treat input as repository-relative.
951 */
952 free(path);
953 path = canonpath;
954 canonpath = NULL;
955 } else {
956 char *child;
957 /* Strip common prefix with repository path. */
958 err = got_path_skip_common_ancestor(&child,
959 repo_abspath, path);
960 if (err)
961 goto done;
962 free(path);
963 path = child;
965 } else {
966 /*
967 * Matched unrelated on-disk path.
968 * Treat input as repository-relative.
969 */
970 free(path);
971 path = canonpath;
972 canonpath = NULL;
976 /* Make in-repository path absolute */
977 if (path[0] != '/') {
978 char *abspath;
979 if (asprintf(&abspath, "/%s", path) == -1) {
980 err = got_error_from_errno("asprintf");
981 goto done;
983 free(path);
984 path = abspath;
987 done:
988 free(canonpath);
989 if (err)
990 free(path);
991 else
992 *in_repo_path = path;
993 return err;
996 static const struct got_error *
997 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
998 const char *path_packidx)
1000 const struct got_error *err = NULL;
1001 size_t i;
1003 for (i = 0; i < repo->pack_cache_size; i++) {
1004 if (repo->packidx_cache[i] == NULL)
1005 break;
1006 if (strcmp(repo->packidx_cache[i]->path_packidx,
1007 path_packidx) == 0) {
1008 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1011 if (i == repo->pack_cache_size) {
1012 do {
1013 i--;
1014 } while (i > 0 && repo->pinned_packidx >= 0 &&
1015 i == repo->pinned_packidx);
1016 err = got_packidx_close(repo->packidx_cache[i]);
1017 if (err)
1018 return err;
1021 repo->packidx_cache[i] = packidx;
1023 return NULL;
1026 int
1027 got_repo_is_packidx_filename(const char *name, size_t len)
1029 if (len != GOT_PACKIDX_NAMELEN)
1030 return 0;
1032 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1033 return 0;
1035 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1036 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1037 return 0;
1039 return 1;
1042 static struct got_packidx_bloom_filter *
1043 get_packidx_bloom_filter(struct got_repository *repo,
1044 const char *path, size_t path_len)
1046 struct got_packidx_bloom_filter key;
1048 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1049 return NULL; /* XXX */
1050 key.path_len = path_len;
1052 return RB_FIND(got_packidx_bloom_filter_tree,
1053 &repo->packidx_bloom_filters, &key);
1056 int
1057 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1058 const char *path_packidx, struct got_object_id *id)
1060 struct got_packidx_bloom_filter *bf;
1062 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1063 if (bf)
1064 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1066 /* No bloom filter means this pack index must be searched. */
1067 return 1;
1070 static const struct got_error *
1071 add_packidx_bloom_filter(struct got_repository *repo,
1072 struct got_packidx *packidx, const char *path_packidx)
1074 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1075 struct got_packidx_bloom_filter *bf;
1076 size_t len;
1079 * Don't use bloom filters for very large pack index files.
1080 * Large pack files will contain a relatively large fraction
1081 * of our objects so we will likely need to visit them anyway.
1082 * The more objects a pack file contains the higher the probability
1083 * of a false-positive match from the bloom filter. And reading
1084 * all object IDs from a large pack index file can be expensive.
1086 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1087 return NULL;
1089 /* Do we already have a filter for this pack index? */
1090 if (get_packidx_bloom_filter(repo, path_packidx,
1091 strlen(path_packidx)) != NULL)
1092 return NULL;
1094 bf = calloc(1, sizeof(*bf));
1095 if (bf == NULL)
1096 return got_error_from_errno("calloc");
1097 bf->bloom = calloc(1, sizeof(*bf->bloom));
1098 if (bf->bloom == NULL) {
1099 free(bf);
1100 return got_error_from_errno("calloc");
1103 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1104 if (len >= sizeof(bf->path)) {
1105 free(bf->bloom);
1106 free(bf);
1107 return got_error(GOT_ERR_NO_SPACE);
1109 bf->path_len = len;
1111 /* Minimum size supported by our bloom filter is 1000 entries. */
1112 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1113 for (i = 0; i < nobjects; i++) {
1114 struct got_packidx_object_id *id;
1115 id = &packidx->hdr.sorted_ids[i];
1116 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1119 RB_INSERT(got_packidx_bloom_filter_tree,
1120 &repo->packidx_bloom_filters, bf);
1121 return NULL;
1124 static void
1125 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1127 struct got_pathlist_entry *pe;
1129 while (!TAILQ_EMPTY(packidx_paths)) {
1130 pe = TAILQ_FIRST(packidx_paths);
1131 TAILQ_REMOVE(packidx_paths, pe, entry);
1132 free((char *)pe->path);
1133 free(pe);
1137 static const struct got_error *
1138 refresh_packidx_paths(struct got_repository *repo)
1140 const struct got_error *err = NULL;
1141 char *objects_pack_dir = NULL;
1142 struct stat sb;
1144 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1145 if (objects_pack_dir == NULL)
1146 return got_error_from_errno("got_repo_get_path_objects_pack");
1148 if (stat(objects_pack_dir, &sb) == -1) {
1149 if (errno != ENOENT) {
1150 err = got_error_from_errno2("stat", objects_pack_dir);
1151 goto done;
1153 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1154 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1155 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1156 purge_packidx_paths(&repo->packidx_paths);
1157 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1158 if (err)
1159 goto done;
1161 done:
1162 free(objects_pack_dir);
1163 return err;
1166 const struct got_error *
1167 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1168 struct got_repository *repo, struct got_object_id *id)
1170 const struct got_error *err;
1171 struct got_pathlist_entry *pe;
1172 size_t i;
1174 /* Search pack index cache. */
1175 for (i = 0; i < repo->pack_cache_size; i++) {
1176 if (repo->packidx_cache[i] == NULL)
1177 break;
1178 if (!got_repo_check_packidx_bloom_filter(repo,
1179 repo->packidx_cache[i]->path_packidx, id))
1180 continue; /* object will not be found in this index */
1181 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1182 if (*idx != -1) {
1183 *packidx = repo->packidx_cache[i];
1185 * Move this cache entry to the front. Repeatedly
1186 * searching a wrong pack index can be expensive.
1188 if (i > 0) {
1189 memmove(&repo->packidx_cache[1],
1190 &repo->packidx_cache[0],
1191 i * sizeof(repo->packidx_cache[0]));
1192 repo->packidx_cache[0] = *packidx;
1193 if (repo->pinned_packidx >= 0 &&
1194 repo->pinned_packidx < i)
1195 repo->pinned_packidx++;
1196 else if (repo->pinned_packidx == i)
1197 repo->pinned_packidx = 0;
1199 return NULL;
1202 /* No luck. Search the filesystem. */
1204 err = refresh_packidx_paths(repo);
1205 if (err)
1206 return err;
1208 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1209 const char *path_packidx = pe->path;
1210 int is_cached = 0;
1212 if (!got_repo_check_packidx_bloom_filter(repo,
1213 pe->path, id))
1214 continue; /* object will not be found in this index */
1216 for (i = 0; i < repo->pack_cache_size; i++) {
1217 if (repo->packidx_cache[i] == NULL)
1218 break;
1219 if (strcmp(repo->packidx_cache[i]->path_packidx,
1220 path_packidx) == 0) {
1221 is_cached = 1;
1222 break;
1225 if (is_cached)
1226 continue; /* already searched */
1228 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1229 path_packidx, 0);
1230 if (err)
1231 goto done;
1233 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1234 if (err)
1235 goto done;
1237 err = cache_packidx(repo, *packidx, path_packidx);
1238 if (err)
1239 goto done;
1241 *idx = got_packidx_get_object_idx(*packidx, id);
1242 if (*idx != -1) {
1243 err = NULL; /* found the object */
1244 goto done;
1248 err = got_error_no_obj(id);
1249 done:
1250 return err;
1253 const struct got_error *
1254 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1255 struct got_repository *repo)
1257 const struct got_error *err = NULL;
1258 DIR *packdir = NULL;
1259 struct dirent *dent;
1260 char *path_packidx = NULL;
1261 int packdir_fd;
1262 struct stat sb;
1264 packdir_fd = openat(got_repo_get_fd(repo),
1265 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1266 if (packdir_fd == -1) {
1267 return got_error_from_errno_fmt("openat: %s/%s",
1268 got_repo_get_path_git_dir(repo),
1269 GOT_OBJECTS_PACK_DIR);
1272 packdir = fdopendir(packdir_fd);
1273 if (packdir == NULL) {
1274 err = got_error_from_errno("fdopendir");
1275 goto done;
1278 if (fstat(packdir_fd, &sb) == -1) {
1279 err = got_error_from_errno("fstat");
1280 goto done;
1282 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1283 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1285 while ((dent = readdir(packdir)) != NULL) {
1286 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1287 continue;
1289 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1290 dent->d_name) == -1) {
1291 err = got_error_from_errno("asprintf");
1292 path_packidx = NULL;
1293 break;
1296 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1297 if (err)
1298 break;
1300 done:
1301 if (err)
1302 free(path_packidx);
1303 if (packdir && closedir(packdir) != 0 && err == NULL)
1304 err = got_error_from_errno("closedir");
1305 return err;
1308 const struct got_error *
1309 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1310 struct got_repository *repo)
1312 const struct got_error *err;
1313 size_t i;
1315 *packidx = NULL;
1317 /* Search pack index cache. */
1318 for (i = 0; i < repo->pack_cache_size; i++) {
1319 if (repo->packidx_cache[i] == NULL)
1320 break;
1321 if (strcmp(repo->packidx_cache[i]->path_packidx,
1322 path_packidx) == 0) {
1323 *packidx = repo->packidx_cache[i];
1324 return NULL;
1327 /* No luck. Search the filesystem. */
1329 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1330 path_packidx, 0);
1331 if (err)
1332 return err;
1334 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1335 if (err)
1336 goto done;
1338 err = cache_packidx(repo, *packidx, path_packidx);
1339 done:
1340 if (err) {
1341 got_packidx_close(*packidx);
1342 *packidx = NULL;
1344 return err;
1347 static const struct got_error *
1348 read_packfile_hdr(int fd, struct got_packidx *packidx)
1350 const struct got_error *err = NULL;
1351 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1352 struct got_packfile_hdr hdr;
1353 ssize_t n;
1355 n = read(fd, &hdr, sizeof(hdr));
1356 if (n < 0)
1357 return got_error_from_errno("read");
1358 if (n != sizeof(hdr))
1359 return got_error(GOT_ERR_BAD_PACKFILE);
1361 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1362 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1363 be32toh(hdr.nobjects) != totobj)
1364 err = got_error(GOT_ERR_BAD_PACKFILE);
1366 return err;
1369 static const struct got_error *
1370 open_packfile(int *fd, struct got_repository *repo,
1371 const char *relpath, struct got_packidx *packidx)
1373 const struct got_error *err = NULL;
1375 *fd = openat(got_repo_get_fd(repo), relpath,
1376 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1377 if (*fd == -1)
1378 return got_error_from_errno_fmt("openat: %s/%s",
1379 got_repo_get_path_git_dir(repo), relpath);
1381 if (packidx) {
1382 err = read_packfile_hdr(*fd, packidx);
1383 if (err) {
1384 close(*fd);
1385 *fd = -1;
1389 return err;
1392 const struct got_error *
1393 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1394 const char *path_packfile, struct got_packidx *packidx)
1396 const struct got_error *err = NULL;
1397 struct got_pack *pack = NULL;
1398 struct stat sb;
1399 size_t i;
1401 if (packp)
1402 *packp = NULL;
1404 for (i = 0; i < repo->pack_cache_size; i++) {
1405 pack = &repo->packs[i];
1406 if (pack->path_packfile == NULL)
1407 break;
1408 if (strcmp(pack->path_packfile, path_packfile) == 0)
1409 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1412 if (i == repo->pack_cache_size) {
1413 struct got_pack tmp;
1414 do {
1415 i--;
1416 } while (i > 0 && repo->pinned_pack >= 0 &&
1417 i == repo->pinned_pack);
1418 err = got_pack_close(&repo->packs[i]);
1419 if (err)
1420 return err;
1421 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1422 return got_error_from_errno("ftruncate");
1423 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1424 return got_error_from_errno("ftruncate");
1425 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1426 memcpy(&repo->packs[i], &repo->packs[0],
1427 sizeof(repo->packs[i]));
1428 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1429 if (repo->pinned_pack == 0)
1430 repo->pinned_pack = i;
1431 else if (repo->pinned_pack == i)
1432 repo->pinned_pack = 0;
1433 i = 0;
1436 pack = &repo->packs[i];
1438 pack->path_packfile = strdup(path_packfile);
1439 if (pack->path_packfile == NULL) {
1440 err = got_error_from_errno("strdup");
1441 goto done;
1444 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1445 if (err)
1446 goto done;
1448 if (fstat(pack->fd, &sb) != 0) {
1449 err = got_error_from_errno("fstat");
1450 goto done;
1452 pack->filesize = sb.st_size;
1454 pack->privsep_child = NULL;
1456 err = got_delta_cache_alloc(&pack->delta_cache);
1457 if (err)
1458 goto done;
1460 #ifndef GOT_PACK_NO_MMAP
1461 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1462 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1463 pack->fd, 0);
1464 if (pack->map == MAP_FAILED) {
1465 if (errno != ENOMEM) {
1466 err = got_error_from_errno("mmap");
1467 goto done;
1469 pack->map = NULL; /* fall back to read(2) */
1472 #endif
1473 done:
1474 if (err) {
1475 if (pack)
1476 got_pack_close(pack);
1477 } else if (packp)
1478 *packp = pack;
1479 return err;
1482 struct got_pack *
1483 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1485 struct got_pack *pack = NULL;
1486 size_t i;
1488 for (i = 0; i < repo->pack_cache_size; i++) {
1489 pack = &repo->packs[i];
1490 if (pack->path_packfile == NULL)
1491 break;
1492 if (strcmp(pack->path_packfile, path_packfile) == 0)
1493 return pack;
1496 return NULL;
1499 const struct got_error *
1500 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1501 struct got_pack *pack)
1503 size_t i;
1504 int pinned_pack = -1, pinned_packidx = -1;
1506 for (i = 0; i < repo->pack_cache_size; i++) {
1507 if (repo->packidx_cache[i] &&
1508 strcmp(repo->packidx_cache[i]->path_packidx,
1509 packidx->path_packidx) == 0)
1510 pinned_packidx = i;
1511 if (repo->packs[i].path_packfile &&
1512 strcmp(repo->packs[i].path_packfile,
1513 pack->path_packfile) == 0)
1514 pinned_pack = i;
1517 if (pinned_packidx == -1 || pinned_pack == -1)
1518 return got_error(GOT_ERR_PIN_PACK);
1520 repo->pinned_pack = pinned_pack;
1521 repo->pinned_packidx = pinned_packidx;
1522 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1523 return NULL;
1526 struct got_pack *
1527 got_repo_get_pinned_pack(struct got_repository *repo)
1529 if (repo->pinned_pack >= 0 &&
1530 repo->pinned_pack < repo->pack_cache_size)
1531 return &repo->packs[repo->pinned_pack];
1533 return NULL;
1536 void
1537 got_repo_unpin_pack(struct got_repository *repo)
1539 repo->pinned_packidx = -1;
1540 repo->pinned_pack = -1;
1541 repo->pinned_pid = 0;
1544 const struct got_error *
1545 got_repo_init(const char *repo_path, const char *head_name)
1547 const struct got_error *err = NULL;
1548 const char *dirnames[] = {
1549 GOT_OBJECTS_DIR,
1550 GOT_OBJECTS_PACK_DIR,
1551 GOT_REFS_DIR,
1553 const char *description_str = "Unnamed repository; "
1554 "edit this file 'description' to name the repository.";
1555 const char *headref = "ref: refs/heads/";
1556 const char *gitconfig_str = "[core]\n"
1557 "\trepositoryformatversion = 0\n"
1558 "\tfilemode = true\n"
1559 "\tbare = true\n";
1560 char *headref_str, *path;
1561 size_t i;
1563 if (!got_path_dir_is_empty(repo_path))
1564 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1566 for (i = 0; i < nitems(dirnames); i++) {
1567 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1568 return got_error_from_errno("asprintf");
1570 err = got_path_mkdir(path);
1571 free(path);
1572 if (err)
1573 return err;
1576 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1577 return got_error_from_errno("asprintf");
1578 err = got_path_create_file(path, description_str);
1579 free(path);
1580 if (err)
1581 return err;
1583 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1584 return got_error_from_errno("asprintf");
1585 if (asprintf(&headref_str, "%s%s", headref,
1586 head_name ? head_name : "main") == -1) {
1587 free(path);
1588 return got_error_from_errno("asprintf");
1590 err = got_path_create_file(path, headref_str);
1591 free(headref_str);
1592 free(path);
1593 if (err)
1594 return err;
1596 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1597 return got_error_from_errno("asprintf");
1598 err = got_path_create_file(path, gitconfig_str);
1599 free(path);
1600 if (err)
1601 return err;
1603 return NULL;
1606 static const struct got_error *
1607 match_packed_object(struct got_object_id **unique_id,
1608 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1610 const struct got_error *err = NULL;
1611 struct got_object_id_queue matched_ids;
1612 struct got_pathlist_entry *pe;
1614 STAILQ_INIT(&matched_ids);
1616 err = refresh_packidx_paths(repo);
1617 if (err)
1618 return err;
1620 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1621 const char *path_packidx = pe->path;
1622 struct got_packidx *packidx;
1623 struct got_object_qid *qid;
1625 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1626 path_packidx, 0);
1627 if (err)
1628 break;
1630 err = got_packidx_match_id_str_prefix(&matched_ids,
1631 packidx, id_str_prefix);
1632 if (err) {
1633 got_packidx_close(packidx);
1634 break;
1636 err = got_packidx_close(packidx);
1637 if (err)
1638 break;
1640 STAILQ_FOREACH(qid, &matched_ids, entry) {
1641 if (obj_type != GOT_OBJ_TYPE_ANY) {
1642 int matched_type;
1643 err = got_object_get_type(&matched_type, repo,
1644 &qid->id);
1645 if (err)
1646 goto done;
1647 if (matched_type != obj_type)
1648 continue;
1650 if (*unique_id == NULL) {
1651 *unique_id = got_object_id_dup(&qid->id);
1652 if (*unique_id == NULL) {
1653 err = got_error_from_errno("malloc");
1654 goto done;
1656 } else {
1657 if (got_object_id_cmp(*unique_id,
1658 &qid->id) == 0)
1659 continue; /* packed multiple times */
1660 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1661 goto done;
1665 done:
1666 got_object_id_queue_free(&matched_ids);
1667 if (err) {
1668 free(*unique_id);
1669 *unique_id = NULL;
1671 return err;
1674 static const struct got_error *
1675 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1676 const char *object_dir, const char *id_str_prefix, int obj_type,
1677 struct got_repository *repo)
1679 const struct got_error *err = NULL;
1680 char *path, *id_str = NULL;
1681 DIR *dir = NULL;
1682 struct dirent *dent;
1683 struct got_object_id id;
1685 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1686 err = got_error_from_errno("asprintf");
1687 goto done;
1690 dir = opendir(path);
1691 if (dir == NULL) {
1692 if (errno == ENOENT) {
1693 err = NULL;
1694 goto done;
1696 err = got_error_from_errno2("opendir", path);
1697 goto done;
1699 while ((dent = readdir(dir)) != NULL) {
1700 int cmp;
1702 free(id_str);
1703 id_str = NULL;
1705 if (strcmp(dent->d_name, ".") == 0 ||
1706 strcmp(dent->d_name, "..") == 0)
1707 continue;
1709 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1710 err = got_error_from_errno("asprintf");
1711 goto done;
1714 if (!got_parse_sha1_digest(id.sha1, id_str))
1715 continue;
1718 * Directory entries do not necessarily appear in
1719 * sorted order, so we must iterate over all of them.
1721 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1722 if (cmp != 0)
1723 continue;
1725 if (*unique_id == NULL) {
1726 if (obj_type != GOT_OBJ_TYPE_ANY) {
1727 int matched_type;
1728 err = got_object_get_type(&matched_type, repo,
1729 &id);
1730 if (err)
1731 goto done;
1732 if (matched_type != obj_type)
1733 continue;
1735 *unique_id = got_object_id_dup(&id);
1736 if (*unique_id == NULL) {
1737 err = got_error_from_errno("got_object_id_dup");
1738 goto done;
1740 } else {
1741 if (got_object_id_cmp(*unique_id, &id) == 0)
1742 continue; /* both packed and loose */
1743 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1744 goto done;
1747 done:
1748 if (dir && closedir(dir) != 0 && err == NULL)
1749 err = got_error_from_errno("closedir");
1750 if (err) {
1751 free(*unique_id);
1752 *unique_id = NULL;
1754 free(id_str);
1755 free(path);
1756 return err;
1759 const struct got_error *
1760 got_repo_match_object_id_prefix(struct got_object_id **id,
1761 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1763 const struct got_error *err = NULL;
1764 char *path_objects = NULL, *object_dir = NULL;
1765 size_t len;
1766 int i;
1768 *id = NULL;
1770 path_objects = got_repo_get_path_objects(repo);
1772 len = strlen(id_str_prefix);
1773 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1774 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1775 goto done;
1778 for (i = 0; i < len; i++) {
1779 if (isxdigit((unsigned char)id_str_prefix[i]))
1780 continue;
1781 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1782 goto done;
1785 if (len >= 2) {
1786 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1787 if (err)
1788 goto done;
1789 object_dir = strndup(id_str_prefix, 2);
1790 if (object_dir == NULL) {
1791 err = got_error_from_errno("strdup");
1792 goto done;
1794 err = match_loose_object(id, path_objects, object_dir,
1795 id_str_prefix, obj_type, repo);
1796 } else if (len == 1) {
1797 int i;
1798 for (i = 0; i < 0xf; i++) {
1799 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1800 == -1) {
1801 err = got_error_from_errno("asprintf");
1802 goto done;
1804 err = match_packed_object(id, repo, object_dir,
1805 obj_type);
1806 if (err)
1807 goto done;
1808 err = match_loose_object(id, path_objects, object_dir,
1809 id_str_prefix, obj_type, repo);
1810 if (err)
1811 goto done;
1813 } else {
1814 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1815 goto done;
1817 done:
1818 free(path_objects);
1819 free(object_dir);
1820 if (err) {
1821 free(*id);
1822 *id = NULL;
1823 } else if (*id == NULL) {
1824 switch (obj_type) {
1825 case GOT_OBJ_TYPE_BLOB:
1826 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1827 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1828 break;
1829 case GOT_OBJ_TYPE_TREE:
1830 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1831 GOT_OBJ_LABEL_TREE, id_str_prefix);
1832 break;
1833 case GOT_OBJ_TYPE_COMMIT:
1834 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1835 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1836 break;
1837 case GOT_OBJ_TYPE_TAG:
1838 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1839 GOT_OBJ_LABEL_TAG, id_str_prefix);
1840 break;
1841 default:
1842 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1843 break;
1847 return err;
1850 const struct got_error *
1851 got_repo_match_object_id(struct got_object_id **id, char **label,
1852 const char *id_str, int obj_type, struct got_reflist_head *refs,
1853 struct got_repository *repo)
1855 const struct got_error *err;
1856 struct got_tag_object *tag;
1857 struct got_reference *ref = NULL;
1859 *id = NULL;
1860 if (label)
1861 *label = NULL;
1863 if (refs) {
1864 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1865 refs, repo);
1866 if (err == NULL) {
1867 *id = got_object_id_dup(
1868 got_object_tag_get_object_id(tag));
1869 if (*id == NULL)
1870 err = got_error_from_errno("got_object_id_dup");
1871 else if (label && asprintf(label, "refs/tags/%s",
1872 got_object_tag_get_name(tag)) == -1) {
1873 err = got_error_from_errno("asprintf");
1874 free(*id);
1875 *id = NULL;
1877 got_object_tag_close(tag);
1878 return err;
1879 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1880 err->code != GOT_ERR_NO_OBJ)
1881 return err;
1884 err = got_ref_open(&ref, repo, id_str, 0);
1885 if (err == NULL) {
1886 err = got_ref_resolve(id, repo, ref);
1887 if (err)
1888 goto done;
1889 if (label) {
1890 *label = strdup(got_ref_get_name(ref));
1891 if (*label == NULL) {
1892 err = got_error_from_errno("strdup");
1893 goto done;
1896 } else {
1897 if (err->code != GOT_ERR_NOT_REF &&
1898 err->code != GOT_ERR_BAD_REF_NAME)
1899 goto done;
1900 err = got_repo_match_object_id_prefix(id, id_str,
1901 obj_type, repo);
1902 if (err) {
1903 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1904 err = got_error_not_ref(id_str);
1905 goto done;
1907 if (label) {
1908 err = got_object_id_str(label, *id);
1909 if (*label == NULL) {
1910 err = got_error_from_errno("strdup");
1911 goto done;
1915 done:
1916 if (ref)
1917 got_ref_close(ref);
1918 return err;
1921 const struct got_error *
1922 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1923 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1925 const struct got_error *err = NULL;
1926 struct got_reflist_entry *re;
1927 struct got_object_id *tag_id;
1928 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1930 *tag = NULL;
1932 TAILQ_FOREACH(re, refs, entry) {
1933 const char *refname;
1934 refname = got_ref_get_name(re->ref);
1935 if (got_ref_is_symbolic(re->ref))
1936 continue;
1937 if (strncmp(refname, "refs/tags/", 10) != 0)
1938 continue;
1939 if (!name_is_absolute)
1940 refname += strlen("refs/tags/");
1941 if (strcmp(refname, name) != 0)
1942 continue;
1943 err = got_ref_resolve(&tag_id, repo, re->ref);
1944 if (err)
1945 break;
1946 err = got_object_open_as_tag(tag, repo, tag_id);
1947 free(tag_id);
1948 if (err)
1949 break;
1950 if (obj_type == GOT_OBJ_TYPE_ANY ||
1951 got_object_tag_get_object_type(*tag) == obj_type)
1952 break;
1953 got_object_tag_close(*tag);
1954 *tag = NULL;
1957 if (err == NULL && *tag == NULL)
1958 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1959 GOT_OBJ_LABEL_TAG, name);
1960 return err;
1963 static const struct got_error *
1964 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1965 const char *name, mode_t mode, struct got_object_id *blob_id)
1967 const struct got_error *err = NULL;
1969 *new_te = NULL;
1971 *new_te = calloc(1, sizeof(**new_te));
1972 if (*new_te == NULL)
1973 return got_error_from_errno("calloc");
1975 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1976 sizeof((*new_te)->name)) {
1977 err = got_error(GOT_ERR_NO_SPACE);
1978 goto done;
1981 if (S_ISLNK(mode)) {
1982 (*new_te)->mode = S_IFLNK;
1983 } else {
1984 (*new_te)->mode = S_IFREG;
1985 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1987 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1988 done:
1989 if (err && *new_te) {
1990 free(*new_te);
1991 *new_te = NULL;
1993 return err;
1996 static const struct got_error *
1997 import_file(struct got_tree_entry **new_te, struct dirent *de,
1998 const char *path, struct got_repository *repo)
2000 const struct got_error *err;
2001 struct got_object_id *blob_id = NULL;
2002 char *filepath;
2003 struct stat sb;
2005 if (asprintf(&filepath, "%s%s%s", path,
2006 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2007 return got_error_from_errno("asprintf");
2009 if (lstat(filepath, &sb) != 0) {
2010 err = got_error_from_errno2("lstat", path);
2011 goto done;
2014 err = got_object_blob_create(&blob_id, filepath, repo);
2015 if (err)
2016 goto done;
2018 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2019 blob_id);
2020 done:
2021 free(filepath);
2022 if (err)
2023 free(blob_id);
2024 return err;
2027 static const struct got_error *
2028 insert_tree_entry(struct got_tree_entry *new_te,
2029 struct got_pathlist_head *paths)
2031 const struct got_error *err = NULL;
2032 struct got_pathlist_entry *new_pe;
2034 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2035 if (err)
2036 return err;
2037 if (new_pe == NULL)
2038 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2039 return NULL;
2042 static const struct got_error *write_tree(struct got_object_id **,
2043 const char *, struct got_pathlist_head *, struct got_repository *,
2044 got_repo_import_cb progress_cb, void *progress_arg);
2046 static const struct got_error *
2047 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2048 const char *path, struct got_pathlist_head *ignores,
2049 struct got_repository *repo,
2050 got_repo_import_cb progress_cb, void *progress_arg)
2052 const struct got_error *err;
2053 struct got_object_id *id = NULL;
2054 char *subdirpath;
2056 if (asprintf(&subdirpath, "%s%s%s", path,
2057 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2058 return got_error_from_errno("asprintf");
2060 (*new_te) = calloc(1, sizeof(**new_te));
2061 if (*new_te == NULL)
2062 return got_error_from_errno("calloc");
2063 (*new_te)->mode = S_IFDIR;
2064 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2065 sizeof((*new_te)->name)) {
2066 err = got_error(GOT_ERR_NO_SPACE);
2067 goto done;
2069 err = write_tree(&id, subdirpath, ignores, repo,
2070 progress_cb, progress_arg);
2071 if (err)
2072 goto done;
2073 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2075 done:
2076 free(id);
2077 free(subdirpath);
2078 if (err) {
2079 free(*new_te);
2080 *new_te = NULL;
2082 return err;
2085 static const struct got_error *
2086 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2087 struct got_pathlist_head *ignores, struct got_repository *repo,
2088 got_repo_import_cb progress_cb, void *progress_arg)
2090 const struct got_error *err = NULL;
2091 DIR *dir;
2092 struct dirent *de;
2093 int nentries;
2094 struct got_tree_entry *new_te = NULL;
2095 struct got_pathlist_head paths;
2096 struct got_pathlist_entry *pe;
2098 *new_tree_id = NULL;
2100 TAILQ_INIT(&paths);
2102 dir = opendir(path_dir);
2103 if (dir == NULL) {
2104 err = got_error_from_errno2("opendir", path_dir);
2105 goto done;
2108 nentries = 0;
2109 while ((de = readdir(dir)) != NULL) {
2110 int ignore = 0;
2111 int type;
2113 if (strcmp(de->d_name, ".") == 0 ||
2114 strcmp(de->d_name, "..") == 0)
2115 continue;
2117 TAILQ_FOREACH(pe, ignores, entry) {
2118 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2119 ignore = 1;
2120 break;
2123 if (ignore)
2124 continue;
2126 err = got_path_dirent_type(&type, path_dir, de);
2127 if (err)
2128 goto done;
2130 if (type == DT_DIR) {
2131 err = import_subdir(&new_te, de, path_dir,
2132 ignores, repo, progress_cb, progress_arg);
2133 if (err) {
2134 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2135 goto done;
2136 err = NULL;
2137 continue;
2139 } else if (type == DT_REG || type == DT_LNK) {
2140 err = import_file(&new_te, de, path_dir, repo);
2141 if (err)
2142 goto done;
2143 } else
2144 continue;
2146 err = insert_tree_entry(new_te, &paths);
2147 if (err)
2148 goto done;
2149 nentries++;
2152 if (TAILQ_EMPTY(&paths)) {
2153 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2154 "cannot create tree without any entries");
2155 goto done;
2158 TAILQ_FOREACH(pe, &paths, entry) {
2159 struct got_tree_entry *te = pe->data;
2160 char *path;
2161 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2162 continue;
2163 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2164 err = got_error_from_errno("asprintf");
2165 goto done;
2167 err = (*progress_cb)(progress_arg, path);
2168 free(path);
2169 if (err)
2170 goto done;
2173 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2174 done:
2175 if (dir)
2176 closedir(dir);
2177 got_pathlist_free(&paths);
2178 return err;
2181 const struct got_error *
2182 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2183 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2184 struct got_repository *repo, got_repo_import_cb progress_cb,
2185 void *progress_arg)
2187 const struct got_error *err;
2188 struct got_object_id *new_tree_id;
2190 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2191 progress_cb, progress_arg);
2192 if (err)
2193 return err;
2195 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2196 author, time(NULL), author, time(NULL), logmsg, repo);
2197 free(new_tree_id);
2198 return err;
2201 const struct got_error *
2202 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2203 struct got_repository *repo)
2205 const struct got_error *err = NULL;
2206 char *path_objects = NULL, *path = NULL;
2207 DIR *dir = NULL;
2208 struct got_object_id id;
2209 int i;
2211 *nobjects = 0;
2212 *ondisk_size = 0;
2214 path_objects = got_repo_get_path_objects(repo);
2215 if (path_objects == NULL)
2216 return got_error_from_errno("got_repo_get_path_objects");
2218 for (i = 0; i <= 0xff; i++) {
2219 struct dirent *dent;
2221 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2222 err = got_error_from_errno("asprintf");
2223 break;
2226 dir = opendir(path);
2227 if (dir == NULL) {
2228 if (errno == ENOENT) {
2229 err = NULL;
2230 continue;
2232 err = got_error_from_errno2("opendir", path);
2233 break;
2236 while ((dent = readdir(dir)) != NULL) {
2237 char *id_str;
2238 int fd;
2239 struct stat sb;
2241 if (strcmp(dent->d_name, ".") == 0 ||
2242 strcmp(dent->d_name, "..") == 0)
2243 continue;
2245 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2246 err = got_error_from_errno("asprintf");
2247 goto done;
2250 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2251 free(id_str);
2252 continue;
2254 free(id_str);
2256 err = got_object_open_loose_fd(&fd, &id, repo);
2257 if (err)
2258 goto done;
2260 if (fstat(fd, &sb) == -1) {
2261 err = got_error_from_errno("fstat");
2262 close(fd);
2263 goto done;
2265 (*nobjects)++;
2266 (*ondisk_size) += sb.st_size;
2268 if (close(fd) == -1) {
2269 err = got_error_from_errno("close");
2270 goto done;
2274 if (closedir(dir) != 0) {
2275 err = got_error_from_errno("closedir");
2276 goto done;
2278 dir = NULL;
2280 free(path);
2281 path = NULL;
2283 done:
2284 if (dir && closedir(dir) != 0 && err == NULL)
2285 err = got_error_from_errno("closedir");
2287 if (err) {
2288 *nobjects = 0;
2289 *ondisk_size = 0;
2291 free(path_objects);
2292 free(path);
2293 return err;
2296 const struct got_error *
2297 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2298 off_t *total_packsize, struct got_repository *repo)
2300 const struct got_error *err = NULL;
2301 DIR *packdir = NULL;
2302 struct dirent *dent;
2303 struct got_packidx *packidx = NULL;
2304 char *path_packidx;
2305 char *path_packfile;
2306 int packdir_fd;
2307 struct stat sb;
2309 *npackfiles = 0;
2310 *nobjects = 0;
2311 *total_packsize = 0;
2313 packdir_fd = openat(got_repo_get_fd(repo),
2314 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2315 if (packdir_fd == -1) {
2316 return got_error_from_errno_fmt("openat: %s/%s",
2317 got_repo_get_path_git_dir(repo),
2318 GOT_OBJECTS_PACK_DIR);
2321 packdir = fdopendir(packdir_fd);
2322 if (packdir == NULL) {
2323 err = got_error_from_errno("fdopendir");
2324 goto done;
2327 while ((dent = readdir(packdir)) != NULL) {
2328 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2329 continue;
2331 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2332 dent->d_name) == -1) {
2333 err = got_error_from_errno("asprintf");
2334 goto done;
2337 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2338 path_packidx, 0);
2339 free(path_packidx);
2340 if (err)
2341 goto done;
2343 if (fstat(packidx->fd, &sb) == -1)
2344 goto done;
2345 *total_packsize += sb.st_size;
2347 err = got_packidx_get_packfile_path(&path_packfile,
2348 packidx->path_packidx);
2349 if (err)
2350 goto done;
2352 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2353 0) == -1) {
2354 free(path_packfile);
2355 goto done;
2357 free(path_packfile);
2358 *total_packsize += sb.st_size;
2360 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2362 (*npackfiles)++;
2364 got_packidx_close(packidx);
2365 packidx = NULL;
2367 done:
2368 if (packidx)
2369 got_packidx_close(packidx);
2370 if (packdir && closedir(packdir) != 0 && err == NULL)
2371 err = got_error_from_errno("closedir");
2372 if (err) {
2373 *npackfiles = 0;
2374 *nobjects = 0;
2375 *total_packsize = 0;
2377 return err;
2380 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2381 got_packidx_bloom_filter_cmp);