3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avstring.h"
23 #include "libavutil/opt.h"
34 #include "os_support.h"
37 /* Some systems may not have S_ISFIFO */
40 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
42 # define S_ISFIFO(m) 0
46 /* standard file protocol */
48 typedef struct FileContext {
55 static const AVOption file_options[] = {
56 { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
57 { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
61 static const AVOption pipe_options[] = {
62 { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
66 static const AVClass file_class = {
68 .item_name = av_default_item_name,
69 .option = file_options,
70 .version = LIBAVUTIL_VERSION_INT,
73 static const AVClass pipe_class = {
75 .item_name = av_default_item_name,
76 .option = pipe_options,
77 .version = LIBAVUTIL_VERSION_INT,
80 static int file_read(URLContext *h, unsigned char *buf, int size)
82 FileContext *c = h->priv_data;
84 size = FFMIN(size, c->blocksize);
85 r = read(c->fd, buf, size);
86 return (-1 == r)?AVERROR(errno):r;
89 static int file_write(URLContext *h, const unsigned char *buf, int size)
91 FileContext *c = h->priv_data;
93 size = FFMIN(size, c->blocksize);
94 r = write(c->fd, buf, size);
95 return (-1 == r)?AVERROR(errno):r;
98 static int file_get_handle(URLContext *h)
100 FileContext *c = h->priv_data;
104 static int file_check(URLContext *h, int mask)
106 #if HAVE_ACCESS && defined(R_OK)
108 if (access(h->filename, F_OK) < 0)
109 return AVERROR(errno);
110 if (mask&AVIO_FLAG_READ)
111 if (access(h->filename, R_OK) >= 0)
112 ret |= AVIO_FLAG_READ;
113 if (mask&AVIO_FLAG_WRITE)
114 if (access(h->filename, W_OK) >= 0)
115 ret |= AVIO_FLAG_WRITE;
118 int ret = stat(h->filename, &st);
120 return AVERROR(errno);
122 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
123 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
128 #if CONFIG_FILE_PROTOCOL
130 static int file_open(URLContext *h, const char *filename, int flags)
132 FileContext *c = h->priv_data;
137 av_strstart(filename, "file:", &filename);
139 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
140 access = O_CREAT | O_RDWR;
143 } else if (flags & AVIO_FLAG_WRITE) {
144 access = O_CREAT | O_WRONLY;
153 fd = open(filename, access, 0666);
155 return AVERROR(errno);
158 h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
163 /* XXX: use llseek */
164 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
166 FileContext *c = h->priv_data;
169 if (whence == AVSEEK_SIZE) {
171 ret = fstat(c->fd, &st);
172 return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
175 ret = lseek(c->fd, pos, whence);
177 return ret < 0 ? AVERROR(errno) : ret;
180 static int file_close(URLContext *h)
182 FileContext *c = h->priv_data;
186 URLProtocol ff_file_protocol = {
188 .url_open = file_open,
189 .url_read = file_read,
190 .url_write = file_write,
191 .url_seek = file_seek,
192 .url_close = file_close,
193 .url_get_file_handle = file_get_handle,
194 .url_check = file_check,
195 .priv_data_size = sizeof(FileContext),
196 .priv_data_class = &file_class,
199 #endif /* CONFIG_FILE_PROTOCOL */
201 #if CONFIG_PIPE_PROTOCOL
203 static int pipe_open(URLContext *h, const char *filename, int flags)
205 FileContext *c = h->priv_data;
208 av_strstart(filename, "pipe:", &filename);
210 fd = strtol(filename, &final, 10);
211 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
212 if (flags & AVIO_FLAG_WRITE) {
219 setmode(fd, O_BINARY);
226 URLProtocol ff_pipe_protocol = {
228 .url_open = pipe_open,
229 .url_read = file_read,
230 .url_write = file_write,
231 .url_get_file_handle = file_get_handle,
232 .url_check = file_check,
233 .priv_data_size = sizeof(FileContext),
234 .priv_data_class = &pipe_class,
237 #endif /* CONFIG_PIPE_PROTOCOL */