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