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"
40 #include "got_version.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #define GOT_PKTMAX 65536
55 struct got_object *indexed;
57 static char *fetchbranch;
58 static char *upstream = "origin";
59 static struct got_object_id zhash = {.sha1={0}};
68 readn(int fd, void *buf, size_t n)
74 r = read(fd, buf + off, n - off);
88 fprintf(stderr, "writepkt: 0000\n");
89 return write(fd, "0000", 4);
94 readpkt(int fd, char *buf, int nbuf)
100 if(readn(fd, len, 4) == -1){
104 n = strtol(len, &e, 16);
107 fprintf(stderr, "readpkt: 0000\n");
110 if(e != len + 4 || n <= 4)
111 err(1, "invalid packet line length");
114 err(1, "buffer too small");
115 if((r = readn(fd, buf, n)) != n)
119 fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
124 writepkt(int fd, char *buf, int nbuf)
129 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
131 if(write(fd, len, 4) != 4)
133 if(write(fd, buf, nbuf) != nbuf)
136 fprintf(stderr, "writepkt: %s:\t", len);
137 fwrite(buf, 1, nbuf, stderr);
138 for(i = 0; i < nbuf; i++){
140 fputc(buf[i], stderr);
148 got_resolve_remote_ref(struct got_object_id *id, char *ref)
154 if(!got_parse_sha1_digest(id->sha1, ref))
157 /* Slightly special handling: translate remote refs to local ones. */
158 if (strcmp(ref, "HEAD") == 0) {
159 if(snprintf(buf, sizeof(buf), ".git/HEAD") >= sizeof(buf))
161 } else if(strstr(ref, "refs/heads") == ref) {
162 ref += strlen("refs/heads");
163 if(snprintf(buf, sizeof(buf),
164 ".git/refs/remotes/%s/%s", upstream, ref) >= sizeof(buf))
166 } else if(strstr(ref, "refs/tags") == ref) {
167 ref += strlen("refs/tags");
168 if(snprintf(buf, sizeof(buf),
169 ".git/refs/tags/%s/%s", upstream, ref) >= sizeof(buf))
177 if((f = open(s, O_RDONLY)) == -1)
179 if(readn(f, buf, sizeof(buf)) < 40)
181 if(!got_parse_sha1_digest(id->sha1, buf))
185 if(r == -1 && strstr(buf, "ref:") == buf)
186 return got_resolve_remote_ref(id, buf + strlen("ref:"));
191 got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
194 uint8_t hexpect[SHA1_DIGEST_LENGTH];
195 char s1[SHA1_DIGEST_STRING_LENGTH + 1];
196 char s2[SHA1_DIGEST_STRING_LENGTH + 1];
197 uint8_t buf[32*1024];
207 if(sz - n - 20 < sizeof(buf))
209 r = readn(fd, buf, nr);
212 SHA1Update(&ctx, buf, nr);
215 SHA1Final(hcomp, &ctx);
217 if(readn(fd, hexpect, sizeof(hexpect)) != sizeof(hexpect))
218 errx(1, "truncated packfile");
219 if(memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0){
220 got_sha1_digest_to_str(hcomp, s1, sizeof(s1));
221 got_sha1_digest_to_str(hexpect, s2, sizeof(s2));
222 printf("hash mismatch %s != %s\n", s1, s2);
229 got_has_object(struct got_object_id *obj)
235 got_make_pack_dir(char *path)
240 if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
242 for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
244 if (mkdir(s, 0755) == -1)
253 got_match_branch(char *br, char *pat)
257 if(strstr(pat, "refs/heads") == pat) {
258 if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
260 } else if(strstr(pat, "heads")) {
261 if (snprintf(name, sizeof(name), "refs/%s", pat) >= sizeof(name))
264 if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
267 return strcmp(br, name) == 0;
270 static const struct got_error *
271 got_tokenize_refline(char **tokens, char *line, int len)
273 const struct got_error *err = NULL;
276 const int maxtokens = 3;
278 for (i = 0; i < maxtokens; i++)
281 for (i = 0; n < len && i < maxtokens; i++) {
282 while (isspace(*line)) {
287 while (*line != '\0' &&
288 (!isspace(*line) || i == maxtokens - 1)) {
292 tokens[i] = strndup(p, line - p);
293 if (tokens[i] == NULL) {
294 err = got_error_from_errno("strndup");
297 /* Skip \0 field-delimiter at end of token. */
298 while (line[0] == '\0' && n < len) {
304 err = got_error(GOT_ERR_NOT_REF);
308 for (j = 0; j < i; j++)
315 struct got_capability {
319 static const struct got_capability got_capabilities[] = {
320 #if 0 /* got-index-pack is not ready for this */
321 { "ofs-delta", NULL },
323 { "agent", "got/" GOT_VERSION_STR },
326 static const struct got_error *
327 match_capability(char **my_capabilities, const char *capa,
328 const struct got_capability *mycapa)
333 equalsign = strchr(capa, '=');
335 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
338 if (strcmp(capa, mycapa->key) != 0)
342 if (asprintf(&s, "%s%s%s%s%s",
343 *my_capabilities != NULL ? *my_capabilities : "",
344 *my_capabilities != NULL ? " " : "",
346 mycapa->value != NULL ? "=" : "",
347 mycapa->value != NULL? mycapa->value : "") == -1)
348 return got_error_from_errno("asprintf");
350 free(*my_capabilities);
351 *my_capabilities = s;
355 static const struct got_error *
356 match_capabilities(char **my_capabilities, char *server_capabilities)
358 const struct got_error *err = NULL;
362 *my_capabilities = NULL;
364 capa = strsep(&server_capabilities, " ");
368 for (i = 0; i < nitems(got_capabilities); i++) {
369 err = match_capability(my_capabilities,
370 capa, &got_capabilities[i]);
379 static const struct got_error *
380 fetch_pack(int fd, int packfd, struct got_object_id *packid,
381 struct imsgbuf *ibuf)
383 const struct got_error *err = NULL;
384 char buf[GOT_PKTMAX], *sp[3];
385 char hashstr[SHA1_DIGEST_STRING_LENGTH];
386 struct got_object_id *have, *want;
387 int is_firstpkt = 1, nref = 0, refsz = 16;
390 char *my_capabilities = NULL;
392 have = malloc(refsz * sizeof(have[0]));
394 return got_error_from_errno("malloc");
395 want = malloc(refsz * sizeof(want[0]));
397 err = got_error_from_errno("malloc");
401 fprintf(stderr, "starting fetch\n");
403 n = readpkt(fd, buf, sizeof(buf));
405 err = got_error_from_errno("readpkt");
410 if (strncmp(buf, "ERR ", 4) == 0) {
411 static char msg[1024];
412 strlcpy(msg, buf + 4, sizeof(msg));
413 err = got_error_msg(GOT_ERR_FETCH_FAILED, msg);
416 err = got_tokenize_refline(sp, buf, n);
419 if (chattygit && sp[2][0] != '\0')
420 fprintf(stderr, "server capabilities: %s\n", sp[2]);
422 err = match_capabilities(&my_capabilities, sp[2]);
425 if (chattygit && my_capabilities)
426 fprintf(stderr, "my matched capabilities: %s\n",
430 if (strstr(sp[1], "^{}"))
432 if (fetchbranch && !got_match_branch(sp[1], fetchbranch))
434 if (refsz == nref + 1){
436 have = realloc(have, refsz * sizeof(have[0]));
438 err = got_error_from_errno("realloc");
441 want = realloc(want, refsz * sizeof(want[0]));
443 err = got_error_from_errno("realloc");
447 if (!got_parse_sha1_digest(want[nref].sha1, sp[0])) {
448 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
452 if (got_resolve_remote_ref(&have[nref], sp[1]) == -1)
453 memset(&have[nref], 0, sizeof(have[nref]));
454 err = got_privsep_send_fetch_progress(ibuf, &want[nref], sp[1]);
458 fprintf(stderr, "remote %s\n", sp[1]);
463 for (i = 0; i < nref; i++){
464 if (got_object_id_cmp(&have[i], &want[i]) == 0)
466 if (got_has_object(&want[i]))
468 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
469 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
470 i == 0 && my_capabilities ? my_capabilities : "");
471 if (n >= sizeof(buf)) {
472 err = got_error(GOT_ERR_NO_SPACE);
475 if (writepkt(fd, buf, n) == -1) {
476 err = got_error_from_errno("writepkt");
482 for (i = 0; i < nref; i++){
483 if (got_object_id_cmp(&have[i], &zhash) == 0)
485 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
486 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
487 if (n >= sizeof(buf)) {
488 err = got_error(GOT_ERR_NO_SPACE);
491 if (writepkt(fd, buf, n + 1) == -1) {
492 err = got_error_from_errno("writepkt");
497 fprintf(stderr, "up to date\n");
500 n = snprintf(buf, sizeof(buf), "done\n");
501 if (writepkt(fd, buf, n) == -1) {
502 err = got_error_from_errno("writepkt");
508 if ((n = readpkt(fd, buf, sizeof(buf))) == -1) {
509 err = got_error_from_errno("readpkt");
515 fprintf(stderr, "fetching...\n");
519 n = readn(fd, buf, sizeof buf);
523 err = got_error_from_errno("readn");
526 w = write(packfd, buf, n);
528 err = got_error_from_errno("write");
532 err = got_error(GOT_ERR_IO);
537 if (lseek(packfd, 0, SEEK_SET) == -1) {
538 err = got_error_from_errno("lseek");
541 if (got_check_pack_hash(packfd, packsz, packid->sha1) == -1)
542 err = got_error(GOT_ERR_BAD_PACKFILE);
551 main(int argc, char **argv)
553 const struct got_error *err = NULL;
554 int fetchfd, packfd = -1;
555 struct got_object_id packid;
559 if(getenv("GOT_DEBUG") != NULL){
560 fprintf(stderr, "fetch-pack being chatty!\n");
564 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
565 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
566 if (err->code == GOT_ERR_PRIVSEP_PIPE)
570 if (imsg.hdr.type == GOT_IMSG_STOP)
572 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
573 err = got_error(GOT_ERR_PRIVSEP_MSG);
576 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
577 err = got_error(GOT_ERR_PRIVSEP_LEN);
582 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
583 if (err->code == GOT_ERR_PRIVSEP_PIPE)
587 if (imsg.hdr.type == GOT_IMSG_STOP)
589 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
590 err = got_error(GOT_ERR_PRIVSEP_MSG);
593 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
594 err = got_error(GOT_ERR_PRIVSEP_LEN);
599 err = fetch_pack(fetchfd, packfd, &packid, &ibuf);
603 if (packfd != -1 && close(packfd) == -1 && err == NULL)
604 err = got_error_from_errno("close");
606 got_privsep_send_error(&ibuf, err);
608 err = got_privsep_send_fetch_done(&ibuf, packid);
610 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
611 got_privsep_send_error(&ibuf, err);