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