Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_delta_cache.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
63 #ifndef nitems
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 #endif
67 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
69 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
70 got_packidx_bloom_filter_cmp);
72 const char *
73 got_repo_get_path(struct got_repository *repo)
74 {
75 return repo->path;
76 }
78 const char *
79 got_repo_get_path_git_dir(struct got_repository *repo)
80 {
81 return repo->path_git_dir;
82 }
84 int
85 got_repo_get_fd(struct got_repository *repo)
86 {
87 return repo->gitdir_fd;
88 }
90 const char *
91 got_repo_get_gitconfig_author_name(struct got_repository *repo)
92 {
93 return repo->gitconfig_author_name;
94 }
96 const char *
97 got_repo_get_gitconfig_author_email(struct got_repository *repo)
98 {
99 return repo->gitconfig_author_email;
102 const char *
103 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
105 return repo->global_gitconfig_author_name;
108 const char *
109 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
111 return repo->global_gitconfig_author_email;
114 const char *
115 got_repo_get_gitconfig_owner(struct got_repository *repo)
117 return repo->gitconfig_owner;
120 void
121 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
122 struct got_repository *repo)
124 *extensions = repo->extensions;
125 *nextensions = repo->nextensions;
128 int
129 got_repo_is_bare(struct got_repository *repo)
131 return (strcmp(repo->path, repo->path_git_dir) == 0);
134 static char *
135 get_path_git_child(struct got_repository *repo, const char *basename)
137 char *path_child;
139 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
140 basename) == -1)
141 return NULL;
143 return path_child;
146 char *
147 got_repo_get_path_objects(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_OBJECTS_DIR);
152 char *
153 got_repo_get_path_objects_pack(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
158 char *
159 got_repo_get_path_refs(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_REFS_DIR);
164 char *
165 got_repo_get_path_packed_refs(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
170 static char *
171 get_path_head(struct got_repository *repo)
173 return get_path_git_child(repo, GOT_HEAD_FILE);
176 char *
177 got_repo_get_path_gitconfig(struct got_repository *repo)
179 return get_path_git_child(repo, GOT_GITCONFIG);
182 char *
183 got_repo_get_path_gotconfig(struct got_repository *repo)
185 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
188 const struct got_gotconfig *
189 got_repo_get_gotconfig(struct got_repository *repo)
191 return repo->gotconfig;
194 void
195 got_repo_get_gitconfig_remotes(int *nremotes,
196 const struct got_remote_repo **remotes, struct got_repository *repo)
198 *nremotes = repo->ngitconfig_remotes;
199 *remotes = repo->gitconfig_remotes;
202 static int
203 is_git_repo(struct got_repository *repo)
205 const char *path_git = got_repo_get_path_git_dir(repo);
206 char *path_objects = got_repo_get_path_objects(repo);
207 char *path_refs = got_repo_get_path_refs(repo);
208 char *path_head = get_path_head(repo);
209 int ret = 0;
210 struct stat sb;
211 struct got_reference *head_ref;
213 if (lstat(path_git, &sb) == -1)
214 goto done;
215 if (!S_ISDIR(sb.st_mode))
216 goto done;
218 if (lstat(path_objects, &sb) == -1)
219 goto done;
220 if (!S_ISDIR(sb.st_mode))
221 goto done;
223 if (lstat(path_refs, &sb) == -1)
224 goto done;
225 if (!S_ISDIR(sb.st_mode))
226 goto done;
228 if (lstat(path_head, &sb) == -1)
229 goto done;
230 if (!S_ISREG(sb.st_mode))
231 goto done;
233 /* Check if the HEAD reference can be opened. */
234 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
235 goto done;
236 got_ref_close(head_ref);
238 ret = 1;
239 done:
240 free(path_objects);
241 free(path_refs);
242 free(path_head);
243 return ret;
247 static const struct got_error *
248 close_tempfiles(int *fds, size_t nfds)
250 const struct got_error *err = NULL;
251 int i;
253 for (i = 0; i < nfds; i++) {
254 if (fds[i] == -1)
255 continue;
256 if (close(fds[i]) == -1) {
257 err = got_error_from_errno("close");
258 break;
261 free(fds);
262 return err;
265 static const struct got_error *
266 open_tempfiles(int **fds, size_t nfds)
268 const struct got_error *err = NULL;
269 int i;
271 *fds = calloc(nfds, sizeof(**fds));
272 if (*fds == NULL)
273 return got_error_from_errno("calloc");
275 for (i = 0; i < nfds; i++)
276 (*fds)[i] = -1;
278 for (i = 0; i < nfds; i++) {
279 (*fds)[i] = got_opentempfd();
280 if ((*fds)[i] == -1) {
281 err = got_error_from_errno("got_opentempfd");
282 close_tempfiles(*fds, nfds);
283 *fds = NULL;
284 return err;
288 return NULL;
291 const struct got_error *
292 got_repo_pack_fds_open(int **pack_fds)
294 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
297 const struct got_error *
298 got_repo_pack_fds_close(int *pack_fds)
300 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
303 const struct got_error *
304 got_repo_temp_fds_open(int **temp_fds)
306 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
309 void
310 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
312 int i;
314 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
315 repo->tempfiles[i] = temp_fds[i];
318 const struct got_error *
319 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
321 int i;
323 *fd = -1;
324 *idx = -1;
326 for (i = 0; i < nitems(repo->tempfiles); i++) {
327 if (repo->tempfile_use_mask & (1 << i))
328 continue;
329 if (repo->tempfiles[i] != -1) {
330 if (ftruncate(repo->tempfiles[i], 0L) == -1)
331 return got_error_from_errno("ftruncate");
332 *fd = repo->tempfiles[i];
333 *idx = i;
334 repo->tempfile_use_mask |= (1 << i);
335 return NULL;
339 return got_error(GOT_ERR_REPO_TEMPFILE);
342 void
343 got_repo_temp_fds_put(int idx, struct got_repository *repo)
345 repo->tempfile_use_mask &= ~(1 << idx);
348 const struct got_error *
349 got_repo_temp_fds_close(int *temp_fds)
351 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
354 const struct got_error *
355 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
356 struct got_object *obj)
358 #ifndef GOT_NO_OBJ_CACHE
359 const struct got_error *err = NULL;
360 err = got_object_cache_add(&repo->objcache, id, obj);
361 if (err) {
362 if (err->code == GOT_ERR_OBJ_EXISTS ||
363 err->code == GOT_ERR_OBJ_TOO_LARGE)
364 err = NULL;
365 return err;
367 obj->refcnt++;
368 #endif
369 return NULL;
372 struct got_object *
373 got_repo_get_cached_object(struct got_repository *repo,
374 struct got_object_id *id)
376 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
379 const struct got_error *
380 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
381 struct got_tree_object *tree)
383 #ifndef GOT_NO_OBJ_CACHE
384 const struct got_error *err = NULL;
385 err = got_object_cache_add(&repo->treecache, id, tree);
386 if (err) {
387 if (err->code == GOT_ERR_OBJ_EXISTS ||
388 err->code == GOT_ERR_OBJ_TOO_LARGE)
389 err = NULL;
390 return err;
392 tree->refcnt++;
393 #endif
394 return NULL;
397 struct got_tree_object *
398 got_repo_get_cached_tree(struct got_repository *repo,
399 struct got_object_id *id)
401 return (struct got_tree_object *)got_object_cache_get(
402 &repo->treecache, id);
405 const struct got_error *
406 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
407 struct got_commit_object *commit)
409 #ifndef GOT_NO_OBJ_CACHE
410 const struct got_error *err = NULL;
411 err = got_object_cache_add(&repo->commitcache, id, commit);
412 if (err) {
413 if (err->code == GOT_ERR_OBJ_EXISTS ||
414 err->code == GOT_ERR_OBJ_TOO_LARGE)
415 err = NULL;
416 return err;
418 commit->refcnt++;
419 #endif
420 return NULL;
423 struct got_commit_object *
424 got_repo_get_cached_commit(struct got_repository *repo,
425 struct got_object_id *id)
427 return (struct got_commit_object *)got_object_cache_get(
428 &repo->commitcache, id);
431 const struct got_error *
432 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
433 struct got_tag_object *tag)
435 #ifndef GOT_NO_OBJ_CACHE
436 const struct got_error *err = NULL;
437 err = got_object_cache_add(&repo->tagcache, id, tag);
438 if (err) {
439 if (err->code == GOT_ERR_OBJ_EXISTS ||
440 err->code == GOT_ERR_OBJ_TOO_LARGE)
441 err = NULL;
442 return err;
444 tag->refcnt++;
445 #endif
446 return NULL;
449 struct got_tag_object *
450 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
452 return (struct got_tag_object *)got_object_cache_get(
453 &repo->tagcache, id);
456 const struct got_error *
457 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
458 struct got_raw_object *raw)
460 #ifndef GOT_NO_OBJ_CACHE
461 const struct got_error *err = NULL;
462 err = got_object_cache_add(&repo->rawcache, id, raw);
463 if (err) {
464 if (err->code == GOT_ERR_OBJ_EXISTS ||
465 err->code == GOT_ERR_OBJ_TOO_LARGE)
466 err = NULL;
467 return err;
469 raw->refcnt++;
470 #endif
471 return NULL;
475 struct got_raw_object *
476 got_repo_get_cached_raw_object(struct got_repository *repo,
477 struct got_object_id *id)
479 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
483 static const struct got_error *
484 open_repo(struct got_repository *repo, const char *path)
486 const struct got_error *err = NULL;
488 repo->gitdir_fd = -1;
490 /* bare git repository? */
491 repo->path_git_dir = strdup(path);
492 if (repo->path_git_dir == NULL)
493 return got_error_from_errno("strdup");
494 if (is_git_repo(repo)) {
495 repo->path = strdup(repo->path_git_dir);
496 if (repo->path == NULL) {
497 err = got_error_from_errno("strdup");
498 goto done;
500 repo->gitdir_fd = open(repo->path_git_dir,
501 O_DIRECTORY | O_CLOEXEC);
502 if (repo->gitdir_fd == -1) {
503 err = got_error_from_errno2("open",
504 repo->path_git_dir);
505 goto done;
507 return NULL;
510 /* git repository with working tree? */
511 free(repo->path_git_dir);
512 repo->path_git_dir = NULL;
513 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
514 err = got_error_from_errno("asprintf");
515 goto done;
517 if (is_git_repo(repo)) {
518 repo->path = strdup(path);
519 if (repo->path == NULL) {
520 err = got_error_from_errno("strdup");
521 goto done;
523 repo->gitdir_fd = open(repo->path_git_dir,
524 O_DIRECTORY | O_CLOEXEC);
525 if (repo->gitdir_fd == -1) {
526 err = got_error_from_errno2("open",
527 repo->path_git_dir);
528 goto done;
530 return NULL;
533 err = got_error(GOT_ERR_NOT_GIT_REPO);
534 done:
535 if (err) {
536 free(repo->path);
537 repo->path = NULL;
538 free(repo->path_git_dir);
539 repo->path_git_dir = NULL;
540 if (repo->gitdir_fd != -1)
541 close(repo->gitdir_fd);
542 repo->gitdir_fd = -1;
545 return err;
548 static const struct got_error *
549 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
551 const struct got_error *err = NULL;
552 char *repo_gitconfig_path = NULL;
554 if (global_gitconfig_path) {
555 /* Read settings from ~/.gitconfig. */
556 int dummy_repo_version;
557 err = got_repo_read_gitconfig(&dummy_repo_version,
558 &repo->global_gitconfig_author_name,
559 &repo->global_gitconfig_author_email,
560 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
561 if (err)
562 return err;
565 /* Read repository's .git/config file. */
566 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
567 if (repo_gitconfig_path == NULL)
568 return got_error_from_errno("got_repo_get_path_gitconfig");
570 err = got_repo_read_gitconfig(
571 &repo->gitconfig_repository_format_version,
572 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
573 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
574 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
575 repo_gitconfig_path);
576 if (err)
577 goto done;
579 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
580 int i;
582 for (i = 0; i < repo->ngitconfig_remotes; i++) {
583 got_repo_free_remote_repo_data(
584 &repo->gitconfig_remotes[i]);
586 free(repo->gitconfig_remotes);
587 repo->gitconfig_remotes = NULL;
588 repo->ngitconfig_remotes = 0;
590 free(repo->gitconfig_author_name);
591 repo->gitconfig_author_name = NULL;
592 free(repo->gitconfig_author_email);
593 repo->gitconfig_author_email = NULL;
595 free(repo->global_gitconfig_author_name);
596 repo->global_gitconfig_author_name = NULL;
597 free(repo->global_gitconfig_author_email);
598 repo->global_gitconfig_author_email = NULL;
601 done:
602 free(repo_gitconfig_path);
603 return err;
606 static const struct got_error *
607 read_gotconfig(struct got_repository *repo)
609 const struct got_error *err = NULL;
610 char *gotconfig_path;
612 gotconfig_path = got_repo_get_path_gotconfig(repo);
613 if (gotconfig_path == NULL)
614 return got_error_from_errno("got_repo_get_path_gotconfig");
616 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
617 free(gotconfig_path);
618 return err;
621 /* Supported repository format extensions. */
622 static const char *const repo_extensions[] = {
623 "noop", /* Got supports repository format version 1. */
624 "preciousObjects", /* Supported by gotadmin cleanup. */
625 "worktreeConfig", /* Got does not care about Git work trees. */
626 };
628 const struct got_error *
629 got_repo_open(struct got_repository **repop, const char *path,
630 const char *global_gitconfig_path, int *pack_fds)
632 struct got_repository *repo = NULL;
633 const struct got_error *err = NULL;
634 char *repo_path = NULL;
635 size_t i, j = 0;
636 struct rlimit rl;
638 *repop = NULL;
640 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
641 return got_error_from_errno("getrlimit");
643 repo = calloc(1, sizeof(*repo));
644 if (repo == NULL)
645 return got_error_from_errno("calloc");
647 RB_INIT(&repo->packidx_bloom_filters);
648 TAILQ_INIT(&repo->packidx_paths);
650 for (i = 0; i < nitems(repo->privsep_children); i++) {
651 memset(&repo->privsep_children[i], 0,
652 sizeof(repo->privsep_children[0]));
653 repo->privsep_children[i].imsg_fd = -1;
656 err = got_object_cache_init(&repo->objcache,
657 GOT_OBJECT_CACHE_TYPE_OBJ);
658 if (err)
659 goto done;
660 err = got_object_cache_init(&repo->treecache,
661 GOT_OBJECT_CACHE_TYPE_TREE);
662 if (err)
663 goto done;
664 err = got_object_cache_init(&repo->commitcache,
665 GOT_OBJECT_CACHE_TYPE_COMMIT);
666 if (err)
667 goto done;
668 err = got_object_cache_init(&repo->tagcache,
669 GOT_OBJECT_CACHE_TYPE_TAG);
670 if (err)
671 goto done;
672 err = got_object_cache_init(&repo->rawcache,
673 GOT_OBJECT_CACHE_TYPE_RAW);
674 if (err)
675 goto done;
677 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
678 if (repo->pack_cache_size > rl.rlim_cur / 8)
679 repo->pack_cache_size = rl.rlim_cur / 8;
680 for (i = 0; i < nitems(repo->packs); i++) {
681 if (pack_fds != NULL && i < repo->pack_cache_size) {
682 repo->packs[i].basefd = pack_fds[j++];
683 repo->packs[i].accumfd = pack_fds[j++];
684 } else {
685 repo->packs[i].basefd = -1;
686 repo->packs[i].accumfd = -1;
689 for (i = 0; i < nitems(repo->tempfiles); i++)
690 repo->tempfiles[i] = -1;
691 repo->pinned_pack = -1;
692 repo->pinned_packidx = -1;
693 repo->pinned_pid = 0;
695 repo_path = realpath(path, NULL);
696 if (repo_path == NULL) {
697 err = got_error_from_errno2("realpath", path);
698 goto done;
701 for (;;) {
702 char *parent_path;
704 err = open_repo(repo, repo_path);
705 if (err == NULL)
706 break;
707 if (err->code != GOT_ERR_NOT_GIT_REPO)
708 goto done;
709 if (repo_path[0] == '/' && repo_path[1] == '\0') {
710 err = got_error(GOT_ERR_NOT_GIT_REPO);
711 goto done;
713 err = got_path_dirname(&parent_path, repo_path);
714 if (err)
715 goto done;
716 free(repo_path);
717 repo_path = parent_path;
720 err = read_gotconfig(repo);
721 if (err)
722 goto done;
724 err = read_gitconfig(repo, global_gitconfig_path);
725 if (err)
726 goto done;
727 if (repo->gitconfig_repository_format_version != 0) {
728 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
729 goto done;
731 for (i = 0; i < repo->nextensions; i++) {
732 char *ext = repo->extensions[i];
733 int j, supported = 0;
734 for (j = 0; j < nitems(repo_extensions); j++) {
735 if (strcmp(ext, repo_extensions[j]) == 0) {
736 supported = 1;
737 break;
740 if (!supported) {
741 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
742 goto done;
746 err = got_repo_list_packidx(&repo->packidx_paths, repo);
747 done:
748 if (err)
749 got_repo_close(repo);
750 else
751 *repop = repo;
752 free(repo_path);
753 return err;
756 const struct got_error *
757 got_repo_close(struct got_repository *repo)
759 const struct got_error *err = NULL, *child_err;
760 struct got_packidx_bloom_filter *bf;
761 struct got_pathlist_entry *pe;
762 size_t i;
764 for (i = 0; i < repo->pack_cache_size; i++) {
765 if (repo->packidx_cache[i] == NULL)
766 break;
767 got_packidx_close(repo->packidx_cache[i]);
770 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
771 &repo->packidx_bloom_filters))) {
772 RB_REMOVE(got_packidx_bloom_filter_tree,
773 &repo->packidx_bloom_filters, bf);
774 bloom_free(bf->bloom);
775 free(bf->bloom);
776 free(bf);
779 for (i = 0; i < repo->pack_cache_size; i++)
780 if (repo->packs[i].path_packfile)
781 if (repo->packs[i].path_packfile)
782 got_pack_close(&repo->packs[i]);
784 free(repo->path);
785 free(repo->path_git_dir);
787 got_object_cache_close(&repo->objcache);
788 got_object_cache_close(&repo->treecache);
789 got_object_cache_close(&repo->commitcache);
790 got_object_cache_close(&repo->tagcache);
791 got_object_cache_close(&repo->rawcache);
793 for (i = 0; i < nitems(repo->privsep_children); i++) {
794 if (repo->privsep_children[i].imsg_fd == -1)
795 continue;
796 imsg_clear(repo->privsep_children[i].ibuf);
797 free(repo->privsep_children[i].ibuf);
798 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
799 child_err = got_privsep_wait_for_child(
800 repo->privsep_children[i].pid);
801 if (child_err && err == NULL)
802 err = child_err;
803 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
804 err == NULL)
805 err = got_error_from_errno("close");
808 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
809 err == NULL)
810 err = got_error_from_errno("close");
812 if (repo->gotconfig)
813 got_gotconfig_free(repo->gotconfig);
814 free(repo->gitconfig_author_name);
815 free(repo->gitconfig_author_email);
816 for (i = 0; i < repo->ngitconfig_remotes; i++)
817 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
818 free(repo->gitconfig_remotes);
819 for (i = 0; i < repo->nextensions; i++)
820 free(repo->extensions[i]);
821 free(repo->extensions);
823 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
824 free((void *)pe->path);
825 got_pathlist_free(&repo->packidx_paths);
826 free(repo);
828 return err;
831 void
832 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
834 int i;
836 free(repo->name);
837 repo->name = NULL;
838 free(repo->fetch_url);
839 repo->fetch_url = NULL;
840 free(repo->send_url);
841 repo->send_url = NULL;
842 for (i = 0; i < repo->nfetch_branches; i++)
843 free(repo->fetch_branches[i]);
844 free(repo->fetch_branches);
845 repo->fetch_branches = NULL;
846 repo->nfetch_branches = 0;
847 for (i = 0; i < repo->nsend_branches; i++)
848 free(repo->send_branches[i]);
849 free(repo->send_branches);
850 repo->send_branches = NULL;
851 repo->nsend_branches = 0;
854 const struct got_error *
855 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
856 const char *input_path)
858 const struct got_error *err = NULL;
859 const char *repo_abspath = NULL;
860 size_t repolen, len;
861 char *canonpath, *path = NULL;
863 *in_repo_path = NULL;
865 canonpath = strdup(input_path);
866 if (canonpath == NULL) {
867 err = got_error_from_errno("strdup");
868 goto done;
870 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
871 if (err)
872 goto done;
874 repo_abspath = got_repo_get_path(repo);
876 if (canonpath[0] == '\0') {
877 path = strdup(canonpath);
878 if (path == NULL) {
879 err = got_error_from_errno("strdup");
880 goto done;
882 } else {
883 path = realpath(canonpath, NULL);
884 if (path == NULL) {
885 if (errno != ENOENT) {
886 err = got_error_from_errno2("realpath",
887 canonpath);
888 goto done;
890 /*
891 * Path is not on disk.
892 * Assume it is already relative to repository root.
893 */
894 path = strdup(canonpath);
895 if (path == NULL) {
896 err = got_error_from_errno("strdup");
897 goto done;
901 repolen = strlen(repo_abspath);
902 len = strlen(path);
905 if (strcmp(path, repo_abspath) == 0) {
906 free(path);
907 path = strdup("");
908 if (path == NULL) {
909 err = got_error_from_errno("strdup");
910 goto done;
912 } else if (len > repolen &&
913 got_path_is_child(path, repo_abspath, repolen)) {
914 /* Matched an on-disk path inside repository. */
915 if (got_repo_is_bare(repo)) {
916 /*
917 * Matched an on-disk path inside repository
918 * database. Treat input as repository-relative.
919 */
920 free(path);
921 path = canonpath;
922 canonpath = NULL;
923 } else {
924 char *child;
925 /* Strip common prefix with repository path. */
926 err = got_path_skip_common_ancestor(&child,
927 repo_abspath, path);
928 if (err)
929 goto done;
930 free(path);
931 path = child;
933 } else {
934 /*
935 * Matched unrelated on-disk path.
936 * Treat input as repository-relative.
937 */
938 free(path);
939 path = canonpath;
940 canonpath = NULL;
944 /* Make in-repository path absolute */
945 if (path[0] != '/') {
946 char *abspath;
947 if (asprintf(&abspath, "/%s", path) == -1) {
948 err = got_error_from_errno("asprintf");
949 goto done;
951 free(path);
952 path = abspath;
955 done:
956 free(canonpath);
957 if (err)
958 free(path);
959 else
960 *in_repo_path = path;
961 return err;
964 static const struct got_error *
965 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
966 const char *path_packidx)
968 const struct got_error *err = NULL;
969 size_t i;
971 for (i = 0; i < repo->pack_cache_size; i++) {
972 if (repo->packidx_cache[i] == NULL)
973 break;
974 if (strcmp(repo->packidx_cache[i]->path_packidx,
975 path_packidx) == 0) {
976 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
979 if (i == repo->pack_cache_size) {
980 do {
981 i--;
982 } while (i > 0 && repo->pinned_packidx >= 0 &&
983 i == repo->pinned_packidx);
984 err = got_packidx_close(repo->packidx_cache[i]);
985 if (err)
986 return err;
989 repo->packidx_cache[i] = packidx;
991 return NULL;
994 int
995 got_repo_is_packidx_filename(const char *name, size_t len)
997 if (len != GOT_PACKIDX_NAMELEN)
998 return 0;
1000 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1001 return 0;
1003 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1004 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1005 return 0;
1007 return 1;
1010 static struct got_packidx_bloom_filter *
1011 get_packidx_bloom_filter(struct got_repository *repo,
1012 const char *path, size_t path_len)
1014 struct got_packidx_bloom_filter key;
1016 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1017 return NULL; /* XXX */
1018 key.path_len = path_len;
1020 return RB_FIND(got_packidx_bloom_filter_tree,
1021 &repo->packidx_bloom_filters, &key);
1024 int
1025 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1026 const char *path_packidx, struct got_object_id *id)
1028 struct got_packidx_bloom_filter *bf;
1030 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1031 if (bf)
1032 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1034 /* No bloom filter means this pack index must be searched. */
1035 return 1;
1038 static const struct got_error *
1039 add_packidx_bloom_filter(struct got_repository *repo,
1040 struct got_packidx *packidx, const char *path_packidx)
1042 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1043 struct got_packidx_bloom_filter *bf;
1044 size_t len;
1047 * Don't use bloom filters for very large pack index files.
1048 * Large pack files will contain a relatively large fraction
1049 * of our objects so we will likely need to visit them anyway.
1050 * The more objects a pack file contains the higher the probability
1051 * of a false-positive match from the bloom filter. And reading
1052 * all object IDs from a large pack index file can be expensive.
1054 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1055 return NULL;
1057 /* Do we already have a filter for this pack index? */
1058 if (get_packidx_bloom_filter(repo, path_packidx,
1059 strlen(path_packidx)) != NULL)
1060 return NULL;
1062 bf = calloc(1, sizeof(*bf));
1063 if (bf == NULL)
1064 return got_error_from_errno("calloc");
1065 bf->bloom = calloc(1, sizeof(*bf->bloom));
1066 if (bf->bloom == NULL) {
1067 free(bf);
1068 return got_error_from_errno("calloc");
1071 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1072 if (len >= sizeof(bf->path)) {
1073 free(bf->bloom);
1074 free(bf);
1075 return got_error(GOT_ERR_NO_SPACE);
1077 bf->path_len = len;
1079 /* Minimum size supported by our bloom filter is 1000 entries. */
1080 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1081 for (i = 0; i < nobjects; i++) {
1082 struct got_packidx_object_id *id;
1083 id = &packidx->hdr.sorted_ids[i];
1084 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1087 RB_INSERT(got_packidx_bloom_filter_tree,
1088 &repo->packidx_bloom_filters, bf);
1089 return NULL;
1092 static void
1093 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1095 struct got_pathlist_entry *pe;
1097 while (!TAILQ_EMPTY(packidx_paths)) {
1098 pe = TAILQ_FIRST(packidx_paths);
1099 TAILQ_REMOVE(packidx_paths, pe, entry);
1100 free((char *)pe->path);
1101 free(pe);
1105 static const struct got_error *
1106 refresh_packidx_paths(struct got_repository *repo)
1108 const struct got_error *err = NULL;
1109 char *objects_pack_dir = NULL;
1110 struct stat sb;
1112 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1113 if (objects_pack_dir == NULL)
1114 return got_error_from_errno("got_repo_get_path_objects_pack");
1116 if (stat(objects_pack_dir, &sb) == -1) {
1117 if (errno != ENOENT) {
1118 err = got_error_from_errno2("stat", objects_pack_dir);
1119 goto done;
1121 } else if (sb.st_mtime != repo->pack_path_mtime) {
1122 purge_packidx_paths(&repo->packidx_paths);
1123 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1124 if (err)
1125 goto done;
1127 done:
1128 free(objects_pack_dir);
1129 return err;
1132 const struct got_error *
1133 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1134 struct got_repository *repo, struct got_object_id *id)
1136 const struct got_error *err;
1137 struct got_pathlist_entry *pe;
1138 size_t i;
1140 /* Search pack index cache. */
1141 for (i = 0; i < repo->pack_cache_size; i++) {
1142 if (repo->packidx_cache[i] == NULL)
1143 break;
1144 if (!got_repo_check_packidx_bloom_filter(repo,
1145 repo->packidx_cache[i]->path_packidx, id))
1146 continue; /* object will not be found in this index */
1147 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1148 if (*idx != -1) {
1149 *packidx = repo->packidx_cache[i];
1151 * Move this cache entry to the front. Repeatedly
1152 * searching a wrong pack index can be expensive.
1154 if (i > 0) {
1155 memmove(&repo->packidx_cache[1],
1156 &repo->packidx_cache[0],
1157 i * sizeof(repo->packidx_cache[0]));
1158 repo->packidx_cache[0] = *packidx;
1159 if (repo->pinned_packidx >= 0 &&
1160 repo->pinned_packidx < i)
1161 repo->pinned_packidx++;
1162 else if (repo->pinned_packidx == i)
1163 repo->pinned_packidx = 0;
1165 return NULL;
1168 /* No luck. Search the filesystem. */
1170 err = refresh_packidx_paths(repo);
1171 if (err)
1172 return err;
1174 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1175 const char *path_packidx = pe->path;
1176 int is_cached = 0;
1178 if (!got_repo_check_packidx_bloom_filter(repo,
1179 pe->path, id))
1180 continue; /* object will not be found in this index */
1182 for (i = 0; i < repo->pack_cache_size; i++) {
1183 if (repo->packidx_cache[i] == NULL)
1184 break;
1185 if (strcmp(repo->packidx_cache[i]->path_packidx,
1186 path_packidx) == 0) {
1187 is_cached = 1;
1188 break;
1191 if (is_cached)
1192 continue; /* already searched */
1194 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1195 path_packidx, 0);
1196 if (err)
1197 goto done;
1199 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1200 if (err)
1201 goto done;
1203 err = cache_packidx(repo, *packidx, path_packidx);
1204 if (err)
1205 goto done;
1207 *idx = got_packidx_get_object_idx(*packidx, id);
1208 if (*idx != -1) {
1209 err = NULL; /* found the object */
1210 goto done;
1214 err = got_error_no_obj(id);
1215 done:
1216 return err;
1219 const struct got_error *
1220 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1221 struct got_repository *repo)
1223 const struct got_error *err = NULL;
1224 DIR *packdir = NULL;
1225 struct dirent *dent;
1226 char *path_packidx = NULL;
1227 int packdir_fd;
1228 struct stat sb;
1230 packdir_fd = openat(got_repo_get_fd(repo),
1231 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1232 if (packdir_fd == -1) {
1233 return got_error_from_errno_fmt("openat: %s/%s",
1234 got_repo_get_path_git_dir(repo),
1235 GOT_OBJECTS_PACK_DIR);
1238 packdir = fdopendir(packdir_fd);
1239 if (packdir == NULL) {
1240 err = got_error_from_errno("fdopendir");
1241 goto done;
1244 if (fstat(packdir_fd, &sb) == -1) {
1245 err = got_error_from_errno("fstat");
1246 goto done;
1248 repo->pack_path_mtime = sb.st_mtime;
1250 while ((dent = readdir(packdir)) != NULL) {
1251 if (!got_repo_is_packidx_filename(dent->d_name,
1252 strlen(dent->d_name)))
1253 continue;
1255 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1256 dent->d_name) == -1) {
1257 err = got_error_from_errno("asprintf");
1258 path_packidx = NULL;
1259 break;
1262 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1263 if (err)
1264 break;
1266 done:
1267 if (err)
1268 free(path_packidx);
1269 if (packdir && closedir(packdir) != 0 && err == NULL)
1270 err = got_error_from_errno("closedir");
1271 return err;
1274 const struct got_error *
1275 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1276 struct got_repository *repo)
1278 const struct got_error *err;
1279 size_t i;
1281 *packidx = NULL;
1283 /* Search pack index cache. */
1284 for (i = 0; i < repo->pack_cache_size; i++) {
1285 if (repo->packidx_cache[i] == NULL)
1286 break;
1287 if (strcmp(repo->packidx_cache[i]->path_packidx,
1288 path_packidx) == 0) {
1289 *packidx = repo->packidx_cache[i];
1290 return NULL;
1293 /* No luck. Search the filesystem. */
1295 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1296 path_packidx, 0);
1297 if (err)
1298 return err;
1300 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1301 if (err)
1302 goto done;
1304 err = cache_packidx(repo, *packidx, path_packidx);
1305 done:
1306 if (err) {
1307 got_packidx_close(*packidx);
1308 *packidx = NULL;
1310 return err;
1313 static const struct got_error *
1314 read_packfile_hdr(int fd, struct got_packidx *packidx)
1316 const struct got_error *err = NULL;
1317 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1318 struct got_packfile_hdr hdr;
1319 ssize_t n;
1321 n = read(fd, &hdr, sizeof(hdr));
1322 if (n < 0)
1323 return got_error_from_errno("read");
1324 if (n != sizeof(hdr))
1325 return got_error(GOT_ERR_BAD_PACKFILE);
1327 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1328 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1329 be32toh(hdr.nobjects) != totobj)
1330 err = got_error(GOT_ERR_BAD_PACKFILE);
1332 return err;
1335 static const struct got_error *
1336 open_packfile(int *fd, struct got_repository *repo,
1337 const char *relpath, struct got_packidx *packidx)
1339 const struct got_error *err = NULL;
1341 *fd = openat(got_repo_get_fd(repo), relpath,
1342 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1343 if (*fd == -1)
1344 return got_error_from_errno_fmt("openat: %s/%s",
1345 got_repo_get_path_git_dir(repo), relpath);
1347 if (packidx) {
1348 err = read_packfile_hdr(*fd, packidx);
1349 if (err) {
1350 close(*fd);
1351 *fd = -1;
1355 return err;
1358 const struct got_error *
1359 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1360 const char *path_packfile, struct got_packidx *packidx)
1362 const struct got_error *err = NULL;
1363 struct got_pack *pack = NULL;
1364 struct stat sb;
1365 size_t i;
1367 if (packp)
1368 *packp = NULL;
1370 for (i = 0; i < repo->pack_cache_size; i++) {
1371 pack = &repo->packs[i];
1372 if (pack->path_packfile == NULL)
1373 break;
1374 if (strcmp(pack->path_packfile, path_packfile) == 0)
1375 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1378 if (i == repo->pack_cache_size) {
1379 struct got_pack tmp;
1380 do {
1381 i--;
1382 } while (i > 0 && repo->pinned_pack >= 0 &&
1383 i == repo->pinned_pack);
1384 err = got_pack_close(&repo->packs[i]);
1385 if (err)
1386 return err;
1387 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1388 return got_error_from_errno("ftruncate");
1389 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1390 return got_error_from_errno("ftruncate");
1391 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1392 memcpy(&repo->packs[i], &repo->packs[0],
1393 sizeof(repo->packs[i]));
1394 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1395 if (repo->pinned_pack == 0)
1396 repo->pinned_pack = i;
1397 else if (repo->pinned_pack == i)
1398 repo->pinned_pack = 0;
1399 i = 0;
1402 pack = &repo->packs[i];
1404 pack->path_packfile = strdup(path_packfile);
1405 if (pack->path_packfile == NULL) {
1406 err = got_error_from_errno("strdup");
1407 goto done;
1410 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1411 if (err)
1412 goto done;
1414 if (fstat(pack->fd, &sb) != 0) {
1415 err = got_error_from_errno("fstat");
1416 goto done;
1418 pack->filesize = sb.st_size;
1420 pack->privsep_child = NULL;
1422 err = got_delta_cache_alloc(&pack->delta_cache);
1423 if (err)
1424 goto done;
1426 #ifndef GOT_PACK_NO_MMAP
1427 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1428 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1429 pack->fd, 0);
1430 if (pack->map == MAP_FAILED) {
1431 if (errno != ENOMEM) {
1432 err = got_error_from_errno("mmap");
1433 goto done;
1435 pack->map = NULL; /* fall back to read(2) */
1438 #endif
1439 done:
1440 if (err) {
1441 if (pack)
1442 got_pack_close(pack);
1443 } else if (packp)
1444 *packp = pack;
1445 return err;
1448 struct got_pack *
1449 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1451 struct got_pack *pack = NULL;
1452 size_t i;
1454 for (i = 0; i < repo->pack_cache_size; i++) {
1455 pack = &repo->packs[i];
1456 if (pack->path_packfile == NULL)
1457 break;
1458 if (strcmp(pack->path_packfile, path_packfile) == 0)
1459 return pack;
1462 return NULL;
1465 const struct got_error *
1466 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1467 struct got_pack *pack)
1469 size_t i;
1470 int pinned_pack = -1, pinned_packidx = -1;
1472 for (i = 0; i < repo->pack_cache_size; i++) {
1473 if (repo->packidx_cache[i] &&
1474 strcmp(repo->packidx_cache[i]->path_packidx,
1475 packidx->path_packidx) == 0)
1476 pinned_packidx = i;
1477 if (repo->packs[i].path_packfile &&
1478 strcmp(repo->packs[i].path_packfile,
1479 pack->path_packfile) == 0)
1480 pinned_pack = i;
1483 if (pinned_packidx == -1 || pinned_pack == -1)
1484 return got_error(GOT_ERR_PIN_PACK);
1486 repo->pinned_pack = pinned_pack;
1487 repo->pinned_packidx = pinned_packidx;
1488 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1489 return NULL;
1492 struct got_pack *
1493 got_repo_get_pinned_pack(struct got_repository *repo)
1495 if (repo->pinned_pack >= 0 &&
1496 repo->pinned_pack < repo->pack_cache_size)
1497 return &repo->packs[repo->pinned_pack];
1499 return NULL;
1502 void
1503 got_repo_unpin_pack(struct got_repository *repo)
1505 repo->pinned_packidx = -1;
1506 repo->pinned_pack = -1;
1507 repo->pinned_pid = 0;
1510 const struct got_error *
1511 got_repo_init(const char *repo_path, const char *head_name)
1513 const struct got_error *err = NULL;
1514 const char *dirnames[] = {
1515 GOT_OBJECTS_DIR,
1516 GOT_OBJECTS_PACK_DIR,
1517 GOT_REFS_DIR,
1519 const char *description_str = "Unnamed repository; "
1520 "edit this file 'description' to name the repository.";
1521 const char *headref = "ref: refs/heads/";
1522 const char *gitconfig_str = "[core]\n"
1523 "\trepositoryformatversion = 0\n"
1524 "\tfilemode = true\n"
1525 "\tbare = true\n";
1526 char *headref_str, *path;
1527 size_t i;
1529 if (!got_path_dir_is_empty(repo_path))
1530 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1532 for (i = 0; i < nitems(dirnames); i++) {
1533 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1534 return got_error_from_errno("asprintf");
1536 err = got_path_mkdir(path);
1537 free(path);
1538 if (err)
1539 return err;
1542 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1543 return got_error_from_errno("asprintf");
1544 err = got_path_create_file(path, description_str);
1545 free(path);
1546 if (err)
1547 return err;
1549 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1550 return got_error_from_errno("asprintf");
1551 if (asprintf(&headref_str, "%s%s", headref,
1552 head_name ? head_name : "main") == -1) {
1553 free(path);
1554 return got_error_from_errno("asprintf");
1556 err = got_path_create_file(path, headref_str);
1557 free(headref_str);
1558 free(path);
1559 if (err)
1560 return err;
1562 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1563 return got_error_from_errno("asprintf");
1564 err = got_path_create_file(path, gitconfig_str);
1565 free(path);
1566 if (err)
1567 return err;
1569 return NULL;
1572 static const struct got_error *
1573 match_packed_object(struct got_object_id **unique_id,
1574 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1576 const struct got_error *err = NULL;
1577 struct got_object_id_queue matched_ids;
1578 struct got_pathlist_entry *pe;
1580 STAILQ_INIT(&matched_ids);
1582 err = refresh_packidx_paths(repo);
1583 if (err)
1584 return err;
1586 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1587 const char *path_packidx = pe->path;
1588 struct got_packidx *packidx;
1589 struct got_object_qid *qid;
1591 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1592 path_packidx, 0);
1593 if (err)
1594 break;
1596 err = got_packidx_match_id_str_prefix(&matched_ids,
1597 packidx, id_str_prefix);
1598 if (err) {
1599 got_packidx_close(packidx);
1600 break;
1602 err = got_packidx_close(packidx);
1603 if (err)
1604 break;
1606 STAILQ_FOREACH(qid, &matched_ids, entry) {
1607 if (obj_type != GOT_OBJ_TYPE_ANY) {
1608 int matched_type;
1609 err = got_object_get_type(&matched_type, repo,
1610 &qid->id);
1611 if (err)
1612 goto done;
1613 if (matched_type != obj_type)
1614 continue;
1616 if (*unique_id == NULL) {
1617 *unique_id = got_object_id_dup(&qid->id);
1618 if (*unique_id == NULL) {
1619 err = got_error_from_errno("malloc");
1620 goto done;
1622 } else {
1623 if (got_object_id_cmp(*unique_id,
1624 &qid->id) == 0)
1625 continue; /* packed multiple times */
1626 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1627 goto done;
1631 done:
1632 got_object_id_queue_free(&matched_ids);
1633 if (err) {
1634 free(*unique_id);
1635 *unique_id = NULL;
1637 return err;
1640 static const struct got_error *
1641 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1642 const char *object_dir, const char *id_str_prefix, int obj_type,
1643 struct got_repository *repo)
1645 const struct got_error *err = NULL;
1646 char *path, *id_str = NULL;
1647 DIR *dir = NULL;
1648 struct dirent *dent;
1649 struct got_object_id id;
1651 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1652 err = got_error_from_errno("asprintf");
1653 goto done;
1656 dir = opendir(path);
1657 if (dir == NULL) {
1658 if (errno == ENOENT) {
1659 err = NULL;
1660 goto done;
1662 err = got_error_from_errno2("opendir", path);
1663 goto done;
1665 while ((dent = readdir(dir)) != NULL) {
1666 int cmp;
1668 free(id_str);
1669 id_str = NULL;
1671 if (strcmp(dent->d_name, ".") == 0 ||
1672 strcmp(dent->d_name, "..") == 0)
1673 continue;
1675 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1676 err = got_error_from_errno("asprintf");
1677 goto done;
1680 if (!got_parse_sha1_digest(id.sha1, id_str))
1681 continue;
1684 * Directory entries do not necessarily appear in
1685 * sorted order, so we must iterate over all of them.
1687 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1688 if (cmp != 0)
1689 continue;
1691 if (*unique_id == NULL) {
1692 if (obj_type != GOT_OBJ_TYPE_ANY) {
1693 int matched_type;
1694 err = got_object_get_type(&matched_type, repo,
1695 &id);
1696 if (err)
1697 goto done;
1698 if (matched_type != obj_type)
1699 continue;
1701 *unique_id = got_object_id_dup(&id);
1702 if (*unique_id == NULL) {
1703 err = got_error_from_errno("got_object_id_dup");
1704 goto done;
1706 } else {
1707 if (got_object_id_cmp(*unique_id, &id) == 0)
1708 continue; /* both packed and loose */
1709 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1710 goto done;
1713 done:
1714 if (dir && closedir(dir) != 0 && err == NULL)
1715 err = got_error_from_errno("closedir");
1716 if (err) {
1717 free(*unique_id);
1718 *unique_id = NULL;
1720 free(id_str);
1721 free(path);
1722 return err;
1725 const struct got_error *
1726 got_repo_match_object_id_prefix(struct got_object_id **id,
1727 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1729 const struct got_error *err = NULL;
1730 char *path_objects = NULL, *object_dir = NULL;
1731 size_t len;
1732 int i;
1734 *id = NULL;
1736 path_objects = got_repo_get_path_objects(repo);
1738 len = strlen(id_str_prefix);
1739 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1740 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1741 goto done;
1744 for (i = 0; i < len; i++) {
1745 if (isxdigit((unsigned char)id_str_prefix[i]))
1746 continue;
1747 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1748 goto done;
1751 if (len >= 2) {
1752 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1753 if (err)
1754 goto done;
1755 object_dir = strndup(id_str_prefix, 2);
1756 if (object_dir == NULL) {
1757 err = got_error_from_errno("strdup");
1758 goto done;
1760 err = match_loose_object(id, path_objects, object_dir,
1761 id_str_prefix, obj_type, repo);
1762 } else if (len == 1) {
1763 int i;
1764 for (i = 0; i < 0xf; i++) {
1765 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1766 == -1) {
1767 err = got_error_from_errno("asprintf");
1768 goto done;
1770 err = match_packed_object(id, repo, object_dir,
1771 obj_type);
1772 if (err)
1773 goto done;
1774 err = match_loose_object(id, path_objects, object_dir,
1775 id_str_prefix, obj_type, repo);
1776 if (err)
1777 goto done;
1779 } else {
1780 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1781 goto done;
1783 done:
1784 free(path_objects);
1785 free(object_dir);
1786 if (err) {
1787 free(*id);
1788 *id = NULL;
1789 } else if (*id == NULL) {
1790 switch (obj_type) {
1791 case GOT_OBJ_TYPE_BLOB:
1792 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1793 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1794 break;
1795 case GOT_OBJ_TYPE_TREE:
1796 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1797 GOT_OBJ_LABEL_TREE, id_str_prefix);
1798 break;
1799 case GOT_OBJ_TYPE_COMMIT:
1800 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1801 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1802 break;
1803 case GOT_OBJ_TYPE_TAG:
1804 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1805 GOT_OBJ_LABEL_TAG, id_str_prefix);
1806 break;
1807 default:
1808 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1809 break;
1813 return err;
1816 const struct got_error *
1817 got_repo_match_object_id(struct got_object_id **id, char **label,
1818 const char *id_str, int obj_type, struct got_reflist_head *refs,
1819 struct got_repository *repo)
1821 const struct got_error *err;
1822 struct got_tag_object *tag;
1823 struct got_reference *ref = NULL;
1825 *id = NULL;
1826 if (label)
1827 *label = NULL;
1829 if (refs) {
1830 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1831 refs, repo);
1832 if (err == NULL) {
1833 *id = got_object_id_dup(
1834 got_object_tag_get_object_id(tag));
1835 if (*id == NULL)
1836 err = got_error_from_errno("got_object_id_dup");
1837 else if (label && asprintf(label, "refs/tags/%s",
1838 got_object_tag_get_name(tag)) == -1) {
1839 err = got_error_from_errno("asprintf");
1840 free(*id);
1841 *id = NULL;
1843 got_object_tag_close(tag);
1844 return err;
1845 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1846 err->code != GOT_ERR_NO_OBJ)
1847 return err;
1850 err = got_ref_open(&ref, repo, id_str, 0);
1851 if (err == NULL) {
1852 err = got_ref_resolve(id, repo, ref);
1853 if (err)
1854 goto done;
1855 if (label) {
1856 *label = strdup(got_ref_get_name(ref));
1857 if (*label == NULL) {
1858 err = got_error_from_errno("strdup");
1859 goto done;
1862 } else {
1863 if (err->code != GOT_ERR_NOT_REF &&
1864 err->code != GOT_ERR_BAD_REF_NAME)
1865 goto done;
1866 err = got_repo_match_object_id_prefix(id, id_str,
1867 obj_type, repo);
1868 if (err) {
1869 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1870 err = got_error_not_ref(id_str);
1871 goto done;
1873 if (label) {
1874 err = got_object_id_str(label, *id);
1875 if (*label == NULL) {
1876 err = got_error_from_errno("strdup");
1877 goto done;
1881 done:
1882 if (ref)
1883 got_ref_close(ref);
1884 return err;
1887 const struct got_error *
1888 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1889 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1891 const struct got_error *err = NULL;
1892 struct got_reflist_entry *re;
1893 struct got_object_id *tag_id;
1894 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1896 *tag = NULL;
1898 TAILQ_FOREACH(re, refs, entry) {
1899 const char *refname;
1900 refname = got_ref_get_name(re->ref);
1901 if (got_ref_is_symbolic(re->ref))
1902 continue;
1903 if (strncmp(refname, "refs/tags/", 10) != 0)
1904 continue;
1905 if (!name_is_absolute)
1906 refname += strlen("refs/tags/");
1907 if (strcmp(refname, name) != 0)
1908 continue;
1909 err = got_ref_resolve(&tag_id, repo, re->ref);
1910 if (err)
1911 break;
1912 err = got_object_open_as_tag(tag, repo, tag_id);
1913 free(tag_id);
1914 if (err)
1915 break;
1916 if (obj_type == GOT_OBJ_TYPE_ANY ||
1917 got_object_tag_get_object_type(*tag) == obj_type)
1918 break;
1919 got_object_tag_close(*tag);
1920 *tag = NULL;
1923 if (err == NULL && *tag == NULL)
1924 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1925 GOT_OBJ_LABEL_TAG, name);
1926 return err;
1929 static const struct got_error *
1930 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1931 const char *name, mode_t mode, struct got_object_id *blob_id)
1933 const struct got_error *err = NULL;
1935 *new_te = NULL;
1937 *new_te = calloc(1, sizeof(**new_te));
1938 if (*new_te == NULL)
1939 return got_error_from_errno("calloc");
1941 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1942 sizeof((*new_te)->name)) {
1943 err = got_error(GOT_ERR_NO_SPACE);
1944 goto done;
1947 if (S_ISLNK(mode)) {
1948 (*new_te)->mode = S_IFLNK;
1949 } else {
1950 (*new_te)->mode = S_IFREG;
1951 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1953 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1954 done:
1955 if (err && *new_te) {
1956 free(*new_te);
1957 *new_te = NULL;
1959 return err;
1962 static const struct got_error *
1963 import_file(struct got_tree_entry **new_te, struct dirent *de,
1964 const char *path, struct got_repository *repo)
1966 const struct got_error *err;
1967 struct got_object_id *blob_id = NULL;
1968 char *filepath;
1969 struct stat sb;
1971 if (asprintf(&filepath, "%s%s%s", path,
1972 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1973 return got_error_from_errno("asprintf");
1975 if (lstat(filepath, &sb) != 0) {
1976 err = got_error_from_errno2("lstat", path);
1977 goto done;
1980 err = got_object_blob_create(&blob_id, filepath, repo);
1981 if (err)
1982 goto done;
1984 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1985 blob_id);
1986 done:
1987 free(filepath);
1988 if (err)
1989 free(blob_id);
1990 return err;
1993 static const struct got_error *
1994 insert_tree_entry(struct got_tree_entry *new_te,
1995 struct got_pathlist_head *paths)
1997 const struct got_error *err = NULL;
1998 struct got_pathlist_entry *new_pe;
2000 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2001 if (err)
2002 return err;
2003 if (new_pe == NULL)
2004 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2005 return NULL;
2008 static const struct got_error *write_tree(struct got_object_id **,
2009 const char *, struct got_pathlist_head *, struct got_repository *,
2010 got_repo_import_cb progress_cb, void *progress_arg);
2012 static const struct got_error *
2013 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2014 const char *path, struct got_pathlist_head *ignores,
2015 struct got_repository *repo,
2016 got_repo_import_cb progress_cb, void *progress_arg)
2018 const struct got_error *err;
2019 struct got_object_id *id = NULL;
2020 char *subdirpath;
2022 if (asprintf(&subdirpath, "%s%s%s", path,
2023 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2024 return got_error_from_errno("asprintf");
2026 (*new_te) = calloc(1, sizeof(**new_te));
2027 if (*new_te == NULL)
2028 return got_error_from_errno("calloc");
2029 (*new_te)->mode = S_IFDIR;
2030 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2031 sizeof((*new_te)->name)) {
2032 err = got_error(GOT_ERR_NO_SPACE);
2033 goto done;
2035 err = write_tree(&id, subdirpath, ignores, repo,
2036 progress_cb, progress_arg);
2037 if (err)
2038 goto done;
2039 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2041 done:
2042 free(id);
2043 free(subdirpath);
2044 if (err) {
2045 free(*new_te);
2046 *new_te = NULL;
2048 return err;
2051 static const struct got_error *
2052 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2053 struct got_pathlist_head *ignores, struct got_repository *repo,
2054 got_repo_import_cb progress_cb, void *progress_arg)
2056 const struct got_error *err = NULL;
2057 DIR *dir;
2058 struct dirent *de;
2059 int nentries;
2060 struct got_tree_entry *new_te = NULL;
2061 struct got_pathlist_head paths;
2062 struct got_pathlist_entry *pe;
2064 *new_tree_id = NULL;
2066 TAILQ_INIT(&paths);
2068 dir = opendir(path_dir);
2069 if (dir == NULL) {
2070 err = got_error_from_errno2("opendir", path_dir);
2071 goto done;
2074 nentries = 0;
2075 while ((de = readdir(dir)) != NULL) {
2076 int ignore = 0;
2077 int type;
2079 if (strcmp(de->d_name, ".") == 0 ||
2080 strcmp(de->d_name, "..") == 0)
2081 continue;
2083 TAILQ_FOREACH(pe, ignores, entry) {
2084 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2085 ignore = 1;
2086 break;
2089 if (ignore)
2090 continue;
2092 err = got_path_dirent_type(&type, path_dir, de);
2093 if (err)
2094 goto done;
2096 if (type == DT_DIR) {
2097 err = import_subdir(&new_te, de, path_dir,
2098 ignores, repo, progress_cb, progress_arg);
2099 if (err) {
2100 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2101 goto done;
2102 err = NULL;
2103 continue;
2105 } else if (type == DT_REG || type == DT_LNK) {
2106 err = import_file(&new_te, de, path_dir, repo);
2107 if (err)
2108 goto done;
2109 } else
2110 continue;
2112 err = insert_tree_entry(new_te, &paths);
2113 if (err)
2114 goto done;
2115 nentries++;
2118 if (TAILQ_EMPTY(&paths)) {
2119 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2120 "cannot create tree without any entries");
2121 goto done;
2124 TAILQ_FOREACH(pe, &paths, entry) {
2125 struct got_tree_entry *te = pe->data;
2126 char *path;
2127 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2128 continue;
2129 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2130 err = got_error_from_errno("asprintf");
2131 goto done;
2133 err = (*progress_cb)(progress_arg, path);
2134 free(path);
2135 if (err)
2136 goto done;
2139 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2140 done:
2141 if (dir)
2142 closedir(dir);
2143 got_pathlist_free(&paths);
2144 return err;
2147 const struct got_error *
2148 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2149 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2150 struct got_repository *repo, got_repo_import_cb progress_cb,
2151 void *progress_arg)
2153 const struct got_error *err;
2154 struct got_object_id *new_tree_id;
2156 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2157 progress_cb, progress_arg);
2158 if (err)
2159 return err;
2161 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2162 author, time(NULL), author, time(NULL), logmsg, repo);
2163 free(new_tree_id);
2164 return err;
2167 const struct got_error *
2168 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2169 struct got_repository *repo)
2171 const struct got_error *err = NULL;
2172 char *path_objects = NULL, *path = NULL;
2173 DIR *dir = NULL;
2174 struct got_object_id id;
2175 int i;
2177 *nobjects = 0;
2178 *ondisk_size = 0;
2180 path_objects = got_repo_get_path_objects(repo);
2181 if (path_objects == NULL)
2182 return got_error_from_errno("got_repo_get_path_objects");
2184 for (i = 0; i <= 0xff; i++) {
2185 struct dirent *dent;
2187 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2188 err = got_error_from_errno("asprintf");
2189 break;
2192 dir = opendir(path);
2193 if (dir == NULL) {
2194 if (errno == ENOENT) {
2195 err = NULL;
2196 continue;
2198 err = got_error_from_errno2("opendir", path);
2199 break;
2202 while ((dent = readdir(dir)) != NULL) {
2203 char *id_str;
2204 int fd;
2205 struct stat sb;
2207 if (strcmp(dent->d_name, ".") == 0 ||
2208 strcmp(dent->d_name, "..") == 0)
2209 continue;
2211 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2212 err = got_error_from_errno("asprintf");
2213 goto done;
2216 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2217 free(id_str);
2218 continue;
2220 free(id_str);
2222 err = got_object_open_loose_fd(&fd, &id, repo);
2223 if (err)
2224 goto done;
2226 if (fstat(fd, &sb) == -1) {
2227 err = got_error_from_errno("fstat");
2228 close(fd);
2229 goto done;
2231 (*nobjects)++;
2232 (*ondisk_size) += sb.st_size;
2234 if (close(fd) == -1) {
2235 err = got_error_from_errno("close");
2236 goto done;
2240 if (closedir(dir) != 0) {
2241 err = got_error_from_errno("closedir");
2242 goto done;
2244 dir = NULL;
2246 free(path);
2247 path = NULL;
2249 done:
2250 if (dir && closedir(dir) != 0 && err == NULL)
2251 err = got_error_from_errno("closedir");
2253 if (err) {
2254 *nobjects = 0;
2255 *ondisk_size = 0;
2257 free(path_objects);
2258 free(path);
2259 return err;
2262 const struct got_error *
2263 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2264 off_t *total_packsize, struct got_repository *repo)
2266 const struct got_error *err = NULL;
2267 DIR *packdir = NULL;
2268 struct dirent *dent;
2269 struct got_packidx *packidx = NULL;
2270 char *path_packidx;
2271 char *path_packfile;
2272 int packdir_fd;
2273 struct stat sb;
2275 *npackfiles = 0;
2276 *nobjects = 0;
2277 *total_packsize = 0;
2279 packdir_fd = openat(got_repo_get_fd(repo),
2280 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2281 if (packdir_fd == -1) {
2282 return got_error_from_errno_fmt("openat: %s/%s",
2283 got_repo_get_path_git_dir(repo),
2284 GOT_OBJECTS_PACK_DIR);
2287 packdir = fdopendir(packdir_fd);
2288 if (packdir == NULL) {
2289 err = got_error_from_errno("fdopendir");
2290 goto done;
2293 while ((dent = readdir(packdir)) != NULL) {
2294 if (!got_repo_is_packidx_filename(dent->d_name,
2295 strlen(dent->d_name)))
2296 continue;
2298 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2299 dent->d_name) == -1) {
2300 err = got_error_from_errno("asprintf");
2301 goto done;
2304 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2305 path_packidx, 0);
2306 free(path_packidx);
2307 if (err)
2308 goto done;
2310 if (fstat(packidx->fd, &sb) == -1)
2311 goto done;
2312 *total_packsize += sb.st_size;
2314 err = got_packidx_get_packfile_path(&path_packfile,
2315 packidx->path_packidx);
2316 if (err)
2317 goto done;
2319 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2320 0) == -1) {
2321 free(path_packfile);
2322 goto done;
2324 free(path_packfile);
2325 *total_packsize += sb.st_size;
2327 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2329 (*npackfiles)++;
2331 got_packidx_close(packidx);
2332 packidx = NULL;
2334 done:
2335 if (packidx)
2336 got_packidx_close(packidx);
2337 if (packdir && closedir(packdir) != 0 && err == NULL)
2338 err = got_error_from_errno("closedir");
2339 if (err) {
2340 *npackfiles = 0;
2341 *nobjects = 0;
2342 *total_packsize = 0;
2344 return err;
2347 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2348 got_packidx_bloom_filter_cmp);