From aaa29eb81e0388b9173223290b6b15f619caa7ab Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 25 Oct 2014 17:11:24 +0200 Subject: stream: fix --stream-dump dropping the file header stream_rar.c peeks the first few bytes when trying to open, which means that opening any stream reads at least 2KB of data (internal buffer size) on opening. This broke --stream-dump, which saved only the data following this initial buffer. Hack it around by writing the current buffer to the capture file too, and move stream_capture_write() above stream_set_capture_file() for this purpose. Cleaner solutions might include: handling the terrible rar thing differently, or using the "proper" stream API for dumping. (The latter is not done, because --stream-dump shares code with the --stream-capture misfeature.) Fixes #1215. --- stream/stream.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'stream') diff --git a/stream/stream.c b/stream/stream.c index e16ee966fe..e51a1666f0 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -438,6 +438,16 @@ static int stream_reconnect(stream_t *s) return 0; } +static void stream_capture_write(stream_t *s, void *buf, size_t len) +{ + if (s->capture_file && len > 0) { + if (fwrite(buf, len, 1, s->capture_file) < 1) { + MP_ERR(s, "Error writing capture file: %s\n", strerror(errno)); + stream_set_capture_file(s, NULL); + } + } +} + void stream_set_capture_file(stream_t *s, const char *filename) { if (!bstr_equals(bstr0(s->capture_filename), bstr0(filename))) { @@ -450,6 +460,8 @@ void stream_set_capture_file(stream_t *s, const char *filename) s->capture_file = fopen(filename, "wb"); if (s->capture_file) { s->capture_filename = talloc_strdup(NULL, filename); + if (s->buf_pos < s->buf_len) + stream_capture_write(s, s->buffer, s->buf_len); } else { MP_ERR(s, "Error opening capture file: %s\n", strerror(errno)); } @@ -457,16 +469,6 @@ void stream_set_capture_file(stream_t *s, const char *filename) } } -static void stream_capture_write(stream_t *s, void *buf, size_t len) -{ - if (s->capture_file && len > 0) { - if (fwrite(buf, len, 1, s->capture_file) < 1) { - MP_ERR(s, "Error writing capture file: %s\n", strerror(errno)); - stream_set_capture_file(s, NULL); - } - } -} - // Read function bypassing the local stream buffer. This will not write into // s->buffer, but into buf[0..len] instead. // Returns < 0 on error, 0 on EOF, and length of bytes read on success. -- cgit v1.2.3