]> git.sesse.net Git - ffmpeg/blob - libavutil/file.c
Merge commit '9ce02e14f01de50fcc6f7f459544b140be66d615'
[ffmpeg] / libavutil / file.c
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"
20 #include "file.h"
21 #include "log.h"
22 #include "mem.h"
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #if HAVE_MMAP
29 #include <sys/mman.h>
30 #elif HAVE_MAPVIEWOFFILE
31 #include <io.h>
32 #include <windows.h>
33 #endif
34
35 typedef struct {
36     const AVClass *class;
37     int   log_offset;
38     void *log_ctx;
39 } FileLogContext;
40
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)
44 };
45
46 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
47                 int log_offset, void *log_ctx)
48 {
49     FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
50     int err, fd = open(filename, O_RDONLY);
51     struct stat st;
52     av_unused void *ptr;
53     off_t off_size;
54     char errbuf[128];
55     *bufptr = NULL;
56
57     if (fd < 0) {
58         err = AVERROR(errno);
59         av_strerror(err, errbuf, sizeof(errbuf));
60         av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
61         return err;
62     }
63
64     if (fstat(fd, &st) < 0) {
65         err = AVERROR(errno);
66         av_strerror(err, errbuf, sizeof(errbuf));
67         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
68         close(fd);
69         return err;
70     }
71
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);
76         close(fd);
77         return AVERROR(EINVAL);
78     }
79     *size = off_size;
80
81 #if HAVE_MMAP
82     ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
83     if (ptr == MAP_FAILED) {
84         err = AVERROR(errno);
85         av_strerror(err, errbuf, sizeof(errbuf));
86         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
87         close(fd);
88         return err;
89     }
90     *bufptr = ptr;
91 #elif HAVE_MAPVIEWOFFILE
92     {
93         HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
94
95         mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
96         if (!mh) {
97             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
98             close(fd);
99             return -1;
100         }
101
102         ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
103         CloseHandle(mh);
104         if (!ptr) {
105             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
106             close(fd);
107             return -1;
108         }
109
110         *bufptr = ptr;
111     }
112 #else
113     *bufptr = av_malloc(*size);
114     if (!*bufptr) {
115         av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
116         close(fd);
117         return AVERROR(ENOMEM);
118     }
119     read(fd, *bufptr, *size);
120 #endif
121
122     close(fd);
123     return 0;
124 }
125
126 void av_file_unmap(uint8_t *bufptr, size_t size)
127 {
128 #if HAVE_MMAP
129     munmap(bufptr, size);
130 #elif HAVE_MAPVIEWOFFILE
131     UnmapViewOfFile(bufptr);
132 #else
133     av_free(bufptr);
134 #endif
135 }
136
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 };
139     int fd=-1;
140 #if !HAVE_MKSTEMP
141     void *ptr= tempnam(NULL, prefix);
142     if(!ptr)
143         ptr= tempnam(".", prefix);
144     *filename = av_strdup(ptr);
145 #undef free
146     free(ptr);
147 #else
148     size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
149     *filename = av_malloc(len);
150 #endif
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);
155     }
156 #if !HAVE_MKSTEMP
157 #   ifndef O_BINARY
158 #       define O_BINARY 0
159 #   endif
160 #   ifndef O_EXCL
161 #       define O_EXCL 0
162 #   endif
163     fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
164 #else
165     snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
166     fd = mkstemp(*filename);
167 #ifdef _WIN32
168     if (fd < 0) {
169         snprintf(*filename, len, "./%sXXXXXX", prefix);
170         fd = mkstemp(*filename);
171     }
172 #endif
173 #endif
174     /* -----common section-----*/
175     if (fd < 0) {
176         int err = AVERROR(errno);
177         av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
178         av_freep(filename);
179         return err;
180     }
181     return fd; /* success */
182 }
183
184 #ifdef TEST
185
186 #undef printf
187
188 int main(void)
189 {
190     uint8_t *buf;
191     size_t size;
192     if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
193         return 1;
194
195     buf[0] = 's';
196     printf("%s", buf);
197     av_file_unmap(buf, size);
198     return 0;
199 }
200 #endif
201