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 const char *
79 got_repo_get_path(struct got_repository *repo)
80 {
81 return repo->path;
82 }
84 const char *
85 got_repo_get_path_git_dir(struct got_repository *repo)
86 {
87 return repo->path_git_dir;
88 }
90 int
91 got_repo_get_fd(struct got_repository *repo)
92 {
93 return repo->gitdir_fd;
94 }
96 enum got_hash_algorithm
97 got_repo_get_hash_algorithm(struct got_repository *repo)
98 {
99 return repo->algo;
102 const char *
103 got_repo_get_gitconfig_author_name(struct got_repository *repo)
105 return repo->gitconfig_author_name;
108 const char *
109 got_repo_get_gitconfig_author_email(struct got_repository *repo)
111 return repo->gitconfig_author_email;
114 const char *
115 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
117 return repo->global_gitconfig_author_name;
120 const char *
121 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
123 return repo->global_gitconfig_author_email;
126 const char *
127 got_repo_get_gitconfig_owner(struct got_repository *repo)
129 return repo->gitconfig_owner;
132 void
133 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
134 struct got_repository *repo)
136 *extensions = repo->extensions;
137 *nextensions = repo->nextensions;
140 int
141 got_repo_is_bare(struct got_repository *repo)
143 return (strcmp(repo->path, repo->path_git_dir) == 0);
146 static char *
147 get_path_git_child(struct got_repository *repo, const char *basename)
149 char *path_child;
151 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
152 basename) == -1)
153 return NULL;
155 return path_child;
158 char *
159 got_repo_get_path_objects(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_OBJECTS_DIR);
164 char *
165 got_repo_get_path_objects_pack(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
170 char *
171 got_repo_get_path_refs(struct got_repository *repo)
173 return get_path_git_child(repo, GOT_REFS_DIR);
176 char *
177 got_repo_get_path_packed_refs(struct got_repository *repo)
179 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
182 static char *
183 get_path_head(struct got_repository *repo)
185 return get_path_git_child(repo, GOT_HEAD_FILE);
188 char *
189 got_repo_get_path_gitconfig(struct got_repository *repo)
191 return get_path_git_child(repo, GOT_GITCONFIG);
194 char *
195 got_repo_get_path_gotconfig(struct got_repository *repo)
197 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
200 const struct got_gotconfig *
201 got_repo_get_gotconfig(struct got_repository *repo)
203 return repo->gotconfig;
206 void
207 got_repo_get_gitconfig_remotes(int *nremotes,
208 const struct got_remote_repo **remotes, struct got_repository *repo)
210 *nremotes = repo->ngitconfig_remotes;
211 *remotes = repo->gitconfig_remotes;
214 static int
215 is_git_repo(struct got_repository *repo)
217 const char *path_git = got_repo_get_path_git_dir(repo);
218 char *path_objects = got_repo_get_path_objects(repo);
219 char *path_refs = got_repo_get_path_refs(repo);
220 char *path_head = get_path_head(repo);
221 int ret = 0;
222 struct stat sb;
223 struct got_reference *head_ref;
225 if (lstat(path_git, &sb) == -1)
226 goto done;
227 if (!S_ISDIR(sb.st_mode))
228 goto done;
230 if (lstat(path_objects, &sb) == -1)
231 goto done;
232 if (!S_ISDIR(sb.st_mode))
233 goto done;
235 if (lstat(path_refs, &sb) == -1)
236 goto done;
237 if (!S_ISDIR(sb.st_mode))
238 goto done;
240 if (lstat(path_head, &sb) == -1)
241 goto done;
242 if (!S_ISREG(sb.st_mode))
243 goto done;
245 /* Check if the HEAD reference can be opened. */
246 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
247 goto done;
248 got_ref_close(head_ref);
250 ret = 1;
251 done:
252 free(path_objects);
253 free(path_refs);
254 free(path_head);
255 return ret;
259 static const struct got_error *
260 close_tempfiles(int *fds, size_t nfds)
262 const struct got_error *err = NULL;
263 int i;
265 for (i = 0; i < nfds; i++) {
266 if (fds[i] == -1)
267 continue;
268 if (close(fds[i]) == -1) {
269 err = got_error_from_errno("close");
270 break;
273 free(fds);
274 return err;
277 static const struct got_error *
278 open_tempfiles(int **fds, size_t array_size, size_t nfds)
280 const struct got_error *err = NULL;
281 int i;
283 *fds = calloc(array_size, sizeof(**fds));
284 if (*fds == NULL)
285 return got_error_from_errno("calloc");
287 for (i = 0; i < array_size; i++)
288 (*fds)[i] = -1;
290 for (i = 0; i < nfds; i++) {
291 (*fds)[i] = got_opentempfd();
292 if ((*fds)[i] == -1) {
293 err = got_error_from_errno("got_opentempfd");
294 close_tempfiles(*fds, nfds);
295 *fds = NULL;
296 return err;
300 return NULL;
303 static const struct got_error *
304 get_pack_cache_size(int *pack_cache_size)
306 struct rlimit rl;
308 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
309 return got_error_from_errno("getrlimit");
311 *pack_cache_size = GOT_PACK_CACHE_SIZE;
312 if (*pack_cache_size > rl.rlim_cur / 8)
313 *pack_cache_size = rl.rlim_cur / 8;
315 return NULL;
318 const struct got_error *
319 got_repo_pack_fds_open(int **pack_fds)
321 const struct got_error *err;
322 int nfds;
324 err = get_pack_cache_size(&nfds);
325 if (err)
326 return err;
328 /*
329 * We need one basefd and one accumfd per cached pack.
330 * Our constants should be set up in a way such that
331 * this error never triggers.
332 */
333 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
334 return got_error(GOT_ERR_NO_SPACE);
336 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
339 const struct got_error *
340 got_repo_pack_fds_close(int *pack_fds)
342 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
345 const struct got_error *
346 got_repo_temp_fds_open(int **temp_fds)
348 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
349 GOT_REPO_NUM_TEMPFILES);
352 void
353 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
355 int i;
357 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
358 repo->tempfiles[i] = temp_fds[i];
361 const struct got_error *
362 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
364 int i;
366 *fd = -1;
367 *idx = -1;
369 for (i = 0; i < nitems(repo->tempfiles); i++) {
370 if (repo->tempfile_use_mask & (1 << i))
371 continue;
372 if (repo->tempfiles[i] != -1) {
373 if (ftruncate(repo->tempfiles[i], 0L) == -1)
374 return got_error_from_errno("ftruncate");
375 *fd = repo->tempfiles[i];
376 *idx = i;
377 repo->tempfile_use_mask |= (1 << i);
378 return NULL;
382 return got_error(GOT_ERR_REPO_TEMPFILE);
385 void
386 got_repo_temp_fds_put(int idx, struct got_repository *repo)
388 repo->tempfile_use_mask &= ~(1 << idx);
391 const struct got_error *
392 got_repo_temp_fds_close(int *temp_fds)
394 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
397 const struct got_error *
398 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
399 struct got_object *obj)
401 #ifndef GOT_NO_OBJ_CACHE
402 const struct got_error *err = NULL;
403 err = got_object_cache_add(&repo->objcache, id, obj);
404 if (err) {
405 if (err->code == GOT_ERR_OBJ_EXISTS ||
406 err->code == GOT_ERR_OBJ_TOO_LARGE)
407 err = NULL;
408 return err;
410 obj->refcnt++;
411 #endif
412 return NULL;
415 struct got_object *
416 got_repo_get_cached_object(struct got_repository *repo,
417 struct got_object_id *id)
419 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
422 const struct got_error *
423 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
424 struct got_tree_object *tree)
426 #ifndef GOT_NO_OBJ_CACHE
427 const struct got_error *err = NULL;
428 err = got_object_cache_add(&repo->treecache, id, tree);
429 if (err) {
430 if (err->code == GOT_ERR_OBJ_EXISTS ||
431 err->code == GOT_ERR_OBJ_TOO_LARGE)
432 err = NULL;
433 return err;
435 tree->refcnt++;
436 #endif
437 return NULL;
440 struct got_tree_object *
441 got_repo_get_cached_tree(struct got_repository *repo,
442 struct got_object_id *id)
444 return (struct got_tree_object *)got_object_cache_get(
445 &repo->treecache, id);
448 const struct got_error *
449 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
450 struct got_commit_object *commit)
452 #ifndef GOT_NO_OBJ_CACHE
453 const struct got_error *err = NULL;
454 err = got_object_cache_add(&repo->commitcache, id, commit);
455 if (err) {
456 if (err->code == GOT_ERR_OBJ_EXISTS ||
457 err->code == GOT_ERR_OBJ_TOO_LARGE)
458 err = NULL;
459 return err;
461 commit->refcnt++;
462 #endif
463 return NULL;
466 struct got_commit_object *
467 got_repo_get_cached_commit(struct got_repository *repo,
468 struct got_object_id *id)
470 return (struct got_commit_object *)got_object_cache_get(
471 &repo->commitcache, id);
474 const struct got_error *
475 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
476 struct got_tag_object *tag)
478 #ifndef GOT_NO_OBJ_CACHE
479 const struct got_error *err = NULL;
480 err = got_object_cache_add(&repo->tagcache, id, tag);
481 if (err) {
482 if (err->code == GOT_ERR_OBJ_EXISTS ||
483 err->code == GOT_ERR_OBJ_TOO_LARGE)
484 err = NULL;
485 return err;
487 tag->refcnt++;
488 #endif
489 return NULL;
492 struct got_tag_object *
493 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
495 return (struct got_tag_object *)got_object_cache_get(
496 &repo->tagcache, id);
499 const struct got_error *
500 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
501 struct got_raw_object *raw)
503 #ifndef GOT_NO_OBJ_CACHE
504 const struct got_error *err = NULL;
505 err = got_object_cache_add(&repo->rawcache, id, raw);
506 if (err) {
507 if (err->code == GOT_ERR_OBJ_EXISTS ||
508 err->code == GOT_ERR_OBJ_TOO_LARGE)
509 err = NULL;
510 return err;
512 raw->refcnt++;
513 #endif
514 return NULL;
518 struct got_raw_object *
519 got_repo_get_cached_raw_object(struct got_repository *repo,
520 struct got_object_id *id)
522 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
526 static const struct got_error *
527 open_repo(struct got_repository *repo, const char *path)
529 const struct got_error *err = NULL;
531 repo->gitdir_fd = -1;
533 /* bare git repository? */
534 repo->path_git_dir = strdup(path);
535 if (repo->path_git_dir == NULL)
536 return got_error_from_errno("strdup");
537 if (is_git_repo(repo)) {
538 repo->path = strdup(repo->path_git_dir);
539 if (repo->path == NULL) {
540 err = got_error_from_errno("strdup");
541 goto done;
543 repo->gitdir_fd = open(repo->path_git_dir,
544 O_DIRECTORY | O_CLOEXEC);
545 if (repo->gitdir_fd == -1) {
546 err = got_error_from_errno2("open",
547 repo->path_git_dir);
548 goto done;
550 return NULL;
553 /* git repository with working tree? */
554 free(repo->path_git_dir);
555 repo->path_git_dir = NULL;
556 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
557 err = got_error_from_errno("asprintf");
558 goto done;
560 if (is_git_repo(repo)) {
561 repo->path = strdup(path);
562 if (repo->path == NULL) {
563 err = got_error_from_errno("strdup");
564 goto done;
566 repo->gitdir_fd = open(repo->path_git_dir,
567 O_DIRECTORY | O_CLOEXEC);
568 if (repo->gitdir_fd == -1) {
569 err = got_error_from_errno2("open",
570 repo->path_git_dir);
571 goto done;
573 return NULL;
576 err = got_error(GOT_ERR_NOT_GIT_REPO);
577 done:
578 if (err) {
579 free(repo->path);
580 repo->path = NULL;
581 free(repo->path_git_dir);
582 repo->path_git_dir = NULL;
583 if (repo->gitdir_fd != -1)
584 close(repo->gitdir_fd);
585 repo->gitdir_fd = -1;
588 return err;
591 static const struct got_error *
592 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
594 const struct got_error *err = NULL;
595 char *repo_gitconfig_path = NULL;
596 char *object_format = NULL;
598 if (global_gitconfig_path) {
599 /* Read settings from ~/.gitconfig. */
600 int dummy_repo_version;
601 err = got_repo_read_gitconfig(&dummy_repo_version,
602 &repo->global_gitconfig_author_name,
603 &repo->global_gitconfig_author_email,
604 NULL, NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
605 if (err)
606 return err;
609 /* Read repository's .git/config file. */
610 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
611 if (repo_gitconfig_path == NULL)
612 return got_error_from_errno("got_repo_get_path_gitconfig");
614 err = got_repo_read_gitconfig(
615 &repo->gitconfig_repository_format_version,
616 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
617 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
618 &repo->gitconfig_owner, &object_format,
619 &repo->extensions, &repo->nextensions,
620 repo_gitconfig_path);
621 if (err)
622 goto done;
624 if (!object_format)
625 repo->algo = GOT_HASH_SHA1;
626 else {
627 if (!strcmp(object_format, "sha256"))
628 repo->algo = GOT_HASH_SHA256;
629 else {
630 err = got_error_msg(GOT_ERR_OBJECT_FORMAT,
631 object_format);
632 goto done;
636 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
637 int i;
639 for (i = 0; i < repo->ngitconfig_remotes; i++) {
640 got_repo_free_remote_repo_data(
641 &repo->gitconfig_remotes[i]);
643 free(repo->gitconfig_remotes);
644 repo->gitconfig_remotes = NULL;
645 repo->ngitconfig_remotes = 0;
647 free(repo->gitconfig_author_name);
648 repo->gitconfig_author_name = NULL;
649 free(repo->gitconfig_author_email);
650 repo->gitconfig_author_email = NULL;
652 free(repo->global_gitconfig_author_name);
653 repo->global_gitconfig_author_name = NULL;
654 free(repo->global_gitconfig_author_email);
655 repo->global_gitconfig_author_email = NULL;
658 done:
659 free(object_format);
660 free(repo_gitconfig_path);
661 return err;
664 static const struct got_error *
665 read_gotconfig(struct got_repository *repo)
667 const struct got_error *err = NULL;
668 char *gotconfig_path;
670 gotconfig_path = got_repo_get_path_gotconfig(repo);
671 if (gotconfig_path == NULL)
672 return got_error_from_errno("got_repo_get_path_gotconfig");
674 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
675 free(gotconfig_path);
676 return err;
679 /* Supported repository format extensions. */
680 static const char *const repo_extensions[] = {
681 "noop", /* Got supports repository format version 1. */
682 "preciousObjects", /* Supported by gotadmin cleanup. */
683 "worktreeConfig", /* Got does not care about Git work trees. */
684 };
686 const struct got_error *
687 got_repo_open(struct got_repository **repop, const char *path,
688 const char *global_gitconfig_path, int *pack_fds)
690 struct got_repository *repo = NULL;
691 const struct got_error *err = NULL;
692 char *repo_path = NULL;
693 size_t i, j = 0;
695 *repop = NULL;
697 repo = calloc(1, sizeof(*repo));
698 if (repo == NULL)
699 return got_error_from_errno("calloc");
701 RB_INIT(&repo->packidx_bloom_filters);
702 TAILQ_INIT(&repo->packidx_paths);
704 for (i = 0; i < nitems(repo->privsep_children); i++) {
705 memset(&repo->privsep_children[i], 0,
706 sizeof(repo->privsep_children[0]));
707 repo->privsep_children[i].imsg_fd = -1;
710 err = got_object_cache_init(&repo->objcache,
711 GOT_OBJECT_CACHE_TYPE_OBJ);
712 if (err)
713 goto done;
714 err = got_object_cache_init(&repo->treecache,
715 GOT_OBJECT_CACHE_TYPE_TREE);
716 if (err)
717 goto done;
718 err = got_object_cache_init(&repo->commitcache,
719 GOT_OBJECT_CACHE_TYPE_COMMIT);
720 if (err)
721 goto done;
722 err = got_object_cache_init(&repo->tagcache,
723 GOT_OBJECT_CACHE_TYPE_TAG);
724 if (err)
725 goto done;
726 err = got_object_cache_init(&repo->rawcache,
727 GOT_OBJECT_CACHE_TYPE_RAW);
728 if (err)
729 goto done;
731 err = get_pack_cache_size(&repo->pack_cache_size);
732 if (err)
733 goto done;
734 for (i = 0; i < nitems(repo->packs); i++) {
735 if (pack_fds != NULL && i < repo->pack_cache_size) {
736 repo->packs[i].basefd = pack_fds[j++];
737 repo->packs[i].accumfd = pack_fds[j++];
738 } else {
739 repo->packs[i].basefd = -1;
740 repo->packs[i].accumfd = -1;
743 for (i = 0; i < nitems(repo->tempfiles); i++)
744 repo->tempfiles[i] = -1;
745 repo->pinned_pack = -1;
746 repo->pinned_packidx = -1;
747 repo->pinned_pid = 0;
749 repo_path = realpath(path, NULL);
750 if (repo_path == NULL) {
751 err = got_error_from_errno2("realpath", path);
752 goto done;
755 for (;;) {
756 char *parent_path;
758 err = open_repo(repo, repo_path);
759 if (err == NULL)
760 break;
761 if (err->code != GOT_ERR_NOT_GIT_REPO)
762 goto done;
763 if (repo_path[0] == '/' && repo_path[1] == '\0') {
764 err = got_error(GOT_ERR_NOT_GIT_REPO);
765 goto done;
767 err = got_path_dirname(&parent_path, repo_path);
768 if (err)
769 goto done;
770 free(repo_path);
771 repo_path = parent_path;
774 err = read_gotconfig(repo);
775 if (err)
776 goto done;
778 err = read_gitconfig(repo, global_gitconfig_path);
779 if (err)
780 goto done;
781 if (repo->gitconfig_repository_format_version > 1) {
782 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
783 goto done;
785 if (repo->gitconfig_repository_format_version == 0 &&
786 repo->algo != GOT_HASH_SHA1) {
787 err = got_error(GOT_ERR_OBJECT_FORMAT);
788 goto done;
790 for (i = 0; i < repo->nextensions; i++) {
791 char *ext = repo->extensions[i];
792 int j, supported = 0;
793 for (j = 0; j < nitems(repo_extensions); j++) {
794 if (strcmp(ext, repo_extensions[j]) == 0) {
795 supported = 1;
796 break;
799 if (!supported) {
800 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
801 goto done;
805 err = got_repo_list_packidx(&repo->packidx_paths, repo);
806 done:
807 if (err)
808 got_repo_close(repo);
809 else
810 *repop = repo;
811 free(repo_path);
812 return err;
815 const struct got_error *
816 got_repo_close(struct got_repository *repo)
818 const struct got_error *err = NULL, *child_err;
819 struct got_packidx_bloom_filter *bf;
820 size_t i;
822 for (i = 0; i < repo->pack_cache_size; i++) {
823 if (repo->packidx_cache[i] == NULL)
824 break;
825 got_packidx_close(repo->packidx_cache[i]);
828 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
829 &repo->packidx_bloom_filters))) {
830 RB_REMOVE(got_packidx_bloom_filter_tree,
831 &repo->packidx_bloom_filters, bf);
832 bloom_free(bf->bloom);
833 free(bf->bloom);
834 free(bf);
837 for (i = 0; i < repo->pack_cache_size; i++)
838 if (repo->packs[i].path_packfile)
839 if (repo->packs[i].path_packfile)
840 got_pack_close(&repo->packs[i]);
842 free(repo->path);
843 free(repo->path_git_dir);
845 got_object_cache_close(&repo->objcache);
846 got_object_cache_close(&repo->treecache);
847 got_object_cache_close(&repo->commitcache);
848 got_object_cache_close(&repo->tagcache);
849 got_object_cache_close(&repo->rawcache);
851 for (i = 0; i < nitems(repo->privsep_children); i++) {
852 if (repo->privsep_children[i].imsg_fd == -1)
853 continue;
854 imsg_clear(repo->privsep_children[i].ibuf);
855 free(repo->privsep_children[i].ibuf);
856 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
857 child_err = got_privsep_wait_for_child(
858 repo->privsep_children[i].pid);
859 if (child_err && err == NULL)
860 err = child_err;
861 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
862 err == NULL)
863 err = got_error_from_errno("close");
866 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
867 err == NULL)
868 err = got_error_from_errno("close");
870 if (repo->gotconfig)
871 got_gotconfig_free(repo->gotconfig);
872 free(repo->gitconfig_author_name);
873 free(repo->gitconfig_author_email);
874 for (i = 0; i < repo->ngitconfig_remotes; i++)
875 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
876 free(repo->gitconfig_remotes);
877 for (i = 0; i < repo->nextensions; i++)
878 free(repo->extensions[i]);
879 free(repo->extensions);
881 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
882 free(repo);
884 return err;
887 void
888 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
890 int i;
892 free(repo->name);
893 repo->name = NULL;
894 free(repo->fetch_url);
895 repo->fetch_url = NULL;
896 free(repo->send_url);
897 repo->send_url = NULL;
898 for (i = 0; i < repo->nfetch_branches; i++)
899 free(repo->fetch_branches[i]);
900 free(repo->fetch_branches);
901 repo->fetch_branches = NULL;
902 repo->nfetch_branches = 0;
903 for (i = 0; i < repo->nsend_branches; i++)
904 free(repo->send_branches[i]);
905 free(repo->send_branches);
906 repo->send_branches = NULL;
907 repo->nsend_branches = 0;
910 const struct got_error *
911 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
912 const char *input_path)
914 const struct got_error *err = NULL;
915 const char *repo_abspath = NULL;
916 size_t repolen, len;
917 char *canonpath, *path = NULL;
919 *in_repo_path = NULL;
921 canonpath = strdup(input_path);
922 if (canonpath == NULL) {
923 err = got_error_from_errno("strdup");
924 goto done;
926 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
927 if (err)
928 goto done;
930 repo_abspath = got_repo_get_path(repo);
932 if (canonpath[0] == '\0') {
933 path = strdup(canonpath);
934 if (path == NULL) {
935 err = got_error_from_errno("strdup");
936 goto done;
938 } else {
939 path = realpath(canonpath, NULL);
940 if (path == NULL) {
941 if (errno != ENOENT) {
942 err = got_error_from_errno2("realpath",
943 canonpath);
944 goto done;
946 /*
947 * Path is not on disk.
948 * Assume it is already relative to repository root.
949 */
950 path = strdup(canonpath);
951 if (path == NULL) {
952 err = got_error_from_errno("strdup");
953 goto done;
957 repolen = strlen(repo_abspath);
958 len = strlen(path);
961 if (strcmp(path, repo_abspath) == 0) {
962 free(path);
963 path = strdup("");
964 if (path == NULL) {
965 err = got_error_from_errno("strdup");
966 goto done;
968 } else if (len > repolen &&
969 got_path_is_child(path, repo_abspath, repolen)) {
970 /* Matched an on-disk path inside repository. */
971 if (got_repo_is_bare(repo)) {
972 /*
973 * Matched an on-disk path inside repository
974 * database. Treat input as repository-relative.
975 */
976 free(path);
977 path = canonpath;
978 canonpath = NULL;
979 } else {
980 char *child;
981 /* Strip common prefix with repository path. */
982 err = got_path_skip_common_ancestor(&child,
983 repo_abspath, path);
984 if (err)
985 goto done;
986 free(path);
987 path = child;
989 } else {
990 /*
991 * Matched unrelated on-disk path.
992 * Treat input as repository-relative.
993 */
994 free(path);
995 path = canonpath;
996 canonpath = NULL;
1000 /* Make in-repository path absolute */
1001 if (path[0] != '/') {
1002 char *abspath;
1003 if (asprintf(&abspath, "/%s", path) == -1) {
1004 err = got_error_from_errno("asprintf");
1005 goto done;
1007 free(path);
1008 path = abspath;
1011 done:
1012 free(canonpath);
1013 if (err)
1014 free(path);
1015 else
1016 *in_repo_path = path;
1017 return err;
1020 static const struct got_error *
1021 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1022 const char *path_packidx)
1024 const struct got_error *err = NULL;
1025 size_t i;
1027 for (i = 0; i < repo->pack_cache_size; i++) {
1028 if (repo->packidx_cache[i] == NULL)
1029 break;
1030 if (strcmp(repo->packidx_cache[i]->path_packidx,
1031 path_packidx) == 0) {
1032 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1035 if (i == repo->pack_cache_size) {
1036 do {
1037 i--;
1038 } while (i > 0 && repo->pinned_packidx >= 0 &&
1039 i == repo->pinned_packidx);
1040 err = got_packidx_close(repo->packidx_cache[i]);
1041 if (err)
1042 return err;
1045 repo->packidx_cache[i] = packidx;
1047 return NULL;
1050 int
1051 got_repo_is_packidx_filename(const char *name, size_t len)
1053 if (len != GOT_PACKIDX_NAMELEN)
1054 return 0;
1056 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1057 return 0;
1059 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1060 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1061 return 0;
1063 return 1;
1066 static struct got_packidx_bloom_filter *
1067 get_packidx_bloom_filter(struct got_repository *repo,
1068 const char *path, size_t path_len)
1070 struct got_packidx_bloom_filter key;
1072 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1073 return NULL; /* XXX */
1074 key.path_len = path_len;
1076 return RB_FIND(got_packidx_bloom_filter_tree,
1077 &repo->packidx_bloom_filters, &key);
1080 int
1081 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1082 const char *path_packidx, struct got_object_id *id)
1084 struct got_packidx_bloom_filter *bf;
1086 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1087 if (bf)
1088 return bloom_check(bf->bloom, id->hash, sizeof(id->hash));
1090 /* No bloom filter means this pack index must be searched. */
1091 return 1;
1094 static const struct got_error *
1095 add_packidx_bloom_filter(struct got_repository *repo,
1096 struct got_packidx *packidx, const char *path_packidx)
1098 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1099 struct got_packidx_bloom_filter *bf;
1100 size_t len;
1103 * Don't use bloom filters for very large pack index files.
1104 * Large pack files will contain a relatively large fraction
1105 * of our objects so we will likely need to visit them anyway.
1106 * The more objects a pack file contains the higher the probability
1107 * of a false-positive match from the bloom filter. And reading
1108 * all object IDs from a large pack index file can be expensive.
1110 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1111 return NULL;
1113 /* Do we already have a filter for this pack index? */
1114 if (get_packidx_bloom_filter(repo, path_packidx,
1115 strlen(path_packidx)) != NULL)
1116 return NULL;
1118 bf = calloc(1, sizeof(*bf));
1119 if (bf == NULL)
1120 return got_error_from_errno("calloc");
1121 bf->bloom = calloc(1, sizeof(*bf->bloom));
1122 if (bf->bloom == NULL) {
1123 free(bf);
1124 return got_error_from_errno("calloc");
1127 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1128 if (len >= sizeof(bf->path)) {
1129 free(bf->bloom);
1130 free(bf);
1131 return got_error(GOT_ERR_NO_SPACE);
1133 bf->path_len = len;
1135 /* Minimum size supported by our bloom filter is 1000 entries. */
1136 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1137 for (i = 0; i < nobjects; i++) {
1138 struct got_packidx_object_id *id;
1139 id = &packidx->hdr.sorted_ids[i];
1140 bloom_add(bf->bloom, id->hash, sizeof(id->hash));
1143 RB_INSERT(got_packidx_bloom_filter_tree,
1144 &repo->packidx_bloom_filters, bf);
1145 return NULL;
1148 static void
1149 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1151 struct got_pathlist_entry *pe;
1153 while (!TAILQ_EMPTY(packidx_paths)) {
1154 pe = TAILQ_FIRST(packidx_paths);
1155 TAILQ_REMOVE(packidx_paths, pe, entry);
1156 free((char *)pe->path);
1157 free(pe);
1161 static const struct got_error *
1162 refresh_packidx_paths(struct got_repository *repo)
1164 const struct got_error *err = NULL;
1165 char *objects_pack_dir = NULL;
1166 struct stat sb;
1168 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1169 if (objects_pack_dir == NULL)
1170 return got_error_from_errno("got_repo_get_path_objects_pack");
1172 if (stat(objects_pack_dir, &sb) == -1) {
1173 if (errno != ENOENT) {
1174 err = got_error_from_errno2("stat", objects_pack_dir);
1175 goto done;
1177 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1178 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1179 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1180 purge_packidx_paths(&repo->packidx_paths);
1181 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1182 if (err)
1183 goto done;
1185 done:
1186 free(objects_pack_dir);
1187 return err;
1190 const struct got_error *
1191 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1192 struct got_repository *repo, struct got_object_id *id)
1194 const struct got_error *err;
1195 struct got_pathlist_entry *pe;
1196 size_t i;
1198 /* Search pack index cache. */
1199 for (i = 0; i < repo->pack_cache_size; i++) {
1200 if (repo->packidx_cache[i] == NULL)
1201 break;
1202 if (!got_repo_check_packidx_bloom_filter(repo,
1203 repo->packidx_cache[i]->path_packidx, id))
1204 continue; /* object will not be found in this index */
1205 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1206 if (*idx != -1) {
1207 *packidx = repo->packidx_cache[i];
1209 * Move this cache entry to the front. Repeatedly
1210 * searching a wrong pack index can be expensive.
1212 if (i > 0) {
1213 memmove(&repo->packidx_cache[1],
1214 &repo->packidx_cache[0],
1215 i * sizeof(repo->packidx_cache[0]));
1216 repo->packidx_cache[0] = *packidx;
1217 if (repo->pinned_packidx >= 0 &&
1218 repo->pinned_packidx < i)
1219 repo->pinned_packidx++;
1220 else if (repo->pinned_packidx == i)
1221 repo->pinned_packidx = 0;
1223 return NULL;
1226 /* No luck. Search the filesystem. */
1228 err = refresh_packidx_paths(repo);
1229 if (err)
1230 return err;
1232 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1233 const char *path_packidx = pe->path;
1234 int is_cached = 0;
1236 if (!got_repo_check_packidx_bloom_filter(repo,
1237 pe->path, id))
1238 continue; /* object will not be found in this index */
1240 for (i = 0; i < repo->pack_cache_size; i++) {
1241 if (repo->packidx_cache[i] == NULL)
1242 break;
1243 if (strcmp(repo->packidx_cache[i]->path_packidx,
1244 path_packidx) == 0) {
1245 is_cached = 1;
1246 break;
1249 if (is_cached)
1250 continue; /* already searched */
1252 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1253 path_packidx, 0);
1254 if (err)
1255 goto done;
1257 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1258 if (err)
1259 goto done;
1261 err = cache_packidx(repo, *packidx, path_packidx);
1262 if (err)
1263 goto done;
1265 *idx = got_packidx_get_object_idx(*packidx, id);
1266 if (*idx != -1) {
1267 err = NULL; /* found the object */
1268 goto done;
1272 err = got_error_no_obj(id);
1273 done:
1274 return err;
1277 const struct got_error *
1278 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1279 struct got_repository *repo)
1281 const struct got_error *err = NULL;
1282 DIR *packdir = NULL;
1283 struct dirent *dent;
1284 char *path_packidx = NULL;
1285 int packdir_fd;
1286 struct stat sb;
1288 packdir_fd = openat(got_repo_get_fd(repo),
1289 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1290 if (packdir_fd == -1) {
1291 return got_error_from_errno_fmt("openat: %s/%s",
1292 got_repo_get_path_git_dir(repo),
1293 GOT_OBJECTS_PACK_DIR);
1296 packdir = fdopendir(packdir_fd);
1297 if (packdir == NULL) {
1298 err = got_error_from_errno("fdopendir");
1299 goto done;
1302 if (fstat(packdir_fd, &sb) == -1) {
1303 err = got_error_from_errno("fstat");
1304 goto done;
1306 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1307 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1309 while ((dent = readdir(packdir)) != NULL) {
1310 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1311 continue;
1313 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1314 dent->d_name) == -1) {
1315 err = got_error_from_errno("asprintf");
1316 path_packidx = NULL;
1317 break;
1320 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1321 if (err)
1322 break;
1324 done:
1325 if (err)
1326 free(path_packidx);
1327 if (packdir && closedir(packdir) != 0 && err == NULL)
1328 err = got_error_from_errno("closedir");
1329 return err;
1332 const struct got_error *
1333 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1334 struct got_repository *repo)
1336 const struct got_error *err;
1337 size_t i;
1339 *packidx = NULL;
1341 /* Search pack index cache. */
1342 for (i = 0; i < repo->pack_cache_size; i++) {
1343 if (repo->packidx_cache[i] == NULL)
1344 break;
1345 if (strcmp(repo->packidx_cache[i]->path_packidx,
1346 path_packidx) == 0) {
1347 *packidx = repo->packidx_cache[i];
1348 return NULL;
1351 /* No luck. Search the filesystem. */
1353 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1354 path_packidx, 0);
1355 if (err)
1356 return err;
1358 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1359 if (err)
1360 goto done;
1362 err = cache_packidx(repo, *packidx, path_packidx);
1363 done:
1364 if (err) {
1365 got_packidx_close(*packidx);
1366 *packidx = NULL;
1368 return err;
1371 static const struct got_error *
1372 read_packfile_hdr(int fd, struct got_packidx *packidx)
1374 const struct got_error *err = NULL;
1375 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1376 struct got_packfile_hdr hdr;
1377 ssize_t n;
1379 n = read(fd, &hdr, sizeof(hdr));
1380 if (n < 0)
1381 return got_error_from_errno("read");
1382 if (n != sizeof(hdr))
1383 return got_error(GOT_ERR_BAD_PACKFILE);
1385 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1386 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1387 be32toh(hdr.nobjects) != totobj)
1388 err = got_error(GOT_ERR_BAD_PACKFILE);
1390 return err;
1393 static const struct got_error *
1394 open_packfile(int *fd, struct got_repository *repo,
1395 const char *relpath, struct got_packidx *packidx)
1397 const struct got_error *err = NULL;
1399 *fd = openat(got_repo_get_fd(repo), relpath,
1400 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1401 if (*fd == -1)
1402 return got_error_from_errno_fmt("openat: %s/%s",
1403 got_repo_get_path_git_dir(repo), relpath);
1405 if (packidx) {
1406 err = read_packfile_hdr(*fd, packidx);
1407 if (err) {
1408 close(*fd);
1409 *fd = -1;
1413 return err;
1416 const struct got_error *
1417 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1418 const char *path_packfile, struct got_packidx *packidx)
1420 const struct got_error *err = NULL;
1421 struct got_pack *pack = NULL;
1422 struct stat sb;
1423 size_t i;
1425 if (packp)
1426 *packp = NULL;
1428 for (i = 0; i < repo->pack_cache_size; i++) {
1429 pack = &repo->packs[i];
1430 if (pack->path_packfile == NULL)
1431 break;
1432 if (strcmp(pack->path_packfile, path_packfile) == 0)
1433 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1436 if (i == repo->pack_cache_size) {
1437 struct got_pack tmp;
1438 do {
1439 i--;
1440 } while (i > 0 && repo->pinned_pack >= 0 &&
1441 i == repo->pinned_pack);
1442 err = got_pack_close(&repo->packs[i]);
1443 if (err)
1444 return err;
1445 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1446 return got_error_from_errno("ftruncate");
1447 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1448 return got_error_from_errno("ftruncate");
1449 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1450 memcpy(&repo->packs[i], &repo->packs[0],
1451 sizeof(repo->packs[i]));
1452 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1453 if (repo->pinned_pack == 0)
1454 repo->pinned_pack = i;
1455 else if (repo->pinned_pack == i)
1456 repo->pinned_pack = 0;
1457 i = 0;
1460 pack = &repo->packs[i];
1462 pack->path_packfile = strdup(path_packfile);
1463 if (pack->path_packfile == NULL) {
1464 err = got_error_from_errno("strdup");
1465 goto done;
1468 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1469 if (err)
1470 goto done;
1472 if (fstat(pack->fd, &sb) != 0) {
1473 err = got_error_from_errno("fstat");
1474 goto done;
1476 pack->filesize = sb.st_size;
1478 pack->privsep_child = NULL;
1480 err = got_delta_cache_alloc(&pack->delta_cache);
1481 if (err)
1482 goto done;
1484 #ifndef GOT_PACK_NO_MMAP
1485 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1486 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1487 pack->fd, 0);
1488 if (pack->map == MAP_FAILED) {
1489 if (errno != ENOMEM) {
1490 err = got_error_from_errno("mmap");
1491 goto done;
1493 pack->map = NULL; /* fall back to read(2) */
1496 #endif
1497 done:
1498 if (err) {
1499 if (pack)
1500 got_pack_close(pack);
1501 } else if (packp)
1502 *packp = pack;
1503 return err;
1506 struct got_pack *
1507 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1509 struct got_pack *pack = NULL;
1510 size_t i;
1512 for (i = 0; i < repo->pack_cache_size; i++) {
1513 pack = &repo->packs[i];
1514 if (pack->path_packfile == NULL)
1515 break;
1516 if (strcmp(pack->path_packfile, path_packfile) == 0)
1517 return pack;
1520 return NULL;
1523 const struct got_error *
1524 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1525 struct got_pack *pack)
1527 size_t i;
1528 int pinned_pack = -1, pinned_packidx = -1;
1530 for (i = 0; i < repo->pack_cache_size; i++) {
1531 if (repo->packidx_cache[i] &&
1532 strcmp(repo->packidx_cache[i]->path_packidx,
1533 packidx->path_packidx) == 0)
1534 pinned_packidx = i;
1535 if (repo->packs[i].path_packfile &&
1536 strcmp(repo->packs[i].path_packfile,
1537 pack->path_packfile) == 0)
1538 pinned_pack = i;
1541 if (pinned_packidx == -1 || pinned_pack == -1)
1542 return got_error(GOT_ERR_PIN_PACK);
1544 repo->pinned_pack = pinned_pack;
1545 repo->pinned_packidx = pinned_packidx;
1546 if (repo->packs[pinned_pack].privsep_child)
1547 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1548 return NULL;
1551 struct got_pack *
1552 got_repo_get_pinned_pack(struct got_repository *repo)
1554 if (repo->pinned_pack >= 0 &&
1555 repo->pinned_pack < repo->pack_cache_size)
1556 return &repo->packs[repo->pinned_pack];
1558 return NULL;
1561 void
1562 got_repo_unpin_pack(struct got_repository *repo)
1564 repo->pinned_packidx = -1;
1565 repo->pinned_pack = -1;
1566 repo->pinned_pid = 0;
1569 const struct got_error *
1570 got_repo_init(const char *repo_path, const char *head_name)
1572 const struct got_error *err = NULL;
1573 const char *dirnames[] = {
1574 GOT_OBJECTS_DIR,
1575 GOT_OBJECTS_PACK_DIR,
1576 GOT_REFS_DIR,
1578 const char *description_str = "Unnamed repository; "
1579 "edit this file 'description' to name the repository.";
1580 const char *headref = "ref: refs/heads/";
1581 const char *gitconfig_str = "[core]\n"
1582 "\trepositoryformatversion = 0\n"
1583 "\tfilemode = true\n"
1584 "\tbare = true\n";
1585 char *headref_str, *path;
1586 size_t i;
1588 if (!got_path_dir_is_empty(repo_path))
1589 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1591 for (i = 0; i < nitems(dirnames); i++) {
1592 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1593 return got_error_from_errno("asprintf");
1595 err = got_path_mkdir(path);
1596 free(path);
1597 if (err)
1598 return err;
1601 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1602 return got_error_from_errno("asprintf");
1603 err = got_path_create_file(path, description_str);
1604 free(path);
1605 if (err)
1606 return err;
1608 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1609 return got_error_from_errno("asprintf");
1610 if (asprintf(&headref_str, "%s%s", headref,
1611 head_name ? head_name : "main") == -1) {
1612 free(path);
1613 return got_error_from_errno("asprintf");
1615 err = got_path_create_file(path, headref_str);
1616 free(headref_str);
1617 free(path);
1618 if (err)
1619 return err;
1621 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1622 return got_error_from_errno("asprintf");
1623 err = got_path_create_file(path, gitconfig_str);
1624 free(path);
1625 if (err)
1626 return err;
1628 return NULL;
1631 static const struct got_error *
1632 match_packed_object(struct got_object_id **unique_id,
1633 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1635 const struct got_error *err = NULL;
1636 struct got_object_id_queue matched_ids;
1637 struct got_pathlist_entry *pe;
1639 STAILQ_INIT(&matched_ids);
1641 err = refresh_packidx_paths(repo);
1642 if (err)
1643 return err;
1645 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1646 const char *path_packidx = pe->path;
1647 struct got_packidx *packidx;
1648 struct got_object_qid *qid;
1650 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1651 path_packidx, 0);
1652 if (err)
1653 break;
1655 err = got_packidx_match_id_str_prefix(&matched_ids,
1656 packidx, id_str_prefix);
1657 if (err) {
1658 got_packidx_close(packidx);
1659 break;
1661 err = got_packidx_close(packidx);
1662 if (err)
1663 break;
1665 STAILQ_FOREACH(qid, &matched_ids, entry) {
1666 if (obj_type != GOT_OBJ_TYPE_ANY) {
1667 int matched_type;
1668 err = got_object_get_type(&matched_type, repo,
1669 &qid->id);
1670 if (err)
1671 goto done;
1672 if (matched_type != obj_type)
1673 continue;
1675 if (*unique_id == NULL) {
1676 *unique_id = got_object_id_dup(&qid->id);
1677 if (*unique_id == NULL) {
1678 err = got_error_from_errno("malloc");
1679 goto done;
1681 } else {
1682 if (got_object_id_cmp(*unique_id,
1683 &qid->id) == 0)
1684 continue; /* packed multiple times */
1685 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1686 goto done;
1690 done:
1691 got_object_id_queue_free(&matched_ids);
1692 if (err) {
1693 free(*unique_id);
1694 *unique_id = NULL;
1696 return err;
1699 static const struct got_error *
1700 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1701 const char *object_dir, const char *id_str_prefix, int obj_type,
1702 struct got_repository *repo)
1704 const struct got_error *err = NULL;
1705 char *path, *id_str = NULL;
1706 DIR *dir = NULL;
1707 struct dirent *dent;
1708 struct got_object_id id;
1710 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1711 err = got_error_from_errno("asprintf");
1712 goto done;
1715 dir = opendir(path);
1716 if (dir == NULL) {
1717 if (errno == ENOENT) {
1718 err = NULL;
1719 goto done;
1721 err = got_error_from_errno2("opendir", path);
1722 goto done;
1724 while ((dent = readdir(dir)) != NULL) {
1725 int cmp;
1727 free(id_str);
1728 id_str = NULL;
1730 if (strcmp(dent->d_name, ".") == 0 ||
1731 strcmp(dent->d_name, "..") == 0)
1732 continue;
1734 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1735 err = got_error_from_errno("asprintf");
1736 goto done;
1739 if (!got_parse_sha1_digest(id.hash, id_str))
1740 continue;
1743 * Directory entries do not necessarily appear in
1744 * sorted order, so we must iterate over all of them.
1746 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1747 if (cmp != 0)
1748 continue;
1750 if (*unique_id == NULL) {
1751 if (obj_type != GOT_OBJ_TYPE_ANY) {
1752 int matched_type;
1753 err = got_object_get_type(&matched_type, repo,
1754 &id);
1755 if (err)
1756 goto done;
1757 if (matched_type != obj_type)
1758 continue;
1760 *unique_id = got_object_id_dup(&id);
1761 if (*unique_id == NULL) {
1762 err = got_error_from_errno("got_object_id_dup");
1763 goto done;
1765 } else {
1766 if (got_object_id_cmp(*unique_id, &id) == 0)
1767 continue; /* both packed and loose */
1768 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1769 goto done;
1772 done:
1773 if (dir && closedir(dir) != 0 && err == NULL)
1774 err = got_error_from_errno("closedir");
1775 if (err) {
1776 free(*unique_id);
1777 *unique_id = NULL;
1779 free(id_str);
1780 free(path);
1781 return err;
1784 const struct got_error *
1785 got_repo_match_object_id_prefix(struct got_object_id **id,
1786 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1788 const struct got_error *err = NULL;
1789 char *path_objects = NULL, *object_dir = NULL;
1790 size_t len;
1791 int i;
1793 *id = NULL;
1795 path_objects = got_repo_get_path_objects(repo);
1797 len = strlen(id_str_prefix);
1798 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1799 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1800 goto done;
1803 for (i = 0; i < len; i++) {
1804 if (isxdigit((unsigned char)id_str_prefix[i]))
1805 continue;
1806 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1807 goto done;
1810 if (len >= 2) {
1811 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1812 if (err)
1813 goto done;
1814 object_dir = strndup(id_str_prefix, 2);
1815 if (object_dir == NULL) {
1816 err = got_error_from_errno("strdup");
1817 goto done;
1819 err = match_loose_object(id, path_objects, object_dir,
1820 id_str_prefix, obj_type, repo);
1821 } else if (len == 1) {
1822 int i;
1823 for (i = 0; i < 0xf; i++) {
1824 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1825 == -1) {
1826 err = got_error_from_errno("asprintf");
1827 goto done;
1829 err = match_packed_object(id, repo, object_dir,
1830 obj_type);
1831 if (err)
1832 goto done;
1833 err = match_loose_object(id, path_objects, object_dir,
1834 id_str_prefix, obj_type, repo);
1835 if (err)
1836 goto done;
1838 } else {
1839 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1840 goto done;
1842 done:
1843 free(path_objects);
1844 free(object_dir);
1845 if (err) {
1846 free(*id);
1847 *id = NULL;
1848 } else if (*id == NULL) {
1849 switch (obj_type) {
1850 case GOT_OBJ_TYPE_BLOB:
1851 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1852 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1853 break;
1854 case GOT_OBJ_TYPE_TREE:
1855 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1856 GOT_OBJ_LABEL_TREE, id_str_prefix);
1857 break;
1858 case GOT_OBJ_TYPE_COMMIT:
1859 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1860 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1861 break;
1862 case GOT_OBJ_TYPE_TAG:
1863 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1864 GOT_OBJ_LABEL_TAG, id_str_prefix);
1865 break;
1866 default:
1867 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1868 break;
1872 return err;
1875 const struct got_error *
1876 got_repo_match_object_id(struct got_object_id **id, char **label,
1877 const char *id_str, int obj_type, struct got_reflist_head *refs,
1878 struct got_repository *repo)
1880 const struct got_error *err;
1881 struct got_tag_object *tag;
1882 struct got_reference *ref = NULL;
1884 *id = NULL;
1885 if (label)
1886 *label = NULL;
1888 if (refs) {
1889 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1890 refs, repo);
1891 if (err == NULL) {
1892 *id = got_object_id_dup(
1893 got_object_tag_get_object_id(tag));
1894 if (*id == NULL)
1895 err = got_error_from_errno("got_object_id_dup");
1896 else if (label && asprintf(label, "refs/tags/%s",
1897 got_object_tag_get_name(tag)) == -1) {
1898 err = got_error_from_errno("asprintf");
1899 free(*id);
1900 *id = NULL;
1902 got_object_tag_close(tag);
1903 return err;
1904 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1905 err->code != GOT_ERR_NO_OBJ)
1906 return err;
1909 err = got_ref_open(&ref, repo, id_str, 0);
1910 if (err == NULL) {
1911 err = got_ref_resolve(id, repo, ref);
1912 if (err)
1913 goto done;
1914 if (label) {
1915 *label = strdup(got_ref_get_name(ref));
1916 if (*label == NULL) {
1917 err = got_error_from_errno("strdup");
1918 goto done;
1921 } else {
1922 if (err->code != GOT_ERR_NOT_REF &&
1923 err->code != GOT_ERR_BAD_REF_NAME)
1924 goto done;
1925 err = got_repo_match_object_id_prefix(id, id_str,
1926 obj_type, repo);
1927 if (err) {
1928 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1929 err = got_error_not_ref(id_str);
1930 goto done;
1932 if (label) {
1933 err = got_object_id_str(label, *id);
1934 if (*label == NULL) {
1935 err = got_error_from_errno("strdup");
1936 goto done;
1940 done:
1941 if (ref)
1942 got_ref_close(ref);
1943 return err;
1946 const struct got_error *
1947 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1948 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1950 const struct got_error *err = NULL;
1951 struct got_reflist_entry *re;
1952 struct got_object_id *tag_id;
1953 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1955 *tag = NULL;
1957 TAILQ_FOREACH(re, refs, entry) {
1958 const char *refname;
1959 refname = got_ref_get_name(re->ref);
1960 if (got_ref_is_symbolic(re->ref))
1961 continue;
1962 if (strncmp(refname, "refs/tags/", 10) != 0)
1963 continue;
1964 if (!name_is_absolute)
1965 refname += strlen("refs/tags/");
1966 if (strcmp(refname, name) != 0)
1967 continue;
1968 err = got_ref_resolve(&tag_id, repo, re->ref);
1969 if (err)
1970 break;
1971 err = got_object_open_as_tag(tag, repo, tag_id);
1972 free(tag_id);
1973 if (err)
1974 break;
1975 if (obj_type == GOT_OBJ_TYPE_ANY ||
1976 got_object_tag_get_object_type(*tag) == obj_type)
1977 break;
1978 got_object_tag_close(*tag);
1979 *tag = NULL;
1982 if (err == NULL && *tag == NULL)
1983 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1984 GOT_OBJ_LABEL_TAG, name);
1985 return err;
1988 static const struct got_error *
1989 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1990 const char *name, mode_t mode, struct got_object_id *blob_id)
1992 const struct got_error *err = NULL;
1994 *new_te = NULL;
1996 *new_te = calloc(1, sizeof(**new_te));
1997 if (*new_te == NULL)
1998 return got_error_from_errno("calloc");
2000 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2001 sizeof((*new_te)->name)) {
2002 err = got_error(GOT_ERR_NO_SPACE);
2003 goto done;
2006 if (S_ISLNK(mode)) {
2007 (*new_te)->mode = S_IFLNK;
2008 } else {
2009 (*new_te)->mode = S_IFREG;
2010 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2012 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2013 done:
2014 if (err && *new_te) {
2015 free(*new_te);
2016 *new_te = NULL;
2018 return err;
2021 static const struct got_error *
2022 import_file(struct got_tree_entry **new_te, struct dirent *de,
2023 const char *path, struct got_repository *repo)
2025 const struct got_error *err;
2026 struct got_object_id *blob_id = NULL;
2027 char *filepath;
2028 struct stat sb;
2030 if (asprintf(&filepath, "%s%s%s", path,
2031 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2032 return got_error_from_errno("asprintf");
2034 if (lstat(filepath, &sb) != 0) {
2035 err = got_error_from_errno2("lstat", path);
2036 goto done;
2039 err = got_object_blob_create(&blob_id, filepath, repo);
2040 if (err)
2041 goto done;
2043 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2044 blob_id);
2045 done:
2046 free(filepath);
2047 if (err)
2048 free(blob_id);
2049 return err;
2052 static const struct got_error *
2053 insert_tree_entry(struct got_tree_entry *new_te,
2054 struct got_pathlist_head *paths)
2056 const struct got_error *err = NULL;
2057 struct got_pathlist_entry *new_pe;
2059 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2060 if (err)
2061 return err;
2062 if (new_pe == NULL)
2063 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2064 return NULL;
2067 static const struct got_error *write_tree(struct got_object_id **,
2068 const char *, struct got_pathlist_head *, struct got_repository *,
2069 got_repo_import_cb progress_cb, void *progress_arg);
2071 static const struct got_error *
2072 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2073 const char *path, struct got_pathlist_head *ignores,
2074 struct got_repository *repo,
2075 got_repo_import_cb progress_cb, void *progress_arg)
2077 const struct got_error *err;
2078 struct got_object_id *id = NULL;
2079 char *subdirpath;
2081 if (asprintf(&subdirpath, "%s%s%s", path,
2082 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2083 return got_error_from_errno("asprintf");
2085 (*new_te) = calloc(1, sizeof(**new_te));
2086 if (*new_te == NULL)
2087 return got_error_from_errno("calloc");
2088 (*new_te)->mode = S_IFDIR;
2089 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2090 sizeof((*new_te)->name)) {
2091 err = got_error(GOT_ERR_NO_SPACE);
2092 goto done;
2094 err = write_tree(&id, subdirpath, ignores, repo,
2095 progress_cb, progress_arg);
2096 if (err)
2097 goto done;
2098 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2100 done:
2101 free(id);
2102 free(subdirpath);
2103 if (err) {
2104 free(*new_te);
2105 *new_te = NULL;
2107 return err;
2110 static const struct got_error *
2111 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2112 struct got_pathlist_head *ignores, struct got_repository *repo,
2113 got_repo_import_cb progress_cb, void *progress_arg)
2115 const struct got_error *err = NULL;
2116 DIR *dir;
2117 struct dirent *de;
2118 int nentries;
2119 struct got_tree_entry *new_te = NULL;
2120 struct got_pathlist_head paths;
2121 struct got_pathlist_entry *pe;
2123 *new_tree_id = NULL;
2125 TAILQ_INIT(&paths);
2127 dir = opendir(path_dir);
2128 if (dir == NULL) {
2129 err = got_error_from_errno2("opendir", path_dir);
2130 goto done;
2133 nentries = 0;
2134 while ((de = readdir(dir)) != NULL) {
2135 int ignore = 0;
2136 int type;
2138 if (strcmp(de->d_name, ".") == 0 ||
2139 strcmp(de->d_name, "..") == 0)
2140 continue;
2142 TAILQ_FOREACH(pe, ignores, entry) {
2143 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2144 ignore = 1;
2145 break;
2148 if (ignore)
2149 continue;
2151 err = got_path_dirent_type(&type, path_dir, de);
2152 if (err)
2153 goto done;
2155 if (type == DT_DIR) {
2156 err = import_subdir(&new_te, de, path_dir,
2157 ignores, repo, progress_cb, progress_arg);
2158 if (err) {
2159 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2160 goto done;
2161 err = NULL;
2162 continue;
2164 } else if (type == DT_REG || type == DT_LNK) {
2165 err = import_file(&new_te, de, path_dir, repo);
2166 if (err)
2167 goto done;
2168 } else
2169 continue;
2171 err = insert_tree_entry(new_te, &paths);
2172 if (err)
2173 goto done;
2174 nentries++;
2177 if (TAILQ_EMPTY(&paths)) {
2178 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2179 "cannot create tree without any entries");
2180 goto done;
2183 TAILQ_FOREACH(pe, &paths, entry) {
2184 struct got_tree_entry *te = pe->data;
2185 char *path;
2186 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2187 continue;
2188 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2189 err = got_error_from_errno("asprintf");
2190 goto done;
2192 err = (*progress_cb)(progress_arg, path);
2193 free(path);
2194 if (err)
2195 goto done;
2198 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2199 done:
2200 if (dir)
2201 closedir(dir);
2202 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2203 return err;
2206 const struct got_error *
2207 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2208 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2209 struct got_repository *repo, got_repo_import_cb progress_cb,
2210 void *progress_arg)
2212 const struct got_error *err;
2213 struct got_object_id *new_tree_id;
2215 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2216 progress_cb, progress_arg);
2217 if (err)
2218 return err;
2220 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2221 author, time(NULL), author, time(NULL), logmsg, repo);
2222 free(new_tree_id);
2223 return err;
2226 const struct got_error *
2227 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2228 struct got_repository *repo)
2230 const struct got_error *err = NULL;
2231 char *path_objects = NULL, *path = NULL;
2232 DIR *dir = NULL;
2233 struct got_object_id id;
2234 int i;
2236 *nobjects = 0;
2237 *ondisk_size = 0;
2239 path_objects = got_repo_get_path_objects(repo);
2240 if (path_objects == NULL)
2241 return got_error_from_errno("got_repo_get_path_objects");
2243 for (i = 0; i <= 0xff; i++) {
2244 struct dirent *dent;
2246 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2247 err = got_error_from_errno("asprintf");
2248 break;
2251 dir = opendir(path);
2252 if (dir == NULL) {
2253 if (errno == ENOENT) {
2254 err = NULL;
2255 continue;
2257 err = got_error_from_errno2("opendir", path);
2258 break;
2261 while ((dent = readdir(dir)) != NULL) {
2262 char *id_str;
2263 int fd;
2264 struct stat sb;
2266 if (strcmp(dent->d_name, ".") == 0 ||
2267 strcmp(dent->d_name, "..") == 0)
2268 continue;
2270 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2271 err = got_error_from_errno("asprintf");
2272 goto done;
2275 if (!got_parse_sha1_digest(id.hash, id_str)) {
2276 free(id_str);
2277 continue;
2279 free(id_str);
2281 err = got_object_open_loose_fd(&fd, &id, repo);
2282 if (err)
2283 goto done;
2285 if (fstat(fd, &sb) == -1) {
2286 err = got_error_from_errno("fstat");
2287 close(fd);
2288 goto done;
2290 (*nobjects)++;
2291 (*ondisk_size) += sb.st_size;
2293 if (close(fd) == -1) {
2294 err = got_error_from_errno("close");
2295 goto done;
2299 if (closedir(dir) != 0) {
2300 err = got_error_from_errno("closedir");
2301 goto done;
2303 dir = NULL;
2305 free(path);
2306 path = NULL;
2308 done:
2309 if (dir && closedir(dir) != 0 && err == NULL)
2310 err = got_error_from_errno("closedir");
2312 if (err) {
2313 *nobjects = 0;
2314 *ondisk_size = 0;
2316 free(path_objects);
2317 free(path);
2318 return err;
2321 const struct got_error *
2322 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2323 off_t *total_packsize, struct got_repository *repo)
2325 const struct got_error *err = NULL;
2326 DIR *packdir = NULL;
2327 struct dirent *dent;
2328 struct got_packidx *packidx = NULL;
2329 char *path_packidx;
2330 char *path_packfile;
2331 int packdir_fd;
2332 struct stat sb;
2334 *npackfiles = 0;
2335 *nobjects = 0;
2336 *total_packsize = 0;
2338 packdir_fd = openat(got_repo_get_fd(repo),
2339 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2340 if (packdir_fd == -1) {
2341 return got_error_from_errno_fmt("openat: %s/%s",
2342 got_repo_get_path_git_dir(repo),
2343 GOT_OBJECTS_PACK_DIR);
2346 packdir = fdopendir(packdir_fd);
2347 if (packdir == NULL) {
2348 err = got_error_from_errno("fdopendir");
2349 goto done;
2352 while ((dent = readdir(packdir)) != NULL) {
2353 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2354 continue;
2356 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2357 dent->d_name) == -1) {
2358 err = got_error_from_errno("asprintf");
2359 goto done;
2362 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2363 path_packidx, 0);
2364 free(path_packidx);
2365 if (err)
2366 goto done;
2368 if (fstat(packidx->fd, &sb) == -1)
2369 goto done;
2370 *total_packsize += sb.st_size;
2372 err = got_packidx_get_packfile_path(&path_packfile,
2373 packidx->path_packidx);
2374 if (err)
2375 goto done;
2377 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2378 0) == -1) {
2379 free(path_packfile);
2380 goto done;
2382 free(path_packfile);
2383 *total_packsize += sb.st_size;
2385 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2387 (*npackfiles)++;
2389 got_packidx_close(packidx);
2390 packidx = NULL;
2392 done:
2393 if (packidx)
2394 got_packidx_close(packidx);
2395 if (packdir && closedir(packdir) != 0 && err == NULL)
2396 err = got_error_from_errno("closedir");
2397 if (err) {
2398 *npackfiles = 0;
2399 *nobjects = 0;
2400 *total_packsize = 0;
2402 return err;
2405 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2406 got_packidx_bloom_filter_cmp);