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