import t, c from stdint import * import platmacro import memhub import os.path import os._win32 import os._posix # os package entry point # Dispatches to _win32 or _posix backend based on platmacro.IS_WINDOWS # os.path is for cross-platform pure string operations, can be used directly # Module-level global MBuddy pointer (initialized by the user in main) _mbuddy: memhub.MemBuddy | t.CPtr # ============================================================================= # File/Directory Attribute Query # ============================================================================= def exists(path: str) -> bool: """Check if the file/directory exists""" if platmacro.IS_WINDOWS: return os._win32.exists(path) return os._posix.exists(path) def isdir(path: str) -> bool: """Check if the path is a directory""" if platmacro.IS_WINDOWS: return os._win32.isdir(path) return os._posix.isdir(path) def isfile(path: str) -> bool: """Check if the path is a regular file""" if platmacro.IS_WINDOWS: return os._win32.isfile(path) return os._posix.isfile(path) def getsize(path: str) -> t.CInt64T: """Returns the file size (in bytes). Returns -1 on failure.""" if platmacro.IS_WINDOWS: return os._win32.getsize(path) return os._posix.getsize(path) # ============================================================================= # Working Directory # ============================================================================= def getcwd() -> str: """Return the current working directory (memory allocated via _mbuddy)""" if platmacro.IS_WINDOWS: return os._win32.getcwd() return os._posix.getcwd_impl() def chdir(path: str) -> int: """Switch the current working directory. Returns 0 if successful, -1 if it fails.""" if platmacro.IS_WINDOWS: return os._win32.chdir(path) return os._posix.chdir_impl(path) # ============================================================================= # File/Directory Operations # ============================================================================= def mkdir(path: str) -> int: """Create a directory. Returns 0 on success, -1 on failure.""" if platmacro.IS_WINDOWS: return os._win32.mkdir(path) return os._posix.mkdir_impl(path) def remove(path: str) -> int: """Delete the file. Returns 0 if successful, -1 if it fails.""" if platmacro.IS_WINDOWS: return os._win32.remove(path) return os._posix.remove(path) def rmdir(path: str) -> int: """Delete empty directories. Returns 0 if successful, -1 if it fails.""" if platmacro.IS_WINDOWS: return os._win32.rmdir(path) return os._posix.rmdir_impl(path) def rename(old_path: str, new_path: str) -> int: """Rename/move a file or directory. Returns 0 on success, -1 on failure.""" if platmacro.IS_WINDOWS: return os._win32.rename(old_path, new_path) return os._posix.rename_impl(old_path, new_path) def chmod(path: str, mode: int) -> int: """Modify file permissions. Returns 0 on success, -1 on failure. On Windows, only the read-only bit (S_IWUSR) is handled; On POSIX, full permission bits are used.""" if platmacro.IS_WINDOWS: return os._win32.chmod(path, mode) return os._posix.chmod_impl(path, mode) # ============================================================================= # Table of Contents Listing # ============================================================================= def listdir(path: str, out_names: str, max_count: ULONG) -> ULONG: """列举目录下的文件/子目录名。 out_names 为调用者提供的字符串指针数组(char**),每个元素指向一个文件名。 文件名通过 _mbuddy 分配内存。 max_count 为 out_names 数组最大容量。 返回实际列举的条目数。失败返回 0。""" if platmacro.IS_WINDOWS: return os._win32.listdir(path, out_names, max_count) return os._posix.listdir(path, out_names, max_count) # ============================================================================= # 命令执行 # ============================================================================= def system(cmd: str) -> int: """执行命令行命令(通过 C 运行时 system())。 命令输出直接到终端(继承父进程 stdout/stderr)。 返回命令的退出码(0 表示成功)。""" if platmacro.IS_WINDOWS: return os._win32.system(cmd) return os._posix.system_impl(cmd)