Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/time.h>
22 #include <sys/stat.h>
24 #include <stdint.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <err.h>
37 #include "got_compat.h"
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_version.h"
42 #include "got_fetch.h"
43 #include "got_reference.h"
45 #include "got_lib_hash.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_pkt.h"
52 #include "got_lib_gitproto.h"
53 #include "got_lib_ratelimit.h"
54 #include "got_lib_poll.h"
56 #ifndef nitems
57 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 #endif
60 struct got_object *indexed;
61 static int chattygot;
63 static const struct got_capability got_capabilities[] = {
64 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
65 { GOT_CAPA_OFS_DELTA, NULL },
66 #if 0
67 { GOT_CAPA_SIDE_BAND_64K, NULL },
68 #endif
69 { GOT_CAPA_REPORT_STATUS, NULL },
70 { GOT_CAPA_DELETE_REFS, NULL },
71 };
73 static const struct got_error *
74 send_upload_progress(struct imsgbuf *ibuf, off_t bytes,
75 struct got_ratelimit *rl)
76 {
77 const struct got_error *err = NULL;
78 int elapsed = 0;
80 if (rl) {
81 err = got_ratelimit_check(&elapsed, rl);
82 if (err || !elapsed)
83 return err;
84 }
86 if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
87 &bytes, sizeof(bytes)) == -1)
88 return got_error_from_errno(
89 "imsg_compose SEND_UPLOAD_PROGRESS");
91 return got_privsep_flush_imsg(ibuf);
92 }
94 static const struct got_error *
95 send_pack_request(struct imsgbuf *ibuf)
96 {
97 if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
98 NULL, 0) == -1)
99 return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
100 return got_privsep_flush_imsg(ibuf);
103 static const struct got_error *
104 send_done(struct imsgbuf *ibuf)
106 if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
107 return got_error_from_errno("imsg_compose SEND_DONE");
108 return got_privsep_flush_imsg(ibuf);
111 static const struct got_error *
112 recv_packfd(int *packfd, struct imsgbuf *ibuf)
114 const struct got_error *err;
115 struct imsg imsg;
117 *packfd = -1;
119 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
120 if (err)
121 return err;
123 if (imsg.hdr.type == GOT_IMSG_STOP) {
124 err = got_error(GOT_ERR_CANCELLED);
125 goto done;
128 if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
129 err = got_error(GOT_ERR_PRIVSEP_MSG);
130 goto done;
133 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
134 err = got_error(GOT_ERR_PRIVSEP_LEN);
135 goto done;
138 *packfd = imsg_get_fd(&imsg);
139 done:
140 imsg_free(&imsg);
141 return err;
144 static const struct got_error *
145 send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
147 const struct got_error *err;
148 unsigned char buf[8192];
149 ssize_t r;
150 off_t wtotal = 0;
151 struct got_ratelimit rl;
153 if (lseek(packfd, 0L, SEEK_SET) == -1)
154 return got_error_from_errno("lseek");
156 got_ratelimit_init(&rl, 0, 500);
158 for (;;) {
159 r = read(packfd, buf, sizeof(buf));
160 if (r == -1)
161 return got_error_from_errno("read");
162 if (r == 0)
163 break;
164 err = got_poll_write_full(sendfd, buf, r);
165 if (err)
166 return NULL;
167 wtotal += r;
168 err = send_upload_progress(ibuf, wtotal, &rl);
169 if (err)
170 return err;
173 return send_upload_progress(ibuf, wtotal, NULL);
176 static const struct got_error *
177 send_error(const char *buf, size_t len)
179 static char msg[1024];
180 size_t i;
182 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
183 if (!isprint((unsigned char)buf[i]))
184 return got_error_msg(GOT_ERR_BAD_PACKET,
185 "non-printable error message received from server");
186 msg[i] = buf[i];
188 msg[i] = '\0';
189 return got_error_msg(GOT_ERR_SEND_FAILED, msg);
192 static const struct got_error *
193 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
194 const char *refname)
196 struct ibuf *wbuf;
197 size_t len, reflen = strlen(refname);
199 len = sizeof(struct got_imsg_send_remote_ref) + reflen;
200 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
201 return got_error(GOT_ERR_NO_SPACE);
203 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
204 if (wbuf == NULL)
205 return got_error_from_errno("imsg_create SEND_REMOTE_REF");
207 /* Keep in sync with struct got_imsg_send_remote_ref definition! */
208 if (imsg_add(wbuf, refid, sizeof(*refid)) == -1)
209 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
210 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
211 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
212 if (imsg_add(wbuf, refname, reflen) == -1)
213 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
215 imsg_close(ibuf, wbuf);
216 return got_privsep_flush_imsg(ibuf);
219 static const struct got_error *
220 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
221 struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
223 struct ibuf *wbuf;
224 size_t i, len, reflen, errmsglen = 0;
225 struct got_pathlist_entry *pe;
226 int ref_valid = 0;
227 char *eol, *sp;
228 const char *errmsg = "";
230 eol = strchr(refname, '\n');
231 if (eol == NULL) {
232 return got_error_msg(GOT_ERR_BAD_PACKET,
233 "unexpected message from server");
235 *eol = '\0';
237 sp = strchr(refname, ' ');
238 if (sp != NULL) {
239 *sp++ = '\0';
240 errmsg = sp;
241 errmsglen = strlen(errmsg);
243 for (i = 0; i < errmsglen; ++i) {
244 if (!isprint((unsigned char)errmsg[i])) {
245 return got_error_msg(GOT_ERR_BAD_PACKET,
246 "non-printable error message received "
247 "from the server");
252 reflen = strlen(refname);
253 if (!got_ref_name_is_valid(refname)) {
254 return got_error_msg(GOT_ERR_BAD_PACKET,
255 "unexpected message from server");
258 TAILQ_FOREACH(pe, refs, entry) {
259 if (strcmp(refname, pe->path) == 0) {
260 ref_valid = 1;
261 break;
264 if (!ref_valid) {
265 TAILQ_FOREACH(pe, delete_refs, entry) {
266 if (strcmp(refname, pe->path) == 0) {
267 ref_valid = 1;
268 break;
272 if (!ref_valid) {
273 return got_error_msg(GOT_ERR_BAD_PACKET,
274 "unexpected message from server");
277 len = sizeof(struct got_imsg_send_ref_status) + reflen + errmsglen;
278 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
279 return got_error(GOT_ERR_NO_SPACE);
281 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
282 0, 0, len);
283 if (wbuf == NULL)
284 return got_error_from_errno("imsg_create SEND_REF_STATUS");
286 /* Keep in sync with struct got_imsg_send_ref_status definition! */
287 if (imsg_add(wbuf, &success, sizeof(success)) == -1)
288 return got_error_from_errno("imsg_add SEND_REF_STATUS");
289 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
290 return got_error_from_errno("imsg_add SEND_REF_STATUS");
291 if (imsg_add(wbuf, &errmsglen, sizeof(errmsglen)) == -1)
292 return got_error_from_errno("imsg_add SEND_REF_STATUS");
293 if (imsg_add(wbuf, refname, reflen) == -1)
294 return got_error_from_errno("imsg_add SEND_REF_STATUS");
295 if (imsg_add(wbuf, errmsg, errmsglen) == -1)
296 return got_error_from_errno("imsg_add SEND_REF_STATUS");
298 imsg_close(ibuf, wbuf);
299 return got_privsep_flush_imsg(ibuf);
302 static const struct got_error *
303 describe_refchange(int *n, int *sent_my_capabilites,
304 const char *my_capabilities, char *buf, size_t bufsize,
305 const char *refname, const char *old_hashstr, const char *new_hashstr)
307 *n = snprintf(buf, bufsize, "%s %s %s",
308 old_hashstr, new_hashstr, refname);
309 if (*n < 0 || (size_t)*n >= bufsize)
310 return got_error(GOT_ERR_NO_SPACE);
312 /*
313 * We must announce our capabilities along with the first
314 * reference. Unfortunately, the protocol requires an embedded
315 * NUL as a separator between reference name and capabilities,
316 * which we have to deal with here.
317 * It also requires a linefeed for terminating packet data.
318 */
319 if (!*sent_my_capabilites && my_capabilities != NULL) {
320 int m;
321 if (*n >= bufsize - 1)
322 return got_error(GOT_ERR_NO_SPACE);
323 m = snprintf(buf + *n + 1, /* offset after '\0' */
324 bufsize - (*n + 1), "%s\n", my_capabilities);
325 if (m < 0 || *n + m >= bufsize)
326 return got_error(GOT_ERR_NO_SPACE);
327 *n += m;
328 *sent_my_capabilites = 1;
329 } else {
330 *n = strlcat(buf, "\n", bufsize);
331 if (*n >= bufsize)
332 return got_error(GOT_ERR_NO_SPACE);
335 return NULL;
338 static const struct got_error *
339 send_pack(int fd, struct got_pathlist_head *refs,
340 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
342 const struct got_error *err = NULL;
343 char buf[GOT_PKT_MAX];
344 const unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
345 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
346 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
347 struct got_pathlist_head their_refs;
348 int is_firstpkt = 1;
349 int n, nsent = 0;
350 int packfd = -1;
351 char *id_str = NULL, *refname = NULL;
352 struct got_object_id *id = NULL;
353 char *server_capabilities = NULL, *my_capabilities = NULL;
354 struct got_pathlist_entry *pe;
355 int sent_my_capabilites = 0;
357 TAILQ_INIT(&their_refs);
359 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
360 return got_error(GOT_ERR_SEND_EMPTY);
362 while (1) {
363 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
364 if (err)
365 goto done;
366 if (n == 0)
367 break;
368 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
369 err = send_error(&buf[4], n - 4);
370 goto done;
372 free(id_str);
373 free(refname);
374 err = got_gitproto_parse_refline(&id_str, &refname,
375 &server_capabilities, buf, n);
376 if (err)
377 goto done;
378 if (is_firstpkt) {
379 if (server_capabilities == NULL) {
380 server_capabilities = strdup("");
381 if (server_capabilities == NULL) {
382 err = got_error_from_errno("strdup");
383 goto done;
386 if (chattygot && server_capabilities[0] != '\0')
387 fprintf(stderr, "%s: server capabilities: %s\n",
388 getprogname(), server_capabilities);
389 err = got_gitproto_match_capabilities(&my_capabilities,
390 NULL, server_capabilities, got_capabilities,
391 nitems(got_capabilities));
392 if (err)
393 goto done;
394 if (chattygot)
395 fprintf(stderr, "%s: my capabilities:%s\n",
396 getprogname(),
397 my_capabilities ? my_capabilities : "");
398 is_firstpkt = 0;
400 if (strstr(refname, "^{}")) {
401 if (chattygot) {
402 fprintf(stderr, "%s: ignoring %s\n",
403 getprogname(), refname);
405 continue;
408 id = malloc(sizeof(*id));
409 if (id == NULL) {
410 err = got_error_from_errno("malloc");
411 goto done;
413 if (!got_parse_object_id(id, id_str, GOT_HASH_SHA1)) {
414 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
415 goto done;
417 err = send_their_ref(ibuf, id, refname);
418 if (err)
419 goto done;
421 err = got_pathlist_append(&their_refs, refname, id);
422 if (err)
423 goto done;
425 if (chattygot)
426 fprintf(stderr, "%s: remote has %s %s\n",
427 getprogname(), refname, id_str);
428 free(id_str);
429 id_str = NULL;
430 refname = NULL; /* do not free; owned by their_refs */
431 id = NULL; /* do not free; owned by their_refs */
434 if (!TAILQ_EMPTY(delete_refs)) {
435 if (my_capabilities == NULL ||
436 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
437 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
438 goto done;
442 TAILQ_FOREACH(pe, delete_refs, entry) {
443 const char *refname = pe->path;
444 struct got_pathlist_entry *their_pe;
445 struct got_object_id *their_id = NULL;
447 TAILQ_FOREACH(their_pe, &their_refs, entry) {
448 const char *their_refname = their_pe->path;
449 if (got_path_cmp(refname, their_refname,
450 strlen(refname), strlen(their_refname)) == 0) {
451 their_id = their_pe->data;
452 break;
455 if (their_id == NULL) {
456 err = got_error_fmt(GOT_ERR_NOT_REF,
457 "%s does not exist in remote repository",
458 refname);
459 goto done;
462 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
463 sizeof(old_hashstr));
464 got_sha1_digest_to_str(zero_id, new_hashstr,
465 sizeof(new_hashstr));
466 err = describe_refchange(&n, &sent_my_capabilites,
467 my_capabilities, buf, sizeof(buf), refname,
468 old_hashstr, new_hashstr);
469 if (err)
470 goto done;
471 err = got_pkt_writepkt(fd, buf, n, chattygot);
472 if (err)
473 goto done;
474 if (chattygot) {
475 fprintf(stderr, "%s: deleting %s %s\n",
476 getprogname(), refname, old_hashstr);
478 nsent++;
481 TAILQ_FOREACH(pe, refs, entry) {
482 const char *refname = pe->path;
483 struct got_object_id *id = pe->data;
484 struct got_object_id *their_id = NULL;
485 struct got_pathlist_entry *their_pe;
487 TAILQ_FOREACH(their_pe, &their_refs, entry) {
488 const char *their_refname = their_pe->path;
489 if (got_path_cmp(refname, their_refname,
490 strlen(refname), strlen(their_refname)) == 0) {
491 their_id = their_pe->data;
492 break;
495 if (their_id) {
496 if (got_object_id_cmp(id, their_id) == 0) {
497 if (chattygot) {
498 fprintf(stderr,
499 "%s: no change for %s\n",
500 getprogname(), refname);
502 continue;
504 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
505 sizeof(old_hashstr));
506 } else {
507 got_sha1_digest_to_str(zero_id, old_hashstr,
508 sizeof(old_hashstr));
510 got_sha1_digest_to_str(id->sha1, new_hashstr,
511 sizeof(new_hashstr));
512 err = describe_refchange(&n, &sent_my_capabilites,
513 my_capabilities, buf, sizeof(buf), refname,
514 old_hashstr, new_hashstr);
515 if (err)
516 goto done;
517 err = got_pkt_writepkt(fd, buf, n, chattygot);
518 if (err)
519 goto done;
520 if (chattygot) {
521 if (their_id) {
522 fprintf(stderr, "%s: updating %s %s -> %s\n",
523 getprogname(), refname, old_hashstr,
524 new_hashstr);
525 } else {
526 fprintf(stderr, "%s: creating %s %s\n",
527 getprogname(), refname, new_hashstr);
530 nsent++;
532 err = got_pkt_flushpkt(fd, chattygot);
533 if (err)
534 goto done;
536 err = send_pack_request(ibuf);
537 if (err)
538 goto done;
540 err = recv_packfd(&packfd, ibuf);
541 if (err)
542 goto done;
544 if (packfd != -1) {
545 err = send_pack_file(fd, packfd, ibuf);
546 if (err)
547 goto done;
550 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
551 if (err)
552 goto done;
553 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
554 err = send_error(&buf[4], n - 4);
555 goto done;
556 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
557 err = got_error_msg(GOT_ERR_BAD_PACKET,
558 "unexpected message from server");
559 goto done;
562 while (nsent > 0) {
563 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
564 if (err)
565 goto done;
566 if (n < 3) {
567 err = got_error_msg(GOT_ERR_BAD_PACKET,
568 "unexpected message from server");
569 goto done;
570 } else if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
571 err = send_error(&buf[4], n - 4);
572 goto done;
573 } else if (strncmp(buf, "ok ", 3) == 0) {
574 err = send_ref_status(ibuf, buf + 3, 1,
575 refs, delete_refs);
576 if (err)
577 goto done;
578 } else if (strncmp(buf, "ng ", 3) == 0) {
579 err = send_ref_status(ibuf, buf + 3, 0,
580 refs, delete_refs);
581 if (err)
582 goto done;
583 } else {
584 err = got_error_msg(GOT_ERR_BAD_PACKET,
585 "unexpected message from server");
586 goto done;
588 nsent--;
591 err = send_done(ibuf);
592 done:
593 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
594 free(id_str);
595 free(id);
596 free(refname);
597 free(server_capabilities);
598 return err;
601 int
602 main(int argc, char **argv)
604 const struct got_error *err = NULL;
605 int sendfd = -1;
606 struct imsgbuf ibuf;
607 struct imsg imsg;
608 struct got_pathlist_head refs;
609 struct got_pathlist_head delete_refs;
610 struct got_imsg_send_request send_req;
611 struct got_imsg_send_ref href;
612 size_t datalen, i;
613 #if 0
614 static int attached;
615 while (!attached)
616 sleep (1);
617 #endif
619 TAILQ_INIT(&refs);
620 TAILQ_INIT(&delete_refs);
622 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
623 #ifndef PROFILE
624 /* revoke access to most system calls */
625 if (pledge("stdio recvfd", NULL) == -1) {
626 err = got_error_from_errno("pledge");
627 got_privsep_send_error(&ibuf, err);
628 return 1;
631 /* revoke fs access */
632 if (landlock_no_fs() == -1) {
633 err = got_error_from_errno("landlock_no_fs");
634 got_privsep_send_error(&ibuf, err);
635 return 1;
637 if (cap_enter() == -1) {
638 err = got_error_from_errno("cap_enter");
639 got_privsep_send_error(&ibuf, err);
640 return 1;
642 #endif
643 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
644 if (err->code == GOT_ERR_PRIVSEP_PIPE)
645 err = NULL;
646 goto done;
648 if (imsg.hdr.type == GOT_IMSG_STOP)
649 goto done;
650 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
651 err = got_error(GOT_ERR_PRIVSEP_MSG);
652 goto done;
654 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
655 if (datalen < sizeof(send_req)) {
656 err = got_error(GOT_ERR_PRIVSEP_LEN);
657 goto done;
659 memcpy(&send_req, imsg.data, sizeof(send_req));
660 sendfd = imsg_get_fd(&imsg);
661 imsg_free(&imsg);
663 if (send_req.verbosity > 0)
664 chattygot += send_req.verbosity;
666 for (i = 0; i < send_req.nrefs; i++) {
667 struct got_object_id *id;
668 char *refname;
670 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
671 if (err->code == GOT_ERR_PRIVSEP_PIPE)
672 err = NULL;
673 goto done;
675 if (imsg.hdr.type == GOT_IMSG_STOP)
676 goto done;
677 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
678 err = got_error(GOT_ERR_PRIVSEP_MSG);
679 goto done;
681 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
682 if (datalen < sizeof(href)) {
683 err = got_error(GOT_ERR_PRIVSEP_LEN);
684 goto done;
686 memcpy(&href, imsg.data, sizeof(href));
687 if (datalen - sizeof(href) < href.name_len) {
688 err = got_error(GOT_ERR_PRIVSEP_LEN);
689 goto done;
691 refname = malloc(href.name_len + 1);
692 if (refname == NULL) {
693 err = got_error_from_errno("malloc");
694 goto done;
696 memcpy(refname, imsg.data + sizeof(href), href.name_len);
697 refname[href.name_len] = '\0';
699 /*
700 * Prevent sending of references that won't make any
701 * sense outside the local repository's context.
702 */
703 if (strncmp(refname, "refs/got/", 9) == 0 ||
704 strncmp(refname, "refs/remotes/", 13) == 0) {
705 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
706 "%s", refname);
707 goto done;
710 id = malloc(sizeof(*id));
711 if (id == NULL) {
712 free(refname);
713 err = got_error_from_errno("malloc");
714 goto done;
716 memcpy(id, &href.id, sizeof(*id));
717 if (href.delete)
718 err = got_pathlist_append(&delete_refs, refname, id);
719 else
720 err = got_pathlist_append(&refs, refname, id);
721 if (err) {
722 free(refname);
723 free(id);
724 goto done;
727 imsg_free(&imsg);
730 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
731 done:
732 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
733 got_pathlist_free(&delete_refs, GOT_PATHLIST_FREE_ALL);
734 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
735 err = got_error_from_errno("close");
736 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
737 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
738 got_privsep_send_error(&ibuf, err);
741 exit(0);