annotate ffmpeg/libavutil/file.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * This file is part of FFmpeg.
yading@11 3 *
yading@11 4 * FFmpeg is free software; you can redistribute it and/or
yading@11 5 * modify it under the terms of the GNU Lesser General Public
yading@11 6 * License as published by the Free Software Foundation; either
yading@11 7 * version 2.1 of the License, or (at your option) any later version.
yading@11 8 *
yading@11 9 * FFmpeg is distributed in the hope that it will be useful,
yading@11 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 12 * Lesser General Public License for more details.
yading@11 13 *
yading@11 14 * You should have received a copy of the GNU Lesser General Public
yading@11 15 * License along with FFmpeg; if not, write to the Free Software
yading@11 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 17 */
yading@11 18
yading@11 19 #include "config.h"
yading@11 20 #include "file.h"
yading@11 21 #include "log.h"
yading@11 22 #include "mem.h"
yading@11 23 #include <fcntl.h>
yading@11 24 #include <sys/stat.h>
yading@11 25 #if HAVE_UNISTD_H
yading@11 26 #include <unistd.h>
yading@11 27 #endif
yading@11 28 #if HAVE_IO_H
yading@11 29 #include <io.h>
yading@11 30 #endif
yading@11 31 #if HAVE_MMAP
yading@11 32 #include <sys/mman.h>
yading@11 33 #elif HAVE_MAPVIEWOFFILE
yading@11 34 #include <windows.h>
yading@11 35 #endif
yading@11 36
yading@11 37 typedef struct {
yading@11 38 const AVClass *class;
yading@11 39 int log_offset;
yading@11 40 void *log_ctx;
yading@11 41 } FileLogContext;
yading@11 42
yading@11 43 static const AVClass file_log_ctx_class = {
yading@11 44 "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
yading@11 45 offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
yading@11 46 };
yading@11 47
yading@11 48 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
yading@11 49 int log_offset, void *log_ctx)
yading@11 50 {
yading@11 51 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
yading@11 52 int err, fd = open(filename, O_RDONLY);
yading@11 53 struct stat st;
yading@11 54 av_unused void *ptr;
yading@11 55 off_t off_size;
yading@11 56 char errbuf[128];
yading@11 57 *bufptr = NULL;
yading@11 58
yading@11 59 if (fd < 0) {
yading@11 60 err = AVERROR(errno);
yading@11 61 av_strerror(err, errbuf, sizeof(errbuf));
yading@11 62 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
yading@11 63 return err;
yading@11 64 }
yading@11 65
yading@11 66 if (fstat(fd, &st) < 0) {
yading@11 67 err = AVERROR(errno);
yading@11 68 av_strerror(err, errbuf, sizeof(errbuf));
yading@11 69 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
yading@11 70 close(fd);
yading@11 71 return err;
yading@11 72 }
yading@11 73
yading@11 74 off_size = st.st_size;
yading@11 75 if (off_size > SIZE_MAX) {
yading@11 76 av_log(&file_log_ctx, AV_LOG_ERROR,
yading@11 77 "File size for file '%s' is too big\n", filename);
yading@11 78 close(fd);
yading@11 79 return AVERROR(EINVAL);
yading@11 80 }
yading@11 81 *size = off_size;
yading@11 82
yading@11 83 #if HAVE_MMAP
yading@11 84 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
yading@11 85 if (ptr == MAP_FAILED) {
yading@11 86 err = AVERROR(errno);
yading@11 87 av_strerror(err, errbuf, sizeof(errbuf));
yading@11 88 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
yading@11 89 close(fd);
yading@11 90 return err;
yading@11 91 }
yading@11 92 *bufptr = ptr;
yading@11 93 #elif HAVE_MAPVIEWOFFILE
yading@11 94 {
yading@11 95 HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
yading@11 96
yading@11 97 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
yading@11 98 if (!mh) {
yading@11 99 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
yading@11 100 close(fd);
yading@11 101 return -1;
yading@11 102 }
yading@11 103
yading@11 104 ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
yading@11 105 CloseHandle(mh);
yading@11 106 if (!ptr) {
yading@11 107 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
yading@11 108 close(fd);
yading@11 109 return -1;
yading@11 110 }
yading@11 111
yading@11 112 *bufptr = ptr;
yading@11 113 }
yading@11 114 #else
yading@11 115 *bufptr = av_malloc(*size);
yading@11 116 if (!*bufptr) {
yading@11 117 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
yading@11 118 close(fd);
yading@11 119 return AVERROR(ENOMEM);
yading@11 120 }
yading@11 121 read(fd, *bufptr, *size);
yading@11 122 #endif
yading@11 123
yading@11 124 close(fd);
yading@11 125 return 0;
yading@11 126 }
yading@11 127
yading@11 128 void av_file_unmap(uint8_t *bufptr, size_t size)
yading@11 129 {
yading@11 130 #if HAVE_MMAP
yading@11 131 munmap(bufptr, size);
yading@11 132 #elif HAVE_MAPVIEWOFFILE
yading@11 133 UnmapViewOfFile(bufptr);
yading@11 134 #else
yading@11 135 av_free(bufptr);
yading@11 136 #endif
yading@11 137 }
yading@11 138
yading@11 139 int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx) {
yading@11 140 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
yading@11 141 int fd=-1;
yading@11 142 #if !HAVE_MKSTEMP
yading@11 143 void *ptr= tempnam(NULL, prefix);
yading@11 144 if(!ptr)
yading@11 145 ptr= tempnam(".", prefix);
yading@11 146 *filename = av_strdup(ptr);
yading@11 147 #undef free
yading@11 148 free(ptr);
yading@11 149 #else
yading@11 150 size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
yading@11 151 *filename = av_malloc(len);
yading@11 152 #endif
yading@11 153 /* -----common section-----*/
yading@11 154 if (*filename == NULL) {
yading@11 155 av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
yading@11 156 return AVERROR(ENOMEM);
yading@11 157 }
yading@11 158 #if !HAVE_MKSTEMP
yading@11 159 # ifndef O_BINARY
yading@11 160 # define O_BINARY 0
yading@11 161 # endif
yading@11 162 # ifndef O_EXCL
yading@11 163 # define O_EXCL 0
yading@11 164 # endif
yading@11 165 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
yading@11 166 #else
yading@11 167 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
yading@11 168 fd = mkstemp(*filename);
yading@11 169 #ifdef _WIN32
yading@11 170 if (fd < 0) {
yading@11 171 snprintf(*filename, len, "./%sXXXXXX", prefix);
yading@11 172 fd = mkstemp(*filename);
yading@11 173 }
yading@11 174 #endif
yading@11 175 #endif
yading@11 176 /* -----common section-----*/
yading@11 177 if (fd < 0) {
yading@11 178 int err = AVERROR(errno);
yading@11 179 av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
yading@11 180 av_freep(filename);
yading@11 181 return err;
yading@11 182 }
yading@11 183 return fd; /* success */
yading@11 184 }
yading@11 185
yading@11 186 #ifdef TEST
yading@11 187
yading@11 188 #undef printf
yading@11 189
yading@11 190 int main(void)
yading@11 191 {
yading@11 192 uint8_t *buf;
yading@11 193 size_t size;
yading@11 194 if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
yading@11 195 return 1;
yading@11 196
yading@11 197 buf[0] = 's';
yading@11 198 printf("%s", buf);
yading@11 199 av_file_unmap(buf, size);
yading@11 200 return 0;
yading@11 201 }
yading@11 202 #endif
yading@11 203