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 {
81 static const AVOption file_options[] = {
82 { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
83 { "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 },
84 { "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 },
88 static const AVOption pipe_options[] = {
89 { "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 },
93 static const AVClass file_class = {
95 .item_name = av_default_item_name,
96 .option = file_options,
97 .version = LIBAVUTIL_VERSION_INT,
100 static const AVClass pipe_class = {
101 .class_name = "pipe",
102 .item_name = av_default_item_name,
103 .option = pipe_options,
104 .version = LIBAVUTIL_VERSION_INT,
107 static int file_read(URLContext *h, unsigned char *buf, int size)
109 FileContext *c = h->priv_data;
111 size = FFMIN(size, c->blocksize);
112 ret = read(c->fd, buf, size);
113 if (ret == 0 && c->follow)
114 return AVERROR(EAGAIN);
117 return (ret == -1) ? AVERROR(errno) : ret;
120 static int file_write(URLContext *h, const unsigned char *buf, int size)
122 FileContext *c = h->priv_data;
124 size = FFMIN(size, c->blocksize);
125 ret = write(c->fd, buf, size);
126 return (ret == -1) ? AVERROR(errno) : ret;
129 static int file_get_handle(URLContext *h)
131 FileContext *c = h->priv_data;
135 static int file_check(URLContext *h, int mask)
138 const char *filename = h->filename;
139 av_strstart(filename, "file:", &filename);
142 #if HAVE_ACCESS && defined(R_OK)
143 if (access(filename, F_OK) < 0)
144 return AVERROR(errno);
145 if (mask&AVIO_FLAG_READ)
146 if (access(filename, R_OK) >= 0)
147 ret |= AVIO_FLAG_READ;
148 if (mask&AVIO_FLAG_WRITE)
149 if (access(filename, W_OK) >= 0)
150 ret |= AVIO_FLAG_WRITE;
154 ret = stat(filename, &st);
156 ret = win32_stat(filename, &st);
159 return AVERROR(errno);
161 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
162 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
168 static int file_delete(URLContext *h)
172 const char *filename = h->filename;
173 av_strstart(filename, "file:", &filename);
175 ret = rmdir(filename);
176 if (ret < 0 && errno == ENOTDIR)
177 ret = unlink(filename);
179 return AVERROR(errno);
183 return AVERROR(ENOSYS);
184 #endif /* HAVE_UNISTD_H */
187 static int file_move(URLContext *h_src, URLContext *h_dst)
189 const char *filename_src = h_src->filename;
190 const char *filename_dst = h_dst->filename;
191 av_strstart(filename_src, "file:", &filename_src);
192 av_strstart(filename_dst, "file:", &filename_dst);
194 if (rename(filename_src, filename_dst) < 0)
195 return AVERROR(errno);
200 #if CONFIG_FILE_PROTOCOL
202 static int file_open(URLContext *h, const char *filename, int flags)
204 FileContext *c = h->priv_data;
209 av_strstart(filename, "file:", &filename);
211 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
212 access = O_CREAT | O_RDWR;
215 } else if (flags & AVIO_FLAG_WRITE) {
216 access = O_CREAT | O_WRONLY;
225 fd = avpriv_open(filename, access, 0666);
227 return AVERROR(errno);
230 h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
232 /* Buffer writes more than the default 32k to improve throughput especially
233 * with networked file systems */
234 if (!h->is_streamed && flags & AVIO_FLAG_WRITE)
235 h->min_packet_size = h->max_packet_size = 262144;
240 /* XXX: use llseek */
241 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
243 FileContext *c = h->priv_data;
246 if (whence == AVSEEK_SIZE) {
248 ret = fstat(c->fd, &st);
249 return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
252 ret = lseek(c->fd, pos, whence);
254 return ret < 0 ? AVERROR(errno) : ret;
257 static int file_close(URLContext *h)
259 FileContext *c = h->priv_data;
263 static int file_open_dir(URLContext *h)
266 FileContext *c = h->priv_data;
268 c->dir = opendir(h->filename);
270 return AVERROR(errno);
274 return AVERROR(ENOSYS);
275 #endif /* HAVE_LSTAT */
278 static int file_read_dir(URLContext *h, AVIODirEntry **next)
281 FileContext *c = h->priv_data;
283 char *fullpath = NULL;
285 *next = ff_alloc_dir_entry();
287 return AVERROR(ENOMEM);
290 dir = readdir(c->dir);
293 return AVERROR(errno);
295 } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
297 fullpath = av_append_path_component(h->filename, dir->d_name);
300 if (!lstat(fullpath, &st)) {
301 if (S_ISDIR(st.st_mode))
302 (*next)->type = AVIO_ENTRY_DIRECTORY;
303 else if (S_ISFIFO(st.st_mode))
304 (*next)->type = AVIO_ENTRY_NAMED_PIPE;
305 else if (S_ISCHR(st.st_mode))
306 (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
307 else if (S_ISBLK(st.st_mode))
308 (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
309 else if (S_ISLNK(st.st_mode))
310 (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
311 else if (S_ISSOCK(st.st_mode))
312 (*next)->type = AVIO_ENTRY_SOCKET;
313 else if (S_ISREG(st.st_mode))
314 (*next)->type = AVIO_ENTRY_FILE;
316 (*next)->type = AVIO_ENTRY_UNKNOWN;
318 (*next)->group_id = st.st_gid;
319 (*next)->user_id = st.st_uid;
320 (*next)->size = st.st_size;
321 (*next)->filemode = st.st_mode & 0777;
322 (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
323 (*next)->access_timestamp = INT64_C(1000000) * st.st_atime;
324 (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
329 (*next)->name = av_strdup(dir->d_name);
332 return AVERROR(ENOSYS);
333 #endif /* HAVE_LSTAT */
336 static int file_close_dir(URLContext *h)
339 FileContext *c = h->priv_data;
343 return AVERROR(ENOSYS);
344 #endif /* HAVE_LSTAT */
347 const URLProtocol ff_file_protocol = {
349 .url_open = file_open,
350 .url_read = file_read,
351 .url_write = file_write,
352 .url_seek = file_seek,
353 .url_close = file_close,
354 .url_get_file_handle = file_get_handle,
355 .url_check = file_check,
356 .url_delete = file_delete,
357 .url_move = file_move,
358 .priv_data_size = sizeof(FileContext),
359 .priv_data_class = &file_class,
360 .url_open_dir = file_open_dir,
361 .url_read_dir = file_read_dir,
362 .url_close_dir = file_close_dir,
363 .default_whitelist = "file,crypto"
366 #endif /* CONFIG_FILE_PROTOCOL */
368 #if CONFIG_PIPE_PROTOCOL
370 static int pipe_open(URLContext *h, const char *filename, int flags)
372 FileContext *c = h->priv_data;
375 av_strstart(filename, "pipe:", &filename);
377 fd = strtol(filename, &final, 10);
378 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
379 if (flags & AVIO_FLAG_WRITE) {
386 setmode(fd, O_BINARY);
393 const URLProtocol ff_pipe_protocol = {
395 .url_open = pipe_open,
396 .url_read = file_read,
397 .url_write = file_write,
398 .url_get_file_handle = file_get_handle,
399 .url_check = file_check,
400 .priv_data_size = sizeof(FileContext),
401 .priv_data_class = &pipe_class,
402 .default_whitelist = "crypto"
405 #endif /* CONFIG_PIPE_PROTOCOL */