From bec218c4ad70d0eb1ae5a1d6a4455d9fd16bad0c Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 14 Sep 2019 12:55:06 +0200 Subject: stream: handle short writes The write functionality is almost unused (only encoding 2-pass mode uses it to write the log file). Moreover, it almost makes no sense to use this in a not local scenario. This change is just to prevent people from duplicating the short write logic across all streams that happen to support writing. Mostly untested; local log file writing still works. --- stream/stream.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/stream/stream.c b/stream/stream.c index 8068effc06..c318554feb 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -419,15 +419,18 @@ struct bstr stream_peek(stream_t *s, int len) int stream_write_buffer(stream_t *s, unsigned char *buf, int len) { - int rd; if (!s->write_buffer) return -1; - rd = s->write_buffer(s, buf, len); - if (rd < 0) - return -1; - s->pos += rd; - assert(rd == len && "stream_write_buffer(): unexpected short write"); - return rd; + int orig_len = len; + while (len) { + int w = s->write_buffer(s, buf, len); + if (w <= 0) + return -1; + s->pos += w; + buf += w; + len -= w; + } + return orig_len; } // Drop len bytes form input, possibly reading more until all is skipped. If -- cgit v1.2.3