00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "config.h"
00020 #include "file.h"
00021 #include "log.h"
00022 #include "mem.h"
00023 #include <fcntl.h>
00024 #include <sys/stat.h>
00025 #if HAVE_UNISTD_H
00026 #include <unistd.h>
00027 #endif
00028 #if HAVE_MMAP
00029 #include <sys/mman.h>
00030 #elif HAVE_MAPVIEWOFFILE
00031 #include <io.h>
00032 #include <windows.h>
00033 #endif
00034
00035 typedef struct {
00036 const AVClass *class;
00037 int log_offset;
00038 void *log_ctx;
00039 } FileLogContext;
00040
00041 static const AVClass file_log_ctx_class = {
00042 "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
00043 offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
00044 };
00045
00046 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
00047 int log_offset, void *log_ctx)
00048 {
00049 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
00050 int err, fd = open(filename, O_RDONLY);
00051 struct stat st;
00052 av_unused void *ptr;
00053 off_t off_size;
00054 char errbuf[128];
00055 *bufptr = NULL;
00056
00057 if (fd < 0) {
00058 err = AVERROR(errno);
00059 av_strerror(err, errbuf, sizeof(errbuf));
00060 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
00061 return err;
00062 }
00063
00064 if (fstat(fd, &st) < 0) {
00065 err = AVERROR(errno);
00066 av_strerror(err, errbuf, sizeof(errbuf));
00067 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
00068 close(fd);
00069 return err;
00070 }
00071
00072 off_size = st.st_size;
00073 if (off_size > SIZE_MAX) {
00074 av_log(&file_log_ctx, AV_LOG_ERROR,
00075 "File size for file '%s' is too big\n", filename);
00076 close(fd);
00077 return AVERROR(EINVAL);
00078 }
00079 *size = off_size;
00080
00081 #if HAVE_MMAP
00082 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
00083 if (ptr == MAP_FAILED) {
00084 err = AVERROR(errno);
00085 av_strerror(err, errbuf, sizeof(errbuf));
00086 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
00087 close(fd);
00088 return err;
00089 }
00090 *bufptr = ptr;
00091 #elif HAVE_MAPVIEWOFFILE
00092 {
00093 HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
00094
00095 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
00096 if (!mh) {
00097 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
00098 close(fd);
00099 return -1;
00100 }
00101
00102 ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
00103 CloseHandle(mh);
00104 if (!ptr) {
00105 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
00106 close(fd);
00107 return -1;
00108 }
00109
00110 *bufptr = ptr;
00111 }
00112 #else
00113 *bufptr = av_malloc(*size);
00114 if (!*bufptr) {
00115 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
00116 close(fd);
00117 return AVERROR(ENOMEM);
00118 }
00119 read(fd, *bufptr, *size);
00120 #endif
00121
00122 close(fd);
00123 return 0;
00124 }
00125
00126 void av_file_unmap(uint8_t *bufptr, size_t size)
00127 {
00128 #if HAVE_MMAP
00129 munmap(bufptr, size);
00130 #elif HAVE_MAPVIEWOFFILE
00131 UnmapViewOfFile(bufptr);
00132 #else
00133 av_free(bufptr);
00134 #endif
00135 }
00136
00137 int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx) {
00138 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
00139 int fd=-1;
00140 #if !HAVE_MKSTEMP
00141 void *ptr= tempnam(NULL, prefix);
00142 if(!ptr)
00143 ptr= tempnam(".", prefix);
00144 *filename = av_strdup(ptr);
00145 #undef free
00146 free(ptr);
00147 #else
00148 size_t len = strlen(prefix) + 12;
00149 *filename = av_malloc(len);
00150 #endif
00151
00152 if (*filename == NULL) {
00153 av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
00154 return AVERROR(ENOMEM);
00155 }
00156 #if !HAVE_MKSTEMP
00157 # ifndef O_BINARY
00158 # define O_BINARY 0
00159 # endif
00160 # ifndef O_EXCL
00161 # define O_EXCL 0
00162 # endif
00163 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
00164 #else
00165 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
00166 fd = mkstemp(*filename);
00167 #ifdef _WIN32
00168 if (fd < 0) {
00169 snprintf(*filename, len, "./%sXXXXXX", prefix);
00170 fd = mkstemp(*filename);
00171 }
00172 #endif
00173 #endif
00174
00175 if (fd < 0) {
00176 int err = AVERROR(errno);
00177 av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
00178 return err;
00179 }
00180 return fd;
00181 }
00182
00183 #ifdef TEST
00184
00185 #undef printf
00186
00187 int main(void)
00188 {
00189 uint8_t *buf;
00190 size_t size;
00191 if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
00192 return 1;
00193
00194 buf[0] = 's';
00195 printf("%s", buf);
00196 av_file_unmap(buf, size);
00197 return 0;
00198 }
00199 #endif
00200