snapshot before regression test
This commit is contained in:
133
includes/os/__init__.py
Normal file
133
includes/os/__init__.py
Normal file
@@ -0,0 +1,133 @@
|
||||
import t, c
|
||||
from stdint import *
|
||||
import platmacro
|
||||
import memhub
|
||||
import os.path
|
||||
import os._win32
|
||||
import os._posix
|
||||
|
||||
|
||||
# os 包入口
|
||||
# 根据 platmacro.IS_WINDOWS 分派到 _win32 或 _posix 后端
|
||||
# os.path 为跨平台纯字符串操作,直接可用
|
||||
|
||||
# 模块级全局 MBuddy 指针(由用户在 main 中赋值初始化)
|
||||
_mbuddy: memhub.MemBuddy | t.CPtr
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 文件/目录属性查询
|
||||
# =============================================================================
|
||||
|
||||
def exists(path: str) -> bool:
|
||||
"""检查文件/目录是否存在"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.exists(path)
|
||||
return os._posix.exists(path)
|
||||
|
||||
|
||||
def isdir(path: str) -> bool:
|
||||
"""检查路径是否为目录"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.isdir(path)
|
||||
return os._posix.isdir(path)
|
||||
|
||||
|
||||
def isfile(path: str) -> bool:
|
||||
"""检查路径是否为普通文件"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.isfile(path)
|
||||
return os._posix.isfile(path)
|
||||
|
||||
|
||||
def getsize(path: str) -> t.CInt64T:
|
||||
"""返回文件大小(字节)。失败返回 -1。"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.getsize(path)
|
||||
return os._posix.getsize(path)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 工作目录
|
||||
# =============================================================================
|
||||
|
||||
def getcwd() -> str:
|
||||
"""返回当前工作目录(通过 _mbuddy 分配内存)"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.getcwd()
|
||||
return os._posix.getcwd_impl()
|
||||
|
||||
|
||||
def chdir(path: str) -> int:
|
||||
"""切换当前工作目录。成功返回 0,失败返回 -1。"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.chdir(path)
|
||||
return os._posix.chdir_impl(path)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 文件/目录操作
|
||||
# =============================================================================
|
||||
|
||||
def mkdir(path: str) -> int:
|
||||
"""创建目录。成功返回 0,失败返回 -1。"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.mkdir(path)
|
||||
return os._posix.mkdir_impl(path)
|
||||
|
||||
|
||||
def remove(path: str) -> int:
|
||||
"""删除文件。成功返回 0,失败返回 -1。"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.remove(path)
|
||||
return os._posix.remove(path)
|
||||
|
||||
|
||||
def rmdir(path: str) -> int:
|
||||
"""删除空目录。成功返回 0,失败返回 -1。"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.rmdir(path)
|
||||
return os._posix.rmdir_impl(path)
|
||||
|
||||
|
||||
def rename(old_path: str, new_path: str) -> int:
|
||||
"""重命名/移动文件或目录。成功返回 0,失败返回 -1。"""
|
||||
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:
|
||||
"""修改文件权限。成功返回 0,失败返回 -1。
|
||||
Windows 上只处理只读位(S_IWUSR);POSIX 上使用完整权限位。"""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return os._win32.chmod(path, mode)
|
||||
return os._posix.chmod_impl(path, mode)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 目录列举
|
||||
# =============================================================================
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user