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 <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_delta_cache.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_sha1.h"
64 #include "got_lib_object_cache.h"
65 #include "got_lib_repository.h"
66 #include "got_lib_gotconfig.h"
68 #ifndef nitems
69 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
70 #endif
72 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
74 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
75 got_packidx_bloom_filter_cmp);
77 static inline int
78 is_boolean_val(const char *val)
79 {
80 return (strcasecmp(val, "true") == 0 ||
81 strcasecmp(val, "false") == 0 ||
82 strcasecmp(val, "on") == 0 ||
83 strcasecmp(val, "off") == 0 ||
84 strcasecmp(val, "yes") == 0 ||
85 strcasecmp(val, "no") == 0 ||
86 strcasecmp(val, "1") == 0 ||
87 strcasecmp(val, "0") == 0);
88 }
90 static inline int
91 get_boolean_val(const char *val)
92 {
93 return (strcasecmp(val, "true") == 0 ||
94 strcasecmp(val, "on") == 0 ||
95 strcasecmp(val, "yes") == 0 ||
96 strcasecmp(val, "1") == 0);
97 }
99 const char *
100 got_repo_get_path(struct got_repository *repo)
102 return repo->path;
105 const char *
106 got_repo_get_path_git_dir(struct got_repository *repo)
108 return repo->path_git_dir;
111 int
112 got_repo_get_fd(struct got_repository *repo)
114 return repo->gitdir_fd;
117 const char *
118 got_repo_get_gitconfig_author_name(struct got_repository *repo)
120 return repo->gitconfig_author_name;
123 const char *
124 got_repo_get_gitconfig_author_email(struct got_repository *repo)
126 return repo->gitconfig_author_email;
129 const char *
130 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
132 return repo->global_gitconfig_author_name;
135 const char *
136 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
138 return repo->global_gitconfig_author_email;
141 const char *
142 got_repo_get_gitconfig_owner(struct got_repository *repo)
144 return repo->gitconfig_owner;
147 int
148 got_repo_has_extension(struct got_repository *repo, const char *ext)
150 int i;
152 for (i = 0; i < repo->nextensions; ++i) {
153 if (!strcasecmp(ext, repo->extnames[i]))
154 return get_boolean_val(repo->extvals[i]);
157 return 0;
160 int
161 got_repo_is_bare(struct got_repository *repo)
163 return (strcmp(repo->path, repo->path_git_dir) == 0);
166 static char *
167 get_path_git_child(struct got_repository *repo, const char *basename)
169 char *path_child;
171 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
172 basename) == -1)
173 return NULL;
175 return path_child;
178 char *
179 got_repo_get_path_objects(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_OBJECTS_DIR);
184 char *
185 got_repo_get_path_objects_pack(struct got_repository *repo)
187 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
190 char *
191 got_repo_get_path_refs(struct got_repository *repo)
193 return get_path_git_child(repo, GOT_REFS_DIR);
196 char *
197 got_repo_get_path_packed_refs(struct got_repository *repo)
199 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
202 static char *
203 get_path_head(struct got_repository *repo)
205 return get_path_git_child(repo, GOT_HEAD_FILE);
208 char *
209 got_repo_get_path_gitconfig(struct got_repository *repo)
211 return get_path_git_child(repo, GOT_GITCONFIG);
214 char *
215 got_repo_get_path_gotconfig(struct got_repository *repo)
217 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
220 const struct got_gotconfig *
221 got_repo_get_gotconfig(struct got_repository *repo)
223 return repo->gotconfig;
226 void
227 got_repo_get_gitconfig_remotes(int *nremotes,
228 const struct got_remote_repo **remotes, struct got_repository *repo)
230 *nremotes = repo->ngitconfig_remotes;
231 *remotes = repo->gitconfig_remotes;
234 static int
235 is_git_repo(struct got_repository *repo)
237 const char *path_git = got_repo_get_path_git_dir(repo);
238 char *path_objects = got_repo_get_path_objects(repo);
239 char *path_refs = got_repo_get_path_refs(repo);
240 char *path_head = get_path_head(repo);
241 int ret = 0;
242 struct stat sb;
243 struct got_reference *head_ref;
245 if (lstat(path_git, &sb) == -1)
246 goto done;
247 if (!S_ISDIR(sb.st_mode))
248 goto done;
250 if (lstat(path_objects, &sb) == -1)
251 goto done;
252 if (!S_ISDIR(sb.st_mode))
253 goto done;
255 if (lstat(path_refs, &sb) == -1)
256 goto done;
257 if (!S_ISDIR(sb.st_mode))
258 goto done;
260 if (lstat(path_head, &sb) == -1)
261 goto done;
262 if (!S_ISREG(sb.st_mode))
263 goto done;
265 /* Check if the HEAD reference can be opened. */
266 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
267 goto done;
268 got_ref_close(head_ref);
270 ret = 1;
271 done:
272 free(path_objects);
273 free(path_refs);
274 free(path_head);
275 return ret;
279 static const struct got_error *
280 close_tempfiles(int *fds, size_t nfds)
282 const struct got_error *err = NULL;
283 int i;
285 for (i = 0; i < nfds; i++) {
286 if (fds[i] == -1)
287 continue;
288 if (close(fds[i]) == -1) {
289 err = got_error_from_errno("close");
290 break;
293 free(fds);
294 return err;
297 static const struct got_error *
298 open_tempfiles(int **fds, size_t array_size, size_t nfds)
300 const struct got_error *err = NULL;
301 int i;
303 *fds = calloc(array_size, sizeof(**fds));
304 if (*fds == NULL)
305 return got_error_from_errno("calloc");
307 for (i = 0; i < array_size; i++)
308 (*fds)[i] = -1;
310 for (i = 0; i < nfds; i++) {
311 (*fds)[i] = got_opentempfd();
312 if ((*fds)[i] == -1) {
313 err = got_error_from_errno("got_opentempfd");
314 close_tempfiles(*fds, nfds);
315 *fds = NULL;
316 return err;
320 return NULL;
323 static const struct got_error *
324 get_pack_cache_size(int *pack_cache_size)
326 struct rlimit rl;
328 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
329 return got_error_from_errno("getrlimit");
331 *pack_cache_size = GOT_PACK_CACHE_SIZE;
332 if (*pack_cache_size > rl.rlim_cur / 8)
333 *pack_cache_size = rl.rlim_cur / 8;
335 return NULL;
338 const struct got_error *
339 got_repo_pack_fds_open(int **pack_fds)
341 const struct got_error *err;
342 int nfds;
344 err = get_pack_cache_size(&nfds);
345 if (err)
346 return err;
348 /*
349 * We need one basefd and one accumfd per cached pack.
350 * Our constants should be set up in a way such that
351 * this error never triggers.
352 */
353 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
354 return got_error(GOT_ERR_NO_SPACE);
356 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
359 const struct got_error *
360 got_repo_pack_fds_close(int *pack_fds)
362 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
365 const struct got_error *
366 got_repo_temp_fds_open(int **temp_fds)
368 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
369 GOT_REPO_NUM_TEMPFILES);
372 void
373 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
375 int i;
377 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
378 repo->tempfiles[i] = temp_fds[i];
381 const struct got_error *
382 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
384 int i;
386 *fd = -1;
387 *idx = -1;
389 for (i = 0; i < nitems(repo->tempfiles); i++) {
390 if (repo->tempfile_use_mask & (1 << i))
391 continue;
392 if (repo->tempfiles[i] != -1) {
393 if (ftruncate(repo->tempfiles[i], 0L) == -1)
394 return got_error_from_errno("ftruncate");
395 *fd = repo->tempfiles[i];
396 *idx = i;
397 repo->tempfile_use_mask |= (1 << i);
398 return NULL;
402 return got_error(GOT_ERR_REPO_TEMPFILE);
405 void
406 got_repo_temp_fds_put(int idx, struct got_repository *repo)
408 repo->tempfile_use_mask &= ~(1 << idx);
411 const struct got_error *
412 got_repo_temp_fds_close(int *temp_fds)
414 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
417 const struct got_error *
418 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
419 struct got_object *obj)
421 #ifndef GOT_NO_OBJ_CACHE
422 const struct got_error *err = NULL;
423 err = got_object_cache_add(&repo->objcache, id, obj);
424 if (err) {
425 if (err->code == GOT_ERR_OBJ_EXISTS ||
426 err->code == GOT_ERR_OBJ_TOO_LARGE)
427 err = NULL;
428 return err;
430 obj->refcnt++;
431 #endif
432 return NULL;
435 struct got_object *
436 got_repo_get_cached_object(struct got_repository *repo,
437 struct got_object_id *id)
439 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
442 const struct got_error *
443 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
444 struct got_tree_object *tree)
446 #ifndef GOT_NO_OBJ_CACHE
447 const struct got_error *err = NULL;
448 err = got_object_cache_add(&repo->treecache, id, tree);
449 if (err) {
450 if (err->code == GOT_ERR_OBJ_EXISTS ||
451 err->code == GOT_ERR_OBJ_TOO_LARGE)
452 err = NULL;
453 return err;
455 tree->refcnt++;
456 #endif
457 return NULL;
460 struct got_tree_object *
461 got_repo_get_cached_tree(struct got_repository *repo,
462 struct got_object_id *id)
464 return (struct got_tree_object *)got_object_cache_get(
465 &repo->treecache, id);
468 const struct got_error *
469 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
470 struct got_commit_object *commit)
472 #ifndef GOT_NO_OBJ_CACHE
473 const struct got_error *err = NULL;
474 err = got_object_cache_add(&repo->commitcache, id, commit);
475 if (err) {
476 if (err->code == GOT_ERR_OBJ_EXISTS ||
477 err->code == GOT_ERR_OBJ_TOO_LARGE)
478 err = NULL;
479 return err;
481 commit->refcnt++;
482 #endif
483 return NULL;
486 struct got_commit_object *
487 got_repo_get_cached_commit(struct got_repository *repo,
488 struct got_object_id *id)
490 return (struct got_commit_object *)got_object_cache_get(
491 &repo->commitcache, id);
494 const struct got_error *
495 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
496 struct got_tag_object *tag)
498 #ifndef GOT_NO_OBJ_CACHE
499 const struct got_error *err = NULL;
500 err = got_object_cache_add(&repo->tagcache, id, tag);
501 if (err) {
502 if (err->code == GOT_ERR_OBJ_EXISTS ||
503 err->code == GOT_ERR_OBJ_TOO_LARGE)
504 err = NULL;
505 return err;
507 tag->refcnt++;
508 #endif
509 return NULL;
512 struct got_tag_object *
513 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
515 return (struct got_tag_object *)got_object_cache_get(
516 &repo->tagcache, id);
519 const struct got_error *
520 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
521 struct got_raw_object *raw)
523 #ifndef GOT_NO_OBJ_CACHE
524 const struct got_error *err = NULL;
525 err = got_object_cache_add(&repo->rawcache, id, raw);
526 if (err) {
527 if (err->code == GOT_ERR_OBJ_EXISTS ||
528 err->code == GOT_ERR_OBJ_TOO_LARGE)
529 err = NULL;
530 return err;
532 raw->refcnt++;
533 #endif
534 return NULL;
538 struct got_raw_object *
539 got_repo_get_cached_raw_object(struct got_repository *repo,
540 struct got_object_id *id)
542 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
546 static const struct got_error *
547 open_repo(struct got_repository *repo, const char *path)
549 const struct got_error *err = NULL;
551 repo->gitdir_fd = -1;
553 /* bare git repository? */
554 repo->path_git_dir = strdup(path);
555 if (repo->path_git_dir == NULL)
556 return got_error_from_errno("strdup");
557 if (is_git_repo(repo)) {
558 repo->path = strdup(repo->path_git_dir);
559 if (repo->path == NULL) {
560 err = got_error_from_errno("strdup");
561 goto done;
563 repo->gitdir_fd = open(repo->path_git_dir,
564 O_DIRECTORY | O_CLOEXEC);
565 if (repo->gitdir_fd == -1) {
566 err = got_error_from_errno2("open",
567 repo->path_git_dir);
568 goto done;
570 return NULL;
573 /* git repository with working tree? */
574 free(repo->path_git_dir);
575 repo->path_git_dir = NULL;
576 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
577 err = got_error_from_errno("asprintf");
578 goto done;
580 if (is_git_repo(repo)) {
581 repo->path = strdup(path);
582 if (repo->path == NULL) {
583 err = got_error_from_errno("strdup");
584 goto done;
586 repo->gitdir_fd = open(repo->path_git_dir,
587 O_DIRECTORY | O_CLOEXEC);
588 if (repo->gitdir_fd == -1) {
589 err = got_error_from_errno2("open",
590 repo->path_git_dir);
591 goto done;
593 return NULL;
596 err = got_error(GOT_ERR_NOT_GIT_REPO);
597 done:
598 if (err) {
599 free(repo->path);
600 repo->path = NULL;
601 free(repo->path_git_dir);
602 repo->path_git_dir = NULL;
603 if (repo->gitdir_fd != -1)
604 close(repo->gitdir_fd);
605 repo->gitdir_fd = -1;
608 return err;
611 static const struct got_error *
612 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
614 const struct got_error *err = NULL;
615 char *repo_gitconfig_path = NULL;
617 if (global_gitconfig_path) {
618 /* Read settings from ~/.gitconfig. */
619 int dummy_repo_version;
620 err = got_repo_read_gitconfig(&dummy_repo_version,
621 &repo->global_gitconfig_author_name,
622 &repo->global_gitconfig_author_email,
623 NULL, NULL, NULL, NULL, NULL, NULL,
624 global_gitconfig_path);
625 if (err)
626 return err;
629 /* Read repository's .git/config file. */
630 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
631 if (repo_gitconfig_path == NULL)
632 return got_error_from_errno("got_repo_get_path_gitconfig");
634 err = got_repo_read_gitconfig(
635 &repo->gitconfig_repository_format_version,
636 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
637 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
638 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
639 &repo->nextensions, repo_gitconfig_path);
640 if (err)
641 goto done;
643 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
644 int i;
646 for (i = 0; i < repo->ngitconfig_remotes; i++) {
647 got_repo_free_remote_repo_data(
648 &repo->gitconfig_remotes[i]);
650 free(repo->gitconfig_remotes);
651 repo->gitconfig_remotes = NULL;
652 repo->ngitconfig_remotes = 0;
654 free(repo->gitconfig_author_name);
655 repo->gitconfig_author_name = NULL;
656 free(repo->gitconfig_author_email);
657 repo->gitconfig_author_email = NULL;
659 free(repo->global_gitconfig_author_name);
660 repo->global_gitconfig_author_name = NULL;
661 free(repo->global_gitconfig_author_email);
662 repo->global_gitconfig_author_email = NULL;
665 done:
666 free(repo_gitconfig_path);
667 return err;
670 static const struct got_error *
671 read_gotconfig(struct got_repository *repo)
673 const struct got_error *err = NULL;
674 char *gotconfig_path;
676 gotconfig_path = got_repo_get_path_gotconfig(repo);
677 if (gotconfig_path == NULL)
678 return got_error_from_errno("got_repo_get_path_gotconfig");
680 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
681 free(gotconfig_path);
682 return err;
685 /* Supported repository format extensions. */
686 static const char *const repo_extensions[] = {
687 "noop", /* Got supports repository format version 1. */
688 "preciousObjects", /* Supported by gotadmin cleanup. */
689 "worktreeConfig", /* Got does not care about Git work trees. */
690 };
692 const struct got_error *
693 got_repo_open(struct got_repository **repop, const char *path,
694 const char *global_gitconfig_path, int *pack_fds)
696 struct got_repository *repo = NULL;
697 const struct got_error *err = NULL;
698 char *repo_path = NULL;
699 size_t i, j = 0;
701 *repop = NULL;
703 repo = calloc(1, sizeof(*repo));
704 if (repo == NULL)
705 return got_error_from_errno("calloc");
707 RB_INIT(&repo->packidx_bloom_filters);
708 TAILQ_INIT(&repo->packidx_paths);
710 for (i = 0; i < nitems(repo->privsep_children); i++) {
711 memset(&repo->privsep_children[i], 0,
712 sizeof(repo->privsep_children[0]));
713 repo->privsep_children[i].imsg_fd = -1;
716 err = got_object_cache_init(&repo->objcache,
717 GOT_OBJECT_CACHE_TYPE_OBJ);
718 if (err)
719 goto done;
720 err = got_object_cache_init(&repo->treecache,
721 GOT_OBJECT_CACHE_TYPE_TREE);
722 if (err)
723 goto done;
724 err = got_object_cache_init(&repo->commitcache,
725 GOT_OBJECT_CACHE_TYPE_COMMIT);
726 if (err)
727 goto done;
728 err = got_object_cache_init(&repo->tagcache,
729 GOT_OBJECT_CACHE_TYPE_TAG);
730 if (err)
731 goto done;
732 err = got_object_cache_init(&repo->rawcache,
733 GOT_OBJECT_CACHE_TYPE_RAW);
734 if (err)
735 goto done;
737 err = get_pack_cache_size(&repo->pack_cache_size);
738 if (err)
739 goto done;
740 for (i = 0; i < nitems(repo->packs); i++) {
741 if (pack_fds != NULL && i < repo->pack_cache_size) {
742 repo->packs[i].basefd = pack_fds[j++];
743 repo->packs[i].accumfd = pack_fds[j++];
744 } else {
745 repo->packs[i].basefd = -1;
746 repo->packs[i].accumfd = -1;
749 for (i = 0; i < nitems(repo->tempfiles); i++)
750 repo->tempfiles[i] = -1;
751 repo->pinned_pack = -1;
752 repo->pinned_packidx = -1;
753 repo->pinned_pid = 0;
755 repo_path = realpath(path, NULL);
756 if (repo_path == NULL) {
757 err = got_error_from_errno2("realpath", path);
758 goto done;
761 for (;;) {
762 char *parent_path;
764 err = open_repo(repo, repo_path);
765 if (err == NULL)
766 break;
767 if (err->code != GOT_ERR_NOT_GIT_REPO)
768 goto done;
769 if (repo_path[0] == '/' && repo_path[1] == '\0') {
770 err = got_error(GOT_ERR_NOT_GIT_REPO);
771 goto done;
773 err = got_path_dirname(&parent_path, repo_path);
774 if (err)
775 goto done;
776 free(repo_path);
777 repo_path = parent_path;
780 err = read_gotconfig(repo);
781 if (err)
782 goto done;
784 err = read_gitconfig(repo, global_gitconfig_path);
785 if (err)
786 goto done;
787 if (repo->gitconfig_repository_format_version != 0) {
788 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
789 goto done;
791 for (i = 0; i < repo->nextensions; i++) {
792 char *ext = repo->extnames[i];
793 char *val = repo->extvals[i];
794 int j, supported = 0;
796 if (!is_boolean_val(val)) {
797 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
798 goto done;
801 if (!get_boolean_val(val))
802 continue;
804 for (j = 0; j < nitems(repo_extensions); j++) {
805 if (strcmp(ext, repo_extensions[j]) == 0) {
806 supported = 1;
807 break;
810 if (!supported) {
811 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
812 goto done;
816 err = got_repo_list_packidx(&repo->packidx_paths, repo);
817 done:
818 if (err)
819 got_repo_close(repo);
820 else
821 *repop = repo;
822 free(repo_path);
823 return err;
826 const struct got_error *
827 got_repo_close(struct got_repository *repo)
829 const struct got_error *err = NULL, *child_err;
830 struct got_packidx_bloom_filter *bf;
831 size_t i;
833 for (i = 0; i < repo->pack_cache_size; i++) {
834 if (repo->packidx_cache[i] == NULL)
835 break;
836 got_packidx_close(repo->packidx_cache[i]);
839 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
840 &repo->packidx_bloom_filters))) {
841 RB_REMOVE(got_packidx_bloom_filter_tree,
842 &repo->packidx_bloom_filters, bf);
843 bloom_free(bf->bloom);
844 free(bf->bloom);
845 free(bf);
848 for (i = 0; i < repo->pack_cache_size; i++)
849 if (repo->packs[i].path_packfile)
850 if (repo->packs[i].path_packfile)
851 got_pack_close(&repo->packs[i]);
853 free(repo->path);
854 free(repo->path_git_dir);
856 got_object_cache_close(&repo->objcache);
857 got_object_cache_close(&repo->treecache);
858 got_object_cache_close(&repo->commitcache);
859 got_object_cache_close(&repo->tagcache);
860 got_object_cache_close(&repo->rawcache);
862 for (i = 0; i < nitems(repo->privsep_children); i++) {
863 if (repo->privsep_children[i].imsg_fd == -1)
864 continue;
865 imsg_clear(repo->privsep_children[i].ibuf);
866 free(repo->privsep_children[i].ibuf);
867 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
868 child_err = got_privsep_wait_for_child(
869 repo->privsep_children[i].pid);
870 if (child_err && err == NULL)
871 err = child_err;
872 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
873 err == NULL)
874 err = got_error_from_errno("close");
877 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
878 err == NULL)
879 err = got_error_from_errno("close");
881 if (repo->gotconfig)
882 got_gotconfig_free(repo->gotconfig);
883 free(repo->gitconfig_author_name);
884 free(repo->gitconfig_author_email);
885 for (i = 0; i < repo->ngitconfig_remotes; i++)
886 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
887 free(repo->gitconfig_remotes);
888 for (i = 0; i < repo->nextensions; i++) {
889 free(repo->extnames[i]);
890 free(repo->extvals[i]);
892 free(repo->extnames);
893 free(repo->extvals);
895 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
896 free(repo);
898 return err;
901 void
902 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
904 int i;
906 free(repo->name);
907 repo->name = NULL;
908 free(repo->fetch_url);
909 repo->fetch_url = NULL;
910 free(repo->send_url);
911 repo->send_url = NULL;
912 for (i = 0; i < repo->nfetch_branches; i++)
913 free(repo->fetch_branches[i]);
914 free(repo->fetch_branches);
915 repo->fetch_branches = NULL;
916 repo->nfetch_branches = 0;
917 for (i = 0; i < repo->nsend_branches; i++)
918 free(repo->send_branches[i]);
919 free(repo->send_branches);
920 repo->send_branches = NULL;
921 repo->nsend_branches = 0;
924 const struct got_error *
925 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
926 const char *input_path)
928 const struct got_error *err = NULL;
929 const char *repo_abspath = NULL;
930 size_t repolen, len;
931 char *canonpath, *path = NULL;
933 *in_repo_path = NULL;
935 canonpath = strdup(input_path);
936 if (canonpath == NULL) {
937 err = got_error_from_errno("strdup");
938 goto done;
940 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
941 if (err)
942 goto done;
944 repo_abspath = got_repo_get_path(repo);
946 if (canonpath[0] == '\0') {
947 path = strdup(canonpath);
948 if (path == NULL) {
949 err = got_error_from_errno("strdup");
950 goto done;
952 } else {
953 path = realpath(canonpath, NULL);
954 if (path == NULL) {
955 if (errno != ENOENT) {
956 err = got_error_from_errno2("realpath",
957 canonpath);
958 goto done;
960 /*
961 * Path is not on disk.
962 * Assume it is already relative to repository root.
963 */
964 path = strdup(canonpath);
965 if (path == NULL) {
966 err = got_error_from_errno("strdup");
967 goto done;
971 repolen = strlen(repo_abspath);
972 len = strlen(path);
975 if (strcmp(path, repo_abspath) == 0) {
976 free(path);
977 path = strdup("");
978 if (path == NULL) {
979 err = got_error_from_errno("strdup");
980 goto done;
982 } else if (len > repolen &&
983 got_path_is_child(path, repo_abspath, repolen)) {
984 /* Matched an on-disk path inside repository. */
985 if (got_repo_is_bare(repo)) {
986 /*
987 * Matched an on-disk path inside repository
988 * database. Treat input as repository-relative.
989 */
990 free(path);
991 path = canonpath;
992 canonpath = NULL;
993 } else {
994 char *child;
995 /* Strip common prefix with repository path. */
996 err = got_path_skip_common_ancestor(&child,
997 repo_abspath, path);
998 if (err)
999 goto done;
1000 free(path);
1001 path = child;
1003 } else {
1005 * Matched unrelated on-disk path.
1006 * Treat input as repository-relative.
1008 free(path);
1009 path = canonpath;
1010 canonpath = NULL;
1014 /* Make in-repository path absolute */
1015 if (path[0] != '/') {
1016 char *abspath;
1017 if (asprintf(&abspath, "/%s", path) == -1) {
1018 err = got_error_from_errno("asprintf");
1019 goto done;
1021 free(path);
1022 path = abspath;
1025 done:
1026 free(canonpath);
1027 if (err)
1028 free(path);
1029 else
1030 *in_repo_path = path;
1031 return err;
1034 static const struct got_error *
1035 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1036 const char *path_packidx)
1038 const struct got_error *err = NULL;
1039 size_t i;
1041 for (i = 0; i < repo->pack_cache_size; i++) {
1042 if (repo->packidx_cache[i] == NULL)
1043 break;
1044 if (strcmp(repo->packidx_cache[i]->path_packidx,
1045 path_packidx) == 0) {
1046 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1049 if (i == repo->pack_cache_size) {
1050 do {
1051 i--;
1052 } while (i > 0 && repo->pinned_packidx >= 0 &&
1053 i == repo->pinned_packidx);
1054 err = got_packidx_close(repo->packidx_cache[i]);
1055 if (err)
1056 return err;
1059 repo->packidx_cache[i] = packidx;
1061 return NULL;
1064 int
1065 got_repo_is_packidx_filename(const char *name, size_t len)
1067 if (len != GOT_PACKIDX_NAMELEN)
1068 return 0;
1070 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1071 return 0;
1073 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1074 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1075 return 0;
1077 return 1;
1080 static struct got_packidx_bloom_filter *
1081 get_packidx_bloom_filter(struct got_repository *repo,
1082 const char *path, size_t path_len)
1084 struct got_packidx_bloom_filter key;
1086 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1087 return NULL; /* XXX */
1088 key.path_len = path_len;
1090 return RB_FIND(got_packidx_bloom_filter_tree,
1091 &repo->packidx_bloom_filters, &key);
1094 int
1095 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1096 const char *path_packidx, struct got_object_id *id)
1098 struct got_packidx_bloom_filter *bf;
1100 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1101 if (bf)
1102 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1104 /* No bloom filter means this pack index must be searched. */
1105 return 1;
1108 static const struct got_error *
1109 add_packidx_bloom_filter(struct got_repository *repo,
1110 struct got_packidx *packidx, const char *path_packidx)
1112 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1113 struct got_packidx_bloom_filter *bf;
1114 size_t len;
1117 * Don't use bloom filters for very large pack index files.
1118 * Large pack files will contain a relatively large fraction
1119 * of our objects so we will likely need to visit them anyway.
1120 * The more objects a pack file contains the higher the probability
1121 * of a false-positive match from the bloom filter. And reading
1122 * all object IDs from a large pack index file can be expensive.
1124 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1125 return NULL;
1127 /* Do we already have a filter for this pack index? */
1128 if (get_packidx_bloom_filter(repo, path_packidx,
1129 strlen(path_packidx)) != NULL)
1130 return NULL;
1132 bf = calloc(1, sizeof(*bf));
1133 if (bf == NULL)
1134 return got_error_from_errno("calloc");
1135 bf->bloom = calloc(1, sizeof(*bf->bloom));
1136 if (bf->bloom == NULL) {
1137 free(bf);
1138 return got_error_from_errno("calloc");
1141 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1142 if (len >= sizeof(bf->path)) {
1143 free(bf->bloom);
1144 free(bf);
1145 return got_error(GOT_ERR_NO_SPACE);
1147 bf->path_len = len;
1149 /* Minimum size supported by our bloom filter is 1000 entries. */
1150 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1151 for (i = 0; i < nobjects; i++) {
1152 struct got_packidx_object_id *id;
1153 id = &packidx->hdr.sorted_ids[i];
1154 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1157 RB_INSERT(got_packidx_bloom_filter_tree,
1158 &repo->packidx_bloom_filters, bf);
1159 return NULL;
1162 static void
1163 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1165 struct got_pathlist_entry *pe;
1167 while (!TAILQ_EMPTY(packidx_paths)) {
1168 pe = TAILQ_FIRST(packidx_paths);
1169 TAILQ_REMOVE(packidx_paths, pe, entry);
1170 free((char *)pe->path);
1171 free(pe);
1175 static const struct got_error *
1176 refresh_packidx_paths(struct got_repository *repo)
1178 const struct got_error *err = NULL;
1179 char *objects_pack_dir = NULL;
1180 struct stat sb;
1182 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1183 if (objects_pack_dir == NULL)
1184 return got_error_from_errno("got_repo_get_path_objects_pack");
1186 if (stat(objects_pack_dir, &sb) == -1) {
1187 if (errno != ENOENT) {
1188 err = got_error_from_errno2("stat", objects_pack_dir);
1189 goto done;
1191 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1192 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1193 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1194 purge_packidx_paths(&repo->packidx_paths);
1195 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1196 if (err)
1197 goto done;
1199 done:
1200 free(objects_pack_dir);
1201 return err;
1204 const struct got_error *
1205 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1206 struct got_repository *repo, struct got_object_id *id)
1208 const struct got_error *err;
1209 struct got_pathlist_entry *pe;
1210 size_t i;
1212 /* Search pack index cache. */
1213 for (i = 0; i < repo->pack_cache_size; i++) {
1214 if (repo->packidx_cache[i] == NULL)
1215 break;
1216 if (!got_repo_check_packidx_bloom_filter(repo,
1217 repo->packidx_cache[i]->path_packidx, id))
1218 continue; /* object will not be found in this index */
1219 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1220 if (*idx != -1) {
1221 *packidx = repo->packidx_cache[i];
1223 * Move this cache entry to the front. Repeatedly
1224 * searching a wrong pack index can be expensive.
1226 if (i > 0) {
1227 memmove(&repo->packidx_cache[1],
1228 &repo->packidx_cache[0],
1229 i * sizeof(repo->packidx_cache[0]));
1230 repo->packidx_cache[0] = *packidx;
1231 if (repo->pinned_packidx >= 0 &&
1232 repo->pinned_packidx < i)
1233 repo->pinned_packidx++;
1234 else if (repo->pinned_packidx == i)
1235 repo->pinned_packidx = 0;
1237 return NULL;
1240 /* No luck. Search the filesystem. */
1242 err = refresh_packidx_paths(repo);
1243 if (err)
1244 return err;
1246 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1247 const char *path_packidx = pe->path;
1248 int is_cached = 0;
1250 if (!got_repo_check_packidx_bloom_filter(repo,
1251 pe->path, id))
1252 continue; /* object will not be found in this index */
1254 for (i = 0; i < repo->pack_cache_size; i++) {
1255 if (repo->packidx_cache[i] == NULL)
1256 break;
1257 if (strcmp(repo->packidx_cache[i]->path_packidx,
1258 path_packidx) == 0) {
1259 is_cached = 1;
1260 break;
1263 if (is_cached)
1264 continue; /* already searched */
1266 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1267 path_packidx, 0);
1268 if (err)
1269 goto done;
1271 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1272 if (err)
1273 goto done;
1275 err = cache_packidx(repo, *packidx, path_packidx);
1276 if (err)
1277 goto done;
1279 *idx = got_packidx_get_object_idx(*packidx, id);
1280 if (*idx != -1) {
1281 err = NULL; /* found the object */
1282 goto done;
1286 err = got_error_no_obj(id);
1287 done:
1288 return err;
1291 const struct got_error *
1292 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1293 struct got_repository *repo)
1295 const struct got_error *err = NULL;
1296 DIR *packdir = NULL;
1297 struct dirent *dent;
1298 char *path_packidx = NULL;
1299 int packdir_fd;
1300 struct stat sb;
1302 packdir_fd = openat(got_repo_get_fd(repo),
1303 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1304 if (packdir_fd == -1) {
1305 return got_error_from_errno_fmt("openat: %s/%s",
1306 got_repo_get_path_git_dir(repo),
1307 GOT_OBJECTS_PACK_DIR);
1310 packdir = fdopendir(packdir_fd);
1311 if (packdir == NULL) {
1312 err = got_error_from_errno("fdopendir");
1313 goto done;
1316 if (fstat(packdir_fd, &sb) == -1) {
1317 err = got_error_from_errno("fstat");
1318 goto done;
1320 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1321 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1323 while ((dent = readdir(packdir)) != NULL) {
1324 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1325 continue;
1327 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1328 dent->d_name) == -1) {
1329 err = got_error_from_errno("asprintf");
1330 path_packidx = NULL;
1331 break;
1334 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1335 if (err)
1336 break;
1338 done:
1339 if (err)
1340 free(path_packidx);
1341 if (packdir && closedir(packdir) != 0 && err == NULL)
1342 err = got_error_from_errno("closedir");
1343 return err;
1346 const struct got_error *
1347 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1348 struct got_repository *repo)
1350 const struct got_error *err;
1351 size_t i;
1353 *packidx = NULL;
1355 /* Search pack index cache. */
1356 for (i = 0; i < repo->pack_cache_size; i++) {
1357 if (repo->packidx_cache[i] == NULL)
1358 break;
1359 if (strcmp(repo->packidx_cache[i]->path_packidx,
1360 path_packidx) == 0) {
1361 *packidx = repo->packidx_cache[i];
1362 return NULL;
1365 /* No luck. Search the filesystem. */
1367 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1368 path_packidx, 0);
1369 if (err)
1370 return err;
1372 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1373 if (err)
1374 goto done;
1376 err = cache_packidx(repo, *packidx, path_packidx);
1377 done:
1378 if (err) {
1379 got_packidx_close(*packidx);
1380 *packidx = NULL;
1382 return err;
1385 static const struct got_error *
1386 read_packfile_hdr(int fd, struct got_packidx *packidx)
1388 const struct got_error *err = NULL;
1389 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1390 struct got_packfile_hdr hdr;
1391 ssize_t n;
1393 n = read(fd, &hdr, sizeof(hdr));
1394 if (n < 0)
1395 return got_error_from_errno("read");
1396 if (n != sizeof(hdr))
1397 return got_error(GOT_ERR_BAD_PACKFILE);
1399 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1400 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1401 be32toh(hdr.nobjects) != totobj)
1402 err = got_error(GOT_ERR_BAD_PACKFILE);
1404 return err;
1407 static const struct got_error *
1408 open_packfile(int *fd, struct got_repository *repo,
1409 const char *relpath, struct got_packidx *packidx)
1411 const struct got_error *err = NULL;
1413 *fd = openat(got_repo_get_fd(repo), relpath,
1414 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1415 if (*fd == -1)
1416 return got_error_from_errno_fmt("openat: %s/%s",
1417 got_repo_get_path_git_dir(repo), relpath);
1419 if (packidx) {
1420 err = read_packfile_hdr(*fd, packidx);
1421 if (err) {
1422 close(*fd);
1423 *fd = -1;
1427 return err;
1430 const struct got_error *
1431 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1432 const char *path_packfile, struct got_packidx *packidx)
1434 const struct got_error *err = NULL;
1435 struct got_pack *pack = NULL;
1436 struct stat sb;
1437 size_t i;
1439 if (packp)
1440 *packp = NULL;
1442 for (i = 0; i < repo->pack_cache_size; i++) {
1443 pack = &repo->packs[i];
1444 if (pack->path_packfile == NULL)
1445 break;
1446 if (strcmp(pack->path_packfile, path_packfile) == 0)
1447 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1450 if (i == repo->pack_cache_size) {
1451 struct got_pack tmp;
1452 do {
1453 i--;
1454 } while (i > 0 && repo->pinned_pack >= 0 &&
1455 i == repo->pinned_pack);
1456 err = got_pack_close(&repo->packs[i]);
1457 if (err)
1458 return err;
1459 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1460 return got_error_from_errno("ftruncate");
1461 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1462 return got_error_from_errno("ftruncate");
1463 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1464 memcpy(&repo->packs[i], &repo->packs[0],
1465 sizeof(repo->packs[i]));
1466 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1467 if (repo->pinned_pack == 0)
1468 repo->pinned_pack = i;
1469 else if (repo->pinned_pack == i)
1470 repo->pinned_pack = 0;
1471 i = 0;
1474 pack = &repo->packs[i];
1476 pack->path_packfile = strdup(path_packfile);
1477 if (pack->path_packfile == NULL) {
1478 err = got_error_from_errno("strdup");
1479 goto done;
1482 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1483 if (err)
1484 goto done;
1486 if (fstat(pack->fd, &sb) != 0) {
1487 err = got_error_from_errno("fstat");
1488 goto done;
1490 pack->filesize = sb.st_size;
1492 pack->privsep_child = NULL;
1494 err = got_delta_cache_alloc(&pack->delta_cache);
1495 if (err)
1496 goto done;
1498 #ifndef GOT_PACK_NO_MMAP
1499 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1500 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1501 pack->fd, 0);
1502 if (pack->map == MAP_FAILED) {
1503 if (errno != ENOMEM) {
1504 err = got_error_from_errno("mmap");
1505 goto done;
1507 pack->map = NULL; /* fall back to read(2) */
1510 #endif
1511 done:
1512 if (err) {
1513 if (pack)
1514 got_pack_close(pack);
1515 } else if (packp)
1516 *packp = pack;
1517 return err;
1520 struct got_pack *
1521 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1523 struct got_pack *pack = NULL;
1524 size_t i;
1526 for (i = 0; i < repo->pack_cache_size; i++) {
1527 pack = &repo->packs[i];
1528 if (pack->path_packfile == NULL)
1529 break;
1530 if (strcmp(pack->path_packfile, path_packfile) == 0)
1531 return pack;
1534 return NULL;
1537 const struct got_error *
1538 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1539 struct got_pack *pack)
1541 size_t i;
1542 int pinned_pack = -1, pinned_packidx = -1;
1544 for (i = 0; i < repo->pack_cache_size; i++) {
1545 if (repo->packidx_cache[i] &&
1546 strcmp(repo->packidx_cache[i]->path_packidx,
1547 packidx->path_packidx) == 0)
1548 pinned_packidx = i;
1549 if (repo->packs[i].path_packfile &&
1550 strcmp(repo->packs[i].path_packfile,
1551 pack->path_packfile) == 0)
1552 pinned_pack = i;
1555 if (pinned_packidx == -1 || pinned_pack == -1)
1556 return got_error(GOT_ERR_PIN_PACK);
1558 repo->pinned_pack = pinned_pack;
1559 repo->pinned_packidx = pinned_packidx;
1560 if (repo->packs[pinned_pack].privsep_child)
1561 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1562 return NULL;
1565 struct got_pack *
1566 got_repo_get_pinned_pack(struct got_repository *repo)
1568 if (repo->pinned_pack >= 0 &&
1569 repo->pinned_pack < repo->pack_cache_size)
1570 return &repo->packs[repo->pinned_pack];
1572 return NULL;
1575 void
1576 got_repo_unpin_pack(struct got_repository *repo)
1578 repo->pinned_packidx = -1;
1579 repo->pinned_pack = -1;
1580 repo->pinned_pid = 0;
1583 const struct got_error *
1584 got_repo_init(const char *repo_path, const char *head_name)
1586 const struct got_error *err = NULL;
1587 const char *dirnames[] = {
1588 GOT_OBJECTS_DIR,
1589 GOT_OBJECTS_PACK_DIR,
1590 GOT_REFS_DIR,
1592 const char *description_str = "Unnamed repository; "
1593 "edit this file 'description' to name the repository.";
1594 const char *headref = "ref: refs/heads/";
1595 const char *gitconfig_str = "[core]\n"
1596 "\trepositoryformatversion = 0\n"
1597 "\tfilemode = true\n"
1598 "\tbare = true\n";
1599 char *headref_str, *path;
1600 size_t i;
1602 if (!got_path_dir_is_empty(repo_path))
1603 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1605 for (i = 0; i < nitems(dirnames); i++) {
1606 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1607 return got_error_from_errno("asprintf");
1609 err = got_path_mkdir(path);
1610 free(path);
1611 if (err)
1612 return err;
1615 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1616 return got_error_from_errno("asprintf");
1617 err = got_path_create_file(path, description_str);
1618 free(path);
1619 if (err)
1620 return err;
1622 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1623 return got_error_from_errno("asprintf");
1624 if (asprintf(&headref_str, "%s%s", headref,
1625 head_name ? head_name : "main") == -1) {
1626 free(path);
1627 return got_error_from_errno("asprintf");
1629 err = got_path_create_file(path, headref_str);
1630 free(headref_str);
1631 free(path);
1632 if (err)
1633 return err;
1635 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1636 return got_error_from_errno("asprintf");
1637 err = got_path_create_file(path, gitconfig_str);
1638 free(path);
1639 if (err)
1640 return err;
1642 return NULL;
1645 static const struct got_error *
1646 match_packed_object(struct got_object_id **unique_id,
1647 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1649 const struct got_error *err = NULL;
1650 struct got_object_id_queue matched_ids;
1651 struct got_pathlist_entry *pe;
1653 STAILQ_INIT(&matched_ids);
1655 err = refresh_packidx_paths(repo);
1656 if (err)
1657 return err;
1659 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1660 const char *path_packidx = pe->path;
1661 struct got_packidx *packidx;
1662 struct got_object_qid *qid;
1664 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1665 path_packidx, 0);
1666 if (err)
1667 break;
1669 err = got_packidx_match_id_str_prefix(&matched_ids,
1670 packidx, id_str_prefix);
1671 if (err) {
1672 got_packidx_close(packidx);
1673 break;
1675 err = got_packidx_close(packidx);
1676 if (err)
1677 break;
1679 STAILQ_FOREACH(qid, &matched_ids, entry) {
1680 if (obj_type != GOT_OBJ_TYPE_ANY) {
1681 int matched_type;
1682 err = got_object_get_type(&matched_type, repo,
1683 &qid->id);
1684 if (err)
1685 goto done;
1686 if (matched_type != obj_type)
1687 continue;
1689 if (*unique_id == NULL) {
1690 *unique_id = got_object_id_dup(&qid->id);
1691 if (*unique_id == NULL) {
1692 err = got_error_from_errno("malloc");
1693 goto done;
1695 } else {
1696 if (got_object_id_cmp(*unique_id,
1697 &qid->id) == 0)
1698 continue; /* packed multiple times */
1699 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1700 goto done;
1704 done:
1705 got_object_id_queue_free(&matched_ids);
1706 if (err) {
1707 free(*unique_id);
1708 *unique_id = NULL;
1710 return err;
1713 static const struct got_error *
1714 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1715 const char *object_dir, const char *id_str_prefix, int obj_type,
1716 struct got_repository *repo)
1718 const struct got_error *err = NULL;
1719 char *path, *id_str = NULL;
1720 DIR *dir = NULL;
1721 struct dirent *dent;
1722 struct got_object_id id;
1724 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1725 err = got_error_from_errno("asprintf");
1726 goto done;
1729 dir = opendir(path);
1730 if (dir == NULL) {
1731 if (errno == ENOENT) {
1732 err = NULL;
1733 goto done;
1735 err = got_error_from_errno2("opendir", path);
1736 goto done;
1738 while ((dent = readdir(dir)) != NULL) {
1739 int cmp;
1741 free(id_str);
1742 id_str = NULL;
1744 if (strcmp(dent->d_name, ".") == 0 ||
1745 strcmp(dent->d_name, "..") == 0)
1746 continue;
1748 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1749 err = got_error_from_errno("asprintf");
1750 goto done;
1753 if (!got_parse_sha1_digest(id.sha1, id_str))
1754 continue;
1757 * Directory entries do not necessarily appear in
1758 * sorted order, so we must iterate over all of them.
1760 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1761 if (cmp != 0)
1762 continue;
1764 if (*unique_id == NULL) {
1765 if (obj_type != GOT_OBJ_TYPE_ANY) {
1766 int matched_type;
1767 err = got_object_get_type(&matched_type, repo,
1768 &id);
1769 if (err)
1770 goto done;
1771 if (matched_type != obj_type)
1772 continue;
1774 *unique_id = got_object_id_dup(&id);
1775 if (*unique_id == NULL) {
1776 err = got_error_from_errno("got_object_id_dup");
1777 goto done;
1779 } else {
1780 if (got_object_id_cmp(*unique_id, &id) == 0)
1781 continue; /* both packed and loose */
1782 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1783 goto done;
1786 done:
1787 if (dir && closedir(dir) != 0 && err == NULL)
1788 err = got_error_from_errno("closedir");
1789 if (err) {
1790 free(*unique_id);
1791 *unique_id = NULL;
1793 free(id_str);
1794 free(path);
1795 return err;
1798 const struct got_error *
1799 got_repo_match_object_id_prefix(struct got_object_id **id,
1800 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1802 const struct got_error *err = NULL;
1803 char *path_objects = NULL, *object_dir = NULL;
1804 size_t len;
1805 int i;
1807 *id = NULL;
1809 path_objects = got_repo_get_path_objects(repo);
1811 len = strlen(id_str_prefix);
1812 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1813 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1814 goto done;
1817 for (i = 0; i < len; i++) {
1818 if (isxdigit((unsigned char)id_str_prefix[i]))
1819 continue;
1820 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1821 goto done;
1824 if (len >= 2) {
1825 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1826 if (err)
1827 goto done;
1828 object_dir = strndup(id_str_prefix, 2);
1829 if (object_dir == NULL) {
1830 err = got_error_from_errno("strdup");
1831 goto done;
1833 err = match_loose_object(id, path_objects, object_dir,
1834 id_str_prefix, obj_type, repo);
1835 } else if (len == 1) {
1836 int i;
1837 for (i = 0; i < 0xf; i++) {
1838 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1839 == -1) {
1840 err = got_error_from_errno("asprintf");
1841 goto done;
1843 err = match_packed_object(id, repo, object_dir,
1844 obj_type);
1845 if (err)
1846 goto done;
1847 err = match_loose_object(id, path_objects, object_dir,
1848 id_str_prefix, obj_type, repo);
1849 if (err)
1850 goto done;
1852 } else {
1853 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1854 goto done;
1856 done:
1857 free(path_objects);
1858 free(object_dir);
1859 if (err) {
1860 free(*id);
1861 *id = NULL;
1862 } else if (*id == NULL) {
1863 switch (obj_type) {
1864 case GOT_OBJ_TYPE_BLOB:
1865 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1866 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1867 break;
1868 case GOT_OBJ_TYPE_TREE:
1869 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1870 GOT_OBJ_LABEL_TREE, id_str_prefix);
1871 break;
1872 case GOT_OBJ_TYPE_COMMIT:
1873 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1874 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1875 break;
1876 case GOT_OBJ_TYPE_TAG:
1877 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1878 GOT_OBJ_LABEL_TAG, id_str_prefix);
1879 break;
1880 default:
1881 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1882 break;
1886 return err;
1889 const struct got_error *
1890 got_repo_match_object_id(struct got_object_id **id, char **label,
1891 const char *id_str, int obj_type, struct got_reflist_head *refs,
1892 struct got_repository *repo)
1894 const struct got_error *err;
1895 struct got_tag_object *tag;
1896 struct got_reference *ref = NULL;
1898 *id = NULL;
1899 if (label)
1900 *label = NULL;
1902 if (refs) {
1903 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1904 refs, repo);
1905 if (err == NULL) {
1906 *id = got_object_id_dup(
1907 got_object_tag_get_object_id(tag));
1908 if (*id == NULL)
1909 err = got_error_from_errno("got_object_id_dup");
1910 else if (label && asprintf(label, "refs/tags/%s",
1911 got_object_tag_get_name(tag)) == -1) {
1912 err = got_error_from_errno("asprintf");
1913 free(*id);
1914 *id = NULL;
1916 got_object_tag_close(tag);
1917 return err;
1918 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1919 err->code != GOT_ERR_NO_OBJ)
1920 return err;
1923 err = got_ref_open(&ref, repo, id_str, 0);
1924 if (err == NULL) {
1925 err = got_ref_resolve(id, repo, ref);
1926 if (err)
1927 goto done;
1928 if (label) {
1929 *label = strdup(got_ref_get_name(ref));
1930 if (*label == NULL) {
1931 err = got_error_from_errno("strdup");
1932 goto done;
1935 } else {
1936 if (err->code != GOT_ERR_NOT_REF &&
1937 err->code != GOT_ERR_BAD_REF_NAME)
1938 goto done;
1939 err = got_repo_match_object_id_prefix(id, id_str,
1940 obj_type, repo);
1941 if (err) {
1942 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1943 err = got_error_not_ref(id_str);
1944 goto done;
1946 if (label) {
1947 err = got_object_id_str(label, *id);
1948 if (*label == NULL) {
1949 err = got_error_from_errno("strdup");
1950 goto done;
1954 done:
1955 if (ref)
1956 got_ref_close(ref);
1957 return err;
1960 const struct got_error *
1961 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1962 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1964 const struct got_error *err = NULL;
1965 struct got_reflist_entry *re;
1966 struct got_object_id *tag_id;
1967 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1969 *tag = NULL;
1971 TAILQ_FOREACH(re, refs, entry) {
1972 const char *refname;
1973 refname = got_ref_get_name(re->ref);
1974 if (got_ref_is_symbolic(re->ref))
1975 continue;
1976 if (strncmp(refname, "refs/tags/", 10) != 0)
1977 continue;
1978 if (!name_is_absolute)
1979 refname += strlen("refs/tags/");
1980 if (strcmp(refname, name) != 0)
1981 continue;
1982 err = got_ref_resolve(&tag_id, repo, re->ref);
1983 if (err)
1984 break;
1985 err = got_object_open_as_tag(tag, repo, tag_id);
1986 free(tag_id);
1987 if (err)
1988 break;
1989 if (obj_type == GOT_OBJ_TYPE_ANY ||
1990 got_object_tag_get_object_type(*tag) == obj_type)
1991 break;
1992 got_object_tag_close(*tag);
1993 *tag = NULL;
1996 if (err == NULL && *tag == NULL)
1997 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1998 GOT_OBJ_LABEL_TAG, name);
1999 return err;
2002 static const struct got_error *
2003 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2004 const char *name, mode_t mode, struct got_object_id *blob_id)
2006 const struct got_error *err = NULL;
2008 *new_te = NULL;
2010 *new_te = calloc(1, sizeof(**new_te));
2011 if (*new_te == NULL)
2012 return got_error_from_errno("calloc");
2014 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2015 sizeof((*new_te)->name)) {
2016 err = got_error(GOT_ERR_NO_SPACE);
2017 goto done;
2020 if (S_ISLNK(mode)) {
2021 (*new_te)->mode = S_IFLNK;
2022 } else {
2023 (*new_te)->mode = S_IFREG;
2024 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2026 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2027 done:
2028 if (err && *new_te) {
2029 free(*new_te);
2030 *new_te = NULL;
2032 return err;
2035 static const struct got_error *
2036 import_file(struct got_tree_entry **new_te, struct dirent *de,
2037 const char *path, struct got_repository *repo)
2039 const struct got_error *err;
2040 struct got_object_id *blob_id = NULL;
2041 char *filepath;
2042 struct stat sb;
2044 if (asprintf(&filepath, "%s%s%s", path,
2045 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2046 return got_error_from_errno("asprintf");
2048 if (lstat(filepath, &sb) != 0) {
2049 err = got_error_from_errno2("lstat", path);
2050 goto done;
2053 err = got_object_blob_create(&blob_id, filepath, repo);
2054 if (err)
2055 goto done;
2057 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2058 blob_id);
2059 done:
2060 free(filepath);
2061 if (err)
2062 free(blob_id);
2063 return err;
2066 static const struct got_error *
2067 insert_tree_entry(struct got_tree_entry *new_te,
2068 struct got_pathlist_head *paths)
2070 const struct got_error *err = NULL;
2071 struct got_pathlist_entry *new_pe;
2073 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2074 if (err)
2075 return err;
2076 if (new_pe == NULL)
2077 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2078 return NULL;
2081 static const struct got_error *write_tree(struct got_object_id **,
2082 const char *, struct got_pathlist_head *, struct got_repository *,
2083 got_repo_import_cb progress_cb, void *progress_arg);
2085 static const struct got_error *
2086 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2087 const char *path, struct got_pathlist_head *ignores,
2088 struct got_repository *repo,
2089 got_repo_import_cb progress_cb, void *progress_arg)
2091 const struct got_error *err;
2092 struct got_object_id *id = NULL;
2093 char *subdirpath;
2095 if (asprintf(&subdirpath, "%s%s%s", path,
2096 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2097 return got_error_from_errno("asprintf");
2099 (*new_te) = calloc(1, sizeof(**new_te));
2100 if (*new_te == NULL)
2101 return got_error_from_errno("calloc");
2102 (*new_te)->mode = S_IFDIR;
2103 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2104 sizeof((*new_te)->name)) {
2105 err = got_error(GOT_ERR_NO_SPACE);
2106 goto done;
2108 err = write_tree(&id, subdirpath, ignores, repo,
2109 progress_cb, progress_arg);
2110 if (err)
2111 goto done;
2112 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2114 done:
2115 free(id);
2116 free(subdirpath);
2117 if (err) {
2118 free(*new_te);
2119 *new_te = NULL;
2121 return err;
2124 static const struct got_error *
2125 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2126 struct got_pathlist_head *ignores, struct got_repository *repo,
2127 got_repo_import_cb progress_cb, void *progress_arg)
2129 const struct got_error *err = NULL;
2130 DIR *dir;
2131 struct dirent *de;
2132 int nentries;
2133 struct got_tree_entry *new_te = NULL;
2134 struct got_pathlist_head paths;
2135 struct got_pathlist_entry *pe;
2137 *new_tree_id = NULL;
2139 TAILQ_INIT(&paths);
2141 dir = opendir(path_dir);
2142 if (dir == NULL) {
2143 err = got_error_from_errno2("opendir", path_dir);
2144 goto done;
2147 nentries = 0;
2148 while ((de = readdir(dir)) != NULL) {
2149 int ignore = 0;
2150 int type;
2152 if (strcmp(de->d_name, ".") == 0 ||
2153 strcmp(de->d_name, "..") == 0)
2154 continue;
2156 err = got_path_dirent_type(&type, path_dir, de);
2157 if (err)
2158 goto done;
2160 TAILQ_FOREACH(pe, ignores, entry) {
2161 if (type == DT_DIR && pe->path_len > 0 &&
2162 pe->path[pe->path_len - 1] == '/') {
2163 char stripped[PATH_MAX];
2165 if (strlcpy(stripped, pe->path,
2166 sizeof(stripped)) >= sizeof(stripped)) {
2167 err = got_error(GOT_ERR_NO_SPACE);
2168 goto done;
2170 got_path_strip_trailing_slashes(stripped);
2171 if (fnmatch(stripped, de->d_name, 0) == 0) {
2172 ignore = 1;
2173 break;
2175 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2176 ignore = 1;
2177 break;
2180 if (ignore)
2181 continue;
2183 if (type == DT_DIR) {
2184 err = import_subdir(&new_te, de, path_dir,
2185 ignores, repo, progress_cb, progress_arg);
2186 if (err) {
2187 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2188 goto done;
2189 err = NULL;
2190 continue;
2192 } else if (type == DT_REG || type == DT_LNK) {
2193 err = import_file(&new_te, de, path_dir, repo);
2194 if (err)
2195 goto done;
2196 } else
2197 continue;
2199 err = insert_tree_entry(new_te, &paths);
2200 if (err)
2201 goto done;
2202 nentries++;
2205 if (TAILQ_EMPTY(&paths)) {
2206 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2207 "cannot create tree without any entries");
2208 goto done;
2211 TAILQ_FOREACH(pe, &paths, entry) {
2212 struct got_tree_entry *te = pe->data;
2213 char *path;
2214 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2215 continue;
2216 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2217 err = got_error_from_errno("asprintf");
2218 goto done;
2220 err = (*progress_cb)(progress_arg, path);
2221 free(path);
2222 if (err)
2223 goto done;
2226 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2227 done:
2228 if (dir)
2229 closedir(dir);
2230 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2231 return err;
2234 const struct got_error *
2235 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2236 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2237 struct got_repository *repo, got_repo_import_cb progress_cb,
2238 void *progress_arg)
2240 const struct got_error *err;
2241 struct got_object_id *new_tree_id;
2243 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2244 progress_cb, progress_arg);
2245 if (err)
2246 return err;
2248 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2249 author, time(NULL), author, time(NULL), logmsg, repo);
2250 free(new_tree_id);
2251 return err;
2254 const struct got_error *
2255 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2256 struct got_repository *repo)
2258 const struct got_error *err = NULL;
2259 char *path_objects = NULL, *path = NULL;
2260 DIR *dir = NULL;
2261 struct got_object_id id;
2262 int i;
2264 *nobjects = 0;
2265 *ondisk_size = 0;
2267 path_objects = got_repo_get_path_objects(repo);
2268 if (path_objects == NULL)
2269 return got_error_from_errno("got_repo_get_path_objects");
2271 for (i = 0; i <= 0xff; i++) {
2272 struct dirent *dent;
2274 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2275 err = got_error_from_errno("asprintf");
2276 break;
2279 dir = opendir(path);
2280 if (dir == NULL) {
2281 if (errno == ENOENT) {
2282 err = NULL;
2283 continue;
2285 err = got_error_from_errno2("opendir", path);
2286 break;
2289 while ((dent = readdir(dir)) != NULL) {
2290 char *id_str;
2291 int fd;
2292 struct stat sb;
2294 if (strcmp(dent->d_name, ".") == 0 ||
2295 strcmp(dent->d_name, "..") == 0)
2296 continue;
2298 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2299 err = got_error_from_errno("asprintf");
2300 goto done;
2303 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2304 free(id_str);
2305 continue;
2307 free(id_str);
2309 err = got_object_open_loose_fd(&fd, &id, repo);
2310 if (err)
2311 goto done;
2313 if (fstat(fd, &sb) == -1) {
2314 err = got_error_from_errno("fstat");
2315 close(fd);
2316 goto done;
2318 (*nobjects)++;
2319 (*ondisk_size) += sb.st_size;
2321 if (close(fd) == -1) {
2322 err = got_error_from_errno("close");
2323 goto done;
2327 if (closedir(dir) != 0) {
2328 err = got_error_from_errno("closedir");
2329 goto done;
2331 dir = NULL;
2333 free(path);
2334 path = NULL;
2336 done:
2337 if (dir && closedir(dir) != 0 && err == NULL)
2338 err = got_error_from_errno("closedir");
2340 if (err) {
2341 *nobjects = 0;
2342 *ondisk_size = 0;
2344 free(path_objects);
2345 free(path);
2346 return err;
2349 const struct got_error *
2350 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2351 off_t *total_packsize, struct got_repository *repo)
2353 const struct got_error *err = NULL;
2354 DIR *packdir = NULL;
2355 struct dirent *dent;
2356 struct got_packidx *packidx = NULL;
2357 char *path_packidx;
2358 char *path_packfile;
2359 int packdir_fd;
2360 struct stat sb;
2362 *npackfiles = 0;
2363 *nobjects = 0;
2364 *total_packsize = 0;
2366 packdir_fd = openat(got_repo_get_fd(repo),
2367 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2368 if (packdir_fd == -1) {
2369 return got_error_from_errno_fmt("openat: %s/%s",
2370 got_repo_get_path_git_dir(repo),
2371 GOT_OBJECTS_PACK_DIR);
2374 packdir = fdopendir(packdir_fd);
2375 if (packdir == NULL) {
2376 err = got_error_from_errno("fdopendir");
2377 goto done;
2380 while ((dent = readdir(packdir)) != NULL) {
2381 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2382 continue;
2384 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2385 dent->d_name) == -1) {
2386 err = got_error_from_errno("asprintf");
2387 goto done;
2390 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2391 path_packidx, 0);
2392 free(path_packidx);
2393 if (err)
2394 goto done;
2396 if (fstat(packidx->fd, &sb) == -1)
2397 goto done;
2398 *total_packsize += sb.st_size;
2400 err = got_packidx_get_packfile_path(&path_packfile,
2401 packidx->path_packidx);
2402 if (err)
2403 goto done;
2405 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2406 0) == -1) {
2407 free(path_packfile);
2408 goto done;
2410 free(path_packfile);
2411 *total_packsize += sb.st_size;
2413 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2415 (*npackfiles)++;
2417 got_packidx_close(packidx);
2418 packidx = NULL;
2420 done:
2421 if (packidx)
2422 got_packidx_close(packidx);
2423 if (packdir && closedir(packdir) != 0 && err == NULL)
2424 err = got_error_from_errno("closedir");
2425 if (err) {
2426 *npackfiles = 0;
2427 *nobjects = 0;
2428 *total_packsize = 0;
2430 return err;
2433 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2434 got_packidx_bloom_filter_cmp);