blob: e7111c1431111c77f0e53811cd9382b88c0583da [file] [log] [blame]
Victor Stinner4e314432010-10-07 21:45:39 +00001#include "Python.h"
Stefan Krah6df5cae2012-11-12 20:14:36 +01002#include "osdefs.h"
Stefan Krah6c01e382014-01-20 15:31:08 +01003#include <locale.h>
4
Victor Stinnerb306d752010-10-07 22:09:40 +00005#ifdef MS_WINDOWS
6# include <windows.h>
7#endif
Victor Stinner4e314432010-10-07 21:45:39 +00008
Brett Cannonefb00c02012-02-29 18:31:31 -05009#ifdef HAVE_LANGINFO_H
10#include <langinfo.h>
11#endif
12
Victor Stinnerdaf45552013-08-28 00:53:59 +020013#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 Stinnere2623772012-11-12 23:04:02 +010021#ifdef __APPLE__
22extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
23#endif
24
Victor Stinnerdaf45552013-08-28 00:53:59 +020025#ifdef O_CLOEXEC
Victor Stinnerb034eee2013-09-07 10:36:04 +020026/* Does open() support the O_CLOEXEC flag? Possible values:
Victor Stinnerdaf45552013-08-28 00:53:59 +020027
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() */
33int _Py_open_cloexec_works = -1;
34#endif
35
Brett Cannonefb00c02012-02-29 18:31:31 -050036PyObject *
37_Py_device_encoding(int fd)
38{
Victor Stinner14b9b112013-06-25 00:37:25 +020039#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050040 UINT cp;
41#endif
42 if (!_PyVerify_fd(fd) || !isatty(fd)) {
43 Py_RETURN_NONE;
44 }
Victor Stinner14b9b112013-06-25 00:37:25 +020045#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050046 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 Stinnerd45c7f82012-12-04 01:34:47 +010066#if !defined(__APPLE__) && !defined(MS_WINDOWS)
67extern 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 Jenvey215c49a2013-01-15 13:24:12 -080083 Values of force_ascii:
Victor Stinnerd45c7f82012-12-04 01:34:47 +010084
Victor Stinnerf6a271a2014-08-01 12:28:48 +020085 1: the workaround is used: Py_EncodeLocale() uses
86 encode_ascii_surrogateescape() and Py_DecodeLocale() uses
Victor Stinnerd45c7f82012-12-04 01:34:47 +010087 decode_ascii_surrogateescape()
Victor Stinnerf6a271a2014-08-01 12:28:48 +020088 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
89 Py_DecodeLocale() uses mbstowcs()
Victor Stinnerd45c7f82012-12-04 01:34:47 +010090 -1: unknown, need to call check_force_ascii() to get the value
91*/
92static int force_ascii = -1;
93
94static int
95check_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
171error:
172 /* if an error occured, force the ASCII encoding */
173 return 1;
174}
175
176static char*
177encode_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))
217static wchar_t*
218decode_ascii_surrogateescape(const char *arg, size_t *size)
219{
220 wchar_t *res;
221 unsigned char *in;
222 wchar_t *out;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600223 size_t argsize = strlen(arg) + 1;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100224
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600225 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
226 return NULL;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600227 res = PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100228 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 Stinner4e314432010-10-07 21:45:39 +0000245
246/* Decode a byte string from the locale encoding with the
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200247 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 Stinner4e314432010-10-07 21:45:39 +0000249 character, escape the bytes using the surrogateescape error handler instead
250 of decoding them.
251
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200252 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 Stinner4e314432010-10-07 21:45:39 +0000255
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200256 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 Stinner19de4c32010-11-08 23:30:46 +0000259
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200260 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 Stinner4e314432010-10-07 21:45:39 +0000265wchar_t*
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200266Py_DecodeLocale(const char* arg, size_t *size)
Victor Stinner4e314432010-10-07 21:45:39 +0000267{
Victor Stinnere2623772012-11-12 23:04:02 +0100268#ifdef __APPLE__
269 wchar_t *wstr;
270 wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100271 if (size != NULL) {
272 if (wstr != NULL)
273 *size = wcslen(wstr);
274 else
275 *size = (size_t)-1;
276 }
Victor Stinnere2623772012-11-12 23:04:02 +0100277 return wstr;
278#else
Victor Stinner4e314432010-10-07 21:45:39 +0000279 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100280 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000281 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200282#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000283 unsigned char *in;
284 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000285 mbstate_t mbs;
286#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100287
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 Stinner4e314432010-10-07 21:45:39 +0000310 if (argsize != (size_t)-1) {
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600311 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 Peterson10ecaa22015-01-04 16:05:39 -0600316 res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner4e314432010-10-07 21:45:39 +0000317 if (!res)
318 goto oom;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600319 count = mbstowcs(res, arg, argsize);
Victor Stinner4e314432010-10-07 21:45:39 +0000320 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 Stinner76df43d2012-10-30 01:42:39 +0100325 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000326 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000327 if (*tmp == 0) {
328 if (size != NULL)
329 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000330 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000331 }
Victor Stinner4e314432010-10-07 21:45:39 +0000332 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200333 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000334 }
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 Petersonf18bf6f2015-01-04 16:03:17 -0600342 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
343 goto oom;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200344 res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000345 if (!res)
346 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000347 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 Stinner1a7425f2013-07-07 16:25:15 +0200360 PyMem_RawFree(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100361 if (size != NULL)
362 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000363 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 Stinner76df43d2012-10-30 01:42:39 +0100373 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner4e314432010-10-07 21:45:39 +0000374 /* 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 Stinnerd45c7f82012-12-04 01:34:47 +0100386 if (size != NULL)
387 *size = out - res;
Victor Stinnere2623772012-11-12 23:04:02 +0100388#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000389 /* 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 Stinnerd45c7f82012-12-04 01:34:47 +0100392 res = decode_ascii_surrogateescape(arg, size);
393 if (res == NULL)
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100394 goto oom;
Victor Stinnere2623772012-11-12 23:04:02 +0100395#endif /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000396 return res;
397oom:
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100398 if (size != NULL)
399 *size = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000400 return NULL;
Victor Stinnere2623772012-11-12 23:04:02 +0100401#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000402}
403
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200404/* 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 Stinner4e314432010-10-07 21:45:39 +0000407
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200408 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 Stinner4e314432010-10-07 21:45:39 +0000410
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200411 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 Stinner2f02a512010-11-08 22:43:46 +0000413
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200414 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
415 character string. */
Victor Stinner4e314432010-10-07 21:45:39 +0000416char*
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200417Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
Victor Stinner4e314432010-10-07 21:45:39 +0000418{
Victor Stinnere2623772012-11-12 23:04:02 +0100419#ifdef __APPLE__
420 Py_ssize_t len;
421 PyObject *unicode, *bytes = NULL;
422 char *cpath;
423
424 unicode = PyUnicode_FromWideChar(text, wcslen(text));
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100425 if (unicode == NULL)
Victor Stinnere2623772012-11-12 23:04:02 +0100426 return NULL;
Victor Stinnere2623772012-11-12 23:04:02 +0100427
428 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
429 Py_DECREF(unicode);
430 if (bytes == NULL) {
431 PyErr_Clear();
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100432 if (error_pos != NULL)
433 *error_pos = (size_t)-1;
Victor Stinnere2623772012-11-12 23:04:02 +0100434 return NULL;
435 }
436
437 len = PyBytes_GET_SIZE(bytes);
438 cpath = PyMem_Malloc(len+1);
439 if (cpath == NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100440 PyErr_Clear();
Victor Stinnere2623772012-11-12 23:04:02 +0100441 Py_DECREF(bytes);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100442 if (error_pos != NULL)
443 *error_pos = (size_t)-1;
Victor Stinnere2623772012-11-12 23:04:02 +0100444 return NULL;
445 }
446 memcpy(cpath, PyBytes_AsString(bytes), len + 1);
447 Py_DECREF(bytes);
448 return cpath;
449#else /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000450 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 Stinnerd45c7f82012-12-04 01:34:47 +0100455#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 Stinner4e314432010-10-07 21:45:39 +0000463 /* 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 Stinner2f02a512010-11-08 22:43:46 +0000490 if (error_pos != NULL)
491 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000492 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 Stinnerd45c7f82012-12-04 01:34:47 +0100503 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000504 break;
505 }
506
507 size += 1; /* nul byte at the end */
508 result = PyMem_Malloc(size);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100509 if (result == NULL) {
510 if (error_pos != NULL)
511 *error_pos = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000512 return NULL;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100513 }
Victor Stinner4e314432010-10-07 21:45:39 +0000514 bytes = result;
515 }
516 return result;
Victor Stinnere2623772012-11-12 23:04:02 +0100517#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000518}
519
Victor Stinner4e314432010-10-07 21:45:39 +0000520/* 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 Stinnerb306d752010-10-07 22:09:40 +0000527#if defined(HAVE_STAT) && !defined(MS_WINDOWS)
Victor Stinner6672d0c2010-10-07 22:53:43 +0000528
529/* Get file status. Encode the path to the locale encoding. */
530
Victor Stinnerb306d752010-10-07 22:09:40 +0000531int
532_Py_wstat(const wchar_t* path, struct stat *buf)
533{
Victor Stinner4e314432010-10-07 21:45:39 +0000534 int err;
535 char *fname;
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200536 fname = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000537 if (fname == NULL) {
538 errno = EINVAL;
539 return -1;
540 }
541 err = stat(fname, buf);
542 PyMem_Free(fname);
543 return err;
Victor Stinner4e314432010-10-07 21:45:39 +0000544}
545#endif
546
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100547
Steve Dowerf2f373f2015-02-21 08:44:05 -0800548#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
549
550#ifdef MS_WINDOWS
551static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
552
553static void
554FILE_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
565void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800566_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800567{
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
579static int
580attributes_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 Dowerbf1f3762015-02-21 15:26:02 -0800594void
Steve Dowera2af1a52015-02-21 10:04:10 -0800595_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800596{
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 Dowerf2f373f2015-02-21 08:44:05 -0800614}
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 */
626int
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 Dowera2af1a52015-02-21 10:04:10 -0800672 _Py_attribute_data_to_stat(&info, 0, result);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800673 /* 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 Stinner6672d0c2010-10-07 22:53:43 +0000684/* 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 Stinnerbd0850b2011-12-18 20:47:30 +0100687 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
688 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000689
690int
Victor Stinnera4a75952010-10-07 22:23:10 +0000691_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000692{
693#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000694 int err;
695 struct _stat wstatbuf;
Victor Stinneree587ea2011-11-17 00:51:38 +0100696 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000697
Victor Stinneree587ea2011-11-17 00:51:38 +0100698 wpath = PyUnicode_AsUnicode(path);
699 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100700 return -2;
Victor Stinneree587ea2011-11-17 00:51:38 +0100701 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000702 if (!err)
703 statbuf->st_mode = wstatbuf.st_mode;
704 return err;
705#else
706 int ret;
Victor Stinnera4a75952010-10-07 22:23:10 +0000707 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000708 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100709 return -2;
Victor Stinner4e314432010-10-07 21:45:39 +0000710 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
711 Py_DECREF(bytes);
712 return ret;
713#endif
714}
715
Steve Dowerf2f373f2015-02-21 08:44:05 -0800716#endif /* HAVE_STAT */
717
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100718
Antoine Pitrou409b5382013-10-12 22:41:17 +0200719static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200720get_inheritable(int fd, int raise)
721{
722#ifdef MS_WINDOWS
723 HANDLE handle;
724 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000725
Victor Stinnerdaf45552013-08-28 00:53:59 +0200726 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 Stinnerb034eee2013-09-07 10:36:04 +0200760 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200761 raise an exception and return -1 on error. */
762int
763_Py_get_inheritable(int fd)
764{
765 return get_inheritable(fd, 1);
766}
767
768static int
769set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
770{
771#ifdef MS_WINDOWS
772 HANDLE handle;
773 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200774#else
775#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
776 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200777 int request;
778 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200779#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200780 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 Stinnerdaf45552013-08-28 00:53:59 +0200825#else
Victor Stinner282124b2014-09-02 11:41:04 +0200826
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 Stinnerdaf45552013-08-28 00:53:59 +0200857 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 Stinnerb034eee2013-09-07 10:36:04 +0200879 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200880static int
881make_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. */
903int
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. */
911int
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 Stinner4e314432010-10-07 21:45:39 +0000943FILE *
944_Py_wfopen(const wchar_t *path, const wchar_t *mode)
945{
Victor Stinner4e314432010-10-07 21:45:39 +0000946 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200947#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000948 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 Stinnerf6a271a2014-08-01 12:28:48 +0200956 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000957 if (cpath == NULL)
958 return NULL;
959 f = fopen(cpath, cmode);
960 PyMem_Free(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +0000961#else
Victor Stinnerdaf45552013-08-28 00:53:59 +0200962 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +0000963#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200964 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 Stinner4e314432010-10-07 21:45:39 +0000971}
972
Victor Stinnerdaf45552013-08-28 00:53:59 +0200973/* Wrapper to fopen(). The file descriptor is created non-inheritable. */
974FILE*
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 Stinner6672d0c2010-10-07 22:53:43 +0000990
991 Return the new file object on success, or NULL if the file cannot be open or
Victor Stinnerdaf45552013-08-28 00:53:59 +0200992 (if PyErr_Occurred()) on unicode error. */
Victor Stinner4e314432010-10-07 21:45:39 +0000993FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +0200994_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +0000995{
Victor Stinnerdaf45552013-08-28 00:53:59 +0200996 FILE *f;
Victor Stinner4e314432010-10-07 21:45:39 +0000997#ifdef MS_WINDOWS
Victor Stinneree587ea2011-11-17 00:51:38 +0100998 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000999 wchar_t wmode[10];
1000 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001001
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001002 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 Stinneree587ea2011-11-17 00:51:38 +01001008 wpath = PyUnicode_AsUnicode(path);
1009 if (wpath == NULL)
1010 return NULL;
1011
Victor Stinner4e314432010-10-07 21:45:39 +00001012 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
1013 if (usize == 0)
1014 return NULL;
1015
Victor Stinnerdaf45552013-08-28 00:53:59 +02001016 f = _wfopen(wpath, wmode);
Victor Stinner4e314432010-10-07 21:45:39 +00001017#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001018 PyObject *bytes;
1019 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001020 return NULL;
1021 f = fopen(PyBytes_AS_STRING(bytes), mode);
1022 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001023#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001024 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 Stinner4e314432010-10-07 21:45:39 +00001031}
1032
1033#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001034
1035/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001036 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001037
Victor Stinner4e314432010-10-07 21:45:39 +00001038int
1039_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1040{
1041 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001042 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001043 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001044 int res;
1045 size_t r1;
1046
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001047 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001048 if (cpath == NULL) {
1049 errno = EINVAL;
1050 return -1;
1051 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001052 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner4e314432010-10-07 21:45:39 +00001053 PyMem_Free(cpath);
1054 if (res == -1)
1055 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001056 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001057 errno = EINVAL;
1058 return -1;
1059 }
1060 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001061 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001062 if (wbuf == NULL) {
1063 errno = EINVAL;
1064 return -1;
1065 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001066 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001067 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001068 errno = EINVAL;
1069 return -1;
1070 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001071 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001072 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001073 return (int)r1;
1074}
1075#endif
1076
1077#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001078
1079/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001080 encoding, decode the result from the locale encoding.
1081 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001082
Victor Stinner4e314432010-10-07 21:45:39 +00001083wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001084_Py_wrealpath(const wchar_t *path,
1085 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001086{
1087 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001088 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001089 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001090 char *res;
1091 size_t r;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001092 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001093 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 Stinner0a1b8cb2010-10-16 22:55:47 +00001101
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001102 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001103 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001104 errno = EINVAL;
1105 return NULL;
1106 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001107 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001108 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001109 errno = EINVAL;
1110 return NULL;
1111 }
1112 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001113 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001114 return resolved_path;
1115}
1116#endif
1117
Victor Stinnerf4061da2010-10-14 12:37:19 +00001118/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001119 including the null character. Decode the path from the locale encoding.
1120 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001121
Victor Stinner4e314432010-10-07 21:45:39 +00001122wchar_t*
1123_Py_wgetcwd(wchar_t *buf, size_t size)
1124{
1125#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001126 int isize = (int)Py_MIN(size, INT_MAX);
1127 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001128#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001129 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001130 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001131 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001132
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001133 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001134 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001135 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001136 if (wname == NULL)
1137 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001138 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001139 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001140 return NULL;
1141 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001142 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001143 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001144 return buf;
1145#endif
1146}
1147
Victor Stinnerdaf45552013-08-28 00:53:59 +02001148/* 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. */
1153int
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 Stinner1db9e7b2014-07-29 22:32:47 +02001217#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. */
1221int
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. */
1239int
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
1264error:
1265 PyErr_SetFromErrno(PyExc_OSError);
1266 return -1;
1267}
1268#endif
1269