mirror of
https://github.com/python/cpython.git
synced 2025-07-20 17:55:21 +00:00
Issue #13772: In os.symlink() under Windows, do not try to guess the link
target's type (file or directory). The detection was buggy and made the call non-atomic (therefore prone to race conditions).
This commit is contained in:
commit
91ecea24f5
4 changed files with 14 additions and 19 deletions
|
@ -2085,11 +2085,9 @@ Files and Directories
|
||||||
*target_is_directory*, which defaults to ``False``.
|
*target_is_directory*, which defaults to ``False``.
|
||||||
|
|
||||||
On Windows, a symlink represents a file or a directory, and does not morph to
|
On Windows, a symlink represents a file or a directory, and does not morph to
|
||||||
the target dynamically. For this reason, when creating a symlink on Windows,
|
the target dynamically. If *target_is_directory* is set to ``True``, the
|
||||||
if the target is not already present, the symlink will default to being a
|
symlink will be created as a directory symlink, otherwise as a file symlink
|
||||||
file symlink. If *target_is_directory* is set to ``True``, the symlink will
|
(the default).
|
||||||
be created as a directory symlink. This parameter is ignored if the target
|
|
||||||
exists (and the symlink is created with the same type as the target).
|
|
||||||
|
|
||||||
Symbolic link support was introduced in Windows 6.0 (Vista). :func:`symlink`
|
Symbolic link support was introduced in Windows 6.0 (Vista). :func:`symlink`
|
||||||
will raise a :exc:`NotImplementedError` on Windows versions earlier than 6.0.
|
will raise a :exc:`NotImplementedError` on Windows versions earlier than 6.0.
|
||||||
|
@ -2102,7 +2100,6 @@ Files and Directories
|
||||||
administrator level. Either obtaining the privilege or running your
|
administrator level. Either obtaining the privilege or running your
|
||||||
application as an administrator are ways to successfully create symlinks.
|
application as an administrator are ways to successfully create symlinks.
|
||||||
|
|
||||||
|
|
||||||
:exc:`OSError` is raised when the function is called by an unprivileged
|
:exc:`OSError` is raised when the function is called by an unprivileged
|
||||||
user.
|
user.
|
||||||
|
|
||||||
|
|
|
@ -500,7 +500,12 @@ class WalkTests(unittest.TestCase):
|
||||||
f.write("I'm " + path + " and proud of it. Blame test_os.\n")
|
f.write("I'm " + path + " and proud of it. Blame test_os.\n")
|
||||||
f.close()
|
f.close()
|
||||||
if support.can_symlink():
|
if support.can_symlink():
|
||||||
os.symlink(os.path.abspath(t2_path), link_path)
|
if os.name == 'nt':
|
||||||
|
def symlink_to_dir(src, dest):
|
||||||
|
os.symlink(src, dest, True)
|
||||||
|
else:
|
||||||
|
symlink_to_dir = os.symlink
|
||||||
|
symlink_to_dir(os.path.abspath(t2_path), link_path)
|
||||||
sub2_tree = (sub2_path, ["link"], ["tmp3"])
|
sub2_tree = (sub2_path, ["link"], ["tmp3"])
|
||||||
else:
|
else:
|
||||||
sub2_tree = (sub2_path, [], ["tmp3"])
|
sub2_tree = (sub2_path, [], ["tmp3"])
|
||||||
|
@ -1131,7 +1136,7 @@ class Win32SymlinkTests(unittest.TestCase):
|
||||||
os.remove(self.missing_link)
|
os.remove(self.missing_link)
|
||||||
|
|
||||||
def test_directory_link(self):
|
def test_directory_link(self):
|
||||||
os.symlink(self.dirlink_target, self.dirlink)
|
os.symlink(self.dirlink_target, self.dirlink, True)
|
||||||
self.assertTrue(os.path.exists(self.dirlink))
|
self.assertTrue(os.path.exists(self.dirlink))
|
||||||
self.assertTrue(os.path.isdir(self.dirlink))
|
self.assertTrue(os.path.isdir(self.dirlink))
|
||||||
self.assertTrue(os.path.islink(self.dirlink))
|
self.assertTrue(os.path.islink(self.dirlink))
|
||||||
|
|
|
@ -458,6 +458,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #13772: In os.symlink() under Windows, do not try to guess the link
|
||||||
|
target's type (file or directory). The detection was buggy and made the
|
||||||
|
call non-atomic (therefore prone to race conditions).
|
||||||
|
|
||||||
- Issue #6631: Disallow relative file paths in urllib urlopen methods.
|
- Issue #6631: Disallow relative file paths in urllib urlopen methods.
|
||||||
|
|
||||||
- Issue #13722: Avoid silencing ImportErrors when initializing the codecs
|
- Issue #13722: Avoid silencing ImportErrors when initializing the codecs
|
||||||
|
|
|
@ -6551,7 +6551,6 @@ win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
wchar_t *wsrc, *wdest;
|
wchar_t *wsrc, *wdest;
|
||||||
int target_is_directory = 0;
|
int target_is_directory = 0;
|
||||||
DWORD res;
|
DWORD res;
|
||||||
WIN32_FILE_ATTRIBUTE_DATA src_info;
|
|
||||||
|
|
||||||
if (!check_CreateSymbolicLinkW())
|
if (!check_CreateSymbolicLinkW())
|
||||||
{
|
{
|
||||||
|
@ -6581,16 +6580,6 @@ win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
if (wsrc == NULL)
|
if (wsrc == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
/* if src is a directory, ensure target_is_directory==1 */
|
|
||||||
if(
|
|
||||||
GetFileAttributesExW(
|
|
||||||
wsrc, GetFileExInfoStandard, &src_info
|
|
||||||
))
|
|
||||||
{
|
|
||||||
target_is_directory = target_is_directory ||
|
|
||||||
(src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
|
||||||
}
|
|
||||||
|
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory);
|
res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory);
|
||||||
Py_END_ALLOW_THREADS
|
Py_END_ALLOW_THREADS
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue