Improve is_subdir function before use in snapshot backups.

This commit is contained in:
Wout Bouckaert 2024-08-11 16:26:49 -06:00
parent 2e47d5b21e
commit cda2e816e0
No known key found for this signature in database

View File

@ -544,17 +544,29 @@ class Helpers:
return mounts
def is_subdir(self, child_path, parent_path):
def is_subdir(self, child_path: str, parent_path: str) -> bool:
"""
Checks if given child_path is a subdirectory of given parent_path. Returns True
or False.
Args:
child_path: Child path to check.
parent_path: Possible parent path of child path.
Returns:
True if child_path is a subdirectory of parent_path. Otherwise, False.
"""
server_path = os.path.realpath(child_path)
root_dir = os.path.realpath(parent_path)
if self.is_os_windows():
try:
relative = os.path.relpath(server_path, root_dir)
except:
except ValueError:
# Windows will crash out if two paths are on different
# Drives We can happily return false if this is the case.
# Since two different drives will not be relative to eachother.
# Since two different drives will not be relative to each-other.
return False
else:
relative = os.path.relpath(server_path, root_dir)