FFmpeg
file.c
Go to the documentation of this file.
1 /*
2  * buffered file I/O
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "config_components.h"
23 
24 #include "libavutil/avstring.h"
25 #include "libavutil/file_open.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
28 #include "avio.h"
29 #if HAVE_DIRENT_H
30 #include <dirent.h>
31 #endif
32 #include <fcntl.h>
33 #if HAVE_IO_H
34 #include <io.h>
35 #endif
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #include <sys/stat.h>
40 #include <stdlib.h>
41 #include "os_support.h"
42 #include "url.h"
43 
44 /* Some systems may not have S_ISFIFO */
45 #ifndef S_ISFIFO
46 # ifdef S_IFIFO
47 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
48 # else
49 # define S_ISFIFO(m) 0
50 # endif
51 #endif
52 
53 /* Not available in POSIX.1-1996 */
54 #ifndef S_ISLNK
55 # ifdef S_IFLNK
56 # define S_ISLNK(m) (((m) & S_IFLNK) == S_IFLNK)
57 # else
58 # define S_ISLNK(m) 0
59 # endif
60 #endif
61 
62 /* Not available in POSIX.1-1996 */
63 #ifndef S_ISSOCK
64 # ifdef S_IFSOCK
65 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
66 # else
67 # define S_ISSOCK(m) 0
68 # endif
69 #endif
70 
71 /* standard file protocol */
72 
73 typedef struct FileContext {
74  const AVClass *class;
75  int fd;
76  int trunc;
77  int blocksize;
78  int follow;
79  int seekable;
80 #if HAVE_DIRENT_H
81  DIR *dir;
82 #endif
83 } FileContext;
84 
85 static const AVOption file_options[] = {
86  { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
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 },
88  { "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 },
89  { "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  { NULL }
91 };
92 
93 static const AVOption pipe_options[] = {
94  { "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  { NULL }
96 };
97 
98 static const AVClass file_class = {
99  .class_name = "file",
100  .item_name = av_default_item_name,
101  .option = file_options,
102  .version = LIBAVUTIL_VERSION_INT,
103 };
104 
105 static const AVClass pipe_class = {
106  .class_name = "pipe",
107  .item_name = av_default_item_name,
108  .option = pipe_options,
109  .version = LIBAVUTIL_VERSION_INT,
110 };
111 
112 static int file_read(URLContext *h, unsigned char *buf, int size)
113 {
114  FileContext *c = h->priv_data;
115  int ret;
116  size = FFMIN(size, c->blocksize);
117  ret = read(c->fd, buf, size);
118  if (ret == 0 && c->follow)
119  return AVERROR(EAGAIN);
120  if (ret == 0)
121  return AVERROR_EOF;
122  return (ret == -1) ? AVERROR(errno) : ret;
123 }
124 
125 static int file_write(URLContext *h, const unsigned char *buf, int size)
126 {
127  FileContext *c = h->priv_data;
128  int ret;
129  size = FFMIN(size, c->blocksize);
130  ret = write(c->fd, buf, size);
131  return (ret == -1) ? AVERROR(errno) : ret;
132 }
133 
135 {
136  FileContext *c = h->priv_data;
137  return c->fd;
138 }
139 
140 static int file_check(URLContext *h, int mask)
141 {
142  int ret = 0;
143  const char *filename = h->filename;
144  av_strstart(filename, "file:", &filename);
145 
146  {
147 #if HAVE_ACCESS && defined(R_OK)
148  if (access(filename, F_OK) < 0)
149  return AVERROR(errno);
150  if (mask&AVIO_FLAG_READ)
151  if (access(filename, R_OK) >= 0)
152  ret |= AVIO_FLAG_READ;
153  if (mask&AVIO_FLAG_WRITE)
154  if (access(filename, W_OK) >= 0)
155  ret |= AVIO_FLAG_WRITE;
156 #else
157  struct stat st;
158  ret = stat(filename, &st);
159  if (ret < 0)
160  return AVERROR(errno);
161 
162  ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
163  ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
164 #endif
165  }
166  return ret;
167 }
168 
169 #if CONFIG_FILE_PROTOCOL
170 
171 static int file_delete(URLContext *h)
172 {
173 #if HAVE_UNISTD_H
174  int ret;
175  const char *filename = h->filename;
176  av_strstart(filename, "file:", &filename);
177 
178  ret = rmdir(filename);
179  if (ret < 0 && (errno == ENOTDIR
180 # ifdef _WIN32
181  || errno == EINVAL
182 # endif
183  ))
184  ret = unlink(filename);
185  if (ret < 0)
186  return AVERROR(errno);
187 
188  return ret;
189 #else
190  return AVERROR(ENOSYS);
191 #endif /* HAVE_UNISTD_H */
192 }
193 
194 static int file_move(URLContext *h_src, URLContext *h_dst)
195 {
196  const char *filename_src = h_src->filename;
197  const char *filename_dst = h_dst->filename;
198  av_strstart(filename_src, "file:", &filename_src);
199  av_strstart(filename_dst, "file:", &filename_dst);
200 
201  if (rename(filename_src, filename_dst) < 0)
202  return AVERROR(errno);
203 
204  return 0;
205 }
206 
207 static int file_open(URLContext *h, const char *filename, int flags)
208 {
209  FileContext *c = h->priv_data;
210  int access;
211  int fd;
212  struct stat st;
213 
214  av_strstart(filename, "file:", &filename);
215 
217  access = O_CREAT | O_RDWR;
218  if (c->trunc)
219  access |= O_TRUNC;
220  } else if (flags & AVIO_FLAG_WRITE) {
221  access = O_CREAT | O_WRONLY;
222  if (c->trunc)
223  access |= O_TRUNC;
224  } else {
225  access = O_RDONLY;
226  }
227 #ifdef O_BINARY
228  access |= O_BINARY;
229 #endif
230  fd = avpriv_open(filename, access, 0666);
231  if (fd == -1)
232  return AVERROR(errno);
233  c->fd = fd;
234 
235  h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
236 
237  /* Buffer writes more than the default 32k to improve throughput especially
238  * with networked file systems */
239  if (!h->is_streamed && flags & AVIO_FLAG_WRITE)
240  h->min_packet_size = h->max_packet_size = 262144;
241 
242  if (c->seekable >= 0)
243  h->is_streamed = !c->seekable;
244 
245  return 0;
246 }
247 
248 /* XXX: use llseek */
249 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
250 {
251  FileContext *c = h->priv_data;
252  int64_t ret;
253 
254  if (whence == AVSEEK_SIZE) {
255  struct stat st;
256  ret = fstat(c->fd, &st);
257  return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
258  }
259 
260  ret = lseek(c->fd, pos, whence);
261 
262  return ret < 0 ? AVERROR(errno) : ret;
263 }
264 
265 static int file_close(URLContext *h)
266 {
267  FileContext *c = h->priv_data;
268  int ret = close(c->fd);
269  return (ret == -1) ? AVERROR(errno) : 0;
270 }
271 
272 static int file_open_dir(URLContext *h)
273 {
274 #if HAVE_LSTAT
275  FileContext *c = h->priv_data;
276 
277  c->dir = opendir(h->filename);
278  if (!c->dir)
279  return AVERROR(errno);
280 
281  return 0;
282 #else
283  return AVERROR(ENOSYS);
284 #endif /* HAVE_LSTAT */
285 }
286 
287 static int file_read_dir(URLContext *h, AVIODirEntry **next)
288 {
289 #if HAVE_LSTAT
290  FileContext *c = h->priv_data;
291  struct dirent *dir;
292  char *fullpath = NULL;
293 
294  *next = ff_alloc_dir_entry();
295  if (!*next)
296  return AVERROR(ENOMEM);
297  do {
298  errno = 0;
299  dir = readdir(c->dir);
300  if (!dir) {
301  av_freep(next);
302  return AVERROR(errno);
303  }
304  } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
305 
306  fullpath = av_append_path_component(h->filename, dir->d_name);
307  if (fullpath) {
308  struct stat st;
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;
324  else
325  (*next)->type = AVIO_ENTRY_UNKNOWN;
326 
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;
334  }
335  av_free(fullpath);
336  }
337 
338  (*next)->name = av_strdup(dir->d_name);
339  return 0;
340 #else
341  return AVERROR(ENOSYS);
342 #endif /* HAVE_LSTAT */
343 }
344 
345 static int file_close_dir(URLContext *h)
346 {
347 #if HAVE_LSTAT
348  FileContext *c = h->priv_data;
349  closedir(c->dir);
350  return 0;
351 #else
352  return AVERROR(ENOSYS);
353 #endif /* HAVE_LSTAT */
354 }
355 
357  .name = "file",
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"
373 };
374 
375 #endif /* CONFIG_FILE_PROTOCOL */
376 
377 #if CONFIG_PIPE_PROTOCOL
378 
379 static int pipe_open(URLContext *h, const char *filename, int flags)
380 {
381  FileContext *c = h->priv_data;
382  int fd;
383  char *final;
384  av_strstart(filename, "pipe:", &filename);
385 
386  fd = strtol(filename, &final, 10);
387  if((filename == final) || *final ) {/* No digits found, or something like 10ab */
388  if (flags & AVIO_FLAG_WRITE) {
389  fd = 1;
390  } else {
391  fd = 0;
392  }
393  }
394 #if HAVE_SETMODE
395  setmode(fd, O_BINARY);
396 #endif
397  c->fd = fd;
398  h->is_streamed = 1;
399  return 0;
400 }
401 
403  .name = "pipe",
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"
412 };
413 
414 #endif /* CONFIG_PIPE_PROTOCOL */
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
S_ISSOCK
#define S_ISSOCK(m)
Definition: file.c:67
URLContext::filename
char * filename
specified URL
Definition: url.h:41
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVOption
AVOption.
Definition: opt.h:251
AVSEEK_SIZE
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:484
AVIO_ENTRY_NAMED_PIPE
@ AVIO_ENTRY_NAMED_PIPE
Definition: avio.h:72
file_check
static int file_check(URLContext *h, int mask)
Definition: file.c:140
URLProtocol
Definition: url.h:53
os_support.h
AVIO_ENTRY_UNKNOWN
@ AVIO_ENTRY_UNKNOWN
Definition: avio.h:68
av_append_path_component
char * av_append_path_component(const char *path, const char *component)
Append path component to the existing path.
Definition: avstring.c:307
AVIO_ENTRY_DIRECTORY
@ AVIO_ENTRY_DIRECTORY
Definition: avio.h:71
trunc
static __device__ float trunc(float a)
Definition: cuda_runtime.h:179
AVIO_ENTRY_CHARACTER_DEVICE
@ AVIO_ENTRY_CHARACTER_DEVICE
Definition: avio.h:70
FileContext::blocksize
int blocksize
Definition: file.c:77
file_write
static int file_write(URLContext *h, const unsigned char *buf, int size)
Definition: file.c:125
file_read
static int file_read(URLContext *h, unsigned char *buf, int size)
Definition: file.c:112
AVIO_ENTRY_SYMBOLIC_LINK
@ AVIO_ENTRY_SYMBOLIC_LINK
Definition: avio.h:73
mask
static const uint16_t mask[17]
Definition: lzw.c:38
FileContext::seekable
int seekable
Definition: file.c:79
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:281
S_ISFIFO
#define S_ISFIFO(m)
Definition: file.c:49
avpriv_open
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:67
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:634
FileContext
Definition: file.c:73
pipe_class
static const AVClass pipe_class
Definition: file.c:105
file_open.h
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AVIO_ENTRY_FILE
@ AVIO_ENTRY_FILE
Definition: avio.h:75
FileContext::fd
int fd
Definition: file.c:75
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
pipe_options
static const AVOption pipe_options[]
Definition: file.c:93
FileContext::follow
int follow
Definition: file.c:78
ff_file_protocol
const URLProtocol ff_file_protocol
size
int size
Definition: twinvq_data.h:10344
AVIODirEntry
Describes single entry of the directory.
Definition: avio.h:87
avio.h
URLProtocol::name
const char * name
Definition: url.h:54
FileContext::trunc
int trunc
Definition: file.c:76
av_strstart
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:37
AVIO_ENTRY_SOCKET
@ AVIO_ENTRY_SOCKET
Definition: avio.h:74
ff_alloc_dir_entry
AVIODirEntry * ff_alloc_dir_entry(void)
Allocate directory entry with default values.
Definition: url.c:325
URLContext
Definition: url.h:37
file_options
static const AVOption file_options[]
Definition: file.c:85
internal.h
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
url.h
file_class
static const AVClass file_class
Definition: file.c:98
S_ISLNK
#define S_ISLNK(m)
Definition: file.c:58
O_BINARY
#define O_BINARY
ff_pipe_protocol
const URLProtocol ff_pipe_protocol
AVIO_ENTRY_BLOCK_DEVICE
@ AVIO_ENTRY_BLOCK_DEVICE
Definition: avio.h:69
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
file_get_handle
static int file_get_handle(URLContext *h)
Definition: file.c:134
pos
unsigned int pos
Definition: spdifenc.c:412
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:633
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:280
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
h
h
Definition: vp9dsp_template.c:2038
avstring.h