pycore_fileutils.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #ifndef Py_INTERNAL_FILEUTILS_H
  2. #define Py_INTERNAL_FILEUTILS_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "Py_BUILD_CORE must be defined to include this header"
  8. #endif
  9. #include <locale.h> /* struct lconv */
  10. struct _fileutils_state {
  11. int force_ascii;
  12. };
  13. typedef enum {
  14. _Py_ERROR_UNKNOWN=0,
  15. _Py_ERROR_STRICT,
  16. _Py_ERROR_SURROGATEESCAPE,
  17. _Py_ERROR_REPLACE,
  18. _Py_ERROR_IGNORE,
  19. _Py_ERROR_BACKSLASHREPLACE,
  20. _Py_ERROR_SURROGATEPASS,
  21. _Py_ERROR_XMLCHARREFREPLACE,
  22. _Py_ERROR_OTHER
  23. } _Py_error_handler;
  24. PyAPI_FUNC(_Py_error_handler) _Py_GetErrorHandler(const char *errors);
  25. PyAPI_FUNC(int) _Py_DecodeLocaleEx(
  26. const char *arg,
  27. wchar_t **wstr,
  28. size_t *wlen,
  29. const char **reason,
  30. int current_locale,
  31. _Py_error_handler errors);
  32. PyAPI_FUNC(int) _Py_EncodeLocaleEx(
  33. const wchar_t *text,
  34. char **str,
  35. size_t *error_pos,
  36. const char **reason,
  37. int current_locale,
  38. _Py_error_handler errors);
  39. PyAPI_FUNC(char*) _Py_EncodeLocaleRaw(
  40. const wchar_t *text,
  41. size_t *error_pos);
  42. PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
  43. #if defined(MS_WINDOWS) || defined(__APPLE__)
  44. /* On Windows, the count parameter of read() is an int (bpo-9015, bpo-9611).
  45. On macOS 10.13, read() and write() with more than INT_MAX bytes
  46. fail with EINVAL (bpo-24658). */
  47. # define _PY_READ_MAX INT_MAX
  48. # define _PY_WRITE_MAX INT_MAX
  49. #else
  50. /* write() should truncate the input to PY_SSIZE_T_MAX bytes,
  51. but it's safer to do it ourself to have a portable behaviour */
  52. # define _PY_READ_MAX PY_SSIZE_T_MAX
  53. # define _PY_WRITE_MAX PY_SSIZE_T_MAX
  54. #endif
  55. #ifdef MS_WINDOWS
  56. struct _Py_stat_struct {
  57. uint64_t st_dev;
  58. uint64_t st_ino;
  59. unsigned short st_mode;
  60. int st_nlink;
  61. int st_uid;
  62. int st_gid;
  63. unsigned long st_rdev;
  64. __int64 st_size;
  65. time_t st_atime;
  66. int st_atime_nsec;
  67. time_t st_mtime;
  68. int st_mtime_nsec;
  69. time_t st_ctime;
  70. int st_ctime_nsec;
  71. time_t st_birthtime;
  72. int st_birthtime_nsec;
  73. unsigned long st_file_attributes;
  74. unsigned long st_reparse_tag;
  75. uint64_t st_ino_high;
  76. };
  77. #else
  78. # define _Py_stat_struct stat
  79. #endif
  80. PyAPI_FUNC(int) _Py_fstat(
  81. int fd,
  82. struct _Py_stat_struct *status);
  83. PyAPI_FUNC(int) _Py_fstat_noraise(
  84. int fd,
  85. struct _Py_stat_struct *status);
  86. PyAPI_FUNC(int) _Py_stat(
  87. PyObject *path,
  88. struct stat *status);
  89. PyAPI_FUNC(int) _Py_open(
  90. const char *pathname,
  91. int flags);
  92. PyAPI_FUNC(int) _Py_open_noraise(
  93. const char *pathname,
  94. int flags);
  95. PyAPI_FUNC(FILE *) _Py_wfopen(
  96. const wchar_t *path,
  97. const wchar_t *mode);
  98. PyAPI_FUNC(Py_ssize_t) _Py_read(
  99. int fd,
  100. void *buf,
  101. size_t count);
  102. PyAPI_FUNC(Py_ssize_t) _Py_write(
  103. int fd,
  104. const void *buf,
  105. size_t count);
  106. PyAPI_FUNC(Py_ssize_t) _Py_write_noraise(
  107. int fd,
  108. const void *buf,
  109. size_t count);
  110. #ifdef HAVE_READLINK
  111. PyAPI_FUNC(int) _Py_wreadlink(
  112. const wchar_t *path,
  113. wchar_t *buf,
  114. /* Number of characters of 'buf' buffer
  115. including the trailing NUL character */
  116. size_t buflen);
  117. #endif
  118. #ifdef HAVE_REALPATH
  119. PyAPI_FUNC(wchar_t*) _Py_wrealpath(
  120. const wchar_t *path,
  121. wchar_t *resolved_path,
  122. /* Number of characters of 'resolved_path' buffer
  123. including the trailing NUL character */
  124. size_t resolved_path_len);
  125. #endif
  126. PyAPI_FUNC(wchar_t*) _Py_wgetcwd(
  127. wchar_t *buf,
  128. /* Number of characters of 'buf' buffer
  129. including the trailing NUL character */
  130. size_t buflen);
  131. PyAPI_FUNC(int) _Py_get_inheritable(int fd);
  132. PyAPI_FUNC(int) _Py_set_inheritable(int fd, int inheritable,
  133. int *atomic_flag_works);
  134. PyAPI_FUNC(int) _Py_set_inheritable_async_safe(int fd, int inheritable,
  135. int *atomic_flag_works);
  136. PyAPI_FUNC(int) _Py_dup(int fd);
  137. PyAPI_FUNC(int) _Py_get_blocking(int fd);
  138. PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
  139. #ifdef MS_WINDOWS
  140. PyAPI_FUNC(void*) _Py_get_osfhandle_noraise(int fd);
  141. PyAPI_FUNC(void*) _Py_get_osfhandle(int fd);
  142. PyAPI_FUNC(int) _Py_open_osfhandle_noraise(void *handle, int flags);
  143. PyAPI_FUNC(int) _Py_open_osfhandle(void *handle, int flags);
  144. #endif /* MS_WINDOWS */
  145. // This is used after getting NULL back from Py_DecodeLocale().
  146. #define DECODE_LOCALE_ERR(NAME, LEN) \
  147. ((LEN) == (size_t)-2) \
  148. ? _PyStatus_ERR("cannot decode " NAME) \
  149. : _PyStatus_NO_MEMORY()
  150. PyAPI_DATA(int) _Py_HasFileSystemDefaultEncodeErrors;
  151. PyAPI_FUNC(int) _Py_DecodeUTF8Ex(
  152. const char *arg,
  153. Py_ssize_t arglen,
  154. wchar_t **wstr,
  155. size_t *wlen,
  156. const char **reason,
  157. _Py_error_handler errors);
  158. PyAPI_FUNC(int) _Py_EncodeUTF8Ex(
  159. const wchar_t *text,
  160. char **str,
  161. size_t *error_pos,
  162. const char **reason,
  163. int raw_malloc,
  164. _Py_error_handler errors);
  165. PyAPI_FUNC(wchar_t*) _Py_DecodeUTF8_surrogateescape(
  166. const char *arg,
  167. Py_ssize_t arglen,
  168. size_t *wlen);
  169. extern int
  170. _Py_wstat(const wchar_t *, struct stat *);
  171. PyAPI_FUNC(int) _Py_GetForceASCII(void);
  172. /* Reset "force ASCII" mode (if it was initialized).
  173. This function should be called when Python changes the LC_CTYPE locale,
  174. so the "force ASCII" mode can be detected again on the new locale
  175. encoding. */
  176. PyAPI_FUNC(void) _Py_ResetForceASCII(void);
  177. PyAPI_FUNC(int) _Py_GetLocaleconvNumeric(
  178. struct lconv *lc,
  179. PyObject **decimal_point,
  180. PyObject **thousands_sep);
  181. PyAPI_FUNC(void) _Py_closerange(int first, int last);
  182. PyAPI_FUNC(wchar_t*) _Py_GetLocaleEncoding(void);
  183. PyAPI_FUNC(PyObject*) _Py_GetLocaleEncodingObject(void);
  184. #ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
  185. extern int _Py_LocaleUsesNonUnicodeWchar(void);
  186. extern wchar_t* _Py_DecodeNonUnicodeWchar(
  187. const wchar_t* native,
  188. Py_ssize_t size);
  189. extern int _Py_EncodeNonUnicodeWchar_InPlace(
  190. wchar_t* unicode,
  191. Py_ssize_t size);
  192. #endif
  193. extern int _Py_isabs(const wchar_t *path);
  194. extern int _Py_abspath(const wchar_t *path, wchar_t **abspath_p);
  195. #ifdef MS_WINDOWS
  196. extern int _PyOS_getfullpathname(const wchar_t *path, wchar_t **abspath_p);
  197. #endif
  198. extern wchar_t * _Py_join_relfile(const wchar_t *dirname,
  199. const wchar_t *relfile);
  200. extern int _Py_add_relfile(wchar_t *dirname,
  201. const wchar_t *relfile,
  202. size_t bufsize);
  203. extern size_t _Py_find_basename(const wchar_t *filename);
  204. PyAPI_FUNC(wchar_t*) _Py_normpath(wchar_t *path, Py_ssize_t size);
  205. extern wchar_t *_Py_normpath_and_size(wchar_t *path, Py_ssize_t size, Py_ssize_t *length);
  206. // The Windows Games API family does not provide these functions
  207. // so provide our own implementations. Remove them in case they get added
  208. // to the Games API family
  209. #if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP)
  210. #include <winerror.h>
  211. extern HRESULT PathCchSkipRoot(const wchar_t *pszPath, const wchar_t **ppszRootEnd);
  212. #endif /* defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) */
  213. // Macros to protect CRT calls against instant termination when passed an
  214. // invalid parameter (bpo-23524). IPH stands for Invalid Parameter Handler.
  215. // Usage:
  216. //
  217. // _Py_BEGIN_SUPPRESS_IPH
  218. // ...
  219. // _Py_END_SUPPRESS_IPH
  220. #if defined _MSC_VER && _MSC_VER >= 1900
  221. # include <stdlib.h> // _set_thread_local_invalid_parameter_handler()
  222. extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
  223. # define _Py_BEGIN_SUPPRESS_IPH \
  224. { _invalid_parameter_handler _Py_old_handler = \
  225. _set_thread_local_invalid_parameter_handler(_Py_silent_invalid_parameter_handler);
  226. # define _Py_END_SUPPRESS_IPH \
  227. _set_thread_local_invalid_parameter_handler(_Py_old_handler); }
  228. #else
  229. # define _Py_BEGIN_SUPPRESS_IPH
  230. # define _Py_END_SUPPRESS_IPH
  231. #endif /* _MSC_VER >= 1900 */
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235. #endif /* !Py_INTERNAL_FILEUTILS_H */