LCOV - code coverage report
Current view: top level - libavutil - file.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 32 9.4 %
Date: 2017-12-15 02:19:58 Functions: 1 3 33.3 %

          Line data    Source code
       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;
      40             :     int   log_offset;
      41             :     void *log_ctx;
      42             : } FileLogContext;
      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           0 : int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
      54             :                 int log_offset, void *log_ctx)
      55             : {
      56           0 :     FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
      57           0 :     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           0 :     *bufptr = NULL;
      63             : 
      64           0 :     if (fd < 0) {
      65           0 :         err = AVERROR(errno);
      66           0 :         av_strerror(err, errbuf, sizeof(errbuf));
      67           0 :         av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
      68           0 :         return err;
      69             :     }
      70             : 
      71           0 :     if (fstat(fd, &st) < 0) {
      72           0 :         err = AVERROR(errno);
      73           0 :         av_strerror(err, errbuf, sizeof(errbuf));
      74           0 :         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
      75           0 :         close(fd);
      76           0 :         return err;
      77             :     }
      78             : 
      79           0 :     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           0 :     *size = off_size;
      87             : 
      88             : #if HAVE_MMAP
      89           0 :     ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
      90           0 :     if (ptr == MAP_FAILED) {
      91           0 :         err = AVERROR(errno);
      92           0 :         av_strerror(err, errbuf, sizeof(errbuf));
      93           0 :         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
      94           0 :         close(fd);
      95           0 :         return err;
      96             :     }
      97           0 :     *bufptr = ptr;
      98             : #elif HAVE_MAPVIEWOFFILE
      99             :     {
     100             :         HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
     101             : 
     102             :         mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
     103             :         if (!mh) {
     104             :             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
     105             :             close(fd);
     106             :             return -1;
     107             :         }
     108             : 
     109             :         ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
     110             :         CloseHandle(mh);
     111             :         if (!ptr) {
     112             :             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
     113             :             close(fd);
     114             :             return -1;
     115             :         }
     116             : 
     117             :         *bufptr = ptr;
     118             :     }
     119             : #else
     120             :     *bufptr = av_malloc(*size);
     121             :     if (!*bufptr) {
     122             :         av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
     123             :         close(fd);
     124             :         return AVERROR(ENOMEM);
     125             :     }
     126             :     read(fd, *bufptr, *size);
     127             : #endif
     128             : 
     129           0 :     close(fd);
     130           0 :     return 0;
     131             : }
     132             : 
     133           1 : void av_file_unmap(uint8_t *bufptr, size_t size)
     134             : {
     135             : #if HAVE_MMAP
     136           1 :     munmap(bufptr, size);
     137             : #elif HAVE_MAPVIEWOFFILE
     138             :     UnmapViewOfFile(bufptr);
     139             : #else
     140             :     av_free(bufptr);
     141             : #endif
     142           1 : }
     143             : 
     144           0 : int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx) {
     145           0 :     return avpriv_tempfile(prefix, filename, log_offset, log_ctx);
     146             : }

Generated by: LCOV version 1.13