LCOV - code coverage report
Current view: top level - libavutil - file_open.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 20 49 40.8 %
Date: 2017-10-18 21:45:51 Functions: 2 3 66.7 %

          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 "internal.h"
      21             : #include "mem.h"
      22             : #include <stdarg.h>
      23             : #include <fcntl.h>
      24             : #include <sys/stat.h>
      25             : #if HAVE_UNISTD_H
      26             : #include <unistd.h>
      27             : #endif
      28             : #if HAVE_IO_H
      29             : #include <io.h>
      30             : #endif
      31             : 
      32             : #if defined(_WIN32) && !defined(__MINGW32CE__)
      33             : #undef open
      34             : #undef lseek
      35             : #undef stat
      36             : #undef fstat
      37             : #include <windows.h>
      38             : #include <share.h>
      39             : #include <errno.h>
      40             : #include "wchar_filename.h"
      41             : 
      42             : static int win32_open(const char *filename_utf8, int oflag, int pmode)
      43             : {
      44             :     int fd;
      45             :     wchar_t *filename_w;
      46             : 
      47             :     /* convert UTF-8 to wide chars */
      48             :     if (utf8towchar(filename_utf8, &filename_w))
      49             :         return -1;
      50             :     if (!filename_w)
      51             :         goto fallback;
      52             : 
      53             :     fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
      54             :     av_freep(&filename_w);
      55             : 
      56             :     if (fd != -1 || (oflag & O_CREAT))
      57             :         return fd;
      58             : 
      59             : fallback:
      60             :     /* filename may be in CP_ACP */
      61             :     return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
      62             : }
      63             : #define open win32_open
      64             : #endif
      65             : 
      66       72690 : int avpriv_open(const char *filename, int flags, ...)
      67             : {
      68             :     int fd;
      69       72690 :     unsigned int mode = 0;
      70             :     va_list ap;
      71             : 
      72       72690 :     va_start(ap, flags);
      73       72690 :     if (flags & O_CREAT)
      74        1943 :         mode = va_arg(ap, unsigned int);
      75       72690 :     va_end(ap);
      76             : 
      77             : #ifdef O_CLOEXEC
      78             :     flags |= O_CLOEXEC;
      79             : #endif
      80             : #ifdef O_NOINHERIT
      81             :     flags |= O_NOINHERIT;
      82             : #endif
      83             : 
      84       72690 :     fd = open(filename, flags, mode);
      85             : #if HAVE_FCNTL
      86       72690 :     if (fd != -1) {
      87       72689 :         if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
      88           0 :             av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
      89             :     }
      90             : #endif
      91             : 
      92       72690 :     return fd;
      93             : }
      94             : 
      95             : typedef struct FileLogContext {
      96             :     const AVClass *class;
      97             :     int   log_offset;
      98             :     void *log_ctx;
      99             : } FileLogContext;
     100             : 
     101             : static const AVClass file_log_ctx_class = {
     102             :     "TEMPFILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
     103             :     offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
     104             : };
     105             : 
     106           1 : int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
     107             : {
     108           1 :     FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
     109           1 :     int fd = -1;
     110             : #if !HAVE_MKSTEMP
     111             :     void *ptr= tempnam(NULL, prefix);
     112             :     if(!ptr)
     113             :         ptr= tempnam(".", prefix);
     114             :     *filename = av_strdup(ptr);
     115             : #undef free
     116             :     free(ptr);
     117             : #else
     118           1 :     size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
     119           1 :     *filename  = av_malloc(len);
     120             : #endif
     121             :     /* -----common section-----*/
     122           1 :     if (!*filename) {
     123           0 :         av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
     124           0 :         return AVERROR(ENOMEM);
     125             :     }
     126             : #if !HAVE_MKSTEMP
     127             : #   ifndef O_BINARY
     128             : #       define O_BINARY 0
     129             : #   endif
     130             : #   ifndef O_EXCL
     131             : #       define O_EXCL 0
     132             : #   endif
     133             :     fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
     134             : #else
     135           1 :     snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
     136           1 :     fd = mkstemp(*filename);
     137             : #if defined(_WIN32) || defined (__ANDROID__)
     138             :     if (fd < 0) {
     139             :         snprintf(*filename, len, "./%sXXXXXX", prefix);
     140             :         fd = mkstemp(*filename);
     141             :     }
     142             : #endif
     143             : #endif
     144             :     /* -----common section-----*/
     145           1 :     if (fd < 0) {
     146           0 :         int err = AVERROR(errno);
     147           0 :         av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
     148           0 :         av_freep(filename);
     149           0 :         return err;
     150             :     }
     151           1 :     return fd; /* success */
     152             : }
     153             : 
     154           0 : FILE *av_fopen_utf8(const char *path, const char *mode)
     155             : {
     156             :     int fd;
     157             :     int access;
     158           0 :     const char *m = mode;
     159             : 
     160           0 :     switch (*m++) {
     161           0 :     case 'r': access = O_RDONLY; break;
     162           0 :     case 'w': access = O_CREAT|O_WRONLY|O_TRUNC; break;
     163           0 :     case 'a': access = O_CREAT|O_WRONLY|O_APPEND; break;
     164           0 :     default :
     165           0 :         errno = EINVAL;
     166           0 :         return NULL;
     167             :     }
     168           0 :     while (*m) {
     169           0 :         if (*m == '+') {
     170           0 :             access &= ~(O_RDONLY | O_WRONLY);
     171           0 :             access |= O_RDWR;
     172           0 :         } else if (*m == 'b') {
     173             : #ifdef O_BINARY
     174             :             access |= O_BINARY;
     175             : #endif
     176           0 :         } else if (*m) {
     177           0 :             errno = EINVAL;
     178           0 :             return NULL;
     179             :         }
     180           0 :         m++;
     181             :     }
     182           0 :     fd = avpriv_open(path, access, 0666);
     183           0 :     if (fd == -1)
     184           0 :         return NULL;
     185           0 :     return fdopen(fd, mode);
     186             : }

Generated by: LCOV version 1.13