You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.8KB

  1. /*
  2. * various OS-feature replacement utilities
  3. * copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVFORMAT_OS_SUPPORT_H
  22. #define AVFORMAT_OS_SUPPORT_H
  23. /**
  24. * @file
  25. * miscellaneous OS support macros and functions.
  26. */
  27. #include "config.h"
  28. #include <sys/stat.h>
  29. #ifdef _WIN32
  30. #if HAVE_DIRECT_H
  31. #include <direct.h>
  32. #endif
  33. #if HAVE_IO_H
  34. #include <io.h>
  35. #endif
  36. #endif
  37. #if defined(_WIN32) && !defined(__MINGW32CE__)
  38. # include <fcntl.h>
  39. # undef lseek
  40. # define lseek(f,p,w) _lseeki64((f), (p), (w))
  41. # undef stat
  42. # define stat _stati64
  43. # undef fstat
  44. # define fstat(f,s) _fstati64((f), (s))
  45. #endif /* defined(_WIN32) && !defined(__MINGW32CE__) */
  46. static inline int is_dos_path(const char *path)
  47. {
  48. #if HAVE_DOS_PATHS
  49. if (path[0] && path[1] == ':')
  50. return 1;
  51. #endif
  52. return 0;
  53. }
  54. #if defined(__OS2__)
  55. #define SHUT_RD 0
  56. #define SHUT_WR 1
  57. #define SHUT_RDWR 2
  58. #endif
  59. #if defined(_WIN32)
  60. #define SHUT_RD SD_RECEIVE
  61. #define SHUT_WR SD_SEND
  62. #define SHUT_RDWR SD_BOTH
  63. #ifndef S_IRUSR
  64. #define S_IRUSR S_IREAD
  65. #endif
  66. #ifndef S_IWUSR
  67. #define S_IWUSR S_IWRITE
  68. #endif
  69. #endif
  70. #if CONFIG_NETWORK
  71. #if !HAVE_SOCKLEN_T
  72. typedef int socklen_t;
  73. #endif
  74. /* most of the time closing a socket is just closing an fd */
  75. #if !HAVE_CLOSESOCKET
  76. #define closesocket close
  77. #endif
  78. #if !HAVE_POLL_H
  79. typedef unsigned long nfds_t;
  80. #if HAVE_WINSOCK2_H
  81. #include <winsock2.h>
  82. #endif
  83. #if !HAVE_STRUCT_POLLFD
  84. struct pollfd {
  85. int fd;
  86. short events; /* events to look for */
  87. short revents; /* events that occurred */
  88. };
  89. /* events & revents */
  90. #define POLLIN 0x0001 /* any readable data available */
  91. #define POLLOUT 0x0002 /* file descriptor is writeable */
  92. #define POLLRDNORM POLLIN
  93. #define POLLWRNORM POLLOUT
  94. #define POLLRDBAND 0x0008 /* priority readable data */
  95. #define POLLWRBAND 0x0010 /* priority data can be written */
  96. #define POLLPRI 0x0020 /* high priority readable data */
  97. /* revents only */
  98. #define POLLERR 0x0004 /* errors pending */
  99. #define POLLHUP 0x0080 /* disconnected */
  100. #define POLLNVAL 0x1000 /* invalid file descriptor */
  101. #endif
  102. int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout);
  103. #define poll ff_poll
  104. #endif /* HAVE_POLL_H */
  105. #endif /* CONFIG_NETWORK */
  106. #if defined(__MINGW32CE__)
  107. #define mkdir(a, b) _mkdir(a)
  108. #elif defined(_WIN32)
  109. #include <stdio.h>
  110. #include <windows.h>
  111. #include "libavutil/wchar_filename.h"
  112. #define DEF_FS_FUNCTION(name, wfunc, afunc) \
  113. static inline int win32_##name(const char *filename_utf8) \
  114. { \
  115. wchar_t *filename_w; \
  116. int ret; \
  117. \
  118. if (utf8towchar(filename_utf8, &filename_w)) \
  119. return -1; \
  120. if (!filename_w) \
  121. goto fallback; \
  122. \
  123. ret = wfunc(filename_w); \
  124. av_free(filename_w); \
  125. return ret; \
  126. \
  127. fallback: \
  128. /* filename may be be in CP_ACP */ \
  129. return afunc(filename_utf8); \
  130. }
  131. DEF_FS_FUNCTION(unlink, _wunlink, _unlink)
  132. DEF_FS_FUNCTION(mkdir, _wmkdir, _mkdir)
  133. DEF_FS_FUNCTION(rmdir, _wrmdir , _rmdir)
  134. static inline int win32_rename(const char *src_utf8, const char *dest_utf8)
  135. {
  136. wchar_t *src_w, *dest_w;
  137. int ret;
  138. if (utf8towchar(src_utf8, &src_w))
  139. return -1;
  140. if (utf8towchar(dest_utf8, &dest_w)) {
  141. av_free(src_w);
  142. return -1;
  143. }
  144. if (!src_w || !dest_w) {
  145. av_free(src_w);
  146. av_free(dest_w);
  147. goto fallback;
  148. }
  149. ret = MoveFileExW(src_w, dest_w, MOVEFILE_REPLACE_EXISTING);
  150. av_free(src_w);
  151. av_free(dest_w);
  152. // Lacking proper mapping from GetLastError() error codes to errno codes
  153. if (ret)
  154. errno = EPERM;
  155. return ret;
  156. fallback:
  157. /* filename may be be in CP_ACP */
  158. #if !HAVE_UWP
  159. ret = MoveFileExA(src_utf8, dest_utf8, MOVEFILE_REPLACE_EXISTING);
  160. if (ret)
  161. errno = EPERM;
  162. #else
  163. /* Windows Phone doesn't have MoveFileExA, and for Windows Store apps,
  164. * it is available but not allowed by the app certification kit. However,
  165. * it's unlikely that anybody would input filenames in CP_ACP there, so this
  166. * fallback is kept mostly for completeness. Alternatively we could
  167. * do MultiByteToWideChar(CP_ACP) and use MoveFileExW, but doing
  168. * explicit conversions with CP_ACP is allegedly forbidden in windows
  169. * store apps (or windows phone), and the notion of a native code page
  170. * doesn't make much sense there. */
  171. ret = rename(src_utf8, dest_utf8);
  172. #endif
  173. return ret;
  174. }
  175. #define mkdir(a, b) win32_mkdir(a)
  176. #define rename win32_rename
  177. #define rmdir win32_rmdir
  178. #define unlink win32_unlink
  179. #endif
  180. #endif /* AVFORMAT_OS_SUPPORT_H */