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 {
54 static const AVOption file_options[] = {
55 { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
59 static const AVClass file_class = {
61 .item_name = av_default_item_name,
62 .option = file_options,
63 .version = LIBAVUTIL_VERSION_INT,
66 static int file_read(URLContext *h, unsigned char *buf, int size)
68 FileContext *c = h->priv_data;
69 int r = read(c->fd, buf, size);
70 return (-1 == r)?AVERROR(errno):r;
73 static int file_write(URLContext *h, const unsigned char *buf, int size)
75 FileContext *c = h->priv_data;
76 int r = write(c->fd, buf, size);
77 return (-1 == r)?AVERROR(errno):r;
80 static int file_get_handle(URLContext *h)
82 FileContext *c = h->priv_data;
86 static int file_check(URLContext *h, int mask)
89 int ret = stat(h->filename, &st);
91 return AVERROR(errno);
93 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
94 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
99 #if CONFIG_FILE_PROTOCOL
101 static int file_open(URLContext *h, const char *filename, int flags)
103 FileContext *c = h->priv_data;
108 av_strstart(filename, "file:", &filename);
110 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
111 access = O_CREAT | O_RDWR;
114 } else if (flags & AVIO_FLAG_WRITE) {
115 access = O_CREAT | O_WRONLY;
124 fd = open(filename, access, 0666);
126 return AVERROR(errno);
129 h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
134 /* XXX: use llseek */
135 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
137 FileContext *c = h->priv_data;
140 if (whence == AVSEEK_SIZE) {
142 ret = fstat(c->fd, &st);
143 return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
146 ret = lseek(c->fd, pos, whence);
148 return ret < 0 ? AVERROR(errno) : ret;
151 static int file_close(URLContext *h)
153 FileContext *c = h->priv_data;
157 URLProtocol ff_file_protocol = {
159 .url_open = file_open,
160 .url_read = file_read,
161 .url_write = file_write,
162 .url_seek = file_seek,
163 .url_close = file_close,
164 .url_get_file_handle = file_get_handle,
165 .url_check = file_check,
166 .priv_data_size = sizeof(FileContext),
167 .priv_data_class = &file_class,
170 #endif /* CONFIG_FILE_PROTOCOL */
172 #if CONFIG_PIPE_PROTOCOL
174 static int pipe_open(URLContext *h, const char *filename, int flags)
176 FileContext *c = h->priv_data;
179 av_strstart(filename, "pipe:", &filename);
181 fd = strtol(filename, &final, 10);
182 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
183 if (flags & AVIO_FLAG_WRITE) {
190 setmode(fd, O_BINARY);
197 URLProtocol ff_pipe_protocol = {
199 .url_open = pipe_open,
200 .url_read = file_read,
201 .url_write = file_write,
202 .url_get_file_handle = file_get_handle,
203 .url_check = file_check,
204 .priv_data_size = sizeof(FileContext),
207 #endif /* CONFIG_PIPE_PROTOCOL */