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
{
82 static const AVOption file_options
[] = {
83 { "truncate", "truncate existing files on write", offsetof(FileContext
, trunc
), AV_OPT_TYPE_BOOL
, { .i64
= 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM
},
84 { "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
},
85 { "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
},
86 { "seekable", "Sets if the file is seekable", offsetof(FileContext
, seekable
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, 0, AV_OPT_FLAG_DECODING_PARAM
| AV_OPT_FLAG_ENCODING_PARAM
},
90 static const AVOption pipe_options
[] = {
91 { "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
},
95 static const AVClass file_class
= {
97 .item_name
= av_default_item_name
,
98 .option
= file_options
,
99 .version
= LIBAVUTIL_VERSION_INT
,
102 static const AVClass pipe_class
= {
103 .class_name
= "pipe",
104 .item_name
= av_default_item_name
,
105 .option
= pipe_options
,
106 .version
= LIBAVUTIL_VERSION_INT
,
109 static int file_read(URLContext
*h
, unsigned char *buf
, int size
)
111 FileContext
*c
= h
->priv_data
;
113 size
= FFMIN(size
, c
->blocksize
);
114 ret
= read(c
->fd
, buf
, size
);
115 if (ret
== 0 && c
->follow
)
116 return AVERROR(EAGAIN
);
119 return (ret
== -1) ? AVERROR(errno
) : ret
;
122 static int file_write(URLContext
*h
, const unsigned char *buf
, int size
)
124 FileContext
*c
= h
->priv_data
;
126 size
= FFMIN(size
, c
->blocksize
);
127 ret
= write(c
->fd
, buf
, size
);
128 return (ret
== -1) ? AVERROR(errno
) : ret
;
131 static int file_get_handle(URLContext
*h
)
133 FileContext
*c
= h
->priv_data
;
137 static int file_check(URLContext
*h
, int mask
)
140 const char *filename
= h
->filename
;
141 av_strstart(filename
, "file:", &filename
);
144 #if HAVE_ACCESS && defined(R_OK)
145 if (access(filename
, F_OK
) < 0)
146 return AVERROR(errno
);
147 if (mask
&AVIO_FLAG_READ
)
148 if (access(filename
, R_OK
) >= 0)
149 ret
|= AVIO_FLAG_READ
;
150 if (mask
&AVIO_FLAG_WRITE
)
151 if (access(filename
, W_OK
) >= 0)
152 ret
|= AVIO_FLAG_WRITE
;
156 ret
= stat(filename
, &st
);
158 ret
= win32_stat(filename
, &st
);
161 return AVERROR(errno
);
163 ret
|= st
.st_mode
&S_IRUSR
? mask
&AVIO_FLAG_READ
: 0;
164 ret
|= st
.st_mode
&S_IWUSR
? mask
&AVIO_FLAG_WRITE
: 0;
170 static int file_delete(URLContext
*h
)
174 const char *filename
= h
->filename
;
175 av_strstart(filename
, "file:", &filename
);
177 ret
= rmdir(filename
);
178 if (ret
< 0 && (errno
== ENOTDIR
183 ret
= unlink(filename
);
185 return AVERROR(errno
);
189 return AVERROR(ENOSYS
);
190 #endif /* HAVE_UNISTD_H */
193 static int file_move(URLContext
*h_src
, URLContext
*h_dst
)
195 const char *filename_src
= h_src
->filename
;
196 const char *filename_dst
= h_dst
->filename
;
197 av_strstart(filename_src
, "file:", &filename_src
);
198 av_strstart(filename_dst
, "file:", &filename_dst
);
200 if (rename(filename_src
, filename_dst
) < 0)
201 return AVERROR(errno
);
206 #if CONFIG_FILE_PROTOCOL
208 static int file_open(URLContext
*h
, const char *filename
, int flags
)
210 FileContext
*c
= h
->priv_data
;
215 av_strstart(filename
, "file:", &filename
);
217 if (flags
& AVIO_FLAG_WRITE
&& flags
& AVIO_FLAG_READ
) {
218 access
= O_CREAT
| O_RDWR
;
221 } else if (flags
& AVIO_FLAG_WRITE
) {
222 access
= O_CREAT
| O_WRONLY
;
231 fd
= avpriv_open(filename
, access
, 0666);
233 return AVERROR(errno
);
236 h
->is_streamed
= !fstat(fd
, &st
) && S_ISFIFO(st
.st_mode
);
238 /* Buffer writes more than the default 32k to improve throughput especially
239 * with networked file systems */
240 if (!h
->is_streamed
&& flags
& AVIO_FLAG_WRITE
)
241 h
->min_packet_size
= h
->max_packet_size
= 262144;
243 if (c
->seekable
>= 0)
244 h
->is_streamed
= !c
->seekable
;
249 /* XXX: use llseek */
250 static int64_t file_seek(URLContext
*h
, int64_t pos
, int whence
)
252 FileContext
*c
= h
->priv_data
;
255 if (whence
== AVSEEK_SIZE
) {
257 ret
= fstat(c
->fd
, &st
);
258 return ret
< 0 ? AVERROR(errno
) : (S_ISFIFO(st
.st_mode
) ? 0 : st
.st_size
);
261 ret
= lseek(c
->fd
, pos
, whence
);
263 return ret
< 0 ? AVERROR(errno
) : ret
;
266 static int file_close(URLContext
*h
)
268 FileContext
*c
= h
->priv_data
;
272 static int file_open_dir(URLContext
*h
)
275 FileContext
*c
= h
->priv_data
;
277 c
->dir
= opendir(h
->filename
);
279 return AVERROR(errno
);
283 return AVERROR(ENOSYS
);
284 #endif /* HAVE_LSTAT */
287 static int file_read_dir(URLContext
*h
, AVIODirEntry
**next
)
290 FileContext
*c
= h
->priv_data
;
292 char *fullpath
= NULL
;
294 *next
= ff_alloc_dir_entry();
296 return AVERROR(ENOMEM
);
299 dir
= readdir(c
->dir
);
302 return AVERROR(errno
);
304 } while (!strcmp(dir
->d_name
, ".") || !strcmp(dir
->d_name
, ".."));
306 fullpath
= av_append_path_component(h
->filename
, dir
->d_name
);
309 if (!lstat(fullpath
, &st
)) {
310 if (S_ISDIR(st
.st_mode
))
311 (*next
)->type
= AVIO_ENTRY_DIRECTORY
;
312 else if (S_ISFIFO(st
.st_mode
))
313 (*next
)->type
= AVIO_ENTRY_NAMED_PIPE
;
314 else if (S_ISCHR(st
.st_mode
))
315 (*next
)->type
= AVIO_ENTRY_CHARACTER_DEVICE
;
316 else if (S_ISBLK(st
.st_mode
))
317 (*next
)->type
= AVIO_ENTRY_BLOCK_DEVICE
;
318 else if (S_ISLNK(st
.st_mode
))
319 (*next
)->type
= AVIO_ENTRY_SYMBOLIC_LINK
;
320 else if (S_ISSOCK(st
.st_mode
))
321 (*next
)->type
= AVIO_ENTRY_SOCKET
;
322 else if (S_ISREG(st
.st_mode
))
323 (*next
)->type
= AVIO_ENTRY_FILE
;
325 (*next
)->type
= AVIO_ENTRY_UNKNOWN
;
327 (*next
)->group_id
= st
.st_gid
;
328 (*next
)->user_id
= st
.st_uid
;
329 (*next
)->size
= st
.st_size
;
330 (*next
)->filemode
= st
.st_mode
& 0777;
331 (*next
)->modification_timestamp
= INT64_C(1000000) * st
.st_mtime
;
332 (*next
)->access_timestamp
= INT64_C(1000000) * st
.st_atime
;
333 (*next
)->status_change_timestamp
= INT64_C(1000000) * st
.st_ctime
;
338 (*next
)->name
= av_strdup(dir
->d_name
);
341 return AVERROR(ENOSYS
);
342 #endif /* HAVE_LSTAT */
345 static int file_close_dir(URLContext
*h
)
348 FileContext
*c
= h
->priv_data
;
352 return AVERROR(ENOSYS
);
353 #endif /* HAVE_LSTAT */
356 const URLProtocol ff_file_protocol
= {
358 .url_open
= file_open
,
359 .url_read
= file_read
,
360 .url_write
= file_write
,
361 .url_seek
= file_seek
,
362 .url_close
= file_close
,
363 .url_get_file_handle
= file_get_handle
,
364 .url_check
= file_check
,
365 .url_delete
= file_delete
,
366 .url_move
= file_move
,
367 .priv_data_size
= sizeof(FileContext
),
368 .priv_data_class
= &file_class
,
369 .url_open_dir
= file_open_dir
,
370 .url_read_dir
= file_read_dir
,
371 .url_close_dir
= file_close_dir
,
372 .default_whitelist
= "file,crypto,data"
375 #endif /* CONFIG_FILE_PROTOCOL */
377 #if CONFIG_PIPE_PROTOCOL
379 static int pipe_open(URLContext
*h
, const char *filename
, int flags
)
381 FileContext
*c
= h
->priv_data
;
384 av_strstart(filename
, "pipe:", &filename
);
386 fd
= strtol(filename
, &final
, 10);
387 if((filename
== final
) || *final
) {/* No digits found, or something like 10ab */
388 if (flags
& AVIO_FLAG_WRITE
) {
395 setmode(fd
, O_BINARY
);
402 const URLProtocol ff_pipe_protocol
= {
404 .url_open
= pipe_open
,
405 .url_read
= file_read
,
406 .url_write
= file_write
,
407 .url_get_file_handle
= file_get_handle
,
408 .url_check
= file_check
,
409 .priv_data_size
= sizeof(FileContext
),
410 .priv_data_class
= &pipe_class
,
411 .default_whitelist
= "crypto,data"
414 #endif /* CONFIG_PIPE_PROTOCOL */