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/internal.h"
24 #include "libavutil/opt.h"
38 #include "os_support.h"
41 /* Some systems may not have S_ISFIFO */
44 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
46 # define S_ISFIFO(m) 0
50 /* Not available in POSIX.1-1996 */
53 # define S_ISLNK(m) (((m) & S_IFLNK) == S_IFLNK)
59 /* Not available in POSIX.1-1996 */
62 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
64 # define S_ISSOCK(m) 0
68 /* standard file protocol */
70 typedef struct FileContext {
80 static const AVOption file_options[] = {
81 { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
82 { "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 },
86 static const AVOption pipe_options[] = {
87 { "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 },
91 static const AVClass file_class = {
93 .item_name = av_default_item_name,
94 .option = file_options,
95 .version = LIBAVUTIL_VERSION_INT,
98 static const AVClass pipe_class = {
100 .item_name = av_default_item_name,
101 .option = pipe_options,
102 .version = LIBAVUTIL_VERSION_INT,
105 static int file_read(URLContext *h, unsigned char *buf, int size)
107 FileContext *c = h->priv_data;
109 size = FFMIN(size, c->blocksize);
110 ret = read(c->fd, buf, size);
111 return (ret == -1) ? AVERROR(errno) : ret;
114 static int file_write(URLContext *h, const unsigned char *buf, int size)
116 FileContext *c = h->priv_data;
118 size = FFMIN(size, c->blocksize);
119 ret = write(c->fd, buf, size);
120 return (ret == -1) ? AVERROR(errno) : ret;
123 static int file_get_handle(URLContext *h)
125 FileContext *c = h->priv_data;
129 static int file_check(URLContext *h, int mask)
132 const char *filename = h->filename;
133 av_strstart(filename, "file:", &filename);
136 #if HAVE_ACCESS && defined(R_OK)
137 if (access(filename, F_OK) < 0)
138 return AVERROR(errno);
139 if (mask&AVIO_FLAG_READ)
140 if (access(filename, R_OK) >= 0)
141 ret |= AVIO_FLAG_READ;
142 if (mask&AVIO_FLAG_WRITE)
143 if (access(filename, W_OK) >= 0)
144 ret |= AVIO_FLAG_WRITE;
147 ret = stat(filename, &st);
149 return AVERROR(errno);
151 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
152 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
158 static int file_delete(URLContext *h)
162 const char *filename = h->filename;
163 av_strstart(filename, "file:", &filename);
165 ret = rmdir(filename);
166 if (ret < 0 && errno == ENOTDIR)
167 ret = unlink(filename);
169 return AVERROR(errno);
173 return AVERROR(ENOSYS);
174 #endif /* HAVE_UNISTD_H */
177 static int file_move(URLContext *h_src, URLContext *h_dst)
180 const char *filename_src = h_src->filename;
181 const char *filename_dst = h_dst->filename;
182 av_strstart(filename_src, "file:", &filename_src);
183 av_strstart(filename_dst, "file:", &filename_dst);
185 if (rename(filename_src, filename_dst) < 0)
186 return AVERROR(errno);
190 return AVERROR(ENOSYS);
191 #endif /* HAVE_UNISTD_H */
194 #if CONFIG_FILE_PROTOCOL
196 static int file_open(URLContext *h, const char *filename, int flags)
198 FileContext *c = h->priv_data;
203 av_strstart(filename, "file:", &filename);
205 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
206 access = O_CREAT | O_RDWR;
209 } else if (flags & AVIO_FLAG_WRITE) {
210 access = O_CREAT | O_WRONLY;
219 fd = avpriv_open(filename, access, 0666);
221 return AVERROR(errno);
224 h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
229 /* XXX: use llseek */
230 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
232 FileContext *c = h->priv_data;
235 if (whence == AVSEEK_SIZE) {
237 ret = fstat(c->fd, &st);
238 return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
241 ret = lseek(c->fd, pos, whence);
243 return ret < 0 ? AVERROR(errno) : ret;
246 static int file_close(URLContext *h)
248 FileContext *c = h->priv_data;
252 static int file_open_dir(URLContext *h)
255 FileContext *c = h->priv_data;
257 c->dir = opendir(h->filename);
259 return AVERROR(errno);
263 return AVERROR(ENOSYS);
264 #endif /* HAVE_LSTAT */
267 static int file_read_dir(URLContext *h, AVIODirEntry **next)
270 FileContext *c = h->priv_data;
272 char *fullpath = NULL;
274 *next = ff_alloc_dir_entry();
276 return AVERROR(ENOMEM);
279 dir = readdir(c->dir);
282 return AVERROR(errno);
284 } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
286 fullpath = av_append_path_component(h->filename, dir->d_name);
289 if (!lstat(fullpath, &st)) {
290 if (S_ISDIR(st.st_mode))
291 (*next)->type = AVIO_ENTRY_DIRECTORY;
292 else if (S_ISFIFO(st.st_mode))
293 (*next)->type = AVIO_ENTRY_NAMED_PIPE;
294 else if (S_ISCHR(st.st_mode))
295 (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
296 else if (S_ISBLK(st.st_mode))
297 (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
298 else if (S_ISLNK(st.st_mode))
299 (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
300 else if (S_ISSOCK(st.st_mode))
301 (*next)->type = AVIO_ENTRY_SOCKET;
302 else if (S_ISREG(st.st_mode))
303 (*next)->type = AVIO_ENTRY_FILE;
305 (*next)->type = AVIO_ENTRY_UNKNOWN;
307 (*next)->group_id = st.st_gid;
308 (*next)->user_id = st.st_uid;
309 (*next)->size = st.st_size;
310 (*next)->filemode = st.st_mode & 0777;
311 (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
312 (*next)->access_timestamp = INT64_C(1000000) * st.st_atime;
313 (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
318 (*next)->name = av_strdup(dir->d_name);
321 return AVERROR(ENOSYS);
322 #endif /* HAVE_LSTAT */
325 static int file_close_dir(URLContext *h)
328 FileContext *c = h->priv_data;
332 return AVERROR(ENOSYS);
333 #endif /* HAVE_LSTAT */
336 URLProtocol ff_file_protocol = {
338 .url_open = file_open,
339 .url_read = file_read,
340 .url_write = file_write,
341 .url_seek = file_seek,
342 .url_close = file_close,
343 .url_get_file_handle = file_get_handle,
344 .url_check = file_check,
345 .url_delete = file_delete,
346 .url_move = file_move,
347 .priv_data_size = sizeof(FileContext),
348 .priv_data_class = &file_class,
349 .url_open_dir = file_open_dir,
350 .url_read_dir = file_read_dir,
351 .url_close_dir = file_close_dir,
354 #endif /* CONFIG_FILE_PROTOCOL */
356 #if CONFIG_PIPE_PROTOCOL
358 static int pipe_open(URLContext *h, const char *filename, int flags)
360 FileContext *c = h->priv_data;
363 av_strstart(filename, "pipe:", &filename);
365 fd = strtol(filename, &final, 10);
366 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
367 if (flags & AVIO_FLAG_WRITE) {
374 setmode(fd, O_BINARY);
381 URLProtocol ff_pipe_protocol = {
383 .url_open = pipe_open,
384 .url_read = file_read,
385 .url_write = file_write,
386 .url_get_file_handle = file_get_handle,
387 .url_check = file_check,
388 .priv_data_size = sizeof(FileContext),
389 .priv_data_class = &pipe_class,
392 #endif /* CONFIG_PIPE_PROTOCOL */