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