Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <sha2.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <zlib.h>
40 #include <errno.h>
41 #include <libgen.h>
42 #include <stdint.h>
43 #include <imsg.h>
44 #include <uuid.h>
46 #include "bloom.h"
48 #include "got_error.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_path.h"
52 #include "got_cancel.h"
53 #include "got_object.h"
54 #include "got_opentemp.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_delta_cache.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_object.h"
60 #include "got_lib_object_parse.h"
61 #include "got_lib_object_create.h"
62 #include "got_lib_pack.h"
63 #include "got_lib_privsep.h"
64 #include "got_lib_hash.h"
65 #include "got_lib_object_cache.h"
66 #include "got_lib_repository.h"
67 #include "got_lib_gotconfig.h"
69 #ifndef nitems
70 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
71 #endif
73 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
75 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
76 got_packidx_bloom_filter_cmp);
78 static inline int
79 is_boolean_val(const char *val)
80 {
81 return (strcasecmp(val, "true") == 0 ||
82 strcasecmp(val, "false") == 0 ||
83 strcasecmp(val, "on") == 0 ||
84 strcasecmp(val, "off") == 0 ||
85 strcasecmp(val, "yes") == 0 ||
86 strcasecmp(val, "no") == 0 ||
87 strcasecmp(val, "1") == 0 ||
88 strcasecmp(val, "0") == 0);
89 }
91 static inline int
92 get_boolean_val(const char *val)
93 {
94 return (strcasecmp(val, "true") == 0 ||
95 strcasecmp(val, "on") == 0 ||
96 strcasecmp(val, "yes") == 0 ||
97 strcasecmp(val, "1") == 0);
98 }
100 const char *
101 got_repo_get_path(struct got_repository *repo)
103 return repo->path;
106 const char *
107 got_repo_get_path_git_dir(struct got_repository *repo)
109 return repo->path_git_dir;
112 int
113 got_repo_get_fd(struct got_repository *repo)
115 return repo->gitdir_fd;
118 enum got_hash_algorithm
119 got_repo_get_object_format(struct got_repository *repo)
121 return repo->algo;
124 const char *
125 got_repo_get_gitconfig_author_name(struct got_repository *repo)
127 return repo->gitconfig_author_name;
130 const char *
131 got_repo_get_gitconfig_author_email(struct got_repository *repo)
133 return repo->gitconfig_author_email;
136 const char *
137 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
139 return repo->global_gitconfig_author_name;
142 const char *
143 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
145 return repo->global_gitconfig_author_email;
148 const char *
149 got_repo_get_gitconfig_owner(struct got_repository *repo)
151 return repo->gitconfig_owner;
154 int
155 got_repo_has_extension(struct got_repository *repo, const char *ext)
157 int i;
159 for (i = 0; i < repo->nextensions; ++i) {
160 if (!strcasecmp(ext, repo->extnames[i]))
161 return get_boolean_val(repo->extvals[i]);
164 return 0;
167 int
168 got_repo_is_bare(struct got_repository *repo)
170 return (strcmp(repo->path, repo->path_git_dir) == 0);
173 static char *
174 get_path_git_child(struct got_repository *repo, const char *basename)
176 char *path_child;
178 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
179 basename) == -1)
180 return NULL;
182 return path_child;
185 char *
186 got_repo_get_path_objects(struct got_repository *repo)
188 return get_path_git_child(repo, GOT_OBJECTS_DIR);
191 char *
192 got_repo_get_path_objects_pack(struct got_repository *repo)
194 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
197 char *
198 got_repo_get_path_refs(struct got_repository *repo)
200 return get_path_git_child(repo, GOT_REFS_DIR);
203 char *
204 got_repo_get_path_packed_refs(struct got_repository *repo)
206 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
209 static char *
210 get_path_head(struct got_repository *repo)
212 return get_path_git_child(repo, GOT_HEAD_FILE);
215 char *
216 got_repo_get_path_gitconfig(struct got_repository *repo)
218 return get_path_git_child(repo, GOT_GITCONFIG);
221 char *
222 got_repo_get_path_gotconfig(struct got_repository *repo)
224 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
227 const struct got_gotconfig *
228 got_repo_get_gotconfig(struct got_repository *repo)
230 return repo->gotconfig;
233 void
234 got_repo_get_gitconfig_remotes(int *nremotes,
235 const struct got_remote_repo **remotes, struct got_repository *repo)
237 *nremotes = repo->ngitconfig_remotes;
238 *remotes = repo->gitconfig_remotes;
241 static int
242 is_git_repo(struct got_repository *repo)
244 const char *path_git = got_repo_get_path_git_dir(repo);
245 char *path_objects = got_repo_get_path_objects(repo);
246 char *path_refs = got_repo_get_path_refs(repo);
247 char *path_head = get_path_head(repo);
248 int ret = 0;
249 struct stat sb;
250 struct got_reference *head_ref;
252 if (lstat(path_git, &sb) == -1)
253 goto done;
254 if (!S_ISDIR(sb.st_mode))
255 goto done;
257 if (lstat(path_objects, &sb) == -1)
258 goto done;
259 if (!S_ISDIR(sb.st_mode))
260 goto done;
262 if (lstat(path_refs, &sb) == -1)
263 goto done;
264 if (!S_ISDIR(sb.st_mode))
265 goto done;
267 if (lstat(path_head, &sb) == -1)
268 goto done;
269 if (!S_ISREG(sb.st_mode))
270 goto done;
272 /* Check if the HEAD reference can be opened. */
273 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
274 goto done;
275 got_ref_close(head_ref);
277 ret = 1;
278 done:
279 free(path_objects);
280 free(path_refs);
281 free(path_head);
282 return ret;
286 static const struct got_error *
287 close_tempfiles(int *fds, size_t nfds)
289 const struct got_error *err = NULL;
290 int i;
292 for (i = 0; i < nfds; i++) {
293 if (fds[i] == -1)
294 continue;
295 if (close(fds[i]) == -1) {
296 err = got_error_from_errno("close");
297 break;
300 free(fds);
301 return err;
304 static const struct got_error *
305 open_tempfiles(int **fds, size_t array_size, size_t nfds)
307 const struct got_error *err = NULL;
308 int i;
310 *fds = calloc(array_size, sizeof(**fds));
311 if (*fds == NULL)
312 return got_error_from_errno("calloc");
314 for (i = 0; i < array_size; i++)
315 (*fds)[i] = -1;
317 for (i = 0; i < nfds; i++) {
318 (*fds)[i] = got_opentempfd();
319 if ((*fds)[i] == -1) {
320 err = got_error_from_errno("got_opentempfd");
321 close_tempfiles(*fds, nfds);
322 *fds = NULL;
323 return err;
327 return NULL;
330 static const struct got_error *
331 get_pack_cache_size(int *pack_cache_size)
333 struct rlimit rl;
335 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
336 return got_error_from_errno("getrlimit");
338 *pack_cache_size = GOT_PACK_CACHE_SIZE;
339 if (*pack_cache_size > rl.rlim_cur / 8)
340 *pack_cache_size = rl.rlim_cur / 8;
342 return NULL;
345 const struct got_error *
346 got_repo_pack_fds_open(int **pack_fds)
348 const struct got_error *err;
349 int nfds;
351 err = get_pack_cache_size(&nfds);
352 if (err)
353 return err;
355 /*
356 * We need one basefd and one accumfd per cached pack.
357 * Our constants should be set up in a way such that
358 * this error never triggers.
359 */
360 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
361 return got_error(GOT_ERR_NO_SPACE);
363 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
366 const struct got_error *
367 got_repo_pack_fds_close(int *pack_fds)
369 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
372 const struct got_error *
373 got_repo_temp_fds_open(int **temp_fds)
375 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
376 GOT_REPO_NUM_TEMPFILES);
379 void
380 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
382 int i;
384 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
385 repo->tempfiles[i] = temp_fds[i];
388 const struct got_error *
389 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
391 int i;
393 *fd = -1;
394 *idx = -1;
396 for (i = 0; i < nitems(repo->tempfiles); i++) {
397 if (repo->tempfile_use_mask & (1 << i))
398 continue;
399 if (repo->tempfiles[i] != -1) {
400 if (ftruncate(repo->tempfiles[i], 0L) == -1)
401 return got_error_from_errno("ftruncate");
402 *fd = repo->tempfiles[i];
403 *idx = i;
404 repo->tempfile_use_mask |= (1 << i);
405 return NULL;
409 return got_error(GOT_ERR_REPO_TEMPFILE);
412 void
413 got_repo_temp_fds_put(int idx, struct got_repository *repo)
415 repo->tempfile_use_mask &= ~(1 << idx);
418 const struct got_error *
419 got_repo_temp_fds_close(int *temp_fds)
421 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
424 const struct got_error *
425 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
426 struct got_object *obj)
428 #ifndef GOT_NO_OBJ_CACHE
429 const struct got_error *err = NULL;
430 err = got_object_cache_add(&repo->objcache, id, obj);
431 if (err) {
432 if (err->code == GOT_ERR_OBJ_EXISTS ||
433 err->code == GOT_ERR_OBJ_TOO_LARGE)
434 err = NULL;
435 return err;
437 obj->refcnt++;
438 #endif
439 return NULL;
442 struct got_object *
443 got_repo_get_cached_object(struct got_repository *repo,
444 struct got_object_id *id)
446 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
449 const struct got_error *
450 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
451 struct got_tree_object *tree)
453 #ifndef GOT_NO_OBJ_CACHE
454 const struct got_error *err = NULL;
455 err = got_object_cache_add(&repo->treecache, id, tree);
456 if (err) {
457 if (err->code == GOT_ERR_OBJ_EXISTS ||
458 err->code == GOT_ERR_OBJ_TOO_LARGE)
459 err = NULL;
460 return err;
462 tree->refcnt++;
463 #endif
464 return NULL;
467 struct got_tree_object *
468 got_repo_get_cached_tree(struct got_repository *repo,
469 struct got_object_id *id)
471 return (struct got_tree_object *)got_object_cache_get(
472 &repo->treecache, id);
475 const struct got_error *
476 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
477 struct got_commit_object *commit)
479 #ifndef GOT_NO_OBJ_CACHE
480 const struct got_error *err = NULL;
481 err = got_object_cache_add(&repo->commitcache, id, commit);
482 if (err) {
483 if (err->code == GOT_ERR_OBJ_EXISTS ||
484 err->code == GOT_ERR_OBJ_TOO_LARGE)
485 err = NULL;
486 return err;
488 commit->refcnt++;
489 #endif
490 return NULL;
493 struct got_commit_object *
494 got_repo_get_cached_commit(struct got_repository *repo,
495 struct got_object_id *id)
497 return (struct got_commit_object *)got_object_cache_get(
498 &repo->commitcache, id);
501 const struct got_error *
502 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
503 struct got_tag_object *tag)
505 #ifndef GOT_NO_OBJ_CACHE
506 const struct got_error *err = NULL;
507 err = got_object_cache_add(&repo->tagcache, id, tag);
508 if (err) {
509 if (err->code == GOT_ERR_OBJ_EXISTS ||
510 err->code == GOT_ERR_OBJ_TOO_LARGE)
511 err = NULL;
512 return err;
514 tag->refcnt++;
515 #endif
516 return NULL;
519 struct got_tag_object *
520 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
522 return (struct got_tag_object *)got_object_cache_get(
523 &repo->tagcache, id);
526 const struct got_error *
527 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
528 struct got_raw_object *raw)
530 #ifndef GOT_NO_OBJ_CACHE
531 const struct got_error *err = NULL;
532 err = got_object_cache_add(&repo->rawcache, id, raw);
533 if (err) {
534 if (err->code == GOT_ERR_OBJ_EXISTS ||
535 err->code == GOT_ERR_OBJ_TOO_LARGE)
536 err = NULL;
537 return err;
539 raw->refcnt++;
540 #endif
541 return NULL;
545 struct got_raw_object *
546 got_repo_get_cached_raw_object(struct got_repository *repo,
547 struct got_object_id *id)
549 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
553 static const struct got_error *
554 open_repo(struct got_repository *repo, const char *path)
556 const struct got_error *err = NULL;
558 repo->gitdir_fd = -1;
560 /* bare git repository? */
561 repo->path_git_dir = strdup(path);
562 if (repo->path_git_dir == NULL)
563 return got_error_from_errno("strdup");
564 if (is_git_repo(repo)) {
565 repo->path = strdup(repo->path_git_dir);
566 if (repo->path == NULL) {
567 err = got_error_from_errno("strdup");
568 goto done;
570 repo->gitdir_fd = open(repo->path_git_dir,
571 O_DIRECTORY | O_CLOEXEC);
572 if (repo->gitdir_fd == -1) {
573 err = got_error_from_errno2("open",
574 repo->path_git_dir);
575 goto done;
577 return NULL;
580 /* git repository with working tree? */
581 free(repo->path_git_dir);
582 repo->path_git_dir = NULL;
583 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
584 err = got_error_from_errno("asprintf");
585 goto done;
587 if (is_git_repo(repo)) {
588 repo->path = strdup(path);
589 if (repo->path == NULL) {
590 err = got_error_from_errno("strdup");
591 goto done;
593 repo->gitdir_fd = open(repo->path_git_dir,
594 O_DIRECTORY | O_CLOEXEC);
595 if (repo->gitdir_fd == -1) {
596 err = got_error_from_errno2("open",
597 repo->path_git_dir);
598 goto done;
600 return NULL;
603 err = got_error(GOT_ERR_NOT_GIT_REPO);
604 done:
605 if (err) {
606 free(repo->path);
607 repo->path = NULL;
608 free(repo->path_git_dir);
609 repo->path_git_dir = NULL;
610 if (repo->gitdir_fd != -1)
611 close(repo->gitdir_fd);
612 repo->gitdir_fd = -1;
615 return err;
618 static const struct got_error *
619 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
621 const struct got_error *err = NULL;
622 char *repo_gitconfig_path = NULL;
624 if (global_gitconfig_path) {
625 /* Read settings from ~/.gitconfig. */
626 int dummy_repo_version;
627 err = got_repo_read_gitconfig(&dummy_repo_version,
628 &repo->global_gitconfig_author_name,
629 &repo->global_gitconfig_author_email,
630 NULL, NULL, NULL, NULL, NULL, NULL,
631 global_gitconfig_path);
632 if (err)
633 return err;
636 /* Read repository's .git/config file. */
637 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
638 if (repo_gitconfig_path == NULL)
639 return got_error_from_errno("got_repo_get_path_gitconfig");
641 err = got_repo_read_gitconfig(
642 &repo->gitconfig_repository_format_version,
643 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
644 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
645 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
646 &repo->nextensions, repo_gitconfig_path);
647 if (err)
648 goto done;
650 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
651 int i;
653 for (i = 0; i < repo->ngitconfig_remotes; i++) {
654 got_repo_free_remote_repo_data(
655 &repo->gitconfig_remotes[i]);
657 free(repo->gitconfig_remotes);
658 repo->gitconfig_remotes = NULL;
659 repo->ngitconfig_remotes = 0;
661 free(repo->gitconfig_author_name);
662 repo->gitconfig_author_name = NULL;
663 free(repo->gitconfig_author_email);
664 repo->gitconfig_author_email = NULL;
666 free(repo->global_gitconfig_author_name);
667 repo->global_gitconfig_author_name = NULL;
668 free(repo->global_gitconfig_author_email);
669 repo->global_gitconfig_author_email = NULL;
672 done:
673 free(repo_gitconfig_path);
674 return err;
677 static const struct got_error *
678 read_gotconfig(struct got_repository *repo)
680 const struct got_error *err = NULL;
681 char *gotconfig_path;
683 gotconfig_path = got_repo_get_path_gotconfig(repo);
684 if (gotconfig_path == NULL)
685 return got_error_from_errno("got_repo_get_path_gotconfig");
687 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
688 free(gotconfig_path);
689 return err;
692 /* Supported repository format extensions. */
693 static const char *const repo_extensions[] = {
694 "noop", /* Got supports repository format version 1. */
695 "preciousObjects", /* Supported by gotadmin cleanup. */
696 "worktreeConfig", /* Got does not care about Git work trees. */
697 };
699 const struct got_error *
700 got_repo_open(struct got_repository **repop, const char *path,
701 const char *global_gitconfig_path, int *pack_fds)
703 struct got_repository *repo = NULL;
704 const struct got_error *err = NULL;
705 char *repo_path = NULL;
706 size_t i, j = 0;
708 *repop = NULL;
710 repo = calloc(1, sizeof(*repo));
711 if (repo == NULL)
712 return got_error_from_errno("calloc");
714 RB_INIT(&repo->packidx_bloom_filters);
715 TAILQ_INIT(&repo->packidx_paths);
717 for (i = 0; i < nitems(repo->privsep_children); i++) {
718 memset(&repo->privsep_children[i], 0,
719 sizeof(repo->privsep_children[0]));
720 repo->privsep_children[i].imsg_fd = -1;
723 err = got_object_cache_init(&repo->objcache,
724 GOT_OBJECT_CACHE_TYPE_OBJ);
725 if (err)
726 goto done;
727 err = got_object_cache_init(&repo->treecache,
728 GOT_OBJECT_CACHE_TYPE_TREE);
729 if (err)
730 goto done;
731 err = got_object_cache_init(&repo->commitcache,
732 GOT_OBJECT_CACHE_TYPE_COMMIT);
733 if (err)
734 goto done;
735 err = got_object_cache_init(&repo->tagcache,
736 GOT_OBJECT_CACHE_TYPE_TAG);
737 if (err)
738 goto done;
739 err = got_object_cache_init(&repo->rawcache,
740 GOT_OBJECT_CACHE_TYPE_RAW);
741 if (err)
742 goto done;
744 err = get_pack_cache_size(&repo->pack_cache_size);
745 if (err)
746 goto done;
747 for (i = 0; i < nitems(repo->packs); i++) {
748 if (pack_fds != NULL && i < repo->pack_cache_size) {
749 repo->packs[i].basefd = pack_fds[j++];
750 repo->packs[i].accumfd = pack_fds[j++];
751 } else {
752 repo->packs[i].basefd = -1;
753 repo->packs[i].accumfd = -1;
756 for (i = 0; i < nitems(repo->tempfiles); i++)
757 repo->tempfiles[i] = -1;
758 repo->pinned_pack = -1;
759 repo->pinned_packidx = -1;
760 repo->pinned_pid = 0;
762 repo_path = realpath(path, NULL);
763 if (repo_path == NULL) {
764 err = got_error_from_errno2("realpath", path);
765 goto done;
768 for (;;) {
769 char *parent_path;
771 err = open_repo(repo, repo_path);
772 if (err == NULL)
773 break;
774 if (err->code != GOT_ERR_NOT_GIT_REPO)
775 goto done;
776 if (repo_path[0] == '/' && repo_path[1] == '\0') {
777 err = got_error(GOT_ERR_NOT_GIT_REPO);
778 goto done;
780 err = got_path_dirname(&parent_path, repo_path);
781 if (err)
782 goto done;
783 free(repo_path);
784 repo_path = parent_path;
787 err = read_gotconfig(repo);
788 if (err)
789 goto done;
791 err = read_gitconfig(repo, global_gitconfig_path);
792 if (err)
793 goto done;
794 if (repo->gitconfig_repository_format_version != 0) {
795 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
796 goto done;
798 for (i = 0; i < repo->nextensions; i++) {
799 char *ext = repo->extnames[i];
800 char *val = repo->extvals[i];
801 int j, supported = 0;
803 if (!is_boolean_val(val)) {
804 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
805 goto done;
808 if (!get_boolean_val(val))
809 continue;
811 for (j = 0; j < nitems(repo_extensions); j++) {
812 if (strcmp(ext, repo_extensions[j]) == 0) {
813 supported = 1;
814 break;
817 if (!supported) {
818 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
819 goto done;
823 err = got_repo_list_packidx(&repo->packidx_paths, repo);
824 done:
825 if (err)
826 got_repo_close(repo);
827 else
828 *repop = repo;
829 free(repo_path);
830 return err;
833 const struct got_error *
834 got_repo_close(struct got_repository *repo)
836 const struct got_error *err = NULL, *child_err;
837 struct got_packidx_bloom_filter *bf;
838 size_t i;
840 for (i = 0; i < repo->pack_cache_size; i++) {
841 if (repo->packidx_cache[i] == NULL)
842 break;
843 got_packidx_close(repo->packidx_cache[i]);
846 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
847 &repo->packidx_bloom_filters))) {
848 RB_REMOVE(got_packidx_bloom_filter_tree,
849 &repo->packidx_bloom_filters, bf);
850 bloom_free(bf->bloom);
851 free(bf->bloom);
852 free(bf);
855 for (i = 0; i < repo->pack_cache_size; i++)
856 if (repo->packs[i].path_packfile)
857 if (repo->packs[i].path_packfile)
858 got_pack_close(&repo->packs[i]);
860 free(repo->path);
861 free(repo->path_git_dir);
863 got_object_cache_close(&repo->objcache);
864 got_object_cache_close(&repo->treecache);
865 got_object_cache_close(&repo->commitcache);
866 got_object_cache_close(&repo->tagcache);
867 got_object_cache_close(&repo->rawcache);
869 for (i = 0; i < nitems(repo->privsep_children); i++) {
870 if (repo->privsep_children[i].imsg_fd == -1)
871 continue;
872 imsg_clear(repo->privsep_children[i].ibuf);
873 free(repo->privsep_children[i].ibuf);
874 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
875 if (err && err->code == GOT_ERR_EOF)
876 err = NULL;
877 child_err = got_privsep_wait_for_child(
878 repo->privsep_children[i].pid);
879 if (child_err && err == NULL)
880 err = child_err;
881 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
882 err == NULL)
883 err = got_error_from_errno("close");
886 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
887 err == NULL)
888 err = got_error_from_errno("close");
890 if (repo->gotconfig)
891 got_gotconfig_free(repo->gotconfig);
892 free(repo->gitconfig_author_name);
893 free(repo->gitconfig_author_email);
894 for (i = 0; i < repo->ngitconfig_remotes; i++)
895 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
896 free(repo->gitconfig_remotes);
897 for (i = 0; i < repo->nextensions; i++) {
898 free(repo->extnames[i]);
899 free(repo->extvals[i]);
901 free(repo->extnames);
902 free(repo->extvals);
904 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
905 free(repo);
907 return err;
910 const struct got_error *
911 got_repo_remote_repo_dup(struct got_remote_repo **newp,
912 const struct got_remote_repo *repo)
914 const struct got_error *err = NULL;
915 struct got_remote_repo *new;
916 int i;
918 new = calloc(1, sizeof(*new));
919 if (new == NULL)
920 return got_error_from_errno("calloc");
922 if (repo->name) {
923 new->name = strdup(repo->name);
924 if (new->name == NULL) {
925 err = got_error_from_errno("strdup");
926 goto done;
930 if (repo->fetch_url) {
931 new->fetch_url = strdup(repo->fetch_url);
932 if (new->fetch_url == NULL) {
933 err = got_error_from_errno("strdup");
934 goto done;
938 if (repo->send_url) {
939 new->send_url = strdup(repo->send_url);
940 if (new->send_url == NULL) {
941 err = got_error_from_errno("strdup");
942 goto done;
946 new->mirror_references = repo->mirror_references;
948 new->fetch_all_branches = repo->fetch_all_branches;
950 new->nfetch_branches = repo->nfetch_branches;
951 if (repo->fetch_branches) {
952 new->fetch_branches = calloc(repo->nfetch_branches,
953 sizeof(char *));
954 if (new->fetch_branches == NULL) {
955 err = got_error_from_errno("calloc");
956 goto done;
958 for (i = 0; i < repo->nfetch_branches; i++) {
959 new->fetch_branches[i] = strdup(
960 repo->fetch_branches[i]);
961 if (new->fetch_branches[i] == NULL) {
962 err = got_error_from_errno("strdup");
963 goto done;
968 new->nsend_branches = repo->nsend_branches;
969 if (repo->send_branches) {
970 new->send_branches = calloc(repo->nsend_branches,
971 sizeof(char *));
972 if (new->send_branches == NULL) {
973 err = got_error_from_errno("calloc");
974 goto done;
976 for (i = 0; i < repo->nsend_branches; i++) {
977 new->send_branches[i] = strdup(
978 repo->send_branches[i]);
979 if (new->send_branches[i] == NULL) {
980 err = got_error_from_errno("strdup");
981 goto done;
986 new->nfetch_refs = repo->nfetch_refs;
987 if (repo->fetch_refs) {
988 new->fetch_refs = calloc(repo->nfetch_refs,
989 sizeof(char *));
990 if (new->fetch_refs == NULL) {
991 err = got_error_from_errno("calloc");
992 goto done;
994 for (i = 0; i < repo->nfetch_refs; i++) {
995 new->fetch_refs[i] = strdup(
996 repo->fetch_refs[i]);
997 if (new->fetch_refs[i] == NULL) {
998 err = got_error_from_errno("strdup");
999 goto done;
1003 done:
1004 if (err) {
1005 got_repo_free_remote_repo_data(new);
1006 free(new);
1007 } else
1008 *newp = new;
1010 return err;
1013 void
1014 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
1016 int i;
1018 if (repo == NULL)
1019 return;
1021 free(repo->name);
1022 repo->name = NULL;
1023 free(repo->fetch_url);
1024 repo->fetch_url = NULL;
1025 free(repo->send_url);
1026 repo->send_url = NULL;
1027 for (i = 0; i < repo->nfetch_branches; i++)
1028 free(repo->fetch_branches[i]);
1029 free(repo->fetch_branches);
1030 repo->fetch_branches = NULL;
1031 repo->nfetch_branches = 0;
1032 for (i = 0; i < repo->nsend_branches; i++)
1033 free(repo->send_branches[i]);
1034 free(repo->send_branches);
1035 repo->send_branches = NULL;
1036 repo->nsend_branches = 0;
1037 for (i = 0; i < repo->nfetch_refs; i++)
1038 free(repo->fetch_refs[i]);
1039 free(repo->fetch_refs);
1040 repo->fetch_refs = NULL;
1041 repo->nfetch_refs = 0;
1044 const struct got_error *
1045 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
1046 const char *input_path)
1048 const struct got_error *err = NULL;
1049 const char *repo_abspath = NULL;
1050 size_t repolen, len;
1051 char *canonpath, *path = NULL;
1053 *in_repo_path = NULL;
1055 canonpath = strdup(input_path);
1056 if (canonpath == NULL) {
1057 err = got_error_from_errno("strdup");
1058 goto done;
1060 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
1061 if (err)
1062 goto done;
1064 repo_abspath = got_repo_get_path(repo);
1066 if (canonpath[0] == '\0') {
1067 path = strdup(canonpath);
1068 if (path == NULL) {
1069 err = got_error_from_errno("strdup");
1070 goto done;
1072 } else {
1073 path = realpath(canonpath, NULL);
1074 if (path == NULL) {
1075 if (errno != ENOENT) {
1076 err = got_error_from_errno2("realpath",
1077 canonpath);
1078 goto done;
1081 * Path is not on disk.
1082 * Assume it is already relative to repository root.
1084 path = strdup(canonpath);
1085 if (path == NULL) {
1086 err = got_error_from_errno("strdup");
1087 goto done;
1091 repolen = strlen(repo_abspath);
1092 len = strlen(path);
1095 if (strcmp(path, repo_abspath) == 0) {
1096 free(path);
1097 path = strdup("");
1098 if (path == NULL) {
1099 err = got_error_from_errno("strdup");
1100 goto done;
1102 } else if (len > repolen &&
1103 got_path_is_child(path, repo_abspath, repolen)) {
1104 /* Matched an on-disk path inside repository. */
1105 if (got_repo_is_bare(repo)) {
1107 * Matched an on-disk path inside repository
1108 * database. Treat input as repository-relative.
1110 free(path);
1111 path = canonpath;
1112 canonpath = NULL;
1113 } else {
1114 char *child;
1115 /* Strip common prefix with repository path. */
1116 err = got_path_skip_common_ancestor(&child,
1117 repo_abspath, path);
1118 if (err)
1119 goto done;
1120 free(path);
1121 path = child;
1123 } else {
1125 * Matched unrelated on-disk path.
1126 * Treat input as repository-relative.
1128 free(path);
1129 path = canonpath;
1130 canonpath = NULL;
1134 /* Make in-repository path absolute */
1135 if (path[0] != '/') {
1136 char *abspath;
1137 if (asprintf(&abspath, "/%s", path) == -1) {
1138 err = got_error_from_errno("asprintf");
1139 goto done;
1141 free(path);
1142 path = abspath;
1145 done:
1146 free(canonpath);
1147 if (err)
1148 free(path);
1149 else
1150 *in_repo_path = path;
1151 return err;
1154 static const struct got_error *
1155 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1156 const char *path_packidx)
1158 const struct got_error *err = NULL;
1159 size_t i;
1161 for (i = 0; i < repo->pack_cache_size; i++) {
1162 if (repo->packidx_cache[i] == NULL)
1163 break;
1164 if (strcmp(repo->packidx_cache[i]->path_packidx,
1165 path_packidx) == 0) {
1166 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1169 if (i == repo->pack_cache_size) {
1170 do {
1171 i--;
1172 } while (i > 0 && repo->pinned_packidx >= 0 &&
1173 i == repo->pinned_packidx);
1174 err = got_packidx_close(repo->packidx_cache[i]);
1175 if (err)
1176 return err;
1179 repo->packidx_cache[i] = packidx;
1181 return NULL;
1184 int
1185 got_repo_is_packidx_filename(const char *name, size_t len)
1187 if (len != GOT_PACKIDX_NAMELEN)
1188 return 0;
1190 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1191 return 0;
1193 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1194 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1195 return 0;
1197 return 1;
1200 static struct got_packidx_bloom_filter *
1201 get_packidx_bloom_filter(struct got_repository *repo,
1202 const char *path, size_t path_len)
1204 struct got_packidx_bloom_filter key;
1206 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1207 return NULL; /* XXX */
1208 key.path_len = path_len;
1210 return RB_FIND(got_packidx_bloom_filter_tree,
1211 &repo->packidx_bloom_filters, &key);
1214 int
1215 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1216 const char *path_packidx, struct got_object_id *id)
1218 struct got_packidx_bloom_filter *bf;
1220 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1221 if (bf)
1222 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1224 /* No bloom filter means this pack index must be searched. */
1225 return 1;
1228 static const struct got_error *
1229 add_packidx_bloom_filter(struct got_repository *repo,
1230 struct got_packidx *packidx, const char *path_packidx)
1232 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1233 struct got_packidx_bloom_filter *bf;
1234 size_t len;
1237 * Don't use bloom filters for very large pack index files.
1238 * Large pack files will contain a relatively large fraction
1239 * of our objects so we will likely need to visit them anyway.
1240 * The more objects a pack file contains the higher the probability
1241 * of a false-positive match from the bloom filter. And reading
1242 * all object IDs from a large pack index file can be expensive.
1244 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1245 return NULL;
1247 /* Do we already have a filter for this pack index? */
1248 if (get_packidx_bloom_filter(repo, path_packidx,
1249 strlen(path_packidx)) != NULL)
1250 return NULL;
1252 bf = calloc(1, sizeof(*bf));
1253 if (bf == NULL)
1254 return got_error_from_errno("calloc");
1255 bf->bloom = calloc(1, sizeof(*bf->bloom));
1256 if (bf->bloom == NULL) {
1257 free(bf);
1258 return got_error_from_errno("calloc");
1261 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1262 if (len >= sizeof(bf->path)) {
1263 free(bf->bloom);
1264 free(bf);
1265 return got_error(GOT_ERR_NO_SPACE);
1267 bf->path_len = len;
1269 /* Minimum size supported by our bloom filter is 1000 entries. */
1270 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1271 for (i = 0; i < nobjects; i++) {
1272 struct got_packidx_object_id *id;
1273 id = &packidx->hdr.sorted_ids[i];
1274 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1277 RB_INSERT(got_packidx_bloom_filter_tree,
1278 &repo->packidx_bloom_filters, bf);
1279 return NULL;
1282 static void
1283 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1285 struct got_pathlist_entry *pe;
1287 while (!TAILQ_EMPTY(packidx_paths)) {
1288 pe = TAILQ_FIRST(packidx_paths);
1289 TAILQ_REMOVE(packidx_paths, pe, entry);
1290 free((char *)pe->path);
1291 free(pe);
1295 static const struct got_error *
1296 refresh_packidx_paths(struct got_repository *repo)
1298 const struct got_error *err = NULL;
1299 char *objects_pack_dir = NULL;
1300 struct stat sb;
1302 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1303 if (objects_pack_dir == NULL)
1304 return got_error_from_errno("got_repo_get_path_objects_pack");
1306 if (stat(objects_pack_dir, &sb) == -1) {
1307 if (errno != ENOENT) {
1308 err = got_error_from_errno2("stat", objects_pack_dir);
1309 goto done;
1311 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1312 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1313 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1314 purge_packidx_paths(&repo->packidx_paths);
1315 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1316 if (err)
1317 goto done;
1319 done:
1320 free(objects_pack_dir);
1321 return err;
1324 const struct got_error *
1325 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1326 struct got_repository *repo, struct got_object_id *id)
1328 const struct got_error *err;
1329 struct got_pathlist_entry *pe;
1330 size_t i;
1332 /* Search pack index cache. */
1333 for (i = 0; i < repo->pack_cache_size; i++) {
1334 if (repo->packidx_cache[i] == NULL)
1335 break;
1336 if (!got_repo_check_packidx_bloom_filter(repo,
1337 repo->packidx_cache[i]->path_packidx, id))
1338 continue; /* object will not be found in this index */
1339 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1340 if (*idx != -1) {
1341 *packidx = repo->packidx_cache[i];
1343 * Move this cache entry to the front. Repeatedly
1344 * searching a wrong pack index can be expensive.
1346 if (i > 0) {
1347 memmove(&repo->packidx_cache[1],
1348 &repo->packidx_cache[0],
1349 i * sizeof(repo->packidx_cache[0]));
1350 repo->packidx_cache[0] = *packidx;
1351 if (repo->pinned_packidx >= 0 &&
1352 repo->pinned_packidx < i)
1353 repo->pinned_packidx++;
1354 else if (repo->pinned_packidx == i)
1355 repo->pinned_packidx = 0;
1357 return NULL;
1360 /* No luck. Search the filesystem. */
1362 err = refresh_packidx_paths(repo);
1363 if (err)
1364 return err;
1366 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1367 const char *path_packidx = pe->path;
1368 int is_cached = 0;
1370 if (!got_repo_check_packidx_bloom_filter(repo,
1371 pe->path, id))
1372 continue; /* object will not be found in this index */
1374 for (i = 0; i < repo->pack_cache_size; i++) {
1375 if (repo->packidx_cache[i] == NULL)
1376 break;
1377 if (strcmp(repo->packidx_cache[i]->path_packidx,
1378 path_packidx) == 0) {
1379 is_cached = 1;
1380 break;
1383 if (is_cached)
1384 continue; /* already searched */
1386 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1387 path_packidx, 0);
1388 if (err)
1389 goto done;
1391 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1392 if (err)
1393 goto done;
1395 err = cache_packidx(repo, *packidx, path_packidx);
1396 if (err)
1397 goto done;
1399 *idx = got_packidx_get_object_idx(*packidx, id);
1400 if (*idx != -1) {
1401 err = NULL; /* found the object */
1402 goto done;
1406 err = got_error_no_obj(id);
1407 done:
1408 return err;
1411 const struct got_error *
1412 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1413 struct got_repository *repo)
1415 const struct got_error *err = NULL;
1416 DIR *packdir = NULL;
1417 struct dirent *dent;
1418 char *path_packidx = NULL;
1419 int packdir_fd;
1420 struct stat sb;
1422 packdir_fd = openat(got_repo_get_fd(repo),
1423 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1424 if (packdir_fd == -1) {
1425 return got_error_from_errno_fmt("openat: %s/%s",
1426 got_repo_get_path_git_dir(repo),
1427 GOT_OBJECTS_PACK_DIR);
1430 packdir = fdopendir(packdir_fd);
1431 if (packdir == NULL) {
1432 err = got_error_from_errno("fdopendir");
1433 goto done;
1436 if (fstat(packdir_fd, &sb) == -1) {
1437 err = got_error_from_errno("fstat");
1438 goto done;
1440 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1441 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1443 while ((dent = readdir(packdir)) != NULL) {
1444 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1445 continue;
1447 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1448 dent->d_name) == -1) {
1449 err = got_error_from_errno("asprintf");
1450 path_packidx = NULL;
1451 break;
1454 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1455 if (err)
1456 break;
1458 done:
1459 if (err)
1460 free(path_packidx);
1461 if (packdir && closedir(packdir) != 0 && err == NULL)
1462 err = got_error_from_errno("closedir");
1463 return err;
1466 const struct got_error *
1467 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1468 struct got_repository *repo)
1470 const struct got_error *err;
1471 size_t i;
1473 *packidx = NULL;
1475 /* Search pack index cache. */
1476 for (i = 0; i < repo->pack_cache_size; i++) {
1477 if (repo->packidx_cache[i] == NULL)
1478 break;
1479 if (strcmp(repo->packidx_cache[i]->path_packidx,
1480 path_packidx) == 0) {
1481 *packidx = repo->packidx_cache[i];
1482 return NULL;
1485 /* No luck. Search the filesystem. */
1487 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1488 path_packidx, 0);
1489 if (err)
1490 return err;
1492 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1493 if (err)
1494 goto done;
1496 err = cache_packidx(repo, *packidx, path_packidx);
1497 done:
1498 if (err) {
1499 got_packidx_close(*packidx);
1500 *packidx = NULL;
1502 return err;
1505 static const struct got_error *
1506 read_packfile_hdr(int fd, struct got_packidx *packidx)
1508 const struct got_error *err = NULL;
1509 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1510 struct got_packfile_hdr hdr;
1511 ssize_t n;
1513 n = read(fd, &hdr, sizeof(hdr));
1514 if (n < 0)
1515 return got_error_from_errno("read");
1516 if (n != sizeof(hdr))
1517 return got_error(GOT_ERR_BAD_PACKFILE);
1519 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1520 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1521 be32toh(hdr.nobjects) != totobj)
1522 err = got_error(GOT_ERR_BAD_PACKFILE);
1524 return err;
1527 static const struct got_error *
1528 open_packfile(int *fd, struct got_repository *repo,
1529 const char *relpath, struct got_packidx *packidx)
1531 const struct got_error *err = NULL;
1533 *fd = openat(got_repo_get_fd(repo), relpath,
1534 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1535 if (*fd == -1)
1536 return got_error_from_errno_fmt("openat: %s/%s",
1537 got_repo_get_path_git_dir(repo), relpath);
1539 if (packidx) {
1540 err = read_packfile_hdr(*fd, packidx);
1541 if (err) {
1542 close(*fd);
1543 *fd = -1;
1547 return err;
1550 const struct got_error *
1551 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1552 const char *path_packfile, struct got_packidx *packidx)
1554 const struct got_error *err = NULL;
1555 struct got_pack *pack = NULL;
1556 struct stat sb;
1557 size_t i;
1559 if (packp)
1560 *packp = NULL;
1562 for (i = 0; i < repo->pack_cache_size; i++) {
1563 pack = &repo->packs[i];
1564 if (pack->path_packfile == NULL)
1565 break;
1566 if (strcmp(pack->path_packfile, path_packfile) == 0)
1567 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1570 if (i == repo->pack_cache_size) {
1571 struct got_pack tmp;
1572 do {
1573 i--;
1574 } while (i > 0 && repo->pinned_pack >= 0 &&
1575 i == repo->pinned_pack);
1576 err = got_pack_close(&repo->packs[i]);
1577 if (err)
1578 return err;
1579 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1580 return got_error_from_errno("ftruncate");
1581 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1582 return got_error_from_errno("ftruncate");
1583 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1584 memcpy(&repo->packs[i], &repo->packs[0],
1585 sizeof(repo->packs[i]));
1586 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1587 if (repo->pinned_pack == 0)
1588 repo->pinned_pack = i;
1589 else if (repo->pinned_pack == i)
1590 repo->pinned_pack = 0;
1591 i = 0;
1594 pack = &repo->packs[i];
1596 pack->path_packfile = strdup(path_packfile);
1597 if (pack->path_packfile == NULL) {
1598 err = got_error_from_errno("strdup");
1599 goto done;
1602 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1603 if (err)
1604 goto done;
1606 if (fstat(pack->fd, &sb) != 0) {
1607 err = got_error_from_errno("fstat");
1608 goto done;
1610 pack->filesize = sb.st_size;
1612 pack->privsep_child = NULL;
1614 err = got_delta_cache_alloc(&pack->delta_cache);
1615 if (err)
1616 goto done;
1618 #ifndef GOT_PACK_NO_MMAP
1619 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1620 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1621 pack->fd, 0);
1622 if (pack->map == MAP_FAILED) {
1623 if (errno != ENOMEM) {
1624 err = got_error_from_errno("mmap");
1625 goto done;
1627 pack->map = NULL; /* fall back to read(2) */
1630 #endif
1631 done:
1632 if (err) {
1633 if (pack)
1634 got_pack_close(pack);
1635 } else if (packp)
1636 *packp = pack;
1637 return err;
1640 struct got_pack *
1641 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1643 struct got_pack *pack = NULL;
1644 size_t i;
1646 for (i = 0; i < repo->pack_cache_size; i++) {
1647 pack = &repo->packs[i];
1648 if (pack->path_packfile == NULL)
1649 break;
1650 if (strcmp(pack->path_packfile, path_packfile) == 0)
1651 return pack;
1654 return NULL;
1657 const struct got_error *
1658 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1659 struct got_pack *pack)
1661 size_t i;
1662 int pinned_pack = -1, pinned_packidx = -1;
1664 for (i = 0; i < repo->pack_cache_size; i++) {
1665 if (repo->packidx_cache[i] &&
1666 strcmp(repo->packidx_cache[i]->path_packidx,
1667 packidx->path_packidx) == 0)
1668 pinned_packidx = i;
1669 if (repo->packs[i].path_packfile &&
1670 strcmp(repo->packs[i].path_packfile,
1671 pack->path_packfile) == 0)
1672 pinned_pack = i;
1675 if (pinned_packidx == -1 || pinned_pack == -1)
1676 return got_error(GOT_ERR_PIN_PACK);
1678 repo->pinned_pack = pinned_pack;
1679 repo->pinned_packidx = pinned_packidx;
1680 if (repo->packs[pinned_pack].privsep_child)
1681 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1682 return NULL;
1685 struct got_pack *
1686 got_repo_get_pinned_pack(struct got_repository *repo)
1688 if (repo->pinned_pack >= 0 &&
1689 repo->pinned_pack < repo->pack_cache_size)
1690 return &repo->packs[repo->pinned_pack];
1692 return NULL;
1695 void
1696 got_repo_unpin_pack(struct got_repository *repo)
1698 repo->pinned_packidx = -1;
1699 repo->pinned_pack = -1;
1700 repo->pinned_pid = 0;
1703 const struct got_error *
1704 got_repo_init(const char *repo_path, const char *head_name)
1706 const struct got_error *err = NULL;
1707 const char *dirnames[] = {
1708 GOT_OBJECTS_DIR,
1709 GOT_OBJECTS_PACK_DIR,
1710 GOT_REFS_DIR,
1712 const char *description_str = "Unnamed repository; "
1713 "edit this file 'description' to name the repository.";
1714 const char *headref = "ref: refs/heads/";
1715 const char *gitconfig_str = "[core]\n"
1716 "\trepositoryformatversion = 0\n"
1717 "\tfilemode = true\n"
1718 "\tbare = true\n";
1719 char *headref_str, *path;
1720 size_t i;
1722 if (!got_path_dir_is_empty(repo_path))
1723 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1725 for (i = 0; i < nitems(dirnames); i++) {
1726 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1727 return got_error_from_errno("asprintf");
1729 err = got_path_mkdir(path);
1730 free(path);
1731 if (err)
1732 return err;
1735 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1736 return got_error_from_errno("asprintf");
1737 err = got_path_create_file(path, description_str);
1738 free(path);
1739 if (err)
1740 return err;
1742 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1743 return got_error_from_errno("asprintf");
1744 if (asprintf(&headref_str, "%s%s", headref,
1745 head_name ? head_name : "main") == -1) {
1746 free(path);
1747 return got_error_from_errno("asprintf");
1749 err = got_path_create_file(path, headref_str);
1750 free(headref_str);
1751 free(path);
1752 if (err)
1753 return err;
1755 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1756 return got_error_from_errno("asprintf");
1757 err = got_path_create_file(path, gitconfig_str);
1758 free(path);
1759 if (err)
1760 return err;
1762 return NULL;
1765 static const struct got_error *
1766 match_packed_object(struct got_object_id **unique_id,
1767 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1769 const struct got_error *err = NULL;
1770 struct got_object_id_queue matched_ids;
1771 struct got_pathlist_entry *pe;
1772 struct timespec tv;
1773 int retries = 0;
1774 const int max_retries = 10;
1776 STAILQ_INIT(&matched_ids);
1778 err = refresh_packidx_paths(repo);
1779 if (err)
1780 return err;
1783 * Opening objects while iterating over the pack-index path
1784 * list is racy. If the set of pack files in the repository
1785 * changes during loop iteration, refresh_packidx_paths() will
1786 * be called again, via got_object_get_type(), invalidating
1787 * the packidx_paths list we are iterating over.
1788 * To work around this we keep track of the current modification
1789 * time and retry the entire loop if it changes.
1791 retry:
1792 tv.tv_sec = repo->pack_path_mtime.tv_sec;
1793 tv.tv_nsec = repo->pack_path_mtime.tv_nsec;
1795 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1796 const char *path_packidx;
1797 struct got_packidx *packidx;
1798 struct got_object_qid *qid;
1801 * If the modification time of the 'objects/pack' directory
1802 * has changed then 'pe' could now be an invalid pointer.
1804 if (tv.tv_sec != repo->pack_path_mtime.tv_sec ||
1805 tv.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1806 if (++retries > max_retries) {
1807 err = got_error_msg(GOT_ERR_TIMEOUT,
1808 "too many concurrent pack file "
1809 "modifications");
1810 goto done;
1812 goto retry;
1815 path_packidx = pe->path;
1817 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1818 path_packidx, 0);
1819 if (err)
1820 break;
1822 got_object_id_queue_free(&matched_ids);
1824 err = got_packidx_match_id_str_prefix(&matched_ids,
1825 packidx, id_str_prefix);
1826 if (err) {
1827 got_packidx_close(packidx);
1828 break;
1830 err = got_packidx_close(packidx);
1831 if (err)
1832 break;
1834 STAILQ_FOREACH(qid, &matched_ids, entry) {
1835 if (obj_type != GOT_OBJ_TYPE_ANY) {
1836 int matched_type;
1837 err = got_object_get_type(&matched_type, repo,
1838 &qid->id);
1839 if (err)
1840 goto done;
1841 if (matched_type != obj_type)
1842 continue;
1844 if (*unique_id == NULL) {
1845 *unique_id = got_object_id_dup(&qid->id);
1846 if (*unique_id == NULL) {
1847 err = got_error_from_errno("malloc");
1848 goto done;
1850 } else {
1851 if (got_object_id_cmp(*unique_id,
1852 &qid->id) == 0)
1853 continue; /* packed multiple times */
1854 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1855 goto done;
1859 done:
1860 got_object_id_queue_free(&matched_ids);
1861 if (err) {
1862 free(*unique_id);
1863 *unique_id = NULL;
1865 return err;
1868 static const struct got_error *
1869 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1870 const char *object_dir, const char *id_str_prefix, int obj_type,
1871 struct got_repository *repo)
1873 const struct got_error *err = NULL;
1874 char *path, *id_str = NULL;
1875 DIR *dir = NULL;
1876 struct dirent *dent;
1877 struct got_object_id id;
1879 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1880 err = got_error_from_errno("asprintf");
1881 goto done;
1884 dir = opendir(path);
1885 if (dir == NULL) {
1886 if (errno == ENOENT) {
1887 err = NULL;
1888 goto done;
1890 err = got_error_from_errno2("opendir", path);
1891 goto done;
1893 while ((dent = readdir(dir)) != NULL) {
1894 int cmp;
1896 free(id_str);
1897 id_str = NULL;
1899 if (strcmp(dent->d_name, ".") == 0 ||
1900 strcmp(dent->d_name, "..") == 0)
1901 continue;
1903 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1904 err = got_error_from_errno("asprintf");
1905 goto done;
1908 if (!got_parse_object_id(&id, id_str, repo->algo))
1909 continue;
1912 * Directory entries do not necessarily appear in
1913 * sorted order, so we must iterate over all of them.
1915 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1916 if (cmp != 0)
1917 continue;
1919 if (*unique_id == NULL) {
1920 if (obj_type != GOT_OBJ_TYPE_ANY) {
1921 int matched_type;
1922 err = got_object_get_type(&matched_type, repo,
1923 &id);
1924 if (err)
1925 goto done;
1926 if (matched_type != obj_type)
1927 continue;
1929 *unique_id = got_object_id_dup(&id);
1930 if (*unique_id == NULL) {
1931 err = got_error_from_errno("got_object_id_dup");
1932 goto done;
1934 } else {
1935 if (got_object_id_cmp(*unique_id, &id) == 0)
1936 continue; /* both packed and loose */
1937 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1938 goto done;
1941 done:
1942 if (dir && closedir(dir) != 0 && err == NULL)
1943 err = got_error_from_errno("closedir");
1944 if (err) {
1945 free(*unique_id);
1946 *unique_id = NULL;
1948 free(id_str);
1949 free(path);
1950 return err;
1953 const struct got_error *
1954 got_repo_match_object_id_prefix(struct got_object_id **id,
1955 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1957 const struct got_error *err = NULL;
1958 char *path_objects = NULL, *object_dir = NULL;
1959 size_t len;
1960 int i;
1962 *id = NULL;
1964 path_objects = got_repo_get_path_objects(repo);
1966 len = strlen(id_str_prefix);
1967 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1968 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1969 goto done;
1972 for (i = 0; i < len; i++) {
1973 if (isxdigit((unsigned char)id_str_prefix[i]))
1974 continue;
1975 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1976 goto done;
1979 if (len >= 2) {
1980 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1981 if (err)
1982 goto done;
1983 object_dir = strndup(id_str_prefix, 2);
1984 if (object_dir == NULL) {
1985 err = got_error_from_errno("strdup");
1986 goto done;
1988 err = match_loose_object(id, path_objects, object_dir,
1989 id_str_prefix, obj_type, repo);
1990 } else if (len == 1) {
1991 int i;
1992 for (i = 0; i < 0xf; i++) {
1993 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1994 == -1) {
1995 err = got_error_from_errno("asprintf");
1996 goto done;
1998 err = match_packed_object(id, repo, object_dir,
1999 obj_type);
2000 if (err)
2001 goto done;
2002 err = match_loose_object(id, path_objects, object_dir,
2003 id_str_prefix, obj_type, repo);
2004 if (err)
2005 goto done;
2007 } else {
2008 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
2009 goto done;
2011 done:
2012 free(path_objects);
2013 free(object_dir);
2014 if (err) {
2015 free(*id);
2016 *id = NULL;
2017 } else if (*id == NULL) {
2018 switch (obj_type) {
2019 case GOT_OBJ_TYPE_BLOB:
2020 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2021 GOT_OBJ_LABEL_BLOB, id_str_prefix);
2022 break;
2023 case GOT_OBJ_TYPE_TREE:
2024 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2025 GOT_OBJ_LABEL_TREE, id_str_prefix);
2026 break;
2027 case GOT_OBJ_TYPE_COMMIT:
2028 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2029 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
2030 break;
2031 case GOT_OBJ_TYPE_TAG:
2032 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2033 GOT_OBJ_LABEL_TAG, id_str_prefix);
2034 break;
2035 default:
2036 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
2037 break;
2041 return err;
2044 const struct got_error *
2045 got_repo_match_object_id(struct got_object_id **id, char **label,
2046 const char *id_str, int obj_type, struct got_reflist_head *refs,
2047 struct got_repository *repo)
2049 const struct got_error *err;
2050 struct got_tag_object *tag;
2051 struct got_reference *ref = NULL;
2053 *id = NULL;
2054 if (label)
2055 *label = NULL;
2057 if (refs) {
2058 err = got_repo_object_match_tag(&tag, id_str, obj_type,
2059 refs, repo);
2060 if (err == NULL) {
2061 *id = got_object_id_dup(
2062 got_object_tag_get_object_id(tag));
2063 if (*id == NULL)
2064 err = got_error_from_errno("got_object_id_dup");
2065 else if (label && asprintf(label, "refs/tags/%s",
2066 got_object_tag_get_name(tag)) == -1) {
2067 err = got_error_from_errno("asprintf");
2068 free(*id);
2069 *id = NULL;
2071 got_object_tag_close(tag);
2072 return err;
2073 } else if (err->code != GOT_ERR_OBJ_TYPE &&
2074 err->code != GOT_ERR_NO_OBJ)
2075 return err;
2078 err = got_ref_open(&ref, repo, id_str, 0);
2079 if (err == NULL) {
2080 err = got_ref_resolve(id, repo, ref);
2081 if (err)
2082 goto done;
2083 if (label) {
2084 *label = strdup(got_ref_get_name(ref));
2085 if (*label == NULL) {
2086 err = got_error_from_errno("strdup");
2087 goto done;
2090 } else {
2091 if (err->code != GOT_ERR_NOT_REF &&
2092 err->code != GOT_ERR_BAD_REF_NAME)
2093 goto done;
2094 err = got_repo_match_object_id_prefix(id, id_str,
2095 obj_type, repo);
2096 if (err) {
2097 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
2098 err = got_error_not_ref(id_str);
2099 goto done;
2101 if (label) {
2102 err = got_object_id_str(label, *id);
2103 if (*label == NULL) {
2104 err = got_error_from_errno("strdup");
2105 goto done;
2109 done:
2110 if (ref)
2111 got_ref_close(ref);
2112 return err;
2115 const struct got_error *
2116 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
2117 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
2119 const struct got_error *err = NULL;
2120 struct got_reflist_entry *re;
2121 struct got_object_id *tag_id;
2122 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
2124 *tag = NULL;
2126 TAILQ_FOREACH(re, refs, entry) {
2127 const char *refname;
2128 refname = got_ref_get_name(re->ref);
2129 if (got_ref_is_symbolic(re->ref))
2130 continue;
2131 if (strncmp(refname, "refs/tags/", 10) != 0)
2132 continue;
2133 if (!name_is_absolute)
2134 refname += strlen("refs/tags/");
2135 if (strcmp(refname, name) != 0)
2136 continue;
2137 err = got_ref_resolve(&tag_id, repo, re->ref);
2138 if (err)
2139 break;
2140 err = got_object_open_as_tag(tag, repo, tag_id);
2141 free(tag_id);
2142 if (err)
2143 break;
2144 if (obj_type == GOT_OBJ_TYPE_ANY ||
2145 got_object_tag_get_object_type(*tag) == obj_type)
2146 break;
2147 got_object_tag_close(*tag);
2148 *tag = NULL;
2151 if (err == NULL && *tag == NULL)
2152 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2153 GOT_OBJ_LABEL_TAG, name);
2154 return err;
2157 const struct got_error *
2158 got_repo_find_object_id(struct got_object_id *id, struct got_repository *repo)
2160 const struct got_error *err;
2161 struct got_object_id *matched_id = NULL;
2162 char *id_str = NULL;
2164 err = got_object_id_str(&id_str, id);
2165 if (err)
2166 return err;
2168 err = got_repo_match_object_id_prefix(&matched_id, id_str,
2169 GOT_OBJ_TYPE_ANY, repo);
2170 free(id_str);
2171 return err;
2174 static const struct got_error *
2175 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2176 const char *name, mode_t mode, struct got_object_id *blob_id)
2178 const struct got_error *err = NULL;
2180 *new_te = NULL;
2182 *new_te = calloc(1, sizeof(**new_te));
2183 if (*new_te == NULL)
2184 return got_error_from_errno("calloc");
2186 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2187 sizeof((*new_te)->name)) {
2188 err = got_error(GOT_ERR_NO_SPACE);
2189 goto done;
2192 if (S_ISLNK(mode)) {
2193 (*new_te)->mode = S_IFLNK;
2194 } else {
2195 (*new_te)->mode = S_IFREG;
2196 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2198 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2199 done:
2200 if (err && *new_te) {
2201 free(*new_te);
2202 *new_te = NULL;
2204 return err;
2207 static const struct got_error *
2208 import_file(struct got_tree_entry **new_te, struct dirent *de,
2209 const char *path, struct got_repository *repo)
2211 const struct got_error *err;
2212 struct got_object_id *blob_id = NULL;
2213 char *filepath;
2214 struct stat sb;
2216 if (asprintf(&filepath, "%s%s%s", path,
2217 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2218 return got_error_from_errno("asprintf");
2220 if (lstat(filepath, &sb) != 0) {
2221 err = got_error_from_errno2("lstat", path);
2222 goto done;
2225 err = got_object_blob_create(&blob_id, filepath, repo);
2226 if (err)
2227 goto done;
2229 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2230 blob_id);
2231 done:
2232 free(filepath);
2233 if (err)
2234 free(blob_id);
2235 return err;
2238 static const struct got_error *
2239 insert_tree_entry(struct got_tree_entry *new_te,
2240 struct got_pathlist_head *paths)
2242 const struct got_error *err = NULL;
2243 struct got_pathlist_entry *new_pe;
2245 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2246 if (err)
2247 return err;
2248 if (new_pe == NULL)
2249 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2250 return NULL;
2253 static const struct got_error *write_tree(struct got_object_id **,
2254 const char *, struct got_pathlist_head *, struct got_repository *,
2255 got_repo_import_cb progress_cb, void *progress_arg);
2257 static const struct got_error *
2258 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2259 const char *path, struct got_pathlist_head *ignores,
2260 struct got_repository *repo,
2261 got_repo_import_cb progress_cb, void *progress_arg)
2263 const struct got_error *err;
2264 struct got_object_id *id = NULL;
2265 char *subdirpath;
2267 if (asprintf(&subdirpath, "%s%s%s", path,
2268 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2269 return got_error_from_errno("asprintf");
2271 (*new_te) = calloc(1, sizeof(**new_te));
2272 if (*new_te == NULL)
2273 return got_error_from_errno("calloc");
2274 (*new_te)->mode = S_IFDIR;
2275 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2276 sizeof((*new_te)->name)) {
2277 err = got_error(GOT_ERR_NO_SPACE);
2278 goto done;
2280 err = write_tree(&id, subdirpath, ignores, repo,
2281 progress_cb, progress_arg);
2282 if (err)
2283 goto done;
2284 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2286 done:
2287 free(id);
2288 free(subdirpath);
2289 if (err) {
2290 free(*new_te);
2291 *new_te = NULL;
2293 return err;
2296 static const struct got_error *
2297 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2298 struct got_pathlist_head *ignores, struct got_repository *repo,
2299 got_repo_import_cb progress_cb, void *progress_arg)
2301 const struct got_error *err = NULL;
2302 DIR *dir;
2303 struct dirent *de;
2304 int nentries;
2305 struct got_tree_entry *new_te = NULL;
2306 struct got_pathlist_head paths;
2307 struct got_pathlist_entry *pe;
2309 *new_tree_id = NULL;
2311 TAILQ_INIT(&paths);
2313 dir = opendir(path_dir);
2314 if (dir == NULL) {
2315 err = got_error_from_errno2("opendir", path_dir);
2316 goto done;
2319 nentries = 0;
2320 while ((de = readdir(dir)) != NULL) {
2321 int ignore = 0;
2322 int type;
2324 if (strcmp(de->d_name, ".") == 0 ||
2325 strcmp(de->d_name, "..") == 0)
2326 continue;
2328 err = got_path_dirent_type(&type, path_dir, de);
2329 if (err)
2330 goto done;
2332 TAILQ_FOREACH(pe, ignores, entry) {
2333 if (type == DT_DIR && pe->path_len > 0 &&
2334 pe->path[pe->path_len - 1] == '/') {
2335 char stripped[PATH_MAX];
2337 if (strlcpy(stripped, pe->path,
2338 sizeof(stripped)) >= sizeof(stripped)) {
2339 err = got_error(GOT_ERR_NO_SPACE);
2340 goto done;
2342 got_path_strip_trailing_slashes(stripped);
2343 if (fnmatch(stripped, de->d_name, 0) == 0) {
2344 ignore = 1;
2345 break;
2347 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2348 ignore = 1;
2349 break;
2352 if (ignore)
2353 continue;
2355 if (type == DT_DIR) {
2356 err = import_subdir(&new_te, de, path_dir,
2357 ignores, repo, progress_cb, progress_arg);
2358 if (err) {
2359 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2360 goto done;
2361 err = NULL;
2362 continue;
2364 } else if (type == DT_REG || type == DT_LNK) {
2365 err = import_file(&new_te, de, path_dir, repo);
2366 if (err)
2367 goto done;
2368 } else
2369 continue;
2371 err = insert_tree_entry(new_te, &paths);
2372 if (err)
2373 goto done;
2374 nentries++;
2377 if (TAILQ_EMPTY(&paths)) {
2378 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2379 "cannot create tree without any entries");
2380 goto done;
2383 TAILQ_FOREACH(pe, &paths, entry) {
2384 struct got_tree_entry *te = pe->data;
2385 char *path;
2386 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2387 continue;
2388 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2389 err = got_error_from_errno("asprintf");
2390 goto done;
2392 err = (*progress_cb)(progress_arg, path);
2393 free(path);
2394 if (err)
2395 goto done;
2398 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2399 done:
2400 if (dir)
2401 closedir(dir);
2402 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2403 return err;
2406 const struct got_error *
2407 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2408 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2409 struct got_repository *repo, got_repo_import_cb progress_cb,
2410 void *progress_arg)
2412 const struct got_error *err;
2413 struct got_object_id *new_tree_id;
2415 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2416 progress_cb, progress_arg);
2417 if (err)
2418 return err;
2420 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2421 author, time(NULL), author, time(NULL), logmsg, repo);
2422 free(new_tree_id);
2423 return err;
2426 const struct got_error *
2427 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2428 struct got_repository *repo)
2430 const struct got_error *err = NULL;
2431 char *path_objects = NULL, *path = NULL;
2432 DIR *dir = NULL;
2433 struct got_object_id id;
2434 int i;
2436 *nobjects = 0;
2437 *ondisk_size = 0;
2439 path_objects = got_repo_get_path_objects(repo);
2440 if (path_objects == NULL)
2441 return got_error_from_errno("got_repo_get_path_objects");
2443 for (i = 0; i <= 0xff; i++) {
2444 struct dirent *dent;
2446 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2447 err = got_error_from_errno("asprintf");
2448 break;
2451 dir = opendir(path);
2452 if (dir == NULL) {
2453 if (errno == ENOENT) {
2454 err = NULL;
2455 continue;
2457 err = got_error_from_errno2("opendir", path);
2458 break;
2461 while ((dent = readdir(dir)) != NULL) {
2462 char *id_str;
2463 int fd;
2464 struct stat sb;
2466 if (strcmp(dent->d_name, ".") == 0 ||
2467 strcmp(dent->d_name, "..") == 0)
2468 continue;
2470 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2471 err = got_error_from_errno("asprintf");
2472 goto done;
2475 if (!got_parse_object_id(&id, id_str, repo->algo)) {
2476 free(id_str);
2477 continue;
2479 free(id_str);
2481 err = got_object_open_loose_fd(&fd, &id, repo);
2482 if (err)
2483 goto done;
2485 if (fstat(fd, &sb) == -1) {
2486 err = got_error_from_errno("fstat");
2487 close(fd);
2488 goto done;
2490 (*nobjects)++;
2491 (*ondisk_size) += sb.st_size;
2493 if (close(fd) == -1) {
2494 err = got_error_from_errno("close");
2495 goto done;
2499 if (closedir(dir) != 0) {
2500 err = got_error_from_errno("closedir");
2501 goto done;
2503 dir = NULL;
2505 free(path);
2506 path = NULL;
2508 done:
2509 if (dir && closedir(dir) != 0 && err == NULL)
2510 err = got_error_from_errno("closedir");
2512 if (err) {
2513 *nobjects = 0;
2514 *ondisk_size = 0;
2516 free(path_objects);
2517 free(path);
2518 return err;
2521 const struct got_error *
2522 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2523 off_t *total_packsize, struct got_repository *repo)
2525 const struct got_error *err = NULL;
2526 DIR *packdir = NULL;
2527 struct dirent *dent;
2528 struct got_packidx *packidx = NULL;
2529 char *path_packidx;
2530 char *path_packfile;
2531 int packdir_fd;
2532 struct stat sb;
2534 *npackfiles = 0;
2535 *nobjects = 0;
2536 *total_packsize = 0;
2538 packdir_fd = openat(got_repo_get_fd(repo),
2539 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2540 if (packdir_fd == -1) {
2541 return got_error_from_errno_fmt("openat: %s/%s",
2542 got_repo_get_path_git_dir(repo),
2543 GOT_OBJECTS_PACK_DIR);
2546 packdir = fdopendir(packdir_fd);
2547 if (packdir == NULL) {
2548 err = got_error_from_errno("fdopendir");
2549 goto done;
2552 while ((dent = readdir(packdir)) != NULL) {
2553 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2554 continue;
2556 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2557 dent->d_name) == -1) {
2558 err = got_error_from_errno("asprintf");
2559 goto done;
2562 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2563 path_packidx, 0);
2564 free(path_packidx);
2565 if (err)
2566 goto done;
2568 if (fstat(packidx->fd, &sb) == -1)
2569 goto done;
2570 *total_packsize += sb.st_size;
2572 err = got_packidx_get_packfile_path(&path_packfile,
2573 packidx->path_packidx);
2574 if (err)
2575 goto done;
2577 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2578 0) == -1) {
2579 free(path_packfile);
2580 goto done;
2582 free(path_packfile);
2583 *total_packsize += sb.st_size;
2585 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2587 (*npackfiles)++;
2589 got_packidx_close(packidx);
2590 packidx = NULL;
2592 done:
2593 if (packidx)
2594 got_packidx_close(packidx);
2595 if (packdir && closedir(packdir) != 0 && err == NULL)
2596 err = got_error_from_errno("closedir");
2597 if (err) {
2598 *npackfiles = 0;
2599 *nobjects = 0;
2600 *total_packsize = 0;
2602 return err;
2605 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2606 got_packidx_bloom_filter_cmp);