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->nfetch_branches = repo->nfetch_branches;
949 if (repo->fetch_branches) {
950 new->fetch_branches = calloc(repo->nfetch_branches,
951 sizeof(char *));
952 if (new->fetch_branches == NULL) {
953 err = got_error_from_errno("calloc");
954 goto done;
956 for (i = 0; i < repo->nfetch_branches; i++) {
957 new->fetch_branches[i] = strdup(
958 repo->fetch_branches[i]);
959 if (new->fetch_branches[i] == NULL) {
960 err = got_error_from_errno("strdup");
961 goto done;
966 new->nsend_branches = repo->nsend_branches;
967 if (repo->send_branches) {
968 new->send_branches = calloc(repo->nsend_branches,
969 sizeof(char *));
970 if (new->send_branches == NULL) {
971 err = got_error_from_errno("calloc");
972 goto done;
974 for (i = 0; i < repo->nsend_branches; i++) {
975 new->send_branches[i] = strdup(
976 repo->send_branches[i]);
977 if (new->send_branches[i] == NULL) {
978 err = got_error_from_errno("strdup");
979 goto done;
984 new->nfetch_refs = repo->nfetch_refs;
985 if (repo->fetch_refs) {
986 new->fetch_refs = calloc(repo->nfetch_refs,
987 sizeof(char *));
988 if (new->fetch_refs == NULL) {
989 err = got_error_from_errno("calloc");
990 goto done;
992 for (i = 0; i < repo->nfetch_refs; i++) {
993 new->fetch_refs[i] = strdup(
994 repo->fetch_refs[i]);
995 if (new->fetch_refs[i] == NULL) {
996 err = got_error_from_errno("strdup");
997 goto done;
1001 done:
1002 if (err) {
1003 got_repo_free_remote_repo_data(new);
1004 free(new);
1005 } else
1006 *newp = new;
1008 return err;
1011 void
1012 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
1014 int i;
1016 if (repo == NULL)
1017 return;
1019 free(repo->name);
1020 repo->name = NULL;
1021 free(repo->fetch_url);
1022 repo->fetch_url = NULL;
1023 free(repo->send_url);
1024 repo->send_url = NULL;
1025 for (i = 0; i < repo->nfetch_branches; i++)
1026 free(repo->fetch_branches[i]);
1027 free(repo->fetch_branches);
1028 repo->fetch_branches = NULL;
1029 repo->nfetch_branches = 0;
1030 for (i = 0; i < repo->nsend_branches; i++)
1031 free(repo->send_branches[i]);
1032 free(repo->send_branches);
1033 repo->send_branches = NULL;
1034 repo->nsend_branches = 0;
1035 for (i = 0; i < repo->nfetch_refs; i++)
1036 free(repo->fetch_refs[i]);
1037 free(repo->fetch_refs);
1038 repo->fetch_refs = NULL;
1039 repo->nfetch_refs = 0;
1042 const struct got_error *
1043 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
1044 const char *input_path)
1046 const struct got_error *err = NULL;
1047 const char *repo_abspath = NULL;
1048 size_t repolen, len;
1049 char *canonpath, *path = NULL;
1051 *in_repo_path = NULL;
1053 canonpath = strdup(input_path);
1054 if (canonpath == NULL) {
1055 err = got_error_from_errno("strdup");
1056 goto done;
1058 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
1059 if (err)
1060 goto done;
1062 repo_abspath = got_repo_get_path(repo);
1064 if (canonpath[0] == '\0') {
1065 path = strdup(canonpath);
1066 if (path == NULL) {
1067 err = got_error_from_errno("strdup");
1068 goto done;
1070 } else {
1071 path = realpath(canonpath, NULL);
1072 if (path == NULL) {
1073 if (errno != ENOENT) {
1074 err = got_error_from_errno2("realpath",
1075 canonpath);
1076 goto done;
1079 * Path is not on disk.
1080 * Assume it is already relative to repository root.
1082 path = strdup(canonpath);
1083 if (path == NULL) {
1084 err = got_error_from_errno("strdup");
1085 goto done;
1089 repolen = strlen(repo_abspath);
1090 len = strlen(path);
1093 if (strcmp(path, repo_abspath) == 0) {
1094 free(path);
1095 path = strdup("");
1096 if (path == NULL) {
1097 err = got_error_from_errno("strdup");
1098 goto done;
1100 } else if (len > repolen &&
1101 got_path_is_child(path, repo_abspath, repolen)) {
1102 /* Matched an on-disk path inside repository. */
1103 if (got_repo_is_bare(repo)) {
1105 * Matched an on-disk path inside repository
1106 * database. Treat input as repository-relative.
1108 free(path);
1109 path = canonpath;
1110 canonpath = NULL;
1111 } else {
1112 char *child;
1113 /* Strip common prefix with repository path. */
1114 err = got_path_skip_common_ancestor(&child,
1115 repo_abspath, path);
1116 if (err)
1117 goto done;
1118 free(path);
1119 path = child;
1121 } else {
1123 * Matched unrelated on-disk path.
1124 * Treat input as repository-relative.
1126 free(path);
1127 path = canonpath;
1128 canonpath = NULL;
1132 /* Make in-repository path absolute */
1133 if (path[0] != '/') {
1134 char *abspath;
1135 if (asprintf(&abspath, "/%s", path) == -1) {
1136 err = got_error_from_errno("asprintf");
1137 goto done;
1139 free(path);
1140 path = abspath;
1143 done:
1144 free(canonpath);
1145 if (err)
1146 free(path);
1147 else
1148 *in_repo_path = path;
1149 return err;
1152 static const struct got_error *
1153 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1154 const char *path_packidx)
1156 const struct got_error *err = NULL;
1157 size_t i;
1159 for (i = 0; i < repo->pack_cache_size; i++) {
1160 if (repo->packidx_cache[i] == NULL)
1161 break;
1162 if (strcmp(repo->packidx_cache[i]->path_packidx,
1163 path_packidx) == 0) {
1164 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1167 if (i == repo->pack_cache_size) {
1168 do {
1169 i--;
1170 } while (i > 0 && repo->pinned_packidx >= 0 &&
1171 i == repo->pinned_packidx);
1172 err = got_packidx_close(repo->packidx_cache[i]);
1173 if (err)
1174 return err;
1177 repo->packidx_cache[i] = packidx;
1179 return NULL;
1182 int
1183 got_repo_is_packidx_filename(const char *name, size_t len)
1185 if (len != GOT_PACKIDX_NAMELEN)
1186 return 0;
1188 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1189 return 0;
1191 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1192 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1193 return 0;
1195 return 1;
1198 static struct got_packidx_bloom_filter *
1199 get_packidx_bloom_filter(struct got_repository *repo,
1200 const char *path, size_t path_len)
1202 struct got_packidx_bloom_filter key;
1204 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1205 return NULL; /* XXX */
1206 key.path_len = path_len;
1208 return RB_FIND(got_packidx_bloom_filter_tree,
1209 &repo->packidx_bloom_filters, &key);
1212 int
1213 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1214 const char *path_packidx, struct got_object_id *id)
1216 struct got_packidx_bloom_filter *bf;
1218 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1219 if (bf)
1220 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1222 /* No bloom filter means this pack index must be searched. */
1223 return 1;
1226 static const struct got_error *
1227 add_packidx_bloom_filter(struct got_repository *repo,
1228 struct got_packidx *packidx, const char *path_packidx)
1230 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1231 struct got_packidx_bloom_filter *bf;
1232 size_t len;
1235 * Don't use bloom filters for very large pack index files.
1236 * Large pack files will contain a relatively large fraction
1237 * of our objects so we will likely need to visit them anyway.
1238 * The more objects a pack file contains the higher the probability
1239 * of a false-positive match from the bloom filter. And reading
1240 * all object IDs from a large pack index file can be expensive.
1242 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1243 return NULL;
1245 /* Do we already have a filter for this pack index? */
1246 if (get_packidx_bloom_filter(repo, path_packidx,
1247 strlen(path_packidx)) != NULL)
1248 return NULL;
1250 bf = calloc(1, sizeof(*bf));
1251 if (bf == NULL)
1252 return got_error_from_errno("calloc");
1253 bf->bloom = calloc(1, sizeof(*bf->bloom));
1254 if (bf->bloom == NULL) {
1255 free(bf);
1256 return got_error_from_errno("calloc");
1259 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1260 if (len >= sizeof(bf->path)) {
1261 free(bf->bloom);
1262 free(bf);
1263 return got_error(GOT_ERR_NO_SPACE);
1265 bf->path_len = len;
1267 /* Minimum size supported by our bloom filter is 1000 entries. */
1268 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1269 for (i = 0; i < nobjects; i++) {
1270 struct got_packidx_object_id *id;
1271 id = &packidx->hdr.sorted_ids[i];
1272 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1275 RB_INSERT(got_packidx_bloom_filter_tree,
1276 &repo->packidx_bloom_filters, bf);
1277 return NULL;
1280 static void
1281 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1283 struct got_pathlist_entry *pe;
1285 while (!TAILQ_EMPTY(packidx_paths)) {
1286 pe = TAILQ_FIRST(packidx_paths);
1287 TAILQ_REMOVE(packidx_paths, pe, entry);
1288 free((char *)pe->path);
1289 free(pe);
1293 static const struct got_error *
1294 refresh_packidx_paths(struct got_repository *repo)
1296 const struct got_error *err = NULL;
1297 char *objects_pack_dir = NULL;
1298 struct stat sb;
1300 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1301 if (objects_pack_dir == NULL)
1302 return got_error_from_errno("got_repo_get_path_objects_pack");
1304 if (stat(objects_pack_dir, &sb) == -1) {
1305 if (errno != ENOENT) {
1306 err = got_error_from_errno2("stat", objects_pack_dir);
1307 goto done;
1309 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1310 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1311 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1312 purge_packidx_paths(&repo->packidx_paths);
1313 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1314 if (err)
1315 goto done;
1317 done:
1318 free(objects_pack_dir);
1319 return err;
1322 const struct got_error *
1323 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1324 struct got_repository *repo, struct got_object_id *id)
1326 const struct got_error *err;
1327 struct got_pathlist_entry *pe;
1328 size_t i;
1330 /* Search pack index cache. */
1331 for (i = 0; i < repo->pack_cache_size; i++) {
1332 if (repo->packidx_cache[i] == NULL)
1333 break;
1334 if (!got_repo_check_packidx_bloom_filter(repo,
1335 repo->packidx_cache[i]->path_packidx, id))
1336 continue; /* object will not be found in this index */
1337 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1338 if (*idx != -1) {
1339 *packidx = repo->packidx_cache[i];
1341 * Move this cache entry to the front. Repeatedly
1342 * searching a wrong pack index can be expensive.
1344 if (i > 0) {
1345 memmove(&repo->packidx_cache[1],
1346 &repo->packidx_cache[0],
1347 i * sizeof(repo->packidx_cache[0]));
1348 repo->packidx_cache[0] = *packidx;
1349 if (repo->pinned_packidx >= 0 &&
1350 repo->pinned_packidx < i)
1351 repo->pinned_packidx++;
1352 else if (repo->pinned_packidx == i)
1353 repo->pinned_packidx = 0;
1355 return NULL;
1358 /* No luck. Search the filesystem. */
1360 err = refresh_packidx_paths(repo);
1361 if (err)
1362 return err;
1364 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1365 const char *path_packidx = pe->path;
1366 int is_cached = 0;
1368 if (!got_repo_check_packidx_bloom_filter(repo,
1369 pe->path, id))
1370 continue; /* object will not be found in this index */
1372 for (i = 0; i < repo->pack_cache_size; i++) {
1373 if (repo->packidx_cache[i] == NULL)
1374 break;
1375 if (strcmp(repo->packidx_cache[i]->path_packidx,
1376 path_packidx) == 0) {
1377 is_cached = 1;
1378 break;
1381 if (is_cached)
1382 continue; /* already searched */
1384 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1385 path_packidx, 0);
1386 if (err)
1387 goto done;
1389 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1390 if (err)
1391 goto done;
1393 err = cache_packidx(repo, *packidx, path_packidx);
1394 if (err)
1395 goto done;
1397 *idx = got_packidx_get_object_idx(*packidx, id);
1398 if (*idx != -1) {
1399 err = NULL; /* found the object */
1400 goto done;
1404 err = got_error_no_obj(id);
1405 done:
1406 return err;
1409 const struct got_error *
1410 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1411 struct got_repository *repo)
1413 const struct got_error *err = NULL;
1414 DIR *packdir = NULL;
1415 struct dirent *dent;
1416 char *path_packidx = NULL;
1417 int packdir_fd;
1418 struct stat sb;
1420 packdir_fd = openat(got_repo_get_fd(repo),
1421 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1422 if (packdir_fd == -1) {
1423 return got_error_from_errno_fmt("openat: %s/%s",
1424 got_repo_get_path_git_dir(repo),
1425 GOT_OBJECTS_PACK_DIR);
1428 packdir = fdopendir(packdir_fd);
1429 if (packdir == NULL) {
1430 err = got_error_from_errno("fdopendir");
1431 goto done;
1434 if (fstat(packdir_fd, &sb) == -1) {
1435 err = got_error_from_errno("fstat");
1436 goto done;
1438 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1439 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1441 while ((dent = readdir(packdir)) != NULL) {
1442 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1443 continue;
1445 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1446 dent->d_name) == -1) {
1447 err = got_error_from_errno("asprintf");
1448 path_packidx = NULL;
1449 break;
1452 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1453 if (err)
1454 break;
1456 done:
1457 if (err)
1458 free(path_packidx);
1459 if (packdir && closedir(packdir) != 0 && err == NULL)
1460 err = got_error_from_errno("closedir");
1461 return err;
1464 const struct got_error *
1465 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1466 struct got_repository *repo)
1468 const struct got_error *err;
1469 size_t i;
1471 *packidx = NULL;
1473 /* Search pack index cache. */
1474 for (i = 0; i < repo->pack_cache_size; i++) {
1475 if (repo->packidx_cache[i] == NULL)
1476 break;
1477 if (strcmp(repo->packidx_cache[i]->path_packidx,
1478 path_packidx) == 0) {
1479 *packidx = repo->packidx_cache[i];
1480 return NULL;
1483 /* No luck. Search the filesystem. */
1485 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1486 path_packidx, 0);
1487 if (err)
1488 return err;
1490 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1491 if (err)
1492 goto done;
1494 err = cache_packidx(repo, *packidx, path_packidx);
1495 done:
1496 if (err) {
1497 got_packidx_close(*packidx);
1498 *packidx = NULL;
1500 return err;
1503 static const struct got_error *
1504 read_packfile_hdr(int fd, struct got_packidx *packidx)
1506 const struct got_error *err = NULL;
1507 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1508 struct got_packfile_hdr hdr;
1509 ssize_t n;
1511 n = read(fd, &hdr, sizeof(hdr));
1512 if (n < 0)
1513 return got_error_from_errno("read");
1514 if (n != sizeof(hdr))
1515 return got_error(GOT_ERR_BAD_PACKFILE);
1517 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1518 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1519 be32toh(hdr.nobjects) != totobj)
1520 err = got_error(GOT_ERR_BAD_PACKFILE);
1522 return err;
1525 static const struct got_error *
1526 open_packfile(int *fd, struct got_repository *repo,
1527 const char *relpath, struct got_packidx *packidx)
1529 const struct got_error *err = NULL;
1531 *fd = openat(got_repo_get_fd(repo), relpath,
1532 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1533 if (*fd == -1)
1534 return got_error_from_errno_fmt("openat: %s/%s",
1535 got_repo_get_path_git_dir(repo), relpath);
1537 if (packidx) {
1538 err = read_packfile_hdr(*fd, packidx);
1539 if (err) {
1540 close(*fd);
1541 *fd = -1;
1545 return err;
1548 const struct got_error *
1549 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1550 const char *path_packfile, struct got_packidx *packidx)
1552 const struct got_error *err = NULL;
1553 struct got_pack *pack = NULL;
1554 struct stat sb;
1555 size_t i;
1557 if (packp)
1558 *packp = NULL;
1560 for (i = 0; i < repo->pack_cache_size; i++) {
1561 pack = &repo->packs[i];
1562 if (pack->path_packfile == NULL)
1563 break;
1564 if (strcmp(pack->path_packfile, path_packfile) == 0)
1565 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1568 if (i == repo->pack_cache_size) {
1569 struct got_pack tmp;
1570 do {
1571 i--;
1572 } while (i > 0 && repo->pinned_pack >= 0 &&
1573 i == repo->pinned_pack);
1574 err = got_pack_close(&repo->packs[i]);
1575 if (err)
1576 return err;
1577 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1578 return got_error_from_errno("ftruncate");
1579 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1580 return got_error_from_errno("ftruncate");
1581 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1582 memcpy(&repo->packs[i], &repo->packs[0],
1583 sizeof(repo->packs[i]));
1584 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1585 if (repo->pinned_pack == 0)
1586 repo->pinned_pack = i;
1587 else if (repo->pinned_pack == i)
1588 repo->pinned_pack = 0;
1589 i = 0;
1592 pack = &repo->packs[i];
1594 pack->path_packfile = strdup(path_packfile);
1595 if (pack->path_packfile == NULL) {
1596 err = got_error_from_errno("strdup");
1597 goto done;
1600 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1601 if (err)
1602 goto done;
1604 if (fstat(pack->fd, &sb) != 0) {
1605 err = got_error_from_errno("fstat");
1606 goto done;
1608 pack->filesize = sb.st_size;
1610 pack->privsep_child = NULL;
1612 err = got_delta_cache_alloc(&pack->delta_cache);
1613 if (err)
1614 goto done;
1616 #ifndef GOT_PACK_NO_MMAP
1617 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1618 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1619 pack->fd, 0);
1620 if (pack->map == MAP_FAILED) {
1621 if (errno != ENOMEM) {
1622 err = got_error_from_errno("mmap");
1623 goto done;
1625 pack->map = NULL; /* fall back to read(2) */
1628 #endif
1629 done:
1630 if (err) {
1631 if (pack)
1632 got_pack_close(pack);
1633 } else if (packp)
1634 *packp = pack;
1635 return err;
1638 struct got_pack *
1639 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1641 struct got_pack *pack = NULL;
1642 size_t i;
1644 for (i = 0; i < repo->pack_cache_size; i++) {
1645 pack = &repo->packs[i];
1646 if (pack->path_packfile == NULL)
1647 break;
1648 if (strcmp(pack->path_packfile, path_packfile) == 0)
1649 return pack;
1652 return NULL;
1655 const struct got_error *
1656 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1657 struct got_pack *pack)
1659 size_t i;
1660 int pinned_pack = -1, pinned_packidx = -1;
1662 for (i = 0; i < repo->pack_cache_size; i++) {
1663 if (repo->packidx_cache[i] &&
1664 strcmp(repo->packidx_cache[i]->path_packidx,
1665 packidx->path_packidx) == 0)
1666 pinned_packidx = i;
1667 if (repo->packs[i].path_packfile &&
1668 strcmp(repo->packs[i].path_packfile,
1669 pack->path_packfile) == 0)
1670 pinned_pack = i;
1673 if (pinned_packidx == -1 || pinned_pack == -1)
1674 return got_error(GOT_ERR_PIN_PACK);
1676 repo->pinned_pack = pinned_pack;
1677 repo->pinned_packidx = pinned_packidx;
1678 if (repo->packs[pinned_pack].privsep_child)
1679 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1680 return NULL;
1683 struct got_pack *
1684 got_repo_get_pinned_pack(struct got_repository *repo)
1686 if (repo->pinned_pack >= 0 &&
1687 repo->pinned_pack < repo->pack_cache_size)
1688 return &repo->packs[repo->pinned_pack];
1690 return NULL;
1693 void
1694 got_repo_unpin_pack(struct got_repository *repo)
1696 repo->pinned_packidx = -1;
1697 repo->pinned_pack = -1;
1698 repo->pinned_pid = 0;
1701 const struct got_error *
1702 got_repo_init(const char *repo_path, const char *head_name)
1704 const struct got_error *err = NULL;
1705 const char *dirnames[] = {
1706 GOT_OBJECTS_DIR,
1707 GOT_OBJECTS_PACK_DIR,
1708 GOT_REFS_DIR,
1710 const char *description_str = "Unnamed repository; "
1711 "edit this file 'description' to name the repository.";
1712 const char *headref = "ref: refs/heads/";
1713 const char *gitconfig_str = "[core]\n"
1714 "\trepositoryformatversion = 0\n"
1715 "\tfilemode = true\n"
1716 "\tbare = true\n";
1717 char *headref_str, *path;
1718 size_t i;
1720 if (!got_path_dir_is_empty(repo_path))
1721 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1723 for (i = 0; i < nitems(dirnames); i++) {
1724 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1725 return got_error_from_errno("asprintf");
1727 err = got_path_mkdir(path);
1728 free(path);
1729 if (err)
1730 return err;
1733 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1734 return got_error_from_errno("asprintf");
1735 err = got_path_create_file(path, description_str);
1736 free(path);
1737 if (err)
1738 return err;
1740 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1741 return got_error_from_errno("asprintf");
1742 if (asprintf(&headref_str, "%s%s", headref,
1743 head_name ? head_name : "main") == -1) {
1744 free(path);
1745 return got_error_from_errno("asprintf");
1747 err = got_path_create_file(path, headref_str);
1748 free(headref_str);
1749 free(path);
1750 if (err)
1751 return err;
1753 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1754 return got_error_from_errno("asprintf");
1755 err = got_path_create_file(path, gitconfig_str);
1756 free(path);
1757 if (err)
1758 return err;
1760 return NULL;
1763 static const struct got_error *
1764 match_packed_object(struct got_object_id **unique_id,
1765 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1767 const struct got_error *err = NULL;
1768 struct got_object_id_queue matched_ids;
1769 struct got_pathlist_entry *pe;
1771 STAILQ_INIT(&matched_ids);
1773 err = refresh_packidx_paths(repo);
1774 if (err)
1775 return err;
1777 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1778 const char *path_packidx = pe->path;
1779 struct got_packidx *packidx;
1780 struct got_object_qid *qid;
1782 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1783 path_packidx, 0);
1784 if (err)
1785 break;
1787 err = got_packidx_match_id_str_prefix(&matched_ids,
1788 packidx, id_str_prefix);
1789 if (err) {
1790 got_packidx_close(packidx);
1791 break;
1793 err = got_packidx_close(packidx);
1794 if (err)
1795 break;
1797 STAILQ_FOREACH(qid, &matched_ids, entry) {
1798 if (obj_type != GOT_OBJ_TYPE_ANY) {
1799 int matched_type;
1800 err = got_object_get_type(&matched_type, repo,
1801 &qid->id);
1802 if (err)
1803 goto done;
1804 if (matched_type != obj_type)
1805 continue;
1807 if (*unique_id == NULL) {
1808 *unique_id = got_object_id_dup(&qid->id);
1809 if (*unique_id == NULL) {
1810 err = got_error_from_errno("malloc");
1811 goto done;
1813 } else {
1814 if (got_object_id_cmp(*unique_id,
1815 &qid->id) == 0)
1816 continue; /* packed multiple times */
1817 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1818 goto done;
1822 done:
1823 got_object_id_queue_free(&matched_ids);
1824 if (err) {
1825 free(*unique_id);
1826 *unique_id = NULL;
1828 return err;
1831 static const struct got_error *
1832 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1833 const char *object_dir, const char *id_str_prefix, int obj_type,
1834 struct got_repository *repo)
1836 const struct got_error *err = NULL;
1837 char *path, *id_str = NULL;
1838 DIR *dir = NULL;
1839 struct dirent *dent;
1840 struct got_object_id id;
1842 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1843 err = got_error_from_errno("asprintf");
1844 goto done;
1847 dir = opendir(path);
1848 if (dir == NULL) {
1849 if (errno == ENOENT) {
1850 err = NULL;
1851 goto done;
1853 err = got_error_from_errno2("opendir", path);
1854 goto done;
1856 while ((dent = readdir(dir)) != NULL) {
1857 int cmp;
1859 free(id_str);
1860 id_str = NULL;
1862 if (strcmp(dent->d_name, ".") == 0 ||
1863 strcmp(dent->d_name, "..") == 0)
1864 continue;
1866 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1867 err = got_error_from_errno("asprintf");
1868 goto done;
1871 if (!got_parse_object_id(&id, id_str, repo->algo))
1872 continue;
1875 * Directory entries do not necessarily appear in
1876 * sorted order, so we must iterate over all of them.
1878 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1879 if (cmp != 0)
1880 continue;
1882 if (*unique_id == NULL) {
1883 if (obj_type != GOT_OBJ_TYPE_ANY) {
1884 int matched_type;
1885 err = got_object_get_type(&matched_type, repo,
1886 &id);
1887 if (err)
1888 goto done;
1889 if (matched_type != obj_type)
1890 continue;
1892 *unique_id = got_object_id_dup(&id);
1893 if (*unique_id == NULL) {
1894 err = got_error_from_errno("got_object_id_dup");
1895 goto done;
1897 } else {
1898 if (got_object_id_cmp(*unique_id, &id) == 0)
1899 continue; /* both packed and loose */
1900 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1901 goto done;
1904 done:
1905 if (dir && closedir(dir) != 0 && err == NULL)
1906 err = got_error_from_errno("closedir");
1907 if (err) {
1908 free(*unique_id);
1909 *unique_id = NULL;
1911 free(id_str);
1912 free(path);
1913 return err;
1916 const struct got_error *
1917 got_repo_match_object_id_prefix(struct got_object_id **id,
1918 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1920 const struct got_error *err = NULL;
1921 char *path_objects = NULL, *object_dir = NULL;
1922 size_t len;
1923 int i;
1925 *id = NULL;
1927 path_objects = got_repo_get_path_objects(repo);
1929 len = strlen(id_str_prefix);
1930 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1931 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1932 goto done;
1935 for (i = 0; i < len; i++) {
1936 if (isxdigit((unsigned char)id_str_prefix[i]))
1937 continue;
1938 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1939 goto done;
1942 if (len >= 2) {
1943 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1944 if (err)
1945 goto done;
1946 object_dir = strndup(id_str_prefix, 2);
1947 if (object_dir == NULL) {
1948 err = got_error_from_errno("strdup");
1949 goto done;
1951 err = match_loose_object(id, path_objects, object_dir,
1952 id_str_prefix, obj_type, repo);
1953 } else if (len == 1) {
1954 int i;
1955 for (i = 0; i < 0xf; i++) {
1956 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1957 == -1) {
1958 err = got_error_from_errno("asprintf");
1959 goto done;
1961 err = match_packed_object(id, repo, object_dir,
1962 obj_type);
1963 if (err)
1964 goto done;
1965 err = match_loose_object(id, path_objects, object_dir,
1966 id_str_prefix, obj_type, repo);
1967 if (err)
1968 goto done;
1970 } else {
1971 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1972 goto done;
1974 done:
1975 free(path_objects);
1976 free(object_dir);
1977 if (err) {
1978 free(*id);
1979 *id = NULL;
1980 } else if (*id == NULL) {
1981 switch (obj_type) {
1982 case GOT_OBJ_TYPE_BLOB:
1983 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1984 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1985 break;
1986 case GOT_OBJ_TYPE_TREE:
1987 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1988 GOT_OBJ_LABEL_TREE, id_str_prefix);
1989 break;
1990 case GOT_OBJ_TYPE_COMMIT:
1991 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1992 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1993 break;
1994 case GOT_OBJ_TYPE_TAG:
1995 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1996 GOT_OBJ_LABEL_TAG, id_str_prefix);
1997 break;
1998 default:
1999 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
2000 break;
2004 return err;
2007 const struct got_error *
2008 got_repo_match_object_id(struct got_object_id **id, char **label,
2009 const char *id_str, int obj_type, struct got_reflist_head *refs,
2010 struct got_repository *repo)
2012 const struct got_error *err;
2013 struct got_tag_object *tag;
2014 struct got_reference *ref = NULL;
2016 *id = NULL;
2017 if (label)
2018 *label = NULL;
2020 if (refs) {
2021 err = got_repo_object_match_tag(&tag, id_str, obj_type,
2022 refs, repo);
2023 if (err == NULL) {
2024 *id = got_object_id_dup(
2025 got_object_tag_get_object_id(tag));
2026 if (*id == NULL)
2027 err = got_error_from_errno("got_object_id_dup");
2028 else if (label && asprintf(label, "refs/tags/%s",
2029 got_object_tag_get_name(tag)) == -1) {
2030 err = got_error_from_errno("asprintf");
2031 free(*id);
2032 *id = NULL;
2034 got_object_tag_close(tag);
2035 return err;
2036 } else if (err->code != GOT_ERR_OBJ_TYPE &&
2037 err->code != GOT_ERR_NO_OBJ)
2038 return err;
2041 err = got_ref_open(&ref, repo, id_str, 0);
2042 if (err == NULL) {
2043 err = got_ref_resolve(id, repo, ref);
2044 if (err)
2045 goto done;
2046 if (label) {
2047 *label = strdup(got_ref_get_name(ref));
2048 if (*label == NULL) {
2049 err = got_error_from_errno("strdup");
2050 goto done;
2053 } else {
2054 if (err->code != GOT_ERR_NOT_REF &&
2055 err->code != GOT_ERR_BAD_REF_NAME)
2056 goto done;
2057 err = got_repo_match_object_id_prefix(id, id_str,
2058 obj_type, repo);
2059 if (err) {
2060 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
2061 err = got_error_not_ref(id_str);
2062 goto done;
2064 if (label) {
2065 err = got_object_id_str(label, *id);
2066 if (*label == NULL) {
2067 err = got_error_from_errno("strdup");
2068 goto done;
2072 done:
2073 if (ref)
2074 got_ref_close(ref);
2075 return err;
2078 const struct got_error *
2079 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
2080 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
2082 const struct got_error *err = NULL;
2083 struct got_reflist_entry *re;
2084 struct got_object_id *tag_id;
2085 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
2087 *tag = NULL;
2089 TAILQ_FOREACH(re, refs, entry) {
2090 const char *refname;
2091 refname = got_ref_get_name(re->ref);
2092 if (got_ref_is_symbolic(re->ref))
2093 continue;
2094 if (strncmp(refname, "refs/tags/", 10) != 0)
2095 continue;
2096 if (!name_is_absolute)
2097 refname += strlen("refs/tags/");
2098 if (strcmp(refname, name) != 0)
2099 continue;
2100 err = got_ref_resolve(&tag_id, repo, re->ref);
2101 if (err)
2102 break;
2103 err = got_object_open_as_tag(tag, repo, tag_id);
2104 free(tag_id);
2105 if (err)
2106 break;
2107 if (obj_type == GOT_OBJ_TYPE_ANY ||
2108 got_object_tag_get_object_type(*tag) == obj_type)
2109 break;
2110 got_object_tag_close(*tag);
2111 *tag = NULL;
2114 if (err == NULL && *tag == NULL)
2115 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2116 GOT_OBJ_LABEL_TAG, name);
2117 return err;
2120 static const struct got_error *
2121 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2122 const char *name, mode_t mode, struct got_object_id *blob_id)
2124 const struct got_error *err = NULL;
2126 *new_te = NULL;
2128 *new_te = calloc(1, sizeof(**new_te));
2129 if (*new_te == NULL)
2130 return got_error_from_errno("calloc");
2132 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2133 sizeof((*new_te)->name)) {
2134 err = got_error(GOT_ERR_NO_SPACE);
2135 goto done;
2138 if (S_ISLNK(mode)) {
2139 (*new_te)->mode = S_IFLNK;
2140 } else {
2141 (*new_te)->mode = S_IFREG;
2142 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2144 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2145 done:
2146 if (err && *new_te) {
2147 free(*new_te);
2148 *new_te = NULL;
2150 return err;
2153 static const struct got_error *
2154 import_file(struct got_tree_entry **new_te, struct dirent *de,
2155 const char *path, struct got_repository *repo)
2157 const struct got_error *err;
2158 struct got_object_id *blob_id = NULL;
2159 char *filepath;
2160 struct stat sb;
2162 if (asprintf(&filepath, "%s%s%s", path,
2163 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2164 return got_error_from_errno("asprintf");
2166 if (lstat(filepath, &sb) != 0) {
2167 err = got_error_from_errno2("lstat", path);
2168 goto done;
2171 err = got_object_blob_create(&blob_id, filepath, repo);
2172 if (err)
2173 goto done;
2175 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2176 blob_id);
2177 done:
2178 free(filepath);
2179 if (err)
2180 free(blob_id);
2181 return err;
2184 static const struct got_error *
2185 insert_tree_entry(struct got_tree_entry *new_te,
2186 struct got_pathlist_head *paths)
2188 const struct got_error *err = NULL;
2189 struct got_pathlist_entry *new_pe;
2191 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2192 if (err)
2193 return err;
2194 if (new_pe == NULL)
2195 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2196 return NULL;
2199 static const struct got_error *write_tree(struct got_object_id **,
2200 const char *, struct got_pathlist_head *, struct got_repository *,
2201 got_repo_import_cb progress_cb, void *progress_arg);
2203 static const struct got_error *
2204 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2205 const char *path, struct got_pathlist_head *ignores,
2206 struct got_repository *repo,
2207 got_repo_import_cb progress_cb, void *progress_arg)
2209 const struct got_error *err;
2210 struct got_object_id *id = NULL;
2211 char *subdirpath;
2213 if (asprintf(&subdirpath, "%s%s%s", path,
2214 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2215 return got_error_from_errno("asprintf");
2217 (*new_te) = calloc(1, sizeof(**new_te));
2218 if (*new_te == NULL)
2219 return got_error_from_errno("calloc");
2220 (*new_te)->mode = S_IFDIR;
2221 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2222 sizeof((*new_te)->name)) {
2223 err = got_error(GOT_ERR_NO_SPACE);
2224 goto done;
2226 err = write_tree(&id, subdirpath, ignores, repo,
2227 progress_cb, progress_arg);
2228 if (err)
2229 goto done;
2230 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2232 done:
2233 free(id);
2234 free(subdirpath);
2235 if (err) {
2236 free(*new_te);
2237 *new_te = NULL;
2239 return err;
2242 static const struct got_error *
2243 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2244 struct got_pathlist_head *ignores, struct got_repository *repo,
2245 got_repo_import_cb progress_cb, void *progress_arg)
2247 const struct got_error *err = NULL;
2248 DIR *dir;
2249 struct dirent *de;
2250 int nentries;
2251 struct got_tree_entry *new_te = NULL;
2252 struct got_pathlist_head paths;
2253 struct got_pathlist_entry *pe;
2255 *new_tree_id = NULL;
2257 TAILQ_INIT(&paths);
2259 dir = opendir(path_dir);
2260 if (dir == NULL) {
2261 err = got_error_from_errno2("opendir", path_dir);
2262 goto done;
2265 nentries = 0;
2266 while ((de = readdir(dir)) != NULL) {
2267 int ignore = 0;
2268 int type;
2270 if (strcmp(de->d_name, ".") == 0 ||
2271 strcmp(de->d_name, "..") == 0)
2272 continue;
2274 err = got_path_dirent_type(&type, path_dir, de);
2275 if (err)
2276 goto done;
2278 TAILQ_FOREACH(pe, ignores, entry) {
2279 if (type == DT_DIR && pe->path_len > 0 &&
2280 pe->path[pe->path_len - 1] == '/') {
2281 char stripped[PATH_MAX];
2283 if (strlcpy(stripped, pe->path,
2284 sizeof(stripped)) >= sizeof(stripped)) {
2285 err = got_error(GOT_ERR_NO_SPACE);
2286 goto done;
2288 got_path_strip_trailing_slashes(stripped);
2289 if (fnmatch(stripped, de->d_name, 0) == 0) {
2290 ignore = 1;
2291 break;
2293 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2294 ignore = 1;
2295 break;
2298 if (ignore)
2299 continue;
2301 if (type == DT_DIR) {
2302 err = import_subdir(&new_te, de, path_dir,
2303 ignores, repo, progress_cb, progress_arg);
2304 if (err) {
2305 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2306 goto done;
2307 err = NULL;
2308 continue;
2310 } else if (type == DT_REG || type == DT_LNK) {
2311 err = import_file(&new_te, de, path_dir, repo);
2312 if (err)
2313 goto done;
2314 } else
2315 continue;
2317 err = insert_tree_entry(new_te, &paths);
2318 if (err)
2319 goto done;
2320 nentries++;
2323 if (TAILQ_EMPTY(&paths)) {
2324 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2325 "cannot create tree without any entries");
2326 goto done;
2329 TAILQ_FOREACH(pe, &paths, entry) {
2330 struct got_tree_entry *te = pe->data;
2331 char *path;
2332 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2333 continue;
2334 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2335 err = got_error_from_errno("asprintf");
2336 goto done;
2338 err = (*progress_cb)(progress_arg, path);
2339 free(path);
2340 if (err)
2341 goto done;
2344 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2345 done:
2346 if (dir)
2347 closedir(dir);
2348 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2349 return err;
2352 const struct got_error *
2353 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2354 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2355 struct got_repository *repo, got_repo_import_cb progress_cb,
2356 void *progress_arg)
2358 const struct got_error *err;
2359 struct got_object_id *new_tree_id;
2361 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2362 progress_cb, progress_arg);
2363 if (err)
2364 return err;
2366 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2367 author, time(NULL), author, time(NULL), logmsg, repo);
2368 free(new_tree_id);
2369 return err;
2372 const struct got_error *
2373 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2374 struct got_repository *repo)
2376 const struct got_error *err = NULL;
2377 char *path_objects = NULL, *path = NULL;
2378 DIR *dir = NULL;
2379 struct got_object_id id;
2380 int i;
2382 *nobjects = 0;
2383 *ondisk_size = 0;
2385 path_objects = got_repo_get_path_objects(repo);
2386 if (path_objects == NULL)
2387 return got_error_from_errno("got_repo_get_path_objects");
2389 for (i = 0; i <= 0xff; i++) {
2390 struct dirent *dent;
2392 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2393 err = got_error_from_errno("asprintf");
2394 break;
2397 dir = opendir(path);
2398 if (dir == NULL) {
2399 if (errno == ENOENT) {
2400 err = NULL;
2401 continue;
2403 err = got_error_from_errno2("opendir", path);
2404 break;
2407 while ((dent = readdir(dir)) != NULL) {
2408 char *id_str;
2409 int fd;
2410 struct stat sb;
2412 if (strcmp(dent->d_name, ".") == 0 ||
2413 strcmp(dent->d_name, "..") == 0)
2414 continue;
2416 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2417 err = got_error_from_errno("asprintf");
2418 goto done;
2421 if (!got_parse_object_id(&id, id_str, repo->algo)) {
2422 free(id_str);
2423 continue;
2425 free(id_str);
2427 err = got_object_open_loose_fd(&fd, &id, repo);
2428 if (err)
2429 goto done;
2431 if (fstat(fd, &sb) == -1) {
2432 err = got_error_from_errno("fstat");
2433 close(fd);
2434 goto done;
2436 (*nobjects)++;
2437 (*ondisk_size) += sb.st_size;
2439 if (close(fd) == -1) {
2440 err = got_error_from_errno("close");
2441 goto done;
2445 if (closedir(dir) != 0) {
2446 err = got_error_from_errno("closedir");
2447 goto done;
2449 dir = NULL;
2451 free(path);
2452 path = NULL;
2454 done:
2455 if (dir && closedir(dir) != 0 && err == NULL)
2456 err = got_error_from_errno("closedir");
2458 if (err) {
2459 *nobjects = 0;
2460 *ondisk_size = 0;
2462 free(path_objects);
2463 free(path);
2464 return err;
2467 const struct got_error *
2468 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2469 off_t *total_packsize, struct got_repository *repo)
2471 const struct got_error *err = NULL;
2472 DIR *packdir = NULL;
2473 struct dirent *dent;
2474 struct got_packidx *packidx = NULL;
2475 char *path_packidx;
2476 char *path_packfile;
2477 int packdir_fd;
2478 struct stat sb;
2480 *npackfiles = 0;
2481 *nobjects = 0;
2482 *total_packsize = 0;
2484 packdir_fd = openat(got_repo_get_fd(repo),
2485 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2486 if (packdir_fd == -1) {
2487 return got_error_from_errno_fmt("openat: %s/%s",
2488 got_repo_get_path_git_dir(repo),
2489 GOT_OBJECTS_PACK_DIR);
2492 packdir = fdopendir(packdir_fd);
2493 if (packdir == NULL) {
2494 err = got_error_from_errno("fdopendir");
2495 goto done;
2498 while ((dent = readdir(packdir)) != NULL) {
2499 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2500 continue;
2502 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2503 dent->d_name) == -1) {
2504 err = got_error_from_errno("asprintf");
2505 goto done;
2508 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2509 path_packidx, 0);
2510 free(path_packidx);
2511 if (err)
2512 goto done;
2514 if (fstat(packidx->fd, &sb) == -1)
2515 goto done;
2516 *total_packsize += sb.st_size;
2518 err = got_packidx_get_packfile_path(&path_packfile,
2519 packidx->path_packidx);
2520 if (err)
2521 goto done;
2523 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2524 0) == -1) {
2525 free(path_packfile);
2526 goto done;
2528 free(path_packfile);
2529 *total_packsize += sb.st_size;
2531 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2533 (*npackfiles)++;
2535 got_packidx_close(packidx);
2536 packidx = NULL;
2538 done:
2539 if (packidx)
2540 got_packidx_close(packidx);
2541 if (packdir && closedir(packdir) != 0 && err == NULL)
2542 err = got_error_from_errno("closedir");
2543 if (err) {
2544 *npackfiles = 0;
2545 *nobjects = 0;
2546 *total_packsize = 0;
2548 return err;
2551 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2552 got_packidx_bloom_filter_cmp);