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 struct stat st;
00089 int ret = stat(h->filename, &st);
00090 if (ret < 0)
00091 return AVERROR(errno);
00092
00093 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
00094 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
00095
00096 return ret;
00097 }
00098
00099 #if CONFIG_FILE_PROTOCOL
00100
00101 static int file_open(URLContext *h, const char *filename, int flags)
00102 {
00103 FileContext *c = h->priv_data;
00104 int access;
00105 int fd;
00106 struct stat st;
00107
00108 av_strstart(filename, "file:", &filename);
00109
00110 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
00111 access = O_CREAT | O_RDWR;
00112 if (c->trunc)
00113 access |= O_TRUNC;
00114 } else if (flags & AVIO_FLAG_WRITE) {
00115 access = O_CREAT | O_WRONLY;
00116 if (c->trunc)
00117 access |= O_TRUNC;
00118 } else {
00119 access = O_RDONLY;
00120 }
00121 #ifdef O_BINARY
00122 access |= O_BINARY;
00123 #endif
00124 fd = open(filename, access, 0666);
00125 if (fd == -1)
00126 return AVERROR(errno);
00127 c->fd = fd;
00128
00129 h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
00130
00131 return 0;
00132 }
00133
00134
00135 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
00136 {
00137 FileContext *c = h->priv_data;
00138 off_t ret;
00139
00140 if (whence == AVSEEK_SIZE) {
00141 struct stat st;
00142 ret = fstat(c->fd, &st);
00143 return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
00144 }
00145
00146 ret = lseek(c->fd, pos, whence);
00147
00148 return ret < 0 ? AVERROR(errno) : ret;
00149 }
00150
00151 static int file_close(URLContext *h)
00152 {
00153 FileContext *c = h->priv_data;
00154 return close(c->fd);
00155 }
00156
00157 URLProtocol ff_file_protocol = {
00158 .name = "file",
00159 .url_open = file_open,
00160 .url_read = file_read,
00161 .url_write = file_write,
00162 .url_seek = file_seek,
00163 .url_close = file_close,
00164 .url_get_file_handle = file_get_handle,
00165 .url_check = file_check,
00166 .priv_data_size = sizeof(FileContext),
00167 .priv_data_class = &file_class,
00168 };
00169
00170 #endif
00171
00172 #if CONFIG_PIPE_PROTOCOL
00173
00174 static int pipe_open(URLContext *h, const char *filename, int flags)
00175 {
00176 FileContext *c = h->priv_data;
00177 int fd;
00178 char *final;
00179 av_strstart(filename, "pipe:", &filename);
00180
00181 fd = strtol(filename, &final, 10);
00182 if((filename == final) || *final ) {
00183 if (flags & AVIO_FLAG_WRITE) {
00184 fd = 1;
00185 } else {
00186 fd = 0;
00187 }
00188 }
00189 #if HAVE_SETMODE
00190 setmode(fd, O_BINARY);
00191 #endif
00192 c->fd = fd;
00193 h->is_streamed = 1;
00194 return 0;
00195 }
00196
00197 URLProtocol ff_pipe_protocol = {
00198 .name = "pipe",
00199 .url_open = pipe_open,
00200 .url_read = file_read,
00201 .url_write = file_write,
00202 .url_get_file_handle = file_get_handle,
00203 .url_check = file_check,
00204 .priv_data_size = sizeof(FileContext),
00205 };
00206
00207 #endif