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"
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #if HAVE_IO_H
30 #include <io.h>
31 #endif
32 #if HAVE_MMAP
33 #include <sys/mman.h>
34 #elif HAVE_MAPVIEWOFFILE
35 #include <windows.h>
36 #endif
37
43
51 };
52
54 int log_offset, void *log_ctx)
55 {
58 struct stat st;
60 off_t off_size;
61 char errbuf[128];
64
65 if (fd < 0) {
69 return err;
70 }
71
72 if (fstat(fd, &st) < 0) {
76 close(fd);
77 return err;
78 }
79
80 off_size = st.st_size;
81 if (off_size > SIZE_MAX) {
83 "File size for file '%s' is too big\n", filename);
84 close(fd);
86 }
88
92 }
93
94 #if HAVE_MMAP
95 ptr = mmap(
NULL, *
size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
96 if (ptr == MAP_FAILED) {
100 close(fd);
102 return err;
103 }
104 *bufptr = ptr;
105 #elif HAVE_MAPVIEWOFFILE
106 {
107 HANDLE
mh, fh = (HANDLE)_get_osfhandle(fd);
108
109 mh = CreateFileMapping(fh,
NULL, PAGE_READONLY, 0, 0,
NULL);
112 close(fd);
114 return -1;
115 }
116
117 ptr = MapViewOfFile(
mh, FILE_MAP_READ, 0, 0, *
size);
119 if (!ptr) {
121 close(fd);
123 return -1;
124 }
125
126 *bufptr = ptr;
127 }
128 #else
130 if (!*bufptr) {
132 close(fd);
135 }
136 read(fd, *bufptr, *
size);
137 #endif
138
140 close(fd);
141 return 0;
142 }
143
145 {
146 if (!
size || !bufptr)
147 return;
148 #if HAVE_MMAP
149 munmap(bufptr,
size);
150 #elif HAVE_MAPVIEWOFFILE
151 UnmapViewOfFile(bufptr);
152 #else
154 #endif
155 }
156
157 int av_tempfile(
const char *prefix,
char **filename,
int log_offset,
void *log_ctx) {
159 }