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 |
| 6 | # include <windows.h> |
| 7 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 8 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9 | #ifdef HAVE_LANGINFO_H |
| 10 | #include <langinfo.h> |
| 11 | #endif |
| 12 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13 | #ifdef HAVE_SYS_IOCTL_H |
| 14 | #include <sys/ioctl.h> |
| 15 | #endif |
| 16 | |
| 17 | #ifdef HAVE_FCNTL_H |
| 18 | #include <fcntl.h> |
| 19 | #endif /* HAVE_FCNTL_H */ |
| 20 | |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 21 | #ifdef __APPLE__ |
| 22 | extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size); |
| 23 | #endif |
| 24 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 25 | #ifdef O_CLOEXEC |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 26 | /* Does open() support the O_CLOEXEC flag? Possible values: |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 27 | |
| 28 | -1: unknown |
| 29 | 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23 |
| 30 | 1: open() supports O_CLOEXEC flag, close-on-exec is set |
| 31 | |
| 32 | The flag is used by _Py_open(), io.FileIO and os.open() */ |
| 33 | int _Py_open_cloexec_works = -1; |
| 34 | #endif |
| 35 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 36 | PyObject * |
| 37 | _Py_device_encoding(int fd) |
| 38 | { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 39 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 40 | UINT cp; |
| 41 | #endif |
| 42 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 43 | Py_RETURN_NONE; |
| 44 | } |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 45 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 46 | if (fd == 0) |
| 47 | cp = GetConsoleCP(); |
| 48 | else if (fd == 1 || fd == 2) |
| 49 | cp = GetConsoleOutputCP(); |
| 50 | else |
| 51 | cp = 0; |
| 52 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 53 | has no console */ |
| 54 | if (cp != 0) |
| 55 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
| 56 | #elif defined(CODESET) |
| 57 | { |
| 58 | char *codeset = nl_langinfo(CODESET); |
| 59 | if (codeset != NULL && codeset[0] != 0) |
| 60 | return PyUnicode_FromString(codeset); |
| 61 | } |
| 62 | #endif |
| 63 | Py_RETURN_NONE; |
| 64 | } |
| 65 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 66 | #if !defined(__APPLE__) && !defined(MS_WINDOWS) |
| 67 | extern int _Py_normalize_encoding(const char *, char *, size_t); |
| 68 | |
| 69 | /* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale. |
| 70 | On these operating systems, nl_langinfo(CODESET) announces an alias of the |
| 71 | ASCII encoding, whereas mbstowcs() and wcstombs() functions use the |
| 72 | ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use |
| 73 | locale.getpreferredencoding() codec. For example, if command line arguments |
| 74 | are decoded by mbstowcs() and encoded back by os.fsencode(), we get a |
| 75 | UnicodeEncodeError instead of retrieving the original byte string. |
| 76 | |
| 77 | The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C", |
| 78 | nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least |
| 79 | one byte in range 0x80-0xff can be decoded from the locale encoding. The |
| 80 | workaround is also enabled on error, for example if getting the locale |
| 81 | failed. |
| 82 | |
Philip Jenvey | 215c49a | 2013-01-15 13:24:12 -0800 | [diff] [blame] | 83 | Values of force_ascii: |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 84 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 85 | 1: the workaround is used: Py_EncodeLocale() uses |
| 86 | encode_ascii_surrogateescape() and Py_DecodeLocale() uses |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 87 | decode_ascii_surrogateescape() |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 88 | 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and |
| 89 | Py_DecodeLocale() uses mbstowcs() |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 90 | -1: unknown, need to call check_force_ascii() to get the value |
| 91 | */ |
| 92 | static int force_ascii = -1; |
| 93 | |
| 94 | static int |
| 95 | check_force_ascii(void) |
| 96 | { |
| 97 | char *loc; |
| 98 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 99 | char *codeset, **alias; |
| 100 | char encoding[100]; |
| 101 | int is_ascii; |
| 102 | unsigned int i; |
| 103 | char* ascii_aliases[] = { |
| 104 | "ascii", |
| 105 | "646", |
| 106 | "ansi-x3.4-1968", |
| 107 | "ansi-x3-4-1968", |
| 108 | "ansi-x3.4-1986", |
| 109 | "cp367", |
| 110 | "csascii", |
| 111 | "ibm367", |
| 112 | "iso646-us", |
| 113 | "iso-646.irv-1991", |
| 114 | "iso-ir-6", |
| 115 | "us", |
| 116 | "us-ascii", |
| 117 | NULL |
| 118 | }; |
| 119 | #endif |
| 120 | |
| 121 | loc = setlocale(LC_CTYPE, NULL); |
| 122 | if (loc == NULL) |
| 123 | goto error; |
| 124 | if (strcmp(loc, "C") != 0) { |
| 125 | /* the LC_CTYPE locale is different than C */ |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 130 | codeset = nl_langinfo(CODESET); |
| 131 | if (!codeset || codeset[0] == '\0') { |
| 132 | /* CODESET is not set or empty */ |
| 133 | goto error; |
| 134 | } |
| 135 | if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) |
| 136 | goto error; |
| 137 | |
| 138 | is_ascii = 0; |
| 139 | for (alias=ascii_aliases; *alias != NULL; alias++) { |
| 140 | if (strcmp(encoding, *alias) == 0) { |
| 141 | is_ascii = 1; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | if (!is_ascii) { |
| 146 | /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */ |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | for (i=0x80; i<0xff; i++) { |
| 151 | unsigned char ch; |
| 152 | wchar_t wch; |
| 153 | size_t res; |
| 154 | |
| 155 | ch = (unsigned char)i; |
| 156 | res = mbstowcs(&wch, (char*)&ch, 1); |
| 157 | if (res != (size_t)-1) { |
| 158 | /* decoding a non-ASCII character from the locale encoding succeed: |
| 159 | the locale encoding is not ASCII, force ASCII */ |
| 160 | return 1; |
| 161 | } |
| 162 | } |
| 163 | /* None of the bytes in the range 0x80-0xff can be decoded from the locale |
| 164 | encoding: the locale encoding is really ASCII */ |
| 165 | return 0; |
| 166 | #else |
| 167 | /* nl_langinfo(CODESET) is not available: always force ASCII */ |
| 168 | return 1; |
| 169 | #endif |
| 170 | |
| 171 | error: |
| 172 | /* if an error occured, force the ASCII encoding */ |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | static char* |
| 177 | encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos) |
| 178 | { |
| 179 | char *result = NULL, *out; |
| 180 | size_t len, i; |
| 181 | wchar_t ch; |
| 182 | |
| 183 | if (error_pos != NULL) |
| 184 | *error_pos = (size_t)-1; |
| 185 | |
| 186 | len = wcslen(text); |
| 187 | |
| 188 | result = PyMem_Malloc(len + 1); /* +1 for NUL byte */ |
| 189 | if (result == NULL) |
| 190 | return NULL; |
| 191 | |
| 192 | out = result; |
| 193 | for (i=0; i<len; i++) { |
| 194 | ch = text[i]; |
| 195 | |
| 196 | if (ch <= 0x7f) { |
| 197 | /* ASCII character */ |
| 198 | *out++ = (char)ch; |
| 199 | } |
| 200 | else if (0xdc80 <= ch && ch <= 0xdcff) { |
| 201 | /* UTF-8b surrogate */ |
| 202 | *out++ = (char)(ch - 0xdc00); |
| 203 | } |
| 204 | else { |
| 205 | if (error_pos != NULL) |
| 206 | *error_pos = i; |
| 207 | PyMem_Free(result); |
| 208 | return NULL; |
| 209 | } |
| 210 | } |
| 211 | *out = '\0'; |
| 212 | return result; |
| 213 | } |
| 214 | #endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */ |
| 215 | |
| 216 | #if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC)) |
| 217 | static wchar_t* |
| 218 | decode_ascii_surrogateescape(const char *arg, size_t *size) |
| 219 | { |
| 220 | wchar_t *res; |
| 221 | unsigned char *in; |
| 222 | wchar_t *out; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 223 | size_t argsize = strlen(arg) + 1; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 224 | |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 225 | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
| 226 | return NULL; |
Benjamin Peterson | 10ecaa2 | 2015-01-04 16:05:39 -0600 | [diff] [blame] | 227 | res = PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 228 | if (!res) |
| 229 | return NULL; |
| 230 | |
| 231 | in = (unsigned char*)arg; |
| 232 | out = res; |
| 233 | while(*in) |
| 234 | if(*in < 128) |
| 235 | *out++ = *in++; |
| 236 | else |
| 237 | *out++ = 0xdc00 + *in++; |
| 238 | *out = 0; |
| 239 | if (size != NULL) |
| 240 | *size = out - res; |
| 241 | return res; |
| 242 | } |
| 243 | #endif |
| 244 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 245 | |
| 246 | /* Decode a byte string from the locale encoding with the |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 247 | surrogateescape error handler: undecodable bytes are decoded as characters |
| 248 | 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] | 249 | character, escape the bytes using the surrogateescape error handler instead |
| 250 | of decoding them. |
| 251 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 252 | Return a pointer to a newly allocated wide character string, use |
| 253 | PyMem_RawFree() to free the memory. If size is not NULL, write the number of |
| 254 | wide characters excluding the null character into *size |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 255 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 256 | Return NULL on decoding error or memory allocation error. If *size* is not |
| 257 | NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on |
| 258 | decoding error. |
Victor Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 259 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 260 | Decoding errors should never happen, unless there is a bug in the C |
| 261 | library. |
| 262 | |
| 263 | Use the Py_EncodeLocale() function to encode the character string back to a |
| 264 | byte string. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 265 | wchar_t* |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 266 | Py_DecodeLocale(const char* arg, size_t *size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 267 | { |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 268 | #ifdef __APPLE__ |
| 269 | wchar_t *wstr; |
| 270 | wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg)); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 271 | if (size != NULL) { |
| 272 | if (wstr != NULL) |
| 273 | *size = wcslen(wstr); |
| 274 | else |
| 275 | *size = (size_t)-1; |
| 276 | } |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 277 | return wstr; |
| 278 | #else |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 279 | wchar_t *res; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 280 | size_t argsize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 281 | size_t count; |
Victor Stinner | 313f10c | 2013-05-07 23:48:56 +0200 | [diff] [blame] | 282 | #ifdef HAVE_MBRTOWC |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 283 | unsigned char *in; |
| 284 | wchar_t *out; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 285 | mbstate_t mbs; |
| 286 | #endif |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 287 | |
| 288 | #ifndef MS_WINDOWS |
| 289 | if (force_ascii == -1) |
| 290 | force_ascii = check_force_ascii(); |
| 291 | |
| 292 | if (force_ascii) { |
| 293 | /* force ASCII encoding to workaround mbstowcs() issue */ |
| 294 | res = decode_ascii_surrogateescape(arg, size); |
| 295 | if (res == NULL) |
| 296 | goto oom; |
| 297 | return res; |
| 298 | } |
| 299 | #endif |
| 300 | |
| 301 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 302 | /* Some platforms have a broken implementation of |
| 303 | * mbstowcs which does not count the characters that |
| 304 | * would result from conversion. Use an upper bound. |
| 305 | */ |
| 306 | argsize = strlen(arg); |
| 307 | #else |
| 308 | argsize = mbstowcs(NULL, arg, 0); |
| 309 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 310 | if (argsize != (size_t)-1) { |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 311 | if (argsize == PY_SSIZE_T_MAX) |
| 312 | goto oom; |
| 313 | argsize += 1; |
| 314 | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
| 315 | goto oom; |
Benjamin Peterson | 10ecaa2 | 2015-01-04 16:05:39 -0600 | [diff] [blame] | 316 | res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 317 | if (!res) |
| 318 | goto oom; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 319 | count = mbstowcs(res, arg, argsize); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 320 | if (count != (size_t)-1) { |
| 321 | wchar_t *tmp; |
| 322 | /* Only use the result if it contains no |
| 323 | surrogate characters. */ |
| 324 | for (tmp = res; *tmp != 0 && |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 325 | !Py_UNICODE_IS_SURROGATE(*tmp); tmp++) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 326 | ; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 327 | if (*tmp == 0) { |
| 328 | if (size != NULL) |
| 329 | *size = count; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 330 | return res; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 331 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 332 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 333 | PyMem_RawFree(res); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 334 | } |
| 335 | /* Conversion failed. Fall back to escaping with surrogateescape. */ |
| 336 | #ifdef HAVE_MBRTOWC |
| 337 | /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ |
| 338 | |
| 339 | /* Overallocate; as multi-byte characters are in the argument, the |
| 340 | actual output could use less memory. */ |
| 341 | argsize = strlen(arg) + 1; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 342 | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
| 343 | goto oom; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 344 | res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 345 | if (!res) |
| 346 | goto oom; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 347 | in = (unsigned char*)arg; |
| 348 | out = res; |
| 349 | memset(&mbs, 0, sizeof mbs); |
| 350 | while (argsize) { |
| 351 | size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); |
| 352 | if (converted == 0) |
| 353 | /* Reached end of string; null char stored. */ |
| 354 | break; |
| 355 | if (converted == (size_t)-2) { |
| 356 | /* Incomplete character. This should never happen, |
| 357 | since we provide everything that we have - |
| 358 | unless there is a bug in the C library, or I |
| 359 | misunderstood how mbrtowc works. */ |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 360 | PyMem_RawFree(res); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 361 | if (size != NULL) |
| 362 | *size = (size_t)-2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 363 | return NULL; |
| 364 | } |
| 365 | if (converted == (size_t)-1) { |
| 366 | /* Conversion error. Escape as UTF-8b, and start over |
| 367 | in the initial shift state. */ |
| 368 | *out++ = 0xdc00 + *in++; |
| 369 | argsize--; |
| 370 | memset(&mbs, 0, sizeof mbs); |
| 371 | continue; |
| 372 | } |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 373 | if (Py_UNICODE_IS_SURROGATE(*out)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 374 | /* Surrogate character. Escape the original |
| 375 | byte sequence with surrogateescape. */ |
| 376 | argsize -= converted; |
| 377 | while (converted--) |
| 378 | *out++ = 0xdc00 + *in++; |
| 379 | continue; |
| 380 | } |
| 381 | /* successfully converted some bytes */ |
| 382 | in += converted; |
| 383 | argsize -= converted; |
| 384 | out++; |
| 385 | } |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 386 | if (size != NULL) |
| 387 | *size = out - res; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 388 | #else /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 389 | /* Cannot use C locale for escaping; manually escape as if charset |
| 390 | is ASCII (i.e. escape all bytes > 128. This will still roundtrip |
| 391 | correctly in the locale's charset, which must be an ASCII superset. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 392 | res = decode_ascii_surrogateescape(arg, size); |
| 393 | if (res == NULL) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 394 | goto oom; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 395 | #endif /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 396 | return res; |
| 397 | oom: |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 398 | if (size != NULL) |
| 399 | *size = (size_t)-1; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 400 | return NULL; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 401 | #endif /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 404 | /* Encode a wide character string to the locale encoding with the |
| 405 | surrogateescape error handler: surrogate characters in the range |
| 406 | U+DC80..U+DCFF are converted to bytes 0x80..0xFF. |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 407 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 408 | Return a pointer to a newly allocated byte string, use PyMem_Free() to free |
| 409 | the memory. Return NULL on encoding or memory allocation error. |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 410 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 411 | If error_pos is not NULL, *error_pos is set to the index of the invalid |
| 412 | character on encoding error, or set to (size_t)-1 otherwise. |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 413 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 414 | Use the Py_DecodeLocale() function to decode the bytes string back to a wide |
| 415 | character string. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 416 | char* |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 417 | Py_EncodeLocale(const wchar_t *text, size_t *error_pos) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 418 | { |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 419 | #ifdef __APPLE__ |
| 420 | Py_ssize_t len; |
| 421 | PyObject *unicode, *bytes = NULL; |
| 422 | char *cpath; |
| 423 | |
| 424 | unicode = PyUnicode_FromWideChar(text, wcslen(text)); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 425 | if (unicode == NULL) |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 426 | return NULL; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 427 | |
| 428 | bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
| 429 | Py_DECREF(unicode); |
| 430 | if (bytes == NULL) { |
| 431 | PyErr_Clear(); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 432 | if (error_pos != NULL) |
| 433 | *error_pos = (size_t)-1; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | len = PyBytes_GET_SIZE(bytes); |
| 438 | cpath = PyMem_Malloc(len+1); |
| 439 | if (cpath == NULL) { |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 440 | PyErr_Clear(); |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 441 | Py_DECREF(bytes); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 442 | if (error_pos != NULL) |
| 443 | *error_pos = (size_t)-1; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 444 | return NULL; |
| 445 | } |
| 446 | memcpy(cpath, PyBytes_AsString(bytes), len + 1); |
| 447 | Py_DECREF(bytes); |
| 448 | return cpath; |
| 449 | #else /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 450 | const size_t len = wcslen(text); |
| 451 | char *result = NULL, *bytes = NULL; |
| 452 | size_t i, size, converted; |
| 453 | wchar_t c, buf[2]; |
| 454 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 455 | #ifndef MS_WINDOWS |
| 456 | if (force_ascii == -1) |
| 457 | force_ascii = check_force_ascii(); |
| 458 | |
| 459 | if (force_ascii) |
| 460 | return encode_ascii_surrogateescape(text, error_pos); |
| 461 | #endif |
| 462 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 463 | /* The function works in two steps: |
| 464 | 1. compute the length of the output buffer in bytes (size) |
| 465 | 2. outputs the bytes */ |
| 466 | size = 0; |
| 467 | buf[1] = 0; |
| 468 | while (1) { |
| 469 | for (i=0; i < len; i++) { |
| 470 | c = text[i]; |
| 471 | if (c >= 0xdc80 && c <= 0xdcff) { |
| 472 | /* UTF-8b surrogate */ |
| 473 | if (bytes != NULL) { |
| 474 | *bytes++ = c - 0xdc00; |
| 475 | size--; |
| 476 | } |
| 477 | else |
| 478 | size++; |
| 479 | continue; |
| 480 | } |
| 481 | else { |
| 482 | buf[0] = c; |
| 483 | if (bytes != NULL) |
| 484 | converted = wcstombs(bytes, buf, size); |
| 485 | else |
| 486 | converted = wcstombs(NULL, buf, 0); |
| 487 | if (converted == (size_t)-1) { |
| 488 | if (result != NULL) |
| 489 | PyMem_Free(result); |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 490 | if (error_pos != NULL) |
| 491 | *error_pos = i; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 492 | return NULL; |
| 493 | } |
| 494 | if (bytes != NULL) { |
| 495 | bytes += converted; |
| 496 | size -= converted; |
| 497 | } |
| 498 | else |
| 499 | size += converted; |
| 500 | } |
| 501 | } |
| 502 | if (result != NULL) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 503 | *bytes = '\0'; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 504 | break; |
| 505 | } |
| 506 | |
| 507 | size += 1; /* nul byte at the end */ |
| 508 | result = PyMem_Malloc(size); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 509 | if (result == NULL) { |
| 510 | if (error_pos != NULL) |
| 511 | *error_pos = (size_t)-1; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 512 | return NULL; |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 513 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 514 | bytes = result; |
| 515 | } |
| 516 | return result; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 517 | #endif /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 520 | /* In principle, this should use HAVE__WSTAT, and _wstat |
| 521 | should be detected by autoconf. However, no current |
| 522 | POSIX system provides that function, so testing for |
| 523 | it is pointless. |
| 524 | Not sure whether the MS_WINDOWS guards are necessary: |
| 525 | perhaps for cygwin/mingw builds? |
| 526 | */ |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 527 | #if defined(HAVE_STAT) && !defined(MS_WINDOWS) |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 528 | |
| 529 | /* Get file status. Encode the path to the locale encoding. */ |
| 530 | |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 531 | int |
| 532 | _Py_wstat(const wchar_t* path, struct stat *buf) |
| 533 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 534 | int err; |
| 535 | char *fname; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 536 | fname = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 537 | if (fname == NULL) { |
| 538 | errno = EINVAL; |
| 539 | return -1; |
| 540 | } |
| 541 | err = stat(fname, buf); |
| 542 | PyMem_Free(fname); |
| 543 | return err; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 544 | } |
| 545 | #endif |
| 546 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 547 | |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 548 | #if defined(HAVE_FSTAT) || defined(MS_WINDOWS) |
| 549 | |
| 550 | #ifdef MS_WINDOWS |
| 551 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 552 | |
| 553 | static void |
| 554 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) |
| 555 | { |
| 556 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 557 | /* Cannot simply cast and dereference in_ptr, |
| 558 | since it might not be aligned properly */ |
| 559 | __int64 in; |
| 560 | memcpy(&in, in_ptr, sizeof(in)); |
| 561 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 562 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); |
| 563 | } |
| 564 | |
| 565 | void |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame^] | 566 | _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] | 567 | { |
| 568 | /* XXX endianness */ |
| 569 | __int64 out; |
| 570 | out = time_in + secs_between_epochs; |
| 571 | out = out * 10000000 + nsec_in / 100; |
| 572 | memcpy(out_ptr, &out, sizeof(out)); |
| 573 | } |
| 574 | |
| 575 | /* Below, we *know* that ugo+r is 0444 */ |
| 576 | #if _S_IREAD != 0400 |
| 577 | #error Unsupported C library |
| 578 | #endif |
| 579 | static int |
| 580 | attributes_to_mode(DWORD attr) |
| 581 | { |
| 582 | int m = 0; |
| 583 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 584 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 585 | else |
| 586 | m |= _S_IFREG; |
| 587 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 588 | m |= 0444; |
| 589 | else |
| 590 | m |= 0666; |
| 591 | return m; |
| 592 | } |
| 593 | |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame^] | 594 | void |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 595 | _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result) |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 596 | { |
| 597 | memset(result, 0, sizeof(*result)); |
| 598 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 599 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 600 | result->st_dev = info->dwVolumeSerialNumber; |
| 601 | result->st_rdev = result->st_dev; |
| 602 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 603 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 604 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 605 | result->st_nlink = info->nNumberOfLinks; |
| 606 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
| 607 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 608 | /* first clear the S_IFMT bits */ |
| 609 | result->st_mode ^= (result->st_mode & S_IFMT); |
| 610 | /* now set the bits that make this a symlink */ |
| 611 | result->st_mode |= S_IFLNK; |
| 612 | } |
| 613 | result->st_file_attributes = info->dwFileAttributes; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 614 | } |
| 615 | #endif |
| 616 | |
| 617 | /* Return information about a file. |
| 618 | |
| 619 | On POSIX, use fstat(). |
| 620 | |
| 621 | On Windows, use GetFileType() and GetFileInformationByHandle() which support |
| 622 | files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger |
| 623 | than 2 GB because the file size type is an signed 32-bit integer: see issue |
| 624 | #23152. |
| 625 | */ |
| 626 | int |
| 627 | _Py_fstat(int fd, struct _Py_stat_struct *result) |
| 628 | { |
| 629 | #ifdef MS_WINDOWS |
| 630 | BY_HANDLE_FILE_INFORMATION info; |
| 631 | HANDLE h; |
| 632 | int type; |
| 633 | |
| 634 | if (!_PyVerify_fd(fd)) |
| 635 | h = INVALID_HANDLE_VALUE; |
| 636 | else |
| 637 | h = (HANDLE)_get_osfhandle(fd); |
| 638 | |
| 639 | /* Protocol violation: we explicitly clear errno, instead of |
| 640 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 641 | errno = 0; |
| 642 | |
| 643 | if (h == INVALID_HANDLE_VALUE) { |
| 644 | /* This is really a C library error (invalid file handle). |
| 645 | We set the Win32 error to the closes one matching. */ |
| 646 | SetLastError(ERROR_INVALID_HANDLE); |
| 647 | return -1; |
| 648 | } |
| 649 | memset(result, 0, sizeof(*result)); |
| 650 | |
| 651 | type = GetFileType(h); |
| 652 | if (type == FILE_TYPE_UNKNOWN) { |
| 653 | DWORD error = GetLastError(); |
| 654 | if (error != 0) { |
| 655 | return -1; |
| 656 | } |
| 657 | /* else: valid but unknown file */ |
| 658 | } |
| 659 | |
| 660 | if (type != FILE_TYPE_DISK) { |
| 661 | if (type == FILE_TYPE_CHAR) |
| 662 | result->st_mode = _S_IFCHR; |
| 663 | else if (type == FILE_TYPE_PIPE) |
| 664 | result->st_mode = _S_IFIFO; |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | if (!GetFileInformationByHandle(h, &info)) { |
| 669 | return -1; |
| 670 | } |
| 671 | |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 672 | _Py_attribute_data_to_stat(&info, 0, result); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 673 | /* specific to fstat() */ |
| 674 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 675 | return 0; |
| 676 | #else |
| 677 | return fstat(fd, result); |
| 678 | #endif |
| 679 | } |
| 680 | #endif /* HAVE_FSTAT || MS_WINDOWS */ |
| 681 | |
| 682 | |
| 683 | #ifdef HAVE_STAT |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 684 | /* Call _wstat() on Windows, or encode the path to the filesystem encoding and |
| 685 | call stat() otherwise. Only fill st_mode attribute on Windows. |
| 686 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 687 | Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was |
| 688 | raised. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 689 | |
| 690 | int |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 691 | _Py_stat(PyObject *path, struct stat *statbuf) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 692 | { |
| 693 | #ifdef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 694 | int err; |
| 695 | struct _stat wstatbuf; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 696 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 697 | |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 698 | wpath = PyUnicode_AsUnicode(path); |
| 699 | if (wpath == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 700 | return -2; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 701 | err = _wstat(wpath, &wstatbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 702 | if (!err) |
| 703 | statbuf->st_mode = wstatbuf.st_mode; |
| 704 | return err; |
| 705 | #else |
| 706 | int ret; |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 707 | PyObject *bytes = PyUnicode_EncodeFSDefault(path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 708 | if (bytes == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 709 | return -2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 710 | ret = stat(PyBytes_AS_STRING(bytes), statbuf); |
| 711 | Py_DECREF(bytes); |
| 712 | return ret; |
| 713 | #endif |
| 714 | } |
| 715 | |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 716 | #endif /* HAVE_STAT */ |
| 717 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 718 | |
Antoine Pitrou | 409b538 | 2013-10-12 22:41:17 +0200 | [diff] [blame] | 719 | static int |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 720 | get_inheritable(int fd, int raise) |
| 721 | { |
| 722 | #ifdef MS_WINDOWS |
| 723 | HANDLE handle; |
| 724 | DWORD flags; |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 725 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 726 | if (!_PyVerify_fd(fd)) { |
| 727 | if (raise) |
| 728 | PyErr_SetFromErrno(PyExc_OSError); |
| 729 | return -1; |
| 730 | } |
| 731 | |
| 732 | handle = (HANDLE)_get_osfhandle(fd); |
| 733 | if (handle == INVALID_HANDLE_VALUE) { |
| 734 | if (raise) |
| 735 | PyErr_SetFromWindowsErr(0); |
| 736 | return -1; |
| 737 | } |
| 738 | |
| 739 | if (!GetHandleInformation(handle, &flags)) { |
| 740 | if (raise) |
| 741 | PyErr_SetFromWindowsErr(0); |
| 742 | return -1; |
| 743 | } |
| 744 | |
| 745 | return (flags & HANDLE_FLAG_INHERIT); |
| 746 | #else |
| 747 | int flags; |
| 748 | |
| 749 | flags = fcntl(fd, F_GETFD, 0); |
| 750 | if (flags == -1) { |
| 751 | if (raise) |
| 752 | PyErr_SetFromErrno(PyExc_OSError); |
| 753 | return -1; |
| 754 | } |
| 755 | return !(flags & FD_CLOEXEC); |
| 756 | #endif |
| 757 | } |
| 758 | |
| 759 | /* Get the inheritable flag of the specified file descriptor. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 760 | 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] | 761 | raise an exception and return -1 on error. */ |
| 762 | int |
| 763 | _Py_get_inheritable(int fd) |
| 764 | { |
| 765 | return get_inheritable(fd, 1); |
| 766 | } |
| 767 | |
| 768 | static int |
| 769 | set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) |
| 770 | { |
| 771 | #ifdef MS_WINDOWS |
| 772 | HANDLE handle; |
| 773 | DWORD flags; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 774 | #else |
| 775 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 776 | static int ioctl_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 777 | int request; |
| 778 | int err; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 779 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 780 | int flags; |
| 781 | int res; |
| 782 | #endif |
| 783 | |
| 784 | /* atomic_flag_works can only be used to make the file descriptor |
| 785 | non-inheritable */ |
| 786 | assert(!(atomic_flag_works != NULL && inheritable)); |
| 787 | |
| 788 | if (atomic_flag_works != NULL && !inheritable) { |
| 789 | if (*atomic_flag_works == -1) { |
| 790 | int inheritable = get_inheritable(fd, raise); |
| 791 | if (inheritable == -1) |
| 792 | return -1; |
| 793 | *atomic_flag_works = !inheritable; |
| 794 | } |
| 795 | |
| 796 | if (*atomic_flag_works) |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | #ifdef MS_WINDOWS |
| 801 | if (!_PyVerify_fd(fd)) { |
| 802 | if (raise) |
| 803 | PyErr_SetFromErrno(PyExc_OSError); |
| 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | handle = (HANDLE)_get_osfhandle(fd); |
| 808 | if (handle == INVALID_HANDLE_VALUE) { |
| 809 | if (raise) |
| 810 | PyErr_SetFromWindowsErr(0); |
| 811 | return -1; |
| 812 | } |
| 813 | |
| 814 | if (inheritable) |
| 815 | flags = HANDLE_FLAG_INHERIT; |
| 816 | else |
| 817 | flags = 0; |
| 818 | if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) { |
| 819 | if (raise) |
| 820 | PyErr_SetFromWindowsErr(0); |
| 821 | return -1; |
| 822 | } |
| 823 | return 0; |
| 824 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 825 | #else |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 826 | |
| 827 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 828 | if (ioctl_works != 0) { |
| 829 | /* fast-path: ioctl() only requires one syscall */ |
| 830 | if (inheritable) |
| 831 | request = FIONCLEX; |
| 832 | else |
| 833 | request = FIOCLEX; |
| 834 | err = ioctl(fd, request, NULL); |
| 835 | if (!err) { |
| 836 | ioctl_works = 1; |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | if (errno != ENOTTY) { |
| 841 | if (raise) |
| 842 | PyErr_SetFromErrno(PyExc_OSError); |
| 843 | return -1; |
| 844 | } |
| 845 | else { |
| 846 | /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for |
| 847 | device". The ioctl is declared but not supported by the kernel. |
| 848 | Remember that ioctl() doesn't work. It is the case on |
| 849 | Illumos-based OS for example. */ |
| 850 | ioctl_works = 0; |
| 851 | } |
| 852 | /* fallback to fcntl() if ioctl() does not work */ |
| 853 | } |
| 854 | #endif |
| 855 | |
| 856 | /* slow-path: fcntl() requires two syscalls */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 857 | flags = fcntl(fd, F_GETFD); |
| 858 | if (flags < 0) { |
| 859 | if (raise) |
| 860 | PyErr_SetFromErrno(PyExc_OSError); |
| 861 | return -1; |
| 862 | } |
| 863 | |
| 864 | if (inheritable) |
| 865 | flags &= ~FD_CLOEXEC; |
| 866 | else |
| 867 | flags |= FD_CLOEXEC; |
| 868 | res = fcntl(fd, F_SETFD, flags); |
| 869 | if (res < 0) { |
| 870 | if (raise) |
| 871 | PyErr_SetFromErrno(PyExc_OSError); |
| 872 | return -1; |
| 873 | } |
| 874 | return 0; |
| 875 | #endif |
| 876 | } |
| 877 | |
| 878 | /* Make the file descriptor non-inheritable. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 879 | Return 0 on success, set errno and return -1 on error. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 880 | static int |
| 881 | make_non_inheritable(int fd) |
| 882 | { |
| 883 | return set_inheritable(fd, 0, 0, NULL); |
| 884 | } |
| 885 | |
| 886 | /* Set the inheritable flag of the specified file descriptor. |
| 887 | On success: return 0, on error: raise an exception if raise is nonzero |
| 888 | and return -1. |
| 889 | |
| 890 | If atomic_flag_works is not NULL: |
| 891 | |
| 892 | * if *atomic_flag_works==-1, check if the inheritable is set on the file |
| 893 | descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and |
| 894 | set the inheritable flag |
| 895 | * if *atomic_flag_works==1: do nothing |
| 896 | * if *atomic_flag_works==0: set inheritable flag to False |
| 897 | |
| 898 | Set atomic_flag_works to NULL if no atomic flag was used to create the |
| 899 | file descriptor. |
| 900 | |
| 901 | atomic_flag_works can only be used to make a file descriptor |
| 902 | non-inheritable: atomic_flag_works must be NULL if inheritable=1. */ |
| 903 | int |
| 904 | _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) |
| 905 | { |
| 906 | return set_inheritable(fd, inheritable, 1, atomic_flag_works); |
| 907 | } |
| 908 | |
| 909 | /* Open a file with the specified flags (wrapper to open() function). |
| 910 | The file descriptor is created non-inheritable. */ |
| 911 | int |
| 912 | _Py_open(const char *pathname, int flags) |
| 913 | { |
| 914 | int fd; |
| 915 | #ifdef MS_WINDOWS |
| 916 | fd = open(pathname, flags | O_NOINHERIT); |
| 917 | if (fd < 0) |
| 918 | return fd; |
| 919 | #else |
| 920 | |
| 921 | int *atomic_flag_works; |
| 922 | #ifdef O_CLOEXEC |
| 923 | atomic_flag_works = &_Py_open_cloexec_works; |
| 924 | flags |= O_CLOEXEC; |
| 925 | #else |
| 926 | atomic_flag_works = NULL; |
| 927 | #endif |
| 928 | fd = open(pathname, flags); |
| 929 | if (fd < 0) |
| 930 | return fd; |
| 931 | |
| 932 | if (set_inheritable(fd, 0, 0, atomic_flag_works) < 0) { |
| 933 | close(fd); |
| 934 | return -1; |
| 935 | } |
| 936 | #endif /* !MS_WINDOWS */ |
| 937 | return fd; |
| 938 | } |
| 939 | |
| 940 | /* Open a file. Use _wfopen() on Windows, encode the path to the locale |
| 941 | encoding and use fopen() otherwise. The file descriptor is created |
| 942 | non-inheritable. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 943 | FILE * |
| 944 | _Py_wfopen(const wchar_t *path, const wchar_t *mode) |
| 945 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 946 | FILE *f; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 947 | #ifndef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 948 | char *cpath; |
| 949 | char cmode[10]; |
| 950 | size_t r; |
| 951 | r = wcstombs(cmode, mode, 10); |
| 952 | if (r == (size_t)-1 || r >= 10) { |
| 953 | errno = EINVAL; |
| 954 | return NULL; |
| 955 | } |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 956 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 957 | if (cpath == NULL) |
| 958 | return NULL; |
| 959 | f = fopen(cpath, cmode); |
| 960 | PyMem_Free(cpath); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 961 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 962 | f = _wfopen(path, mode); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 963 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 964 | if (f == NULL) |
| 965 | return NULL; |
| 966 | if (make_non_inheritable(fileno(f)) < 0) { |
| 967 | fclose(f); |
| 968 | return NULL; |
| 969 | } |
| 970 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 973 | /* Wrapper to fopen(). The file descriptor is created non-inheritable. */ |
| 974 | FILE* |
| 975 | _Py_fopen(const char *pathname, const char *mode) |
| 976 | { |
| 977 | FILE *f = fopen(pathname, mode); |
| 978 | if (f == NULL) |
| 979 | return NULL; |
| 980 | if (make_non_inheritable(fileno(f)) < 0) { |
| 981 | fclose(f); |
| 982 | return NULL; |
| 983 | } |
| 984 | return f; |
| 985 | } |
| 986 | |
| 987 | /* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem |
| 988 | encoding and call fopen() otherwise. The file descriptor is created |
| 989 | non-inheritable. |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 990 | |
| 991 | Return the new file object on success, or NULL if the file cannot be open or |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 992 | (if PyErr_Occurred()) on unicode error. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 993 | FILE* |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 994 | _Py_fopen_obj(PyObject *path, const char *mode) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 995 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 996 | FILE *f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 997 | #ifdef MS_WINDOWS |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 998 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 999 | wchar_t wmode[10]; |
| 1000 | int usize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 1002 | if (!PyUnicode_Check(path)) { |
| 1003 | PyErr_Format(PyExc_TypeError, |
| 1004 | "str file path expected under Windows, got %R", |
| 1005 | Py_TYPE(path)); |
| 1006 | return NULL; |
| 1007 | } |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 1008 | wpath = PyUnicode_AsUnicode(path); |
| 1009 | if (wpath == NULL) |
| 1010 | return NULL; |
| 1011 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1012 | usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); |
| 1013 | if (usize == 0) |
| 1014 | return NULL; |
| 1015 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1016 | f = _wfopen(wpath, wmode); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1017 | #else |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 1018 | PyObject *bytes; |
| 1019 | if (!PyUnicode_FSConverter(path, &bytes)) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1020 | return NULL; |
| 1021 | f = fopen(PyBytes_AS_STRING(bytes), mode); |
| 1022 | Py_DECREF(bytes); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1023 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1024 | if (f == NULL) |
| 1025 | return NULL; |
| 1026 | if (make_non_inheritable(fileno(f)) < 0) { |
| 1027 | fclose(f); |
| 1028 | return NULL; |
| 1029 | } |
| 1030 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | #ifdef HAVE_READLINK |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1034 | |
| 1035 | /* 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] | 1036 | the result from the locale encoding. Return -1 on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1037 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1038 | int |
| 1039 | _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) |
| 1040 | { |
| 1041 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1042 | char cbuf[MAXPATHLEN]; |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1043 | wchar_t *wbuf; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1044 | int res; |
| 1045 | size_t r1; |
| 1046 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1047 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1048 | if (cpath == NULL) { |
| 1049 | errno = EINVAL; |
| 1050 | return -1; |
| 1051 | } |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1052 | res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf)); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1053 | PyMem_Free(cpath); |
| 1054 | if (res == -1) |
| 1055 | return -1; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1056 | if (res == Py_ARRAY_LENGTH(cbuf)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1057 | errno = EINVAL; |
| 1058 | return -1; |
| 1059 | } |
| 1060 | cbuf[res] = '\0'; /* buf will be null terminated */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1061 | wbuf = Py_DecodeLocale(cbuf, &r1); |
Victor Stinner | 350147b | 2010-10-16 22:52:09 +0000 | [diff] [blame] | 1062 | if (wbuf == NULL) { |
| 1063 | errno = EINVAL; |
| 1064 | return -1; |
| 1065 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1066 | if (bufsiz <= r1) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1067 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1068 | errno = EINVAL; |
| 1069 | return -1; |
| 1070 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1071 | wcsncpy(buf, wbuf, bufsiz); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1072 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1073 | return (int)r1; |
| 1074 | } |
| 1075 | #endif |
| 1076 | |
| 1077 | #ifdef HAVE_REALPATH |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1078 | |
| 1079 | /* Return the canonicalized absolute pathname. Encode path to the locale |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1080 | encoding, decode the result from the locale encoding. |
| 1081 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1082 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1083 | wchar_t* |
Victor Stinner | 015f4d8 | 2010-10-07 22:29:53 +0000 | [diff] [blame] | 1084 | _Py_wrealpath(const wchar_t *path, |
| 1085 | wchar_t *resolved_path, size_t resolved_path_size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1086 | { |
| 1087 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1088 | char cresolved_path[MAXPATHLEN]; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1089 | wchar_t *wresolved_path; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1090 | char *res; |
| 1091 | size_t r; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1092 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1093 | if (cpath == NULL) { |
| 1094 | errno = EINVAL; |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | res = realpath(cpath, cresolved_path); |
| 1098 | PyMem_Free(cpath); |
| 1099 | if (res == NULL) |
| 1100 | return NULL; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1101 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1102 | wresolved_path = Py_DecodeLocale(cresolved_path, &r); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1103 | if (wresolved_path == NULL) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1104 | errno = EINVAL; |
| 1105 | return NULL; |
| 1106 | } |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1107 | if (resolved_path_size <= r) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1108 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1109 | errno = EINVAL; |
| 1110 | return NULL; |
| 1111 | } |
| 1112 | wcsncpy(resolved_path, wresolved_path, resolved_path_size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1113 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1114 | return resolved_path; |
| 1115 | } |
| 1116 | #endif |
| 1117 | |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1118 | /* Get the current directory. size is the buffer size in wide characters |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1119 | including the null character. Decode the path from the locale encoding. |
| 1120 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1121 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1122 | wchar_t* |
| 1123 | _Py_wgetcwd(wchar_t *buf, size_t size) |
| 1124 | { |
| 1125 | #ifdef MS_WINDOWS |
Victor Stinner | 56785ea | 2013-06-05 00:46:29 +0200 | [diff] [blame] | 1126 | int isize = (int)Py_MIN(size, INT_MAX); |
| 1127 | return _wgetcwd(buf, isize); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1128 | #else |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1129 | char fname[MAXPATHLEN]; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1130 | wchar_t *wname; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1131 | size_t len; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1132 | |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1133 | if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1134 | return NULL; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1135 | wname = Py_DecodeLocale(fname, &len); |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1136 | if (wname == NULL) |
| 1137 | return NULL; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1138 | if (size <= len) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1139 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1140 | return NULL; |
| 1141 | } |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1142 | wcsncpy(buf, wname, size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1143 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1144 | return buf; |
| 1145 | #endif |
| 1146 | } |
| 1147 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1148 | /* Duplicate a file descriptor. The new file descriptor is created as |
| 1149 | non-inheritable. Return a new file descriptor on success, raise an OSError |
| 1150 | exception and return -1 on error. |
| 1151 | |
| 1152 | The GIL is released to call dup(). The caller must hold the GIL. */ |
| 1153 | int |
| 1154 | _Py_dup(int fd) |
| 1155 | { |
| 1156 | #ifdef MS_WINDOWS |
| 1157 | HANDLE handle; |
| 1158 | DWORD ftype; |
| 1159 | #endif |
| 1160 | |
| 1161 | if (!_PyVerify_fd(fd)) { |
| 1162 | PyErr_SetFromErrno(PyExc_OSError); |
| 1163 | return -1; |
| 1164 | } |
| 1165 | |
| 1166 | #ifdef MS_WINDOWS |
| 1167 | handle = (HANDLE)_get_osfhandle(fd); |
| 1168 | if (handle == INVALID_HANDLE_VALUE) { |
| 1169 | PyErr_SetFromWindowsErr(0); |
| 1170 | return -1; |
| 1171 | } |
| 1172 | |
| 1173 | /* get the file type, ignore the error if it failed */ |
| 1174 | ftype = GetFileType(handle); |
| 1175 | |
| 1176 | Py_BEGIN_ALLOW_THREADS |
| 1177 | fd = dup(fd); |
| 1178 | Py_END_ALLOW_THREADS |
| 1179 | if (fd < 0) { |
| 1180 | PyErr_SetFromErrno(PyExc_OSError); |
| 1181 | return -1; |
| 1182 | } |
| 1183 | |
| 1184 | /* Character files like console cannot be make non-inheritable */ |
| 1185 | if (ftype != FILE_TYPE_CHAR) { |
| 1186 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
| 1187 | close(fd); |
| 1188 | return -1; |
| 1189 | } |
| 1190 | } |
| 1191 | #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC) |
| 1192 | Py_BEGIN_ALLOW_THREADS |
| 1193 | fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 1194 | Py_END_ALLOW_THREADS |
| 1195 | if (fd < 0) { |
| 1196 | PyErr_SetFromErrno(PyExc_OSError); |
| 1197 | return -1; |
| 1198 | } |
| 1199 | |
| 1200 | #else |
| 1201 | Py_BEGIN_ALLOW_THREADS |
| 1202 | fd = dup(fd); |
| 1203 | Py_END_ALLOW_THREADS |
| 1204 | if (fd < 0) { |
| 1205 | PyErr_SetFromErrno(PyExc_OSError); |
| 1206 | return -1; |
| 1207 | } |
| 1208 | |
| 1209 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
| 1210 | close(fd); |
| 1211 | return -1; |
| 1212 | } |
| 1213 | #endif |
| 1214 | return fd; |
| 1215 | } |
| 1216 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1217 | #ifndef MS_WINDOWS |
| 1218 | /* Get the blocking mode of the file descriptor. |
| 1219 | Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared, |
| 1220 | raise an exception and return -1 on error. */ |
| 1221 | int |
| 1222 | _Py_get_blocking(int fd) |
| 1223 | { |
| 1224 | int flags = fcntl(fd, F_GETFL, 0); |
| 1225 | if (flags < 0) { |
| 1226 | PyErr_SetFromErrno(PyExc_OSError); |
| 1227 | return -1; |
| 1228 | } |
| 1229 | |
| 1230 | return !(flags & O_NONBLOCK); |
| 1231 | } |
| 1232 | |
| 1233 | /* Set the blocking mode of the specified file descriptor. |
| 1234 | |
| 1235 | Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag |
| 1236 | otherwise. |
| 1237 | |
| 1238 | Return 0 on success, raise an exception and return -1 on error. */ |
| 1239 | int |
| 1240 | _Py_set_blocking(int fd, int blocking) |
| 1241 | { |
| 1242 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO) |
| 1243 | int arg = !blocking; |
| 1244 | if (ioctl(fd, FIONBIO, &arg) < 0) |
| 1245 | goto error; |
| 1246 | #else |
| 1247 | int flags, res; |
| 1248 | |
| 1249 | flags = fcntl(fd, F_GETFL, 0); |
| 1250 | if (flags < 0) |
| 1251 | goto error; |
| 1252 | |
| 1253 | if (blocking) |
| 1254 | flags = flags & (~O_NONBLOCK); |
| 1255 | else |
| 1256 | flags = flags | O_NONBLOCK; |
| 1257 | |
| 1258 | res = fcntl(fd, F_SETFL, flags); |
| 1259 | if (res < 0) |
| 1260 | goto error; |
| 1261 | #endif |
| 1262 | return 0; |
| 1263 | |
| 1264 | error: |
| 1265 | PyErr_SetFromErrno(PyExc_OSError); |
| 1266 | return -1; |
| 1267 | } |
| 1268 | #endif |
| 1269 | |