file_open.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. static int win32_open(const char *filename_utf8, int oflag, int pmode)
  39. {
  40. int fd;
  41. int num_chars;
  42. wchar_t *filename_w;
  43. /* convert UTF-8 to wide chars */
  44. num_chars = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename_utf8, -1, NULL, 0);
  45. if (num_chars <= 0)
  46. goto fallback;
  47. filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
  48. if (!filename_w) {
  49. errno = ENOMEM;
  50. return -1;
  51. }
  52. MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
  53. fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
  54. av_freep(&filename_w);
  55. if (fd != -1 || (oflag & O_CREAT))
  56. return fd;
  57. fallback:
  58. /* filename may be in CP_ACP */
  59. return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
  60. }
  61. #define open win32_open
  62. #endif
  63. int avpriv_open(const char *filename, int flags, ...)
  64. {
  65. int fd;
  66. unsigned int mode = 0;
  67. va_list ap;
  68. va_start(ap, flags);
  69. if (flags & O_CREAT)
  70. mode = va_arg(ap, unsigned int);
  71. va_end(ap);
  72. #ifdef O_CLOEXEC
  73. flags |= O_CLOEXEC;
  74. #endif
  75. fd = open(filename, flags, mode);
  76. #if HAVE_FCNTL
  77. if (fd != -1) {
  78. if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
  79. av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
  80. }
  81. #endif
  82. return fd;
  83. }