Skip to content

Commit 455b4a6

Browse files
committed
add a generic find mountpoint function
Signed-off-by: Zen <[email protected]>
1 parent cc00de5 commit 455b4a6

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/ugrd/fs/mounts.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "desultory"
2-
__version__ = "6.3.0"
2+
__version__ = "6.4.0"
33

44
from pathlib import Path
55
from typing import Union
@@ -54,6 +54,18 @@ def _resolve_dev(self, device_path) -> str:
5454
return device_path
5555

5656

57+
def _find_mountpoint(self, path: str) -> str:
58+
""" Finds the mountpoint of a file or directory,
59+
Checks if the parent dir is a mountpoint, if not, recursively checks the parent dir."""
60+
check_path = Path(path).resolve()
61+
parent = check_path.parent if not check_path.is_dir() else check_path
62+
if str(parent) in self["_mounts"]:
63+
return str(parent)
64+
elif parent == Path("/"): # The root mount SHOULD always be found...
65+
raise AutodetectError("Mountpoint not found for: %s" % path)
66+
return _find_mountpoint(self, parent.parent)
67+
68+
5769
def _resolve_device_mountpoint(self, device) -> str:
5870
"""Gets the mountpoint of a device based on the device path."""
5971
for mountpoint, mount_info in self["_mounts"].items():
@@ -82,12 +94,9 @@ def _resolve_overlay_lower_device(self, mountpoint) -> dict:
8294

8395
while self["_mounts"][mountpoint]["fstype"] == "overlay":
8496
lowerdir = _resolve_overlay_lower_dir(self, mountpoint)
85-
lower_path = Path(lowerdir)
86-
while str(lower_path) not in self["_mounts"]:
87-
lower_path = lower_path.parent
88-
if lower_path == Path("/"):
89-
raise AutodetectError("Lowerdir mount not found: %s" % lowerdir)
90-
mountpoint = str(lower_path)
97+
mountpoint = _find_mountpoint(self, lowerdir)
98+
if mountpoint == "/": # The lowerdir mount should never be the root mount
99+
raise AutodetectError("Lowerdir mount not found: %s" % lowerdir)
91100

92101
return self["_mounts"][mountpoint]["device"]
93102

0 commit comments

Comments
 (0)