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 const char *
119 got_repo_get_gitconfig_author_name(struct got_repository *repo)
121 return repo->gitconfig_author_name;
124 const char *
125 got_repo_get_gitconfig_author_email(struct got_repository *repo)
127 return repo->gitconfig_author_email;
130 const char *
131 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
133 return repo->global_gitconfig_author_name;
136 const char *
137 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
139 return repo->global_gitconfig_author_email;
142 const char *
143 got_repo_get_gitconfig_owner(struct got_repository *repo)
145 return repo->gitconfig_owner;
148 int
149 got_repo_has_extension(struct got_repository *repo, const char *ext)
151 int i;
153 for (i = 0; i < repo->nextensions; ++i) {
154 if (!strcasecmp(ext, repo->extnames[i]))
155 return get_boolean_val(repo->extvals[i]);
158 return 0;
161 int
162 got_repo_is_bare(struct got_repository *repo)
164 return (strcmp(repo->path, repo->path_git_dir) == 0);
167 static char *
168 get_path_git_child(struct got_repository *repo, const char *basename)
170 char *path_child;
172 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
173 basename) == -1)
174 return NULL;
176 return path_child;
179 char *
180 got_repo_get_path_objects(struct got_repository *repo)
182 return get_path_git_child(repo, GOT_OBJECTS_DIR);
185 char *
186 got_repo_get_path_objects_pack(struct got_repository *repo)
188 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
191 char *
192 got_repo_get_path_refs(struct got_repository *repo)
194 return get_path_git_child(repo, GOT_REFS_DIR);
197 char *
198 got_repo_get_path_packed_refs(struct got_repository *repo)
200 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
203 static char *
204 get_path_head(struct got_repository *repo)
206 return get_path_git_child(repo, GOT_HEAD_FILE);
209 char *
210 got_repo_get_path_gitconfig(struct got_repository *repo)
212 return get_path_git_child(repo, GOT_GITCONFIG);
215 char *
216 got_repo_get_path_gotconfig(struct got_repository *repo)
218 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
221 const struct got_gotconfig *
222 got_repo_get_gotconfig(struct got_repository *repo)
224 return repo->gotconfig;
227 void
228 got_repo_get_gitconfig_remotes(int *nremotes,
229 const struct got_remote_repo **remotes, struct got_repository *repo)
231 *nremotes = repo->ngitconfig_remotes;
232 *remotes = repo->gitconfig_remotes;
235 static int
236 is_git_repo(struct got_repository *repo)
238 const char *path_git = got_repo_get_path_git_dir(repo);
239 char *path_objects = got_repo_get_path_objects(repo);
240 char *path_refs = got_repo_get_path_refs(repo);
241 char *path_head = get_path_head(repo);
242 int ret = 0;
243 struct stat sb;
244 struct got_reference *head_ref;
246 if (lstat(path_git, &sb) == -1)
247 goto done;
248 if (!S_ISDIR(sb.st_mode))
249 goto done;
251 if (lstat(path_objects, &sb) == -1)
252 goto done;
253 if (!S_ISDIR(sb.st_mode))
254 goto done;
256 if (lstat(path_refs, &sb) == -1)
257 goto done;
258 if (!S_ISDIR(sb.st_mode))
259 goto done;
261 if (lstat(path_head, &sb) == -1)
262 goto done;
263 if (!S_ISREG(sb.st_mode))
264 goto done;
266 /* Check if the HEAD reference can be opened. */
267 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
268 goto done;
269 got_ref_close(head_ref);
271 ret = 1;
272 done:
273 free(path_objects);
274 free(path_refs);
275 free(path_head);
276 return ret;
280 static const struct got_error *
281 close_tempfiles(int *fds, size_t nfds)
283 const struct got_error *err = NULL;
284 int i;
286 for (i = 0; i < nfds; i++) {
287 if (fds[i] == -1)
288 continue;
289 if (close(fds[i]) == -1) {
290 err = got_error_from_errno("close");
291 break;
294 free(fds);
295 return err;
298 static const struct got_error *
299 open_tempfiles(int **fds, size_t array_size, size_t nfds)
301 const struct got_error *err = NULL;
302 int i;
304 *fds = calloc(array_size, sizeof(**fds));
305 if (*fds == NULL)
306 return got_error_from_errno("calloc");
308 for (i = 0; i < array_size; i++)
309 (*fds)[i] = -1;
311 for (i = 0; i < nfds; i++) {
312 (*fds)[i] = got_opentempfd();
313 if ((*fds)[i] == -1) {
314 err = got_error_from_errno("got_opentempfd");
315 close_tempfiles(*fds, nfds);
316 *fds = NULL;
317 return err;
321 return NULL;
324 static const struct got_error *
325 get_pack_cache_size(int *pack_cache_size)
327 struct rlimit rl;
329 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
330 return got_error_from_errno("getrlimit");
332 *pack_cache_size = GOT_PACK_CACHE_SIZE;
333 if (*pack_cache_size > rl.rlim_cur / 8)
334 *pack_cache_size = rl.rlim_cur / 8;
336 return NULL;
339 const struct got_error *
340 got_repo_pack_fds_open(int **pack_fds)
342 const struct got_error *err;
343 int nfds;
345 err = get_pack_cache_size(&nfds);
346 if (err)
347 return err;
349 /*
350 * We need one basefd and one accumfd per cached pack.
351 * Our constants should be set up in a way such that
352 * this error never triggers.
353 */
354 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
355 return got_error(GOT_ERR_NO_SPACE);
357 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
360 const struct got_error *
361 got_repo_pack_fds_close(int *pack_fds)
363 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
366 const struct got_error *
367 got_repo_temp_fds_open(int **temp_fds)
369 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
370 GOT_REPO_NUM_TEMPFILES);
373 void
374 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
376 int i;
378 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
379 repo->tempfiles[i] = temp_fds[i];
382 const struct got_error *
383 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
385 int i;
387 *fd = -1;
388 *idx = -1;
390 for (i = 0; i < nitems(repo->tempfiles); i++) {
391 if (repo->tempfile_use_mask & (1 << i))
392 continue;
393 if (repo->tempfiles[i] != -1) {
394 if (ftruncate(repo->tempfiles[i], 0L) == -1)
395 return got_error_from_errno("ftruncate");
396 *fd = repo->tempfiles[i];
397 *idx = i;
398 repo->tempfile_use_mask |= (1 << i);
399 return NULL;
403 return got_error(GOT_ERR_REPO_TEMPFILE);
406 void
407 got_repo_temp_fds_put(int idx, struct got_repository *repo)
409 repo->tempfile_use_mask &= ~(1 << idx);
412 const struct got_error *
413 got_repo_temp_fds_close(int *temp_fds)
415 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
418 const struct got_error *
419 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
420 struct got_object *obj)
422 #ifndef GOT_NO_OBJ_CACHE
423 const struct got_error *err = NULL;
424 err = got_object_cache_add(&repo->objcache, id, obj);
425 if (err) {
426 if (err->code == GOT_ERR_OBJ_EXISTS ||
427 err->code == GOT_ERR_OBJ_TOO_LARGE)
428 err = NULL;
429 return err;
431 obj->refcnt++;
432 #endif
433 return NULL;
436 struct got_object *
437 got_repo_get_cached_object(struct got_repository *repo,
438 struct got_object_id *id)
440 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
443 const struct got_error *
444 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
445 struct got_tree_object *tree)
447 #ifndef GOT_NO_OBJ_CACHE
448 const struct got_error *err = NULL;
449 err = got_object_cache_add(&repo->treecache, id, tree);
450 if (err) {
451 if (err->code == GOT_ERR_OBJ_EXISTS ||
452 err->code == GOT_ERR_OBJ_TOO_LARGE)
453 err = NULL;
454 return err;
456 tree->refcnt++;
457 #endif
458 return NULL;
461 struct got_tree_object *
462 got_repo_get_cached_tree(struct got_repository *repo,
463 struct got_object_id *id)
465 return (struct got_tree_object *)got_object_cache_get(
466 &repo->treecache, id);
469 const struct got_error *
470 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
471 struct got_commit_object *commit)
473 #ifndef GOT_NO_OBJ_CACHE
474 const struct got_error *err = NULL;
475 err = got_object_cache_add(&repo->commitcache, id, commit);
476 if (err) {
477 if (err->code == GOT_ERR_OBJ_EXISTS ||
478 err->code == GOT_ERR_OBJ_TOO_LARGE)
479 err = NULL;
480 return err;
482 commit->refcnt++;
483 #endif
484 return NULL;
487 struct got_commit_object *
488 got_repo_get_cached_commit(struct got_repository *repo,
489 struct got_object_id *id)
491 return (struct got_commit_object *)got_object_cache_get(
492 &repo->commitcache, id);
495 const struct got_error *
496 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
497 struct got_tag_object *tag)
499 #ifndef GOT_NO_OBJ_CACHE
500 const struct got_error *err = NULL;
501 err = got_object_cache_add(&repo->tagcache, id, tag);
502 if (err) {
503 if (err->code == GOT_ERR_OBJ_EXISTS ||
504 err->code == GOT_ERR_OBJ_TOO_LARGE)
505 err = NULL;
506 return err;
508 tag->refcnt++;
509 #endif
510 return NULL;
513 struct got_tag_object *
514 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
516 return (struct got_tag_object *)got_object_cache_get(
517 &repo->tagcache, id);
520 const struct got_error *
521 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
522 struct got_raw_object *raw)
524 #ifndef GOT_NO_OBJ_CACHE
525 const struct got_error *err = NULL;
526 err = got_object_cache_add(&repo->rawcache, id, raw);
527 if (err) {
528 if (err->code == GOT_ERR_OBJ_EXISTS ||
529 err->code == GOT_ERR_OBJ_TOO_LARGE)
530 err = NULL;
531 return err;
533 raw->refcnt++;
534 #endif
535 return NULL;
539 struct got_raw_object *
540 got_repo_get_cached_raw_object(struct got_repository *repo,
541 struct got_object_id *id)
543 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
547 static const struct got_error *
548 open_repo(struct got_repository *repo, const char *path)
550 const struct got_error *err = NULL;
552 repo->gitdir_fd = -1;
554 /* bare git repository? */
555 repo->path_git_dir = strdup(path);
556 if (repo->path_git_dir == NULL)
557 return got_error_from_errno("strdup");
558 if (is_git_repo(repo)) {
559 repo->path = strdup(repo->path_git_dir);
560 if (repo->path == NULL) {
561 err = got_error_from_errno("strdup");
562 goto done;
564 repo->gitdir_fd = open(repo->path_git_dir,
565 O_DIRECTORY | O_CLOEXEC);
566 if (repo->gitdir_fd == -1) {
567 err = got_error_from_errno2("open",
568 repo->path_git_dir);
569 goto done;
571 return NULL;
574 /* git repository with working tree? */
575 free(repo->path_git_dir);
576 repo->path_git_dir = NULL;
577 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
578 err = got_error_from_errno("asprintf");
579 goto done;
581 if (is_git_repo(repo)) {
582 repo->path = strdup(path);
583 if (repo->path == NULL) {
584 err = got_error_from_errno("strdup");
585 goto done;
587 repo->gitdir_fd = open(repo->path_git_dir,
588 O_DIRECTORY | O_CLOEXEC);
589 if (repo->gitdir_fd == -1) {
590 err = got_error_from_errno2("open",
591 repo->path_git_dir);
592 goto done;
594 return NULL;
597 err = got_error(GOT_ERR_NOT_GIT_REPO);
598 done:
599 if (err) {
600 free(repo->path);
601 repo->path = NULL;
602 free(repo->path_git_dir);
603 repo->path_git_dir = NULL;
604 if (repo->gitdir_fd != -1)
605 close(repo->gitdir_fd);
606 repo->gitdir_fd = -1;
609 return err;
612 static const struct got_error *
613 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
615 const struct got_error *err = NULL;
616 char *repo_gitconfig_path = NULL;
618 if (global_gitconfig_path) {
619 /* Read settings from ~/.gitconfig. */
620 int dummy_repo_version;
621 err = got_repo_read_gitconfig(&dummy_repo_version,
622 &repo->global_gitconfig_author_name,
623 &repo->global_gitconfig_author_email,
624 NULL, NULL, NULL, NULL, NULL, NULL,
625 global_gitconfig_path);
626 if (err)
627 return err;
630 /* Read repository's .git/config file. */
631 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
632 if (repo_gitconfig_path == NULL)
633 return got_error_from_errno("got_repo_get_path_gitconfig");
635 err = got_repo_read_gitconfig(
636 &repo->gitconfig_repository_format_version,
637 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
638 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
639 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
640 &repo->nextensions, repo_gitconfig_path);
641 if (err)
642 goto done;
644 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
645 int i;
647 for (i = 0; i < repo->ngitconfig_remotes; i++) {
648 got_repo_free_remote_repo_data(
649 &repo->gitconfig_remotes[i]);
651 free(repo->gitconfig_remotes);
652 repo->gitconfig_remotes = NULL;
653 repo->ngitconfig_remotes = 0;
655 free(repo->gitconfig_author_name);
656 repo->gitconfig_author_name = NULL;
657 free(repo->gitconfig_author_email);
658 repo->gitconfig_author_email = NULL;
660 free(repo->global_gitconfig_author_name);
661 repo->global_gitconfig_author_name = NULL;
662 free(repo->global_gitconfig_author_email);
663 repo->global_gitconfig_author_email = NULL;
666 done:
667 free(repo_gitconfig_path);
668 return err;
671 static const struct got_error *
672 read_gotconfig(struct got_repository *repo)
674 const struct got_error *err = NULL;
675 char *gotconfig_path;
677 gotconfig_path = got_repo_get_path_gotconfig(repo);
678 if (gotconfig_path == NULL)
679 return got_error_from_errno("got_repo_get_path_gotconfig");
681 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
682 free(gotconfig_path);
683 return err;
686 /* Supported repository format extensions. */
687 static const char *const repo_extensions[] = {
688 "noop", /* Got supports repository format version 1. */
689 "preciousObjects", /* Supported by gotadmin cleanup. */
690 "worktreeConfig", /* Got does not care about Git work trees. */
691 };
693 const struct got_error *
694 got_repo_open(struct got_repository **repop, const char *path,
695 const char *global_gitconfig_path, int *pack_fds)
697 struct got_repository *repo = NULL;
698 const struct got_error *err = NULL;
699 char *repo_path = NULL;
700 size_t i, j = 0;
702 *repop = NULL;
704 repo = calloc(1, sizeof(*repo));
705 if (repo == NULL)
706 return got_error_from_errno("calloc");
708 RB_INIT(&repo->packidx_bloom_filters);
709 TAILQ_INIT(&repo->packidx_paths);
711 for (i = 0; i < nitems(repo->privsep_children); i++) {
712 memset(&repo->privsep_children[i], 0,
713 sizeof(repo->privsep_children[0]));
714 repo->privsep_children[i].imsg_fd = -1;
717 err = got_object_cache_init(&repo->objcache,
718 GOT_OBJECT_CACHE_TYPE_OBJ);
719 if (err)
720 goto done;
721 err = got_object_cache_init(&repo->treecache,
722 GOT_OBJECT_CACHE_TYPE_TREE);
723 if (err)
724 goto done;
725 err = got_object_cache_init(&repo->commitcache,
726 GOT_OBJECT_CACHE_TYPE_COMMIT);
727 if (err)
728 goto done;
729 err = got_object_cache_init(&repo->tagcache,
730 GOT_OBJECT_CACHE_TYPE_TAG);
731 if (err)
732 goto done;
733 err = got_object_cache_init(&repo->rawcache,
734 GOT_OBJECT_CACHE_TYPE_RAW);
735 if (err)
736 goto done;
738 err = get_pack_cache_size(&repo->pack_cache_size);
739 if (err)
740 goto done;
741 for (i = 0; i < nitems(repo->packs); i++) {
742 if (pack_fds != NULL && i < repo->pack_cache_size) {
743 repo->packs[i].basefd = pack_fds[j++];
744 repo->packs[i].accumfd = pack_fds[j++];
745 } else {
746 repo->packs[i].basefd = -1;
747 repo->packs[i].accumfd = -1;
750 for (i = 0; i < nitems(repo->tempfiles); i++)
751 repo->tempfiles[i] = -1;
752 repo->pinned_pack = -1;
753 repo->pinned_packidx = -1;
754 repo->pinned_pid = 0;
756 repo_path = realpath(path, NULL);
757 if (repo_path == NULL) {
758 err = got_error_from_errno2("realpath", path);
759 goto done;
762 for (;;) {
763 char *parent_path;
765 err = open_repo(repo, repo_path);
766 if (err == NULL)
767 break;
768 if (err->code != GOT_ERR_NOT_GIT_REPO)
769 goto done;
770 if (repo_path[0] == '/' && repo_path[1] == '\0') {
771 err = got_error(GOT_ERR_NOT_GIT_REPO);
772 goto done;
774 err = got_path_dirname(&parent_path, repo_path);
775 if (err)
776 goto done;
777 free(repo_path);
778 repo_path = parent_path;
781 err = read_gotconfig(repo);
782 if (err)
783 goto done;
785 err = read_gitconfig(repo, global_gitconfig_path);
786 if (err)
787 goto done;
788 if (repo->gitconfig_repository_format_version != 0) {
789 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
790 goto done;
792 for (i = 0; i < repo->nextensions; i++) {
793 char *ext = repo->extnames[i];
794 char *val = repo->extvals[i];
795 int j, supported = 0;
797 if (!is_boolean_val(val)) {
798 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
799 goto done;
802 if (!get_boolean_val(val))
803 continue;
805 for (j = 0; j < nitems(repo_extensions); j++) {
806 if (strcmp(ext, repo_extensions[j]) == 0) {
807 supported = 1;
808 break;
811 if (!supported) {
812 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
813 goto done;
817 err = got_repo_list_packidx(&repo->packidx_paths, repo);
818 done:
819 if (err)
820 got_repo_close(repo);
821 else
822 *repop = repo;
823 free(repo_path);
824 return err;
827 const struct got_error *
828 got_repo_close(struct got_repository *repo)
830 const struct got_error *err = NULL, *child_err;
831 struct got_packidx_bloom_filter *bf;
832 size_t i;
834 for (i = 0; i < repo->pack_cache_size; i++) {
835 if (repo->packidx_cache[i] == NULL)
836 break;
837 got_packidx_close(repo->packidx_cache[i]);
840 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
841 &repo->packidx_bloom_filters))) {
842 RB_REMOVE(got_packidx_bloom_filter_tree,
843 &repo->packidx_bloom_filters, bf);
844 bloom_free(bf->bloom);
845 free(bf->bloom);
846 free(bf);
849 for (i = 0; i < repo->pack_cache_size; i++)
850 if (repo->packs[i].path_packfile)
851 if (repo->packs[i].path_packfile)
852 got_pack_close(&repo->packs[i]);
854 free(repo->path);
855 free(repo->path_git_dir);
857 got_object_cache_close(&repo->objcache);
858 got_object_cache_close(&repo->treecache);
859 got_object_cache_close(&repo->commitcache);
860 got_object_cache_close(&repo->tagcache);
861 got_object_cache_close(&repo->rawcache);
863 for (i = 0; i < nitems(repo->privsep_children); i++) {
864 if (repo->privsep_children[i].imsg_fd == -1)
865 continue;
866 imsg_clear(repo->privsep_children[i].ibuf);
867 free(repo->privsep_children[i].ibuf);
868 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
869 child_err = got_privsep_wait_for_child(
870 repo->privsep_children[i].pid);
871 if (child_err && err == NULL)
872 err = child_err;
873 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
874 err == NULL)
875 err = got_error_from_errno("close");
878 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
879 err == NULL)
880 err = got_error_from_errno("close");
882 if (repo->gotconfig)
883 got_gotconfig_free(repo->gotconfig);
884 free(repo->gitconfig_author_name);
885 free(repo->gitconfig_author_email);
886 for (i = 0; i < repo->ngitconfig_remotes; i++)
887 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
888 free(repo->gitconfig_remotes);
889 for (i = 0; i < repo->nextensions; i++) {
890 free(repo->extnames[i]);
891 free(repo->extvals[i]);
893 free(repo->extnames);
894 free(repo->extvals);
896 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
897 free(repo);
899 return err;
902 void
903 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
905 int i;
907 free(repo->name);
908 repo->name = NULL;
909 free(repo->fetch_url);
910 repo->fetch_url = NULL;
911 free(repo->send_url);
912 repo->send_url = NULL;
913 for (i = 0; i < repo->nfetch_branches; i++)
914 free(repo->fetch_branches[i]);
915 free(repo->fetch_branches);
916 repo->fetch_branches = NULL;
917 repo->nfetch_branches = 0;
918 for (i = 0; i < repo->nsend_branches; i++)
919 free(repo->send_branches[i]);
920 free(repo->send_branches);
921 repo->send_branches = NULL;
922 repo->nsend_branches = 0;
925 const struct got_error *
926 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
927 const char *input_path)
929 const struct got_error *err = NULL;
930 const char *repo_abspath = NULL;
931 size_t repolen, len;
932 char *canonpath, *path = NULL;
934 *in_repo_path = NULL;
936 canonpath = strdup(input_path);
937 if (canonpath == NULL) {
938 err = got_error_from_errno("strdup");
939 goto done;
941 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
942 if (err)
943 goto done;
945 repo_abspath = got_repo_get_path(repo);
947 if (canonpath[0] == '\0') {
948 path = strdup(canonpath);
949 if (path == NULL) {
950 err = got_error_from_errno("strdup");
951 goto done;
953 } else {
954 path = realpath(canonpath, NULL);
955 if (path == NULL) {
956 if (errno != ENOENT) {
957 err = got_error_from_errno2("realpath",
958 canonpath);
959 goto done;
961 /*
962 * Path is not on disk.
963 * Assume it is already relative to repository root.
964 */
965 path = strdup(canonpath);
966 if (path == NULL) {
967 err = got_error_from_errno("strdup");
968 goto done;
972 repolen = strlen(repo_abspath);
973 len = strlen(path);
976 if (strcmp(path, repo_abspath) == 0) {
977 free(path);
978 path = strdup("");
979 if (path == NULL) {
980 err = got_error_from_errno("strdup");
981 goto done;
983 } else if (len > repolen &&
984 got_path_is_child(path, repo_abspath, repolen)) {
985 /* Matched an on-disk path inside repository. */
986 if (got_repo_is_bare(repo)) {
987 /*
988 * Matched an on-disk path inside repository
989 * database. Treat input as repository-relative.
990 */
991 free(path);
992 path = canonpath;
993 canonpath = NULL;
994 } else {
995 char *child;
996 /* Strip common prefix with repository path. */
997 err = got_path_skip_common_ancestor(&child,
998 repo_abspath, path);
999 if (err)
1000 goto done;
1001 free(path);
1002 path = child;
1004 } else {
1006 * Matched unrelated on-disk path.
1007 * Treat input as repository-relative.
1009 free(path);
1010 path = canonpath;
1011 canonpath = NULL;
1015 /* Make in-repository path absolute */
1016 if (path[0] != '/') {
1017 char *abspath;
1018 if (asprintf(&abspath, "/%s", path) == -1) {
1019 err = got_error_from_errno("asprintf");
1020 goto done;
1022 free(path);
1023 path = abspath;
1026 done:
1027 free(canonpath);
1028 if (err)
1029 free(path);
1030 else
1031 *in_repo_path = path;
1032 return err;
1035 static const struct got_error *
1036 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1037 const char *path_packidx)
1039 const struct got_error *err = NULL;
1040 size_t i;
1042 for (i = 0; i < repo->pack_cache_size; i++) {
1043 if (repo->packidx_cache[i] == NULL)
1044 break;
1045 if (strcmp(repo->packidx_cache[i]->path_packidx,
1046 path_packidx) == 0) {
1047 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1050 if (i == repo->pack_cache_size) {
1051 do {
1052 i--;
1053 } while (i > 0 && repo->pinned_packidx >= 0 &&
1054 i == repo->pinned_packidx);
1055 err = got_packidx_close(repo->packidx_cache[i]);
1056 if (err)
1057 return err;
1060 repo->packidx_cache[i] = packidx;
1062 return NULL;
1065 int
1066 got_repo_is_packidx_filename(const char *name, size_t len)
1068 if (len != GOT_PACKIDX_NAMELEN)
1069 return 0;
1071 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1072 return 0;
1074 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1075 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1076 return 0;
1078 return 1;
1081 static struct got_packidx_bloom_filter *
1082 get_packidx_bloom_filter(struct got_repository *repo,
1083 const char *path, size_t path_len)
1085 struct got_packidx_bloom_filter key;
1087 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1088 return NULL; /* XXX */
1089 key.path_len = path_len;
1091 return RB_FIND(got_packidx_bloom_filter_tree,
1092 &repo->packidx_bloom_filters, &key);
1095 int
1096 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1097 const char *path_packidx, struct got_object_id *id)
1099 struct got_packidx_bloom_filter *bf;
1101 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1102 if (bf)
1103 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1105 /* No bloom filter means this pack index must be searched. */
1106 return 1;
1109 static const struct got_error *
1110 add_packidx_bloom_filter(struct got_repository *repo,
1111 struct got_packidx *packidx, const char *path_packidx)
1113 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1114 struct got_packidx_bloom_filter *bf;
1115 size_t len;
1118 * Don't use bloom filters for very large pack index files.
1119 * Large pack files will contain a relatively large fraction
1120 * of our objects so we will likely need to visit them anyway.
1121 * The more objects a pack file contains the higher the probability
1122 * of a false-positive match from the bloom filter. And reading
1123 * all object IDs from a large pack index file can be expensive.
1125 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1126 return NULL;
1128 /* Do we already have a filter for this pack index? */
1129 if (get_packidx_bloom_filter(repo, path_packidx,
1130 strlen(path_packidx)) != NULL)
1131 return NULL;
1133 bf = calloc(1, sizeof(*bf));
1134 if (bf == NULL)
1135 return got_error_from_errno("calloc");
1136 bf->bloom = calloc(1, sizeof(*bf->bloom));
1137 if (bf->bloom == NULL) {
1138 free(bf);
1139 return got_error_from_errno("calloc");
1142 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1143 if (len >= sizeof(bf->path)) {
1144 free(bf->bloom);
1145 free(bf);
1146 return got_error(GOT_ERR_NO_SPACE);
1148 bf->path_len = len;
1150 /* Minimum size supported by our bloom filter is 1000 entries. */
1151 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1152 for (i = 0; i < nobjects; i++) {
1153 struct got_packidx_object_id *id;
1154 id = &packidx->hdr.sorted_ids[i];
1155 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1158 RB_INSERT(got_packidx_bloom_filter_tree,
1159 &repo->packidx_bloom_filters, bf);
1160 return NULL;
1163 static void
1164 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1166 struct got_pathlist_entry *pe;
1168 while (!TAILQ_EMPTY(packidx_paths)) {
1169 pe = TAILQ_FIRST(packidx_paths);
1170 TAILQ_REMOVE(packidx_paths, pe, entry);
1171 free((char *)pe->path);
1172 free(pe);
1176 static const struct got_error *
1177 refresh_packidx_paths(struct got_repository *repo)
1179 const struct got_error *err = NULL;
1180 char *objects_pack_dir = NULL;
1181 struct stat sb;
1183 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1184 if (objects_pack_dir == NULL)
1185 return got_error_from_errno("got_repo_get_path_objects_pack");
1187 if (stat(objects_pack_dir, &sb) == -1) {
1188 if (errno != ENOENT) {
1189 err = got_error_from_errno2("stat", objects_pack_dir);
1190 goto done;
1192 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1193 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1194 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1195 purge_packidx_paths(&repo->packidx_paths);
1196 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1197 if (err)
1198 goto done;
1200 done:
1201 free(objects_pack_dir);
1202 return err;
1205 const struct got_error *
1206 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1207 struct got_repository *repo, struct got_object_id *id)
1209 const struct got_error *err;
1210 struct got_pathlist_entry *pe;
1211 size_t i;
1213 /* Search pack index cache. */
1214 for (i = 0; i < repo->pack_cache_size; i++) {
1215 if (repo->packidx_cache[i] == NULL)
1216 break;
1217 if (!got_repo_check_packidx_bloom_filter(repo,
1218 repo->packidx_cache[i]->path_packidx, id))
1219 continue; /* object will not be found in this index */
1220 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1221 if (*idx != -1) {
1222 *packidx = repo->packidx_cache[i];
1224 * Move this cache entry to the front. Repeatedly
1225 * searching a wrong pack index can be expensive.
1227 if (i > 0) {
1228 memmove(&repo->packidx_cache[1],
1229 &repo->packidx_cache[0],
1230 i * sizeof(repo->packidx_cache[0]));
1231 repo->packidx_cache[0] = *packidx;
1232 if (repo->pinned_packidx >= 0 &&
1233 repo->pinned_packidx < i)
1234 repo->pinned_packidx++;
1235 else if (repo->pinned_packidx == i)
1236 repo->pinned_packidx = 0;
1238 return NULL;
1241 /* No luck. Search the filesystem. */
1243 err = refresh_packidx_paths(repo);
1244 if (err)
1245 return err;
1247 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1248 const char *path_packidx = pe->path;
1249 int is_cached = 0;
1251 if (!got_repo_check_packidx_bloom_filter(repo,
1252 pe->path, id))
1253 continue; /* object will not be found in this index */
1255 for (i = 0; i < repo->pack_cache_size; i++) {
1256 if (repo->packidx_cache[i] == NULL)
1257 break;
1258 if (strcmp(repo->packidx_cache[i]->path_packidx,
1259 path_packidx) == 0) {
1260 is_cached = 1;
1261 break;
1264 if (is_cached)
1265 continue; /* already searched */
1267 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1268 path_packidx, 0);
1269 if (err)
1270 goto done;
1272 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1273 if (err)
1274 goto done;
1276 err = cache_packidx(repo, *packidx, path_packidx);
1277 if (err)
1278 goto done;
1280 *idx = got_packidx_get_object_idx(*packidx, id);
1281 if (*idx != -1) {
1282 err = NULL; /* found the object */
1283 goto done;
1287 err = got_error_no_obj(id);
1288 done:
1289 return err;
1292 const struct got_error *
1293 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1294 struct got_repository *repo)
1296 const struct got_error *err = NULL;
1297 DIR *packdir = NULL;
1298 struct dirent *dent;
1299 char *path_packidx = NULL;
1300 int packdir_fd;
1301 struct stat sb;
1303 packdir_fd = openat(got_repo_get_fd(repo),
1304 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1305 if (packdir_fd == -1) {
1306 return got_error_from_errno_fmt("openat: %s/%s",
1307 got_repo_get_path_git_dir(repo),
1308 GOT_OBJECTS_PACK_DIR);
1311 packdir = fdopendir(packdir_fd);
1312 if (packdir == NULL) {
1313 err = got_error_from_errno("fdopendir");
1314 goto done;
1317 if (fstat(packdir_fd, &sb) == -1) {
1318 err = got_error_from_errno("fstat");
1319 goto done;
1321 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1322 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1324 while ((dent = readdir(packdir)) != NULL) {
1325 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1326 continue;
1328 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1329 dent->d_name) == -1) {
1330 err = got_error_from_errno("asprintf");
1331 path_packidx = NULL;
1332 break;
1335 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1336 if (err)
1337 break;
1339 done:
1340 if (err)
1341 free(path_packidx);
1342 if (packdir && closedir(packdir) != 0 && err == NULL)
1343 err = got_error_from_errno("closedir");
1344 return err;
1347 const struct got_error *
1348 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1349 struct got_repository *repo)
1351 const struct got_error *err;
1352 size_t i;
1354 *packidx = NULL;
1356 /* Search pack index cache. */
1357 for (i = 0; i < repo->pack_cache_size; i++) {
1358 if (repo->packidx_cache[i] == NULL)
1359 break;
1360 if (strcmp(repo->packidx_cache[i]->path_packidx,
1361 path_packidx) == 0) {
1362 *packidx = repo->packidx_cache[i];
1363 return NULL;
1366 /* No luck. Search the filesystem. */
1368 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1369 path_packidx, 0);
1370 if (err)
1371 return err;
1373 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1374 if (err)
1375 goto done;
1377 err = cache_packidx(repo, *packidx, path_packidx);
1378 done:
1379 if (err) {
1380 got_packidx_close(*packidx);
1381 *packidx = NULL;
1383 return err;
1386 static const struct got_error *
1387 read_packfile_hdr(int fd, struct got_packidx *packidx)
1389 const struct got_error *err = NULL;
1390 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1391 struct got_packfile_hdr hdr;
1392 ssize_t n;
1394 n = read(fd, &hdr, sizeof(hdr));
1395 if (n < 0)
1396 return got_error_from_errno("read");
1397 if (n != sizeof(hdr))
1398 return got_error(GOT_ERR_BAD_PACKFILE);
1400 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1401 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1402 be32toh(hdr.nobjects) != totobj)
1403 err = got_error(GOT_ERR_BAD_PACKFILE);
1405 return err;
1408 static const struct got_error *
1409 open_packfile(int *fd, struct got_repository *repo,
1410 const char *relpath, struct got_packidx *packidx)
1412 const struct got_error *err = NULL;
1414 *fd = openat(got_repo_get_fd(repo), relpath,
1415 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1416 if (*fd == -1)
1417 return got_error_from_errno_fmt("openat: %s/%s",
1418 got_repo_get_path_git_dir(repo), relpath);
1420 if (packidx) {
1421 err = read_packfile_hdr(*fd, packidx);
1422 if (err) {
1423 close(*fd);
1424 *fd = -1;
1428 return err;
1431 const struct got_error *
1432 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1433 const char *path_packfile, struct got_packidx *packidx)
1435 const struct got_error *err = NULL;
1436 struct got_pack *pack = NULL;
1437 struct stat sb;
1438 size_t i;
1440 if (packp)
1441 *packp = NULL;
1443 for (i = 0; i < repo->pack_cache_size; i++) {
1444 pack = &repo->packs[i];
1445 if (pack->path_packfile == NULL)
1446 break;
1447 if (strcmp(pack->path_packfile, path_packfile) == 0)
1448 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1451 if (i == repo->pack_cache_size) {
1452 struct got_pack tmp;
1453 do {
1454 i--;
1455 } while (i > 0 && repo->pinned_pack >= 0 &&
1456 i == repo->pinned_pack);
1457 err = got_pack_close(&repo->packs[i]);
1458 if (err)
1459 return err;
1460 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1461 return got_error_from_errno("ftruncate");
1462 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1463 return got_error_from_errno("ftruncate");
1464 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1465 memcpy(&repo->packs[i], &repo->packs[0],
1466 sizeof(repo->packs[i]));
1467 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1468 if (repo->pinned_pack == 0)
1469 repo->pinned_pack = i;
1470 else if (repo->pinned_pack == i)
1471 repo->pinned_pack = 0;
1472 i = 0;
1475 pack = &repo->packs[i];
1477 pack->path_packfile = strdup(path_packfile);
1478 if (pack->path_packfile == NULL) {
1479 err = got_error_from_errno("strdup");
1480 goto done;
1483 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1484 if (err)
1485 goto done;
1487 if (fstat(pack->fd, &sb) != 0) {
1488 err = got_error_from_errno("fstat");
1489 goto done;
1491 pack->filesize = sb.st_size;
1493 pack->privsep_child = NULL;
1495 err = got_delta_cache_alloc(&pack->delta_cache);
1496 if (err)
1497 goto done;
1499 #ifndef GOT_PACK_NO_MMAP
1500 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1501 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1502 pack->fd, 0);
1503 if (pack->map == MAP_FAILED) {
1504 if (errno != ENOMEM) {
1505 err = got_error_from_errno("mmap");
1506 goto done;
1508 pack->map = NULL; /* fall back to read(2) */
1511 #endif
1512 done:
1513 if (err) {
1514 if (pack)
1515 got_pack_close(pack);
1516 } else if (packp)
1517 *packp = pack;
1518 return err;
1521 struct got_pack *
1522 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1524 struct got_pack *pack = NULL;
1525 size_t i;
1527 for (i = 0; i < repo->pack_cache_size; i++) {
1528 pack = &repo->packs[i];
1529 if (pack->path_packfile == NULL)
1530 break;
1531 if (strcmp(pack->path_packfile, path_packfile) == 0)
1532 return pack;
1535 return NULL;
1538 const struct got_error *
1539 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1540 struct got_pack *pack)
1542 size_t i;
1543 int pinned_pack = -1, pinned_packidx = -1;
1545 for (i = 0; i < repo->pack_cache_size; i++) {
1546 if (repo->packidx_cache[i] &&
1547 strcmp(repo->packidx_cache[i]->path_packidx,
1548 packidx->path_packidx) == 0)
1549 pinned_packidx = i;
1550 if (repo->packs[i].path_packfile &&
1551 strcmp(repo->packs[i].path_packfile,
1552 pack->path_packfile) == 0)
1553 pinned_pack = i;
1556 if (pinned_packidx == -1 || pinned_pack == -1)
1557 return got_error(GOT_ERR_PIN_PACK);
1559 repo->pinned_pack = pinned_pack;
1560 repo->pinned_packidx = pinned_packidx;
1561 if (repo->packs[pinned_pack].privsep_child)
1562 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1563 return NULL;
1566 struct got_pack *
1567 got_repo_get_pinned_pack(struct got_repository *repo)
1569 if (repo->pinned_pack >= 0 &&
1570 repo->pinned_pack < repo->pack_cache_size)
1571 return &repo->packs[repo->pinned_pack];
1573 return NULL;
1576 void
1577 got_repo_unpin_pack(struct got_repository *repo)
1579 repo->pinned_packidx = -1;
1580 repo->pinned_pack = -1;
1581 repo->pinned_pid = 0;
1584 const struct got_error *
1585 got_repo_init(const char *repo_path, const char *head_name)
1587 const struct got_error *err = NULL;
1588 const char *dirnames[] = {
1589 GOT_OBJECTS_DIR,
1590 GOT_OBJECTS_PACK_DIR,
1591 GOT_REFS_DIR,
1593 const char *description_str = "Unnamed repository; "
1594 "edit this file 'description' to name the repository.";
1595 const char *headref = "ref: refs/heads/";
1596 const char *gitconfig_str = "[core]\n"
1597 "\trepositoryformatversion = 0\n"
1598 "\tfilemode = true\n"
1599 "\tbare = true\n";
1600 char *headref_str, *path;
1601 size_t i;
1603 if (!got_path_dir_is_empty(repo_path))
1604 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1606 for (i = 0; i < nitems(dirnames); i++) {
1607 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1608 return got_error_from_errno("asprintf");
1610 err = got_path_mkdir(path);
1611 free(path);
1612 if (err)
1613 return err;
1616 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1617 return got_error_from_errno("asprintf");
1618 err = got_path_create_file(path, description_str);
1619 free(path);
1620 if (err)
1621 return err;
1623 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1624 return got_error_from_errno("asprintf");
1625 if (asprintf(&headref_str, "%s%s", headref,
1626 head_name ? head_name : "main") == -1) {
1627 free(path);
1628 return got_error_from_errno("asprintf");
1630 err = got_path_create_file(path, headref_str);
1631 free(headref_str);
1632 free(path);
1633 if (err)
1634 return err;
1636 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1637 return got_error_from_errno("asprintf");
1638 err = got_path_create_file(path, gitconfig_str);
1639 free(path);
1640 if (err)
1641 return err;
1643 return NULL;
1646 static const struct got_error *
1647 match_packed_object(struct got_object_id **unique_id,
1648 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1650 const struct got_error *err = NULL;
1651 struct got_object_id_queue matched_ids;
1652 struct got_pathlist_entry *pe;
1654 STAILQ_INIT(&matched_ids);
1656 err = refresh_packidx_paths(repo);
1657 if (err)
1658 return err;
1660 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1661 const char *path_packidx = pe->path;
1662 struct got_packidx *packidx;
1663 struct got_object_qid *qid;
1665 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1666 path_packidx, 0);
1667 if (err)
1668 break;
1670 err = got_packidx_match_id_str_prefix(&matched_ids,
1671 packidx, id_str_prefix);
1672 if (err) {
1673 got_packidx_close(packidx);
1674 break;
1676 err = got_packidx_close(packidx);
1677 if (err)
1678 break;
1680 STAILQ_FOREACH(qid, &matched_ids, entry) {
1681 if (obj_type != GOT_OBJ_TYPE_ANY) {
1682 int matched_type;
1683 err = got_object_get_type(&matched_type, repo,
1684 &qid->id);
1685 if (err)
1686 goto done;
1687 if (matched_type != obj_type)
1688 continue;
1690 if (*unique_id == NULL) {
1691 *unique_id = got_object_id_dup(&qid->id);
1692 if (*unique_id == NULL) {
1693 err = got_error_from_errno("malloc");
1694 goto done;
1696 } else {
1697 if (got_object_id_cmp(*unique_id,
1698 &qid->id) == 0)
1699 continue; /* packed multiple times */
1700 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1701 goto done;
1705 done:
1706 got_object_id_queue_free(&matched_ids);
1707 if (err) {
1708 free(*unique_id);
1709 *unique_id = NULL;
1711 return err;
1714 static const struct got_error *
1715 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1716 const char *object_dir, const char *id_str_prefix, int obj_type,
1717 struct got_repository *repo)
1719 const struct got_error *err = NULL;
1720 char *path, *id_str = NULL;
1721 DIR *dir = NULL;
1722 struct dirent *dent;
1723 struct got_object_id id;
1725 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1726 err = got_error_from_errno("asprintf");
1727 goto done;
1730 dir = opendir(path);
1731 if (dir == NULL) {
1732 if (errno == ENOENT) {
1733 err = NULL;
1734 goto done;
1736 err = got_error_from_errno2("opendir", path);
1737 goto done;
1739 while ((dent = readdir(dir)) != NULL) {
1740 int cmp;
1741 enum got_hash_algorithm algo = GOT_HASH_SHA1;
1743 free(id_str);
1744 id_str = NULL;
1746 if (strcmp(dent->d_name, ".") == 0 ||
1747 strcmp(dent->d_name, "..") == 0)
1748 continue;
1750 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1751 err = got_error_from_errno("asprintf");
1752 goto done;
1755 if (!got_parse_object_id(&id, id_str, algo))
1756 continue;
1759 * Directory entries do not necessarily appear in
1760 * sorted order, so we must iterate over all of them.
1762 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1763 if (cmp != 0)
1764 continue;
1766 if (*unique_id == NULL) {
1767 if (obj_type != GOT_OBJ_TYPE_ANY) {
1768 int matched_type;
1769 err = got_object_get_type(&matched_type, repo,
1770 &id);
1771 if (err)
1772 goto done;
1773 if (matched_type != obj_type)
1774 continue;
1776 *unique_id = got_object_id_dup(&id);
1777 if (*unique_id == NULL) {
1778 err = got_error_from_errno("got_object_id_dup");
1779 goto done;
1781 } else {
1782 if (got_object_id_cmp(*unique_id, &id) == 0)
1783 continue; /* both packed and loose */
1784 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1785 goto done;
1788 done:
1789 if (dir && closedir(dir) != 0 && err == NULL)
1790 err = got_error_from_errno("closedir");
1791 if (err) {
1792 free(*unique_id);
1793 *unique_id = NULL;
1795 free(id_str);
1796 free(path);
1797 return err;
1800 const struct got_error *
1801 got_repo_match_object_id_prefix(struct got_object_id **id,
1802 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1804 const struct got_error *err = NULL;
1805 char *path_objects = NULL, *object_dir = NULL;
1806 size_t len;
1807 int i;
1809 *id = NULL;
1811 path_objects = got_repo_get_path_objects(repo);
1813 len = strlen(id_str_prefix);
1814 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1815 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1816 goto done;
1819 for (i = 0; i < len; i++) {
1820 if (isxdigit((unsigned char)id_str_prefix[i]))
1821 continue;
1822 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1823 goto done;
1826 if (len >= 2) {
1827 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1828 if (err)
1829 goto done;
1830 object_dir = strndup(id_str_prefix, 2);
1831 if (object_dir == NULL) {
1832 err = got_error_from_errno("strdup");
1833 goto done;
1835 err = match_loose_object(id, path_objects, object_dir,
1836 id_str_prefix, obj_type, repo);
1837 } else if (len == 1) {
1838 int i;
1839 for (i = 0; i < 0xf; i++) {
1840 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1841 == -1) {
1842 err = got_error_from_errno("asprintf");
1843 goto done;
1845 err = match_packed_object(id, repo, object_dir,
1846 obj_type);
1847 if (err)
1848 goto done;
1849 err = match_loose_object(id, path_objects, object_dir,
1850 id_str_prefix, obj_type, repo);
1851 if (err)
1852 goto done;
1854 } else {
1855 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1856 goto done;
1858 done:
1859 free(path_objects);
1860 free(object_dir);
1861 if (err) {
1862 free(*id);
1863 *id = NULL;
1864 } else if (*id == NULL) {
1865 switch (obj_type) {
1866 case GOT_OBJ_TYPE_BLOB:
1867 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1868 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1869 break;
1870 case GOT_OBJ_TYPE_TREE:
1871 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1872 GOT_OBJ_LABEL_TREE, id_str_prefix);
1873 break;
1874 case GOT_OBJ_TYPE_COMMIT:
1875 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1876 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1877 break;
1878 case GOT_OBJ_TYPE_TAG:
1879 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1880 GOT_OBJ_LABEL_TAG, id_str_prefix);
1881 break;
1882 default:
1883 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1884 break;
1888 return err;
1891 const struct got_error *
1892 got_repo_match_object_id(struct got_object_id **id, char **label,
1893 const char *id_str, int obj_type, struct got_reflist_head *refs,
1894 struct got_repository *repo)
1896 const struct got_error *err;
1897 struct got_tag_object *tag;
1898 struct got_reference *ref = NULL;
1900 *id = NULL;
1901 if (label)
1902 *label = NULL;
1904 if (refs) {
1905 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1906 refs, repo);
1907 if (err == NULL) {
1908 *id = got_object_id_dup(
1909 got_object_tag_get_object_id(tag));
1910 if (*id == NULL)
1911 err = got_error_from_errno("got_object_id_dup");
1912 else if (label && asprintf(label, "refs/tags/%s",
1913 got_object_tag_get_name(tag)) == -1) {
1914 err = got_error_from_errno("asprintf");
1915 free(*id);
1916 *id = NULL;
1918 got_object_tag_close(tag);
1919 return err;
1920 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1921 err->code != GOT_ERR_NO_OBJ)
1922 return err;
1925 err = got_ref_open(&ref, repo, id_str, 0);
1926 if (err == NULL) {
1927 err = got_ref_resolve(id, repo, ref);
1928 if (err)
1929 goto done;
1930 if (label) {
1931 *label = strdup(got_ref_get_name(ref));
1932 if (*label == NULL) {
1933 err = got_error_from_errno("strdup");
1934 goto done;
1937 } else {
1938 if (err->code != GOT_ERR_NOT_REF &&
1939 err->code != GOT_ERR_BAD_REF_NAME)
1940 goto done;
1941 err = got_repo_match_object_id_prefix(id, id_str,
1942 obj_type, repo);
1943 if (err) {
1944 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1945 err = got_error_not_ref(id_str);
1946 goto done;
1948 if (label) {
1949 err = got_object_id_str(label, *id);
1950 if (*label == NULL) {
1951 err = got_error_from_errno("strdup");
1952 goto done;
1956 done:
1957 if (ref)
1958 got_ref_close(ref);
1959 return err;
1962 const struct got_error *
1963 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1964 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_reflist_entry *re;
1968 struct got_object_id *tag_id;
1969 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1971 *tag = NULL;
1973 TAILQ_FOREACH(re, refs, entry) {
1974 const char *refname;
1975 refname = got_ref_get_name(re->ref);
1976 if (got_ref_is_symbolic(re->ref))
1977 continue;
1978 if (strncmp(refname, "refs/tags/", 10) != 0)
1979 continue;
1980 if (!name_is_absolute)
1981 refname += strlen("refs/tags/");
1982 if (strcmp(refname, name) != 0)
1983 continue;
1984 err = got_ref_resolve(&tag_id, repo, re->ref);
1985 if (err)
1986 break;
1987 err = got_object_open_as_tag(tag, repo, tag_id);
1988 free(tag_id);
1989 if (err)
1990 break;
1991 if (obj_type == GOT_OBJ_TYPE_ANY ||
1992 got_object_tag_get_object_type(*tag) == obj_type)
1993 break;
1994 got_object_tag_close(*tag);
1995 *tag = NULL;
1998 if (err == NULL && *tag == NULL)
1999 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2000 GOT_OBJ_LABEL_TAG, name);
2001 return err;
2004 static const struct got_error *
2005 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2006 const char *name, mode_t mode, struct got_object_id *blob_id)
2008 const struct got_error *err = NULL;
2010 *new_te = NULL;
2012 *new_te = calloc(1, sizeof(**new_te));
2013 if (*new_te == NULL)
2014 return got_error_from_errno("calloc");
2016 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2017 sizeof((*new_te)->name)) {
2018 err = got_error(GOT_ERR_NO_SPACE);
2019 goto done;
2022 if (S_ISLNK(mode)) {
2023 (*new_te)->mode = S_IFLNK;
2024 } else {
2025 (*new_te)->mode = S_IFREG;
2026 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2028 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2029 done:
2030 if (err && *new_te) {
2031 free(*new_te);
2032 *new_te = NULL;
2034 return err;
2037 static const struct got_error *
2038 import_file(struct got_tree_entry **new_te, struct dirent *de,
2039 const char *path, struct got_repository *repo)
2041 const struct got_error *err;
2042 struct got_object_id *blob_id = NULL;
2043 char *filepath;
2044 struct stat sb;
2046 if (asprintf(&filepath, "%s%s%s", path,
2047 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2048 return got_error_from_errno("asprintf");
2050 if (lstat(filepath, &sb) != 0) {
2051 err = got_error_from_errno2("lstat", path);
2052 goto done;
2055 err = got_object_blob_create(&blob_id, filepath, repo);
2056 if (err)
2057 goto done;
2059 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2060 blob_id);
2061 done:
2062 free(filepath);
2063 if (err)
2064 free(blob_id);
2065 return err;
2068 static const struct got_error *
2069 insert_tree_entry(struct got_tree_entry *new_te,
2070 struct got_pathlist_head *paths)
2072 const struct got_error *err = NULL;
2073 struct got_pathlist_entry *new_pe;
2075 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2076 if (err)
2077 return err;
2078 if (new_pe == NULL)
2079 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2080 return NULL;
2083 static const struct got_error *write_tree(struct got_object_id **,
2084 const char *, struct got_pathlist_head *, struct got_repository *,
2085 got_repo_import_cb progress_cb, void *progress_arg);
2087 static const struct got_error *
2088 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2089 const char *path, struct got_pathlist_head *ignores,
2090 struct got_repository *repo,
2091 got_repo_import_cb progress_cb, void *progress_arg)
2093 const struct got_error *err;
2094 struct got_object_id *id = NULL;
2095 char *subdirpath;
2097 if (asprintf(&subdirpath, "%s%s%s", path,
2098 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2099 return got_error_from_errno("asprintf");
2101 (*new_te) = calloc(1, sizeof(**new_te));
2102 if (*new_te == NULL)
2103 return got_error_from_errno("calloc");
2104 (*new_te)->mode = S_IFDIR;
2105 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2106 sizeof((*new_te)->name)) {
2107 err = got_error(GOT_ERR_NO_SPACE);
2108 goto done;
2110 err = write_tree(&id, subdirpath, ignores, repo,
2111 progress_cb, progress_arg);
2112 if (err)
2113 goto done;
2114 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2116 done:
2117 free(id);
2118 free(subdirpath);
2119 if (err) {
2120 free(*new_te);
2121 *new_te = NULL;
2123 return err;
2126 static const struct got_error *
2127 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2128 struct got_pathlist_head *ignores, struct got_repository *repo,
2129 got_repo_import_cb progress_cb, void *progress_arg)
2131 const struct got_error *err = NULL;
2132 DIR *dir;
2133 struct dirent *de;
2134 int nentries;
2135 struct got_tree_entry *new_te = NULL;
2136 struct got_pathlist_head paths;
2137 struct got_pathlist_entry *pe;
2139 *new_tree_id = NULL;
2141 TAILQ_INIT(&paths);
2143 dir = opendir(path_dir);
2144 if (dir == NULL) {
2145 err = got_error_from_errno2("opendir", path_dir);
2146 goto done;
2149 nentries = 0;
2150 while ((de = readdir(dir)) != NULL) {
2151 int ignore = 0;
2152 int type;
2154 if (strcmp(de->d_name, ".") == 0 ||
2155 strcmp(de->d_name, "..") == 0)
2156 continue;
2158 err = got_path_dirent_type(&type, path_dir, de);
2159 if (err)
2160 goto done;
2162 TAILQ_FOREACH(pe, ignores, entry) {
2163 if (type == DT_DIR && pe->path_len > 0 &&
2164 pe->path[pe->path_len - 1] == '/') {
2165 char stripped[PATH_MAX];
2167 if (strlcpy(stripped, pe->path,
2168 sizeof(stripped)) >= sizeof(stripped)) {
2169 err = got_error(GOT_ERR_NO_SPACE);
2170 goto done;
2172 got_path_strip_trailing_slashes(stripped);
2173 if (fnmatch(stripped, de->d_name, 0) == 0) {
2174 ignore = 1;
2175 break;
2177 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2178 ignore = 1;
2179 break;
2182 if (ignore)
2183 continue;
2185 if (type == DT_DIR) {
2186 err = import_subdir(&new_te, de, path_dir,
2187 ignores, repo, progress_cb, progress_arg);
2188 if (err) {
2189 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2190 goto done;
2191 err = NULL;
2192 continue;
2194 } else if (type == DT_REG || type == DT_LNK) {
2195 err = import_file(&new_te, de, path_dir, repo);
2196 if (err)
2197 goto done;
2198 } else
2199 continue;
2201 err = insert_tree_entry(new_te, &paths);
2202 if (err)
2203 goto done;
2204 nentries++;
2207 if (TAILQ_EMPTY(&paths)) {
2208 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2209 "cannot create tree without any entries");
2210 goto done;
2213 TAILQ_FOREACH(pe, &paths, entry) {
2214 struct got_tree_entry *te = pe->data;
2215 char *path;
2216 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2217 continue;
2218 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2219 err = got_error_from_errno("asprintf");
2220 goto done;
2222 err = (*progress_cb)(progress_arg, path);
2223 free(path);
2224 if (err)
2225 goto done;
2228 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2229 done:
2230 if (dir)
2231 closedir(dir);
2232 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2233 return err;
2236 const struct got_error *
2237 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2238 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2239 struct got_repository *repo, got_repo_import_cb progress_cb,
2240 void *progress_arg)
2242 const struct got_error *err;
2243 struct got_object_id *new_tree_id;
2245 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2246 progress_cb, progress_arg);
2247 if (err)
2248 return err;
2250 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2251 author, time(NULL), author, time(NULL), logmsg, repo);
2252 free(new_tree_id);
2253 return err;
2256 const struct got_error *
2257 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2258 struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 char *path_objects = NULL, *path = NULL;
2262 DIR *dir = NULL;
2263 struct got_object_id id;
2264 int i;
2266 *nobjects = 0;
2267 *ondisk_size = 0;
2269 path_objects = got_repo_get_path_objects(repo);
2270 if (path_objects == NULL)
2271 return got_error_from_errno("got_repo_get_path_objects");
2273 for (i = 0; i <= 0xff; i++) {
2274 struct dirent *dent;
2276 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2277 err = got_error_from_errno("asprintf");
2278 break;
2281 dir = opendir(path);
2282 if (dir == NULL) {
2283 if (errno == ENOENT) {
2284 err = NULL;
2285 continue;
2287 err = got_error_from_errno2("opendir", path);
2288 break;
2291 while ((dent = readdir(dir)) != NULL) {
2292 char *id_str;
2293 int fd;
2294 struct stat sb;
2295 enum got_hash_algorithm algo = GOT_HASH_SHA1;
2297 if (strcmp(dent->d_name, ".") == 0 ||
2298 strcmp(dent->d_name, "..") == 0)
2299 continue;
2301 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2302 err = got_error_from_errno("asprintf");
2303 goto done;
2306 if (!got_parse_object_id(&id, id_str, algo)) {
2307 free(id_str);
2308 continue;
2310 free(id_str);
2312 err = got_object_open_loose_fd(&fd, &id, repo);
2313 if (err)
2314 goto done;
2316 if (fstat(fd, &sb) == -1) {
2317 err = got_error_from_errno("fstat");
2318 close(fd);
2319 goto done;
2321 (*nobjects)++;
2322 (*ondisk_size) += sb.st_size;
2324 if (close(fd) == -1) {
2325 err = got_error_from_errno("close");
2326 goto done;
2330 if (closedir(dir) != 0) {
2331 err = got_error_from_errno("closedir");
2332 goto done;
2334 dir = NULL;
2336 free(path);
2337 path = NULL;
2339 done:
2340 if (dir && closedir(dir) != 0 && err == NULL)
2341 err = got_error_from_errno("closedir");
2343 if (err) {
2344 *nobjects = 0;
2345 *ondisk_size = 0;
2347 free(path_objects);
2348 free(path);
2349 return err;
2352 const struct got_error *
2353 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2354 off_t *total_packsize, struct got_repository *repo)
2356 const struct got_error *err = NULL;
2357 DIR *packdir = NULL;
2358 struct dirent *dent;
2359 struct got_packidx *packidx = NULL;
2360 char *path_packidx;
2361 char *path_packfile;
2362 int packdir_fd;
2363 struct stat sb;
2365 *npackfiles = 0;
2366 *nobjects = 0;
2367 *total_packsize = 0;
2369 packdir_fd = openat(got_repo_get_fd(repo),
2370 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2371 if (packdir_fd == -1) {
2372 return got_error_from_errno_fmt("openat: %s/%s",
2373 got_repo_get_path_git_dir(repo),
2374 GOT_OBJECTS_PACK_DIR);
2377 packdir = fdopendir(packdir_fd);
2378 if (packdir == NULL) {
2379 err = got_error_from_errno("fdopendir");
2380 goto done;
2383 while ((dent = readdir(packdir)) != NULL) {
2384 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2385 continue;
2387 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2388 dent->d_name) == -1) {
2389 err = got_error_from_errno("asprintf");
2390 goto done;
2393 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2394 path_packidx, 0);
2395 free(path_packidx);
2396 if (err)
2397 goto done;
2399 if (fstat(packidx->fd, &sb) == -1)
2400 goto done;
2401 *total_packsize += sb.st_size;
2403 err = got_packidx_get_packfile_path(&path_packfile,
2404 packidx->path_packidx);
2405 if (err)
2406 goto done;
2408 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2409 0) == -1) {
2410 free(path_packfile);
2411 goto done;
2413 free(path_packfile);
2414 *total_packsize += sb.st_size;
2416 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2418 (*npackfiles)++;
2420 got_packidx_close(packidx);
2421 packidx = NULL;
2423 done:
2424 if (packidx)
2425 got_packidx_close(packidx);
2426 if (packdir && closedir(packdir) != 0 && err == NULL)
2427 err = got_error_from_errno("closedir");
2428 if (err) {
2429 *npackfiles = 0;
2430 *nobjects = 0;
2431 *total_packsize = 0;
2433 return err;
2436 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2437 got_packidx_bloom_filter_cmp);