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"
40 #include "got_path.h"
41 #include "got_version.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 #define GOT_PKTMAX 65536
56 struct got_object *indexed;
57 static int chattygit;
58 static char *fetchbranch;
59 static struct got_object_id zhash = {.sha1={0}};
61 static const struct got_error *
62 readn(ssize_t *off, int fd, void *buf, size_t n)
63 {
64 ssize_t r;
66 *off = 0;
67 while (*off != n) {
68 r = read(fd, buf + *off, n - *off);
69 if (r == -1)
70 return got_error_from_errno("read");
71 if (r == 0)
72 return NULL;
73 *off += r;
74 }
75 return NULL;
76 }
78 static const struct got_error *
79 flushpkt(int fd)
80 {
81 ssize_t w;
83 if (chattygit)
84 fprintf(stderr, "writepkt: 0000\n");
86 w = write(fd, "0000", 4);
87 if (w == -1)
88 return got_error_from_errno("write");
89 if (w != 4)
90 return got_error(GOT_ERR_IO);
91 return NULL;
92 }
94 /*
95 * Packet header contains a 4-byte hexstring which specifies the length
96 * of data which follows.
97 */
98 static const struct got_error *
99 read_pkthdr(int *datalen, int fd)
101 static const struct got_error *err = NULL;
102 char lenstr[5];
103 long len;
104 char *e;
105 int n, i;
106 ssize_t r;
108 *datalen = 0;
110 err = readn(&r, fd, lenstr, 4);
111 if (err)
112 return err;
113 if (r == 0) /* implicit "0000" */
114 return NULL;
115 if (r != 4)
116 return got_error_msg(GOT_ERR_BAD_PACKET,
117 "wrong packet header length");
119 lenstr[4] = '\0';
120 for (i = 0; i < 4; i++) {
121 if (!isxdigit(lenstr[i]))
122 return got_error_msg(GOT_ERR_BAD_PACKET,
123 "packet length not specified in hex");
125 errno = 0;
126 len = strtol(lenstr, &e, 16);
127 if (lenstr[0] == '\0' || *e != '\0')
128 return got_error(GOT_ERR_BAD_PACKET);
129 if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
130 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
131 if (len > INT_MAX || len < INT_MIN)
132 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
133 n = len;
134 if (n == 0)
135 return NULL;
136 if (n <= 4)
137 return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
138 n -= 4;
140 *datalen = n;
141 return NULL;
144 static const struct got_error *
145 readpkt(int *outlen, int fd, char *buf, int buflen)
147 const struct got_error *err = NULL;
148 int datalen;
149 ssize_t n;
151 err = read_pkthdr(&datalen, fd);
152 if (err)
153 return err;
155 if (datalen > buflen)
156 return got_error(GOT_ERR_NO_SPACE);
158 err = readn(&n, fd, buf, datalen);
159 if (err)
160 return err;
161 if (n != datalen)
162 return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
164 *outlen = n;
165 return NULL;
168 static const struct got_error *
169 writepkt(int fd, char *buf, int nbuf)
171 char len[5];
172 int i;
173 ssize_t w;
175 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
176 return got_error(GOT_ERR_NO_SPACE);
177 w = write(fd, len, 4);
178 if (w == -1)
179 return got_error_from_errno("write");
180 if (w != 4)
181 return got_error(GOT_ERR_IO);
182 w = write(fd, buf, nbuf);
183 if (w == -1)
184 return got_error_from_errno("write");
185 if (w != nbuf)
186 return got_error(GOT_ERR_IO);
187 if (chattygit) {
188 fprintf(stderr, "writepkt: %s:\t", len);
189 fwrite(buf, 1, nbuf, stderr);
190 for (i = 0; i < nbuf; i++) {
191 if (isprint(buf[i]))
192 fputc(buf[i], stderr);
194 fputc('\n', stderr);
196 return NULL;
199 static const struct got_error *
200 match_remote_ref(struct got_pathlist_head *have_refs, struct got_object_id *id,
201 char *refname, char *id_str)
203 struct got_pathlist_entry *pe;
205 memset(id, 0, sizeof(*id));
207 TAILQ_FOREACH(pe, have_refs, entry) {
208 if (strcmp(pe->path, refname) == 0) {
209 if (!got_parse_sha1_digest(id->sha1, id_str))
210 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
211 break;
214 return NULL;
217 static int
218 match_branch(char *br, char *pat)
220 char name[128];
222 if (strstr(pat, "refs/heads") == pat) {
223 if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
224 return -1;
225 } else if (strstr(pat, "heads")) {
226 if (snprintf(name, sizeof(name), "refs/%s", pat)
227 >= sizeof(name))
228 return -1;
229 } else {
230 if (snprintf(name, sizeof(name), "refs/heads/%s", pat)
231 >= sizeof(name))
232 return -1;
234 return strcmp(br, name) == 0;
237 static const struct got_error *
238 tokenize_refline(char **tokens, char *line, int len, int maxtokens)
240 const struct got_error *err = NULL;
241 char *p;
242 size_t i, n = 0;
244 for (i = 0; i < maxtokens; i++)
245 tokens[i] = NULL;
247 for (i = 0; n < len && i < maxtokens; i++) {
248 while (isspace(*line)) {
249 line++;
250 n++;
252 p = line;
253 while (*line != '\0' &&
254 (!isspace(*line) || i == maxtokens - 1)) {
255 line++;
256 n++;
258 tokens[i] = strndup(p, line - p);
259 if (tokens[i] == NULL) {
260 err = got_error_from_errno("strndup");
261 goto done;
263 /* Skip \0 field-delimiter at end of token. */
264 while (line[0] == '\0' && n < len) {
265 line++;
266 n++;
269 if (i <= 2)
270 err = got_error(GOT_ERR_NOT_REF);
271 done:
272 if (err) {
273 int j;
274 for (j = 0; j < i; j++)
275 free(tokens[j]);
276 tokens[j] = NULL;
278 return err;
281 static const struct got_error *
282 parse_refline(char **id_str, char **refname, char **server_capabilities,
283 char *line, int len)
285 const struct got_error *err = NULL;
286 char *tokens[3];
288 err = tokenize_refline(tokens, line, len, nitems(tokens));
289 if (err)
290 return err;
292 if (tokens[0])
293 *id_str = tokens[0];
294 if (tokens[1])
295 *refname = tokens[1];
296 if (tokens[2])
297 *server_capabilities = tokens[2];
299 return NULL;
302 #define GOT_CAPA_AGENT "agent"
303 #define GOT_CAPA_OFS_DELTA "ofs-delta"
304 #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
306 #define GOT_SIDEBAND_PACKFILE_DATA 1
307 #define GOT_SIDEBAND_PROGRESS_INFO 2
308 #define GOT_SIDEBAND_ERROR_INFO 3
311 struct got_capability {
312 const char *key;
313 const char *value;
314 };
315 static const struct got_capability got_capabilities[] = {
316 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
317 { GOT_CAPA_OFS_DELTA, NULL },
318 { GOT_CAPA_SIDE_BAND_64K, NULL },
319 };
321 static const struct got_error *
322 match_capability(char **my_capabilities, const char *capa,
323 const struct got_capability *mycapa)
325 char *equalsign;
326 char *s;
328 equalsign = strchr(capa, '=');
329 if (equalsign) {
330 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
331 return NULL;
332 } else {
333 if (strcmp(capa, mycapa->key) != 0)
334 return NULL;
337 if (asprintf(&s, "%s%s%s%s%s",
338 *my_capabilities != NULL ? *my_capabilities : "",
339 *my_capabilities != NULL ? " " : "",
340 mycapa->key,
341 mycapa->value != NULL ? "=" : "",
342 mycapa->value != NULL? mycapa->value : "") == -1)
343 return got_error_from_errno("asprintf");
345 free(*my_capabilities);
346 *my_capabilities = s;
347 return NULL;
350 static const struct got_error *
351 add_symref(struct got_pathlist_head *symrefs, char *capa)
353 const struct got_error *err = NULL;
354 char *colon, *name = NULL, *target = NULL;
356 /* Need at least "A:B" */
357 if (strlen(capa) < 3)
358 return NULL;
360 colon = strchr(capa, ':');
361 if (colon == NULL)
362 return NULL;
364 *colon = '\0';
365 name = strdup(capa);
366 if (name == NULL)
367 return got_error_from_errno("strdup");
369 target = strdup(colon + 1);
370 if (target == NULL) {
371 err = got_error_from_errno("strdup");
372 goto done;
375 /* We can't validate the ref itself here. The main process will. */
376 err = got_pathlist_append(symrefs, name, target);
377 done:
378 if (err) {
379 free(name);
380 free(target);
382 return err;
385 static const struct got_error *
386 match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
387 char *server_capabilities)
389 const struct got_error *err = NULL;
390 char *capa, *equalsign;
391 int i;
393 *my_capabilities = NULL;
394 do {
395 capa = strsep(&server_capabilities, " ");
396 if (capa == NULL)
397 return NULL;
399 equalsign = strchr(capa, '=');
400 if (equalsign != NULL &&
401 strncmp(capa, "symref", equalsign - capa) == 0) {
402 err = add_symref(symrefs, equalsign + 1);
403 if (err)
404 break;
405 continue;
408 for (i = 0; i < nitems(got_capabilities); i++) {
409 err = match_capability(my_capabilities,
410 capa, &got_capabilities[i]);
411 if (err)
412 break;
414 } while (capa);
416 return err;
419 static const struct got_error *
420 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
422 int i;
424 if (len == 0)
425 return NULL;
427 /*
428 * Truncate messages which exceed the maximum imsg payload size.
429 * Server may send up to 64k.
430 */
431 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
432 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
434 /* Only allow printable ASCII. */
435 for (i = 0; i < len; i++) {
436 if (isprint((unsigned char)buf[i]) ||
437 isspace((unsigned char)buf[i]))
438 continue;
439 return got_error_msg(GOT_ERR_BAD_PACKET,
440 "non-printable progress message received from server");
443 return got_privsep_send_fetch_server_progress(ibuf, buf, len);
446 static const struct got_error *
447 fetch_error(const char *buf, size_t len)
449 static char msg[1024];
450 int i;
452 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
453 if (!isprint(buf[i]))
454 return got_error_msg(GOT_ERR_BAD_PACKET,
455 "non-printable error message received from server");
456 msg[i] = buf[i];
458 msg[i] = '\0';
459 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
462 static const struct got_error *
463 fetch_pack(int fd, int packfd, struct got_object_id *packid,
464 struct got_pathlist_head *have_refs, struct imsgbuf *ibuf)
466 const struct got_error *err = NULL;
467 char buf[GOT_PKTMAX];
468 char hashstr[SHA1_DIGEST_STRING_LENGTH];
469 struct got_object_id *have, *want;
470 int is_firstpkt = 1, nref = 0, refsz = 16;
471 int i, n, req;
472 off_t packsz = 0, last_reported_packsz = 0;
473 char *id_str = NULL, *refname = NULL;
474 char *server_capabilities = NULL, *my_capabilities = NULL;
475 struct got_pathlist_head symrefs;
476 struct got_pathlist_entry *pe;
477 int have_sidebands = 0;
479 TAILQ_INIT(&symrefs);
481 have = malloc(refsz * sizeof(have[0]));
482 if (have == NULL)
483 return got_error_from_errno("malloc");
484 want = malloc(refsz * sizeof(want[0]));
485 if (want == NULL) {
486 err = got_error_from_errno("malloc");
487 goto done;
489 if (chattygit)
490 fprintf(stderr, "starting fetch\n");
491 while (1) {
492 err = readpkt(&n, fd, buf, sizeof(buf));
493 if (err)
494 goto done;
495 if (n == 0)
496 break;
497 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
498 err = fetch_error(&buf[4], n - 4);
499 goto done;
501 err = parse_refline(&id_str, &refname, &server_capabilities,
502 buf, n);
503 if (err)
504 goto done;
505 if (chattygit && server_capabilities[0] != '\0')
506 fprintf(stderr, "server capabilities: %s\n",
507 server_capabilities);
508 if (is_firstpkt) {
509 err = match_capabilities(&my_capabilities, &symrefs,
510 server_capabilities);
511 if (err)
512 goto done;
513 if (chattygit && my_capabilities)
514 fprintf(stderr, "my matched capabilities: %s\n",
515 my_capabilities);
516 err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
517 if (err)
518 goto done;
520 is_firstpkt = 0;
521 if (strstr(refname, "^{}"))
522 continue;
523 if (fetchbranch && !match_branch(refname, fetchbranch))
524 continue;
525 if (refsz == nref + 1) {
526 refsz *= 2;
527 have = reallocarray(have, refsz, sizeof(have[0]));
528 if (have == NULL) {
529 err = got_error_from_errno("reallocarray");
530 goto done;
532 want = reallocarray(want, refsz, sizeof(want[0]));
533 if (want == NULL) {
534 err = got_error_from_errno("reallocarray");
535 goto done;
538 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
539 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
540 goto done;
543 err = match_remote_ref(have_refs, &have[nref], id_str, refname);
544 if (err)
545 goto done;
547 err = got_privsep_send_fetch_ref(ibuf, &want[nref],
548 refname);
549 if (err)
550 goto done;
551 if (chattygit)
552 fprintf(stderr, "remote %s\n", refname);
553 nref++;
556 req = 0;
557 for (i = 0; i < nref; i++) {
558 if (got_object_id_cmp(&have[i], &want[i]) == 0)
559 continue;
560 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
561 n = snprintf(buf, sizeof(buf), "want %s%s%s\n", hashstr,
562 i == 0 && my_capabilities ? " " : "",
563 i == 0 && my_capabilities ? my_capabilities : "");
564 if (n >= sizeof(buf)) {
565 err = got_error(GOT_ERR_NO_SPACE);
566 goto done;
568 err = writepkt(fd, buf, n);
569 if (err)
570 goto done;
571 req = 1;
573 err = flushpkt(fd);
574 if (err)
575 goto done;
576 for (i = 0; i < nref; i++) {
577 if (got_object_id_cmp(&have[i], &zhash) == 0)
578 continue;
579 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
580 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
581 if (n >= sizeof(buf)) {
582 err = got_error(GOT_ERR_NO_SPACE);
583 goto done;
585 err = writepkt(fd, buf, n + 1);
586 if (err)
587 goto done;
589 if (!req) {
590 if (chattygit)
591 fprintf(stderr, "up to date\n");
592 err = flushpkt(fd);
593 if (err)
594 goto done;
596 n = snprintf(buf, sizeof(buf), "done\n");
597 err = writepkt(fd, buf, n);
598 if (err)
599 goto done;
600 if (!req)
601 return 0;
603 err = readpkt(&n, fd, buf, sizeof(buf));
604 if (err)
605 goto done;
606 /*
607 * For now, we only support a full clone, in which case the server
608 * will now send a "NAK" (meaning no common objects were found).
609 */
610 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
611 err = got_error_msg(GOT_ERR_BAD_PACKET,
612 "unexpected message from server");
613 goto done;
616 if (chattygit)
617 fprintf(stderr, "fetching...\n");
619 if (my_capabilities != NULL &&
620 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
621 have_sidebands = 1;
623 while (1) {
624 ssize_t r = 0, w;
625 int datalen = -1;
627 if (have_sidebands) {
628 err = read_pkthdr(&datalen, fd);
629 if (err)
630 goto done;
631 if (datalen <= 0)
632 break;
634 /* Read sideband channel ID (one byte). */
635 r = read(fd, buf, 1);
636 if (r == -1) {
637 err = got_error_from_errno("read");
638 goto done;
640 if (r != 1) {
641 err = got_error_msg(GOT_ERR_BAD_PACKET,
642 "short packet");
643 goto done;
645 if (datalen > sizeof(buf) - 5) {
646 err = got_error_msg(GOT_ERR_BAD_PACKET,
647 "bad packet length");
648 goto done;
650 datalen--; /* sideband ID has been read */
651 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
652 /* Read packfile data. */
653 err = readn(&r, fd, buf, datalen);
654 if (err)
655 goto done;
656 if (r != datalen) {
657 err = got_error_msg(GOT_ERR_BAD_PACKET,
658 "packet too short");
659 goto done;
661 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
662 err = readn(&r, fd, buf, datalen);
663 if (err)
664 goto done;
665 if (r != datalen) {
666 err = got_error_msg(GOT_ERR_BAD_PACKET,
667 "packet too short");
668 goto done;
670 err = fetch_progress(ibuf, buf, r);
671 if (err)
672 goto done;
673 continue;
674 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
675 err = readn(&r, fd, buf, datalen);
676 if (err)
677 goto done;
678 if (r != datalen) {
679 err = got_error_msg(GOT_ERR_BAD_PACKET,
680 "packet too short");
681 goto done;
683 err = fetch_error(buf, r);
684 goto done;
685 } else {
686 err = got_error_msg(GOT_ERR_BAD_PACKET,
687 "unknown side-band received from server");
688 goto done;
690 } else {
691 /* No sideband channel. Every byte is packfile data. */
692 err = readn(&r, fd, buf, sizeof buf);
693 if (err)
694 goto done;
695 if (r <= 0)
696 break;
699 /* Write packfile data to temporary pack file. */
700 w = write(packfd, buf, r);
701 if (w == -1) {
702 err = got_error_from_errno("write");
703 goto done;
705 if (w != r) {
706 err = got_error(GOT_ERR_IO);
707 goto done;
709 packsz += w;
711 /* Don't send too many progress privsep messages. */
712 if (packsz > last_reported_packsz + 1024) {
713 err = got_privsep_send_fetch_download_progress(ibuf,
714 packsz);
715 if (err)
716 goto done;
717 last_reported_packsz = packsz;
720 err = got_privsep_send_fetch_download_progress(ibuf, packsz);
721 if (err)
722 goto done;
723 done:
724 TAILQ_FOREACH(pe, &symrefs, entry) {
725 free((void *)pe->path);
726 free(pe->data);
728 got_pathlist_free(&symrefs);
729 free(have);
730 free(want);
731 free(id_str);
732 free(refname);
733 free(server_capabilities);
734 return err;
738 int
739 main(int argc, char **argv)
741 const struct got_error *err = NULL;
742 int fetchfd, packfd = -1;
743 struct got_object_id packid;
744 struct imsgbuf ibuf;
745 struct imsg imsg;
746 struct got_pathlist_head have_refs;
747 struct got_imsg_fetch_have_refs *fetch_have_refs = NULL;
748 size_t datalen;
750 TAILQ_INIT(&have_refs);
752 if (getenv("GOT_DEBUG") != NULL) {
753 fprintf(stderr, "fetch-pack being chatty!\n");
754 chattygit = 1;
757 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
758 #ifndef PROFILE
759 /* revoke access to most system calls */
760 if (pledge("stdio recvfd", NULL) == -1) {
761 err = got_error_from_errno("pledge");
762 got_privsep_send_error(&ibuf, err);
763 return 1;
765 #endif
766 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
767 if (err->code == GOT_ERR_PRIVSEP_PIPE)
768 err = NULL;
769 goto done;
771 if (imsg.hdr.type == GOT_IMSG_STOP)
772 goto done;
773 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
774 err = got_error(GOT_ERR_PRIVSEP_MSG);
775 goto done;
777 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
778 if (datalen < sizeof(struct got_imsg_fetch_have_refs)) {
779 err = got_error(GOT_ERR_PRIVSEP_LEN);
780 goto done;
782 fetch_have_refs = (struct got_imsg_fetch_have_refs *)imsg.data;
783 if (datalen != sizeof(struct got_imsg_fetch_have_refs) +
784 sizeof(struct got_imsg_fetch_have_ref) *
785 fetch_have_refs->n_have_refs) {
786 err = got_error(GOT_ERR_PRIVSEP_LEN);
787 goto done;
789 if (fetch_have_refs->n_have_refs != 0) {
790 /* TODO: Incremental fetch support */
791 err = got_error(GOT_ERR_NOT_IMPL);
792 goto done;
794 fetchfd = imsg.fd;
796 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
797 if (err->code == GOT_ERR_PRIVSEP_PIPE)
798 err = NULL;
799 goto done;
801 if (imsg.hdr.type == GOT_IMSG_STOP)
802 goto done;
803 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
804 err = got_error(GOT_ERR_PRIVSEP_MSG);
805 goto done;
807 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
808 err = got_error(GOT_ERR_PRIVSEP_LEN);
809 goto done;
811 packfd = imsg.fd;
813 err = fetch_pack(fetchfd, packfd, &packid, &have_refs, &ibuf);
814 done:
815 if (packfd != -1 && close(packfd) == -1 && err == NULL)
816 err = got_error_from_errno("close");
817 if (err != NULL)
818 got_privsep_send_error(&ibuf, err);
819 else
820 err = got_privsep_send_fetch_done(&ibuf, packid);
821 if (err != NULL) {
822 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
823 got_privsep_send_error(&ibuf, err);
826 exit(0);