"""Filesystem health check utilities for pre-flight I/O validation.""" import os import shutil def check_readable(path): """Check if a file or directory is readable. :param str path: Path to check :return: True if readable, False otherwise :rtype: bool """ return os.path.exists(path) and os.access(path, os.R_OK) def check_writable(path): """Check if a path is writable. For files, checks the file. For non-existent paths, checks the parent directory. :param str path: Path to check :return: True if writable, False otherwise :rtype: bool """ if os.path.exists(path): return os.access(path, os.W_OK) parent = os.path.dirname(path) or "." return os.path.exists(parent) and os.access(parent, os.W_OK) def check_disk_space_mb(path, required_mb=10): """Check if sufficient disk space is available at the given path. :param str path: Path to check (file or directory) :param int required_mb: Minimum required free space in MB :return: True if sufficient space available, False otherwise :rtype: bool """ try: check_path = path if os.path.isdir(path) else os.path.dirname(path) or "." usage = shutil.disk_usage(check_path) free_mb = usage.free / (1024 * 1024) return free_mb >= required_mb except OSError: return False