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 > 1) {
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 (repo->gitconfig_repository_format_version == 1 &&
804 strcasecmp(ext, "objectformat") == 0) {
805 if (strcmp(val, "sha1") == 0)
806 continue;
807 if (strcmp(val, "sha256") == 0) {
808 repo->algo = GOT_HASH_SHA256;
809 continue;
811 err = got_error_path(val, GOT_ERR_OBJECT_FORMAT);
812 goto done;
815 if (!is_boolean_val(val)) {
816 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
817 goto done;
820 if (!get_boolean_val(val))
821 continue;
823 for (j = 0; j < nitems(repo_extensions); j++) {
824 if (strcmp(ext, repo_extensions[j]) == 0) {
825 supported = 1;
826 break;
829 if (!supported) {
830 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
831 goto done;
835 err = got_repo_list_packidx(&repo->packidx_paths, repo);
836 done:
837 if (err)
838 got_repo_close(repo);
839 else
840 *repop = repo;
841 free(repo_path);
842 return err;
845 const struct got_error *
846 got_repo_close(struct got_repository *repo)
848 const struct got_error *err = NULL, *child_err;
849 struct got_packidx_bloom_filter *bf;
850 size_t i;
852 for (i = 0; i < repo->pack_cache_size; i++) {
853 if (repo->packidx_cache[i] == NULL)
854 break;
855 got_packidx_close(repo->packidx_cache[i]);
858 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
859 &repo->packidx_bloom_filters))) {
860 RB_REMOVE(got_packidx_bloom_filter_tree,
861 &repo->packidx_bloom_filters, bf);
862 bloom_free(bf->bloom);
863 free(bf->bloom);
864 free(bf);
867 for (i = 0; i < repo->pack_cache_size; i++)
868 if (repo->packs[i].path_packfile)
869 if (repo->packs[i].path_packfile)
870 got_pack_close(&repo->packs[i]);
872 free(repo->path);
873 free(repo->path_git_dir);
875 got_object_cache_close(&repo->objcache);
876 got_object_cache_close(&repo->treecache);
877 got_object_cache_close(&repo->commitcache);
878 got_object_cache_close(&repo->tagcache);
879 got_object_cache_close(&repo->rawcache);
881 for (i = 0; i < nitems(repo->privsep_children); i++) {
882 if (repo->privsep_children[i].imsg_fd == -1)
883 continue;
884 imsg_clear(repo->privsep_children[i].ibuf);
885 free(repo->privsep_children[i].ibuf);
886 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
887 child_err = got_privsep_wait_for_child(
888 repo->privsep_children[i].pid);
889 if (child_err && err == NULL)
890 err = child_err;
891 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
892 err == NULL)
893 err = got_error_from_errno("close");
896 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
897 err == NULL)
898 err = got_error_from_errno("close");
900 if (repo->gotconfig)
901 got_gotconfig_free(repo->gotconfig);
902 free(repo->gitconfig_author_name);
903 free(repo->gitconfig_author_email);
904 for (i = 0; i < repo->ngitconfig_remotes; i++)
905 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
906 free(repo->gitconfig_remotes);
907 for (i = 0; i < repo->nextensions; i++) {
908 free(repo->extnames[i]);
909 free(repo->extvals[i]);
911 free(repo->extnames);
912 free(repo->extvals);
914 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
915 free(repo);
917 return err;
920 void
921 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
923 int i;
925 free(repo->name);
926 repo->name = NULL;
927 free(repo->fetch_url);
928 repo->fetch_url = NULL;
929 free(repo->send_url);
930 repo->send_url = NULL;
931 for (i = 0; i < repo->nfetch_branches; i++)
932 free(repo->fetch_branches[i]);
933 free(repo->fetch_branches);
934 repo->fetch_branches = NULL;
935 repo->nfetch_branches = 0;
936 for (i = 0; i < repo->nsend_branches; i++)
937 free(repo->send_branches[i]);
938 free(repo->send_branches);
939 repo->send_branches = NULL;
940 repo->nsend_branches = 0;
943 const struct got_error *
944 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
945 const char *input_path)
947 const struct got_error *err = NULL;
948 const char *repo_abspath = NULL;
949 size_t repolen, len;
950 char *canonpath, *path = NULL;
952 *in_repo_path = NULL;
954 canonpath = strdup(input_path);
955 if (canonpath == NULL) {
956 err = got_error_from_errno("strdup");
957 goto done;
959 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
960 if (err)
961 goto done;
963 repo_abspath = got_repo_get_path(repo);
965 if (canonpath[0] == '\0') {
966 path = strdup(canonpath);
967 if (path == NULL) {
968 err = got_error_from_errno("strdup");
969 goto done;
971 } else {
972 path = realpath(canonpath, NULL);
973 if (path == NULL) {
974 if (errno != ENOENT) {
975 err = got_error_from_errno2("realpath",
976 canonpath);
977 goto done;
979 /*
980 * Path is not on disk.
981 * Assume it is already relative to repository root.
982 */
983 path = strdup(canonpath);
984 if (path == NULL) {
985 err = got_error_from_errno("strdup");
986 goto done;
990 repolen = strlen(repo_abspath);
991 len = strlen(path);
994 if (strcmp(path, repo_abspath) == 0) {
995 free(path);
996 path = strdup("");
997 if (path == NULL) {
998 err = got_error_from_errno("strdup");
999 goto done;
1001 } else if (len > repolen &&
1002 got_path_is_child(path, repo_abspath, repolen)) {
1003 /* Matched an on-disk path inside repository. */
1004 if (got_repo_is_bare(repo)) {
1006 * Matched an on-disk path inside repository
1007 * database. Treat input as repository-relative.
1009 free(path);
1010 path = canonpath;
1011 canonpath = NULL;
1012 } else {
1013 char *child;
1014 /* Strip common prefix with repository path. */
1015 err = got_path_skip_common_ancestor(&child,
1016 repo_abspath, path);
1017 if (err)
1018 goto done;
1019 free(path);
1020 path = child;
1022 } else {
1024 * Matched unrelated on-disk path.
1025 * Treat input as repository-relative.
1027 free(path);
1028 path = canonpath;
1029 canonpath = NULL;
1033 /* Make in-repository path absolute */
1034 if (path[0] != '/') {
1035 char *abspath;
1036 if (asprintf(&abspath, "/%s", path) == -1) {
1037 err = got_error_from_errno("asprintf");
1038 goto done;
1040 free(path);
1041 path = abspath;
1044 done:
1045 free(canonpath);
1046 if (err)
1047 free(path);
1048 else
1049 *in_repo_path = path;
1050 return err;
1053 static const struct got_error *
1054 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1055 const char *path_packidx)
1057 const struct got_error *err = NULL;
1058 size_t i;
1060 for (i = 0; i < repo->pack_cache_size; i++) {
1061 if (repo->packidx_cache[i] == NULL)
1062 break;
1063 if (strcmp(repo->packidx_cache[i]->path_packidx,
1064 path_packidx) == 0) {
1065 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1068 if (i == repo->pack_cache_size) {
1069 do {
1070 i--;
1071 } while (i > 0 && repo->pinned_packidx >= 0 &&
1072 i == repo->pinned_packidx);
1073 err = got_packidx_close(repo->packidx_cache[i]);
1074 if (err)
1075 return err;
1078 repo->packidx_cache[i] = packidx;
1080 return NULL;
1083 int
1084 got_repo_is_packidx_filename(const char *name, size_t len,
1085 enum got_hash_algorithm algo)
1087 size_t idlen;
1089 idlen = got_hash_digest_string_length(algo);
1091 if (len != GOT_PACKIDX_NAMELEN(idlen))
1092 return 0;
1094 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1095 return 0;
1097 if (strcmp(name + strlen(GOT_PACK_PREFIX) + idlen - 1,
1098 GOT_PACKIDX_SUFFIX) != 0)
1099 return 0;
1101 return 1;
1104 static struct got_packidx_bloom_filter *
1105 get_packidx_bloom_filter(struct got_repository *repo,
1106 const char *path, size_t path_len)
1108 struct got_packidx_bloom_filter key;
1110 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1111 return NULL; /* XXX */
1112 key.path_len = path_len;
1114 return RB_FIND(got_packidx_bloom_filter_tree,
1115 &repo->packidx_bloom_filters, &key);
1118 int
1119 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1120 const char *path_packidx, struct got_object_id *id)
1122 struct got_packidx_bloom_filter *bf;
1124 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1125 if (bf)
1126 return bloom_check(bf->bloom, id->hash,
1127 got_hash_digest_length(id->algo));
1129 /* No bloom filter means this pack index must be searched. */
1130 return 1;
1133 static const struct got_error *
1134 add_packidx_bloom_filter(struct got_repository *repo,
1135 struct got_packidx *packidx, const char *path_packidx)
1137 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1138 struct got_packidx_bloom_filter *bf;
1139 size_t len, idlen;
1141 idlen = got_hash_digest_length(repo->algo);
1144 * Don't use bloom filters for very large pack index files.
1145 * Large pack files will contain a relatively large fraction
1146 * of our objects so we will likely need to visit them anyway.
1147 * The more objects a pack file contains the higher the probability
1148 * of a false-positive match from the bloom filter. And reading
1149 * all object IDs from a large pack index file can be expensive.
1151 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1152 return NULL;
1154 /* Do we already have a filter for this pack index? */
1155 if (get_packidx_bloom_filter(repo, path_packidx,
1156 strlen(path_packidx)) != NULL)
1157 return NULL;
1159 bf = calloc(1, sizeof(*bf));
1160 if (bf == NULL)
1161 return got_error_from_errno("calloc");
1162 bf->bloom = calloc(1, sizeof(*bf->bloom));
1163 if (bf->bloom == NULL) {
1164 free(bf);
1165 return got_error_from_errno("calloc");
1168 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1169 if (len >= sizeof(bf->path)) {
1170 free(bf->bloom);
1171 free(bf);
1172 return got_error(GOT_ERR_NO_SPACE);
1174 bf->path_len = len;
1176 /* Minimum size supported by our bloom filter is 1000 entries. */
1177 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1178 for (i = 0; i < nobjects; i++) {
1179 struct got_packidx_object_id *id;
1180 id = packidx->hdr.sorted_ids + i * idlen;
1181 bloom_add(bf->bloom, id->hash, idlen);
1184 RB_INSERT(got_packidx_bloom_filter_tree,
1185 &repo->packidx_bloom_filters, bf);
1186 return NULL;
1189 static void
1190 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1192 struct got_pathlist_entry *pe;
1194 while (!TAILQ_EMPTY(packidx_paths)) {
1195 pe = TAILQ_FIRST(packidx_paths);
1196 TAILQ_REMOVE(packidx_paths, pe, entry);
1197 free((char *)pe->path);
1198 free(pe);
1202 static const struct got_error *
1203 refresh_packidx_paths(struct got_repository *repo)
1205 const struct got_error *err = NULL;
1206 char *objects_pack_dir = NULL;
1207 struct stat sb;
1209 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1210 if (objects_pack_dir == NULL)
1211 return got_error_from_errno("got_repo_get_path_objects_pack");
1213 if (stat(objects_pack_dir, &sb) == -1) {
1214 if (errno != ENOENT) {
1215 err = got_error_from_errno2("stat", objects_pack_dir);
1216 goto done;
1218 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1219 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1220 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1221 purge_packidx_paths(&repo->packidx_paths);
1222 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1223 if (err)
1224 goto done;
1226 done:
1227 free(objects_pack_dir);
1228 return err;
1231 const struct got_error *
1232 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1233 struct got_repository *repo, struct got_object_id *id)
1235 const struct got_error *err;
1236 struct got_pathlist_entry *pe;
1237 size_t i;
1239 /* Search pack index cache. */
1240 for (i = 0; i < repo->pack_cache_size; i++) {
1241 if (repo->packidx_cache[i] == NULL)
1242 break;
1243 if (!got_repo_check_packidx_bloom_filter(repo,
1244 repo->packidx_cache[i]->path_packidx, id))
1245 continue; /* object will not be found in this index */
1246 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1247 if (*idx != -1) {
1248 *packidx = repo->packidx_cache[i];
1250 * Move this cache entry to the front. Repeatedly
1251 * searching a wrong pack index can be expensive.
1253 if (i > 0) {
1254 memmove(&repo->packidx_cache[1],
1255 &repo->packidx_cache[0],
1256 i * sizeof(repo->packidx_cache[0]));
1257 repo->packidx_cache[0] = *packidx;
1258 if (repo->pinned_packidx >= 0 &&
1259 repo->pinned_packidx < i)
1260 repo->pinned_packidx++;
1261 else if (repo->pinned_packidx == i)
1262 repo->pinned_packidx = 0;
1264 return NULL;
1267 /* No luck. Search the filesystem. */
1269 err = refresh_packidx_paths(repo);
1270 if (err)
1271 return err;
1273 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1274 const char *path_packidx = pe->path;
1275 int is_cached = 0;
1277 if (!got_repo_check_packidx_bloom_filter(repo,
1278 pe->path, id))
1279 continue; /* object will not be found in this index */
1281 for (i = 0; i < repo->pack_cache_size; i++) {
1282 if (repo->packidx_cache[i] == NULL)
1283 break;
1284 if (strcmp(repo->packidx_cache[i]->path_packidx,
1285 path_packidx) == 0) {
1286 is_cached = 1;
1287 break;
1290 if (is_cached)
1291 continue; /* already searched */
1293 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1294 path_packidx, 0, repo->algo);
1295 if (err)
1296 goto done;
1298 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1299 if (err)
1300 goto done;
1302 err = cache_packidx(repo, *packidx, path_packidx);
1303 if (err)
1304 goto done;
1306 *idx = got_packidx_get_object_idx(*packidx, id);
1307 if (*idx != -1) {
1308 err = NULL; /* found the object */
1309 goto done;
1313 err = got_error_no_obj(id);
1314 done:
1315 return err;
1318 const struct got_error *
1319 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1320 struct got_repository *repo)
1322 const struct got_error *err = NULL;
1323 DIR *packdir = NULL;
1324 struct dirent *dent;
1325 char *path_packidx = NULL;
1326 int packdir_fd;
1327 struct stat sb;
1329 packdir_fd = openat(got_repo_get_fd(repo),
1330 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1331 if (packdir_fd == -1) {
1332 return got_error_from_errno_fmt("openat: %s/%s",
1333 got_repo_get_path_git_dir(repo),
1334 GOT_OBJECTS_PACK_DIR);
1337 packdir = fdopendir(packdir_fd);
1338 if (packdir == NULL) {
1339 err = got_error_from_errno("fdopendir");
1340 goto done;
1343 if (fstat(packdir_fd, &sb) == -1) {
1344 err = got_error_from_errno("fstat");
1345 goto done;
1347 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1348 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1350 while ((dent = readdir(packdir)) != NULL) {
1351 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen,
1352 repo->algo))
1353 continue;
1355 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1356 dent->d_name) == -1) {
1357 err = got_error_from_errno("asprintf");
1358 path_packidx = NULL;
1359 break;
1362 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1363 if (err)
1364 break;
1366 done:
1367 if (err)
1368 free(path_packidx);
1369 if (packdir && closedir(packdir) != 0 && err == NULL)
1370 err = got_error_from_errno("closedir");
1371 return err;
1374 const struct got_error *
1375 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1376 struct got_repository *repo)
1378 const struct got_error *err;
1379 size_t i;
1381 *packidx = NULL;
1383 /* Search pack index cache. */
1384 for (i = 0; i < repo->pack_cache_size; i++) {
1385 if (repo->packidx_cache[i] == NULL)
1386 break;
1387 if (strcmp(repo->packidx_cache[i]->path_packidx,
1388 path_packidx) == 0) {
1389 *packidx = repo->packidx_cache[i];
1390 return NULL;
1393 /* No luck. Search the filesystem. */
1395 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1396 path_packidx, 0, repo->algo);
1397 if (err)
1398 return err;
1400 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1401 if (err)
1402 goto done;
1404 err = cache_packidx(repo, *packidx, path_packidx);
1405 done:
1406 if (err) {
1407 got_packidx_close(*packidx);
1408 *packidx = NULL;
1410 return err;
1413 static const struct got_error *
1414 read_packfile_hdr(int fd, struct got_packidx *packidx)
1416 const struct got_error *err = NULL;
1417 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1418 struct got_packfile_hdr hdr;
1419 ssize_t n;
1421 n = read(fd, &hdr, sizeof(hdr));
1422 if (n < 0)
1423 return got_error_from_errno("read");
1424 if (n != sizeof(hdr))
1425 return got_error(GOT_ERR_BAD_PACKFILE);
1427 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1428 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1429 be32toh(hdr.nobjects) != totobj)
1430 err = got_error(GOT_ERR_BAD_PACKFILE);
1432 return err;
1435 static const struct got_error *
1436 open_packfile(int *fd, struct got_repository *repo,
1437 const char *relpath, struct got_packidx *packidx)
1439 const struct got_error *err = NULL;
1441 *fd = openat(got_repo_get_fd(repo), relpath,
1442 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1443 if (*fd == -1)
1444 return got_error_from_errno_fmt("openat: %s/%s",
1445 got_repo_get_path_git_dir(repo), relpath);
1447 if (packidx) {
1448 err = read_packfile_hdr(*fd, packidx);
1449 if (err) {
1450 close(*fd);
1451 *fd = -1;
1455 return err;
1458 const struct got_error *
1459 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1460 const char *path_packfile, struct got_packidx *packidx)
1462 const struct got_error *err = NULL;
1463 struct got_pack *pack = NULL;
1464 struct stat sb;
1465 size_t i;
1467 if (packp)
1468 *packp = NULL;
1470 for (i = 0; i < repo->pack_cache_size; i++) {
1471 pack = &repo->packs[i];
1472 if (pack->path_packfile == NULL)
1473 break;
1474 if (strcmp(pack->path_packfile, path_packfile) == 0)
1475 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1478 if (i == repo->pack_cache_size) {
1479 struct got_pack tmp;
1480 do {
1481 i--;
1482 } while (i > 0 && repo->pinned_pack >= 0 &&
1483 i == repo->pinned_pack);
1484 err = got_pack_close(&repo->packs[i]);
1485 if (err)
1486 return err;
1487 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1488 return got_error_from_errno("ftruncate");
1489 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1490 return got_error_from_errno("ftruncate");
1491 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1492 memcpy(&repo->packs[i], &repo->packs[0],
1493 sizeof(repo->packs[i]));
1494 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1495 if (repo->pinned_pack == 0)
1496 repo->pinned_pack = i;
1497 else if (repo->pinned_pack == i)
1498 repo->pinned_pack = 0;
1499 i = 0;
1502 pack = &repo->packs[i];
1504 pack->path_packfile = strdup(path_packfile);
1505 if (pack->path_packfile == NULL) {
1506 err = got_error_from_errno("strdup");
1507 goto done;
1510 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1511 if (err)
1512 goto done;
1514 if (fstat(pack->fd, &sb) != 0) {
1515 err = got_error_from_errno("fstat");
1516 goto done;
1518 pack->filesize = sb.st_size;
1519 pack->algo = got_repo_get_object_format(repo);
1521 pack->privsep_child = NULL;
1523 err = got_delta_cache_alloc(&pack->delta_cache);
1524 if (err)
1525 goto done;
1527 #ifndef GOT_PACK_NO_MMAP
1528 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1529 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1530 pack->fd, 0);
1531 if (pack->map == MAP_FAILED) {
1532 if (errno != ENOMEM) {
1533 err = got_error_from_errno("mmap");
1534 goto done;
1536 pack->map = NULL; /* fall back to read(2) */
1539 #endif
1540 done:
1541 if (err) {
1542 if (pack)
1543 got_pack_close(pack);
1544 } else if (packp)
1545 *packp = pack;
1546 return err;
1549 struct got_pack *
1550 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1552 struct got_pack *pack = NULL;
1553 size_t i;
1555 for (i = 0; i < repo->pack_cache_size; i++) {
1556 pack = &repo->packs[i];
1557 if (pack->path_packfile == NULL)
1558 break;
1559 if (strcmp(pack->path_packfile, path_packfile) == 0)
1560 return pack;
1563 return NULL;
1566 const struct got_error *
1567 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1568 struct got_pack *pack)
1570 size_t i;
1571 int pinned_pack = -1, pinned_packidx = -1;
1573 for (i = 0; i < repo->pack_cache_size; i++) {
1574 if (repo->packidx_cache[i] &&
1575 strcmp(repo->packidx_cache[i]->path_packidx,
1576 packidx->path_packidx) == 0)
1577 pinned_packidx = i;
1578 if (repo->packs[i].path_packfile &&
1579 strcmp(repo->packs[i].path_packfile,
1580 pack->path_packfile) == 0)
1581 pinned_pack = i;
1584 if (pinned_packidx == -1 || pinned_pack == -1)
1585 return got_error(GOT_ERR_PIN_PACK);
1587 repo->pinned_pack = pinned_pack;
1588 repo->pinned_packidx = pinned_packidx;
1589 if (repo->packs[pinned_pack].privsep_child)
1590 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1591 return NULL;
1594 struct got_pack *
1595 got_repo_get_pinned_pack(struct got_repository *repo)
1597 if (repo->pinned_pack >= 0 &&
1598 repo->pinned_pack < repo->pack_cache_size)
1599 return &repo->packs[repo->pinned_pack];
1601 return NULL;
1604 void
1605 got_repo_unpin_pack(struct got_repository *repo)
1607 repo->pinned_packidx = -1;
1608 repo->pinned_pack = -1;
1609 repo->pinned_pid = 0;
1612 const struct got_error *
1613 got_repo_init(const char *repo_path, const char *head_name)
1615 const struct got_error *err = NULL;
1616 const char *dirnames[] = {
1617 GOT_OBJECTS_DIR,
1618 GOT_OBJECTS_PACK_DIR,
1619 GOT_REFS_DIR,
1621 const char *description_str = "Unnamed repository; "
1622 "edit this file 'description' to name the repository.";
1623 const char *headref = "ref: refs/heads/";
1624 const char *gitconfig_str = "[core]\n"
1625 "\trepositoryformatversion = 0\n"
1626 "\tfilemode = true\n"
1627 "\tbare = true\n";
1628 char *headref_str, *path;
1629 size_t i;
1631 if (!got_path_dir_is_empty(repo_path))
1632 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1634 for (i = 0; i < nitems(dirnames); i++) {
1635 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1636 return got_error_from_errno("asprintf");
1638 err = got_path_mkdir(path);
1639 free(path);
1640 if (err)
1641 return err;
1644 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1645 return got_error_from_errno("asprintf");
1646 err = got_path_create_file(path, description_str);
1647 free(path);
1648 if (err)
1649 return err;
1651 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1652 return got_error_from_errno("asprintf");
1653 if (asprintf(&headref_str, "%s%s", headref,
1654 head_name ? head_name : "main") == -1) {
1655 free(path);
1656 return got_error_from_errno("asprintf");
1658 err = got_path_create_file(path, headref_str);
1659 free(headref_str);
1660 free(path);
1661 if (err)
1662 return err;
1664 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1665 return got_error_from_errno("asprintf");
1666 err = got_path_create_file(path, gitconfig_str);
1667 free(path);
1668 if (err)
1669 return err;
1671 return NULL;
1674 static const struct got_error *
1675 match_packed_object(struct got_object_id **unique_id,
1676 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1678 const struct got_error *err = NULL;
1679 struct got_object_id_queue matched_ids;
1680 struct got_pathlist_entry *pe;
1682 STAILQ_INIT(&matched_ids);
1684 err = refresh_packidx_paths(repo);
1685 if (err)
1686 return err;
1688 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1689 const char *path_packidx = pe->path;
1690 struct got_packidx *packidx;
1691 struct got_object_qid *qid;
1693 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1694 path_packidx, 0, repo->algo);
1695 if (err)
1696 break;
1698 err = got_packidx_match_id_str_prefix(&matched_ids,
1699 packidx, id_str_prefix);
1700 if (err) {
1701 got_packidx_close(packidx);
1702 break;
1704 err = got_packidx_close(packidx);
1705 if (err)
1706 break;
1708 STAILQ_FOREACH(qid, &matched_ids, entry) {
1709 if (obj_type != GOT_OBJ_TYPE_ANY) {
1710 int matched_type;
1711 err = got_object_get_type(&matched_type, repo,
1712 &qid->id);
1713 if (err)
1714 goto done;
1715 if (matched_type != obj_type)
1716 continue;
1718 if (*unique_id == NULL) {
1719 *unique_id = got_object_id_dup(&qid->id);
1720 if (*unique_id == NULL) {
1721 err = got_error_from_errno("malloc");
1722 goto done;
1724 } else {
1725 if (got_object_id_cmp(*unique_id,
1726 &qid->id) == 0)
1727 continue; /* packed multiple times */
1728 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1729 goto done;
1733 done:
1734 got_object_id_queue_free(&matched_ids);
1735 if (err) {
1736 free(*unique_id);
1737 *unique_id = NULL;
1739 return err;
1742 static const struct got_error *
1743 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1744 const char *object_dir, const char *id_str_prefix, int obj_type,
1745 struct got_repository *repo)
1747 const struct got_error *err = NULL;
1748 char *path, *id_str = NULL;
1749 DIR *dir = NULL;
1750 struct dirent *dent;
1751 struct got_object_id id;
1753 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1754 err = got_error_from_errno("asprintf");
1755 goto done;
1758 dir = opendir(path);
1759 if (dir == NULL) {
1760 if (errno == ENOENT) {
1761 err = NULL;
1762 goto done;
1764 err = got_error_from_errno2("opendir", path);
1765 goto done;
1767 while ((dent = readdir(dir)) != NULL) {
1768 int cmp;
1770 free(id_str);
1771 id_str = NULL;
1773 if (strcmp(dent->d_name, ".") == 0 ||
1774 strcmp(dent->d_name, "..") == 0)
1775 continue;
1777 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1778 err = got_error_from_errno("asprintf");
1779 goto done;
1783 memset(&id, 0, sizeof(id));
1784 id.algo = repo->algo;
1785 if (!got_parse_hash_digest(id.hash, id_str, repo->algo))
1786 continue;
1789 * Directory entries do not necessarily appear in
1790 * sorted order, so we must iterate over all of them.
1792 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1793 if (cmp != 0)
1794 continue;
1796 if (*unique_id == NULL) {
1797 if (obj_type != GOT_OBJ_TYPE_ANY) {
1798 int matched_type;
1799 err = got_object_get_type(&matched_type, repo,
1800 &id);
1801 if (err)
1802 goto done;
1803 if (matched_type != obj_type)
1804 continue;
1806 *unique_id = got_object_id_dup(&id);
1807 if (*unique_id == NULL) {
1808 err = got_error_from_errno("got_object_id_dup");
1809 goto done;
1811 } else {
1812 if (got_object_id_cmp(*unique_id, &id) == 0)
1813 continue; /* both packed and loose */
1814 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1815 goto done;
1818 done:
1819 if (dir && closedir(dir) != 0 && err == NULL)
1820 err = got_error_from_errno("closedir");
1821 if (err) {
1822 free(*unique_id);
1823 *unique_id = NULL;
1825 free(id_str);
1826 free(path);
1827 return err;
1830 const struct got_error *
1831 got_repo_match_object_id_prefix(struct got_object_id **id,
1832 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1834 const struct got_error *err = NULL;
1835 char *path_objects = NULL, *object_dir = NULL;
1836 size_t len;
1837 int i;
1839 *id = NULL;
1841 path_objects = got_repo_get_path_objects(repo);
1843 len = strlen(id_str_prefix);
1844 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1845 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1846 goto done;
1849 for (i = 0; i < len; i++) {
1850 if (isxdigit((unsigned char)id_str_prefix[i]))
1851 continue;
1852 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1853 goto done;
1856 if (len >= 2) {
1857 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1858 if (err)
1859 goto done;
1860 object_dir = strndup(id_str_prefix, 2);
1861 if (object_dir == NULL) {
1862 err = got_error_from_errno("strdup");
1863 goto done;
1865 err = match_loose_object(id, path_objects, object_dir,
1866 id_str_prefix, obj_type, repo);
1867 } else if (len == 1) {
1868 int i;
1869 for (i = 0; i < 0xf; i++) {
1870 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1871 == -1) {
1872 err = got_error_from_errno("asprintf");
1873 goto done;
1875 err = match_packed_object(id, repo, object_dir,
1876 obj_type);
1877 if (err)
1878 goto done;
1879 err = match_loose_object(id, path_objects, object_dir,
1880 id_str_prefix, obj_type, repo);
1881 if (err)
1882 goto done;
1884 } else {
1885 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1886 goto done;
1888 done:
1889 free(path_objects);
1890 free(object_dir);
1891 if (err) {
1892 free(*id);
1893 *id = NULL;
1894 } else if (*id == NULL) {
1895 switch (obj_type) {
1896 case GOT_OBJ_TYPE_BLOB:
1897 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1898 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1899 break;
1900 case GOT_OBJ_TYPE_TREE:
1901 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1902 GOT_OBJ_LABEL_TREE, id_str_prefix);
1903 break;
1904 case GOT_OBJ_TYPE_COMMIT:
1905 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1906 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1907 break;
1908 case GOT_OBJ_TYPE_TAG:
1909 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1910 GOT_OBJ_LABEL_TAG, id_str_prefix);
1911 break;
1912 default:
1913 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1914 break;
1918 return err;
1921 const struct got_error *
1922 got_repo_match_object_id(struct got_object_id **id, char **label,
1923 const char *id_str, int obj_type, struct got_reflist_head *refs,
1924 struct got_repository *repo)
1926 const struct got_error *err;
1927 struct got_tag_object *tag;
1928 struct got_reference *ref = NULL;
1930 *id = NULL;
1931 if (label)
1932 *label = NULL;
1934 if (refs) {
1935 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1936 refs, repo);
1937 if (err == NULL) {
1938 *id = got_object_id_dup(
1939 got_object_tag_get_object_id(tag));
1940 if (*id == NULL)
1941 err = got_error_from_errno("got_object_id_dup");
1942 else if (label && asprintf(label, "refs/tags/%s",
1943 got_object_tag_get_name(tag)) == -1) {
1944 err = got_error_from_errno("asprintf");
1945 free(*id);
1946 *id = NULL;
1948 got_object_tag_close(tag);
1949 return err;
1950 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1951 err->code != GOT_ERR_NO_OBJ)
1952 return err;
1955 err = got_ref_open(&ref, repo, id_str, 0);
1956 if (err == NULL) {
1957 err = got_ref_resolve(id, repo, ref);
1958 if (err)
1959 goto done;
1960 if (label) {
1961 *label = strdup(got_ref_get_name(ref));
1962 if (*label == NULL) {
1963 err = got_error_from_errno("strdup");
1964 goto done;
1967 } else {
1968 if (err->code != GOT_ERR_NOT_REF &&
1969 err->code != GOT_ERR_BAD_REF_NAME)
1970 goto done;
1971 err = got_repo_match_object_id_prefix(id, id_str,
1972 obj_type, repo);
1973 if (err) {
1974 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1975 err = got_error_not_ref(id_str);
1976 goto done;
1978 if (label) {
1979 err = got_object_id_str(label, *id);
1980 if (*label == NULL) {
1981 err = got_error_from_errno("strdup");
1982 goto done;
1986 done:
1987 if (ref)
1988 got_ref_close(ref);
1989 return err;
1992 const struct got_error *
1993 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1994 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1996 const struct got_error *err = NULL;
1997 struct got_reflist_entry *re;
1998 struct got_object_id *tag_id;
1999 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
2001 *tag = NULL;
2003 TAILQ_FOREACH(re, refs, entry) {
2004 const char *refname;
2005 refname = got_ref_get_name(re->ref);
2006 if (got_ref_is_symbolic(re->ref))
2007 continue;
2008 if (strncmp(refname, "refs/tags/", 10) != 0)
2009 continue;
2010 if (!name_is_absolute)
2011 refname += strlen("refs/tags/");
2012 if (strcmp(refname, name) != 0)
2013 continue;
2014 err = got_ref_resolve(&tag_id, repo, re->ref);
2015 if (err)
2016 break;
2017 err = got_object_open_as_tag(tag, repo, tag_id);
2018 free(tag_id);
2019 if (err)
2020 break;
2021 if (obj_type == GOT_OBJ_TYPE_ANY ||
2022 got_object_tag_get_object_type(*tag) == obj_type)
2023 break;
2024 got_object_tag_close(*tag);
2025 *tag = NULL;
2028 if (err == NULL && *tag == NULL)
2029 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2030 GOT_OBJ_LABEL_TAG, name);
2031 return err;
2034 static const struct got_error *
2035 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2036 const char *name, mode_t mode, struct got_object_id *blob_id)
2038 const struct got_error *err = NULL;
2040 *new_te = NULL;
2042 *new_te = calloc(1, sizeof(**new_te));
2043 if (*new_te == NULL)
2044 return got_error_from_errno("calloc");
2046 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2047 sizeof((*new_te)->name)) {
2048 err = got_error(GOT_ERR_NO_SPACE);
2049 goto done;
2052 if (S_ISLNK(mode)) {
2053 (*new_te)->mode = S_IFLNK;
2054 } else {
2055 (*new_te)->mode = S_IFREG;
2056 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2058 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2059 done:
2060 if (err && *new_te) {
2061 free(*new_te);
2062 *new_te = NULL;
2064 return err;
2067 static const struct got_error *
2068 import_file(struct got_tree_entry **new_te, struct dirent *de,
2069 const char *path, struct got_repository *repo)
2071 const struct got_error *err;
2072 struct got_object_id *blob_id = NULL;
2073 char *filepath;
2074 struct stat sb;
2076 if (asprintf(&filepath, "%s%s%s", path,
2077 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2078 return got_error_from_errno("asprintf");
2080 if (lstat(filepath, &sb) != 0) {
2081 err = got_error_from_errno2("lstat", path);
2082 goto done;
2085 err = got_object_blob_create(&blob_id, filepath, repo);
2086 if (err)
2087 goto done;
2089 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2090 blob_id);
2091 done:
2092 free(filepath);
2093 if (err)
2094 free(blob_id);
2095 return err;
2098 static const struct got_error *
2099 insert_tree_entry(struct got_tree_entry *new_te,
2100 struct got_pathlist_head *paths)
2102 const struct got_error *err = NULL;
2103 struct got_pathlist_entry *new_pe;
2105 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2106 if (err)
2107 return err;
2108 if (new_pe == NULL)
2109 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2110 return NULL;
2113 static const struct got_error *write_tree(struct got_object_id **,
2114 const char *, struct got_pathlist_head *, struct got_repository *,
2115 got_repo_import_cb progress_cb, void *progress_arg);
2117 static const struct got_error *
2118 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2119 const char *path, struct got_pathlist_head *ignores,
2120 struct got_repository *repo,
2121 got_repo_import_cb progress_cb, void *progress_arg)
2123 const struct got_error *err;
2124 struct got_object_id *id = NULL;
2125 char *subdirpath;
2127 if (asprintf(&subdirpath, "%s%s%s", path,
2128 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2129 return got_error_from_errno("asprintf");
2131 (*new_te) = calloc(1, sizeof(**new_te));
2132 if (*new_te == NULL)
2133 return got_error_from_errno("calloc");
2134 (*new_te)->mode = S_IFDIR;
2135 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2136 sizeof((*new_te)->name)) {
2137 err = got_error(GOT_ERR_NO_SPACE);
2138 goto done;
2140 err = write_tree(&id, subdirpath, ignores, repo,
2141 progress_cb, progress_arg);
2142 if (err)
2143 goto done;
2144 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2146 done:
2147 free(id);
2148 free(subdirpath);
2149 if (err) {
2150 free(*new_te);
2151 *new_te = NULL;
2153 return err;
2156 static const struct got_error *
2157 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2158 struct got_pathlist_head *ignores, struct got_repository *repo,
2159 got_repo_import_cb progress_cb, void *progress_arg)
2161 const struct got_error *err = NULL;
2162 DIR *dir;
2163 struct dirent *de;
2164 int nentries;
2165 struct got_tree_entry *new_te = NULL;
2166 struct got_pathlist_head paths;
2167 struct got_pathlist_entry *pe;
2169 *new_tree_id = NULL;
2171 TAILQ_INIT(&paths);
2173 dir = opendir(path_dir);
2174 if (dir == NULL) {
2175 err = got_error_from_errno2("opendir", path_dir);
2176 goto done;
2179 nentries = 0;
2180 while ((de = readdir(dir)) != NULL) {
2181 int ignore = 0;
2182 int type;
2184 if (strcmp(de->d_name, ".") == 0 ||
2185 strcmp(de->d_name, "..") == 0)
2186 continue;
2188 err = got_path_dirent_type(&type, path_dir, de);
2189 if (err)
2190 goto done;
2192 TAILQ_FOREACH(pe, ignores, entry) {
2193 if (type == DT_DIR && pe->path_len > 0 &&
2194 pe->path[pe->path_len - 1] == '/') {
2195 char stripped[PATH_MAX];
2197 if (strlcpy(stripped, pe->path,
2198 sizeof(stripped)) >= sizeof(stripped)) {
2199 err = got_error(GOT_ERR_NO_SPACE);
2200 goto done;
2202 got_path_strip_trailing_slashes(stripped);
2203 if (fnmatch(stripped, de->d_name, 0) == 0) {
2204 ignore = 1;
2205 break;
2207 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2208 ignore = 1;
2209 break;
2212 if (ignore)
2213 continue;
2215 if (type == DT_DIR) {
2216 err = import_subdir(&new_te, de, path_dir,
2217 ignores, repo, progress_cb, progress_arg);
2218 if (err) {
2219 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2220 goto done;
2221 err = NULL;
2222 continue;
2224 } else if (type == DT_REG || type == DT_LNK) {
2225 err = import_file(&new_te, de, path_dir, repo);
2226 if (err)
2227 goto done;
2228 } else
2229 continue;
2231 err = insert_tree_entry(new_te, &paths);
2232 if (err)
2233 goto done;
2234 nentries++;
2237 if (TAILQ_EMPTY(&paths)) {
2238 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2239 "cannot create tree without any entries");
2240 goto done;
2243 TAILQ_FOREACH(pe, &paths, entry) {
2244 struct got_tree_entry *te = pe->data;
2245 char *path;
2246 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2247 continue;
2248 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2249 err = got_error_from_errno("asprintf");
2250 goto done;
2252 err = (*progress_cb)(progress_arg, path);
2253 free(path);
2254 if (err)
2255 goto done;
2258 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2259 done:
2260 if (dir)
2261 closedir(dir);
2262 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2263 return err;
2266 const struct got_error *
2267 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2268 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2269 struct got_repository *repo, got_repo_import_cb progress_cb,
2270 void *progress_arg)
2272 const struct got_error *err;
2273 struct got_object_id *new_tree_id;
2275 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2276 progress_cb, progress_arg);
2277 if (err)
2278 return err;
2280 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2281 author, time(NULL), author, time(NULL), logmsg, repo);
2282 free(new_tree_id);
2283 return err;
2286 const struct got_error *
2287 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2288 struct got_repository *repo)
2290 const struct got_error *err = NULL;
2291 char *path_objects = NULL, *path = NULL;
2292 DIR *dir = NULL;
2293 struct got_object_id id;
2294 int i;
2296 *nobjects = 0;
2297 *ondisk_size = 0;
2299 path_objects = got_repo_get_path_objects(repo);
2300 if (path_objects == NULL)
2301 return got_error_from_errno("got_repo_get_path_objects");
2303 for (i = 0; i <= 0xff; i++) {
2304 struct dirent *dent;
2306 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2307 err = got_error_from_errno("asprintf");
2308 break;
2311 dir = opendir(path);
2312 if (dir == NULL) {
2313 if (errno == ENOENT) {
2314 err = NULL;
2315 continue;
2317 err = got_error_from_errno2("opendir", path);
2318 break;
2321 while ((dent = readdir(dir)) != NULL) {
2322 char *id_str;
2323 int fd;
2324 struct stat sb;
2326 if (strcmp(dent->d_name, ".") == 0 ||
2327 strcmp(dent->d_name, "..") == 0)
2328 continue;
2330 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2331 err = got_error_from_errno("asprintf");
2332 goto done;
2335 memset(&id, 0, sizeof(id));
2336 id.algo = repo->algo;
2337 if (!got_parse_hash_digest(id.hash, id_str,
2338 repo->algo)) {
2339 free(id_str);
2340 continue;
2342 free(id_str);
2344 err = got_object_open_loose_fd(&fd, &id, repo);
2345 if (err)
2346 goto done;
2348 if (fstat(fd, &sb) == -1) {
2349 err = got_error_from_errno("fstat");
2350 close(fd);
2351 goto done;
2353 (*nobjects)++;
2354 (*ondisk_size) += sb.st_size;
2356 if (close(fd) == -1) {
2357 err = got_error_from_errno("close");
2358 goto done;
2362 if (closedir(dir) != 0) {
2363 err = got_error_from_errno("closedir");
2364 goto done;
2366 dir = NULL;
2368 free(path);
2369 path = NULL;
2371 done:
2372 if (dir && closedir(dir) != 0 && err == NULL)
2373 err = got_error_from_errno("closedir");
2375 if (err) {
2376 *nobjects = 0;
2377 *ondisk_size = 0;
2379 free(path_objects);
2380 free(path);
2381 return err;
2384 const struct got_error *
2385 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2386 off_t *total_packsize, struct got_repository *repo)
2388 const struct got_error *err = NULL;
2389 DIR *packdir = NULL;
2390 struct dirent *dent;
2391 struct got_packidx *packidx = NULL;
2392 char *path_packidx;
2393 char *path_packfile;
2394 int packdir_fd;
2395 struct stat sb;
2397 *npackfiles = 0;
2398 *nobjects = 0;
2399 *total_packsize = 0;
2401 packdir_fd = openat(got_repo_get_fd(repo),
2402 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2403 if (packdir_fd == -1) {
2404 return got_error_from_errno_fmt("openat: %s/%s",
2405 got_repo_get_path_git_dir(repo),
2406 GOT_OBJECTS_PACK_DIR);
2409 packdir = fdopendir(packdir_fd);
2410 if (packdir == NULL) {
2411 err = got_error_from_errno("fdopendir");
2412 goto done;
2415 while ((dent = readdir(packdir)) != NULL) {
2416 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen,
2417 repo->algo))
2418 continue;
2420 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2421 dent->d_name) == -1) {
2422 err = got_error_from_errno("asprintf");
2423 goto done;
2426 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2427 path_packidx, 0, repo->algo);
2428 free(path_packidx);
2429 if (err)
2430 goto done;
2432 if (fstat(packidx->fd, &sb) == -1)
2433 goto done;
2434 *total_packsize += sb.st_size;
2436 err = got_packidx_get_packfile_path(&path_packfile,
2437 packidx->path_packidx);
2438 if (err)
2439 goto done;
2441 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2442 0) == -1) {
2443 free(path_packfile);
2444 goto done;
2446 free(path_packfile);
2447 *total_packsize += sb.st_size;
2449 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2451 (*npackfiles)++;
2453 got_packidx_close(packidx);
2454 packidx = NULL;
2456 done:
2457 if (packidx)
2458 got_packidx_close(packidx);
2459 if (packdir && closedir(packdir) != 0 && err == NULL)
2460 err = got_error_from_errno("closedir");
2461 if (err) {
2462 *npackfiles = 0;
2463 *nobjects = 0;
2464 *total_packsize = 0;
2466 return err;
2469 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2470 got_packidx_bloom_filter_cmp);