00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avstring.h"
00023 #include "avformat.h"
00024 #include <fcntl.h>
00025 #if HAVE_SETMODE
00026 #include <io.h>
00027 #endif
00028 #include <unistd.h>
00029 #include <sys/stat.h>
00030 #include <stdlib.h>
00031 #include "os_support.h"
00032 #include "url.h"
00033
00034
00035
00036
00037 static int file_read(URLContext *h, unsigned char *buf, int size)
00038 {
00039 int fd = (intptr_t) h->priv_data;
00040 return read(fd, buf, size);
00041 }
00042
00043 static int file_write(URLContext *h, const unsigned char *buf, int size)
00044 {
00045 int fd = (intptr_t) h->priv_data;
00046 return write(fd, buf, size);
00047 }
00048
00049 static int file_get_handle(URLContext *h)
00050 {
00051 return (intptr_t) h->priv_data;
00052 }
00053
00054 static int file_check(URLContext *h, int mask)
00055 {
00056 struct stat st;
00057 int ret = stat(h->filename, &st);
00058 if (ret < 0)
00059 return AVERROR(errno);
00060
00061 ret |= st.st_mode&S_IRUSR ? mask&AVIO_RDONLY : 0;
00062 ret |= st.st_mode&S_IWUSR ? mask&AVIO_WRONLY : 0;
00063 ret |= st.st_mode&S_IWUSR && st.st_mode&S_IRUSR ? mask&AVIO_RDWR : 0;
00064
00065 return ret;
00066 }
00067
00068 #if CONFIG_FILE_PROTOCOL
00069
00070 static int file_open(URLContext *h, const char *filename, int flags)
00071 {
00072 int access;
00073 int fd;
00074
00075 av_strstart(filename, "file:", &filename);
00076
00077 if (flags & AVIO_RDWR) {
00078 access = O_CREAT | O_TRUNC | O_RDWR;
00079 } else if (flags & AVIO_WRONLY) {
00080 access = O_CREAT | O_TRUNC | O_WRONLY;
00081 } else {
00082 access = O_RDONLY;
00083 }
00084 #ifdef O_BINARY
00085 access |= O_BINARY;
00086 #endif
00087 fd = open(filename, access, 0666);
00088 if (fd == -1)
00089 return AVERROR(errno);
00090 h->priv_data = (void *) (intptr_t) fd;
00091 return 0;
00092 }
00093
00094
00095 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
00096 {
00097 int fd = (intptr_t) h->priv_data;
00098 if (whence == AVSEEK_SIZE) {
00099 struct stat st;
00100 int ret = fstat(fd, &st);
00101 return ret < 0 ? AVERROR(errno) : st.st_size;
00102 }
00103 return lseek(fd, pos, whence);
00104 }
00105
00106 static int file_close(URLContext *h)
00107 {
00108 int fd = (intptr_t) h->priv_data;
00109 return close(fd);
00110 }
00111
00112 URLProtocol ff_file_protocol = {
00113 .name = "file",
00114 .url_open = file_open,
00115 .url_read = file_read,
00116 .url_write = file_write,
00117 .url_seek = file_seek,
00118 .url_close = file_close,
00119 .url_get_file_handle = file_get_handle,
00120 .url_check = file_check,
00121 };
00122
00123 #endif
00124
00125 #if CONFIG_PIPE_PROTOCOL
00126
00127 static int pipe_open(URLContext *h, const char *filename, int flags)
00128 {
00129 int fd;
00130 char *final;
00131 av_strstart(filename, "pipe:", &filename);
00132
00133 fd = strtol(filename, &final, 10);
00134 if((filename == final) || *final ) {
00135 if (flags & AVIO_WRONLY) {
00136 fd = 1;
00137 } else {
00138 fd = 0;
00139 }
00140 }
00141 #if HAVE_SETMODE
00142 setmode(fd, O_BINARY);
00143 #endif
00144 h->priv_data = (void *) (intptr_t) fd;
00145 h->is_streamed = 1;
00146 return 0;
00147 }
00148
00149 URLProtocol ff_pipe_protocol = {
00150 .name = "pipe",
00151 .url_open = pipe_open,
00152 .url_read = file_read,
00153 .url_write = file_write,
00154 .url_get_file_handle = file_get_handle,
00155 .url_check = file_check,
00156 };
00157
00158 #endif