libavutil: Don't use fcntl if the function does not exist
[libav.git] / libavutil / file.c
CommitLineData
0328b9ea 1/*
2912e87a 2 * This file is part of Libav.
0328b9ea 3 *
2912e87a 4 * Libav is free software; you can redistribute it and/or
0328b9ea
SS
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 *
2912e87a 9 * Libav is distributed in the hope that it will be useful,
0328b9ea
SS
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
2912e87a 15 * License along with Libav; if not, write to the Free Software
0328b9ea
SS
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
1d9c2dc8 19#include "config.h"
0328b9ea 20#include "file.h"
869b04e8 21#include "internal.h"
abc78a5a 22#include "log.h"
70766c21 23#include "mem.h"
869b04e8 24#include <stdarg.h>
0328b9ea
SS
25#include <fcntl.h>
26#include <sys/stat.h>
d11baad0 27#if HAVE_UNISTD_H
0328b9ea 28#include <unistd.h>
d11baad0 29#endif
15816c8e
MS
30#if HAVE_IO_H
31#include <io.h>
32#endif
0328b9ea
SS
33#if HAVE_MMAP
34#include <sys/mman.h>
73f6d31e 35#elif HAVE_MAPVIEWOFFILE
73f6d31e 36#include <windows.h>
0328b9ea
SS
37#endif
38
869b04e8
RDC
39int 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);
0f1fb6c0 55#if HAVE_FCNTL
869b04e8
RDC
56 if (fd != -1)
57 fcntl(fd, F_SETFD, FD_CLOEXEC);
0f1fb6c0
DB
58#endif
59
869b04e8
RDC
60 return fd;
61}
62
0328b9ea
SS
63typedef struct {
64 const AVClass *class;
65 int log_offset;
66 void *log_ctx;
67} FileLogContext;
68
69static const AVClass file_log_ctx_class = {
70 "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
71 offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
72};
73
74int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
75 int log_offset, void *log_ctx)
76{
77 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
869b04e8 78 int err, fd = avpriv_open(filename, O_RDONLY);
0328b9ea
SS
79 struct stat st;
80 av_unused void *ptr;
81 off_t off_size;
82 char errbuf[128];
0328b9ea
SS
83 *bufptr = NULL;
84
85 if (fd < 0) {
86 err = AVERROR(errno);
87 av_strerror(err, errbuf, sizeof(errbuf));
88 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
89 return err;
90 }
91
f02cbc45 92 if (fstat(fd, &st) < 0) {
0328b9ea
SS
93 err = AVERROR(errno);
94 av_strerror(err, errbuf, sizeof(errbuf));
f02cbc45 95 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
0328b9ea
SS
96 close(fd);
97 return err;
98 }
99
100 off_size = st.st_size;
490a022d 101 if (off_size > SIZE_MAX) {
0328b9ea
SS
102 av_log(&file_log_ctx, AV_LOG_ERROR,
103 "File size for file '%s' is too big\n", filename);
104 close(fd);
105 return AVERROR(EINVAL);
106 }
107 *size = off_size;
108
109#if HAVE_MMAP
110 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
d0989bed 111 if (ptr == MAP_FAILED) {
0328b9ea
SS
112 err = AVERROR(errno);
113 av_strerror(err, errbuf, sizeof(errbuf));
114 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
115 close(fd);
116 return err;
117 }
118 *bufptr = ptr;
73f6d31e
DV
119#elif HAVE_MAPVIEWOFFILE
120 {
121 HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
122
123 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
124 if (!mh) {
125 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
126 close(fd);
127 return -1;
128 }
129
130 ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
131 CloseHandle(mh);
132 if (!ptr) {
133 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
134 close(fd);
135 return -1;
136 }
137
138 *bufptr = ptr;
139 }
0328b9ea
SS
140#else
141 *bufptr = av_malloc(*size);
142 if (!*bufptr) {
143 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
144 close(fd);
145 return AVERROR(ENOMEM);
146 }
147 read(fd, *bufptr, *size);
148#endif
149
150 close(fd);
151 return 0;
152}
153
154void av_file_unmap(uint8_t *bufptr, size_t size)
155{
156#if HAVE_MMAP
157 munmap(bufptr, size);
73f6d31e
DV
158#elif HAVE_MAPVIEWOFFILE
159 UnmapViewOfFile(bufptr);
0328b9ea
SS
160#else
161 av_free(bufptr);
162#endif
163}