From f63193f58f7214ef3a4f82be045f8a3cfd14b8ac Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 12 Jul 2013 22:07:07 +0200 Subject: stream: remove fd member Stream implementations could set this to a unix file descriptor. The generic stream code could use it as fallback for a few things. This was confusing and insane. In most cases, the stream implementations defined all callbacks, so setting the fd member didn't have any advantages, other than avoiding defining a private struct to store it. It appears that even if the stream implementation used close() on the fd (or something equivalent), stream.c would close() it a second time (and on windows, even would call closesocket()), which should be proof for the insanity of this code. For stream_file.c, additionally make sure we don't close stdin or stdout if "-" is used as filename. For stream_vcd.c, remove the control() code. This code most likely didn't make the slightest sense, because it used a different type for stream->priv. It also leaked memory. Maybe it worked, but it's incorrect and insignificant anyway, so kill it. This code was added with commit 9521c19 (svn commit 31019). Untested for all protocols other than stream_file.c. --- stream/stream_file.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'stream/stream_file.c') diff --git a/stream/stream_file.c b/stream/stream_file.c index 79ebbb3eef..6166610ec6 100644 --- a/stream/stream_file.c +++ b/stream/stream_file.c @@ -32,6 +32,11 @@ #include "core/m_option.h" #include "core/m_struct.h" +struct priv { + int fd; + bool close; +}; + static struct stream_priv_s { char* filename; char *filename2; @@ -54,15 +59,17 @@ static const struct m_struct_st stream_opts = { }; static int fill_buffer(stream_t *s, char* buffer, int max_len){ - int r = read(s->fd,buffer,max_len); + struct priv *p = s->priv; + int r = read(p->fd,buffer,max_len); return (r <= 0) ? -1 : r; } static int write_buffer(stream_t *s, char* buffer, int len) { + struct priv *p = s->priv; int r; int wr = 0; while (wr < len) { - r = write(s->fd,buffer,len); + r = write(p->fd,buffer,len); if (r <= 0) return -1; wr += r; @@ -72,8 +79,9 @@ static int write_buffer(stream_t *s, char* buffer, int len) { } static int seek(stream_t *s,int64_t newpos) { + struct priv *p = s->priv; s->pos = newpos; - if(lseek(s->fd,s->pos,SEEK_SET)<0) { + if(lseek(p->fd,s->pos,SEEK_SET)<0) { return 0; } return 1; @@ -95,12 +103,13 @@ static int seek_forward(stream_t *s,int64_t newpos) { } static int control(stream_t *s, int cmd, void *arg) { + struct priv *p = s->priv; switch(cmd) { case STREAM_CTRL_GET_SIZE: { off_t size; - size = lseek(s->fd, 0, SEEK_END); - lseek(s->fd, s->pos, SEEK_SET); + size = lseek(p->fd, 0, SEEK_END); + lseek(p->fd, s->pos, SEEK_SET); if(size != (off_t)-1) { *(uint64_t*)arg = size; return 1; @@ -110,6 +119,13 @@ static int control(stream_t *s, int cmd, void *arg) { return STREAM_UNSUPPORTED; } +static void s_close(stream_t *s) +{ + struct priv *p = s->priv; + if (p->close && p->fd >= 0) + close(p->fd); +} + static int open_f(stream_t *stream,int mode, void* opts) { int f; @@ -117,6 +133,9 @@ static int open_f(stream_t *stream,int mode, void* opts) int64_t len; unsigned char *filename; struct stream_priv_s* p = (struct stream_priv_s*)opts; + struct priv *priv = talloc_ptrtype(stream, priv); + *priv = (struct priv) { .fd = -1 }; + stream->priv = priv; if(mode == STREAM_READ) m = O_RDONLY; @@ -163,6 +182,8 @@ static int open_f(stream_t *stream,int mode, void* opts) setmode(fileno(stdout),O_BINARY); #endif } + priv->fd = f; + priv->close = false; } else { mode_t openmode = S_IRUSR|S_IWUSR; #ifndef __MINGW32__ @@ -184,6 +205,8 @@ static int open_f(stream_t *stream,int mode, void* opts) return STREAM_ERROR; } #endif + priv->fd = f; + priv->close = true; } len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET); @@ -203,11 +226,11 @@ static int open_f(stream_t *stream,int mode, void* opts) mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %"PRId64" bytes\n", (int64_t)len); - stream->fd = f; stream->fill_buffer = fill_buffer; stream->write_buffer = write_buffer; stream->control = control; stream->read_chunk = 64*1024; + stream->close = s_close; m_struct_free(&stream_opts,opts); return STREAM_OK; -- cgit v1.2.3