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