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 size_t i;
790 for (i = 0; i < repo->pack_cache_size; i++) {
791 if (repo->packidx_cache[i] == NULL)
792 break;
793 got_packidx_close(repo->packidx_cache[i]);
796 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
797 &repo->packidx_bloom_filters))) {
798 RB_REMOVE(got_packidx_bloom_filter_tree,
799 &repo->packidx_bloom_filters, bf);
800 bloom_free(bf->bloom);
801 free(bf->bloom);
802 free(bf);
805 for (i = 0; i < repo->pack_cache_size; i++)
806 if (repo->packs[i].path_packfile)
807 if (repo->packs[i].path_packfile)
808 got_pack_close(&repo->packs[i]);
810 free(repo->path);
811 free(repo->path_git_dir);
813 got_object_cache_close(&repo->objcache);
814 got_object_cache_close(&repo->treecache);
815 got_object_cache_close(&repo->commitcache);
816 got_object_cache_close(&repo->tagcache);
817 got_object_cache_close(&repo->rawcache);
819 for (i = 0; i < nitems(repo->privsep_children); i++) {
820 if (repo->privsep_children[i].imsg_fd == -1)
821 continue;
822 imsg_clear(repo->privsep_children[i].ibuf);
823 free(repo->privsep_children[i].ibuf);
824 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
825 child_err = got_privsep_wait_for_child(
826 repo->privsep_children[i].pid);
827 if (child_err && err == NULL)
828 err = child_err;
829 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
830 err == NULL)
831 err = got_error_from_errno("close");
834 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
835 err == NULL)
836 err = got_error_from_errno("close");
838 if (repo->gotconfig)
839 got_gotconfig_free(repo->gotconfig);
840 free(repo->gitconfig_author_name);
841 free(repo->gitconfig_author_email);
842 for (i = 0; i < repo->ngitconfig_remotes; i++)
843 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
844 free(repo->gitconfig_remotes);
845 for (i = 0; i < repo->nextensions; i++)
846 free(repo->extensions[i]);
847 free(repo->extensions);
849 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
850 free(repo);
852 return err;
855 void
856 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
858 int i;
860 free(repo->name);
861 repo->name = NULL;
862 free(repo->fetch_url);
863 repo->fetch_url = NULL;
864 free(repo->send_url);
865 repo->send_url = NULL;
866 for (i = 0; i < repo->nfetch_branches; i++)
867 free(repo->fetch_branches[i]);
868 free(repo->fetch_branches);
869 repo->fetch_branches = NULL;
870 repo->nfetch_branches = 0;
871 for (i = 0; i < repo->nsend_branches; i++)
872 free(repo->send_branches[i]);
873 free(repo->send_branches);
874 repo->send_branches = NULL;
875 repo->nsend_branches = 0;
878 const struct got_error *
879 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
880 const char *input_path)
882 const struct got_error *err = NULL;
883 const char *repo_abspath = NULL;
884 size_t repolen, len;
885 char *canonpath, *path = NULL;
887 *in_repo_path = NULL;
889 canonpath = strdup(input_path);
890 if (canonpath == NULL) {
891 err = got_error_from_errno("strdup");
892 goto done;
894 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
895 if (err)
896 goto done;
898 repo_abspath = got_repo_get_path(repo);
900 if (canonpath[0] == '\0') {
901 path = strdup(canonpath);
902 if (path == NULL) {
903 err = got_error_from_errno("strdup");
904 goto done;
906 } else {
907 path = realpath(canonpath, NULL);
908 if (path == NULL) {
909 if (errno != ENOENT) {
910 err = got_error_from_errno2("realpath",
911 canonpath);
912 goto done;
914 /*
915 * Path is not on disk.
916 * Assume it is already relative to repository root.
917 */
918 path = strdup(canonpath);
919 if (path == NULL) {
920 err = got_error_from_errno("strdup");
921 goto done;
925 repolen = strlen(repo_abspath);
926 len = strlen(path);
929 if (strcmp(path, repo_abspath) == 0) {
930 free(path);
931 path = strdup("");
932 if (path == NULL) {
933 err = got_error_from_errno("strdup");
934 goto done;
936 } else if (len > repolen &&
937 got_path_is_child(path, repo_abspath, repolen)) {
938 /* Matched an on-disk path inside repository. */
939 if (got_repo_is_bare(repo)) {
940 /*
941 * Matched an on-disk path inside repository
942 * database. Treat input as repository-relative.
943 */
944 free(path);
945 path = canonpath;
946 canonpath = NULL;
947 } else {
948 char *child;
949 /* Strip common prefix with repository path. */
950 err = got_path_skip_common_ancestor(&child,
951 repo_abspath, path);
952 if (err)
953 goto done;
954 free(path);
955 path = child;
957 } else {
958 /*
959 * Matched unrelated on-disk path.
960 * Treat input as repository-relative.
961 */
962 free(path);
963 path = canonpath;
964 canonpath = NULL;
968 /* Make in-repository path absolute */
969 if (path[0] != '/') {
970 char *abspath;
971 if (asprintf(&abspath, "/%s", path) == -1) {
972 err = got_error_from_errno("asprintf");
973 goto done;
975 free(path);
976 path = abspath;
979 done:
980 free(canonpath);
981 if (err)
982 free(path);
983 else
984 *in_repo_path = path;
985 return err;
988 static const struct got_error *
989 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
990 const char *path_packidx)
992 const struct got_error *err = NULL;
993 size_t i;
995 for (i = 0; i < repo->pack_cache_size; i++) {
996 if (repo->packidx_cache[i] == NULL)
997 break;
998 if (strcmp(repo->packidx_cache[i]->path_packidx,
999 path_packidx) == 0) {
1000 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1003 if (i == repo->pack_cache_size) {
1004 do {
1005 i--;
1006 } while (i > 0 && repo->pinned_packidx >= 0 &&
1007 i == repo->pinned_packidx);
1008 err = got_packidx_close(repo->packidx_cache[i]);
1009 if (err)
1010 return err;
1013 repo->packidx_cache[i] = packidx;
1015 return NULL;
1018 int
1019 got_repo_is_packidx_filename(const char *name, size_t len)
1021 if (len != GOT_PACKIDX_NAMELEN)
1022 return 0;
1024 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1025 return 0;
1027 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1028 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1029 return 0;
1031 return 1;
1034 static struct got_packidx_bloom_filter *
1035 get_packidx_bloom_filter(struct got_repository *repo,
1036 const char *path, size_t path_len)
1038 struct got_packidx_bloom_filter key;
1040 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1041 return NULL; /* XXX */
1042 key.path_len = path_len;
1044 return RB_FIND(got_packidx_bloom_filter_tree,
1045 &repo->packidx_bloom_filters, &key);
1048 int
1049 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1050 const char *path_packidx, struct got_object_id *id)
1052 struct got_packidx_bloom_filter *bf;
1054 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1055 if (bf)
1056 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1058 /* No bloom filter means this pack index must be searched. */
1059 return 1;
1062 static const struct got_error *
1063 add_packidx_bloom_filter(struct got_repository *repo,
1064 struct got_packidx *packidx, const char *path_packidx)
1066 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1067 struct got_packidx_bloom_filter *bf;
1068 size_t len;
1071 * Don't use bloom filters for very large pack index files.
1072 * Large pack files will contain a relatively large fraction
1073 * of our objects so we will likely need to visit them anyway.
1074 * The more objects a pack file contains the higher the probability
1075 * of a false-positive match from the bloom filter. And reading
1076 * all object IDs from a large pack index file can be expensive.
1078 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1079 return NULL;
1081 /* Do we already have a filter for this pack index? */
1082 if (get_packidx_bloom_filter(repo, path_packidx,
1083 strlen(path_packidx)) != NULL)
1084 return NULL;
1086 bf = calloc(1, sizeof(*bf));
1087 if (bf == NULL)
1088 return got_error_from_errno("calloc");
1089 bf->bloom = calloc(1, sizeof(*bf->bloom));
1090 if (bf->bloom == NULL) {
1091 free(bf);
1092 return got_error_from_errno("calloc");
1095 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1096 if (len >= sizeof(bf->path)) {
1097 free(bf->bloom);
1098 free(bf);
1099 return got_error(GOT_ERR_NO_SPACE);
1101 bf->path_len = len;
1103 /* Minimum size supported by our bloom filter is 1000 entries. */
1104 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1105 for (i = 0; i < nobjects; i++) {
1106 struct got_packidx_object_id *id;
1107 id = &packidx->hdr.sorted_ids[i];
1108 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1111 RB_INSERT(got_packidx_bloom_filter_tree,
1112 &repo->packidx_bloom_filters, bf);
1113 return NULL;
1116 static void
1117 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1119 struct got_pathlist_entry *pe;
1121 while (!TAILQ_EMPTY(packidx_paths)) {
1122 pe = TAILQ_FIRST(packidx_paths);
1123 TAILQ_REMOVE(packidx_paths, pe, entry);
1124 free((char *)pe->path);
1125 free(pe);
1129 static const struct got_error *
1130 refresh_packidx_paths(struct got_repository *repo)
1132 const struct got_error *err = NULL;
1133 char *objects_pack_dir = NULL;
1134 struct stat sb;
1136 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1137 if (objects_pack_dir == NULL)
1138 return got_error_from_errno("got_repo_get_path_objects_pack");
1140 if (stat(objects_pack_dir, &sb) == -1) {
1141 if (errno != ENOENT) {
1142 err = got_error_from_errno2("stat", objects_pack_dir);
1143 goto done;
1145 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1146 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1147 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1148 purge_packidx_paths(&repo->packidx_paths);
1149 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1150 if (err)
1151 goto done;
1153 done:
1154 free(objects_pack_dir);
1155 return err;
1158 const struct got_error *
1159 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1160 struct got_repository *repo, struct got_object_id *id)
1162 const struct got_error *err;
1163 struct got_pathlist_entry *pe;
1164 size_t i;
1166 /* Search pack index cache. */
1167 for (i = 0; i < repo->pack_cache_size; i++) {
1168 if (repo->packidx_cache[i] == NULL)
1169 break;
1170 if (!got_repo_check_packidx_bloom_filter(repo,
1171 repo->packidx_cache[i]->path_packidx, id))
1172 continue; /* object will not be found in this index */
1173 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1174 if (*idx != -1) {
1175 *packidx = repo->packidx_cache[i];
1177 * Move this cache entry to the front. Repeatedly
1178 * searching a wrong pack index can be expensive.
1180 if (i > 0) {
1181 memmove(&repo->packidx_cache[1],
1182 &repo->packidx_cache[0],
1183 i * sizeof(repo->packidx_cache[0]));
1184 repo->packidx_cache[0] = *packidx;
1185 if (repo->pinned_packidx >= 0 &&
1186 repo->pinned_packidx < i)
1187 repo->pinned_packidx++;
1188 else if (repo->pinned_packidx == i)
1189 repo->pinned_packidx = 0;
1191 return NULL;
1194 /* No luck. Search the filesystem. */
1196 err = refresh_packidx_paths(repo);
1197 if (err)
1198 return err;
1200 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1201 const char *path_packidx = pe->path;
1202 int is_cached = 0;
1204 if (!got_repo_check_packidx_bloom_filter(repo,
1205 pe->path, id))
1206 continue; /* object will not be found in this index */
1208 for (i = 0; i < repo->pack_cache_size; i++) {
1209 if (repo->packidx_cache[i] == NULL)
1210 break;
1211 if (strcmp(repo->packidx_cache[i]->path_packidx,
1212 path_packidx) == 0) {
1213 is_cached = 1;
1214 break;
1217 if (is_cached)
1218 continue; /* already searched */
1220 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1221 path_packidx, 0);
1222 if (err)
1223 goto done;
1225 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1226 if (err)
1227 goto done;
1229 err = cache_packidx(repo, *packidx, path_packidx);
1230 if (err)
1231 goto done;
1233 *idx = got_packidx_get_object_idx(*packidx, id);
1234 if (*idx != -1) {
1235 err = NULL; /* found the object */
1236 goto done;
1240 err = got_error_no_obj(id);
1241 done:
1242 return err;
1245 const struct got_error *
1246 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1247 struct got_repository *repo)
1249 const struct got_error *err = NULL;
1250 DIR *packdir = NULL;
1251 struct dirent *dent;
1252 char *path_packidx = NULL;
1253 int packdir_fd;
1254 struct stat sb;
1256 packdir_fd = openat(got_repo_get_fd(repo),
1257 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1258 if (packdir_fd == -1) {
1259 return got_error_from_errno_fmt("openat: %s/%s",
1260 got_repo_get_path_git_dir(repo),
1261 GOT_OBJECTS_PACK_DIR);
1264 packdir = fdopendir(packdir_fd);
1265 if (packdir == NULL) {
1266 err = got_error_from_errno("fdopendir");
1267 goto done;
1270 if (fstat(packdir_fd, &sb) == -1) {
1271 err = got_error_from_errno("fstat");
1272 goto done;
1274 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1275 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1277 while ((dent = readdir(packdir)) != NULL) {
1278 if (!got_repo_is_packidx_filename(dent->d_name,
1279 strlen(dent->d_name)))
1280 continue;
1282 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1283 dent->d_name) == -1) {
1284 err = got_error_from_errno("asprintf");
1285 path_packidx = NULL;
1286 break;
1289 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1290 if (err)
1291 break;
1293 done:
1294 if (err)
1295 free(path_packidx);
1296 if (packdir && closedir(packdir) != 0 && err == NULL)
1297 err = got_error_from_errno("closedir");
1298 return err;
1301 const struct got_error *
1302 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1303 struct got_repository *repo)
1305 const struct got_error *err;
1306 size_t i;
1308 *packidx = NULL;
1310 /* Search pack index cache. */
1311 for (i = 0; i < repo->pack_cache_size; i++) {
1312 if (repo->packidx_cache[i] == NULL)
1313 break;
1314 if (strcmp(repo->packidx_cache[i]->path_packidx,
1315 path_packidx) == 0) {
1316 *packidx = repo->packidx_cache[i];
1317 return NULL;
1320 /* No luck. Search the filesystem. */
1322 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1323 path_packidx, 0);
1324 if (err)
1325 return err;
1327 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1328 if (err)
1329 goto done;
1331 err = cache_packidx(repo, *packidx, path_packidx);
1332 done:
1333 if (err) {
1334 got_packidx_close(*packidx);
1335 *packidx = NULL;
1337 return err;
1340 static const struct got_error *
1341 read_packfile_hdr(int fd, struct got_packidx *packidx)
1343 const struct got_error *err = NULL;
1344 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1345 struct got_packfile_hdr hdr;
1346 ssize_t n;
1348 n = read(fd, &hdr, sizeof(hdr));
1349 if (n < 0)
1350 return got_error_from_errno("read");
1351 if (n != sizeof(hdr))
1352 return got_error(GOT_ERR_BAD_PACKFILE);
1354 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1355 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1356 be32toh(hdr.nobjects) != totobj)
1357 err = got_error(GOT_ERR_BAD_PACKFILE);
1359 return err;
1362 static const struct got_error *
1363 open_packfile(int *fd, struct got_repository *repo,
1364 const char *relpath, struct got_packidx *packidx)
1366 const struct got_error *err = NULL;
1368 *fd = openat(got_repo_get_fd(repo), relpath,
1369 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1370 if (*fd == -1)
1371 return got_error_from_errno_fmt("openat: %s/%s",
1372 got_repo_get_path_git_dir(repo), relpath);
1374 if (packidx) {
1375 err = read_packfile_hdr(*fd, packidx);
1376 if (err) {
1377 close(*fd);
1378 *fd = -1;
1382 return err;
1385 const struct got_error *
1386 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1387 const char *path_packfile, struct got_packidx *packidx)
1389 const struct got_error *err = NULL;
1390 struct got_pack *pack = NULL;
1391 struct stat sb;
1392 size_t i;
1394 if (packp)
1395 *packp = NULL;
1397 for (i = 0; i < repo->pack_cache_size; i++) {
1398 pack = &repo->packs[i];
1399 if (pack->path_packfile == NULL)
1400 break;
1401 if (strcmp(pack->path_packfile, path_packfile) == 0)
1402 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1405 if (i == repo->pack_cache_size) {
1406 struct got_pack tmp;
1407 do {
1408 i--;
1409 } while (i > 0 && repo->pinned_pack >= 0 &&
1410 i == repo->pinned_pack);
1411 err = got_pack_close(&repo->packs[i]);
1412 if (err)
1413 return err;
1414 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1415 return got_error_from_errno("ftruncate");
1416 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1417 return got_error_from_errno("ftruncate");
1418 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1419 memcpy(&repo->packs[i], &repo->packs[0],
1420 sizeof(repo->packs[i]));
1421 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1422 if (repo->pinned_pack == 0)
1423 repo->pinned_pack = i;
1424 else if (repo->pinned_pack == i)
1425 repo->pinned_pack = 0;
1426 i = 0;
1429 pack = &repo->packs[i];
1431 pack->path_packfile = strdup(path_packfile);
1432 if (pack->path_packfile == NULL) {
1433 err = got_error_from_errno("strdup");
1434 goto done;
1437 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1438 if (err)
1439 goto done;
1441 if (fstat(pack->fd, &sb) != 0) {
1442 err = got_error_from_errno("fstat");
1443 goto done;
1445 pack->filesize = sb.st_size;
1447 pack->privsep_child = NULL;
1449 err = got_delta_cache_alloc(&pack->delta_cache);
1450 if (err)
1451 goto done;
1453 #ifndef GOT_PACK_NO_MMAP
1454 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1455 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1456 pack->fd, 0);
1457 if (pack->map == MAP_FAILED) {
1458 if (errno != ENOMEM) {
1459 err = got_error_from_errno("mmap");
1460 goto done;
1462 pack->map = NULL; /* fall back to read(2) */
1465 #endif
1466 done:
1467 if (err) {
1468 if (pack)
1469 got_pack_close(pack);
1470 } else if (packp)
1471 *packp = pack;
1472 return err;
1475 struct got_pack *
1476 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1478 struct got_pack *pack = NULL;
1479 size_t i;
1481 for (i = 0; i < repo->pack_cache_size; i++) {
1482 pack = &repo->packs[i];
1483 if (pack->path_packfile == NULL)
1484 break;
1485 if (strcmp(pack->path_packfile, path_packfile) == 0)
1486 return pack;
1489 return NULL;
1492 const struct got_error *
1493 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1494 struct got_pack *pack)
1496 size_t i;
1497 int pinned_pack = -1, pinned_packidx = -1;
1499 for (i = 0; i < repo->pack_cache_size; i++) {
1500 if (repo->packidx_cache[i] &&
1501 strcmp(repo->packidx_cache[i]->path_packidx,
1502 packidx->path_packidx) == 0)
1503 pinned_packidx = i;
1504 if (repo->packs[i].path_packfile &&
1505 strcmp(repo->packs[i].path_packfile,
1506 pack->path_packfile) == 0)
1507 pinned_pack = i;
1510 if (pinned_packidx == -1 || pinned_pack == -1)
1511 return got_error(GOT_ERR_PIN_PACK);
1513 repo->pinned_pack = pinned_pack;
1514 repo->pinned_packidx = pinned_packidx;
1515 if (repo->packs[pinned_pack].privsep_child)
1516 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1517 return NULL;
1520 struct got_pack *
1521 got_repo_get_pinned_pack(struct got_repository *repo)
1523 if (repo->pinned_pack >= 0 &&
1524 repo->pinned_pack < repo->pack_cache_size)
1525 return &repo->packs[repo->pinned_pack];
1527 return NULL;
1530 void
1531 got_repo_unpin_pack(struct got_repository *repo)
1533 repo->pinned_packidx = -1;
1534 repo->pinned_pack = -1;
1535 repo->pinned_pid = 0;
1538 const struct got_error *
1539 got_repo_init(const char *repo_path, const char *head_name)
1541 const struct got_error *err = NULL;
1542 const char *dirnames[] = {
1543 GOT_OBJECTS_DIR,
1544 GOT_OBJECTS_PACK_DIR,
1545 GOT_REFS_DIR,
1547 const char *description_str = "Unnamed repository; "
1548 "edit this file 'description' to name the repository.";
1549 const char *headref = "ref: refs/heads/";
1550 const char *gitconfig_str = "[core]\n"
1551 "\trepositoryformatversion = 0\n"
1552 "\tfilemode = true\n"
1553 "\tbare = true\n";
1554 char *headref_str, *path;
1555 size_t i;
1557 if (!got_path_dir_is_empty(repo_path))
1558 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1560 for (i = 0; i < nitems(dirnames); i++) {
1561 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1562 return got_error_from_errno("asprintf");
1564 err = got_path_mkdir(path);
1565 free(path);
1566 if (err)
1567 return err;
1570 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1571 return got_error_from_errno("asprintf");
1572 err = got_path_create_file(path, description_str);
1573 free(path);
1574 if (err)
1575 return err;
1577 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1578 return got_error_from_errno("asprintf");
1579 if (asprintf(&headref_str, "%s%s", headref,
1580 head_name ? head_name : "main") == -1) {
1581 free(path);
1582 return got_error_from_errno("asprintf");
1584 err = got_path_create_file(path, headref_str);
1585 free(headref_str);
1586 free(path);
1587 if (err)
1588 return err;
1590 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1591 return got_error_from_errno("asprintf");
1592 err = got_path_create_file(path, gitconfig_str);
1593 free(path);
1594 if (err)
1595 return err;
1597 return NULL;
1600 static const struct got_error *
1601 match_packed_object(struct got_object_id **unique_id,
1602 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1604 const struct got_error *err = NULL;
1605 struct got_object_id_queue matched_ids;
1606 struct got_pathlist_entry *pe;
1608 STAILQ_INIT(&matched_ids);
1610 err = refresh_packidx_paths(repo);
1611 if (err)
1612 return err;
1614 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1615 const char *path_packidx = pe->path;
1616 struct got_packidx *packidx;
1617 struct got_object_qid *qid;
1619 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1620 path_packidx, 0);
1621 if (err)
1622 break;
1624 err = got_packidx_match_id_str_prefix(&matched_ids,
1625 packidx, id_str_prefix);
1626 if (err) {
1627 got_packidx_close(packidx);
1628 break;
1630 err = got_packidx_close(packidx);
1631 if (err)
1632 break;
1634 STAILQ_FOREACH(qid, &matched_ids, entry) {
1635 if (obj_type != GOT_OBJ_TYPE_ANY) {
1636 int matched_type;
1637 err = got_object_get_type(&matched_type, repo,
1638 &qid->id);
1639 if (err)
1640 goto done;
1641 if (matched_type != obj_type)
1642 continue;
1644 if (*unique_id == NULL) {
1645 *unique_id = got_object_id_dup(&qid->id);
1646 if (*unique_id == NULL) {
1647 err = got_error_from_errno("malloc");
1648 goto done;
1650 } else {
1651 if (got_object_id_cmp(*unique_id,
1652 &qid->id) == 0)
1653 continue; /* packed multiple times */
1654 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1655 goto done;
1659 done:
1660 got_object_id_queue_free(&matched_ids);
1661 if (err) {
1662 free(*unique_id);
1663 *unique_id = NULL;
1665 return err;
1668 static const struct got_error *
1669 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1670 const char *object_dir, const char *id_str_prefix, int obj_type,
1671 struct got_repository *repo)
1673 const struct got_error *err = NULL;
1674 char *path, *id_str = NULL;
1675 DIR *dir = NULL;
1676 struct dirent *dent;
1677 struct got_object_id id;
1679 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1680 err = got_error_from_errno("asprintf");
1681 goto done;
1684 dir = opendir(path);
1685 if (dir == NULL) {
1686 if (errno == ENOENT) {
1687 err = NULL;
1688 goto done;
1690 err = got_error_from_errno2("opendir", path);
1691 goto done;
1693 while ((dent = readdir(dir)) != NULL) {
1694 int cmp;
1696 free(id_str);
1697 id_str = NULL;
1699 if (strcmp(dent->d_name, ".") == 0 ||
1700 strcmp(dent->d_name, "..") == 0)
1701 continue;
1703 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1704 err = got_error_from_errno("asprintf");
1705 goto done;
1708 if (!got_parse_sha1_digest(id.sha1, id_str))
1709 continue;
1712 * Directory entries do not necessarily appear in
1713 * sorted order, so we must iterate over all of them.
1715 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1716 if (cmp != 0)
1717 continue;
1719 if (*unique_id == NULL) {
1720 if (obj_type != GOT_OBJ_TYPE_ANY) {
1721 int matched_type;
1722 err = got_object_get_type(&matched_type, repo,
1723 &id);
1724 if (err)
1725 goto done;
1726 if (matched_type != obj_type)
1727 continue;
1729 *unique_id = got_object_id_dup(&id);
1730 if (*unique_id == NULL) {
1731 err = got_error_from_errno("got_object_id_dup");
1732 goto done;
1734 } else {
1735 if (got_object_id_cmp(*unique_id, &id) == 0)
1736 continue; /* both packed and loose */
1737 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1738 goto done;
1741 done:
1742 if (dir && closedir(dir) != 0 && err == NULL)
1743 err = got_error_from_errno("closedir");
1744 if (err) {
1745 free(*unique_id);
1746 *unique_id = NULL;
1748 free(id_str);
1749 free(path);
1750 return err;
1753 const struct got_error *
1754 got_repo_match_object_id_prefix(struct got_object_id **id,
1755 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1757 const struct got_error *err = NULL;
1758 char *path_objects = NULL, *object_dir = NULL;
1759 size_t len;
1760 int i;
1762 *id = NULL;
1764 path_objects = got_repo_get_path_objects(repo);
1766 len = strlen(id_str_prefix);
1767 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1768 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1769 goto done;
1772 for (i = 0; i < len; i++) {
1773 if (isxdigit((unsigned char)id_str_prefix[i]))
1774 continue;
1775 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1776 goto done;
1779 if (len >= 2) {
1780 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1781 if (err)
1782 goto done;
1783 object_dir = strndup(id_str_prefix, 2);
1784 if (object_dir == NULL) {
1785 err = got_error_from_errno("strdup");
1786 goto done;
1788 err = match_loose_object(id, path_objects, object_dir,
1789 id_str_prefix, obj_type, repo);
1790 } else if (len == 1) {
1791 int i;
1792 for (i = 0; i < 0xf; i++) {
1793 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1794 == -1) {
1795 err = got_error_from_errno("asprintf");
1796 goto done;
1798 err = match_packed_object(id, repo, object_dir,
1799 obj_type);
1800 if (err)
1801 goto done;
1802 err = match_loose_object(id, path_objects, object_dir,
1803 id_str_prefix, obj_type, repo);
1804 if (err)
1805 goto done;
1807 } else {
1808 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1809 goto done;
1811 done:
1812 free(path_objects);
1813 free(object_dir);
1814 if (err) {
1815 free(*id);
1816 *id = NULL;
1817 } else if (*id == NULL) {
1818 switch (obj_type) {
1819 case GOT_OBJ_TYPE_BLOB:
1820 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1821 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1822 break;
1823 case GOT_OBJ_TYPE_TREE:
1824 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1825 GOT_OBJ_LABEL_TREE, id_str_prefix);
1826 break;
1827 case GOT_OBJ_TYPE_COMMIT:
1828 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1829 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1830 break;
1831 case GOT_OBJ_TYPE_TAG:
1832 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1833 GOT_OBJ_LABEL_TAG, id_str_prefix);
1834 break;
1835 default:
1836 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1837 break;
1841 return err;
1844 const struct got_error *
1845 got_repo_match_object_id(struct got_object_id **id, char **label,
1846 const char *id_str, int obj_type, struct got_reflist_head *refs,
1847 struct got_repository *repo)
1849 const struct got_error *err;
1850 struct got_tag_object *tag;
1851 struct got_reference *ref = NULL;
1853 *id = NULL;
1854 if (label)
1855 *label = NULL;
1857 if (refs) {
1858 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1859 refs, repo);
1860 if (err == NULL) {
1861 *id = got_object_id_dup(
1862 got_object_tag_get_object_id(tag));
1863 if (*id == NULL)
1864 err = got_error_from_errno("got_object_id_dup");
1865 else if (label && asprintf(label, "refs/tags/%s",
1866 got_object_tag_get_name(tag)) == -1) {
1867 err = got_error_from_errno("asprintf");
1868 free(*id);
1869 *id = NULL;
1871 got_object_tag_close(tag);
1872 return err;
1873 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1874 err->code != GOT_ERR_NO_OBJ)
1875 return err;
1878 err = got_ref_open(&ref, repo, id_str, 0);
1879 if (err == NULL) {
1880 err = got_ref_resolve(id, repo, ref);
1881 if (err)
1882 goto done;
1883 if (label) {
1884 *label = strdup(got_ref_get_name(ref));
1885 if (*label == NULL) {
1886 err = got_error_from_errno("strdup");
1887 goto done;
1890 } else {
1891 if (err->code != GOT_ERR_NOT_REF &&
1892 err->code != GOT_ERR_BAD_REF_NAME)
1893 goto done;
1894 err = got_repo_match_object_id_prefix(id, id_str,
1895 obj_type, repo);
1896 if (err) {
1897 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1898 err = got_error_not_ref(id_str);
1899 goto done;
1901 if (label) {
1902 err = got_object_id_str(label, *id);
1903 if (*label == NULL) {
1904 err = got_error_from_errno("strdup");
1905 goto done;
1909 done:
1910 if (ref)
1911 got_ref_close(ref);
1912 return err;
1915 const struct got_error *
1916 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1917 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1919 const struct got_error *err = NULL;
1920 struct got_reflist_entry *re;
1921 struct got_object_id *tag_id;
1922 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1924 *tag = NULL;
1926 TAILQ_FOREACH(re, refs, entry) {
1927 const char *refname;
1928 refname = got_ref_get_name(re->ref);
1929 if (got_ref_is_symbolic(re->ref))
1930 continue;
1931 if (strncmp(refname, "refs/tags/", 10) != 0)
1932 continue;
1933 if (!name_is_absolute)
1934 refname += strlen("refs/tags/");
1935 if (strcmp(refname, name) != 0)
1936 continue;
1937 err = got_ref_resolve(&tag_id, repo, re->ref);
1938 if (err)
1939 break;
1940 err = got_object_open_as_tag(tag, repo, tag_id);
1941 free(tag_id);
1942 if (err)
1943 break;
1944 if (obj_type == GOT_OBJ_TYPE_ANY ||
1945 got_object_tag_get_object_type(*tag) == obj_type)
1946 break;
1947 got_object_tag_close(*tag);
1948 *tag = NULL;
1951 if (err == NULL && *tag == NULL)
1952 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1953 GOT_OBJ_LABEL_TAG, name);
1954 return err;
1957 static const struct got_error *
1958 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1959 const char *name, mode_t mode, struct got_object_id *blob_id)
1961 const struct got_error *err = NULL;
1963 *new_te = NULL;
1965 *new_te = calloc(1, sizeof(**new_te));
1966 if (*new_te == NULL)
1967 return got_error_from_errno("calloc");
1969 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1970 sizeof((*new_te)->name)) {
1971 err = got_error(GOT_ERR_NO_SPACE);
1972 goto done;
1975 if (S_ISLNK(mode)) {
1976 (*new_te)->mode = S_IFLNK;
1977 } else {
1978 (*new_te)->mode = S_IFREG;
1979 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1981 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1982 done:
1983 if (err && *new_te) {
1984 free(*new_te);
1985 *new_te = NULL;
1987 return err;
1990 static const struct got_error *
1991 import_file(struct got_tree_entry **new_te, struct dirent *de,
1992 const char *path, struct got_repository *repo)
1994 const struct got_error *err;
1995 struct got_object_id *blob_id = NULL;
1996 char *filepath;
1997 struct stat sb;
1999 if (asprintf(&filepath, "%s%s%s", path,
2000 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2001 return got_error_from_errno("asprintf");
2003 if (lstat(filepath, &sb) != 0) {
2004 err = got_error_from_errno2("lstat", path);
2005 goto done;
2008 err = got_object_blob_create(&blob_id, filepath, repo);
2009 if (err)
2010 goto done;
2012 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2013 blob_id);
2014 done:
2015 free(filepath);
2016 if (err)
2017 free(blob_id);
2018 return err;
2021 static const struct got_error *
2022 insert_tree_entry(struct got_tree_entry *new_te,
2023 struct got_pathlist_head *paths)
2025 const struct got_error *err = NULL;
2026 struct got_pathlist_entry *new_pe;
2028 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2029 if (err)
2030 return err;
2031 if (new_pe == NULL)
2032 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2033 return NULL;
2036 static const struct got_error *write_tree(struct got_object_id **,
2037 const char *, struct got_pathlist_head *, struct got_repository *,
2038 got_repo_import_cb progress_cb, void *progress_arg);
2040 static const struct got_error *
2041 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2042 const char *path, struct got_pathlist_head *ignores,
2043 struct got_repository *repo,
2044 got_repo_import_cb progress_cb, void *progress_arg)
2046 const struct got_error *err;
2047 struct got_object_id *id = NULL;
2048 char *subdirpath;
2050 if (asprintf(&subdirpath, "%s%s%s", path,
2051 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2052 return got_error_from_errno("asprintf");
2054 (*new_te) = calloc(1, sizeof(**new_te));
2055 if (*new_te == NULL)
2056 return got_error_from_errno("calloc");
2057 (*new_te)->mode = S_IFDIR;
2058 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2059 sizeof((*new_te)->name)) {
2060 err = got_error(GOT_ERR_NO_SPACE);
2061 goto done;
2063 err = write_tree(&id, subdirpath, ignores, repo,
2064 progress_cb, progress_arg);
2065 if (err)
2066 goto done;
2067 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2069 done:
2070 free(id);
2071 free(subdirpath);
2072 if (err) {
2073 free(*new_te);
2074 *new_te = NULL;
2076 return err;
2079 static const struct got_error *
2080 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2081 struct got_pathlist_head *ignores, struct got_repository *repo,
2082 got_repo_import_cb progress_cb, void *progress_arg)
2084 const struct got_error *err = NULL;
2085 DIR *dir;
2086 struct dirent *de;
2087 int nentries;
2088 struct got_tree_entry *new_te = NULL;
2089 struct got_pathlist_head paths;
2090 struct got_pathlist_entry *pe;
2092 *new_tree_id = NULL;
2094 TAILQ_INIT(&paths);
2096 dir = opendir(path_dir);
2097 if (dir == NULL) {
2098 err = got_error_from_errno2("opendir", path_dir);
2099 goto done;
2102 nentries = 0;
2103 while ((de = readdir(dir)) != NULL) {
2104 int ignore = 0;
2105 int type;
2107 if (strcmp(de->d_name, ".") == 0 ||
2108 strcmp(de->d_name, "..") == 0)
2109 continue;
2111 TAILQ_FOREACH(pe, ignores, entry) {
2112 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2113 ignore = 1;
2114 break;
2117 if (ignore)
2118 continue;
2120 err = got_path_dirent_type(&type, path_dir, de);
2121 if (err)
2122 goto done;
2124 if (type == DT_DIR) {
2125 err = import_subdir(&new_te, de, path_dir,
2126 ignores, repo, progress_cb, progress_arg);
2127 if (err) {
2128 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2129 goto done;
2130 err = NULL;
2131 continue;
2133 } else if (type == DT_REG || type == DT_LNK) {
2134 err = import_file(&new_te, de, path_dir, repo);
2135 if (err)
2136 goto done;
2137 } else
2138 continue;
2140 err = insert_tree_entry(new_te, &paths);
2141 if (err)
2142 goto done;
2143 nentries++;
2146 if (TAILQ_EMPTY(&paths)) {
2147 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2148 "cannot create tree without any entries");
2149 goto done;
2152 TAILQ_FOREACH(pe, &paths, entry) {
2153 struct got_tree_entry *te = pe->data;
2154 char *path;
2155 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2156 continue;
2157 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2158 err = got_error_from_errno("asprintf");
2159 goto done;
2161 err = (*progress_cb)(progress_arg, path);
2162 free(path);
2163 if (err)
2164 goto done;
2167 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2168 done:
2169 if (dir)
2170 closedir(dir);
2171 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2172 return err;
2175 const struct got_error *
2176 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2177 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2178 struct got_repository *repo, got_repo_import_cb progress_cb,
2179 void *progress_arg)
2181 const struct got_error *err;
2182 struct got_object_id *new_tree_id;
2184 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2185 progress_cb, progress_arg);
2186 if (err)
2187 return err;
2189 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2190 author, time(NULL), author, time(NULL), logmsg, repo);
2191 free(new_tree_id);
2192 return err;
2195 const struct got_error *
2196 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2197 struct got_repository *repo)
2199 const struct got_error *err = NULL;
2200 char *path_objects = NULL, *path = NULL;
2201 DIR *dir = NULL;
2202 struct got_object_id id;
2203 int i;
2205 *nobjects = 0;
2206 *ondisk_size = 0;
2208 path_objects = got_repo_get_path_objects(repo);
2209 if (path_objects == NULL)
2210 return got_error_from_errno("got_repo_get_path_objects");
2212 for (i = 0; i <= 0xff; i++) {
2213 struct dirent *dent;
2215 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2216 err = got_error_from_errno("asprintf");
2217 break;
2220 dir = opendir(path);
2221 if (dir == NULL) {
2222 if (errno == ENOENT) {
2223 err = NULL;
2224 continue;
2226 err = got_error_from_errno2("opendir", path);
2227 break;
2230 while ((dent = readdir(dir)) != NULL) {
2231 char *id_str;
2232 int fd;
2233 struct stat sb;
2235 if (strcmp(dent->d_name, ".") == 0 ||
2236 strcmp(dent->d_name, "..") == 0)
2237 continue;
2239 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2240 err = got_error_from_errno("asprintf");
2241 goto done;
2244 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2245 free(id_str);
2246 continue;
2248 free(id_str);
2250 err = got_object_open_loose_fd(&fd, &id, repo);
2251 if (err)
2252 goto done;
2254 if (fstat(fd, &sb) == -1) {
2255 err = got_error_from_errno("fstat");
2256 close(fd);
2257 goto done;
2259 (*nobjects)++;
2260 (*ondisk_size) += sb.st_size;
2262 if (close(fd) == -1) {
2263 err = got_error_from_errno("close");
2264 goto done;
2268 if (closedir(dir) != 0) {
2269 err = got_error_from_errno("closedir");
2270 goto done;
2272 dir = NULL;
2274 free(path);
2275 path = NULL;
2277 done:
2278 if (dir && closedir(dir) != 0 && err == NULL)
2279 err = got_error_from_errno("closedir");
2281 if (err) {
2282 *nobjects = 0;
2283 *ondisk_size = 0;
2285 free(path_objects);
2286 free(path);
2287 return err;
2290 const struct got_error *
2291 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2292 off_t *total_packsize, struct got_repository *repo)
2294 const struct got_error *err = NULL;
2295 DIR *packdir = NULL;
2296 struct dirent *dent;
2297 struct got_packidx *packidx = NULL;
2298 char *path_packidx;
2299 char *path_packfile;
2300 int packdir_fd;
2301 struct stat sb;
2303 *npackfiles = 0;
2304 *nobjects = 0;
2305 *total_packsize = 0;
2307 packdir_fd = openat(got_repo_get_fd(repo),
2308 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2309 if (packdir_fd == -1) {
2310 return got_error_from_errno_fmt("openat: %s/%s",
2311 got_repo_get_path_git_dir(repo),
2312 GOT_OBJECTS_PACK_DIR);
2315 packdir = fdopendir(packdir_fd);
2316 if (packdir == NULL) {
2317 err = got_error_from_errno("fdopendir");
2318 goto done;
2321 while ((dent = readdir(packdir)) != NULL) {
2322 if (!got_repo_is_packidx_filename(dent->d_name,
2323 strlen(dent->d_name)))
2324 continue;
2326 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2327 dent->d_name) == -1) {
2328 err = got_error_from_errno("asprintf");
2329 goto done;
2332 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2333 path_packidx, 0);
2334 free(path_packidx);
2335 if (err)
2336 goto done;
2338 if (fstat(packidx->fd, &sb) == -1)
2339 goto done;
2340 *total_packsize += sb.st_size;
2342 err = got_packidx_get_packfile_path(&path_packfile,
2343 packidx->path_packidx);
2344 if (err)
2345 goto done;
2347 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2348 0) == -1) {
2349 free(path_packfile);
2350 goto done;
2352 free(path_packfile);
2353 *total_packsize += sb.st_size;
2355 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2357 (*npackfiles)++;
2359 got_packidx_close(packidx);
2360 packidx = NULL;
2362 done:
2363 if (packidx)
2364 got_packidx_close(packidx);
2365 if (packdir && closedir(packdir) != 0 && err == NULL)
2366 err = got_error_from_errno("closedir");
2367 if (err) {
2368 *npackfiles = 0;
2369 *nobjects = 0;
2370 *total_packsize = 0;
2372 return err;
2375 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2376 got_packidx_bloom_filter_cmp);