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"
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #if HAVE_IO_H
31 #include <io.h>
32 #endif
33 #if HAVE_MMAP
34 #include <sys/mman.h>
35 #elif HAVE_MAPVIEWOFFILE
36 #include <windows.h>
37 #endif
38
44
52 };
53
55 int log_offset, void *log_ctx)
56 {
59 struct stat st;
61 off_t off_size;
62 char errbuf[128];
65
66 if (fd < 0) {
70 return err;
71 }
72
73 if (fstat(fd, &st) < 0) {
77 close(fd);
78 return err;
79 }
80
81 off_size = st.st_size;
82 if (off_size > SIZE_MAX) {
84 "File size for file '%s' is too big\n", filename);
85 close(fd);
87 }
89
93 }
94
95 #if HAVE_MMAP
96 ptr = mmap(
NULL, *
size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
97 if (ptr == MAP_FAILED) {
101 close(fd);
103 return err;
104 }
105 *bufptr = ptr;
106 #elif HAVE_MAPVIEWOFFILE
107 {
108 HANDLE
mh, fh = (HANDLE)_get_osfhandle(fd);
109
110 mh = CreateFileMapping(fh,
NULL, PAGE_READONLY, 0, 0,
NULL);
113 close(fd);
115 return -1;
116 }
117
118 ptr = MapViewOfFile(
mh, FILE_MAP_READ, 0, 0, *
size);
120 if (!ptr) {
122 close(fd);
124 return -1;
125 }
126
127 *bufptr = ptr;
128 }
129 #else
131 if (!*bufptr) {
133 close(fd);
136 }
138 #endif
139
141 close(fd);
142 return 0;
143 }
144
146 {
147 if (!
size || !bufptr)
148 return;
149 #if HAVE_MMAP
150 munmap(bufptr,
size);
151 #elif HAVE_MAPVIEWOFFILE
152 UnmapViewOfFile(bufptr);
153 #else
155 #endif
156 }
157
158 #if FF_API_AV_FOPEN_UTF8
159 int av_tempfile(
const char *prefix,
char **filename,
int log_offset,
void *log_ctx) {
161 }
162 #endif