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 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1428 pack->fd, 0);
1429 if (pack->map == MAP_FAILED) {
1430 if (errno != ENOMEM) {
1431 err = got_error_from_errno("mmap");
1432 goto done;
1434 pack->map = NULL; /* fall back to read(2) */
1436 #endif
1437 done:
1438 if (err) {
1439 if (pack)
1440 got_pack_close(pack);
1441 } else if (packp)
1442 *packp = pack;
1443 return err;
1446 struct got_pack *
1447 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1449 struct got_pack *pack = NULL;
1450 size_t i;
1452 for (i = 0; i < repo->pack_cache_size; i++) {
1453 pack = &repo->packs[i];
1454 if (pack->path_packfile == NULL)
1455 break;
1456 if (strcmp(pack->path_packfile, path_packfile) == 0)
1457 return pack;
1460 return NULL;
1463 const struct got_error *
1464 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1465 struct got_pack *pack)
1467 size_t i;
1468 int pinned_pack = -1, pinned_packidx = -1;
1470 for (i = 0; i < repo->pack_cache_size; i++) {
1471 if (repo->packidx_cache[i] &&
1472 strcmp(repo->packidx_cache[i]->path_packidx,
1473 packidx->path_packidx) == 0)
1474 pinned_packidx = i;
1475 if (repo->packs[i].path_packfile &&
1476 strcmp(repo->packs[i].path_packfile,
1477 pack->path_packfile) == 0)
1478 pinned_pack = i;
1481 if (pinned_packidx == -1 || pinned_pack == -1)
1482 return got_error(GOT_ERR_PIN_PACK);
1484 repo->pinned_pack = pinned_pack;
1485 repo->pinned_packidx = pinned_packidx;
1486 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1487 return NULL;
1490 struct got_pack *
1491 got_repo_get_pinned_pack(struct got_repository *repo)
1493 if (repo->pinned_pack >= 0 &&
1494 repo->pinned_pack < repo->pack_cache_size)
1495 return &repo->packs[repo->pinned_pack];
1497 return NULL;
1500 void
1501 got_repo_unpin_pack(struct got_repository *repo)
1503 repo->pinned_packidx = -1;
1504 repo->pinned_pack = -1;
1505 repo->pinned_pid = 0;
1508 const struct got_error *
1509 got_repo_init(const char *repo_path, const char *head_name)
1511 const struct got_error *err = NULL;
1512 const char *dirnames[] = {
1513 GOT_OBJECTS_DIR,
1514 GOT_OBJECTS_PACK_DIR,
1515 GOT_REFS_DIR,
1517 const char *description_str = "Unnamed repository; "
1518 "edit this file 'description' to name the repository.";
1519 const char *headref = "ref: refs/heads/";
1520 const char *gitconfig_str = "[core]\n"
1521 "\trepositoryformatversion = 0\n"
1522 "\tfilemode = true\n"
1523 "\tbare = true\n";
1524 char *headref_str, *path;
1525 size_t i;
1527 if (!got_path_dir_is_empty(repo_path))
1528 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1530 for (i = 0; i < nitems(dirnames); i++) {
1531 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1532 return got_error_from_errno("asprintf");
1534 err = got_path_mkdir(path);
1535 free(path);
1536 if (err)
1537 return err;
1540 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1541 return got_error_from_errno("asprintf");
1542 err = got_path_create_file(path, description_str);
1543 free(path);
1544 if (err)
1545 return err;
1547 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1548 return got_error_from_errno("asprintf");
1549 if (asprintf(&headref_str, "%s%s", headref,
1550 head_name ? head_name : "main") == -1) {
1551 free(path);
1552 return got_error_from_errno("asprintf");
1554 err = got_path_create_file(path, headref_str);
1555 free(headref_str);
1556 free(path);
1557 if (err)
1558 return err;
1560 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1561 return got_error_from_errno("asprintf");
1562 err = got_path_create_file(path, gitconfig_str);
1563 free(path);
1564 if (err)
1565 return err;
1567 return NULL;
1570 static const struct got_error *
1571 match_packed_object(struct got_object_id **unique_id,
1572 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1574 const struct got_error *err = NULL;
1575 struct got_object_id_queue matched_ids;
1576 struct got_pathlist_entry *pe;
1578 STAILQ_INIT(&matched_ids);
1580 err = refresh_packidx_paths(repo);
1581 if (err)
1582 return err;
1584 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1585 const char *path_packidx = pe->path;
1586 struct got_packidx *packidx;
1587 struct got_object_qid *qid;
1589 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1590 path_packidx, 0);
1591 if (err)
1592 break;
1594 err = got_packidx_match_id_str_prefix(&matched_ids,
1595 packidx, id_str_prefix);
1596 if (err) {
1597 got_packidx_close(packidx);
1598 break;
1600 err = got_packidx_close(packidx);
1601 if (err)
1602 break;
1604 STAILQ_FOREACH(qid, &matched_ids, entry) {
1605 if (obj_type != GOT_OBJ_TYPE_ANY) {
1606 int matched_type;
1607 err = got_object_get_type(&matched_type, repo,
1608 &qid->id);
1609 if (err)
1610 goto done;
1611 if (matched_type != obj_type)
1612 continue;
1614 if (*unique_id == NULL) {
1615 *unique_id = got_object_id_dup(&qid->id);
1616 if (*unique_id == NULL) {
1617 err = got_error_from_errno("malloc");
1618 goto done;
1620 } else {
1621 if (got_object_id_cmp(*unique_id,
1622 &qid->id) == 0)
1623 continue; /* packed multiple times */
1624 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1625 goto done;
1629 done:
1630 got_object_id_queue_free(&matched_ids);
1631 if (err) {
1632 free(*unique_id);
1633 *unique_id = NULL;
1635 return err;
1638 static const struct got_error *
1639 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1640 const char *object_dir, const char *id_str_prefix, int obj_type,
1641 struct got_repository *repo)
1643 const struct got_error *err = NULL;
1644 char *path, *id_str = NULL;
1645 DIR *dir = NULL;
1646 struct dirent *dent;
1647 struct got_object_id id;
1649 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1650 err = got_error_from_errno("asprintf");
1651 goto done;
1654 dir = opendir(path);
1655 if (dir == NULL) {
1656 if (errno == ENOENT) {
1657 err = NULL;
1658 goto done;
1660 err = got_error_from_errno2("opendir", path);
1661 goto done;
1663 while ((dent = readdir(dir)) != NULL) {
1664 int cmp;
1666 free(id_str);
1667 id_str = NULL;
1669 if (strcmp(dent->d_name, ".") == 0 ||
1670 strcmp(dent->d_name, "..") == 0)
1671 continue;
1673 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1674 err = got_error_from_errno("asprintf");
1675 goto done;
1678 if (!got_parse_sha1_digest(id.sha1, id_str))
1679 continue;
1682 * Directory entries do not necessarily appear in
1683 * sorted order, so we must iterate over all of them.
1685 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1686 if (cmp != 0)
1687 continue;
1689 if (*unique_id == NULL) {
1690 if (obj_type != GOT_OBJ_TYPE_ANY) {
1691 int matched_type;
1692 err = got_object_get_type(&matched_type, repo,
1693 &id);
1694 if (err)
1695 goto done;
1696 if (matched_type != obj_type)
1697 continue;
1699 *unique_id = got_object_id_dup(&id);
1700 if (*unique_id == NULL) {
1701 err = got_error_from_errno("got_object_id_dup");
1702 goto done;
1704 } else {
1705 if (got_object_id_cmp(*unique_id, &id) == 0)
1706 continue; /* both packed and loose */
1707 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1708 goto done;
1711 done:
1712 if (dir && closedir(dir) != 0 && err == NULL)
1713 err = got_error_from_errno("closedir");
1714 if (err) {
1715 free(*unique_id);
1716 *unique_id = NULL;
1718 free(id_str);
1719 free(path);
1720 return err;
1723 const struct got_error *
1724 got_repo_match_object_id_prefix(struct got_object_id **id,
1725 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1727 const struct got_error *err = NULL;
1728 char *path_objects = NULL, *object_dir = NULL;
1729 size_t len;
1730 int i;
1732 *id = NULL;
1734 path_objects = got_repo_get_path_objects(repo);
1736 len = strlen(id_str_prefix);
1737 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1738 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1739 goto done;
1742 for (i = 0; i < len; i++) {
1743 if (isxdigit((unsigned char)id_str_prefix[i]))
1744 continue;
1745 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1746 goto done;
1749 if (len >= 2) {
1750 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1751 if (err)
1752 goto done;
1753 object_dir = strndup(id_str_prefix, 2);
1754 if (object_dir == NULL) {
1755 err = got_error_from_errno("strdup");
1756 goto done;
1758 err = match_loose_object(id, path_objects, object_dir,
1759 id_str_prefix, obj_type, repo);
1760 } else if (len == 1) {
1761 int i;
1762 for (i = 0; i < 0xf; i++) {
1763 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1764 == -1) {
1765 err = got_error_from_errno("asprintf");
1766 goto done;
1768 err = match_packed_object(id, repo, object_dir,
1769 obj_type);
1770 if (err)
1771 goto done;
1772 err = match_loose_object(id, path_objects, object_dir,
1773 id_str_prefix, obj_type, repo);
1774 if (err)
1775 goto done;
1777 } else {
1778 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1779 goto done;
1781 done:
1782 free(path_objects);
1783 free(object_dir);
1784 if (err) {
1785 free(*id);
1786 *id = NULL;
1787 } else if (*id == NULL) {
1788 switch (obj_type) {
1789 case GOT_OBJ_TYPE_BLOB:
1790 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1791 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1792 break;
1793 case GOT_OBJ_TYPE_TREE:
1794 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1795 GOT_OBJ_LABEL_TREE, id_str_prefix);
1796 break;
1797 case GOT_OBJ_TYPE_COMMIT:
1798 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1799 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1800 break;
1801 case GOT_OBJ_TYPE_TAG:
1802 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1803 GOT_OBJ_LABEL_TAG, id_str_prefix);
1804 break;
1805 default:
1806 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1807 break;
1811 return err;
1814 const struct got_error *
1815 got_repo_match_object_id(struct got_object_id **id, char **label,
1816 const char *id_str, int obj_type, struct got_reflist_head *refs,
1817 struct got_repository *repo)
1819 const struct got_error *err;
1820 struct got_tag_object *tag;
1821 struct got_reference *ref = NULL;
1823 *id = NULL;
1824 if (label)
1825 *label = NULL;
1827 if (refs) {
1828 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1829 refs, repo);
1830 if (err == NULL) {
1831 *id = got_object_id_dup(
1832 got_object_tag_get_object_id(tag));
1833 if (*id == NULL)
1834 err = got_error_from_errno("got_object_id_dup");
1835 else if (label && asprintf(label, "refs/tags/%s",
1836 got_object_tag_get_name(tag)) == -1) {
1837 err = got_error_from_errno("asprintf");
1838 free(*id);
1839 *id = NULL;
1841 got_object_tag_close(tag);
1842 return err;
1843 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1844 err->code != GOT_ERR_NO_OBJ)
1845 return err;
1848 err = got_ref_open(&ref, repo, id_str, 0);
1849 if (err == NULL) {
1850 err = got_ref_resolve(id, repo, ref);
1851 if (err)
1852 goto done;
1853 if (label) {
1854 *label = strdup(got_ref_get_name(ref));
1855 if (*label == NULL) {
1856 err = got_error_from_errno("strdup");
1857 goto done;
1860 } else {
1861 if (err->code != GOT_ERR_NOT_REF &&
1862 err->code != GOT_ERR_BAD_REF_NAME)
1863 goto done;
1864 err = got_repo_match_object_id_prefix(id, id_str,
1865 obj_type, repo);
1866 if (err) {
1867 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1868 err = got_error_not_ref(id_str);
1869 goto done;
1871 if (label) {
1872 err = got_object_id_str(label, *id);
1873 if (*label == NULL) {
1874 err = got_error_from_errno("strdup");
1875 goto done;
1879 done:
1880 if (ref)
1881 got_ref_close(ref);
1882 return err;
1885 const struct got_error *
1886 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1887 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1889 const struct got_error *err = NULL;
1890 struct got_reflist_entry *re;
1891 struct got_object_id *tag_id;
1892 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1894 *tag = NULL;
1896 TAILQ_FOREACH(re, refs, entry) {
1897 const char *refname;
1898 refname = got_ref_get_name(re->ref);
1899 if (got_ref_is_symbolic(re->ref))
1900 continue;
1901 if (strncmp(refname, "refs/tags/", 10) != 0)
1902 continue;
1903 if (!name_is_absolute)
1904 refname += strlen("refs/tags/");
1905 if (strcmp(refname, name) != 0)
1906 continue;
1907 err = got_ref_resolve(&tag_id, repo, re->ref);
1908 if (err)
1909 break;
1910 err = got_object_open_as_tag(tag, repo, tag_id);
1911 free(tag_id);
1912 if (err)
1913 break;
1914 if (obj_type == GOT_OBJ_TYPE_ANY ||
1915 got_object_tag_get_object_type(*tag) == obj_type)
1916 break;
1917 got_object_tag_close(*tag);
1918 *tag = NULL;
1921 if (err == NULL && *tag == NULL)
1922 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1923 GOT_OBJ_LABEL_TAG, name);
1924 return err;
1927 static const struct got_error *
1928 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1929 const char *name, mode_t mode, struct got_object_id *blob_id)
1931 const struct got_error *err = NULL;
1933 *new_te = NULL;
1935 *new_te = calloc(1, sizeof(**new_te));
1936 if (*new_te == NULL)
1937 return got_error_from_errno("calloc");
1939 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1940 sizeof((*new_te)->name)) {
1941 err = got_error(GOT_ERR_NO_SPACE);
1942 goto done;
1945 if (S_ISLNK(mode)) {
1946 (*new_te)->mode = S_IFLNK;
1947 } else {
1948 (*new_te)->mode = S_IFREG;
1949 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1951 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1952 done:
1953 if (err && *new_te) {
1954 free(*new_te);
1955 *new_te = NULL;
1957 return err;
1960 static const struct got_error *
1961 import_file(struct got_tree_entry **new_te, struct dirent *de,
1962 const char *path, struct got_repository *repo)
1964 const struct got_error *err;
1965 struct got_object_id *blob_id = NULL;
1966 char *filepath;
1967 struct stat sb;
1969 if (asprintf(&filepath, "%s%s%s", path,
1970 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1971 return got_error_from_errno("asprintf");
1973 if (lstat(filepath, &sb) != 0) {
1974 err = got_error_from_errno2("lstat", path);
1975 goto done;
1978 err = got_object_blob_create(&blob_id, filepath, repo);
1979 if (err)
1980 goto done;
1982 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1983 blob_id);
1984 done:
1985 free(filepath);
1986 if (err)
1987 free(blob_id);
1988 return err;
1991 static const struct got_error *
1992 insert_tree_entry(struct got_tree_entry *new_te,
1993 struct got_pathlist_head *paths)
1995 const struct got_error *err = NULL;
1996 struct got_pathlist_entry *new_pe;
1998 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1999 if (err)
2000 return err;
2001 if (new_pe == NULL)
2002 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2003 return NULL;
2006 static const struct got_error *write_tree(struct got_object_id **,
2007 const char *, struct got_pathlist_head *, struct got_repository *,
2008 got_repo_import_cb progress_cb, void *progress_arg);
2010 static const struct got_error *
2011 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2012 const char *path, struct got_pathlist_head *ignores,
2013 struct got_repository *repo,
2014 got_repo_import_cb progress_cb, void *progress_arg)
2016 const struct got_error *err;
2017 struct got_object_id *id = NULL;
2018 char *subdirpath;
2020 if (asprintf(&subdirpath, "%s%s%s", path,
2021 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2022 return got_error_from_errno("asprintf");
2024 (*new_te) = calloc(1, sizeof(**new_te));
2025 if (*new_te == NULL)
2026 return got_error_from_errno("calloc");
2027 (*new_te)->mode = S_IFDIR;
2028 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2029 sizeof((*new_te)->name)) {
2030 err = got_error(GOT_ERR_NO_SPACE);
2031 goto done;
2033 err = write_tree(&id, subdirpath, ignores, repo,
2034 progress_cb, progress_arg);
2035 if (err)
2036 goto done;
2037 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2039 done:
2040 free(id);
2041 free(subdirpath);
2042 if (err) {
2043 free(*new_te);
2044 *new_te = NULL;
2046 return err;
2049 static const struct got_error *
2050 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2051 struct got_pathlist_head *ignores, struct got_repository *repo,
2052 got_repo_import_cb progress_cb, void *progress_arg)
2054 const struct got_error *err = NULL;
2055 DIR *dir;
2056 struct dirent *de;
2057 int nentries;
2058 struct got_tree_entry *new_te = NULL;
2059 struct got_pathlist_head paths;
2060 struct got_pathlist_entry *pe;
2062 *new_tree_id = NULL;
2064 TAILQ_INIT(&paths);
2066 dir = opendir(path_dir);
2067 if (dir == NULL) {
2068 err = got_error_from_errno2("opendir", path_dir);
2069 goto done;
2072 nentries = 0;
2073 while ((de = readdir(dir)) != NULL) {
2074 int ignore = 0;
2075 int type;
2077 if (strcmp(de->d_name, ".") == 0 ||
2078 strcmp(de->d_name, "..") == 0)
2079 continue;
2081 TAILQ_FOREACH(pe, ignores, entry) {
2082 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2083 ignore = 1;
2084 break;
2087 if (ignore)
2088 continue;
2090 err = got_path_dirent_type(&type, path_dir, de);
2091 if (err)
2092 goto done;
2094 if (type == DT_DIR) {
2095 err = import_subdir(&new_te, de, path_dir,
2096 ignores, repo, progress_cb, progress_arg);
2097 if (err) {
2098 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2099 goto done;
2100 err = NULL;
2101 continue;
2103 } else if (type == DT_REG || type == DT_LNK) {
2104 err = import_file(&new_te, de, path_dir, repo);
2105 if (err)
2106 goto done;
2107 } else
2108 continue;
2110 err = insert_tree_entry(new_te, &paths);
2111 if (err)
2112 goto done;
2113 nentries++;
2116 if (TAILQ_EMPTY(&paths)) {
2117 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2118 "cannot create tree without any entries");
2119 goto done;
2122 TAILQ_FOREACH(pe, &paths, entry) {
2123 struct got_tree_entry *te = pe->data;
2124 char *path;
2125 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2126 continue;
2127 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2128 err = got_error_from_errno("asprintf");
2129 goto done;
2131 err = (*progress_cb)(progress_arg, path);
2132 free(path);
2133 if (err)
2134 goto done;
2137 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2138 done:
2139 if (dir)
2140 closedir(dir);
2141 got_pathlist_free(&paths);
2142 return err;
2145 const struct got_error *
2146 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2147 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2148 struct got_repository *repo, got_repo_import_cb progress_cb,
2149 void *progress_arg)
2151 const struct got_error *err;
2152 struct got_object_id *new_tree_id;
2154 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2155 progress_cb, progress_arg);
2156 if (err)
2157 return err;
2159 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2160 author, time(NULL), author, time(NULL), logmsg, repo);
2161 free(new_tree_id);
2162 return err;
2165 const struct got_error *
2166 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2167 struct got_repository *repo)
2169 const struct got_error *err = NULL;
2170 char *path_objects = NULL, *path = NULL;
2171 DIR *dir = NULL;
2172 struct got_object_id id;
2173 int i;
2175 *nobjects = 0;
2176 *ondisk_size = 0;
2178 path_objects = got_repo_get_path_objects(repo);
2179 if (path_objects == NULL)
2180 return got_error_from_errno("got_repo_get_path_objects");
2182 for (i = 0; i <= 0xff; i++) {
2183 struct dirent *dent;
2185 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2186 err = got_error_from_errno("asprintf");
2187 break;
2190 dir = opendir(path);
2191 if (dir == NULL) {
2192 if (errno == ENOENT) {
2193 err = NULL;
2194 continue;
2196 err = got_error_from_errno2("opendir", path);
2197 break;
2200 while ((dent = readdir(dir)) != NULL) {
2201 char *id_str;
2202 int fd;
2203 struct stat sb;
2205 if (strcmp(dent->d_name, ".") == 0 ||
2206 strcmp(dent->d_name, "..") == 0)
2207 continue;
2209 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2210 err = got_error_from_errno("asprintf");
2211 goto done;
2214 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2215 free(id_str);
2216 continue;
2218 free(id_str);
2220 err = got_object_open_loose_fd(&fd, &id, repo);
2221 if (err)
2222 goto done;
2224 if (fstat(fd, &sb) == -1) {
2225 err = got_error_from_errno("fstat");
2226 close(fd);
2227 goto done;
2229 (*nobjects)++;
2230 (*ondisk_size) += sb.st_size;
2232 if (close(fd) == -1) {
2233 err = got_error_from_errno("close");
2234 goto done;
2238 if (closedir(dir) != 0) {
2239 err = got_error_from_errno("closedir");
2240 goto done;
2242 dir = NULL;
2244 free(path);
2245 path = NULL;
2247 done:
2248 if (dir && closedir(dir) != 0 && err == NULL)
2249 err = got_error_from_errno("closedir");
2251 if (err) {
2252 *nobjects = 0;
2253 *ondisk_size = 0;
2255 free(path_objects);
2256 free(path);
2257 return err;
2260 const struct got_error *
2261 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2262 off_t *total_packsize, struct got_repository *repo)
2264 const struct got_error *err = NULL;
2265 DIR *packdir = NULL;
2266 struct dirent *dent;
2267 struct got_packidx *packidx = NULL;
2268 char *path_packidx;
2269 char *path_packfile;
2270 int packdir_fd;
2271 struct stat sb;
2273 *npackfiles = 0;
2274 *nobjects = 0;
2275 *total_packsize = 0;
2277 packdir_fd = openat(got_repo_get_fd(repo),
2278 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2279 if (packdir_fd == -1) {
2280 return got_error_from_errno_fmt("openat: %s/%s",
2281 got_repo_get_path_git_dir(repo),
2282 GOT_OBJECTS_PACK_DIR);
2285 packdir = fdopendir(packdir_fd);
2286 if (packdir == NULL) {
2287 err = got_error_from_errno("fdopendir");
2288 goto done;
2291 while ((dent = readdir(packdir)) != NULL) {
2292 if (!got_repo_is_packidx_filename(dent->d_name,
2293 strlen(dent->d_name)))
2294 continue;
2296 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2297 dent->d_name) == -1) {
2298 err = got_error_from_errno("asprintf");
2299 goto done;
2302 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2303 path_packidx, 0);
2304 free(path_packidx);
2305 if (err)
2306 goto done;
2308 if (fstat(packidx->fd, &sb) == -1)
2309 goto done;
2310 *total_packsize += sb.st_size;
2312 err = got_packidx_get_packfile_path(&path_packfile,
2313 packidx->path_packidx);
2314 if (err)
2315 goto done;
2317 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2318 0) == -1) {
2319 free(path_packfile);
2320 goto done;
2322 free(path_packfile);
2323 *total_packsize += sb.st_size;
2325 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2327 (*npackfiles)++;
2329 got_packidx_close(packidx);
2330 packidx = NULL;
2332 done:
2333 if (packidx)
2334 got_packidx_close(packidx);
2335 if (packdir && closedir(packdir) != 0 && err == NULL)
2336 err = got_error_from_errno("closedir");
2337 if (err) {
2338 *npackfiles = 0;
2339 *nobjects = 0;
2340 *total_packsize = 0;
2342 return err;
2345 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2346 got_packidx_bloom_filter_cmp);