Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <sys/syslimits.h>
24 #include <stdint.h>
25 #include <errno.h>
26 #include <imsg.h>
27 #include <limits.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <sha1.h>
34 #include <fcntl.h>
35 #include <zlib.h>
36 #include <err.h>
38 #include "got_error.h"
39 #include "got_object.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_privsep.h"
48 #define GOT_PKTMAX 65536
50 struct got_object *indexed;
51 static int chattygit;
52 static char *fetchbranch;
53 static char *upstream = "origin";
54 static char *packtmp = ".git/objects/pack/fetch.tmp";
55 static struct got_object_id zhash = {.sha1={0}};
57 static char*
58 strip(char *ref)
59 {
60 return ref;
61 }
63 int
64 readn(int fd, void *buf, size_t n)
65 {
66 ssize_t r, off;
68 off = 0;
69 while (off != n) {
70 r = read(fd, buf + off, n - off);
71 if (r < 0)
72 return -1;
73 if (r == 0)
74 return off;
75 off += r;
76 }
77 return off;
78 }
80 int
81 flushpkt(int fd)
82 {
83 if(chattygit)
84 fprintf(stderr, "writepkt: 0000\n");
85 return write(fd, "0000", 4);
86 }
89 int
90 readpkt(int fd, char *buf, int nbuf)
91 {
92 char len[5];
93 char *e;
94 int n, r;
96 if(readn(fd, len, 4) == -1){
97 return -1;
98 }
99 len[4] = 0;
100 n = strtol(len, &e, 16);
101 if(n == 0){
102 if(chattygit)
103 fprintf(stderr, "readpkt: 0000\n");
104 return 0;
106 if(e != len + 4 || n <= 4)
107 err(1, "invalid packet line length");
108 n -= 4;
109 if(n >= nbuf)
110 err(1, "buffer too small");
111 if((r = readn(fd, buf, n)) != n)
112 return -1;
113 buf[n] = 0;
114 if(chattygit)
115 fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
116 return n;
119 int
120 writepkt(int fd, char *buf, int nbuf)
122 char len[5];
123 int i;
125 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
126 return -1;
127 if(write(fd, len, 4) != 4)
128 return -1;
129 if(write(fd, buf, nbuf) != nbuf)
130 return -1;
131 if(chattygit){
132 fprintf(stderr, "writepkt: %s:\t", len);
133 fwrite(buf, 1, nbuf, stderr);
134 for(i = 0; i < nbuf; i++){
135 if(isprint(buf[i]))
136 fputc(buf[i], stderr);
139 return 0;
143 int
144 got_resolve_remote_ref(struct got_object_id *id, char *ref)
146 char buf[128], *s;
147 int r, f;
149 ref = strip(ref);
150 if(!got_parse_sha1_digest(id->sha1, ref))
151 return 0;
153 /* Slightly special handling: translate remote refs to local ones. */
154 if (strcmp(ref, "HEAD") == 0) {
155 if(snprintf(buf, sizeof(buf), ".git/HEAD") >= sizeof(buf))
156 return -1;
157 } else if(strstr(ref, "refs/heads") == ref) {
158 ref += strlen("refs/heads");
159 if(snprintf(buf, sizeof(buf),
160 ".git/refs/remotes/%s/%s", upstream, ref) >= sizeof(buf))
161 return -1;
162 } else if(strstr(ref, "refs/tags") == ref) {
163 ref += strlen("refs/tags");
164 if(snprintf(buf, sizeof(buf),
165 ".git/refs/tags/%s/%s", upstream, ref) >= sizeof(buf))
166 return -1;
167 } else {
168 return -1;
171 r = -1;
172 s = strip(buf);
173 if((f = open(s, O_RDONLY)) == -1)
174 goto err;
175 if(readn(f, buf, sizeof(buf)) < 40)
176 goto err;
177 if(!got_parse_sha1_digest(id->sha1, buf))
178 goto err;
179 err:
180 close(f);
181 if(r == -1 && strstr(buf, "ref:") == buf)
182 return got_resolve_remote_ref(id, buf + strlen("ref:"));
183 return r;
186 static int
187 got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
189 SHA1_CTX ctx;
190 uint8_t hexpect[SHA1_DIGEST_LENGTH];
191 char s1[SHA1_DIGEST_STRING_LENGTH + 1];
192 char s2[SHA1_DIGEST_STRING_LENGTH + 1];
193 uint8_t buf[32*1024];
194 ssize_t n, r, nr;
196 if(sz < 28)
197 return -1;
199 n = 0;
200 SHA1Init(&ctx);
201 while(n < sz - 20){
202 nr = sizeof(buf);
203 if(sz - n - 20 < sizeof(buf))
204 nr = sz - n - 20;
205 r = readn(fd, buf, nr);
206 if(r != nr)
207 return -1;
208 SHA1Update(&ctx, buf, nr);
209 n += r;
211 SHA1Final(hcomp, &ctx);
213 if(readn(fd, hexpect, sizeof(hexpect)) != sizeof(hexpect))
214 errx(1, "truncated packfile");
215 if(memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0){
216 got_sha1_digest_to_str(hcomp, s1, sizeof(s1));
217 got_sha1_digest_to_str(hexpect, s2, sizeof(s2));
218 printf("hash mismatch %s != %s\n", s1, s2);
219 return -1;
221 return 0;
224 int
225 got_has_object(struct got_object_id *obj)
227 return 0;
230 /*static */int
231 got_make_pack_dir(char *path)
233 char s[128];
234 char *p;
236 if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
237 return -1;
238 for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
239 *p = 0;
240 if (mkdir(s, 0755) == -1)
241 if(errno != EEXIST)
242 return -1;
243 *p = '/';
245 return 0;
248 static int
249 got_match_branch(char *br, char *pat)
251 char name[128];
253 if(strstr(pat, "refs/heads") == pat) {
254 if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
255 return -1;
256 } else if(strstr(pat, "heads")) {
257 if (snprintf(name, sizeof(name), "refs/%s", pat) >= sizeof(name))
258 return -1;
259 } else {
260 if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
261 return -1;
263 return strcmp(br, name) == 0;
266 static int
267 got_tokenize_refline(char *line, char **sp, size_t nsp)
269 char *p;
270 size_t i, j;
272 for (i = 0; i < nsp; i++) {
273 while (isspace(*line))
274 line++;
275 p = line;
276 while (*line != '\0' && (!isspace(*line) || i == nsp - 1))
277 line++;
278 sp[i] = strndup(p, line - p);
280 for (j = i; j < nsp; j++)
281 sp[j] = NULL;
282 return i;
285 static int
286 got_fetch_pack(int fd, int packfd, char *packtmp, struct got_object_id *packid)
288 char buf[GOT_PKTMAX], idxtmp[256], *sp[3];
289 char hashstr[SHA1_DIGEST_STRING_LENGTH];
290 struct got_object_id *have, *want;
291 int nref, refsz;
292 int i, n, req;
293 off_t packsz;
295 nref = 0;
296 refsz = 16;
297 have = malloc(refsz * sizeof(have[0]));
298 if(have == NULL)
299 err(1, "malloc packs");
300 want = malloc(refsz * sizeof(want[0]));
301 if(want == NULL)
302 err(1, "malloc packs");
303 if(chattygit)
304 fprintf(stderr, "starting fetch\n");
305 while(1){
306 n = readpkt(fd, buf, sizeof(buf));
307 if(n == -1){
308 err(1, "readpkt:");
309 return -1;
311 if(n == 0)
312 break;
313 if(strncmp(buf, "ERR ", 4) == 0)
314 err(1, "%s", buf + 4);
315 if(got_tokenize_refline(buf, sp, 3) <= 2)
316 err(1, "malformed ref line");
317 if(strstr(sp[1], "^{}"))
318 continue;
319 if(fetchbranch && !got_match_branch(sp[1], fetchbranch))
320 continue;
321 if(refsz == nref + 1){
322 refsz *= 2;
323 have = realloc(have, refsz * sizeof(have[0]));
324 if (have == NULL)
325 err(1, "realloc have");
326 want = realloc(want, refsz * sizeof(want[0]));
327 if (want == NULL)
328 err(1, "realloc want");
330 if(!got_parse_sha1_digest(want[nref].sha1, sp[0]))
331 errx(1, "invalid hash %s", sp[0]);
332 if (got_resolve_remote_ref(&have[nref], sp[1]) == -1)
333 memset(&have[nref], 0, sizeof(have[nref]));
334 // TODO: send IMSG with progress.
335 if (chattygit)
336 fprintf(stderr, "remote %s\n", sp[1]);
337 nref++;
340 req = 0;
341 for(i = 0; i < nref; i++){
342 if (got_object_id_cmp(&have[i], &want[i]) == 0)
343 continue;
344 if (got_has_object(&want[i]))
345 continue;
346 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
347 n = snprintf(buf, sizeof(buf), "want %s\n", hashstr);
348 if (n >= sizeof(buf))
349 errx(1, "undersized buffer");
350 if(writepkt(fd, buf, n) == -1)
351 errx(1, "could not send want");
352 req = 1;
354 flushpkt(fd);
355 for(i = 0; i < nref; i++){
356 if (got_object_id_cmp(&have[i], &zhash) == 0)
357 continue;
358 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
359 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
360 if (n >= sizeof(buf))
361 errx(1, "undersized buffer");
362 if (writepkt(fd, buf, n + 1) == -1)
363 errx(1, "could not send have");
365 if(!req){
366 fprintf(stderr, "up to date\n");
367 flushpkt(fd);
369 n = snprintf(buf, sizeof(buf), "done\n");
370 if(writepkt(fd, buf, n) == -1)
371 errx(1, "lost connection write");
372 if(!req)
373 return 0;
375 if((n = readpkt(fd, buf, sizeof(buf))) == -1)
376 errx(1, "lost connection read");
377 buf[n] = 0;
379 fprintf(stderr, "fetching...\n");
380 packsz = 0;
381 while(1){
382 n = readn(fd, buf, sizeof buf);
383 if(n == 0)
384 break;
385 if(n == -1 || write(packfd, buf, n) != n)
386 err(1, "could not fetch packfile:");
387 packsz += n;
389 if(lseek(packfd, 0, SEEK_SET) == -1)
390 err(1, "packfile seek:");
391 if(got_check_pack_hash(packfd, packsz, packid->sha1) == -1)
392 errx(1, "corrupt packfile");
393 close(packfd);
394 n = strlen(packtmp) - strlen(".tmp");
395 memcpy(idxtmp, packtmp, n);
396 memcpy(idxtmp + n, ".idx", strlen(".idx") + 1);
397 return 0;
401 int
402 main(int argc, char **argv)
404 const struct got_error *err = NULL;
405 int fetchfd, packfd;
406 struct got_object_id packid;
407 struct imsgbuf ibuf;
408 struct imsg imsg;
410 if(getenv("GOT_DEBUG") != NULL){
411 fprintf(stderr, "fetch-pack being chatty!\n");
412 chattygit = 1;
415 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
416 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
417 if (err->code == GOT_ERR_PRIVSEP_PIPE)
418 err = NULL;
419 goto done;
421 if (imsg.hdr.type == GOT_IMSG_STOP)
422 goto done;
423 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
424 err = got_error(GOT_ERR_PRIVSEP_MSG);
425 goto done;
427 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
428 err = got_error(GOT_ERR_PRIVSEP_LEN);
429 goto done;
431 fetchfd = imsg.fd;
433 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
434 if (err->code == GOT_ERR_PRIVSEP_PIPE)
435 err = NULL;
436 goto done;
438 if (imsg.hdr.type == GOT_IMSG_STOP)
439 goto done;
440 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
441 err = got_error(GOT_ERR_PRIVSEP_MSG);
442 goto done;
444 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
445 err = got_error(GOT_ERR_PRIVSEP_LEN);
446 goto done;
448 packfd = imsg.fd;
450 if(got_fetch_pack(fetchfd, packfd, packtmp, &packid) == -1) {
451 err = got_error(GOT_ERR_FETCH_FAILED);
452 goto done;
454 done:
455 if(err != NULL)
456 got_privsep_send_error(&ibuf, err);
457 else
458 err = got_privsep_send_fetch_done(&ibuf, packid);
459 if(err != NULL) {
460 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
461 got_privsep_send_error(&ibuf, err);
464 exit(0);