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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_delta_cache.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
63 #ifndef nitems
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 #endif
67 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
69 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
70 got_packidx_bloom_filter_cmp);
72 static inline int
73 is_boolean_val(const char *val)
74 {
75 return (strcasecmp(val, "true") == 0 ||
76 strcasecmp(val, "false") == 0 ||
77 strcasecmp(val, "on") == 0 ||
78 strcasecmp(val, "off") == 0 ||
79 strcasecmp(val, "yes") == 0 ||
80 strcasecmp(val, "no") == 0 ||
81 strcasecmp(val, "1") == 0 ||
82 strcasecmp(val, "0") == 0);
83 }
85 static inline int
86 get_boolean_val(const char *val)
87 {
88 return (strcasecmp(val, "true") == 0 ||
89 strcasecmp(val, "on") == 0 ||
90 strcasecmp(val, "yes") == 0 ||
91 strcasecmp(val, "1") == 0);
92 }
94 const char *
95 got_repo_get_path(struct got_repository *repo)
96 {
97 return repo->path;
98 }
100 const char *
101 got_repo_get_path_git_dir(struct got_repository *repo)
103 return repo->path_git_dir;
106 int
107 got_repo_get_fd(struct got_repository *repo)
109 return repo->gitdir_fd;
112 const char *
113 got_repo_get_gitconfig_author_name(struct got_repository *repo)
115 return repo->gitconfig_author_name;
118 const char *
119 got_repo_get_gitconfig_author_email(struct got_repository *repo)
121 return repo->gitconfig_author_email;
124 const char *
125 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
127 return repo->global_gitconfig_author_name;
130 const char *
131 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
133 return repo->global_gitconfig_author_email;
136 const char *
137 got_repo_get_gitconfig_owner(struct got_repository *repo)
139 return repo->gitconfig_owner;
142 int
143 got_repo_has_extension(struct got_repository *repo, const char *ext)
145 int i;
147 for (i = 0; i < repo->nextensions; ++i) {
148 if (!strcasecmp(ext, repo->extnames[i]))
149 return get_boolean_val(repo->extvals[i]);
152 return 0;
155 int
156 got_repo_is_bare(struct got_repository *repo)
158 return (strcmp(repo->path, repo->path_git_dir) == 0);
161 static char *
162 get_path_git_child(struct got_repository *repo, const char *basename)
164 char *path_child;
166 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
167 basename) == -1)
168 return NULL;
170 return path_child;
173 char *
174 got_repo_get_path_objects(struct got_repository *repo)
176 return get_path_git_child(repo, GOT_OBJECTS_DIR);
179 char *
180 got_repo_get_path_objects_pack(struct got_repository *repo)
182 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
185 char *
186 got_repo_get_path_refs(struct got_repository *repo)
188 return get_path_git_child(repo, GOT_REFS_DIR);
191 char *
192 got_repo_get_path_packed_refs(struct got_repository *repo)
194 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
197 static char *
198 get_path_head(struct got_repository *repo)
200 return get_path_git_child(repo, GOT_HEAD_FILE);
203 char *
204 got_repo_get_path_gitconfig(struct got_repository *repo)
206 return get_path_git_child(repo, GOT_GITCONFIG);
209 char *
210 got_repo_get_path_gotconfig(struct got_repository *repo)
212 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
215 const struct got_gotconfig *
216 got_repo_get_gotconfig(struct got_repository *repo)
218 return repo->gotconfig;
221 void
222 got_repo_get_gitconfig_remotes(int *nremotes,
223 const struct got_remote_repo **remotes, struct got_repository *repo)
225 *nremotes = repo->ngitconfig_remotes;
226 *remotes = repo->gitconfig_remotes;
229 static int
230 is_git_repo(struct got_repository *repo)
232 const char *path_git = got_repo_get_path_git_dir(repo);
233 char *path_objects = got_repo_get_path_objects(repo);
234 char *path_refs = got_repo_get_path_refs(repo);
235 char *path_head = get_path_head(repo);
236 int ret = 0;
237 struct stat sb;
238 struct got_reference *head_ref;
240 if (lstat(path_git, &sb) == -1)
241 goto done;
242 if (!S_ISDIR(sb.st_mode))
243 goto done;
245 if (lstat(path_objects, &sb) == -1)
246 goto done;
247 if (!S_ISDIR(sb.st_mode))
248 goto done;
250 if (lstat(path_refs, &sb) == -1)
251 goto done;
252 if (!S_ISDIR(sb.st_mode))
253 goto done;
255 if (lstat(path_head, &sb) == -1)
256 goto done;
257 if (!S_ISREG(sb.st_mode))
258 goto done;
260 /* Check if the HEAD reference can be opened. */
261 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
262 goto done;
263 got_ref_close(head_ref);
265 ret = 1;
266 done:
267 free(path_objects);
268 free(path_refs);
269 free(path_head);
270 return ret;
274 static const struct got_error *
275 close_tempfiles(int *fds, size_t nfds)
277 const struct got_error *err = NULL;
278 int i;
280 for (i = 0; i < nfds; i++) {
281 if (fds[i] == -1)
282 continue;
283 if (close(fds[i]) == -1) {
284 err = got_error_from_errno("close");
285 break;
288 free(fds);
289 return err;
292 static const struct got_error *
293 open_tempfiles(int **fds, size_t array_size, size_t nfds)
295 const struct got_error *err = NULL;
296 int i;
298 *fds = calloc(array_size, sizeof(**fds));
299 if (*fds == NULL)
300 return got_error_from_errno("calloc");
302 for (i = 0; i < array_size; i++)
303 (*fds)[i] = -1;
305 for (i = 0; i < nfds; i++) {
306 (*fds)[i] = got_opentempfd();
307 if ((*fds)[i] == -1) {
308 err = got_error_from_errno("got_opentempfd");
309 close_tempfiles(*fds, nfds);
310 *fds = NULL;
311 return err;
315 return NULL;
318 static const struct got_error *
319 get_pack_cache_size(int *pack_cache_size)
321 struct rlimit rl;
323 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
324 return got_error_from_errno("getrlimit");
326 *pack_cache_size = GOT_PACK_CACHE_SIZE;
327 if (*pack_cache_size > rl.rlim_cur / 8)
328 *pack_cache_size = rl.rlim_cur / 8;
330 return NULL;
333 const struct got_error *
334 got_repo_pack_fds_open(int **pack_fds)
336 const struct got_error *err;
337 int nfds;
339 err = get_pack_cache_size(&nfds);
340 if (err)
341 return err;
343 /*
344 * We need one basefd and one accumfd per cached pack.
345 * Our constants should be set up in a way such that
346 * this error never triggers.
347 */
348 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
349 return got_error(GOT_ERR_NO_SPACE);
351 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
354 const struct got_error *
355 got_repo_pack_fds_close(int *pack_fds)
357 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
360 const struct got_error *
361 got_repo_temp_fds_open(int **temp_fds)
363 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
364 GOT_REPO_NUM_TEMPFILES);
367 void
368 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
370 int i;
372 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
373 repo->tempfiles[i] = temp_fds[i];
376 const struct got_error *
377 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
379 int i;
381 *fd = -1;
382 *idx = -1;
384 for (i = 0; i < nitems(repo->tempfiles); i++) {
385 if (repo->tempfile_use_mask & (1 << i))
386 continue;
387 if (repo->tempfiles[i] != -1) {
388 if (ftruncate(repo->tempfiles[i], 0L) == -1)
389 return got_error_from_errno("ftruncate");
390 *fd = repo->tempfiles[i];
391 *idx = i;
392 repo->tempfile_use_mask |= (1 << i);
393 return NULL;
397 return got_error(GOT_ERR_REPO_TEMPFILE);
400 void
401 got_repo_temp_fds_put(int idx, struct got_repository *repo)
403 repo->tempfile_use_mask &= ~(1 << idx);
406 const struct got_error *
407 got_repo_temp_fds_close(int *temp_fds)
409 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
412 const struct got_error *
413 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
414 struct got_object *obj)
416 #ifndef GOT_NO_OBJ_CACHE
417 const struct got_error *err = NULL;
418 err = got_object_cache_add(&repo->objcache, id, obj);
419 if (err) {
420 if (err->code == GOT_ERR_OBJ_EXISTS ||
421 err->code == GOT_ERR_OBJ_TOO_LARGE)
422 err = NULL;
423 return err;
425 obj->refcnt++;
426 #endif
427 return NULL;
430 struct got_object *
431 got_repo_get_cached_object(struct got_repository *repo,
432 struct got_object_id *id)
434 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
437 const struct got_error *
438 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
439 struct got_tree_object *tree)
441 #ifndef GOT_NO_OBJ_CACHE
442 const struct got_error *err = NULL;
443 err = got_object_cache_add(&repo->treecache, id, tree);
444 if (err) {
445 if (err->code == GOT_ERR_OBJ_EXISTS ||
446 err->code == GOT_ERR_OBJ_TOO_LARGE)
447 err = NULL;
448 return err;
450 tree->refcnt++;
451 #endif
452 return NULL;
455 struct got_tree_object *
456 got_repo_get_cached_tree(struct got_repository *repo,
457 struct got_object_id *id)
459 return (struct got_tree_object *)got_object_cache_get(
460 &repo->treecache, id);
463 const struct got_error *
464 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
465 struct got_commit_object *commit)
467 #ifndef GOT_NO_OBJ_CACHE
468 const struct got_error *err = NULL;
469 err = got_object_cache_add(&repo->commitcache, id, commit);
470 if (err) {
471 if (err->code == GOT_ERR_OBJ_EXISTS ||
472 err->code == GOT_ERR_OBJ_TOO_LARGE)
473 err = NULL;
474 return err;
476 commit->refcnt++;
477 #endif
478 return NULL;
481 struct got_commit_object *
482 got_repo_get_cached_commit(struct got_repository *repo,
483 struct got_object_id *id)
485 return (struct got_commit_object *)got_object_cache_get(
486 &repo->commitcache, id);
489 const struct got_error *
490 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
491 struct got_tag_object *tag)
493 #ifndef GOT_NO_OBJ_CACHE
494 const struct got_error *err = NULL;
495 err = got_object_cache_add(&repo->tagcache, id, tag);
496 if (err) {
497 if (err->code == GOT_ERR_OBJ_EXISTS ||
498 err->code == GOT_ERR_OBJ_TOO_LARGE)
499 err = NULL;
500 return err;
502 tag->refcnt++;
503 #endif
504 return NULL;
507 struct got_tag_object *
508 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
510 return (struct got_tag_object *)got_object_cache_get(
511 &repo->tagcache, id);
514 const struct got_error *
515 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
516 struct got_raw_object *raw)
518 #ifndef GOT_NO_OBJ_CACHE
519 const struct got_error *err = NULL;
520 err = got_object_cache_add(&repo->rawcache, id, raw);
521 if (err) {
522 if (err->code == GOT_ERR_OBJ_EXISTS ||
523 err->code == GOT_ERR_OBJ_TOO_LARGE)
524 err = NULL;
525 return err;
527 raw->refcnt++;
528 #endif
529 return NULL;
533 struct got_raw_object *
534 got_repo_get_cached_raw_object(struct got_repository *repo,
535 struct got_object_id *id)
537 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
541 static const struct got_error *
542 open_repo(struct got_repository *repo, const char *path)
544 const struct got_error *err = NULL;
546 repo->gitdir_fd = -1;
548 /* bare git repository? */
549 repo->path_git_dir = strdup(path);
550 if (repo->path_git_dir == NULL)
551 return got_error_from_errno("strdup");
552 if (is_git_repo(repo)) {
553 repo->path = strdup(repo->path_git_dir);
554 if (repo->path == NULL) {
555 err = got_error_from_errno("strdup");
556 goto done;
558 repo->gitdir_fd = open(repo->path_git_dir,
559 O_DIRECTORY | O_CLOEXEC);
560 if (repo->gitdir_fd == -1) {
561 err = got_error_from_errno2("open",
562 repo->path_git_dir);
563 goto done;
565 return NULL;
568 /* git repository with working tree? */
569 free(repo->path_git_dir);
570 repo->path_git_dir = NULL;
571 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
572 err = got_error_from_errno("asprintf");
573 goto done;
575 if (is_git_repo(repo)) {
576 repo->path = strdup(path);
577 if (repo->path == NULL) {
578 err = got_error_from_errno("strdup");
579 goto done;
581 repo->gitdir_fd = open(repo->path_git_dir,
582 O_DIRECTORY | O_CLOEXEC);
583 if (repo->gitdir_fd == -1) {
584 err = got_error_from_errno2("open",
585 repo->path_git_dir);
586 goto done;
588 return NULL;
591 err = got_error(GOT_ERR_NOT_GIT_REPO);
592 done:
593 if (err) {
594 free(repo->path);
595 repo->path = NULL;
596 free(repo->path_git_dir);
597 repo->path_git_dir = NULL;
598 if (repo->gitdir_fd != -1)
599 close(repo->gitdir_fd);
600 repo->gitdir_fd = -1;
603 return err;
606 static const struct got_error *
607 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
609 const struct got_error *err = NULL;
610 char *repo_gitconfig_path = NULL;
612 if (global_gitconfig_path) {
613 /* Read settings from ~/.gitconfig. */
614 int dummy_repo_version;
615 err = got_repo_read_gitconfig(&dummy_repo_version,
616 &repo->global_gitconfig_author_name,
617 &repo->global_gitconfig_author_email,
618 NULL, NULL, NULL, NULL, NULL, NULL,
619 global_gitconfig_path);
620 if (err)
621 return err;
624 /* Read repository's .git/config file. */
625 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
626 if (repo_gitconfig_path == NULL)
627 return got_error_from_errno("got_repo_get_path_gitconfig");
629 err = got_repo_read_gitconfig(
630 &repo->gitconfig_repository_format_version,
631 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
632 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
633 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
634 &repo->nextensions, repo_gitconfig_path);
635 if (err)
636 goto done;
638 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
639 int i;
641 for (i = 0; i < repo->ngitconfig_remotes; i++) {
642 got_repo_free_remote_repo_data(
643 &repo->gitconfig_remotes[i]);
645 free(repo->gitconfig_remotes);
646 repo->gitconfig_remotes = NULL;
647 repo->ngitconfig_remotes = 0;
649 free(repo->gitconfig_author_name);
650 repo->gitconfig_author_name = NULL;
651 free(repo->gitconfig_author_email);
652 repo->gitconfig_author_email = NULL;
654 free(repo->global_gitconfig_author_name);
655 repo->global_gitconfig_author_name = NULL;
656 free(repo->global_gitconfig_author_email);
657 repo->global_gitconfig_author_email = NULL;
660 done:
661 free(repo_gitconfig_path);
662 return err;
665 static const struct got_error *
666 read_gotconfig(struct got_repository *repo)
668 const struct got_error *err = NULL;
669 char *gotconfig_path;
671 gotconfig_path = got_repo_get_path_gotconfig(repo);
672 if (gotconfig_path == NULL)
673 return got_error_from_errno("got_repo_get_path_gotconfig");
675 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
676 free(gotconfig_path);
677 return err;
680 /* Supported repository format extensions. */
681 static const char *const repo_extensions[] = {
682 "noop", /* Got supports repository format version 1. */
683 "preciousObjects", /* Supported by gotadmin cleanup. */
684 "worktreeConfig", /* Got does not care about Git work trees. */
685 };
687 const struct got_error *
688 got_repo_open(struct got_repository **repop, const char *path,
689 const char *global_gitconfig_path, int *pack_fds)
691 struct got_repository *repo = NULL;
692 const struct got_error *err = NULL;
693 char *repo_path = NULL;
694 size_t i, j = 0;
696 *repop = NULL;
698 repo = calloc(1, sizeof(*repo));
699 if (repo == NULL)
700 return got_error_from_errno("calloc");
702 RB_INIT(&repo->packidx_bloom_filters);
703 TAILQ_INIT(&repo->packidx_paths);
705 for (i = 0; i < nitems(repo->privsep_children); i++) {
706 memset(&repo->privsep_children[i], 0,
707 sizeof(repo->privsep_children[0]));
708 repo->privsep_children[i].imsg_fd = -1;
711 err = got_object_cache_init(&repo->objcache,
712 GOT_OBJECT_CACHE_TYPE_OBJ);
713 if (err)
714 goto done;
715 err = got_object_cache_init(&repo->treecache,
716 GOT_OBJECT_CACHE_TYPE_TREE);
717 if (err)
718 goto done;
719 err = got_object_cache_init(&repo->commitcache,
720 GOT_OBJECT_CACHE_TYPE_COMMIT);
721 if (err)
722 goto done;
723 err = got_object_cache_init(&repo->tagcache,
724 GOT_OBJECT_CACHE_TYPE_TAG);
725 if (err)
726 goto done;
727 err = got_object_cache_init(&repo->rawcache,
728 GOT_OBJECT_CACHE_TYPE_RAW);
729 if (err)
730 goto done;
732 err = get_pack_cache_size(&repo->pack_cache_size);
733 if (err)
734 goto done;
735 for (i = 0; i < nitems(repo->packs); i++) {
736 if (pack_fds != NULL && i < repo->pack_cache_size) {
737 repo->packs[i].basefd = pack_fds[j++];
738 repo->packs[i].accumfd = pack_fds[j++];
739 } else {
740 repo->packs[i].basefd = -1;
741 repo->packs[i].accumfd = -1;
744 for (i = 0; i < nitems(repo->tempfiles); i++)
745 repo->tempfiles[i] = -1;
746 repo->pinned_pack = -1;
747 repo->pinned_packidx = -1;
748 repo->pinned_pid = 0;
750 repo_path = realpath(path, NULL);
751 if (repo_path == NULL) {
752 err = got_error_from_errno2("realpath", path);
753 goto done;
756 for (;;) {
757 char *parent_path;
759 err = open_repo(repo, repo_path);
760 if (err == NULL)
761 break;
762 if (err->code != GOT_ERR_NOT_GIT_REPO)
763 goto done;
764 if (repo_path[0] == '/' && repo_path[1] == '\0') {
765 err = got_error(GOT_ERR_NOT_GIT_REPO);
766 goto done;
768 err = got_path_dirname(&parent_path, repo_path);
769 if (err)
770 goto done;
771 free(repo_path);
772 repo_path = parent_path;
775 err = read_gotconfig(repo);
776 if (err)
777 goto done;
779 err = read_gitconfig(repo, global_gitconfig_path);
780 if (err)
781 goto done;
782 if (repo->gitconfig_repository_format_version != 0) {
783 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
784 goto done;
786 for (i = 0; i < repo->nextensions; i++) {
787 char *ext = repo->extnames[i];
788 char *val = repo->extvals[i];
789 int j, supported = 0;
791 if (!is_boolean_val(val)) {
792 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
793 goto done;
796 if (!get_boolean_val(val))
797 continue;
799 for (j = 0; j < nitems(repo_extensions); j++) {
800 if (strcmp(ext, repo_extensions[j]) == 0) {
801 supported = 1;
802 break;
805 if (!supported) {
806 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
807 goto done;
811 err = got_repo_list_packidx(&repo->packidx_paths, repo);
812 done:
813 if (err)
814 got_repo_close(repo);
815 else
816 *repop = repo;
817 free(repo_path);
818 return err;
821 const struct got_error *
822 got_repo_close(struct got_repository *repo)
824 const struct got_error *err = NULL, *child_err;
825 struct got_packidx_bloom_filter *bf;
826 size_t i;
828 for (i = 0; i < repo->pack_cache_size; i++) {
829 if (repo->packidx_cache[i] == NULL)
830 break;
831 got_packidx_close(repo->packidx_cache[i]);
834 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
835 &repo->packidx_bloom_filters))) {
836 RB_REMOVE(got_packidx_bloom_filter_tree,
837 &repo->packidx_bloom_filters, bf);
838 bloom_free(bf->bloom);
839 free(bf->bloom);
840 free(bf);
843 for (i = 0; i < repo->pack_cache_size; i++)
844 if (repo->packs[i].path_packfile)
845 if (repo->packs[i].path_packfile)
846 got_pack_close(&repo->packs[i]);
848 free(repo->path);
849 free(repo->path_git_dir);
851 got_object_cache_close(&repo->objcache);
852 got_object_cache_close(&repo->treecache);
853 got_object_cache_close(&repo->commitcache);
854 got_object_cache_close(&repo->tagcache);
855 got_object_cache_close(&repo->rawcache);
857 for (i = 0; i < nitems(repo->privsep_children); i++) {
858 if (repo->privsep_children[i].imsg_fd == -1)
859 continue;
860 imsg_clear(repo->privsep_children[i].ibuf);
861 free(repo->privsep_children[i].ibuf);
862 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
863 child_err = got_privsep_wait_for_child(
864 repo->privsep_children[i].pid);
865 if (child_err && err == NULL)
866 err = child_err;
867 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
868 err == NULL)
869 err = got_error_from_errno("close");
872 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
873 err == NULL)
874 err = got_error_from_errno("close");
876 if (repo->gotconfig)
877 got_gotconfig_free(repo->gotconfig);
878 free(repo->gitconfig_author_name);
879 free(repo->gitconfig_author_email);
880 for (i = 0; i < repo->ngitconfig_remotes; i++)
881 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
882 free(repo->gitconfig_remotes);
883 for (i = 0; i < repo->nextensions; i++) {
884 free(repo->extnames[i]);
885 free(repo->extvals[i]);
887 free(repo->extnames);
888 free(repo->extvals);
890 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
891 free(repo);
893 return err;
896 void
897 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
899 int i;
901 free(repo->name);
902 repo->name = NULL;
903 free(repo->fetch_url);
904 repo->fetch_url = NULL;
905 free(repo->send_url);
906 repo->send_url = NULL;
907 for (i = 0; i < repo->nfetch_branches; i++)
908 free(repo->fetch_branches[i]);
909 free(repo->fetch_branches);
910 repo->fetch_branches = NULL;
911 repo->nfetch_branches = 0;
912 for (i = 0; i < repo->nsend_branches; i++)
913 free(repo->send_branches[i]);
914 free(repo->send_branches);
915 repo->send_branches = NULL;
916 repo->nsend_branches = 0;
919 const struct got_error *
920 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
921 const char *input_path)
923 const struct got_error *err = NULL;
924 const char *repo_abspath = NULL;
925 size_t repolen, len;
926 char *canonpath, *path = NULL;
928 *in_repo_path = NULL;
930 canonpath = strdup(input_path);
931 if (canonpath == NULL) {
932 err = got_error_from_errno("strdup");
933 goto done;
935 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
936 if (err)
937 goto done;
939 repo_abspath = got_repo_get_path(repo);
941 if (canonpath[0] == '\0') {
942 path = strdup(canonpath);
943 if (path == NULL) {
944 err = got_error_from_errno("strdup");
945 goto done;
947 } else {
948 path = realpath(canonpath, NULL);
949 if (path == NULL) {
950 if (errno != ENOENT) {
951 err = got_error_from_errno2("realpath",
952 canonpath);
953 goto done;
955 /*
956 * Path is not on disk.
957 * Assume it is already relative to repository root.
958 */
959 path = strdup(canonpath);
960 if (path == NULL) {
961 err = got_error_from_errno("strdup");
962 goto done;
966 repolen = strlen(repo_abspath);
967 len = strlen(path);
970 if (strcmp(path, repo_abspath) == 0) {
971 free(path);
972 path = strdup("");
973 if (path == NULL) {
974 err = got_error_from_errno("strdup");
975 goto done;
977 } else if (len > repolen &&
978 got_path_is_child(path, repo_abspath, repolen)) {
979 /* Matched an on-disk path inside repository. */
980 if (got_repo_is_bare(repo)) {
981 /*
982 * Matched an on-disk path inside repository
983 * database. Treat input as repository-relative.
984 */
985 free(path);
986 path = canonpath;
987 canonpath = NULL;
988 } else {
989 char *child;
990 /* Strip common prefix with repository path. */
991 err = got_path_skip_common_ancestor(&child,
992 repo_abspath, path);
993 if (err)
994 goto done;
995 free(path);
996 path = child;
998 } else {
999 /*
1000 * Matched unrelated on-disk path.
1001 * Treat input as repository-relative.
1003 free(path);
1004 path = canonpath;
1005 canonpath = NULL;
1009 /* Make in-repository path absolute */
1010 if (path[0] != '/') {
1011 char *abspath;
1012 if (asprintf(&abspath, "/%s", path) == -1) {
1013 err = got_error_from_errno("asprintf");
1014 goto done;
1016 free(path);
1017 path = abspath;
1020 done:
1021 free(canonpath);
1022 if (err)
1023 free(path);
1024 else
1025 *in_repo_path = path;
1026 return err;
1029 static const struct got_error *
1030 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1031 const char *path_packidx)
1033 const struct got_error *err = NULL;
1034 size_t i;
1036 for (i = 0; i < repo->pack_cache_size; i++) {
1037 if (repo->packidx_cache[i] == NULL)
1038 break;
1039 if (strcmp(repo->packidx_cache[i]->path_packidx,
1040 path_packidx) == 0) {
1041 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1044 if (i == repo->pack_cache_size) {
1045 do {
1046 i--;
1047 } while (i > 0 && repo->pinned_packidx >= 0 &&
1048 i == repo->pinned_packidx);
1049 err = got_packidx_close(repo->packidx_cache[i]);
1050 if (err)
1051 return err;
1054 repo->packidx_cache[i] = packidx;
1056 return NULL;
1059 int
1060 got_repo_is_packidx_filename(const char *name, size_t len)
1062 if (len != GOT_PACKIDX_NAMELEN)
1063 return 0;
1065 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1066 return 0;
1068 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1069 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1070 return 0;
1072 return 1;
1075 static struct got_packidx_bloom_filter *
1076 get_packidx_bloom_filter(struct got_repository *repo,
1077 const char *path, size_t path_len)
1079 struct got_packidx_bloom_filter key;
1081 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1082 return NULL; /* XXX */
1083 key.path_len = path_len;
1085 return RB_FIND(got_packidx_bloom_filter_tree,
1086 &repo->packidx_bloom_filters, &key);
1089 int
1090 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1091 const char *path_packidx, struct got_object_id *id)
1093 struct got_packidx_bloom_filter *bf;
1095 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1096 if (bf)
1097 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1099 /* No bloom filter means this pack index must be searched. */
1100 return 1;
1103 static const struct got_error *
1104 add_packidx_bloom_filter(struct got_repository *repo,
1105 struct got_packidx *packidx, const char *path_packidx)
1107 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1108 struct got_packidx_bloom_filter *bf;
1109 size_t len;
1112 * Don't use bloom filters for very large pack index files.
1113 * Large pack files will contain a relatively large fraction
1114 * of our objects so we will likely need to visit them anyway.
1115 * The more objects a pack file contains the higher the probability
1116 * of a false-positive match from the bloom filter. And reading
1117 * all object IDs from a large pack index file can be expensive.
1119 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1120 return NULL;
1122 /* Do we already have a filter for this pack index? */
1123 if (get_packidx_bloom_filter(repo, path_packidx,
1124 strlen(path_packidx)) != NULL)
1125 return NULL;
1127 bf = calloc(1, sizeof(*bf));
1128 if (bf == NULL)
1129 return got_error_from_errno("calloc");
1130 bf->bloom = calloc(1, sizeof(*bf->bloom));
1131 if (bf->bloom == NULL) {
1132 free(bf);
1133 return got_error_from_errno("calloc");
1136 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1137 if (len >= sizeof(bf->path)) {
1138 free(bf->bloom);
1139 free(bf);
1140 return got_error(GOT_ERR_NO_SPACE);
1142 bf->path_len = len;
1144 /* Minimum size supported by our bloom filter is 1000 entries. */
1145 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1146 for (i = 0; i < nobjects; i++) {
1147 struct got_packidx_object_id *id;
1148 id = &packidx->hdr.sorted_ids[i];
1149 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1152 RB_INSERT(got_packidx_bloom_filter_tree,
1153 &repo->packidx_bloom_filters, bf);
1154 return NULL;
1157 static void
1158 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1160 struct got_pathlist_entry *pe;
1162 while (!TAILQ_EMPTY(packidx_paths)) {
1163 pe = TAILQ_FIRST(packidx_paths);
1164 TAILQ_REMOVE(packidx_paths, pe, entry);
1165 free((char *)pe->path);
1166 free(pe);
1170 static const struct got_error *
1171 refresh_packidx_paths(struct got_repository *repo)
1173 const struct got_error *err = NULL;
1174 char *objects_pack_dir = NULL;
1175 struct stat sb;
1177 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1178 if (objects_pack_dir == NULL)
1179 return got_error_from_errno("got_repo_get_path_objects_pack");
1181 if (stat(objects_pack_dir, &sb) == -1) {
1182 if (errno != ENOENT) {
1183 err = got_error_from_errno2("stat", objects_pack_dir);
1184 goto done;
1186 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1187 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1188 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1189 purge_packidx_paths(&repo->packidx_paths);
1190 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1191 if (err)
1192 goto done;
1194 done:
1195 free(objects_pack_dir);
1196 return err;
1199 const struct got_error *
1200 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1201 struct got_repository *repo, struct got_object_id *id)
1203 const struct got_error *err;
1204 struct got_pathlist_entry *pe;
1205 size_t i;
1207 /* Search pack index cache. */
1208 for (i = 0; i < repo->pack_cache_size; i++) {
1209 if (repo->packidx_cache[i] == NULL)
1210 break;
1211 if (!got_repo_check_packidx_bloom_filter(repo,
1212 repo->packidx_cache[i]->path_packidx, id))
1213 continue; /* object will not be found in this index */
1214 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1215 if (*idx != -1) {
1216 *packidx = repo->packidx_cache[i];
1218 * Move this cache entry to the front. Repeatedly
1219 * searching a wrong pack index can be expensive.
1221 if (i > 0) {
1222 memmove(&repo->packidx_cache[1],
1223 &repo->packidx_cache[0],
1224 i * sizeof(repo->packidx_cache[0]));
1225 repo->packidx_cache[0] = *packidx;
1226 if (repo->pinned_packidx >= 0 &&
1227 repo->pinned_packidx < i)
1228 repo->pinned_packidx++;
1229 else if (repo->pinned_packidx == i)
1230 repo->pinned_packidx = 0;
1232 return NULL;
1235 /* No luck. Search the filesystem. */
1237 err = refresh_packidx_paths(repo);
1238 if (err)
1239 return err;
1241 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1242 const char *path_packidx = pe->path;
1243 int is_cached = 0;
1245 if (!got_repo_check_packidx_bloom_filter(repo,
1246 pe->path, id))
1247 continue; /* object will not be found in this index */
1249 for (i = 0; i < repo->pack_cache_size; i++) {
1250 if (repo->packidx_cache[i] == NULL)
1251 break;
1252 if (strcmp(repo->packidx_cache[i]->path_packidx,
1253 path_packidx) == 0) {
1254 is_cached = 1;
1255 break;
1258 if (is_cached)
1259 continue; /* already searched */
1261 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1262 path_packidx, 0);
1263 if (err)
1264 goto done;
1266 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1267 if (err)
1268 goto done;
1270 err = cache_packidx(repo, *packidx, path_packidx);
1271 if (err)
1272 goto done;
1274 *idx = got_packidx_get_object_idx(*packidx, id);
1275 if (*idx != -1) {
1276 err = NULL; /* found the object */
1277 goto done;
1281 err = got_error_no_obj(id);
1282 done:
1283 return err;
1286 const struct got_error *
1287 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1288 struct got_repository *repo)
1290 const struct got_error *err = NULL;
1291 DIR *packdir = NULL;
1292 struct dirent *dent;
1293 char *path_packidx = NULL;
1294 int packdir_fd;
1295 struct stat sb;
1297 packdir_fd = openat(got_repo_get_fd(repo),
1298 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1299 if (packdir_fd == -1) {
1300 return got_error_from_errno_fmt("openat: %s/%s",
1301 got_repo_get_path_git_dir(repo),
1302 GOT_OBJECTS_PACK_DIR);
1305 packdir = fdopendir(packdir_fd);
1306 if (packdir == NULL) {
1307 err = got_error_from_errno("fdopendir");
1308 goto done;
1311 if (fstat(packdir_fd, &sb) == -1) {
1312 err = got_error_from_errno("fstat");
1313 goto done;
1315 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1316 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1318 while ((dent = readdir(packdir)) != NULL) {
1319 if (!got_repo_is_packidx_filename(dent->d_name,
1320 strlen(dent->d_name)))
1321 continue;
1323 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1324 dent->d_name) == -1) {
1325 err = got_error_from_errno("asprintf");
1326 path_packidx = NULL;
1327 break;
1330 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1331 if (err)
1332 break;
1334 done:
1335 if (err)
1336 free(path_packidx);
1337 if (packdir && closedir(packdir) != 0 && err == NULL)
1338 err = got_error_from_errno("closedir");
1339 return err;
1342 const struct got_error *
1343 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1344 struct got_repository *repo)
1346 const struct got_error *err;
1347 size_t i;
1349 *packidx = NULL;
1351 /* Search pack index cache. */
1352 for (i = 0; i < repo->pack_cache_size; i++) {
1353 if (repo->packidx_cache[i] == NULL)
1354 break;
1355 if (strcmp(repo->packidx_cache[i]->path_packidx,
1356 path_packidx) == 0) {
1357 *packidx = repo->packidx_cache[i];
1358 return NULL;
1361 /* No luck. Search the filesystem. */
1363 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1364 path_packidx, 0);
1365 if (err)
1366 return err;
1368 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1369 if (err)
1370 goto done;
1372 err = cache_packidx(repo, *packidx, path_packidx);
1373 done:
1374 if (err) {
1375 got_packidx_close(*packidx);
1376 *packidx = NULL;
1378 return err;
1381 static const struct got_error *
1382 read_packfile_hdr(int fd, struct got_packidx *packidx)
1384 const struct got_error *err = NULL;
1385 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1386 struct got_packfile_hdr hdr;
1387 ssize_t n;
1389 n = read(fd, &hdr, sizeof(hdr));
1390 if (n < 0)
1391 return got_error_from_errno("read");
1392 if (n != sizeof(hdr))
1393 return got_error(GOT_ERR_BAD_PACKFILE);
1395 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1396 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1397 be32toh(hdr.nobjects) != totobj)
1398 err = got_error(GOT_ERR_BAD_PACKFILE);
1400 return err;
1403 static const struct got_error *
1404 open_packfile(int *fd, struct got_repository *repo,
1405 const char *relpath, struct got_packidx *packidx)
1407 const struct got_error *err = NULL;
1409 *fd = openat(got_repo_get_fd(repo), relpath,
1410 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1411 if (*fd == -1)
1412 return got_error_from_errno_fmt("openat: %s/%s",
1413 got_repo_get_path_git_dir(repo), relpath);
1415 if (packidx) {
1416 err = read_packfile_hdr(*fd, packidx);
1417 if (err) {
1418 close(*fd);
1419 *fd = -1;
1423 return err;
1426 const struct got_error *
1427 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1428 const char *path_packfile, struct got_packidx *packidx)
1430 const struct got_error *err = NULL;
1431 struct got_pack *pack = NULL;
1432 struct stat sb;
1433 size_t i;
1435 if (packp)
1436 *packp = NULL;
1438 for (i = 0; i < repo->pack_cache_size; i++) {
1439 pack = &repo->packs[i];
1440 if (pack->path_packfile == NULL)
1441 break;
1442 if (strcmp(pack->path_packfile, path_packfile) == 0)
1443 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1446 if (i == repo->pack_cache_size) {
1447 struct got_pack tmp;
1448 do {
1449 i--;
1450 } while (i > 0 && repo->pinned_pack >= 0 &&
1451 i == repo->pinned_pack);
1452 err = got_pack_close(&repo->packs[i]);
1453 if (err)
1454 return err;
1455 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1456 return got_error_from_errno("ftruncate");
1457 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1458 return got_error_from_errno("ftruncate");
1459 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1460 memcpy(&repo->packs[i], &repo->packs[0],
1461 sizeof(repo->packs[i]));
1462 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1463 if (repo->pinned_pack == 0)
1464 repo->pinned_pack = i;
1465 else if (repo->pinned_pack == i)
1466 repo->pinned_pack = 0;
1467 i = 0;
1470 pack = &repo->packs[i];
1472 pack->path_packfile = strdup(path_packfile);
1473 if (pack->path_packfile == NULL) {
1474 err = got_error_from_errno("strdup");
1475 goto done;
1478 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1479 if (err)
1480 goto done;
1482 if (fstat(pack->fd, &sb) != 0) {
1483 err = got_error_from_errno("fstat");
1484 goto done;
1486 pack->filesize = sb.st_size;
1488 pack->privsep_child = NULL;
1490 err = got_delta_cache_alloc(&pack->delta_cache);
1491 if (err)
1492 goto done;
1494 #ifndef GOT_PACK_NO_MMAP
1495 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1496 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1497 pack->fd, 0);
1498 if (pack->map == MAP_FAILED) {
1499 if (errno != ENOMEM) {
1500 err = got_error_from_errno("mmap");
1501 goto done;
1503 pack->map = NULL; /* fall back to read(2) */
1506 #endif
1507 done:
1508 if (err) {
1509 if (pack)
1510 got_pack_close(pack);
1511 } else if (packp)
1512 *packp = pack;
1513 return err;
1516 struct got_pack *
1517 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1519 struct got_pack *pack = NULL;
1520 size_t i;
1522 for (i = 0; i < repo->pack_cache_size; i++) {
1523 pack = &repo->packs[i];
1524 if (pack->path_packfile == NULL)
1525 break;
1526 if (strcmp(pack->path_packfile, path_packfile) == 0)
1527 return pack;
1530 return NULL;
1533 const struct got_error *
1534 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1535 struct got_pack *pack)
1537 size_t i;
1538 int pinned_pack = -1, pinned_packidx = -1;
1540 for (i = 0; i < repo->pack_cache_size; i++) {
1541 if (repo->packidx_cache[i] &&
1542 strcmp(repo->packidx_cache[i]->path_packidx,
1543 packidx->path_packidx) == 0)
1544 pinned_packidx = i;
1545 if (repo->packs[i].path_packfile &&
1546 strcmp(repo->packs[i].path_packfile,
1547 pack->path_packfile) == 0)
1548 pinned_pack = i;
1551 if (pinned_packidx == -1 || pinned_pack == -1)
1552 return got_error(GOT_ERR_PIN_PACK);
1554 repo->pinned_pack = pinned_pack;
1555 repo->pinned_packidx = pinned_packidx;
1556 if (repo->packs[pinned_pack].privsep_child)
1557 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1558 return NULL;
1561 struct got_pack *
1562 got_repo_get_pinned_pack(struct got_repository *repo)
1564 if (repo->pinned_pack >= 0 &&
1565 repo->pinned_pack < repo->pack_cache_size)
1566 return &repo->packs[repo->pinned_pack];
1568 return NULL;
1571 void
1572 got_repo_unpin_pack(struct got_repository *repo)
1574 repo->pinned_packidx = -1;
1575 repo->pinned_pack = -1;
1576 repo->pinned_pid = 0;
1579 const struct got_error *
1580 got_repo_init(const char *repo_path, const char *head_name)
1582 const struct got_error *err = NULL;
1583 const char *dirnames[] = {
1584 GOT_OBJECTS_DIR,
1585 GOT_OBJECTS_PACK_DIR,
1586 GOT_REFS_DIR,
1588 const char *description_str = "Unnamed repository; "
1589 "edit this file 'description' to name the repository.";
1590 const char *headref = "ref: refs/heads/";
1591 const char *gitconfig_str = "[core]\n"
1592 "\trepositoryformatversion = 0\n"
1593 "\tfilemode = true\n"
1594 "\tbare = true\n";
1595 char *headref_str, *path;
1596 size_t i;
1598 if (!got_path_dir_is_empty(repo_path))
1599 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1601 for (i = 0; i < nitems(dirnames); i++) {
1602 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1603 return got_error_from_errno("asprintf");
1605 err = got_path_mkdir(path);
1606 free(path);
1607 if (err)
1608 return err;
1611 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1612 return got_error_from_errno("asprintf");
1613 err = got_path_create_file(path, description_str);
1614 free(path);
1615 if (err)
1616 return err;
1618 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1619 return got_error_from_errno("asprintf");
1620 if (asprintf(&headref_str, "%s%s", headref,
1621 head_name ? head_name : "main") == -1) {
1622 free(path);
1623 return got_error_from_errno("asprintf");
1625 err = got_path_create_file(path, headref_str);
1626 free(headref_str);
1627 free(path);
1628 if (err)
1629 return err;
1631 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1632 return got_error_from_errno("asprintf");
1633 err = got_path_create_file(path, gitconfig_str);
1634 free(path);
1635 if (err)
1636 return err;
1638 return NULL;
1641 static const struct got_error *
1642 match_packed_object(struct got_object_id **unique_id,
1643 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1645 const struct got_error *err = NULL;
1646 struct got_object_id_queue matched_ids;
1647 struct got_pathlist_entry *pe;
1649 STAILQ_INIT(&matched_ids);
1651 err = refresh_packidx_paths(repo);
1652 if (err)
1653 return err;
1655 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1656 const char *path_packidx = pe->path;
1657 struct got_packidx *packidx;
1658 struct got_object_qid *qid;
1660 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1661 path_packidx, 0);
1662 if (err)
1663 break;
1665 err = got_packidx_match_id_str_prefix(&matched_ids,
1666 packidx, id_str_prefix);
1667 if (err) {
1668 got_packidx_close(packidx);
1669 break;
1671 err = got_packidx_close(packidx);
1672 if (err)
1673 break;
1675 STAILQ_FOREACH(qid, &matched_ids, entry) {
1676 if (obj_type != GOT_OBJ_TYPE_ANY) {
1677 int matched_type;
1678 err = got_object_get_type(&matched_type, repo,
1679 &qid->id);
1680 if (err)
1681 goto done;
1682 if (matched_type != obj_type)
1683 continue;
1685 if (*unique_id == NULL) {
1686 *unique_id = got_object_id_dup(&qid->id);
1687 if (*unique_id == NULL) {
1688 err = got_error_from_errno("malloc");
1689 goto done;
1691 } else {
1692 if (got_object_id_cmp(*unique_id,
1693 &qid->id) == 0)
1694 continue; /* packed multiple times */
1695 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1696 goto done;
1700 done:
1701 got_object_id_queue_free(&matched_ids);
1702 if (err) {
1703 free(*unique_id);
1704 *unique_id = NULL;
1706 return err;
1709 static const struct got_error *
1710 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1711 const char *object_dir, const char *id_str_prefix, int obj_type,
1712 struct got_repository *repo)
1714 const struct got_error *err = NULL;
1715 char *path, *id_str = NULL;
1716 DIR *dir = NULL;
1717 struct dirent *dent;
1718 struct got_object_id id;
1720 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1721 err = got_error_from_errno("asprintf");
1722 goto done;
1725 dir = opendir(path);
1726 if (dir == NULL) {
1727 if (errno == ENOENT) {
1728 err = NULL;
1729 goto done;
1731 err = got_error_from_errno2("opendir", path);
1732 goto done;
1734 while ((dent = readdir(dir)) != NULL) {
1735 int cmp;
1737 free(id_str);
1738 id_str = NULL;
1740 if (strcmp(dent->d_name, ".") == 0 ||
1741 strcmp(dent->d_name, "..") == 0)
1742 continue;
1744 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1745 err = got_error_from_errno("asprintf");
1746 goto done;
1749 if (!got_parse_sha1_digest(id.sha1, id_str))
1750 continue;
1753 * Directory entries do not necessarily appear in
1754 * sorted order, so we must iterate over all of them.
1756 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1757 if (cmp != 0)
1758 continue;
1760 if (*unique_id == NULL) {
1761 if (obj_type != GOT_OBJ_TYPE_ANY) {
1762 int matched_type;
1763 err = got_object_get_type(&matched_type, repo,
1764 &id);
1765 if (err)
1766 goto done;
1767 if (matched_type != obj_type)
1768 continue;
1770 *unique_id = got_object_id_dup(&id);
1771 if (*unique_id == NULL) {
1772 err = got_error_from_errno("got_object_id_dup");
1773 goto done;
1775 } else {
1776 if (got_object_id_cmp(*unique_id, &id) == 0)
1777 continue; /* both packed and loose */
1778 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1779 goto done;
1782 done:
1783 if (dir && closedir(dir) != 0 && err == NULL)
1784 err = got_error_from_errno("closedir");
1785 if (err) {
1786 free(*unique_id);
1787 *unique_id = NULL;
1789 free(id_str);
1790 free(path);
1791 return err;
1794 const struct got_error *
1795 got_repo_match_object_id_prefix(struct got_object_id **id,
1796 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1798 const struct got_error *err = NULL;
1799 char *path_objects = NULL, *object_dir = NULL;
1800 size_t len;
1801 int i;
1803 *id = NULL;
1805 path_objects = got_repo_get_path_objects(repo);
1807 len = strlen(id_str_prefix);
1808 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1809 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1810 goto done;
1813 for (i = 0; i < len; i++) {
1814 if (isxdigit((unsigned char)id_str_prefix[i]))
1815 continue;
1816 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1817 goto done;
1820 if (len >= 2) {
1821 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1822 if (err)
1823 goto done;
1824 object_dir = strndup(id_str_prefix, 2);
1825 if (object_dir == NULL) {
1826 err = got_error_from_errno("strdup");
1827 goto done;
1829 err = match_loose_object(id, path_objects, object_dir,
1830 id_str_prefix, obj_type, repo);
1831 } else if (len == 1) {
1832 int i;
1833 for (i = 0; i < 0xf; i++) {
1834 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1835 == -1) {
1836 err = got_error_from_errno("asprintf");
1837 goto done;
1839 err = match_packed_object(id, repo, object_dir,
1840 obj_type);
1841 if (err)
1842 goto done;
1843 err = match_loose_object(id, path_objects, object_dir,
1844 id_str_prefix, obj_type, repo);
1845 if (err)
1846 goto done;
1848 } else {
1849 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1850 goto done;
1852 done:
1853 free(path_objects);
1854 free(object_dir);
1855 if (err) {
1856 free(*id);
1857 *id = NULL;
1858 } else if (*id == NULL) {
1859 switch (obj_type) {
1860 case GOT_OBJ_TYPE_BLOB:
1861 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1862 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1863 break;
1864 case GOT_OBJ_TYPE_TREE:
1865 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1866 GOT_OBJ_LABEL_TREE, id_str_prefix);
1867 break;
1868 case GOT_OBJ_TYPE_COMMIT:
1869 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1870 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1871 break;
1872 case GOT_OBJ_TYPE_TAG:
1873 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1874 GOT_OBJ_LABEL_TAG, id_str_prefix);
1875 break;
1876 default:
1877 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1878 break;
1882 return err;
1885 const struct got_error *
1886 got_repo_match_object_id(struct got_object_id **id, char **label,
1887 const char *id_str, int obj_type, struct got_reflist_head *refs,
1888 struct got_repository *repo)
1890 const struct got_error *err;
1891 struct got_tag_object *tag;
1892 struct got_reference *ref = NULL;
1894 *id = NULL;
1895 if (label)
1896 *label = NULL;
1898 if (refs) {
1899 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1900 refs, repo);
1901 if (err == NULL) {
1902 *id = got_object_id_dup(
1903 got_object_tag_get_object_id(tag));
1904 if (*id == NULL)
1905 err = got_error_from_errno("got_object_id_dup");
1906 else if (label && asprintf(label, "refs/tags/%s",
1907 got_object_tag_get_name(tag)) == -1) {
1908 err = got_error_from_errno("asprintf");
1909 free(*id);
1910 *id = NULL;
1912 got_object_tag_close(tag);
1913 return err;
1914 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1915 err->code != GOT_ERR_NO_OBJ)
1916 return err;
1919 err = got_ref_open(&ref, repo, id_str, 0);
1920 if (err == NULL) {
1921 err = got_ref_resolve(id, repo, ref);
1922 if (err)
1923 goto done;
1924 if (label) {
1925 *label = strdup(got_ref_get_name(ref));
1926 if (*label == NULL) {
1927 err = got_error_from_errno("strdup");
1928 goto done;
1931 } else {
1932 if (err->code != GOT_ERR_NOT_REF &&
1933 err->code != GOT_ERR_BAD_REF_NAME)
1934 goto done;
1935 err = got_repo_match_object_id_prefix(id, id_str,
1936 obj_type, repo);
1937 if (err) {
1938 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1939 err = got_error_not_ref(id_str);
1940 goto done;
1942 if (label) {
1943 err = got_object_id_str(label, *id);
1944 if (*label == NULL) {
1945 err = got_error_from_errno("strdup");
1946 goto done;
1950 done:
1951 if (ref)
1952 got_ref_close(ref);
1953 return err;
1956 const struct got_error *
1957 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1958 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1960 const struct got_error *err = NULL;
1961 struct got_reflist_entry *re;
1962 struct got_object_id *tag_id;
1963 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1965 *tag = NULL;
1967 TAILQ_FOREACH(re, refs, entry) {
1968 const char *refname;
1969 refname = got_ref_get_name(re->ref);
1970 if (got_ref_is_symbolic(re->ref))
1971 continue;
1972 if (strncmp(refname, "refs/tags/", 10) != 0)
1973 continue;
1974 if (!name_is_absolute)
1975 refname += strlen("refs/tags/");
1976 if (strcmp(refname, name) != 0)
1977 continue;
1978 err = got_ref_resolve(&tag_id, repo, re->ref);
1979 if (err)
1980 break;
1981 err = got_object_open_as_tag(tag, repo, tag_id);
1982 free(tag_id);
1983 if (err)
1984 break;
1985 if (obj_type == GOT_OBJ_TYPE_ANY ||
1986 got_object_tag_get_object_type(*tag) == obj_type)
1987 break;
1988 got_object_tag_close(*tag);
1989 *tag = NULL;
1992 if (err == NULL && *tag == NULL)
1993 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1994 GOT_OBJ_LABEL_TAG, name);
1995 return err;
1998 static const struct got_error *
1999 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2000 const char *name, mode_t mode, struct got_object_id *blob_id)
2002 const struct got_error *err = NULL;
2004 *new_te = NULL;
2006 *new_te = calloc(1, sizeof(**new_te));
2007 if (*new_te == NULL)
2008 return got_error_from_errno("calloc");
2010 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2011 sizeof((*new_te)->name)) {
2012 err = got_error(GOT_ERR_NO_SPACE);
2013 goto done;
2016 if (S_ISLNK(mode)) {
2017 (*new_te)->mode = S_IFLNK;
2018 } else {
2019 (*new_te)->mode = S_IFREG;
2020 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2022 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2023 done:
2024 if (err && *new_te) {
2025 free(*new_te);
2026 *new_te = NULL;
2028 return err;
2031 static const struct got_error *
2032 import_file(struct got_tree_entry **new_te, struct dirent *de,
2033 const char *path, struct got_repository *repo)
2035 const struct got_error *err;
2036 struct got_object_id *blob_id = NULL;
2037 char *filepath;
2038 struct stat sb;
2040 if (asprintf(&filepath, "%s%s%s", path,
2041 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2042 return got_error_from_errno("asprintf");
2044 if (lstat(filepath, &sb) != 0) {
2045 err = got_error_from_errno2("lstat", path);
2046 goto done;
2049 err = got_object_blob_create(&blob_id, filepath, repo);
2050 if (err)
2051 goto done;
2053 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2054 blob_id);
2055 done:
2056 free(filepath);
2057 if (err)
2058 free(blob_id);
2059 return err;
2062 static const struct got_error *
2063 insert_tree_entry(struct got_tree_entry *new_te,
2064 struct got_pathlist_head *paths)
2066 const struct got_error *err = NULL;
2067 struct got_pathlist_entry *new_pe;
2069 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2070 if (err)
2071 return err;
2072 if (new_pe == NULL)
2073 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2074 return NULL;
2077 static const struct got_error *write_tree(struct got_object_id **,
2078 const char *, struct got_pathlist_head *, struct got_repository *,
2079 got_repo_import_cb progress_cb, void *progress_arg);
2081 static const struct got_error *
2082 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2083 const char *path, struct got_pathlist_head *ignores,
2084 struct got_repository *repo,
2085 got_repo_import_cb progress_cb, void *progress_arg)
2087 const struct got_error *err;
2088 struct got_object_id *id = NULL;
2089 char *subdirpath;
2091 if (asprintf(&subdirpath, "%s%s%s", path,
2092 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2093 return got_error_from_errno("asprintf");
2095 (*new_te) = calloc(1, sizeof(**new_te));
2096 if (*new_te == NULL)
2097 return got_error_from_errno("calloc");
2098 (*new_te)->mode = S_IFDIR;
2099 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2100 sizeof((*new_te)->name)) {
2101 err = got_error(GOT_ERR_NO_SPACE);
2102 goto done;
2104 err = write_tree(&id, subdirpath, ignores, repo,
2105 progress_cb, progress_arg);
2106 if (err)
2107 goto done;
2108 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2110 done:
2111 free(id);
2112 free(subdirpath);
2113 if (err) {
2114 free(*new_te);
2115 *new_te = NULL;
2117 return err;
2120 static const struct got_error *
2121 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2122 struct got_pathlist_head *ignores, struct got_repository *repo,
2123 got_repo_import_cb progress_cb, void *progress_arg)
2125 const struct got_error *err = NULL;
2126 DIR *dir;
2127 struct dirent *de;
2128 int nentries;
2129 struct got_tree_entry *new_te = NULL;
2130 struct got_pathlist_head paths;
2131 struct got_pathlist_entry *pe;
2133 *new_tree_id = NULL;
2135 TAILQ_INIT(&paths);
2137 dir = opendir(path_dir);
2138 if (dir == NULL) {
2139 err = got_error_from_errno2("opendir", path_dir);
2140 goto done;
2143 nentries = 0;
2144 while ((de = readdir(dir)) != NULL) {
2145 int ignore = 0;
2146 int type;
2148 if (strcmp(de->d_name, ".") == 0 ||
2149 strcmp(de->d_name, "..") == 0)
2150 continue;
2152 err = got_path_dirent_type(&type, path_dir, de);
2153 if (err)
2154 goto done;
2156 TAILQ_FOREACH(pe, ignores, entry) {
2157 if (type == DT_DIR && pe->path_len > 0 &&
2158 pe->path[pe->path_len - 1] == '/') {
2159 char stripped[PATH_MAX];
2161 if (strlcpy(stripped, pe->path,
2162 sizeof(stripped)) >= sizeof(stripped)) {
2163 err = got_error(GOT_ERR_NO_SPACE);
2164 goto done;
2166 got_path_strip_trailing_slashes(stripped);
2167 if (fnmatch(stripped, de->d_name, 0) == 0) {
2168 ignore = 1;
2169 break;
2171 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2172 ignore = 1;
2173 break;
2176 if (ignore)
2177 continue;
2179 if (type == DT_DIR) {
2180 err = import_subdir(&new_te, de, path_dir,
2181 ignores, repo, progress_cb, progress_arg);
2182 if (err) {
2183 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2184 goto done;
2185 err = NULL;
2186 continue;
2188 } else if (type == DT_REG || type == DT_LNK) {
2189 err = import_file(&new_te, de, path_dir, repo);
2190 if (err)
2191 goto done;
2192 } else
2193 continue;
2195 err = insert_tree_entry(new_te, &paths);
2196 if (err)
2197 goto done;
2198 nentries++;
2201 if (TAILQ_EMPTY(&paths)) {
2202 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2203 "cannot create tree without any entries");
2204 goto done;
2207 TAILQ_FOREACH(pe, &paths, entry) {
2208 struct got_tree_entry *te = pe->data;
2209 char *path;
2210 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2211 continue;
2212 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2213 err = got_error_from_errno("asprintf");
2214 goto done;
2216 err = (*progress_cb)(progress_arg, path);
2217 free(path);
2218 if (err)
2219 goto done;
2222 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2223 done:
2224 if (dir)
2225 closedir(dir);
2226 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2227 return err;
2230 const struct got_error *
2231 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2232 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2233 struct got_repository *repo, got_repo_import_cb progress_cb,
2234 void *progress_arg)
2236 const struct got_error *err;
2237 struct got_object_id *new_tree_id;
2239 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2240 progress_cb, progress_arg);
2241 if (err)
2242 return err;
2244 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2245 author, time(NULL), author, time(NULL), logmsg, repo);
2246 free(new_tree_id);
2247 return err;
2250 const struct got_error *
2251 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2252 struct got_repository *repo)
2254 const struct got_error *err = NULL;
2255 char *path_objects = NULL, *path = NULL;
2256 DIR *dir = NULL;
2257 struct got_object_id id;
2258 int i;
2260 *nobjects = 0;
2261 *ondisk_size = 0;
2263 path_objects = got_repo_get_path_objects(repo);
2264 if (path_objects == NULL)
2265 return got_error_from_errno("got_repo_get_path_objects");
2267 for (i = 0; i <= 0xff; i++) {
2268 struct dirent *dent;
2270 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2271 err = got_error_from_errno("asprintf");
2272 break;
2275 dir = opendir(path);
2276 if (dir == NULL) {
2277 if (errno == ENOENT) {
2278 err = NULL;
2279 continue;
2281 err = got_error_from_errno2("opendir", path);
2282 break;
2285 while ((dent = readdir(dir)) != NULL) {
2286 char *id_str;
2287 int fd;
2288 struct stat sb;
2290 if (strcmp(dent->d_name, ".") == 0 ||
2291 strcmp(dent->d_name, "..") == 0)
2292 continue;
2294 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2295 err = got_error_from_errno("asprintf");
2296 goto done;
2299 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2300 free(id_str);
2301 continue;
2303 free(id_str);
2305 err = got_object_open_loose_fd(&fd, &id, repo);
2306 if (err)
2307 goto done;
2309 if (fstat(fd, &sb) == -1) {
2310 err = got_error_from_errno("fstat");
2311 close(fd);
2312 goto done;
2314 (*nobjects)++;
2315 (*ondisk_size) += sb.st_size;
2317 if (close(fd) == -1) {
2318 err = got_error_from_errno("close");
2319 goto done;
2323 if (closedir(dir) != 0) {
2324 err = got_error_from_errno("closedir");
2325 goto done;
2327 dir = NULL;
2329 free(path);
2330 path = NULL;
2332 done:
2333 if (dir && closedir(dir) != 0 && err == NULL)
2334 err = got_error_from_errno("closedir");
2336 if (err) {
2337 *nobjects = 0;
2338 *ondisk_size = 0;
2340 free(path_objects);
2341 free(path);
2342 return err;
2345 const struct got_error *
2346 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2347 off_t *total_packsize, struct got_repository *repo)
2349 const struct got_error *err = NULL;
2350 DIR *packdir = NULL;
2351 struct dirent *dent;
2352 struct got_packidx *packidx = NULL;
2353 char *path_packidx;
2354 char *path_packfile;
2355 int packdir_fd;
2356 struct stat sb;
2358 *npackfiles = 0;
2359 *nobjects = 0;
2360 *total_packsize = 0;
2362 packdir_fd = openat(got_repo_get_fd(repo),
2363 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2364 if (packdir_fd == -1) {
2365 return got_error_from_errno_fmt("openat: %s/%s",
2366 got_repo_get_path_git_dir(repo),
2367 GOT_OBJECTS_PACK_DIR);
2370 packdir = fdopendir(packdir_fd);
2371 if (packdir == NULL) {
2372 err = got_error_from_errno("fdopendir");
2373 goto done;
2376 while ((dent = readdir(packdir)) != NULL) {
2377 if (!got_repo_is_packidx_filename(dent->d_name,
2378 strlen(dent->d_name)))
2379 continue;
2381 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2382 dent->d_name) == -1) {
2383 err = got_error_from_errno("asprintf");
2384 goto done;
2387 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2388 path_packidx, 0);
2389 free(path_packidx);
2390 if (err)
2391 goto done;
2393 if (fstat(packidx->fd, &sb) == -1)
2394 goto done;
2395 *total_packsize += sb.st_size;
2397 err = got_packidx_get_packfile_path(&path_packfile,
2398 packidx->path_packidx);
2399 if (err)
2400 goto done;
2402 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2403 0) == -1) {
2404 free(path_packfile);
2405 goto done;
2407 free(path_packfile);
2408 *total_packsize += sb.st_size;
2410 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2412 (*npackfiles)++;
2414 got_packidx_close(packidx);
2415 packidx = NULL;
2417 done:
2418 if (packidx)
2419 got_packidx_close(packidx);
2420 if (packdir && closedir(packdir) != 0 && err == NULL)
2421 err = got_error_from_errno("closedir");
2422 if (err) {
2423 *npackfiles = 0;
2424 *nobjects = 0;
2425 *total_packsize = 0;
2427 return err;
2430 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2431 got_packidx_bloom_filter_cmp);