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 "libavutil/avstring.h"
23 #include "libavutil/internal.h"
24 #include "libavutil/opt.h"
25 #include "avformat.h"
26 #if HAVE_DIRENT_H
27 #include <dirent.h>
28 #endif
29 #include <fcntl.h>
30 #if HAVE_IO_H
31 #include <io.h>
32 #endif
33 #if HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <sys/stat.h>
37 #include <stdlib.h>
38 #include "os_support.h"
39 #include "url.h"
40 
41 /* Some systems may not have S_ISFIFO */
42 #ifndef S_ISFIFO
43 # ifdef S_IFIFO
44 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
45 # else
46 # define S_ISFIFO(m) 0
47 # endif
48 #endif
49 
50 /* Not available in POSIX.1-1996 */
51 #ifndef S_ISLNK
52 # ifdef S_IFLNK
53 # define S_ISLNK(m) (((m) & S_IFLNK) == S_IFLNK)
54 # else
55 # define S_ISLNK(m) 0
56 # endif
57 #endif
58 
59 /* Not available in POSIX.1-1996 */
60 #ifndef S_ISSOCK
61 # ifdef S_IFSOCK
62 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
63 # else
64 # define S_ISSOCK(m) 0
65 # endif
66 #endif
67 
68 /* standard file protocol */
69 
70 typedef struct FileContext {
71  const AVClass *class;
72  int fd;
73  int trunc;
74  int blocksize;
75  int follow;
76  int seekable;
77 #if HAVE_DIRENT_H
78  DIR *dir;
79 #endif
80 } FileContext;
81 
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 },
87  { NULL }
88 };
89 
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 },
92  { NULL }
93 };
94 
95 static const AVClass file_class = {
96  .class_name = "file",
97  .item_name = av_default_item_name,
98  .option = file_options,
99  .version = LIBAVUTIL_VERSION_INT,
100 };
101 
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,
107 };
108 
109 static int file_read(URLContext *h, unsigned char *buf, int size)
110 {
111  FileContext *c = h->priv_data;
112  int ret;
113  size = FFMIN(size, c->blocksize);
114  ret = read(c->fd, buf, size);
115  if (ret == 0 && c->follow)
116  return AVERROR(EAGAIN);
117  if (ret == 0)
118  return AVERROR_EOF;
119  return (ret == -1) ? AVERROR(errno) : ret;
120 }
121 
122 static int file_write(URLContext *h, const unsigned char *buf, int size)
123 {
124  FileContext *c = h->priv_data;
125  int ret;
126  size = FFMIN(size, c->blocksize);
127  ret = write(c->fd, buf, size);
128  return (ret == -1) ? AVERROR(errno) : ret;
129 }
130 
132 {
133  FileContext *c = h->priv_data;
134  return c->fd;
135 }
136 
137 static int file_check(URLContext *h, int mask)
138 {
139  int ret = 0;
140  const char *filename = h->filename;
141  av_strstart(filename, "file:", &filename);
142 
143  {
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;
153 #else
154  struct stat st;
155 # ifndef _WIN32
156  ret = stat(filename, &st);
157 # else
158  ret = win32_stat(filename, &st);
159 # endif
160  if (ret < 0)
161  return AVERROR(errno);
162 
163  ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
164  ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
165 #endif
166  }
167  return ret;
168 }
169 
170 #if CONFIG_FILE_PROTOCOL
171 
172 static int file_delete(URLContext *h)
173 {
174 #if HAVE_UNISTD_H
175  int ret;
176  const char *filename = h->filename;
177  av_strstart(filename, "file:", &filename);
178 
179  ret = rmdir(filename);
180  if (ret < 0 && (errno == ENOTDIR
181 # ifdef _WIN32
182  || errno == EINVAL
183 # endif
184  ))
185  ret = unlink(filename);
186  if (ret < 0)
187  return AVERROR(errno);
188 
189  return ret;
190 #else
191  return AVERROR(ENOSYS);
192 #endif /* HAVE_UNISTD_H */
193 }
194 
195 static int file_move(URLContext *h_src, URLContext *h_dst)
196 {
197  const char *filename_src = h_src->filename;
198  const char *filename_dst = h_dst->filename;
199  av_strstart(filename_src, "file:", &filename_src);
200  av_strstart(filename_dst, "file:", &filename_dst);
201 
202  if (rename(filename_src, filename_dst) < 0)
203  return AVERROR(errno);
204 
205  return 0;
206 }
207 
208 static int file_open(URLContext *h, const char *filename, int flags)
209 {
210  FileContext *c = h->priv_data;
211  int access;
212  int fd;
213  struct stat st;
214 
215  av_strstart(filename, "file:", &filename);
216 
218  access = O_CREAT | O_RDWR;
219  if (c->trunc)
220  access |= O_TRUNC;
221  } else if (flags & AVIO_FLAG_WRITE) {
222  access = O_CREAT | O_WRONLY;
223  if (c->trunc)
224  access |= O_TRUNC;
225  } else {
226  access = O_RDONLY;
227  }
228 #ifdef O_BINARY
229  access |= O_BINARY;
230 #endif
231  fd = avpriv_open(filename, access, 0666);
232  if (fd == -1)
233  return AVERROR(errno);
234  c->fd = fd;
235 
236  h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
237 
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;
242 
243  if (c->seekable >= 0)
244  h->is_streamed = !c->seekable;
245 
246  return 0;
247 }
248 
249 /* XXX: use llseek */
250 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
251 {
252  FileContext *c = h->priv_data;
253  int64_t ret;
254 
255  if (whence == AVSEEK_SIZE) {
256  struct stat st;
257  ret = fstat(c->fd, &st);
258  return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
259  }
260 
261  ret = lseek(c->fd, pos, whence);
262 
263  return ret < 0 ? AVERROR(errno) : ret;
264 }
265 
266 static int file_close(URLContext *h)
267 {
268  FileContext *c = h->priv_data;
269  int ret = close(c->fd);
270  return (ret == -1) ? AVERROR(errno) : 0;
271 }
272 
273 static int file_open_dir(URLContext *h)
274 {
275 #if HAVE_LSTAT
276  FileContext *c = h->priv_data;
277 
278  c->dir = opendir(h->filename);
279  if (!c->dir)
280  return AVERROR(errno);
281 
282  return 0;
283 #else
284  return AVERROR(ENOSYS);
285 #endif /* HAVE_LSTAT */
286 }
287 
288 static int file_read_dir(URLContext *h, AVIODirEntry **next)
289 {
290 #if HAVE_LSTAT
291  FileContext *c = h->priv_data;
292  struct dirent *dir;
293  char *fullpath = NULL;
294 
295  *next = ff_alloc_dir_entry();
296  if (!*next)
297  return AVERROR(ENOMEM);
298  do {
299  errno = 0;
300  dir = readdir(c->dir);
301  if (!dir) {
302  av_freep(next);
303  return AVERROR(errno);
304  }
305  } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
306 
307  fullpath = av_append_path_component(h->filename, dir->d_name);
308  if (fullpath) {
309  struct stat st;
310  if (!lstat(fullpath, &st)) {
311  if (S_ISDIR(st.st_mode))
312  (*next)->type = AVIO_ENTRY_DIRECTORY;
313  else if (S_ISFIFO(st.st_mode))
314  (*next)->type = AVIO_ENTRY_NAMED_PIPE;
315  else if (S_ISCHR(st.st_mode))
316  (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
317  else if (S_ISBLK(st.st_mode))
318  (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
319  else if (S_ISLNK(st.st_mode))
320  (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
321  else if (S_ISSOCK(st.st_mode))
322  (*next)->type = AVIO_ENTRY_SOCKET;
323  else if (S_ISREG(st.st_mode))
324  (*next)->type = AVIO_ENTRY_FILE;
325  else
326  (*next)->type = AVIO_ENTRY_UNKNOWN;
327 
328  (*next)->group_id = st.st_gid;
329  (*next)->user_id = st.st_uid;
330  (*next)->size = st.st_size;
331  (*next)->filemode = st.st_mode & 0777;
332  (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
333  (*next)->access_timestamp = INT64_C(1000000) * st.st_atime;
334  (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
335  }
336  av_free(fullpath);
337  }
338 
339  (*next)->name = av_strdup(dir->d_name);
340  return 0;
341 #else
342  return AVERROR(ENOSYS);
343 #endif /* HAVE_LSTAT */
344 }
345 
346 static int file_close_dir(URLContext *h)
347 {
348 #if HAVE_LSTAT
349  FileContext *c = h->priv_data;
350  closedir(c->dir);
351  return 0;
352 #else
353  return AVERROR(ENOSYS);
354 #endif /* HAVE_LSTAT */
355 }
356 
358  .name = "file",
359  .url_open = file_open,
360  .url_read = file_read,
361  .url_write = file_write,
362  .url_seek = file_seek,
363  .url_close = file_close,
364  .url_get_file_handle = file_get_handle,
365  .url_check = file_check,
366  .url_delete = file_delete,
367  .url_move = file_move,
368  .priv_data_size = sizeof(FileContext),
369  .priv_data_class = &file_class,
370  .url_open_dir = file_open_dir,
371  .url_read_dir = file_read_dir,
372  .url_close_dir = file_close_dir,
373  .default_whitelist = "file,crypto,data"
374 };
375 
376 #endif /* CONFIG_FILE_PROTOCOL */
377 
378 #if CONFIG_PIPE_PROTOCOL
379 
380 static int pipe_open(URLContext *h, const char *filename, int flags)
381 {
382  FileContext *c = h->priv_data;
383  int fd;
384  char *final;
385  av_strstart(filename, "pipe:", &filename);
386 
387  fd = strtol(filename, &final, 10);
388  if((filename == final) || *final ) {/* No digits found, or something like 10ab */
389  if (flags & AVIO_FLAG_WRITE) {
390  fd = 1;
391  } else {
392  fd = 0;
393  }
394  }
395 #if HAVE_SETMODE
396  setmode(fd, O_BINARY);
397 #endif
398  c->fd = fd;
399  h->is_streamed = 1;
400  return 0;
401 }
402 
404  .name = "pipe",
405  .url_open = pipe_open,
406  .url_read = file_read,
407  .url_write = file_write,
408  .url_get_file_handle = file_get_handle,
409  .url_check = file_check,
410  .priv_data_size = sizeof(FileContext),
411  .priv_data_class = &pipe_class,
412  .default_whitelist = "crypto,data"
413 };
414 
415 #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:64
URLContext::filename
char * filename
specified URL
Definition: url.h:42
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVOption
AVOption.
Definition: opt.h:247
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:478
AVIO_ENTRY_NAMED_PIPE
@ AVIO_ENTRY_NAMED_PIPE
Definition: avio.h:71
file_check
static int file_check(URLContext *h, int mask)
Definition: file.c:137
URLProtocol
Definition: url.h:54
os_support.h
AVIO_ENTRY_UNKNOWN
@ AVIO_ENTRY_UNKNOWN
Definition: avio.h:67
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:304
AVIO_ENTRY_DIRECTORY
@ AVIO_ENTRY_DIRECTORY
Definition: avio.h:70
trunc
static __device__ float trunc(float a)
Definition: cuda_runtime.h:179
AVIO_ENTRY_CHARACTER_DEVICE
@ AVIO_ENTRY_CHARACTER_DEVICE
Definition: avio.h:69
FileContext::blocksize
int blocksize
Definition: file.c:74
file_write
static int file_write(URLContext *h, const unsigned char *buf, int size)
Definition: file.c:122
file_read
static int file_read(URLContext *h, unsigned char *buf, int size)
Definition: file.c:109
AVIO_ENTRY_SYMBOLIC_LINK
@ AVIO_ENTRY_SYMBOLIC_LINK
Definition: avio.h:72
mask
static const uint16_t mask[17]
Definition: lzw.c:38
FileContext::seekable
int seekable
Definition: file.c:76
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:277
S_ISFIFO
#define S_ISFIFO(m)
Definition: file.c:46
avpriv_open
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:66
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:622
FileContext
Definition: file.c:70
pipe_class
static const AVClass pipe_class
Definition: file.c:102
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:235
AVIO_ENTRY_FILE
@ AVIO_ENTRY_FILE
Definition: avio.h:74
FileContext::fd
int fd
Definition: file.c:72
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:90
FileContext::follow
int follow
Definition: file.c:75
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:86
URLProtocol::name
const char * name
Definition: url.h:55
FileContext::trunc
int trunc
Definition: file.c:73
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:34
AVIO_ENTRY_SOCKET
@ AVIO_ENTRY_SOCKET
Definition: avio.h:73
ff_alloc_dir_entry
AVIODirEntry * ff_alloc_dir_entry(void)
Allocate directory entry with default values.
Definition: url.c:325
URLContext
Definition: url.h:38
file_options
static const AVOption file_options[]
Definition: file.c:82
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:278
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
url.h
file_class
static const AVClass file_class
Definition: file.c:95
S_ISLNK
#define S_ISLNK(m)
Definition: file.c:55
O_BINARY
#define O_BINARY
ff_pipe_protocol
const URLProtocol ff_pipe_protocol
AVIO_ENTRY_BLOCK_DEVICE
@ AVIO_ENTRY_BLOCK_DEVICE
Definition: avio.h:68
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:131
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:621
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:279
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
h
h
Definition: vp9dsp_template.c:2038
avstring.h