FFmpeg
file_open.c
Go to the documentation of this file.
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 int avpriv_open(const char *filename, int flags, ...)
68 {
69  int fd;
70  unsigned int mode = 0;
71  va_list ap;
72 
73  va_start(ap, flags);
74  if (flags & O_CREAT)
75  mode = va_arg(ap, unsigned int);
76  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  fd = open(filename, flags, mode);
86 #if HAVE_FCNTL
87  if (fd != -1) {
88  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  return fd;
94 }
95 
96 typedef struct FileLogContext {
97  const AVClass *class;
98  int log_offset;
99  void *log_ctx;
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 int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
112 {
113  FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
114  int fd = -1;
115 #if !HAVE_MKSTEMP
116  void *ptr= tempnam(NULL, prefix);
117  if(!ptr)
118  ptr= tempnam(".", prefix);
119  *filename = av_strdup(ptr);
120 #undef free
121  free(ptr);
122 #else
123  size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
124  *filename = av_malloc(len);
125 #endif
126  /* -----common section-----*/
127  if (!*filename) {
128  av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
129  return AVERROR(ENOMEM);
130  }
131 #if !HAVE_MKSTEMP
132 # ifndef O_BINARY
133 # define O_BINARY 0
134 # endif
135 # ifndef O_EXCL
136 # define O_EXCL 0
137 # endif
138  fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
139 #else
140  snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
141  fd = mkstemp(*filename);
142 #if defined(_WIN32) || defined (__ANDROID__) || defined(__DJGPP__)
143  if (fd < 0) {
144  snprintf(*filename, len, "./%sXXXXXX", prefix);
145  fd = mkstemp(*filename);
146  }
147 #endif
148 #endif
149  /* -----common section-----*/
150  if (fd < 0) {
151  int err = AVERROR(errno);
152  av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
153  av_freep(filename);
154  return err;
155  }
156  return fd; /* success */
157 }
158 
159 FILE *avpriv_fopen_utf8(const char *path, const char *mode)
160 {
161  int fd;
162  int access;
163  const char *m = mode;
164 
165  switch (*m++) {
166  case 'r': access = O_RDONLY; break;
167  case 'w': access = O_CREAT|O_WRONLY|O_TRUNC; break;
168  case 'a': access = O_CREAT|O_WRONLY|O_APPEND; break;
169  default :
170  errno = EINVAL;
171  return NULL;
172  }
173  while (*m) {
174  if (*m == '+') {
175  access &= ~(O_RDONLY | O_WRONLY);
176  access |= O_RDWR;
177  } else if (*m == 'b') {
178 #ifdef O_BINARY
179  access |= O_BINARY;
180 #endif
181  } else if (*m) {
182  errno = EINVAL;
183  return NULL;
184  }
185  m++;
186  }
187  fd = avpriv_open(path, access, 0666);
188  if (fd == -1)
189  return NULL;
190  return fdopen(fd, mode);
191 }
192 
193 #if FF_API_AV_FOPEN_UTF8
194 FILE *av_fopen_utf8(const char *path, const char *mode)
195 {
196  return avpriv_fopen_utf8(path, mode);
197 }
198 #endif
wchar_filename.h
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
FileLogContext::log_offset
int log_offset
Definition: file.c:41
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
avpriv_open
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:67
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
file_open.h
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
O_EXCL
#define O_EXCL
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
avpriv_tempfile
int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
Wrapper to work around the lack of mkstemp() on mingw.
Definition: file_open.c:111
av_fopen_utf8
FILE * av_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition: file_open.c:194
FileLogContext
Definition: file.c:39
file_log_ctx_class
static const AVClass file_log_ctx_class
Definition: file_open.c:102
len
int len
Definition: vorbis_enc_data.h:426
avpriv_fopen_utf8
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition: file_open.c:159
O_BINARY
#define O_BINARY
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
FileLogContext::log_ctx
void * log_ctx
Definition: file.c:42
mode
mode
Definition: ebur128.h:83
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:280
avutil.h
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
snprintf
#define snprintf
Definition: snprintf.h:34