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
30 #elif HAVE_MAPVIEWOFFILE
41 static const AVClass file_log_ctx_class = {
42 "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
43 offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
46 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
47 int log_offset, void *log_ctx)
49 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
50 int err, fd = open(filename, O_RDONLY);
59 av_strerror(err, errbuf, sizeof(errbuf));
60 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
64 if (fstat(fd, &st) < 0) {
66 av_strerror(err, errbuf, sizeof(errbuf));
67 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
72 off_size = st.st_size;
73 if (off_size > SIZE_MAX) {
74 av_log(&file_log_ctx, AV_LOG_ERROR,
75 "File size for file '%s' is too big\n", filename);
77 return AVERROR(EINVAL);
82 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
83 if (ptr == MAP_FAILED) {
85 av_strerror(err, errbuf, sizeof(errbuf));
86 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
91 #elif HAVE_MAPVIEWOFFILE
93 HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
95 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
97 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
102 ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
105 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
113 *bufptr = av_malloc(*size);
115 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
117 return AVERROR(ENOMEM);
119 read(fd, *bufptr, *size);
126 void av_file_unmap(uint8_t *bufptr, size_t size)
129 munmap(bufptr, size);
130 #elif HAVE_MAPVIEWOFFILE
131 UnmapViewOfFile(bufptr);
137 int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx) {
138 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
141 void *ptr= tempnam(NULL, prefix);
143 ptr= tempnam(".", prefix);
144 *filename = av_strdup(ptr);
148 size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
149 *filename = av_malloc(len);
151 /* -----common section-----*/
152 if (*filename == NULL) {
153 av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
154 return AVERROR(ENOMEM);
163 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
165 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
166 fd = mkstemp(*filename);
169 snprintf(*filename, len, "./%sXXXXXX", prefix);
170 fd = mkstemp(*filename);
174 /* -----common section-----*/
176 int err = AVERROR(errno);
177 av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
181 return fd; /* success */
192 if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
197 av_file_unmap(buf, size);