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 FileLogContext {
39  const AVClass *class;
41  void *log_ctx;
43 
44 static const AVClass file_log_ctx_class = {
45  .class_name = "FILE",
46  .item_name = av_default_item_name,
47  .option = NULL,
48  .version = LIBAVUTIL_VERSION_INT,
49  .log_level_offset_offset = offsetof(FileLogContext, log_offset),
50  .parent_log_context_offset = offsetof(FileLogContext, log_ctx),
51 };
52 
53 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
54  int log_offset, void *log_ctx)
55 {
56  FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
57  int err, fd = avpriv_open(filename, O_RDONLY);
58  struct stat st;
59  av_unused void *ptr;
60  off_t off_size;
61  char errbuf[128];
62  *bufptr = NULL;
63 
64  if (fd < 0) {
65  err = AVERROR(errno);
66  av_strerror(err, errbuf, sizeof(errbuf));
67  av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
68  return err;
69  }
70 
71  if (fstat(fd, &st) < 0) {
72  err = AVERROR(errno);
73  av_strerror(err, errbuf, sizeof(errbuf));
74  av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
75  close(fd);
76  return err;
77  }
78 
79  off_size = st.st_size;
80  if (off_size > SIZE_MAX) {
81  av_log(&file_log_ctx, AV_LOG_ERROR,
82  "File size for file '%s' is too big\n", filename);
83  close(fd);
84  return AVERROR(EINVAL);
85  }
86  *size = off_size;
87 
88  if (!*size) {
89  *bufptr = NULL;
90  goto out;
91  }
92 
93 #if HAVE_MMAP
94  ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
95  if (ptr == MAP_FAILED) {
96  err = AVERROR(errno);
97  av_strerror(err, errbuf, sizeof(errbuf));
98  av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
99  close(fd);
100  return err;
101  }
102  *bufptr = ptr;
103 #elif HAVE_MAPVIEWOFFILE
104  {
105  HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
106 
107  mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
108  if (!mh) {
109  av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
110  close(fd);
111  return -1;
112  }
113 
114  ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
115  CloseHandle(mh);
116  if (!ptr) {
117  av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
118  close(fd);
119  return -1;
120  }
121 
122  *bufptr = ptr;
123  }
124 #else
125  *bufptr = av_malloc(*size);
126  if (!*bufptr) {
127  av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
128  close(fd);
129  return AVERROR(ENOMEM);
130  }
131  read(fd, *bufptr, *size);
132 #endif
133 
134 out:
135  close(fd);
136  return 0;
137 }
138 
139 void av_file_unmap(uint8_t *bufptr, size_t size)
140 {
141  if (!size)
142  return;
143 #if HAVE_MMAP
144  munmap(bufptr, size);
145 #elif HAVE_MAPVIEWOFFILE
146  UnmapViewOfFile(bufptr);
147 #else
148  av_free(bufptr);
149 #endif
150 }
151 
152 int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx) {
153  return avpriv_tempfile(prefix, filename, log_offset, log_ctx);
154 }
#define NULL
Definition: coverity.c:32
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
Memory handling functions.
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
Wrapper to work around the lack of mkstemp() on mingw.
Definition: file_open.c:110
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
uint8_t
#define av_malloc(s)
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:66
Misc file utilities.
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition: file.c:139
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition: file.c:53
#define AVERROR(e)
Definition: error.h:43
common internal API header
PVOID HANDLE
int log_offset
Definition: file.c:40
int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
Wrapper to work around the lack of mkstemp() on mingw.
Definition: file.c:152
Describe the class of an AVClass context structure.
Definition: log.h:67
void * log_ctx
Definition: file.c:41
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:105
static const AVClass file_log_ctx_class
Definition: file.c:44
#define av_free(p)
FILE * out
Definition: movenc.c:54
#define av_unused
Definition: attributes.h:125
#define mh