FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/file.c
Date: 2026-09-24 08:25:19
Exec Total Coverage
Lines: 31 55 56.4%
Functions: 4 6 66.7%
Branches: 11 30 36.7%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "config.h"
20 #include "error.h"
21 #include "file.h"
22 #include "file_open.h"
23 #include "internal.h"
24 #include "log.h"
25 #include "mem.h"
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #if HAVE_IO_H
32 #include <io.h>
33 #endif
34 #if HAVE_MMAP
35 #include <sys/mman.h>
36 #elif HAVE_MAPVIEWOFFILE
37 #include <windows.h>
38 #endif
39
40 #ifdef _WIN32
41 /* struct stat has a 32 bit st_size with the Windows SDK, use the 64 bit variants */
42 #undef stat
43 #undef fstat
44 #define stat _stat64
45 #define fstat _fstat64
46 #endif
47
48 typedef struct FileLogContext {
49 const AVClass *class;
50 int log_offset;
51 void *log_ctx;
52 } FileLogContext;
53
54 static const AVClass file_log_ctx_class = {
55 .class_name = "FILE",
56 .item_name = av_default_item_name,
57 .option = NULL,
58 .version = LIBAVUTIL_VERSION_INT,
59 .log_level_offset_offset = offsetof(FileLogContext, log_offset),
60 .parent_log_context_offset = offsetof(FileLogContext, log_ctx),
61 };
62
63 #if HAVE_MMAP || HAVE_MAPVIEWOFFILE
64 /* Map the first size bytes of the file, as a private copy or shared and
65 * writable. The shared mapping extends the file to size, on Windows the
66 * mapping object does it. */
67 8 static int map_file(int fd, size_t size, int shared, void **ptr)
68 {
69 #if HAVE_MMAP
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (shared) {
71 struct stat st;
72 if (fstat(fd, &st) < 0)
73 return AVERROR(errno);
74 if (st.st_size < size && ftruncate(fd, size) < 0)
75 return AVERROR(errno);
76 }
77
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 void *map = mmap(NULL, size, PROT_READ | PROT_WRITE,
79 shared ? MAP_SHARED : MAP_PRIVATE, fd, 0);
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (map == MAP_FAILED)
81 return AVERROR(errno);
82 #else
83 HANDLE fh = (HANDLE)_get_osfhandle(fd);
84 if (fh == INVALID_HANDLE_VALUE)
85 return AVERROR(EBADF);
86
87 HANDLE mh = CreateFileMapping(fh, NULL,
88 shared ? PAGE_READWRITE : PAGE_READONLY,
89 (uint64_t)size >> 32, size, NULL);
90 if (!mh)
91 return AVERROR(EIO);
92
93 void *map = MapViewOfFile(mh, shared ? FILE_MAP_ALL_ACCESS : FILE_MAP_COPY,
94 0, 0, size);
95 CloseHandle(mh);
96 if (!map)
97 return AVERROR(EIO);
98 #endif
99
100 8 *ptr = map;
101 8 return 0;
102 }
103
104 8 static void unmap_file(void *ptr, size_t size)
105 {
106 #if HAVE_MMAP
107 8 munmap(ptr, size);
108 #else
109 UnmapViewOfFile(ptr);
110 #endif
111 8 }
112 #endif
113
114 10 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
115 int log_offset, void *log_ctx)
116 {
117 10 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
118 10 int err, fd = avpriv_open(filename, O_RDONLY);
119 struct stat st;
120 av_unused void *ptr;
121 10 *bufptr = NULL;
122 10 *size = 0;
123
124
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (fd < 0) {
125 2 err = AVERROR(errno);
126 2 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, av_err2str(err));
127 2 return err;
128 }
129
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (fstat(fd, &st) < 0) {
131 err = AVERROR(errno);
132 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", av_err2str(err));
133 close(fd);
134 return err;
135 }
136
137 if (st.st_size > SIZE_MAX) {
138 av_log(&file_log_ctx, AV_LOG_ERROR,
139 "File size for file '%s' is too big\n", filename);
140 close(fd);
141 return AVERROR(EINVAL);
142 }
143 8 *size = st.st_size;
144
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!*size) {
146 *bufptr = NULL;
147 goto out;
148 }
149
150 #if HAVE_MMAP || HAVE_MAPVIEWOFFILE
151 8 err = map_file(fd, *size, 0, &ptr);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (err < 0) {
153 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot map file '%s': %s\n", filename, av_err2str(err));
154 close(fd);
155 *size = 0;
156 return err;
157 }
158 8 *bufptr = ptr;
159 #else
160 *bufptr = av_malloc(*size);
161 if (!*bufptr) {
162 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
163 close(fd);
164 *size = 0;
165 return AVERROR(ENOMEM);
166 }
167 read(fd, *bufptr, *size);
168 #endif
169
170 8 out:
171 8 close(fd);
172 8 return 0;
173 }
174
175 9 void av_file_unmap(uint8_t *bufptr, size_t size)
176 {
177
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
9 if (!size || !bufptr)
178 1 return;
179 #if HAVE_MMAP || HAVE_MAPVIEWOFFILE
180 8 unmap_file(bufptr, size);
181 #else
182 av_free(bufptr);
183 #endif
184 }
185
186 int av_file_map_shared(int fd, size_t size, void **bufptr)
187 {
188 *bufptr = NULL;
189 if (!size)
190 return AVERROR(EINVAL);
191 #if HAVE_MMAP || HAVE_MAPVIEWOFFILE
192 return map_file(fd, size, 1, bufptr);
193 #else
194 return AVERROR(ENOSYS);
195 #endif
196 }
197
198 void av_file_unmap_shared(void *bufptr, size_t size)
199 {
200 #if HAVE_MMAP || HAVE_MAPVIEWOFFILE
201 if (size && bufptr)
202 unmap_file(bufptr, size);
203 #endif
204 }
205