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 int
126 got_repo_has_extension(struct got_repository *repo, const char *ext)
128 int i;
130 for (i = 0; i < repo->nextensions; ++i) {
131 if (!strcasecmp(ext, repo->extensions[i]))
132 return 1;
135 return 0;
138 int
139 got_repo_is_bare(struct got_repository *repo)
141 return (strcmp(repo->path, repo->path_git_dir) == 0);
144 static char *
145 get_path_git_child(struct got_repository *repo, const char *basename)
147 char *path_child;
149 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
150 basename) == -1)
151 return NULL;
153 return path_child;
156 char *
157 got_repo_get_path_objects(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_DIR);
162 char *
163 got_repo_get_path_objects_pack(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
168 char *
169 got_repo_get_path_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_REFS_DIR);
174 char *
175 got_repo_get_path_packed_refs(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
180 static char *
181 get_path_head(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_HEAD_FILE);
186 char *
187 got_repo_get_path_gitconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GITCONFIG);
192 char *
193 got_repo_get_path_gotconfig(struct got_repository *repo)
195 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
198 const struct got_gotconfig *
199 got_repo_get_gotconfig(struct got_repository *repo)
201 return repo->gotconfig;
204 void
205 got_repo_get_gitconfig_remotes(int *nremotes,
206 const struct got_remote_repo **remotes, struct got_repository *repo)
208 *nremotes = repo->ngitconfig_remotes;
209 *remotes = repo->gitconfig_remotes;
212 static int
213 is_git_repo(struct got_repository *repo)
215 const char *path_git = got_repo_get_path_git_dir(repo);
216 char *path_objects = got_repo_get_path_objects(repo);
217 char *path_refs = got_repo_get_path_refs(repo);
218 char *path_head = get_path_head(repo);
219 int ret = 0;
220 struct stat sb;
221 struct got_reference *head_ref;
223 if (lstat(path_git, &sb) == -1)
224 goto done;
225 if (!S_ISDIR(sb.st_mode))
226 goto done;
228 if (lstat(path_objects, &sb) == -1)
229 goto done;
230 if (!S_ISDIR(sb.st_mode))
231 goto done;
233 if (lstat(path_refs, &sb) == -1)
234 goto done;
235 if (!S_ISDIR(sb.st_mode))
236 goto done;
238 if (lstat(path_head, &sb) == -1)
239 goto done;
240 if (!S_ISREG(sb.st_mode))
241 goto done;
243 /* Check if the HEAD reference can be opened. */
244 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
245 goto done;
246 got_ref_close(head_ref);
248 ret = 1;
249 done:
250 free(path_objects);
251 free(path_refs);
252 free(path_head);
253 return ret;
257 static const struct got_error *
258 close_tempfiles(int *fds, size_t nfds)
260 const struct got_error *err = NULL;
261 int i;
263 for (i = 0; i < nfds; i++) {
264 if (fds[i] == -1)
265 continue;
266 if (close(fds[i]) == -1) {
267 err = got_error_from_errno("close");
268 break;
271 free(fds);
272 return err;
275 static const struct got_error *
276 open_tempfiles(int **fds, size_t array_size, size_t nfds)
278 const struct got_error *err = NULL;
279 int i;
281 *fds = calloc(array_size, sizeof(**fds));
282 if (*fds == NULL)
283 return got_error_from_errno("calloc");
285 for (i = 0; i < array_size; i++)
286 (*fds)[i] = -1;
288 for (i = 0; i < nfds; i++) {
289 (*fds)[i] = got_opentempfd();
290 if ((*fds)[i] == -1) {
291 err = got_error_from_errno("got_opentempfd");
292 close_tempfiles(*fds, nfds);
293 *fds = NULL;
294 return err;
298 return NULL;
301 static const struct got_error *
302 get_pack_cache_size(int *pack_cache_size)
304 struct rlimit rl;
306 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
307 return got_error_from_errno("getrlimit");
309 *pack_cache_size = GOT_PACK_CACHE_SIZE;
310 if (*pack_cache_size > rl.rlim_cur / 8)
311 *pack_cache_size = rl.rlim_cur / 8;
313 return NULL;
316 const struct got_error *
317 got_repo_pack_fds_open(int **pack_fds)
319 const struct got_error *err;
320 int nfds;
322 err = get_pack_cache_size(&nfds);
323 if (err)
324 return err;
326 /*
327 * We need one basefd and one accumfd per cached pack.
328 * Our constants should be set up in a way such that
329 * this error never triggers.
330 */
331 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
332 return got_error(GOT_ERR_NO_SPACE);
334 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
337 const struct got_error *
338 got_repo_pack_fds_close(int *pack_fds)
340 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
343 const struct got_error *
344 got_repo_temp_fds_open(int **temp_fds)
346 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
347 GOT_REPO_NUM_TEMPFILES);
350 void
351 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
353 int i;
355 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
356 repo->tempfiles[i] = temp_fds[i];
359 const struct got_error *
360 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
362 int i;
364 *fd = -1;
365 *idx = -1;
367 for (i = 0; i < nitems(repo->tempfiles); i++) {
368 if (repo->tempfile_use_mask & (1 << i))
369 continue;
370 if (repo->tempfiles[i] != -1) {
371 if (ftruncate(repo->tempfiles[i], 0L) == -1)
372 return got_error_from_errno("ftruncate");
373 *fd = repo->tempfiles[i];
374 *idx = i;
375 repo->tempfile_use_mask |= (1 << i);
376 return NULL;
380 return got_error(GOT_ERR_REPO_TEMPFILE);
383 void
384 got_repo_temp_fds_put(int idx, struct got_repository *repo)
386 repo->tempfile_use_mask &= ~(1 << idx);
389 const struct got_error *
390 got_repo_temp_fds_close(int *temp_fds)
392 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
395 const struct got_error *
396 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
397 struct got_object *obj)
399 #ifndef GOT_NO_OBJ_CACHE
400 const struct got_error *err = NULL;
401 err = got_object_cache_add(&repo->objcache, id, obj);
402 if (err) {
403 if (err->code == GOT_ERR_OBJ_EXISTS ||
404 err->code == GOT_ERR_OBJ_TOO_LARGE)
405 err = NULL;
406 return err;
408 obj->refcnt++;
409 #endif
410 return NULL;
413 struct got_object *
414 got_repo_get_cached_object(struct got_repository *repo,
415 struct got_object_id *id)
417 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
420 const struct got_error *
421 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
422 struct got_tree_object *tree)
424 #ifndef GOT_NO_OBJ_CACHE
425 const struct got_error *err = NULL;
426 err = got_object_cache_add(&repo->treecache, id, tree);
427 if (err) {
428 if (err->code == GOT_ERR_OBJ_EXISTS ||
429 err->code == GOT_ERR_OBJ_TOO_LARGE)
430 err = NULL;
431 return err;
433 tree->refcnt++;
434 #endif
435 return NULL;
438 struct got_tree_object *
439 got_repo_get_cached_tree(struct got_repository *repo,
440 struct got_object_id *id)
442 return (struct got_tree_object *)got_object_cache_get(
443 &repo->treecache, id);
446 const struct got_error *
447 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
448 struct got_commit_object *commit)
450 #ifndef GOT_NO_OBJ_CACHE
451 const struct got_error *err = NULL;
452 err = got_object_cache_add(&repo->commitcache, id, commit);
453 if (err) {
454 if (err->code == GOT_ERR_OBJ_EXISTS ||
455 err->code == GOT_ERR_OBJ_TOO_LARGE)
456 err = NULL;
457 return err;
459 commit->refcnt++;
460 #endif
461 return NULL;
464 struct got_commit_object *
465 got_repo_get_cached_commit(struct got_repository *repo,
466 struct got_object_id *id)
468 return (struct got_commit_object *)got_object_cache_get(
469 &repo->commitcache, id);
472 const struct got_error *
473 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
474 struct got_tag_object *tag)
476 #ifndef GOT_NO_OBJ_CACHE
477 const struct got_error *err = NULL;
478 err = got_object_cache_add(&repo->tagcache, id, tag);
479 if (err) {
480 if (err->code == GOT_ERR_OBJ_EXISTS ||
481 err->code == GOT_ERR_OBJ_TOO_LARGE)
482 err = NULL;
483 return err;
485 tag->refcnt++;
486 #endif
487 return NULL;
490 struct got_tag_object *
491 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
493 return (struct got_tag_object *)got_object_cache_get(
494 &repo->tagcache, id);
497 const struct got_error *
498 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
499 struct got_raw_object *raw)
501 #ifndef GOT_NO_OBJ_CACHE
502 const struct got_error *err = NULL;
503 err = got_object_cache_add(&repo->rawcache, id, raw);
504 if (err) {
505 if (err->code == GOT_ERR_OBJ_EXISTS ||
506 err->code == GOT_ERR_OBJ_TOO_LARGE)
507 err = NULL;
508 return err;
510 raw->refcnt++;
511 #endif
512 return NULL;
516 struct got_raw_object *
517 got_repo_get_cached_raw_object(struct got_repository *repo,
518 struct got_object_id *id)
520 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
524 static const struct got_error *
525 open_repo(struct got_repository *repo, const char *path)
527 const struct got_error *err = NULL;
529 repo->gitdir_fd = -1;
531 /* bare git repository? */
532 repo->path_git_dir = strdup(path);
533 if (repo->path_git_dir == NULL)
534 return got_error_from_errno("strdup");
535 if (is_git_repo(repo)) {
536 repo->path = strdup(repo->path_git_dir);
537 if (repo->path == NULL) {
538 err = got_error_from_errno("strdup");
539 goto done;
541 repo->gitdir_fd = open(repo->path_git_dir,
542 O_DIRECTORY | O_CLOEXEC);
543 if (repo->gitdir_fd == -1) {
544 err = got_error_from_errno2("open",
545 repo->path_git_dir);
546 goto done;
548 return NULL;
551 /* git repository with working tree? */
552 free(repo->path_git_dir);
553 repo->path_git_dir = NULL;
554 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
555 err = got_error_from_errno("asprintf");
556 goto done;
558 if (is_git_repo(repo)) {
559 repo->path = strdup(path);
560 if (repo->path == NULL) {
561 err = got_error_from_errno("strdup");
562 goto done;
564 repo->gitdir_fd = open(repo->path_git_dir,
565 O_DIRECTORY | O_CLOEXEC);
566 if (repo->gitdir_fd == -1) {
567 err = got_error_from_errno2("open",
568 repo->path_git_dir);
569 goto done;
571 return NULL;
574 err = got_error(GOT_ERR_NOT_GIT_REPO);
575 done:
576 if (err) {
577 free(repo->path);
578 repo->path = NULL;
579 free(repo->path_git_dir);
580 repo->path_git_dir = NULL;
581 if (repo->gitdir_fd != -1)
582 close(repo->gitdir_fd);
583 repo->gitdir_fd = -1;
586 return err;
589 static const struct got_error *
590 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
592 const struct got_error *err = NULL;
593 char *repo_gitconfig_path = NULL;
595 if (global_gitconfig_path) {
596 /* Read settings from ~/.gitconfig. */
597 int dummy_repo_version;
598 err = got_repo_read_gitconfig(&dummy_repo_version,
599 &repo->global_gitconfig_author_name,
600 &repo->global_gitconfig_author_email,
601 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
602 if (err)
603 return err;
606 /* Read repository's .git/config file. */
607 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
608 if (repo_gitconfig_path == NULL)
609 return got_error_from_errno("got_repo_get_path_gitconfig");
611 err = got_repo_read_gitconfig(
612 &repo->gitconfig_repository_format_version,
613 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
614 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
615 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
616 repo_gitconfig_path);
617 if (err)
618 goto done;
620 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
621 int i;
623 for (i = 0; i < repo->ngitconfig_remotes; i++) {
624 got_repo_free_remote_repo_data(
625 &repo->gitconfig_remotes[i]);
627 free(repo->gitconfig_remotes);
628 repo->gitconfig_remotes = NULL;
629 repo->ngitconfig_remotes = 0;
631 free(repo->gitconfig_author_name);
632 repo->gitconfig_author_name = NULL;
633 free(repo->gitconfig_author_email);
634 repo->gitconfig_author_email = NULL;
636 free(repo->global_gitconfig_author_name);
637 repo->global_gitconfig_author_name = NULL;
638 free(repo->global_gitconfig_author_email);
639 repo->global_gitconfig_author_email = NULL;
642 done:
643 free(repo_gitconfig_path);
644 return err;
647 static const struct got_error *
648 read_gotconfig(struct got_repository *repo)
650 const struct got_error *err = NULL;
651 char *gotconfig_path;
653 gotconfig_path = got_repo_get_path_gotconfig(repo);
654 if (gotconfig_path == NULL)
655 return got_error_from_errno("got_repo_get_path_gotconfig");
657 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
658 free(gotconfig_path);
659 return err;
662 /* Supported repository format extensions. */
663 static const char *const repo_extensions[] = {
664 "noop", /* Got supports repository format version 1. */
665 "preciousObjects", /* Supported by gotadmin cleanup. */
666 "worktreeConfig", /* Got does not care about Git work trees. */
667 };
669 const struct got_error *
670 got_repo_open(struct got_repository **repop, const char *path,
671 const char *global_gitconfig_path, int *pack_fds)
673 struct got_repository *repo = NULL;
674 const struct got_error *err = NULL;
675 char *repo_path = NULL;
676 size_t i, j = 0;
678 *repop = NULL;
680 repo = calloc(1, sizeof(*repo));
681 if (repo == NULL)
682 return got_error_from_errno("calloc");
684 RB_INIT(&repo->packidx_bloom_filters);
685 TAILQ_INIT(&repo->packidx_paths);
687 for (i = 0; i < nitems(repo->privsep_children); i++) {
688 memset(&repo->privsep_children[i], 0,
689 sizeof(repo->privsep_children[0]));
690 repo->privsep_children[i].imsg_fd = -1;
693 err = got_object_cache_init(&repo->objcache,
694 GOT_OBJECT_CACHE_TYPE_OBJ);
695 if (err)
696 goto done;
697 err = got_object_cache_init(&repo->treecache,
698 GOT_OBJECT_CACHE_TYPE_TREE);
699 if (err)
700 goto done;
701 err = got_object_cache_init(&repo->commitcache,
702 GOT_OBJECT_CACHE_TYPE_COMMIT);
703 if (err)
704 goto done;
705 err = got_object_cache_init(&repo->tagcache,
706 GOT_OBJECT_CACHE_TYPE_TAG);
707 if (err)
708 goto done;
709 err = got_object_cache_init(&repo->rawcache,
710 GOT_OBJECT_CACHE_TYPE_RAW);
711 if (err)
712 goto done;
714 err = get_pack_cache_size(&repo->pack_cache_size);
715 if (err)
716 goto done;
717 for (i = 0; i < nitems(repo->packs); i++) {
718 if (pack_fds != NULL && i < repo->pack_cache_size) {
719 repo->packs[i].basefd = pack_fds[j++];
720 repo->packs[i].accumfd = pack_fds[j++];
721 } else {
722 repo->packs[i].basefd = -1;
723 repo->packs[i].accumfd = -1;
726 for (i = 0; i < nitems(repo->tempfiles); i++)
727 repo->tempfiles[i] = -1;
728 repo->pinned_pack = -1;
729 repo->pinned_packidx = -1;
730 repo->pinned_pid = 0;
732 repo_path = realpath(path, NULL);
733 if (repo_path == NULL) {
734 err = got_error_from_errno2("realpath", path);
735 goto done;
738 for (;;) {
739 char *parent_path;
741 err = open_repo(repo, repo_path);
742 if (err == NULL)
743 break;
744 if (err->code != GOT_ERR_NOT_GIT_REPO)
745 goto done;
746 if (repo_path[0] == '/' && repo_path[1] == '\0') {
747 err = got_error(GOT_ERR_NOT_GIT_REPO);
748 goto done;
750 err = got_path_dirname(&parent_path, repo_path);
751 if (err)
752 goto done;
753 free(repo_path);
754 repo_path = parent_path;
757 err = read_gotconfig(repo);
758 if (err)
759 goto done;
761 err = read_gitconfig(repo, global_gitconfig_path);
762 if (err)
763 goto done;
764 if (repo->gitconfig_repository_format_version != 0) {
765 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
766 goto done;
768 for (i = 0; i < repo->nextensions; i++) {
769 char *ext = repo->extensions[i];
770 int j, supported = 0;
771 for (j = 0; j < nitems(repo_extensions); j++) {
772 if (strcmp(ext, repo_extensions[j]) == 0) {
773 supported = 1;
774 break;
777 if (!supported) {
778 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
779 goto done;
783 err = got_repo_list_packidx(&repo->packidx_paths, repo);
784 done:
785 if (err)
786 got_repo_close(repo);
787 else
788 *repop = repo;
789 free(repo_path);
790 return err;
793 const struct got_error *
794 got_repo_close(struct got_repository *repo)
796 const struct got_error *err = NULL, *child_err;
797 struct got_packidx_bloom_filter *bf;
798 size_t i;
800 for (i = 0; i < repo->pack_cache_size; i++) {
801 if (repo->packidx_cache[i] == NULL)
802 break;
803 got_packidx_close(repo->packidx_cache[i]);
806 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
807 &repo->packidx_bloom_filters))) {
808 RB_REMOVE(got_packidx_bloom_filter_tree,
809 &repo->packidx_bloom_filters, bf);
810 bloom_free(bf->bloom);
811 free(bf->bloom);
812 free(bf);
815 for (i = 0; i < repo->pack_cache_size; i++)
816 if (repo->packs[i].path_packfile)
817 if (repo->packs[i].path_packfile)
818 got_pack_close(&repo->packs[i]);
820 free(repo->path);
821 free(repo->path_git_dir);
823 got_object_cache_close(&repo->objcache);
824 got_object_cache_close(&repo->treecache);
825 got_object_cache_close(&repo->commitcache);
826 got_object_cache_close(&repo->tagcache);
827 got_object_cache_close(&repo->rawcache);
829 for (i = 0; i < nitems(repo->privsep_children); i++) {
830 if (repo->privsep_children[i].imsg_fd == -1)
831 continue;
832 imsg_clear(repo->privsep_children[i].ibuf);
833 free(repo->privsep_children[i].ibuf);
834 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
835 child_err = got_privsep_wait_for_child(
836 repo->privsep_children[i].pid);
837 if (child_err && err == NULL)
838 err = child_err;
839 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
840 err == NULL)
841 err = got_error_from_errno("close");
844 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
845 err == NULL)
846 err = got_error_from_errno("close");
848 if (repo->gotconfig)
849 got_gotconfig_free(repo->gotconfig);
850 free(repo->gitconfig_author_name);
851 free(repo->gitconfig_author_email);
852 for (i = 0; i < repo->ngitconfig_remotes; i++)
853 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
854 free(repo->gitconfig_remotes);
855 for (i = 0; i < repo->nextensions; i++)
856 free(repo->extensions[i]);
857 free(repo->extensions);
859 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
860 free(repo);
862 return err;
865 void
866 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
868 int i;
870 free(repo->name);
871 repo->name = NULL;
872 free(repo->fetch_url);
873 repo->fetch_url = NULL;
874 free(repo->send_url);
875 repo->send_url = NULL;
876 for (i = 0; i < repo->nfetch_branches; i++)
877 free(repo->fetch_branches[i]);
878 free(repo->fetch_branches);
879 repo->fetch_branches = NULL;
880 repo->nfetch_branches = 0;
881 for (i = 0; i < repo->nsend_branches; i++)
882 free(repo->send_branches[i]);
883 free(repo->send_branches);
884 repo->send_branches = NULL;
885 repo->nsend_branches = 0;
888 const struct got_error *
889 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
890 const char *input_path)
892 const struct got_error *err = NULL;
893 const char *repo_abspath = NULL;
894 size_t repolen, len;
895 char *canonpath, *path = NULL;
897 *in_repo_path = NULL;
899 canonpath = strdup(input_path);
900 if (canonpath == NULL) {
901 err = got_error_from_errno("strdup");
902 goto done;
904 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
905 if (err)
906 goto done;
908 repo_abspath = got_repo_get_path(repo);
910 if (canonpath[0] == '\0') {
911 path = strdup(canonpath);
912 if (path == NULL) {
913 err = got_error_from_errno("strdup");
914 goto done;
916 } else {
917 path = realpath(canonpath, NULL);
918 if (path == NULL) {
919 if (errno != ENOENT) {
920 err = got_error_from_errno2("realpath",
921 canonpath);
922 goto done;
924 /*
925 * Path is not on disk.
926 * Assume it is already relative to repository root.
927 */
928 path = strdup(canonpath);
929 if (path == NULL) {
930 err = got_error_from_errno("strdup");
931 goto done;
935 repolen = strlen(repo_abspath);
936 len = strlen(path);
939 if (strcmp(path, repo_abspath) == 0) {
940 free(path);
941 path = strdup("");
942 if (path == NULL) {
943 err = got_error_from_errno("strdup");
944 goto done;
946 } else if (len > repolen &&
947 got_path_is_child(path, repo_abspath, repolen)) {
948 /* Matched an on-disk path inside repository. */
949 if (got_repo_is_bare(repo)) {
950 /*
951 * Matched an on-disk path inside repository
952 * database. Treat input as repository-relative.
953 */
954 free(path);
955 path = canonpath;
956 canonpath = NULL;
957 } else {
958 char *child;
959 /* Strip common prefix with repository path. */
960 err = got_path_skip_common_ancestor(&child,
961 repo_abspath, path);
962 if (err)
963 goto done;
964 free(path);
965 path = child;
967 } else {
968 /*
969 * Matched unrelated on-disk path.
970 * Treat input as repository-relative.
971 */
972 free(path);
973 path = canonpath;
974 canonpath = NULL;
978 /* Make in-repository path absolute */
979 if (path[0] != '/') {
980 char *abspath;
981 if (asprintf(&abspath, "/%s", path) == -1) {
982 err = got_error_from_errno("asprintf");
983 goto done;
985 free(path);
986 path = abspath;
989 done:
990 free(canonpath);
991 if (err)
992 free(path);
993 else
994 *in_repo_path = path;
995 return err;
998 static const struct got_error *
999 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1000 const char *path_packidx)
1002 const struct got_error *err = NULL;
1003 size_t i;
1005 for (i = 0; i < repo->pack_cache_size; i++) {
1006 if (repo->packidx_cache[i] == NULL)
1007 break;
1008 if (strcmp(repo->packidx_cache[i]->path_packidx,
1009 path_packidx) == 0) {
1010 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1013 if (i == repo->pack_cache_size) {
1014 do {
1015 i--;
1016 } while (i > 0 && repo->pinned_packidx >= 0 &&
1017 i == repo->pinned_packidx);
1018 err = got_packidx_close(repo->packidx_cache[i]);
1019 if (err)
1020 return err;
1023 repo->packidx_cache[i] = packidx;
1025 return NULL;
1028 int
1029 got_repo_is_packidx_filename(const char *name, size_t len)
1031 if (len != GOT_PACKIDX_NAMELEN)
1032 return 0;
1034 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1035 return 0;
1037 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1038 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1039 return 0;
1041 return 1;
1044 static struct got_packidx_bloom_filter *
1045 get_packidx_bloom_filter(struct got_repository *repo,
1046 const char *path, size_t path_len)
1048 struct got_packidx_bloom_filter key;
1050 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1051 return NULL; /* XXX */
1052 key.path_len = path_len;
1054 return RB_FIND(got_packidx_bloom_filter_tree,
1055 &repo->packidx_bloom_filters, &key);
1058 int
1059 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1060 const char *path_packidx, struct got_object_id *id)
1062 struct got_packidx_bloom_filter *bf;
1064 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1065 if (bf)
1066 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1068 /* No bloom filter means this pack index must be searched. */
1069 return 1;
1072 static const struct got_error *
1073 add_packidx_bloom_filter(struct got_repository *repo,
1074 struct got_packidx *packidx, const char *path_packidx)
1076 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1077 struct got_packidx_bloom_filter *bf;
1078 size_t len;
1081 * Don't use bloom filters for very large pack index files.
1082 * Large pack files will contain a relatively large fraction
1083 * of our objects so we will likely need to visit them anyway.
1084 * The more objects a pack file contains the higher the probability
1085 * of a false-positive match from the bloom filter. And reading
1086 * all object IDs from a large pack index file can be expensive.
1088 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1089 return NULL;
1091 /* Do we already have a filter for this pack index? */
1092 if (get_packidx_bloom_filter(repo, path_packidx,
1093 strlen(path_packidx)) != NULL)
1094 return NULL;
1096 bf = calloc(1, sizeof(*bf));
1097 if (bf == NULL)
1098 return got_error_from_errno("calloc");
1099 bf->bloom = calloc(1, sizeof(*bf->bloom));
1100 if (bf->bloom == NULL) {
1101 free(bf);
1102 return got_error_from_errno("calloc");
1105 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1106 if (len >= sizeof(bf->path)) {
1107 free(bf->bloom);
1108 free(bf);
1109 return got_error(GOT_ERR_NO_SPACE);
1111 bf->path_len = len;
1113 /* Minimum size supported by our bloom filter is 1000 entries. */
1114 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1115 for (i = 0; i < nobjects; i++) {
1116 struct got_packidx_object_id *id;
1117 id = &packidx->hdr.sorted_ids[i];
1118 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1121 RB_INSERT(got_packidx_bloom_filter_tree,
1122 &repo->packidx_bloom_filters, bf);
1123 return NULL;
1126 static void
1127 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1129 struct got_pathlist_entry *pe;
1131 while (!TAILQ_EMPTY(packidx_paths)) {
1132 pe = TAILQ_FIRST(packidx_paths);
1133 TAILQ_REMOVE(packidx_paths, pe, entry);
1134 free((char *)pe->path);
1135 free(pe);
1139 static const struct got_error *
1140 refresh_packidx_paths(struct got_repository *repo)
1142 const struct got_error *err = NULL;
1143 char *objects_pack_dir = NULL;
1144 struct stat sb;
1146 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1147 if (objects_pack_dir == NULL)
1148 return got_error_from_errno("got_repo_get_path_objects_pack");
1150 if (stat(objects_pack_dir, &sb) == -1) {
1151 if (errno != ENOENT) {
1152 err = got_error_from_errno2("stat", objects_pack_dir);
1153 goto done;
1155 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1156 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1157 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1158 purge_packidx_paths(&repo->packidx_paths);
1159 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1160 if (err)
1161 goto done;
1163 done:
1164 free(objects_pack_dir);
1165 return err;
1168 const struct got_error *
1169 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1170 struct got_repository *repo, struct got_object_id *id)
1172 const struct got_error *err;
1173 struct got_pathlist_entry *pe;
1174 size_t i;
1176 /* Search pack index cache. */
1177 for (i = 0; i < repo->pack_cache_size; i++) {
1178 if (repo->packidx_cache[i] == NULL)
1179 break;
1180 if (!got_repo_check_packidx_bloom_filter(repo,
1181 repo->packidx_cache[i]->path_packidx, id))
1182 continue; /* object will not be found in this index */
1183 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1184 if (*idx != -1) {
1185 *packidx = repo->packidx_cache[i];
1187 * Move this cache entry to the front. Repeatedly
1188 * searching a wrong pack index can be expensive.
1190 if (i > 0) {
1191 memmove(&repo->packidx_cache[1],
1192 &repo->packidx_cache[0],
1193 i * sizeof(repo->packidx_cache[0]));
1194 repo->packidx_cache[0] = *packidx;
1195 if (repo->pinned_packidx >= 0 &&
1196 repo->pinned_packidx < i)
1197 repo->pinned_packidx++;
1198 else if (repo->pinned_packidx == i)
1199 repo->pinned_packidx = 0;
1201 return NULL;
1204 /* No luck. Search the filesystem. */
1206 err = refresh_packidx_paths(repo);
1207 if (err)
1208 return err;
1210 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1211 const char *path_packidx = pe->path;
1212 int is_cached = 0;
1214 if (!got_repo_check_packidx_bloom_filter(repo,
1215 pe->path, id))
1216 continue; /* object will not be found in this index */
1218 for (i = 0; i < repo->pack_cache_size; i++) {
1219 if (repo->packidx_cache[i] == NULL)
1220 break;
1221 if (strcmp(repo->packidx_cache[i]->path_packidx,
1222 path_packidx) == 0) {
1223 is_cached = 1;
1224 break;
1227 if (is_cached)
1228 continue; /* already searched */
1230 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1231 path_packidx, 0);
1232 if (err)
1233 goto done;
1235 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1236 if (err)
1237 goto done;
1239 err = cache_packidx(repo, *packidx, path_packidx);
1240 if (err)
1241 goto done;
1243 *idx = got_packidx_get_object_idx(*packidx, id);
1244 if (*idx != -1) {
1245 err = NULL; /* found the object */
1246 goto done;
1250 err = got_error_no_obj(id);
1251 done:
1252 return err;
1255 const struct got_error *
1256 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1257 struct got_repository *repo)
1259 const struct got_error *err = NULL;
1260 DIR *packdir = NULL;
1261 struct dirent *dent;
1262 char *path_packidx = NULL;
1263 int packdir_fd;
1264 struct stat sb;
1266 packdir_fd = openat(got_repo_get_fd(repo),
1267 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1268 if (packdir_fd == -1) {
1269 return got_error_from_errno_fmt("openat: %s/%s",
1270 got_repo_get_path_git_dir(repo),
1271 GOT_OBJECTS_PACK_DIR);
1274 packdir = fdopendir(packdir_fd);
1275 if (packdir == NULL) {
1276 err = got_error_from_errno("fdopendir");
1277 goto done;
1280 if (fstat(packdir_fd, &sb) == -1) {
1281 err = got_error_from_errno("fstat");
1282 goto done;
1284 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1285 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1287 while ((dent = readdir(packdir)) != NULL) {
1288 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1289 continue;
1291 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1292 dent->d_name) == -1) {
1293 err = got_error_from_errno("asprintf");
1294 path_packidx = NULL;
1295 break;
1298 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1299 if (err)
1300 break;
1302 done:
1303 if (err)
1304 free(path_packidx);
1305 if (packdir && closedir(packdir) != 0 && err == NULL)
1306 err = got_error_from_errno("closedir");
1307 return err;
1310 const struct got_error *
1311 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1312 struct got_repository *repo)
1314 const struct got_error *err;
1315 size_t i;
1317 *packidx = NULL;
1319 /* Search pack index cache. */
1320 for (i = 0; i < repo->pack_cache_size; i++) {
1321 if (repo->packidx_cache[i] == NULL)
1322 break;
1323 if (strcmp(repo->packidx_cache[i]->path_packidx,
1324 path_packidx) == 0) {
1325 *packidx = repo->packidx_cache[i];
1326 return NULL;
1329 /* No luck. Search the filesystem. */
1331 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1332 path_packidx, 0);
1333 if (err)
1334 return err;
1336 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1337 if (err)
1338 goto done;
1340 err = cache_packidx(repo, *packidx, path_packidx);
1341 done:
1342 if (err) {
1343 got_packidx_close(*packidx);
1344 *packidx = NULL;
1346 return err;
1349 static const struct got_error *
1350 read_packfile_hdr(int fd, struct got_packidx *packidx)
1352 const struct got_error *err = NULL;
1353 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1354 struct got_packfile_hdr hdr;
1355 ssize_t n;
1357 n = read(fd, &hdr, sizeof(hdr));
1358 if (n < 0)
1359 return got_error_from_errno("read");
1360 if (n != sizeof(hdr))
1361 return got_error(GOT_ERR_BAD_PACKFILE);
1363 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1364 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1365 be32toh(hdr.nobjects) != totobj)
1366 err = got_error(GOT_ERR_BAD_PACKFILE);
1368 return err;
1371 static const struct got_error *
1372 open_packfile(int *fd, struct got_repository *repo,
1373 const char *relpath, struct got_packidx *packidx)
1375 const struct got_error *err = NULL;
1377 *fd = openat(got_repo_get_fd(repo), relpath,
1378 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1379 if (*fd == -1)
1380 return got_error_from_errno_fmt("openat: %s/%s",
1381 got_repo_get_path_git_dir(repo), relpath);
1383 if (packidx) {
1384 err = read_packfile_hdr(*fd, packidx);
1385 if (err) {
1386 close(*fd);
1387 *fd = -1;
1391 return err;
1394 const struct got_error *
1395 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1396 const char *path_packfile, struct got_packidx *packidx)
1398 const struct got_error *err = NULL;
1399 struct got_pack *pack = NULL;
1400 struct stat sb;
1401 size_t i;
1403 if (packp)
1404 *packp = NULL;
1406 for (i = 0; i < repo->pack_cache_size; i++) {
1407 pack = &repo->packs[i];
1408 if (pack->path_packfile == NULL)
1409 break;
1410 if (strcmp(pack->path_packfile, path_packfile) == 0)
1411 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1414 if (i == repo->pack_cache_size) {
1415 struct got_pack tmp;
1416 do {
1417 i--;
1418 } while (i > 0 && repo->pinned_pack >= 0 &&
1419 i == repo->pinned_pack);
1420 err = got_pack_close(&repo->packs[i]);
1421 if (err)
1422 return err;
1423 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1424 return got_error_from_errno("ftruncate");
1425 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1426 return got_error_from_errno("ftruncate");
1427 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1428 memcpy(&repo->packs[i], &repo->packs[0],
1429 sizeof(repo->packs[i]));
1430 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1431 if (repo->pinned_pack == 0)
1432 repo->pinned_pack = i;
1433 else if (repo->pinned_pack == i)
1434 repo->pinned_pack = 0;
1435 i = 0;
1438 pack = &repo->packs[i];
1440 pack->path_packfile = strdup(path_packfile);
1441 if (pack->path_packfile == NULL) {
1442 err = got_error_from_errno("strdup");
1443 goto done;
1446 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1447 if (err)
1448 goto done;
1450 if (fstat(pack->fd, &sb) != 0) {
1451 err = got_error_from_errno("fstat");
1452 goto done;
1454 pack->filesize = sb.st_size;
1456 pack->privsep_child = NULL;
1458 err = got_delta_cache_alloc(&pack->delta_cache);
1459 if (err)
1460 goto done;
1462 #ifndef GOT_PACK_NO_MMAP
1463 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1464 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1465 pack->fd, 0);
1466 if (pack->map == MAP_FAILED) {
1467 if (errno != ENOMEM) {
1468 err = got_error_from_errno("mmap");
1469 goto done;
1471 pack->map = NULL; /* fall back to read(2) */
1474 #endif
1475 done:
1476 if (err) {
1477 if (pack)
1478 got_pack_close(pack);
1479 } else if (packp)
1480 *packp = pack;
1481 return err;
1484 struct got_pack *
1485 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1487 struct got_pack *pack = NULL;
1488 size_t i;
1490 for (i = 0; i < repo->pack_cache_size; i++) {
1491 pack = &repo->packs[i];
1492 if (pack->path_packfile == NULL)
1493 break;
1494 if (strcmp(pack->path_packfile, path_packfile) == 0)
1495 return pack;
1498 return NULL;
1501 const struct got_error *
1502 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1503 struct got_pack *pack)
1505 size_t i;
1506 int pinned_pack = -1, pinned_packidx = -1;
1508 for (i = 0; i < repo->pack_cache_size; i++) {
1509 if (repo->packidx_cache[i] &&
1510 strcmp(repo->packidx_cache[i]->path_packidx,
1511 packidx->path_packidx) == 0)
1512 pinned_packidx = i;
1513 if (repo->packs[i].path_packfile &&
1514 strcmp(repo->packs[i].path_packfile,
1515 pack->path_packfile) == 0)
1516 pinned_pack = i;
1519 if (pinned_packidx == -1 || pinned_pack == -1)
1520 return got_error(GOT_ERR_PIN_PACK);
1522 repo->pinned_pack = pinned_pack;
1523 repo->pinned_packidx = pinned_packidx;
1524 if (repo->packs[pinned_pack].privsep_child)
1525 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1526 return NULL;
1529 struct got_pack *
1530 got_repo_get_pinned_pack(struct got_repository *repo)
1532 if (repo->pinned_pack >= 0 &&
1533 repo->pinned_pack < repo->pack_cache_size)
1534 return &repo->packs[repo->pinned_pack];
1536 return NULL;
1539 void
1540 got_repo_unpin_pack(struct got_repository *repo)
1542 repo->pinned_packidx = -1;
1543 repo->pinned_pack = -1;
1544 repo->pinned_pid = 0;
1547 const struct got_error *
1548 got_repo_init(const char *repo_path, const char *head_name)
1550 const struct got_error *err = NULL;
1551 const char *dirnames[] = {
1552 GOT_OBJECTS_DIR,
1553 GOT_OBJECTS_PACK_DIR,
1554 GOT_REFS_DIR,
1556 const char *description_str = "Unnamed repository; "
1557 "edit this file 'description' to name the repository.";
1558 const char *headref = "ref: refs/heads/";
1559 const char *gitconfig_str = "[core]\n"
1560 "\trepositoryformatversion = 0\n"
1561 "\tfilemode = true\n"
1562 "\tbare = true\n";
1563 char *headref_str, *path;
1564 size_t i;
1566 if (!got_path_dir_is_empty(repo_path))
1567 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1569 for (i = 0; i < nitems(dirnames); i++) {
1570 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1571 return got_error_from_errno("asprintf");
1573 err = got_path_mkdir(path);
1574 free(path);
1575 if (err)
1576 return err;
1579 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1580 return got_error_from_errno("asprintf");
1581 err = got_path_create_file(path, description_str);
1582 free(path);
1583 if (err)
1584 return err;
1586 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1587 return got_error_from_errno("asprintf");
1588 if (asprintf(&headref_str, "%s%s", headref,
1589 head_name ? head_name : "main") == -1) {
1590 free(path);
1591 return got_error_from_errno("asprintf");
1593 err = got_path_create_file(path, headref_str);
1594 free(headref_str);
1595 free(path);
1596 if (err)
1597 return err;
1599 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1600 return got_error_from_errno("asprintf");
1601 err = got_path_create_file(path, gitconfig_str);
1602 free(path);
1603 if (err)
1604 return err;
1606 return NULL;
1609 static const struct got_error *
1610 match_packed_object(struct got_object_id **unique_id,
1611 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1613 const struct got_error *err = NULL;
1614 struct got_object_id_queue matched_ids;
1615 struct got_pathlist_entry *pe;
1617 STAILQ_INIT(&matched_ids);
1619 err = refresh_packidx_paths(repo);
1620 if (err)
1621 return err;
1623 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1624 const char *path_packidx = pe->path;
1625 struct got_packidx *packidx;
1626 struct got_object_qid *qid;
1628 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1629 path_packidx, 0);
1630 if (err)
1631 break;
1633 err = got_packidx_match_id_str_prefix(&matched_ids,
1634 packidx, id_str_prefix);
1635 if (err) {
1636 got_packidx_close(packidx);
1637 break;
1639 err = got_packidx_close(packidx);
1640 if (err)
1641 break;
1643 STAILQ_FOREACH(qid, &matched_ids, entry) {
1644 if (obj_type != GOT_OBJ_TYPE_ANY) {
1645 int matched_type;
1646 err = got_object_get_type(&matched_type, repo,
1647 &qid->id);
1648 if (err)
1649 goto done;
1650 if (matched_type != obj_type)
1651 continue;
1653 if (*unique_id == NULL) {
1654 *unique_id = got_object_id_dup(&qid->id);
1655 if (*unique_id == NULL) {
1656 err = got_error_from_errno("malloc");
1657 goto done;
1659 } else {
1660 if (got_object_id_cmp(*unique_id,
1661 &qid->id) == 0)
1662 continue; /* packed multiple times */
1663 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1664 goto done;
1668 done:
1669 got_object_id_queue_free(&matched_ids);
1670 if (err) {
1671 free(*unique_id);
1672 *unique_id = NULL;
1674 return err;
1677 static const struct got_error *
1678 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1679 const char *object_dir, const char *id_str_prefix, int obj_type,
1680 struct got_repository *repo)
1682 const struct got_error *err = NULL;
1683 char *path, *id_str = NULL;
1684 DIR *dir = NULL;
1685 struct dirent *dent;
1686 struct got_object_id id;
1688 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1689 err = got_error_from_errno("asprintf");
1690 goto done;
1693 dir = opendir(path);
1694 if (dir == NULL) {
1695 if (errno == ENOENT) {
1696 err = NULL;
1697 goto done;
1699 err = got_error_from_errno2("opendir", path);
1700 goto done;
1702 while ((dent = readdir(dir)) != NULL) {
1703 int cmp;
1705 free(id_str);
1706 id_str = NULL;
1708 if (strcmp(dent->d_name, ".") == 0 ||
1709 strcmp(dent->d_name, "..") == 0)
1710 continue;
1712 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1713 err = got_error_from_errno("asprintf");
1714 goto done;
1717 if (!got_parse_sha1_digest(id.sha1, id_str))
1718 continue;
1721 * Directory entries do not necessarily appear in
1722 * sorted order, so we must iterate over all of them.
1724 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1725 if (cmp != 0)
1726 continue;
1728 if (*unique_id == NULL) {
1729 if (obj_type != GOT_OBJ_TYPE_ANY) {
1730 int matched_type;
1731 err = got_object_get_type(&matched_type, repo,
1732 &id);
1733 if (err)
1734 goto done;
1735 if (matched_type != obj_type)
1736 continue;
1738 *unique_id = got_object_id_dup(&id);
1739 if (*unique_id == NULL) {
1740 err = got_error_from_errno("got_object_id_dup");
1741 goto done;
1743 } else {
1744 if (got_object_id_cmp(*unique_id, &id) == 0)
1745 continue; /* both packed and loose */
1746 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1747 goto done;
1750 done:
1751 if (dir && closedir(dir) != 0 && err == NULL)
1752 err = got_error_from_errno("closedir");
1753 if (err) {
1754 free(*unique_id);
1755 *unique_id = NULL;
1757 free(id_str);
1758 free(path);
1759 return err;
1762 const struct got_error *
1763 got_repo_match_object_id_prefix(struct got_object_id **id,
1764 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1766 const struct got_error *err = NULL;
1767 char *path_objects = NULL, *object_dir = NULL;
1768 size_t len;
1769 int i;
1771 *id = NULL;
1773 path_objects = got_repo_get_path_objects(repo);
1775 len = strlen(id_str_prefix);
1776 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1777 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1778 goto done;
1781 for (i = 0; i < len; i++) {
1782 if (isxdigit((unsigned char)id_str_prefix[i]))
1783 continue;
1784 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1785 goto done;
1788 if (len >= 2) {
1789 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1790 if (err)
1791 goto done;
1792 object_dir = strndup(id_str_prefix, 2);
1793 if (object_dir == NULL) {
1794 err = got_error_from_errno("strdup");
1795 goto done;
1797 err = match_loose_object(id, path_objects, object_dir,
1798 id_str_prefix, obj_type, repo);
1799 } else if (len == 1) {
1800 int i;
1801 for (i = 0; i < 0xf; i++) {
1802 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1803 == -1) {
1804 err = got_error_from_errno("asprintf");
1805 goto done;
1807 err = match_packed_object(id, repo, object_dir,
1808 obj_type);
1809 if (err)
1810 goto done;
1811 err = match_loose_object(id, path_objects, object_dir,
1812 id_str_prefix, obj_type, repo);
1813 if (err)
1814 goto done;
1816 } else {
1817 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1818 goto done;
1820 done:
1821 free(path_objects);
1822 free(object_dir);
1823 if (err) {
1824 free(*id);
1825 *id = NULL;
1826 } else if (*id == NULL) {
1827 switch (obj_type) {
1828 case GOT_OBJ_TYPE_BLOB:
1829 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1830 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1831 break;
1832 case GOT_OBJ_TYPE_TREE:
1833 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1834 GOT_OBJ_LABEL_TREE, id_str_prefix);
1835 break;
1836 case GOT_OBJ_TYPE_COMMIT:
1837 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1838 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1839 break;
1840 case GOT_OBJ_TYPE_TAG:
1841 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1842 GOT_OBJ_LABEL_TAG, id_str_prefix);
1843 break;
1844 default:
1845 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1846 break;
1850 return err;
1853 const struct got_error *
1854 got_repo_match_object_id(struct got_object_id **id, char **label,
1855 const char *id_str, int obj_type, struct got_reflist_head *refs,
1856 struct got_repository *repo)
1858 const struct got_error *err;
1859 struct got_tag_object *tag;
1860 struct got_reference *ref = NULL;
1862 *id = NULL;
1863 if (label)
1864 *label = NULL;
1866 if (refs) {
1867 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1868 refs, repo);
1869 if (err == NULL) {
1870 *id = got_object_id_dup(
1871 got_object_tag_get_object_id(tag));
1872 if (*id == NULL)
1873 err = got_error_from_errno("got_object_id_dup");
1874 else if (label && asprintf(label, "refs/tags/%s",
1875 got_object_tag_get_name(tag)) == -1) {
1876 err = got_error_from_errno("asprintf");
1877 free(*id);
1878 *id = NULL;
1880 got_object_tag_close(tag);
1881 return err;
1882 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1883 err->code != GOT_ERR_NO_OBJ)
1884 return err;
1887 err = got_ref_open(&ref, repo, id_str, 0);
1888 if (err == NULL) {
1889 err = got_ref_resolve(id, repo, ref);
1890 if (err)
1891 goto done;
1892 if (label) {
1893 *label = strdup(got_ref_get_name(ref));
1894 if (*label == NULL) {
1895 err = got_error_from_errno("strdup");
1896 goto done;
1899 } else {
1900 if (err->code != GOT_ERR_NOT_REF &&
1901 err->code != GOT_ERR_BAD_REF_NAME)
1902 goto done;
1903 err = got_repo_match_object_id_prefix(id, id_str,
1904 obj_type, repo);
1905 if (err) {
1906 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1907 err = got_error_not_ref(id_str);
1908 goto done;
1910 if (label) {
1911 err = got_object_id_str(label, *id);
1912 if (*label == NULL) {
1913 err = got_error_from_errno("strdup");
1914 goto done;
1918 done:
1919 if (ref)
1920 got_ref_close(ref);
1921 return err;
1924 const struct got_error *
1925 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1926 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1928 const struct got_error *err = NULL;
1929 struct got_reflist_entry *re;
1930 struct got_object_id *tag_id;
1931 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1933 *tag = NULL;
1935 TAILQ_FOREACH(re, refs, entry) {
1936 const char *refname;
1937 refname = got_ref_get_name(re->ref);
1938 if (got_ref_is_symbolic(re->ref))
1939 continue;
1940 if (strncmp(refname, "refs/tags/", 10) != 0)
1941 continue;
1942 if (!name_is_absolute)
1943 refname += strlen("refs/tags/");
1944 if (strcmp(refname, name) != 0)
1945 continue;
1946 err = got_ref_resolve(&tag_id, repo, re->ref);
1947 if (err)
1948 break;
1949 err = got_object_open_as_tag(tag, repo, tag_id);
1950 free(tag_id);
1951 if (err)
1952 break;
1953 if (obj_type == GOT_OBJ_TYPE_ANY ||
1954 got_object_tag_get_object_type(*tag) == obj_type)
1955 break;
1956 got_object_tag_close(*tag);
1957 *tag = NULL;
1960 if (err == NULL && *tag == NULL)
1961 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1962 GOT_OBJ_LABEL_TAG, name);
1963 return err;
1966 static const struct got_error *
1967 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1968 const char *name, mode_t mode, struct got_object_id *blob_id)
1970 const struct got_error *err = NULL;
1972 *new_te = NULL;
1974 *new_te = calloc(1, sizeof(**new_te));
1975 if (*new_te == NULL)
1976 return got_error_from_errno("calloc");
1978 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1979 sizeof((*new_te)->name)) {
1980 err = got_error(GOT_ERR_NO_SPACE);
1981 goto done;
1984 if (S_ISLNK(mode)) {
1985 (*new_te)->mode = S_IFLNK;
1986 } else {
1987 (*new_te)->mode = S_IFREG;
1988 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1990 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1991 done:
1992 if (err && *new_te) {
1993 free(*new_te);
1994 *new_te = NULL;
1996 return err;
1999 static const struct got_error *
2000 import_file(struct got_tree_entry **new_te, struct dirent *de,
2001 const char *path, struct got_repository *repo)
2003 const struct got_error *err;
2004 struct got_object_id *blob_id = NULL;
2005 char *filepath;
2006 struct stat sb;
2008 if (asprintf(&filepath, "%s%s%s", path,
2009 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2010 return got_error_from_errno("asprintf");
2012 if (lstat(filepath, &sb) != 0) {
2013 err = got_error_from_errno2("lstat", path);
2014 goto done;
2017 err = got_object_blob_create(&blob_id, filepath, repo);
2018 if (err)
2019 goto done;
2021 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2022 blob_id);
2023 done:
2024 free(filepath);
2025 if (err)
2026 free(blob_id);
2027 return err;
2030 static const struct got_error *
2031 insert_tree_entry(struct got_tree_entry *new_te,
2032 struct got_pathlist_head *paths)
2034 const struct got_error *err = NULL;
2035 struct got_pathlist_entry *new_pe;
2037 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2038 if (err)
2039 return err;
2040 if (new_pe == NULL)
2041 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2042 return NULL;
2045 static const struct got_error *write_tree(struct got_object_id **,
2046 const char *, struct got_pathlist_head *, struct got_repository *,
2047 got_repo_import_cb progress_cb, void *progress_arg);
2049 static const struct got_error *
2050 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2051 const char *path, struct got_pathlist_head *ignores,
2052 struct got_repository *repo,
2053 got_repo_import_cb progress_cb, void *progress_arg)
2055 const struct got_error *err;
2056 struct got_object_id *id = NULL;
2057 char *subdirpath;
2059 if (asprintf(&subdirpath, "%s%s%s", path,
2060 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2061 return got_error_from_errno("asprintf");
2063 (*new_te) = calloc(1, sizeof(**new_te));
2064 if (*new_te == NULL)
2065 return got_error_from_errno("calloc");
2066 (*new_te)->mode = S_IFDIR;
2067 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2068 sizeof((*new_te)->name)) {
2069 err = got_error(GOT_ERR_NO_SPACE);
2070 goto done;
2072 err = write_tree(&id, subdirpath, ignores, repo,
2073 progress_cb, progress_arg);
2074 if (err)
2075 goto done;
2076 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2078 done:
2079 free(id);
2080 free(subdirpath);
2081 if (err) {
2082 free(*new_te);
2083 *new_te = NULL;
2085 return err;
2088 static const struct got_error *
2089 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2090 struct got_pathlist_head *ignores, struct got_repository *repo,
2091 got_repo_import_cb progress_cb, void *progress_arg)
2093 const struct got_error *err = NULL;
2094 DIR *dir;
2095 struct dirent *de;
2096 int nentries;
2097 struct got_tree_entry *new_te = NULL;
2098 struct got_pathlist_head paths;
2099 struct got_pathlist_entry *pe;
2101 *new_tree_id = NULL;
2103 TAILQ_INIT(&paths);
2105 dir = opendir(path_dir);
2106 if (dir == NULL) {
2107 err = got_error_from_errno2("opendir", path_dir);
2108 goto done;
2111 nentries = 0;
2112 while ((de = readdir(dir)) != NULL) {
2113 int ignore = 0;
2114 int type;
2116 if (strcmp(de->d_name, ".") == 0 ||
2117 strcmp(de->d_name, "..") == 0)
2118 continue;
2120 TAILQ_FOREACH(pe, ignores, entry) {
2121 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2122 ignore = 1;
2123 break;
2126 if (ignore)
2127 continue;
2129 err = got_path_dirent_type(&type, path_dir, de);
2130 if (err)
2131 goto done;
2133 if (type == DT_DIR) {
2134 err = import_subdir(&new_te, de, path_dir,
2135 ignores, repo, progress_cb, progress_arg);
2136 if (err) {
2137 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2138 goto done;
2139 err = NULL;
2140 continue;
2142 } else if (type == DT_REG || type == DT_LNK) {
2143 err = import_file(&new_te, de, path_dir, repo);
2144 if (err)
2145 goto done;
2146 } else
2147 continue;
2149 err = insert_tree_entry(new_te, &paths);
2150 if (err)
2151 goto done;
2152 nentries++;
2155 if (TAILQ_EMPTY(&paths)) {
2156 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2157 "cannot create tree without any entries");
2158 goto done;
2161 TAILQ_FOREACH(pe, &paths, entry) {
2162 struct got_tree_entry *te = pe->data;
2163 char *path;
2164 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2165 continue;
2166 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2167 err = got_error_from_errno("asprintf");
2168 goto done;
2170 err = (*progress_cb)(progress_arg, path);
2171 free(path);
2172 if (err)
2173 goto done;
2176 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2177 done:
2178 if (dir)
2179 closedir(dir);
2180 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2181 return err;
2184 const struct got_error *
2185 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2186 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2187 struct got_repository *repo, got_repo_import_cb progress_cb,
2188 void *progress_arg)
2190 const struct got_error *err;
2191 struct got_object_id *new_tree_id;
2193 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2194 progress_cb, progress_arg);
2195 if (err)
2196 return err;
2198 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2199 author, time(NULL), author, time(NULL), logmsg, repo);
2200 free(new_tree_id);
2201 return err;
2204 const struct got_error *
2205 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2206 struct got_repository *repo)
2208 const struct got_error *err = NULL;
2209 char *path_objects = NULL, *path = NULL;
2210 DIR *dir = NULL;
2211 struct got_object_id id;
2212 int i;
2214 *nobjects = 0;
2215 *ondisk_size = 0;
2217 path_objects = got_repo_get_path_objects(repo);
2218 if (path_objects == NULL)
2219 return got_error_from_errno("got_repo_get_path_objects");
2221 for (i = 0; i <= 0xff; i++) {
2222 struct dirent *dent;
2224 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2225 err = got_error_from_errno("asprintf");
2226 break;
2229 dir = opendir(path);
2230 if (dir == NULL) {
2231 if (errno == ENOENT) {
2232 err = NULL;
2233 continue;
2235 err = got_error_from_errno2("opendir", path);
2236 break;
2239 while ((dent = readdir(dir)) != NULL) {
2240 char *id_str;
2241 int fd;
2242 struct stat sb;
2244 if (strcmp(dent->d_name, ".") == 0 ||
2245 strcmp(dent->d_name, "..") == 0)
2246 continue;
2248 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2249 err = got_error_from_errno("asprintf");
2250 goto done;
2253 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2254 free(id_str);
2255 continue;
2257 free(id_str);
2259 err = got_object_open_loose_fd(&fd, &id, repo);
2260 if (err)
2261 goto done;
2263 if (fstat(fd, &sb) == -1) {
2264 err = got_error_from_errno("fstat");
2265 close(fd);
2266 goto done;
2268 (*nobjects)++;
2269 (*ondisk_size) += sb.st_size;
2271 if (close(fd) == -1) {
2272 err = got_error_from_errno("close");
2273 goto done;
2277 if (closedir(dir) != 0) {
2278 err = got_error_from_errno("closedir");
2279 goto done;
2281 dir = NULL;
2283 free(path);
2284 path = NULL;
2286 done:
2287 if (dir && closedir(dir) != 0 && err == NULL)
2288 err = got_error_from_errno("closedir");
2290 if (err) {
2291 *nobjects = 0;
2292 *ondisk_size = 0;
2294 free(path_objects);
2295 free(path);
2296 return err;
2299 const struct got_error *
2300 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2301 off_t *total_packsize, struct got_repository *repo)
2303 const struct got_error *err = NULL;
2304 DIR *packdir = NULL;
2305 struct dirent *dent;
2306 struct got_packidx *packidx = NULL;
2307 char *path_packidx;
2308 char *path_packfile;
2309 int packdir_fd;
2310 struct stat sb;
2312 *npackfiles = 0;
2313 *nobjects = 0;
2314 *total_packsize = 0;
2316 packdir_fd = openat(got_repo_get_fd(repo),
2317 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2318 if (packdir_fd == -1) {
2319 return got_error_from_errno_fmt("openat: %s/%s",
2320 got_repo_get_path_git_dir(repo),
2321 GOT_OBJECTS_PACK_DIR);
2324 packdir = fdopendir(packdir_fd);
2325 if (packdir == NULL) {
2326 err = got_error_from_errno("fdopendir");
2327 goto done;
2330 while ((dent = readdir(packdir)) != NULL) {
2331 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2332 continue;
2334 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2335 dent->d_name) == -1) {
2336 err = got_error_from_errno("asprintf");
2337 goto done;
2340 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2341 path_packidx, 0);
2342 free(path_packidx);
2343 if (err)
2344 goto done;
2346 if (fstat(packidx->fd, &sb) == -1)
2347 goto done;
2348 *total_packsize += sb.st_size;
2350 err = got_packidx_get_packfile_path(&path_packfile,
2351 packidx->path_packidx);
2352 if (err)
2353 goto done;
2355 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2356 0) == -1) {
2357 free(path_packfile);
2358 goto done;
2360 free(path_packfile);
2361 *total_packsize += sb.st_size;
2363 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2365 (*npackfiles)++;
2367 got_packidx_close(packidx);
2368 packidx = NULL;
2370 done:
2371 if (packidx)
2372 got_packidx_close(packidx);
2373 if (packdir && closedir(packdir) != 0 && err == NULL)
2374 err = got_error_from_errno("closedir");
2375 if (err) {
2376 *npackfiles = 0;
2377 *nobjects = 0;
2378 *total_packsize = 0;
2380 return err;
2383 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2384 got_packidx_bloom_filter_cmp);