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.

251 lines
7.2KB

  1. /*
  2. * various OS-feature replacement utilities
  3. * copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. # ifdef lseek
  40. # undef lseek
  41. # endif
  42. # define lseek(f,p,w) _lseeki64((f), (p), (w))
  43. # ifdef stat
  44. # undef stat
  45. # endif
  46. # define stat _stati64
  47. # ifdef fstat
  48. # undef fstat
  49. # endif
  50. # define fstat(f,s) _fstati64((f), (s))
  51. #endif /* defined(_WIN32) */
  52. #ifdef __ANDROID__
  53. # if HAVE_UNISTD_H
  54. # include <unistd.h>
  55. # endif
  56. # ifdef lseek
  57. # undef lseek
  58. # endif
  59. # define lseek(f,p,w) lseek64((f), (p), (w))
  60. #endif
  61. static inline int is_dos_path(const char *path)
  62. {
  63. #if HAVE_DOS_PATHS
  64. if (path[0] && path[1] == ':')
  65. return 1;
  66. #endif
  67. return 0;
  68. }
  69. #if defined(_WIN32)
  70. #ifndef S_IRUSR
  71. #define S_IRUSR S_IREAD
  72. #endif
  73. #ifndef S_IWUSR
  74. #define S_IWUSR S_IWRITE
  75. #endif
  76. #endif
  77. #if CONFIG_NETWORK
  78. #if defined(_WIN32)
  79. #define SHUT_RD SD_RECEIVE
  80. #define SHUT_WR SD_SEND
  81. #define SHUT_RDWR SD_BOTH
  82. #else
  83. #include <sys/socket.h>
  84. #if !defined(SHUT_RD) /* OS/2, DJGPP */
  85. #define SHUT_RD 0
  86. #define SHUT_WR 1
  87. #define SHUT_RDWR 2
  88. #endif
  89. #endif
  90. #if !HAVE_SOCKLEN_T
  91. typedef int socklen_t;
  92. #endif
  93. /* most of the time closing a socket is just closing an fd */
  94. #if !HAVE_CLOSESOCKET
  95. #define closesocket close
  96. #endif
  97. #if !HAVE_POLL_H
  98. typedef unsigned long nfds_t;
  99. #if HAVE_WINSOCK2_H
  100. #include <winsock2.h>
  101. #endif
  102. #if !HAVE_STRUCT_POLLFD
  103. struct pollfd {
  104. int fd;
  105. short events; /* events to look for */
  106. short revents; /* events that occurred */
  107. };
  108. /* events & revents */
  109. #define POLLIN 0x0001 /* any readable data available */
  110. #define POLLOUT 0x0002 /* file descriptor is writeable */
  111. #define POLLRDNORM POLLIN
  112. #define POLLWRNORM POLLOUT
  113. #define POLLRDBAND 0x0008 /* priority readable data */
  114. #define POLLWRBAND 0x0010 /* priority data can be written */
  115. #define POLLPRI 0x0020 /* high priority readable data */
  116. /* revents only */
  117. #define POLLERR 0x0004 /* errors pending */
  118. #define POLLHUP 0x0080 /* disconnected */
  119. #define POLLNVAL 0x1000 /* invalid file descriptor */
  120. #endif
  121. int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout);
  122. #define poll ff_poll
  123. #endif /* HAVE_POLL_H */
  124. #endif /* CONFIG_NETWORK */
  125. #ifdef _WIN32
  126. #include <stdio.h>
  127. #include <windows.h>
  128. #include "libavutil/wchar_filename.h"
  129. #define DEF_FS_FUNCTION(name, wfunc, afunc) \
  130. static inline int win32_##name(const char *filename_utf8) \
  131. { \
  132. wchar_t *filename_w; \
  133. int ret; \
  134. \
  135. if (utf8towchar(filename_utf8, &filename_w)) \
  136. return -1; \
  137. if (!filename_w) \
  138. goto fallback; \
  139. \
  140. ret = wfunc(filename_w); \
  141. av_free(filename_w); \
  142. return ret; \
  143. \
  144. fallback: \
  145. /* filename may be be in CP_ACP */ \
  146. return afunc(filename_utf8); \
  147. }
  148. DEF_FS_FUNCTION(unlink, _wunlink, _unlink)
  149. DEF_FS_FUNCTION(mkdir, _wmkdir, _mkdir)
  150. DEF_FS_FUNCTION(rmdir, _wrmdir , _rmdir)
  151. #define DEF_FS_FUNCTION2(name, wfunc, afunc, partype) \
  152. static inline int win32_##name(const char *filename_utf8, partype par) \
  153. { \
  154. wchar_t *filename_w; \
  155. int ret; \
  156. \
  157. if (utf8towchar(filename_utf8, &filename_w)) \
  158. return -1; \
  159. if (!filename_w) \
  160. goto fallback; \
  161. \
  162. ret = wfunc(filename_w, par); \
  163. av_free(filename_w); \
  164. return ret; \
  165. \
  166. fallback: \
  167. /* filename may be be in CP_ACP */ \
  168. return afunc(filename_utf8, par); \
  169. }
  170. DEF_FS_FUNCTION2(access, _waccess, _access, int)
  171. DEF_FS_FUNCTION2(stat, _wstati64, _stati64, struct stat*)
  172. static inline int win32_rename(const char *src_utf8, const char *dest_utf8)
  173. {
  174. wchar_t *src_w, *dest_w;
  175. int ret;
  176. if (utf8towchar(src_utf8, &src_w))
  177. return -1;
  178. if (utf8towchar(dest_utf8, &dest_w)) {
  179. av_free(src_w);
  180. return -1;
  181. }
  182. if (!src_w || !dest_w) {
  183. av_free(src_w);
  184. av_free(dest_w);
  185. goto fallback;
  186. }
  187. ret = MoveFileExW(src_w, dest_w, MOVEFILE_REPLACE_EXISTING);
  188. av_free(src_w);
  189. av_free(dest_w);
  190. // Lacking proper mapping from GetLastError() error codes to errno codes
  191. if (ret)
  192. errno = EPERM;
  193. return ret;
  194. fallback:
  195. /* filename may be be in CP_ACP */
  196. #if !HAVE_UWP
  197. ret = MoveFileExA(src_utf8, dest_utf8, MOVEFILE_REPLACE_EXISTING);
  198. if (ret)
  199. errno = EPERM;
  200. #else
  201. /* Windows Phone doesn't have MoveFileExA, and for Windows Store apps,
  202. * it is available but not allowed by the app certification kit. However,
  203. * it's unlikely that anybody would input filenames in CP_ACP there, so this
  204. * fallback is kept mostly for completeness. Alternatively we could
  205. * do MultiByteToWideChar(CP_ACP) and use MoveFileExW, but doing
  206. * explicit conversions with CP_ACP is allegedly forbidden in windows
  207. * store apps (or windows phone), and the notion of a native code page
  208. * doesn't make much sense there. */
  209. ret = rename(src_utf8, dest_utf8);
  210. #endif
  211. return ret;
  212. }
  213. #define mkdir(a, b) win32_mkdir(a)
  214. #define rename win32_rename
  215. #define rmdir win32_rmdir
  216. #define unlink win32_unlink
  217. #define access win32_access
  218. #endif
  219. #endif /* AVFORMAT_OS_SUPPORT_H */