• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavutil/file.c

Go to the documentation of this file.
00001 /*
00002  * This file is part of Libav.
00003  *
00004  * Libav is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * Libav is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with Libav; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00017  */
00018 
00019 #include "file.h"
00020 #include "log.h"
00021 #include <fcntl.h>
00022 #include <sys/stat.h>
00023 #include <unistd.h>
00024 #if HAVE_MMAP
00025 #include <sys/mman.h>
00026 #elif HAVE_MAPVIEWOFFILE
00027 #include <io.h>
00028 #include <windows.h>
00029 #endif
00030 
00031 typedef struct {
00032     const AVClass *class;
00033     int   log_offset;
00034     void *log_ctx;
00035 } FileLogContext;
00036 
00037 static const AVClass file_log_ctx_class = {
00038     "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
00039     offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
00040 };
00041 
00042 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
00043                 int log_offset, void *log_ctx)
00044 {
00045     FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
00046     int err, fd = open(filename, O_RDONLY);
00047     struct stat st;
00048     av_unused void *ptr;
00049     off_t off_size;
00050     char errbuf[128];
00051     *bufptr = NULL;
00052 
00053     if (fd < 0) {
00054         err = AVERROR(errno);
00055         av_strerror(err, errbuf, sizeof(errbuf));
00056         av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
00057         return err;
00058     }
00059 
00060     if (fstat(fd, &st) < 0) {
00061         err = AVERROR(errno);
00062         av_strerror(err, errbuf, sizeof(errbuf));
00063         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
00064         close(fd);
00065         return err;
00066     }
00067 
00068     off_size = st.st_size;
00069     if (off_size > SIZE_MAX) {
00070         av_log(&file_log_ctx, AV_LOG_ERROR,
00071                "File size for file '%s' is too big\n", filename);
00072         close(fd);
00073         return AVERROR(EINVAL);
00074     }
00075     *size = off_size;
00076 
00077 #if HAVE_MMAP
00078     ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
00079     if (ptr == MAP_FAILED) {
00080         err = AVERROR(errno);
00081         av_strerror(err, errbuf, sizeof(errbuf));
00082         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
00083         close(fd);
00084         return err;
00085     }
00086     *bufptr = ptr;
00087 #elif HAVE_MAPVIEWOFFILE
00088     {
00089         HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
00090 
00091         mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
00092         if (!mh) {
00093             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
00094             close(fd);
00095             return -1;
00096         }
00097 
00098         ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
00099         CloseHandle(mh);
00100         if (!ptr) {
00101             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
00102             close(fd);
00103             return -1;
00104         }
00105 
00106         *bufptr = ptr;
00107     }
00108 #else
00109     *bufptr = av_malloc(*size);
00110     if (!*bufptr) {
00111         av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
00112         close(fd);
00113         return AVERROR(ENOMEM);
00114     }
00115     read(fd, *bufptr, *size);
00116 #endif
00117 
00118     close(fd);
00119     return 0;
00120 }
00121 
00122 void av_file_unmap(uint8_t *bufptr, size_t size)
00123 {
00124 #if HAVE_MMAP
00125     munmap(bufptr, size);
00126 #elif HAVE_MAPVIEWOFFILE
00127     UnmapViewOfFile(bufptr);
00128 #else
00129     av_free(bufptr);
00130 #endif
00131 }
00132 
00133 #ifdef TEST
00134 
00135 #undef printf
00136 
00137 int main(void)
00138 {
00139     uint8_t *buf;
00140     size_t size;
00141     if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
00142         return 1;
00143 
00144     buf[0] = 's';
00145     printf("%s", buf);
00146     av_file_unmap(buf, size);
00147     return 0;
00148 }
00149 #endif
Generated on Thu Jan 24 2013 17:08:55 for Libav by doxygen 1.7.1