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.
This commit is contained in:
wm4 2013-07-12 22:07:07 +02:00
parent b66c609b48
commit f63193f58f
8 changed files with 57 additions and 66 deletions

View File

@ -76,6 +76,7 @@ typedef struct {
} dvb_config_t;
typedef struct {
int fd;
int card;
int fe_fd;
int sec_fd;

View File

@ -315,13 +315,7 @@ static int stream_read_unbuffered(stream_t *s, void *buf, int len)
int orig_len = len;
s->buf_pos = s->buf_len = 0;
// we will retry even if we already reached EOF previously.
if (s->fill_buffer) {
len = s->fill_buffer(s, buf, len);
} else if (s->fd >= 0) {
len = read(s->fd, buf, len);
} else {
len = 0;
}
len = s->fill_buffer ? s->fill_buffer(s, buf, len) : -1;
if (len < 0)
len = 0;
if (len == 0) {
@ -590,8 +584,6 @@ static stream_t *new_stream(size_t min_size)
min_size = FFMAX(min_size, TOTAL_BUFFER_SIZE);
stream_t *s = talloc_size(NULL, sizeof(stream_t) + min_size);
memset(s, 0, sizeof(stream_t));
s->fd = -2;
return s;
}
@ -604,9 +596,6 @@ void free_stream(stream_t *s)
if (s->close)
s->close(s);
if (s->fd > 0) {
close(s->fd);
}
free_stream(s->uncached_stream);
talloc_free(s);
}

View File

@ -134,7 +134,6 @@ typedef struct stream {
// Close
void (*close)(struct stream *s);
int fd; // file descriptor, see man open(2)
enum streamtype type; // see STREAMTYPE_*
enum streamtype uncached_type; // if stream is cache, type of wrapped str.
int flags; // MP_STREAM_SEEK_* or'ed flags

View File

@ -443,7 +443,7 @@ static int dvb_streaming_read(stream_t *stream, char *buffer, int size)
tries = priv->retry + 1;
fd = stream->fd;
fd = priv->fd;
while(pos < size)
{
pfds[0].fd = fd;
@ -535,7 +535,7 @@ int dvb_set_channel(stream_t *stream, int card, int n)
priv->list = new_list;
priv->retry = 5;
new_list->current = n;
stream->fd = priv->dvr_fd;
priv->fd = priv->dvr_fd;
mp_msg(MSGT_DEMUX, MSGL_V, "DVB_SET_CHANNEL: new channel name=%s, card: %d, channel %d\n", channel->name, card, n);
stream->buf_pos = stream->buf_len = 0;

View File

@ -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;

View File

@ -26,6 +26,10 @@
#include "core/m_option.h"
#include "core/m_struct.h"
struct priv {
int fd;
};
static struct stream_priv_s {
} stream_priv_dflts = {
};
@ -72,10 +76,11 @@ static void smb_auth_fn(const char *server, const char *share,
}
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 = smbc_lseek(s->fd,0,SEEK_END);
smbc_lseek(s->fd,s->pos,SEEK_SET);
off_t size = smbc_lseek(p->fd,0,SEEK_END);
smbc_lseek(p->fd,s->pos,SEEK_SET);
if(size != (off_t)-1) {
*(uint64_t *)arg = size;
return 1;
@ -86,23 +91,26 @@ static int control(stream_t *s, int cmd, void *arg) {
}
static int seek(stream_t *s,int64_t newpos) {
struct priv *p = s->priv;
s->pos = newpos;
if(smbc_lseek(s->fd,s->pos,SEEK_SET)<0) {
if(smbc_lseek(p->fd,s->pos,SEEK_SET)<0) {
return 0;
}
return 1;
}
static int fill_buffer(stream_t *s, char* buffer, int max_len){
int r = smbc_read(s->fd,buffer,max_len);
struct priv *p = s->priv;
int r = smbc_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 = smbc_write(s->fd,buffer,len);
r = smbc_write(p->fd,buffer,len);
if (r <= 0)
return -1;
wr += r;
@ -112,7 +120,8 @@ static int write_buffer(stream_t *s, char* buffer, int len) {
}
static void close_f(stream_t *s){
smbc_close(s->fd);
struct priv *p = s->priv;
smbc_close(p->fd);
}
static int open_f (stream_t *stream, int mode, void *opts)
@ -122,6 +131,9 @@ static int open_f (stream_t *stream, int mode, void *opts)
int64_t len;
int fd, err;
struct priv *priv = talloc_zero(stream, struct priv);
stream->priv = priv;
filename = stream->url;
if(mode == STREAM_READ)
@ -165,7 +177,7 @@ static int open_f (stream_t *stream, int mode, void *opts)
stream->seek = seek;
if(mode == STREAM_READ) stream->end_pos = len;
}
stream->fd = fd;
priv->fd = fd;
stream->fill_buffer = fill_buffer;
stream->write_buffer = write_buffer;
stream->close = close_f;

View File

@ -92,43 +92,10 @@ static int seek(stream_t *s,int64_t newpos) {
return 1;
}
static int control(stream_t *stream, int cmd, void *arg) {
struct stream_priv_s *p = stream->priv;
switch(cmd) {
case STREAM_CTRL_GET_NUM_TITLES:
case STREAM_CTRL_GET_NUM_CHAPTERS:
{
mp_vcd_priv_t *vcd = vcd_read_toc(stream->fd);
if (!vcd)
break;
*(unsigned int *)arg = vcd_end_track(vcd);
return STREAM_OK;
}
case STREAM_CTRL_SEEK_TO_CHAPTER:
{
int r;
unsigned int track = *(unsigned int *)arg + 1;
mp_vcd_priv_t *vcd = vcd_read_toc(stream->fd);
if (!vcd)
break;
r = vcd_seek_to_track(vcd, track);
if (r >= 0) {
p->track = track;
return STREAM_OK;
}
break;
}
case STREAM_CTRL_GET_CURRENT_CHAPTER:
{
*(unsigned int *)arg = p->track - 1;
return STREAM_OK;
}
}
return STREAM_UNSUPPORTED;
}
static void close_s(stream_t *stream) {
free(stream->priv);
mp_vcd_priv_t *p = stream->priv;
close(p->fd);
free(p);
}
static int open_s(stream_t *stream,int mode, void* opts)
@ -220,7 +187,6 @@ static int open_s(stream_t *stream,int mode, void* opts)
}
#endif
stream->fd = f;
stream->sector_size = VCD_SECTOR_DATA;
stream->start_pos=ret;
stream->end_pos=ret2;
@ -228,7 +194,6 @@ static int open_s(stream_t *stream,int mode, void* opts)
stream->fill_buffer = fill_buffer;
stream->seek = seek;
stream->control = control;
stream->close = close_s;
stream->demuxer = "lavf"; // mpegps ( or "vcd"?)

View File

@ -147,10 +147,12 @@ static mp_vcd_priv_t* vcd_read_toc(int fd){
return vcd;
}
/*
static int vcd_end_track(mp_vcd_priv_t* vcd)
{
return vcd->tochdr.cdth_trk1;
}
*/
static int vcd_read(mp_vcd_priv_t* vcd,char *mem){
#ifndef sun