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_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 #endif
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
76 const char *
77 got_repo_get_path(struct got_repository *repo)
78 {
79 return repo->path;
80 }
82 const char *
83 got_repo_get_path_git_dir(struct got_repository *repo)
84 {
85 return repo->path_git_dir;
86 }
88 int
89 got_repo_get_fd(struct got_repository *repo)
90 {
91 return repo->gitdir_fd;
92 }
94 const char *
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_name;
98 }
100 const char *
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
106 const char *
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
112 const char *
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
118 const char *
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
124 void
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
132 int
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
138 static char *
139 get_path_git_child(struct got_repository *repo, const char *basename)
141 char *path_child;
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
144 basename) == -1)
145 return NULL;
147 return path_child;
150 char *
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
156 char *
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
162 char *
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
168 char *
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
174 static char *
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
180 char *
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
186 char *
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
198 void
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
206 static int
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
213 int ret = 0;
214 struct stat sb;
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_objects, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_refs, &sb) == -1)
228 goto done;
229 if (!S_ISDIR(sb.st_mode))
230 goto done;
232 if (lstat(path_head, &sb) == -1)
233 goto done;
234 if (!S_ISREG(sb.st_mode))
235 goto done;
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
239 goto done;
240 got_ref_close(head_ref);
242 ret = 1;
243 done:
244 free(path_objects);
245 free(path_refs);
246 free(path_head);
247 return ret;
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i;
257 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
258 if (*pack_fds == NULL)
259 return got_error_from_errno("calloc");
261 /*
262 * got_repo_pack_fds_close will try to close all of the
263 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
264 * a value from got_opentempfd(), which would result in a close(0) if
265 * we do not initialize to -1 here.
266 */
267 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
268 (*pack_fds)[i] = -1;
270 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
271 (*pack_fds)[i] = got_opentempfd();
272 if ((*pack_fds)[i] == -1) {
273 err = got_error_from_errno("got_opentempfd");
274 got_repo_pack_fds_close(*pack_fds);
275 *pack_fds = NULL;
276 return err;
280 return NULL;
283 const struct got_error *
284 got_repo_pack_fds_close(int *pack_fds)
286 const struct got_error *err = NULL;
287 int i;
289 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
290 if (pack_fds[i] == -1)
291 continue;
292 if (close(pack_fds[i]) == -1) {
293 err = got_error_from_errno("close");
294 break;
297 free(pack_fds);
298 return err;
301 const struct got_error *
302 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
303 struct got_object *obj)
305 #ifndef GOT_NO_OBJ_CACHE
306 const struct got_error *err = NULL;
307 err = got_object_cache_add(&repo->objcache, id, obj);
308 if (err) {
309 if (err->code == GOT_ERR_OBJ_EXISTS ||
310 err->code == GOT_ERR_OBJ_TOO_LARGE)
311 err = NULL;
312 return err;
314 obj->refcnt++;
315 #endif
316 return NULL;
319 struct got_object *
320 got_repo_get_cached_object(struct got_repository *repo,
321 struct got_object_id *id)
323 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
326 const struct got_error *
327 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
328 struct got_tree_object *tree)
330 #ifndef GOT_NO_OBJ_CACHE
331 const struct got_error *err = NULL;
332 err = got_object_cache_add(&repo->treecache, id, tree);
333 if (err) {
334 if (err->code == GOT_ERR_OBJ_EXISTS ||
335 err->code == GOT_ERR_OBJ_TOO_LARGE)
336 err = NULL;
337 return err;
339 tree->refcnt++;
340 #endif
341 return NULL;
344 struct got_tree_object *
345 got_repo_get_cached_tree(struct got_repository *repo,
346 struct got_object_id *id)
348 return (struct got_tree_object *)got_object_cache_get(
349 &repo->treecache, id);
352 const struct got_error *
353 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
354 struct got_commit_object *commit)
356 #ifndef GOT_NO_OBJ_CACHE
357 const struct got_error *err = NULL;
358 err = got_object_cache_add(&repo->commitcache, id, commit);
359 if (err) {
360 if (err->code == GOT_ERR_OBJ_EXISTS ||
361 err->code == GOT_ERR_OBJ_TOO_LARGE)
362 err = NULL;
363 return err;
365 commit->refcnt++;
366 #endif
367 return NULL;
370 struct got_commit_object *
371 got_repo_get_cached_commit(struct got_repository *repo,
372 struct got_object_id *id)
374 return (struct got_commit_object *)got_object_cache_get(
375 &repo->commitcache, id);
378 const struct got_error *
379 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
380 struct got_tag_object *tag)
382 #ifndef GOT_NO_OBJ_CACHE
383 const struct got_error *err = NULL;
384 err = got_object_cache_add(&repo->tagcache, id, tag);
385 if (err) {
386 if (err->code == GOT_ERR_OBJ_EXISTS ||
387 err->code == GOT_ERR_OBJ_TOO_LARGE)
388 err = NULL;
389 return err;
391 tag->refcnt++;
392 #endif
393 return NULL;
396 struct got_tag_object *
397 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
399 return (struct got_tag_object *)got_object_cache_get(
400 &repo->tagcache, id);
403 const struct got_error *
404 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
405 struct got_raw_object *raw)
407 #ifndef GOT_NO_OBJ_CACHE
408 const struct got_error *err = NULL;
409 err = got_object_cache_add(&repo->rawcache, id, raw);
410 if (err) {
411 if (err->code == GOT_ERR_OBJ_EXISTS ||
412 err->code == GOT_ERR_OBJ_TOO_LARGE)
413 err = NULL;
414 return err;
416 raw->refcnt++;
417 #endif
418 return NULL;
422 struct got_raw_object *
423 got_repo_get_cached_raw_object(struct got_repository *repo,
424 struct got_object_id *id)
426 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
430 static const struct got_error *
431 open_repo(struct got_repository *repo, const char *path)
433 const struct got_error *err = NULL;
435 repo->gitdir_fd = -1;
437 /* bare git repository? */
438 repo->path_git_dir = strdup(path);
439 if (repo->path_git_dir == NULL)
440 return got_error_from_errno("strdup");
441 if (is_git_repo(repo)) {
442 repo->path = strdup(repo->path_git_dir);
443 if (repo->path == NULL) {
444 err = got_error_from_errno("strdup");
445 goto done;
447 repo->gitdir_fd = open(repo->path_git_dir,
448 O_DIRECTORY | O_CLOEXEC);
449 if (repo->gitdir_fd == -1) {
450 err = got_error_from_errno2("open",
451 repo->path_git_dir);
452 goto done;
454 return NULL;
457 /* git repository with working tree? */
458 free(repo->path_git_dir);
459 repo->path_git_dir = NULL;
460 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
461 err = got_error_from_errno("asprintf");
462 goto done;
464 if (is_git_repo(repo)) {
465 repo->path = strdup(path);
466 if (repo->path == NULL) {
467 err = got_error_from_errno("strdup");
468 goto done;
470 repo->gitdir_fd = open(repo->path_git_dir,
471 O_DIRECTORY | O_CLOEXEC);
472 if (repo->gitdir_fd == -1) {
473 err = got_error_from_errno2("open",
474 repo->path_git_dir);
475 goto done;
477 return NULL;
480 err = got_error(GOT_ERR_NOT_GIT_REPO);
481 done:
482 if (err) {
483 free(repo->path);
484 repo->path = NULL;
485 free(repo->path_git_dir);
486 repo->path_git_dir = NULL;
487 if (repo->gitdir_fd != -1)
488 close(repo->gitdir_fd);
489 repo->gitdir_fd = -1;
492 return err;
495 static const struct got_error *
496 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
498 const struct got_error *err = NULL;
499 char *repo_gitconfig_path = NULL;
501 if (global_gitconfig_path) {
502 /* Read settings from ~/.gitconfig. */
503 int dummy_repo_version;
504 err = got_repo_read_gitconfig(&dummy_repo_version,
505 &repo->global_gitconfig_author_name,
506 &repo->global_gitconfig_author_email,
507 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
508 if (err)
509 return err;
512 /* Read repository's .git/config file. */
513 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
514 if (repo_gitconfig_path == NULL)
515 return got_error_from_errno("got_repo_get_path_gitconfig");
517 err = got_repo_read_gitconfig(
518 &repo->gitconfig_repository_format_version,
519 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
520 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
521 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
522 repo_gitconfig_path);
523 if (err)
524 goto done;
526 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
527 int i;
529 for (i = 0; i < repo->ngitconfig_remotes; i++) {
530 got_repo_free_remote_repo_data(
531 &repo->gitconfig_remotes[i]);
533 free(repo->gitconfig_remotes);
534 repo->gitconfig_remotes = NULL;
535 repo->ngitconfig_remotes = 0;
537 free(repo->gitconfig_author_name);
538 repo->gitconfig_author_name = NULL;
539 free(repo->gitconfig_author_email);
540 repo->gitconfig_author_email = NULL;
542 free(repo->global_gitconfig_author_name);
543 repo->global_gitconfig_author_name = NULL;
544 free(repo->global_gitconfig_author_email);
545 repo->global_gitconfig_author_email = NULL;
548 done:
549 free(repo_gitconfig_path);
550 return err;
553 static const struct got_error *
554 read_gotconfig(struct got_repository *repo)
556 const struct got_error *err = NULL;
557 char *gotconfig_path;
559 gotconfig_path = got_repo_get_path_gotconfig(repo);
560 if (gotconfig_path == NULL)
561 return got_error_from_errno("got_repo_get_path_gotconfig");
563 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
564 free(gotconfig_path);
565 return err;
568 /* Supported repository format extensions. */
569 static const char *const repo_extensions[] = {
570 "noop", /* Got supports repository format version 1. */
571 "preciousObjects", /* Supported by gotadmin cleanup. */
572 "worktreeConfig", /* Got does not care about Git work trees. */
573 };
575 const struct got_error *
576 got_repo_open(struct got_repository **repop, const char *path,
577 const char *global_gitconfig_path, int *pack_fds)
579 struct got_repository *repo = NULL;
580 const struct got_error *err = NULL;
581 char *repo_path = NULL;
582 size_t i, j = 0;
583 struct rlimit rl;
585 *repop = NULL;
587 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
588 return got_error_from_errno("getrlimit");
590 repo = calloc(1, sizeof(*repo));
591 if (repo == NULL)
592 return got_error_from_errno("calloc");
594 RB_INIT(&repo->packidx_bloom_filters);
595 TAILQ_INIT(&repo->packidx_paths);
597 for (i = 0; i < nitems(repo->privsep_children); i++) {
598 memset(&repo->privsep_children[i], 0,
599 sizeof(repo->privsep_children[0]));
600 repo->privsep_children[i].imsg_fd = -1;
603 err = got_object_cache_init(&repo->objcache,
604 GOT_OBJECT_CACHE_TYPE_OBJ);
605 if (err)
606 goto done;
607 err = got_object_cache_init(&repo->treecache,
608 GOT_OBJECT_CACHE_TYPE_TREE);
609 if (err)
610 goto done;
611 err = got_object_cache_init(&repo->commitcache,
612 GOT_OBJECT_CACHE_TYPE_COMMIT);
613 if (err)
614 goto done;
615 err = got_object_cache_init(&repo->tagcache,
616 GOT_OBJECT_CACHE_TYPE_TAG);
617 if (err)
618 goto done;
619 err = got_object_cache_init(&repo->rawcache,
620 GOT_OBJECT_CACHE_TYPE_RAW);
621 if (err)
622 goto done;
624 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
625 if (repo->pack_cache_size > rl.rlim_cur / 8)
626 repo->pack_cache_size = rl.rlim_cur / 8;
627 for (i = 0; i < nitems(repo->packs); i++) {
628 if (i < repo->pack_cache_size) {
629 repo->packs[i].basefd = pack_fds[j++];
630 repo->packs[i].accumfd = pack_fds[j++];
631 } else {
632 repo->packs[i].basefd = -1;
633 repo->packs[i].accumfd = -1;
636 repo->pinned_pack = -1;
637 repo->pinned_packidx = -1;
638 repo->pinned_pid = 0;
640 repo_path = realpath(path, NULL);
641 if (repo_path == NULL) {
642 err = got_error_from_errno2("realpath", path);
643 goto done;
646 for (;;) {
647 char *parent_path;
649 err = open_repo(repo, repo_path);
650 if (err == NULL)
651 break;
652 if (err->code != GOT_ERR_NOT_GIT_REPO)
653 goto done;
654 if (repo_path[0] == '/' && repo_path[1] == '\0') {
655 err = got_error(GOT_ERR_NOT_GIT_REPO);
656 goto done;
658 err = got_path_dirname(&parent_path, repo_path);
659 if (err)
660 goto done;
661 free(repo_path);
662 repo_path = parent_path;
665 err = read_gotconfig(repo);
666 if (err)
667 goto done;
669 err = read_gitconfig(repo, global_gitconfig_path);
670 if (err)
671 goto done;
672 if (repo->gitconfig_repository_format_version != 0) {
673 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
674 goto done;
676 for (i = 0; i < repo->nextensions; i++) {
677 char *ext = repo->extensions[i];
678 int j, supported = 0;
679 for (j = 0; j < nitems(repo_extensions); j++) {
680 if (strcmp(ext, repo_extensions[j]) == 0) {
681 supported = 1;
682 break;
685 if (!supported) {
686 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
687 goto done;
691 err = got_repo_list_packidx(&repo->packidx_paths, repo);
692 done:
693 if (err)
694 got_repo_close(repo);
695 else
696 *repop = repo;
697 free(repo_path);
698 return err;
701 const struct got_error *
702 got_repo_close(struct got_repository *repo)
704 const struct got_error *err = NULL, *child_err;
705 struct got_packidx_bloom_filter *bf;
706 struct got_pathlist_entry *pe;
707 size_t i;
709 for (i = 0; i < repo->pack_cache_size; i++) {
710 if (repo->packidx_cache[i] == NULL)
711 break;
712 got_packidx_close(repo->packidx_cache[i]);
715 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
716 &repo->packidx_bloom_filters))) {
717 RB_REMOVE(got_packidx_bloom_filter_tree,
718 &repo->packidx_bloom_filters, bf);
719 bloom_free(bf->bloom);
720 free(bf->bloom);
721 free(bf);
724 for (i = 0; i < repo->pack_cache_size; i++)
725 if (repo->packs[i].path_packfile)
726 if (repo->packs[i].path_packfile)
727 got_pack_close(&repo->packs[i]);
729 free(repo->path);
730 free(repo->path_git_dir);
732 got_object_cache_close(&repo->objcache);
733 got_object_cache_close(&repo->treecache);
734 got_object_cache_close(&repo->commitcache);
735 got_object_cache_close(&repo->tagcache);
736 got_object_cache_close(&repo->rawcache);
738 for (i = 0; i < nitems(repo->privsep_children); i++) {
739 if (repo->privsep_children[i].imsg_fd == -1)
740 continue;
741 imsg_clear(repo->privsep_children[i].ibuf);
742 free(repo->privsep_children[i].ibuf);
743 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
744 child_err = got_privsep_wait_for_child(
745 repo->privsep_children[i].pid);
746 if (child_err && err == NULL)
747 err = child_err;
748 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
749 err == NULL)
750 err = got_error_from_errno("close");
753 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
754 err == NULL)
755 err = got_error_from_errno("close");
757 if (repo->gotconfig)
758 got_gotconfig_free(repo->gotconfig);
759 free(repo->gitconfig_author_name);
760 free(repo->gitconfig_author_email);
761 for (i = 0; i < repo->ngitconfig_remotes; i++)
762 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
763 free(repo->gitconfig_remotes);
764 for (i = 0; i < repo->nextensions; i++)
765 free(repo->extensions[i]);
766 free(repo->extensions);
768 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
769 free((void *)pe->path);
770 got_pathlist_free(&repo->packidx_paths);
771 free(repo);
773 return err;
776 void
777 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
779 int i;
781 free(repo->name);
782 repo->name = NULL;
783 free(repo->fetch_url);
784 repo->fetch_url = NULL;
785 free(repo->send_url);
786 repo->send_url = NULL;
787 for (i = 0; i < repo->nfetch_branches; i++)
788 free(repo->fetch_branches[i]);
789 free(repo->fetch_branches);
790 repo->fetch_branches = NULL;
791 repo->nfetch_branches = 0;
792 for (i = 0; i < repo->nsend_branches; i++)
793 free(repo->send_branches[i]);
794 free(repo->send_branches);
795 repo->send_branches = NULL;
796 repo->nsend_branches = 0;
799 const struct got_error *
800 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
801 const char *input_path)
803 const struct got_error *err = NULL;
804 const char *repo_abspath = NULL;
805 size_t repolen, len;
806 char *canonpath, *path = NULL;
808 *in_repo_path = NULL;
810 canonpath = strdup(input_path);
811 if (canonpath == NULL) {
812 err = got_error_from_errno("strdup");
813 goto done;
815 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
816 if (err)
817 goto done;
819 repo_abspath = got_repo_get_path(repo);
821 if (canonpath[0] == '\0') {
822 path = strdup(canonpath);
823 if (path == NULL) {
824 err = got_error_from_errno("strdup");
825 goto done;
827 } else {
828 path = realpath(canonpath, NULL);
829 if (path == NULL) {
830 if (errno != ENOENT) {
831 err = got_error_from_errno2("realpath",
832 canonpath);
833 goto done;
835 /*
836 * Path is not on disk.
837 * Assume it is already relative to repository root.
838 */
839 path = strdup(canonpath);
840 if (path == NULL) {
841 err = got_error_from_errno("strdup");
842 goto done;
846 repolen = strlen(repo_abspath);
847 len = strlen(path);
850 if (strcmp(path, repo_abspath) == 0) {
851 free(path);
852 path = strdup("");
853 if (path == NULL) {
854 err = got_error_from_errno("strdup");
855 goto done;
857 } else if (len > repolen &&
858 got_path_is_child(path, repo_abspath, repolen)) {
859 /* Matched an on-disk path inside repository. */
860 if (got_repo_is_bare(repo)) {
861 /*
862 * Matched an on-disk path inside repository
863 * database. Treat input as repository-relative.
864 */
865 free(path);
866 path = canonpath;
867 canonpath = NULL;
868 } else {
869 char *child;
870 /* Strip common prefix with repository path. */
871 err = got_path_skip_common_ancestor(&child,
872 repo_abspath, path);
873 if (err)
874 goto done;
875 free(path);
876 path = child;
878 } else {
879 /*
880 * Matched unrelated on-disk path.
881 * Treat input as repository-relative.
882 */
883 free(path);
884 path = canonpath;
885 canonpath = NULL;
889 /* Make in-repository path absolute */
890 if (path[0] != '/') {
891 char *abspath;
892 if (asprintf(&abspath, "/%s", path) == -1) {
893 err = got_error_from_errno("asprintf");
894 goto done;
896 free(path);
897 path = abspath;
900 done:
901 free(canonpath);
902 if (err)
903 free(path);
904 else
905 *in_repo_path = path;
906 return err;
909 static const struct got_error *
910 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
911 const char *path_packidx)
913 const struct got_error *err = NULL;
914 size_t i;
916 for (i = 0; i < repo->pack_cache_size; i++) {
917 if (repo->packidx_cache[i] == NULL)
918 break;
919 if (strcmp(repo->packidx_cache[i]->path_packidx,
920 path_packidx) == 0) {
921 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
924 if (i == repo->pack_cache_size) {
925 do {
926 i--;
927 } while (i > 0 && repo->pinned_packidx >= 0 &&
928 i == repo->pinned_packidx);
929 err = got_packidx_close(repo->packidx_cache[i]);
930 if (err)
931 return err;
934 repo->packidx_cache[i] = packidx;
936 return NULL;
939 int
940 got_repo_is_packidx_filename(const char *name, size_t len)
942 if (len != GOT_PACKIDX_NAMELEN)
943 return 0;
945 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
946 return 0;
948 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
949 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
950 return 0;
952 return 1;
955 static struct got_packidx_bloom_filter *
956 get_packidx_bloom_filter(struct got_repository *repo,
957 const char *path, size_t path_len)
959 struct got_packidx_bloom_filter key;
961 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
962 return NULL; /* XXX */
963 key.path_len = path_len;
965 return RB_FIND(got_packidx_bloom_filter_tree,
966 &repo->packidx_bloom_filters, &key);
969 int
970 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
971 const char *path_packidx, struct got_object_id *id)
973 struct got_packidx_bloom_filter *bf;
975 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
976 if (bf)
977 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
979 /* No bloom filter means this pack index must be searched. */
980 return 1;
983 static const struct got_error *
984 add_packidx_bloom_filter(struct got_repository *repo,
985 struct got_packidx *packidx, const char *path_packidx)
987 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
988 struct got_packidx_bloom_filter *bf;
989 size_t len;
991 /*
992 * Don't use bloom filters for very large pack index files.
993 * Large pack files will contain a relatively large fraction
994 * of our objects so we will likely need to visit them anyway.
995 * The more objects a pack file contains the higher the probability
996 * of a false-positive match from the bloom filter. And reading
997 * all object IDs from a large pack index file can be expensive.
998 */
999 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1000 return NULL;
1002 /* Do we already have a filter for this pack index? */
1003 if (get_packidx_bloom_filter(repo, path_packidx,
1004 strlen(path_packidx)) != NULL)
1005 return NULL;
1007 bf = calloc(1, sizeof(*bf));
1008 if (bf == NULL)
1009 return got_error_from_errno("calloc");
1010 bf->bloom = calloc(1, sizeof(*bf->bloom));
1011 if (bf->bloom == NULL) {
1012 free(bf);
1013 return got_error_from_errno("calloc");
1016 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1017 if (len >= sizeof(bf->path)) {
1018 free(bf->bloom);
1019 free(bf);
1020 return got_error(GOT_ERR_NO_SPACE);
1022 bf->path_len = len;
1024 /* Minimum size supported by our bloom filter is 1000 entries. */
1025 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1026 for (i = 0; i < nobjects; i++) {
1027 struct got_packidx_object_id *id;
1028 id = &packidx->hdr.sorted_ids[i];
1029 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1032 RB_INSERT(got_packidx_bloom_filter_tree,
1033 &repo->packidx_bloom_filters, bf);
1034 return NULL;
1037 const struct got_error *
1038 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1039 struct got_repository *repo, struct got_object_id *id)
1041 const struct got_error *err;
1042 struct got_pathlist_entry *pe;
1043 size_t i;
1045 /* Search pack index cache. */
1046 for (i = 0; i < repo->pack_cache_size; i++) {
1047 if (repo->packidx_cache[i] == NULL)
1048 break;
1049 if (!got_repo_check_packidx_bloom_filter(repo,
1050 repo->packidx_cache[i]->path_packidx, id))
1051 continue; /* object will not be found in this index */
1052 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1053 if (*idx != -1) {
1054 *packidx = repo->packidx_cache[i];
1056 * Move this cache entry to the front. Repeatedly
1057 * searching a wrong pack index can be expensive.
1059 if (i > 0) {
1060 memmove(&repo->packidx_cache[1],
1061 &repo->packidx_cache[0],
1062 i * sizeof(repo->packidx_cache[0]));
1063 repo->packidx_cache[0] = *packidx;
1064 if (repo->pinned_packidx >= 0 &&
1065 repo->pinned_packidx < i)
1066 repo->pinned_packidx++;
1067 else if (repo->pinned_packidx == i)
1068 repo->pinned_packidx = 0;
1070 return NULL;
1073 /* No luck. Search the filesystem. */
1075 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1076 const char *path_packidx = pe->path;
1077 int is_cached = 0;
1079 if (!got_repo_check_packidx_bloom_filter(repo,
1080 pe->path, id))
1081 continue; /* object will not be found in this index */
1083 for (i = 0; i < repo->pack_cache_size; i++) {
1084 if (repo->packidx_cache[i] == NULL)
1085 break;
1086 if (strcmp(repo->packidx_cache[i]->path_packidx,
1087 path_packidx) == 0) {
1088 is_cached = 1;
1089 break;
1092 if (is_cached)
1093 continue; /* already searched */
1095 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1096 path_packidx, 0);
1097 if (err)
1098 goto done;
1100 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1101 if (err)
1102 goto done;
1104 err = cache_packidx(repo, *packidx, path_packidx);
1105 if (err)
1106 goto done;
1108 *idx = got_packidx_get_object_idx(*packidx, id);
1109 if (*idx != -1) {
1110 err = NULL; /* found the object */
1111 goto done;
1115 err = got_error_no_obj(id);
1116 done:
1117 return err;
1120 const struct got_error *
1121 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1122 struct got_repository *repo)
1124 const struct got_error *err = NULL;
1125 DIR *packdir = NULL;
1126 struct dirent *dent;
1127 char *path_packidx = NULL;
1128 int packdir_fd;
1129 struct stat sb;
1131 packdir_fd = openat(got_repo_get_fd(repo),
1132 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1133 if (packdir_fd == -1) {
1134 return got_error_from_errno_fmt("openat: %s/%s",
1135 got_repo_get_path_git_dir(repo),
1136 GOT_OBJECTS_PACK_DIR);
1139 packdir = fdopendir(packdir_fd);
1140 if (packdir == NULL) {
1141 err = got_error_from_errno("fdopendir");
1142 goto done;
1145 if (fstat(packdir_fd, &sb) == -1) {
1146 err = got_error_from_errno("fstat");
1147 goto done;
1149 repo->pack_path_mtime = sb.st_mtime;
1151 while ((dent = readdir(packdir)) != NULL) {
1152 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1153 continue;
1155 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1156 dent->d_name) == -1) {
1157 err = got_error_from_errno("asprintf");
1158 path_packidx = NULL;
1159 break;
1162 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1163 if (err)
1164 break;
1166 done:
1167 if (err)
1168 free(path_packidx);
1169 if (packdir && closedir(packdir) != 0 && err == NULL)
1170 err = got_error_from_errno("closedir");
1171 return err;
1174 const struct got_error *
1175 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1176 struct got_repository *repo)
1178 const struct got_error *err;
1179 size_t i;
1181 *packidx = NULL;
1183 /* Search pack index cache. */
1184 for (i = 0; i < repo->pack_cache_size; i++) {
1185 if (repo->packidx_cache[i] == NULL)
1186 break;
1187 if (strcmp(repo->packidx_cache[i]->path_packidx,
1188 path_packidx) == 0) {
1189 *packidx = repo->packidx_cache[i];
1190 return NULL;
1193 /* No luck. Search the filesystem. */
1195 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1196 path_packidx, 0);
1197 if (err)
1198 return err;
1200 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1201 if (err)
1202 goto done;
1204 err = cache_packidx(repo, *packidx, path_packidx);
1205 done:
1206 if (err) {
1207 got_packidx_close(*packidx);
1208 *packidx = NULL;
1210 return err;
1213 static const struct got_error *
1214 read_packfile_hdr(int fd, struct got_packidx *packidx)
1216 const struct got_error *err = NULL;
1217 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1218 struct got_packfile_hdr hdr;
1219 ssize_t n;
1221 n = read(fd, &hdr, sizeof(hdr));
1222 if (n < 0)
1223 return got_error_from_errno("read");
1224 if (n != sizeof(hdr))
1225 return got_error(GOT_ERR_BAD_PACKFILE);
1227 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1228 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1229 be32toh(hdr.nobjects) != totobj)
1230 err = got_error(GOT_ERR_BAD_PACKFILE);
1232 return err;
1235 static const struct got_error *
1236 open_packfile(int *fd, struct got_repository *repo,
1237 const char *relpath, struct got_packidx *packidx)
1239 const struct got_error *err = NULL;
1241 *fd = openat(got_repo_get_fd(repo), relpath,
1242 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1243 if (*fd == -1)
1244 return got_error_from_errno_fmt("openat: %s/%s",
1245 got_repo_get_path_git_dir(repo), relpath);
1247 if (packidx) {
1248 err = read_packfile_hdr(*fd, packidx);
1249 if (err) {
1250 close(*fd);
1251 *fd = -1;
1255 return err;
1258 const struct got_error *
1259 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1260 const char *path_packfile, struct got_packidx *packidx)
1262 const struct got_error *err = NULL;
1263 struct got_pack *pack = NULL;
1264 struct stat sb;
1265 size_t i;
1267 if (packp)
1268 *packp = NULL;
1270 for (i = 0; i < repo->pack_cache_size; i++) {
1271 pack = &repo->packs[i];
1272 if (pack->path_packfile == NULL)
1273 break;
1274 if (strcmp(pack->path_packfile, path_packfile) == 0)
1275 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1278 if (i == repo->pack_cache_size) {
1279 struct got_pack tmp;
1280 do {
1281 i--;
1282 } while (i > 0 && repo->pinned_pack >= 0 &&
1283 i == repo->pinned_pack);
1284 err = got_pack_close(&repo->packs[i]);
1285 if (err)
1286 return err;
1287 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1288 return got_error_from_errno("ftruncate");
1289 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1290 return got_error_from_errno("ftruncate");
1291 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1292 memcpy(&repo->packs[i], &repo->packs[0],
1293 sizeof(repo->packs[i]));
1294 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1295 if (repo->pinned_pack == 0)
1296 repo->pinned_pack = i;
1297 else if (repo->pinned_pack == i)
1298 repo->pinned_pack = 0;
1299 i = 0;
1302 pack = &repo->packs[i];
1304 pack->path_packfile = strdup(path_packfile);
1305 if (pack->path_packfile == NULL) {
1306 err = got_error_from_errno("strdup");
1307 goto done;
1310 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1311 if (err)
1312 goto done;
1314 if (fstat(pack->fd, &sb) != 0) {
1315 err = got_error_from_errno("fstat");
1316 goto done;
1318 pack->filesize = sb.st_size;
1320 pack->privsep_child = NULL;
1322 #ifndef GOT_PACK_NO_MMAP
1323 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1324 pack->fd, 0);
1325 if (pack->map == MAP_FAILED) {
1326 if (errno != ENOMEM) {
1327 err = got_error_from_errno("mmap");
1328 goto done;
1330 pack->map = NULL; /* fall back to read(2) */
1332 #endif
1333 done:
1334 if (err) {
1335 if (pack) {
1336 free(pack->path_packfile);
1337 memset(pack, 0, sizeof(*pack));
1339 } else if (packp)
1340 *packp = pack;
1341 return err;
1344 struct got_pack *
1345 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1347 struct got_pack *pack = NULL;
1348 size_t i;
1350 for (i = 0; i < repo->pack_cache_size; i++) {
1351 pack = &repo->packs[i];
1352 if (pack->path_packfile == NULL)
1353 break;
1354 if (strcmp(pack->path_packfile, path_packfile) == 0)
1355 return pack;
1358 return NULL;
1361 const struct got_error *
1362 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1363 struct got_pack *pack)
1365 size_t i;
1366 int pinned_pack = -1, pinned_packidx = -1;
1368 for (i = 0; i < repo->pack_cache_size; i++) {
1369 if (repo->packidx_cache[i] &&
1370 strcmp(repo->packidx_cache[i]->path_packidx,
1371 packidx->path_packidx) == 0)
1372 pinned_packidx = i;
1373 if (repo->packs[i].path_packfile &&
1374 strcmp(repo->packs[i].path_packfile,
1375 pack->path_packfile) == 0)
1376 pinned_pack = i;
1379 if (pinned_packidx == -1 || pinned_pack == -1)
1380 return got_error(GOT_ERR_PIN_PACK);
1382 repo->pinned_pack = pinned_pack;
1383 repo->pinned_packidx = pinned_packidx;
1384 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1385 return NULL;
1388 struct got_pack *
1389 got_repo_get_pinned_pack(struct got_repository *repo)
1391 if (repo->pinned_pack >= 0 &&
1392 repo->pinned_pack < repo->pack_cache_size)
1393 return &repo->packs[repo->pinned_pack];
1395 return NULL;
1398 void
1399 got_repo_unpin_pack(struct got_repository *repo)
1401 repo->pinned_packidx = -1;
1402 repo->pinned_pack = -1;
1403 repo->pinned_pid = 0;
1406 const struct got_error *
1407 got_repo_init(const char *repo_path, const char *head_name)
1409 const struct got_error *err = NULL;
1410 const char *dirnames[] = {
1411 GOT_OBJECTS_DIR,
1412 GOT_OBJECTS_PACK_DIR,
1413 GOT_REFS_DIR,
1415 const char *description_str = "Unnamed repository; "
1416 "edit this file 'description' to name the repository.";
1417 const char *headref = "ref: refs/heads/";
1418 const char *gitconfig_str = "[core]\n"
1419 "\trepositoryformatversion = 0\n"
1420 "\tfilemode = true\n"
1421 "\tbare = true\n";
1422 char *headref_str, *path;
1423 size_t i;
1425 if (!got_path_dir_is_empty(repo_path))
1426 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1428 for (i = 0; i < nitems(dirnames); i++) {
1429 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1430 return got_error_from_errno("asprintf");
1432 err = got_path_mkdir(path);
1433 free(path);
1434 if (err)
1435 return err;
1438 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1439 return got_error_from_errno("asprintf");
1440 err = got_path_create_file(path, description_str);
1441 free(path);
1442 if (err)
1443 return err;
1445 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1446 return got_error_from_errno("asprintf");
1447 if (asprintf(&headref_str, "%s%s", headref,
1448 head_name ? head_name : "main") == -1) {
1449 free(path);
1450 return got_error_from_errno("asprintf");
1452 err = got_path_create_file(path, headref_str);
1453 free(headref_str);
1454 free(path);
1455 if (err)
1456 return err;
1458 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1459 return got_error_from_errno("asprintf");
1460 err = got_path_create_file(path, gitconfig_str);
1461 free(path);
1462 if (err)
1463 return err;
1465 return NULL;
1468 static void
1469 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1471 struct got_pathlist_entry *pe;
1473 while (!TAILQ_EMPTY(packidx_paths)) {
1474 pe = TAILQ_FIRST(packidx_paths);
1475 TAILQ_REMOVE(packidx_paths, pe, entry);
1476 free((char *)pe->path);
1477 free(pe);
1481 static const struct got_error *
1482 match_packed_object(struct got_object_id **unique_id,
1483 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1485 const struct got_error *err = NULL;
1486 char *objects_pack_dir = NULL;
1487 struct got_object_id_queue matched_ids;
1488 struct got_pathlist_entry *pe;
1489 struct stat sb;
1491 STAILQ_INIT(&matched_ids);
1493 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1494 if (objects_pack_dir == NULL)
1495 return got_error_from_errno("got_repo_get_path_objects_pack");
1497 if (stat(objects_pack_dir, &sb) == -1) {
1498 if (errno != ENOENT) {
1499 err = got_error_from_errno2("stat", objects_pack_dir);
1500 goto done;
1502 } else if (sb.st_mtime != repo->pack_path_mtime) {
1503 purge_packidx_paths(&repo->packidx_paths);
1504 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1505 if (err)
1506 goto done;
1509 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1510 const char *path_packidx = pe->path;
1511 struct got_packidx *packidx;
1512 struct got_object_qid *qid;
1514 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1515 path_packidx, 0);
1516 if (err)
1517 break;
1519 err = got_packidx_match_id_str_prefix(&matched_ids,
1520 packidx, id_str_prefix);
1521 if (err) {
1522 got_packidx_close(packidx);
1523 break;
1525 err = got_packidx_close(packidx);
1526 if (err)
1527 break;
1529 STAILQ_FOREACH(qid, &matched_ids, entry) {
1530 if (obj_type != GOT_OBJ_TYPE_ANY) {
1531 int matched_type;
1532 err = got_object_get_type(&matched_type, repo,
1533 &qid->id);
1534 if (err)
1535 goto done;
1536 if (matched_type != obj_type)
1537 continue;
1539 if (*unique_id == NULL) {
1540 *unique_id = got_object_id_dup(&qid->id);
1541 if (*unique_id == NULL) {
1542 err = got_error_from_errno("malloc");
1543 goto done;
1545 } else {
1546 if (got_object_id_cmp(*unique_id,
1547 &qid->id) == 0)
1548 continue; /* packed multiple times */
1549 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1550 goto done;
1554 done:
1555 free(objects_pack_dir);
1556 got_object_id_queue_free(&matched_ids);
1557 if (err) {
1558 free(*unique_id);
1559 *unique_id = NULL;
1561 return err;
1564 static const struct got_error *
1565 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1566 const char *object_dir, const char *id_str_prefix, int obj_type,
1567 struct got_repository *repo)
1569 const struct got_error *err = NULL;
1570 char *path, *id_str = NULL;
1571 DIR *dir = NULL;
1572 struct dirent *dent;
1573 struct got_object_id id;
1575 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1576 err = got_error_from_errno("asprintf");
1577 goto done;
1580 dir = opendir(path);
1581 if (dir == NULL) {
1582 if (errno == ENOENT) {
1583 err = NULL;
1584 goto done;
1586 err = got_error_from_errno2("opendir", path);
1587 goto done;
1589 while ((dent = readdir(dir)) != NULL) {
1590 int cmp;
1592 free(id_str);
1593 id_str = NULL;
1595 if (strcmp(dent->d_name, ".") == 0 ||
1596 strcmp(dent->d_name, "..") == 0)
1597 continue;
1599 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1600 err = got_error_from_errno("asprintf");
1601 goto done;
1604 if (!got_parse_sha1_digest(id.sha1, id_str))
1605 continue;
1608 * Directory entries do not necessarily appear in
1609 * sorted order, so we must iterate over all of them.
1611 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1612 if (cmp != 0)
1613 continue;
1615 if (*unique_id == NULL) {
1616 if (obj_type != GOT_OBJ_TYPE_ANY) {
1617 int matched_type;
1618 err = got_object_get_type(&matched_type, repo,
1619 &id);
1620 if (err)
1621 goto done;
1622 if (matched_type != obj_type)
1623 continue;
1625 *unique_id = got_object_id_dup(&id);
1626 if (*unique_id == NULL) {
1627 err = got_error_from_errno("got_object_id_dup");
1628 goto done;
1630 } else {
1631 if (got_object_id_cmp(*unique_id, &id) == 0)
1632 continue; /* both packed and loose */
1633 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1634 goto done;
1637 done:
1638 if (dir && closedir(dir) != 0 && err == NULL)
1639 err = got_error_from_errno("closedir");
1640 if (err) {
1641 free(*unique_id);
1642 *unique_id = NULL;
1644 free(id_str);
1645 free(path);
1646 return err;
1649 const struct got_error *
1650 got_repo_match_object_id_prefix(struct got_object_id **id,
1651 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1653 const struct got_error *err = NULL;
1654 char *path_objects = NULL, *object_dir = NULL;
1655 size_t len;
1656 int i;
1658 *id = NULL;
1660 path_objects = got_repo_get_path_objects(repo);
1662 len = strlen(id_str_prefix);
1663 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1664 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1665 goto done;
1668 for (i = 0; i < len; i++) {
1669 if (isxdigit((unsigned char)id_str_prefix[i]))
1670 continue;
1671 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1672 goto done;
1675 if (len >= 2) {
1676 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1677 if (err)
1678 goto done;
1679 object_dir = strndup(id_str_prefix, 2);
1680 if (object_dir == NULL) {
1681 err = got_error_from_errno("strdup");
1682 goto done;
1684 err = match_loose_object(id, path_objects, object_dir,
1685 id_str_prefix, obj_type, repo);
1686 } else if (len == 1) {
1687 int i;
1688 for (i = 0; i < 0xf; i++) {
1689 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1690 == -1) {
1691 err = got_error_from_errno("asprintf");
1692 goto done;
1694 err = match_packed_object(id, repo, object_dir,
1695 obj_type);
1696 if (err)
1697 goto done;
1698 err = match_loose_object(id, path_objects, object_dir,
1699 id_str_prefix, obj_type, repo);
1700 if (err)
1701 goto done;
1703 } else {
1704 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1705 goto done;
1707 done:
1708 free(path_objects);
1709 free(object_dir);
1710 if (err) {
1711 free(*id);
1712 *id = NULL;
1713 } else if (*id == NULL) {
1714 switch (obj_type) {
1715 case GOT_OBJ_TYPE_BLOB:
1716 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1717 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1718 break;
1719 case GOT_OBJ_TYPE_TREE:
1720 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1721 GOT_OBJ_LABEL_TREE, id_str_prefix);
1722 break;
1723 case GOT_OBJ_TYPE_COMMIT:
1724 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1725 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1726 break;
1727 case GOT_OBJ_TYPE_TAG:
1728 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1729 GOT_OBJ_LABEL_TAG, id_str_prefix);
1730 break;
1731 default:
1732 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1733 break;
1737 return err;
1740 const struct got_error *
1741 got_repo_match_object_id(struct got_object_id **id, char **label,
1742 const char *id_str, int obj_type, struct got_reflist_head *refs,
1743 struct got_repository *repo)
1745 const struct got_error *err;
1746 struct got_tag_object *tag;
1747 struct got_reference *ref = NULL;
1749 *id = NULL;
1750 if (label)
1751 *label = NULL;
1753 if (refs) {
1754 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1755 refs, repo);
1756 if (err == NULL) {
1757 *id = got_object_id_dup(
1758 got_object_tag_get_object_id(tag));
1759 if (*id == NULL)
1760 err = got_error_from_errno("got_object_id_dup");
1761 else if (label && asprintf(label, "refs/tags/%s",
1762 got_object_tag_get_name(tag)) == -1) {
1763 err = got_error_from_errno("asprintf");
1764 free(*id);
1765 *id = NULL;
1767 got_object_tag_close(tag);
1768 return err;
1769 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1770 err->code != GOT_ERR_NO_OBJ)
1771 return err;
1774 err = got_ref_open(&ref, repo, id_str, 0);
1775 if (err == NULL) {
1776 err = got_ref_resolve(id, repo, ref);
1777 if (err)
1778 goto done;
1779 if (label) {
1780 *label = strdup(got_ref_get_name(ref));
1781 if (*label == NULL) {
1782 err = got_error_from_errno("strdup");
1783 goto done;
1786 } else {
1787 if (err->code != GOT_ERR_NOT_REF &&
1788 err->code != GOT_ERR_BAD_REF_NAME)
1789 goto done;
1790 err = got_repo_match_object_id_prefix(id, id_str,
1791 obj_type, repo);
1792 if (err) {
1793 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1794 err = got_error_not_ref(id_str);
1795 goto done;
1797 if (label) {
1798 err = got_object_id_str(label, *id);
1799 if (*label == NULL) {
1800 err = got_error_from_errno("strdup");
1801 goto done;
1805 done:
1806 if (ref)
1807 got_ref_close(ref);
1808 return err;
1811 const struct got_error *
1812 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1813 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1815 const struct got_error *err = NULL;
1816 struct got_reflist_entry *re;
1817 struct got_object_id *tag_id;
1818 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1820 *tag = NULL;
1822 TAILQ_FOREACH(re, refs, entry) {
1823 const char *refname;
1824 refname = got_ref_get_name(re->ref);
1825 if (got_ref_is_symbolic(re->ref))
1826 continue;
1827 if (strncmp(refname, "refs/tags/", 10) != 0)
1828 continue;
1829 if (!name_is_absolute)
1830 refname += strlen("refs/tags/");
1831 if (strcmp(refname, name) != 0)
1832 continue;
1833 err = got_ref_resolve(&tag_id, repo, re->ref);
1834 if (err)
1835 break;
1836 err = got_object_open_as_tag(tag, repo, tag_id);
1837 free(tag_id);
1838 if (err)
1839 break;
1840 if (obj_type == GOT_OBJ_TYPE_ANY ||
1841 got_object_tag_get_object_type(*tag) == obj_type)
1842 break;
1843 got_object_tag_close(*tag);
1844 *tag = NULL;
1847 if (err == NULL && *tag == NULL)
1848 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1849 GOT_OBJ_LABEL_TAG, name);
1850 return err;
1853 static const struct got_error *
1854 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1855 const char *name, mode_t mode, struct got_object_id *blob_id)
1857 const struct got_error *err = NULL;
1859 *new_te = NULL;
1861 *new_te = calloc(1, sizeof(**new_te));
1862 if (*new_te == NULL)
1863 return got_error_from_errno("calloc");
1865 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1866 sizeof((*new_te)->name)) {
1867 err = got_error(GOT_ERR_NO_SPACE);
1868 goto done;
1871 if (S_ISLNK(mode)) {
1872 (*new_te)->mode = S_IFLNK;
1873 } else {
1874 (*new_te)->mode = S_IFREG;
1875 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1877 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1878 done:
1879 if (err && *new_te) {
1880 free(*new_te);
1881 *new_te = NULL;
1883 return err;
1886 static const struct got_error *
1887 import_file(struct got_tree_entry **new_te, struct dirent *de,
1888 const char *path, struct got_repository *repo)
1890 const struct got_error *err;
1891 struct got_object_id *blob_id = NULL;
1892 char *filepath;
1893 struct stat sb;
1895 if (asprintf(&filepath, "%s%s%s", path,
1896 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1897 return got_error_from_errno("asprintf");
1899 if (lstat(filepath, &sb) != 0) {
1900 err = got_error_from_errno2("lstat", path);
1901 goto done;
1904 err = got_object_blob_create(&blob_id, filepath, repo);
1905 if (err)
1906 goto done;
1908 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1909 blob_id);
1910 done:
1911 free(filepath);
1912 if (err)
1913 free(blob_id);
1914 return err;
1917 static const struct got_error *
1918 insert_tree_entry(struct got_tree_entry *new_te,
1919 struct got_pathlist_head *paths)
1921 const struct got_error *err = NULL;
1922 struct got_pathlist_entry *new_pe;
1924 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1925 if (err)
1926 return err;
1927 if (new_pe == NULL)
1928 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1929 return NULL;
1932 static const struct got_error *write_tree(struct got_object_id **,
1933 const char *, struct got_pathlist_head *, struct got_repository *,
1934 got_repo_import_cb progress_cb, void *progress_arg);
1936 static const struct got_error *
1937 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1938 const char *path, struct got_pathlist_head *ignores,
1939 struct got_repository *repo,
1940 got_repo_import_cb progress_cb, void *progress_arg)
1942 const struct got_error *err;
1943 struct got_object_id *id = NULL;
1944 char *subdirpath;
1946 if (asprintf(&subdirpath, "%s%s%s", path,
1947 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1948 return got_error_from_errno("asprintf");
1950 (*new_te) = calloc(1, sizeof(**new_te));
1951 if (*new_te == NULL)
1952 return got_error_from_errno("calloc");
1953 (*new_te)->mode = S_IFDIR;
1954 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1955 sizeof((*new_te)->name)) {
1956 err = got_error(GOT_ERR_NO_SPACE);
1957 goto done;
1959 err = write_tree(&id, subdirpath, ignores, repo,
1960 progress_cb, progress_arg);
1961 if (err)
1962 goto done;
1963 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1965 done:
1966 free(id);
1967 free(subdirpath);
1968 if (err) {
1969 free(*new_te);
1970 *new_te = NULL;
1972 return err;
1975 static const struct got_error *
1976 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1977 struct got_pathlist_head *ignores, struct got_repository *repo,
1978 got_repo_import_cb progress_cb, void *progress_arg)
1980 const struct got_error *err = NULL;
1981 DIR *dir;
1982 struct dirent *de;
1983 int nentries;
1984 struct got_tree_entry *new_te = NULL;
1985 struct got_pathlist_head paths;
1986 struct got_pathlist_entry *pe;
1988 *new_tree_id = NULL;
1990 TAILQ_INIT(&paths);
1992 dir = opendir(path_dir);
1993 if (dir == NULL) {
1994 err = got_error_from_errno2("opendir", path_dir);
1995 goto done;
1998 nentries = 0;
1999 while ((de = readdir(dir)) != NULL) {
2000 int ignore = 0;
2001 int type;
2003 if (strcmp(de->d_name, ".") == 0 ||
2004 strcmp(de->d_name, "..") == 0)
2005 continue;
2007 TAILQ_FOREACH(pe, ignores, entry) {
2008 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2009 ignore = 1;
2010 break;
2013 if (ignore)
2014 continue;
2016 err = got_path_dirent_type(&type, path_dir, de);
2017 if (err)
2018 goto done;
2020 if (type == DT_DIR) {
2021 err = import_subdir(&new_te, de, path_dir,
2022 ignores, repo, progress_cb, progress_arg);
2023 if (err) {
2024 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2025 goto done;
2026 err = NULL;
2027 continue;
2029 } else if (type == DT_REG || type == DT_LNK) {
2030 err = import_file(&new_te, de, path_dir, repo);
2031 if (err)
2032 goto done;
2033 } else
2034 continue;
2036 err = insert_tree_entry(new_te, &paths);
2037 if (err)
2038 goto done;
2039 nentries++;
2042 if (TAILQ_EMPTY(&paths)) {
2043 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2044 "cannot create tree without any entries");
2045 goto done;
2048 TAILQ_FOREACH(pe, &paths, entry) {
2049 struct got_tree_entry *te = pe->data;
2050 char *path;
2051 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2052 continue;
2053 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2054 err = got_error_from_errno("asprintf");
2055 goto done;
2057 err = (*progress_cb)(progress_arg, path);
2058 free(path);
2059 if (err)
2060 goto done;
2063 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2064 done:
2065 if (dir)
2066 closedir(dir);
2067 got_pathlist_free(&paths);
2068 return err;
2071 const struct got_error *
2072 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2073 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2074 struct got_repository *repo, got_repo_import_cb progress_cb,
2075 void *progress_arg)
2077 const struct got_error *err;
2078 struct got_object_id *new_tree_id;
2080 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2081 progress_cb, progress_arg);
2082 if (err)
2083 return err;
2085 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2086 author, time(NULL), author, time(NULL), logmsg, repo);
2087 free(new_tree_id);
2088 return err;
2091 const struct got_error *
2092 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2093 struct got_repository *repo)
2095 const struct got_error *err = NULL;
2096 char *path_objects = NULL, *path = NULL;
2097 DIR *dir = NULL;
2098 struct got_object_id id;
2099 int i;
2101 *nobjects = 0;
2102 *ondisk_size = 0;
2104 path_objects = got_repo_get_path_objects(repo);
2105 if (path_objects == NULL)
2106 return got_error_from_errno("got_repo_get_path_objects");
2108 for (i = 0; i <= 0xff; i++) {
2109 struct dirent *dent;
2111 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2112 err = got_error_from_errno("asprintf");
2113 break;
2116 dir = opendir(path);
2117 if (dir == NULL) {
2118 if (errno == ENOENT) {
2119 err = NULL;
2120 continue;
2122 err = got_error_from_errno2("opendir", path);
2123 break;
2126 while ((dent = readdir(dir)) != NULL) {
2127 char *id_str;
2128 int fd;
2129 struct stat sb;
2131 if (strcmp(dent->d_name, ".") == 0 ||
2132 strcmp(dent->d_name, "..") == 0)
2133 continue;
2135 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2136 err = got_error_from_errno("asprintf");
2137 goto done;
2140 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2141 free(id_str);
2142 continue;
2144 free(id_str);
2146 err = got_object_open_loose_fd(&fd, &id, repo);
2147 if (err)
2148 goto done;
2150 if (fstat(fd, &sb) == -1) {
2151 err = got_error_from_errno("fstat");
2152 close(fd);
2153 goto done;
2155 (*nobjects)++;
2156 (*ondisk_size) += sb.st_size;
2158 if (close(fd) == -1) {
2159 err = got_error_from_errno("close");
2160 goto done;
2164 if (closedir(dir) != 0) {
2165 err = got_error_from_errno("closedir");
2166 goto done;
2168 dir = NULL;
2170 free(path);
2171 path = NULL;
2173 done:
2174 if (dir && closedir(dir) != 0 && err == NULL)
2175 err = got_error_from_errno("closedir");
2177 if (err) {
2178 *nobjects = 0;
2179 *ondisk_size = 0;
2181 free(path_objects);
2182 free(path);
2183 return err;
2186 const struct got_error *
2187 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2188 off_t *total_packsize, struct got_repository *repo)
2190 const struct got_error *err = NULL;
2191 DIR *packdir = NULL;
2192 struct dirent *dent;
2193 struct got_packidx *packidx = NULL;
2194 char *path_packidx;
2195 char *path_packfile;
2196 int packdir_fd;
2197 struct stat sb;
2199 *npackfiles = 0;
2200 *nobjects = 0;
2201 *total_packsize = 0;
2203 packdir_fd = openat(got_repo_get_fd(repo),
2204 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2205 if (packdir_fd == -1) {
2206 return got_error_from_errno_fmt("openat: %s/%s",
2207 got_repo_get_path_git_dir(repo),
2208 GOT_OBJECTS_PACK_DIR);
2211 packdir = fdopendir(packdir_fd);
2212 if (packdir == NULL) {
2213 err = got_error_from_errno("fdopendir");
2214 goto done;
2217 while ((dent = readdir(packdir)) != NULL) {
2218 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2219 continue;
2221 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2222 dent->d_name) == -1) {
2223 err = got_error_from_errno("asprintf");
2224 goto done;
2227 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2228 path_packidx, 0);
2229 free(path_packidx);
2230 if (err)
2231 goto done;
2233 if (fstat(packidx->fd, &sb) == -1)
2234 goto done;
2235 *total_packsize += sb.st_size;
2237 err = got_packidx_get_packfile_path(&path_packfile,
2238 packidx->path_packidx);
2239 if (err)
2240 goto done;
2242 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2243 0) == -1) {
2244 free(path_packfile);
2245 goto done;
2247 free(path_packfile);
2248 *total_packsize += sb.st_size;
2250 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2252 (*npackfiles)++;
2254 got_packidx_close(packidx);
2255 packidx = NULL;
2257 done:
2258 if (packidx)
2259 got_packidx_close(packidx);
2260 if (packdir && closedir(packdir) != 0 && err == NULL)
2261 err = got_error_from_errno("closedir");
2262 if (err) {
2263 *npackfiles = 0;
2264 *nobjects = 0;
2265 *total_packsize = 0;
2267 return err;
2270 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2271 got_packidx_bloom_filter_cmp);