1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

file: Use a normal private context for storing the file descriptor

Previously the file descriptor was stored in the priv_data pointer.

Signed-off-by: Martin Storsjö <martin@martin.st>
This commit is contained in:
Martin Storsjö 2012-09-09 22:35:50 +03:00
parent 5fa22ae346
commit 0697d81269

View File

@ -36,21 +36,26 @@
/* standard file protocol */ /* standard file protocol */
typedef struct FileContext {
int fd;
} FileContext;
static int file_read(URLContext *h, unsigned char *buf, int size) static int file_read(URLContext *h, unsigned char *buf, int size)
{ {
int fd = (intptr_t) h->priv_data; FileContext *c = h->priv_data;
return read(fd, buf, size); return read(c->fd, buf, size);
} }
static int file_write(URLContext *h, const unsigned char *buf, int size) static int file_write(URLContext *h, const unsigned char *buf, int size)
{ {
int fd = (intptr_t) h->priv_data; FileContext *c = h->priv_data;
return write(fd, buf, size); return write(c->fd, buf, size);
} }
static int file_get_handle(URLContext *h) static int file_get_handle(URLContext *h)
{ {
return (intptr_t) h->priv_data; FileContext *c = h->priv_data;
return c->fd;
} }
static int file_check(URLContext *h, int mask) static int file_check(URLContext *h, int mask)
@ -70,6 +75,7 @@ static int file_check(URLContext *h, int mask)
static int file_open(URLContext *h, const char *filename, int flags) static int file_open(URLContext *h, const char *filename, int flags)
{ {
FileContext *c = h->priv_data;
int access; int access;
int fd; int fd;
@ -88,26 +94,26 @@ static int file_open(URLContext *h, const char *filename, int flags)
fd = open(filename, access, 0666); fd = open(filename, access, 0666);
if (fd == -1) if (fd == -1)
return AVERROR(errno); return AVERROR(errno);
h->priv_data = (void *) (intptr_t) fd; c->fd = fd;
return 0; return 0;
} }
/* XXX: use llseek */ /* XXX: use llseek */
static int64_t file_seek(URLContext *h, int64_t pos, int whence) static int64_t file_seek(URLContext *h, int64_t pos, int whence)
{ {
int fd = (intptr_t) h->priv_data; FileContext *c = h->priv_data;
if (whence == AVSEEK_SIZE) { if (whence == AVSEEK_SIZE) {
struct stat st; struct stat st;
int ret = fstat(fd, &st); int ret = fstat(c->fd, &st);
return ret < 0 ? AVERROR(errno) : st.st_size; return ret < 0 ? AVERROR(errno) : st.st_size;
} }
return lseek(fd, pos, whence); return lseek(c->fd, pos, whence);
} }
static int file_close(URLContext *h) static int file_close(URLContext *h)
{ {
int fd = (intptr_t) h->priv_data; FileContext *c = h->priv_data;
return close(fd); return close(c->fd);
} }
URLProtocol ff_file_protocol = { URLProtocol ff_file_protocol = {
@ -119,6 +125,7 @@ URLProtocol ff_file_protocol = {
.url_close = file_close, .url_close = file_close,
.url_get_file_handle = file_get_handle, .url_get_file_handle = file_get_handle,
.url_check = file_check, .url_check = file_check,
.priv_data_size = sizeof(FileContext),
}; };
#endif /* CONFIG_FILE_PROTOCOL */ #endif /* CONFIG_FILE_PROTOCOL */
@ -127,6 +134,7 @@ URLProtocol ff_file_protocol = {
static int pipe_open(URLContext *h, const char *filename, int flags) static int pipe_open(URLContext *h, const char *filename, int flags)
{ {
FileContext *c = h->priv_data;
int fd; int fd;
char *final; char *final;
av_strstart(filename, "pipe:", &filename); av_strstart(filename, "pipe:", &filename);
@ -142,7 +150,7 @@ static int pipe_open(URLContext *h, const char *filename, int flags)
#if HAVE_SETMODE #if HAVE_SETMODE
setmode(fd, O_BINARY); setmode(fd, O_BINARY);
#endif #endif
h->priv_data = (void *) (intptr_t) fd; c->fd = fd;
h->is_streamed = 1; h->is_streamed = 1;
return 0; return 0;
} }
@ -154,6 +162,7 @@ URLProtocol ff_pipe_protocol = {
.url_write = file_write, .url_write = file_write,
.url_get_file_handle = file_get_handle, .url_get_file_handle = file_get_handle,
.url_check = file_check, .url_check = file_check,
.priv_data_size = sizeof(FileContext),
}; };
#endif /* CONFIG_PIPE_PROTOCOL */ #endif /* CONFIG_PIPE_PROTOCOL */