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
47 };
48
50 int log_offset, void *log_ctx)
51 {
54 struct stat st;
56 off_t off_size;
57 char errbuf[128];
58 *bufptr = NULL;
59
60 if (fd < 0) {
64 return err;
65 }
66
67 if (fstat(fd, &st) < 0) {
72 return err;
73 }
74
75 off_size = st.st_size;
76 if (off_size > SIZE_MAX) {
78 "File size for file '%s' is too big\n", filename);
81 }
82 *size = off_size;
83
84 #if HAVE_MMAP
85 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
86 if (ptr == MAP_FAILED) {
91 return err;
92 }
93 *bufptr = ptr;
94 #elif HAVE_MAPVIEWOFFILE
95 {
97
98 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
99 if (!mh) {
102 return -1;
103 }
104
105 ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
106 CloseHandle(mh);
107 if (!ptr) {
110 return -1;
111 }
112
113 *bufptr = ptr;
114 }
115 #else
117 if (!*bufptr) {
121 }
122 read(fd, *bufptr, *size);
123 #endif
124
126 return 0;
127 }
128
130 {
131 #if HAVE_MMAP
132 munmap(bufptr, size);
133 #elif HAVE_MAPVIEWOFFILE
134 UnmapViewOfFile(bufptr);
135 #else
137 #endif
138 }
139
140 int av_tempfile(
const char *prefix,
char **filename,
int log_offset,
void *log_ctx) {
142 int fd=-1;
143 #if !HAVE_MKSTEMP
144 void *ptr= tempnam(NULL, prefix);
145 if(!ptr)
146 ptr= tempnam(".", prefix);
148 #undef free
149 free(ptr);
150 #else
151 size_t len = strlen(prefix) + 12;
/* room for "/tmp/" and "XXXXXX0円" */
153 #endif
154 /* -----common section-----*/
155 if (*filename == NULL) {
158 }
159 #if !HAVE_MKSTEMP
160 # ifndef O_BINARY
161 # define O_BINARY 0
162 # endif
163 # ifndef O_EXCL
164 # define O_EXCL 0
165 # endif
167 #else
168 snprintf(*filename, len,
"/tmp/%sXXXXXX", prefix);
169 fd = mkstemp(*filename);
170 #ifdef _WIN32
171 if (fd < 0) {
172 snprintf(*filename, len,
"./%sXXXXXX", prefix);
173 fd = mkstemp(*filename);
174 }
175 #endif
176 #endif
177 /* -----common section-----*/
178 if (fd < 0) {
180 av_log(&file_log_ctx,
AV_LOG_ERROR,
"ff_tempfile: Cannot open temporary file %s\n", *filename);
182 return err;
183 }
184 return fd; /* success */
185 }
186
187 #ifdef TEST
188
189 #undef printf
190
192 {
195 if (
av_file_map(
"file.c", &buf, &size, 0, NULL) < 0)
196 return 1;
197
198 buf[0] = 's';
199 printf("%s", buf);
201 return 0;
202 }
203 #endif
204