FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/file.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 70 141 49.6%
Functions: 8 13 61.5%
Branches: 37 94 39.4%

Line Branch Exec Source
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 195097 static int file_read(URLContext *h, unsigned char *buf, int size)
113 {
114 195097 FileContext *c = h->priv_data;
115 int ret;
116 195097 size = FFMIN(size, c->blocksize);
117 195097 ret = read(c->fd, buf, size);
118
3/4
✓ Branch 0 taken 11754 times.
✓ Branch 1 taken 183343 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11754 times.
195097 if (ret == 0 && c->follow)
119 return AVERROR(EAGAIN);
120
2/2
✓ Branch 0 taken 11754 times.
✓ Branch 1 taken 183343 times.
195097 if (ret == 0)
121 11754 return AVERROR_EOF;
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183343 times.
183343 return (ret == -1) ? AVERROR(errno) : ret;
123 }
124
125 226484 static int file_write(URLContext *h, const unsigned char *buf, int size)
126 {
127 226484 FileContext *c = h->priv_data;
128 int ret;
129 226484 size = FFMIN(size, c->blocksize);
130 226484 ret = write(c->fd, buf, size);
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 226484 times.
226484 return (ret == -1) ? AVERROR(errno) : ret;
132 }
133
134 static int file_get_handle(URLContext *h)
135 {
136 FileContext *c = h->priv_data;
137 return c->fd;
138 }
139
140 43992 static int file_check(URLContext *h, int mask)
141 {
142 43992 int ret = 0;
143 43992 const char *filename = h->filename;
144 43992 av_strstart(filename, "file:", &filename);
145
146 {
147 #if HAVE_ACCESS && defined(R_OK)
148
2/2
✓ Branch 1 taken 10428 times.
✓ Branch 2 taken 33564 times.
43992 if (access(filename, F_OK) < 0)
149 10428 return AVERROR(errno);
150
1/2
✓ Branch 0 taken 33564 times.
✗ Branch 1 not taken.
33564 if (mask&AVIO_FLAG_READ)
151
1/2
✓ Branch 1 taken 33564 times.
✗ Branch 2 not taken.
33564 if (access(filename, R_OK) >= 0)
152 33564 ret |= AVIO_FLAG_READ;
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33564 times.
33564 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 33564 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 78 static int file_move(URLContext *h_src, URLContext *h_dst)
195 {
196 78 const char *filename_src = h_src->filename;
197 78 const char *filename_dst = h_dst->filename;
198 78 av_strstart(filename_src, "file:", &filename_src);
199 78 av_strstart(filename_dst, "file:", &filename_dst);
200
201
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
78 if (rename(filename_src, filename_dst) < 0)
202 return AVERROR(errno);
203
204 78 return 0;
205 }
206
207 92055 static int file_open(URLContext *h, const char *filename, int flags)
208 {
209 92055 FileContext *c = h->priv_data;
210 int access;
211 int fd;
212 struct stat st;
213
214 92055 av_strstart(filename, "file:", &filename);
215
216
3/4
✓ Branch 0 taken 2405 times.
✓ Branch 1 taken 89650 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2405 times.
92055 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
217 access = O_CREAT | O_RDWR;
218 if (c->trunc)
219 access |= O_TRUNC;
220
2/2
✓ Branch 0 taken 2405 times.
✓ Branch 1 taken 89650 times.
92055 } else if (flags & AVIO_FLAG_WRITE) {
221 2405 access = O_CREAT | O_WRONLY;
222
1/2
✓ Branch 0 taken 2405 times.
✗ Branch 1 not taken.
2405 if (c->trunc)
223 2405 access |= O_TRUNC;
224 } else {
225 89650 access = O_RDONLY;
226 }
227 #ifdef O_BINARY
228 access |= O_BINARY;
229 #endif
230 92055 fd = avpriv_open(filename, access, 0666);
231
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 92054 times.
92055 if (fd == -1)
232 1 return AVERROR(errno);
233 92054 c->fd = fd;
234
235
2/4
✓ Branch 1 taken 92054 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 92054 times.
92054 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
3/4
✓ Branch 0 taken 92054 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2405 times.
✓ Branch 3 taken 89649 times.
92054 if (!h->is_streamed && flags & AVIO_FLAG_WRITE)
240 2405 h->min_packet_size = h->max_packet_size = 262144;
241
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92054 times.
92054 if (c->seekable >= 0)
243 h->is_streamed = !c->seekable;
244
245 92054 return 0;
246 }
247
248 /* XXX: use llseek */
249 295729 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
250 {
251 295729 FileContext *c = h->priv_data;
252 int64_t ret;
253
254
2/2
✓ Branch 0 taken 192177 times.
✓ Branch 1 taken 103552 times.
295729 if (whence == AVSEEK_SIZE) {
255 struct stat st;
256 192177 ret = fstat(c->fd, &st);
257
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 192177 times.
✓ Branch 2 taken 192177 times.
✗ Branch 3 not taken.
192177 return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
258 }
259
260 103552 ret = lseek(c->fd, pos, whence);
261
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103552 times.
103552 return ret < 0 ? AVERROR(errno) : ret;
263 }
264
265 92054 static int file_close(URLContext *h)
266 {
267 92054 FileContext *c = h->priv_data;
268 92054 int ret = close(c->fd);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92054 times.
92054 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
356 const URLProtocol ff_file_protocol = {
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 2487 static int pipe_open(URLContext *h, const char *filename, int flags)
380 {
381 2487 FileContext *c = h->priv_data;
382 int fd;
383 char *final;
384 2487 av_strstart(filename, "pipe:", &filename);
385
386 2487 fd = strtol(filename, &final, 10);
387
3/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 2464 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
2487 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
388
1/2
✓ Branch 0 taken 2464 times.
✗ Branch 1 not taken.
2464 if (flags & AVIO_FLAG_WRITE) {
389 2464 fd = 1;
390 } else {
391 fd = 0;
392 }
393 }
394 #if HAVE_SETMODE
395 setmode(fd, O_BINARY);
396 #endif
397 2487 c->fd = fd;
398 2487 h->is_streamed = 1;
399 2487 return 0;
400 }
401
402 const URLProtocol ff_pipe_protocol = {
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 */
415