file.c
Go to the documentation of this file.
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 "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;
34  void *log_ctx;
36 
37 static const AVClass file_log_ctx_class = {
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 #ifdef TEST
134 
135 #undef printf
136 
137 int main(void)
138 {
139  uint8_t *buf;
140  size_t size;
141  if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
142  return 1;
143 
144  buf[0] = 's';
145  printf("%s", buf);
146  av_file_unmap(buf, size);
147  return 0;
148 }
149 #endif