file_open.c 4.8 KB

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