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 {
47 static const AVOption file_options[] = {
48 { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
52 static const AVClass file_class = {
54 .item_name = av_default_item_name,
55 .option = file_options,
56 .version = LIBAVUTIL_VERSION_INT,
59 static int file_read(URLContext *h, unsigned char *buf, int size)
61 FileContext *c = h->priv_data;
62 int ret = read(c->fd, buf, size);
63 return (ret == -1) ? AVERROR(errno) : ret;
66 static int file_write(URLContext *h, const unsigned char *buf, int size)
68 FileContext *c = h->priv_data;
69 int ret = write(c->fd, buf, size);
70 return (ret == -1) ? AVERROR(errno) : ret;
73 static int file_get_handle(URLContext *h)
75 FileContext *c = h->priv_data;
79 static int file_check(URLContext *h, int mask)
82 int ret = stat(h->filename, &st);
84 return AVERROR(errno);
86 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
87 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
92 #if CONFIG_FILE_PROTOCOL
94 static int file_open(URLContext *h, const char *filename, int flags)
96 FileContext *c = h->priv_data;
100 av_strstart(filename, "file:", &filename);
102 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
103 access = O_CREAT | O_RDWR;
106 } else if (flags & AVIO_FLAG_WRITE) {
107 access = O_CREAT | O_WRONLY;
116 fd = avpriv_open(filename, access, 0666);
118 return AVERROR(errno);
123 /* XXX: use llseek */
124 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
126 FileContext *c = h->priv_data;
129 if (whence == AVSEEK_SIZE) {
132 ret = fstat(c->fd, &st);
133 return ret < 0 ? AVERROR(errno) : st.st_size;
136 ret = lseek(c->fd, pos, whence);
138 return ret < 0 ? AVERROR(errno) : ret;
141 static int file_close(URLContext *h)
143 FileContext *c = h->priv_data;
147 const URLProtocol ff_file_protocol = {
149 .url_open = file_open,
150 .url_read = file_read,
151 .url_write = file_write,
152 .url_seek = file_seek,
153 .url_close = file_close,
154 .url_get_file_handle = file_get_handle,
155 .url_check = file_check,
156 .priv_data_size = sizeof(FileContext),
157 .priv_data_class = &file_class,
160 #endif /* CONFIG_FILE_PROTOCOL */
162 #if CONFIG_PIPE_PROTOCOL
164 static int pipe_open(URLContext *h, const char *filename, int flags)
166 FileContext *c = h->priv_data;
169 av_strstart(filename, "pipe:", &filename);
171 fd = strtol(filename, &final, 10);
172 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
173 if (flags & AVIO_FLAG_WRITE) {
180 setmode(fd, O_BINARY);
187 const URLProtocol ff_pipe_protocol = {
189 .url_open = pipe_open,
190 .url_read = file_read,
191 .url_write = file_write,
192 .url_get_file_handle = file_get_handle,
193 .url_check = file_check,
194 .priv_data_size = sizeof(FileContext),
197 #endif /* CONFIG_PIPE_PROTOCOL */