2 * Copyright (c) 2019, 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/queue.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_cache.h"
40 #include "got_lib_privsep.h"
41 #include "got_lib_pack.h"
42 #include "got_lib_repository.h"
44 const struct got_error *
45 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
46 char **gitconfig_author_name, char **gitconfig_author_email,
47 struct got_remote_repo **remotes, int *nremotes,
48 char **gitconfig_owner, char ***extnames, char ***extvals,
49 int *nextensions, const char *gitconfig_path)
51 const struct got_error *err = NULL, *child_err = NULL;
53 int imsg_fds[2] = { -1, -1 };
57 *gitconfig_repository_format_version = 0;
64 *gitconfig_author_name = NULL;
65 *gitconfig_author_email = NULL;
71 *gitconfig_owner = NULL;
73 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
77 return got_error_from_errno2("open", gitconfig_path);
80 ibuf = calloc(1, sizeof(*ibuf));
82 err = got_error_from_errno("calloc");
86 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
87 err = got_error_from_errno("socketpair");
93 err = got_error_from_errno("fork");
95 } else if (pid == 0) {
96 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
101 if (close(imsg_fds[1]) == -1) {
102 err = got_error_from_errno("close");
106 imsg_init(ibuf, imsg_fds[0]);
108 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
113 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
117 err = got_privsep_recv_gitconfig_int(
118 gitconfig_repository_format_version, ibuf);
122 if (extnames && extvals && nextensions) {
123 err = got_privsep_send_gitconfig_repository_extensions_req(
127 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
130 if (*nextensions > 0) {
132 *extnames = calloc(*nextensions, sizeof(char *));
133 if (*extnames == NULL) {
134 err = got_error_from_errno("calloc");
137 *extvals = calloc(*nextensions, sizeof(char *));
138 if (*extvals == NULL) {
139 err = got_error_from_errno("calloc");
142 for (i = 0; i < *nextensions; i++) {
144 err = got_privsep_recv_gitconfig_pair(&ext,
148 (*extnames)[i] = ext;
154 err = got_privsep_send_gitconfig_author_name_req(ibuf);
158 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
162 err = got_privsep_send_gitconfig_author_email_req(ibuf);
166 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
170 if (remotes && nremotes) {
171 err = got_privsep_send_gitconfig_remotes_req(ibuf);
175 err = got_privsep_recv_gitconfig_remotes(remotes,
181 if (gitconfig_owner) {
182 err = got_privsep_send_gitconfig_owner_req(ibuf);
185 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
190 err = got_privsep_send_stop(imsg_fds[0]);
191 child_err = got_privsep_wait_for_child(pid);
192 if (child_err && err == NULL)
195 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
196 err = got_error_from_errno("close");
197 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
198 err = got_error_from_errno("close");
199 if (fd != -1 && close(fd) == -1 && err == NULL)
200 err = got_error_from_errno2("close", gitconfig_path);