Make U+FE0F VARIATION SELECTOR-16 change the width from 1 to 2. GitHub
[tmux-openbsd.git] / file.c
blob280f35479721526a483c36799ac549b61a7e9cb2
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <imsg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "tmux.h"
34 * IPC file handling. Both client and server use the same data structures
35 * (client_file and client_files) to store list of active files. Most functions
36 * are for use either in client or server but not both.
39 static int file_next_stream = 3;
41 RB_GENERATE(client_files, client_file, entry, file_cmp);
43 /* Get path for file, either as given or from working directory. */
44 static char *
45 file_get_path(struct client *c, const char *file)
47 char *path;
49 if (*file == '/')
50 path = xstrdup(file);
51 else
52 xasprintf(&path, "%s/%s", server_client_get_cwd(c, NULL), file);
53 return (path);
56 /* Tree comparison function. */
57 int
58 file_cmp(struct client_file *cf1, struct client_file *cf2)
60 if (cf1->stream < cf2->stream)
61 return (-1);
62 if (cf1->stream > cf2->stream)
63 return (1);
64 return (0);
68 * Create a file object in the client process - the peer is the server to send
69 * messages to. Check callback is fired when the file is finished with so the
70 * process can decide if it needs to exit (if it is waiting for files to
71 * flush).
73 struct client_file *
74 file_create_with_peer(struct tmuxpeer *peer, struct client_files *files,
75 int stream, client_file_cb cb, void *cbdata)
77 struct client_file *cf;
79 cf = xcalloc(1, sizeof *cf);
80 cf->c = NULL;
81 cf->references = 1;
82 cf->stream = stream;
84 cf->buffer = evbuffer_new();
85 if (cf->buffer == NULL)
86 fatalx("out of memory");
88 cf->cb = cb;
89 cf->data = cbdata;
91 cf->peer = peer;
92 cf->tree = files;
93 RB_INSERT(client_files, files, cf);
95 return (cf);
98 /* Create a file object in the server, communicating with the given client. */
99 struct client_file *
100 file_create_with_client(struct client *c, int stream, client_file_cb cb,
101 void *cbdata)
103 struct client_file *cf;
105 if (c != NULL && (c->flags & CLIENT_ATTACHED))
106 c = NULL;
108 cf = xcalloc(1, sizeof *cf);
109 cf->c = c;
110 cf->references = 1;
111 cf->stream = stream;
113 cf->buffer = evbuffer_new();
114 if (cf->buffer == NULL)
115 fatalx("out of memory");
117 cf->cb = cb;
118 cf->data = cbdata;
120 if (cf->c != NULL) {
121 cf->peer = cf->c->peer;
122 cf->tree = &cf->c->files;
123 RB_INSERT(client_files, &cf->c->files, cf);
124 cf->c->references++;
127 return (cf);
130 /* Free a file. */
131 void
132 file_free(struct client_file *cf)
134 if (--cf->references != 0)
135 return;
137 evbuffer_free(cf->buffer);
138 free(cf->path);
140 if (cf->tree != NULL)
141 RB_REMOVE(client_files, cf->tree, cf);
142 if (cf->c != NULL)
143 server_client_unref(cf->c);
145 free(cf);
148 /* Event to fire the done callback. */
149 static void
150 file_fire_done_cb(__unused int fd, __unused short events, void *arg)
152 struct client_file *cf = arg;
153 struct client *c = cf->c;
155 if (cf->cb != NULL && (c == NULL || (~c->flags & CLIENT_DEAD)))
156 cf->cb(c, cf->path, cf->error, 1, cf->buffer, cf->data);
157 file_free(cf);
160 /* Add an event to fire the done callback (used by the server). */
161 void
162 file_fire_done(struct client_file *cf)
164 event_once(-1, EV_TIMEOUT, file_fire_done_cb, cf, NULL);
167 /* Fire the read callback. */
168 void
169 file_fire_read(struct client_file *cf)
171 if (cf->cb != NULL)
172 cf->cb(cf->c, cf->path, cf->error, 0, cf->buffer, cf->data);
175 /* Can this file be printed to? */
177 file_can_print(struct client *c)
179 if (c == NULL)
180 return (0);
181 if (c->session != NULL && (~c->flags & CLIENT_CONTROL))
182 return (0);
183 return (1);
186 /* Print a message to a file. */
187 void
188 file_print(struct client *c, const char *fmt, ...)
190 va_list ap;
192 va_start(ap, fmt);
193 file_vprint(c, fmt, ap);
194 va_end(ap);
197 /* Print a message to a file. */
198 void
199 file_vprint(struct client *c, const char *fmt, va_list ap)
201 struct client_file find, *cf;
202 struct msg_write_open msg;
204 if (!file_can_print(c))
205 return;
207 find.stream = 1;
208 if ((cf = RB_FIND(client_files, &c->files, &find)) == NULL) {
209 cf = file_create_with_client(c, 1, NULL, NULL);
210 cf->path = xstrdup("-");
212 evbuffer_add_vprintf(cf->buffer, fmt, ap);
214 msg.stream = 1;
215 msg.fd = STDOUT_FILENO;
216 msg.flags = 0;
217 proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg);
218 } else {
219 evbuffer_add_vprintf(cf->buffer, fmt, ap);
220 file_push(cf);
224 /* Print a buffer to a file. */
225 void
226 file_print_buffer(struct client *c, void *data, size_t size)
228 struct client_file find, *cf;
229 struct msg_write_open msg;
231 if (!file_can_print(c))
232 return;
234 find.stream = 1;
235 if ((cf = RB_FIND(client_files, &c->files, &find)) == NULL) {
236 cf = file_create_with_client(c, 1, NULL, NULL);
237 cf->path = xstrdup("-");
239 evbuffer_add(cf->buffer, data, size);
241 msg.stream = 1;
242 msg.fd = STDOUT_FILENO;
243 msg.flags = 0;
244 proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg);
245 } else {
246 evbuffer_add(cf->buffer, data, size);
247 file_push(cf);
251 /* Report an error to a file. */
252 void
253 file_error(struct client *c, const char *fmt, ...)
255 struct client_file find, *cf;
256 struct msg_write_open msg;
257 va_list ap;
259 if (!file_can_print(c))
260 return;
262 va_start(ap, fmt);
264 find.stream = 2;
265 if ((cf = RB_FIND(client_files, &c->files, &find)) == NULL) {
266 cf = file_create_with_client(c, 2, NULL, NULL);
267 cf->path = xstrdup("-");
269 evbuffer_add_vprintf(cf->buffer, fmt, ap);
271 msg.stream = 2;
272 msg.fd = STDERR_FILENO;
273 msg.flags = 0;
274 proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg);
275 } else {
276 evbuffer_add_vprintf(cf->buffer, fmt, ap);
277 file_push(cf);
280 va_end(ap);
283 /* Write data to a file. */
284 void
285 file_write(struct client *c, const char *path, int flags, const void *bdata,
286 size_t bsize, client_file_cb cb, void *cbdata)
288 struct client_file *cf;
289 struct msg_write_open *msg;
290 size_t msglen;
291 int fd = -1;
292 u_int stream = file_next_stream++;
293 FILE *f;
294 const char *mode;
296 if (strcmp(path, "-") == 0) {
297 cf = file_create_with_client(c, stream, cb, cbdata);
298 cf->path = xstrdup("-");
300 fd = STDOUT_FILENO;
301 if (c == NULL ||
302 (c->flags & CLIENT_ATTACHED) ||
303 (c->flags & CLIENT_CONTROL)) {
304 cf->error = EBADF;
305 goto done;
307 goto skip;
310 cf = file_create_with_client(c, stream, cb, cbdata);
311 cf->path = file_get_path(c, path);
313 if (c == NULL || c->flags & CLIENT_ATTACHED) {
314 if (flags & O_APPEND)
315 mode = "ab";
316 else
317 mode = "wb";
318 f = fopen(cf->path, mode);
319 if (f == NULL) {
320 cf->error = errno;
321 goto done;
323 if (fwrite(bdata, 1, bsize, f) != bsize) {
324 fclose(f);
325 cf->error = EIO;
326 goto done;
328 fclose(f);
329 goto done;
332 skip:
333 evbuffer_add(cf->buffer, bdata, bsize);
335 msglen = strlen(cf->path) + 1 + sizeof *msg;
336 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
337 cf->error = E2BIG;
338 goto done;
340 msg = xmalloc(msglen);
341 msg->stream = cf->stream;
342 msg->fd = fd;
343 msg->flags = flags;
344 memcpy(msg + 1, cf->path, msglen - sizeof *msg);
345 if (proc_send(cf->peer, MSG_WRITE_OPEN, -1, msg, msglen) != 0) {
346 free(msg);
347 cf->error = EINVAL;
348 goto done;
350 free(msg);
351 return;
353 done:
354 file_fire_done(cf);
357 /* Read a file. */
358 void
359 file_read(struct client *c, const char *path, client_file_cb cb, void *cbdata)
361 struct client_file *cf;
362 struct msg_read_open *msg;
363 size_t msglen;
364 int fd = -1;
365 u_int stream = file_next_stream++;
366 FILE *f;
367 size_t size;
368 char buffer[BUFSIZ];
370 if (strcmp(path, "-") == 0) {
371 cf = file_create_with_client(c, stream, cb, cbdata);
372 cf->path = xstrdup("-");
374 fd = STDIN_FILENO;
375 if (c == NULL ||
376 (c->flags & CLIENT_ATTACHED) ||
377 (c->flags & CLIENT_CONTROL)) {
378 cf->error = EBADF;
379 goto done;
381 goto skip;
384 cf = file_create_with_client(c, stream, cb, cbdata);
385 cf->path = file_get_path(c, path);
387 if (c == NULL || c->flags & CLIENT_ATTACHED) {
388 f = fopen(cf->path, "rb");
389 if (f == NULL) {
390 cf->error = errno;
391 goto done;
393 for (;;) {
394 size = fread(buffer, 1, sizeof buffer, f);
395 if (evbuffer_add(cf->buffer, buffer, size) != 0) {
396 cf->error = ENOMEM;
397 goto done;
399 if (size != sizeof buffer)
400 break;
402 if (ferror(f)) {
403 cf->error = EIO;
404 goto done;
406 fclose(f);
407 goto done;
410 skip:
411 msglen = strlen(cf->path) + 1 + sizeof *msg;
412 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
413 cf->error = E2BIG;
414 goto done;
416 msg = xmalloc(msglen);
417 msg->stream = cf->stream;
418 msg->fd = fd;
419 memcpy(msg + 1, cf->path, msglen - sizeof *msg);
420 if (proc_send(cf->peer, MSG_READ_OPEN, -1, msg, msglen) != 0) {
421 free(msg);
422 cf->error = EINVAL;
423 goto done;
425 free(msg);
426 return;
428 done:
429 file_fire_done(cf);
432 /* Push event, fired if there is more writing to be done. */
433 static void
434 file_push_cb(__unused int fd, __unused short events, void *arg)
436 struct client_file *cf = arg;
438 if (cf->c == NULL || ~cf->c->flags & CLIENT_DEAD)
439 file_push(cf);
440 file_free(cf);
443 /* Push uwritten data to the client for a file, if it will accept it. */
444 void
445 file_push(struct client_file *cf)
447 struct msg_write_data *msg;
448 size_t msglen, sent, left;
449 struct msg_write_close close;
451 msg = xmalloc(sizeof *msg);
452 left = EVBUFFER_LENGTH(cf->buffer);
453 while (left != 0) {
454 sent = left;
455 if (sent > MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg)
456 sent = MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg;
458 msglen = (sizeof *msg) + sent;
459 msg = xrealloc(msg, msglen);
460 msg->stream = cf->stream;
461 memcpy(msg + 1, EVBUFFER_DATA(cf->buffer), sent);
462 if (proc_send(cf->peer, MSG_WRITE, -1, msg, msglen) != 0)
463 break;
464 evbuffer_drain(cf->buffer, sent);
466 left = EVBUFFER_LENGTH(cf->buffer);
467 log_debug("file %d sent %zu, left %zu", cf->stream, sent, left);
469 if (left != 0) {
470 cf->references++;
471 event_once(-1, EV_TIMEOUT, file_push_cb, cf, NULL);
472 } else if (cf->stream > 2) {
473 close.stream = cf->stream;
474 proc_send(cf->peer, MSG_WRITE_CLOSE, -1, &close, sizeof close);
475 file_fire_done(cf);
477 free(msg);
480 /* Check if any files have data left to write. */
482 file_write_left(struct client_files *files)
484 struct client_file *cf;
485 size_t left;
486 int waiting = 0;
488 RB_FOREACH(cf, client_files, files) {
489 if (cf->event == NULL)
490 continue;
491 left = EVBUFFER_LENGTH(cf->event->output);
492 if (left != 0) {
493 waiting++;
494 log_debug("file %u %zu bytes left", cf->stream, left);
497 return (waiting != 0);
500 /* Client file write error callback. */
501 static void
502 file_write_error_callback(__unused struct bufferevent *bev, __unused short what,
503 void *arg)
505 struct client_file *cf = arg;
507 log_debug("write error file %d", cf->stream);
509 bufferevent_free(cf->event);
510 cf->event = NULL;
512 close(cf->fd);
513 cf->fd = -1;
515 if (cf->cb != NULL)
516 cf->cb(NULL, NULL, 0, -1, NULL, cf->data);
519 /* Client file write callback. */
520 static void
521 file_write_callback(__unused struct bufferevent *bev, void *arg)
523 struct client_file *cf = arg;
525 log_debug("write check file %d", cf->stream);
527 if (cf->cb != NULL)
528 cf->cb(NULL, NULL, 0, -1, NULL, cf->data);
530 if (cf->closed && EVBUFFER_LENGTH(cf->event->output) == 0) {
531 bufferevent_free(cf->event);
532 close(cf->fd);
533 RB_REMOVE(client_files, cf->tree, cf);
534 file_free(cf);
538 /* Handle a file write open message (client). */
539 void
540 file_write_open(struct client_files *files, struct tmuxpeer *peer,
541 struct imsg *imsg, int allow_streams, int close_received,
542 client_file_cb cb, void *cbdata)
544 struct msg_write_open *msg = imsg->data;
545 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
546 const char *path;
547 struct msg_write_ready reply;
548 struct client_file find, *cf;
549 const int flags = O_NONBLOCK|O_WRONLY|O_CREAT;
550 int error = 0;
552 if (msglen < sizeof *msg)
553 fatalx("bad MSG_WRITE_OPEN size");
554 if (msglen == sizeof *msg)
555 path = "-";
556 else
557 path = (const char *)(msg + 1);
558 log_debug("open write file %d %s", msg->stream, path);
560 find.stream = msg->stream;
561 if (RB_FIND(client_files, files, &find) != NULL) {
562 error = EBADF;
563 goto reply;
565 cf = file_create_with_peer(peer, files, msg->stream, cb, cbdata);
566 if (cf->closed) {
567 error = EBADF;
568 goto reply;
571 cf->fd = -1;
572 if (msg->fd == -1)
573 cf->fd = open(path, msg->flags|flags, 0644);
574 else if (allow_streams) {
575 if (msg->fd != STDOUT_FILENO && msg->fd != STDERR_FILENO)
576 errno = EBADF;
577 else {
578 cf->fd = dup(msg->fd);
579 if (close_received)
580 close(msg->fd); /* can only be used once */
582 } else
583 errno = EBADF;
584 if (cf->fd == -1) {
585 error = errno;
586 goto reply;
589 cf->event = bufferevent_new(cf->fd, NULL, file_write_callback,
590 file_write_error_callback, cf);
591 if (cf->event == NULL)
592 fatalx("out of memory");
593 bufferevent_enable(cf->event, EV_WRITE);
594 goto reply;
596 reply:
597 reply.stream = msg->stream;
598 reply.error = error;
599 proc_send(peer, MSG_WRITE_READY, -1, &reply, sizeof reply);
602 /* Handle a file write data message (client). */
603 void
604 file_write_data(struct client_files *files, struct imsg *imsg)
606 struct msg_write_data *msg = imsg->data;
607 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
608 struct client_file find, *cf;
609 size_t size = msglen - sizeof *msg;
611 if (msglen < sizeof *msg)
612 fatalx("bad MSG_WRITE size");
613 find.stream = msg->stream;
614 if ((cf = RB_FIND(client_files, files, &find)) == NULL)
615 fatalx("unknown stream number");
616 log_debug("write %zu to file %d", size, cf->stream);
618 if (cf->event != NULL)
619 bufferevent_write(cf->event, msg + 1, size);
622 /* Handle a file write close message (client). */
623 void
624 file_write_close(struct client_files *files, struct imsg *imsg)
626 struct msg_write_close *msg = imsg->data;
627 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
628 struct client_file find, *cf;
630 if (msglen != sizeof *msg)
631 fatalx("bad MSG_WRITE_CLOSE size");
632 find.stream = msg->stream;
633 if ((cf = RB_FIND(client_files, files, &find)) == NULL)
634 fatalx("unknown stream number");
635 log_debug("close file %d", cf->stream);
637 if (cf->event == NULL || EVBUFFER_LENGTH(cf->event->output) == 0) {
638 if (cf->event != NULL)
639 bufferevent_free(cf->event);
640 if (cf->fd != -1)
641 close(cf->fd);
642 RB_REMOVE(client_files, files, cf);
643 file_free(cf);
647 /* Client file read error callback. */
648 static void
649 file_read_error_callback(__unused struct bufferevent *bev, __unused short what,
650 void *arg)
652 struct client_file *cf = arg;
653 struct msg_read_done msg;
655 log_debug("read error file %d", cf->stream);
657 msg.stream = cf->stream;
658 msg.error = 0;
659 proc_send(cf->peer, MSG_READ_DONE, -1, &msg, sizeof msg);
661 bufferevent_free(cf->event);
662 close(cf->fd);
663 RB_REMOVE(client_files, cf->tree, cf);
664 file_free(cf);
667 /* Client file read callback. */
668 static void
669 file_read_callback(__unused struct bufferevent *bev, void *arg)
671 struct client_file *cf = arg;
672 void *bdata;
673 size_t bsize;
674 struct msg_read_data *msg;
675 size_t msglen;
677 msg = xmalloc(sizeof *msg);
678 for (;;) {
679 bdata = EVBUFFER_DATA(cf->event->input);
680 bsize = EVBUFFER_LENGTH(cf->event->input);
682 if (bsize == 0)
683 break;
684 if (bsize > MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg)
685 bsize = MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg;
686 log_debug("read %zu from file %d", bsize, cf->stream);
688 msglen = (sizeof *msg) + bsize;
689 msg = xrealloc(msg, msglen);
690 msg->stream = cf->stream;
691 memcpy(msg + 1, bdata, bsize);
692 proc_send(cf->peer, MSG_READ, -1, msg, msglen);
694 evbuffer_drain(cf->event->input, bsize);
696 free(msg);
699 /* Handle a file read open message (client). */
700 void
701 file_read_open(struct client_files *files, struct tmuxpeer *peer,
702 struct imsg *imsg, int allow_streams, int close_received, client_file_cb cb,
703 void *cbdata)
705 struct msg_read_open *msg = imsg->data;
706 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
707 const char *path;
708 struct msg_read_done reply;
709 struct client_file find, *cf;
710 const int flags = O_NONBLOCK|O_RDONLY;
711 int error;
713 if (msglen < sizeof *msg)
714 fatalx("bad MSG_READ_OPEN size");
715 if (msglen == sizeof *msg)
716 path = "-";
717 else
718 path = (const char *)(msg + 1);
719 log_debug("open read file %d %s", msg->stream, path);
721 find.stream = msg->stream;
722 if (RB_FIND(client_files, files, &find) != NULL) {
723 error = EBADF;
724 goto reply;
726 cf = file_create_with_peer(peer, files, msg->stream, cb, cbdata);
727 if (cf->closed) {
728 error = EBADF;
729 goto reply;
732 cf->fd = -1;
733 if (msg->fd == -1)
734 cf->fd = open(path, flags);
735 else if (allow_streams) {
736 if (msg->fd != STDIN_FILENO)
737 errno = EBADF;
738 else {
739 cf->fd = dup(msg->fd);
740 if (close_received)
741 close(msg->fd); /* can only be used once */
743 } else
744 errno = EBADF;
745 if (cf->fd == -1) {
746 error = errno;
747 goto reply;
750 cf->event = bufferevent_new(cf->fd, file_read_callback, NULL,
751 file_read_error_callback, cf);
752 if (cf->event == NULL)
753 fatalx("out of memory");
754 bufferevent_enable(cf->event, EV_READ);
755 return;
757 reply:
758 reply.stream = msg->stream;
759 reply.error = error;
760 proc_send(peer, MSG_READ_DONE, -1, &reply, sizeof reply);
763 /* Handle a write ready message (server). */
764 void
765 file_write_ready(struct client_files *files, struct imsg *imsg)
767 struct msg_write_ready *msg = imsg->data;
768 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
769 struct client_file find, *cf;
771 if (msglen != sizeof *msg)
772 fatalx("bad MSG_WRITE_READY size");
773 find.stream = msg->stream;
774 if ((cf = RB_FIND(client_files, files, &find)) == NULL)
775 return;
776 if (msg->error != 0) {
777 cf->error = msg->error;
778 file_fire_done(cf);
779 } else
780 file_push(cf);
783 /* Handle read data message (server). */
784 void
785 file_read_data(struct client_files *files, struct imsg *imsg)
787 struct msg_read_data *msg = imsg->data;
788 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
789 struct client_file find, *cf;
790 void *bdata = msg + 1;
791 size_t bsize = msglen - sizeof *msg;
793 if (msglen < sizeof *msg)
794 fatalx("bad MSG_READ_DATA size");
795 find.stream = msg->stream;
796 if ((cf = RB_FIND(client_files, files, &find)) == NULL)
797 return;
799 log_debug("file %d read %zu bytes", cf->stream, bsize);
800 if (cf->error == 0) {
801 if (evbuffer_add(cf->buffer, bdata, bsize) != 0) {
802 cf->error = ENOMEM;
803 file_fire_done(cf);
804 } else
805 file_fire_read(cf);
809 /* Handle a read done message (server). */
810 void
811 file_read_done(struct client_files *files, struct imsg *imsg)
813 struct msg_read_done *msg = imsg->data;
814 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
815 struct client_file find, *cf;
817 if (msglen != sizeof *msg)
818 fatalx("bad MSG_READ_DONE size");
819 find.stream = msg->stream;
820 if ((cf = RB_FIND(client_files, files, &find)) == NULL)
821 return;
823 log_debug("file %d read done", cf->stream);
824 cf->error = msg->error;
825 file_fire_done(cf);