• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_INTERNAL_FILEUTILS_H
2 #define Py_INTERNAL_FILEUTILS_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #ifndef Py_BUILD_CORE
8 #  error "this header requires Py_BUILD_CORE define"
9 #endif
10 
11 #include <locale.h>               // struct lconv
12 
13 
14 /* A routine to check if a file descriptor can be select()-ed. */
15 #ifdef _MSC_VER
16     /* On Windows, any socket fd can be select()-ed, no matter how high */
17     #define _PyIsSelectable_fd(FD) (1)
18 #else
19     #define _PyIsSelectable_fd(FD) ((unsigned int)(FD) < (unsigned int)FD_SETSIZE)
20 #endif
21 
22 struct _fileutils_state {
23     int force_ascii;
24 };
25 
26 typedef enum {
27     _Py_ERROR_UNKNOWN=0,
28     _Py_ERROR_STRICT,
29     _Py_ERROR_SURROGATEESCAPE,
30     _Py_ERROR_REPLACE,
31     _Py_ERROR_IGNORE,
32     _Py_ERROR_BACKSLASHREPLACE,
33     _Py_ERROR_SURROGATEPASS,
34     _Py_ERROR_XMLCHARREFREPLACE,
35     _Py_ERROR_OTHER
36 } _Py_error_handler;
37 
38 // Export for '_testinternalcapi' shared extension
39 PyAPI_FUNC(_Py_error_handler) _Py_GetErrorHandler(const char *errors);
40 
41 // Export for '_testinternalcapi' shared extension
42 PyAPI_FUNC(int) _Py_DecodeLocaleEx(
43     const char *arg,
44     wchar_t **wstr,
45     size_t *wlen,
46     const char **reason,
47     int current_locale,
48     _Py_error_handler errors);
49 
50 // Export for '_testinternalcapi' shared extension
51 PyAPI_FUNC(int) _Py_EncodeLocaleEx(
52     const wchar_t *text,
53     char **str,
54     size_t *error_pos,
55     const char **reason,
56     int current_locale,
57     _Py_error_handler errors);
58 
59 extern char* _Py_EncodeLocaleRaw(
60     const wchar_t *text,
61     size_t *error_pos);
62 
63 extern PyObject* _Py_device_encoding(int);
64 
65 #if defined(MS_WINDOWS) || defined(__APPLE__)
66     /* On Windows, the count parameter of read() is an int (bpo-9015, bpo-9611).
67        On macOS 10.13, read() and write() with more than INT_MAX bytes
68        fail with EINVAL (bpo-24658). */
69 #   define _PY_READ_MAX  INT_MAX
70 #   define _PY_WRITE_MAX INT_MAX
71 #else
72     /* write() should truncate the input to PY_SSIZE_T_MAX bytes,
73        but it's safer to do it ourself to have a portable behaviour */
74 #   define _PY_READ_MAX  PY_SSIZE_T_MAX
75 #   define _PY_WRITE_MAX PY_SSIZE_T_MAX
76 #endif
77 
78 #ifdef MS_WINDOWS
79 struct _Py_stat_struct {
80     uint64_t st_dev;
81     uint64_t st_ino;
82     unsigned short st_mode;
83     int st_nlink;
84     int st_uid;
85     int st_gid;
86     unsigned long st_rdev;
87     __int64 st_size;
88     time_t st_atime;
89     int st_atime_nsec;
90     time_t st_mtime;
91     int st_mtime_nsec;
92     time_t st_ctime;
93     int st_ctime_nsec;
94     time_t st_birthtime;
95     int st_birthtime_nsec;
96     unsigned long st_file_attributes;
97     unsigned long st_reparse_tag;
98     uint64_t st_ino_high;
99 };
100 #else
101 #  define _Py_stat_struct stat
102 #endif
103 
104 // Export for 'mmap' shared extension
105 PyAPI_FUNC(int) _Py_fstat(
106     int fd,
107     struct _Py_stat_struct *status);
108 
109 // Export for 'mmap' shared extension
110 PyAPI_FUNC(int) _Py_fstat_noraise(
111     int fd,
112     struct _Py_stat_struct *status);
113 
114 // Export for '_tkinter' shared extension
115 PyAPI_FUNC(int) _Py_stat(
116     PyObject *path,
117     struct stat *status);
118 
119 // Export for 'select' shared extension (Solaris newDevPollObject())
120 PyAPI_FUNC(int) _Py_open(
121     const char *pathname,
122     int flags);
123 
124 // Export for '_posixsubprocess' shared extension
125 PyAPI_FUNC(int) _Py_open_noraise(
126     const char *pathname,
127     int flags);
128 
129 extern FILE* _Py_wfopen(
130     const wchar_t *path,
131     const wchar_t *mode);
132 
133 extern Py_ssize_t _Py_read(
134     int fd,
135     void *buf,
136     size_t count);
137 
138 // Export for 'select' shared extension (Solaris devpoll_flush())
139 PyAPI_FUNC(Py_ssize_t) _Py_write(
140     int fd,
141     const void *buf,
142     size_t count);
143 
144 // Export for '_posixsubprocess' shared extension
145 PyAPI_FUNC(Py_ssize_t) _Py_write_noraise(
146     int fd,
147     const void *buf,
148     size_t count);
149 
150 #ifdef HAVE_READLINK
151 extern int _Py_wreadlink(
152     const wchar_t *path,
153     wchar_t *buf,
154     /* Number of characters of 'buf' buffer
155        including the trailing NUL character */
156     size_t buflen);
157 #endif
158 
159 #ifdef HAVE_REALPATH
160 extern wchar_t* _Py_wrealpath(
161     const wchar_t *path,
162     wchar_t *resolved_path,
163     /* Number of characters of 'resolved_path' buffer
164        including the trailing NUL character */
165     size_t resolved_path_len);
166 #endif
167 
168 extern wchar_t* _Py_wgetcwd(
169     wchar_t *buf,
170     /* Number of characters of 'buf' buffer
171        including the trailing NUL character */
172     size_t buflen);
173 
174 extern int _Py_get_inheritable(int fd);
175 
176 // Export for '_socket' shared extension
177 PyAPI_FUNC(int) _Py_set_inheritable(int fd, int inheritable,
178                                     int *atomic_flag_works);
179 
180 // Export for '_posixsubprocess' shared extension
181 PyAPI_FUNC(int) _Py_set_inheritable_async_safe(int fd, int inheritable,
182                                                int *atomic_flag_works);
183 
184 // Export for '_socket' shared extension
185 PyAPI_FUNC(int) _Py_dup(int fd);
186 
187 extern int _Py_get_blocking(int fd);
188 
189 extern int _Py_set_blocking(int fd, int blocking);
190 
191 #ifdef MS_WINDOWS
192 extern void* _Py_get_osfhandle_noraise(int fd);
193 
194 // Export for '_testconsole' shared extension
195 PyAPI_FUNC(void*) _Py_get_osfhandle(int fd);
196 
197 extern int _Py_open_osfhandle_noraise(void *handle, int flags);
198 
199 extern int _Py_open_osfhandle(void *handle, int flags);
200 #endif  /* MS_WINDOWS */
201 
202 // This is used after getting NULL back from Py_DecodeLocale().
203 #define DECODE_LOCALE_ERR(NAME, LEN) \
204     ((LEN) == (size_t)-2) \
205      ? _PyStatus_ERR("cannot decode " NAME) \
206      : _PyStatus_NO_MEMORY()
207 
208 extern int _Py_HasFileSystemDefaultEncodeErrors;
209 
210 extern int _Py_DecodeUTF8Ex(
211     const char *arg,
212     Py_ssize_t arglen,
213     wchar_t **wstr,
214     size_t *wlen,
215     const char **reason,
216     _Py_error_handler errors);
217 
218 extern int _Py_EncodeUTF8Ex(
219     const wchar_t *text,
220     char **str,
221     size_t *error_pos,
222     const char **reason,
223     int raw_malloc,
224     _Py_error_handler errors);
225 
226 extern wchar_t* _Py_DecodeUTF8_surrogateescape(
227     const char *arg,
228     Py_ssize_t arglen,
229     size_t *wlen);
230 
231 extern int
232 _Py_wstat(const wchar_t *, struct stat *);
233 
234 extern int _Py_GetForceASCII(void);
235 
236 /* Reset "force ASCII" mode (if it was initialized).
237 
238    This function should be called when Python changes the LC_CTYPE locale,
239    so the "force ASCII" mode can be detected again on the new locale
240    encoding. */
241 extern void _Py_ResetForceASCII(void);
242 
243 
244 extern int _Py_GetLocaleconvNumeric(
245     struct lconv *lc,
246     PyObject **decimal_point,
247     PyObject **thousands_sep);
248 
249 // Export for '_posixsubprocess' (on macOS)
250 PyAPI_FUNC(void) _Py_closerange(int first, int last);
251 
252 extern wchar_t* _Py_GetLocaleEncoding(void);
253 extern PyObject* _Py_GetLocaleEncodingObject(void);
254 
255 #ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
256 extern int _Py_LocaleUsesNonUnicodeWchar(void);
257 
258 extern wchar_t* _Py_DecodeNonUnicodeWchar(
259     const wchar_t* native,
260     Py_ssize_t size);
261 
262 extern int _Py_EncodeNonUnicodeWchar_InPlace(
263     wchar_t* unicode,
264     Py_ssize_t size);
265 #endif
266 
267 extern int _Py_isabs(const wchar_t *path);
268 extern int _Py_abspath(const wchar_t *path, wchar_t **abspath_p);
269 #ifdef MS_WINDOWS
270 extern int _PyOS_getfullpathname(const wchar_t *path, wchar_t **abspath_p);
271 #endif
272 extern wchar_t* _Py_join_relfile(const wchar_t *dirname,
273                                  const wchar_t *relfile);
274 extern int _Py_add_relfile(wchar_t *dirname,
275                            const wchar_t *relfile,
276                            size_t bufsize);
277 extern size_t _Py_find_basename(const wchar_t *filename);
278 
279 // Export for '_testinternalcapi' shared extension
280 PyAPI_FUNC(wchar_t*) _Py_normpath(wchar_t *path, Py_ssize_t size);
281 
282 extern wchar_t *_Py_normpath_and_size(wchar_t *path, Py_ssize_t size, Py_ssize_t *length);
283 
284 // The Windows Games API family does not provide these functions
285 // so provide our own implementations. Remove them in case they get added
286 // to the Games API family
287 #if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP)
288 #include <winerror.h>             // HRESULT
289 
290 extern HRESULT PathCchSkipRoot(const wchar_t *pszPath, const wchar_t **ppszRootEnd);
291 #endif /* defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) */
292 
293 extern void _Py_skiproot(const wchar_t *path, Py_ssize_t size, Py_ssize_t *drvsize, Py_ssize_t *rootsize);
294 
295 // Macros to protect CRT calls against instant termination when passed an
296 // invalid parameter (bpo-23524). IPH stands for Invalid Parameter Handler.
297 // Usage:
298 //
299 //      _Py_BEGIN_SUPPRESS_IPH
300 //      ...
301 //      _Py_END_SUPPRESS_IPH
302 #if defined _MSC_VER && _MSC_VER >= 1900
303 
304 #  include <stdlib.h>   // _set_thread_local_invalid_parameter_handler()
305 
306    extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
307 #  define _Py_BEGIN_SUPPRESS_IPH \
308     { _invalid_parameter_handler _Py_old_handler = \
309       _set_thread_local_invalid_parameter_handler(_Py_silent_invalid_parameter_handler);
310 #  define _Py_END_SUPPRESS_IPH \
311     _set_thread_local_invalid_parameter_handler(_Py_old_handler); }
312 #else
313 #  define _Py_BEGIN_SUPPRESS_IPH
314 #  define _Py_END_SUPPRESS_IPH
315 #endif /* _MSC_VER >= 1900 */
316 
317 // Export for 'select' shared extension (Argument Clinic code)
318 PyAPI_FUNC(int) _PyLong_FileDescriptor_Converter(PyObject *, void *);
319 
320 // Export for test_peg_generator
321 PyAPI_FUNC(char*) _Py_UniversalNewlineFgetsWithSize(char *, int, FILE*, PyObject *, size_t*);
322 
323 extern int _PyFile_Flush(PyObject *);
324 
325 #ifndef MS_WINDOWS
326 extern int _Py_GetTicksPerSecond(long *ticks_per_second);
327 #endif
328 
329 // Export for '_testcapi' shared extension
330 PyAPI_FUNC(int) _Py_IsValidFD(int fd);
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 #endif /* !Py_INTERNAL_FILEUTILS_H */
336