diff options
| -rw-r--r-- | fungus/utils_pure.py | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/fungus/utils_pure.py b/fungus/utils_pure.py index 88c156e..cde9be4 100644 --- a/fungus/utils_pure.py +++ b/fungus/utils_pure.py @@ -21,13 +21,23 @@ def format_title(title): return re.sub(r"\s+", "-", title) -def is_valid_prefix_path(path: str) -> bool: +def is_valid_prefix_path(path: str, prefixes_dir: str | None = None) -> bool: """Check whether *path* lies within the configured prefixes directory. Returns ``True`` only if the resolved path is the prefixes directory itself or a descendant of it. + + Parameters + ---------- + path : str + The path to check. + prefixes_dir : str, optional + The prefixes directory to check against. Defaults to the value + from ``fungus.constants``. """ - from fungus.constants import prefixes_dir + if prefixes_dir is None: + from fungus.constants import prefixes_dir as _pd + prefixes_dir = _pd expanded = os.path.expanduser(path) resolved = os.path.realpath(expanded) @@ -36,22 +46,40 @@ def is_valid_prefix_path(path: str) -> bool: return resolved == safe_root or resolved.startswith(safe_root + os.sep) -def is_safe_prefix_path(path: str) -> bool: +def is_safe_prefix_path( + path: str, + user_data: str | None = None, + user_config: str | None = None, +) -> bool: """Check whether *path* is a safe target for recursive deletion. Returns ``True`` only if the resolved path lies within the fungus user-data or user-config directories (``~/.local/share/fungus-launcher`` or ``~/.config/fungus-launcher``), or is one of those directories itself. + + Parameters + ---------- + path : str + The path to check. + user_data : str, optional + The user-data directory. Defaults to ``PathManager.user_data()``. + user_config : str, optional + The user-config directory. Defaults to ``PathManager.user_config()``. """ - from fungus.path_manager import PathManager + if user_data is None: + from fungus.path_manager import PathManager + user_data = PathManager.user_data() + if user_config is None: + from fungus.path_manager import PathManager + user_config = PathManager.user_config() expanded = os.path.expanduser(path) resolved = os.path.realpath(expanded) safe_roots = [ - os.path.realpath(PathManager.user_data()), - os.path.realpath(PathManager.user_config()), + os.path.realpath(user_data), + os.path.realpath(user_config), ] for root in safe_roots: |
