Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_delta_cache.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_sha1.h"
64 #include "got_lib_object_cache.h"
65 #include "got_lib_repository.h"
66 #include "got_lib_gotconfig.h"
68 #ifndef nitems
69 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
70 #endif
72 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
74 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
75 got_packidx_bloom_filter_cmp);
77 const char *
78 got_repo_get_path(struct got_repository *repo)
79 {
80 return repo->path;
81 }
83 const char *
84 got_repo_get_path_git_dir(struct got_repository *repo)
85 {
86 return repo->path_git_dir;
87 }
89 int
90 got_repo_get_fd(struct got_repository *repo)
91 {
92 return repo->gitdir_fd;
93 }
95 const char *
96 got_repo_get_gitconfig_author_name(struct got_repository *repo)
97 {
98 return repo->gitconfig_author_name;
99 }
101 const char *
102 got_repo_get_gitconfig_author_email(struct got_repository *repo)
104 return repo->gitconfig_author_email;
107 const char *
108 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
110 return repo->global_gitconfig_author_name;
113 const char *
114 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
116 return repo->global_gitconfig_author_email;
119 const char *
120 got_repo_get_gitconfig_owner(struct got_repository *repo)
122 return repo->gitconfig_owner;
125 void
126 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
127 struct got_repository *repo)
129 *extensions = repo->extensions;
130 *nextensions = repo->nextensions;
133 int
134 got_repo_is_bare(struct got_repository *repo)
136 return (strcmp(repo->path, repo->path_git_dir) == 0);
139 static char *
140 get_path_git_child(struct got_repository *repo, const char *basename)
142 char *path_child;
144 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
145 basename) == -1)
146 return NULL;
148 return path_child;
151 char *
152 got_repo_get_path_objects(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_DIR);
157 char *
158 got_repo_get_path_objects_pack(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
163 char *
164 got_repo_get_path_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_REFS_DIR);
169 char *
170 got_repo_get_path_packed_refs(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
175 static char *
176 get_path_head(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_HEAD_FILE);
181 char *
182 got_repo_get_path_gitconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GITCONFIG);
187 char *
188 got_repo_get_path_gotconfig(struct got_repository *repo)
190 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
193 const struct got_gotconfig *
194 got_repo_get_gotconfig(struct got_repository *repo)
196 return repo->gotconfig;
199 void
200 got_repo_get_gitconfig_remotes(int *nremotes,
201 const struct got_remote_repo **remotes, struct got_repository *repo)
203 *nremotes = repo->ngitconfig_remotes;
204 *remotes = repo->gitconfig_remotes;
207 static int
208 is_git_repo(struct got_repository *repo)
210 const char *path_git = got_repo_get_path_git_dir(repo);
211 char *path_objects = got_repo_get_path_objects(repo);
212 char *path_refs = got_repo_get_path_refs(repo);
213 char *path_head = get_path_head(repo);
214 int ret = 0;
215 struct stat sb;
216 struct got_reference *head_ref;
218 if (lstat(path_git, &sb) == -1)
219 goto done;
220 if (!S_ISDIR(sb.st_mode))
221 goto done;
223 if (lstat(path_objects, &sb) == -1)
224 goto done;
225 if (!S_ISDIR(sb.st_mode))
226 goto done;
228 if (lstat(path_refs, &sb) == -1)
229 goto done;
230 if (!S_ISDIR(sb.st_mode))
231 goto done;
233 if (lstat(path_head, &sb) == -1)
234 goto done;
235 if (!S_ISREG(sb.st_mode))
236 goto done;
238 /* Check if the HEAD reference can be opened. */
239 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
240 goto done;
241 got_ref_close(head_ref);
243 ret = 1;
244 done:
245 free(path_objects);
246 free(path_refs);
247 free(path_head);
248 return ret;
252 static const struct got_error *
253 close_tempfiles(int *fds, size_t nfds)
255 const struct got_error *err = NULL;
256 int i;
258 for (i = 0; i < nfds; i++) {
259 if (fds[i] == -1)
260 continue;
261 if (close(fds[i]) == -1) {
262 err = got_error_from_errno("close");
263 break;
266 free(fds);
267 return err;
270 static const struct got_error *
271 open_tempfiles(int **fds, size_t nfds)
273 const struct got_error *err = NULL;
274 int i;
276 *fds = calloc(nfds, sizeof(**fds));
277 if (*fds == NULL)
278 return got_error_from_errno("calloc");
280 for (i = 0; i < nfds; i++)
281 (*fds)[i] = -1;
283 for (i = 0; i < nfds; i++) {
284 (*fds)[i] = got_opentempfd();
285 if ((*fds)[i] == -1) {
286 err = got_error_from_errno("got_opentempfd");
287 close_tempfiles(*fds, nfds);
288 *fds = NULL;
289 return err;
293 return NULL;
296 const struct got_error *
297 got_repo_pack_fds_open(int **pack_fds)
299 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
302 const struct got_error *
303 got_repo_pack_fds_close(int *pack_fds)
305 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
308 const struct got_error *
309 got_repo_temp_fds_open(int **temp_fds)
311 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
314 void
315 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
317 int i;
319 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
320 repo->tempfiles[i] = temp_fds[i];
323 const struct got_error *
324 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
326 int i;
328 *fd = -1;
329 *idx = -1;
331 for (i = 0; i < nitems(repo->tempfiles); i++) {
332 if (repo->tempfile_use_mask & (1 << i))
333 continue;
334 if (repo->tempfiles[i] != -1) {
335 if (ftruncate(repo->tempfiles[i], 0L) == -1)
336 return got_error_from_errno("ftruncate");
337 *fd = repo->tempfiles[i];
338 *idx = i;
339 repo->tempfile_use_mask |= (1 << i);
340 return NULL;
344 return got_error(GOT_ERR_REPO_TEMPFILE);
347 void
348 got_repo_temp_fds_put(int idx, struct got_repository *repo)
350 repo->tempfile_use_mask &= ~(1 << idx);
353 const struct got_error *
354 got_repo_temp_fds_close(int *temp_fds)
356 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
359 const struct got_error *
360 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
361 struct got_object *obj)
363 #ifndef GOT_NO_OBJ_CACHE
364 const struct got_error *err = NULL;
365 err = got_object_cache_add(&repo->objcache, id, obj);
366 if (err) {
367 if (err->code == GOT_ERR_OBJ_EXISTS ||
368 err->code == GOT_ERR_OBJ_TOO_LARGE)
369 err = NULL;
370 return err;
372 obj->refcnt++;
373 #endif
374 return NULL;
377 struct got_object *
378 got_repo_get_cached_object(struct got_repository *repo,
379 struct got_object_id *id)
381 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
384 const struct got_error *
385 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
386 struct got_tree_object *tree)
388 #ifndef GOT_NO_OBJ_CACHE
389 const struct got_error *err = NULL;
390 err = got_object_cache_add(&repo->treecache, id, tree);
391 if (err) {
392 if (err->code == GOT_ERR_OBJ_EXISTS ||
393 err->code == GOT_ERR_OBJ_TOO_LARGE)
394 err = NULL;
395 return err;
397 tree->refcnt++;
398 #endif
399 return NULL;
402 struct got_tree_object *
403 got_repo_get_cached_tree(struct got_repository *repo,
404 struct got_object_id *id)
406 return (struct got_tree_object *)got_object_cache_get(
407 &repo->treecache, id);
410 const struct got_error *
411 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
412 struct got_commit_object *commit)
414 #ifndef GOT_NO_OBJ_CACHE
415 const struct got_error *err = NULL;
416 err = got_object_cache_add(&repo->commitcache, id, commit);
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 commit->refcnt++;
424 #endif
425 return NULL;
428 struct got_commit_object *
429 got_repo_get_cached_commit(struct got_repository *repo,
430 struct got_object_id *id)
432 return (struct got_commit_object *)got_object_cache_get(
433 &repo->commitcache, id);
436 const struct got_error *
437 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
438 struct got_tag_object *tag)
440 #ifndef GOT_NO_OBJ_CACHE
441 const struct got_error *err = NULL;
442 err = got_object_cache_add(&repo->tagcache, id, tag);
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 tag->refcnt++;
450 #endif
451 return NULL;
454 struct got_tag_object *
455 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
457 return (struct got_tag_object *)got_object_cache_get(
458 &repo->tagcache, id);
461 const struct got_error *
462 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
463 struct got_raw_object *raw)
465 #ifndef GOT_NO_OBJ_CACHE
466 const struct got_error *err = NULL;
467 err = got_object_cache_add(&repo->rawcache, id, raw);
468 if (err) {
469 if (err->code == GOT_ERR_OBJ_EXISTS ||
470 err->code == GOT_ERR_OBJ_TOO_LARGE)
471 err = NULL;
472 return err;
474 raw->refcnt++;
475 #endif
476 return NULL;
480 struct got_raw_object *
481 got_repo_get_cached_raw_object(struct got_repository *repo,
482 struct got_object_id *id)
484 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
488 static const struct got_error *
489 open_repo(struct got_repository *repo, const char *path)
491 const struct got_error *err = NULL;
493 repo->gitdir_fd = -1;
495 /* bare git repository? */
496 repo->path_git_dir = strdup(path);
497 if (repo->path_git_dir == NULL)
498 return got_error_from_errno("strdup");
499 if (is_git_repo(repo)) {
500 repo->path = strdup(repo->path_git_dir);
501 if (repo->path == NULL) {
502 err = got_error_from_errno("strdup");
503 goto done;
505 repo->gitdir_fd = open(repo->path_git_dir,
506 O_DIRECTORY | O_CLOEXEC);
507 if (repo->gitdir_fd == -1) {
508 err = got_error_from_errno2("open",
509 repo->path_git_dir);
510 goto done;
512 return NULL;
515 /* git repository with working tree? */
516 free(repo->path_git_dir);
517 repo->path_git_dir = NULL;
518 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
519 err = got_error_from_errno("asprintf");
520 goto done;
522 if (is_git_repo(repo)) {
523 repo->path = strdup(path);
524 if (repo->path == NULL) {
525 err = got_error_from_errno("strdup");
526 goto done;
528 repo->gitdir_fd = open(repo->path_git_dir,
529 O_DIRECTORY | O_CLOEXEC);
530 if (repo->gitdir_fd == -1) {
531 err = got_error_from_errno2("open",
532 repo->path_git_dir);
533 goto done;
535 return NULL;
538 err = got_error(GOT_ERR_NOT_GIT_REPO);
539 done:
540 if (err) {
541 free(repo->path);
542 repo->path = NULL;
543 free(repo->path_git_dir);
544 repo->path_git_dir = NULL;
545 if (repo->gitdir_fd != -1)
546 close(repo->gitdir_fd);
547 repo->gitdir_fd = -1;
550 return err;
553 static const struct got_error *
554 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
556 const struct got_error *err = NULL;
557 char *repo_gitconfig_path = NULL;
559 if (global_gitconfig_path) {
560 /* Read settings from ~/.gitconfig. */
561 int dummy_repo_version;
562 err = got_repo_read_gitconfig(&dummy_repo_version,
563 &repo->global_gitconfig_author_name,
564 &repo->global_gitconfig_author_email,
565 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
566 if (err)
567 return err;
570 /* Read repository's .git/config file. */
571 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
572 if (repo_gitconfig_path == NULL)
573 return got_error_from_errno("got_repo_get_path_gitconfig");
575 err = got_repo_read_gitconfig(
576 &repo->gitconfig_repository_format_version,
577 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
578 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
579 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
580 repo_gitconfig_path);
581 if (err)
582 goto done;
584 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
585 int i;
587 for (i = 0; i < repo->ngitconfig_remotes; i++) {
588 got_repo_free_remote_repo_data(
589 &repo->gitconfig_remotes[i]);
591 free(repo->gitconfig_remotes);
592 repo->gitconfig_remotes = NULL;
593 repo->ngitconfig_remotes = 0;
595 free(repo->gitconfig_author_name);
596 repo->gitconfig_author_name = NULL;
597 free(repo->gitconfig_author_email);
598 repo->gitconfig_author_email = NULL;
600 free(repo->global_gitconfig_author_name);
601 repo->global_gitconfig_author_name = NULL;
602 free(repo->global_gitconfig_author_email);
603 repo->global_gitconfig_author_email = NULL;
606 done:
607 free(repo_gitconfig_path);
608 return err;
611 static const struct got_error *
612 read_gotconfig(struct got_repository *repo)
614 const struct got_error *err = NULL;
615 char *gotconfig_path;
617 gotconfig_path = got_repo_get_path_gotconfig(repo);
618 if (gotconfig_path == NULL)
619 return got_error_from_errno("got_repo_get_path_gotconfig");
621 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
622 free(gotconfig_path);
623 return err;
626 /* Supported repository format extensions. */
627 static const char *const repo_extensions[] = {
628 "noop", /* Got supports repository format version 1. */
629 "preciousObjects", /* Supported by gotadmin cleanup. */
630 "worktreeConfig", /* Got does not care about Git work trees. */
631 };
633 const struct got_error *
634 got_repo_open(struct got_repository **repop, const char *path,
635 const char *global_gitconfig_path, int *pack_fds)
637 struct got_repository *repo = NULL;
638 const struct got_error *err = NULL;
639 char *repo_path = NULL;
640 size_t i, j = 0;
641 struct rlimit rl;
643 *repop = NULL;
645 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
646 return got_error_from_errno("getrlimit");
648 repo = calloc(1, sizeof(*repo));
649 if (repo == NULL)
650 return got_error_from_errno("calloc");
652 RB_INIT(&repo->packidx_bloom_filters);
653 TAILQ_INIT(&repo->packidx_paths);
655 for (i = 0; i < nitems(repo->privsep_children); i++) {
656 memset(&repo->privsep_children[i], 0,
657 sizeof(repo->privsep_children[0]));
658 repo->privsep_children[i].imsg_fd = -1;
661 err = got_object_cache_init(&repo->objcache,
662 GOT_OBJECT_CACHE_TYPE_OBJ);
663 if (err)
664 goto done;
665 err = got_object_cache_init(&repo->treecache,
666 GOT_OBJECT_CACHE_TYPE_TREE);
667 if (err)
668 goto done;
669 err = got_object_cache_init(&repo->commitcache,
670 GOT_OBJECT_CACHE_TYPE_COMMIT);
671 if (err)
672 goto done;
673 err = got_object_cache_init(&repo->tagcache,
674 GOT_OBJECT_CACHE_TYPE_TAG);
675 if (err)
676 goto done;
677 err = got_object_cache_init(&repo->rawcache,
678 GOT_OBJECT_CACHE_TYPE_RAW);
679 if (err)
680 goto done;
682 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
683 if (repo->pack_cache_size > rl.rlim_cur / 8)
684 repo->pack_cache_size = rl.rlim_cur / 8;
685 for (i = 0; i < nitems(repo->packs); i++) {
686 if (pack_fds != NULL && i < repo->pack_cache_size) {
687 repo->packs[i].basefd = pack_fds[j++];
688 repo->packs[i].accumfd = pack_fds[j++];
689 } else {
690 repo->packs[i].basefd = -1;
691 repo->packs[i].accumfd = -1;
694 for (i = 0; i < nitems(repo->tempfiles); i++)
695 repo->tempfiles[i] = -1;
696 repo->pinned_pack = -1;
697 repo->pinned_packidx = -1;
698 repo->pinned_pid = 0;
700 repo_path = realpath(path, NULL);
701 if (repo_path == NULL) {
702 err = got_error_from_errno2("realpath", path);
703 goto done;
706 for (;;) {
707 char *parent_path;
709 err = open_repo(repo, repo_path);
710 if (err == NULL)
711 break;
712 if (err->code != GOT_ERR_NOT_GIT_REPO)
713 goto done;
714 if (repo_path[0] == '/' && repo_path[1] == '\0') {
715 err = got_error(GOT_ERR_NOT_GIT_REPO);
716 goto done;
718 err = got_path_dirname(&parent_path, repo_path);
719 if (err)
720 goto done;
721 free(repo_path);
722 repo_path = parent_path;
725 err = read_gotconfig(repo);
726 if (err)
727 goto done;
729 err = read_gitconfig(repo, global_gitconfig_path);
730 if (err)
731 goto done;
732 if (repo->gitconfig_repository_format_version != 0) {
733 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
734 goto done;
736 for (i = 0; i < repo->nextensions; i++) {
737 char *ext = repo->extensions[i];
738 int j, supported = 0;
739 for (j = 0; j < nitems(repo_extensions); j++) {
740 if (strcmp(ext, repo_extensions[j]) == 0) {
741 supported = 1;
742 break;
745 if (!supported) {
746 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
747 goto done;
751 err = got_repo_list_packidx(&repo->packidx_paths, repo);
752 done:
753 if (err)
754 got_repo_close(repo);
755 else
756 *repop = repo;
757 free(repo_path);
758 return err;
761 const struct got_error *
762 got_repo_close(struct got_repository *repo)
764 const struct got_error *err = NULL, *child_err;
765 struct got_packidx_bloom_filter *bf;
766 struct got_pathlist_entry *pe;
767 size_t i;
769 for (i = 0; i < repo->pack_cache_size; i++) {
770 if (repo->packidx_cache[i] == NULL)
771 break;
772 got_packidx_close(repo->packidx_cache[i]);
775 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
776 &repo->packidx_bloom_filters))) {
777 RB_REMOVE(got_packidx_bloom_filter_tree,
778 &repo->packidx_bloom_filters, bf);
779 bloom_free(bf->bloom);
780 free(bf->bloom);
781 free(bf);
784 for (i = 0; i < repo->pack_cache_size; i++)
785 if (repo->packs[i].path_packfile)
786 if (repo->packs[i].path_packfile)
787 got_pack_close(&repo->packs[i]);
789 free(repo->path);
790 free(repo->path_git_dir);
792 got_object_cache_close(&repo->objcache);
793 got_object_cache_close(&repo->treecache);
794 got_object_cache_close(&repo->commitcache);
795 got_object_cache_close(&repo->tagcache);
796 got_object_cache_close(&repo->rawcache);
798 for (i = 0; i < nitems(repo->privsep_children); i++) {
799 if (repo->privsep_children[i].imsg_fd == -1)
800 continue;
801 imsg_clear(repo->privsep_children[i].ibuf);
802 free(repo->privsep_children[i].ibuf);
803 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
804 child_err = got_privsep_wait_for_child(
805 repo->privsep_children[i].pid);
806 if (child_err && err == NULL)
807 err = child_err;
808 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
809 err == NULL)
810 err = got_error_from_errno("close");
813 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
814 err == NULL)
815 err = got_error_from_errno("close");
817 if (repo->gotconfig)
818 got_gotconfig_free(repo->gotconfig);
819 free(repo->gitconfig_author_name);
820 free(repo->gitconfig_author_email);
821 for (i = 0; i < repo->ngitconfig_remotes; i++)
822 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
823 free(repo->gitconfig_remotes);
824 for (i = 0; i < repo->nextensions; i++)
825 free(repo->extensions[i]);
826 free(repo->extensions);
828 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
829 free((void *)pe->path);
830 got_pathlist_free(&repo->packidx_paths);
831 free(repo);
833 return err;
836 void
837 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
839 int i;
841 free(repo->name);
842 repo->name = NULL;
843 free(repo->fetch_url);
844 repo->fetch_url = NULL;
845 free(repo->send_url);
846 repo->send_url = NULL;
847 for (i = 0; i < repo->nfetch_branches; i++)
848 free(repo->fetch_branches[i]);
849 free(repo->fetch_branches);
850 repo->fetch_branches = NULL;
851 repo->nfetch_branches = 0;
852 for (i = 0; i < repo->nsend_branches; i++)
853 free(repo->send_branches[i]);
854 free(repo->send_branches);
855 repo->send_branches = NULL;
856 repo->nsend_branches = 0;
859 const struct got_error *
860 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
861 const char *input_path)
863 const struct got_error *err = NULL;
864 const char *repo_abspath = NULL;
865 size_t repolen, len;
866 char *canonpath, *path = NULL;
868 *in_repo_path = NULL;
870 canonpath = strdup(input_path);
871 if (canonpath == NULL) {
872 err = got_error_from_errno("strdup");
873 goto done;
875 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
876 if (err)
877 goto done;
879 repo_abspath = got_repo_get_path(repo);
881 if (canonpath[0] == '\0') {
882 path = strdup(canonpath);
883 if (path == NULL) {
884 err = got_error_from_errno("strdup");
885 goto done;
887 } else {
888 path = realpath(canonpath, NULL);
889 if (path == NULL) {
890 if (errno != ENOENT) {
891 err = got_error_from_errno2("realpath",
892 canonpath);
893 goto done;
895 /*
896 * Path is not on disk.
897 * Assume it is already relative to repository root.
898 */
899 path = strdup(canonpath);
900 if (path == NULL) {
901 err = got_error_from_errno("strdup");
902 goto done;
906 repolen = strlen(repo_abspath);
907 len = strlen(path);
910 if (strcmp(path, repo_abspath) == 0) {
911 free(path);
912 path = strdup("");
913 if (path == NULL) {
914 err = got_error_from_errno("strdup");
915 goto done;
917 } else if (len > repolen &&
918 got_path_is_child(path, repo_abspath, repolen)) {
919 /* Matched an on-disk path inside repository. */
920 if (got_repo_is_bare(repo)) {
921 /*
922 * Matched an on-disk path inside repository
923 * database. Treat input as repository-relative.
924 */
925 free(path);
926 path = canonpath;
927 canonpath = NULL;
928 } else {
929 char *child;
930 /* Strip common prefix with repository path. */
931 err = got_path_skip_common_ancestor(&child,
932 repo_abspath, path);
933 if (err)
934 goto done;
935 free(path);
936 path = child;
938 } else {
939 /*
940 * Matched unrelated on-disk path.
941 * Treat input as repository-relative.
942 */
943 free(path);
944 path = canonpath;
945 canonpath = NULL;
949 /* Make in-repository path absolute */
950 if (path[0] != '/') {
951 char *abspath;
952 if (asprintf(&abspath, "/%s", path) == -1) {
953 err = got_error_from_errno("asprintf");
954 goto done;
956 free(path);
957 path = abspath;
960 done:
961 free(canonpath);
962 if (err)
963 free(path);
964 else
965 *in_repo_path = path;
966 return err;
969 static const struct got_error *
970 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
971 const char *path_packidx)
973 const struct got_error *err = NULL;
974 size_t i;
976 for (i = 0; i < repo->pack_cache_size; i++) {
977 if (repo->packidx_cache[i] == NULL)
978 break;
979 if (strcmp(repo->packidx_cache[i]->path_packidx,
980 path_packidx) == 0) {
981 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
984 if (i == repo->pack_cache_size) {
985 do {
986 i--;
987 } while (i > 0 && repo->pinned_packidx >= 0 &&
988 i == repo->pinned_packidx);
989 err = got_packidx_close(repo->packidx_cache[i]);
990 if (err)
991 return err;
994 repo->packidx_cache[i] = packidx;
996 return NULL;
999 int
1000 got_repo_is_packidx_filename(const char *name, size_t len)
1002 if (len != GOT_PACKIDX_NAMELEN)
1003 return 0;
1005 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1006 return 0;
1008 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1009 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1010 return 0;
1012 return 1;
1015 static struct got_packidx_bloom_filter *
1016 get_packidx_bloom_filter(struct got_repository *repo,
1017 const char *path, size_t path_len)
1019 struct got_packidx_bloom_filter key;
1021 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1022 return NULL; /* XXX */
1023 key.path_len = path_len;
1025 return RB_FIND(got_packidx_bloom_filter_tree,
1026 &repo->packidx_bloom_filters, &key);
1029 int
1030 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1031 const char *path_packidx, struct got_object_id *id)
1033 struct got_packidx_bloom_filter *bf;
1035 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1036 if (bf)
1037 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1039 /* No bloom filter means this pack index must be searched. */
1040 return 1;
1043 static const struct got_error *
1044 add_packidx_bloom_filter(struct got_repository *repo,
1045 struct got_packidx *packidx, const char *path_packidx)
1047 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1048 struct got_packidx_bloom_filter *bf;
1049 size_t len;
1052 * Don't use bloom filters for very large pack index files.
1053 * Large pack files will contain a relatively large fraction
1054 * of our objects so we will likely need to visit them anyway.
1055 * The more objects a pack file contains the higher the probability
1056 * of a false-positive match from the bloom filter. And reading
1057 * all object IDs from a large pack index file can be expensive.
1059 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1060 return NULL;
1062 /* Do we already have a filter for this pack index? */
1063 if (get_packidx_bloom_filter(repo, path_packidx,
1064 strlen(path_packidx)) != NULL)
1065 return NULL;
1067 bf = calloc(1, sizeof(*bf));
1068 if (bf == NULL)
1069 return got_error_from_errno("calloc");
1070 bf->bloom = calloc(1, sizeof(*bf->bloom));
1071 if (bf->bloom == NULL) {
1072 free(bf);
1073 return got_error_from_errno("calloc");
1076 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1077 if (len >= sizeof(bf->path)) {
1078 free(bf->bloom);
1079 free(bf);
1080 return got_error(GOT_ERR_NO_SPACE);
1082 bf->path_len = len;
1084 /* Minimum size supported by our bloom filter is 1000 entries. */
1085 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1086 for (i = 0; i < nobjects; i++) {
1087 struct got_packidx_object_id *id;
1088 id = &packidx->hdr.sorted_ids[i];
1089 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1092 RB_INSERT(got_packidx_bloom_filter_tree,
1093 &repo->packidx_bloom_filters, bf);
1094 return NULL;
1097 static void
1098 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1100 struct got_pathlist_entry *pe;
1102 while (!TAILQ_EMPTY(packidx_paths)) {
1103 pe = TAILQ_FIRST(packidx_paths);
1104 TAILQ_REMOVE(packidx_paths, pe, entry);
1105 free((char *)pe->path);
1106 free(pe);
1110 static const struct got_error *
1111 refresh_packidx_paths(struct got_repository *repo)
1113 const struct got_error *err = NULL;
1114 char *objects_pack_dir = NULL;
1115 struct stat sb;
1117 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1118 if (objects_pack_dir == NULL)
1119 return got_error_from_errno("got_repo_get_path_objects_pack");
1121 if (stat(objects_pack_dir, &sb) == -1) {
1122 if (errno != ENOENT) {
1123 err = got_error_from_errno2("stat", objects_pack_dir);
1124 goto done;
1126 } else if (sb.st_mtime != repo->pack_path_mtime) {
1127 purge_packidx_paths(&repo->packidx_paths);
1128 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1129 if (err)
1130 goto done;
1132 done:
1133 free(objects_pack_dir);
1134 return err;
1137 const struct got_error *
1138 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1139 struct got_repository *repo, struct got_object_id *id)
1141 const struct got_error *err;
1142 struct got_pathlist_entry *pe;
1143 size_t i;
1145 /* Search pack index cache. */
1146 for (i = 0; i < repo->pack_cache_size; i++) {
1147 if (repo->packidx_cache[i] == NULL)
1148 break;
1149 if (!got_repo_check_packidx_bloom_filter(repo,
1150 repo->packidx_cache[i]->path_packidx, id))
1151 continue; /* object will not be found in this index */
1152 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1153 if (*idx != -1) {
1154 *packidx = repo->packidx_cache[i];
1156 * Move this cache entry to the front. Repeatedly
1157 * searching a wrong pack index can be expensive.
1159 if (i > 0) {
1160 memmove(&repo->packidx_cache[1],
1161 &repo->packidx_cache[0],
1162 i * sizeof(repo->packidx_cache[0]));
1163 repo->packidx_cache[0] = *packidx;
1164 if (repo->pinned_packidx >= 0 &&
1165 repo->pinned_packidx < i)
1166 repo->pinned_packidx++;
1167 else if (repo->pinned_packidx == i)
1168 repo->pinned_packidx = 0;
1170 return NULL;
1173 /* No luck. Search the filesystem. */
1175 err = refresh_packidx_paths(repo);
1176 if (err)
1177 return err;
1179 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1180 const char *path_packidx = pe->path;
1181 int is_cached = 0;
1183 if (!got_repo_check_packidx_bloom_filter(repo,
1184 pe->path, id))
1185 continue; /* object will not be found in this index */
1187 for (i = 0; i < repo->pack_cache_size; i++) {
1188 if (repo->packidx_cache[i] == NULL)
1189 break;
1190 if (strcmp(repo->packidx_cache[i]->path_packidx,
1191 path_packidx) == 0) {
1192 is_cached = 1;
1193 break;
1196 if (is_cached)
1197 continue; /* already searched */
1199 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1200 path_packidx, 0);
1201 if (err)
1202 goto done;
1204 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1205 if (err)
1206 goto done;
1208 err = cache_packidx(repo, *packidx, path_packidx);
1209 if (err)
1210 goto done;
1212 *idx = got_packidx_get_object_idx(*packidx, id);
1213 if (*idx != -1) {
1214 err = NULL; /* found the object */
1215 goto done;
1219 err = got_error_no_obj(id);
1220 done:
1221 return err;
1224 const struct got_error *
1225 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1226 struct got_repository *repo)
1228 const struct got_error *err = NULL;
1229 DIR *packdir = NULL;
1230 struct dirent *dent;
1231 char *path_packidx = NULL;
1232 int packdir_fd;
1233 struct stat sb;
1235 packdir_fd = openat(got_repo_get_fd(repo),
1236 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1237 if (packdir_fd == -1) {
1238 return got_error_from_errno_fmt("openat: %s/%s",
1239 got_repo_get_path_git_dir(repo),
1240 GOT_OBJECTS_PACK_DIR);
1243 packdir = fdopendir(packdir_fd);
1244 if (packdir == NULL) {
1245 err = got_error_from_errno("fdopendir");
1246 goto done;
1249 if (fstat(packdir_fd, &sb) == -1) {
1250 err = got_error_from_errno("fstat");
1251 goto done;
1253 repo->pack_path_mtime = sb.st_mtime;
1255 while ((dent = readdir(packdir)) != NULL) {
1256 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1257 continue;
1259 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1260 dent->d_name) == -1) {
1261 err = got_error_from_errno("asprintf");
1262 path_packidx = NULL;
1263 break;
1266 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1267 if (err)
1268 break;
1270 done:
1271 if (err)
1272 free(path_packidx);
1273 if (packdir && closedir(packdir) != 0 && err == NULL)
1274 err = got_error_from_errno("closedir");
1275 return err;
1278 const struct got_error *
1279 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1280 struct got_repository *repo)
1282 const struct got_error *err;
1283 size_t i;
1285 *packidx = NULL;
1287 /* Search pack index cache. */
1288 for (i = 0; i < repo->pack_cache_size; i++) {
1289 if (repo->packidx_cache[i] == NULL)
1290 break;
1291 if (strcmp(repo->packidx_cache[i]->path_packidx,
1292 path_packidx) == 0) {
1293 *packidx = repo->packidx_cache[i];
1294 return NULL;
1297 /* No luck. Search the filesystem. */
1299 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1300 path_packidx, 0);
1301 if (err)
1302 return err;
1304 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1305 if (err)
1306 goto done;
1308 err = cache_packidx(repo, *packidx, path_packidx);
1309 done:
1310 if (err) {
1311 got_packidx_close(*packidx);
1312 *packidx = NULL;
1314 return err;
1317 static const struct got_error *
1318 read_packfile_hdr(int fd, struct got_packidx *packidx)
1320 const struct got_error *err = NULL;
1321 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1322 struct got_packfile_hdr hdr;
1323 ssize_t n;
1325 n = read(fd, &hdr, sizeof(hdr));
1326 if (n < 0)
1327 return got_error_from_errno("read");
1328 if (n != sizeof(hdr))
1329 return got_error(GOT_ERR_BAD_PACKFILE);
1331 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1332 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1333 be32toh(hdr.nobjects) != totobj)
1334 err = got_error(GOT_ERR_BAD_PACKFILE);
1336 return err;
1339 static const struct got_error *
1340 open_packfile(int *fd, struct got_repository *repo,
1341 const char *relpath, struct got_packidx *packidx)
1343 const struct got_error *err = NULL;
1345 *fd = openat(got_repo_get_fd(repo), relpath,
1346 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1347 if (*fd == -1)
1348 return got_error_from_errno_fmt("openat: %s/%s",
1349 got_repo_get_path_git_dir(repo), relpath);
1351 if (packidx) {
1352 err = read_packfile_hdr(*fd, packidx);
1353 if (err) {
1354 close(*fd);
1355 *fd = -1;
1359 return err;
1362 const struct got_error *
1363 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1364 const char *path_packfile, struct got_packidx *packidx)
1366 const struct got_error *err = NULL;
1367 struct got_pack *pack = NULL;
1368 struct stat sb;
1369 size_t i;
1371 if (packp)
1372 *packp = NULL;
1374 for (i = 0; i < repo->pack_cache_size; i++) {
1375 pack = &repo->packs[i];
1376 if (pack->path_packfile == NULL)
1377 break;
1378 if (strcmp(pack->path_packfile, path_packfile) == 0)
1379 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1382 if (i == repo->pack_cache_size) {
1383 struct got_pack tmp;
1384 do {
1385 i--;
1386 } while (i > 0 && repo->pinned_pack >= 0 &&
1387 i == repo->pinned_pack);
1388 err = got_pack_close(&repo->packs[i]);
1389 if (err)
1390 return err;
1391 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1392 return got_error_from_errno("ftruncate");
1393 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1394 return got_error_from_errno("ftruncate");
1395 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1396 memcpy(&repo->packs[i], &repo->packs[0],
1397 sizeof(repo->packs[i]));
1398 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1399 if (repo->pinned_pack == 0)
1400 repo->pinned_pack = i;
1401 else if (repo->pinned_pack == i)
1402 repo->pinned_pack = 0;
1403 i = 0;
1406 pack = &repo->packs[i];
1408 pack->path_packfile = strdup(path_packfile);
1409 if (pack->path_packfile == NULL) {
1410 err = got_error_from_errno("strdup");
1411 goto done;
1414 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1415 if (err)
1416 goto done;
1418 if (fstat(pack->fd, &sb) != 0) {
1419 err = got_error_from_errno("fstat");
1420 goto done;
1422 pack->filesize = sb.st_size;
1424 pack->privsep_child = NULL;
1426 err = got_delta_cache_alloc(&pack->delta_cache);
1427 if (err)
1428 goto done;
1430 #ifndef GOT_PACK_NO_MMAP
1431 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1432 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1433 pack->fd, 0);
1434 if (pack->map == MAP_FAILED) {
1435 if (errno != ENOMEM) {
1436 err = got_error_from_errno("mmap");
1437 goto done;
1439 pack->map = NULL; /* fall back to read(2) */
1442 #endif
1443 done:
1444 if (err) {
1445 if (pack)
1446 got_pack_close(pack);
1447 } else if (packp)
1448 *packp = pack;
1449 return err;
1452 struct got_pack *
1453 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1455 struct got_pack *pack = NULL;
1456 size_t i;
1458 for (i = 0; i < repo->pack_cache_size; i++) {
1459 pack = &repo->packs[i];
1460 if (pack->path_packfile == NULL)
1461 break;
1462 if (strcmp(pack->path_packfile, path_packfile) == 0)
1463 return pack;
1466 return NULL;
1469 const struct got_error *
1470 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1471 struct got_pack *pack)
1473 size_t i;
1474 int pinned_pack = -1, pinned_packidx = -1;
1476 for (i = 0; i < repo->pack_cache_size; i++) {
1477 if (repo->packidx_cache[i] &&
1478 strcmp(repo->packidx_cache[i]->path_packidx,
1479 packidx->path_packidx) == 0)
1480 pinned_packidx = i;
1481 if (repo->packs[i].path_packfile &&
1482 strcmp(repo->packs[i].path_packfile,
1483 pack->path_packfile) == 0)
1484 pinned_pack = i;
1487 if (pinned_packidx == -1 || pinned_pack == -1)
1488 return got_error(GOT_ERR_PIN_PACK);
1490 repo->pinned_pack = pinned_pack;
1491 repo->pinned_packidx = pinned_packidx;
1492 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1493 return NULL;
1496 struct got_pack *
1497 got_repo_get_pinned_pack(struct got_repository *repo)
1499 if (repo->pinned_pack >= 0 &&
1500 repo->pinned_pack < repo->pack_cache_size)
1501 return &repo->packs[repo->pinned_pack];
1503 return NULL;
1506 void
1507 got_repo_unpin_pack(struct got_repository *repo)
1509 repo->pinned_packidx = -1;
1510 repo->pinned_pack = -1;
1511 repo->pinned_pid = 0;
1514 const struct got_error *
1515 got_repo_init(const char *repo_path, const char *head_name)
1517 const struct got_error *err = NULL;
1518 const char *dirnames[] = {
1519 GOT_OBJECTS_DIR,
1520 GOT_OBJECTS_PACK_DIR,
1521 GOT_REFS_DIR,
1523 const char *description_str = "Unnamed repository; "
1524 "edit this file 'description' to name the repository.";
1525 const char *headref = "ref: refs/heads/";
1526 const char *gitconfig_str = "[core]\n"
1527 "\trepositoryformatversion = 0\n"
1528 "\tfilemode = true\n"
1529 "\tbare = true\n";
1530 char *headref_str, *path;
1531 size_t i;
1533 if (!got_path_dir_is_empty(repo_path))
1534 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1536 for (i = 0; i < nitems(dirnames); i++) {
1537 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1538 return got_error_from_errno("asprintf");
1540 err = got_path_mkdir(path);
1541 free(path);
1542 if (err)
1543 return err;
1546 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1547 return got_error_from_errno("asprintf");
1548 err = got_path_create_file(path, description_str);
1549 free(path);
1550 if (err)
1551 return err;
1553 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1554 return got_error_from_errno("asprintf");
1555 if (asprintf(&headref_str, "%s%s", headref,
1556 head_name ? head_name : "main") == -1) {
1557 free(path);
1558 return got_error_from_errno("asprintf");
1560 err = got_path_create_file(path, headref_str);
1561 free(headref_str);
1562 free(path);
1563 if (err)
1564 return err;
1566 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1567 return got_error_from_errno("asprintf");
1568 err = got_path_create_file(path, gitconfig_str);
1569 free(path);
1570 if (err)
1571 return err;
1573 return NULL;
1576 static const struct got_error *
1577 match_packed_object(struct got_object_id **unique_id,
1578 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1580 const struct got_error *err = NULL;
1581 struct got_object_id_queue matched_ids;
1582 struct got_pathlist_entry *pe;
1584 STAILQ_INIT(&matched_ids);
1586 err = refresh_packidx_paths(repo);
1587 if (err)
1588 return err;
1590 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1591 const char *path_packidx = pe->path;
1592 struct got_packidx *packidx;
1593 struct got_object_qid *qid;
1595 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1596 path_packidx, 0);
1597 if (err)
1598 break;
1600 err = got_packidx_match_id_str_prefix(&matched_ids,
1601 packidx, id_str_prefix);
1602 if (err) {
1603 got_packidx_close(packidx);
1604 break;
1606 err = got_packidx_close(packidx);
1607 if (err)
1608 break;
1610 STAILQ_FOREACH(qid, &matched_ids, entry) {
1611 if (obj_type != GOT_OBJ_TYPE_ANY) {
1612 int matched_type;
1613 err = got_object_get_type(&matched_type, repo,
1614 &qid->id);
1615 if (err)
1616 goto done;
1617 if (matched_type != obj_type)
1618 continue;
1620 if (*unique_id == NULL) {
1621 *unique_id = got_object_id_dup(&qid->id);
1622 if (*unique_id == NULL) {
1623 err = got_error_from_errno("malloc");
1624 goto done;
1626 } else {
1627 if (got_object_id_cmp(*unique_id,
1628 &qid->id) == 0)
1629 continue; /* packed multiple times */
1630 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1631 goto done;
1635 done:
1636 got_object_id_queue_free(&matched_ids);
1637 if (err) {
1638 free(*unique_id);
1639 *unique_id = NULL;
1641 return err;
1644 static const struct got_error *
1645 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1646 const char *object_dir, const char *id_str_prefix, int obj_type,
1647 struct got_repository *repo)
1649 const struct got_error *err = NULL;
1650 char *path, *id_str = NULL;
1651 DIR *dir = NULL;
1652 struct dirent *dent;
1653 struct got_object_id id;
1655 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1656 err = got_error_from_errno("asprintf");
1657 goto done;
1660 dir = opendir(path);
1661 if (dir == NULL) {
1662 if (errno == ENOENT) {
1663 err = NULL;
1664 goto done;
1666 err = got_error_from_errno2("opendir", path);
1667 goto done;
1669 while ((dent = readdir(dir)) != NULL) {
1670 int cmp;
1672 free(id_str);
1673 id_str = NULL;
1675 if (strcmp(dent->d_name, ".") == 0 ||
1676 strcmp(dent->d_name, "..") == 0)
1677 continue;
1679 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1680 err = got_error_from_errno("asprintf");
1681 goto done;
1684 if (!got_parse_sha1_digest(id.sha1, id_str))
1685 continue;
1688 * Directory entries do not necessarily appear in
1689 * sorted order, so we must iterate over all of them.
1691 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1692 if (cmp != 0)
1693 continue;
1695 if (*unique_id == NULL) {
1696 if (obj_type != GOT_OBJ_TYPE_ANY) {
1697 int matched_type;
1698 err = got_object_get_type(&matched_type, repo,
1699 &id);
1700 if (err)
1701 goto done;
1702 if (matched_type != obj_type)
1703 continue;
1705 *unique_id = got_object_id_dup(&id);
1706 if (*unique_id == NULL) {
1707 err = got_error_from_errno("got_object_id_dup");
1708 goto done;
1710 } else {
1711 if (got_object_id_cmp(*unique_id, &id) == 0)
1712 continue; /* both packed and loose */
1713 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1714 goto done;
1717 done:
1718 if (dir && closedir(dir) != 0 && err == NULL)
1719 err = got_error_from_errno("closedir");
1720 if (err) {
1721 free(*unique_id);
1722 *unique_id = NULL;
1724 free(id_str);
1725 free(path);
1726 return err;
1729 const struct got_error *
1730 got_repo_match_object_id_prefix(struct got_object_id **id,
1731 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1733 const struct got_error *err = NULL;
1734 char *path_objects = NULL, *object_dir = NULL;
1735 size_t len;
1736 int i;
1738 *id = NULL;
1740 path_objects = got_repo_get_path_objects(repo);
1742 len = strlen(id_str_prefix);
1743 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1744 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1745 goto done;
1748 for (i = 0; i < len; i++) {
1749 if (isxdigit((unsigned char)id_str_prefix[i]))
1750 continue;
1751 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1752 goto done;
1755 if (len >= 2) {
1756 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1757 if (err)
1758 goto done;
1759 object_dir = strndup(id_str_prefix, 2);
1760 if (object_dir == NULL) {
1761 err = got_error_from_errno("strdup");
1762 goto done;
1764 err = match_loose_object(id, path_objects, object_dir,
1765 id_str_prefix, obj_type, repo);
1766 } else if (len == 1) {
1767 int i;
1768 for (i = 0; i < 0xf; i++) {
1769 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1770 == -1) {
1771 err = got_error_from_errno("asprintf");
1772 goto done;
1774 err = match_packed_object(id, repo, object_dir,
1775 obj_type);
1776 if (err)
1777 goto done;
1778 err = match_loose_object(id, path_objects, object_dir,
1779 id_str_prefix, obj_type, repo);
1780 if (err)
1781 goto done;
1783 } else {
1784 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1785 goto done;
1787 done:
1788 free(path_objects);
1789 free(object_dir);
1790 if (err) {
1791 free(*id);
1792 *id = NULL;
1793 } else if (*id == NULL) {
1794 switch (obj_type) {
1795 case GOT_OBJ_TYPE_BLOB:
1796 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1797 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1798 break;
1799 case GOT_OBJ_TYPE_TREE:
1800 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1801 GOT_OBJ_LABEL_TREE, id_str_prefix);
1802 break;
1803 case GOT_OBJ_TYPE_COMMIT:
1804 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1805 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1806 break;
1807 case GOT_OBJ_TYPE_TAG:
1808 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1809 GOT_OBJ_LABEL_TAG, id_str_prefix);
1810 break;
1811 default:
1812 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1813 break;
1817 return err;
1820 const struct got_error *
1821 got_repo_match_object_id(struct got_object_id **id, char **label,
1822 const char *id_str, int obj_type, struct got_reflist_head *refs,
1823 struct got_repository *repo)
1825 const struct got_error *err;
1826 struct got_tag_object *tag;
1827 struct got_reference *ref = NULL;
1829 *id = NULL;
1830 if (label)
1831 *label = NULL;
1833 if (refs) {
1834 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1835 refs, repo);
1836 if (err == NULL) {
1837 *id = got_object_id_dup(
1838 got_object_tag_get_object_id(tag));
1839 if (*id == NULL)
1840 err = got_error_from_errno("got_object_id_dup");
1841 else if (label && asprintf(label, "refs/tags/%s",
1842 got_object_tag_get_name(tag)) == -1) {
1843 err = got_error_from_errno("asprintf");
1844 free(*id);
1845 *id = NULL;
1847 got_object_tag_close(tag);
1848 return err;
1849 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1850 err->code != GOT_ERR_NO_OBJ)
1851 return err;
1854 err = got_ref_open(&ref, repo, id_str, 0);
1855 if (err == NULL) {
1856 err = got_ref_resolve(id, repo, ref);
1857 if (err)
1858 goto done;
1859 if (label) {
1860 *label = strdup(got_ref_get_name(ref));
1861 if (*label == NULL) {
1862 err = got_error_from_errno("strdup");
1863 goto done;
1866 } else {
1867 if (err->code != GOT_ERR_NOT_REF &&
1868 err->code != GOT_ERR_BAD_REF_NAME)
1869 goto done;
1870 err = got_repo_match_object_id_prefix(id, id_str,
1871 obj_type, repo);
1872 if (err) {
1873 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1874 err = got_error_not_ref(id_str);
1875 goto done;
1877 if (label) {
1878 err = got_object_id_str(label, *id);
1879 if (*label == NULL) {
1880 err = got_error_from_errno("strdup");
1881 goto done;
1885 done:
1886 if (ref)
1887 got_ref_close(ref);
1888 return err;
1891 const struct got_error *
1892 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1893 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1895 const struct got_error *err = NULL;
1896 struct got_reflist_entry *re;
1897 struct got_object_id *tag_id;
1898 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1900 *tag = NULL;
1902 TAILQ_FOREACH(re, refs, entry) {
1903 const char *refname;
1904 refname = got_ref_get_name(re->ref);
1905 if (got_ref_is_symbolic(re->ref))
1906 continue;
1907 if (strncmp(refname, "refs/tags/", 10) != 0)
1908 continue;
1909 if (!name_is_absolute)
1910 refname += strlen("refs/tags/");
1911 if (strcmp(refname, name) != 0)
1912 continue;
1913 err = got_ref_resolve(&tag_id, repo, re->ref);
1914 if (err)
1915 break;
1916 err = got_object_open_as_tag(tag, repo, tag_id);
1917 free(tag_id);
1918 if (err)
1919 break;
1920 if (obj_type == GOT_OBJ_TYPE_ANY ||
1921 got_object_tag_get_object_type(*tag) == obj_type)
1922 break;
1923 got_object_tag_close(*tag);
1924 *tag = NULL;
1927 if (err == NULL && *tag == NULL)
1928 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1929 GOT_OBJ_LABEL_TAG, name);
1930 return err;
1933 static const struct got_error *
1934 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1935 const char *name, mode_t mode, struct got_object_id *blob_id)
1937 const struct got_error *err = NULL;
1939 *new_te = NULL;
1941 *new_te = calloc(1, sizeof(**new_te));
1942 if (*new_te == NULL)
1943 return got_error_from_errno("calloc");
1945 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1946 sizeof((*new_te)->name)) {
1947 err = got_error(GOT_ERR_NO_SPACE);
1948 goto done;
1951 if (S_ISLNK(mode)) {
1952 (*new_te)->mode = S_IFLNK;
1953 } else {
1954 (*new_te)->mode = S_IFREG;
1955 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1957 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1958 done:
1959 if (err && *new_te) {
1960 free(*new_te);
1961 *new_te = NULL;
1963 return err;
1966 static const struct got_error *
1967 import_file(struct got_tree_entry **new_te, struct dirent *de,
1968 const char *path, struct got_repository *repo)
1970 const struct got_error *err;
1971 struct got_object_id *blob_id = NULL;
1972 char *filepath;
1973 struct stat sb;
1975 if (asprintf(&filepath, "%s%s%s", path,
1976 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1977 return got_error_from_errno("asprintf");
1979 if (lstat(filepath, &sb) != 0) {
1980 err = got_error_from_errno2("lstat", path);
1981 goto done;
1984 err = got_object_blob_create(&blob_id, filepath, repo);
1985 if (err)
1986 goto done;
1988 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1989 blob_id);
1990 done:
1991 free(filepath);
1992 if (err)
1993 free(blob_id);
1994 return err;
1997 static const struct got_error *
1998 insert_tree_entry(struct got_tree_entry *new_te,
1999 struct got_pathlist_head *paths)
2001 const struct got_error *err = NULL;
2002 struct got_pathlist_entry *new_pe;
2004 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2005 if (err)
2006 return err;
2007 if (new_pe == NULL)
2008 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2009 return NULL;
2012 static const struct got_error *write_tree(struct got_object_id **,
2013 const char *, struct got_pathlist_head *, struct got_repository *,
2014 got_repo_import_cb progress_cb, void *progress_arg);
2016 static const struct got_error *
2017 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2018 const char *path, struct got_pathlist_head *ignores,
2019 struct got_repository *repo,
2020 got_repo_import_cb progress_cb, void *progress_arg)
2022 const struct got_error *err;
2023 struct got_object_id *id = NULL;
2024 char *subdirpath;
2026 if (asprintf(&subdirpath, "%s%s%s", path,
2027 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2028 return got_error_from_errno("asprintf");
2030 (*new_te) = calloc(1, sizeof(**new_te));
2031 if (*new_te == NULL)
2032 return got_error_from_errno("calloc");
2033 (*new_te)->mode = S_IFDIR;
2034 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2035 sizeof((*new_te)->name)) {
2036 err = got_error(GOT_ERR_NO_SPACE);
2037 goto done;
2039 err = write_tree(&id, subdirpath, ignores, repo,
2040 progress_cb, progress_arg);
2041 if (err)
2042 goto done;
2043 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2045 done:
2046 free(id);
2047 free(subdirpath);
2048 if (err) {
2049 free(*new_te);
2050 *new_te = NULL;
2052 return err;
2055 static const struct got_error *
2056 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2057 struct got_pathlist_head *ignores, struct got_repository *repo,
2058 got_repo_import_cb progress_cb, void *progress_arg)
2060 const struct got_error *err = NULL;
2061 DIR *dir;
2062 struct dirent *de;
2063 int nentries;
2064 struct got_tree_entry *new_te = NULL;
2065 struct got_pathlist_head paths;
2066 struct got_pathlist_entry *pe;
2068 *new_tree_id = NULL;
2070 TAILQ_INIT(&paths);
2072 dir = opendir(path_dir);
2073 if (dir == NULL) {
2074 err = got_error_from_errno2("opendir", path_dir);
2075 goto done;
2078 nentries = 0;
2079 while ((de = readdir(dir)) != NULL) {
2080 int ignore = 0;
2081 int type;
2083 if (strcmp(de->d_name, ".") == 0 ||
2084 strcmp(de->d_name, "..") == 0)
2085 continue;
2087 TAILQ_FOREACH(pe, ignores, entry) {
2088 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2089 ignore = 1;
2090 break;
2093 if (ignore)
2094 continue;
2096 err = got_path_dirent_type(&type, path_dir, de);
2097 if (err)
2098 goto done;
2100 if (type == DT_DIR) {
2101 err = import_subdir(&new_te, de, path_dir,
2102 ignores, repo, progress_cb, progress_arg);
2103 if (err) {
2104 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2105 goto done;
2106 err = NULL;
2107 continue;
2109 } else if (type == DT_REG || type == DT_LNK) {
2110 err = import_file(&new_te, de, path_dir, repo);
2111 if (err)
2112 goto done;
2113 } else
2114 continue;
2116 err = insert_tree_entry(new_te, &paths);
2117 if (err)
2118 goto done;
2119 nentries++;
2122 if (TAILQ_EMPTY(&paths)) {
2123 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2124 "cannot create tree without any entries");
2125 goto done;
2128 TAILQ_FOREACH(pe, &paths, entry) {
2129 struct got_tree_entry *te = pe->data;
2130 char *path;
2131 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2132 continue;
2133 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2134 err = got_error_from_errno("asprintf");
2135 goto done;
2137 err = (*progress_cb)(progress_arg, path);
2138 free(path);
2139 if (err)
2140 goto done;
2143 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2144 done:
2145 if (dir)
2146 closedir(dir);
2147 got_pathlist_free(&paths);
2148 return err;
2151 const struct got_error *
2152 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2153 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2154 struct got_repository *repo, got_repo_import_cb progress_cb,
2155 void *progress_arg)
2157 const struct got_error *err;
2158 struct got_object_id *new_tree_id;
2160 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2161 progress_cb, progress_arg);
2162 if (err)
2163 return err;
2165 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2166 author, time(NULL), author, time(NULL), logmsg, repo);
2167 free(new_tree_id);
2168 return err;
2171 const struct got_error *
2172 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2173 struct got_repository *repo)
2175 const struct got_error *err = NULL;
2176 char *path_objects = NULL, *path = NULL;
2177 DIR *dir = NULL;
2178 struct got_object_id id;
2179 int i;
2181 *nobjects = 0;
2182 *ondisk_size = 0;
2184 path_objects = got_repo_get_path_objects(repo);
2185 if (path_objects == NULL)
2186 return got_error_from_errno("got_repo_get_path_objects");
2188 for (i = 0; i <= 0xff; i++) {
2189 struct dirent *dent;
2191 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2192 err = got_error_from_errno("asprintf");
2193 break;
2196 dir = opendir(path);
2197 if (dir == NULL) {
2198 if (errno == ENOENT) {
2199 err = NULL;
2200 continue;
2202 err = got_error_from_errno2("opendir", path);
2203 break;
2206 while ((dent = readdir(dir)) != NULL) {
2207 char *id_str;
2208 int fd;
2209 struct stat sb;
2211 if (strcmp(dent->d_name, ".") == 0 ||
2212 strcmp(dent->d_name, "..") == 0)
2213 continue;
2215 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2216 err = got_error_from_errno("asprintf");
2217 goto done;
2220 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2221 free(id_str);
2222 continue;
2224 free(id_str);
2226 err = got_object_open_loose_fd(&fd, &id, repo);
2227 if (err)
2228 goto done;
2230 if (fstat(fd, &sb) == -1) {
2231 err = got_error_from_errno("fstat");
2232 close(fd);
2233 goto done;
2235 (*nobjects)++;
2236 (*ondisk_size) += sb.st_size;
2238 if (close(fd) == -1) {
2239 err = got_error_from_errno("close");
2240 goto done;
2244 if (closedir(dir) != 0) {
2245 err = got_error_from_errno("closedir");
2246 goto done;
2248 dir = NULL;
2250 free(path);
2251 path = NULL;
2253 done:
2254 if (dir && closedir(dir) != 0 && err == NULL)
2255 err = got_error_from_errno("closedir");
2257 if (err) {
2258 *nobjects = 0;
2259 *ondisk_size = 0;
2261 free(path_objects);
2262 free(path);
2263 return err;
2266 const struct got_error *
2267 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2268 off_t *total_packsize, struct got_repository *repo)
2270 const struct got_error *err = NULL;
2271 DIR *packdir = NULL;
2272 struct dirent *dent;
2273 struct got_packidx *packidx = NULL;
2274 char *path_packidx;
2275 char *path_packfile;
2276 int packdir_fd;
2277 struct stat sb;
2279 *npackfiles = 0;
2280 *nobjects = 0;
2281 *total_packsize = 0;
2283 packdir_fd = openat(got_repo_get_fd(repo),
2284 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2285 if (packdir_fd == -1) {
2286 return got_error_from_errno_fmt("openat: %s/%s",
2287 got_repo_get_path_git_dir(repo),
2288 GOT_OBJECTS_PACK_DIR);
2291 packdir = fdopendir(packdir_fd);
2292 if (packdir == NULL) {
2293 err = got_error_from_errno("fdopendir");
2294 goto done;
2297 while ((dent = readdir(packdir)) != NULL) {
2298 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2299 continue;
2301 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2302 dent->d_name) == -1) {
2303 err = got_error_from_errno("asprintf");
2304 goto done;
2307 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2308 path_packidx, 0);
2309 free(path_packidx);
2310 if (err)
2311 goto done;
2313 if (fstat(packidx->fd, &sb) == -1)
2314 goto done;
2315 *total_packsize += sb.st_size;
2317 err = got_packidx_get_packfile_path(&path_packfile,
2318 packidx->path_packidx);
2319 if (err)
2320 goto done;
2322 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2323 0) == -1) {
2324 free(path_packfile);
2325 goto done;
2327 free(path_packfile);
2328 *total_packsize += sb.st_size;
2330 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2332 (*npackfiles)++;
2334 got_packidx_close(packidx);
2335 packidx = NULL;
2337 done:
2338 if (packidx)
2339 got_packidx_close(packidx);
2340 if (packdir && closedir(packdir) != 0 && err == NULL)
2341 err = got_error_from_errno("closedir");
2342 if (err) {
2343 *npackfiles = 0;
2344 *nobjects = 0;
2345 *total_packsize = 0;
2347 return err;
2350 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2351 got_packidx_bloom_filter_cmp);