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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 #endif
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
71 const char *
72 got_repo_get_path(struct got_repository *repo)
73 {
74 return repo->path;
75 }
77 const char *
78 got_repo_get_path_git_dir(struct got_repository *repo)
79 {
80 return repo->path_git_dir;
81 }
83 int
84 got_repo_get_fd(struct got_repository *repo)
85 {
86 return repo->gitdir_fd;
87 }
89 const char *
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
91 {
92 return repo->gitconfig_author_name;
93 }
95 const char *
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
97 {
98 return repo->gitconfig_author_email;
99 }
101 const char *
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
107 const char *
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
113 const char *
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
119 void
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
127 int
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
133 static char *
134 get_path_git_child(struct got_repository *repo, const char *basename)
136 char *path_child;
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
139 basename) == -1)
140 return NULL;
142 return path_child;
145 char *
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
151 char *
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
157 char *
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
163 char *
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
169 static char *
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
175 char *
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
181 char *
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
193 void
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
201 static int
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
208 int ret = 0;
209 struct stat sb;
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
213 goto done;
214 if (!S_ISDIR(sb.st_mode))
215 goto done;
217 if (lstat(path_objects, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_refs, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_head, &sb) == -1)
228 goto done;
229 if (!S_ISREG(sb.st_mode))
230 goto done;
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
234 goto done;
235 got_ref_close(head_ref);
237 ret = 1;
238 done:
239 free(path_objects);
240 free(path_refs);
241 free(path_head);
242 return ret;
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
250 int i;
252 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
253 if (*pack_fds == NULL)
254 return got_error_from_errno("calloc");
256 /*
257 * got_repo_pack_fds_close will try to close all of the
258 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
259 * a value from got_opentempfd(), which would result in a close(0) if
260 * we do not initialize to -1 here.
261 */
262 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
263 (*pack_fds)[i] = -1;
265 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
266 (*pack_fds)[i] = got_opentempfd();
267 if ((*pack_fds)[i] == -1) {
268 err = got_error_from_errno("got_opentempfd");
269 got_repo_pack_fds_close(*pack_fds);
270 *pack_fds = NULL;
271 return err;
275 return NULL;
278 const struct got_error *
279 got_repo_pack_fds_close(int *pack_fds)
281 const struct got_error *err = NULL;
282 int i;
284 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
285 if (pack_fds[i] == -1)
286 continue;
287 if (close(pack_fds[i]) == -1) {
288 err = got_error_from_errno("close");
289 break;
292 free(pack_fds);
293 return err;
296 const struct got_error *
297 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
298 struct got_object *obj)
300 #ifndef GOT_NO_OBJ_CACHE
301 const struct got_error *err = NULL;
302 err = got_object_cache_add(&repo->objcache, id, obj);
303 if (err) {
304 if (err->code == GOT_ERR_OBJ_EXISTS ||
305 err->code == GOT_ERR_OBJ_TOO_LARGE)
306 err = NULL;
307 return err;
309 obj->refcnt++;
310 #endif
311 return NULL;
314 struct got_object *
315 got_repo_get_cached_object(struct got_repository *repo,
316 struct got_object_id *id)
318 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
321 const struct got_error *
322 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
323 struct got_tree_object *tree)
325 #ifndef GOT_NO_OBJ_CACHE
326 const struct got_error *err = NULL;
327 err = got_object_cache_add(&repo->treecache, id, tree);
328 if (err) {
329 if (err->code == GOT_ERR_OBJ_EXISTS ||
330 err->code == GOT_ERR_OBJ_TOO_LARGE)
331 err = NULL;
332 return err;
334 tree->refcnt++;
335 #endif
336 return NULL;
339 struct got_tree_object *
340 got_repo_get_cached_tree(struct got_repository *repo,
341 struct got_object_id *id)
343 return (struct got_tree_object *)got_object_cache_get(
344 &repo->treecache, id);
347 const struct got_error *
348 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
349 struct got_commit_object *commit)
351 #ifndef GOT_NO_OBJ_CACHE
352 const struct got_error *err = NULL;
353 err = got_object_cache_add(&repo->commitcache, id, commit);
354 if (err) {
355 if (err->code == GOT_ERR_OBJ_EXISTS ||
356 err->code == GOT_ERR_OBJ_TOO_LARGE)
357 err = NULL;
358 return err;
360 commit->refcnt++;
361 #endif
362 return NULL;
365 struct got_commit_object *
366 got_repo_get_cached_commit(struct got_repository *repo,
367 struct got_object_id *id)
369 return (struct got_commit_object *)got_object_cache_get(
370 &repo->commitcache, id);
373 const struct got_error *
374 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
375 struct got_tag_object *tag)
377 #ifndef GOT_NO_OBJ_CACHE
378 const struct got_error *err = NULL;
379 err = got_object_cache_add(&repo->tagcache, id, tag);
380 if (err) {
381 if (err->code == GOT_ERR_OBJ_EXISTS ||
382 err->code == GOT_ERR_OBJ_TOO_LARGE)
383 err = NULL;
384 return err;
386 tag->refcnt++;
387 #endif
388 return NULL;
391 struct got_tag_object *
392 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
394 return (struct got_tag_object *)got_object_cache_get(
395 &repo->tagcache, id);
398 const struct got_error *
399 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
400 struct got_raw_object *raw)
402 #ifndef GOT_NO_OBJ_CACHE
403 const struct got_error *err = NULL;
404 err = got_object_cache_add(&repo->rawcache, id, raw);
405 if (err) {
406 if (err->code == GOT_ERR_OBJ_EXISTS ||
407 err->code == GOT_ERR_OBJ_TOO_LARGE)
408 err = NULL;
409 return err;
411 raw->refcnt++;
412 #endif
413 return NULL;
417 struct got_raw_object *
418 got_repo_get_cached_raw_object(struct got_repository *repo,
419 struct got_object_id *id)
421 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
425 static const struct got_error *
426 open_repo(struct got_repository *repo, const char *path)
428 const struct got_error *err = NULL;
430 repo->gitdir_fd = -1;
432 /* bare git repository? */
433 repo->path_git_dir = strdup(path);
434 if (repo->path_git_dir == NULL)
435 return got_error_from_errno("strdup");
436 if (is_git_repo(repo)) {
437 repo->path = strdup(repo->path_git_dir);
438 if (repo->path == NULL) {
439 err = got_error_from_errno("strdup");
440 goto done;
442 repo->gitdir_fd = open(repo->path_git_dir,
443 O_DIRECTORY | O_CLOEXEC);
444 if (repo->gitdir_fd == -1) {
445 err = got_error_from_errno2("open",
446 repo->path_git_dir);
447 goto done;
449 return NULL;
452 /* git repository with working tree? */
453 free(repo->path_git_dir);
454 repo->path_git_dir = NULL;
455 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
456 err = got_error_from_errno("asprintf");
457 goto done;
459 if (is_git_repo(repo)) {
460 repo->path = strdup(path);
461 if (repo->path == NULL) {
462 err = got_error_from_errno("strdup");
463 goto done;
465 repo->gitdir_fd = open(repo->path_git_dir,
466 O_DIRECTORY | O_CLOEXEC);
467 if (repo->gitdir_fd == -1) {
468 err = got_error_from_errno2("open",
469 repo->path_git_dir);
470 goto done;
472 return NULL;
475 err = got_error(GOT_ERR_NOT_GIT_REPO);
476 done:
477 if (err) {
478 free(repo->path);
479 repo->path = NULL;
480 free(repo->path_git_dir);
481 repo->path_git_dir = NULL;
482 if (repo->gitdir_fd != -1)
483 close(repo->gitdir_fd);
484 repo->gitdir_fd = -1;
487 return err;
490 static const struct got_error *
491 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
493 const struct got_error *err = NULL;
494 char *repo_gitconfig_path = NULL;
496 if (global_gitconfig_path) {
497 /* Read settings from ~/.gitconfig. */
498 int dummy_repo_version;
499 err = got_repo_read_gitconfig(&dummy_repo_version,
500 &repo->global_gitconfig_author_name,
501 &repo->global_gitconfig_author_email,
502 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
503 if (err)
504 return err;
507 /* Read repository's .git/config file. */
508 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
509 if (repo_gitconfig_path == NULL)
510 return got_error_from_errno("got_repo_get_path_gitconfig");
512 err = got_repo_read_gitconfig(
513 &repo->gitconfig_repository_format_version,
514 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
515 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
516 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
517 repo_gitconfig_path);
518 if (err)
519 goto done;
521 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
522 int i;
524 for (i = 0; i < repo->ngitconfig_remotes; i++) {
525 got_repo_free_remote_repo_data(
526 &repo->gitconfig_remotes[i]);
528 free(repo->gitconfig_remotes);
529 repo->gitconfig_remotes = NULL;
530 repo->ngitconfig_remotes = 0;
532 free(repo->gitconfig_author_name);
533 repo->gitconfig_author_name = NULL;
534 free(repo->gitconfig_author_email);
535 repo->gitconfig_author_email = NULL;
537 free(repo->global_gitconfig_author_name);
538 repo->global_gitconfig_author_name = NULL;
539 free(repo->global_gitconfig_author_email);
540 repo->global_gitconfig_author_email = NULL;
543 done:
544 free(repo_gitconfig_path);
545 return err;
548 static const struct got_error *
549 read_gotconfig(struct got_repository *repo)
551 const struct got_error *err = NULL;
552 char *gotconfig_path;
554 gotconfig_path = got_repo_get_path_gotconfig(repo);
555 if (gotconfig_path == NULL)
556 return got_error_from_errno("got_repo_get_path_gotconfig");
558 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
559 free(gotconfig_path);
560 return err;
563 /* Supported repository format extensions. */
564 static const char *const repo_extensions[] = {
565 "noop", /* Got supports repository format version 1. */
566 "preciousObjects", /* Supported by gotadmin cleanup. */
567 "worktreeConfig", /* Got does not care about Git work trees. */
568 };
570 const struct got_error *
571 got_repo_open(struct got_repository **repop, const char *path,
572 const char *global_gitconfig_path, int *pack_fds)
574 struct got_repository *repo = NULL;
575 const struct got_error *err = NULL;
576 char *repo_path = NULL;
577 size_t i, j = 0;
578 struct rlimit rl;
580 *repop = NULL;
582 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
583 return got_error_from_errno("getrlimit");
585 repo = calloc(1, sizeof(*repo));
586 if (repo == NULL)
587 return got_error_from_errno("calloc");
589 RB_INIT(&repo->packidx_bloom_filters);
590 TAILQ_INIT(&repo->packidx_paths);
592 for (i = 0; i < nitems(repo->privsep_children); i++) {
593 memset(&repo->privsep_children[i], 0,
594 sizeof(repo->privsep_children[0]));
595 repo->privsep_children[i].imsg_fd = -1;
598 err = got_object_cache_init(&repo->objcache,
599 GOT_OBJECT_CACHE_TYPE_OBJ);
600 if (err)
601 goto done;
602 err = got_object_cache_init(&repo->treecache,
603 GOT_OBJECT_CACHE_TYPE_TREE);
604 if (err)
605 goto done;
606 err = got_object_cache_init(&repo->commitcache,
607 GOT_OBJECT_CACHE_TYPE_COMMIT);
608 if (err)
609 goto done;
610 err = got_object_cache_init(&repo->tagcache,
611 GOT_OBJECT_CACHE_TYPE_TAG);
612 if (err)
613 goto done;
614 err = got_object_cache_init(&repo->rawcache,
615 GOT_OBJECT_CACHE_TYPE_RAW);
616 if (err)
617 goto done;
619 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
620 if (repo->pack_cache_size > rl.rlim_cur / 8)
621 repo->pack_cache_size = rl.rlim_cur / 8;
622 for (i = 0; i < nitems(repo->packs); i++) {
623 if (i < repo->pack_cache_size) {
624 repo->packs[i].basefd = pack_fds[j++];
625 repo->packs[i].accumfd = pack_fds[j++];
626 } else {
627 repo->packs[i].basefd = -1;
628 repo->packs[i].accumfd = -1;
631 repo->pinned_pack = -1;
632 repo->pinned_packidx = -1;
633 repo->pinned_pid = 0;
635 repo_path = realpath(path, NULL);
636 if (repo_path == NULL) {
637 err = got_error_from_errno2("realpath", path);
638 goto done;
641 for (;;) {
642 char *parent_path;
644 err = open_repo(repo, repo_path);
645 if (err == NULL)
646 break;
647 if (err->code != GOT_ERR_NOT_GIT_REPO)
648 goto done;
649 if (repo_path[0] == '/' && repo_path[1] == '\0') {
650 err = got_error(GOT_ERR_NOT_GIT_REPO);
651 goto done;
653 err = got_path_dirname(&parent_path, repo_path);
654 if (err)
655 goto done;
656 free(repo_path);
657 repo_path = parent_path;
660 err = read_gotconfig(repo);
661 if (err)
662 goto done;
664 err = read_gitconfig(repo, global_gitconfig_path);
665 if (err)
666 goto done;
667 if (repo->gitconfig_repository_format_version != 0) {
668 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
669 goto done;
671 for (i = 0; i < repo->nextensions; i++) {
672 char *ext = repo->extensions[i];
673 int j, supported = 0;
674 for (j = 0; j < nitems(repo_extensions); j++) {
675 if (strcmp(ext, repo_extensions[j]) == 0) {
676 supported = 1;
677 break;
680 if (!supported) {
681 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
682 goto done;
686 err = got_repo_list_packidx(&repo->packidx_paths, repo);
687 done:
688 if (err)
689 got_repo_close(repo);
690 else
691 *repop = repo;
692 free(repo_path);
693 return err;
696 const struct got_error *
697 got_repo_close(struct got_repository *repo)
699 const struct got_error *err = NULL, *child_err;
700 struct got_packidx_bloom_filter *bf;
701 struct got_pathlist_entry *pe;
702 size_t i;
704 for (i = 0; i < repo->pack_cache_size; i++) {
705 if (repo->packidx_cache[i] == NULL)
706 break;
707 got_packidx_close(repo->packidx_cache[i]);
710 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
711 &repo->packidx_bloom_filters))) {
712 RB_REMOVE(got_packidx_bloom_filter_tree,
713 &repo->packidx_bloom_filters, bf);
714 bloom_free(bf->bloom);
715 free(bf->bloom);
716 free(bf);
719 for (i = 0; i < repo->pack_cache_size; i++)
720 if (repo->packs[i].path_packfile)
721 if (repo->packs[i].path_packfile)
722 got_pack_close(&repo->packs[i]);
724 free(repo->path);
725 free(repo->path_git_dir);
727 got_object_cache_close(&repo->objcache);
728 got_object_cache_close(&repo->treecache);
729 got_object_cache_close(&repo->commitcache);
730 got_object_cache_close(&repo->tagcache);
731 got_object_cache_close(&repo->rawcache);
733 for (i = 0; i < nitems(repo->privsep_children); i++) {
734 if (repo->privsep_children[i].imsg_fd == -1)
735 continue;
736 imsg_clear(repo->privsep_children[i].ibuf);
737 free(repo->privsep_children[i].ibuf);
738 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
739 child_err = got_privsep_wait_for_child(
740 repo->privsep_children[i].pid);
741 if (child_err && err == NULL)
742 err = child_err;
743 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
744 err == NULL)
745 err = got_error_from_errno("close");
748 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
749 err == NULL)
750 err = got_error_from_errno("close");
752 if (repo->gotconfig)
753 got_gotconfig_free(repo->gotconfig);
754 free(repo->gitconfig_author_name);
755 free(repo->gitconfig_author_email);
756 for (i = 0; i < repo->ngitconfig_remotes; i++)
757 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
758 free(repo->gitconfig_remotes);
759 for (i = 0; i < repo->nextensions; i++)
760 free(repo->extensions[i]);
761 free(repo->extensions);
763 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
764 free((void *)pe->path);
765 got_pathlist_free(&repo->packidx_paths);
766 free(repo);
768 return err;
771 void
772 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
774 int i;
776 free(repo->name);
777 repo->name = NULL;
778 free(repo->fetch_url);
779 repo->fetch_url = NULL;
780 free(repo->send_url);
781 repo->send_url = NULL;
782 for (i = 0; i < repo->nfetch_branches; i++)
783 free(repo->fetch_branches[i]);
784 free(repo->fetch_branches);
785 repo->fetch_branches = NULL;
786 repo->nfetch_branches = 0;
787 for (i = 0; i < repo->nsend_branches; i++)
788 free(repo->send_branches[i]);
789 free(repo->send_branches);
790 repo->send_branches = NULL;
791 repo->nsend_branches = 0;
794 const struct got_error *
795 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
796 const char *input_path)
798 const struct got_error *err = NULL;
799 const char *repo_abspath = NULL;
800 size_t repolen, len;
801 char *canonpath, *path = NULL;
803 *in_repo_path = NULL;
805 canonpath = strdup(input_path);
806 if (canonpath == NULL) {
807 err = got_error_from_errno("strdup");
808 goto done;
810 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
811 if (err)
812 goto done;
814 repo_abspath = got_repo_get_path(repo);
816 if (canonpath[0] == '\0') {
817 path = strdup(canonpath);
818 if (path == NULL) {
819 err = got_error_from_errno("strdup");
820 goto done;
822 } else {
823 path = realpath(canonpath, NULL);
824 if (path == NULL) {
825 if (errno != ENOENT) {
826 err = got_error_from_errno2("realpath",
827 canonpath);
828 goto done;
830 /*
831 * Path is not on disk.
832 * Assume it is already relative to repository root.
833 */
834 path = strdup(canonpath);
835 if (path == NULL) {
836 err = got_error_from_errno("strdup");
837 goto done;
841 repolen = strlen(repo_abspath);
842 len = strlen(path);
845 if (strcmp(path, repo_abspath) == 0) {
846 free(path);
847 path = strdup("");
848 if (path == NULL) {
849 err = got_error_from_errno("strdup");
850 goto done;
852 } else if (len > repolen &&
853 got_path_is_child(path, repo_abspath, repolen)) {
854 /* Matched an on-disk path inside repository. */
855 if (got_repo_is_bare(repo)) {
856 /*
857 * Matched an on-disk path inside repository
858 * database. Treat input as repository-relative.
859 */
860 free(path);
861 path = canonpath;
862 canonpath = NULL;
863 } else {
864 char *child;
865 /* Strip common prefix with repository path. */
866 err = got_path_skip_common_ancestor(&child,
867 repo_abspath, path);
868 if (err)
869 goto done;
870 free(path);
871 path = child;
873 } else {
874 /*
875 * Matched unrelated on-disk path.
876 * Treat input as repository-relative.
877 */
878 free(path);
879 path = canonpath;
880 canonpath = NULL;
884 /* Make in-repository path absolute */
885 if (path[0] != '/') {
886 char *abspath;
887 if (asprintf(&abspath, "/%s", path) == -1) {
888 err = got_error_from_errno("asprintf");
889 goto done;
891 free(path);
892 path = abspath;
895 done:
896 free(canonpath);
897 if (err)
898 free(path);
899 else
900 *in_repo_path = path;
901 return err;
904 static const struct got_error *
905 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
906 const char *path_packidx)
908 const struct got_error *err = NULL;
909 size_t i;
911 for (i = 0; i < repo->pack_cache_size; i++) {
912 if (repo->packidx_cache[i] == NULL)
913 break;
914 if (strcmp(repo->packidx_cache[i]->path_packidx,
915 path_packidx) == 0) {
916 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
919 if (i == repo->pack_cache_size) {
920 do {
921 i--;
922 } while (i > 0 && repo->pinned_packidx >= 0 &&
923 i == repo->pinned_packidx);
924 err = got_packidx_close(repo->packidx_cache[i]);
925 if (err)
926 return err;
929 repo->packidx_cache[i] = packidx;
931 return NULL;
934 int
935 got_repo_is_packidx_filename(const char *name, size_t len)
937 if (len != GOT_PACKIDX_NAMELEN)
938 return 0;
940 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
941 return 0;
943 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
944 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
945 return 0;
947 return 1;
950 static struct got_packidx_bloom_filter *
951 get_packidx_bloom_filter(struct got_repository *repo,
952 const char *path, size_t path_len)
954 struct got_packidx_bloom_filter key;
956 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
957 return NULL; /* XXX */
958 key.path_len = path_len;
960 return RB_FIND(got_packidx_bloom_filter_tree,
961 &repo->packidx_bloom_filters, &key);
964 int
965 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
966 const char *path_packidx, struct got_object_id *id)
968 struct got_packidx_bloom_filter *bf;
970 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
971 if (bf)
972 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
974 /* No bloom filter means this pack index must be searched. */
975 return 1;
978 static const struct got_error *
979 add_packidx_bloom_filter(struct got_repository *repo,
980 struct got_packidx *packidx, const char *path_packidx)
982 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
983 struct got_packidx_bloom_filter *bf;
984 size_t len;
986 /*
987 * Don't use bloom filters for very large pack index files.
988 * Large pack files will contain a relatively large fraction
989 * of our objects so we will likely need to visit them anyway.
990 * The more objects a pack file contains the higher the probability
991 * of a false-positive match from the bloom filter. And reading
992 * all object IDs from a large pack index file can be expensive.
993 */
994 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
995 return NULL;
997 /* Do we already have a filter for this pack index? */
998 if (get_packidx_bloom_filter(repo, path_packidx,
999 strlen(path_packidx)) != NULL)
1000 return NULL;
1002 bf = calloc(1, sizeof(*bf));
1003 if (bf == NULL)
1004 return got_error_from_errno("calloc");
1005 bf->bloom = calloc(1, sizeof(*bf->bloom));
1006 if (bf->bloom == NULL) {
1007 free(bf);
1008 return got_error_from_errno("calloc");
1011 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1012 if (len >= sizeof(bf->path)) {
1013 free(bf->bloom);
1014 free(bf);
1015 return got_error(GOT_ERR_NO_SPACE);
1017 bf->path_len = len;
1019 /* Minimum size supported by our bloom filter is 1000 entries. */
1020 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1021 for (i = 0; i < nobjects; i++) {
1022 struct got_packidx_object_id *id;
1023 id = &packidx->hdr.sorted_ids[i];
1024 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1027 RB_INSERT(got_packidx_bloom_filter_tree,
1028 &repo->packidx_bloom_filters, bf);
1029 return NULL;
1032 const struct got_error *
1033 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1034 struct got_repository *repo, struct got_object_id *id)
1036 const struct got_error *err;
1037 struct got_pathlist_entry *pe;
1038 size_t i;
1040 /* Search pack index cache. */
1041 for (i = 0; i < repo->pack_cache_size; i++) {
1042 if (repo->packidx_cache[i] == NULL)
1043 break;
1044 if (!got_repo_check_packidx_bloom_filter(repo,
1045 repo->packidx_cache[i]->path_packidx, id))
1046 continue; /* object will not be found in this index */
1047 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1048 if (*idx != -1) {
1049 *packidx = repo->packidx_cache[i];
1051 * Move this cache entry to the front. Repeatedly
1052 * searching a wrong pack index can be expensive.
1054 if (i > 0) {
1055 memmove(&repo->packidx_cache[1],
1056 &repo->packidx_cache[0],
1057 i * sizeof(repo->packidx_cache[0]));
1058 repo->packidx_cache[0] = *packidx;
1059 if (repo->pinned_packidx >= 0 &&
1060 repo->pinned_packidx < i)
1061 repo->pinned_packidx++;
1062 else if (repo->pinned_packidx == i)
1063 repo->pinned_packidx = 0;
1065 return NULL;
1068 /* No luck. Search the filesystem. */
1070 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1071 const char *path_packidx = pe->path;
1072 int is_cached = 0;
1074 if (!got_repo_check_packidx_bloom_filter(repo,
1075 pe->path, id))
1076 continue; /* object will not be found in this index */
1078 for (i = 0; i < repo->pack_cache_size; i++) {
1079 if (repo->packidx_cache[i] == NULL)
1080 break;
1081 if (strcmp(repo->packidx_cache[i]->path_packidx,
1082 path_packidx) == 0) {
1083 is_cached = 1;
1084 break;
1087 if (is_cached)
1088 continue; /* already searched */
1090 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1091 path_packidx, 0);
1092 if (err)
1093 goto done;
1095 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1096 if (err)
1097 goto done;
1099 err = cache_packidx(repo, *packidx, path_packidx);
1100 if (err)
1101 goto done;
1103 *idx = got_packidx_get_object_idx(*packidx, id);
1104 if (*idx != -1) {
1105 err = NULL; /* found the object */
1106 goto done;
1110 err = got_error_no_obj(id);
1111 done:
1112 return err;
1115 const struct got_error *
1116 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1117 struct got_repository *repo)
1119 const struct got_error *err = NULL;
1120 DIR *packdir = NULL;
1121 struct dirent *dent;
1122 char *path_packidx = NULL;
1123 int packdir_fd;
1124 struct stat sb;
1126 packdir_fd = openat(got_repo_get_fd(repo),
1127 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1128 if (packdir_fd == -1) {
1129 return got_error_from_errno_fmt("openat: %s/%s",
1130 got_repo_get_path_git_dir(repo),
1131 GOT_OBJECTS_PACK_DIR);
1134 packdir = fdopendir(packdir_fd);
1135 if (packdir == NULL) {
1136 err = got_error_from_errno("fdopendir");
1137 goto done;
1140 if (fstat(packdir_fd, &sb) == -1) {
1141 err = got_error_from_errno("fstat");
1142 goto done;
1144 repo->pack_path_mtime = sb.st_mtime;
1146 while ((dent = readdir(packdir)) != NULL) {
1147 if (!got_repo_is_packidx_filename(dent->d_name,
1148 strlen(dent->d_name)))
1149 continue;
1151 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1152 dent->d_name) == -1) {
1153 err = got_error_from_errno("asprintf");
1154 path_packidx = NULL;
1155 break;
1158 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1159 if (err)
1160 break;
1162 done:
1163 if (err)
1164 free(path_packidx);
1165 if (packdir && closedir(packdir) != 0 && err == NULL)
1166 err = got_error_from_errno("closedir");
1167 return err;
1170 const struct got_error *
1171 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1172 struct got_repository *repo)
1174 const struct got_error *err;
1175 size_t i;
1177 *packidx = NULL;
1179 /* Search pack index cache. */
1180 for (i = 0; i < repo->pack_cache_size; i++) {
1181 if (repo->packidx_cache[i] == NULL)
1182 break;
1183 if (strcmp(repo->packidx_cache[i]->path_packidx,
1184 path_packidx) == 0) {
1185 *packidx = repo->packidx_cache[i];
1186 return NULL;
1189 /* No luck. Search the filesystem. */
1191 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1192 path_packidx, 0);
1193 if (err)
1194 return err;
1196 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1197 if (err)
1198 goto done;
1200 err = cache_packidx(repo, *packidx, path_packidx);
1201 done:
1202 if (err) {
1203 got_packidx_close(*packidx);
1204 *packidx = NULL;
1206 return err;
1209 static const struct got_error *
1210 read_packfile_hdr(int fd, struct got_packidx *packidx)
1212 const struct got_error *err = NULL;
1213 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1214 struct got_packfile_hdr hdr;
1215 ssize_t n;
1217 n = read(fd, &hdr, sizeof(hdr));
1218 if (n < 0)
1219 return got_error_from_errno("read");
1220 if (n != sizeof(hdr))
1221 return got_error(GOT_ERR_BAD_PACKFILE);
1223 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1224 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1225 be32toh(hdr.nobjects) != totobj)
1226 err = got_error(GOT_ERR_BAD_PACKFILE);
1228 return err;
1231 static const struct got_error *
1232 open_packfile(int *fd, struct got_repository *repo,
1233 const char *relpath, struct got_packidx *packidx)
1235 const struct got_error *err = NULL;
1237 *fd = openat(got_repo_get_fd(repo), relpath,
1238 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1239 if (*fd == -1)
1240 return got_error_from_errno_fmt("openat: %s/%s",
1241 got_repo_get_path_git_dir(repo), relpath);
1243 if (packidx) {
1244 err = read_packfile_hdr(*fd, packidx);
1245 if (err) {
1246 close(*fd);
1247 *fd = -1;
1251 return err;
1254 const struct got_error *
1255 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1256 const char *path_packfile, struct got_packidx *packidx)
1258 const struct got_error *err = NULL;
1259 struct got_pack *pack = NULL;
1260 struct stat sb;
1261 size_t i;
1263 if (packp)
1264 *packp = NULL;
1266 for (i = 0; i < repo->pack_cache_size; i++) {
1267 pack = &repo->packs[i];
1268 if (pack->path_packfile == NULL)
1269 break;
1270 if (strcmp(pack->path_packfile, path_packfile) == 0)
1271 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1274 if (i == repo->pack_cache_size) {
1275 struct got_pack tmp;
1276 do {
1277 i--;
1278 } while (i > 0 && repo->pinned_pack >= 0 &&
1279 i == repo->pinned_pack);
1280 err = got_pack_close(&repo->packs[i]);
1281 if (err)
1282 return err;
1283 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1284 return got_error_from_errno("ftruncate");
1285 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1286 return got_error_from_errno("ftruncate");
1287 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1288 memcpy(&repo->packs[i], &repo->packs[0],
1289 sizeof(repo->packs[i]));
1290 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1291 if (repo->pinned_pack == 0)
1292 repo->pinned_pack = i;
1293 else if (repo->pinned_pack == i)
1294 repo->pinned_pack = 0;
1295 i = 0;
1298 pack = &repo->packs[i];
1300 pack->path_packfile = strdup(path_packfile);
1301 if (pack->path_packfile == NULL) {
1302 err = got_error_from_errno("strdup");
1303 goto done;
1306 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1307 if (err)
1308 goto done;
1310 if (fstat(pack->fd, &sb) != 0) {
1311 err = got_error_from_errno("fstat");
1312 goto done;
1314 pack->filesize = sb.st_size;
1316 pack->privsep_child = NULL;
1318 #ifndef GOT_PACK_NO_MMAP
1319 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1320 pack->fd, 0);
1321 if (pack->map == MAP_FAILED) {
1322 if (errno != ENOMEM) {
1323 err = got_error_from_errno("mmap");
1324 goto done;
1326 pack->map = NULL; /* fall back to read(2) */
1328 #endif
1329 done:
1330 if (err) {
1331 if (pack) {
1332 free(pack->path_packfile);
1333 memset(pack, 0, sizeof(*pack));
1335 } else if (packp)
1336 *packp = pack;
1337 return err;
1340 struct got_pack *
1341 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1343 struct got_pack *pack = NULL;
1344 size_t i;
1346 for (i = 0; i < repo->pack_cache_size; i++) {
1347 pack = &repo->packs[i];
1348 if (pack->path_packfile == NULL)
1349 break;
1350 if (strcmp(pack->path_packfile, path_packfile) == 0)
1351 return pack;
1354 return NULL;
1357 const struct got_error *
1358 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1359 struct got_pack *pack)
1361 size_t i;
1362 int pinned_pack = -1, pinned_packidx = -1;
1364 for (i = 0; i < repo->pack_cache_size; i++) {
1365 if (repo->packidx_cache[i] &&
1366 strcmp(repo->packidx_cache[i]->path_packidx,
1367 packidx->path_packidx) == 0)
1368 pinned_packidx = i;
1369 if (repo->packs[i].path_packfile &&
1370 strcmp(repo->packs[i].path_packfile,
1371 pack->path_packfile) == 0)
1372 pinned_pack = i;
1375 if (pinned_packidx == -1 || pinned_pack == -1)
1376 return got_error(GOT_ERR_PIN_PACK);
1378 repo->pinned_pack = pinned_pack;
1379 repo->pinned_packidx = pinned_packidx;
1380 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1381 return NULL;
1384 struct got_pack *
1385 got_repo_get_pinned_pack(struct got_repository *repo)
1387 if (repo->pinned_pack >= 0 &&
1388 repo->pinned_pack < repo->pack_cache_size)
1389 return &repo->packs[repo->pinned_pack];
1391 return NULL;
1394 void
1395 got_repo_unpin_pack(struct got_repository *repo)
1397 repo->pinned_packidx = -1;
1398 repo->pinned_pack = -1;
1399 repo->pinned_pid = 0;
1402 const struct got_error *
1403 got_repo_init(const char *repo_path, const char *head_name)
1405 const struct got_error *err = NULL;
1406 const char *dirnames[] = {
1407 GOT_OBJECTS_DIR,
1408 GOT_OBJECTS_PACK_DIR,
1409 GOT_REFS_DIR,
1411 const char *description_str = "Unnamed repository; "
1412 "edit this file 'description' to name the repository.";
1413 const char *headref = "ref: refs/heads/";
1414 const char *gitconfig_str = "[core]\n"
1415 "\trepositoryformatversion = 0\n"
1416 "\tfilemode = true\n"
1417 "\tbare = true\n";
1418 char *headref_str, *path;
1419 size_t i;
1421 if (!got_path_dir_is_empty(repo_path))
1422 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1424 for (i = 0; i < nitems(dirnames); i++) {
1425 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1426 return got_error_from_errno("asprintf");
1428 err = got_path_mkdir(path);
1429 free(path);
1430 if (err)
1431 return err;
1434 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1435 return got_error_from_errno("asprintf");
1436 err = got_path_create_file(path, description_str);
1437 free(path);
1438 if (err)
1439 return err;
1441 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1442 return got_error_from_errno("asprintf");
1443 if (asprintf(&headref_str, "%s%s", headref,
1444 head_name ? head_name : "main") == -1) {
1445 free(path);
1446 return got_error_from_errno("asprintf");
1448 err = got_path_create_file(path, headref_str);
1449 free(headref_str);
1450 free(path);
1451 if (err)
1452 return err;
1454 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1455 return got_error_from_errno("asprintf");
1456 err = got_path_create_file(path, gitconfig_str);
1457 free(path);
1458 if (err)
1459 return err;
1461 return NULL;
1464 static void
1465 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1467 struct got_pathlist_entry *pe;
1469 while (!TAILQ_EMPTY(packidx_paths)) {
1470 pe = TAILQ_FIRST(packidx_paths);
1471 TAILQ_REMOVE(packidx_paths, pe, entry);
1472 free((char *)pe->path);
1473 free(pe);
1477 static const struct got_error *
1478 match_packed_object(struct got_object_id **unique_id,
1479 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1481 const struct got_error *err = NULL;
1482 char *objects_pack_dir = NULL;
1483 struct got_object_id_queue matched_ids;
1484 struct got_pathlist_entry *pe;
1485 struct stat sb;
1487 STAILQ_INIT(&matched_ids);
1489 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1490 if (objects_pack_dir == NULL)
1491 return got_error_from_errno("got_repo_get_path_objects_pack");
1493 if (stat(objects_pack_dir, &sb) == -1) {
1494 if (errno != ENOENT) {
1495 err = got_error_from_errno2("stat", objects_pack_dir);
1496 goto done;
1498 } else if (sb.st_mtime != repo->pack_path_mtime) {
1499 purge_packidx_paths(&repo->packidx_paths);
1500 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1501 if (err)
1502 goto done;
1505 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1506 const char *path_packidx = pe->path;
1507 struct got_packidx *packidx;
1508 struct got_object_qid *qid;
1510 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1511 path_packidx, 0);
1512 if (err)
1513 break;
1515 err = got_packidx_match_id_str_prefix(&matched_ids,
1516 packidx, id_str_prefix);
1517 if (err) {
1518 got_packidx_close(packidx);
1519 break;
1521 err = got_packidx_close(packidx);
1522 if (err)
1523 break;
1525 STAILQ_FOREACH(qid, &matched_ids, entry) {
1526 if (obj_type != GOT_OBJ_TYPE_ANY) {
1527 int matched_type;
1528 err = got_object_get_type(&matched_type, repo,
1529 &qid->id);
1530 if (err)
1531 goto done;
1532 if (matched_type != obj_type)
1533 continue;
1535 if (*unique_id == NULL) {
1536 *unique_id = got_object_id_dup(&qid->id);
1537 if (*unique_id == NULL) {
1538 err = got_error_from_errno("malloc");
1539 goto done;
1541 } else {
1542 if (got_object_id_cmp(*unique_id,
1543 &qid->id) == 0)
1544 continue; /* packed multiple times */
1545 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1546 goto done;
1550 done:
1551 free(objects_pack_dir);
1552 got_object_id_queue_free(&matched_ids);
1553 if (err) {
1554 free(*unique_id);
1555 *unique_id = NULL;
1557 return err;
1560 static const struct got_error *
1561 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1562 const char *object_dir, const char *id_str_prefix, int obj_type,
1563 struct got_repository *repo)
1565 const struct got_error *err = NULL;
1566 char *path, *id_str = NULL;
1567 DIR *dir = NULL;
1568 struct dirent *dent;
1569 struct got_object_id id;
1571 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1572 err = got_error_from_errno("asprintf");
1573 goto done;
1576 dir = opendir(path);
1577 if (dir == NULL) {
1578 if (errno == ENOENT) {
1579 err = NULL;
1580 goto done;
1582 err = got_error_from_errno2("opendir", path);
1583 goto done;
1585 while ((dent = readdir(dir)) != NULL) {
1586 int cmp;
1588 free(id_str);
1589 id_str = NULL;
1591 if (strcmp(dent->d_name, ".") == 0 ||
1592 strcmp(dent->d_name, "..") == 0)
1593 continue;
1595 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1596 err = got_error_from_errno("asprintf");
1597 goto done;
1600 if (!got_parse_sha1_digest(id.sha1, id_str))
1601 continue;
1604 * Directory entries do not necessarily appear in
1605 * sorted order, so we must iterate over all of them.
1607 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1608 if (cmp != 0)
1609 continue;
1611 if (*unique_id == NULL) {
1612 if (obj_type != GOT_OBJ_TYPE_ANY) {
1613 int matched_type;
1614 err = got_object_get_type(&matched_type, repo,
1615 &id);
1616 if (err)
1617 goto done;
1618 if (matched_type != obj_type)
1619 continue;
1621 *unique_id = got_object_id_dup(&id);
1622 if (*unique_id == NULL) {
1623 err = got_error_from_errno("got_object_id_dup");
1624 goto done;
1626 } else {
1627 if (got_object_id_cmp(*unique_id, &id) == 0)
1628 continue; /* both packed and loose */
1629 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1630 goto done;
1633 done:
1634 if (dir && closedir(dir) != 0 && err == NULL)
1635 err = got_error_from_errno("closedir");
1636 if (err) {
1637 free(*unique_id);
1638 *unique_id = NULL;
1640 free(id_str);
1641 free(path);
1642 return err;
1645 const struct got_error *
1646 got_repo_match_object_id_prefix(struct got_object_id **id,
1647 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1649 const struct got_error *err = NULL;
1650 char *path_objects = NULL, *object_dir = NULL;
1651 size_t len;
1652 int i;
1654 *id = NULL;
1656 path_objects = got_repo_get_path_objects(repo);
1658 len = strlen(id_str_prefix);
1659 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1660 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1661 goto done;
1664 for (i = 0; i < len; i++) {
1665 if (isxdigit((unsigned char)id_str_prefix[i]))
1666 continue;
1667 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1668 goto done;
1671 if (len >= 2) {
1672 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1673 if (err)
1674 goto done;
1675 object_dir = strndup(id_str_prefix, 2);
1676 if (object_dir == NULL) {
1677 err = got_error_from_errno("strdup");
1678 goto done;
1680 err = match_loose_object(id, path_objects, object_dir,
1681 id_str_prefix, obj_type, repo);
1682 } else if (len == 1) {
1683 int i;
1684 for (i = 0; i < 0xf; i++) {
1685 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1686 == -1) {
1687 err = got_error_from_errno("asprintf");
1688 goto done;
1690 err = match_packed_object(id, repo, object_dir,
1691 obj_type);
1692 if (err)
1693 goto done;
1694 err = match_loose_object(id, path_objects, object_dir,
1695 id_str_prefix, obj_type, repo);
1696 if (err)
1697 goto done;
1699 } else {
1700 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1701 goto done;
1703 done:
1704 free(path_objects);
1705 free(object_dir);
1706 if (err) {
1707 free(*id);
1708 *id = NULL;
1709 } else if (*id == NULL) {
1710 switch (obj_type) {
1711 case GOT_OBJ_TYPE_BLOB:
1712 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1713 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1714 break;
1715 case GOT_OBJ_TYPE_TREE:
1716 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1717 GOT_OBJ_LABEL_TREE, id_str_prefix);
1718 break;
1719 case GOT_OBJ_TYPE_COMMIT:
1720 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1721 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1722 break;
1723 case GOT_OBJ_TYPE_TAG:
1724 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1725 GOT_OBJ_LABEL_TAG, id_str_prefix);
1726 break;
1727 default:
1728 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1729 break;
1733 return err;
1736 const struct got_error *
1737 got_repo_match_object_id(struct got_object_id **id, char **label,
1738 const char *id_str, int obj_type, struct got_reflist_head *refs,
1739 struct got_repository *repo)
1741 const struct got_error *err;
1742 struct got_tag_object *tag;
1743 struct got_reference *ref = NULL;
1745 *id = NULL;
1746 if (label)
1747 *label = NULL;
1749 if (refs) {
1750 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1751 refs, repo);
1752 if (err == NULL) {
1753 *id = got_object_id_dup(
1754 got_object_tag_get_object_id(tag));
1755 if (*id == NULL)
1756 err = got_error_from_errno("got_object_id_dup");
1757 else if (label && asprintf(label, "refs/tags/%s",
1758 got_object_tag_get_name(tag)) == -1) {
1759 err = got_error_from_errno("asprintf");
1760 free(*id);
1761 *id = NULL;
1763 got_object_tag_close(tag);
1764 return err;
1765 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1766 err->code != GOT_ERR_NO_OBJ)
1767 return err;
1770 err = got_ref_open(&ref, repo, id_str, 0);
1771 if (err == NULL) {
1772 err = got_ref_resolve(id, repo, ref);
1773 if (err)
1774 goto done;
1775 if (label) {
1776 *label = strdup(got_ref_get_name(ref));
1777 if (*label == NULL) {
1778 err = got_error_from_errno("strdup");
1779 goto done;
1782 } else {
1783 if (err->code != GOT_ERR_NOT_REF &&
1784 err->code != GOT_ERR_BAD_REF_NAME)
1785 goto done;
1786 err = got_repo_match_object_id_prefix(id, id_str,
1787 obj_type, repo);
1788 if (err) {
1789 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1790 err = got_error_not_ref(id_str);
1791 goto done;
1793 if (label) {
1794 err = got_object_id_str(label, *id);
1795 if (*label == NULL) {
1796 err = got_error_from_errno("strdup");
1797 goto done;
1801 done:
1802 if (ref)
1803 got_ref_close(ref);
1804 return err;
1807 const struct got_error *
1808 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1809 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1811 const struct got_error *err = NULL;
1812 struct got_reflist_entry *re;
1813 struct got_object_id *tag_id;
1814 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1816 *tag = NULL;
1818 TAILQ_FOREACH(re, refs, entry) {
1819 const char *refname;
1820 refname = got_ref_get_name(re->ref);
1821 if (got_ref_is_symbolic(re->ref))
1822 continue;
1823 if (strncmp(refname, "refs/tags/", 10) != 0)
1824 continue;
1825 if (!name_is_absolute)
1826 refname += strlen("refs/tags/");
1827 if (strcmp(refname, name) != 0)
1828 continue;
1829 err = got_ref_resolve(&tag_id, repo, re->ref);
1830 if (err)
1831 break;
1832 err = got_object_open_as_tag(tag, repo, tag_id);
1833 free(tag_id);
1834 if (err)
1835 break;
1836 if (obj_type == GOT_OBJ_TYPE_ANY ||
1837 got_object_tag_get_object_type(*tag) == obj_type)
1838 break;
1839 got_object_tag_close(*tag);
1840 *tag = NULL;
1843 if (err == NULL && *tag == NULL)
1844 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1845 GOT_OBJ_LABEL_TAG, name);
1846 return err;
1849 static const struct got_error *
1850 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1851 const char *name, mode_t mode, struct got_object_id *blob_id)
1853 const struct got_error *err = NULL;
1855 *new_te = NULL;
1857 *new_te = calloc(1, sizeof(**new_te));
1858 if (*new_te == NULL)
1859 return got_error_from_errno("calloc");
1861 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1862 sizeof((*new_te)->name)) {
1863 err = got_error(GOT_ERR_NO_SPACE);
1864 goto done;
1867 if (S_ISLNK(mode)) {
1868 (*new_te)->mode = S_IFLNK;
1869 } else {
1870 (*new_te)->mode = S_IFREG;
1871 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1873 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1874 done:
1875 if (err && *new_te) {
1876 free(*new_te);
1877 *new_te = NULL;
1879 return err;
1882 static const struct got_error *
1883 import_file(struct got_tree_entry **new_te, struct dirent *de,
1884 const char *path, struct got_repository *repo)
1886 const struct got_error *err;
1887 struct got_object_id *blob_id = NULL;
1888 char *filepath;
1889 struct stat sb;
1891 if (asprintf(&filepath, "%s%s%s", path,
1892 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1893 return got_error_from_errno("asprintf");
1895 if (lstat(filepath, &sb) != 0) {
1896 err = got_error_from_errno2("lstat", path);
1897 goto done;
1900 err = got_object_blob_create(&blob_id, filepath, repo);
1901 if (err)
1902 goto done;
1904 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1905 blob_id);
1906 done:
1907 free(filepath);
1908 if (err)
1909 free(blob_id);
1910 return err;
1913 static const struct got_error *
1914 insert_tree_entry(struct got_tree_entry *new_te,
1915 struct got_pathlist_head *paths)
1917 const struct got_error *err = NULL;
1918 struct got_pathlist_entry *new_pe;
1920 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1921 if (err)
1922 return err;
1923 if (new_pe == NULL)
1924 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1925 return NULL;
1928 static const struct got_error *write_tree(struct got_object_id **,
1929 const char *, struct got_pathlist_head *, struct got_repository *,
1930 got_repo_import_cb progress_cb, void *progress_arg);
1932 static const struct got_error *
1933 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1934 const char *path, struct got_pathlist_head *ignores,
1935 struct got_repository *repo,
1936 got_repo_import_cb progress_cb, void *progress_arg)
1938 const struct got_error *err;
1939 struct got_object_id *id = NULL;
1940 char *subdirpath;
1942 if (asprintf(&subdirpath, "%s%s%s", path,
1943 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1944 return got_error_from_errno("asprintf");
1946 (*new_te) = calloc(1, sizeof(**new_te));
1947 if (*new_te == NULL)
1948 return got_error_from_errno("calloc");
1949 (*new_te)->mode = S_IFDIR;
1950 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1951 sizeof((*new_te)->name)) {
1952 err = got_error(GOT_ERR_NO_SPACE);
1953 goto done;
1955 err = write_tree(&id, subdirpath, ignores, repo,
1956 progress_cb, progress_arg);
1957 if (err)
1958 goto done;
1959 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1961 done:
1962 free(id);
1963 free(subdirpath);
1964 if (err) {
1965 free(*new_te);
1966 *new_te = NULL;
1968 return err;
1971 static const struct got_error *
1972 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1973 struct got_pathlist_head *ignores, struct got_repository *repo,
1974 got_repo_import_cb progress_cb, void *progress_arg)
1976 const struct got_error *err = NULL;
1977 DIR *dir;
1978 struct dirent *de;
1979 int nentries;
1980 struct got_tree_entry *new_te = NULL;
1981 struct got_pathlist_head paths;
1982 struct got_pathlist_entry *pe;
1984 *new_tree_id = NULL;
1986 TAILQ_INIT(&paths);
1988 dir = opendir(path_dir);
1989 if (dir == NULL) {
1990 err = got_error_from_errno2("opendir", path_dir);
1991 goto done;
1994 nentries = 0;
1995 while ((de = readdir(dir)) != NULL) {
1996 int ignore = 0;
1997 int type;
1999 if (strcmp(de->d_name, ".") == 0 ||
2000 strcmp(de->d_name, "..") == 0)
2001 continue;
2003 TAILQ_FOREACH(pe, ignores, entry) {
2004 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2005 ignore = 1;
2006 break;
2009 if (ignore)
2010 continue;
2012 err = got_path_dirent_type(&type, path_dir, de);
2013 if (err)
2014 goto done;
2016 if (type == DT_DIR) {
2017 err = import_subdir(&new_te, de, path_dir,
2018 ignores, repo, progress_cb, progress_arg);
2019 if (err) {
2020 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2021 goto done;
2022 err = NULL;
2023 continue;
2025 } else if (type == DT_REG || type == DT_LNK) {
2026 err = import_file(&new_te, de, path_dir, repo);
2027 if (err)
2028 goto done;
2029 } else
2030 continue;
2032 err = insert_tree_entry(new_te, &paths);
2033 if (err)
2034 goto done;
2035 nentries++;
2038 if (TAILQ_EMPTY(&paths)) {
2039 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2040 "cannot create tree without any entries");
2041 goto done;
2044 TAILQ_FOREACH(pe, &paths, entry) {
2045 struct got_tree_entry *te = pe->data;
2046 char *path;
2047 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2048 continue;
2049 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2050 err = got_error_from_errno("asprintf");
2051 goto done;
2053 err = (*progress_cb)(progress_arg, path);
2054 free(path);
2055 if (err)
2056 goto done;
2059 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2060 done:
2061 if (dir)
2062 closedir(dir);
2063 got_pathlist_free(&paths);
2064 return err;
2067 const struct got_error *
2068 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2069 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2070 struct got_repository *repo, got_repo_import_cb progress_cb,
2071 void *progress_arg)
2073 const struct got_error *err;
2074 struct got_object_id *new_tree_id;
2076 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2077 progress_cb, progress_arg);
2078 if (err)
2079 return err;
2081 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2082 author, time(NULL), author, time(NULL), logmsg, repo);
2083 free(new_tree_id);
2084 return err;
2087 const struct got_error *
2088 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2089 struct got_repository *repo)
2091 const struct got_error *err = NULL;
2092 char *path_objects = NULL, *path = NULL;
2093 DIR *dir = NULL;
2094 struct got_object_id id;
2095 int i;
2097 *nobjects = 0;
2098 *ondisk_size = 0;
2100 path_objects = got_repo_get_path_objects(repo);
2101 if (path_objects == NULL)
2102 return got_error_from_errno("got_repo_get_path_objects");
2104 for (i = 0; i <= 0xff; i++) {
2105 struct dirent *dent;
2107 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2108 err = got_error_from_errno("asprintf");
2109 break;
2112 dir = opendir(path);
2113 if (dir == NULL) {
2114 if (errno == ENOENT) {
2115 err = NULL;
2116 continue;
2118 err = got_error_from_errno2("opendir", path);
2119 break;
2122 while ((dent = readdir(dir)) != NULL) {
2123 char *id_str;
2124 int fd;
2125 struct stat sb;
2127 if (strcmp(dent->d_name, ".") == 0 ||
2128 strcmp(dent->d_name, "..") == 0)
2129 continue;
2131 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2132 err = got_error_from_errno("asprintf");
2133 goto done;
2136 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2137 free(id_str);
2138 continue;
2140 free(id_str);
2142 err = got_object_open_loose_fd(&fd, &id, repo);
2143 if (err)
2144 goto done;
2146 if (fstat(fd, &sb) == -1) {
2147 err = got_error_from_errno("fstat");
2148 close(fd);
2149 goto done;
2151 (*nobjects)++;
2152 (*ondisk_size) += sb.st_size;
2154 if (close(fd) == -1) {
2155 err = got_error_from_errno("close");
2156 goto done;
2160 if (closedir(dir) != 0) {
2161 err = got_error_from_errno("closedir");
2162 goto done;
2164 dir = NULL;
2166 free(path);
2167 path = NULL;
2169 done:
2170 if (dir && closedir(dir) != 0 && err == NULL)
2171 err = got_error_from_errno("closedir");
2173 if (err) {
2174 *nobjects = 0;
2175 *ondisk_size = 0;
2177 free(path_objects);
2178 free(path);
2179 return err;
2182 const struct got_error *
2183 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2184 off_t *total_packsize, struct got_repository *repo)
2186 const struct got_error *err = NULL;
2187 DIR *packdir = NULL;
2188 struct dirent *dent;
2189 struct got_packidx *packidx = NULL;
2190 char *path_packidx;
2191 char *path_packfile;
2192 int packdir_fd;
2193 struct stat sb;
2195 *npackfiles = 0;
2196 *nobjects = 0;
2197 *total_packsize = 0;
2199 packdir_fd = openat(got_repo_get_fd(repo),
2200 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2201 if (packdir_fd == -1) {
2202 return got_error_from_errno_fmt("openat: %s/%s",
2203 got_repo_get_path_git_dir(repo),
2204 GOT_OBJECTS_PACK_DIR);
2207 packdir = fdopendir(packdir_fd);
2208 if (packdir == NULL) {
2209 err = got_error_from_errno("fdopendir");
2210 goto done;
2213 while ((dent = readdir(packdir)) != NULL) {
2214 if (!got_repo_is_packidx_filename(dent->d_name,
2215 strlen(dent->d_name)))
2216 continue;
2218 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2219 dent->d_name) == -1) {
2220 err = got_error_from_errno("asprintf");
2221 goto done;
2224 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2225 path_packidx, 0);
2226 free(path_packidx);
2227 if (err)
2228 goto done;
2230 if (fstat(packidx->fd, &sb) == -1)
2231 goto done;
2232 *total_packsize += sb.st_size;
2234 err = got_packidx_get_packfile_path(&path_packfile,
2235 packidx->path_packidx);
2236 if (err)
2237 goto done;
2239 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2240 0) == -1) {
2241 free(path_packfile);
2242 goto done;
2244 free(path_packfile);
2245 *total_packsize += sb.st_size;
2247 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2249 (*npackfiles)++;
2251 got_packidx_close(packidx);
2252 packidx = NULL;
2254 done:
2255 if (packidx)
2256 got_packidx_close(packidx);
2257 if (packdir && closedir(packdir) != 0 && err == NULL)
2258 err = got_error_from_errno("closedir");
2259 if (err) {
2260 *npackfiles = 0;
2261 *nobjects = 0;
2262 *total_packsize = 0;
2264 return err;
2267 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2268 got_packidx_bloom_filter_cmp);