file_open.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #if defined(_WIN32) && !defined(__MINGW32CE__)
  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. "TEMPFILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
  89. offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
  90. };
  91. int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
  92. {
  93. FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
  94. int fd = -1;
  95. #if !HAVE_MKSTEMP
  96. void *ptr= tempnam(NULL, prefix);
  97. if(!ptr)
  98. ptr= tempnam(".", prefix);
  99. *filename = av_strdup(ptr);
  100. #undef free
  101. free(ptr);
  102. #else
  103. size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
  104. *filename = av_malloc(len);
  105. #endif
  106. /* -----common section-----*/
  107. if (!*filename) {
  108. av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
  109. return AVERROR(ENOMEM);
  110. }
  111. #if !HAVE_MKSTEMP
  112. # ifndef O_BINARY
  113. # define O_BINARY 0
  114. # endif
  115. # ifndef O_EXCL
  116. # define O_EXCL 0
  117. # endif
  118. fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
  119. #else
  120. snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
  121. fd = mkstemp(*filename);
  122. #if defined(_WIN32) || defined (__ANDROID__)
  123. if (fd < 0) {
  124. snprintf(*filename, len, "./%sXXXXXX", prefix);
  125. fd = mkstemp(*filename);
  126. }
  127. #endif
  128. #endif
  129. /* -----common section-----*/
  130. if (fd < 0) {
  131. int err = AVERROR(errno);
  132. av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
  133. av_freep(filename);
  134. return err;
  135. }
  136. return fd; /* success */
  137. }
  138. FILE *av_fopen_utf8(const char *path, const char *mode)
  139. {
  140. int fd;
  141. int access;
  142. const char *m = mode;
  143. switch (*m++) {
  144. case 'r': access = O_RDONLY; break;
  145. case 'w': access = O_CREAT|O_WRONLY|O_TRUNC; break;
  146. case 'a': access = O_CREAT|O_WRONLY|O_APPEND; break;
  147. default :
  148. errno = EINVAL;
  149. return NULL;
  150. }
  151. while (*m) {
  152. if (*m == '+') {
  153. access &= ~(O_RDONLY | O_WRONLY);
  154. access |= O_RDWR;
  155. } else if (*m == 'b') {
  156. #ifdef O_BINARY
  157. access |= O_BINARY;
  158. #endif
  159. } else if (*m) {
  160. errno = EINVAL;
  161. return NULL;
  162. }
  163. m++;
  164. }
  165. fd = avpriv_open(path, access, 0666);
  166. if (fd == -1)
  167. return NULL;
  168. return fdopen(fd, mode);
  169. }