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 struct got_object_id zhash = {.sha1={0}};
63 readn(int fd, void *buf, size_t n)
69 r = read(fd, buf + off, n - off);
83 fprintf(stderr, "writepkt: 0000\n");
84 return write(fd, "0000", 4);
89 readpkt(int fd, char *buf, int nbuf)
95 if(readn(fd, len, 4) == -1){
99 n = strtol(len, &e, 16);
102 fprintf(stderr, "readpkt: 0000\n");
105 if(e != len + 4 || n <= 4)
106 err(1, "invalid packet line length");
109 err(1, "buffer too small");
110 if((r = readn(fd, buf, n)) != n)
114 fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
119 writepkt(int fd, char *buf, int nbuf)
124 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
126 if(write(fd, len, 4) != 4)
128 if(write(fd, buf, nbuf) != nbuf)
131 fprintf(stderr, "writepkt: %s:\t", len);
132 fwrite(buf, 1, nbuf, stderr);
133 for(i = 0; i < nbuf; i++){
135 fputc(buf[i], stderr);
143 got_resolve_remote_ref(struct got_object_id *id, char *ref)
149 if(!got_parse_sha1_digest(id->sha1, ref))
152 /* Slightly special handling: translate remote refs to local ones. */
153 if (strcmp(ref, "HEAD") == 0) {
154 if(snprintf(buf, sizeof(buf), ".git/HEAD") >= sizeof(buf))
156 } else if(strstr(ref, "refs/heads") == ref) {
157 ref += strlen("refs/heads");
158 if(snprintf(buf, sizeof(buf),
159 ".git/refs/remotes/%s/%s", upstream, ref) >= sizeof(buf))
161 } else if(strstr(ref, "refs/tags") == ref) {
162 ref += strlen("refs/tags");
163 if(snprintf(buf, sizeof(buf),
164 ".git/refs/tags/%s/%s", upstream, ref) >= sizeof(buf))
172 if((f = open(s, O_RDONLY)) == -1)
174 if(readn(f, buf, sizeof(buf)) < 40)
176 if(!got_parse_sha1_digest(id->sha1, buf))
180 if(r == -1 && strstr(buf, "ref:") == buf)
181 return got_resolve_remote_ref(id, buf + strlen("ref:"));
186 got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
189 uint8_t hexpect[SHA1_DIGEST_LENGTH];
190 char s1[SHA1_DIGEST_STRING_LENGTH + 1];
191 char s2[SHA1_DIGEST_STRING_LENGTH + 1];
192 uint8_t buf[32*1024];
202 if(sz - n - 20 < sizeof(buf))
204 r = readn(fd, buf, nr);
207 SHA1Update(&ctx, buf, nr);
210 SHA1Final(hcomp, &ctx);
212 if(readn(fd, hexpect, sizeof(hexpect)) != sizeof(hexpect))
213 errx(1, "truncated packfile");
214 if(memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0){
215 got_sha1_digest_to_str(hcomp, s1, sizeof(s1));
216 got_sha1_digest_to_str(hexpect, s2, sizeof(s2));
217 printf("hash mismatch %s != %s\n", s1, s2);
224 got_has_object(struct got_object_id *obj)
230 got_make_pack_dir(char *path)
235 if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
237 for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
239 if (mkdir(s, 0755) == -1)
248 got_match_branch(char *br, char *pat)
252 if(strstr(pat, "refs/heads") == pat) {
253 if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
255 } else if(strstr(pat, "heads")) {
256 if (snprintf(name, sizeof(name), "refs/%s", pat) >= sizeof(name))
259 if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
262 return strcmp(br, name) == 0;
266 got_tokenize_refline(char *line, char **sp, size_t nsp)
271 for (i = 0; i < nsp; i++) {
272 while (isspace(*line))
275 while (*line != '\0' && (!isspace(*line) || i == nsp - 1))
277 sp[i] = strndup(p, line - p);
279 for (j = i; j < nsp; j++)
284 static const struct got_error *
285 fetch_pack(int fd, int packfd, struct got_object_id *packid,
286 struct imsgbuf *ibuf)
288 const struct got_error *err = NULL;
289 char buf[GOT_PKTMAX], *sp[3];
290 char hashstr[SHA1_DIGEST_STRING_LENGTH];
291 struct got_object_id *have, *want;
298 have = malloc(refsz * sizeof(have[0]));
300 return got_error_from_errno("malloc");
301 want = malloc(refsz * sizeof(want[0]));
303 err = got_error_from_errno("malloc");
307 fprintf(stderr, "starting fetch\n");
309 n = readpkt(fd, buf, sizeof(buf));
311 err = got_error_from_errno("readpkt:");
316 if (strncmp(buf, "ERR ", 4) == 0) {
317 static char msg[1024];
318 strlcpy(msg, buf + 4, sizeof(msg));
319 err = got_error_msg(GOT_ERR_FETCH_FAILED, msg);
322 if (got_tokenize_refline(buf, sp, 3) <= 2) {
323 err = got_error(GOT_ERR_NOT_REF);
326 if (strstr(sp[1], "^{}"))
328 if (fetchbranch && !got_match_branch(sp[1], fetchbranch))
330 if (refsz == nref + 1){
332 have = realloc(have, refsz * sizeof(have[0]));
334 err = got_error_from_errno("realloc");
337 want = realloc(want, refsz * sizeof(want[0]));
339 err = got_error_from_errno("realloc");
343 if (!got_parse_sha1_digest(want[nref].sha1, sp[0])) {
344 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
348 if (got_resolve_remote_ref(&have[nref], sp[1]) == -1)
349 memset(&have[nref], 0, sizeof(have[nref]));
350 err = got_privsep_send_fetch_progress(ibuf, &want[nref], sp[1]);
354 fprintf(stderr, "remote %s\n", sp[1]);
359 for (i = 0; i < nref; i++){
360 if (got_object_id_cmp(&have[i], &want[i]) == 0)
362 if (got_has_object(&want[i]))
364 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
365 n = snprintf(buf, sizeof(buf), "want %s\n", hashstr);
366 if (n >= sizeof(buf)) {
367 err = got_error(GOT_ERR_NO_SPACE);
370 if (writepkt(fd, buf, n) == -1) {
371 err = got_error_from_errno("writepkt");
377 for (i = 0; i < nref; i++){
378 if (got_object_id_cmp(&have[i], &zhash) == 0)
380 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
381 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
382 if (n >= sizeof(buf)) {
383 err = got_error(GOT_ERR_NO_SPACE);
386 if (writepkt(fd, buf, n + 1) == -1) {
387 err = got_error_from_errno("writepkt");
392 fprintf(stderr, "up to date\n");
395 n = snprintf(buf, sizeof(buf), "done\n");
396 if (writepkt(fd, buf, n) == -1) {
397 err = got_error_from_errno("writepkt");
403 if ((n = readpkt(fd, buf, sizeof(buf))) == -1) {
404 err = got_error_from_errno("readpkt");
410 fprintf(stderr, "fetching...\n");
414 n = readn(fd, buf, sizeof buf);
418 err = got_error_from_errno("readn");
421 w = write(packfd, buf, n);
423 err = got_error_from_errno("write");
427 err = got_error(GOT_ERR_IO);
432 if (lseek(packfd, 0, SEEK_SET) == -1) {
433 err = got_error_from_errno("lseek");
436 if (got_check_pack_hash(packfd, packsz, packid->sha1) == -1)
437 err = got_error(GOT_ERR_BAD_PACKFILE);
446 main(int argc, char **argv)
448 const struct got_error *err = NULL;
449 int fetchfd, packfd = -1;
450 struct got_object_id packid;
454 if(getenv("GOT_DEBUG") != NULL){
455 fprintf(stderr, "fetch-pack being chatty!\n");
459 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
460 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
461 if (err->code == GOT_ERR_PRIVSEP_PIPE)
465 if (imsg.hdr.type == GOT_IMSG_STOP)
467 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
468 err = got_error(GOT_ERR_PRIVSEP_MSG);
471 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
472 err = got_error(GOT_ERR_PRIVSEP_LEN);
477 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
478 if (err->code == GOT_ERR_PRIVSEP_PIPE)
482 if (imsg.hdr.type == GOT_IMSG_STOP)
484 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
485 err = got_error(GOT_ERR_PRIVSEP_MSG);
488 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
489 err = got_error(GOT_ERR_PRIVSEP_LEN);
494 err = fetch_pack(fetchfd, packfd, &packid, &ibuf);
498 if (packfd != -1 && close(packfd) == -1 && err == NULL)
499 err = got_error_from_errno("close");
501 got_privsep_send_error(&ibuf, err);
503 err = got_privsep_send_fetch_done(&ibuf, packid);
505 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
506 got_privsep_send_error(&ibuf, err);