2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
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>
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>
38 93658fb9 2020-03-18 stsp #include "got_error.h"
39 93658fb9 2020-03-18 stsp #include "got_object.h"
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"
48 93658fb9 2020-03-18 stsp #define GOT_PKTMAX 65536
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}};
57 93658fb9 2020-03-18 stsp static char*
58 93658fb9 2020-03-18 stsp strip(char *ref)
64 93658fb9 2020-03-18 stsp readn(int fd, void *buf, size_t n)
66 93658fb9 2020-03-18 stsp ssize_t r, off;
69 93658fb9 2020-03-18 stsp while (off != n) {
70 93658fb9 2020-03-18 stsp r = read(fd, buf + off, n - off);
81 93658fb9 2020-03-18 stsp flushpkt(int fd)
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);
90 93658fb9 2020-03-18 stsp readpkt(int fd, char *buf, int nbuf)
92 93658fb9 2020-03-18 stsp char len[5];
96 93658fb9 2020-03-18 stsp if(readn(fd, len, 4) == -1){
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");
106 93658fb9 2020-03-18 stsp if(e != len + 4 || n <= 4)
107 93658fb9 2020-03-18 stsp err(1, "invalid packet line length");
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)
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);
120 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
122 93658fb9 2020-03-18 stsp char len[5];
125 93658fb9 2020-03-18 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
127 93658fb9 2020-03-18 stsp if(write(fd, len, 4) != 4)
129 93658fb9 2020-03-18 stsp if(write(fd, buf, nbuf) != nbuf)
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);
144 93658fb9 2020-03-18 stsp got_resolve_remote_ref(struct got_object_id *id, char *ref)
146 93658fb9 2020-03-18 stsp char buf[128], *s;
149 93658fb9 2020-03-18 stsp ref = strip(ref);
150 93658fb9 2020-03-18 stsp if(!got_parse_sha1_digest(id->sha1, ref))
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))
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))
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))
172 93658fb9 2020-03-18 stsp s = strip(buf);
173 93658fb9 2020-03-18 stsp if((f = open(s, O_RDONLY)) == -1)
175 93658fb9 2020-03-18 stsp if(readn(f, buf, sizeof(buf)) < 40)
177 93658fb9 2020-03-18 stsp if(!got_parse_sha1_digest(id->sha1, buf))
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:"));
187 93658fb9 2020-03-18 stsp got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
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;
196 93658fb9 2020-03-18 stsp if(sz < 28)
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)
208 93658fb9 2020-03-18 stsp SHA1Update(&ctx, buf, nr);
211 93658fb9 2020-03-18 stsp SHA1Final(hcomp, &ctx);
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);
225 93658fb9 2020-03-18 stsp got_has_object(struct got_object_id *obj)
230 93658fb9 2020-03-18 stsp /*static */int
231 93658fb9 2020-03-18 stsp got_make_pack_dir(char *path)
233 93658fb9 2020-03-18 stsp char s[128];
236 93658fb9 2020-03-18 stsp if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
238 93658fb9 2020-03-18 stsp for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
240 93658fb9 2020-03-18 stsp if (mkdir(s, 0755) == -1)
241 93658fb9 2020-03-18 stsp if(errno != EEXIST)
249 93658fb9 2020-03-18 stsp got_match_branch(char *br, char *pat)
251 93658fb9 2020-03-18 stsp char name[128];
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))
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))
260 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
263 93658fb9 2020-03-18 stsp return strcmp(br, name) == 0;
267 93658fb9 2020-03-18 stsp got_tokenize_refline(char *line, char **sp, size_t nsp)
270 93658fb9 2020-03-18 stsp size_t i, j;
272 93658fb9 2020-03-18 stsp for (i = 0; i < nsp; i++) {
273 93658fb9 2020-03-18 stsp while (isspace(*line))
276 93658fb9 2020-03-18 stsp while (*line != '\0' && (!isspace(*line) || i == nsp - 1))
278 93658fb9 2020-03-18 stsp sp[i] = strndup(p, line - p);
280 93658fb9 2020-03-18 stsp for (j = i; j < nsp; j++)
281 93658fb9 2020-03-18 stsp sp[j] = NULL;
286 93658fb9 2020-03-18 stsp got_fetch_pack(int fd, int packfd, char *packtmp, struct got_object_id *packid)
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;
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");
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:");
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], "^{}"))
319 93658fb9 2020-03-18 stsp if(fetchbranch && !got_match_branch(sp[1], fetchbranch))
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");
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]);
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)
344 93658fb9 2020-03-18 stsp if (got_has_object(&want[i]))
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");
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)
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");
366 93658fb9 2020-03-18 stsp fprintf(stderr, "up to date\n");
367 93658fb9 2020-03-18 stsp flushpkt(fd);
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");
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;
379 93658fb9 2020-03-18 stsp fprintf(stderr, "fetching...\n");
380 93658fb9 2020-03-18 stsp packsz = 0;
382 93658fb9 2020-03-18 stsp n = readn(fd, buf, sizeof buf);
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;
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);
402 93658fb9 2020-03-18 stsp main(int argc, char **argv)
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;
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;
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;
421 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
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);
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);
431 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
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;
438 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
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);
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);
448 93658fb9 2020-03-18 stsp packfd = imsg.fd;
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);
455 93658fb9 2020-03-18 stsp if(err != NULL)
456 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
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);