Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <sha2.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <zlib.h>
40 #include <errno.h>
41 #include <libgen.h>
42 #include <stdint.h>
43 #include <imsg.h>
44 #include <uuid.h>
46 #include "bloom.h"
48 #include "got_error.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_path.h"
52 #include "got_cancel.h"
53 #include "got_object.h"
54 #include "got_opentemp.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_delta_cache.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_object.h"
60 #include "got_lib_object_parse.h"
61 #include "got_lib_object_create.h"
62 #include "got_lib_pack.h"
63 #include "got_lib_privsep.h"
64 #include "got_lib_hash.h"
65 #include "got_lib_object_cache.h"
66 #include "got_lib_repository.h"
67 #include "got_lib_gotconfig.h"
69 #ifndef nitems
70 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
71 #endif
73 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
75 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
76 got_packidx_bloom_filter_cmp);
78 static inline int
79 is_boolean_val(const char *val)
80 {
81 return (strcasecmp(val, "true") == 0 ||
82 strcasecmp(val, "false") == 0 ||
83 strcasecmp(val, "on") == 0 ||
84 strcasecmp(val, "off") == 0 ||
85 strcasecmp(val, "yes") == 0 ||
86 strcasecmp(val, "no") == 0 ||
87 strcasecmp(val, "1") == 0 ||
88 strcasecmp(val, "0") == 0);
89 }
91 static inline int
92 get_boolean_val(const char *val)
93 {
94 return (strcasecmp(val, "true") == 0 ||
95 strcasecmp(val, "on") == 0 ||
96 strcasecmp(val, "yes") == 0 ||
97 strcasecmp(val, "1") == 0);
98 }
100 const char *
101 got_repo_get_path(struct got_repository *repo)
103 return repo->path;
106 const char *
107 got_repo_get_path_git_dir(struct got_repository *repo)
109 return repo->path_git_dir;
112 int
113 got_repo_get_fd(struct got_repository *repo)
115 return repo->gitdir_fd;
118 enum got_hash_algorithm
119 got_repo_get_object_format(struct got_repository *repo)
121 return repo->algo;
124 const char *
125 got_repo_get_gitconfig_author_name(struct got_repository *repo)
127 return repo->gitconfig_author_name;
130 const char *
131 got_repo_get_gitconfig_author_email(struct got_repository *repo)
133 return repo->gitconfig_author_email;
136 const char *
137 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
139 return repo->global_gitconfig_author_name;
142 const char *
143 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
145 return repo->global_gitconfig_author_email;
148 const char *
149 got_repo_get_gitconfig_owner(struct got_repository *repo)
151 return repo->gitconfig_owner;
154 int
155 got_repo_has_extension(struct got_repository *repo, const char *ext)
157 int i;
159 for (i = 0; i < repo->nextensions; ++i) {
160 if (!strcasecmp(ext, repo->extnames[i]))
161 return get_boolean_val(repo->extvals[i]);
164 return 0;
167 int
168 got_repo_is_bare(struct got_repository *repo)
170 return (strcmp(repo->path, repo->path_git_dir) == 0);
173 static char *
174 get_path_git_child(struct got_repository *repo, const char *basename)
176 char *path_child;
178 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
179 basename) == -1)
180 return NULL;
182 return path_child;
185 char *
186 got_repo_get_path_objects(struct got_repository *repo)
188 return get_path_git_child(repo, GOT_OBJECTS_DIR);
191 char *
192 got_repo_get_path_objects_pack(struct got_repository *repo)
194 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
197 char *
198 got_repo_get_path_refs(struct got_repository *repo)
200 return get_path_git_child(repo, GOT_REFS_DIR);
203 char *
204 got_repo_get_path_packed_refs(struct got_repository *repo)
206 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
209 static char *
210 get_path_head(struct got_repository *repo)
212 return get_path_git_child(repo, GOT_HEAD_FILE);
215 char *
216 got_repo_get_path_gitconfig(struct got_repository *repo)
218 return get_path_git_child(repo, GOT_GITCONFIG);
221 char *
222 got_repo_get_path_gotconfig(struct got_repository *repo)
224 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
227 const struct got_gotconfig *
228 got_repo_get_gotconfig(struct got_repository *repo)
230 return repo->gotconfig;
233 void
234 got_repo_get_gitconfig_remotes(int *nremotes,
235 const struct got_remote_repo **remotes, struct got_repository *repo)
237 *nremotes = repo->ngitconfig_remotes;
238 *remotes = repo->gitconfig_remotes;
241 static int
242 is_git_repo(struct got_repository *repo)
244 const char *path_git = got_repo_get_path_git_dir(repo);
245 char *path_objects = got_repo_get_path_objects(repo);
246 char *path_refs = got_repo_get_path_refs(repo);
247 char *path_head = get_path_head(repo);
248 int ret = 0;
249 struct stat sb;
250 struct got_reference *head_ref;
252 if (lstat(path_git, &sb) == -1)
253 goto done;
254 if (!S_ISDIR(sb.st_mode))
255 goto done;
257 if (lstat(path_objects, &sb) == -1)
258 goto done;
259 if (!S_ISDIR(sb.st_mode))
260 goto done;
262 if (lstat(path_refs, &sb) == -1)
263 goto done;
264 if (!S_ISDIR(sb.st_mode))
265 goto done;
267 if (lstat(path_head, &sb) == -1)
268 goto done;
269 if (!S_ISREG(sb.st_mode))
270 goto done;
272 /* Check if the HEAD reference can be opened. */
273 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
274 goto done;
275 got_ref_close(head_ref);
277 ret = 1;
278 done:
279 free(path_objects);
280 free(path_refs);
281 free(path_head);
282 return ret;
286 static const struct got_error *
287 close_tempfiles(int *fds, size_t nfds)
289 const struct got_error *err = NULL;
290 int i;
292 for (i = 0; i < nfds; i++) {
293 if (fds[i] == -1)
294 continue;
295 if (close(fds[i]) == -1) {
296 err = got_error_from_errno("close");
297 break;
300 free(fds);
301 return err;
304 static const struct got_error *
305 open_tempfiles(int **fds, size_t array_size, size_t nfds)
307 const struct got_error *err = NULL;
308 int i;
310 *fds = calloc(array_size, sizeof(**fds));
311 if (*fds == NULL)
312 return got_error_from_errno("calloc");
314 for (i = 0; i < array_size; i++)
315 (*fds)[i] = -1;
317 for (i = 0; i < nfds; i++) {
318 (*fds)[i] = got_opentempfd();
319 if ((*fds)[i] == -1) {
320 err = got_error_from_errno("got_opentempfd");
321 close_tempfiles(*fds, nfds);
322 *fds = NULL;
323 return err;
327 return NULL;
330 static const struct got_error *
331 get_pack_cache_size(int *pack_cache_size)
333 struct rlimit rl;
335 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
336 return got_error_from_errno("getrlimit");
338 *pack_cache_size = GOT_PACK_CACHE_SIZE;
339 if (*pack_cache_size > rl.rlim_cur / 8)
340 *pack_cache_size = rl.rlim_cur / 8;
342 return NULL;
345 const struct got_error *
346 got_repo_pack_fds_open(int **pack_fds)
348 const struct got_error *err;
349 int nfds;
351 err = get_pack_cache_size(&nfds);
352 if (err)
353 return err;
355 /*
356 * We need one basefd and one accumfd per cached pack.
357 * Our constants should be set up in a way such that
358 * this error never triggers.
359 */
360 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
361 return got_error(GOT_ERR_NO_SPACE);
363 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
366 const struct got_error *
367 got_repo_pack_fds_close(int *pack_fds)
369 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
372 const struct got_error *
373 got_repo_temp_fds_open(int **temp_fds)
375 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
376 GOT_REPO_NUM_TEMPFILES);
379 void
380 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
382 int i;
384 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
385 repo->tempfiles[i] = temp_fds[i];
388 const struct got_error *
389 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
391 int i;
393 *fd = -1;
394 *idx = -1;
396 for (i = 0; i < nitems(repo->tempfiles); i++) {
397 if (repo->tempfile_use_mask & (1 << i))
398 continue;
399 if (repo->tempfiles[i] != -1) {
400 if (ftruncate(repo->tempfiles[i], 0L) == -1)
401 return got_error_from_errno("ftruncate");
402 *fd = repo->tempfiles[i];
403 *idx = i;
404 repo->tempfile_use_mask |= (1 << i);
405 return NULL;
409 return got_error(GOT_ERR_REPO_TEMPFILE);
412 void
413 got_repo_temp_fds_put(int idx, struct got_repository *repo)
415 repo->tempfile_use_mask &= ~(1 << idx);
418 const struct got_error *
419 got_repo_temp_fds_close(int *temp_fds)
421 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
424 const struct got_error *
425 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
426 struct got_object *obj)
428 #ifndef GOT_NO_OBJ_CACHE
429 const struct got_error *err = NULL;
430 err = got_object_cache_add(&repo->objcache, id, obj);
431 if (err) {
432 if (err->code == GOT_ERR_OBJ_EXISTS ||
433 err->code == GOT_ERR_OBJ_TOO_LARGE)
434 err = NULL;
435 return err;
437 obj->refcnt++;
438 #endif
439 return NULL;
442 struct got_object *
443 got_repo_get_cached_object(struct got_repository *repo,
444 struct got_object_id *id)
446 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
449 const struct got_error *
450 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
451 struct got_tree_object *tree)
453 #ifndef GOT_NO_OBJ_CACHE
454 const struct got_error *err = NULL;
455 err = got_object_cache_add(&repo->treecache, id, tree);
456 if (err) {
457 if (err->code == GOT_ERR_OBJ_EXISTS ||
458 err->code == GOT_ERR_OBJ_TOO_LARGE)
459 err = NULL;
460 return err;
462 tree->refcnt++;
463 #endif
464 return NULL;
467 struct got_tree_object *
468 got_repo_get_cached_tree(struct got_repository *repo,
469 struct got_object_id *id)
471 return (struct got_tree_object *)got_object_cache_get(
472 &repo->treecache, id);
475 const struct got_error *
476 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
477 struct got_commit_object *commit)
479 #ifndef GOT_NO_OBJ_CACHE
480 const struct got_error *err = NULL;
481 err = got_object_cache_add(&repo->commitcache, id, commit);
482 if (err) {
483 if (err->code == GOT_ERR_OBJ_EXISTS ||
484 err->code == GOT_ERR_OBJ_TOO_LARGE)
485 err = NULL;
486 return err;
488 commit->refcnt++;
489 #endif
490 return NULL;
493 struct got_commit_object *
494 got_repo_get_cached_commit(struct got_repository *repo,
495 struct got_object_id *id)
497 return (struct got_commit_object *)got_object_cache_get(
498 &repo->commitcache, id);
501 const struct got_error *
502 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
503 struct got_tag_object *tag)
505 #ifndef GOT_NO_OBJ_CACHE
506 const struct got_error *err = NULL;
507 err = got_object_cache_add(&repo->tagcache, id, tag);
508 if (err) {
509 if (err->code == GOT_ERR_OBJ_EXISTS ||
510 err->code == GOT_ERR_OBJ_TOO_LARGE)
511 err = NULL;
512 return err;
514 tag->refcnt++;
515 #endif
516 return NULL;
519 struct got_tag_object *
520 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
522 return (struct got_tag_object *)got_object_cache_get(
523 &repo->tagcache, id);
526 const struct got_error *
527 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
528 struct got_raw_object *raw)
530 #ifndef GOT_NO_OBJ_CACHE
531 const struct got_error *err = NULL;
532 err = got_object_cache_add(&repo->rawcache, id, raw);
533 if (err) {
534 if (err->code == GOT_ERR_OBJ_EXISTS ||
535 err->code == GOT_ERR_OBJ_TOO_LARGE)
536 err = NULL;
537 return err;
539 raw->refcnt++;
540 #endif
541 return NULL;
545 struct got_raw_object *
546 got_repo_get_cached_raw_object(struct got_repository *repo,
547 struct got_object_id *id)
549 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
553 static const struct got_error *
554 open_repo(struct got_repository *repo, const char *path)
556 const struct got_error *err = NULL;
558 repo->gitdir_fd = -1;
560 /* bare git repository? */
561 repo->path_git_dir = strdup(path);
562 if (repo->path_git_dir == NULL)
563 return got_error_from_errno("strdup");
564 if (is_git_repo(repo)) {
565 repo->path = strdup(repo->path_git_dir);
566 if (repo->path == NULL) {
567 err = got_error_from_errno("strdup");
568 goto done;
570 repo->gitdir_fd = open(repo->path_git_dir,
571 O_DIRECTORY | O_CLOEXEC);
572 if (repo->gitdir_fd == -1) {
573 err = got_error_from_errno2("open",
574 repo->path_git_dir);
575 goto done;
577 return NULL;
580 /* git repository with working tree? */
581 free(repo->path_git_dir);
582 repo->path_git_dir = NULL;
583 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
584 err = got_error_from_errno("asprintf");
585 goto done;
587 if (is_git_repo(repo)) {
588 repo->path = strdup(path);
589 if (repo->path == NULL) {
590 err = got_error_from_errno("strdup");
591 goto done;
593 repo->gitdir_fd = open(repo->path_git_dir,
594 O_DIRECTORY | O_CLOEXEC);
595 if (repo->gitdir_fd == -1) {
596 err = got_error_from_errno2("open",
597 repo->path_git_dir);
598 goto done;
600 return NULL;
603 err = got_error(GOT_ERR_NOT_GIT_REPO);
604 done:
605 if (err) {
606 free(repo->path);
607 repo->path = NULL;
608 free(repo->path_git_dir);
609 repo->path_git_dir = NULL;
610 if (repo->gitdir_fd != -1)
611 close(repo->gitdir_fd);
612 repo->gitdir_fd = -1;
615 return err;
618 static const struct got_error *
619 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
621 const struct got_error *err = NULL;
622 char *repo_gitconfig_path = NULL;
624 if (global_gitconfig_path) {
625 /* Read settings from ~/.gitconfig. */
626 int dummy_repo_version;
627 err = got_repo_read_gitconfig(&dummy_repo_version,
628 &repo->global_gitconfig_author_name,
629 &repo->global_gitconfig_author_email,
630 NULL, NULL, NULL, NULL, NULL, NULL,
631 global_gitconfig_path);
632 if (err)
633 return err;
636 /* Read repository's .git/config file. */
637 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
638 if (repo_gitconfig_path == NULL)
639 return got_error_from_errno("got_repo_get_path_gitconfig");
641 err = got_repo_read_gitconfig(
642 &repo->gitconfig_repository_format_version,
643 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
644 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
645 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
646 &repo->nextensions, repo_gitconfig_path);
647 if (err)
648 goto done;
650 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
651 int i;
653 for (i = 0; i < repo->ngitconfig_remotes; i++) {
654 got_repo_free_remote_repo_data(
655 &repo->gitconfig_remotes[i]);
657 free(repo->gitconfig_remotes);
658 repo->gitconfig_remotes = NULL;
659 repo->ngitconfig_remotes = 0;
661 free(repo->gitconfig_author_name);
662 repo->gitconfig_author_name = NULL;
663 free(repo->gitconfig_author_email);
664 repo->gitconfig_author_email = NULL;
666 free(repo->global_gitconfig_author_name);
667 repo->global_gitconfig_author_name = NULL;
668 free(repo->global_gitconfig_author_email);
669 repo->global_gitconfig_author_email = NULL;
672 done:
673 free(repo_gitconfig_path);
674 return err;
677 static const struct got_error *
678 read_gotconfig(struct got_repository *repo)
680 const struct got_error *err = NULL;
681 char *gotconfig_path;
683 gotconfig_path = got_repo_get_path_gotconfig(repo);
684 if (gotconfig_path == NULL)
685 return got_error_from_errno("got_repo_get_path_gotconfig");
687 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
688 free(gotconfig_path);
689 return err;
692 /* Supported repository format extensions. */
693 static const char *const repo_extensions[] = {
694 "noop", /* Got supports repository format version 1. */
695 "preciousObjects", /* Supported by gotadmin cleanup. */
696 "worktreeConfig", /* Got does not care about Git work trees. */
697 };
699 const struct got_error *
700 got_repo_open(struct got_repository **repop, const char *path,
701 const char *global_gitconfig_path, int *pack_fds)
703 struct got_repository *repo = NULL;
704 const struct got_error *err = NULL;
705 char *repo_path = NULL;
706 size_t i, j = 0;
708 *repop = NULL;
710 repo = calloc(1, sizeof(*repo));
711 if (repo == NULL)
712 return got_error_from_errno("calloc");
714 RB_INIT(&repo->packidx_bloom_filters);
715 TAILQ_INIT(&repo->packidx_paths);
717 for (i = 0; i < nitems(repo->privsep_children); i++) {
718 memset(&repo->privsep_children[i], 0,
719 sizeof(repo->privsep_children[0]));
720 repo->privsep_children[i].imsg_fd = -1;
723 err = got_object_cache_init(&repo->objcache,
724 GOT_OBJECT_CACHE_TYPE_OBJ);
725 if (err)
726 goto done;
727 err = got_object_cache_init(&repo->treecache,
728 GOT_OBJECT_CACHE_TYPE_TREE);
729 if (err)
730 goto done;
731 err = got_object_cache_init(&repo->commitcache,
732 GOT_OBJECT_CACHE_TYPE_COMMIT);
733 if (err)
734 goto done;
735 err = got_object_cache_init(&repo->tagcache,
736 GOT_OBJECT_CACHE_TYPE_TAG);
737 if (err)
738 goto done;
739 err = got_object_cache_init(&repo->rawcache,
740 GOT_OBJECT_CACHE_TYPE_RAW);
741 if (err)
742 goto done;
744 err = get_pack_cache_size(&repo->pack_cache_size);
745 if (err)
746 goto done;
747 for (i = 0; i < nitems(repo->packs); i++) {
748 if (pack_fds != NULL && i < repo->pack_cache_size) {
749 repo->packs[i].basefd = pack_fds[j++];
750 repo->packs[i].accumfd = pack_fds[j++];
751 } else {
752 repo->packs[i].basefd = -1;
753 repo->packs[i].accumfd = -1;
756 for (i = 0; i < nitems(repo->tempfiles); i++)
757 repo->tempfiles[i] = -1;
758 repo->pinned_pack = -1;
759 repo->pinned_packidx = -1;
760 repo->pinned_pid = 0;
762 repo_path = realpath(path, NULL);
763 if (repo_path == NULL) {
764 err = got_error_from_errno2("realpath", path);
765 goto done;
768 for (;;) {
769 char *parent_path;
771 err = open_repo(repo, repo_path);
772 if (err == NULL)
773 break;
774 if (err->code != GOT_ERR_NOT_GIT_REPO)
775 goto done;
776 if (repo_path[0] == '/' && repo_path[1] == '\0') {
777 err = got_error(GOT_ERR_NOT_GIT_REPO);
778 goto done;
780 err = got_path_dirname(&parent_path, repo_path);
781 if (err)
782 goto done;
783 free(repo_path);
784 repo_path = parent_path;
787 err = read_gotconfig(repo);
788 if (err)
789 goto done;
791 err = read_gitconfig(repo, global_gitconfig_path);
792 if (err)
793 goto done;
794 if (repo->gitconfig_repository_format_version != 0) {
795 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
796 goto done;
798 for (i = 0; i < repo->nextensions; i++) {
799 char *ext = repo->extnames[i];
800 char *val = repo->extvals[i];
801 int j, supported = 0;
803 if (!is_boolean_val(val)) {
804 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
805 goto done;
808 if (!get_boolean_val(val))
809 continue;
811 for (j = 0; j < nitems(repo_extensions); j++) {
812 if (strcmp(ext, repo_extensions[j]) == 0) {
813 supported = 1;
814 break;
817 if (!supported) {
818 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
819 goto done;
823 err = got_repo_list_packidx(&repo->packidx_paths, repo);
824 done:
825 if (err)
826 got_repo_close(repo);
827 else
828 *repop = repo;
829 free(repo_path);
830 return err;
833 const struct got_error *
834 got_repo_close(struct got_repository *repo)
836 const struct got_error *err = NULL, *child_err;
837 struct got_packidx_bloom_filter *bf;
838 size_t i;
840 for (i = 0; i < repo->pack_cache_size; i++) {
841 if (repo->packidx_cache[i] == NULL)
842 break;
843 got_packidx_close(repo->packidx_cache[i]);
846 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
847 &repo->packidx_bloom_filters))) {
848 RB_REMOVE(got_packidx_bloom_filter_tree,
849 &repo->packidx_bloom_filters, bf);
850 bloom_free(bf->bloom);
851 free(bf->bloom);
852 free(bf);
855 for (i = 0; i < repo->pack_cache_size; i++)
856 if (repo->packs[i].path_packfile)
857 if (repo->packs[i].path_packfile)
858 got_pack_close(&repo->packs[i]);
860 free(repo->path);
861 free(repo->path_git_dir);
863 got_object_cache_close(&repo->objcache);
864 got_object_cache_close(&repo->treecache);
865 got_object_cache_close(&repo->commitcache);
866 got_object_cache_close(&repo->tagcache);
867 got_object_cache_close(&repo->rawcache);
869 for (i = 0; i < nitems(repo->privsep_children); i++) {
870 if (repo->privsep_children[i].imsg_fd == -1)
871 continue;
872 imsg_clear(repo->privsep_children[i].ibuf);
873 free(repo->privsep_children[i].ibuf);
874 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
875 child_err = got_privsep_wait_for_child(
876 repo->privsep_children[i].pid);
877 if (child_err && err == NULL)
878 err = child_err;
879 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
880 err == NULL)
881 err = got_error_from_errno("close");
884 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
885 err == NULL)
886 err = got_error_from_errno("close");
888 if (repo->gotconfig)
889 got_gotconfig_free(repo->gotconfig);
890 free(repo->gitconfig_author_name);
891 free(repo->gitconfig_author_email);
892 for (i = 0; i < repo->ngitconfig_remotes; i++)
893 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
894 free(repo->gitconfig_remotes);
895 for (i = 0; i < repo->nextensions; i++) {
896 free(repo->extnames[i]);
897 free(repo->extvals[i]);
899 free(repo->extnames);
900 free(repo->extvals);
902 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
903 free(repo);
905 return err;
908 void
909 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
911 int i;
913 free(repo->name);
914 repo->name = NULL;
915 free(repo->fetch_url);
916 repo->fetch_url = NULL;
917 free(repo->send_url);
918 repo->send_url = NULL;
919 for (i = 0; i < repo->nfetch_branches; i++)
920 free(repo->fetch_branches[i]);
921 free(repo->fetch_branches);
922 repo->fetch_branches = NULL;
923 repo->nfetch_branches = 0;
924 for (i = 0; i < repo->nsend_branches; i++)
925 free(repo->send_branches[i]);
926 free(repo->send_branches);
927 repo->send_branches = NULL;
928 repo->nsend_branches = 0;
931 const struct got_error *
932 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
933 const char *input_path)
935 const struct got_error *err = NULL;
936 const char *repo_abspath = NULL;
937 size_t repolen, len;
938 char *canonpath, *path = NULL;
940 *in_repo_path = NULL;
942 canonpath = strdup(input_path);
943 if (canonpath == NULL) {
944 err = got_error_from_errno("strdup");
945 goto done;
947 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
948 if (err)
949 goto done;
951 repo_abspath = got_repo_get_path(repo);
953 if (canonpath[0] == '\0') {
954 path = strdup(canonpath);
955 if (path == NULL) {
956 err = got_error_from_errno("strdup");
957 goto done;
959 } else {
960 path = realpath(canonpath, NULL);
961 if (path == NULL) {
962 if (errno != ENOENT) {
963 err = got_error_from_errno2("realpath",
964 canonpath);
965 goto done;
967 /*
968 * Path is not on disk.
969 * Assume it is already relative to repository root.
970 */
971 path = strdup(canonpath);
972 if (path == NULL) {
973 err = got_error_from_errno("strdup");
974 goto done;
978 repolen = strlen(repo_abspath);
979 len = strlen(path);
982 if (strcmp(path, repo_abspath) == 0) {
983 free(path);
984 path = strdup("");
985 if (path == NULL) {
986 err = got_error_from_errno("strdup");
987 goto done;
989 } else if (len > repolen &&
990 got_path_is_child(path, repo_abspath, repolen)) {
991 /* Matched an on-disk path inside repository. */
992 if (got_repo_is_bare(repo)) {
993 /*
994 * Matched an on-disk path inside repository
995 * database. Treat input as repository-relative.
996 */
997 free(path);
998 path = canonpath;
999 canonpath = NULL;
1000 } else {
1001 char *child;
1002 /* Strip common prefix with repository path. */
1003 err = got_path_skip_common_ancestor(&child,
1004 repo_abspath, path);
1005 if (err)
1006 goto done;
1007 free(path);
1008 path = child;
1010 } else {
1012 * Matched unrelated on-disk path.
1013 * Treat input as repository-relative.
1015 free(path);
1016 path = canonpath;
1017 canonpath = NULL;
1021 /* Make in-repository path absolute */
1022 if (path[0] != '/') {
1023 char *abspath;
1024 if (asprintf(&abspath, "/%s", path) == -1) {
1025 err = got_error_from_errno("asprintf");
1026 goto done;
1028 free(path);
1029 path = abspath;
1032 done:
1033 free(canonpath);
1034 if (err)
1035 free(path);
1036 else
1037 *in_repo_path = path;
1038 return err;
1041 static const struct got_error *
1042 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1043 const char *path_packidx)
1045 const struct got_error *err = NULL;
1046 size_t i;
1048 for (i = 0; i < repo->pack_cache_size; i++) {
1049 if (repo->packidx_cache[i] == NULL)
1050 break;
1051 if (strcmp(repo->packidx_cache[i]->path_packidx,
1052 path_packidx) == 0) {
1053 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1056 if (i == repo->pack_cache_size) {
1057 do {
1058 i--;
1059 } while (i > 0 && repo->pinned_packidx >= 0 &&
1060 i == repo->pinned_packidx);
1061 err = got_packidx_close(repo->packidx_cache[i]);
1062 if (err)
1063 return err;
1066 repo->packidx_cache[i] = packidx;
1068 return NULL;
1071 int
1072 got_repo_is_packidx_filename(const char *name, size_t len)
1074 if (len != GOT_PACKIDX_NAMELEN)
1075 return 0;
1077 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1078 return 0;
1080 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1081 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1082 return 0;
1084 return 1;
1087 static struct got_packidx_bloom_filter *
1088 get_packidx_bloom_filter(struct got_repository *repo,
1089 const char *path, size_t path_len)
1091 struct got_packidx_bloom_filter key;
1093 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1094 return NULL; /* XXX */
1095 key.path_len = path_len;
1097 return RB_FIND(got_packidx_bloom_filter_tree,
1098 &repo->packidx_bloom_filters, &key);
1101 int
1102 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1103 const char *path_packidx, struct got_object_id *id)
1105 struct got_packidx_bloom_filter *bf;
1107 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1108 if (bf)
1109 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1111 /* No bloom filter means this pack index must be searched. */
1112 return 1;
1115 static const struct got_error *
1116 add_packidx_bloom_filter(struct got_repository *repo,
1117 struct got_packidx *packidx, const char *path_packidx)
1119 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1120 struct got_packidx_bloom_filter *bf;
1121 size_t len;
1124 * Don't use bloom filters for very large pack index files.
1125 * Large pack files will contain a relatively large fraction
1126 * of our objects so we will likely need to visit them anyway.
1127 * The more objects a pack file contains the higher the probability
1128 * of a false-positive match from the bloom filter. And reading
1129 * all object IDs from a large pack index file can be expensive.
1131 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1132 return NULL;
1134 /* Do we already have a filter for this pack index? */
1135 if (get_packidx_bloom_filter(repo, path_packidx,
1136 strlen(path_packidx)) != NULL)
1137 return NULL;
1139 bf = calloc(1, sizeof(*bf));
1140 if (bf == NULL)
1141 return got_error_from_errno("calloc");
1142 bf->bloom = calloc(1, sizeof(*bf->bloom));
1143 if (bf->bloom == NULL) {
1144 free(bf);
1145 return got_error_from_errno("calloc");
1148 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1149 if (len >= sizeof(bf->path)) {
1150 free(bf->bloom);
1151 free(bf);
1152 return got_error(GOT_ERR_NO_SPACE);
1154 bf->path_len = len;
1156 /* Minimum size supported by our bloom filter is 1000 entries. */
1157 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1158 for (i = 0; i < nobjects; i++) {
1159 struct got_packidx_object_id *id;
1160 id = &packidx->hdr.sorted_ids[i];
1161 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1164 RB_INSERT(got_packidx_bloom_filter_tree,
1165 &repo->packidx_bloom_filters, bf);
1166 return NULL;
1169 static void
1170 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1172 struct got_pathlist_entry *pe;
1174 while (!TAILQ_EMPTY(packidx_paths)) {
1175 pe = TAILQ_FIRST(packidx_paths);
1176 TAILQ_REMOVE(packidx_paths, pe, entry);
1177 free((char *)pe->path);
1178 free(pe);
1182 static const struct got_error *
1183 refresh_packidx_paths(struct got_repository *repo)
1185 const struct got_error *err = NULL;
1186 char *objects_pack_dir = NULL;
1187 struct stat sb;
1189 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1190 if (objects_pack_dir == NULL)
1191 return got_error_from_errno("got_repo_get_path_objects_pack");
1193 if (stat(objects_pack_dir, &sb) == -1) {
1194 if (errno != ENOENT) {
1195 err = got_error_from_errno2("stat", objects_pack_dir);
1196 goto done;
1198 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1199 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1200 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1201 purge_packidx_paths(&repo->packidx_paths);
1202 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1203 if (err)
1204 goto done;
1206 done:
1207 free(objects_pack_dir);
1208 return err;
1211 const struct got_error *
1212 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1213 struct got_repository *repo, struct got_object_id *id)
1215 const struct got_error *err;
1216 struct got_pathlist_entry *pe;
1217 size_t i;
1219 /* Search pack index cache. */
1220 for (i = 0; i < repo->pack_cache_size; i++) {
1221 if (repo->packidx_cache[i] == NULL)
1222 break;
1223 if (!got_repo_check_packidx_bloom_filter(repo,
1224 repo->packidx_cache[i]->path_packidx, id))
1225 continue; /* object will not be found in this index */
1226 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1227 if (*idx != -1) {
1228 *packidx = repo->packidx_cache[i];
1230 * Move this cache entry to the front. Repeatedly
1231 * searching a wrong pack index can be expensive.
1233 if (i > 0) {
1234 memmove(&repo->packidx_cache[1],
1235 &repo->packidx_cache[0],
1236 i * sizeof(repo->packidx_cache[0]));
1237 repo->packidx_cache[0] = *packidx;
1238 if (repo->pinned_packidx >= 0 &&
1239 repo->pinned_packidx < i)
1240 repo->pinned_packidx++;
1241 else if (repo->pinned_packidx == i)
1242 repo->pinned_packidx = 0;
1244 return NULL;
1247 /* No luck. Search the filesystem. */
1249 err = refresh_packidx_paths(repo);
1250 if (err)
1251 return err;
1253 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1254 const char *path_packidx = pe->path;
1255 int is_cached = 0;
1257 if (!got_repo_check_packidx_bloom_filter(repo,
1258 pe->path, id))
1259 continue; /* object will not be found in this index */
1261 for (i = 0; i < repo->pack_cache_size; i++) {
1262 if (repo->packidx_cache[i] == NULL)
1263 break;
1264 if (strcmp(repo->packidx_cache[i]->path_packidx,
1265 path_packidx) == 0) {
1266 is_cached = 1;
1267 break;
1270 if (is_cached)
1271 continue; /* already searched */
1273 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1274 path_packidx, 0);
1275 if (err)
1276 goto done;
1278 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1279 if (err)
1280 goto done;
1282 err = cache_packidx(repo, *packidx, path_packidx);
1283 if (err)
1284 goto done;
1286 *idx = got_packidx_get_object_idx(*packidx, id);
1287 if (*idx != -1) {
1288 err = NULL; /* found the object */
1289 goto done;
1293 err = got_error_no_obj(id);
1294 done:
1295 return err;
1298 const struct got_error *
1299 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1300 struct got_repository *repo)
1302 const struct got_error *err = NULL;
1303 DIR *packdir = NULL;
1304 struct dirent *dent;
1305 char *path_packidx = NULL;
1306 int packdir_fd;
1307 struct stat sb;
1309 packdir_fd = openat(got_repo_get_fd(repo),
1310 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1311 if (packdir_fd == -1) {
1312 return got_error_from_errno_fmt("openat: %s/%s",
1313 got_repo_get_path_git_dir(repo),
1314 GOT_OBJECTS_PACK_DIR);
1317 packdir = fdopendir(packdir_fd);
1318 if (packdir == NULL) {
1319 err = got_error_from_errno("fdopendir");
1320 goto done;
1323 if (fstat(packdir_fd, &sb) == -1) {
1324 err = got_error_from_errno("fstat");
1325 goto done;
1327 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1328 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1330 while ((dent = readdir(packdir)) != NULL) {
1331 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1332 continue;
1334 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1335 dent->d_name) == -1) {
1336 err = got_error_from_errno("asprintf");
1337 path_packidx = NULL;
1338 break;
1341 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1342 if (err)
1343 break;
1345 done:
1346 if (err)
1347 free(path_packidx);
1348 if (packdir && closedir(packdir) != 0 && err == NULL)
1349 err = got_error_from_errno("closedir");
1350 return err;
1353 const struct got_error *
1354 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1355 struct got_repository *repo)
1357 const struct got_error *err;
1358 size_t i;
1360 *packidx = NULL;
1362 /* Search pack index cache. */
1363 for (i = 0; i < repo->pack_cache_size; i++) {
1364 if (repo->packidx_cache[i] == NULL)
1365 break;
1366 if (strcmp(repo->packidx_cache[i]->path_packidx,
1367 path_packidx) == 0) {
1368 *packidx = repo->packidx_cache[i];
1369 return NULL;
1372 /* No luck. Search the filesystem. */
1374 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1375 path_packidx, 0);
1376 if (err)
1377 return err;
1379 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1380 if (err)
1381 goto done;
1383 err = cache_packidx(repo, *packidx, path_packidx);
1384 done:
1385 if (err) {
1386 got_packidx_close(*packidx);
1387 *packidx = NULL;
1389 return err;
1392 static const struct got_error *
1393 read_packfile_hdr(int fd, struct got_packidx *packidx)
1395 const struct got_error *err = NULL;
1396 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1397 struct got_packfile_hdr hdr;
1398 ssize_t n;
1400 n = read(fd, &hdr, sizeof(hdr));
1401 if (n < 0)
1402 return got_error_from_errno("read");
1403 if (n != sizeof(hdr))
1404 return got_error(GOT_ERR_BAD_PACKFILE);
1406 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1407 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1408 be32toh(hdr.nobjects) != totobj)
1409 err = got_error(GOT_ERR_BAD_PACKFILE);
1411 return err;
1414 static const struct got_error *
1415 open_packfile(int *fd, struct got_repository *repo,
1416 const char *relpath, struct got_packidx *packidx)
1418 const struct got_error *err = NULL;
1420 *fd = openat(got_repo_get_fd(repo), relpath,
1421 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1422 if (*fd == -1)
1423 return got_error_from_errno_fmt("openat: %s/%s",
1424 got_repo_get_path_git_dir(repo), relpath);
1426 if (packidx) {
1427 err = read_packfile_hdr(*fd, packidx);
1428 if (err) {
1429 close(*fd);
1430 *fd = -1;
1434 return err;
1437 const struct got_error *
1438 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1439 const char *path_packfile, struct got_packidx *packidx)
1441 const struct got_error *err = NULL;
1442 struct got_pack *pack = NULL;
1443 struct stat sb;
1444 size_t i;
1446 if (packp)
1447 *packp = NULL;
1449 for (i = 0; i < repo->pack_cache_size; i++) {
1450 pack = &repo->packs[i];
1451 if (pack->path_packfile == NULL)
1452 break;
1453 if (strcmp(pack->path_packfile, path_packfile) == 0)
1454 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1457 if (i == repo->pack_cache_size) {
1458 struct got_pack tmp;
1459 do {
1460 i--;
1461 } while (i > 0 && repo->pinned_pack >= 0 &&
1462 i == repo->pinned_pack);
1463 err = got_pack_close(&repo->packs[i]);
1464 if (err)
1465 return err;
1466 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1467 return got_error_from_errno("ftruncate");
1468 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1469 return got_error_from_errno("ftruncate");
1470 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1471 memcpy(&repo->packs[i], &repo->packs[0],
1472 sizeof(repo->packs[i]));
1473 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1474 if (repo->pinned_pack == 0)
1475 repo->pinned_pack = i;
1476 else if (repo->pinned_pack == i)
1477 repo->pinned_pack = 0;
1478 i = 0;
1481 pack = &repo->packs[i];
1483 pack->path_packfile = strdup(path_packfile);
1484 if (pack->path_packfile == NULL) {
1485 err = got_error_from_errno("strdup");
1486 goto done;
1489 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1490 if (err)
1491 goto done;
1493 if (fstat(pack->fd, &sb) != 0) {
1494 err = got_error_from_errno("fstat");
1495 goto done;
1497 pack->filesize = sb.st_size;
1499 pack->privsep_child = NULL;
1501 err = got_delta_cache_alloc(&pack->delta_cache);
1502 if (err)
1503 goto done;
1505 #ifndef GOT_PACK_NO_MMAP
1506 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1507 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1508 pack->fd, 0);
1509 if (pack->map == MAP_FAILED) {
1510 if (errno != ENOMEM) {
1511 err = got_error_from_errno("mmap");
1512 goto done;
1514 pack->map = NULL; /* fall back to read(2) */
1517 #endif
1518 done:
1519 if (err) {
1520 if (pack)
1521 got_pack_close(pack);
1522 } else if (packp)
1523 *packp = pack;
1524 return err;
1527 struct got_pack *
1528 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1530 struct got_pack *pack = NULL;
1531 size_t i;
1533 for (i = 0; i < repo->pack_cache_size; i++) {
1534 pack = &repo->packs[i];
1535 if (pack->path_packfile == NULL)
1536 break;
1537 if (strcmp(pack->path_packfile, path_packfile) == 0)
1538 return pack;
1541 return NULL;
1544 const struct got_error *
1545 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1546 struct got_pack *pack)
1548 size_t i;
1549 int pinned_pack = -1, pinned_packidx = -1;
1551 for (i = 0; i < repo->pack_cache_size; i++) {
1552 if (repo->packidx_cache[i] &&
1553 strcmp(repo->packidx_cache[i]->path_packidx,
1554 packidx->path_packidx) == 0)
1555 pinned_packidx = i;
1556 if (repo->packs[i].path_packfile &&
1557 strcmp(repo->packs[i].path_packfile,
1558 pack->path_packfile) == 0)
1559 pinned_pack = i;
1562 if (pinned_packidx == -1 || pinned_pack == -1)
1563 return got_error(GOT_ERR_PIN_PACK);
1565 repo->pinned_pack = pinned_pack;
1566 repo->pinned_packidx = pinned_packidx;
1567 if (repo->packs[pinned_pack].privsep_child)
1568 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1569 return NULL;
1572 struct got_pack *
1573 got_repo_get_pinned_pack(struct got_repository *repo)
1575 if (repo->pinned_pack >= 0 &&
1576 repo->pinned_pack < repo->pack_cache_size)
1577 return &repo->packs[repo->pinned_pack];
1579 return NULL;
1582 void
1583 got_repo_unpin_pack(struct got_repository *repo)
1585 repo->pinned_packidx = -1;
1586 repo->pinned_pack = -1;
1587 repo->pinned_pid = 0;
1590 const struct got_error *
1591 got_repo_init(const char *repo_path, const char *head_name)
1593 const struct got_error *err = NULL;
1594 const char *dirnames[] = {
1595 GOT_OBJECTS_DIR,
1596 GOT_OBJECTS_PACK_DIR,
1597 GOT_REFS_DIR,
1599 const char *description_str = "Unnamed repository; "
1600 "edit this file 'description' to name the repository.";
1601 const char *headref = "ref: refs/heads/";
1602 const char *gitconfig_str = "[core]\n"
1603 "\trepositoryformatversion = 0\n"
1604 "\tfilemode = true\n"
1605 "\tbare = true\n";
1606 char *headref_str, *path;
1607 size_t i;
1609 if (!got_path_dir_is_empty(repo_path))
1610 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1612 for (i = 0; i < nitems(dirnames); i++) {
1613 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1614 return got_error_from_errno("asprintf");
1616 err = got_path_mkdir(path);
1617 free(path);
1618 if (err)
1619 return err;
1622 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1623 return got_error_from_errno("asprintf");
1624 err = got_path_create_file(path, description_str);
1625 free(path);
1626 if (err)
1627 return err;
1629 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1630 return got_error_from_errno("asprintf");
1631 if (asprintf(&headref_str, "%s%s", headref,
1632 head_name ? head_name : "main") == -1) {
1633 free(path);
1634 return got_error_from_errno("asprintf");
1636 err = got_path_create_file(path, headref_str);
1637 free(headref_str);
1638 free(path);
1639 if (err)
1640 return err;
1642 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1643 return got_error_from_errno("asprintf");
1644 err = got_path_create_file(path, gitconfig_str);
1645 free(path);
1646 if (err)
1647 return err;
1649 return NULL;
1652 static const struct got_error *
1653 match_packed_object(struct got_object_id **unique_id,
1654 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1656 const struct got_error *err = NULL;
1657 struct got_object_id_queue matched_ids;
1658 struct got_pathlist_entry *pe;
1660 STAILQ_INIT(&matched_ids);
1662 err = refresh_packidx_paths(repo);
1663 if (err)
1664 return err;
1666 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1667 const char *path_packidx = pe->path;
1668 struct got_packidx *packidx;
1669 struct got_object_qid *qid;
1671 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1672 path_packidx, 0);
1673 if (err)
1674 break;
1676 err = got_packidx_match_id_str_prefix(&matched_ids,
1677 packidx, id_str_prefix);
1678 if (err) {
1679 got_packidx_close(packidx);
1680 break;
1682 err = got_packidx_close(packidx);
1683 if (err)
1684 break;
1686 STAILQ_FOREACH(qid, &matched_ids, entry) {
1687 if (obj_type != GOT_OBJ_TYPE_ANY) {
1688 int matched_type;
1689 err = got_object_get_type(&matched_type, repo,
1690 &qid->id);
1691 if (err)
1692 goto done;
1693 if (matched_type != obj_type)
1694 continue;
1696 if (*unique_id == NULL) {
1697 *unique_id = got_object_id_dup(&qid->id);
1698 if (*unique_id == NULL) {
1699 err = got_error_from_errno("malloc");
1700 goto done;
1702 } else {
1703 if (got_object_id_cmp(*unique_id,
1704 &qid->id) == 0)
1705 continue; /* packed multiple times */
1706 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1707 goto done;
1711 done:
1712 got_object_id_queue_free(&matched_ids);
1713 if (err) {
1714 free(*unique_id);
1715 *unique_id = NULL;
1717 return err;
1720 static const struct got_error *
1721 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1722 const char *object_dir, const char *id_str_prefix, int obj_type,
1723 struct got_repository *repo)
1725 const struct got_error *err = NULL;
1726 char *path, *id_str = NULL;
1727 DIR *dir = NULL;
1728 struct dirent *dent;
1729 struct got_object_id id;
1731 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1732 err = got_error_from_errno("asprintf");
1733 goto done;
1736 dir = opendir(path);
1737 if (dir == NULL) {
1738 if (errno == ENOENT) {
1739 err = NULL;
1740 goto done;
1742 err = got_error_from_errno2("opendir", path);
1743 goto done;
1745 while ((dent = readdir(dir)) != NULL) {
1746 int cmp;
1748 free(id_str);
1749 id_str = NULL;
1751 if (strcmp(dent->d_name, ".") == 0 ||
1752 strcmp(dent->d_name, "..") == 0)
1753 continue;
1755 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1756 err = got_error_from_errno("asprintf");
1757 goto done;
1760 if (!got_parse_object_id(&id, id_str, repo->algo))
1761 continue;
1764 * Directory entries do not necessarily appear in
1765 * sorted order, so we must iterate over all of them.
1767 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1768 if (cmp != 0)
1769 continue;
1771 if (*unique_id == NULL) {
1772 if (obj_type != GOT_OBJ_TYPE_ANY) {
1773 int matched_type;
1774 err = got_object_get_type(&matched_type, repo,
1775 &id);
1776 if (err)
1777 goto done;
1778 if (matched_type != obj_type)
1779 continue;
1781 *unique_id = got_object_id_dup(&id);
1782 if (*unique_id == NULL) {
1783 err = got_error_from_errno("got_object_id_dup");
1784 goto done;
1786 } else {
1787 if (got_object_id_cmp(*unique_id, &id) == 0)
1788 continue; /* both packed and loose */
1789 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1790 goto done;
1793 done:
1794 if (dir && closedir(dir) != 0 && err == NULL)
1795 err = got_error_from_errno("closedir");
1796 if (err) {
1797 free(*unique_id);
1798 *unique_id = NULL;
1800 free(id_str);
1801 free(path);
1802 return err;
1805 const struct got_error *
1806 got_repo_match_object_id_prefix(struct got_object_id **id,
1807 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1809 const struct got_error *err = NULL;
1810 char *path_objects = NULL, *object_dir = NULL;
1811 size_t len;
1812 int i;
1814 *id = NULL;
1816 path_objects = got_repo_get_path_objects(repo);
1818 len = strlen(id_str_prefix);
1819 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1820 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1821 goto done;
1824 for (i = 0; i < len; i++) {
1825 if (isxdigit((unsigned char)id_str_prefix[i]))
1826 continue;
1827 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1828 goto done;
1831 if (len >= 2) {
1832 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1833 if (err)
1834 goto done;
1835 object_dir = strndup(id_str_prefix, 2);
1836 if (object_dir == NULL) {
1837 err = got_error_from_errno("strdup");
1838 goto done;
1840 err = match_loose_object(id, path_objects, object_dir,
1841 id_str_prefix, obj_type, repo);
1842 } else if (len == 1) {
1843 int i;
1844 for (i = 0; i < 0xf; i++) {
1845 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1846 == -1) {
1847 err = got_error_from_errno("asprintf");
1848 goto done;
1850 err = match_packed_object(id, repo, object_dir,
1851 obj_type);
1852 if (err)
1853 goto done;
1854 err = match_loose_object(id, path_objects, object_dir,
1855 id_str_prefix, obj_type, repo);
1856 if (err)
1857 goto done;
1859 } else {
1860 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1861 goto done;
1863 done:
1864 free(path_objects);
1865 free(object_dir);
1866 if (err) {
1867 free(*id);
1868 *id = NULL;
1869 } else if (*id == NULL) {
1870 switch (obj_type) {
1871 case GOT_OBJ_TYPE_BLOB:
1872 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1873 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1874 break;
1875 case GOT_OBJ_TYPE_TREE:
1876 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1877 GOT_OBJ_LABEL_TREE, id_str_prefix);
1878 break;
1879 case GOT_OBJ_TYPE_COMMIT:
1880 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1881 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1882 break;
1883 case GOT_OBJ_TYPE_TAG:
1884 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1885 GOT_OBJ_LABEL_TAG, id_str_prefix);
1886 break;
1887 default:
1888 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1889 break;
1893 return err;
1896 const struct got_error *
1897 got_repo_match_object_id(struct got_object_id **id, char **label,
1898 const char *id_str, int obj_type, struct got_reflist_head *refs,
1899 struct got_repository *repo)
1901 const struct got_error *err;
1902 struct got_tag_object *tag;
1903 struct got_reference *ref = NULL;
1905 *id = NULL;
1906 if (label)
1907 *label = NULL;
1909 if (refs) {
1910 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1911 refs, repo);
1912 if (err == NULL) {
1913 *id = got_object_id_dup(
1914 got_object_tag_get_object_id(tag));
1915 if (*id == NULL)
1916 err = got_error_from_errno("got_object_id_dup");
1917 else if (label && asprintf(label, "refs/tags/%s",
1918 got_object_tag_get_name(tag)) == -1) {
1919 err = got_error_from_errno("asprintf");
1920 free(*id);
1921 *id = NULL;
1923 got_object_tag_close(tag);
1924 return err;
1925 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1926 err->code != GOT_ERR_NO_OBJ)
1927 return err;
1930 err = got_ref_open(&ref, repo, id_str, 0);
1931 if (err == NULL) {
1932 err = got_ref_resolve(id, repo, ref);
1933 if (err)
1934 goto done;
1935 if (label) {
1936 *label = strdup(got_ref_get_name(ref));
1937 if (*label == NULL) {
1938 err = got_error_from_errno("strdup");
1939 goto done;
1942 } else {
1943 if (err->code != GOT_ERR_NOT_REF &&
1944 err->code != GOT_ERR_BAD_REF_NAME)
1945 goto done;
1946 err = got_repo_match_object_id_prefix(id, id_str,
1947 obj_type, repo);
1948 if (err) {
1949 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1950 err = got_error_not_ref(id_str);
1951 goto done;
1953 if (label) {
1954 err = got_object_id_str(label, *id);
1955 if (*label == NULL) {
1956 err = got_error_from_errno("strdup");
1957 goto done;
1961 done:
1962 if (ref)
1963 got_ref_close(ref);
1964 return err;
1967 const struct got_error *
1968 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1969 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1971 const struct got_error *err = NULL;
1972 struct got_reflist_entry *re;
1973 struct got_object_id *tag_id;
1974 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1976 *tag = NULL;
1978 TAILQ_FOREACH(re, refs, entry) {
1979 const char *refname;
1980 refname = got_ref_get_name(re->ref);
1981 if (got_ref_is_symbolic(re->ref))
1982 continue;
1983 if (strncmp(refname, "refs/tags/", 10) != 0)
1984 continue;
1985 if (!name_is_absolute)
1986 refname += strlen("refs/tags/");
1987 if (strcmp(refname, name) != 0)
1988 continue;
1989 err = got_ref_resolve(&tag_id, repo, re->ref);
1990 if (err)
1991 break;
1992 err = got_object_open_as_tag(tag, repo, tag_id);
1993 free(tag_id);
1994 if (err)
1995 break;
1996 if (obj_type == GOT_OBJ_TYPE_ANY ||
1997 got_object_tag_get_object_type(*tag) == obj_type)
1998 break;
1999 got_object_tag_close(*tag);
2000 *tag = NULL;
2003 if (err == NULL && *tag == NULL)
2004 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2005 GOT_OBJ_LABEL_TAG, name);
2006 return err;
2009 static const struct got_error *
2010 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2011 const char *name, mode_t mode, struct got_object_id *blob_id)
2013 const struct got_error *err = NULL;
2015 *new_te = NULL;
2017 *new_te = calloc(1, sizeof(**new_te));
2018 if (*new_te == NULL)
2019 return got_error_from_errno("calloc");
2021 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2022 sizeof((*new_te)->name)) {
2023 err = got_error(GOT_ERR_NO_SPACE);
2024 goto done;
2027 if (S_ISLNK(mode)) {
2028 (*new_te)->mode = S_IFLNK;
2029 } else {
2030 (*new_te)->mode = S_IFREG;
2031 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2033 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2034 done:
2035 if (err && *new_te) {
2036 free(*new_te);
2037 *new_te = NULL;
2039 return err;
2042 static const struct got_error *
2043 import_file(struct got_tree_entry **new_te, struct dirent *de,
2044 const char *path, struct got_repository *repo)
2046 const struct got_error *err;
2047 struct got_object_id *blob_id = NULL;
2048 char *filepath;
2049 struct stat sb;
2051 if (asprintf(&filepath, "%s%s%s", path,
2052 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2053 return got_error_from_errno("asprintf");
2055 if (lstat(filepath, &sb) != 0) {
2056 err = got_error_from_errno2("lstat", path);
2057 goto done;
2060 err = got_object_blob_create(&blob_id, filepath, repo);
2061 if (err)
2062 goto done;
2064 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2065 blob_id);
2066 done:
2067 free(filepath);
2068 if (err)
2069 free(blob_id);
2070 return err;
2073 static const struct got_error *
2074 insert_tree_entry(struct got_tree_entry *new_te,
2075 struct got_pathlist_head *paths)
2077 const struct got_error *err = NULL;
2078 struct got_pathlist_entry *new_pe;
2080 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2081 if (err)
2082 return err;
2083 if (new_pe == NULL)
2084 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2085 return NULL;
2088 static const struct got_error *write_tree(struct got_object_id **,
2089 const char *, struct got_pathlist_head *, struct got_repository *,
2090 got_repo_import_cb progress_cb, void *progress_arg);
2092 static const struct got_error *
2093 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2094 const char *path, struct got_pathlist_head *ignores,
2095 struct got_repository *repo,
2096 got_repo_import_cb progress_cb, void *progress_arg)
2098 const struct got_error *err;
2099 struct got_object_id *id = NULL;
2100 char *subdirpath;
2102 if (asprintf(&subdirpath, "%s%s%s", path,
2103 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2104 return got_error_from_errno("asprintf");
2106 (*new_te) = calloc(1, sizeof(**new_te));
2107 if (*new_te == NULL)
2108 return got_error_from_errno("calloc");
2109 (*new_te)->mode = S_IFDIR;
2110 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2111 sizeof((*new_te)->name)) {
2112 err = got_error(GOT_ERR_NO_SPACE);
2113 goto done;
2115 err = write_tree(&id, subdirpath, ignores, repo,
2116 progress_cb, progress_arg);
2117 if (err)
2118 goto done;
2119 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2121 done:
2122 free(id);
2123 free(subdirpath);
2124 if (err) {
2125 free(*new_te);
2126 *new_te = NULL;
2128 return err;
2131 static const struct got_error *
2132 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2133 struct got_pathlist_head *ignores, struct got_repository *repo,
2134 got_repo_import_cb progress_cb, void *progress_arg)
2136 const struct got_error *err = NULL;
2137 DIR *dir;
2138 struct dirent *de;
2139 int nentries;
2140 struct got_tree_entry *new_te = NULL;
2141 struct got_pathlist_head paths;
2142 struct got_pathlist_entry *pe;
2144 *new_tree_id = NULL;
2146 TAILQ_INIT(&paths);
2148 dir = opendir(path_dir);
2149 if (dir == NULL) {
2150 err = got_error_from_errno2("opendir", path_dir);
2151 goto done;
2154 nentries = 0;
2155 while ((de = readdir(dir)) != NULL) {
2156 int ignore = 0;
2157 int type;
2159 if (strcmp(de->d_name, ".") == 0 ||
2160 strcmp(de->d_name, "..") == 0)
2161 continue;
2163 err = got_path_dirent_type(&type, path_dir, de);
2164 if (err)
2165 goto done;
2167 TAILQ_FOREACH(pe, ignores, entry) {
2168 if (type == DT_DIR && pe->path_len > 0 &&
2169 pe->path[pe->path_len - 1] == '/') {
2170 char stripped[PATH_MAX];
2172 if (strlcpy(stripped, pe->path,
2173 sizeof(stripped)) >= sizeof(stripped)) {
2174 err = got_error(GOT_ERR_NO_SPACE);
2175 goto done;
2177 got_path_strip_trailing_slashes(stripped);
2178 if (fnmatch(stripped, de->d_name, 0) == 0) {
2179 ignore = 1;
2180 break;
2182 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2183 ignore = 1;
2184 break;
2187 if (ignore)
2188 continue;
2190 if (type == DT_DIR) {
2191 err = import_subdir(&new_te, de, path_dir,
2192 ignores, repo, progress_cb, progress_arg);
2193 if (err) {
2194 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2195 goto done;
2196 err = NULL;
2197 continue;
2199 } else if (type == DT_REG || type == DT_LNK) {
2200 err = import_file(&new_te, de, path_dir, repo);
2201 if (err)
2202 goto done;
2203 } else
2204 continue;
2206 err = insert_tree_entry(new_te, &paths);
2207 if (err)
2208 goto done;
2209 nentries++;
2212 if (TAILQ_EMPTY(&paths)) {
2213 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2214 "cannot create tree without any entries");
2215 goto done;
2218 TAILQ_FOREACH(pe, &paths, entry) {
2219 struct got_tree_entry *te = pe->data;
2220 char *path;
2221 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2222 continue;
2223 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2224 err = got_error_from_errno("asprintf");
2225 goto done;
2227 err = (*progress_cb)(progress_arg, path);
2228 free(path);
2229 if (err)
2230 goto done;
2233 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2234 done:
2235 if (dir)
2236 closedir(dir);
2237 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2238 return err;
2241 const struct got_error *
2242 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2243 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2244 struct got_repository *repo, got_repo_import_cb progress_cb,
2245 void *progress_arg)
2247 const struct got_error *err;
2248 struct got_object_id *new_tree_id;
2250 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2251 progress_cb, progress_arg);
2252 if (err)
2253 return err;
2255 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2256 author, time(NULL), author, time(NULL), logmsg, repo);
2257 free(new_tree_id);
2258 return err;
2261 const struct got_error *
2262 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2263 struct got_repository *repo)
2265 const struct got_error *err = NULL;
2266 char *path_objects = NULL, *path = NULL;
2267 DIR *dir = NULL;
2268 struct got_object_id id;
2269 int i;
2271 *nobjects = 0;
2272 *ondisk_size = 0;
2274 path_objects = got_repo_get_path_objects(repo);
2275 if (path_objects == NULL)
2276 return got_error_from_errno("got_repo_get_path_objects");
2278 for (i = 0; i <= 0xff; i++) {
2279 struct dirent *dent;
2281 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2282 err = got_error_from_errno("asprintf");
2283 break;
2286 dir = opendir(path);
2287 if (dir == NULL) {
2288 if (errno == ENOENT) {
2289 err = NULL;
2290 continue;
2292 err = got_error_from_errno2("opendir", path);
2293 break;
2296 while ((dent = readdir(dir)) != NULL) {
2297 char *id_str;
2298 int fd;
2299 struct stat sb;
2301 if (strcmp(dent->d_name, ".") == 0 ||
2302 strcmp(dent->d_name, "..") == 0)
2303 continue;
2305 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2306 err = got_error_from_errno("asprintf");
2307 goto done;
2310 if (!got_parse_object_id(&id, id_str, repo->algo)) {
2311 free(id_str);
2312 continue;
2314 free(id_str);
2316 err = got_object_open_loose_fd(&fd, &id, repo);
2317 if (err)
2318 goto done;
2320 if (fstat(fd, &sb) == -1) {
2321 err = got_error_from_errno("fstat");
2322 close(fd);
2323 goto done;
2325 (*nobjects)++;
2326 (*ondisk_size) += sb.st_size;
2328 if (close(fd) == -1) {
2329 err = got_error_from_errno("close");
2330 goto done;
2334 if (closedir(dir) != 0) {
2335 err = got_error_from_errno("closedir");
2336 goto done;
2338 dir = NULL;
2340 free(path);
2341 path = NULL;
2343 done:
2344 if (dir && closedir(dir) != 0 && err == NULL)
2345 err = got_error_from_errno("closedir");
2347 if (err) {
2348 *nobjects = 0;
2349 *ondisk_size = 0;
2351 free(path_objects);
2352 free(path);
2353 return err;
2356 const struct got_error *
2357 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2358 off_t *total_packsize, struct got_repository *repo)
2360 const struct got_error *err = NULL;
2361 DIR *packdir = NULL;
2362 struct dirent *dent;
2363 struct got_packidx *packidx = NULL;
2364 char *path_packidx;
2365 char *path_packfile;
2366 int packdir_fd;
2367 struct stat sb;
2369 *npackfiles = 0;
2370 *nobjects = 0;
2371 *total_packsize = 0;
2373 packdir_fd = openat(got_repo_get_fd(repo),
2374 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2375 if (packdir_fd == -1) {
2376 return got_error_from_errno_fmt("openat: %s/%s",
2377 got_repo_get_path_git_dir(repo),
2378 GOT_OBJECTS_PACK_DIR);
2381 packdir = fdopendir(packdir_fd);
2382 if (packdir == NULL) {
2383 err = got_error_from_errno("fdopendir");
2384 goto done;
2387 while ((dent = readdir(packdir)) != NULL) {
2388 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2389 continue;
2391 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2392 dent->d_name) == -1) {
2393 err = got_error_from_errno("asprintf");
2394 goto done;
2397 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2398 path_packidx, 0);
2399 free(path_packidx);
2400 if (err)
2401 goto done;
2403 if (fstat(packidx->fd, &sb) == -1)
2404 goto done;
2405 *total_packsize += sb.st_size;
2407 err = got_packidx_get_packfile_path(&path_packfile,
2408 packidx->path_packidx);
2409 if (err)
2410 goto done;
2412 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2413 0) == -1) {
2414 free(path_packfile);
2415 goto done;
2417 free(path_packfile);
2418 *total_packsize += sb.st_size;
2420 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2422 (*npackfiles)++;
2424 got_packidx_close(packidx);
2425 packidx = NULL;
2427 done:
2428 if (packidx)
2429 got_packidx_close(packidx);
2430 if (packdir && closedir(packdir) != 0 && err == NULL)
2431 err = got_error_from_errno("closedir");
2432 if (err) {
2433 *npackfiles = 0;
2434 *nobjects = 0;
2435 *total_packsize = 0;
2437 return err;
2440 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2441 got_packidx_bloom_filter_cmp);