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 close(packdir_fd);
1434 goto done;
1437 if (fstat(packdir_fd, &sb) == -1) {
1438 err = got_error_from_errno("fstat");
1439 goto done;
1441 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1442 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1444 while ((dent = readdir(packdir)) != NULL) {
1445 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1446 continue;
1448 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1449 dent->d_name) == -1) {
1450 err = got_error_from_errno("asprintf");
1451 path_packidx = NULL;
1452 break;
1455 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1456 if (err)
1457 break;
1459 done:
1460 if (err)
1461 free(path_packidx);
1462 if (packdir && closedir(packdir) != 0 && err == NULL)
1463 err = got_error_from_errno("closedir");
1464 return err;
1467 const struct got_error *
1468 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1469 struct got_repository *repo)
1471 const struct got_error *err;
1472 size_t i;
1474 *packidx = NULL;
1476 /* Search pack index cache. */
1477 for (i = 0; i < repo->pack_cache_size; i++) {
1478 if (repo->packidx_cache[i] == NULL)
1479 break;
1480 if (strcmp(repo->packidx_cache[i]->path_packidx,
1481 path_packidx) == 0) {
1482 *packidx = repo->packidx_cache[i];
1483 return NULL;
1486 /* No luck. Search the filesystem. */
1488 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1489 path_packidx, 0);
1490 if (err)
1491 return err;
1493 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1494 if (err)
1495 goto done;
1497 err = cache_packidx(repo, *packidx, path_packidx);
1498 done:
1499 if (err) {
1500 got_packidx_close(*packidx);
1501 *packidx = NULL;
1503 return err;
1506 static const struct got_error *
1507 read_packfile_hdr(int fd, struct got_packidx *packidx)
1509 const struct got_error *err = NULL;
1510 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1511 struct got_packfile_hdr hdr;
1512 ssize_t n;
1514 n = read(fd, &hdr, sizeof(hdr));
1515 if (n < 0)
1516 return got_error_from_errno("read");
1517 if (n != sizeof(hdr))
1518 return got_error(GOT_ERR_BAD_PACKFILE);
1520 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1521 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1522 be32toh(hdr.nobjects) != totobj)
1523 err = got_error(GOT_ERR_BAD_PACKFILE);
1525 return err;
1528 static const struct got_error *
1529 open_packfile(int *fd, struct got_repository *repo,
1530 const char *relpath, struct got_packidx *packidx)
1532 const struct got_error *err = NULL;
1534 *fd = openat(got_repo_get_fd(repo), relpath,
1535 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1536 if (*fd == -1)
1537 return got_error_from_errno_fmt("openat: %s/%s",
1538 got_repo_get_path_git_dir(repo), relpath);
1540 if (packidx) {
1541 err = read_packfile_hdr(*fd, packidx);
1542 if (err) {
1543 close(*fd);
1544 *fd = -1;
1548 return err;
1551 const struct got_error *
1552 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1553 const char *path_packfile, struct got_packidx *packidx)
1555 const struct got_error *err = NULL;
1556 struct got_pack *pack = NULL;
1557 struct stat sb;
1558 size_t i;
1560 if (packp)
1561 *packp = NULL;
1563 for (i = 0; i < repo->pack_cache_size; i++) {
1564 pack = &repo->packs[i];
1565 if (pack->path_packfile == NULL)
1566 break;
1567 if (strcmp(pack->path_packfile, path_packfile) == 0)
1568 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1571 if (i == repo->pack_cache_size) {
1572 struct got_pack tmp;
1573 do {
1574 i--;
1575 } while (i > 0 && repo->pinned_pack >= 0 &&
1576 i == repo->pinned_pack);
1577 err = got_pack_close(&repo->packs[i]);
1578 if (err)
1579 return err;
1580 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1581 return got_error_from_errno("ftruncate");
1582 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1583 return got_error_from_errno("ftruncate");
1584 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1585 memcpy(&repo->packs[i], &repo->packs[0],
1586 sizeof(repo->packs[i]));
1587 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1588 if (repo->pinned_pack == 0)
1589 repo->pinned_pack = i;
1590 else if (repo->pinned_pack == i)
1591 repo->pinned_pack = 0;
1592 i = 0;
1595 pack = &repo->packs[i];
1597 pack->path_packfile = strdup(path_packfile);
1598 if (pack->path_packfile == NULL) {
1599 err = got_error_from_errno("strdup");
1600 goto done;
1603 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1604 if (err)
1605 goto done;
1607 if (fstat(pack->fd, &sb) != 0) {
1608 err = got_error_from_errno("fstat");
1609 goto done;
1611 pack->filesize = sb.st_size;
1613 pack->privsep_child = NULL;
1615 err = got_delta_cache_alloc(&pack->delta_cache);
1616 if (err)
1617 goto done;
1619 #ifndef GOT_PACK_NO_MMAP
1620 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1621 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1622 pack->fd, 0);
1623 if (pack->map == MAP_FAILED) {
1624 if (errno != ENOMEM) {
1625 err = got_error_from_errno("mmap");
1626 goto done;
1628 pack->map = NULL; /* fall back to read(2) */
1631 #endif
1632 done:
1633 if (err) {
1634 if (pack)
1635 got_pack_close(pack);
1636 } else if (packp)
1637 *packp = pack;
1638 return err;
1641 struct got_pack *
1642 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1644 struct got_pack *pack = NULL;
1645 size_t i;
1647 for (i = 0; i < repo->pack_cache_size; i++) {
1648 pack = &repo->packs[i];
1649 if (pack->path_packfile == NULL)
1650 break;
1651 if (strcmp(pack->path_packfile, path_packfile) == 0)
1652 return pack;
1655 return NULL;
1658 const struct got_error *
1659 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1660 struct got_pack *pack)
1662 size_t i;
1663 int pinned_pack = -1, pinned_packidx = -1;
1665 for (i = 0; i < repo->pack_cache_size; i++) {
1666 if (repo->packidx_cache[i] &&
1667 strcmp(repo->packidx_cache[i]->path_packidx,
1668 packidx->path_packidx) == 0)
1669 pinned_packidx = i;
1670 if (repo->packs[i].path_packfile &&
1671 strcmp(repo->packs[i].path_packfile,
1672 pack->path_packfile) == 0)
1673 pinned_pack = i;
1676 if (pinned_packidx == -1 || pinned_pack == -1)
1677 return got_error(GOT_ERR_PIN_PACK);
1679 repo->pinned_pack = pinned_pack;
1680 repo->pinned_packidx = pinned_packidx;
1681 if (repo->packs[pinned_pack].privsep_child)
1682 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1683 return NULL;
1686 struct got_pack *
1687 got_repo_get_pinned_pack(struct got_repository *repo)
1689 if (repo->pinned_pack >= 0 &&
1690 repo->pinned_pack < repo->pack_cache_size)
1691 return &repo->packs[repo->pinned_pack];
1693 return NULL;
1696 void
1697 got_repo_unpin_pack(struct got_repository *repo)
1699 repo->pinned_packidx = -1;
1700 repo->pinned_pack = -1;
1701 repo->pinned_pid = 0;
1704 const struct got_error *
1705 got_repo_init(const char *repo_path, const char *head_name)
1707 const struct got_error *err = NULL;
1708 const char *dirnames[] = {
1709 GOT_OBJECTS_DIR,
1710 GOT_OBJECTS_PACK_DIR,
1711 GOT_REFS_DIR,
1713 const char *description_str = "Unnamed repository; "
1714 "edit this file 'description' to name the repository.";
1715 const char *headref = "ref: refs/heads/";
1716 const char *gitconfig_str = "[core]\n"
1717 "\trepositoryformatversion = 0\n"
1718 "\tfilemode = true\n"
1719 "\tbare = true\n";
1720 char *headref_str, *path;
1721 size_t i;
1723 if (!got_path_dir_is_empty(repo_path))
1724 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1726 for (i = 0; i < nitems(dirnames); i++) {
1727 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1728 return got_error_from_errno("asprintf");
1730 err = got_path_mkdir(path);
1731 free(path);
1732 if (err)
1733 return err;
1736 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1737 return got_error_from_errno("asprintf");
1738 err = got_path_create_file(path, description_str);
1739 free(path);
1740 if (err)
1741 return err;
1743 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1744 return got_error_from_errno("asprintf");
1745 if (asprintf(&headref_str, "%s%s", headref,
1746 head_name ? head_name : "main") == -1) {
1747 free(path);
1748 return got_error_from_errno("asprintf");
1750 err = got_path_create_file(path, headref_str);
1751 free(headref_str);
1752 free(path);
1753 if (err)
1754 return err;
1756 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1757 return got_error_from_errno("asprintf");
1758 err = got_path_create_file(path, gitconfig_str);
1759 free(path);
1760 if (err)
1761 return err;
1763 return NULL;
1766 static const struct got_error *
1767 match_packed_object(struct got_object_id **unique_id,
1768 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1770 const struct got_error *err = NULL;
1771 struct got_object_id_queue matched_ids;
1772 struct got_pathlist_entry *pe;
1773 struct timespec tv;
1774 int retries = 0;
1775 const int max_retries = 10;
1777 STAILQ_INIT(&matched_ids);
1779 err = refresh_packidx_paths(repo);
1780 if (err)
1781 return err;
1784 * Opening objects while iterating over the pack-index path
1785 * list is racy. If the set of pack files in the repository
1786 * changes during loop iteration, refresh_packidx_paths() will
1787 * be called again, via got_object_get_type(), invalidating
1788 * the packidx_paths list we are iterating over.
1789 * To work around this we keep track of the current modification
1790 * time and retry the entire loop if it changes.
1792 retry:
1793 tv.tv_sec = repo->pack_path_mtime.tv_sec;
1794 tv.tv_nsec = repo->pack_path_mtime.tv_nsec;
1796 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1797 const char *path_packidx;
1798 struct got_packidx *packidx;
1799 struct got_object_qid *qid;
1802 * If the modification time of the 'objects/pack' directory
1803 * has changed then 'pe' could now be an invalid pointer.
1805 if (tv.tv_sec != repo->pack_path_mtime.tv_sec ||
1806 tv.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1807 if (++retries > max_retries) {
1808 err = got_error_msg(GOT_ERR_TIMEOUT,
1809 "too many concurrent pack file "
1810 "modifications");
1811 goto done;
1813 goto retry;
1816 path_packidx = pe->path;
1818 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1819 path_packidx, 0);
1820 if (err)
1821 break;
1823 got_object_id_queue_free(&matched_ids);
1825 err = got_packidx_match_id_str_prefix(&matched_ids,
1826 packidx, id_str_prefix);
1827 if (err) {
1828 got_packidx_close(packidx);
1829 break;
1831 err = got_packidx_close(packidx);
1832 if (err)
1833 break;
1835 STAILQ_FOREACH(qid, &matched_ids, entry) {
1836 if (obj_type != GOT_OBJ_TYPE_ANY) {
1837 int matched_type;
1838 err = got_object_get_type(&matched_type, repo,
1839 &qid->id);
1840 if (err)
1841 goto done;
1842 if (matched_type != obj_type)
1843 continue;
1845 if (*unique_id == NULL) {
1846 *unique_id = got_object_id_dup(&qid->id);
1847 if (*unique_id == NULL) {
1848 err = got_error_from_errno("malloc");
1849 goto done;
1851 } else {
1852 if (got_object_id_cmp(*unique_id,
1853 &qid->id) == 0)
1854 continue; /* packed multiple times */
1855 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1856 goto done;
1860 done:
1861 got_object_id_queue_free(&matched_ids);
1862 if (err) {
1863 free(*unique_id);
1864 *unique_id = NULL;
1866 return err;
1869 static const struct got_error *
1870 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1871 const char *object_dir, const char *id_str_prefix, int obj_type,
1872 struct got_repository *repo)
1874 const struct got_error *err = NULL;
1875 char *path, *id_str = NULL;
1876 DIR *dir = NULL;
1877 struct dirent *dent;
1878 struct got_object_id id;
1880 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1881 err = got_error_from_errno("asprintf");
1882 goto done;
1885 dir = opendir(path);
1886 if (dir == NULL) {
1887 if (errno == ENOENT) {
1888 err = NULL;
1889 goto done;
1891 err = got_error_from_errno2("opendir", path);
1892 goto done;
1894 while ((dent = readdir(dir)) != NULL) {
1895 int cmp;
1897 free(id_str);
1898 id_str = NULL;
1900 if (strcmp(dent->d_name, ".") == 0 ||
1901 strcmp(dent->d_name, "..") == 0)
1902 continue;
1904 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1905 err = got_error_from_errno("asprintf");
1906 goto done;
1909 if (!got_parse_object_id(&id, id_str, repo->algo))
1910 continue;
1913 * Directory entries do not necessarily appear in
1914 * sorted order, so we must iterate over all of them.
1916 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1917 if (cmp != 0)
1918 continue;
1920 if (*unique_id == NULL) {
1921 if (obj_type != GOT_OBJ_TYPE_ANY) {
1922 int matched_type;
1923 err = got_object_get_type(&matched_type, repo,
1924 &id);
1925 if (err)
1926 goto done;
1927 if (matched_type != obj_type)
1928 continue;
1930 *unique_id = got_object_id_dup(&id);
1931 if (*unique_id == NULL) {
1932 err = got_error_from_errno("got_object_id_dup");
1933 goto done;
1935 } else {
1936 if (got_object_id_cmp(*unique_id, &id) == 0)
1937 continue; /* both packed and loose */
1938 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1939 goto done;
1942 done:
1943 if (dir && closedir(dir) != 0 && err == NULL)
1944 err = got_error_from_errno("closedir");
1945 if (err) {
1946 free(*unique_id);
1947 *unique_id = NULL;
1949 free(id_str);
1950 free(path);
1951 return err;
1954 const struct got_error *
1955 got_repo_match_object_id_prefix(struct got_object_id **id,
1956 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1958 const struct got_error *err = NULL;
1959 char *path_objects = NULL, *object_dir = NULL;
1960 size_t len;
1961 int i;
1963 *id = NULL;
1965 path_objects = got_repo_get_path_objects(repo);
1967 len = strlen(id_str_prefix);
1968 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1969 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1970 goto done;
1973 for (i = 0; i < len; i++) {
1974 if (isxdigit((unsigned char)id_str_prefix[i]))
1975 continue;
1976 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1977 goto done;
1980 if (len >= 2) {
1981 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1982 if (err)
1983 goto done;
1984 object_dir = strndup(id_str_prefix, 2);
1985 if (object_dir == NULL) {
1986 err = got_error_from_errno("strdup");
1987 goto done;
1989 err = match_loose_object(id, path_objects, object_dir,
1990 id_str_prefix, obj_type, repo);
1991 } else if (len == 1) {
1992 int i;
1993 for (i = 0; i < 0xf; i++) {
1994 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1995 == -1) {
1996 err = got_error_from_errno("asprintf");
1997 goto done;
1999 err = match_packed_object(id, repo, object_dir,
2000 obj_type);
2001 if (err)
2002 goto done;
2003 err = match_loose_object(id, path_objects, object_dir,
2004 id_str_prefix, obj_type, repo);
2005 if (err)
2006 goto done;
2008 } else {
2009 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
2010 goto done;
2012 done:
2013 free(path_objects);
2014 free(object_dir);
2015 if (err) {
2016 free(*id);
2017 *id = NULL;
2018 } else if (*id == NULL) {
2019 switch (obj_type) {
2020 case GOT_OBJ_TYPE_BLOB:
2021 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2022 GOT_OBJ_LABEL_BLOB, id_str_prefix);
2023 break;
2024 case GOT_OBJ_TYPE_TREE:
2025 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2026 GOT_OBJ_LABEL_TREE, id_str_prefix);
2027 break;
2028 case GOT_OBJ_TYPE_COMMIT:
2029 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2030 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
2031 break;
2032 case GOT_OBJ_TYPE_TAG:
2033 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2034 GOT_OBJ_LABEL_TAG, id_str_prefix);
2035 break;
2036 default:
2037 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
2038 break;
2042 return err;
2045 const struct got_error *
2046 got_repo_match_object_id(struct got_object_id **id, char **label,
2047 const char *id_str, int obj_type, struct got_reflist_head *refs,
2048 struct got_repository *repo)
2050 const struct got_error *err;
2051 struct got_tag_object *tag;
2052 struct got_reference *ref = NULL;
2054 *id = NULL;
2055 if (label)
2056 *label = NULL;
2058 if (refs) {
2059 err = got_repo_object_match_tag(&tag, id_str, obj_type,
2060 refs, repo);
2061 if (err == NULL) {
2062 *id = got_object_id_dup(
2063 got_object_tag_get_object_id(tag));
2064 if (*id == NULL)
2065 err = got_error_from_errno("got_object_id_dup");
2066 else if (label && asprintf(label, "refs/tags/%s",
2067 got_object_tag_get_name(tag)) == -1) {
2068 err = got_error_from_errno("asprintf");
2069 free(*id);
2070 *id = NULL;
2072 got_object_tag_close(tag);
2073 return err;
2074 } else if (err->code != GOT_ERR_OBJ_TYPE &&
2075 err->code != GOT_ERR_NO_OBJ)
2076 return err;
2079 err = got_ref_open(&ref, repo, id_str, 0);
2080 if (err == NULL) {
2081 err = got_ref_resolve(id, repo, ref);
2082 if (err)
2083 goto done;
2084 if (label) {
2085 *label = strdup(got_ref_get_name(ref));
2086 if (*label == NULL) {
2087 err = got_error_from_errno("strdup");
2088 goto done;
2091 } else {
2092 if (err->code != GOT_ERR_NOT_REF &&
2093 err->code != GOT_ERR_BAD_REF_NAME)
2094 goto done;
2095 err = got_repo_match_object_id_prefix(id, id_str,
2096 obj_type, repo);
2097 if (err) {
2098 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
2099 err = got_error_not_ref(id_str);
2100 goto done;
2102 if (label) {
2103 err = got_object_id_str(label, *id);
2104 if (*label == NULL) {
2105 err = got_error_from_errno("strdup");
2106 goto done;
2110 done:
2111 if (ref)
2112 got_ref_close(ref);
2113 return err;
2116 const struct got_error *
2117 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
2118 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
2120 const struct got_error *err = NULL;
2121 struct got_reflist_entry *re;
2122 struct got_object_id *tag_id;
2123 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
2125 *tag = NULL;
2127 TAILQ_FOREACH(re, refs, entry) {
2128 const char *refname;
2129 refname = got_ref_get_name(re->ref);
2130 if (got_ref_is_symbolic(re->ref))
2131 continue;
2132 if (strncmp(refname, "refs/tags/", 10) != 0)
2133 continue;
2134 if (!name_is_absolute)
2135 refname += strlen("refs/tags/");
2136 if (strcmp(refname, name) != 0)
2137 continue;
2138 err = got_ref_resolve(&tag_id, repo, re->ref);
2139 if (err)
2140 break;
2141 err = got_object_open_as_tag(tag, repo, tag_id);
2142 free(tag_id);
2143 if (err)
2144 break;
2145 if (obj_type == GOT_OBJ_TYPE_ANY ||
2146 got_object_tag_get_object_type(*tag) == obj_type)
2147 break;
2148 got_object_tag_close(*tag);
2149 *tag = NULL;
2152 if (err == NULL && *tag == NULL)
2153 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2154 GOT_OBJ_LABEL_TAG, name);
2155 return err;
2158 const struct got_error *
2159 got_repo_find_object_id(struct got_object_id *id, struct got_repository *repo)
2161 const struct got_error *err;
2162 struct got_object_id *matched_id = NULL;
2163 char *id_str = NULL;
2165 err = got_object_id_str(&id_str, id);
2166 if (err)
2167 return err;
2169 err = got_repo_match_object_id_prefix(&matched_id, id_str,
2170 GOT_OBJ_TYPE_ANY, repo);
2171 free(id_str);
2172 return err;
2175 static const struct got_error *
2176 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2177 const char *name, mode_t mode, struct got_object_id *blob_id)
2179 const struct got_error *err = NULL;
2181 *new_te = NULL;
2183 *new_te = calloc(1, sizeof(**new_te));
2184 if (*new_te == NULL)
2185 return got_error_from_errno("calloc");
2187 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2188 sizeof((*new_te)->name)) {
2189 err = got_error(GOT_ERR_NO_SPACE);
2190 goto done;
2193 if (S_ISLNK(mode)) {
2194 (*new_te)->mode = S_IFLNK;
2195 } else {
2196 (*new_te)->mode = S_IFREG;
2197 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2199 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2200 done:
2201 if (err && *new_te) {
2202 free(*new_te);
2203 *new_te = NULL;
2205 return err;
2208 static const struct got_error *
2209 import_file(struct got_tree_entry **new_te, struct dirent *de,
2210 const char *path, struct got_repository *repo)
2212 const struct got_error *err;
2213 struct got_object_id *blob_id = NULL;
2214 char *filepath;
2215 struct stat sb;
2217 if (asprintf(&filepath, "%s%s%s", path,
2218 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2219 return got_error_from_errno("asprintf");
2221 if (lstat(filepath, &sb) != 0) {
2222 err = got_error_from_errno2("lstat", path);
2223 goto done;
2226 err = got_object_blob_create(&blob_id, filepath, repo);
2227 if (err)
2228 goto done;
2230 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2231 blob_id);
2232 done:
2233 free(filepath);
2234 if (err)
2235 free(blob_id);
2236 return err;
2239 static const struct got_error *
2240 insert_tree_entry(struct got_tree_entry *new_te,
2241 struct got_pathlist_head *paths)
2243 const struct got_error *err = NULL;
2244 struct got_pathlist_entry *new_pe;
2246 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2247 if (err)
2248 return err;
2249 if (new_pe == NULL)
2250 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2251 return NULL;
2254 static const struct got_error *write_tree(struct got_object_id **,
2255 const char *, struct got_pathlist_head *, struct got_repository *,
2256 got_repo_import_cb progress_cb, void *progress_arg);
2258 static const struct got_error *
2259 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2260 const char *path, struct got_pathlist_head *ignores,
2261 struct got_repository *repo,
2262 got_repo_import_cb progress_cb, void *progress_arg)
2264 const struct got_error *err;
2265 struct got_object_id *id = NULL;
2266 char *subdirpath;
2268 if (asprintf(&subdirpath, "%s%s%s", path,
2269 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2270 return got_error_from_errno("asprintf");
2272 (*new_te) = calloc(1, sizeof(**new_te));
2273 if (*new_te == NULL)
2274 return got_error_from_errno("calloc");
2275 (*new_te)->mode = S_IFDIR;
2276 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2277 sizeof((*new_te)->name)) {
2278 err = got_error(GOT_ERR_NO_SPACE);
2279 goto done;
2281 err = write_tree(&id, subdirpath, ignores, repo,
2282 progress_cb, progress_arg);
2283 if (err)
2284 goto done;
2285 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2287 done:
2288 free(id);
2289 free(subdirpath);
2290 if (err) {
2291 free(*new_te);
2292 *new_te = NULL;
2294 return err;
2297 static const struct got_error *
2298 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2299 struct got_pathlist_head *ignores, struct got_repository *repo,
2300 got_repo_import_cb progress_cb, void *progress_arg)
2302 const struct got_error *err = NULL;
2303 DIR *dir;
2304 struct dirent *de;
2305 int nentries;
2306 struct got_tree_entry *new_te = NULL;
2307 struct got_pathlist_head paths;
2308 struct got_pathlist_entry *pe;
2310 *new_tree_id = NULL;
2312 TAILQ_INIT(&paths);
2314 dir = opendir(path_dir);
2315 if (dir == NULL) {
2316 err = got_error_from_errno2("opendir", path_dir);
2317 goto done;
2320 nentries = 0;
2321 while ((de = readdir(dir)) != NULL) {
2322 int ignore = 0;
2323 int type;
2325 if (strcmp(de->d_name, ".") == 0 ||
2326 strcmp(de->d_name, "..") == 0)
2327 continue;
2329 err = got_path_dirent_type(&type, path_dir, de);
2330 if (err)
2331 goto done;
2333 TAILQ_FOREACH(pe, ignores, entry) {
2334 if (type == DT_DIR && pe->path_len > 0 &&
2335 pe->path[pe->path_len - 1] == '/') {
2336 char stripped[PATH_MAX];
2338 if (strlcpy(stripped, pe->path,
2339 sizeof(stripped)) >= sizeof(stripped)) {
2340 err = got_error(GOT_ERR_NO_SPACE);
2341 goto done;
2343 got_path_strip_trailing_slashes(stripped);
2344 if (fnmatch(stripped, de->d_name, 0) == 0) {
2345 ignore = 1;
2346 break;
2348 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2349 ignore = 1;
2350 break;
2353 if (ignore)
2354 continue;
2356 if (type == DT_DIR) {
2357 err = import_subdir(&new_te, de, path_dir,
2358 ignores, repo, progress_cb, progress_arg);
2359 if (err) {
2360 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2361 goto done;
2362 err = NULL;
2363 continue;
2365 } else if (type == DT_REG || type == DT_LNK) {
2366 err = import_file(&new_te, de, path_dir, repo);
2367 if (err)
2368 goto done;
2369 } else
2370 continue;
2372 err = insert_tree_entry(new_te, &paths);
2373 if (err)
2374 goto done;
2375 nentries++;
2378 if (TAILQ_EMPTY(&paths)) {
2379 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2380 "cannot create tree without any entries");
2381 goto done;
2384 TAILQ_FOREACH(pe, &paths, entry) {
2385 struct got_tree_entry *te = pe->data;
2386 char *path;
2387 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2388 continue;
2389 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2390 err = got_error_from_errno("asprintf");
2391 goto done;
2393 err = (*progress_cb)(progress_arg, path);
2394 free(path);
2395 if (err)
2396 goto done;
2399 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2400 done:
2401 if (dir)
2402 closedir(dir);
2403 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2404 return err;
2407 const struct got_error *
2408 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2409 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2410 struct got_repository *repo, got_repo_import_cb progress_cb,
2411 void *progress_arg)
2413 const struct got_error *err;
2414 struct got_object_id *new_tree_id;
2416 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2417 progress_cb, progress_arg);
2418 if (err)
2419 return err;
2421 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2422 author, time(NULL), author, time(NULL), logmsg, repo);
2423 free(new_tree_id);
2424 return err;
2427 const struct got_error *
2428 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2429 struct got_repository *repo)
2431 const struct got_error *err = NULL;
2432 char *path_objects = NULL, *path = NULL;
2433 DIR *dir = NULL;
2434 struct got_object_id id;
2435 int i;
2437 *nobjects = 0;
2438 *ondisk_size = 0;
2440 path_objects = got_repo_get_path_objects(repo);
2441 if (path_objects == NULL)
2442 return got_error_from_errno("got_repo_get_path_objects");
2444 for (i = 0; i <= 0xff; i++) {
2445 struct dirent *dent;
2447 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2448 err = got_error_from_errno("asprintf");
2449 break;
2452 dir = opendir(path);
2453 if (dir == NULL) {
2454 if (errno == ENOENT) {
2455 err = NULL;
2456 continue;
2458 err = got_error_from_errno2("opendir", path);
2459 break;
2462 while ((dent = readdir(dir)) != NULL) {
2463 char *id_str;
2464 int fd;
2465 struct stat sb;
2467 if (strcmp(dent->d_name, ".") == 0 ||
2468 strcmp(dent->d_name, "..") == 0)
2469 continue;
2471 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2472 err = got_error_from_errno("asprintf");
2473 goto done;
2476 if (!got_parse_object_id(&id, id_str, repo->algo)) {
2477 free(id_str);
2478 continue;
2480 free(id_str);
2482 err = got_object_open_loose_fd(&fd, &id, repo);
2483 if (err)
2484 goto done;
2486 if (fstat(fd, &sb) == -1) {
2487 err = got_error_from_errno("fstat");
2488 close(fd);
2489 goto done;
2491 (*nobjects)++;
2492 (*ondisk_size) += sb.st_size;
2494 if (close(fd) == -1) {
2495 err = got_error_from_errno("close");
2496 goto done;
2500 if (closedir(dir) != 0) {
2501 err = got_error_from_errno("closedir");
2502 goto done;
2504 dir = NULL;
2506 free(path);
2507 path = NULL;
2509 done:
2510 if (dir && closedir(dir) != 0 && err == NULL)
2511 err = got_error_from_errno("closedir");
2513 if (err) {
2514 *nobjects = 0;
2515 *ondisk_size = 0;
2517 free(path_objects);
2518 free(path);
2519 return err;
2522 const struct got_error *
2523 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2524 off_t *total_packsize, struct got_repository *repo)
2526 const struct got_error *err = NULL;
2527 DIR *packdir = NULL;
2528 struct dirent *dent;
2529 struct got_packidx *packidx = NULL;
2530 char *path_packidx;
2531 char *path_packfile;
2532 int packdir_fd;
2533 struct stat sb;
2535 *npackfiles = 0;
2536 *nobjects = 0;
2537 *total_packsize = 0;
2539 packdir_fd = openat(got_repo_get_fd(repo),
2540 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2541 if (packdir_fd == -1) {
2542 return got_error_from_errno_fmt("openat: %s/%s",
2543 got_repo_get_path_git_dir(repo),
2544 GOT_OBJECTS_PACK_DIR);
2547 packdir = fdopendir(packdir_fd);
2548 if (packdir == NULL) {
2549 err = got_error_from_errno("fdopendir");
2550 close(packdir_fd);
2551 goto done;
2554 while ((dent = readdir(packdir)) != NULL) {
2555 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2556 continue;
2558 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2559 dent->d_name) == -1) {
2560 err = got_error_from_errno("asprintf");
2561 goto done;
2564 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2565 path_packidx, 0);
2566 free(path_packidx);
2567 if (err)
2568 goto done;
2570 if (fstat(packidx->fd, &sb) == -1)
2571 goto done;
2572 *total_packsize += sb.st_size;
2574 err = got_packidx_get_packfile_path(&path_packfile,
2575 packidx->path_packidx);
2576 if (err)
2577 goto done;
2579 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2580 0) == -1) {
2581 free(path_packfile);
2582 goto done;
2584 free(path_packfile);
2585 *total_packsize += sb.st_size;
2587 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2589 (*npackfiles)++;
2591 got_packidx_close(packidx);
2592 packidx = NULL;
2594 done:
2595 if (packidx)
2596 got_packidx_close(packidx);
2597 if (packdir && closedir(packdir) != 0 && err == NULL)
2598 err = got_error_from_errno("closedir");
2599 if (err) {
2600 *npackfiles = 0;
2601 *nobjects = 0;
2602 *total_packsize = 0;
2604 return err;
2607 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2608 got_packidx_bloom_filter_cmp);