2013-08-09 07:58:15 +00:00
|
|
|
/*
|
2013-08-10 08:36:18 +00:00
|
|
|
* This file is part of FFmpeg.
|
2013-08-09 07:58:15 +00:00
|
|
|
*
|
2013-08-10 08:36:18 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2013-08-09 07:58:15 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-08-10 08:36:18 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2013-08-09 07:58:15 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-08-10 08:36:18 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2013-08-09 07:58:15 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_WIN32) && !defined(__MINGW32CE__)
|
|
|
|
#undef open
|
|
|
|
#undef lseek
|
|
|
|
#undef stat
|
|
|
|
#undef fstat
|
|
|
|
#include <windows.h>
|
|
|
|
#include <share.h>
|
|
|
|
#include <errno.h>
|
2014-11-25 08:39:50 +00:00
|
|
|
#include "wchar_filename.h"
|
2013-08-09 07:58:15 +00:00
|
|
|
|
|
|
|
static int win32_open(const char *filename_utf8, int oflag, int pmode)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
wchar_t *filename_w;
|
|
|
|
|
|
|
|
/* convert UTF-8 to wide chars */
|
2014-11-25 08:39:50 +00:00
|
|
|
if (utf8towchar(filename_utf8, &filename_w))
|
2013-08-09 07:58:15 +00:00
|
|
|
return -1;
|
2014-11-25 08:39:50 +00:00
|
|
|
if (!filename_w)
|
|
|
|
goto fallback;
|
2013-08-09 07:58:15 +00:00
|
|
|
|
|
|
|
fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
|
|
|
|
av_freep(&filename_w);
|
|
|
|
|
|
|
|
if (fd != -1 || (oflag & O_CREAT))
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
fallback:
|
2013-08-12 17:40:29 +00:00
|
|
|
/* filename may be in CP_ACP */
|
2013-08-09 07:58:15 +00:00
|
|
|
return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
|
|
|
|
}
|
|
|
|
#define open win32_open
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int avpriv_open(const char *filename, int flags, ...)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
unsigned int mode = 0;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, flags);
|
|
|
|
if (flags & O_CREAT)
|
|
|
|
mode = va_arg(ap, unsigned int);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
#ifdef O_CLOEXEC
|
|
|
|
flags |= O_CLOEXEC;
|
|
|
|
#endif
|
2015-10-29 08:11:37 +00:00
|
|
|
#ifdef O_NOINHERIT
|
|
|
|
flags |= O_NOINHERIT;
|
|
|
|
#endif
|
2013-08-09 07:58:15 +00:00
|
|
|
|
|
|
|
fd = open(filename, flags, mode);
|
|
|
|
#if HAVE_FCNTL
|
2013-10-20 16:36:09 +00:00
|
|
|
if (fd != -1) {
|
|
|
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
|
|
|
|
}
|
2013-08-09 07:58:15 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
2013-11-01 00:51:14 +00:00
|
|
|
|
2016-03-09 13:33:25 +00:00
|
|
|
typedef struct FileLogContext {
|
|
|
|
const AVClass *class;
|
|
|
|
int log_offset;
|
|
|
|
void *log_ctx;
|
|
|
|
} FileLogContext;
|
|
|
|
|
|
|
|
static const AVClass file_log_ctx_class = {
|
|
|
|
"TEMPFILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
|
|
|
|
offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
|
|
|
|
};
|
|
|
|
|
|
|
|
int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
|
|
|
|
{
|
|
|
|
FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
|
|
|
|
int fd = -1;
|
|
|
|
#if !HAVE_MKSTEMP
|
|
|
|
void *ptr= tempnam(NULL, prefix);
|
|
|
|
if(!ptr)
|
|
|
|
ptr= tempnam(".", prefix);
|
|
|
|
*filename = av_strdup(ptr);
|
|
|
|
#undef free
|
|
|
|
free(ptr);
|
|
|
|
#else
|
|
|
|
size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
|
|
|
|
*filename = av_malloc(len);
|
|
|
|
#endif
|
|
|
|
/* -----common section-----*/
|
|
|
|
if (!*filename) {
|
|
|
|
av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
#if !HAVE_MKSTEMP
|
|
|
|
# ifndef O_BINARY
|
|
|
|
# define O_BINARY 0
|
|
|
|
# endif
|
|
|
|
# ifndef O_EXCL
|
|
|
|
# define O_EXCL 0
|
|
|
|
# endif
|
|
|
|
fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
|
|
|
|
#else
|
|
|
|
snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
|
|
|
|
fd = mkstemp(*filename);
|
2016-06-11 20:41:18 +00:00
|
|
|
#if defined(_WIN32) || defined (__ANDROID__)
|
2016-03-09 13:33:25 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
snprintf(*filename, len, "./%sXXXXXX", prefix);
|
|
|
|
fd = mkstemp(*filename);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
/* -----common section-----*/
|
|
|
|
if (fd < 0) {
|
|
|
|
int err = AVERROR(errno);
|
|
|
|
av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
|
|
|
|
av_freep(filename);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return fd; /* success */
|
|
|
|
}
|
|
|
|
|
2013-11-01 00:51:14 +00:00
|
|
|
FILE *av_fopen_utf8(const char *path, const char *mode)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int access;
|
|
|
|
const char *m = mode;
|
|
|
|
|
|
|
|
switch (*m++) {
|
|
|
|
case 'r': access = O_RDONLY; break;
|
|
|
|
case 'w': access = O_CREAT|O_WRONLY|O_TRUNC; break;
|
|
|
|
case 'a': access = O_CREAT|O_WRONLY|O_APPEND; break;
|
|
|
|
default :
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while (*m) {
|
|
|
|
if (*m == '+') {
|
|
|
|
access &= ~(O_RDONLY | O_WRONLY);
|
|
|
|
access |= O_RDWR;
|
|
|
|
} else if (*m == 'b') {
|
|
|
|
#ifdef O_BINARY
|
|
|
|
access |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
} else if (*m) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
m++;
|
|
|
|
}
|
|
|
|
fd = avpriv_open(path, access, 0666);
|
|
|
|
if (fd == -1)
|
|
|
|
return NULL;
|
|
|
|
return fdopen(fd, mode);
|
|
|
|
}
|