Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Stefan Krah | 6df5cae | 2012-11-12 20:14:36 +0100 | [diff] [blame] | 2 | #include "osdefs.h" |
Stefan Krah | 6c01e38 | 2014-01-20 15:31:08 +0100 | [diff] [blame] | 3 | #include <locale.h> |
| 4 | |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 5 | #ifdef MS_WINDOWS |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 6 | # include <malloc.h> |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 7 | # include <windows.h> |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8 | extern int winerror_to_errno(int); |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 9 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 10 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11 | #ifdef HAVE_LANGINFO_H |
| 12 | #include <langinfo.h> |
| 13 | #endif |
| 14 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 15 | #ifdef HAVE_SYS_IOCTL_H |
| 16 | #include <sys/ioctl.h> |
| 17 | #endif |
| 18 | |
| 19 | #ifdef HAVE_FCNTL_H |
| 20 | #include <fcntl.h> |
| 21 | #endif /* HAVE_FCNTL_H */ |
| 22 | |
Xavier de Gaye | 76febd0 | 2016-12-15 20:59:58 +0100 | [diff] [blame] | 23 | #if defined(__APPLE__) || defined(__ANDROID__) |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 24 | extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size); |
| 25 | #endif |
| 26 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 27 | #ifdef O_CLOEXEC |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 28 | /* Does open() support the O_CLOEXEC flag? Possible values: |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 29 | |
| 30 | -1: unknown |
| 31 | 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23 |
| 32 | 1: open() supports O_CLOEXEC flag, close-on-exec is set |
| 33 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 34 | The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO |
| 35 | and os.open(). */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 36 | int _Py_open_cloexec_works = -1; |
| 37 | #endif |
| 38 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 39 | PyObject * |
| 40 | _Py_device_encoding(int fd) |
| 41 | { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 42 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 43 | UINT cp; |
| 44 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 45 | int valid; |
| 46 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 47 | valid = isatty(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 48 | _Py_END_SUPPRESS_IPH |
| 49 | if (!valid) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 50 | Py_RETURN_NONE; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 51 | |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 52 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 53 | if (fd == 0) |
| 54 | cp = GetConsoleCP(); |
| 55 | else if (fd == 1 || fd == 2) |
| 56 | cp = GetConsoleOutputCP(); |
| 57 | else |
| 58 | cp = 0; |
| 59 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 60 | has no console */ |
| 61 | if (cp != 0) |
| 62 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
| 63 | #elif defined(CODESET) |
| 64 | { |
| 65 | char *codeset = nl_langinfo(CODESET); |
| 66 | if (codeset != NULL && codeset[0] != 0) |
| 67 | return PyUnicode_FromString(codeset); |
| 68 | } |
| 69 | #endif |
| 70 | Py_RETURN_NONE; |
| 71 | } |
| 72 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 73 | #if !defined(__APPLE__) && !defined(MS_WINDOWS) |
| 74 | extern int _Py_normalize_encoding(const char *, char *, size_t); |
| 75 | |
| 76 | /* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale. |
| 77 | On these operating systems, nl_langinfo(CODESET) announces an alias of the |
| 78 | ASCII encoding, whereas mbstowcs() and wcstombs() functions use the |
| 79 | ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use |
| 80 | locale.getpreferredencoding() codec. For example, if command line arguments |
| 81 | are decoded by mbstowcs() and encoded back by os.fsencode(), we get a |
| 82 | UnicodeEncodeError instead of retrieving the original byte string. |
| 83 | |
| 84 | The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C", |
| 85 | nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least |
| 86 | one byte in range 0x80-0xff can be decoded from the locale encoding. The |
| 87 | workaround is also enabled on error, for example if getting the locale |
| 88 | failed. |
| 89 | |
Philip Jenvey | 215c49a | 2013-01-15 13:24:12 -0800 | [diff] [blame] | 90 | Values of force_ascii: |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 91 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 92 | 1: the workaround is used: Py_EncodeLocale() uses |
| 93 | encode_ascii_surrogateescape() and Py_DecodeLocale() uses |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 94 | decode_ascii_surrogateescape() |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 95 | 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and |
| 96 | Py_DecodeLocale() uses mbstowcs() |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 97 | -1: unknown, need to call check_force_ascii() to get the value |
| 98 | */ |
| 99 | static int force_ascii = -1; |
| 100 | |
| 101 | static int |
| 102 | check_force_ascii(void) |
| 103 | { |
| 104 | char *loc; |
| 105 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 106 | char *codeset, **alias; |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 107 | char encoding[20]; /* longest name: "iso_646.irv_1991\0" */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 108 | int is_ascii; |
| 109 | unsigned int i; |
| 110 | char* ascii_aliases[] = { |
| 111 | "ascii", |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 112 | /* Aliases from Lib/encodings/aliases.py */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 113 | "646", |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 114 | "ansi_x3.4_1968", |
| 115 | "ansi_x3.4_1986", |
| 116 | "ansi_x3_4_1968", |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 117 | "cp367", |
| 118 | "csascii", |
| 119 | "ibm367", |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 120 | "iso646_us", |
| 121 | "iso_646.irv_1991", |
| 122 | "iso_ir_6", |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 123 | "us", |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 124 | "us_ascii", |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 125 | NULL |
| 126 | }; |
| 127 | #endif |
| 128 | |
| 129 | loc = setlocale(LC_CTYPE, NULL); |
| 130 | if (loc == NULL) |
| 131 | goto error; |
| 132 | if (strcmp(loc, "C") != 0) { |
| 133 | /* the LC_CTYPE locale is different than C */ |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 138 | codeset = nl_langinfo(CODESET); |
| 139 | if (!codeset || codeset[0] == '\0') { |
| 140 | /* CODESET is not set or empty */ |
| 141 | goto error; |
| 142 | } |
| 143 | if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) |
| 144 | goto error; |
| 145 | |
| 146 | is_ascii = 0; |
| 147 | for (alias=ascii_aliases; *alias != NULL; alias++) { |
| 148 | if (strcmp(encoding, *alias) == 0) { |
| 149 | is_ascii = 1; |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | if (!is_ascii) { |
| 154 | /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */ |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | for (i=0x80; i<0xff; i++) { |
| 159 | unsigned char ch; |
| 160 | wchar_t wch; |
| 161 | size_t res; |
| 162 | |
| 163 | ch = (unsigned char)i; |
| 164 | res = mbstowcs(&wch, (char*)&ch, 1); |
| 165 | if (res != (size_t)-1) { |
| 166 | /* decoding a non-ASCII character from the locale encoding succeed: |
| 167 | the locale encoding is not ASCII, force ASCII */ |
| 168 | return 1; |
| 169 | } |
| 170 | } |
| 171 | /* None of the bytes in the range 0x80-0xff can be decoded from the locale |
| 172 | encoding: the locale encoding is really ASCII */ |
| 173 | return 0; |
| 174 | #else |
| 175 | /* nl_langinfo(CODESET) is not available: always force ASCII */ |
| 176 | return 1; |
| 177 | #endif |
| 178 | |
| 179 | error: |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 180 | /* if an error occurred, force the ASCII encoding */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | static char* |
| 185 | encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos) |
| 186 | { |
| 187 | char *result = NULL, *out; |
| 188 | size_t len, i; |
| 189 | wchar_t ch; |
| 190 | |
| 191 | if (error_pos != NULL) |
| 192 | *error_pos = (size_t)-1; |
| 193 | |
| 194 | len = wcslen(text); |
| 195 | |
| 196 | result = PyMem_Malloc(len + 1); /* +1 for NUL byte */ |
| 197 | if (result == NULL) |
| 198 | return NULL; |
| 199 | |
| 200 | out = result; |
| 201 | for (i=0; i<len; i++) { |
| 202 | ch = text[i]; |
| 203 | |
| 204 | if (ch <= 0x7f) { |
| 205 | /* ASCII character */ |
| 206 | *out++ = (char)ch; |
| 207 | } |
| 208 | else if (0xdc80 <= ch && ch <= 0xdcff) { |
| 209 | /* UTF-8b surrogate */ |
| 210 | *out++ = (char)(ch - 0xdc00); |
| 211 | } |
| 212 | else { |
| 213 | if (error_pos != NULL) |
| 214 | *error_pos = i; |
| 215 | PyMem_Free(result); |
| 216 | return NULL; |
| 217 | } |
| 218 | } |
| 219 | *out = '\0'; |
| 220 | return result; |
| 221 | } |
| 222 | #endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */ |
| 223 | |
| 224 | #if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC)) |
| 225 | static wchar_t* |
| 226 | decode_ascii_surrogateescape(const char *arg, size_t *size) |
| 227 | { |
| 228 | wchar_t *res; |
| 229 | unsigned char *in; |
| 230 | wchar_t *out; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 231 | size_t argsize = strlen(arg) + 1; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 232 | |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 233 | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
| 234 | return NULL; |
Benjamin Peterson | 10ecaa2 | 2015-01-04 16:05:39 -0600 | [diff] [blame] | 235 | res = PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 236 | if (!res) |
| 237 | return NULL; |
| 238 | |
| 239 | in = (unsigned char*)arg; |
| 240 | out = res; |
| 241 | while(*in) |
| 242 | if(*in < 128) |
| 243 | *out++ = *in++; |
| 244 | else |
| 245 | *out++ = 0xdc00 + *in++; |
| 246 | *out = 0; |
| 247 | if (size != NULL) |
| 248 | *size = out - res; |
| 249 | return res; |
| 250 | } |
| 251 | #endif |
| 252 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 253 | |
| 254 | /* Decode a byte string from the locale encoding with the |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 255 | surrogateescape error handler: undecodable bytes are decoded as characters |
| 256 | in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 257 | character, escape the bytes using the surrogateescape error handler instead |
| 258 | of decoding them. |
| 259 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 260 | Return a pointer to a newly allocated wide character string, use |
| 261 | PyMem_RawFree() to free the memory. If size is not NULL, write the number of |
| 262 | wide characters excluding the null character into *size |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 263 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 264 | Return NULL on decoding error or memory allocation error. If *size* is not |
| 265 | NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on |
| 266 | decoding error. |
Victor Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 267 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 268 | Decoding errors should never happen, unless there is a bug in the C |
| 269 | library. |
| 270 | |
| 271 | Use the Py_EncodeLocale() function to encode the character string back to a |
| 272 | byte string. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 273 | wchar_t* |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 274 | Py_DecodeLocale(const char* arg, size_t *size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 275 | { |
Xavier de Gaye | 76febd0 | 2016-12-15 20:59:58 +0100 | [diff] [blame] | 276 | #if defined(__APPLE__) || defined(__ANDROID__) |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 277 | wchar_t *wstr; |
| 278 | wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg)); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 279 | if (size != NULL) { |
| 280 | if (wstr != NULL) |
| 281 | *size = wcslen(wstr); |
| 282 | else |
| 283 | *size = (size_t)-1; |
| 284 | } |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 285 | return wstr; |
| 286 | #else |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 287 | wchar_t *res; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 288 | size_t argsize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 289 | size_t count; |
Victor Stinner | 313f10c | 2013-05-07 23:48:56 +0200 | [diff] [blame] | 290 | #ifdef HAVE_MBRTOWC |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 291 | unsigned char *in; |
| 292 | wchar_t *out; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 293 | mbstate_t mbs; |
| 294 | #endif |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 295 | |
| 296 | #ifndef MS_WINDOWS |
| 297 | if (force_ascii == -1) |
| 298 | force_ascii = check_force_ascii(); |
| 299 | |
| 300 | if (force_ascii) { |
| 301 | /* force ASCII encoding to workaround mbstowcs() issue */ |
| 302 | res = decode_ascii_surrogateescape(arg, size); |
| 303 | if (res == NULL) |
| 304 | goto oom; |
| 305 | return res; |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 310 | /* Some platforms have a broken implementation of |
| 311 | * mbstowcs which does not count the characters that |
| 312 | * would result from conversion. Use an upper bound. |
| 313 | */ |
| 314 | argsize = strlen(arg); |
| 315 | #else |
| 316 | argsize = mbstowcs(NULL, arg, 0); |
| 317 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 318 | if (argsize != (size_t)-1) { |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 319 | if (argsize == PY_SSIZE_T_MAX) |
| 320 | goto oom; |
| 321 | argsize += 1; |
| 322 | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
| 323 | goto oom; |
Benjamin Peterson | 10ecaa2 | 2015-01-04 16:05:39 -0600 | [diff] [blame] | 324 | res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 325 | if (!res) |
| 326 | goto oom; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 327 | count = mbstowcs(res, arg, argsize); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 328 | if (count != (size_t)-1) { |
| 329 | wchar_t *tmp; |
| 330 | /* Only use the result if it contains no |
| 331 | surrogate characters. */ |
| 332 | for (tmp = res; *tmp != 0 && |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 333 | !Py_UNICODE_IS_SURROGATE(*tmp); tmp++) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 334 | ; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 335 | if (*tmp == 0) { |
| 336 | if (size != NULL) |
| 337 | *size = count; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 338 | return res; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 339 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 340 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 341 | PyMem_RawFree(res); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 342 | } |
| 343 | /* Conversion failed. Fall back to escaping with surrogateescape. */ |
| 344 | #ifdef HAVE_MBRTOWC |
| 345 | /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ |
| 346 | |
| 347 | /* Overallocate; as multi-byte characters are in the argument, the |
| 348 | actual output could use less memory. */ |
| 349 | argsize = strlen(arg) + 1; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 350 | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
| 351 | goto oom; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 352 | res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 353 | if (!res) |
| 354 | goto oom; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 355 | in = (unsigned char*)arg; |
| 356 | out = res; |
| 357 | memset(&mbs, 0, sizeof mbs); |
| 358 | while (argsize) { |
| 359 | size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); |
| 360 | if (converted == 0) |
| 361 | /* Reached end of string; null char stored. */ |
| 362 | break; |
| 363 | if (converted == (size_t)-2) { |
| 364 | /* Incomplete character. This should never happen, |
| 365 | since we provide everything that we have - |
| 366 | unless there is a bug in the C library, or I |
| 367 | misunderstood how mbrtowc works. */ |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 368 | PyMem_RawFree(res); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 369 | if (size != NULL) |
| 370 | *size = (size_t)-2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
| 373 | if (converted == (size_t)-1) { |
| 374 | /* Conversion error. Escape as UTF-8b, and start over |
| 375 | in the initial shift state. */ |
| 376 | *out++ = 0xdc00 + *in++; |
| 377 | argsize--; |
| 378 | memset(&mbs, 0, sizeof mbs); |
| 379 | continue; |
| 380 | } |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 381 | if (Py_UNICODE_IS_SURROGATE(*out)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 382 | /* Surrogate character. Escape the original |
| 383 | byte sequence with surrogateescape. */ |
| 384 | argsize -= converted; |
| 385 | while (converted--) |
| 386 | *out++ = 0xdc00 + *in++; |
| 387 | continue; |
| 388 | } |
| 389 | /* successfully converted some bytes */ |
| 390 | in += converted; |
| 391 | argsize -= converted; |
| 392 | out++; |
| 393 | } |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 394 | if (size != NULL) |
| 395 | *size = out - res; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 396 | #else /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 397 | /* Cannot use C locale for escaping; manually escape as if charset |
| 398 | is ASCII (i.e. escape all bytes > 128. This will still roundtrip |
| 399 | correctly in the locale's charset, which must be an ASCII superset. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 400 | res = decode_ascii_surrogateescape(arg, size); |
| 401 | if (res == NULL) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 402 | goto oom; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 403 | #endif /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 404 | return res; |
| 405 | oom: |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 406 | if (size != NULL) |
| 407 | *size = (size_t)-1; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 408 | return NULL; |
Xavier de Gaye | 76febd0 | 2016-12-15 20:59:58 +0100 | [diff] [blame] | 409 | #endif /* __APPLE__ or __ANDROID__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 412 | /* Encode a wide character string to the locale encoding with the |
| 413 | surrogateescape error handler: surrogate characters in the range |
| 414 | U+DC80..U+DCFF are converted to bytes 0x80..0xFF. |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 415 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 416 | Return a pointer to a newly allocated byte string, use PyMem_Free() to free |
| 417 | the memory. Return NULL on encoding or memory allocation error. |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 418 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 419 | If error_pos is not NULL, *error_pos is set to the index of the invalid |
| 420 | character on encoding error, or set to (size_t)-1 otherwise. |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 421 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 422 | Use the Py_DecodeLocale() function to decode the bytes string back to a wide |
| 423 | character string. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 424 | char* |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 425 | Py_EncodeLocale(const wchar_t *text, size_t *error_pos) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 426 | { |
Xavier de Gaye | 76febd0 | 2016-12-15 20:59:58 +0100 | [diff] [blame] | 427 | #if defined(__APPLE__) || defined(__ANDROID__) |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 428 | Py_ssize_t len; |
| 429 | PyObject *unicode, *bytes = NULL; |
| 430 | char *cpath; |
| 431 | |
| 432 | unicode = PyUnicode_FromWideChar(text, wcslen(text)); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 433 | if (unicode == NULL) |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 434 | return NULL; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 435 | |
| 436 | bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
| 437 | Py_DECREF(unicode); |
| 438 | if (bytes == NULL) { |
| 439 | PyErr_Clear(); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 440 | if (error_pos != NULL) |
| 441 | *error_pos = (size_t)-1; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 442 | return NULL; |
| 443 | } |
| 444 | |
| 445 | len = PyBytes_GET_SIZE(bytes); |
| 446 | cpath = PyMem_Malloc(len+1); |
| 447 | if (cpath == NULL) { |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 448 | PyErr_Clear(); |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 449 | Py_DECREF(bytes); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 450 | if (error_pos != NULL) |
| 451 | *error_pos = (size_t)-1; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 452 | return NULL; |
| 453 | } |
| 454 | memcpy(cpath, PyBytes_AsString(bytes), len + 1); |
| 455 | Py_DECREF(bytes); |
| 456 | return cpath; |
| 457 | #else /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 458 | const size_t len = wcslen(text); |
| 459 | char *result = NULL, *bytes = NULL; |
| 460 | size_t i, size, converted; |
| 461 | wchar_t c, buf[2]; |
| 462 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 463 | #ifndef MS_WINDOWS |
| 464 | if (force_ascii == -1) |
| 465 | force_ascii = check_force_ascii(); |
| 466 | |
| 467 | if (force_ascii) |
| 468 | return encode_ascii_surrogateescape(text, error_pos); |
| 469 | #endif |
| 470 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 471 | /* The function works in two steps: |
| 472 | 1. compute the length of the output buffer in bytes (size) |
| 473 | 2. outputs the bytes */ |
| 474 | size = 0; |
| 475 | buf[1] = 0; |
| 476 | while (1) { |
| 477 | for (i=0; i < len; i++) { |
| 478 | c = text[i]; |
| 479 | if (c >= 0xdc80 && c <= 0xdcff) { |
| 480 | /* UTF-8b surrogate */ |
| 481 | if (bytes != NULL) { |
| 482 | *bytes++ = c - 0xdc00; |
| 483 | size--; |
| 484 | } |
| 485 | else |
| 486 | size++; |
| 487 | continue; |
| 488 | } |
| 489 | else { |
| 490 | buf[0] = c; |
| 491 | if (bytes != NULL) |
| 492 | converted = wcstombs(bytes, buf, size); |
| 493 | else |
| 494 | converted = wcstombs(NULL, buf, 0); |
| 495 | if (converted == (size_t)-1) { |
| 496 | if (result != NULL) |
| 497 | PyMem_Free(result); |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 498 | if (error_pos != NULL) |
| 499 | *error_pos = i; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
| 502 | if (bytes != NULL) { |
| 503 | bytes += converted; |
| 504 | size -= converted; |
| 505 | } |
| 506 | else |
| 507 | size += converted; |
| 508 | } |
| 509 | } |
| 510 | if (result != NULL) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 511 | *bytes = '\0'; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 512 | break; |
| 513 | } |
| 514 | |
| 515 | size += 1; /* nul byte at the end */ |
| 516 | result = PyMem_Malloc(size); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 517 | if (result == NULL) { |
| 518 | if (error_pos != NULL) |
| 519 | *error_pos = (size_t)-1; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 520 | return NULL; |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 521 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 522 | bytes = result; |
| 523 | } |
| 524 | return result; |
Xavier de Gaye | 76febd0 | 2016-12-15 20:59:58 +0100 | [diff] [blame] | 525 | #endif /* __APPLE__ or __ANDROID__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 528 | |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 529 | #ifdef MS_WINDOWS |
| 530 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 531 | |
| 532 | static void |
| 533 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) |
| 534 | { |
| 535 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 536 | /* Cannot simply cast and dereference in_ptr, |
| 537 | since it might not be aligned properly */ |
| 538 | __int64 in; |
| 539 | memcpy(&in, in_ptr, sizeof(in)); |
| 540 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 541 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); |
| 542 | } |
| 543 | |
| 544 | void |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 545 | _Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 546 | { |
| 547 | /* XXX endianness */ |
| 548 | __int64 out; |
| 549 | out = time_in + secs_between_epochs; |
| 550 | out = out * 10000000 + nsec_in / 100; |
| 551 | memcpy(out_ptr, &out, sizeof(out)); |
| 552 | } |
| 553 | |
| 554 | /* Below, we *know* that ugo+r is 0444 */ |
| 555 | #if _S_IREAD != 0400 |
| 556 | #error Unsupported C library |
| 557 | #endif |
| 558 | static int |
| 559 | attributes_to_mode(DWORD attr) |
| 560 | { |
| 561 | int m = 0; |
| 562 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 563 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 564 | else |
| 565 | m |= _S_IFREG; |
| 566 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 567 | m |= 0444; |
| 568 | else |
| 569 | m |= 0666; |
| 570 | return m; |
| 571 | } |
| 572 | |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 573 | void |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 574 | _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, |
| 575 | struct _Py_stat_struct *result) |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 576 | { |
| 577 | memset(result, 0, sizeof(*result)); |
| 578 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 579 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 580 | result->st_dev = info->dwVolumeSerialNumber; |
| 581 | result->st_rdev = result->st_dev; |
| 582 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 583 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 584 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 585 | result->st_nlink = info->nNumberOfLinks; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 586 | result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 587 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 588 | /* first clear the S_IFMT bits */ |
| 589 | result->st_mode ^= (result->st_mode & S_IFMT); |
| 590 | /* now set the bits that make this a symlink */ |
| 591 | result->st_mode |= S_IFLNK; |
| 592 | } |
| 593 | result->st_file_attributes = info->dwFileAttributes; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 594 | } |
| 595 | #endif |
| 596 | |
| 597 | /* Return information about a file. |
| 598 | |
| 599 | On POSIX, use fstat(). |
| 600 | |
| 601 | On Windows, use GetFileType() and GetFileInformationByHandle() which support |
| 602 | files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger |
Martin Panter | 6f9b010 | 2015-12-17 10:18:28 +0000 | [diff] [blame] | 603 | than 2 GB because the file size type is a signed 32-bit integer: see issue |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 604 | #23152. |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 605 | |
| 606 | On Windows, set the last Windows error and return nonzero on error. On |
| 607 | POSIX, set errno and return nonzero on error. Fill status and return 0 on |
| 608 | success. */ |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 609 | int |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 610 | _Py_fstat_noraise(int fd, struct _Py_stat_struct *status) |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 611 | { |
| 612 | #ifdef MS_WINDOWS |
| 613 | BY_HANDLE_FILE_INFORMATION info; |
| 614 | HANDLE h; |
| 615 | int type; |
| 616 | |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 617 | _Py_BEGIN_SUPPRESS_IPH |
| 618 | h = (HANDLE)_get_osfhandle(fd); |
| 619 | _Py_END_SUPPRESS_IPH |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 620 | |
| 621 | if (h == INVALID_HANDLE_VALUE) { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 622 | /* errno is already set by _get_osfhandle, but we also set |
| 623 | the Win32 error for callers who expect that */ |
Steve Dower | 8acde7d | 2015-03-07 18:14:07 -0800 | [diff] [blame] | 624 | SetLastError(ERROR_INVALID_HANDLE); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 625 | return -1; |
| 626 | } |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 627 | memset(status, 0, sizeof(*status)); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 628 | |
| 629 | type = GetFileType(h); |
| 630 | if (type == FILE_TYPE_UNKNOWN) { |
| 631 | DWORD error = GetLastError(); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 632 | if (error != 0) { |
| 633 | errno = winerror_to_errno(error); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 634 | return -1; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 635 | } |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 636 | /* else: valid but unknown file */ |
| 637 | } |
| 638 | |
| 639 | if (type != FILE_TYPE_DISK) { |
| 640 | if (type == FILE_TYPE_CHAR) |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 641 | status->st_mode = _S_IFCHR; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 642 | else if (type == FILE_TYPE_PIPE) |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 643 | status->st_mode = _S_IFIFO; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | if (!GetFileInformationByHandle(h, &info)) { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 648 | /* The Win32 error is already set, but we also set errno for |
| 649 | callers who expect it */ |
| 650 | errno = winerror_to_errno(GetLastError()); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 651 | return -1; |
| 652 | } |
| 653 | |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 654 | _Py_attribute_data_to_stat(&info, 0, status); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 655 | /* specific to fstat() */ |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 656 | status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 657 | return 0; |
| 658 | #else |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 659 | return fstat(fd, status); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 660 | #endif |
| 661 | } |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 662 | |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 663 | /* Return information about a file. |
| 664 | |
| 665 | On POSIX, use fstat(). |
| 666 | |
| 667 | On Windows, use GetFileType() and GetFileInformationByHandle() which support |
| 668 | files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger |
Martin Panter | 6f9b010 | 2015-12-17 10:18:28 +0000 | [diff] [blame] | 669 | than 2 GB because the file size type is a signed 32-bit integer: see issue |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 670 | #23152. |
| 671 | |
| 672 | Raise an exception and return -1 on error. On Windows, set the last Windows |
| 673 | error on error. On POSIX, set errno on error. Fill status and return 0 on |
| 674 | success. |
| 675 | |
Victor Stinner | 6f4fae8 | 2015-04-01 18:34:32 +0200 | [diff] [blame] | 676 | Release the GIL to call GetFileType() and GetFileInformationByHandle(), or |
| 677 | to call fstat(). The caller must hold the GIL. */ |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 678 | int |
| 679 | _Py_fstat(int fd, struct _Py_stat_struct *status) |
| 680 | { |
| 681 | int res; |
| 682 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 683 | #ifdef WITH_THREAD |
| 684 | assert(PyGILState_Check()); |
| 685 | #endif |
| 686 | |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 687 | Py_BEGIN_ALLOW_THREADS |
| 688 | res = _Py_fstat_noraise(fd, status); |
| 689 | Py_END_ALLOW_THREADS |
| 690 | |
| 691 | if (res != 0) { |
| 692 | #ifdef MS_WINDOWS |
| 693 | PyErr_SetFromWindowsErr(0); |
| 694 | #else |
| 695 | PyErr_SetFromErrno(PyExc_OSError); |
| 696 | #endif |
| 697 | return -1; |
| 698 | } |
| 699 | return 0; |
| 700 | } |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 701 | |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 702 | /* Call _wstat() on Windows, or encode the path to the filesystem encoding and |
| 703 | call stat() otherwise. Only fill st_mode attribute on Windows. |
| 704 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 705 | Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was |
| 706 | raised. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 707 | |
| 708 | int |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 709 | _Py_stat(PyObject *path, struct stat *statbuf) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 710 | { |
| 711 | #ifdef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 712 | int err; |
| 713 | struct _stat wstatbuf; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 714 | const wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 715 | |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 716 | wpath = _PyUnicode_AsUnicode(path); |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 717 | if (wpath == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 718 | return -2; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 719 | |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 720 | err = _wstat(wpath, &wstatbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 721 | if (!err) |
| 722 | statbuf->st_mode = wstatbuf.st_mode; |
| 723 | return err; |
| 724 | #else |
| 725 | int ret; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 726 | PyObject *bytes; |
| 727 | char *cpath; |
| 728 | |
| 729 | bytes = PyUnicode_EncodeFSDefault(path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 730 | if (bytes == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 731 | return -2; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 732 | |
| 733 | /* check for embedded null bytes */ |
| 734 | if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) { |
| 735 | Py_DECREF(bytes); |
| 736 | return -2; |
| 737 | } |
| 738 | |
| 739 | ret = stat(cpath, statbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 740 | Py_DECREF(bytes); |
| 741 | return ret; |
| 742 | #endif |
| 743 | } |
| 744 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 745 | |
Antoine Pitrou | 409b538 | 2013-10-12 22:41:17 +0200 | [diff] [blame] | 746 | static int |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 747 | get_inheritable(int fd, int raise) |
| 748 | { |
| 749 | #ifdef MS_WINDOWS |
| 750 | HANDLE handle; |
| 751 | DWORD flags; |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 752 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 753 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 754 | handle = (HANDLE)_get_osfhandle(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 755 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 756 | if (handle == INVALID_HANDLE_VALUE) { |
| 757 | if (raise) |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 758 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 759 | return -1; |
| 760 | } |
| 761 | |
| 762 | if (!GetHandleInformation(handle, &flags)) { |
| 763 | if (raise) |
| 764 | PyErr_SetFromWindowsErr(0); |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | return (flags & HANDLE_FLAG_INHERIT); |
| 769 | #else |
| 770 | int flags; |
| 771 | |
| 772 | flags = fcntl(fd, F_GETFD, 0); |
| 773 | if (flags == -1) { |
| 774 | if (raise) |
| 775 | PyErr_SetFromErrno(PyExc_OSError); |
| 776 | return -1; |
| 777 | } |
| 778 | return !(flags & FD_CLOEXEC); |
| 779 | #endif |
| 780 | } |
| 781 | |
| 782 | /* Get the inheritable flag of the specified file descriptor. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 783 | Return 1 if the file descriptor can be inherited, 0 if it cannot, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 784 | raise an exception and return -1 on error. */ |
| 785 | int |
| 786 | _Py_get_inheritable(int fd) |
| 787 | { |
| 788 | return get_inheritable(fd, 1); |
| 789 | } |
| 790 | |
| 791 | static int |
| 792 | set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) |
| 793 | { |
| 794 | #ifdef MS_WINDOWS |
| 795 | HANDLE handle; |
| 796 | DWORD flags; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 797 | #else |
| 798 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 799 | static int ioctl_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 800 | int request; |
| 801 | int err; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 802 | #endif |
Victor Stinner | a858bbd | 2016-04-17 16:51:52 +0200 | [diff] [blame] | 803 | int flags, new_flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 804 | int res; |
| 805 | #endif |
| 806 | |
| 807 | /* atomic_flag_works can only be used to make the file descriptor |
| 808 | non-inheritable */ |
| 809 | assert(!(atomic_flag_works != NULL && inheritable)); |
| 810 | |
| 811 | if (atomic_flag_works != NULL && !inheritable) { |
| 812 | if (*atomic_flag_works == -1) { |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 813 | int isInheritable = get_inheritable(fd, raise); |
| 814 | if (isInheritable == -1) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 815 | return -1; |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 816 | *atomic_flag_works = !isInheritable; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | if (*atomic_flag_works) |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | #ifdef MS_WINDOWS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 824 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 825 | handle = (HANDLE)_get_osfhandle(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 826 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 827 | if (handle == INVALID_HANDLE_VALUE) { |
| 828 | if (raise) |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 829 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 830 | return -1; |
| 831 | } |
| 832 | |
| 833 | if (inheritable) |
| 834 | flags = HANDLE_FLAG_INHERIT; |
| 835 | else |
| 836 | flags = 0; |
| 837 | if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) { |
| 838 | if (raise) |
| 839 | PyErr_SetFromWindowsErr(0); |
| 840 | return -1; |
| 841 | } |
| 842 | return 0; |
| 843 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 844 | #else |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 845 | |
| 846 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 847 | if (ioctl_works != 0) { |
| 848 | /* fast-path: ioctl() only requires one syscall */ |
| 849 | if (inheritable) |
| 850 | request = FIONCLEX; |
| 851 | else |
| 852 | request = FIOCLEX; |
| 853 | err = ioctl(fd, request, NULL); |
| 854 | if (!err) { |
| 855 | ioctl_works = 1; |
| 856 | return 0; |
| 857 | } |
| 858 | |
Victor Stinner | 3116cc4 | 2016-05-19 16:46:18 +0200 | [diff] [blame] | 859 | if (errno != ENOTTY && errno != EACCES) { |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 860 | if (raise) |
| 861 | PyErr_SetFromErrno(PyExc_OSError); |
| 862 | return -1; |
| 863 | } |
| 864 | else { |
| 865 | /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for |
| 866 | device". The ioctl is declared but not supported by the kernel. |
| 867 | Remember that ioctl() doesn't work. It is the case on |
Victor Stinner | 3116cc4 | 2016-05-19 16:46:18 +0200 | [diff] [blame] | 868 | Illumos-based OS for example. |
| 869 | |
| 870 | Issue #27057: When SELinux policy disallows ioctl it will fail |
| 871 | with EACCES. While FIOCLEX is safe operation it may be |
| 872 | unavailable because ioctl was denied altogether. |
| 873 | This can be the case on Android. */ |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 874 | ioctl_works = 0; |
| 875 | } |
| 876 | /* fallback to fcntl() if ioctl() does not work */ |
| 877 | } |
| 878 | #endif |
| 879 | |
| 880 | /* slow-path: fcntl() requires two syscalls */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 881 | flags = fcntl(fd, F_GETFD); |
| 882 | if (flags < 0) { |
| 883 | if (raise) |
| 884 | PyErr_SetFromErrno(PyExc_OSError); |
| 885 | return -1; |
| 886 | } |
| 887 | |
Victor Stinner | a858bbd | 2016-04-17 16:51:52 +0200 | [diff] [blame] | 888 | if (inheritable) { |
| 889 | new_flags = flags & ~FD_CLOEXEC; |
| 890 | } |
| 891 | else { |
| 892 | new_flags = flags | FD_CLOEXEC; |
| 893 | } |
| 894 | |
| 895 | if (new_flags == flags) { |
| 896 | /* FD_CLOEXEC flag already set/cleared: nothing to do */ |
| 897 | return 0; |
| 898 | } |
| 899 | |
Xavier de Gaye | ec5d3cd | 2016-11-19 16:19:29 +0100 | [diff] [blame] | 900 | res = fcntl(fd, F_SETFD, new_flags); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 901 | if (res < 0) { |
| 902 | if (raise) |
| 903 | PyErr_SetFromErrno(PyExc_OSError); |
| 904 | return -1; |
| 905 | } |
| 906 | return 0; |
| 907 | #endif |
| 908 | } |
| 909 | |
| 910 | /* Make the file descriptor non-inheritable. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 911 | Return 0 on success, set errno and return -1 on error. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 912 | static int |
| 913 | make_non_inheritable(int fd) |
| 914 | { |
| 915 | return set_inheritable(fd, 0, 0, NULL); |
| 916 | } |
| 917 | |
| 918 | /* Set the inheritable flag of the specified file descriptor. |
| 919 | On success: return 0, on error: raise an exception if raise is nonzero |
| 920 | and return -1. |
| 921 | |
| 922 | If atomic_flag_works is not NULL: |
| 923 | |
| 924 | * if *atomic_flag_works==-1, check if the inheritable is set on the file |
| 925 | descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and |
| 926 | set the inheritable flag |
| 927 | * if *atomic_flag_works==1: do nothing |
| 928 | * if *atomic_flag_works==0: set inheritable flag to False |
| 929 | |
| 930 | Set atomic_flag_works to NULL if no atomic flag was used to create the |
| 931 | file descriptor. |
| 932 | |
| 933 | atomic_flag_works can only be used to make a file descriptor |
| 934 | non-inheritable: atomic_flag_works must be NULL if inheritable=1. */ |
| 935 | int |
| 936 | _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) |
| 937 | { |
| 938 | return set_inheritable(fd, inheritable, 1, atomic_flag_works); |
| 939 | } |
| 940 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 941 | static int |
| 942 | _Py_open_impl(const char *pathname, int flags, int gil_held) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 943 | { |
| 944 | int fd; |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 945 | int async_err = 0; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 946 | #ifndef MS_WINDOWS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 947 | int *atomic_flag_works; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 948 | #endif |
| 949 | |
| 950 | #ifdef MS_WINDOWS |
| 951 | flags |= O_NOINHERIT; |
| 952 | #elif defined(O_CLOEXEC) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 953 | atomic_flag_works = &_Py_open_cloexec_works; |
| 954 | flags |= O_CLOEXEC; |
| 955 | #else |
| 956 | atomic_flag_works = NULL; |
| 957 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 958 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 959 | if (gil_held) { |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 960 | do { |
| 961 | Py_BEGIN_ALLOW_THREADS |
| 962 | fd = open(pathname, flags); |
| 963 | Py_END_ALLOW_THREADS |
| 964 | } while (fd < 0 |
| 965 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 966 | if (async_err) |
| 967 | return -1; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 968 | if (fd < 0) { |
| 969 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname); |
| 970 | return -1; |
| 971 | } |
| 972 | } |
| 973 | else { |
| 974 | fd = open(pathname, flags); |
| 975 | if (fd < 0) |
| 976 | return -1; |
| 977 | } |
| 978 | |
| 979 | #ifndef MS_WINDOWS |
| 980 | if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 981 | close(fd); |
| 982 | return -1; |
| 983 | } |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 984 | #endif |
| 985 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 986 | return fd; |
| 987 | } |
| 988 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 989 | /* Open a file with the specified flags (wrapper to open() function). |
| 990 | Return a file descriptor on success. Raise an exception and return -1 on |
| 991 | error. |
| 992 | |
| 993 | The file descriptor is created non-inheritable. |
| 994 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 995 | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
| 996 | except if the Python signal handler raises an exception. |
| 997 | |
Victor Stinner | 6f4fae8 | 2015-04-01 18:34:32 +0200 | [diff] [blame] | 998 | Release the GIL to call open(). The caller must hold the GIL. */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 999 | int |
| 1000 | _Py_open(const char *pathname, int flags) |
| 1001 | { |
Victor Stinner | bc5b80b | 2015-10-11 09:54:42 +0200 | [diff] [blame] | 1002 | #ifdef WITH_THREAD |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1003 | /* _Py_open() must be called with the GIL held. */ |
| 1004 | assert(PyGILState_Check()); |
Victor Stinner | bc5b80b | 2015-10-11 09:54:42 +0200 | [diff] [blame] | 1005 | #endif |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1006 | return _Py_open_impl(pathname, flags, 1); |
| 1007 | } |
| 1008 | |
| 1009 | /* Open a file with the specified flags (wrapper to open() function). |
| 1010 | Return a file descriptor on success. Set errno and return -1 on error. |
| 1011 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1012 | The file descriptor is created non-inheritable. |
| 1013 | |
| 1014 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1015 | int |
| 1016 | _Py_open_noraise(const char *pathname, int flags) |
| 1017 | { |
| 1018 | return _Py_open_impl(pathname, flags, 0); |
| 1019 | } |
| 1020 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1021 | /* Open a file. Use _wfopen() on Windows, encode the path to the locale |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1022 | encoding and use fopen() otherwise. |
| 1023 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1024 | The file descriptor is created non-inheritable. |
| 1025 | |
| 1026 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1027 | FILE * |
| 1028 | _Py_wfopen(const wchar_t *path, const wchar_t *mode) |
| 1029 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1030 | FILE *f; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1031 | #ifndef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1032 | char *cpath; |
| 1033 | char cmode[10]; |
| 1034 | size_t r; |
| 1035 | r = wcstombs(cmode, mode, 10); |
| 1036 | if (r == (size_t)-1 || r >= 10) { |
| 1037 | errno = EINVAL; |
| 1038 | return NULL; |
| 1039 | } |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1040 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1041 | if (cpath == NULL) |
| 1042 | return NULL; |
| 1043 | f = fopen(cpath, cmode); |
| 1044 | PyMem_Free(cpath); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1045 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1046 | f = _wfopen(path, mode); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1047 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1048 | if (f == NULL) |
| 1049 | return NULL; |
| 1050 | if (make_non_inheritable(fileno(f)) < 0) { |
| 1051 | fclose(f); |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1057 | /* Wrapper to fopen(). |
| 1058 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1059 | The file descriptor is created non-inheritable. |
| 1060 | |
| 1061 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1062 | FILE* |
| 1063 | _Py_fopen(const char *pathname, const char *mode) |
| 1064 | { |
| 1065 | FILE *f = fopen(pathname, mode); |
| 1066 | if (f == NULL) |
| 1067 | return NULL; |
| 1068 | if (make_non_inheritable(fileno(f)) < 0) { |
| 1069 | fclose(f); |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | return f; |
| 1073 | } |
| 1074 | |
| 1075 | /* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1076 | encoding and call fopen() otherwise. |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1077 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1078 | Return the new file object on success. Raise an exception and return NULL |
| 1079 | on error. |
| 1080 | |
| 1081 | The file descriptor is created non-inheritable. |
| 1082 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1083 | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
| 1084 | except if the Python signal handler raises an exception. |
| 1085 | |
Victor Stinner | 6f4fae8 | 2015-04-01 18:34:32 +0200 | [diff] [blame] | 1086 | Release the GIL to call _wfopen() or fopen(). The caller must hold |
| 1087 | the GIL. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1088 | FILE* |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1089 | _Py_fopen_obj(PyObject *path, const char *mode) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1090 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1091 | FILE *f; |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1092 | int async_err = 0; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1093 | #ifdef MS_WINDOWS |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 1094 | const wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1095 | wchar_t wmode[10]; |
| 1096 | int usize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1097 | |
Victor Stinner | bc5b80b | 2015-10-11 09:54:42 +0200 | [diff] [blame] | 1098 | #ifdef WITH_THREAD |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1099 | assert(PyGILState_Check()); |
Victor Stinner | bc5b80b | 2015-10-11 09:54:42 +0200 | [diff] [blame] | 1100 | #endif |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1101 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 1102 | if (!PyUnicode_Check(path)) { |
| 1103 | PyErr_Format(PyExc_TypeError, |
| 1104 | "str file path expected under Windows, got %R", |
| 1105 | Py_TYPE(path)); |
| 1106 | return NULL; |
| 1107 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 1108 | wpath = _PyUnicode_AsUnicode(path); |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 1109 | if (wpath == NULL) |
| 1110 | return NULL; |
| 1111 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1112 | usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1113 | if (usize == 0) { |
| 1114 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1115 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1116 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1117 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1118 | do { |
| 1119 | Py_BEGIN_ALLOW_THREADS |
| 1120 | f = _wfopen(wpath, wmode); |
| 1121 | Py_END_ALLOW_THREADS |
| 1122 | } while (f == NULL |
| 1123 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1124 | #else |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 1125 | PyObject *bytes; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1126 | char *path_bytes; |
| 1127 | |
Victor Stinner | bc5b80b | 2015-10-11 09:54:42 +0200 | [diff] [blame] | 1128 | #ifdef WITH_THREAD |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1129 | assert(PyGILState_Check()); |
Victor Stinner | bc5b80b | 2015-10-11 09:54:42 +0200 | [diff] [blame] | 1130 | #endif |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1131 | |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 1132 | if (!PyUnicode_FSConverter(path, &bytes)) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1133 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1134 | path_bytes = PyBytes_AS_STRING(bytes); |
| 1135 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1136 | do { |
| 1137 | Py_BEGIN_ALLOW_THREADS |
| 1138 | f = fopen(path_bytes, mode); |
| 1139 | Py_END_ALLOW_THREADS |
| 1140 | } while (f == NULL |
| 1141 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1142 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1143 | Py_DECREF(bytes); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1144 | #endif |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1145 | if (async_err) |
| 1146 | return NULL; |
| 1147 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1148 | if (f == NULL) { |
| 1149 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1150 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | if (set_inheritable(fileno(f), 0, 1, NULL) < 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1154 | fclose(f); |
| 1155 | return NULL; |
| 1156 | } |
| 1157 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1160 | /* Read count bytes from fd into buf. |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1161 | |
| 1162 | On success, return the number of read bytes, it can be lower than count. |
| 1163 | If the current file offset is at or past the end of file, no bytes are read, |
| 1164 | and read() returns zero. |
| 1165 | |
| 1166 | On error, raise an exception, set errno and return -1. |
| 1167 | |
| 1168 | When interrupted by a signal (read() fails with EINTR), retry the syscall. |
| 1169 | If the Python signal handler raises an exception, the function returns -1 |
| 1170 | (the syscall is not retried). |
| 1171 | |
| 1172 | Release the GIL to call read(). The caller must hold the GIL. */ |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1173 | Py_ssize_t |
| 1174 | _Py_read(int fd, void *buf, size_t count) |
| 1175 | { |
| 1176 | Py_ssize_t n; |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1177 | int err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1178 | int async_err = 0; |
| 1179 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1180 | #ifdef WITH_THREAD |
| 1181 | assert(PyGILState_Check()); |
| 1182 | #endif |
| 1183 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1184 | /* _Py_read() must not be called with an exception set, otherwise the |
| 1185 | * caller may think that read() was interrupted by a signal and the signal |
| 1186 | * handler raised an exception. */ |
| 1187 | assert(!PyErr_Occurred()); |
| 1188 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1189 | #ifdef MS_WINDOWS |
| 1190 | if (count > INT_MAX) { |
| 1191 | /* On Windows, the count parameter of read() is an int */ |
| 1192 | count = INT_MAX; |
| 1193 | } |
| 1194 | #else |
| 1195 | if (count > PY_SSIZE_T_MAX) { |
| 1196 | /* if count is greater than PY_SSIZE_T_MAX, |
| 1197 | * read() result is undefined */ |
| 1198 | count = PY_SSIZE_T_MAX; |
| 1199 | } |
| 1200 | #endif |
| 1201 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1202 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1203 | do { |
| 1204 | Py_BEGIN_ALLOW_THREADS |
| 1205 | errno = 0; |
| 1206 | #ifdef MS_WINDOWS |
| 1207 | n = read(fd, buf, (int)count); |
| 1208 | #else |
| 1209 | n = read(fd, buf, count); |
| 1210 | #endif |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1211 | /* save/restore errno because PyErr_CheckSignals() |
| 1212 | * and PyErr_SetFromErrno() can modify it */ |
| 1213 | err = errno; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1214 | Py_END_ALLOW_THREADS |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1215 | } while (n < 0 && err == EINTR && |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1216 | !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1217 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1218 | |
| 1219 | if (async_err) { |
| 1220 | /* read() was interrupted by a signal (failed with EINTR) |
| 1221 | * and the Python signal handler raised an exception */ |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1222 | errno = err; |
| 1223 | assert(errno == EINTR && PyErr_Occurred()); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1224 | return -1; |
| 1225 | } |
| 1226 | if (n < 0) { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1227 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1228 | errno = err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1229 | return -1; |
| 1230 | } |
| 1231 | |
| 1232 | return n; |
| 1233 | } |
| 1234 | |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1235 | static Py_ssize_t |
| 1236 | _Py_write_impl(int fd, const void *buf, size_t count, int gil_held) |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1237 | { |
| 1238 | Py_ssize_t n; |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1239 | int err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1240 | int async_err = 0; |
| 1241 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1242 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1243 | #ifdef MS_WINDOWS |
| 1244 | if (count > 32767 && isatty(fd)) { |
| 1245 | /* Issue #11395: the Windows console returns an error (12: not |
| 1246 | enough space error) on writing into stdout if stdout mode is |
| 1247 | binary and the length is greater than 66,000 bytes (or less, |
| 1248 | depending on heap usage). */ |
| 1249 | count = 32767; |
| 1250 | } |
| 1251 | else if (count > INT_MAX) |
| 1252 | count = INT_MAX; |
| 1253 | #else |
| 1254 | if (count > PY_SSIZE_T_MAX) { |
| 1255 | /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer |
| 1256 | * to do it ourself to have a portable behaviour. */ |
| 1257 | count = PY_SSIZE_T_MAX; |
| 1258 | } |
| 1259 | #endif |
| 1260 | |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1261 | if (gil_held) { |
| 1262 | do { |
| 1263 | Py_BEGIN_ALLOW_THREADS |
| 1264 | errno = 0; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1265 | #ifdef MS_WINDOWS |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1266 | n = write(fd, buf, (int)count); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1267 | #else |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1268 | n = write(fd, buf, count); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1269 | #endif |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1270 | /* save/restore errno because PyErr_CheckSignals() |
| 1271 | * and PyErr_SetFromErrno() can modify it */ |
| 1272 | err = errno; |
| 1273 | Py_END_ALLOW_THREADS |
| 1274 | } while (n < 0 && err == EINTR && |
| 1275 | !(async_err = PyErr_CheckSignals())); |
| 1276 | } |
| 1277 | else { |
| 1278 | do { |
| 1279 | errno = 0; |
| 1280 | #ifdef MS_WINDOWS |
| 1281 | n = write(fd, buf, (int)count); |
| 1282 | #else |
| 1283 | n = write(fd, buf, count); |
| 1284 | #endif |
| 1285 | err = errno; |
| 1286 | } while (n < 0 && err == EINTR); |
| 1287 | } |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1288 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1289 | |
| 1290 | if (async_err) { |
| 1291 | /* write() was interrupted by a signal (failed with EINTR) |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1292 | and the Python signal handler raised an exception (if gil_held is |
| 1293 | nonzero). */ |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1294 | errno = err; |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1295 | assert(errno == EINTR && (!gil_held || PyErr_Occurred())); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1296 | return -1; |
| 1297 | } |
| 1298 | if (n < 0) { |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1299 | if (gil_held) |
| 1300 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1301 | errno = err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1302 | return -1; |
| 1303 | } |
| 1304 | |
| 1305 | return n; |
| 1306 | } |
| 1307 | |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1308 | /* Write count bytes of buf into fd. |
| 1309 | |
| 1310 | On success, return the number of written bytes, it can be lower than count |
| 1311 | including 0. On error, raise an exception, set errno and return -1. |
| 1312 | |
| 1313 | When interrupted by a signal (write() fails with EINTR), retry the syscall. |
| 1314 | If the Python signal handler raises an exception, the function returns -1 |
| 1315 | (the syscall is not retried). |
| 1316 | |
| 1317 | Release the GIL to call write(). The caller must hold the GIL. */ |
| 1318 | Py_ssize_t |
| 1319 | _Py_write(int fd, const void *buf, size_t count) |
| 1320 | { |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1321 | #ifdef WITH_THREAD |
| 1322 | assert(PyGILState_Check()); |
| 1323 | #endif |
| 1324 | |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1325 | /* _Py_write() must not be called with an exception set, otherwise the |
| 1326 | * caller may think that write() was interrupted by a signal and the signal |
| 1327 | * handler raised an exception. */ |
| 1328 | assert(!PyErr_Occurred()); |
| 1329 | |
| 1330 | return _Py_write_impl(fd, buf, count, 1); |
| 1331 | } |
| 1332 | |
| 1333 | /* Write count bytes of buf into fd. |
| 1334 | * |
| 1335 | * On success, return the number of written bytes, it can be lower than count |
| 1336 | * including 0. On error, set errno and return -1. |
| 1337 | * |
| 1338 | * When interrupted by a signal (write() fails with EINTR), retry the syscall |
| 1339 | * without calling the Python signal handler. */ |
| 1340 | Py_ssize_t |
| 1341 | _Py_write_noraise(int fd, const void *buf, size_t count) |
| 1342 | { |
| 1343 | return _Py_write_impl(fd, buf, count, 0); |
| 1344 | } |
| 1345 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1346 | #ifdef HAVE_READLINK |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1347 | |
| 1348 | /* Read value of symbolic link. Encode the path to the locale encoding, decode |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1349 | the result from the locale encoding. Return -1 on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1350 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1351 | int |
| 1352 | _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) |
| 1353 | { |
| 1354 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1355 | char cbuf[MAXPATHLEN]; |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1356 | wchar_t *wbuf; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1357 | int res; |
| 1358 | size_t r1; |
| 1359 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1360 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1361 | if (cpath == NULL) { |
| 1362 | errno = EINVAL; |
| 1363 | return -1; |
| 1364 | } |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1365 | res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf)); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1366 | PyMem_Free(cpath); |
| 1367 | if (res == -1) |
| 1368 | return -1; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1369 | if (res == Py_ARRAY_LENGTH(cbuf)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1370 | errno = EINVAL; |
| 1371 | return -1; |
| 1372 | } |
| 1373 | cbuf[res] = '\0'; /* buf will be null terminated */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1374 | wbuf = Py_DecodeLocale(cbuf, &r1); |
Victor Stinner | 350147b | 2010-10-16 22:52:09 +0000 | [diff] [blame] | 1375 | if (wbuf == NULL) { |
| 1376 | errno = EINVAL; |
| 1377 | return -1; |
| 1378 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1379 | if (bufsiz <= r1) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1380 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1381 | errno = EINVAL; |
| 1382 | return -1; |
| 1383 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1384 | wcsncpy(buf, wbuf, bufsiz); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1385 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1386 | return (int)r1; |
| 1387 | } |
| 1388 | #endif |
| 1389 | |
| 1390 | #ifdef HAVE_REALPATH |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1391 | |
| 1392 | /* Return the canonicalized absolute pathname. Encode path to the locale |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1393 | encoding, decode the result from the locale encoding. |
| 1394 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1395 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1396 | wchar_t* |
Victor Stinner | 015f4d8 | 2010-10-07 22:29:53 +0000 | [diff] [blame] | 1397 | _Py_wrealpath(const wchar_t *path, |
| 1398 | wchar_t *resolved_path, size_t resolved_path_size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1399 | { |
| 1400 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1401 | char cresolved_path[MAXPATHLEN]; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1402 | wchar_t *wresolved_path; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1403 | char *res; |
| 1404 | size_t r; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1405 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1406 | if (cpath == NULL) { |
| 1407 | errno = EINVAL; |
| 1408 | return NULL; |
| 1409 | } |
| 1410 | res = realpath(cpath, cresolved_path); |
| 1411 | PyMem_Free(cpath); |
| 1412 | if (res == NULL) |
| 1413 | return NULL; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1414 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1415 | wresolved_path = Py_DecodeLocale(cresolved_path, &r); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1416 | if (wresolved_path == NULL) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1417 | errno = EINVAL; |
| 1418 | return NULL; |
| 1419 | } |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1420 | if (resolved_path_size <= r) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1421 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1422 | errno = EINVAL; |
| 1423 | return NULL; |
| 1424 | } |
| 1425 | wcsncpy(resolved_path, wresolved_path, resolved_path_size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1426 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1427 | return resolved_path; |
| 1428 | } |
| 1429 | #endif |
| 1430 | |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1431 | /* Get the current directory. size is the buffer size in wide characters |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1432 | including the null character. Decode the path from the locale encoding. |
| 1433 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1434 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1435 | wchar_t* |
| 1436 | _Py_wgetcwd(wchar_t *buf, size_t size) |
| 1437 | { |
| 1438 | #ifdef MS_WINDOWS |
Victor Stinner | 56785ea | 2013-06-05 00:46:29 +0200 | [diff] [blame] | 1439 | int isize = (int)Py_MIN(size, INT_MAX); |
| 1440 | return _wgetcwd(buf, isize); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1441 | #else |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1442 | char fname[MAXPATHLEN]; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1443 | wchar_t *wname; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1444 | size_t len; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1445 | |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1446 | if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1447 | return NULL; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1448 | wname = Py_DecodeLocale(fname, &len); |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1449 | if (wname == NULL) |
| 1450 | return NULL; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1451 | if (size <= len) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1452 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1453 | return NULL; |
| 1454 | } |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1455 | wcsncpy(buf, wname, size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1456 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1457 | return buf; |
| 1458 | #endif |
| 1459 | } |
| 1460 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1461 | /* Duplicate a file descriptor. The new file descriptor is created as |
| 1462 | non-inheritable. Return a new file descriptor on success, raise an OSError |
| 1463 | exception and return -1 on error. |
| 1464 | |
| 1465 | The GIL is released to call dup(). The caller must hold the GIL. */ |
| 1466 | int |
| 1467 | _Py_dup(int fd) |
| 1468 | { |
| 1469 | #ifdef MS_WINDOWS |
| 1470 | HANDLE handle; |
| 1471 | DWORD ftype; |
| 1472 | #endif |
| 1473 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1474 | #ifdef WITH_THREAD |
| 1475 | assert(PyGILState_Check()); |
| 1476 | #endif |
| 1477 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1478 | #ifdef MS_WINDOWS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1479 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1480 | handle = (HANDLE)_get_osfhandle(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1481 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1482 | if (handle == INVALID_HANDLE_VALUE) { |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 1483 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1484 | return -1; |
| 1485 | } |
| 1486 | |
| 1487 | /* get the file type, ignore the error if it failed */ |
| 1488 | ftype = GetFileType(handle); |
| 1489 | |
| 1490 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1491 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1492 | fd = dup(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1493 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1494 | Py_END_ALLOW_THREADS |
| 1495 | if (fd < 0) { |
| 1496 | PyErr_SetFromErrno(PyExc_OSError); |
| 1497 | return -1; |
| 1498 | } |
| 1499 | |
| 1500 | /* Character files like console cannot be make non-inheritable */ |
| 1501 | if (ftype != FILE_TYPE_CHAR) { |
| 1502 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1503 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1504 | close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1505 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1506 | return -1; |
| 1507 | } |
| 1508 | } |
| 1509 | #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC) |
| 1510 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1511 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1512 | fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1513 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1514 | Py_END_ALLOW_THREADS |
| 1515 | if (fd < 0) { |
| 1516 | PyErr_SetFromErrno(PyExc_OSError); |
| 1517 | return -1; |
| 1518 | } |
| 1519 | |
| 1520 | #else |
| 1521 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1522 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1523 | fd = dup(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1524 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1525 | Py_END_ALLOW_THREADS |
| 1526 | if (fd < 0) { |
| 1527 | PyErr_SetFromErrno(PyExc_OSError); |
| 1528 | return -1; |
| 1529 | } |
| 1530 | |
| 1531 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1532 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1533 | close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1534 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1535 | return -1; |
| 1536 | } |
| 1537 | #endif |
| 1538 | return fd; |
| 1539 | } |
| 1540 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1541 | #ifndef MS_WINDOWS |
| 1542 | /* Get the blocking mode of the file descriptor. |
| 1543 | Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared, |
| 1544 | raise an exception and return -1 on error. */ |
| 1545 | int |
| 1546 | _Py_get_blocking(int fd) |
| 1547 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1548 | int flags; |
| 1549 | _Py_BEGIN_SUPPRESS_IPH |
| 1550 | flags = fcntl(fd, F_GETFL, 0); |
| 1551 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1552 | if (flags < 0) { |
| 1553 | PyErr_SetFromErrno(PyExc_OSError); |
| 1554 | return -1; |
| 1555 | } |
| 1556 | |
| 1557 | return !(flags & O_NONBLOCK); |
| 1558 | } |
| 1559 | |
| 1560 | /* Set the blocking mode of the specified file descriptor. |
| 1561 | |
| 1562 | Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag |
| 1563 | otherwise. |
| 1564 | |
| 1565 | Return 0 on success, raise an exception and return -1 on error. */ |
| 1566 | int |
| 1567 | _Py_set_blocking(int fd, int blocking) |
| 1568 | { |
| 1569 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO) |
| 1570 | int arg = !blocking; |
| 1571 | if (ioctl(fd, FIONBIO, &arg) < 0) |
| 1572 | goto error; |
| 1573 | #else |
| 1574 | int flags, res; |
| 1575 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1576 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1577 | flags = fcntl(fd, F_GETFL, 0); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1578 | if (flags >= 0) { |
| 1579 | if (blocking) |
| 1580 | flags = flags & (~O_NONBLOCK); |
| 1581 | else |
| 1582 | flags = flags | O_NONBLOCK; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1583 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1584 | res = fcntl(fd, F_SETFL, flags); |
| 1585 | } else { |
| 1586 | res = -1; |
| 1587 | } |
| 1588 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1589 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1590 | if (res < 0) |
| 1591 | goto error; |
| 1592 | #endif |
| 1593 | return 0; |
| 1594 | |
| 1595 | error: |
| 1596 | PyErr_SetFromErrno(PyExc_OSError); |
| 1597 | return -1; |
| 1598 | } |
| 1599 | #endif |