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.

207 lines
5.7KB

  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. #ifdef _WIN32
  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) */
  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. #ifdef _WIN32
  107. #include <stdio.h>
  108. #include <windows.h>
  109. #include "libavutil/wchar_filename.h"
  110. #define DEF_FS_FUNCTION(name, wfunc, afunc) \
  111. static inline int win32_##name(const char *filename_utf8) \
  112. { \
  113. wchar_t *filename_w; \
  114. int ret; \
  115. \
  116. if (utf8towchar(filename_utf8, &filename_w)) \
  117. return -1; \
  118. if (!filename_w) \
  119. goto fallback; \
  120. \
  121. ret = wfunc(filename_w); \
  122. av_free(filename_w); \
  123. return ret; \
  124. \
  125. fallback: \
  126. /* filename may be be in CP_ACP */ \
  127. return afunc(filename_utf8); \
  128. }
  129. DEF_FS_FUNCTION(unlink, _wunlink, _unlink)
  130. DEF_FS_FUNCTION(mkdir, _wmkdir, _mkdir)
  131. DEF_FS_FUNCTION(rmdir, _wrmdir , _rmdir)
  132. static inline int win32_rename(const char *src_utf8, const char *dest_utf8)
  133. {
  134. wchar_t *src_w, *dest_w;
  135. int ret;
  136. if (utf8towchar(src_utf8, &src_w))
  137. return -1;
  138. if (utf8towchar(dest_utf8, &dest_w)) {
  139. av_free(src_w);
  140. return -1;
  141. }
  142. if (!src_w || !dest_w) {
  143. av_free(src_w);
  144. av_free(dest_w);
  145. goto fallback;
  146. }
  147. ret = MoveFileExW(src_w, dest_w, MOVEFILE_REPLACE_EXISTING);
  148. av_free(src_w);
  149. av_free(dest_w);
  150. // Lacking proper mapping from GetLastError() error codes to errno codes
  151. if (ret)
  152. errno = EPERM;
  153. return ret;
  154. fallback:
  155. /* filename may be be in CP_ACP */
  156. #if !HAVE_UWP
  157. ret = MoveFileExA(src_utf8, dest_utf8, MOVEFILE_REPLACE_EXISTING);
  158. if (ret)
  159. errno = EPERM;
  160. #else
  161. /* Windows Phone doesn't have MoveFileExA, and for Windows Store apps,
  162. * it is available but not allowed by the app certification kit. However,
  163. * it's unlikely that anybody would input filenames in CP_ACP there, so this
  164. * fallback is kept mostly for completeness. Alternatively we could
  165. * do MultiByteToWideChar(CP_ACP) and use MoveFileExW, but doing
  166. * explicit conversions with CP_ACP is allegedly forbidden in windows
  167. * store apps (or windows phone), and the notion of a native code page
  168. * doesn't make much sense there. */
  169. ret = rename(src_utf8, dest_utf8);
  170. #endif
  171. return ret;
  172. }
  173. #define mkdir(a, b) win32_mkdir(a)
  174. #define rename win32_rename
  175. #define rmdir win32_rmdir
  176. #define unlink win32_unlink
  177. #endif
  178. #endif /* AVFORMAT_OS_SUPPORT_H */