os_support.h 6.1 KB

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