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 "libavutil/opt.h"
00024 #include "avformat.h"
00025 #include <fcntl.h>
00026 #if HAVE_IO_H
00027 #include <io.h>
00028 #endif
00029 #if HAVE_UNISTD_H
00030 #include <unistd.h>
00031 #endif
00032 #include <sys/stat.h>
00033 #include <stdlib.h>
00034 #include "os_support.h"
00035 #include "url.h"
00036
00037
00038 #ifndef S_ISFIFO
00039 # ifdef S_IFIFO
00040 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
00041 # else
00042 # define S_ISFIFO(m) 0
00043 # endif
00044 #endif
00045
00046
00047
00048 typedef struct FileContext {
00049 const AVClass *class;
00050 int fd;
00051 int trunc;
00052 } FileContext;
00053
00054 static const AVOption file_options[] = {
00055 { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
00056 { NULL }
00057 };
00058
00059 static const AVClass file_class = {
00060 .class_name = "file",
00061 .item_name = av_default_item_name,
00062 .option = file_options,
00063 .version = LIBAVUTIL_VERSION_INT,
00064 };
00065
00066 static int file_read(URLContext *h, unsigned char *buf, int size)
00067 {
00068 FileContext *c = h->priv_data;
00069 int r = read(c->fd, buf, size);
00070 return (-1 == r)?AVERROR(errno):r;
00071 }
00072
00073 static int file_write(URLContext *h, const unsigned char *buf, int size)
00074 {
00075 FileContext *c = h->priv_data;
00076 int r = write(c->fd, buf, size);
00077 return (-1 == r)?AVERROR(errno):r;
00078 }
00079
00080 static int file_get_handle(URLContext *h)
00081 {
00082 FileContext *c = h->priv_data;
00083 return c->fd;
00084 }
00085
00086 static int file_check(URLContext *h, int mask)
00087 {
00088 #if HAVE_ACCESS && defined(R_OK)
00089 int ret = 0;
00090 if (access(h->filename, F_OK) < 0)
00091 return AVERROR(errno);
00092 if (mask&AVIO_FLAG_READ)
00093 if (access(h->filename, R_OK) >= 0)
00094 ret |= AVIO_FLAG_READ;
00095 if (mask&AVIO_FLAG_WRITE)
00096 if (access(h->filename, W_OK) >= 0)
00097 ret |= AVIO_FLAG_WRITE;
00098 #else
00099 struct stat st;
00100 int ret = stat(h->filename, &st);
00101 if (ret < 0)
00102 return AVERROR(errno);
00103
00104 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
00105 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
00106 #endif
00107 return ret;
00108 }
00109
00110 #if CONFIG_FILE_PROTOCOL
00111
00112 static int file_open(URLContext *h, const char *filename, int flags)
00113 {
00114 FileContext *c = h->priv_data;
00115 int access;
00116 int fd;
00117 struct stat st;
00118
00119 av_strstart(filename, "file:", &filename);
00120
00121 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
00122 access = O_CREAT | O_RDWR;
00123 if (c->trunc)
00124 access |= O_TRUNC;
00125 } else if (flags & AVIO_FLAG_WRITE) {
00126 access = O_CREAT | O_WRONLY;
00127 if (c->trunc)
00128 access |= O_TRUNC;
00129 } else {
00130 access = O_RDONLY;
00131 }
00132 #ifdef O_BINARY
00133 access |= O_BINARY;
00134 #endif
00135 fd = open(filename, access, 0666);
00136 if (fd == -1)
00137 return AVERROR(errno);
00138 c->fd = fd;
00139
00140 h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
00141
00142 return 0;
00143 }
00144
00145
00146 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
00147 {
00148 FileContext *c = h->priv_data;
00149 int64_t ret;
00150
00151 if (whence == AVSEEK_SIZE) {
00152 struct stat st;
00153 ret = fstat(c->fd, &st);
00154 return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
00155 }
00156
00157 ret = lseek(c->fd, pos, whence);
00158
00159 return ret < 0 ? AVERROR(errno) : ret;
00160 }
00161
00162 static int file_close(URLContext *h)
00163 {
00164 FileContext *c = h->priv_data;
00165 return close(c->fd);
00166 }
00167
00168 URLProtocol ff_file_protocol = {
00169 .name = "file",
00170 .url_open = file_open,
00171 .url_read = file_read,
00172 .url_write = file_write,
00173 .url_seek = file_seek,
00174 .url_close = file_close,
00175 .url_get_file_handle = file_get_handle,
00176 .url_check = file_check,
00177 .priv_data_size = sizeof(FileContext),
00178 .priv_data_class = &file_class,
00179 };
00180
00181 #endif
00182
00183 #if CONFIG_PIPE_PROTOCOL
00184
00185 static int pipe_open(URLContext *h, const char *filename, int flags)
00186 {
00187 FileContext *c = h->priv_data;
00188 int fd;
00189 char *final;
00190 av_strstart(filename, "pipe:", &filename);
00191
00192 fd = strtol(filename, &final, 10);
00193 if((filename == final) || *final ) {
00194 if (flags & AVIO_FLAG_WRITE) {
00195 fd = 1;
00196 } else {
00197 fd = 0;
00198 }
00199 }
00200 #if HAVE_SETMODE
00201 setmode(fd, O_BINARY);
00202 #endif
00203 c->fd = fd;
00204 h->is_streamed = 1;
00205 return 0;
00206 }
00207
00208 URLProtocol ff_pipe_protocol = {
00209 .name = "pipe",
00210 .url_open = pipe_open,
00211 .url_read = file_read,
00212 .url_write = file_write,
00213 .url_get_file_handle = file_get_handle,
00214 .url_check = file_check,
00215 .priv_data_size = sizeof(FileContext),
00216 };
00217
00218 #endif