2 * This file is part of FFmpeg.
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.
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.
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
34 #elif HAVE_MAPVIEWOFFILE
44 static const AVClass file_log_ctx_class = {
45 "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
46 offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
49 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
50 int log_offset, void *log_ctx)
52 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
53 int err, fd = avpriv_open(filename, O_RDONLY);
62 av_strerror(err, errbuf, sizeof(errbuf));
63 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
67 if (fstat(fd, &st) < 0) {
69 av_strerror(err, errbuf, sizeof(errbuf));
70 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
75 off_size = st.st_size;
76 if (off_size > SIZE_MAX) {
77 av_log(&file_log_ctx, AV_LOG_ERROR,
78 "File size for file '%s' is too big\n", filename);
80 return AVERROR(EINVAL);
85 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
86 if (ptr == MAP_FAILED) {
88 av_strerror(err, errbuf, sizeof(errbuf));
89 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
94 #elif HAVE_MAPVIEWOFFILE
96 HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
98 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
100 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
105 ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
108 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
116 *bufptr = av_malloc(*size);
118 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
120 return AVERROR(ENOMEM);
122 read(fd, *bufptr, *size);
129 void av_file_unmap(uint8_t *bufptr, size_t size)
132 munmap(bufptr, size);
133 #elif HAVE_MAPVIEWOFFILE
134 UnmapViewOfFile(bufptr);
140 int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx) {
141 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
144 void *ptr= tempnam(NULL, prefix);
146 ptr= tempnam(".", prefix);
147 *filename = av_strdup(ptr);
151 size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
152 *filename = av_malloc(len);
154 /* -----common section-----*/
155 if (*filename == NULL) {
156 av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
157 return AVERROR(ENOMEM);
166 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
168 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
169 fd = mkstemp(*filename);
172 snprintf(*filename, len, "./%sXXXXXX", prefix);
173 fd = mkstemp(*filename);
177 /* -----common section-----*/
179 int err = AVERROR(errno);
180 av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
184 return fd; /* success */
195 if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
200 av_file_unmap(buf, size);