Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 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/queue.h>
19 93658fb9 2020-03-18 stsp #include <sys/uio.h>
20 93658fb9 2020-03-18 stsp #include <sys/time.h>
21 93658fb9 2020-03-18 stsp #include <sys/stat.h>
22 93658fb9 2020-03-18 stsp #include <sys/syslimits.h>
23 93658fb9 2020-03-18 stsp
24 93658fb9 2020-03-18 stsp #include <stdint.h>
25 93658fb9 2020-03-18 stsp #include <errno.h>
26 93658fb9 2020-03-18 stsp #include <imsg.h>
27 93658fb9 2020-03-18 stsp #include <limits.h>
28 93658fb9 2020-03-18 stsp #include <signal.h>
29 93658fb9 2020-03-18 stsp #include <stdio.h>
30 93658fb9 2020-03-18 stsp #include <stdlib.h>
31 93658fb9 2020-03-18 stsp #include <string.h>
32 93658fb9 2020-03-18 stsp #include <ctype.h>
33 93658fb9 2020-03-18 stsp #include <sha1.h>
34 93658fb9 2020-03-18 stsp #include <fcntl.h>
35 93658fb9 2020-03-18 stsp #include <zlib.h>
36 93658fb9 2020-03-18 stsp #include <err.h>
37 93658fb9 2020-03-18 stsp
38 93658fb9 2020-03-18 stsp #include "got_error.h"
39 93658fb9 2020-03-18 stsp #include "got_object.h"
40 abe0f35f 2020-03-18 stsp #include "got_path.h"
41 8a29a085 2020-03-18 stsp #include "got_version.h"
42 93658fb9 2020-03-18 stsp
43 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
44 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
45 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
48 0872c0b0 2020-03-18 stsp #include "got_lib_pack.h"
49 93658fb9 2020-03-18 stsp
50 8a29a085 2020-03-18 stsp #ifndef nitems
51 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 8a29a085 2020-03-18 stsp #endif
53 8a29a085 2020-03-18 stsp
54 93658fb9 2020-03-18 stsp #define GOT_PKTMAX 65536
55 93658fb9 2020-03-18 stsp
56 93658fb9 2020-03-18 stsp struct got_object *indexed;
57 93658fb9 2020-03-18 stsp static int chattygit;
58 93658fb9 2020-03-18 stsp static char *fetchbranch;
59 93658fb9 2020-03-18 stsp static struct got_object_id zhash = {.sha1={0}};
60 93658fb9 2020-03-18 stsp
61 fe53745c 2020-03-18 stsp static const struct got_error *
62 fe53745c 2020-03-18 stsp readn(ssize_t *off, int fd, void *buf, size_t n)
63 93658fb9 2020-03-18 stsp {
64 fe53745c 2020-03-18 stsp ssize_t r;
65 93658fb9 2020-03-18 stsp
66 fe53745c 2020-03-18 stsp *off = 0;
67 fe53745c 2020-03-18 stsp while (*off != n) {
68 fe53745c 2020-03-18 stsp r = read(fd, buf + *off, n - *off);
69 fe53745c 2020-03-18 stsp if (r == -1)
70 fe53745c 2020-03-18 stsp return got_error_from_errno("read");
71 93658fb9 2020-03-18 stsp if (r == 0)
72 fe53745c 2020-03-18 stsp return NULL;
73 fe53745c 2020-03-18 stsp *off += r;
74 93658fb9 2020-03-18 stsp }
75 fe53745c 2020-03-18 stsp return NULL;
76 93658fb9 2020-03-18 stsp }
77 93658fb9 2020-03-18 stsp
78 38c670f1 2020-03-18 stsp static const struct got_error *
79 93658fb9 2020-03-18 stsp flushpkt(int fd)
80 93658fb9 2020-03-18 stsp {
81 38c670f1 2020-03-18 stsp ssize_t w;
82 38c670f1 2020-03-18 stsp
83 cf875574 2020-03-18 stsp if (chattygit)
84 93658fb9 2020-03-18 stsp fprintf(stderr, "writepkt: 0000\n");
85 38c670f1 2020-03-18 stsp
86 38c670f1 2020-03-18 stsp w = write(fd, "0000", 4);
87 38c670f1 2020-03-18 stsp if (w == -1)
88 38c670f1 2020-03-18 stsp return got_error_from_errno("write");
89 38c670f1 2020-03-18 stsp if (w != 4)
90 38c670f1 2020-03-18 stsp return got_error(GOT_ERR_IO);
91 38c670f1 2020-03-18 stsp return NULL;
92 93658fb9 2020-03-18 stsp }
93 93658fb9 2020-03-18 stsp
94 531c3985 2020-03-18 stsp /*
95 531c3985 2020-03-18 stsp * Packet header contains a 4-byte hexstring which specifies the length
96 531c3985 2020-03-18 stsp * of data which follows.
97 531c3985 2020-03-18 stsp */
98 fe53745c 2020-03-18 stsp static const struct got_error *
99 531c3985 2020-03-18 stsp read_pkthdr(int *datalen, int fd)
100 93658fb9 2020-03-18 stsp {
101 531c3985 2020-03-18 stsp static const struct got_error *err = NULL;
102 4dc8ee09 2020-03-18 stsp char lenstr[5];
103 4dc8ee09 2020-03-18 stsp long len;
104 93658fb9 2020-03-18 stsp char *e;
105 54d1a70f 2020-03-18 stsp int n, i;
106 fe53745c 2020-03-18 stsp ssize_t r;
107 93658fb9 2020-03-18 stsp
108 531c3985 2020-03-18 stsp *datalen = 0;
109 fe53745c 2020-03-18 stsp
110 4dc8ee09 2020-03-18 stsp err = readn(&r, fd, lenstr, 4);
111 fe53745c 2020-03-18 stsp if (err)
112 fe53745c 2020-03-18 stsp return err;
113 531c3985 2020-03-18 stsp if (r == 0) /* implicit "0000" */
114 531c3985 2020-03-18 stsp return NULL;
115 4dc8ee09 2020-03-18 stsp if (r != 4)
116 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
117 531c3985 2020-03-18 stsp "wrong packet header length");
118 fe53745c 2020-03-18 stsp
119 4dc8ee09 2020-03-18 stsp lenstr[4] = '\0';
120 54d1a70f 2020-03-18 stsp for (i = 0; i < 4; i++) {
121 54d1a70f 2020-03-18 stsp if (!isxdigit(lenstr[i]))
122 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
123 531c3985 2020-03-18 stsp "packet length not specified in hex");
124 54d1a70f 2020-03-18 stsp }
125 4dc8ee09 2020-03-18 stsp errno = 0;
126 4dc8ee09 2020-03-18 stsp len = strtol(lenstr, &e, 16);
127 4dc8ee09 2020-03-18 stsp if (lenstr[0] == '\0' || *e != '\0')
128 4dc8ee09 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKET);
129 4dc8ee09 2020-03-18 stsp if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
130 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
131 4dc8ee09 2020-03-18 stsp if (len > INT_MAX || len < INT_MIN)
132 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
133 4dc8ee09 2020-03-18 stsp n = len;
134 531c3985 2020-03-18 stsp if (n == 0)
135 fe53745c 2020-03-18 stsp return NULL;
136 4dc8ee09 2020-03-18 stsp if (n <= 4)
137 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
138 93658fb9 2020-03-18 stsp n -= 4;
139 531c3985 2020-03-18 stsp
140 531c3985 2020-03-18 stsp *datalen = n;
141 531c3985 2020-03-18 stsp return NULL;
142 531c3985 2020-03-18 stsp }
143 531c3985 2020-03-18 stsp
144 531c3985 2020-03-18 stsp static const struct got_error *
145 531c3985 2020-03-18 stsp readpkt(int *outlen, int fd, char *buf, int buflen)
146 531c3985 2020-03-18 stsp {
147 531c3985 2020-03-18 stsp const struct got_error *err = NULL;
148 531c3985 2020-03-18 stsp int datalen;
149 531c3985 2020-03-18 stsp ssize_t n;
150 531c3985 2020-03-18 stsp
151 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
152 531c3985 2020-03-18 stsp if (err)
153 531c3985 2020-03-18 stsp return err;
154 531c3985 2020-03-18 stsp
155 531c3985 2020-03-18 stsp if (datalen > buflen)
156 fe53745c 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
157 fe53745c 2020-03-18 stsp
158 531c3985 2020-03-18 stsp err = readn(&n, fd, buf, datalen);
159 fe53745c 2020-03-18 stsp if (err)
160 fe53745c 2020-03-18 stsp return err;
161 531c3985 2020-03-18 stsp if (n != datalen)
162 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
163 fe53745c 2020-03-18 stsp
164 fe53745c 2020-03-18 stsp *outlen = n;
165 fe53745c 2020-03-18 stsp return NULL;
166 93658fb9 2020-03-18 stsp }
167 93658fb9 2020-03-18 stsp
168 344e4747 2020-03-18 stsp static const struct got_error *
169 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
170 93658fb9 2020-03-18 stsp {
171 93658fb9 2020-03-18 stsp char len[5];
172 93658fb9 2020-03-18 stsp int i;
173 344e4747 2020-03-18 stsp ssize_t w;
174 93658fb9 2020-03-18 stsp
175 93658fb9 2020-03-18 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
176 344e4747 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
177 344e4747 2020-03-18 stsp w = write(fd, len, 4);
178 344e4747 2020-03-18 stsp if (w == -1)
179 344e4747 2020-03-18 stsp return got_error_from_errno("write");
180 344e4747 2020-03-18 stsp if (w != 4)
181 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
182 344e4747 2020-03-18 stsp w = write(fd, buf, nbuf);
183 344e4747 2020-03-18 stsp if (w == -1)
184 344e4747 2020-03-18 stsp return got_error_from_errno("write");
185 344e4747 2020-03-18 stsp if (w != nbuf)
186 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
187 cf875574 2020-03-18 stsp if (chattygit) {
188 93658fb9 2020-03-18 stsp fprintf(stderr, "writepkt: %s:\t", len);
189 93658fb9 2020-03-18 stsp fwrite(buf, 1, nbuf, stderr);
190 cf875574 2020-03-18 stsp for (i = 0; i < nbuf; i++) {
191 cf875574 2020-03-18 stsp if (isprint(buf[i]))
192 93658fb9 2020-03-18 stsp fputc(buf[i], stderr);
193 93658fb9 2020-03-18 stsp }
194 f78e0473 2020-03-18 stsp fputc('\n', stderr);
195 93658fb9 2020-03-18 stsp }
196 344e4747 2020-03-18 stsp return NULL;
197 93658fb9 2020-03-18 stsp }
198 93658fb9 2020-03-18 stsp
199 33501562 2020-03-18 stsp static const struct got_error *
200 33501562 2020-03-18 stsp match_remote_ref(struct got_pathlist_head *have_refs, struct got_object_id *id,
201 33501562 2020-03-18 stsp char *refname, char *id_str)
202 93658fb9 2020-03-18 stsp {
203 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
204 93658fb9 2020-03-18 stsp
205 33501562 2020-03-18 stsp memset(id, 0, sizeof(*id));
206 93658fb9 2020-03-18 stsp
207 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
208 33501562 2020-03-18 stsp if (strcmp(pe->path, refname) == 0) {
209 33501562 2020-03-18 stsp if (!got_parse_sha1_digest(id->sha1, id_str))
210 33501562 2020-03-18 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
211 33501562 2020-03-18 stsp break;
212 33501562 2020-03-18 stsp }
213 93658fb9 2020-03-18 stsp }
214 33501562 2020-03-18 stsp return NULL;
215 93658fb9 2020-03-18 stsp }
216 93658fb9 2020-03-18 stsp
217 93658fb9 2020-03-18 stsp static int
218 1ff21071 2020-03-18 stsp match_branch(char *br, char *pat)
219 93658fb9 2020-03-18 stsp {
220 93658fb9 2020-03-18 stsp char name[128];
221 93658fb9 2020-03-18 stsp
222 cf875574 2020-03-18 stsp if (strstr(pat, "refs/heads") == pat) {
223 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
224 93658fb9 2020-03-18 stsp return -1;
225 cf875574 2020-03-18 stsp } else if (strstr(pat, "heads")) {
226 cf875574 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/%s", pat)
227 cf875574 2020-03-18 stsp >= sizeof(name))
228 93658fb9 2020-03-18 stsp return -1;
229 93658fb9 2020-03-18 stsp } else {
230 cf875574 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/heads/%s", pat)
231 cf875574 2020-03-18 stsp >= sizeof(name))
232 93658fb9 2020-03-18 stsp return -1;
233 93658fb9 2020-03-18 stsp }
234 93658fb9 2020-03-18 stsp return strcmp(br, name) == 0;
235 93658fb9 2020-03-18 stsp }
236 93658fb9 2020-03-18 stsp
237 0d0a341c 2020-03-18 stsp static const struct got_error *
238 00cd0e0a 2020-03-18 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
239 93658fb9 2020-03-18 stsp {
240 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
241 93658fb9 2020-03-18 stsp char *p;
242 0d0a341c 2020-03-18 stsp size_t i, n = 0;
243 93658fb9 2020-03-18 stsp
244 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
245 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
246 0d0a341c 2020-03-18 stsp
247 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
248 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
249 93658fb9 2020-03-18 stsp line++;
250 0d0a341c 2020-03-18 stsp n++;
251 0d0a341c 2020-03-18 stsp }
252 93658fb9 2020-03-18 stsp p = line;
253 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
254 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
255 93658fb9 2020-03-18 stsp line++;
256 0d0a341c 2020-03-18 stsp n++;
257 0d0a341c 2020-03-18 stsp }
258 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
259 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
260 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
261 0d0a341c 2020-03-18 stsp goto done;
262 0d0a341c 2020-03-18 stsp }
263 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
264 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
265 0d0a341c 2020-03-18 stsp line++;
266 0d0a341c 2020-03-18 stsp n++;
267 0d0a341c 2020-03-18 stsp }
268 93658fb9 2020-03-18 stsp }
269 0d0a341c 2020-03-18 stsp if (i <= 2)
270 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
271 0d0a341c 2020-03-18 stsp done:
272 0d0a341c 2020-03-18 stsp if (err) {
273 0d0a341c 2020-03-18 stsp int j;
274 0d0a341c 2020-03-18 stsp for (j = 0; j < i; j++)
275 0d0a341c 2020-03-18 stsp free(tokens[j]);
276 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
277 0d0a341c 2020-03-18 stsp }
278 0d0a341c 2020-03-18 stsp return err;
279 8a29a085 2020-03-18 stsp }
280 00cd0e0a 2020-03-18 stsp
281 00cd0e0a 2020-03-18 stsp static const struct got_error *
282 00cd0e0a 2020-03-18 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
283 00cd0e0a 2020-03-18 stsp char *line, int len)
284 00cd0e0a 2020-03-18 stsp {
285 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
286 00cd0e0a 2020-03-18 stsp char *tokens[3];
287 8a29a085 2020-03-18 stsp
288 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
289 00cd0e0a 2020-03-18 stsp if (err)
290 00cd0e0a 2020-03-18 stsp return err;
291 00cd0e0a 2020-03-18 stsp
292 00cd0e0a 2020-03-18 stsp if (tokens[0])
293 00cd0e0a 2020-03-18 stsp *id_str = tokens[0];
294 00cd0e0a 2020-03-18 stsp if (tokens[1])
295 00cd0e0a 2020-03-18 stsp *refname = tokens[1];
296 00cd0e0a 2020-03-18 stsp if (tokens[2])
297 00cd0e0a 2020-03-18 stsp *server_capabilities = tokens[2];
298 00cd0e0a 2020-03-18 stsp
299 00cd0e0a 2020-03-18 stsp return NULL;
300 00cd0e0a 2020-03-18 stsp }
301 00cd0e0a 2020-03-18 stsp
302 531c3985 2020-03-18 stsp #define GOT_CAPA_AGENT "agent"
303 531c3985 2020-03-18 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
304 531c3985 2020-03-18 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
305 531c3985 2020-03-18 stsp
306 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
307 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
308 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_ERROR_INFO 3
309 531c3985 2020-03-18 stsp
310 531c3985 2020-03-18 stsp
311 8a29a085 2020-03-18 stsp struct got_capability {
312 8a29a085 2020-03-18 stsp const char *key;
313 8a29a085 2020-03-18 stsp const char *value;
314 8a29a085 2020-03-18 stsp };
315 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
316 531c3985 2020-03-18 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
317 531c3985 2020-03-18 stsp { GOT_CAPA_OFS_DELTA, NULL },
318 531c3985 2020-03-18 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
319 8a29a085 2020-03-18 stsp };
320 8a29a085 2020-03-18 stsp
321 8a29a085 2020-03-18 stsp static const struct got_error *
322 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
323 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
324 8a29a085 2020-03-18 stsp {
325 8a29a085 2020-03-18 stsp char *equalsign;
326 8a29a085 2020-03-18 stsp char *s;
327 8a29a085 2020-03-18 stsp
328 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
329 8a29a085 2020-03-18 stsp if (equalsign) {
330 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
331 8a29a085 2020-03-18 stsp return NULL;
332 8a29a085 2020-03-18 stsp } else {
333 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
334 8a29a085 2020-03-18 stsp return NULL;
335 8a29a085 2020-03-18 stsp }
336 8a29a085 2020-03-18 stsp
337 8a29a085 2020-03-18 stsp if (asprintf(&s, "%s%s%s%s%s",
338 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
339 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? " " : "",
340 8a29a085 2020-03-18 stsp mycapa->key,
341 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
342 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
343 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
344 8a29a085 2020-03-18 stsp
345 8a29a085 2020-03-18 stsp free(*my_capabilities);
346 8a29a085 2020-03-18 stsp *my_capabilities = s;
347 8a29a085 2020-03-18 stsp return NULL;
348 93658fb9 2020-03-18 stsp }
349 93658fb9 2020-03-18 stsp
350 9ff10419 2020-03-18 stsp static const struct got_error *
351 01538ce4 2020-03-18 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
352 8a29a085 2020-03-18 stsp {
353 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
354 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
355 abe0f35f 2020-03-18 stsp
356 abe0f35f 2020-03-18 stsp /* Need at least "A:B" */
357 abe0f35f 2020-03-18 stsp if (strlen(capa) < 3)
358 abe0f35f 2020-03-18 stsp return NULL;
359 abe0f35f 2020-03-18 stsp
360 abe0f35f 2020-03-18 stsp colon = strchr(capa, ':');
361 abe0f35f 2020-03-18 stsp if (colon == NULL)
362 abe0f35f 2020-03-18 stsp return NULL;
363 abe0f35f 2020-03-18 stsp
364 abe0f35f 2020-03-18 stsp *colon = '\0';
365 abe0f35f 2020-03-18 stsp name = strdup(capa);
366 abe0f35f 2020-03-18 stsp if (name == NULL)
367 abe0f35f 2020-03-18 stsp return got_error_from_errno("strdup");
368 abe0f35f 2020-03-18 stsp
369 abe0f35f 2020-03-18 stsp target = strdup(colon + 1);
370 abe0f35f 2020-03-18 stsp if (target == NULL) {
371 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strdup");
372 abe0f35f 2020-03-18 stsp goto done;
373 abe0f35f 2020-03-18 stsp }
374 abe0f35f 2020-03-18 stsp
375 abe0f35f 2020-03-18 stsp /* We can't validate the ref itself here. The main process will. */
376 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
377 abe0f35f 2020-03-18 stsp done:
378 abe0f35f 2020-03-18 stsp if (err) {
379 abe0f35f 2020-03-18 stsp free(name);
380 abe0f35f 2020-03-18 stsp free(target);
381 abe0f35f 2020-03-18 stsp }
382 abe0f35f 2020-03-18 stsp return err;
383 abe0f35f 2020-03-18 stsp }
384 abe0f35f 2020-03-18 stsp
385 abe0f35f 2020-03-18 stsp static const struct got_error *
386 abe0f35f 2020-03-18 stsp match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
387 abe0f35f 2020-03-18 stsp char *server_capabilities)
388 abe0f35f 2020-03-18 stsp {
389 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
390 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
391 8a29a085 2020-03-18 stsp int i;
392 8a29a085 2020-03-18 stsp
393 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
394 8a29a085 2020-03-18 stsp do {
395 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
396 8a29a085 2020-03-18 stsp if (capa == NULL)
397 8a29a085 2020-03-18 stsp return NULL;
398 8a29a085 2020-03-18 stsp
399 abe0f35f 2020-03-18 stsp equalsign = strchr(capa, '=');
400 abe0f35f 2020-03-18 stsp if (equalsign != NULL &&
401 abe0f35f 2020-03-18 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
402 abe0f35f 2020-03-18 stsp err = add_symref(symrefs, equalsign + 1);
403 abe0f35f 2020-03-18 stsp if (err)
404 abe0f35f 2020-03-18 stsp break;
405 abe0f35f 2020-03-18 stsp continue;
406 abe0f35f 2020-03-18 stsp }
407 abe0f35f 2020-03-18 stsp
408 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
409 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
410 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
411 8a29a085 2020-03-18 stsp if (err)
412 8a29a085 2020-03-18 stsp break;
413 8a29a085 2020-03-18 stsp }
414 8a29a085 2020-03-18 stsp } while (capa);
415 8a29a085 2020-03-18 stsp
416 8a29a085 2020-03-18 stsp return err;
417 531c3985 2020-03-18 stsp }
418 531c3985 2020-03-18 stsp
419 531c3985 2020-03-18 stsp static const struct got_error *
420 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
421 531c3985 2020-03-18 stsp {
422 531c3985 2020-03-18 stsp int i;
423 531c3985 2020-03-18 stsp
424 531c3985 2020-03-18 stsp if (len == 0)
425 531c3985 2020-03-18 stsp return NULL;
426 531c3985 2020-03-18 stsp
427 531c3985 2020-03-18 stsp /*
428 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
429 531c3985 2020-03-18 stsp * Server may send up to 64k.
430 531c3985 2020-03-18 stsp */
431 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
432 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
433 531c3985 2020-03-18 stsp
434 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
435 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
436 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
437 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
438 531c3985 2020-03-18 stsp continue;
439 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
440 531c3985 2020-03-18 stsp "non-printable progress message received from server");
441 531c3985 2020-03-18 stsp }
442 531c3985 2020-03-18 stsp
443 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
444 8a29a085 2020-03-18 stsp }
445 abe0f35f 2020-03-18 stsp
446 8a29a085 2020-03-18 stsp static const struct got_error *
447 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
448 531c3985 2020-03-18 stsp {
449 531c3985 2020-03-18 stsp static char msg[1024];
450 531c3985 2020-03-18 stsp int i;
451 531c3985 2020-03-18 stsp
452 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
453 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
454 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
455 531c3985 2020-03-18 stsp "non-printable error message received from server");
456 531c3985 2020-03-18 stsp msg[i] = buf[i];
457 531c3985 2020-03-18 stsp }
458 531c3985 2020-03-18 stsp msg[i] = '\0';
459 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
460 531c3985 2020-03-18 stsp }
461 531c3985 2020-03-18 stsp
462 531c3985 2020-03-18 stsp static const struct got_error *
463 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
464 33501562 2020-03-18 stsp struct got_pathlist_head *have_refs, struct imsgbuf *ibuf)
465 93658fb9 2020-03-18 stsp {
466 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
467 00cd0e0a 2020-03-18 stsp char buf[GOT_PKTMAX];
468 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
469 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
470 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
471 93658fb9 2020-03-18 stsp int i, n, req;
472 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
473 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
474 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
475 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
476 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
477 531c3985 2020-03-18 stsp int have_sidebands = 0;
478 93658fb9 2020-03-18 stsp
479 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
480 abe0f35f 2020-03-18 stsp
481 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
482 9ff10419 2020-03-18 stsp if (have == NULL)
483 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
484 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
485 9ff10419 2020-03-18 stsp if (want == NULL) {
486 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
487 9ff10419 2020-03-18 stsp goto done;
488 9ff10419 2020-03-18 stsp }
489 9ff10419 2020-03-18 stsp if (chattygit)
490 9ff10419 2020-03-18 stsp fprintf(stderr, "starting fetch\n");
491 9ff10419 2020-03-18 stsp while (1) {
492 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
493 fe53745c 2020-03-18 stsp if (err)
494 9ff10419 2020-03-18 stsp goto done;
495 9ff10419 2020-03-18 stsp if (n == 0)
496 93658fb9 2020-03-18 stsp break;
497 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
498 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
499 9ff10419 2020-03-18 stsp goto done;
500 9ff10419 2020-03-18 stsp }
501 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
502 00cd0e0a 2020-03-18 stsp buf, n);
503 0d0a341c 2020-03-18 stsp if (err)
504 9ff10419 2020-03-18 stsp goto done;
505 00cd0e0a 2020-03-18 stsp if (chattygit && server_capabilities[0] != '\0')
506 00cd0e0a 2020-03-18 stsp fprintf(stderr, "server capabilities: %s\n",
507 00cd0e0a 2020-03-18 stsp server_capabilities);
508 8a29a085 2020-03-18 stsp if (is_firstpkt) {
509 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
510 00cd0e0a 2020-03-18 stsp server_capabilities);
511 8a29a085 2020-03-18 stsp if (err)
512 8a29a085 2020-03-18 stsp goto done;
513 8a29a085 2020-03-18 stsp if (chattygit && my_capabilities)
514 8a29a085 2020-03-18 stsp fprintf(stderr, "my matched capabilities: %s\n",
515 8a29a085 2020-03-18 stsp my_capabilities);
516 abe0f35f 2020-03-18 stsp err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
517 abe0f35f 2020-03-18 stsp if (err)
518 abe0f35f 2020-03-18 stsp goto done;
519 8a29a085 2020-03-18 stsp }
520 8a29a085 2020-03-18 stsp is_firstpkt = 0;
521 00cd0e0a 2020-03-18 stsp if (strstr(refname, "^{}"))
522 93658fb9 2020-03-18 stsp continue;
523 00cd0e0a 2020-03-18 stsp if (fetchbranch && !match_branch(refname, fetchbranch))
524 93658fb9 2020-03-18 stsp continue;
525 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
526 93658fb9 2020-03-18 stsp refsz *= 2;
527 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
528 9ff10419 2020-03-18 stsp if (have == NULL) {
529 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
530 9ff10419 2020-03-18 stsp goto done;
531 9ff10419 2020-03-18 stsp }
532 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
533 9ff10419 2020-03-18 stsp if (want == NULL) {
534 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
535 9ff10419 2020-03-18 stsp goto done;
536 9ff10419 2020-03-18 stsp }
537 93658fb9 2020-03-18 stsp }
538 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
539 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
540 9ff10419 2020-03-18 stsp goto done;
541 9ff10419 2020-03-18 stsp }
542 9ff10419 2020-03-18 stsp
543 00cd0e0a 2020-03-18 stsp err = match_remote_ref(have_refs, &have[nref], id_str, refname);
544 33501562 2020-03-18 stsp if (err)
545 33501562 2020-03-18 stsp goto done;
546 33501562 2020-03-18 stsp
547 ea7396b9 2020-03-18 stsp err = got_privsep_send_fetch_ref(ibuf, &want[nref],
548 00cd0e0a 2020-03-18 stsp refname);
549 8f2d01a6 2020-03-18 stsp if (err)
550 8f2d01a6 2020-03-18 stsp goto done;
551 93658fb9 2020-03-18 stsp if (chattygit)
552 00cd0e0a 2020-03-18 stsp fprintf(stderr, "remote %s\n", refname);
553 93658fb9 2020-03-18 stsp nref++;
554 93658fb9 2020-03-18 stsp }
555 93658fb9 2020-03-18 stsp
556 93658fb9 2020-03-18 stsp req = 0;
557 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
558 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
559 93658fb9 2020-03-18 stsp continue;
560 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
561 13ce8c93 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "want %s%s%s\n", hashstr,
562 13ce8c93 2020-03-18 stsp i == 0 && my_capabilities ? " " : "",
563 8a29a085 2020-03-18 stsp i == 0 && my_capabilities ? my_capabilities : "");
564 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
565 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
566 9ff10419 2020-03-18 stsp goto done;
567 9ff10419 2020-03-18 stsp }
568 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
569 344e4747 2020-03-18 stsp if (err)
570 9ff10419 2020-03-18 stsp goto done;
571 93658fb9 2020-03-18 stsp req = 1;
572 93658fb9 2020-03-18 stsp }
573 38c670f1 2020-03-18 stsp err = flushpkt(fd);
574 38c670f1 2020-03-18 stsp if (err)
575 38c670f1 2020-03-18 stsp goto done;
576 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
577 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
578 93658fb9 2020-03-18 stsp continue;
579 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
580 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
581 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
582 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
583 9ff10419 2020-03-18 stsp goto done;
584 9ff10419 2020-03-18 stsp }
585 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n + 1);
586 344e4747 2020-03-18 stsp if (err)
587 9ff10419 2020-03-18 stsp goto done;
588 93658fb9 2020-03-18 stsp }
589 cf875574 2020-03-18 stsp if (!req) {
590 3b9fb585 2020-03-18 stsp if (chattygit)
591 3b9fb585 2020-03-18 stsp fprintf(stderr, "up to date\n");
592 38c670f1 2020-03-18 stsp err = flushpkt(fd);
593 38c670f1 2020-03-18 stsp if (err)
594 38c670f1 2020-03-18 stsp goto done;
595 93658fb9 2020-03-18 stsp }
596 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
597 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
598 344e4747 2020-03-18 stsp if (err)
599 9ff10419 2020-03-18 stsp goto done;
600 9ff10419 2020-03-18 stsp if (!req)
601 93658fb9 2020-03-18 stsp return 0;
602 93658fb9 2020-03-18 stsp
603 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
604 fe53745c 2020-03-18 stsp if (err)
605 9ff10419 2020-03-18 stsp goto done;
606 04c53c18 2020-03-18 stsp /*
607 04c53c18 2020-03-18 stsp * For now, we only support a full clone, in which case the server
608 04c53c18 2020-03-18 stsp * will now send a "NAK" (meaning no common objects were found).
609 04c53c18 2020-03-18 stsp */
610 04c53c18 2020-03-18 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
611 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
612 531c3985 2020-03-18 stsp "unexpected message from server");
613 04c53c18 2020-03-18 stsp goto done;
614 04c53c18 2020-03-18 stsp }
615 93658fb9 2020-03-18 stsp
616 8f2d01a6 2020-03-18 stsp if (chattygit)
617 8f2d01a6 2020-03-18 stsp fprintf(stderr, "fetching...\n");
618 531c3985 2020-03-18 stsp
619 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
620 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
621 531c3985 2020-03-18 stsp have_sidebands = 1;
622 531c3985 2020-03-18 stsp
623 9ff10419 2020-03-18 stsp while (1) {
624 531c3985 2020-03-18 stsp ssize_t r = 0, w;
625 531c3985 2020-03-18 stsp int datalen = -1;
626 531c3985 2020-03-18 stsp
627 531c3985 2020-03-18 stsp if (have_sidebands) {
628 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
629 531c3985 2020-03-18 stsp if (err)
630 531c3985 2020-03-18 stsp goto done;
631 531c3985 2020-03-18 stsp if (datalen <= 0)
632 531c3985 2020-03-18 stsp break;
633 531c3985 2020-03-18 stsp
634 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
635 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
636 531c3985 2020-03-18 stsp if (r == -1) {
637 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
638 531c3985 2020-03-18 stsp goto done;
639 531c3985 2020-03-18 stsp }
640 531c3985 2020-03-18 stsp if (r != 1) {
641 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
642 531c3985 2020-03-18 stsp "short packet");
643 531c3985 2020-03-18 stsp goto done;
644 531c3985 2020-03-18 stsp }
645 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
646 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
647 531c3985 2020-03-18 stsp "bad packet length");
648 531c3985 2020-03-18 stsp goto done;
649 531c3985 2020-03-18 stsp }
650 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
651 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
652 531c3985 2020-03-18 stsp /* Read packfile data. */
653 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
654 531c3985 2020-03-18 stsp if (err)
655 531c3985 2020-03-18 stsp goto done;
656 531c3985 2020-03-18 stsp if (r != datalen) {
657 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
658 531c3985 2020-03-18 stsp "packet too short");
659 531c3985 2020-03-18 stsp goto done;
660 531c3985 2020-03-18 stsp }
661 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
662 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
663 531c3985 2020-03-18 stsp if (err)
664 531c3985 2020-03-18 stsp goto done;
665 531c3985 2020-03-18 stsp if (r != datalen) {
666 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
667 531c3985 2020-03-18 stsp "packet too short");
668 531c3985 2020-03-18 stsp goto done;
669 531c3985 2020-03-18 stsp }
670 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
671 531c3985 2020-03-18 stsp if (err)
672 531c3985 2020-03-18 stsp goto done;
673 531c3985 2020-03-18 stsp continue;
674 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
675 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
676 531c3985 2020-03-18 stsp if (err)
677 531c3985 2020-03-18 stsp goto done;
678 531c3985 2020-03-18 stsp if (r != datalen) {
679 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
680 531c3985 2020-03-18 stsp "packet too short");
681 531c3985 2020-03-18 stsp goto done;
682 531c3985 2020-03-18 stsp }
683 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
684 531c3985 2020-03-18 stsp goto done;
685 531c3985 2020-03-18 stsp } else {
686 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
687 531c3985 2020-03-18 stsp "unknown side-band received from server");
688 531c3985 2020-03-18 stsp goto done;
689 531c3985 2020-03-18 stsp }
690 531c3985 2020-03-18 stsp } else {
691 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
692 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
693 531c3985 2020-03-18 stsp if (err)
694 531c3985 2020-03-18 stsp goto done;
695 531c3985 2020-03-18 stsp if (r <= 0)
696 531c3985 2020-03-18 stsp break;
697 531c3985 2020-03-18 stsp }
698 531c3985 2020-03-18 stsp
699 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
700 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
701 9ff10419 2020-03-18 stsp if (w == -1) {
702 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
703 9ff10419 2020-03-18 stsp goto done;
704 9ff10419 2020-03-18 stsp }
705 fe53745c 2020-03-18 stsp if (w != r) {
706 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
707 9ff10419 2020-03-18 stsp goto done;
708 9ff10419 2020-03-18 stsp }
709 531c3985 2020-03-18 stsp packsz += w;
710 5672d305 2020-03-18 stsp
711 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
712 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
713 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf,
714 5672d305 2020-03-18 stsp packsz);
715 5672d305 2020-03-18 stsp if (err)
716 5672d305 2020-03-18 stsp goto done;
717 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
718 5672d305 2020-03-18 stsp }
719 93658fb9 2020-03-18 stsp }
720 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf, packsz);
721 5672d305 2020-03-18 stsp if (err)
722 5672d305 2020-03-18 stsp goto done;
723 9ff10419 2020-03-18 stsp done:
724 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
725 abe0f35f 2020-03-18 stsp free((void *)pe->path);
726 abe0f35f 2020-03-18 stsp free(pe->data);
727 abe0f35f 2020-03-18 stsp }
728 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
729 9ff10419 2020-03-18 stsp free(have);
730 9ff10419 2020-03-18 stsp free(want);
731 00cd0e0a 2020-03-18 stsp free(id_str);
732 00cd0e0a 2020-03-18 stsp free(refname);
733 00cd0e0a 2020-03-18 stsp free(server_capabilities);
734 8f2d01a6 2020-03-18 stsp return err;
735 93658fb9 2020-03-18 stsp }
736 93658fb9 2020-03-18 stsp
737 93658fb9 2020-03-18 stsp
738 93658fb9 2020-03-18 stsp int
739 93658fb9 2020-03-18 stsp main(int argc, char **argv)
740 93658fb9 2020-03-18 stsp {
741 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
742 9ff10419 2020-03-18 stsp int fetchfd, packfd = -1;
743 93658fb9 2020-03-18 stsp struct got_object_id packid;
744 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
745 93658fb9 2020-03-18 stsp struct imsg imsg;
746 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
747 33501562 2020-03-18 stsp struct got_imsg_fetch_have_refs *fetch_have_refs = NULL;
748 33501562 2020-03-18 stsp size_t datalen;
749 33501562 2020-03-18 stsp
750 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
751 93658fb9 2020-03-18 stsp
752 cf875574 2020-03-18 stsp if (getenv("GOT_DEBUG") != NULL) {
753 93658fb9 2020-03-18 stsp fprintf(stderr, "fetch-pack being chatty!\n");
754 93658fb9 2020-03-18 stsp chattygit = 1;
755 93658fb9 2020-03-18 stsp }
756 93658fb9 2020-03-18 stsp
757 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
758 ffb5f621 2020-03-18 stsp #ifndef PROFILE
759 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
760 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
761 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
762 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
763 ffb5f621 2020-03-18 stsp return 1;
764 ffb5f621 2020-03-18 stsp }
765 ffb5f621 2020-03-18 stsp #endif
766 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
767 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
768 93658fb9 2020-03-18 stsp err = NULL;
769 93658fb9 2020-03-18 stsp goto done;
770 93658fb9 2020-03-18 stsp }
771 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
772 93658fb9 2020-03-18 stsp goto done;
773 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
774 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
775 93658fb9 2020-03-18 stsp goto done;
776 93658fb9 2020-03-18 stsp }
777 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
778 33501562 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_fetch_have_refs)) {
779 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
780 93658fb9 2020-03-18 stsp goto done;
781 93658fb9 2020-03-18 stsp }
782 33501562 2020-03-18 stsp fetch_have_refs = (struct got_imsg_fetch_have_refs *)imsg.data;
783 33501562 2020-03-18 stsp if (datalen != sizeof(struct got_imsg_fetch_have_refs) +
784 33501562 2020-03-18 stsp sizeof(struct got_imsg_fetch_have_ref) *
785 33501562 2020-03-18 stsp fetch_have_refs->n_have_refs) {
786 33501562 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
787 33501562 2020-03-18 stsp goto done;
788 33501562 2020-03-18 stsp }
789 33501562 2020-03-18 stsp if (fetch_have_refs->n_have_refs != 0) {
790 33501562 2020-03-18 stsp /* TODO: Incremental fetch support */
791 33501562 2020-03-18 stsp err = got_error(GOT_ERR_NOT_IMPL);
792 33501562 2020-03-18 stsp goto done;
793 33501562 2020-03-18 stsp }
794 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
795 93658fb9 2020-03-18 stsp
796 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
797 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
798 93658fb9 2020-03-18 stsp err = NULL;
799 93658fb9 2020-03-18 stsp goto done;
800 93658fb9 2020-03-18 stsp }
801 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
802 93658fb9 2020-03-18 stsp goto done;
803 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
804 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
805 93658fb9 2020-03-18 stsp goto done;
806 93658fb9 2020-03-18 stsp }
807 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
808 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
809 93658fb9 2020-03-18 stsp goto done;
810 93658fb9 2020-03-18 stsp }
811 93658fb9 2020-03-18 stsp packfd = imsg.fd;
812 93658fb9 2020-03-18 stsp
813 33501562 2020-03-18 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs, &ibuf);
814 93658fb9 2020-03-18 stsp done:
815 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
816 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
817 9ff10419 2020-03-18 stsp if (err != NULL)
818 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
819 93658fb9 2020-03-18 stsp else
820 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
821 cf875574 2020-03-18 stsp if (err != NULL) {
822 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
823 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
824 93658fb9 2020-03-18 stsp }
825 93658fb9 2020-03-18 stsp
826 93658fb9 2020-03-18 stsp exit(0);
827 93658fb9 2020-03-18 stsp }