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(repo_gitconfig_path);
660 return err;
663 static const struct got_error *
664 read_gotconfig(struct got_repository *repo)
666 const struct got_error *err = NULL;
667 char *gotconfig_path;
669 gotconfig_path = got_repo_get_path_gotconfig(repo);
670 if (gotconfig_path == NULL)
671 return got_error_from_errno("got_repo_get_path_gotconfig");
673 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
674 free(gotconfig_path);
675 return err;
678 /* Supported repository format extensions. */
679 static const char *const repo_extensions[] = {
680 "noop", /* Got supports repository format version 1. */
681 "preciousObjects", /* Supported by gotadmin cleanup. */
682 "worktreeConfig", /* Got does not care about Git work trees. */
683 "sha256", /* hack */
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(struct got_repository *repo, const char *name,
1052 size_t len)
1054 size_t hashlen = SHA1_DIGEST_STRING_LENGTH;
1056 if (repo->algo == GOT_HASH_SHA256)
1057 hashlen = SHA256_DIGEST_STRING_LENGTH;
1059 if (len != GOT_PACKIDX_NAMELEN)
1060 return 0;
1062 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1063 return 0;
1065 if (strcmp(name + strlen(GOT_PACK_PREFIX) + hashlen - 1,
1066 GOT_PACKIDX_SUFFIX) != 0)
1067 return 0;
1069 return 1;
1072 static struct got_packidx_bloom_filter *
1073 get_packidx_bloom_filter(struct got_repository *repo,
1074 const char *path, size_t path_len)
1076 struct got_packidx_bloom_filter key;
1078 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1079 return NULL; /* XXX */
1080 key.path_len = path_len;
1082 return RB_FIND(got_packidx_bloom_filter_tree,
1083 &repo->packidx_bloom_filters, &key);
1086 int
1087 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1088 const char *path_packidx, struct got_object_id *id)
1090 struct got_packidx_bloom_filter *bf;
1092 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1093 if (bf)
1094 return bloom_check(bf->bloom, id->hash, sizeof(id->hash));
1096 /* No bloom filter means this pack index must be searched. */
1097 return 1;
1100 static const struct got_error *
1101 add_packidx_bloom_filter(struct got_repository *repo,
1102 struct got_packidx *packidx, const char *path_packidx)
1104 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1105 struct got_packidx_bloom_filter *bf;
1106 size_t len;
1109 * Don't use bloom filters for very large pack index files.
1110 * Large pack files will contain a relatively large fraction
1111 * of our objects so we will likely need to visit them anyway.
1112 * The more objects a pack file contains the higher the probability
1113 * of a false-positive match from the bloom filter. And reading
1114 * all object IDs from a large pack index file can be expensive.
1116 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1117 return NULL;
1119 /* Do we already have a filter for this pack index? */
1120 if (get_packidx_bloom_filter(repo, path_packidx,
1121 strlen(path_packidx)) != NULL)
1122 return NULL;
1124 bf = calloc(1, sizeof(*bf));
1125 if (bf == NULL)
1126 return got_error_from_errno("calloc");
1127 bf->bloom = calloc(1, sizeof(*bf->bloom));
1128 if (bf->bloom == NULL) {
1129 free(bf);
1130 return got_error_from_errno("calloc");
1133 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1134 if (len >= sizeof(bf->path)) {
1135 free(bf->bloom);
1136 free(bf);
1137 return got_error(GOT_ERR_NO_SPACE);
1139 bf->path_len = len;
1141 /* Minimum size supported by our bloom filter is 1000 entries. */
1142 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1143 for (i = 0; i < nobjects; i++) {
1144 struct got_packidx_object_id *id;
1145 id = &packidx->hdr.sorted_ids[i];
1146 bloom_add(bf->bloom, id->hash, sizeof(id->hash));
1149 RB_INSERT(got_packidx_bloom_filter_tree,
1150 &repo->packidx_bloom_filters, bf);
1151 return NULL;
1154 static void
1155 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1157 struct got_pathlist_entry *pe;
1159 while (!TAILQ_EMPTY(packidx_paths)) {
1160 pe = TAILQ_FIRST(packidx_paths);
1161 TAILQ_REMOVE(packidx_paths, pe, entry);
1162 free((char *)pe->path);
1163 free(pe);
1167 static const struct got_error *
1168 refresh_packidx_paths(struct got_repository *repo)
1170 const struct got_error *err = NULL;
1171 char *objects_pack_dir = NULL;
1172 struct stat sb;
1174 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1175 if (objects_pack_dir == NULL)
1176 return got_error_from_errno("got_repo_get_path_objects_pack");
1178 if (stat(objects_pack_dir, &sb) == -1) {
1179 if (errno != ENOENT) {
1180 err = got_error_from_errno2("stat", objects_pack_dir);
1181 goto done;
1183 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1184 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1185 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1186 purge_packidx_paths(&repo->packidx_paths);
1187 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1188 if (err)
1189 goto done;
1191 done:
1192 free(objects_pack_dir);
1193 return err;
1196 const struct got_error *
1197 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1198 struct got_repository *repo, struct got_object_id *id)
1200 const struct got_error *err;
1201 struct got_pathlist_entry *pe;
1202 size_t i;
1204 /* Search pack index cache. */
1205 for (i = 0; i < repo->pack_cache_size; i++) {
1206 if (repo->packidx_cache[i] == NULL)
1207 break;
1208 if (!got_repo_check_packidx_bloom_filter(repo,
1209 repo->packidx_cache[i]->path_packidx, id))
1210 continue; /* object will not be found in this index */
1211 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1212 if (*idx != -1) {
1213 *packidx = repo->packidx_cache[i];
1215 * Move this cache entry to the front. Repeatedly
1216 * searching a wrong pack index can be expensive.
1218 if (i > 0) {
1219 memmove(&repo->packidx_cache[1],
1220 &repo->packidx_cache[0],
1221 i * sizeof(repo->packidx_cache[0]));
1222 repo->packidx_cache[0] = *packidx;
1223 if (repo->pinned_packidx >= 0 &&
1224 repo->pinned_packidx < i)
1225 repo->pinned_packidx++;
1226 else if (repo->pinned_packidx == i)
1227 repo->pinned_packidx = 0;
1229 return NULL;
1232 /* No luck. Search the filesystem. */
1234 err = refresh_packidx_paths(repo);
1235 if (err)
1236 return err;
1238 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1239 const char *path_packidx = pe->path;
1240 int is_cached = 0;
1242 if (!got_repo_check_packidx_bloom_filter(repo,
1243 pe->path, id))
1244 continue; /* object will not be found in this index */
1246 for (i = 0; i < repo->pack_cache_size; i++) {
1247 if (repo->packidx_cache[i] == NULL)
1248 break;
1249 if (strcmp(repo->packidx_cache[i]->path_packidx,
1250 path_packidx) == 0) {
1251 is_cached = 1;
1252 break;
1255 if (is_cached)
1256 continue; /* already searched */
1258 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1259 path_packidx, 0, repo->algo);
1260 if (err)
1261 goto done;
1263 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1264 if (err)
1265 goto done;
1267 err = cache_packidx(repo, *packidx, path_packidx);
1268 if (err)
1269 goto done;
1271 *idx = got_packidx_get_object_idx(*packidx, id);
1272 if (*idx != -1) {
1273 err = NULL; /* found the object */
1274 goto done;
1278 err = got_error_no_obj(id);
1279 done:
1280 return err;
1283 const struct got_error *
1284 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1285 struct got_repository *repo)
1287 const struct got_error *err = NULL;
1288 DIR *packdir = NULL;
1289 struct dirent *dent;
1290 char *path_packidx = NULL;
1291 int packdir_fd;
1292 struct stat sb;
1294 packdir_fd = openat(got_repo_get_fd(repo),
1295 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1296 if (packdir_fd == -1) {
1297 return got_error_from_errno_fmt("openat: %s/%s",
1298 got_repo_get_path_git_dir(repo),
1299 GOT_OBJECTS_PACK_DIR);
1302 packdir = fdopendir(packdir_fd);
1303 if (packdir == NULL) {
1304 err = got_error_from_errno("fdopendir");
1305 goto done;
1308 if (fstat(packdir_fd, &sb) == -1) {
1309 err = got_error_from_errno("fstat");
1310 goto done;
1312 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1313 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1315 while ((dent = readdir(packdir)) != NULL) {
1316 if (!got_repo_is_packidx_filename(repo, dent->d_name,
1317 dent->d_namlen))
1318 continue;
1320 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1321 dent->d_name) == -1) {
1322 err = got_error_from_errno("asprintf");
1323 path_packidx = NULL;
1324 break;
1327 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1328 if (err)
1329 break;
1331 done:
1332 if (err)
1333 free(path_packidx);
1334 if (packdir && closedir(packdir) != 0 && err == NULL)
1335 err = got_error_from_errno("closedir");
1336 return err;
1339 const struct got_error *
1340 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1341 struct got_repository *repo)
1343 const struct got_error *err;
1344 size_t i;
1346 *packidx = NULL;
1348 /* Search pack index cache. */
1349 for (i = 0; i < repo->pack_cache_size; i++) {
1350 if (repo->packidx_cache[i] == NULL)
1351 break;
1352 if (strcmp(repo->packidx_cache[i]->path_packidx,
1353 path_packidx) == 0) {
1354 *packidx = repo->packidx_cache[i];
1355 return NULL;
1358 /* No luck. Search the filesystem. */
1360 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1361 path_packidx, 0, repo->algo);
1362 if (err)
1363 return err;
1365 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1366 if (err)
1367 goto done;
1369 err = cache_packidx(repo, *packidx, path_packidx);
1370 done:
1371 if (err) {
1372 got_packidx_close(*packidx);
1373 *packidx = NULL;
1375 return err;
1378 static const struct got_error *
1379 read_packfile_hdr(int fd, struct got_packidx *packidx)
1381 const struct got_error *err = NULL;
1382 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1383 struct got_packfile_hdr hdr;
1384 ssize_t n;
1386 n = read(fd, &hdr, sizeof(hdr));
1387 if (n < 0)
1388 return got_error_from_errno("read");
1389 if (n != sizeof(hdr))
1390 return got_error(GOT_ERR_BAD_PACKFILE);
1392 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1393 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1394 be32toh(hdr.nobjects) != totobj)
1395 err = got_error(GOT_ERR_BAD_PACKFILE);
1397 return err;
1400 static const struct got_error *
1401 open_packfile(int *fd, struct got_repository *repo,
1402 const char *relpath, struct got_packidx *packidx)
1404 const struct got_error *err = NULL;
1406 *fd = openat(got_repo_get_fd(repo), relpath,
1407 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1408 if (*fd == -1)
1409 return got_error_from_errno_fmt("openat: %s/%s",
1410 got_repo_get_path_git_dir(repo), relpath);
1412 if (packidx) {
1413 err = read_packfile_hdr(*fd, packidx);
1414 if (err) {
1415 close(*fd);
1416 *fd = -1;
1420 return err;
1423 const struct got_error *
1424 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1425 const char *path_packfile, struct got_packidx *packidx)
1427 const struct got_error *err = NULL;
1428 struct got_pack *pack = NULL;
1429 struct stat sb;
1430 size_t i;
1432 if (packp)
1433 *packp = NULL;
1435 for (i = 0; i < repo->pack_cache_size; i++) {
1436 pack = &repo->packs[i];
1437 if (pack->path_packfile == NULL)
1438 break;
1439 if (strcmp(pack->path_packfile, path_packfile) == 0)
1440 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1443 if (i == repo->pack_cache_size) {
1444 struct got_pack tmp;
1445 do {
1446 i--;
1447 } while (i > 0 && repo->pinned_pack >= 0 &&
1448 i == repo->pinned_pack);
1449 err = got_pack_close(&repo->packs[i]);
1450 if (err)
1451 return err;
1452 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1453 return got_error_from_errno("ftruncate");
1454 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1455 return got_error_from_errno("ftruncate");
1456 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1457 memcpy(&repo->packs[i], &repo->packs[0],
1458 sizeof(repo->packs[i]));
1459 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1460 if (repo->pinned_pack == 0)
1461 repo->pinned_pack = i;
1462 else if (repo->pinned_pack == i)
1463 repo->pinned_pack = 0;
1464 i = 0;
1467 pack = &repo->packs[i];
1469 pack->path_packfile = strdup(path_packfile);
1470 if (pack->path_packfile == NULL) {
1471 err = got_error_from_errno("strdup");
1472 goto done;
1475 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1476 if (err)
1477 goto done;
1479 if (fstat(pack->fd, &sb) != 0) {
1480 err = got_error_from_errno("fstat");
1481 goto done;
1483 pack->filesize = sb.st_size;
1485 pack->privsep_child = NULL;
1487 err = got_delta_cache_alloc(&pack->delta_cache);
1488 if (err)
1489 goto done;
1491 #ifndef GOT_PACK_NO_MMAP
1492 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1493 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1494 pack->fd, 0);
1495 if (pack->map == MAP_FAILED) {
1496 if (errno != ENOMEM) {
1497 err = got_error_from_errno("mmap");
1498 goto done;
1500 pack->map = NULL; /* fall back to read(2) */
1503 #endif
1504 done:
1505 if (err) {
1506 if (pack)
1507 got_pack_close(pack);
1508 } else if (packp)
1509 *packp = pack;
1510 return err;
1513 struct got_pack *
1514 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1516 struct got_pack *pack = NULL;
1517 size_t i;
1519 for (i = 0; i < repo->pack_cache_size; i++) {
1520 pack = &repo->packs[i];
1521 if (pack->path_packfile == NULL)
1522 break;
1523 if (strcmp(pack->path_packfile, path_packfile) == 0)
1524 return pack;
1527 return NULL;
1530 const struct got_error *
1531 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1532 struct got_pack *pack)
1534 size_t i;
1535 int pinned_pack = -1, pinned_packidx = -1;
1537 for (i = 0; i < repo->pack_cache_size; i++) {
1538 if (repo->packidx_cache[i] &&
1539 strcmp(repo->packidx_cache[i]->path_packidx,
1540 packidx->path_packidx) == 0)
1541 pinned_packidx = i;
1542 if (repo->packs[i].path_packfile &&
1543 strcmp(repo->packs[i].path_packfile,
1544 pack->path_packfile) == 0)
1545 pinned_pack = i;
1548 if (pinned_packidx == -1 || pinned_pack == -1)
1549 return got_error(GOT_ERR_PIN_PACK);
1551 repo->pinned_pack = pinned_pack;
1552 repo->pinned_packidx = pinned_packidx;
1553 if (repo->packs[pinned_pack].privsep_child)
1554 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1555 return NULL;
1558 struct got_pack *
1559 got_repo_get_pinned_pack(struct got_repository *repo)
1561 if (repo->pinned_pack >= 0 &&
1562 repo->pinned_pack < repo->pack_cache_size)
1563 return &repo->packs[repo->pinned_pack];
1565 return NULL;
1568 void
1569 got_repo_unpin_pack(struct got_repository *repo)
1571 repo->pinned_packidx = -1;
1572 repo->pinned_pack = -1;
1573 repo->pinned_pid = 0;
1576 const struct got_error *
1577 got_repo_init(const char *repo_path, const char *head_name)
1579 const struct got_error *err = NULL;
1580 const char *dirnames[] = {
1581 GOT_OBJECTS_DIR,
1582 GOT_OBJECTS_PACK_DIR,
1583 GOT_REFS_DIR,
1585 const char *description_str = "Unnamed repository; "
1586 "edit this file 'description' to name the repository.";
1587 const char *headref = "ref: refs/heads/";
1588 const char *gitconfig_str = "[core]\n"
1589 "\trepositoryformatversion = 0\n"
1590 "\tfilemode = true\n"
1591 "\tbare = true\n";
1592 char *headref_str, *path;
1593 size_t i;
1595 if (!got_path_dir_is_empty(repo_path))
1596 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1598 for (i = 0; i < nitems(dirnames); i++) {
1599 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1600 return got_error_from_errno("asprintf");
1602 err = got_path_mkdir(path);
1603 free(path);
1604 if (err)
1605 return err;
1608 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1609 return got_error_from_errno("asprintf");
1610 err = got_path_create_file(path, description_str);
1611 free(path);
1612 if (err)
1613 return err;
1615 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1616 return got_error_from_errno("asprintf");
1617 if (asprintf(&headref_str, "%s%s", headref,
1618 head_name ? head_name : "main") == -1) {
1619 free(path);
1620 return got_error_from_errno("asprintf");
1622 err = got_path_create_file(path, headref_str);
1623 free(headref_str);
1624 free(path);
1625 if (err)
1626 return err;
1628 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1629 return got_error_from_errno("asprintf");
1630 err = got_path_create_file(path, gitconfig_str);
1631 free(path);
1632 if (err)
1633 return err;
1635 return NULL;
1638 static const struct got_error *
1639 match_packed_object(struct got_object_id **unique_id,
1640 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1642 const struct got_error *err = NULL;
1643 struct got_object_id_queue matched_ids;
1644 struct got_pathlist_entry *pe;
1646 STAILQ_INIT(&matched_ids);
1648 err = refresh_packidx_paths(repo);
1649 if (err)
1650 return err;
1652 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1653 const char *path_packidx = pe->path;
1654 struct got_packidx *packidx;
1655 struct got_object_qid *qid;
1657 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1658 path_packidx, 0, repo->algo);
1659 if (err)
1660 break;
1662 err = got_packidx_match_id_str_prefix(&matched_ids,
1663 packidx, id_str_prefix);
1664 if (err) {
1665 got_packidx_close(packidx);
1666 break;
1668 err = got_packidx_close(packidx);
1669 if (err)
1670 break;
1672 STAILQ_FOREACH(qid, &matched_ids, entry) {
1673 if (obj_type != GOT_OBJ_TYPE_ANY) {
1674 int matched_type;
1675 err = got_object_get_type(&matched_type, repo,
1676 &qid->id);
1677 if (err)
1678 goto done;
1679 if (matched_type != obj_type)
1680 continue;
1682 if (*unique_id == NULL) {
1683 *unique_id = got_object_id_dup(&qid->id);
1684 if (*unique_id == NULL) {
1685 err = got_error_from_errno("malloc");
1686 goto done;
1688 } else {
1689 if (got_object_id_cmp(*unique_id,
1690 &qid->id) == 0)
1691 continue; /* packed multiple times */
1692 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1693 goto done;
1697 done:
1698 got_object_id_queue_free(&matched_ids);
1699 if (err) {
1700 free(*unique_id);
1701 *unique_id = NULL;
1703 return err;
1706 static const struct got_error *
1707 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1708 const char *object_dir, const char *id_str_prefix, int obj_type,
1709 struct got_repository *repo)
1711 const struct got_error *err = NULL;
1712 char *path, *id_str = NULL;
1713 DIR *dir = NULL;
1714 struct dirent *dent;
1715 struct got_object_id id;
1717 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1718 err = got_error_from_errno("asprintf");
1719 goto done;
1722 dir = opendir(path);
1723 if (dir == NULL) {
1724 if (errno == ENOENT) {
1725 err = NULL;
1726 goto done;
1728 err = got_error_from_errno2("opendir", path);
1729 goto done;
1731 while ((dent = readdir(dir)) != NULL) {
1732 int cmp;
1734 free(id_str);
1735 id_str = NULL;
1737 if (strcmp(dent->d_name, ".") == 0 ||
1738 strcmp(dent->d_name, "..") == 0)
1739 continue;
1741 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1742 err = got_error_from_errno("asprintf");
1743 goto done;
1746 id.algo = repo->algo;
1747 if (!got_parse_hash_digest(id.hash, id_str, repo->algo, NULL))
1748 continue;
1751 * Directory entries do not necessarily appear in
1752 * sorted order, so we must iterate over all of them.
1754 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1755 if (cmp != 0)
1756 continue;
1758 if (*unique_id == NULL) {
1759 if (obj_type != GOT_OBJ_TYPE_ANY) {
1760 int matched_type;
1761 err = got_object_get_type(&matched_type, repo,
1762 &id);
1763 if (err)
1764 goto done;
1765 if (matched_type != obj_type)
1766 continue;
1768 *unique_id = got_object_id_dup(&id);
1769 if (*unique_id == NULL) {
1770 err = got_error_from_errno("got_object_id_dup");
1771 goto done;
1773 } else {
1774 if (got_object_id_cmp(*unique_id, &id) == 0)
1775 continue; /* both packed and loose */
1776 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1777 goto done;
1780 done:
1781 if (dir && closedir(dir) != 0 && err == NULL)
1782 err = got_error_from_errno("closedir");
1783 if (err) {
1784 free(*unique_id);
1785 *unique_id = NULL;
1787 free(id_str);
1788 free(path);
1789 return err;
1792 const struct got_error *
1793 got_repo_match_object_id_prefix(struct got_object_id **id,
1794 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1796 const struct got_error *err = NULL;
1797 char *path_objects = NULL, *object_dir = NULL;
1798 size_t len;
1799 int i;
1801 *id = NULL;
1803 path_objects = got_repo_get_path_objects(repo);
1805 len = strlen(id_str_prefix);
1806 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1807 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1808 goto done;
1811 for (i = 0; i < len; i++) {
1812 if (isxdigit((unsigned char)id_str_prefix[i]))
1813 continue;
1814 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1815 goto done;
1818 if (len >= 2) {
1819 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1820 if (err)
1821 goto done;
1822 object_dir = strndup(id_str_prefix, 2);
1823 if (object_dir == NULL) {
1824 err = got_error_from_errno("strdup");
1825 goto done;
1827 err = match_loose_object(id, path_objects, object_dir,
1828 id_str_prefix, obj_type, repo);
1829 } else if (len == 1) {
1830 int i;
1831 for (i = 0; i < 0xf; i++) {
1832 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1833 == -1) {
1834 err = got_error_from_errno("asprintf");
1835 goto done;
1837 err = match_packed_object(id, repo, object_dir,
1838 obj_type);
1839 if (err)
1840 goto done;
1841 err = match_loose_object(id, path_objects, object_dir,
1842 id_str_prefix, obj_type, repo);
1843 if (err)
1844 goto done;
1846 } else {
1847 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1848 goto done;
1850 done:
1851 free(path_objects);
1852 free(object_dir);
1853 if (err) {
1854 free(*id);
1855 *id = NULL;
1856 } else if (*id == NULL) {
1857 switch (obj_type) {
1858 case GOT_OBJ_TYPE_BLOB:
1859 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1860 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1861 break;
1862 case GOT_OBJ_TYPE_TREE:
1863 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1864 GOT_OBJ_LABEL_TREE, id_str_prefix);
1865 break;
1866 case GOT_OBJ_TYPE_COMMIT:
1867 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1868 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1869 break;
1870 case GOT_OBJ_TYPE_TAG:
1871 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1872 GOT_OBJ_LABEL_TAG, id_str_prefix);
1873 break;
1874 default:
1875 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1876 break;
1880 return err;
1883 const struct got_error *
1884 got_repo_match_object_id(struct got_object_id **id, char **label,
1885 const char *id_str, int obj_type, struct got_reflist_head *refs,
1886 struct got_repository *repo)
1888 const struct got_error *err;
1889 struct got_tag_object *tag;
1890 struct got_reference *ref = NULL;
1892 *id = NULL;
1893 if (label)
1894 *label = NULL;
1896 if (refs) {
1897 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1898 refs, repo);
1899 if (err == NULL) {
1900 *id = got_object_id_dup(
1901 got_object_tag_get_object_id(tag));
1902 if (*id == NULL)
1903 err = got_error_from_errno("got_object_id_dup");
1904 else if (label && asprintf(label, "refs/tags/%s",
1905 got_object_tag_get_name(tag)) == -1) {
1906 err = got_error_from_errno("asprintf");
1907 free(*id);
1908 *id = NULL;
1910 got_object_tag_close(tag);
1911 return err;
1912 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1913 err->code != GOT_ERR_NO_OBJ)
1914 return err;
1917 err = got_ref_open(&ref, repo, id_str, 0);
1918 if (err == NULL) {
1919 err = got_ref_resolve(id, repo, ref);
1920 if (err)
1921 goto done;
1922 if (label) {
1923 *label = strdup(got_ref_get_name(ref));
1924 if (*label == NULL) {
1925 err = got_error_from_errno("strdup");
1926 goto done;
1929 } else {
1930 if (err->code != GOT_ERR_NOT_REF &&
1931 err->code != GOT_ERR_BAD_REF_NAME)
1932 goto done;
1933 err = got_repo_match_object_id_prefix(id, id_str,
1934 obj_type, repo);
1935 if (err) {
1936 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1937 err = got_error_not_ref(id_str);
1938 goto done;
1940 if (label) {
1941 err = got_object_id_str(label, *id);
1942 if (*label == NULL) {
1943 err = got_error_from_errno("strdup");
1944 goto done;
1948 done:
1949 if (ref)
1950 got_ref_close(ref);
1951 return err;
1954 const struct got_error *
1955 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1956 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1958 const struct got_error *err = NULL;
1959 struct got_reflist_entry *re;
1960 struct got_object_id *tag_id;
1961 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1963 *tag = NULL;
1965 TAILQ_FOREACH(re, refs, entry) {
1966 const char *refname;
1967 refname = got_ref_get_name(re->ref);
1968 if (got_ref_is_symbolic(re->ref))
1969 continue;
1970 if (strncmp(refname, "refs/tags/", 10) != 0)
1971 continue;
1972 if (!name_is_absolute)
1973 refname += strlen("refs/tags/");
1974 if (strcmp(refname, name) != 0)
1975 continue;
1976 err = got_ref_resolve(&tag_id, repo, re->ref);
1977 if (err)
1978 break;
1979 err = got_object_open_as_tag(tag, repo, tag_id);
1980 free(tag_id);
1981 if (err)
1982 break;
1983 if (obj_type == GOT_OBJ_TYPE_ANY ||
1984 got_object_tag_get_object_type(*tag) == obj_type)
1985 break;
1986 got_object_tag_close(*tag);
1987 *tag = NULL;
1990 if (err == NULL && *tag == NULL)
1991 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1992 GOT_OBJ_LABEL_TAG, name);
1993 return err;
1996 static const struct got_error *
1997 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1998 const char *name, mode_t mode, struct got_object_id *blob_id)
2000 const struct got_error *err = NULL;
2002 *new_te = NULL;
2004 *new_te = calloc(1, sizeof(**new_te));
2005 if (*new_te == NULL)
2006 return got_error_from_errno("calloc");
2008 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2009 sizeof((*new_te)->name)) {
2010 err = got_error(GOT_ERR_NO_SPACE);
2011 goto done;
2014 if (S_ISLNK(mode)) {
2015 (*new_te)->mode = S_IFLNK;
2016 } else {
2017 (*new_te)->mode = S_IFREG;
2018 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2020 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2021 done:
2022 if (err && *new_te) {
2023 free(*new_te);
2024 *new_te = NULL;
2026 return err;
2029 static const struct got_error *
2030 import_file(struct got_tree_entry **new_te, struct dirent *de,
2031 const char *path, struct got_repository *repo)
2033 const struct got_error *err;
2034 struct got_object_id *blob_id = NULL;
2035 char *filepath;
2036 struct stat sb;
2038 if (asprintf(&filepath, "%s%s%s", path,
2039 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2040 return got_error_from_errno("asprintf");
2042 if (lstat(filepath, &sb) != 0) {
2043 err = got_error_from_errno2("lstat", path);
2044 goto done;
2047 err = got_object_blob_create(&blob_id, filepath, repo);
2048 if (err)
2049 goto done;
2051 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2052 blob_id);
2053 done:
2054 free(filepath);
2055 if (err)
2056 free(blob_id);
2057 return err;
2060 static const struct got_error *
2061 insert_tree_entry(struct got_tree_entry *new_te,
2062 struct got_pathlist_head *paths)
2064 const struct got_error *err = NULL;
2065 struct got_pathlist_entry *new_pe;
2067 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2068 if (err)
2069 return err;
2070 if (new_pe == NULL)
2071 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2072 return NULL;
2075 static const struct got_error *write_tree(struct got_object_id **,
2076 const char *, struct got_pathlist_head *, struct got_repository *,
2077 got_repo_import_cb progress_cb, void *progress_arg);
2079 static const struct got_error *
2080 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2081 const char *path, struct got_pathlist_head *ignores,
2082 struct got_repository *repo,
2083 got_repo_import_cb progress_cb, void *progress_arg)
2085 const struct got_error *err;
2086 struct got_object_id *id = NULL;
2087 char *subdirpath;
2089 if (asprintf(&subdirpath, "%s%s%s", path,
2090 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2091 return got_error_from_errno("asprintf");
2093 (*new_te) = calloc(1, sizeof(**new_te));
2094 if (*new_te == NULL)
2095 return got_error_from_errno("calloc");
2096 (*new_te)->mode = S_IFDIR;
2097 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2098 sizeof((*new_te)->name)) {
2099 err = got_error(GOT_ERR_NO_SPACE);
2100 goto done;
2102 err = write_tree(&id, subdirpath, ignores, repo,
2103 progress_cb, progress_arg);
2104 if (err)
2105 goto done;
2106 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2108 done:
2109 free(id);
2110 free(subdirpath);
2111 if (err) {
2112 free(*new_te);
2113 *new_te = NULL;
2115 return err;
2118 static const struct got_error *
2119 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2120 struct got_pathlist_head *ignores, struct got_repository *repo,
2121 got_repo_import_cb progress_cb, void *progress_arg)
2123 const struct got_error *err = NULL;
2124 DIR *dir;
2125 struct dirent *de;
2126 int nentries;
2127 struct got_tree_entry *new_te = NULL;
2128 struct got_pathlist_head paths;
2129 struct got_pathlist_entry *pe;
2131 *new_tree_id = NULL;
2133 TAILQ_INIT(&paths);
2135 dir = opendir(path_dir);
2136 if (dir == NULL) {
2137 err = got_error_from_errno2("opendir", path_dir);
2138 goto done;
2141 nentries = 0;
2142 while ((de = readdir(dir)) != NULL) {
2143 int ignore = 0;
2144 int type;
2146 if (strcmp(de->d_name, ".") == 0 ||
2147 strcmp(de->d_name, "..") == 0)
2148 continue;
2150 TAILQ_FOREACH(pe, ignores, entry) {
2151 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2152 ignore = 1;
2153 break;
2156 if (ignore)
2157 continue;
2159 err = got_path_dirent_type(&type, path_dir, de);
2160 if (err)
2161 goto done;
2163 if (type == DT_DIR) {
2164 err = import_subdir(&new_te, de, path_dir,
2165 ignores, repo, progress_cb, progress_arg);
2166 if (err) {
2167 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2168 goto done;
2169 err = NULL;
2170 continue;
2172 } else if (type == DT_REG || type == DT_LNK) {
2173 err = import_file(&new_te, de, path_dir, repo);
2174 if (err)
2175 goto done;
2176 } else
2177 continue;
2179 err = insert_tree_entry(new_te, &paths);
2180 if (err)
2181 goto done;
2182 nentries++;
2185 if (TAILQ_EMPTY(&paths)) {
2186 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2187 "cannot create tree without any entries");
2188 goto done;
2191 TAILQ_FOREACH(pe, &paths, entry) {
2192 struct got_tree_entry *te = pe->data;
2193 char *path;
2194 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2195 continue;
2196 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2197 err = got_error_from_errno("asprintf");
2198 goto done;
2200 err = (*progress_cb)(progress_arg, path);
2201 free(path);
2202 if (err)
2203 goto done;
2206 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2207 done:
2208 if (dir)
2209 closedir(dir);
2210 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2211 return err;
2214 const struct got_error *
2215 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2216 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2217 struct got_repository *repo, got_repo_import_cb progress_cb,
2218 void *progress_arg)
2220 const struct got_error *err;
2221 struct got_object_id *new_tree_id;
2223 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2224 progress_cb, progress_arg);
2225 if (err)
2226 return err;
2228 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2229 author, time(NULL), author, time(NULL), logmsg, repo);
2230 free(new_tree_id);
2231 return err;
2234 const struct got_error *
2235 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2236 struct got_repository *repo)
2238 const struct got_error *err = NULL;
2239 char *path_objects = NULL, *path = NULL;
2240 DIR *dir = NULL;
2241 struct got_object_id id;
2242 int i;
2244 *nobjects = 0;
2245 *ondisk_size = 0;
2247 path_objects = got_repo_get_path_objects(repo);
2248 if (path_objects == NULL)
2249 return got_error_from_errno("got_repo_get_path_objects");
2251 for (i = 0; i <= 0xff; i++) {
2252 struct dirent *dent;
2254 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2255 err = got_error_from_errno("asprintf");
2256 break;
2259 dir = opendir(path);
2260 if (dir == NULL) {
2261 if (errno == ENOENT) {
2262 err = NULL;
2263 continue;
2265 err = got_error_from_errno2("opendir", path);
2266 break;
2269 while ((dent = readdir(dir)) != NULL) {
2270 char *id_str;
2271 int fd;
2272 struct stat sb;
2274 if (strcmp(dent->d_name, ".") == 0 ||
2275 strcmp(dent->d_name, "..") == 0)
2276 continue;
2278 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2279 err = got_error_from_errno("asprintf");
2280 goto done;
2283 /* XXX: sha256 */
2284 if (!got_parse_sha1_digest(id.hash, id_str)) {
2285 free(id_str);
2286 continue;
2288 free(id_str);
2290 err = got_object_open_loose_fd(&fd, &id, repo);
2291 if (err)
2292 goto done;
2294 if (fstat(fd, &sb) == -1) {
2295 err = got_error_from_errno("fstat");
2296 close(fd);
2297 goto done;
2299 (*nobjects)++;
2300 (*ondisk_size) += sb.st_size;
2302 if (close(fd) == -1) {
2303 err = got_error_from_errno("close");
2304 goto done;
2308 if (closedir(dir) != 0) {
2309 err = got_error_from_errno("closedir");
2310 goto done;
2312 dir = NULL;
2314 free(path);
2315 path = NULL;
2317 done:
2318 if (dir && closedir(dir) != 0 && err == NULL)
2319 err = got_error_from_errno("closedir");
2321 if (err) {
2322 *nobjects = 0;
2323 *ondisk_size = 0;
2325 free(path_objects);
2326 free(path);
2327 return err;
2330 const struct got_error *
2331 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2332 off_t *total_packsize, struct got_repository *repo)
2334 const struct got_error *err = NULL;
2335 DIR *packdir = NULL;
2336 struct dirent *dent;
2337 struct got_packidx *packidx = NULL;
2338 char *path_packidx;
2339 char *path_packfile;
2340 int packdir_fd;
2341 struct stat sb;
2343 *npackfiles = 0;
2344 *nobjects = 0;
2345 *total_packsize = 0;
2347 packdir_fd = openat(got_repo_get_fd(repo),
2348 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2349 if (packdir_fd == -1) {
2350 return got_error_from_errno_fmt("openat: %s/%s",
2351 got_repo_get_path_git_dir(repo),
2352 GOT_OBJECTS_PACK_DIR);
2355 packdir = fdopendir(packdir_fd);
2356 if (packdir == NULL) {
2357 err = got_error_from_errno("fdopendir");
2358 goto done;
2361 while ((dent = readdir(packdir)) != NULL) {
2362 if (!got_repo_is_packidx_filename(repo, dent->d_name,
2363 dent->d_namlen))
2364 continue;
2366 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2367 dent->d_name) == -1) {
2368 err = got_error_from_errno("asprintf");
2369 goto done;
2372 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2373 path_packidx, 0, repo->algo);
2374 free(path_packidx);
2375 if (err)
2376 goto done;
2378 if (fstat(packidx->fd, &sb) == -1)
2379 goto done;
2380 *total_packsize += sb.st_size;
2382 err = got_packidx_get_packfile_path(&path_packfile,
2383 packidx->path_packidx);
2384 if (err)
2385 goto done;
2387 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2388 0) == -1) {
2389 free(path_packfile);
2390 goto done;
2392 free(path_packfile);
2393 *total_packsize += sb.st_size;
2395 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2397 (*npackfiles)++;
2399 got_packidx_close(packidx);
2400 packidx = NULL;
2402 done:
2403 if (packidx)
2404 got_packidx_close(packidx);
2405 if (packdir && closedir(packdir) != 0 && err == NULL)
2406 err = got_error_from_errno("closedir");
2407 if (err) {
2408 *npackfiles = 0;
2409 *nobjects = 0;
2410 *total_packsize = 0;
2412 return err;
2415 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2416 got_packidx_bloom_filter_cmp);