# SPDX-License-Identifier: AGPL-3.0-only # Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0 """External media path helpers.""" from __future__ import annotations import getpass import os import platform from pathlib import Path from utils.paths.sensitive import ( contains_sensitive_path_component, is_sensitive_path_component, ) def _is_linux_media_mount_path(path: str, media_root: Path | str) -> bool: normalized = os.path.normpath(os.path.realpath(os.path.expanduser(path))) root = os.path.normpath(os.path.realpath(os.path.expanduser(str(media_root)))) try: rel = os.path.relpath(normalized, root) except ValueError: return False if rel == "." or rel == ".." or rel.startswith(f"..{os.sep}"): return False parts = [part for part in rel.split(os.sep) if part] return len(parts) >= 2 and all(part not in (".", "..") for part in parts[:2]) def is_linux_run_media_path(path: str) -> bool: """True for Linux removable-media paths under /run/media//.""" if platform.system() != "Linux": return False return _is_linux_media_mount_path(path, "/run/media") def _current_username() -> str | None: try: user = getpass.getuser().strip() except Exception: return None return user or None def _contains_sensitive_media_component(path: Path, media_root: Path) -> bool: try: rel = path.relative_to(media_root) except ValueError: rel = path return contains_sensitive_path_component(str(rel)) def linux_run_media_mount_roots( base: Path | str = "/run/media", *, user: str | None = None ) -> list[Path]: """Readable /run/media// roots for the folder browser.""" if platform.system() != "Linux": return [] user = user or _current_username() if not user or user in (".", "..") or os.sep in user: return [] base_path = Path(base) try: resolved_base = base_path.resolve() except (OSError, RuntimeError, ValueError): return [] roots: list[Path] = [] seen: set[str] = set() user_dir = base_path / user try: if not user_dir.is_dir(): return [] volume_dirs = list(user_dir.iterdir()) except (OSError, RuntimeError, ValueError): return [] for volume_dir in volume_dirs: if is_sensitive_path_component(volume_dir.name): continue try: resolved = volume_dir.resolve() except (OSError, RuntimeError, ValueError): continue if not _is_linux_media_mount_path(str(resolved), resolved_base): continue if _contains_sensitive_media_component(resolved, resolved_base): continue key = os.path.normcase(os.path.realpath(str(resolved))) if key in seen: continue try: is_dir = resolved.is_dir() except OSError: continue if is_dir and os.access(resolved, os.R_OK | os.X_OK): seen.add(key) roots.append(resolved) return roots