add049d24bdc7378a7a32a6a224eab28e1d77f68
[libav.git] / libavutil / file.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; 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 "internal.h"
22 #include "log.h"
23 #include "mem.h"
24 #include <stdarg.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #if HAVE_IO_H
31 #include <io.h>
32 #endif
33 #if HAVE_MMAP
34 #include <sys/mman.h>
35 #elif HAVE_MAPVIEWOFFILE
36 #include <windows.h>
37 #endif
38
39 int avpriv_open(const char *filename, int flags, ...)
40 {
41 int fd;
42 unsigned int mode = 0;
43 va_list ap;
44
45 va_start(ap, flags);
46 if (flags & O_CREAT)
47 mode = va_arg(ap, unsigned int);
48 va_end(ap);
49
50 #ifdef O_CLOEXEC
51 flags |= O_CLOEXEC;
52 #endif
53
54 fd = open(filename, flags, mode);
55 if (fd != -1)
56 fcntl(fd, F_SETFD, FD_CLOEXEC);
57 return fd;
58 }
59
60 typedef struct {
61 const AVClass *class;
62 int log_offset;
63 void *log_ctx;
64 } FileLogContext;
65
66 static const AVClass file_log_ctx_class = {
67 "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
68 offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
69 };
70
71 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
72 int log_offset, void *log_ctx)
73 {
74 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
75 int err, fd = avpriv_open(filename, O_RDONLY);
76 struct stat st;
77 av_unused void *ptr;
78 off_t off_size;
79 char errbuf[128];
80 *bufptr = NULL;
81
82 if (fd < 0) {
83 err = AVERROR(errno);
84 av_strerror(err, errbuf, sizeof(errbuf));
85 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
86 return err;
87 }
88
89 if (fstat(fd, &st) < 0) {
90 err = AVERROR(errno);
91 av_strerror(err, errbuf, sizeof(errbuf));
92 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
93 close(fd);
94 return err;
95 }
96
97 off_size = st.st_size;
98 if (off_size > SIZE_MAX) {
99 av_log(&file_log_ctx, AV_LOG_ERROR,
100 "File size for file '%s' is too big\n", filename);
101 close(fd);
102 return AVERROR(EINVAL);
103 }
104 *size = off_size;
105
106 #if HAVE_MMAP
107 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
108 if (ptr == MAP_FAILED) {
109 err = AVERROR(errno);
110 av_strerror(err, errbuf, sizeof(errbuf));
111 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
112 close(fd);
113 return err;
114 }
115 *bufptr = ptr;
116 #elif HAVE_MAPVIEWOFFILE
117 {
118 HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
119
120 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
121 if (!mh) {
122 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
123 close(fd);
124 return -1;
125 }
126
127 ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
128 CloseHandle(mh);
129 if (!ptr) {
130 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
131 close(fd);
132 return -1;
133 }
134
135 *bufptr = ptr;
136 }
137 #else
138 *bufptr = av_malloc(*size);
139 if (!*bufptr) {
140 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
141 close(fd);
142 return AVERROR(ENOMEM);
143 }
144 read(fd, *bufptr, *size);
145 #endif
146
147 close(fd);
148 return 0;
149 }
150
151 void av_file_unmap(uint8_t *bufptr, size_t size)
152 {
153 #if HAVE_MMAP
154 munmap(bufptr, size);
155 #elif HAVE_MAPVIEWOFFILE
156 UnmapViewOfFile(bufptr);
157 #else
158 av_free(bufptr);
159 #endif
160 }