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 "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24 #include <sys/mman.h>
25 #include <sys/resource.h>
27 #include <ctype.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 <string.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <zlib.h>
38 #include <errno.h>
39 #include <libgen.h>
40 #include <stdint.h>
42 #include "bloom.h"
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_delta_cache.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_hash.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
63 #include "got_lib_gotconfig.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
67 #endif
69 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
71 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
72 got_packidx_bloom_filter_cmp);
74 static inline int
75 is_boolean_val(const char *val)
76 {
77 return (strcasecmp(val, "true") == 0 ||
78 strcasecmp(val, "false") == 0 ||
79 strcasecmp(val, "on") == 0 ||
80 strcasecmp(val, "off") == 0 ||
81 strcasecmp(val, "yes") == 0 ||
82 strcasecmp(val, "no") == 0 ||
83 strcasecmp(val, "1") == 0 ||
84 strcasecmp(val, "0") == 0);
85 }
87 static inline int
88 get_boolean_val(const char *val)
89 {
90 return (strcasecmp(val, "true") == 0 ||
91 strcasecmp(val, "on") == 0 ||
92 strcasecmp(val, "yes") == 0 ||
93 strcasecmp(val, "1") == 0);
94 }
96 const char *
97 got_repo_get_path(struct got_repository *repo)
98 {
99 return repo->path;
102 const char *
103 got_repo_get_path_git_dir(struct got_repository *repo)
105 return repo->path_git_dir;
108 int
109 got_repo_get_fd(struct got_repository *repo)
111 return repo->gitdir_fd;
114 enum got_hash_algorithm
115 got_repo_get_object_format(struct got_repository *repo)
117 return repo->algo;
120 const char *
121 got_repo_get_gitconfig_author_name(struct got_repository *repo)
123 return repo->gitconfig_author_name;
126 const char *
127 got_repo_get_gitconfig_author_email(struct got_repository *repo)
129 return repo->gitconfig_author_email;
132 const char *
133 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
135 return repo->global_gitconfig_author_name;
138 const char *
139 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
141 return repo->global_gitconfig_author_email;
144 const char *
145 got_repo_get_gitconfig_owner(struct got_repository *repo)
147 return repo->gitconfig_owner;
150 int
151 got_repo_has_extension(struct got_repository *repo, const char *ext)
153 int i;
155 for (i = 0; i < repo->nextensions; ++i) {
156 if (!strcasecmp(ext, repo->extnames[i]))
157 return get_boolean_val(repo->extvals[i]);
160 return 0;
163 int
164 got_repo_is_bare(struct got_repository *repo)
166 return (strcmp(repo->path, repo->path_git_dir) == 0);
169 static char *
170 get_path_git_child(struct got_repository *repo, const char *basename)
172 char *path_child;
174 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
175 basename) == -1)
176 return NULL;
178 return path_child;
181 char *
182 got_repo_get_path_objects(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_OBJECTS_DIR);
187 char *
188 got_repo_get_path_objects_pack(struct got_repository *repo)
190 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
193 char *
194 got_repo_get_path_refs(struct got_repository *repo)
196 return get_path_git_child(repo, GOT_REFS_DIR);
199 char *
200 got_repo_get_path_packed_refs(struct got_repository *repo)
202 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
205 static char *
206 get_path_head(struct got_repository *repo)
208 return get_path_git_child(repo, GOT_HEAD_FILE);
211 char *
212 got_repo_get_path_gitconfig(struct got_repository *repo)
214 return get_path_git_child(repo, GOT_GITCONFIG);
217 char *
218 got_repo_get_path_gotconfig(struct got_repository *repo)
220 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
223 const struct got_gotconfig *
224 got_repo_get_gotconfig(struct got_repository *repo)
226 return repo->gotconfig;
229 void
230 got_repo_get_gitconfig_remotes(int *nremotes,
231 const struct got_remote_repo **remotes, struct got_repository *repo)
233 *nremotes = repo->ngitconfig_remotes;
234 *remotes = repo->gitconfig_remotes;
237 static int
238 is_git_repo(struct got_repository *repo)
240 const char *path_git = got_repo_get_path_git_dir(repo);
241 char *path_objects = got_repo_get_path_objects(repo);
242 char *path_refs = got_repo_get_path_refs(repo);
243 char *path_head = get_path_head(repo);
244 int ret = 0;
245 struct stat sb;
246 struct got_reference *head_ref;
248 if (lstat(path_git, &sb) == -1)
249 goto done;
250 if (!S_ISDIR(sb.st_mode))
251 goto done;
253 if (lstat(path_objects, &sb) == -1)
254 goto done;
255 if (!S_ISDIR(sb.st_mode))
256 goto done;
258 if (lstat(path_refs, &sb) == -1)
259 goto done;
260 if (!S_ISDIR(sb.st_mode))
261 goto done;
263 if (lstat(path_head, &sb) == -1)
264 goto done;
265 if (!S_ISREG(sb.st_mode))
266 goto done;
268 /* Check if the HEAD reference can be opened. */
269 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
270 goto done;
271 got_ref_close(head_ref);
273 ret = 1;
274 done:
275 free(path_objects);
276 free(path_refs);
277 free(path_head);
278 return ret;
282 static const struct got_error *
283 close_tempfiles(int *fds, size_t nfds)
285 const struct got_error *err = NULL;
286 int i;
288 for (i = 0; i < nfds; i++) {
289 if (fds[i] == -1)
290 continue;
291 if (close(fds[i]) == -1) {
292 err = got_error_from_errno("close");
293 break;
296 free(fds);
297 return err;
300 static const struct got_error *
301 open_tempfiles(int **fds, size_t array_size, size_t nfds)
303 const struct got_error *err = NULL;
304 int i;
306 *fds = calloc(array_size, sizeof(**fds));
307 if (*fds == NULL)
308 return got_error_from_errno("calloc");
310 for (i = 0; i < array_size; i++)
311 (*fds)[i] = -1;
313 for (i = 0; i < nfds; i++) {
314 (*fds)[i] = got_opentempfd();
315 if ((*fds)[i] == -1) {
316 err = got_error_from_errno("got_opentempfd");
317 close_tempfiles(*fds, nfds);
318 *fds = NULL;
319 return err;
323 return NULL;
326 static const struct got_error *
327 get_pack_cache_size(int *pack_cache_size)
329 struct rlimit rl;
331 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
332 return got_error_from_errno("getrlimit");
334 *pack_cache_size = GOT_PACK_CACHE_SIZE;
335 if (*pack_cache_size > rl.rlim_cur / 8)
336 *pack_cache_size = rl.rlim_cur / 8;
338 return NULL;
341 const struct got_error *
342 got_repo_pack_fds_open(int **pack_fds)
344 const struct got_error *err;
345 int nfds;
347 err = get_pack_cache_size(&nfds);
348 if (err)
349 return err;
351 /*
352 * We need one basefd and one accumfd per cached pack.
353 * Our constants should be set up in a way such that
354 * this error never triggers.
355 */
356 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
357 return got_error(GOT_ERR_NO_SPACE);
359 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
362 const struct got_error *
363 got_repo_pack_fds_close(int *pack_fds)
365 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
368 const struct got_error *
369 got_repo_temp_fds_open(int **temp_fds)
371 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
372 GOT_REPO_NUM_TEMPFILES);
375 void
376 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
378 int i;
380 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
381 repo->tempfiles[i] = temp_fds[i];
384 const struct got_error *
385 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
387 int i;
389 *fd = -1;
390 *idx = -1;
392 for (i = 0; i < nitems(repo->tempfiles); i++) {
393 if (repo->tempfile_use_mask & (1 << i))
394 continue;
395 if (repo->tempfiles[i] != -1) {
396 if (ftruncate(repo->tempfiles[i], 0L) == -1)
397 return got_error_from_errno("ftruncate");
398 *fd = repo->tempfiles[i];
399 *idx = i;
400 repo->tempfile_use_mask |= (1 << i);
401 return NULL;
405 return got_error(GOT_ERR_REPO_TEMPFILE);
408 void
409 got_repo_temp_fds_put(int idx, struct got_repository *repo)
411 repo->tempfile_use_mask &= ~(1 << idx);
414 const struct got_error *
415 got_repo_temp_fds_close(int *temp_fds)
417 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
420 const struct got_error *
421 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
422 struct got_object *obj)
424 #ifndef GOT_NO_OBJ_CACHE
425 const struct got_error *err = NULL;
426 err = got_object_cache_add(&repo->objcache, id, obj);
427 if (err) {
428 if (err->code == GOT_ERR_OBJ_EXISTS ||
429 err->code == GOT_ERR_OBJ_TOO_LARGE)
430 err = NULL;
431 return err;
433 obj->refcnt++;
434 #endif
435 return NULL;
438 struct got_object *
439 got_repo_get_cached_object(struct got_repository *repo,
440 struct got_object_id *id)
442 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
445 const struct got_error *
446 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
447 struct got_tree_object *tree)
449 #ifndef GOT_NO_OBJ_CACHE
450 const struct got_error *err = NULL;
451 err = got_object_cache_add(&repo->treecache, id, tree);
452 if (err) {
453 if (err->code == GOT_ERR_OBJ_EXISTS ||
454 err->code == GOT_ERR_OBJ_TOO_LARGE)
455 err = NULL;
456 return err;
458 tree->refcnt++;
459 #endif
460 return NULL;
463 struct got_tree_object *
464 got_repo_get_cached_tree(struct got_repository *repo,
465 struct got_object_id *id)
467 return (struct got_tree_object *)got_object_cache_get(
468 &repo->treecache, id);
471 const struct got_error *
472 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
473 struct got_commit_object *commit)
475 #ifndef GOT_NO_OBJ_CACHE
476 const struct got_error *err = NULL;
477 err = got_object_cache_add(&repo->commitcache, id, commit);
478 if (err) {
479 if (err->code == GOT_ERR_OBJ_EXISTS ||
480 err->code == GOT_ERR_OBJ_TOO_LARGE)
481 err = NULL;
482 return err;
484 commit->refcnt++;
485 #endif
486 return NULL;
489 struct got_commit_object *
490 got_repo_get_cached_commit(struct got_repository *repo,
491 struct got_object_id *id)
493 return (struct got_commit_object *)got_object_cache_get(
494 &repo->commitcache, id);
497 const struct got_error *
498 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
499 struct got_tag_object *tag)
501 #ifndef GOT_NO_OBJ_CACHE
502 const struct got_error *err = NULL;
503 err = got_object_cache_add(&repo->tagcache, id, tag);
504 if (err) {
505 if (err->code == GOT_ERR_OBJ_EXISTS ||
506 err->code == GOT_ERR_OBJ_TOO_LARGE)
507 err = NULL;
508 return err;
510 tag->refcnt++;
511 #endif
512 return NULL;
515 struct got_tag_object *
516 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
518 return (struct got_tag_object *)got_object_cache_get(
519 &repo->tagcache, id);
522 const struct got_error *
523 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
524 struct got_raw_object *raw)
526 #ifndef GOT_NO_OBJ_CACHE
527 const struct got_error *err = NULL;
528 err = got_object_cache_add(&repo->rawcache, id, raw);
529 if (err) {
530 if (err->code == GOT_ERR_OBJ_EXISTS ||
531 err->code == GOT_ERR_OBJ_TOO_LARGE)
532 err = NULL;
533 return err;
535 raw->refcnt++;
536 #endif
537 return NULL;
541 struct got_raw_object *
542 got_repo_get_cached_raw_object(struct got_repository *repo,
543 struct got_object_id *id)
545 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
549 static const struct got_error *
550 open_repo(struct got_repository *repo, const char *path)
552 const struct got_error *err = NULL;
554 repo->gitdir_fd = -1;
556 /* bare git repository? */
557 repo->path_git_dir = strdup(path);
558 if (repo->path_git_dir == NULL)
559 return got_error_from_errno("strdup");
560 if (is_git_repo(repo)) {
561 repo->path = strdup(repo->path_git_dir);
562 if (repo->path == NULL) {
563 err = got_error_from_errno("strdup");
564 goto done;
566 repo->gitdir_fd = open(repo->path_git_dir,
567 O_DIRECTORY | O_CLOEXEC);
568 if (repo->gitdir_fd == -1) {
569 err = got_error_from_errno2("open",
570 repo->path_git_dir);
571 goto done;
573 return NULL;
576 /* git repository with working tree? */
577 free(repo->path_git_dir);
578 repo->path_git_dir = NULL;
579 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
580 err = got_error_from_errno("asprintf");
581 goto done;
583 if (is_git_repo(repo)) {
584 repo->path = strdup(path);
585 if (repo->path == NULL) {
586 err = got_error_from_errno("strdup");
587 goto done;
589 repo->gitdir_fd = open(repo->path_git_dir,
590 O_DIRECTORY | O_CLOEXEC);
591 if (repo->gitdir_fd == -1) {
592 err = got_error_from_errno2("open",
593 repo->path_git_dir);
594 goto done;
596 return NULL;
599 err = got_error(GOT_ERR_NOT_GIT_REPO);
600 done:
601 if (err) {
602 free(repo->path);
603 repo->path = NULL;
604 free(repo->path_git_dir);
605 repo->path_git_dir = NULL;
606 if (repo->gitdir_fd != -1)
607 close(repo->gitdir_fd);
608 repo->gitdir_fd = -1;
611 return err;
614 static const struct got_error *
615 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
617 const struct got_error *err = NULL;
618 char *repo_gitconfig_path = NULL;
620 if (global_gitconfig_path) {
621 /* Read settings from ~/.gitconfig. */
622 int dummy_repo_version;
623 err = got_repo_read_gitconfig(&dummy_repo_version,
624 &repo->global_gitconfig_author_name,
625 &repo->global_gitconfig_author_email,
626 NULL, NULL, NULL, NULL, NULL, NULL,
627 global_gitconfig_path);
628 if (err)
629 return err;
632 /* Read repository's .git/config file. */
633 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
634 if (repo_gitconfig_path == NULL)
635 return got_error_from_errno("got_repo_get_path_gitconfig");
637 err = got_repo_read_gitconfig(
638 &repo->gitconfig_repository_format_version,
639 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
640 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
641 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
642 &repo->nextensions, repo_gitconfig_path);
643 if (err)
644 goto done;
646 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
647 int i;
649 for (i = 0; i < repo->ngitconfig_remotes; i++) {
650 got_repo_free_remote_repo_data(
651 &repo->gitconfig_remotes[i]);
653 free(repo->gitconfig_remotes);
654 repo->gitconfig_remotes = NULL;
655 repo->ngitconfig_remotes = 0;
657 free(repo->gitconfig_author_name);
658 repo->gitconfig_author_name = NULL;
659 free(repo->gitconfig_author_email);
660 repo->gitconfig_author_email = NULL;
662 free(repo->global_gitconfig_author_name);
663 repo->global_gitconfig_author_name = NULL;
664 free(repo->global_gitconfig_author_email);
665 repo->global_gitconfig_author_email = NULL;
668 done:
669 free(repo_gitconfig_path);
670 return err;
673 static const struct got_error *
674 read_gotconfig(struct got_repository *repo)
676 const struct got_error *err = NULL;
677 char *gotconfig_path;
679 gotconfig_path = got_repo_get_path_gotconfig(repo);
680 if (gotconfig_path == NULL)
681 return got_error_from_errno("got_repo_get_path_gotconfig");
683 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
684 free(gotconfig_path);
685 return err;
688 /* Supported repository format extensions. */
689 static const char *const repo_extensions[] = {
690 "noop", /* Got supports repository format version 1. */
691 "preciousObjects", /* Supported by gotadmin cleanup. */
692 "worktreeConfig", /* Got does not care about Git work trees. */
693 };
695 const struct got_error *
696 got_repo_open(struct got_repository **repop, const char *path,
697 const char *global_gitconfig_path, int *pack_fds)
699 struct got_repository *repo = NULL;
700 const struct got_error *err = NULL;
701 char *repo_path = NULL;
702 size_t i, j = 0;
704 *repop = NULL;
706 repo = calloc(1, sizeof(*repo));
707 if (repo == NULL)
708 return got_error_from_errno("calloc");
710 RB_INIT(&repo->packidx_bloom_filters);
711 TAILQ_INIT(&repo->packidx_paths);
713 for (i = 0; i < nitems(repo->privsep_children); i++) {
714 memset(&repo->privsep_children[i], 0,
715 sizeof(repo->privsep_children[0]));
716 repo->privsep_children[i].imsg_fd = -1;
719 err = got_object_cache_init(&repo->objcache,
720 GOT_OBJECT_CACHE_TYPE_OBJ);
721 if (err)
722 goto done;
723 err = got_object_cache_init(&repo->treecache,
724 GOT_OBJECT_CACHE_TYPE_TREE);
725 if (err)
726 goto done;
727 err = got_object_cache_init(&repo->commitcache,
728 GOT_OBJECT_CACHE_TYPE_COMMIT);
729 if (err)
730 goto done;
731 err = got_object_cache_init(&repo->tagcache,
732 GOT_OBJECT_CACHE_TYPE_TAG);
733 if (err)
734 goto done;
735 err = got_object_cache_init(&repo->rawcache,
736 GOT_OBJECT_CACHE_TYPE_RAW);
737 if (err)
738 goto done;
740 err = get_pack_cache_size(&repo->pack_cache_size);
741 if (err)
742 goto done;
743 for (i = 0; i < nitems(repo->packs); i++) {
744 if (pack_fds != NULL && i < repo->pack_cache_size) {
745 repo->packs[i].basefd = pack_fds[j++];
746 repo->packs[i].accumfd = pack_fds[j++];
747 } else {
748 repo->packs[i].basefd = -1;
749 repo->packs[i].accumfd = -1;
752 for (i = 0; i < nitems(repo->tempfiles); i++)
753 repo->tempfiles[i] = -1;
754 repo->pinned_pack = -1;
755 repo->pinned_packidx = -1;
756 repo->pinned_pid = 0;
758 repo_path = realpath(path, NULL);
759 if (repo_path == NULL) {
760 err = got_error_from_errno2("realpath", path);
761 goto done;
764 for (;;) {
765 char *parent_path;
767 err = open_repo(repo, repo_path);
768 if (err == NULL)
769 break;
770 if (err->code != GOT_ERR_NOT_GIT_REPO)
771 goto done;
772 if (repo_path[0] == '/' && repo_path[1] == '\0') {
773 err = got_error(GOT_ERR_NOT_GIT_REPO);
774 goto done;
776 err = got_path_dirname(&parent_path, repo_path);
777 if (err)
778 goto done;
779 free(repo_path);
780 repo_path = parent_path;
783 err = read_gotconfig(repo);
784 if (err)
785 goto done;
787 err = read_gitconfig(repo, global_gitconfig_path);
788 if (err)
789 goto done;
790 if (repo->gitconfig_repository_format_version != 0) {
791 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
792 goto done;
794 for (i = 0; i < repo->nextensions; i++) {
795 char *ext = repo->extnames[i];
796 char *val = repo->extvals[i];
797 int j, supported = 0;
799 if (!is_boolean_val(val)) {
800 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
801 goto done;
804 if (!get_boolean_val(val))
805 continue;
807 for (j = 0; j < nitems(repo_extensions); j++) {
808 if (strcmp(ext, repo_extensions[j]) == 0) {
809 supported = 1;
810 break;
813 if (!supported) {
814 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
815 goto done;
819 err = got_repo_list_packidx(&repo->packidx_paths, repo);
820 done:
821 if (err)
822 got_repo_close(repo);
823 else
824 *repop = repo;
825 free(repo_path);
826 return err;
829 const struct got_error *
830 got_repo_close(struct got_repository *repo)
832 const struct got_error *err = NULL, *child_err;
833 struct got_packidx_bloom_filter *bf;
834 size_t i;
836 for (i = 0; i < repo->pack_cache_size; i++) {
837 if (repo->packidx_cache[i] == NULL)
838 break;
839 got_packidx_close(repo->packidx_cache[i]);
842 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
843 &repo->packidx_bloom_filters))) {
844 RB_REMOVE(got_packidx_bloom_filter_tree,
845 &repo->packidx_bloom_filters, bf);
846 bloom_free(bf->bloom);
847 free(bf->bloom);
848 free(bf);
851 for (i = 0; i < repo->pack_cache_size; i++)
852 if (repo->packs[i].path_packfile)
853 if (repo->packs[i].path_packfile)
854 got_pack_close(&repo->packs[i]);
856 free(repo->path);
857 free(repo->path_git_dir);
859 got_object_cache_close(&repo->objcache);
860 got_object_cache_close(&repo->treecache);
861 got_object_cache_close(&repo->commitcache);
862 got_object_cache_close(&repo->tagcache);
863 got_object_cache_close(&repo->rawcache);
865 for (i = 0; i < nitems(repo->privsep_children); i++) {
866 if (repo->privsep_children[i].imsg_fd == -1)
867 continue;
868 imsg_clear(repo->privsep_children[i].ibuf);
869 free(repo->privsep_children[i].ibuf);
870 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
871 if (err && err->code == GOT_ERR_EOF)
872 err = NULL;
873 child_err = got_privsep_wait_for_child(
874 repo->privsep_children[i].pid);
875 if (child_err && err == NULL)
876 err = child_err;
877 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
878 err == NULL)
879 err = got_error_from_errno("close");
882 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
883 err == NULL)
884 err = got_error_from_errno("close");
886 if (repo->gotconfig)
887 got_gotconfig_free(repo->gotconfig);
888 free(repo->gitconfig_author_name);
889 free(repo->gitconfig_author_email);
890 for (i = 0; i < repo->ngitconfig_remotes; i++)
891 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
892 free(repo->gitconfig_remotes);
893 for (i = 0; i < repo->nextensions; i++) {
894 free(repo->extnames[i]);
895 free(repo->extvals[i]);
897 free(repo->extnames);
898 free(repo->extvals);
900 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
901 free(repo);
903 return err;
906 const struct got_error *
907 got_repo_remote_repo_dup(struct got_remote_repo **newp,
908 const struct got_remote_repo *repo)
910 const struct got_error *err = NULL;
911 struct got_remote_repo *new;
912 int i;
914 new = calloc(1, sizeof(*new));
915 if (new == NULL)
916 return got_error_from_errno("calloc");
918 if (repo->name) {
919 new->name = strdup(repo->name);
920 if (new->name == NULL) {
921 err = got_error_from_errno("strdup");
922 goto done;
926 if (repo->fetch_url) {
927 new->fetch_url = strdup(repo->fetch_url);
928 if (new->fetch_url == NULL) {
929 err = got_error_from_errno("strdup");
930 goto done;
934 if (repo->send_url) {
935 new->send_url = strdup(repo->send_url);
936 if (new->send_url == NULL) {
937 err = got_error_from_errno("strdup");
938 goto done;
942 new->mirror_references = repo->mirror_references;
944 new->fetch_all_branches = repo->fetch_all_branches;
946 new->nfetch_branches = repo->nfetch_branches;
947 if (repo->fetch_branches) {
948 new->fetch_branches = calloc(repo->nfetch_branches,
949 sizeof(char *));
950 if (new->fetch_branches == NULL) {
951 err = got_error_from_errno("calloc");
952 goto done;
954 for (i = 0; i < repo->nfetch_branches; i++) {
955 new->fetch_branches[i] = strdup(
956 repo->fetch_branches[i]);
957 if (new->fetch_branches[i] == NULL) {
958 err = got_error_from_errno("strdup");
959 goto done;
964 new->nsend_branches = repo->nsend_branches;
965 if (repo->send_branches) {
966 new->send_branches = calloc(repo->nsend_branches,
967 sizeof(char *));
968 if (new->send_branches == NULL) {
969 err = got_error_from_errno("calloc");
970 goto done;
972 for (i = 0; i < repo->nsend_branches; i++) {
973 new->send_branches[i] = strdup(
974 repo->send_branches[i]);
975 if (new->send_branches[i] == NULL) {
976 err = got_error_from_errno("strdup");
977 goto done;
982 new->nfetch_refs = repo->nfetch_refs;
983 if (repo->fetch_refs) {
984 new->fetch_refs = calloc(repo->nfetch_refs,
985 sizeof(char *));
986 if (new->fetch_refs == NULL) {
987 err = got_error_from_errno("calloc");
988 goto done;
990 for (i = 0; i < repo->nfetch_refs; i++) {
991 new->fetch_refs[i] = strdup(
992 repo->fetch_refs[i]);
993 if (new->fetch_refs[i] == NULL) {
994 err = got_error_from_errno("strdup");
995 goto done;
999 done:
1000 if (err) {
1001 got_repo_free_remote_repo_data(new);
1002 free(new);
1003 } else
1004 *newp = new;
1006 return err;
1009 void
1010 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
1012 int i;
1014 if (repo == NULL)
1015 return;
1017 free(repo->name);
1018 repo->name = NULL;
1019 free(repo->fetch_url);
1020 repo->fetch_url = NULL;
1021 free(repo->send_url);
1022 repo->send_url = NULL;
1023 for (i = 0; i < repo->nfetch_branches; i++)
1024 free(repo->fetch_branches[i]);
1025 free(repo->fetch_branches);
1026 repo->fetch_branches = NULL;
1027 repo->nfetch_branches = 0;
1028 for (i = 0; i < repo->nsend_branches; i++)
1029 free(repo->send_branches[i]);
1030 free(repo->send_branches);
1031 repo->send_branches = NULL;
1032 repo->nsend_branches = 0;
1033 for (i = 0; i < repo->nfetch_refs; i++)
1034 free(repo->fetch_refs[i]);
1035 free(repo->fetch_refs);
1036 repo->fetch_refs = NULL;
1037 repo->nfetch_refs = 0;
1040 const struct got_error *
1041 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
1042 const char *input_path)
1044 const struct got_error *err = NULL;
1045 const char *repo_abspath = NULL;
1046 size_t repolen, len;
1047 char *canonpath, *path = NULL;
1049 *in_repo_path = NULL;
1051 canonpath = strdup(input_path);
1052 if (canonpath == NULL) {
1053 err = got_error_from_errno("strdup");
1054 goto done;
1056 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
1057 if (err)
1058 goto done;
1060 repo_abspath = got_repo_get_path(repo);
1062 if (canonpath[0] == '\0') {
1063 path = strdup(canonpath);
1064 if (path == NULL) {
1065 err = got_error_from_errno("strdup");
1066 goto done;
1068 } else {
1069 path = realpath(canonpath, NULL);
1070 if (path == NULL) {
1071 if (errno != ENOENT) {
1072 err = got_error_from_errno2("realpath",
1073 canonpath);
1074 goto done;
1077 * Path is not on disk.
1078 * Assume it is already relative to repository root.
1080 path = strdup(canonpath);
1081 if (path == NULL) {
1082 err = got_error_from_errno("strdup");
1083 goto done;
1087 repolen = strlen(repo_abspath);
1088 len = strlen(path);
1091 if (strcmp(path, repo_abspath) == 0) {
1092 free(path);
1093 path = strdup("");
1094 if (path == NULL) {
1095 err = got_error_from_errno("strdup");
1096 goto done;
1098 } else if (len > repolen &&
1099 got_path_is_child(path, repo_abspath, repolen)) {
1100 /* Matched an on-disk path inside repository. */
1101 if (got_repo_is_bare(repo)) {
1103 * Matched an on-disk path inside repository
1104 * database. Treat input as repository-relative.
1106 free(path);
1107 path = canonpath;
1108 canonpath = NULL;
1109 } else {
1110 char *child;
1111 /* Strip common prefix with repository path. */
1112 err = got_path_skip_common_ancestor(&child,
1113 repo_abspath, path);
1114 if (err)
1115 goto done;
1116 free(path);
1117 path = child;
1119 } else {
1121 * Matched unrelated on-disk path.
1122 * Treat input as repository-relative.
1124 free(path);
1125 path = canonpath;
1126 canonpath = NULL;
1130 /* Make in-repository path absolute */
1131 if (path[0] != '/') {
1132 char *abspath;
1133 if (asprintf(&abspath, "/%s", path) == -1) {
1134 err = got_error_from_errno("asprintf");
1135 goto done;
1137 free(path);
1138 path = abspath;
1141 done:
1142 free(canonpath);
1143 if (err)
1144 free(path);
1145 else
1146 *in_repo_path = path;
1147 return err;
1150 static const struct got_error *
1151 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1152 const char *path_packidx)
1154 const struct got_error *err = NULL;
1155 size_t i;
1157 for (i = 0; i < repo->pack_cache_size; i++) {
1158 if (repo->packidx_cache[i] == NULL)
1159 break;
1160 if (strcmp(repo->packidx_cache[i]->path_packidx,
1161 path_packidx) == 0) {
1162 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1165 if (i == repo->pack_cache_size) {
1166 do {
1167 i--;
1168 } while (i > 0 && repo->pinned_packidx >= 0 &&
1169 i == repo->pinned_packidx);
1170 err = got_packidx_close(repo->packidx_cache[i]);
1171 if (err)
1172 return err;
1175 repo->packidx_cache[i] = packidx;
1177 return NULL;
1180 int
1181 got_repo_is_packidx_filename(const char *name, size_t len)
1183 if (len != GOT_PACKIDX_NAMELEN)
1184 return 0;
1186 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1187 return 0;
1189 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1190 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1191 return 0;
1193 return 1;
1196 static struct got_packidx_bloom_filter *
1197 get_packidx_bloom_filter(struct got_repository *repo,
1198 const char *path, size_t path_len)
1200 struct got_packidx_bloom_filter key;
1202 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1203 return NULL; /* XXX */
1204 key.path_len = path_len;
1206 return RB_FIND(got_packidx_bloom_filter_tree,
1207 &repo->packidx_bloom_filters, &key);
1210 int
1211 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1212 const char *path_packidx, struct got_object_id *id)
1214 struct got_packidx_bloom_filter *bf;
1216 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1217 if (bf)
1218 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1220 /* No bloom filter means this pack index must be searched. */
1221 return 1;
1224 static const struct got_error *
1225 add_packidx_bloom_filter(struct got_repository *repo,
1226 struct got_packidx *packidx, const char *path_packidx)
1228 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1229 struct got_packidx_bloom_filter *bf;
1230 size_t len;
1233 * Don't use bloom filters for very large pack index files.
1234 * Large pack files will contain a relatively large fraction
1235 * of our objects so we will likely need to visit them anyway.
1236 * The more objects a pack file contains the higher the probability
1237 * of a false-positive match from the bloom filter. And reading
1238 * all object IDs from a large pack index file can be expensive.
1240 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1241 return NULL;
1243 /* Do we already have a filter for this pack index? */
1244 if (get_packidx_bloom_filter(repo, path_packidx,
1245 strlen(path_packidx)) != NULL)
1246 return NULL;
1248 bf = calloc(1, sizeof(*bf));
1249 if (bf == NULL)
1250 return got_error_from_errno("calloc");
1251 bf->bloom = calloc(1, sizeof(*bf->bloom));
1252 if (bf->bloom == NULL) {
1253 free(bf);
1254 return got_error_from_errno("calloc");
1257 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1258 if (len >= sizeof(bf->path)) {
1259 free(bf->bloom);
1260 free(bf);
1261 return got_error(GOT_ERR_NO_SPACE);
1263 bf->path_len = len;
1265 /* Minimum size supported by our bloom filter is 1000 entries. */
1266 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1267 for (i = 0; i < nobjects; i++) {
1268 struct got_packidx_object_id *id;
1269 id = &packidx->hdr.sorted_ids[i];
1270 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1273 RB_INSERT(got_packidx_bloom_filter_tree,
1274 &repo->packidx_bloom_filters, bf);
1275 return NULL;
1278 static void
1279 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1281 struct got_pathlist_entry *pe;
1283 while (!TAILQ_EMPTY(packidx_paths)) {
1284 pe = TAILQ_FIRST(packidx_paths);
1285 TAILQ_REMOVE(packidx_paths, pe, entry);
1286 free((char *)pe->path);
1287 free(pe);
1291 static const struct got_error *
1292 refresh_packidx_paths(struct got_repository *repo)
1294 const struct got_error *err = NULL;
1295 char *objects_pack_dir = NULL;
1296 struct stat sb;
1298 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1299 if (objects_pack_dir == NULL)
1300 return got_error_from_errno("got_repo_get_path_objects_pack");
1302 if (stat(objects_pack_dir, &sb) == -1) {
1303 if (errno != ENOENT) {
1304 err = got_error_from_errno2("stat", objects_pack_dir);
1305 goto done;
1307 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1308 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1309 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1310 purge_packidx_paths(&repo->packidx_paths);
1311 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1312 if (err)
1313 goto done;
1315 done:
1316 free(objects_pack_dir);
1317 return err;
1320 const struct got_error *
1321 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1322 struct got_repository *repo, struct got_object_id *id)
1324 const struct got_error *err;
1325 struct got_pathlist_entry *pe;
1326 size_t i;
1328 /* Search pack index cache. */
1329 for (i = 0; i < repo->pack_cache_size; i++) {
1330 if (repo->packidx_cache[i] == NULL)
1331 break;
1332 if (!got_repo_check_packidx_bloom_filter(repo,
1333 repo->packidx_cache[i]->path_packidx, id))
1334 continue; /* object will not be found in this index */
1335 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1336 if (*idx != -1) {
1337 *packidx = repo->packidx_cache[i];
1339 * Move this cache entry to the front. Repeatedly
1340 * searching a wrong pack index can be expensive.
1342 if (i > 0) {
1343 memmove(&repo->packidx_cache[1],
1344 &repo->packidx_cache[0],
1345 i * sizeof(repo->packidx_cache[0]));
1346 repo->packidx_cache[0] = *packidx;
1347 if (repo->pinned_packidx >= 0 &&
1348 repo->pinned_packidx < i)
1349 repo->pinned_packidx++;
1350 else if (repo->pinned_packidx == i)
1351 repo->pinned_packidx = 0;
1353 return NULL;
1356 /* No luck. Search the filesystem. */
1358 err = refresh_packidx_paths(repo);
1359 if (err)
1360 return err;
1362 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1363 const char *path_packidx = pe->path;
1364 int is_cached = 0;
1366 if (!got_repo_check_packidx_bloom_filter(repo,
1367 pe->path, id))
1368 continue; /* object will not be found in this index */
1370 for (i = 0; i < repo->pack_cache_size; i++) {
1371 if (repo->packidx_cache[i] == NULL)
1372 break;
1373 if (strcmp(repo->packidx_cache[i]->path_packidx,
1374 path_packidx) == 0) {
1375 is_cached = 1;
1376 break;
1379 if (is_cached)
1380 continue; /* already searched */
1382 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1383 path_packidx, 0);
1384 if (err)
1385 goto done;
1387 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1388 if (err)
1389 goto done;
1391 err = cache_packidx(repo, *packidx, path_packidx);
1392 if (err)
1393 goto done;
1395 *idx = got_packidx_get_object_idx(*packidx, id);
1396 if (*idx != -1) {
1397 err = NULL; /* found the object */
1398 goto done;
1402 err = got_error_no_obj(id);
1403 done:
1404 return err;
1407 const struct got_error *
1408 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1409 struct got_repository *repo)
1411 const struct got_error *err = NULL;
1412 DIR *packdir = NULL;
1413 struct dirent *dent;
1414 char *path_packidx = NULL;
1415 int packdir_fd;
1416 struct stat sb;
1418 packdir_fd = openat(got_repo_get_fd(repo),
1419 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1420 if (packdir_fd == -1) {
1421 return got_error_from_errno_fmt("openat: %s/%s",
1422 got_repo_get_path_git_dir(repo),
1423 GOT_OBJECTS_PACK_DIR);
1426 packdir = fdopendir(packdir_fd);
1427 if (packdir == NULL) {
1428 err = got_error_from_errno("fdopendir");
1429 goto done;
1432 if (fstat(packdir_fd, &sb) == -1) {
1433 err = got_error_from_errno("fstat");
1434 goto done;
1436 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1437 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1439 while ((dent = readdir(packdir)) != NULL) {
1440 if (!got_repo_is_packidx_filename(dent->d_name,
1441 strlen(dent->d_name)))
1442 continue;
1444 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1445 dent->d_name) == -1) {
1446 err = got_error_from_errno("asprintf");
1447 path_packidx = NULL;
1448 break;
1451 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1452 if (err)
1453 break;
1455 done:
1456 if (err)
1457 free(path_packidx);
1458 if (packdir && closedir(packdir) != 0 && err == NULL)
1459 err = got_error_from_errno("closedir");
1460 return err;
1463 const struct got_error *
1464 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1465 struct got_repository *repo)
1467 const struct got_error *err;
1468 size_t i;
1470 *packidx = NULL;
1472 /* Search pack index cache. */
1473 for (i = 0; i < repo->pack_cache_size; i++) {
1474 if (repo->packidx_cache[i] == NULL)
1475 break;
1476 if (strcmp(repo->packidx_cache[i]->path_packidx,
1477 path_packidx) == 0) {
1478 *packidx = repo->packidx_cache[i];
1479 return NULL;
1482 /* No luck. Search the filesystem. */
1484 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1485 path_packidx, 0);
1486 if (err)
1487 return err;
1489 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1490 if (err)
1491 goto done;
1493 err = cache_packidx(repo, *packidx, path_packidx);
1494 done:
1495 if (err) {
1496 got_packidx_close(*packidx);
1497 *packidx = NULL;
1499 return err;
1502 static const struct got_error *
1503 read_packfile_hdr(int fd, struct got_packidx *packidx)
1505 const struct got_error *err = NULL;
1506 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1507 struct got_packfile_hdr hdr;
1508 ssize_t n;
1510 n = read(fd, &hdr, sizeof(hdr));
1511 if (n < 0)
1512 return got_error_from_errno("read");
1513 if (n != sizeof(hdr))
1514 return got_error(GOT_ERR_BAD_PACKFILE);
1516 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1517 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1518 be32toh(hdr.nobjects) != totobj)
1519 err = got_error(GOT_ERR_BAD_PACKFILE);
1521 return err;
1524 static const struct got_error *
1525 open_packfile(int *fd, struct got_repository *repo,
1526 const char *relpath, struct got_packidx *packidx)
1528 const struct got_error *err = NULL;
1530 *fd = openat(got_repo_get_fd(repo), relpath,
1531 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1532 if (*fd == -1)
1533 return got_error_from_errno_fmt("openat: %s/%s",
1534 got_repo_get_path_git_dir(repo), relpath);
1536 if (packidx) {
1537 err = read_packfile_hdr(*fd, packidx);
1538 if (err) {
1539 close(*fd);
1540 *fd = -1;
1544 return err;
1547 const struct got_error *
1548 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1549 const char *path_packfile, struct got_packidx *packidx)
1551 const struct got_error *err = NULL;
1552 struct got_pack *pack = NULL;
1553 struct stat sb;
1554 size_t i;
1556 if (packp)
1557 *packp = NULL;
1559 for (i = 0; i < repo->pack_cache_size; i++) {
1560 pack = &repo->packs[i];
1561 if (pack->path_packfile == NULL)
1562 break;
1563 if (strcmp(pack->path_packfile, path_packfile) == 0)
1564 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1567 if (i == repo->pack_cache_size) {
1568 struct got_pack tmp;
1569 do {
1570 i--;
1571 } while (i > 0 && repo->pinned_pack >= 0 &&
1572 i == repo->pinned_pack);
1573 err = got_pack_close(&repo->packs[i]);
1574 if (err)
1575 return err;
1576 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1577 return got_error_from_errno("ftruncate");
1578 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1579 return got_error_from_errno("ftruncate");
1580 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1581 memcpy(&repo->packs[i], &repo->packs[0],
1582 sizeof(repo->packs[i]));
1583 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1584 if (repo->pinned_pack == 0)
1585 repo->pinned_pack = i;
1586 else if (repo->pinned_pack == i)
1587 repo->pinned_pack = 0;
1588 i = 0;
1591 pack = &repo->packs[i];
1593 pack->path_packfile = strdup(path_packfile);
1594 if (pack->path_packfile == NULL) {
1595 err = got_error_from_errno("strdup");
1596 goto done;
1599 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1600 if (err)
1601 goto done;
1603 if (fstat(pack->fd, &sb) != 0) {
1604 err = got_error_from_errno("fstat");
1605 goto done;
1607 pack->filesize = sb.st_size;
1609 pack->privsep_child = NULL;
1611 err = got_delta_cache_alloc(&pack->delta_cache);
1612 if (err)
1613 goto done;
1615 #ifndef GOT_PACK_NO_MMAP
1616 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1617 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1618 pack->fd, 0);
1619 if (pack->map == MAP_FAILED) {
1620 if (errno != ENOMEM) {
1621 err = got_error_from_errno("mmap");
1622 goto done;
1624 pack->map = NULL; /* fall back to read(2) */
1627 #endif
1628 done:
1629 if (err) {
1630 if (pack)
1631 got_pack_close(pack);
1632 } else if (packp)
1633 *packp = pack;
1634 return err;
1637 struct got_pack *
1638 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1640 struct got_pack *pack = NULL;
1641 size_t i;
1643 for (i = 0; i < repo->pack_cache_size; i++) {
1644 pack = &repo->packs[i];
1645 if (pack->path_packfile == NULL)
1646 break;
1647 if (strcmp(pack->path_packfile, path_packfile) == 0)
1648 return pack;
1651 return NULL;
1654 const struct got_error *
1655 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1656 struct got_pack *pack)
1658 size_t i;
1659 int pinned_pack = -1, pinned_packidx = -1;
1661 for (i = 0; i < repo->pack_cache_size; i++) {
1662 if (repo->packidx_cache[i] &&
1663 strcmp(repo->packidx_cache[i]->path_packidx,
1664 packidx->path_packidx) == 0)
1665 pinned_packidx = i;
1666 if (repo->packs[i].path_packfile &&
1667 strcmp(repo->packs[i].path_packfile,
1668 pack->path_packfile) == 0)
1669 pinned_pack = i;
1672 if (pinned_packidx == -1 || pinned_pack == -1)
1673 return got_error(GOT_ERR_PIN_PACK);
1675 repo->pinned_pack = pinned_pack;
1676 repo->pinned_packidx = pinned_packidx;
1677 if (repo->packs[pinned_pack].privsep_child)
1678 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1679 return NULL;
1682 struct got_pack *
1683 got_repo_get_pinned_pack(struct got_repository *repo)
1685 if (repo->pinned_pack >= 0 &&
1686 repo->pinned_pack < repo->pack_cache_size)
1687 return &repo->packs[repo->pinned_pack];
1689 return NULL;
1692 void
1693 got_repo_unpin_pack(struct got_repository *repo)
1695 repo->pinned_packidx = -1;
1696 repo->pinned_pack = -1;
1697 repo->pinned_pid = 0;
1700 const struct got_error *
1701 got_repo_init(const char *repo_path, const char *head_name)
1703 const struct got_error *err = NULL;
1704 const char *dirnames[] = {
1705 GOT_OBJECTS_DIR,
1706 GOT_OBJECTS_PACK_DIR,
1707 GOT_REFS_DIR,
1709 const char *description_str = "Unnamed repository; "
1710 "edit this file 'description' to name the repository.";
1711 const char *headref = "ref: refs/heads/";
1712 const char *gitconfig_str = "[core]\n"
1713 "\trepositoryformatversion = 0\n"
1714 "\tfilemode = true\n"
1715 "\tbare = true\n";
1716 char *headref_str, *path;
1717 size_t i;
1719 if (!got_path_dir_is_empty(repo_path))
1720 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1722 for (i = 0; i < nitems(dirnames); i++) {
1723 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1724 return got_error_from_errno("asprintf");
1726 err = got_path_mkdir(path);
1727 free(path);
1728 if (err)
1729 return err;
1732 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1733 return got_error_from_errno("asprintf");
1734 err = got_path_create_file(path, description_str);
1735 free(path);
1736 if (err)
1737 return err;
1739 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1740 return got_error_from_errno("asprintf");
1741 if (asprintf(&headref_str, "%s%s", headref,
1742 head_name ? head_name : "main") == -1) {
1743 free(path);
1744 return got_error_from_errno("asprintf");
1746 err = got_path_create_file(path, headref_str);
1747 free(headref_str);
1748 free(path);
1749 if (err)
1750 return err;
1752 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1753 return got_error_from_errno("asprintf");
1754 err = got_path_create_file(path, gitconfig_str);
1755 free(path);
1756 if (err)
1757 return err;
1759 return NULL;
1762 static const struct got_error *
1763 match_packed_object(struct got_object_id **unique_id,
1764 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1766 const struct got_error *err = NULL;
1767 struct got_object_id_queue matched_ids;
1768 struct got_pathlist_entry *pe;
1769 struct timespec tv;
1770 int retries = 0;
1771 const int max_retries = 10;
1773 STAILQ_INIT(&matched_ids);
1775 err = refresh_packidx_paths(repo);
1776 if (err)
1777 return err;
1780 * Opening objects while iterating over the pack-index path
1781 * list is racy. If the set of pack files in the repository
1782 * changes during loop iteration, refresh_packidx_paths() will
1783 * be called again, via got_object_get_type(), invalidating
1784 * the packidx_paths list we are iterating over.
1785 * To work around this we keep track of the current modification
1786 * time and retry the entire loop if it changes.
1788 retry:
1789 tv.tv_sec = repo->pack_path_mtime.tv_sec;
1790 tv.tv_nsec = repo->pack_path_mtime.tv_nsec;
1792 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1793 const char *path_packidx;
1794 struct got_packidx *packidx;
1795 struct got_object_qid *qid;
1798 * If the modification time of the 'objects/pack' directory
1799 * has changed then 'pe' could now be an invalid pointer.
1801 if (tv.tv_sec != repo->pack_path_mtime.tv_sec ||
1802 tv.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1803 if (++retries > max_retries) {
1804 err = got_error_msg(GOT_ERR_TIMEOUT,
1805 "too many concurrent pack file "
1806 "modifications");
1807 goto done;
1809 goto retry;
1812 path_packidx = pe->path;
1814 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1815 path_packidx, 0);
1816 if (err)
1817 break;
1819 got_object_id_queue_free(&matched_ids);
1821 err = got_packidx_match_id_str_prefix(&matched_ids,
1822 packidx, id_str_prefix);
1823 if (err) {
1824 got_packidx_close(packidx);
1825 break;
1827 err = got_packidx_close(packidx);
1828 if (err)
1829 break;
1831 STAILQ_FOREACH(qid, &matched_ids, entry) {
1832 if (obj_type != GOT_OBJ_TYPE_ANY) {
1833 int matched_type;
1834 err = got_object_get_type(&matched_type, repo,
1835 &qid->id);
1836 if (err)
1837 goto done;
1838 if (matched_type != obj_type)
1839 continue;
1841 if (*unique_id == NULL) {
1842 *unique_id = got_object_id_dup(&qid->id);
1843 if (*unique_id == NULL) {
1844 err = got_error_from_errno("malloc");
1845 goto done;
1847 } else {
1848 if (got_object_id_cmp(*unique_id,
1849 &qid->id) == 0)
1850 continue; /* packed multiple times */
1851 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1852 goto done;
1856 done:
1857 got_object_id_queue_free(&matched_ids);
1858 if (err) {
1859 free(*unique_id);
1860 *unique_id = NULL;
1862 return err;
1865 static const struct got_error *
1866 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1867 const char *object_dir, const char *id_str_prefix, int obj_type,
1868 struct got_repository *repo)
1870 const struct got_error *err = NULL;
1871 char *path, *id_str = NULL;
1872 DIR *dir = NULL;
1873 struct dirent *dent;
1874 struct got_object_id id;
1876 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1877 err = got_error_from_errno("asprintf");
1878 goto done;
1881 dir = opendir(path);
1882 if (dir == NULL) {
1883 if (errno == ENOENT) {
1884 err = NULL;
1885 goto done;
1887 err = got_error_from_errno2("opendir", path);
1888 goto done;
1890 while ((dent = readdir(dir)) != NULL) {
1891 int cmp;
1893 free(id_str);
1894 id_str = NULL;
1896 if (strcmp(dent->d_name, ".") == 0 ||
1897 strcmp(dent->d_name, "..") == 0)
1898 continue;
1900 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1901 err = got_error_from_errno("asprintf");
1902 goto done;
1905 if (!got_parse_object_id(&id, id_str, repo->algo))
1906 continue;
1909 * Directory entries do not necessarily appear in
1910 * sorted order, so we must iterate over all of them.
1912 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1913 if (cmp != 0)
1914 continue;
1916 if (*unique_id == NULL) {
1917 if (obj_type != GOT_OBJ_TYPE_ANY) {
1918 int matched_type;
1919 err = got_object_get_type(&matched_type, repo,
1920 &id);
1921 if (err)
1922 goto done;
1923 if (matched_type != obj_type)
1924 continue;
1926 *unique_id = got_object_id_dup(&id);
1927 if (*unique_id == NULL) {
1928 err = got_error_from_errno("got_object_id_dup");
1929 goto done;
1931 } else {
1932 if (got_object_id_cmp(*unique_id, &id) == 0)
1933 continue; /* both packed and loose */
1934 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1935 goto done;
1938 done:
1939 if (dir && closedir(dir) != 0 && err == NULL)
1940 err = got_error_from_errno("closedir");
1941 if (err) {
1942 free(*unique_id);
1943 *unique_id = NULL;
1945 free(id_str);
1946 free(path);
1947 return err;
1950 const struct got_error *
1951 got_repo_match_object_id_prefix(struct got_object_id **id,
1952 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1954 const struct got_error *err = NULL;
1955 char *path_objects = NULL, *object_dir = NULL;
1956 size_t len;
1957 int i;
1959 *id = NULL;
1961 path_objects = got_repo_get_path_objects(repo);
1963 len = strlen(id_str_prefix);
1964 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1965 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1966 goto done;
1969 for (i = 0; i < len; i++) {
1970 if (isxdigit((unsigned char)id_str_prefix[i]))
1971 continue;
1972 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1973 goto done;
1976 if (len >= 2) {
1977 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1978 if (err)
1979 goto done;
1980 object_dir = strndup(id_str_prefix, 2);
1981 if (object_dir == NULL) {
1982 err = got_error_from_errno("strdup");
1983 goto done;
1985 err = match_loose_object(id, path_objects, object_dir,
1986 id_str_prefix, obj_type, repo);
1987 } else if (len == 1) {
1988 int i;
1989 for (i = 0; i < 0xf; i++) {
1990 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1991 == -1) {
1992 err = got_error_from_errno("asprintf");
1993 goto done;
1995 err = match_packed_object(id, repo, object_dir,
1996 obj_type);
1997 if (err)
1998 goto done;
1999 err = match_loose_object(id, path_objects, object_dir,
2000 id_str_prefix, obj_type, repo);
2001 if (err)
2002 goto done;
2004 } else {
2005 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
2006 goto done;
2008 done:
2009 free(path_objects);
2010 free(object_dir);
2011 if (err) {
2012 free(*id);
2013 *id = NULL;
2014 } else if (*id == NULL) {
2015 switch (obj_type) {
2016 case GOT_OBJ_TYPE_BLOB:
2017 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2018 GOT_OBJ_LABEL_BLOB, id_str_prefix);
2019 break;
2020 case GOT_OBJ_TYPE_TREE:
2021 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2022 GOT_OBJ_LABEL_TREE, id_str_prefix);
2023 break;
2024 case GOT_OBJ_TYPE_COMMIT:
2025 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2026 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
2027 break;
2028 case GOT_OBJ_TYPE_TAG:
2029 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2030 GOT_OBJ_LABEL_TAG, id_str_prefix);
2031 break;
2032 default:
2033 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
2034 break;
2038 return err;
2041 const struct got_error *
2042 got_repo_match_object_id(struct got_object_id **id, char **label,
2043 const char *id_str, int obj_type, struct got_reflist_head *refs,
2044 struct got_repository *repo)
2046 const struct got_error *err;
2047 struct got_tag_object *tag;
2048 struct got_reference *ref = NULL;
2050 *id = NULL;
2051 if (label)
2052 *label = NULL;
2054 if (refs) {
2055 err = got_repo_object_match_tag(&tag, id_str, obj_type,
2056 refs, repo);
2057 if (err == NULL) {
2058 *id = got_object_id_dup(
2059 got_object_tag_get_object_id(tag));
2060 if (*id == NULL)
2061 err = got_error_from_errno("got_object_id_dup");
2062 else if (label && asprintf(label, "refs/tags/%s",
2063 got_object_tag_get_name(tag)) == -1) {
2064 err = got_error_from_errno("asprintf");
2065 free(*id);
2066 *id = NULL;
2068 got_object_tag_close(tag);
2069 return err;
2070 } else if (err->code != GOT_ERR_OBJ_TYPE &&
2071 err->code != GOT_ERR_NO_OBJ)
2072 return err;
2075 err = got_ref_open(&ref, repo, id_str, 0);
2076 if (err == NULL) {
2077 err = got_ref_resolve(id, repo, ref);
2078 if (err)
2079 goto done;
2080 if (label) {
2081 *label = strdup(got_ref_get_name(ref));
2082 if (*label == NULL) {
2083 err = got_error_from_errno("strdup");
2084 goto done;
2087 } else {
2088 if (err->code != GOT_ERR_NOT_REF &&
2089 err->code != GOT_ERR_BAD_REF_NAME)
2090 goto done;
2091 err = got_repo_match_object_id_prefix(id, id_str,
2092 obj_type, repo);
2093 if (err) {
2094 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
2095 err = got_error_not_ref(id_str);
2096 goto done;
2098 if (label) {
2099 err = got_object_id_str(label, *id);
2100 if (*label == NULL) {
2101 err = got_error_from_errno("strdup");
2102 goto done;
2106 done:
2107 if (ref)
2108 got_ref_close(ref);
2109 return err;
2112 const struct got_error *
2113 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
2114 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
2116 const struct got_error *err = NULL;
2117 struct got_reflist_entry *re;
2118 struct got_object_id *tag_id;
2119 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
2121 *tag = NULL;
2123 TAILQ_FOREACH(re, refs, entry) {
2124 const char *refname;
2125 refname = got_ref_get_name(re->ref);
2126 if (got_ref_is_symbolic(re->ref))
2127 continue;
2128 if (strncmp(refname, "refs/tags/", 10) != 0)
2129 continue;
2130 if (!name_is_absolute)
2131 refname += strlen("refs/tags/");
2132 if (strcmp(refname, name) != 0)
2133 continue;
2134 err = got_ref_resolve(&tag_id, repo, re->ref);
2135 if (err)
2136 break;
2137 err = got_object_open_as_tag(tag, repo, tag_id);
2138 free(tag_id);
2139 if (err)
2140 break;
2141 if (obj_type == GOT_OBJ_TYPE_ANY ||
2142 got_object_tag_get_object_type(*tag) == obj_type)
2143 break;
2144 got_object_tag_close(*tag);
2145 *tag = NULL;
2148 if (err == NULL && *tag == NULL)
2149 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2150 GOT_OBJ_LABEL_TAG, name);
2151 return err;
2154 const struct got_error *
2155 got_repo_find_object_id(struct got_object_id *id, struct got_repository *repo)
2157 const struct got_error *err;
2158 struct got_object_id *matched_id = NULL;
2159 char *id_str = NULL;
2161 err = got_object_id_str(&id_str, id);
2162 if (err)
2163 return err;
2165 err = got_repo_match_object_id_prefix(&matched_id, id_str,
2166 GOT_OBJ_TYPE_ANY, repo);
2167 free(id_str);
2168 return err;
2171 static const struct got_error *
2172 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2173 const char *name, mode_t mode, struct got_object_id *blob_id)
2175 const struct got_error *err = NULL;
2177 *new_te = NULL;
2179 *new_te = calloc(1, sizeof(**new_te));
2180 if (*new_te == NULL)
2181 return got_error_from_errno("calloc");
2183 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2184 sizeof((*new_te)->name)) {
2185 err = got_error(GOT_ERR_NO_SPACE);
2186 goto done;
2189 if (S_ISLNK(mode)) {
2190 (*new_te)->mode = S_IFLNK;
2191 } else {
2192 (*new_te)->mode = S_IFREG;
2193 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2195 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2196 done:
2197 if (err && *new_te) {
2198 free(*new_te);
2199 *new_te = NULL;
2201 return err;
2204 static const struct got_error *
2205 import_file(struct got_tree_entry **new_te, struct dirent *de,
2206 const char *path, struct got_repository *repo)
2208 const struct got_error *err;
2209 struct got_object_id *blob_id = NULL;
2210 char *filepath;
2211 struct stat sb;
2213 if (asprintf(&filepath, "%s%s%s", path,
2214 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2215 return got_error_from_errno("asprintf");
2217 if (lstat(filepath, &sb) != 0) {
2218 err = got_error_from_errno2("lstat", path);
2219 goto done;
2222 err = got_object_blob_create(&blob_id, filepath, repo);
2223 if (err)
2224 goto done;
2226 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2227 blob_id);
2228 done:
2229 free(filepath);
2230 if (err)
2231 free(blob_id);
2232 return err;
2235 static const struct got_error *
2236 insert_tree_entry(struct got_tree_entry *new_te,
2237 struct got_pathlist_head *paths)
2239 const struct got_error *err = NULL;
2240 struct got_pathlist_entry *new_pe;
2242 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2243 if (err)
2244 return err;
2245 if (new_pe == NULL)
2246 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2247 return NULL;
2250 static const struct got_error *write_tree(struct got_object_id **,
2251 const char *, struct got_pathlist_head *, struct got_repository *,
2252 got_repo_import_cb progress_cb, void *progress_arg);
2254 static const struct got_error *
2255 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2256 const char *path, struct got_pathlist_head *ignores,
2257 struct got_repository *repo,
2258 got_repo_import_cb progress_cb, void *progress_arg)
2260 const struct got_error *err;
2261 struct got_object_id *id = NULL;
2262 char *subdirpath;
2264 if (asprintf(&subdirpath, "%s%s%s", path,
2265 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2266 return got_error_from_errno("asprintf");
2268 (*new_te) = calloc(1, sizeof(**new_te));
2269 if (*new_te == NULL)
2270 return got_error_from_errno("calloc");
2271 (*new_te)->mode = S_IFDIR;
2272 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2273 sizeof((*new_te)->name)) {
2274 err = got_error(GOT_ERR_NO_SPACE);
2275 goto done;
2277 err = write_tree(&id, subdirpath, ignores, repo,
2278 progress_cb, progress_arg);
2279 if (err)
2280 goto done;
2281 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2283 done:
2284 free(id);
2285 free(subdirpath);
2286 if (err) {
2287 free(*new_te);
2288 *new_te = NULL;
2290 return err;
2293 static const struct got_error *
2294 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2295 struct got_pathlist_head *ignores, struct got_repository *repo,
2296 got_repo_import_cb progress_cb, void *progress_arg)
2298 const struct got_error *err = NULL;
2299 DIR *dir;
2300 struct dirent *de;
2301 int nentries;
2302 struct got_tree_entry *new_te = NULL;
2303 struct got_pathlist_head paths;
2304 struct got_pathlist_entry *pe;
2306 *new_tree_id = NULL;
2308 TAILQ_INIT(&paths);
2310 dir = opendir(path_dir);
2311 if (dir == NULL) {
2312 err = got_error_from_errno2("opendir", path_dir);
2313 goto done;
2316 nentries = 0;
2317 while ((de = readdir(dir)) != NULL) {
2318 int ignore = 0;
2319 int type;
2321 if (strcmp(de->d_name, ".") == 0 ||
2322 strcmp(de->d_name, "..") == 0)
2323 continue;
2325 err = got_path_dirent_type(&type, path_dir, de);
2326 if (err)
2327 goto done;
2329 TAILQ_FOREACH(pe, ignores, entry) {
2330 if (type == DT_DIR && pe->path_len > 0 &&
2331 pe->path[pe->path_len - 1] == '/') {
2332 char stripped[PATH_MAX];
2334 if (strlcpy(stripped, pe->path,
2335 sizeof(stripped)) >= sizeof(stripped)) {
2336 err = got_error(GOT_ERR_NO_SPACE);
2337 goto done;
2339 got_path_strip_trailing_slashes(stripped);
2340 if (fnmatch(stripped, de->d_name, 0) == 0) {
2341 ignore = 1;
2342 break;
2344 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2345 ignore = 1;
2346 break;
2349 if (ignore)
2350 continue;
2352 if (type == DT_DIR) {
2353 err = import_subdir(&new_te, de, path_dir,
2354 ignores, repo, progress_cb, progress_arg);
2355 if (err) {
2356 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2357 goto done;
2358 err = NULL;
2359 continue;
2361 } else if (type == DT_REG || type == DT_LNK) {
2362 err = import_file(&new_te, de, path_dir, repo);
2363 if (err)
2364 goto done;
2365 } else
2366 continue;
2368 err = insert_tree_entry(new_te, &paths);
2369 if (err)
2370 goto done;
2371 nentries++;
2374 if (TAILQ_EMPTY(&paths)) {
2375 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2376 "cannot create tree without any entries");
2377 goto done;
2380 TAILQ_FOREACH(pe, &paths, entry) {
2381 struct got_tree_entry *te = pe->data;
2382 char *path;
2383 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2384 continue;
2385 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2386 err = got_error_from_errno("asprintf");
2387 goto done;
2389 err = (*progress_cb)(progress_arg, path);
2390 free(path);
2391 if (err)
2392 goto done;
2395 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2396 done:
2397 if (dir)
2398 closedir(dir);
2399 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2400 return err;
2403 const struct got_error *
2404 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2405 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2406 struct got_repository *repo, got_repo_import_cb progress_cb,
2407 void *progress_arg)
2409 const struct got_error *err;
2410 struct got_object_id *new_tree_id;
2412 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2413 progress_cb, progress_arg);
2414 if (err)
2415 return err;
2417 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2418 author, time(NULL), author, time(NULL), logmsg, repo);
2419 free(new_tree_id);
2420 return err;
2423 const struct got_error *
2424 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2425 struct got_repository *repo)
2427 const struct got_error *err = NULL;
2428 char *path_objects = NULL, *path = NULL;
2429 DIR *dir = NULL;
2430 struct got_object_id id;
2431 int i;
2433 *nobjects = 0;
2434 *ondisk_size = 0;
2436 path_objects = got_repo_get_path_objects(repo);
2437 if (path_objects == NULL)
2438 return got_error_from_errno("got_repo_get_path_objects");
2440 for (i = 0; i <= 0xff; i++) {
2441 struct dirent *dent;
2443 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2444 err = got_error_from_errno("asprintf");
2445 break;
2448 dir = opendir(path);
2449 if (dir == NULL) {
2450 if (errno == ENOENT) {
2451 err = NULL;
2452 continue;
2454 err = got_error_from_errno2("opendir", path);
2455 break;
2458 while ((dent = readdir(dir)) != NULL) {
2459 char *id_str;
2460 int fd;
2461 struct stat sb;
2463 if (strcmp(dent->d_name, ".") == 0 ||
2464 strcmp(dent->d_name, "..") == 0)
2465 continue;
2467 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2468 err = got_error_from_errno("asprintf");
2469 goto done;
2472 if (!got_parse_object_id(&id, id_str, repo->algo)) {
2473 free(id_str);
2474 continue;
2476 free(id_str);
2478 err = got_object_open_loose_fd(&fd, &id, repo);
2479 if (err)
2480 goto done;
2482 if (fstat(fd, &sb) == -1) {
2483 err = got_error_from_errno("fstat");
2484 close(fd);
2485 goto done;
2487 (*nobjects)++;
2488 (*ondisk_size) += sb.st_size;
2490 if (close(fd) == -1) {
2491 err = got_error_from_errno("close");
2492 goto done;
2496 if (closedir(dir) != 0) {
2497 err = got_error_from_errno("closedir");
2498 goto done;
2500 dir = NULL;
2502 free(path);
2503 path = NULL;
2505 done:
2506 if (dir && closedir(dir) != 0 && err == NULL)
2507 err = got_error_from_errno("closedir");
2509 if (err) {
2510 *nobjects = 0;
2511 *ondisk_size = 0;
2513 free(path_objects);
2514 free(path);
2515 return err;
2518 const struct got_error *
2519 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2520 off_t *total_packsize, struct got_repository *repo)
2522 const struct got_error *err = NULL;
2523 DIR *packdir = NULL;
2524 struct dirent *dent;
2525 struct got_packidx *packidx = NULL;
2526 char *path_packidx;
2527 char *path_packfile;
2528 int packdir_fd;
2529 struct stat sb;
2531 *npackfiles = 0;
2532 *nobjects = 0;
2533 *total_packsize = 0;
2535 packdir_fd = openat(got_repo_get_fd(repo),
2536 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2537 if (packdir_fd == -1) {
2538 return got_error_from_errno_fmt("openat: %s/%s",
2539 got_repo_get_path_git_dir(repo),
2540 GOT_OBJECTS_PACK_DIR);
2543 packdir = fdopendir(packdir_fd);
2544 if (packdir == NULL) {
2545 err = got_error_from_errno("fdopendir");
2546 goto done;
2549 while ((dent = readdir(packdir)) != NULL) {
2550 if (!got_repo_is_packidx_filename(dent->d_name,
2551 strlen(dent->d_name)))
2552 continue;
2554 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2555 dent->d_name) == -1) {
2556 err = got_error_from_errno("asprintf");
2557 goto done;
2560 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2561 path_packidx, 0);
2562 free(path_packidx);
2563 if (err)
2564 goto done;
2566 if (fstat(packidx->fd, &sb) == -1)
2567 goto done;
2568 *total_packsize += sb.st_size;
2570 err = got_packidx_get_packfile_path(&path_packfile,
2571 packidx->path_packidx);
2572 if (err)
2573 goto done;
2575 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2576 0) == -1) {
2577 free(path_packfile);
2578 goto done;
2580 free(path_packfile);
2581 *total_packsize += sb.st_size;
2583 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2585 (*npackfiles)++;
2587 got_packidx_close(packidx);
2588 packidx = NULL;
2590 done:
2591 if (packidx)
2592 got_packidx_close(packidx);
2593 if (packdir && closedir(packdir) != 0 && err == NULL)
2594 err = got_error_from_errno("closedir");
2595 if (err) {
2596 *npackfiles = 0;
2597 *nobjects = 0;
2598 *total_packsize = 0;
2600 return err;
2603 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2604 got_packidx_bloom_filter_cmp);