2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
22 #include <sys/syslimits.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;
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}};
64 readn(int fd, void *buf, size_t n)
70 r = read(fd, buf + off, n - off);
84 fprintf(stderr, "writepkt: 0000\n");
85 return write(fd, "0000", 4);
90 readpkt(int fd, char *buf, int nbuf)
96 if(readn(fd, len, 4) == -1){
100 n = strtol(len, &e, 16);
103 fprintf(stderr, "readpkt: 0000\n");
106 if(e != len + 4 || n <= 4)
107 err(1, "invalid packet line length");
110 err(1, "buffer too small");
111 if((r = readn(fd, buf, n)) != n)
115 fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
120 writepkt(int fd, char *buf, int nbuf)
125 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
127 if(write(fd, len, 4) != 4)
129 if(write(fd, buf, nbuf) != nbuf)
132 fprintf(stderr, "writepkt: %s:\t", len);
133 fwrite(buf, 1, nbuf, stderr);
134 for(i = 0; i < nbuf; i++){
136 fputc(buf[i], stderr);
144 got_resolve_remote_ref(struct got_object_id *id, char *ref)
150 if(!got_parse_sha1_digest(id->sha1, ref))
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))
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))
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))
173 if((f = open(s, O_RDONLY)) == -1)
175 if(readn(f, buf, sizeof(buf)) < 40)
177 if(!got_parse_sha1_digest(id->sha1, buf))
181 if(r == -1 && strstr(buf, "ref:") == buf)
182 return got_resolve_remote_ref(id, buf + strlen("ref:"));
187 got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
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];
203 if(sz - n - 20 < sizeof(buf))
205 r = readn(fd, buf, nr);
208 SHA1Update(&ctx, buf, nr);
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);
225 got_has_object(struct got_object_id *obj)
231 got_make_pack_dir(char *path)
236 if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
238 for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
240 if (mkdir(s, 0755) == -1)
249 got_match_branch(char *br, char *pat)
253 if(strstr(pat, "refs/heads") == pat) {
254 if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
256 } else if(strstr(pat, "heads")) {
257 if (snprintf(name, sizeof(name), "refs/%s", pat) >= sizeof(name))
260 if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
263 return strcmp(br, name) == 0;
267 got_tokenize_refline(char *line, char **sp, size_t nsp)
272 for (i = 0; i < nsp; i++) {
273 while (isspace(*line))
276 while (*line != '\0' && (!isspace(*line) || i == nsp - 1))
278 sp[i] = strndup(p, line - p);
280 for (j = i; j < nsp; j++)
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;
297 have = malloc(refsz * sizeof(have[0]));
299 err(1, "malloc packs");
300 want = malloc(refsz * sizeof(want[0]));
302 err(1, "malloc packs");
304 fprintf(stderr, "starting fetch\n");
306 n = readpkt(fd, buf, sizeof(buf));
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], "^{}"))
319 if(fetchbranch && !got_match_branch(sp[1], fetchbranch))
321 if(refsz == nref + 1){
323 have = realloc(have, refsz * sizeof(have[0]));
325 err(1, "realloc have");
326 want = realloc(want, refsz * sizeof(want[0]));
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.
336 fprintf(stderr, "remote %s\n", sp[1]);
341 for(i = 0; i < nref; i++){
342 if (got_object_id_cmp(&have[i], &want[i]) == 0)
344 if (got_has_object(&want[i]))
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");
355 for(i = 0; i < nref; i++){
356 if (got_object_id_cmp(&have[i], &zhash) == 0)
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");
366 fprintf(stderr, "up to date\n");
369 n = snprintf(buf, sizeof(buf), "done\n");
370 if(writepkt(fd, buf, n) == -1)
371 errx(1, "lost connection write");
375 if((n = readpkt(fd, buf, sizeof(buf))) == -1)
376 errx(1, "lost connection read");
379 fprintf(stderr, "fetching...\n");
382 n = readn(fd, buf, sizeof buf);
385 if(n == -1 || write(packfd, buf, n) != n)
386 err(1, "could not fetch packfile:");
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");
394 n = strlen(packtmp) - strlen(".tmp");
395 memcpy(idxtmp, packtmp, n);
396 memcpy(idxtmp + n, ".idx", strlen(".idx") + 1);
402 main(int argc, char **argv)
404 const struct got_error *err = NULL;
406 struct got_object_id packid;
410 if(getenv("GOT_DEBUG") != NULL){
411 fprintf(stderr, "fetch-pack being chatty!\n");
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)
421 if (imsg.hdr.type == GOT_IMSG_STOP)
423 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
424 err = got_error(GOT_ERR_PRIVSEP_MSG);
427 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
428 err = got_error(GOT_ERR_PRIVSEP_LEN);
433 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
434 if (err->code == GOT_ERR_PRIVSEP_PIPE)
438 if (imsg.hdr.type == GOT_IMSG_STOP)
440 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
441 err = got_error(GOT_ERR_PRIVSEP_MSG);
444 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
445 err = got_error(GOT_ERR_PRIVSEP_LEN);
450 if(got_fetch_pack(fetchfd, packfd, packtmp, &packid) == -1) {
451 err = got_error(GOT_ERR_FETCH_FAILED);
456 got_privsep_send_error(&ibuf, err);
458 err = got_privsep_send_fetch_done(&ibuf, packid);
460 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
461 got_privsep_send_error(&ibuf, err);