bpo-32556: nt._getfinalpathname, nt._getvolumepathname and nt._getdiskusage now correctly convert from bytes. (GH-5761)

This commit is contained in:
Steve Dower 2018-02-22 10:39:10 -08:00 committed by GitHub
parent 451d1edaf4
commit 23ad6d0d1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 110 additions and 58 deletions

View file

@ -3720,29 +3720,26 @@ os__getfullpathname_impl(PyObject *module, path_t *path)
/*[clinic input]
os._getfinalpathname
path: unicode
path: path_t
/
A helper function for samepath on windows.
[clinic start generated code]*/
static PyObject *
os__getfinalpathname_impl(PyObject *module, PyObject *path)
/*[clinic end generated code: output=9bd78d0e52782e75 input=71d5e89334891bf4]*/
os__getfinalpathname_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
{
HANDLE hFile;
int buf_size;
wchar_t *target_path;
int result_length;
PyObject *result;
const wchar_t *path_wchar;
path_wchar = _PyUnicode_AsUnicode(path);
if (path_wchar == NULL)
return NULL;
const char *err = NULL;
Py_BEGIN_ALLOW_THREADS
hFile = CreateFileW(
path_wchar,
path->wide,
0, /* desired access */
0, /* share mode */
NULL, /* security attributes */
@ -3751,32 +3748,54 @@ os__getfinalpathname_impl(PyObject *module, PyObject *path)
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if(hFile == INVALID_HANDLE_VALUE)
return win32_error_object("CreateFileW", path);
if (hFile == INVALID_HANDLE_VALUE) {
err = "CreateFileW";
goto done1;
}
/* We have a good handle to the target, use it to determine the
target path name. */
buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
if(!buf_size)
return win32_error_object("GetFinalPathNameByHandle", path);
if (!buf_size) {
err = "GetFinalPathNameByHandle";
goto done1;
}
done1:
Py_END_ALLOW_THREADS
if (err)
return win32_error_object(err, path->object);
target_path = PyMem_New(wchar_t, buf_size+1);
if(!target_path)
return PyErr_NoMemory();
Py_BEGIN_ALLOW_THREADS
result_length = GetFinalPathNameByHandleW(hFile, target_path,
buf_size, VOLUME_NAME_DOS);
if(!result_length)
return win32_error_object("GetFinalPathNamyByHandle", path);
if (!result_length) {
err = "GetFinalPathNameByHandle";
goto done2;
}
if(!CloseHandle(hFile))
return win32_error_object("CloseHandle", path);
if (!CloseHandle(hFile)) {
err = "CloseHandle";
goto done2;
}
done2:
Py_END_ALLOW_THREADS
if (err) {
PyMem_Free(target_path);
return win32_error_object(err, path->object);
}
target_path[result_length] = 0;
result = PyUnicode_FromWideChar(target_path, result_length);
PyMem_Free(target_path);
if (path->narrow)
Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
return result;
}
/*[clinic input]
@ -3811,28 +3830,22 @@ os__isdir_impl(PyObject *module, path_t *path)
/*[clinic input]
os._getvolumepathname
path: unicode
path: path_t
A helper function for ismount on Win32.
[clinic start generated code]*/
static PyObject *
os__getvolumepathname_impl(PyObject *module, PyObject *path)
/*[clinic end generated code: output=cbdcbd1059ceef4c input=7eacadc40acbda6b]*/
os__getvolumepathname_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
{
PyObject *result;
const wchar_t *path_wchar;
wchar_t *mountpath=NULL;
size_t buflen;
BOOL ret;
path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen);
if (path_wchar == NULL)
return NULL;
buflen += 1;
/* Volume path should be shorter than entire path */
buflen = Py_MAX(buflen, MAX_PATH);
buflen = Py_MAX(path->length, MAX_PATH);
if (buflen > PY_DWORD_MAX) {
PyErr_SetString(PyExc_OverflowError, "path too long");
@ -3844,15 +3857,17 @@ os__getvolumepathname_impl(PyObject *module, PyObject *path)
return PyErr_NoMemory();
Py_BEGIN_ALLOW_THREADS
ret = GetVolumePathNameW(path_wchar, mountpath,
ret = GetVolumePathNameW(path->wide, mountpath,
Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Py_END_ALLOW_THREADS
if (!ret) {
result = win32_error_object("_getvolumepathname", path);
result = win32_error_object("_getvolumepathname", path->object);
goto exit;
}
result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
if (path->narrow)
Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
exit:
PyMem_Free(mountpath);
@ -9844,20 +9859,20 @@ os_statvfs_impl(PyObject *module, path_t *path)
/*[clinic input]
os._getdiskusage
path: Py_UNICODE
path: path_t
Return disk usage statistics about the given path as a (total, free) tuple.
[clinic start generated code]*/
static PyObject *
os__getdiskusage_impl(PyObject *module, Py_UNICODE *path)
/*[clinic end generated code: output=76d6adcd86b1db0b input=6458133aed893c78]*/
os__getdiskusage_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
{
BOOL retval;
ULARGE_INTEGER _, total, free;
Py_BEGIN_ALLOW_THREADS
retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Py_END_ALLOW_THREADS
if (retval == 0)
return PyErr_SetFromWindowsErr(0);