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 93658fb9 2020-03-18 stsp
41 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
42 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
43 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
44 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
45 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
47 93658fb9 2020-03-18 stsp
48 93658fb9 2020-03-18 stsp #define GOT_PKTMAX 65536
49 93658fb9 2020-03-18 stsp
50 93658fb9 2020-03-18 stsp struct got_object *indexed;
51 93658fb9 2020-03-18 stsp static int chattygit;
52 93658fb9 2020-03-18 stsp static char *fetchbranch;
53 93658fb9 2020-03-18 stsp static char *upstream = "origin";
54 93658fb9 2020-03-18 stsp static struct got_object_id zhash = {.sha1={0}};
55 93658fb9 2020-03-18 stsp
56 93658fb9 2020-03-18 stsp static char*
57 93658fb9 2020-03-18 stsp strip(char *ref)
58 93658fb9 2020-03-18 stsp {
59 93658fb9 2020-03-18 stsp return ref;
60 93658fb9 2020-03-18 stsp }
61 93658fb9 2020-03-18 stsp
62 93658fb9 2020-03-18 stsp int
63 93658fb9 2020-03-18 stsp readn(int fd, void *buf, size_t n)
64 93658fb9 2020-03-18 stsp {
65 93658fb9 2020-03-18 stsp ssize_t r, off;
66 93658fb9 2020-03-18 stsp
67 93658fb9 2020-03-18 stsp off = 0;
68 93658fb9 2020-03-18 stsp while (off != n) {
69 93658fb9 2020-03-18 stsp r = read(fd, buf + off, n - off);
70 93658fb9 2020-03-18 stsp if (r < 0)
71 93658fb9 2020-03-18 stsp return -1;
72 93658fb9 2020-03-18 stsp if (r == 0)
73 93658fb9 2020-03-18 stsp return off;
74 93658fb9 2020-03-18 stsp off += r;
75 93658fb9 2020-03-18 stsp }
76 93658fb9 2020-03-18 stsp return off;
77 93658fb9 2020-03-18 stsp }
78 93658fb9 2020-03-18 stsp
79 93658fb9 2020-03-18 stsp int
80 93658fb9 2020-03-18 stsp flushpkt(int fd)
81 93658fb9 2020-03-18 stsp {
82 93658fb9 2020-03-18 stsp if(chattygit)
83 93658fb9 2020-03-18 stsp fprintf(stderr, "writepkt: 0000\n");
84 93658fb9 2020-03-18 stsp return write(fd, "0000", 4);
85 93658fb9 2020-03-18 stsp }
86 93658fb9 2020-03-18 stsp
87 93658fb9 2020-03-18 stsp
88 93658fb9 2020-03-18 stsp int
89 93658fb9 2020-03-18 stsp readpkt(int fd, char *buf, int nbuf)
90 93658fb9 2020-03-18 stsp {
91 93658fb9 2020-03-18 stsp char len[5];
92 93658fb9 2020-03-18 stsp char *e;
93 93658fb9 2020-03-18 stsp int n, r;
94 93658fb9 2020-03-18 stsp
95 93658fb9 2020-03-18 stsp if(readn(fd, len, 4) == -1){
96 93658fb9 2020-03-18 stsp return -1;
97 93658fb9 2020-03-18 stsp }
98 93658fb9 2020-03-18 stsp len[4] = 0;
99 93658fb9 2020-03-18 stsp n = strtol(len, &e, 16);
100 93658fb9 2020-03-18 stsp if(n == 0){
101 93658fb9 2020-03-18 stsp if(chattygit)
102 93658fb9 2020-03-18 stsp fprintf(stderr, "readpkt: 0000\n");
103 93658fb9 2020-03-18 stsp return 0;
104 93658fb9 2020-03-18 stsp }
105 93658fb9 2020-03-18 stsp if(e != len + 4 || n <= 4)
106 93658fb9 2020-03-18 stsp err(1, "invalid packet line length");
107 93658fb9 2020-03-18 stsp n -= 4;
108 93658fb9 2020-03-18 stsp if(n >= nbuf)
109 93658fb9 2020-03-18 stsp err(1, "buffer too small");
110 93658fb9 2020-03-18 stsp if((r = readn(fd, buf, n)) != n)
111 93658fb9 2020-03-18 stsp return -1;
112 93658fb9 2020-03-18 stsp buf[n] = 0;
113 93658fb9 2020-03-18 stsp if(chattygit)
114 93658fb9 2020-03-18 stsp fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
115 93658fb9 2020-03-18 stsp return n;
116 93658fb9 2020-03-18 stsp }
117 93658fb9 2020-03-18 stsp
118 93658fb9 2020-03-18 stsp int
119 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
120 93658fb9 2020-03-18 stsp {
121 93658fb9 2020-03-18 stsp char len[5];
122 93658fb9 2020-03-18 stsp int i;
123 93658fb9 2020-03-18 stsp
124 93658fb9 2020-03-18 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
125 93658fb9 2020-03-18 stsp return -1;
126 93658fb9 2020-03-18 stsp if(write(fd, len, 4) != 4)
127 93658fb9 2020-03-18 stsp return -1;
128 93658fb9 2020-03-18 stsp if(write(fd, buf, nbuf) != nbuf)
129 93658fb9 2020-03-18 stsp return -1;
130 93658fb9 2020-03-18 stsp if(chattygit){
131 93658fb9 2020-03-18 stsp fprintf(stderr, "writepkt: %s:\t", len);
132 93658fb9 2020-03-18 stsp fwrite(buf, 1, nbuf, stderr);
133 93658fb9 2020-03-18 stsp for(i = 0; i < nbuf; i++){
134 93658fb9 2020-03-18 stsp if(isprint(buf[i]))
135 93658fb9 2020-03-18 stsp fputc(buf[i], stderr);
136 93658fb9 2020-03-18 stsp }
137 93658fb9 2020-03-18 stsp }
138 93658fb9 2020-03-18 stsp return 0;
139 93658fb9 2020-03-18 stsp }
140 93658fb9 2020-03-18 stsp
141 93658fb9 2020-03-18 stsp
142 93658fb9 2020-03-18 stsp int
143 93658fb9 2020-03-18 stsp got_resolve_remote_ref(struct got_object_id *id, char *ref)
144 93658fb9 2020-03-18 stsp {
145 93658fb9 2020-03-18 stsp char buf[128], *s;
146 93658fb9 2020-03-18 stsp int r, f;
147 93658fb9 2020-03-18 stsp
148 93658fb9 2020-03-18 stsp ref = strip(ref);
149 93658fb9 2020-03-18 stsp if(!got_parse_sha1_digest(id->sha1, ref))
150 93658fb9 2020-03-18 stsp return 0;
151 93658fb9 2020-03-18 stsp
152 93658fb9 2020-03-18 stsp /* Slightly special handling: translate remote refs to local ones. */
153 93658fb9 2020-03-18 stsp if (strcmp(ref, "HEAD") == 0) {
154 93658fb9 2020-03-18 stsp if(snprintf(buf, sizeof(buf), ".git/HEAD") >= sizeof(buf))
155 93658fb9 2020-03-18 stsp return -1;
156 93658fb9 2020-03-18 stsp } else if(strstr(ref, "refs/heads") == ref) {
157 93658fb9 2020-03-18 stsp ref += strlen("refs/heads");
158 93658fb9 2020-03-18 stsp if(snprintf(buf, sizeof(buf),
159 93658fb9 2020-03-18 stsp ".git/refs/remotes/%s/%s", upstream, ref) >= sizeof(buf))
160 93658fb9 2020-03-18 stsp return -1;
161 93658fb9 2020-03-18 stsp } else if(strstr(ref, "refs/tags") == ref) {
162 93658fb9 2020-03-18 stsp ref += strlen("refs/tags");
163 93658fb9 2020-03-18 stsp if(snprintf(buf, sizeof(buf),
164 93658fb9 2020-03-18 stsp ".git/refs/tags/%s/%s", upstream, ref) >= sizeof(buf))
165 93658fb9 2020-03-18 stsp return -1;
166 93658fb9 2020-03-18 stsp } else {
167 93658fb9 2020-03-18 stsp return -1;
168 93658fb9 2020-03-18 stsp }
169 93658fb9 2020-03-18 stsp
170 93658fb9 2020-03-18 stsp r = -1;
171 93658fb9 2020-03-18 stsp s = strip(buf);
172 93658fb9 2020-03-18 stsp if((f = open(s, O_RDONLY)) == -1)
173 93658fb9 2020-03-18 stsp goto err;
174 93658fb9 2020-03-18 stsp if(readn(f, buf, sizeof(buf)) < 40)
175 93658fb9 2020-03-18 stsp goto err;
176 93658fb9 2020-03-18 stsp if(!got_parse_sha1_digest(id->sha1, buf))
177 93658fb9 2020-03-18 stsp goto err;
178 93658fb9 2020-03-18 stsp err:
179 93658fb9 2020-03-18 stsp close(f);
180 93658fb9 2020-03-18 stsp if(r == -1 && strstr(buf, "ref:") == buf)
181 93658fb9 2020-03-18 stsp return got_resolve_remote_ref(id, buf + strlen("ref:"));
182 93658fb9 2020-03-18 stsp return r;
183 93658fb9 2020-03-18 stsp }
184 93658fb9 2020-03-18 stsp
185 93658fb9 2020-03-18 stsp static int
186 93658fb9 2020-03-18 stsp got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
187 93658fb9 2020-03-18 stsp {
188 93658fb9 2020-03-18 stsp SHA1_CTX ctx;
189 93658fb9 2020-03-18 stsp uint8_t hexpect[SHA1_DIGEST_LENGTH];
190 93658fb9 2020-03-18 stsp char s1[SHA1_DIGEST_STRING_LENGTH + 1];
191 93658fb9 2020-03-18 stsp char s2[SHA1_DIGEST_STRING_LENGTH + 1];
192 93658fb9 2020-03-18 stsp uint8_t buf[32*1024];
193 93658fb9 2020-03-18 stsp ssize_t n, r, nr;
194 93658fb9 2020-03-18 stsp
195 93658fb9 2020-03-18 stsp if(sz < 28)
196 93658fb9 2020-03-18 stsp return -1;
197 93658fb9 2020-03-18 stsp
198 93658fb9 2020-03-18 stsp n = 0;
199 93658fb9 2020-03-18 stsp SHA1Init(&ctx);
200 93658fb9 2020-03-18 stsp while(n < sz - 20){
201 93658fb9 2020-03-18 stsp nr = sizeof(buf);
202 93658fb9 2020-03-18 stsp if(sz - n - 20 < sizeof(buf))
203 93658fb9 2020-03-18 stsp nr = sz - n - 20;
204 93658fb9 2020-03-18 stsp r = readn(fd, buf, nr);
205 93658fb9 2020-03-18 stsp if(r != nr)
206 93658fb9 2020-03-18 stsp return -1;
207 93658fb9 2020-03-18 stsp SHA1Update(&ctx, buf, nr);
208 93658fb9 2020-03-18 stsp n += r;
209 93658fb9 2020-03-18 stsp }
210 93658fb9 2020-03-18 stsp SHA1Final(hcomp, &ctx);
211 93658fb9 2020-03-18 stsp
212 93658fb9 2020-03-18 stsp if(readn(fd, hexpect, sizeof(hexpect)) != sizeof(hexpect))
213 93658fb9 2020-03-18 stsp errx(1, "truncated packfile");
214 93658fb9 2020-03-18 stsp if(memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0){
215 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(hcomp, s1, sizeof(s1));
216 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(hexpect, s2, sizeof(s2));
217 93658fb9 2020-03-18 stsp printf("hash mismatch %s != %s\n", s1, s2);
218 93658fb9 2020-03-18 stsp return -1;
219 93658fb9 2020-03-18 stsp }
220 93658fb9 2020-03-18 stsp return 0;
221 93658fb9 2020-03-18 stsp }
222 93658fb9 2020-03-18 stsp
223 93658fb9 2020-03-18 stsp int
224 93658fb9 2020-03-18 stsp got_has_object(struct got_object_id *obj)
225 93658fb9 2020-03-18 stsp {
226 93658fb9 2020-03-18 stsp return 0;
227 93658fb9 2020-03-18 stsp }
228 93658fb9 2020-03-18 stsp
229 93658fb9 2020-03-18 stsp /*static */int
230 93658fb9 2020-03-18 stsp got_make_pack_dir(char *path)
231 93658fb9 2020-03-18 stsp {
232 93658fb9 2020-03-18 stsp char s[128];
233 93658fb9 2020-03-18 stsp char *p;
234 93658fb9 2020-03-18 stsp
235 93658fb9 2020-03-18 stsp if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
236 93658fb9 2020-03-18 stsp return -1;
237 93658fb9 2020-03-18 stsp for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
238 93658fb9 2020-03-18 stsp *p = 0;
239 93658fb9 2020-03-18 stsp if (mkdir(s, 0755) == -1)
240 93658fb9 2020-03-18 stsp if(errno != EEXIST)
241 93658fb9 2020-03-18 stsp return -1;
242 93658fb9 2020-03-18 stsp *p = '/';
243 93658fb9 2020-03-18 stsp }
244 93658fb9 2020-03-18 stsp return 0;
245 93658fb9 2020-03-18 stsp }
246 93658fb9 2020-03-18 stsp
247 93658fb9 2020-03-18 stsp static int
248 93658fb9 2020-03-18 stsp got_match_branch(char *br, char *pat)
249 93658fb9 2020-03-18 stsp {
250 93658fb9 2020-03-18 stsp char name[128];
251 93658fb9 2020-03-18 stsp
252 93658fb9 2020-03-18 stsp if(strstr(pat, "refs/heads") == pat) {
253 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
254 93658fb9 2020-03-18 stsp return -1;
255 93658fb9 2020-03-18 stsp } else if(strstr(pat, "heads")) {
256 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/%s", pat) >= sizeof(name))
257 93658fb9 2020-03-18 stsp return -1;
258 93658fb9 2020-03-18 stsp } else {
259 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
260 93658fb9 2020-03-18 stsp return -1;
261 93658fb9 2020-03-18 stsp }
262 93658fb9 2020-03-18 stsp return strcmp(br, name) == 0;
263 93658fb9 2020-03-18 stsp }
264 93658fb9 2020-03-18 stsp
265 93658fb9 2020-03-18 stsp static int
266 93658fb9 2020-03-18 stsp got_tokenize_refline(char *line, char **sp, size_t nsp)
267 93658fb9 2020-03-18 stsp {
268 93658fb9 2020-03-18 stsp char *p;
269 93658fb9 2020-03-18 stsp size_t i, j;
270 93658fb9 2020-03-18 stsp
271 93658fb9 2020-03-18 stsp for (i = 0; i < nsp; i++) {
272 93658fb9 2020-03-18 stsp while (isspace(*line))
273 93658fb9 2020-03-18 stsp line++;
274 93658fb9 2020-03-18 stsp p = line;
275 93658fb9 2020-03-18 stsp while (*line != '\0' && (!isspace(*line) || i == nsp - 1))
276 93658fb9 2020-03-18 stsp line++;
277 93658fb9 2020-03-18 stsp sp[i] = strndup(p, line - p);
278 93658fb9 2020-03-18 stsp }
279 93658fb9 2020-03-18 stsp for (j = i; j < nsp; j++)
280 93658fb9 2020-03-18 stsp sp[j] = NULL;
281 93658fb9 2020-03-18 stsp return i;
282 93658fb9 2020-03-18 stsp }
283 93658fb9 2020-03-18 stsp
284 9ff10419 2020-03-18 stsp static const struct got_error *
285 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
286 8f2d01a6 2020-03-18 stsp struct imsgbuf *ibuf)
287 93658fb9 2020-03-18 stsp {
288 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
289 ccbf9d19 2020-03-18 stsp char buf[GOT_PKTMAX], *sp[3];
290 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
291 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
292 93658fb9 2020-03-18 stsp int nref, refsz;
293 93658fb9 2020-03-18 stsp int i, n, req;
294 93658fb9 2020-03-18 stsp off_t packsz;
295 93658fb9 2020-03-18 stsp
296 93658fb9 2020-03-18 stsp nref = 0;
297 93658fb9 2020-03-18 stsp refsz = 16;
298 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
299 9ff10419 2020-03-18 stsp if (have == NULL)
300 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
301 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
302 9ff10419 2020-03-18 stsp if (want == NULL) {
303 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
304 9ff10419 2020-03-18 stsp goto done;
305 9ff10419 2020-03-18 stsp }
306 9ff10419 2020-03-18 stsp if (chattygit)
307 9ff10419 2020-03-18 stsp fprintf(stderr, "starting fetch\n");
308 9ff10419 2020-03-18 stsp while (1) {
309 93658fb9 2020-03-18 stsp n = readpkt(fd, buf, sizeof(buf));
310 9ff10419 2020-03-18 stsp if (n == -1){
311 9ff10419 2020-03-18 stsp err = got_error_from_errno("readpkt:");
312 9ff10419 2020-03-18 stsp goto done;
313 9ff10419 2020-03-18 stsp }
314 9ff10419 2020-03-18 stsp if (n == 0)
315 93658fb9 2020-03-18 stsp break;
316 9ff10419 2020-03-18 stsp if (strncmp(buf, "ERR ", 4) == 0) {
317 9ff10419 2020-03-18 stsp static char msg[1024];
318 9ff10419 2020-03-18 stsp strlcpy(msg, buf + 4, sizeof(msg));
319 9ff10419 2020-03-18 stsp err = got_error_msg(GOT_ERR_FETCH_FAILED, msg);
320 9ff10419 2020-03-18 stsp goto done;
321 9ff10419 2020-03-18 stsp }
322 9ff10419 2020-03-18 stsp if (got_tokenize_refline(buf, sp, 3) <= 2) {
323 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
324 9ff10419 2020-03-18 stsp goto done;
325 9ff10419 2020-03-18 stsp }
326 9ff10419 2020-03-18 stsp if (strstr(sp[1], "^{}"))
327 93658fb9 2020-03-18 stsp continue;
328 9ff10419 2020-03-18 stsp if (fetchbranch && !got_match_branch(sp[1], fetchbranch))
329 93658fb9 2020-03-18 stsp continue;
330 9ff10419 2020-03-18 stsp if (refsz == nref + 1){
331 93658fb9 2020-03-18 stsp refsz *= 2;
332 93658fb9 2020-03-18 stsp have = realloc(have, refsz * sizeof(have[0]));
333 9ff10419 2020-03-18 stsp if (have == NULL) {
334 9ff10419 2020-03-18 stsp err = got_error_from_errno("realloc");
335 9ff10419 2020-03-18 stsp goto done;
336 9ff10419 2020-03-18 stsp }
337 93658fb9 2020-03-18 stsp want = realloc(want, refsz * sizeof(want[0]));
338 9ff10419 2020-03-18 stsp if (want == NULL) {
339 9ff10419 2020-03-18 stsp err = got_error_from_errno("realloc");
340 9ff10419 2020-03-18 stsp goto done;
341 9ff10419 2020-03-18 stsp }
342 93658fb9 2020-03-18 stsp }
343 9ff10419 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, sp[0])) {
344 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
345 9ff10419 2020-03-18 stsp goto done;
346 9ff10419 2020-03-18 stsp }
347 9ff10419 2020-03-18 stsp
348 93658fb9 2020-03-18 stsp if (got_resolve_remote_ref(&have[nref], sp[1]) == -1)
349 93658fb9 2020-03-18 stsp memset(&have[nref], 0, sizeof(have[nref]));
350 8f2d01a6 2020-03-18 stsp err = got_privsep_send_fetch_progress(ibuf, &want[nref], sp[1]);
351 8f2d01a6 2020-03-18 stsp if (err)
352 8f2d01a6 2020-03-18 stsp goto done;
353 93658fb9 2020-03-18 stsp if (chattygit)
354 93658fb9 2020-03-18 stsp fprintf(stderr, "remote %s\n", sp[1]);
355 93658fb9 2020-03-18 stsp nref++;
356 93658fb9 2020-03-18 stsp }
357 93658fb9 2020-03-18 stsp
358 93658fb9 2020-03-18 stsp req = 0;
359 9ff10419 2020-03-18 stsp for (i = 0; i < nref; i++){
360 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
361 93658fb9 2020-03-18 stsp continue;
362 93658fb9 2020-03-18 stsp if (got_has_object(&want[i]))
363 93658fb9 2020-03-18 stsp continue;
364 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
365 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "want %s\n", hashstr);
366 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
367 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
368 9ff10419 2020-03-18 stsp goto done;
369 9ff10419 2020-03-18 stsp }
370 9ff10419 2020-03-18 stsp if (writepkt(fd, buf, n) == -1) {
371 9ff10419 2020-03-18 stsp err = got_error_from_errno("writepkt");
372 9ff10419 2020-03-18 stsp goto done;
373 9ff10419 2020-03-18 stsp }
374 93658fb9 2020-03-18 stsp req = 1;
375 93658fb9 2020-03-18 stsp }
376 93658fb9 2020-03-18 stsp flushpkt(fd);
377 9ff10419 2020-03-18 stsp for (i = 0; i < nref; i++){
378 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
379 93658fb9 2020-03-18 stsp continue;
380 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
381 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
382 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
383 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
384 9ff10419 2020-03-18 stsp goto done;
385 9ff10419 2020-03-18 stsp }
386 9ff10419 2020-03-18 stsp if (writepkt(fd, buf, n + 1) == -1) {
387 9ff10419 2020-03-18 stsp err = got_error_from_errno("writepkt");
388 9ff10419 2020-03-18 stsp goto done;
389 9ff10419 2020-03-18 stsp }
390 93658fb9 2020-03-18 stsp }
391 9ff10419 2020-03-18 stsp if (!req){
392 93658fb9 2020-03-18 stsp fprintf(stderr, "up to date\n");
393 93658fb9 2020-03-18 stsp flushpkt(fd);
394 93658fb9 2020-03-18 stsp }
395 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
396 9ff10419 2020-03-18 stsp if (writepkt(fd, buf, n) == -1) {
397 9ff10419 2020-03-18 stsp err = got_error_from_errno("writepkt");
398 9ff10419 2020-03-18 stsp goto done;
399 9ff10419 2020-03-18 stsp }
400 9ff10419 2020-03-18 stsp if (!req)
401 93658fb9 2020-03-18 stsp return 0;
402 93658fb9 2020-03-18 stsp
403 9ff10419 2020-03-18 stsp if ((n = readpkt(fd, buf, sizeof(buf))) == -1) {
404 9ff10419 2020-03-18 stsp err = got_error_from_errno("readpkt");
405 9ff10419 2020-03-18 stsp goto done;
406 9ff10419 2020-03-18 stsp }
407 93658fb9 2020-03-18 stsp buf[n] = 0;
408 93658fb9 2020-03-18 stsp
409 8f2d01a6 2020-03-18 stsp if (chattygit)
410 8f2d01a6 2020-03-18 stsp fprintf(stderr, "fetching...\n");
411 93658fb9 2020-03-18 stsp packsz = 0;
412 9ff10419 2020-03-18 stsp while (1) {
413 9ff10419 2020-03-18 stsp ssize_t w;
414 93658fb9 2020-03-18 stsp n = readn(fd, buf, sizeof buf);
415 9ff10419 2020-03-18 stsp if (n == 0)
416 93658fb9 2020-03-18 stsp break;
417 9ff10419 2020-03-18 stsp if (n == -1) {
418 9ff10419 2020-03-18 stsp err = got_error_from_errno("readn");
419 9ff10419 2020-03-18 stsp goto done;
420 9ff10419 2020-03-18 stsp }
421 9ff10419 2020-03-18 stsp w = write(packfd, buf, n);
422 9ff10419 2020-03-18 stsp if (w == -1) {
423 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
424 9ff10419 2020-03-18 stsp goto done;
425 9ff10419 2020-03-18 stsp }
426 9ff10419 2020-03-18 stsp if (w != n) {
427 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
428 9ff10419 2020-03-18 stsp goto done;
429 9ff10419 2020-03-18 stsp }
430 93658fb9 2020-03-18 stsp packsz += n;
431 93658fb9 2020-03-18 stsp }
432 9ff10419 2020-03-18 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
433 9ff10419 2020-03-18 stsp err = got_error_from_errno("lseek");
434 9ff10419 2020-03-18 stsp goto done;
435 9ff10419 2020-03-18 stsp }
436 9ff10419 2020-03-18 stsp if (got_check_pack_hash(packfd, packsz, packid->sha1) == -1)
437 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
438 9ff10419 2020-03-18 stsp done:
439 9ff10419 2020-03-18 stsp free(have);
440 9ff10419 2020-03-18 stsp free(want);
441 8f2d01a6 2020-03-18 stsp return err;
442 93658fb9 2020-03-18 stsp }
443 93658fb9 2020-03-18 stsp
444 93658fb9 2020-03-18 stsp
445 93658fb9 2020-03-18 stsp int
446 93658fb9 2020-03-18 stsp main(int argc, char **argv)
447 93658fb9 2020-03-18 stsp {
448 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
449 9ff10419 2020-03-18 stsp int fetchfd, packfd = -1;
450 93658fb9 2020-03-18 stsp struct got_object_id packid;
451 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
452 93658fb9 2020-03-18 stsp struct imsg imsg;
453 93658fb9 2020-03-18 stsp
454 93658fb9 2020-03-18 stsp if(getenv("GOT_DEBUG") != NULL){
455 93658fb9 2020-03-18 stsp fprintf(stderr, "fetch-pack being chatty!\n");
456 93658fb9 2020-03-18 stsp chattygit = 1;
457 93658fb9 2020-03-18 stsp }
458 93658fb9 2020-03-18 stsp
459 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
460 93658fb9 2020-03-18 stsp if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
461 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
462 93658fb9 2020-03-18 stsp err = NULL;
463 93658fb9 2020-03-18 stsp goto done;
464 93658fb9 2020-03-18 stsp }
465 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
466 93658fb9 2020-03-18 stsp goto done;
467 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
468 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
469 93658fb9 2020-03-18 stsp goto done;
470 93658fb9 2020-03-18 stsp }
471 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
472 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
473 93658fb9 2020-03-18 stsp goto done;
474 93658fb9 2020-03-18 stsp }
475 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
476 93658fb9 2020-03-18 stsp
477 93658fb9 2020-03-18 stsp if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
478 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
479 93658fb9 2020-03-18 stsp err = NULL;
480 93658fb9 2020-03-18 stsp goto done;
481 93658fb9 2020-03-18 stsp }
482 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
483 93658fb9 2020-03-18 stsp goto done;
484 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
485 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
486 93658fb9 2020-03-18 stsp goto done;
487 93658fb9 2020-03-18 stsp }
488 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
489 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
490 93658fb9 2020-03-18 stsp goto done;
491 93658fb9 2020-03-18 stsp }
492 93658fb9 2020-03-18 stsp packfd = imsg.fd;
493 93658fb9 2020-03-18 stsp
494 8f2d01a6 2020-03-18 stsp err = fetch_pack(fetchfd, packfd, &packid, &ibuf);
495 9ff10419 2020-03-18 stsp if (err)
496 93658fb9 2020-03-18 stsp goto done;
497 93658fb9 2020-03-18 stsp done:
498 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
499 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
500 9ff10419 2020-03-18 stsp if (err != NULL)
501 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
502 93658fb9 2020-03-18 stsp else
503 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
504 93658fb9 2020-03-18 stsp if(err != NULL) {
505 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
506 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
507 93658fb9 2020-03-18 stsp }
508 93658fb9 2020-03-18 stsp
509 93658fb9 2020-03-18 stsp exit(0);
510 93658fb9 2020-03-18 stsp }