3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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/internal.h"
24 #include "libavutil/opt.h"
35 #include "os_support.h"
39 /* standard file protocol */
41 typedef struct FileContext {
48 static const AVOption file_options[] = {
49 { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
50 { "follow", "Follow a file as it is being written", offsetof(FileContext, follow), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
54 static const AVClass file_class = {
56 .item_name = av_default_item_name,
57 .option = file_options,
58 .version = LIBAVUTIL_VERSION_INT,
61 static int file_read(URLContext *h, unsigned char *buf, int size)
63 FileContext *c = h->priv_data;
64 int ret = read(c->fd, buf, size);
65 if (ret == 0 && c->follow)
66 return AVERROR(EAGAIN);
67 return (ret == -1) ? AVERROR(errno) : ret;
70 static int file_write(URLContext *h, const unsigned char *buf, int size)
72 FileContext *c = h->priv_data;
73 int ret = write(c->fd, buf, size);
74 return (ret == -1) ? AVERROR(errno) : ret;
77 static int file_get_handle(URLContext *h)
79 FileContext *c = h->priv_data;
83 static int file_check(URLContext *h, int mask)
85 const char *filename = h->filename;
89 av_strstart(filename, "file:", &filename);
91 ret = stat(filename, &st);
93 return AVERROR(errno);
95 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
96 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
101 #if CONFIG_FILE_PROTOCOL
103 static int file_open(URLContext *h, const char *filename, int flags)
105 FileContext *c = h->priv_data;
109 av_strstart(filename, "file:", &filename);
111 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
112 access = O_CREAT | O_RDWR;
115 } else if (flags & AVIO_FLAG_WRITE) {
116 access = O_CREAT | O_WRONLY;
125 fd = avpriv_open(filename, access, 0666);
127 return AVERROR(errno);
132 /* XXX: use llseek */
133 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
135 FileContext *c = h->priv_data;
138 if (whence == AVSEEK_SIZE) {
141 ret = fstat(c->fd, &st);
142 return ret < 0 ? AVERROR(errno) : st.st_size;
145 ret = lseek(c->fd, pos, whence);
147 return ret < 0 ? AVERROR(errno) : ret;
150 static int file_close(URLContext *h)
152 FileContext *c = h->priv_data;
156 const URLProtocol ff_file_protocol = {
158 .url_open = file_open,
159 .url_read = file_read,
160 .url_write = file_write,
161 .url_seek = file_seek,
162 .url_close = file_close,
163 .url_get_file_handle = file_get_handle,
164 .url_check = file_check,
165 .priv_data_size = sizeof(FileContext),
166 .priv_data_class = &file_class,
169 #endif /* CONFIG_FILE_PROTOCOL */
171 #if CONFIG_PIPE_PROTOCOL
173 static int pipe_open(URLContext *h, const char *filename, int flags)
175 FileContext *c = h->priv_data;
178 av_strstart(filename, "pipe:", &filename);
180 fd = strtol(filename, &final, 10);
181 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
182 if (flags & AVIO_FLAG_WRITE) {
189 setmode(fd, O_BINARY);
196 const URLProtocol ff_pipe_protocol = {
198 .url_open = pipe_open,
199 .url_read = file_read,
200 .url_write = file_write,
201 .url_get_file_handle = file_get_handle,
202 .url_check = file_check,
203 .priv_data_size = sizeof(FileContext),
206 #endif /* CONFIG_PIPE_PROTOCOL */