mirror of https://github.com/python/cpython
posixpath.realpath() now detects symlink loops and returns the path just before
the loop starts. Closes bug #930024. Thanks AM Kuchling.
This commit is contained in:
parent
711e7d97e4
commit
f50299c378
|
@ -405,13 +405,37 @@ symbolic links encountered in the path."""
|
||||||
bits = ['/'] + filename.split('/')[1:]
|
bits = ['/'] + filename.split('/')[1:]
|
||||||
for i in range(2, len(bits)+1):
|
for i in range(2, len(bits)+1):
|
||||||
component = join(*bits[0:i])
|
component = join(*bits[0:i])
|
||||||
if islink(component):
|
# Resolve symbolic links.
|
||||||
resolved = os.readlink(component)
|
if islink(component):
|
||||||
(dir, file) = split(component)
|
resolved = _resolve_link(component)
|
||||||
resolved = normpath(join(dir, resolved))
|
if resolved is None:
|
||||||
newpath = join(*([resolved] + bits[i:]))
|
# Infinite loop -- return original component + rest of the path
|
||||||
return realpath(newpath)
|
return join(*([component] + bits[i:]))
|
||||||
|
else:
|
||||||
|
newpath = join(*([resolved] + bits[i:]))
|
||||||
|
return realpath(newpath)
|
||||||
|
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_link(path):
|
||||||
|
"""Internal helper function. Takes a path and follows symlinks
|
||||||
|
until we either arrive at something that isn't a symlink, or
|
||||||
|
encounter a path we've seen before (meaning that there's a loop).
|
||||||
|
"""
|
||||||
|
paths_seen = []
|
||||||
|
while islink(path):
|
||||||
|
if path in paths_seen:
|
||||||
|
# Already seen this path, so we must have a symlink loop
|
||||||
|
return None
|
||||||
|
paths_seen.append(path)
|
||||||
|
# Resolve where the link points to
|
||||||
|
resolved = os.readlink(path)
|
||||||
|
if not abspath(resolved):
|
||||||
|
dir = dirname(path)
|
||||||
|
path = normpath(join(dir, resolved))
|
||||||
|
else:
|
||||||
|
path = normpath(resolved)
|
||||||
|
return path
|
||||||
|
|
||||||
supports_unicode_filenames = False
|
supports_unicode_filenames = False
|
||||||
|
|
|
@ -29,6 +29,10 @@ Extension modules
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Bug #930024: posixpath.realpath() now handles infinite loops in symlinks by
|
||||||
|
returning the last point in the path that was not part of any loop. Thanks
|
||||||
|
AM Kuchling.
|
||||||
|
|
||||||
- Bug #980327: ntpath not handles compressing erroneous slashes between the
|
- Bug #980327: ntpath not handles compressing erroneous slashes between the
|
||||||
drive letter and the rest of the path. Also clearly handles UNC addresses now
|
drive letter and the rest of the path. Also clearly handles UNC addresses now
|
||||||
as well. Thanks Paul Moore.
|
as well. Thanks Paul Moore.
|
||||||
|
|
Loading…
Reference in New Issue