Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 93658fb9 2020-03-18 stsp *
4 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
5 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
6 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
7 93658fb9 2020-03-18 stsp *
8 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 93658fb9 2020-03-18 stsp */
16 93658fb9 2020-03-18 stsp
17 93658fb9 2020-03-18 stsp #include <sys/types.h>
18 93658fb9 2020-03-18 stsp #include <sys/stat.h>
19 93658fb9 2020-03-18 stsp #include <sys/queue.h>
20 93658fb9 2020-03-18 stsp #include <sys/uio.h>
21 93658fb9 2020-03-18 stsp #include <sys/socket.h>
22 93658fb9 2020-03-18 stsp #include <sys/wait.h>
23 93658fb9 2020-03-18 stsp #include <sys/syslimits.h>
24 93658fb9 2020-03-18 stsp #include <sys/resource.h>
25 93658fb9 2020-03-18 stsp #include <sys/socket.h>
26 93658fb9 2020-03-18 stsp
27 93658fb9 2020-03-18 stsp #include <errno.h>
28 5cc27ede 2020-03-18 stsp #include <err.h>
29 93658fb9 2020-03-18 stsp #include <fcntl.h>
30 93658fb9 2020-03-18 stsp #include <stdio.h>
31 93658fb9 2020-03-18 stsp #include <stdlib.h>
32 93658fb9 2020-03-18 stsp #include <string.h>
33 93658fb9 2020-03-18 stsp #include <stdint.h>
34 93658fb9 2020-03-18 stsp #include <sha1.h>
35 93658fb9 2020-03-18 stsp #include <zlib.h>
36 93658fb9 2020-03-18 stsp #include <ctype.h>
37 93658fb9 2020-03-18 stsp #include <limits.h>
38 93658fb9 2020-03-18 stsp #include <imsg.h>
39 93658fb9 2020-03-18 stsp #include <time.h>
40 93658fb9 2020-03-18 stsp #include <uuid.h>
41 93658fb9 2020-03-18 stsp #include <netdb.h>
42 93658fb9 2020-03-18 stsp #include <netinet/in.h>
43 93658fb9 2020-03-18 stsp
44 93658fb9 2020-03-18 stsp #include "got_error.h"
45 93658fb9 2020-03-18 stsp #include "got_reference.h"
46 93658fb9 2020-03-18 stsp #include "got_repository.h"
47 93658fb9 2020-03-18 stsp #include "got_path.h"
48 93658fb9 2020-03-18 stsp #include "got_cancel.h"
49 93658fb9 2020-03-18 stsp #include "got_worktree.h"
50 93658fb9 2020-03-18 stsp #include "got_object.h"
51 fe4e1501 2020-03-18 stsp #include "got_opentemp.h"
52 82ebf666 2020-03-18 stsp #include "got_fetch.h"
53 93658fb9 2020-03-18 stsp
54 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
55 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
56 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
57 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
58 93658fb9 2020-03-18 stsp #include "got_lib_object_create.h"
59 93658fb9 2020-03-18 stsp #include "got_lib_pack.h"
60 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
61 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
62 93658fb9 2020-03-18 stsp #include "got_lib_object_cache.h"
63 93658fb9 2020-03-18 stsp #include "got_lib_repository.h"
64 93658fb9 2020-03-18 stsp
65 93658fb9 2020-03-18 stsp #define GOT_PROTOMAX 64
66 93658fb9 2020-03-18 stsp #define GOT_HOSTMAX 256
67 93658fb9 2020-03-18 stsp #define GOT_PATHMAX 512
68 93658fb9 2020-03-18 stsp #define GOT_REPOMAX 256
69 93658fb9 2020-03-18 stsp #define GOT_PORTMAX 16
70 93658fb9 2020-03-18 stsp #define GOT_URIMAX 1024
71 93658fb9 2020-03-18 stsp
72 93658fb9 2020-03-18 stsp static int
73 93658fb9 2020-03-18 stsp hassuffix(char *base, char *suf)
74 93658fb9 2020-03-18 stsp {
75 93658fb9 2020-03-18 stsp int nb, ns;
76 93658fb9 2020-03-18 stsp
77 93658fb9 2020-03-18 stsp nb = strlen(base);
78 93658fb9 2020-03-18 stsp ns = strlen(suf);
79 93658fb9 2020-03-18 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
80 93658fb9 2020-03-18 stsp return 1;
81 93658fb9 2020-03-18 stsp return 0;
82 93658fb9 2020-03-18 stsp }
83 93658fb9 2020-03-18 stsp
84 5cc27ede 2020-03-18 stsp static const struct got_error *
85 09838ffc 2020-03-18 stsp dial_ssh(int *fetchfd, const char *host, const char *port, const char *path,
86 09838ffc 2020-03-18 stsp const char *direction)
87 93658fb9 2020-03-18 stsp {
88 5cc27ede 2020-03-18 stsp const struct got_error *error = NULL;
89 93658fb9 2020-03-18 stsp int pid, pfd[2];
90 93658fb9 2020-03-18 stsp char cmd[64];
91 93658fb9 2020-03-18 stsp
92 5cc27ede 2020-03-18 stsp *fetchfd = -1;
93 5cc27ede 2020-03-18 stsp
94 93658fb9 2020-03-18 stsp if (pipe(pfd) == -1)
95 5cc27ede 2020-03-18 stsp return got_error_from_errno("pipe");
96 5cc27ede 2020-03-18 stsp
97 93658fb9 2020-03-18 stsp pid = fork();
98 5cc27ede 2020-03-18 stsp if (pid == -1) {
99 5cc27ede 2020-03-18 stsp error = got_error_from_errno("fork");
100 5cc27ede 2020-03-18 stsp close(pfd[0]);
101 93658fb9 2020-03-18 stsp close(pfd[1]);
102 5cc27ede 2020-03-18 stsp return error;
103 5cc27ede 2020-03-18 stsp } else if (pid == 0) {
104 5cc27ede 2020-03-18 stsp int n;
105 5cc27ede 2020-03-18 stsp close(pfd[1]);
106 93658fb9 2020-03-18 stsp dup2(pfd[0], 0);
107 93658fb9 2020-03-18 stsp dup2(pfd[0], 1);
108 5cc27ede 2020-03-18 stsp n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
109 5cc27ede 2020-03-18 stsp if (n < 0 || n >= sizeof(cmd))
110 5cc27ede 2020-03-18 stsp err(1, "snprintf");
111 5cc27ede 2020-03-18 stsp if (execlp("ssh", "ssh", host, cmd, path, NULL) == -1)
112 5cc27ede 2020-03-18 stsp err(1, "execlp");
113 5cc27ede 2020-03-18 stsp abort(); /* not reached */
114 5cc27ede 2020-03-18 stsp } else {
115 93658fb9 2020-03-18 stsp close(pfd[0]);
116 5cc27ede 2020-03-18 stsp *fetchfd = pfd[1];
117 5cc27ede 2020-03-18 stsp return NULL;
118 93658fb9 2020-03-18 stsp }
119 93658fb9 2020-03-18 stsp }
120 93658fb9 2020-03-18 stsp
121 5cc27ede 2020-03-18 stsp static const struct got_error *
122 09838ffc 2020-03-18 stsp dial_git(int *fetchfd, const char *host, const char *port, const char *path,
123 09838ffc 2020-03-18 stsp const char *direction)
124 93658fb9 2020-03-18 stsp {
125 5cc27ede 2020-03-18 stsp const struct got_error *err = NULL;
126 93658fb9 2020-03-18 stsp struct addrinfo hints, *servinfo, *p;
127 5cc27ede 2020-03-18 stsp char *cmd = NULL, *pkt = NULL;
128 4312a498 2020-03-18 stsp int fd = -1, totlen, r, eaicode;
129 5cc27ede 2020-03-18 stsp
130 5cc27ede 2020-03-18 stsp *fetchfd = -1;
131 93658fb9 2020-03-18 stsp
132 93658fb9 2020-03-18 stsp memset(&hints, 0, sizeof hints);
133 93658fb9 2020-03-18 stsp hints.ai_family = AF_UNSPEC;
134 93658fb9 2020-03-18 stsp hints.ai_socktype = SOCK_STREAM;
135 5cc27ede 2020-03-18 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
136 5cc27ede 2020-03-18 stsp if (eaicode)
137 5cc27ede 2020-03-18 stsp return got_error_msg(GOT_ERR_ADDRINFO, gai_strerror(eaicode));
138 93658fb9 2020-03-18 stsp
139 93658fb9 2020-03-18 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
140 93658fb9 2020-03-18 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
141 93658fb9 2020-03-18 stsp p->ai_protocol)) == -1)
142 93658fb9 2020-03-18 stsp continue;
143 93658fb9 2020-03-18 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
144 93658fb9 2020-03-18 stsp break;
145 5cc27ede 2020-03-18 stsp err = got_error_from_errno("connect");
146 93658fb9 2020-03-18 stsp close(fd);
147 93658fb9 2020-03-18 stsp }
148 93658fb9 2020-03-18 stsp if (p == NULL)
149 5cc27ede 2020-03-18 stsp goto done;
150 93658fb9 2020-03-18 stsp
151 4312a498 2020-03-18 stsp if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
152 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
153 5cc27ede 2020-03-18 stsp goto done;
154 5cc27ede 2020-03-18 stsp }
155 4312a498 2020-03-18 stsp totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
156 4312a498 2020-03-18 stsp if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
157 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
158 5cc27ede 2020-03-18 stsp goto done;
159 5cc27ede 2020-03-18 stsp }
160 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
161 4312a498 2020-03-18 stsp if (r == -1) {
162 4312a498 2020-03-18 stsp err = got_error_from_errno("write");
163 4312a498 2020-03-18 stsp goto done;
164 4312a498 2020-03-18 stsp }
165 4312a498 2020-03-18 stsp if (asprintf(&pkt, "host=%s", host) == -1) {
166 4312a498 2020-03-18 stsp err = got_error_from_errno("asprintf");
167 4312a498 2020-03-18 stsp goto done;
168 4312a498 2020-03-18 stsp }
169 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
170 4312a498 2020-03-18 stsp if (r == -1) {
171 5cc27ede 2020-03-18 stsp err = got_error_from_errno("write");
172 4312a498 2020-03-18 stsp goto done;
173 4312a498 2020-03-18 stsp }
174 5cc27ede 2020-03-18 stsp done:
175 93658fb9 2020-03-18 stsp free(cmd);
176 93658fb9 2020-03-18 stsp free(pkt);
177 5cc27ede 2020-03-18 stsp if (err) {
178 5cc27ede 2020-03-18 stsp if (fd != -1)
179 5cc27ede 2020-03-18 stsp close(fd);
180 5cc27ede 2020-03-18 stsp } else
181 5cc27ede 2020-03-18 stsp *fetchfd = fd;
182 5cc27ede 2020-03-18 stsp return err;
183 93658fb9 2020-03-18 stsp }
184 93658fb9 2020-03-18 stsp
185 82ebf666 2020-03-18 stsp const struct got_error *
186 82ebf666 2020-03-18 stsp got_fetch_parse_uri(char **proto, char **host, char **port,
187 82ebf666 2020-03-18 stsp char **server_path, char **repo_name, const char *uri)
188 93658fb9 2020-03-18 stsp {
189 82ebf666 2020-03-18 stsp const struct got_error *err = NULL;
190 93658fb9 2020-03-18 stsp char *s, *p, *q;
191 93658fb9 2020-03-18 stsp int n, hasport;
192 93658fb9 2020-03-18 stsp
193 82ebf666 2020-03-18 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
194 82ebf666 2020-03-18 stsp
195 93658fb9 2020-03-18 stsp p = strstr(uri, "://");
196 93658fb9 2020-03-18 stsp if (!p) {
197 82ebf666 2020-03-18 stsp return got_error(GOT_ERR_PARSE_URI);
198 93658fb9 2020-03-18 stsp }
199 82ebf666 2020-03-18 stsp *proto = strndup(uri, p - uri);
200 82ebf666 2020-03-18 stsp if (proto == NULL) {
201 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
202 82ebf666 2020-03-18 stsp goto done;
203 82ebf666 2020-03-18 stsp }
204 82ebf666 2020-03-18 stsp
205 82ebf666 2020-03-18 stsp hasport = (strcmp(*proto, "git") == 0 ||
206 82ebf666 2020-03-18 stsp strstr(*proto, "http") == *proto);
207 93658fb9 2020-03-18 stsp s = p + 3;
208 93658fb9 2020-03-18 stsp p = NULL;
209 93658fb9 2020-03-18 stsp if (!hasport) {
210 93658fb9 2020-03-18 stsp p = strstr(s, ":");
211 93658fb9 2020-03-18 stsp if (p != NULL)
212 93658fb9 2020-03-18 stsp p++;
213 93658fb9 2020-03-18 stsp }
214 93658fb9 2020-03-18 stsp if (p == NULL)
215 93658fb9 2020-03-18 stsp p = strstr(s, "/");
216 93658fb9 2020-03-18 stsp if (p == NULL || strlen(p) == 1) {
217 82ebf666 2020-03-18 stsp err = got_error(GOT_ERR_PARSE_URI);
218 82ebf666 2020-03-18 stsp goto done;
219 93658fb9 2020-03-18 stsp }
220 93658fb9 2020-03-18 stsp
221 93658fb9 2020-03-18 stsp q = memchr(s, ':', p - s);
222 93658fb9 2020-03-18 stsp if (q) {
223 82ebf666 2020-03-18 stsp *host = strndup(s, q - s);
224 82ebf666 2020-03-18 stsp if (*host == NULL) {
225 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
226 82ebf666 2020-03-18 stsp goto done;
227 82ebf666 2020-03-18 stsp }
228 82ebf666 2020-03-18 stsp *port = strndup(q + 1, p - (q + 1));
229 82ebf666 2020-03-18 stsp if (*port == NULL) {
230 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
231 82ebf666 2020-03-18 stsp goto done;
232 82ebf666 2020-03-18 stsp }
233 82ebf666 2020-03-18 stsp } else {
234 82ebf666 2020-03-18 stsp *host = strndup(s, p - s);
235 82ebf666 2020-03-18 stsp if (*host == NULL) {
236 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
237 82ebf666 2020-03-18 stsp goto done;
238 82ebf666 2020-03-18 stsp }
239 82ebf666 2020-03-18 stsp if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
240 82ebf666 2020-03-18 stsp err = got_error_from_errno("asprintf");
241 82ebf666 2020-03-18 stsp goto done;
242 82ebf666 2020-03-18 stsp }
243 93658fb9 2020-03-18 stsp }
244 93658fb9 2020-03-18 stsp
245 82ebf666 2020-03-18 stsp *server_path = strdup(p);
246 82ebf666 2020-03-18 stsp if (*server_path == NULL) {
247 82ebf666 2020-03-18 stsp err = got_error_from_errno("strdup");
248 82ebf666 2020-03-18 stsp goto done;
249 82ebf666 2020-03-18 stsp }
250 82ebf666 2020-03-18 stsp
251 93658fb9 2020-03-18 stsp p = strrchr(p, '/') + 1;
252 93658fb9 2020-03-18 stsp if (!p || strlen(p) == 0) {
253 93658fb9 2020-03-18 stsp //werrstr("missing repository in uri");
254 82ebf666 2020-03-18 stsp err = got_error(GOT_ERR_PARSE_URI);
255 82ebf666 2020-03-18 stsp goto done;
256 93658fb9 2020-03-18 stsp }
257 93658fb9 2020-03-18 stsp n = strlen(p);
258 93658fb9 2020-03-18 stsp if (hassuffix(p, ".git"))
259 93658fb9 2020-03-18 stsp n -= 4;
260 82ebf666 2020-03-18 stsp *repo_name = strndup(p, (p + n) - p);
261 82ebf666 2020-03-18 stsp if (*repo_name == NULL) {
262 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
263 82ebf666 2020-03-18 stsp goto done;
264 82ebf666 2020-03-18 stsp }
265 82ebf666 2020-03-18 stsp done:
266 82ebf666 2020-03-18 stsp if (err) {
267 82ebf666 2020-03-18 stsp free(*proto);
268 82ebf666 2020-03-18 stsp *proto = NULL;
269 82ebf666 2020-03-18 stsp free(*host);
270 82ebf666 2020-03-18 stsp *host = NULL;
271 82ebf666 2020-03-18 stsp free(*port);
272 82ebf666 2020-03-18 stsp *port = NULL;
273 82ebf666 2020-03-18 stsp free(*server_path);
274 82ebf666 2020-03-18 stsp *server_path = NULL;
275 82ebf666 2020-03-18 stsp free(*repo_name);
276 82ebf666 2020-03-18 stsp *repo_name = NULL;
277 82ebf666 2020-03-18 stsp }
278 82ebf666 2020-03-18 stsp return err;
279 93658fb9 2020-03-18 stsp }
280 93658fb9 2020-03-18 stsp
281 93658fb9 2020-03-18 stsp const struct got_error*
282 09838ffc 2020-03-18 stsp got_fetch(const char *proto, const char *host, const char *port,
283 09838ffc 2020-03-18 stsp const char *server_path, const char *repo_name,
284 bb64b798 2020-03-18 stsp const char *branch_filter, struct got_repository *repo)
285 93658fb9 2020-03-18 stsp {
286 8e278d17 2020-03-18 stsp int imsg_fetchfds[2], imsg_idxfds[2], fetchfd = -1;
287 8e278d17 2020-03-18 stsp int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1;
288 8e278d17 2020-03-18 stsp int status, done = 0;
289 8f2d01a6 2020-03-18 stsp struct got_object_id *packhash = NULL;
290 93658fb9 2020-03-18 stsp const struct got_error *err;
291 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
292 93658fb9 2020-03-18 stsp pid_t pid;
293 bb64b798 2020-03-18 stsp char *tmppackpath = NULL, *tmpidxpath = NULL;
294 afa77e03 2020-03-18 stsp char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
295 bb64b798 2020-03-18 stsp const char *repo_path = got_repo_get_path(repo);
296 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
297 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
298 ee61b6d3 2020-03-18 stsp char *path;
299 abe0f35f 2020-03-18 stsp
300 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
301 93658fb9 2020-03-18 stsp
302 93658fb9 2020-03-18 stsp fetchfd = -1;
303 bb64b798 2020-03-18 stsp
304 ee61b6d3 2020-03-18 stsp if (asprintf(&path, "%s/objects/path", repo_path) == -1) {
305 ee61b6d3 2020-03-18 stsp err = got_error_from_errno("asprintf");
306 8e278d17 2020-03-18 stsp goto done;
307 8e278d17 2020-03-18 stsp }
308 ee61b6d3 2020-03-18 stsp err = got_path_mkdir(path);
309 ee61b6d3 2020-03-18 stsp free(path);
310 ee61b6d3 2020-03-18 stsp if (err)
311 8e278d17 2020-03-18 stsp goto done;
312 ee61b6d3 2020-03-18 stsp if (asprintf(&path, "%s/objects/path/fetching.pack", repo_path) == -1) {
313 ee61b6d3 2020-03-18 stsp err = got_error_from_errno("asprintf");
314 ee61b6d3 2020-03-18 stsp goto done;
315 8e278d17 2020-03-18 stsp }
316 ee61b6d3 2020-03-18 stsp err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
317 ee61b6d3 2020-03-18 stsp free(path);
318 fe4e1501 2020-03-18 stsp if (err)
319 8e278d17 2020-03-18 stsp goto done;
320 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
321 8e278d17 2020-03-18 stsp if (npackfd == -1) {
322 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
323 8e278d17 2020-03-18 stsp goto done;
324 8e278d17 2020-03-18 stsp }
325 ee61b6d3 2020-03-18 stsp if (asprintf(&path, "%s/objects/path/fetching.idx", repo_path) == -1) {
326 ee61b6d3 2020-03-18 stsp err = got_error_from_errno("asprintf");
327 ee61b6d3 2020-03-18 stsp goto done;
328 ee61b6d3 2020-03-18 stsp }
329 ee61b6d3 2020-03-18 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
330 ee61b6d3 2020-03-18 stsp free(path);
331 fe4e1501 2020-03-18 stsp if (err)
332 8e278d17 2020-03-18 stsp goto done;
333 93658fb9 2020-03-18 stsp nidxfd = dup(idxfd);
334 8e278d17 2020-03-18 stsp if (nidxfd == -1) {
335 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
336 8e278d17 2020-03-18 stsp goto done;
337 8e278d17 2020-03-18 stsp }
338 93658fb9 2020-03-18 stsp
339 93658fb9 2020-03-18 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
340 ee61b6d3 2020-03-18 stsp err = dial_ssh(&fetchfd, host, port, server_path, "upload");
341 93658fb9 2020-03-18 stsp else if (strcmp(proto, "git") == 0)
342 ee61b6d3 2020-03-18 stsp err = dial_git(&fetchfd, host, port, server_path, "upload");
343 93658fb9 2020-03-18 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
344 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PROTO);
345 93658fb9 2020-03-18 stsp else
346 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PROTO);
347 75fd4eb2 2020-03-18 stsp if (err)
348 8e278d17 2020-03-18 stsp goto done;
349 93658fb9 2020-03-18 stsp
350 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
351 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
352 8e278d17 2020-03-18 stsp goto done;
353 8e278d17 2020-03-18 stsp }
354 93658fb9 2020-03-18 stsp
355 93658fb9 2020-03-18 stsp pid = fork();
356 8e278d17 2020-03-18 stsp if (pid == -1) {
357 8e278d17 2020-03-18 stsp err = got_error_from_errno("fork");
358 8e278d17 2020-03-18 stsp goto done;
359 8e278d17 2020-03-18 stsp } else if (pid == 0){
360 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_fetchfds,
361 8e278d17 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK, ".");
362 93658fb9 2020-03-18 stsp }
363 93658fb9 2020-03-18 stsp
364 8e278d17 2020-03-18 stsp if (close(imsg_fetchfds[1]) != 0) {
365 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
366 8e278d17 2020-03-18 stsp goto done;
367 8e278d17 2020-03-18 stsp }
368 93658fb9 2020-03-18 stsp imsg_init(&ibuf, imsg_fetchfds[0]);
369 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_req(&ibuf, fetchfd);
370 93658fb9 2020-03-18 stsp if (err != NULL)
371 8e278d17 2020-03-18 stsp goto done;
372 8e278d17 2020-03-18 stsp fetchfd = -1;
373 93658fb9 2020-03-18 stsp err = got_privsep_send_tmpfd(&ibuf, npackfd);
374 93658fb9 2020-03-18 stsp if (err != NULL)
375 8e278d17 2020-03-18 stsp goto done;
376 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
377 8e278d17 2020-03-18 stsp if (npackfd == -1) {
378 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
379 8e278d17 2020-03-18 stsp goto done;
380 8e278d17 2020-03-18 stsp }
381 8e278d17 2020-03-18 stsp
382 8f2d01a6 2020-03-18 stsp while (!done) {
383 8f2d01a6 2020-03-18 stsp struct got_object_id *id;
384 8f2d01a6 2020-03-18 stsp char *refname;
385 8e278d17 2020-03-18 stsp
386 8f2d01a6 2020-03-18 stsp err = got_privsep_recv_fetch_progress(&done,
387 abe0f35f 2020-03-18 stsp &id, &refname, &symrefs, &ibuf);
388 8f2d01a6 2020-03-18 stsp if (err != NULL)
389 8e278d17 2020-03-18 stsp goto done;
390 8f2d01a6 2020-03-18 stsp if (done) {
391 8f2d01a6 2020-03-18 stsp packhash = got_object_id_dup(id);
392 8e278d17 2020-03-18 stsp if (packhash == NULL) {
393 8e278d17 2020-03-18 stsp err = got_error_from_errno(
394 8f2d01a6 2020-03-18 stsp "got_object_id_dup");
395 8e278d17 2020-03-18 stsp goto done;
396 8e278d17 2020-03-18 stsp }
397 abe0f35f 2020-03-18 stsp printf("symrefs:");
398 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
399 abe0f35f 2020-03-18 stsp printf(" %s:%s", pe->path,
400 abe0f35f 2020-03-18 stsp (const char *)pe->data);
401 abe0f35f 2020-03-18 stsp }
402 abe0f35f 2020-03-18 stsp printf("\n");
403 ee61b6d3 2020-03-18 stsp } else if (refname && id) {
404 ee61b6d3 2020-03-18 stsp struct got_reference *ref;
405 8f2d01a6 2020-03-18 stsp char *id_str;
406 8f2d01a6 2020-03-18 stsp /* TODO Use a progress callback */
407 8f2d01a6 2020-03-18 stsp err = got_object_id_str(&id_str, id);
408 8f2d01a6 2020-03-18 stsp if (err)
409 8e278d17 2020-03-18 stsp goto done;
410 8f2d01a6 2020-03-18 stsp printf( "%.12s %s\n", id_str, refname);
411 ee61b6d3 2020-03-18 stsp
412 ee61b6d3 2020-03-18 stsp err = got_ref_alloc(&ref, refname, id);
413 ee61b6d3 2020-03-18 stsp if (err)
414 ee61b6d3 2020-03-18 stsp goto done;
415 ee61b6d3 2020-03-18 stsp
416 ee61b6d3 2020-03-18 stsp err = got_ref_write(ref, repo);
417 ee61b6d3 2020-03-18 stsp got_ref_close(ref);
418 ee61b6d3 2020-03-18 stsp if (err)
419 ee61b6d3 2020-03-18 stsp goto done;
420 8f2d01a6 2020-03-18 stsp }
421 8f2d01a6 2020-03-18 stsp }
422 8e278d17 2020-03-18 stsp if (waitpid(pid, &status, 0) == -1) {
423 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
424 8e278d17 2020-03-18 stsp goto done;
425 8e278d17 2020-03-18 stsp }
426 8e278d17 2020-03-18 stsp
427 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
428 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
429 8e278d17 2020-03-18 stsp goto done;
430 8e278d17 2020-03-18 stsp }
431 93658fb9 2020-03-18 stsp pid = fork();
432 8e278d17 2020-03-18 stsp if (pid == -1) {
433 8e278d17 2020-03-18 stsp err= got_error_from_errno("fork");
434 8e278d17 2020-03-18 stsp goto done;
435 8e278d17 2020-03-18 stsp } else if (pid == 0)
436 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_idxfds,
437 8e278d17 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK, ".");
438 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[1]) != 0) {
439 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
440 8e278d17 2020-03-18 stsp goto done;
441 8e278d17 2020-03-18 stsp }
442 93658fb9 2020-03-18 stsp imsg_init(&ibuf, imsg_idxfds[0]);
443 93658fb9 2020-03-18 stsp
444 8f2d01a6 2020-03-18 stsp err = got_privsep_send_index_pack_req(&ibuf, npackfd, packhash);
445 93658fb9 2020-03-18 stsp if (err != NULL)
446 8e278d17 2020-03-18 stsp goto done;
447 8e278d17 2020-03-18 stsp npackfd = -1;
448 93658fb9 2020-03-18 stsp err = got_privsep_send_tmpfd(&ibuf, nidxfd);
449 93658fb9 2020-03-18 stsp if (err != NULL)
450 8e278d17 2020-03-18 stsp goto done;
451 8e278d17 2020-03-18 stsp nidxfd = -1;
452 93658fb9 2020-03-18 stsp err = got_privsep_wait_index_pack_done(&ibuf);
453 93658fb9 2020-03-18 stsp if (err != NULL)
454 8e278d17 2020-03-18 stsp goto done;
455 93658fb9 2020-03-18 stsp imsg_clear(&ibuf);
456 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[0]) == -1) {
457 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
458 8e278d17 2020-03-18 stsp goto done;
459 8e278d17 2020-03-18 stsp }
460 8e278d17 2020-03-18 stsp if (waitpid(pid, &status, 0) == -1) {
461 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
462 8e278d17 2020-03-18 stsp goto done;
463 8e278d17 2020-03-18 stsp }
464 93658fb9 2020-03-18 stsp
465 8f2d01a6 2020-03-18 stsp err = got_object_id_str(&id_str, packhash);
466 afa77e03 2020-03-18 stsp if (err)
467 8e278d17 2020-03-18 stsp goto done;
468 ee61b6d3 2020-03-18 stsp if (asprintf(&packpath, "%s/objects/pack/pack-%s.pack",
469 ee61b6d3 2020-03-18 stsp repo_path, id_str) == -1) {
470 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
471 8e278d17 2020-03-18 stsp goto done;
472 8e278d17 2020-03-18 stsp }
473 93658fb9 2020-03-18 stsp
474 ee61b6d3 2020-03-18 stsp if (asprintf(&idxpath, "%s/objects/pack/pack-%s.idx",
475 ee61b6d3 2020-03-18 stsp repo_path, id_str) == -1) {
476 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
477 8e278d17 2020-03-18 stsp goto done;
478 8e278d17 2020-03-18 stsp }
479 afa77e03 2020-03-18 stsp
480 8e278d17 2020-03-18 stsp if (rename(tmppackpath, packpath) == -1) {
481 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmppackpath, packpath);
482 8e278d17 2020-03-18 stsp goto done;
483 8e278d17 2020-03-18 stsp }
484 8e278d17 2020-03-18 stsp if (rename(tmpidxpath, idxpath) == -1) {
485 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
486 8e278d17 2020-03-18 stsp goto done;
487 8e278d17 2020-03-18 stsp }
488 ee61b6d3 2020-03-18 stsp
489 ee61b6d3 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
490 ee61b6d3 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
491 ee61b6d3 2020-03-18 stsp struct got_reference *symref, *target_ref;
492 ee61b6d3 2020-03-18 stsp const char *refname = pe->path;
493 ee61b6d3 2020-03-18 stsp const char *target = pe->data;
494 ee61b6d3 2020-03-18 stsp
495 ee61b6d3 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
496 ee61b6d3 2020-03-18 stsp continue;
497 ee61b6d3 2020-03-18 stsp
498 ee61b6d3 2020-03-18 stsp err = got_ref_open(&target_ref, repo, target, 0);
499 ee61b6d3 2020-03-18 stsp if (err) {
500 ee61b6d3 2020-03-18 stsp if (err->code == GOT_ERR_NOT_REF)
501 ee61b6d3 2020-03-18 stsp continue;
502 ee61b6d3 2020-03-18 stsp goto done;
503 ee61b6d3 2020-03-18 stsp }
504 ee61b6d3 2020-03-18 stsp
505 ee61b6d3 2020-03-18 stsp err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
506 ee61b6d3 2020-03-18 stsp got_ref_close(target_ref);
507 ee61b6d3 2020-03-18 stsp if (err)
508 ee61b6d3 2020-03-18 stsp goto done;
509 ee61b6d3 2020-03-18 stsp
510 ee61b6d3 2020-03-18 stsp err = got_ref_write(symref, repo);
511 ee61b6d3 2020-03-18 stsp got_ref_close(symref);
512 ee61b6d3 2020-03-18 stsp if (err)
513 ee61b6d3 2020-03-18 stsp goto done;
514 ee61b6d3 2020-03-18 stsp break;
515 ee61b6d3 2020-03-18 stsp }
516 ee61b6d3 2020-03-18 stsp
517 8e278d17 2020-03-18 stsp done:
518 8e278d17 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
519 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
520 8e278d17 2020-03-18 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
521 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
522 8e278d17 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
523 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
524 8e278d17 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
525 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
526 afa77e03 2020-03-18 stsp free(tmppackpath);
527 afa77e03 2020-03-18 stsp free(tmpidxpath);
528 fe4e1501 2020-03-18 stsp free(idxpath);
529 afa77e03 2020-03-18 stsp free(packpath);
530 8f2d01a6 2020-03-18 stsp free(packhash);
531 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
532 abe0f35f 2020-03-18 stsp free((void *)pe->path);
533 abe0f35f 2020-03-18 stsp free(pe->data);
534 abe0f35f 2020-03-18 stsp }
535 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
536 fe4e1501 2020-03-18 stsp
537 8e278d17 2020-03-18 stsp return err;
538 93658fb9 2020-03-18 stsp }