update ismount() to match upstream from cPython (#64586)

pull/66568/head
Mohammadreza Abdoli 5 years ago committed by Sam Doran
parent 864a3fd59d
commit 7ae5331218

@ -0,0 +1,2 @@
bugfixes:
- ismount - clone upstream version of ismount() (https://github.com/ansible/ansible/issues/63977)

@ -53,38 +53,38 @@ import os
def ismount(path): def ismount(path):
"""Test whether a path is a mount point """Test whether a path is a mount point
clone of os.path.ismount (from cpython Lib/posixpath.py) This is a copy of the upstream version of ismount(). Originally this was copied here as a workaround
fixed to solve https://github.com/ansible/ansible-modules-core/issues/2186 until Python issue 2466 was fixed. Now it is here so this will work on older versions of Python
and workaround non-fixed http://bugs.python.org/issue2466 that may not have the upstream fix.
this should be rewritten as soon as python issue 2466 is fixed https://github.com/ansible/ansible-modules-core/issues/2186
probably check for python version and use os.path.ismount if fixed http://bugs.python.org/issue2466
"""
to remove replace in this file ismount( -> os.path.ismount( and remove this
function"""
try: try:
s1 = os.lstat(path) s1 = os.lstat(path)
except OSError: except (OSError, ValueError):
# the OSError should be handled with more care # It doesn't exist -- so not a mount point. :-)
# it could be a "permission denied" but path is still a mount
return False return False
else: else:
# A symlink can never be a mount point # A symlink can never be a mount point
if os.path.stat.S_ISLNK(s1.st_mode): if os.path.stat.S_ISLNK(s1.st_mode):
return False return False
parent = os.path.join(path, os.path.pardir) if isinstance(path, bytes):
parent = os.path.join(path, b'..')
else:
parent = os.path.join(path, '..')
parent = os.path.realpath(parent) parent = os.path.realpath(parent)
try: try:
s2 = os.lstat(parent) s2 = os.lstat(parent)
except OSError: except (OSError, ValueError):
# one should handle the returned OSError with more care to figure
# out whether this is still a mount
return False return False
if s1.st_dev != s2.st_dev: dev1 = s1.st_dev
dev2 = s2.st_dev
if dev1 != dev2:
return True # path/.. on a different device as path return True # path/.. on a different device as path
if s1.st_ino == s2.st_ino: ino1 = s1.st_ino
return True # path/.. is the same i-node as path, i.e. path=='/' ino2 = s2.st_ino
if ino1 == ino2:
return True # path/.. is the same i-node as path
return False return False

Loading…
Cancel
Save