Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
40 #include "bloom.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_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_delta_cache.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
63 #ifndef nitems
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 #endif
67 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
69 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
70 got_packidx_bloom_filter_cmp);
72 const char *
73 got_repo_get_path(struct got_repository *repo)
74 {
75 return repo->path;
76 }
78 const char *
79 got_repo_get_path_git_dir(struct got_repository *repo)
80 {
81 return repo->path_git_dir;
82 }
84 int
85 got_repo_get_fd(struct got_repository *repo)
86 {
87 return repo->gitdir_fd;
88 }
90 const char *
91 got_repo_get_gitconfig_author_name(struct got_repository *repo)
92 {
93 return repo->gitconfig_author_name;
94 }
96 const char *
97 got_repo_get_gitconfig_author_email(struct got_repository *repo)
98 {
99 return repo->gitconfig_author_email;
102 const char *
103 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
105 return repo->global_gitconfig_author_name;
108 const char *
109 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
111 return repo->global_gitconfig_author_email;
114 const char *
115 got_repo_get_gitconfig_owner(struct got_repository *repo)
117 return repo->gitconfig_owner;
120 void
121 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
122 struct got_repository *repo)
124 *extensions = repo->extensions;
125 *nextensions = repo->nextensions;
128 int
129 got_repo_is_bare(struct got_repository *repo)
131 return (strcmp(repo->path, repo->path_git_dir) == 0);
134 static char *
135 get_path_git_child(struct got_repository *repo, const char *basename)
137 char *path_child;
139 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
140 basename) == -1)
141 return NULL;
143 return path_child;
146 char *
147 got_repo_get_path_objects(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_OBJECTS_DIR);
152 char *
153 got_repo_get_path_objects_pack(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
158 char *
159 got_repo_get_path_refs(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_REFS_DIR);
164 char *
165 got_repo_get_path_packed_refs(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
170 static char *
171 get_path_head(struct got_repository *repo)
173 return get_path_git_child(repo, GOT_HEAD_FILE);
176 char *
177 got_repo_get_path_gitconfig(struct got_repository *repo)
179 return get_path_git_child(repo, GOT_GITCONFIG);
182 char *
183 got_repo_get_path_gotconfig(struct got_repository *repo)
185 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
188 const struct got_gotconfig *
189 got_repo_get_gotconfig(struct got_repository *repo)
191 return repo->gotconfig;
194 void
195 got_repo_get_gitconfig_remotes(int *nremotes,
196 const struct got_remote_repo **remotes, struct got_repository *repo)
198 *nremotes = repo->ngitconfig_remotes;
199 *remotes = repo->gitconfig_remotes;
202 static int
203 is_git_repo(struct got_repository *repo)
205 const char *path_git = got_repo_get_path_git_dir(repo);
206 char *path_objects = got_repo_get_path_objects(repo);
207 char *path_refs = got_repo_get_path_refs(repo);
208 char *path_head = get_path_head(repo);
209 int ret = 0;
210 struct stat sb;
211 struct got_reference *head_ref;
213 if (lstat(path_git, &sb) == -1)
214 goto done;
215 if (!S_ISDIR(sb.st_mode))
216 goto done;
218 if (lstat(path_objects, &sb) == -1)
219 goto done;
220 if (!S_ISDIR(sb.st_mode))
221 goto done;
223 if (lstat(path_refs, &sb) == -1)
224 goto done;
225 if (!S_ISDIR(sb.st_mode))
226 goto done;
228 if (lstat(path_head, &sb) == -1)
229 goto done;
230 if (!S_ISREG(sb.st_mode))
231 goto done;
233 /* Check if the HEAD reference can be opened. */
234 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
235 goto done;
236 got_ref_close(head_ref);
238 ret = 1;
239 done:
240 free(path_objects);
241 free(path_refs);
242 free(path_head);
243 return ret;
247 static const struct got_error *
248 close_tempfiles(int *fds, size_t nfds)
250 const struct got_error *err = NULL;
251 int i;
253 for (i = 0; i < nfds; i++) {
254 if (fds[i] == -1)
255 continue;
256 if (close(fds[i]) == -1) {
257 err = got_error_from_errno("close");
258 break;
261 free(fds);
262 return err;
265 static const struct got_error *
266 open_tempfiles(int **fds, size_t array_size, size_t nfds)
268 const struct got_error *err = NULL;
269 int i;
271 *fds = calloc(array_size, sizeof(**fds));
272 if (*fds == NULL)
273 return got_error_from_errno("calloc");
275 for (i = 0; i < array_size; i++)
276 (*fds)[i] = -1;
278 for (i = 0; i < nfds; i++) {
279 (*fds)[i] = got_opentempfd();
280 if ((*fds)[i] == -1) {
281 err = got_error_from_errno("got_opentempfd");
282 close_tempfiles(*fds, nfds);
283 *fds = NULL;
284 return err;
288 return NULL;
291 static const struct got_error *
292 get_pack_cache_size(int *pack_cache_size)
294 struct rlimit rl;
296 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
297 return got_error_from_errno("getrlimit");
299 *pack_cache_size = GOT_PACK_CACHE_SIZE;
300 if (*pack_cache_size > rl.rlim_cur / 8)
301 *pack_cache_size = rl.rlim_cur / 8;
303 return NULL;
306 const struct got_error *
307 got_repo_pack_fds_open(int **pack_fds)
309 const struct got_error *err;
310 int nfds;
312 err = get_pack_cache_size(&nfds);
313 if (err)
314 return err;
316 /*
317 * We need one basefd and one accumfd per cached pack.
318 * Our constants should be set up in a way such that
319 * this error never triggers.
320 */
321 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
322 return got_error(GOT_ERR_NO_SPACE);
324 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
327 const struct got_error *
328 got_repo_pack_fds_close(int *pack_fds)
330 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
333 const struct got_error *
334 got_repo_temp_fds_open(int **temp_fds)
336 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
337 GOT_REPO_NUM_TEMPFILES);
340 void
341 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
343 int i;
345 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
346 repo->tempfiles[i] = temp_fds[i];
349 const struct got_error *
350 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
352 int i;
354 *fd = -1;
355 *idx = -1;
357 for (i = 0; i < nitems(repo->tempfiles); i++) {
358 if (repo->tempfile_use_mask & (1 << i))
359 continue;
360 if (repo->tempfiles[i] != -1) {
361 if (ftruncate(repo->tempfiles[i], 0L) == -1)
362 return got_error_from_errno("ftruncate");
363 *fd = repo->tempfiles[i];
364 *idx = i;
365 repo->tempfile_use_mask |= (1 << i);
366 return NULL;
370 return got_error(GOT_ERR_REPO_TEMPFILE);
373 void
374 got_repo_temp_fds_put(int idx, struct got_repository *repo)
376 repo->tempfile_use_mask &= ~(1 << idx);
379 const struct got_error *
380 got_repo_temp_fds_close(int *temp_fds)
382 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
385 const struct got_error *
386 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
387 struct got_object *obj)
389 #ifndef GOT_NO_OBJ_CACHE
390 const struct got_error *err = NULL;
391 err = got_object_cache_add(&repo->objcache, id, obj);
392 if (err) {
393 if (err->code == GOT_ERR_OBJ_EXISTS ||
394 err->code == GOT_ERR_OBJ_TOO_LARGE)
395 err = NULL;
396 return err;
398 obj->refcnt++;
399 #endif
400 return NULL;
403 struct got_object *
404 got_repo_get_cached_object(struct got_repository *repo,
405 struct got_object_id *id)
407 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
410 const struct got_error *
411 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
412 struct got_tree_object *tree)
414 #ifndef GOT_NO_OBJ_CACHE
415 const struct got_error *err = NULL;
416 err = got_object_cache_add(&repo->treecache, id, tree);
417 if (err) {
418 if (err->code == GOT_ERR_OBJ_EXISTS ||
419 err->code == GOT_ERR_OBJ_TOO_LARGE)
420 err = NULL;
421 return err;
423 tree->refcnt++;
424 #endif
425 return NULL;
428 struct got_tree_object *
429 got_repo_get_cached_tree(struct got_repository *repo,
430 struct got_object_id *id)
432 return (struct got_tree_object *)got_object_cache_get(
433 &repo->treecache, id);
436 const struct got_error *
437 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
438 struct got_commit_object *commit)
440 #ifndef GOT_NO_OBJ_CACHE
441 const struct got_error *err = NULL;
442 err = got_object_cache_add(&repo->commitcache, id, commit);
443 if (err) {
444 if (err->code == GOT_ERR_OBJ_EXISTS ||
445 err->code == GOT_ERR_OBJ_TOO_LARGE)
446 err = NULL;
447 return err;
449 commit->refcnt++;
450 #endif
451 return NULL;
454 struct got_commit_object *
455 got_repo_get_cached_commit(struct got_repository *repo,
456 struct got_object_id *id)
458 return (struct got_commit_object *)got_object_cache_get(
459 &repo->commitcache, id);
462 const struct got_error *
463 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
464 struct got_tag_object *tag)
466 #ifndef GOT_NO_OBJ_CACHE
467 const struct got_error *err = NULL;
468 err = got_object_cache_add(&repo->tagcache, id, tag);
469 if (err) {
470 if (err->code == GOT_ERR_OBJ_EXISTS ||
471 err->code == GOT_ERR_OBJ_TOO_LARGE)
472 err = NULL;
473 return err;
475 tag->refcnt++;
476 #endif
477 return NULL;
480 struct got_tag_object *
481 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
483 return (struct got_tag_object *)got_object_cache_get(
484 &repo->tagcache, id);
487 const struct got_error *
488 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
489 struct got_raw_object *raw)
491 #ifndef GOT_NO_OBJ_CACHE
492 const struct got_error *err = NULL;
493 err = got_object_cache_add(&repo->rawcache, id, raw);
494 if (err) {
495 if (err->code == GOT_ERR_OBJ_EXISTS ||
496 err->code == GOT_ERR_OBJ_TOO_LARGE)
497 err = NULL;
498 return err;
500 raw->refcnt++;
501 #endif
502 return NULL;
506 struct got_raw_object *
507 got_repo_get_cached_raw_object(struct got_repository *repo,
508 struct got_object_id *id)
510 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
514 static const struct got_error *
515 open_repo(struct got_repository *repo, const char *path)
517 const struct got_error *err = NULL;
519 repo->gitdir_fd = -1;
521 /* bare git repository? */
522 repo->path_git_dir = strdup(path);
523 if (repo->path_git_dir == NULL)
524 return got_error_from_errno("strdup");
525 if (is_git_repo(repo)) {
526 repo->path = strdup(repo->path_git_dir);
527 if (repo->path == NULL) {
528 err = got_error_from_errno("strdup");
529 goto done;
531 repo->gitdir_fd = open(repo->path_git_dir,
532 O_DIRECTORY | O_CLOEXEC);
533 if (repo->gitdir_fd == -1) {
534 err = got_error_from_errno2("open",
535 repo->path_git_dir);
536 goto done;
538 return NULL;
541 /* git repository with working tree? */
542 free(repo->path_git_dir);
543 repo->path_git_dir = NULL;
544 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
548 if (is_git_repo(repo)) {
549 repo->path = strdup(path);
550 if (repo->path == NULL) {
551 err = got_error_from_errno("strdup");
552 goto done;
554 repo->gitdir_fd = open(repo->path_git_dir,
555 O_DIRECTORY | O_CLOEXEC);
556 if (repo->gitdir_fd == -1) {
557 err = got_error_from_errno2("open",
558 repo->path_git_dir);
559 goto done;
561 return NULL;
564 err = got_error(GOT_ERR_NOT_GIT_REPO);
565 done:
566 if (err) {
567 free(repo->path);
568 repo->path = NULL;
569 free(repo->path_git_dir);
570 repo->path_git_dir = NULL;
571 if (repo->gitdir_fd != -1)
572 close(repo->gitdir_fd);
573 repo->gitdir_fd = -1;
576 return err;
579 static const struct got_error *
580 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
582 const struct got_error *err = NULL;
583 char *repo_gitconfig_path = NULL;
585 if (global_gitconfig_path) {
586 /* Read settings from ~/.gitconfig. */
587 int dummy_repo_version;
588 err = got_repo_read_gitconfig(&dummy_repo_version,
589 &repo->global_gitconfig_author_name,
590 &repo->global_gitconfig_author_email,
591 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
592 if (err)
593 return err;
596 /* Read repository's .git/config file. */
597 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
598 if (repo_gitconfig_path == NULL)
599 return got_error_from_errno("got_repo_get_path_gitconfig");
601 err = got_repo_read_gitconfig(
602 &repo->gitconfig_repository_format_version,
603 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
604 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
605 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
606 repo_gitconfig_path);
607 if (err)
608 goto done;
610 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
611 int i;
613 for (i = 0; i < repo->ngitconfig_remotes; i++) {
614 got_repo_free_remote_repo_data(
615 &repo->gitconfig_remotes[i]);
617 free(repo->gitconfig_remotes);
618 repo->gitconfig_remotes = NULL;
619 repo->ngitconfig_remotes = 0;
621 free(repo->gitconfig_author_name);
622 repo->gitconfig_author_name = NULL;
623 free(repo->gitconfig_author_email);
624 repo->gitconfig_author_email = NULL;
626 free(repo->global_gitconfig_author_name);
627 repo->global_gitconfig_author_name = NULL;
628 free(repo->global_gitconfig_author_email);
629 repo->global_gitconfig_author_email = NULL;
632 done:
633 free(repo_gitconfig_path);
634 return err;
637 static const struct got_error *
638 read_gotconfig(struct got_repository *repo)
640 const struct got_error *err = NULL;
641 char *gotconfig_path;
643 gotconfig_path = got_repo_get_path_gotconfig(repo);
644 if (gotconfig_path == NULL)
645 return got_error_from_errno("got_repo_get_path_gotconfig");
647 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
648 free(gotconfig_path);
649 return err;
652 /* Supported repository format extensions. */
653 static const char *const repo_extensions[] = {
654 "noop", /* Got supports repository format version 1. */
655 "preciousObjects", /* Supported by gotadmin cleanup. */
656 "worktreeConfig", /* Got does not care about Git work trees. */
657 };
659 const struct got_error *
660 got_repo_open(struct got_repository **repop, const char *path,
661 const char *global_gitconfig_path, int *pack_fds)
663 struct got_repository *repo = NULL;
664 const struct got_error *err = NULL;
665 char *repo_path = NULL;
666 size_t i, j = 0;
668 *repop = NULL;
670 repo = calloc(1, sizeof(*repo));
671 if (repo == NULL)
672 return got_error_from_errno("calloc");
674 RB_INIT(&repo->packidx_bloom_filters);
675 TAILQ_INIT(&repo->packidx_paths);
677 for (i = 0; i < nitems(repo->privsep_children); i++) {
678 memset(&repo->privsep_children[i], 0,
679 sizeof(repo->privsep_children[0]));
680 repo->privsep_children[i].imsg_fd = -1;
683 err = got_object_cache_init(&repo->objcache,
684 GOT_OBJECT_CACHE_TYPE_OBJ);
685 if (err)
686 goto done;
687 err = got_object_cache_init(&repo->treecache,
688 GOT_OBJECT_CACHE_TYPE_TREE);
689 if (err)
690 goto done;
691 err = got_object_cache_init(&repo->commitcache,
692 GOT_OBJECT_CACHE_TYPE_COMMIT);
693 if (err)
694 goto done;
695 err = got_object_cache_init(&repo->tagcache,
696 GOT_OBJECT_CACHE_TYPE_TAG);
697 if (err)
698 goto done;
699 err = got_object_cache_init(&repo->rawcache,
700 GOT_OBJECT_CACHE_TYPE_RAW);
701 if (err)
702 goto done;
704 err = get_pack_cache_size(&repo->pack_cache_size);
705 if (err)
706 goto done;
707 for (i = 0; i < nitems(repo->packs); i++) {
708 if (pack_fds != NULL && i < repo->pack_cache_size) {
709 repo->packs[i].basefd = pack_fds[j++];
710 repo->packs[i].accumfd = pack_fds[j++];
711 } else {
712 repo->packs[i].basefd = -1;
713 repo->packs[i].accumfd = -1;
716 for (i = 0; i < nitems(repo->tempfiles); i++)
717 repo->tempfiles[i] = -1;
718 repo->pinned_pack = -1;
719 repo->pinned_packidx = -1;
720 repo->pinned_pid = 0;
722 repo_path = realpath(path, NULL);
723 if (repo_path == NULL) {
724 err = got_error_from_errno2("realpath", path);
725 goto done;
728 for (;;) {
729 char *parent_path;
731 err = open_repo(repo, repo_path);
732 if (err == NULL)
733 break;
734 if (err->code != GOT_ERR_NOT_GIT_REPO)
735 goto done;
736 if (repo_path[0] == '/' && repo_path[1] == '\0') {
737 err = got_error(GOT_ERR_NOT_GIT_REPO);
738 goto done;
740 err = got_path_dirname(&parent_path, repo_path);
741 if (err)
742 goto done;
743 free(repo_path);
744 repo_path = parent_path;
747 err = read_gotconfig(repo);
748 if (err)
749 goto done;
751 err = read_gitconfig(repo, global_gitconfig_path);
752 if (err)
753 goto done;
754 if (repo->gitconfig_repository_format_version != 0) {
755 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
756 goto done;
758 for (i = 0; i < repo->nextensions; i++) {
759 char *ext = repo->extensions[i];
760 int j, supported = 0;
761 for (j = 0; j < nitems(repo_extensions); j++) {
762 if (strcmp(ext, repo_extensions[j]) == 0) {
763 supported = 1;
764 break;
767 if (!supported) {
768 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
769 goto done;
773 err = got_repo_list_packidx(&repo->packidx_paths, repo);
774 done:
775 if (err)
776 got_repo_close(repo);
777 else
778 *repop = repo;
779 free(repo_path);
780 return err;
783 const struct got_error *
784 got_repo_close(struct got_repository *repo)
786 const struct got_error *err = NULL, *child_err;
787 struct got_packidx_bloom_filter *bf;
788 struct got_pathlist_entry *pe;
789 size_t i;
791 for (i = 0; i < repo->pack_cache_size; i++) {
792 if (repo->packidx_cache[i] == NULL)
793 break;
794 got_packidx_close(repo->packidx_cache[i]);
797 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
798 &repo->packidx_bloom_filters))) {
799 RB_REMOVE(got_packidx_bloom_filter_tree,
800 &repo->packidx_bloom_filters, bf);
801 bloom_free(bf->bloom);
802 free(bf->bloom);
803 free(bf);
806 for (i = 0; i < repo->pack_cache_size; i++)
807 if (repo->packs[i].path_packfile)
808 if (repo->packs[i].path_packfile)
809 got_pack_close(&repo->packs[i]);
811 free(repo->path);
812 free(repo->path_git_dir);
814 got_object_cache_close(&repo->objcache);
815 got_object_cache_close(&repo->treecache);
816 got_object_cache_close(&repo->commitcache);
817 got_object_cache_close(&repo->tagcache);
818 got_object_cache_close(&repo->rawcache);
820 for (i = 0; i < nitems(repo->privsep_children); i++) {
821 if (repo->privsep_children[i].imsg_fd == -1)
822 continue;
823 imsg_clear(repo->privsep_children[i].ibuf);
824 free(repo->privsep_children[i].ibuf);
825 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
826 child_err = got_privsep_wait_for_child(
827 repo->privsep_children[i].pid);
828 if (child_err && err == NULL)
829 err = child_err;
830 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
831 err == NULL)
832 err = got_error_from_errno("close");
835 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
836 err == NULL)
837 err = got_error_from_errno("close");
839 if (repo->gotconfig)
840 got_gotconfig_free(repo->gotconfig);
841 free(repo->gitconfig_author_name);
842 free(repo->gitconfig_author_email);
843 for (i = 0; i < repo->ngitconfig_remotes; i++)
844 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
845 free(repo->gitconfig_remotes);
846 for (i = 0; i < repo->nextensions; i++)
847 free(repo->extensions[i]);
848 free(repo->extensions);
850 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
851 free((void *)pe->path);
852 got_pathlist_free(&repo->packidx_paths);
853 free(repo);
855 return err;
858 void
859 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
861 int i;
863 free(repo->name);
864 repo->name = NULL;
865 free(repo->fetch_url);
866 repo->fetch_url = NULL;
867 free(repo->send_url);
868 repo->send_url = NULL;
869 for (i = 0; i < repo->nfetch_branches; i++)
870 free(repo->fetch_branches[i]);
871 free(repo->fetch_branches);
872 repo->fetch_branches = NULL;
873 repo->nfetch_branches = 0;
874 for (i = 0; i < repo->nsend_branches; i++)
875 free(repo->send_branches[i]);
876 free(repo->send_branches);
877 repo->send_branches = NULL;
878 repo->nsend_branches = 0;
881 const struct got_error *
882 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
883 const char *input_path)
885 const struct got_error *err = NULL;
886 const char *repo_abspath = NULL;
887 size_t repolen, len;
888 char *canonpath, *path = NULL;
890 *in_repo_path = NULL;
892 canonpath = strdup(input_path);
893 if (canonpath == NULL) {
894 err = got_error_from_errno("strdup");
895 goto done;
897 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
898 if (err)
899 goto done;
901 repo_abspath = got_repo_get_path(repo);
903 if (canonpath[0] == '\0') {
904 path = strdup(canonpath);
905 if (path == NULL) {
906 err = got_error_from_errno("strdup");
907 goto done;
909 } else {
910 path = realpath(canonpath, NULL);
911 if (path == NULL) {
912 if (errno != ENOENT) {
913 err = got_error_from_errno2("realpath",
914 canonpath);
915 goto done;
917 /*
918 * Path is not on disk.
919 * Assume it is already relative to repository root.
920 */
921 path = strdup(canonpath);
922 if (path == NULL) {
923 err = got_error_from_errno("strdup");
924 goto done;
928 repolen = strlen(repo_abspath);
929 len = strlen(path);
932 if (strcmp(path, repo_abspath) == 0) {
933 free(path);
934 path = strdup("");
935 if (path == NULL) {
936 err = got_error_from_errno("strdup");
937 goto done;
939 } else if (len > repolen &&
940 got_path_is_child(path, repo_abspath, repolen)) {
941 /* Matched an on-disk path inside repository. */
942 if (got_repo_is_bare(repo)) {
943 /*
944 * Matched an on-disk path inside repository
945 * database. Treat input as repository-relative.
946 */
947 free(path);
948 path = canonpath;
949 canonpath = NULL;
950 } else {
951 char *child;
952 /* Strip common prefix with repository path. */
953 err = got_path_skip_common_ancestor(&child,
954 repo_abspath, path);
955 if (err)
956 goto done;
957 free(path);
958 path = child;
960 } else {
961 /*
962 * Matched unrelated on-disk path.
963 * Treat input as repository-relative.
964 */
965 free(path);
966 path = canonpath;
967 canonpath = NULL;
971 /* Make in-repository path absolute */
972 if (path[0] != '/') {
973 char *abspath;
974 if (asprintf(&abspath, "/%s", path) == -1) {
975 err = got_error_from_errno("asprintf");
976 goto done;
978 free(path);
979 path = abspath;
982 done:
983 free(canonpath);
984 if (err)
985 free(path);
986 else
987 *in_repo_path = path;
988 return err;
991 static const struct got_error *
992 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
993 const char *path_packidx)
995 const struct got_error *err = NULL;
996 size_t i;
998 for (i = 0; i < repo->pack_cache_size; i++) {
999 if (repo->packidx_cache[i] == NULL)
1000 break;
1001 if (strcmp(repo->packidx_cache[i]->path_packidx,
1002 path_packidx) == 0) {
1003 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1006 if (i == repo->pack_cache_size) {
1007 do {
1008 i--;
1009 } while (i > 0 && repo->pinned_packidx >= 0 &&
1010 i == repo->pinned_packidx);
1011 err = got_packidx_close(repo->packidx_cache[i]);
1012 if (err)
1013 return err;
1016 repo->packidx_cache[i] = packidx;
1018 return NULL;
1021 int
1022 got_repo_is_packidx_filename(const char *name, size_t len)
1024 if (len != GOT_PACKIDX_NAMELEN)
1025 return 0;
1027 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1028 return 0;
1030 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1031 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1032 return 0;
1034 return 1;
1037 static struct got_packidx_bloom_filter *
1038 get_packidx_bloom_filter(struct got_repository *repo,
1039 const char *path, size_t path_len)
1041 struct got_packidx_bloom_filter key;
1043 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1044 return NULL; /* XXX */
1045 key.path_len = path_len;
1047 return RB_FIND(got_packidx_bloom_filter_tree,
1048 &repo->packidx_bloom_filters, &key);
1051 int
1052 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1053 const char *path_packidx, struct got_object_id *id)
1055 struct got_packidx_bloom_filter *bf;
1057 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1058 if (bf)
1059 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1061 /* No bloom filter means this pack index must be searched. */
1062 return 1;
1065 static const struct got_error *
1066 add_packidx_bloom_filter(struct got_repository *repo,
1067 struct got_packidx *packidx, const char *path_packidx)
1069 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1070 struct got_packidx_bloom_filter *bf;
1071 size_t len;
1074 * Don't use bloom filters for very large pack index files.
1075 * Large pack files will contain a relatively large fraction
1076 * of our objects so we will likely need to visit them anyway.
1077 * The more objects a pack file contains the higher the probability
1078 * of a false-positive match from the bloom filter. And reading
1079 * all object IDs from a large pack index file can be expensive.
1081 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1082 return NULL;
1084 /* Do we already have a filter for this pack index? */
1085 if (get_packidx_bloom_filter(repo, path_packidx,
1086 strlen(path_packidx)) != NULL)
1087 return NULL;
1089 bf = calloc(1, sizeof(*bf));
1090 if (bf == NULL)
1091 return got_error_from_errno("calloc");
1092 bf->bloom = calloc(1, sizeof(*bf->bloom));
1093 if (bf->bloom == NULL) {
1094 free(bf);
1095 return got_error_from_errno("calloc");
1098 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1099 if (len >= sizeof(bf->path)) {
1100 free(bf->bloom);
1101 free(bf);
1102 return got_error(GOT_ERR_NO_SPACE);
1104 bf->path_len = len;
1106 /* Minimum size supported by our bloom filter is 1000 entries. */
1107 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1108 for (i = 0; i < nobjects; i++) {
1109 struct got_packidx_object_id *id;
1110 id = &packidx->hdr.sorted_ids[i];
1111 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1114 RB_INSERT(got_packidx_bloom_filter_tree,
1115 &repo->packidx_bloom_filters, bf);
1116 return NULL;
1119 static void
1120 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1122 struct got_pathlist_entry *pe;
1124 while (!TAILQ_EMPTY(packidx_paths)) {
1125 pe = TAILQ_FIRST(packidx_paths);
1126 TAILQ_REMOVE(packidx_paths, pe, entry);
1127 free((char *)pe->path);
1128 free(pe);
1132 static const struct got_error *
1133 refresh_packidx_paths(struct got_repository *repo)
1135 const struct got_error *err = NULL;
1136 char *objects_pack_dir = NULL;
1137 struct stat sb;
1139 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1140 if (objects_pack_dir == NULL)
1141 return got_error_from_errno("got_repo_get_path_objects_pack");
1143 if (stat(objects_pack_dir, &sb) == -1) {
1144 if (errno != ENOENT) {
1145 err = got_error_from_errno2("stat", objects_pack_dir);
1146 goto done;
1148 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1149 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1150 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1151 purge_packidx_paths(&repo->packidx_paths);
1152 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1153 if (err)
1154 goto done;
1156 done:
1157 free(objects_pack_dir);
1158 return err;
1161 const struct got_error *
1162 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1163 struct got_repository *repo, struct got_object_id *id)
1165 const struct got_error *err;
1166 struct got_pathlist_entry *pe;
1167 size_t i;
1169 /* Search pack index cache. */
1170 for (i = 0; i < repo->pack_cache_size; i++) {
1171 if (repo->packidx_cache[i] == NULL)
1172 break;
1173 if (!got_repo_check_packidx_bloom_filter(repo,
1174 repo->packidx_cache[i]->path_packidx, id))
1175 continue; /* object will not be found in this index */
1176 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1177 if (*idx != -1) {
1178 *packidx = repo->packidx_cache[i];
1180 * Move this cache entry to the front. Repeatedly
1181 * searching a wrong pack index can be expensive.
1183 if (i > 0) {
1184 memmove(&repo->packidx_cache[1],
1185 &repo->packidx_cache[0],
1186 i * sizeof(repo->packidx_cache[0]));
1187 repo->packidx_cache[0] = *packidx;
1188 if (repo->pinned_packidx >= 0 &&
1189 repo->pinned_packidx < i)
1190 repo->pinned_packidx++;
1191 else if (repo->pinned_packidx == i)
1192 repo->pinned_packidx = 0;
1194 return NULL;
1197 /* No luck. Search the filesystem. */
1199 err = refresh_packidx_paths(repo);
1200 if (err)
1201 return err;
1203 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1204 const char *path_packidx = pe->path;
1205 int is_cached = 0;
1207 if (!got_repo_check_packidx_bloom_filter(repo,
1208 pe->path, id))
1209 continue; /* object will not be found in this index */
1211 for (i = 0; i < repo->pack_cache_size; i++) {
1212 if (repo->packidx_cache[i] == NULL)
1213 break;
1214 if (strcmp(repo->packidx_cache[i]->path_packidx,
1215 path_packidx) == 0) {
1216 is_cached = 1;
1217 break;
1220 if (is_cached)
1221 continue; /* already searched */
1223 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1224 path_packidx, 0);
1225 if (err)
1226 goto done;
1228 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1229 if (err)
1230 goto done;
1232 err = cache_packidx(repo, *packidx, path_packidx);
1233 if (err)
1234 goto done;
1236 *idx = got_packidx_get_object_idx(*packidx, id);
1237 if (*idx != -1) {
1238 err = NULL; /* found the object */
1239 goto done;
1243 err = got_error_no_obj(id);
1244 done:
1245 return err;
1248 const struct got_error *
1249 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1250 struct got_repository *repo)
1252 const struct got_error *err = NULL;
1253 DIR *packdir = NULL;
1254 struct dirent *dent;
1255 char *path_packidx = NULL;
1256 int packdir_fd;
1257 struct stat sb;
1259 packdir_fd = openat(got_repo_get_fd(repo),
1260 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1261 if (packdir_fd == -1) {
1262 return got_error_from_errno_fmt("openat: %s/%s",
1263 got_repo_get_path_git_dir(repo),
1264 GOT_OBJECTS_PACK_DIR);
1267 packdir = fdopendir(packdir_fd);
1268 if (packdir == NULL) {
1269 err = got_error_from_errno("fdopendir");
1270 goto done;
1273 if (fstat(packdir_fd, &sb) == -1) {
1274 err = got_error_from_errno("fstat");
1275 goto done;
1277 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1278 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1280 while ((dent = readdir(packdir)) != NULL) {
1281 if (!got_repo_is_packidx_filename(dent->d_name,
1282 strlen(dent->d_name)))
1283 continue;
1285 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1286 dent->d_name) == -1) {
1287 err = got_error_from_errno("asprintf");
1288 path_packidx = NULL;
1289 break;
1292 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1293 if (err)
1294 break;
1296 done:
1297 if (err)
1298 free(path_packidx);
1299 if (packdir && closedir(packdir) != 0 && err == NULL)
1300 err = got_error_from_errno("closedir");
1301 return err;
1304 const struct got_error *
1305 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1306 struct got_repository *repo)
1308 const struct got_error *err;
1309 size_t i;
1311 *packidx = NULL;
1313 /* Search pack index cache. */
1314 for (i = 0; i < repo->pack_cache_size; i++) {
1315 if (repo->packidx_cache[i] == NULL)
1316 break;
1317 if (strcmp(repo->packidx_cache[i]->path_packidx,
1318 path_packidx) == 0) {
1319 *packidx = repo->packidx_cache[i];
1320 return NULL;
1323 /* No luck. Search the filesystem. */
1325 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1326 path_packidx, 0);
1327 if (err)
1328 return err;
1330 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1331 if (err)
1332 goto done;
1334 err = cache_packidx(repo, *packidx, path_packidx);
1335 done:
1336 if (err) {
1337 got_packidx_close(*packidx);
1338 *packidx = NULL;
1340 return err;
1343 static const struct got_error *
1344 read_packfile_hdr(int fd, struct got_packidx *packidx)
1346 const struct got_error *err = NULL;
1347 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1348 struct got_packfile_hdr hdr;
1349 ssize_t n;
1351 n = read(fd, &hdr, sizeof(hdr));
1352 if (n < 0)
1353 return got_error_from_errno("read");
1354 if (n != sizeof(hdr))
1355 return got_error(GOT_ERR_BAD_PACKFILE);
1357 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1358 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1359 be32toh(hdr.nobjects) != totobj)
1360 err = got_error(GOT_ERR_BAD_PACKFILE);
1362 return err;
1365 static const struct got_error *
1366 open_packfile(int *fd, struct got_repository *repo,
1367 const char *relpath, struct got_packidx *packidx)
1369 const struct got_error *err = NULL;
1371 *fd = openat(got_repo_get_fd(repo), relpath,
1372 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1373 if (*fd == -1)
1374 return got_error_from_errno_fmt("openat: %s/%s",
1375 got_repo_get_path_git_dir(repo), relpath);
1377 if (packidx) {
1378 err = read_packfile_hdr(*fd, packidx);
1379 if (err) {
1380 close(*fd);
1381 *fd = -1;
1385 return err;
1388 const struct got_error *
1389 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1390 const char *path_packfile, struct got_packidx *packidx)
1392 const struct got_error *err = NULL;
1393 struct got_pack *pack = NULL;
1394 struct stat sb;
1395 size_t i;
1397 if (packp)
1398 *packp = NULL;
1400 for (i = 0; i < repo->pack_cache_size; i++) {
1401 pack = &repo->packs[i];
1402 if (pack->path_packfile == NULL)
1403 break;
1404 if (strcmp(pack->path_packfile, path_packfile) == 0)
1405 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1408 if (i == repo->pack_cache_size) {
1409 struct got_pack tmp;
1410 do {
1411 i--;
1412 } while (i > 0 && repo->pinned_pack >= 0 &&
1413 i == repo->pinned_pack);
1414 err = got_pack_close(&repo->packs[i]);
1415 if (err)
1416 return err;
1417 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1418 return got_error_from_errno("ftruncate");
1419 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1420 return got_error_from_errno("ftruncate");
1421 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1422 memcpy(&repo->packs[i], &repo->packs[0],
1423 sizeof(repo->packs[i]));
1424 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1425 if (repo->pinned_pack == 0)
1426 repo->pinned_pack = i;
1427 else if (repo->pinned_pack == i)
1428 repo->pinned_pack = 0;
1429 i = 0;
1432 pack = &repo->packs[i];
1434 pack->path_packfile = strdup(path_packfile);
1435 if (pack->path_packfile == NULL) {
1436 err = got_error_from_errno("strdup");
1437 goto done;
1440 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1441 if (err)
1442 goto done;
1444 if (fstat(pack->fd, &sb) != 0) {
1445 err = got_error_from_errno("fstat");
1446 goto done;
1448 pack->filesize = sb.st_size;
1450 pack->privsep_child = NULL;
1452 err = got_delta_cache_alloc(&pack->delta_cache);
1453 if (err)
1454 goto done;
1456 #ifndef GOT_PACK_NO_MMAP
1457 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1458 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1459 pack->fd, 0);
1460 if (pack->map == MAP_FAILED) {
1461 if (errno != ENOMEM) {
1462 err = got_error_from_errno("mmap");
1463 goto done;
1465 pack->map = NULL; /* fall back to read(2) */
1468 #endif
1469 done:
1470 if (err) {
1471 if (pack)
1472 got_pack_close(pack);
1473 } else if (packp)
1474 *packp = pack;
1475 return err;
1478 struct got_pack *
1479 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1481 struct got_pack *pack = NULL;
1482 size_t i;
1484 for (i = 0; i < repo->pack_cache_size; i++) {
1485 pack = &repo->packs[i];
1486 if (pack->path_packfile == NULL)
1487 break;
1488 if (strcmp(pack->path_packfile, path_packfile) == 0)
1489 return pack;
1492 return NULL;
1495 const struct got_error *
1496 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1497 struct got_pack *pack)
1499 size_t i;
1500 int pinned_pack = -1, pinned_packidx = -1;
1502 for (i = 0; i < repo->pack_cache_size; i++) {
1503 if (repo->packidx_cache[i] &&
1504 strcmp(repo->packidx_cache[i]->path_packidx,
1505 packidx->path_packidx) == 0)
1506 pinned_packidx = i;
1507 if (repo->packs[i].path_packfile &&
1508 strcmp(repo->packs[i].path_packfile,
1509 pack->path_packfile) == 0)
1510 pinned_pack = i;
1513 if (pinned_packidx == -1 || pinned_pack == -1)
1514 return got_error(GOT_ERR_PIN_PACK);
1516 repo->pinned_pack = pinned_pack;
1517 repo->pinned_packidx = pinned_packidx;
1518 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1519 return NULL;
1522 struct got_pack *
1523 got_repo_get_pinned_pack(struct got_repository *repo)
1525 if (repo->pinned_pack >= 0 &&
1526 repo->pinned_pack < repo->pack_cache_size)
1527 return &repo->packs[repo->pinned_pack];
1529 return NULL;
1532 void
1533 got_repo_unpin_pack(struct got_repository *repo)
1535 repo->pinned_packidx = -1;
1536 repo->pinned_pack = -1;
1537 repo->pinned_pid = 0;
1540 const struct got_error *
1541 got_repo_init(const char *repo_path, const char *head_name)
1543 const struct got_error *err = NULL;
1544 const char *dirnames[] = {
1545 GOT_OBJECTS_DIR,
1546 GOT_OBJECTS_PACK_DIR,
1547 GOT_REFS_DIR,
1549 const char *description_str = "Unnamed repository; "
1550 "edit this file 'description' to name the repository.";
1551 const char *headref = "ref: refs/heads/";
1552 const char *gitconfig_str = "[core]\n"
1553 "\trepositoryformatversion = 0\n"
1554 "\tfilemode = true\n"
1555 "\tbare = true\n";
1556 char *headref_str, *path;
1557 size_t i;
1559 if (!got_path_dir_is_empty(repo_path))
1560 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1562 for (i = 0; i < nitems(dirnames); i++) {
1563 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1564 return got_error_from_errno("asprintf");
1566 err = got_path_mkdir(path);
1567 free(path);
1568 if (err)
1569 return err;
1572 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1573 return got_error_from_errno("asprintf");
1574 err = got_path_create_file(path, description_str);
1575 free(path);
1576 if (err)
1577 return err;
1579 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1580 return got_error_from_errno("asprintf");
1581 if (asprintf(&headref_str, "%s%s", headref,
1582 head_name ? head_name : "main") == -1) {
1583 free(path);
1584 return got_error_from_errno("asprintf");
1586 err = got_path_create_file(path, headref_str);
1587 free(headref_str);
1588 free(path);
1589 if (err)
1590 return err;
1592 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1593 return got_error_from_errno("asprintf");
1594 err = got_path_create_file(path, gitconfig_str);
1595 free(path);
1596 if (err)
1597 return err;
1599 return NULL;
1602 static const struct got_error *
1603 match_packed_object(struct got_object_id **unique_id,
1604 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1606 const struct got_error *err = NULL;
1607 struct got_object_id_queue matched_ids;
1608 struct got_pathlist_entry *pe;
1610 STAILQ_INIT(&matched_ids);
1612 err = refresh_packidx_paths(repo);
1613 if (err)
1614 return err;
1616 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1617 const char *path_packidx = pe->path;
1618 struct got_packidx *packidx;
1619 struct got_object_qid *qid;
1621 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1622 path_packidx, 0);
1623 if (err)
1624 break;
1626 err = got_packidx_match_id_str_prefix(&matched_ids,
1627 packidx, id_str_prefix);
1628 if (err) {
1629 got_packidx_close(packidx);
1630 break;
1632 err = got_packidx_close(packidx);
1633 if (err)
1634 break;
1636 STAILQ_FOREACH(qid, &matched_ids, entry) {
1637 if (obj_type != GOT_OBJ_TYPE_ANY) {
1638 int matched_type;
1639 err = got_object_get_type(&matched_type, repo,
1640 &qid->id);
1641 if (err)
1642 goto done;
1643 if (matched_type != obj_type)
1644 continue;
1646 if (*unique_id == NULL) {
1647 *unique_id = got_object_id_dup(&qid->id);
1648 if (*unique_id == NULL) {
1649 err = got_error_from_errno("malloc");
1650 goto done;
1652 } else {
1653 if (got_object_id_cmp(*unique_id,
1654 &qid->id) == 0)
1655 continue; /* packed multiple times */
1656 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1657 goto done;
1661 done:
1662 got_object_id_queue_free(&matched_ids);
1663 if (err) {
1664 free(*unique_id);
1665 *unique_id = NULL;
1667 return err;
1670 static const struct got_error *
1671 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1672 const char *object_dir, const char *id_str_prefix, int obj_type,
1673 struct got_repository *repo)
1675 const struct got_error *err = NULL;
1676 char *path, *id_str = NULL;
1677 DIR *dir = NULL;
1678 struct dirent *dent;
1679 struct got_object_id id;
1681 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1682 err = got_error_from_errno("asprintf");
1683 goto done;
1686 dir = opendir(path);
1687 if (dir == NULL) {
1688 if (errno == ENOENT) {
1689 err = NULL;
1690 goto done;
1692 err = got_error_from_errno2("opendir", path);
1693 goto done;
1695 while ((dent = readdir(dir)) != NULL) {
1696 int cmp;
1698 free(id_str);
1699 id_str = NULL;
1701 if (strcmp(dent->d_name, ".") == 0 ||
1702 strcmp(dent->d_name, "..") == 0)
1703 continue;
1705 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1706 err = got_error_from_errno("asprintf");
1707 goto done;
1710 if (!got_parse_sha1_digest(id.sha1, id_str))
1711 continue;
1714 * Directory entries do not necessarily appear in
1715 * sorted order, so we must iterate over all of them.
1717 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1718 if (cmp != 0)
1719 continue;
1721 if (*unique_id == NULL) {
1722 if (obj_type != GOT_OBJ_TYPE_ANY) {
1723 int matched_type;
1724 err = got_object_get_type(&matched_type, repo,
1725 &id);
1726 if (err)
1727 goto done;
1728 if (matched_type != obj_type)
1729 continue;
1731 *unique_id = got_object_id_dup(&id);
1732 if (*unique_id == NULL) {
1733 err = got_error_from_errno("got_object_id_dup");
1734 goto done;
1736 } else {
1737 if (got_object_id_cmp(*unique_id, &id) == 0)
1738 continue; /* both packed and loose */
1739 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1740 goto done;
1743 done:
1744 if (dir && closedir(dir) != 0 && err == NULL)
1745 err = got_error_from_errno("closedir");
1746 if (err) {
1747 free(*unique_id);
1748 *unique_id = NULL;
1750 free(id_str);
1751 free(path);
1752 return err;
1755 const struct got_error *
1756 got_repo_match_object_id_prefix(struct got_object_id **id,
1757 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1759 const struct got_error *err = NULL;
1760 char *path_objects = NULL, *object_dir = NULL;
1761 size_t len;
1762 int i;
1764 *id = NULL;
1766 path_objects = got_repo_get_path_objects(repo);
1768 len = strlen(id_str_prefix);
1769 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1770 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1771 goto done;
1774 for (i = 0; i < len; i++) {
1775 if (isxdigit((unsigned char)id_str_prefix[i]))
1776 continue;
1777 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1778 goto done;
1781 if (len >= 2) {
1782 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1783 if (err)
1784 goto done;
1785 object_dir = strndup(id_str_prefix, 2);
1786 if (object_dir == NULL) {
1787 err = got_error_from_errno("strdup");
1788 goto done;
1790 err = match_loose_object(id, path_objects, object_dir,
1791 id_str_prefix, obj_type, repo);
1792 } else if (len == 1) {
1793 int i;
1794 for (i = 0; i < 0xf; i++) {
1795 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1796 == -1) {
1797 err = got_error_from_errno("asprintf");
1798 goto done;
1800 err = match_packed_object(id, repo, object_dir,
1801 obj_type);
1802 if (err)
1803 goto done;
1804 err = match_loose_object(id, path_objects, object_dir,
1805 id_str_prefix, obj_type, repo);
1806 if (err)
1807 goto done;
1809 } else {
1810 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1811 goto done;
1813 done:
1814 free(path_objects);
1815 free(object_dir);
1816 if (err) {
1817 free(*id);
1818 *id = NULL;
1819 } else if (*id == NULL) {
1820 switch (obj_type) {
1821 case GOT_OBJ_TYPE_BLOB:
1822 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1823 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1824 break;
1825 case GOT_OBJ_TYPE_TREE:
1826 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1827 GOT_OBJ_LABEL_TREE, id_str_prefix);
1828 break;
1829 case GOT_OBJ_TYPE_COMMIT:
1830 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1831 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1832 break;
1833 case GOT_OBJ_TYPE_TAG:
1834 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1835 GOT_OBJ_LABEL_TAG, id_str_prefix);
1836 break;
1837 default:
1838 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1839 break;
1843 return err;
1846 const struct got_error *
1847 got_repo_match_object_id(struct got_object_id **id, char **label,
1848 const char *id_str, int obj_type, struct got_reflist_head *refs,
1849 struct got_repository *repo)
1851 const struct got_error *err;
1852 struct got_tag_object *tag;
1853 struct got_reference *ref = NULL;
1855 *id = NULL;
1856 if (label)
1857 *label = NULL;
1859 if (refs) {
1860 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1861 refs, repo);
1862 if (err == NULL) {
1863 *id = got_object_id_dup(
1864 got_object_tag_get_object_id(tag));
1865 if (*id == NULL)
1866 err = got_error_from_errno("got_object_id_dup");
1867 else if (label && asprintf(label, "refs/tags/%s",
1868 got_object_tag_get_name(tag)) == -1) {
1869 err = got_error_from_errno("asprintf");
1870 free(*id);
1871 *id = NULL;
1873 got_object_tag_close(tag);
1874 return err;
1875 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1876 err->code != GOT_ERR_NO_OBJ)
1877 return err;
1880 err = got_ref_open(&ref, repo, id_str, 0);
1881 if (err == NULL) {
1882 err = got_ref_resolve(id, repo, ref);
1883 if (err)
1884 goto done;
1885 if (label) {
1886 *label = strdup(got_ref_get_name(ref));
1887 if (*label == NULL) {
1888 err = got_error_from_errno("strdup");
1889 goto done;
1892 } else {
1893 if (err->code != GOT_ERR_NOT_REF &&
1894 err->code != GOT_ERR_BAD_REF_NAME)
1895 goto done;
1896 err = got_repo_match_object_id_prefix(id, id_str,
1897 obj_type, repo);
1898 if (err) {
1899 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1900 err = got_error_not_ref(id_str);
1901 goto done;
1903 if (label) {
1904 err = got_object_id_str(label, *id);
1905 if (*label == NULL) {
1906 err = got_error_from_errno("strdup");
1907 goto done;
1911 done:
1912 if (ref)
1913 got_ref_close(ref);
1914 return err;
1917 const struct got_error *
1918 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1919 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1921 const struct got_error *err = NULL;
1922 struct got_reflist_entry *re;
1923 struct got_object_id *tag_id;
1924 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1926 *tag = NULL;
1928 TAILQ_FOREACH(re, refs, entry) {
1929 const char *refname;
1930 refname = got_ref_get_name(re->ref);
1931 if (got_ref_is_symbolic(re->ref))
1932 continue;
1933 if (strncmp(refname, "refs/tags/", 10) != 0)
1934 continue;
1935 if (!name_is_absolute)
1936 refname += strlen("refs/tags/");
1937 if (strcmp(refname, name) != 0)
1938 continue;
1939 err = got_ref_resolve(&tag_id, repo, re->ref);
1940 if (err)
1941 break;
1942 err = got_object_open_as_tag(tag, repo, tag_id);
1943 free(tag_id);
1944 if (err)
1945 break;
1946 if (obj_type == GOT_OBJ_TYPE_ANY ||
1947 got_object_tag_get_object_type(*tag) == obj_type)
1948 break;
1949 got_object_tag_close(*tag);
1950 *tag = NULL;
1953 if (err == NULL && *tag == NULL)
1954 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1955 GOT_OBJ_LABEL_TAG, name);
1956 return err;
1959 static const struct got_error *
1960 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1961 const char *name, mode_t mode, struct got_object_id *blob_id)
1963 const struct got_error *err = NULL;
1965 *new_te = NULL;
1967 *new_te = calloc(1, sizeof(**new_te));
1968 if (*new_te == NULL)
1969 return got_error_from_errno("calloc");
1971 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1972 sizeof((*new_te)->name)) {
1973 err = got_error(GOT_ERR_NO_SPACE);
1974 goto done;
1977 if (S_ISLNK(mode)) {
1978 (*new_te)->mode = S_IFLNK;
1979 } else {
1980 (*new_te)->mode = S_IFREG;
1981 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1983 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1984 done:
1985 if (err && *new_te) {
1986 free(*new_te);
1987 *new_te = NULL;
1989 return err;
1992 static const struct got_error *
1993 import_file(struct got_tree_entry **new_te, struct dirent *de,
1994 const char *path, struct got_repository *repo)
1996 const struct got_error *err;
1997 struct got_object_id *blob_id = NULL;
1998 char *filepath;
1999 struct stat sb;
2001 if (asprintf(&filepath, "%s%s%s", path,
2002 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2003 return got_error_from_errno("asprintf");
2005 if (lstat(filepath, &sb) != 0) {
2006 err = got_error_from_errno2("lstat", path);
2007 goto done;
2010 err = got_object_blob_create(&blob_id, filepath, repo);
2011 if (err)
2012 goto done;
2014 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2015 blob_id);
2016 done:
2017 free(filepath);
2018 if (err)
2019 free(blob_id);
2020 return err;
2023 static const struct got_error *
2024 insert_tree_entry(struct got_tree_entry *new_te,
2025 struct got_pathlist_head *paths)
2027 const struct got_error *err = NULL;
2028 struct got_pathlist_entry *new_pe;
2030 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2031 if (err)
2032 return err;
2033 if (new_pe == NULL)
2034 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2035 return NULL;
2038 static const struct got_error *write_tree(struct got_object_id **,
2039 const char *, struct got_pathlist_head *, struct got_repository *,
2040 got_repo_import_cb progress_cb, void *progress_arg);
2042 static const struct got_error *
2043 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2044 const char *path, struct got_pathlist_head *ignores,
2045 struct got_repository *repo,
2046 got_repo_import_cb progress_cb, void *progress_arg)
2048 const struct got_error *err;
2049 struct got_object_id *id = NULL;
2050 char *subdirpath;
2052 if (asprintf(&subdirpath, "%s%s%s", path,
2053 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2054 return got_error_from_errno("asprintf");
2056 (*new_te) = calloc(1, sizeof(**new_te));
2057 if (*new_te == NULL)
2058 return got_error_from_errno("calloc");
2059 (*new_te)->mode = S_IFDIR;
2060 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2061 sizeof((*new_te)->name)) {
2062 err = got_error(GOT_ERR_NO_SPACE);
2063 goto done;
2065 err = write_tree(&id, subdirpath, ignores, repo,
2066 progress_cb, progress_arg);
2067 if (err)
2068 goto done;
2069 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2071 done:
2072 free(id);
2073 free(subdirpath);
2074 if (err) {
2075 free(*new_te);
2076 *new_te = NULL;
2078 return err;
2081 static const struct got_error *
2082 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2083 struct got_pathlist_head *ignores, struct got_repository *repo,
2084 got_repo_import_cb progress_cb, void *progress_arg)
2086 const struct got_error *err = NULL;
2087 DIR *dir;
2088 struct dirent *de;
2089 int nentries;
2090 struct got_tree_entry *new_te = NULL;
2091 struct got_pathlist_head paths;
2092 struct got_pathlist_entry *pe;
2094 *new_tree_id = NULL;
2096 TAILQ_INIT(&paths);
2098 dir = opendir(path_dir);
2099 if (dir == NULL) {
2100 err = got_error_from_errno2("opendir", path_dir);
2101 goto done;
2104 nentries = 0;
2105 while ((de = readdir(dir)) != NULL) {
2106 int ignore = 0;
2107 int type;
2109 if (strcmp(de->d_name, ".") == 0 ||
2110 strcmp(de->d_name, "..") == 0)
2111 continue;
2113 TAILQ_FOREACH(pe, ignores, entry) {
2114 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2115 ignore = 1;
2116 break;
2119 if (ignore)
2120 continue;
2122 err = got_path_dirent_type(&type, path_dir, de);
2123 if (err)
2124 goto done;
2126 if (type == DT_DIR) {
2127 err = import_subdir(&new_te, de, path_dir,
2128 ignores, repo, progress_cb, progress_arg);
2129 if (err) {
2130 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2131 goto done;
2132 err = NULL;
2133 continue;
2135 } else if (type == DT_REG || type == DT_LNK) {
2136 err = import_file(&new_te, de, path_dir, repo);
2137 if (err)
2138 goto done;
2139 } else
2140 continue;
2142 err = insert_tree_entry(new_te, &paths);
2143 if (err)
2144 goto done;
2145 nentries++;
2148 if (TAILQ_EMPTY(&paths)) {
2149 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2150 "cannot create tree without any entries");
2151 goto done;
2154 TAILQ_FOREACH(pe, &paths, entry) {
2155 struct got_tree_entry *te = pe->data;
2156 char *path;
2157 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2158 continue;
2159 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2160 err = got_error_from_errno("asprintf");
2161 goto done;
2163 err = (*progress_cb)(progress_arg, path);
2164 free(path);
2165 if (err)
2166 goto done;
2169 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2170 done:
2171 if (dir)
2172 closedir(dir);
2173 got_pathlist_free(&paths);
2174 return err;
2177 const struct got_error *
2178 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2179 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2180 struct got_repository *repo, got_repo_import_cb progress_cb,
2181 void *progress_arg)
2183 const struct got_error *err;
2184 struct got_object_id *new_tree_id;
2186 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2187 progress_cb, progress_arg);
2188 if (err)
2189 return err;
2191 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2192 author, time(NULL), author, time(NULL), logmsg, repo);
2193 free(new_tree_id);
2194 return err;
2197 const struct got_error *
2198 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2199 struct got_repository *repo)
2201 const struct got_error *err = NULL;
2202 char *path_objects = NULL, *path = NULL;
2203 DIR *dir = NULL;
2204 struct got_object_id id;
2205 int i;
2207 *nobjects = 0;
2208 *ondisk_size = 0;
2210 path_objects = got_repo_get_path_objects(repo);
2211 if (path_objects == NULL)
2212 return got_error_from_errno("got_repo_get_path_objects");
2214 for (i = 0; i <= 0xff; i++) {
2215 struct dirent *dent;
2217 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2218 err = got_error_from_errno("asprintf");
2219 break;
2222 dir = opendir(path);
2223 if (dir == NULL) {
2224 if (errno == ENOENT) {
2225 err = NULL;
2226 continue;
2228 err = got_error_from_errno2("opendir", path);
2229 break;
2232 while ((dent = readdir(dir)) != NULL) {
2233 char *id_str;
2234 int fd;
2235 struct stat sb;
2237 if (strcmp(dent->d_name, ".") == 0 ||
2238 strcmp(dent->d_name, "..") == 0)
2239 continue;
2241 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2242 err = got_error_from_errno("asprintf");
2243 goto done;
2246 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2247 free(id_str);
2248 continue;
2250 free(id_str);
2252 err = got_object_open_loose_fd(&fd, &id, repo);
2253 if (err)
2254 goto done;
2256 if (fstat(fd, &sb) == -1) {
2257 err = got_error_from_errno("fstat");
2258 close(fd);
2259 goto done;
2261 (*nobjects)++;
2262 (*ondisk_size) += sb.st_size;
2264 if (close(fd) == -1) {
2265 err = got_error_from_errno("close");
2266 goto done;
2270 if (closedir(dir) != 0) {
2271 err = got_error_from_errno("closedir");
2272 goto done;
2274 dir = NULL;
2276 free(path);
2277 path = NULL;
2279 done:
2280 if (dir && closedir(dir) != 0 && err == NULL)
2281 err = got_error_from_errno("closedir");
2283 if (err) {
2284 *nobjects = 0;
2285 *ondisk_size = 0;
2287 free(path_objects);
2288 free(path);
2289 return err;
2292 const struct got_error *
2293 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2294 off_t *total_packsize, struct got_repository *repo)
2296 const struct got_error *err = NULL;
2297 DIR *packdir = NULL;
2298 struct dirent *dent;
2299 struct got_packidx *packidx = NULL;
2300 char *path_packidx;
2301 char *path_packfile;
2302 int packdir_fd;
2303 struct stat sb;
2305 *npackfiles = 0;
2306 *nobjects = 0;
2307 *total_packsize = 0;
2309 packdir_fd = openat(got_repo_get_fd(repo),
2310 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2311 if (packdir_fd == -1) {
2312 return got_error_from_errno_fmt("openat: %s/%s",
2313 got_repo_get_path_git_dir(repo),
2314 GOT_OBJECTS_PACK_DIR);
2317 packdir = fdopendir(packdir_fd);
2318 if (packdir == NULL) {
2319 err = got_error_from_errno("fdopendir");
2320 goto done;
2323 while ((dent = readdir(packdir)) != NULL) {
2324 if (!got_repo_is_packidx_filename(dent->d_name,
2325 strlen(dent->d_name)))
2326 continue;
2328 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2329 dent->d_name) == -1) {
2330 err = got_error_from_errno("asprintf");
2331 goto done;
2334 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2335 path_packidx, 0);
2336 free(path_packidx);
2337 if (err)
2338 goto done;
2340 if (fstat(packidx->fd, &sb) == -1)
2341 goto done;
2342 *total_packsize += sb.st_size;
2344 err = got_packidx_get_packfile_path(&path_packfile,
2345 packidx->path_packidx);
2346 if (err)
2347 goto done;
2349 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2350 0) == -1) {
2351 free(path_packfile);
2352 goto done;
2354 free(path_packfile);
2355 *total_packsize += sb.st_size;
2357 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2359 (*npackfiles)++;
2361 got_packidx_close(packidx);
2362 packidx = NULL;
2364 done:
2365 if (packidx)
2366 got_packidx_close(packidx);
2367 if (packdir && closedir(packdir) != 0 && err == NULL)
2368 err = got_error_from_errno("closedir");
2369 if (err) {
2370 *npackfiles = 0;
2371 *nobjects = 0;
2372 *total_packsize = 0;
2374 return err;
2377 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2378 got_packidx_bloom_filter_cmp);