From c13b33617bc9cf40e4121a39addbecd9c2072e57 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 13 Sep 2013 07:52:54 +0200 Subject: [PATCH] handle partial writes --- spiceterm.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/spiceterm.c b/spiceterm.c index e0eb1cf..c3c7b7e 100644 --- a/spiceterm.c +++ b/spiceterm.c @@ -1791,26 +1791,39 @@ static void master_watch(int master, int event, void *opaque) { spiceTerm *vt = (spiceTerm *)opaque; + int c; // fixme: if (!vt->mark_active) { if (event == SPICE_WATCH_EVENT_READ) { char buffer[1024]; - int c; while ((c = read(master, buffer, 1024)) == -1) { if (errno != EAGAIN) break; } if (c == -1) { - g_error("got read error"); // fixme + perror("master pipe read error"); // fixme } spiceterm_puts (vt, buffer, c); } else { if (vt->ibuf_count > 0) { DPRINTF(1, "write input %x %d", vt->ibuf[0], vt->ibuf_count); - write (master, vt->ibuf, vt->ibuf_count); - vt->ibuf_count = 0; // fixme: what if not all data written + if ((c = write (master, vt->ibuf, vt->ibuf_count)) >= 0) { + if (c == vt->ibuf_count) { + vt->ibuf_count = 0; + } else if (c > 0) { + // not all data written + memmove(vt->ibuf, vt->ibuf + c, vt->ibuf_count - c); + vt->ibuf_count -= c; + } else { + // nothing written -ignore and try later + } + } else { + perror("master pipe write error"); + } + } + if (vt->ibuf_count == 0) { + spiceterm_update_watch_mask(vt, FALSE); } - spiceterm_update_watch_mask(vt, FALSE); } } -- 2.39.2