Rewrote the comments in the libraries under 'includes' in English (excluding those inside folders)
This commit is contained in:
@@ -4,34 +4,34 @@ import platmacro
|
||||
import memhub
|
||||
import string
|
||||
|
||||
# Windows 后端依赖
|
||||
# Windows backend dependencies
|
||||
import w32.win32base
|
||||
import w32.win32file
|
||||
|
||||
# POSIX 后端依赖
|
||||
# POSIX backend dependencies
|
||||
import posix
|
||||
|
||||
|
||||
# 文件拷贝缓冲区大小
|
||||
COPY_BUF_SIZE: t.CDefine = 65536
|
||||
# File copy buffer size
|
||||
COPY_BUF_SIZE: t.CDefine = 65536 # 2^16
|
||||
|
||||
# 模块级全局 MBuddy 指针(由用户在 main 中赋值初始化)
|
||||
# Module-level global MBuddy pointer (initialized by the user in main)
|
||||
_mbuddy: memhub.MemBuddy | t.CPtr
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# copyfile:拷贝文件内容(不保留权限/元数据)
|
||||
# copyfile: Copy file content (without preserving permissions/metadata)
|
||||
# =============================================================================
|
||||
|
||||
def copyfile(src: str, dst: str) -> int:
|
||||
"""拷贝文件内容。成功返回 0,失败返回 -1。"""
|
||||
"""Copy file content. Returns 0, otherwise -1."""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return _copyfile_win32(src, dst)
|
||||
return _copyfile_posix(src, dst)
|
||||
|
||||
|
||||
def _copyfile_win32(src: str, dst: str) -> int:
|
||||
"""Windows:使用 CopyFileA 内核实现(高效)"""
|
||||
"""Windows: Use CopyFileA kernel implementation (efficient)."""
|
||||
result: BOOL = w32.win32file.CopyFileA(src, dst, 0)
|
||||
if result == 0:
|
||||
return -1
|
||||
@@ -39,7 +39,7 @@ def _copyfile_win32(src: str, dst: str) -> int:
|
||||
|
||||
|
||||
def _copyfile_posix(src: str, dst: str) -> int:
|
||||
"""POSIX:手动 read/write 循环拷贝"""
|
||||
"""POSIX: Manual file read/write loop copy"""
|
||||
fd_in: INT = posix.open(src, posix.O_RDONLY, 0)
|
||||
if fd_in < 0:
|
||||
return -1
|
||||
@@ -70,23 +70,23 @@ def _copyfile_posix(src: str, dst: str) -> int:
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# copy:拷贝文件并保留权限
|
||||
# copy: Copy file and preserve permissions/metadata (POSIX only)
|
||||
# =============================================================================
|
||||
|
||||
def copy(src: str, dst: str) -> int:
|
||||
"""拷贝文件并保留权限(POSIX 上调用 stat+chmod)。成功返回 0,失败返回 -1。"""
|
||||
"""Copy file and preserve permissions/metadata (POSIX only). Returns 0, otherwise -1."""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return _copy_win32(src, dst)
|
||||
return _copy_posix(src, dst)
|
||||
|
||||
|
||||
def _copy_win32(src: str, dst: str) -> int:
|
||||
"""Windows:拷贝文件(CopyFileA,Windows 权限模型简单,直接委托 copyfile)"""
|
||||
"""Windows: Copy file (using CopyFileA kernel implementation (simple, delegate to copyfile)."""
|
||||
return _copyfile_win32(src, dst)
|
||||
|
||||
|
||||
def _copy_posix(src: str, dst: str) -> int:
|
||||
"""POSIX:拷贝文件并保留权限(stat+chmod)"""
|
||||
"""POSIX: Copy file and preserve permissions (stat+chmod). Returns 0, otherwise -1."""
|
||||
result: int = _copyfile_posix(src, dst)
|
||||
if result != 0:
|
||||
return -1
|
||||
@@ -97,20 +97,20 @@ def _copy_posix(src: str, dst: str) -> int:
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# rmtree:递归删除目录树
|
||||
# rmtree: Recursively remove directory tree (POSIX only)
|
||||
# =============================================================================
|
||||
|
||||
def rmtree(path: str) -> int:
|
||||
"""递归删除目录树。成功返回 0,失败返回 -1。"""
|
||||
"""Recursively delete a directory tree. Returns 0 on success and -1 on failure."""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return _rmtree_win32(path)
|
||||
return _rmtree_posix(path)
|
||||
|
||||
|
||||
def _rmtree_win32(path: str) -> int:
|
||||
"""Windows:FindFirstFileA/FindNextFileA 递归删除"""
|
||||
"""Windows: Recursively delete a directory tree using FindFirstFileA/FindNextFileA."""
|
||||
path_len: t.CSizeT = len(path)
|
||||
# 构造搜索模式 path\*
|
||||
# Construct search pattern path\*
|
||||
pattern: bytes = _mbuddy.alloc(path_len + 3)
|
||||
if pattern == None:
|
||||
return -1
|
||||
@@ -136,7 +136,7 @@ def _rmtree_win32(path: str) -> int:
|
||||
has_more: int = 1
|
||||
while has_more == 1:
|
||||
name: str = find_data.cFileName
|
||||
# 跳过 . 和 ..
|
||||
# Skip . and ..
|
||||
skip: bool = False
|
||||
if name[0] == '.':
|
||||
if name[1] == 0:
|
||||
@@ -144,7 +144,7 @@ def _rmtree_win32(path: str) -> int:
|
||||
elif name[1] == '.' and name[2] == 0:
|
||||
skip = True
|
||||
if not skip:
|
||||
# 构造完整路径 path\name
|
||||
# Construct full path path\name
|
||||
name_len: t.CSizeT = len(name)
|
||||
full_path: bytes = _mbuddy.alloc(path_len + name_len + 2)
|
||||
if full_path == None:
|
||||
@@ -162,23 +162,23 @@ def _rmtree_win32(path: str) -> int:
|
||||
full_path[j] = ch
|
||||
j += 1
|
||||
full_path[j] = 0
|
||||
# 判断是文件还是目录
|
||||
# Check if it's a directory or a file
|
||||
if (find_data.dwFileAttributes & w32.win32file.FILE_ATTRIBUTE_DIRECTORY) != 0:
|
||||
# 递归删除子目录
|
||||
# Recursively delete subdirectory
|
||||
sub_result: int = _rmtree_win32(str(full_path))
|
||||
if sub_result != 0:
|
||||
result = -1
|
||||
else:
|
||||
# 删除文件
|
||||
# Delete file
|
||||
if w32.win32file.DeleteFileA(str(full_path)) == 0:
|
||||
result = -1
|
||||
_mbuddy.free(full_path)
|
||||
# 继续搜索下一个条目
|
||||
# Continue searching next entry
|
||||
if w32.win32file.FindNextFileA(handle, t.CPtr(c.Addr(find_data))) == 0:
|
||||
has_more = 0
|
||||
|
||||
w32.win32file.FindClose(handle)
|
||||
# 最后删除空目录本身
|
||||
# Delete empty directory itself
|
||||
if result == 0:
|
||||
if w32.win32file.RemoveDirectoryA(path) == 0:
|
||||
result = -1
|
||||
@@ -186,7 +186,7 @@ def _rmtree_win32(path: str) -> int:
|
||||
|
||||
|
||||
def _rmtree_posix(path: str) -> int:
|
||||
"""POSIX:opendir/readdir 递归删除"""
|
||||
"""POSIX: Recursively delete a directory tree using opendir/readdir."""
|
||||
dirp: posix.DIR = posix.opendir(path)
|
||||
if dirp == None:
|
||||
return -1
|
||||
@@ -196,7 +196,7 @@ def _rmtree_posix(path: str) -> int:
|
||||
entry: posix.Dirent = posix.readdir(dirp)
|
||||
while entry != None:
|
||||
name: str = entry.d_name
|
||||
# 跳过 . 和 ..
|
||||
# Skip . and ..
|
||||
skip: bool = False
|
||||
if name[0] == '.':
|
||||
if name[1] == 0:
|
||||
@@ -204,7 +204,7 @@ def _rmtree_posix(path: str) -> int:
|
||||
elif name[1] == '.' and name[2] == 0:
|
||||
skip = True
|
||||
if not skip:
|
||||
# 构造完整路径 path/name
|
||||
# Construct full path path/name
|
||||
name_len: t.CSizeT = len(name)
|
||||
full_path: bytes = _mbuddy.alloc(path_len + name_len + 2)
|
||||
if full_path == None:
|
||||
@@ -222,21 +222,21 @@ def _rmtree_posix(path: str) -> int:
|
||||
full_path[j] = ch
|
||||
j += 1
|
||||
full_path[j] = 0
|
||||
# 判断条目类型
|
||||
# Check entry type
|
||||
if entry.d_type == posix.DT_DIR:
|
||||
# 递归删除子目录
|
||||
# Recursively delete subdirectory
|
||||
sub_result: int = _rmtree_posix(str(full_path))
|
||||
if sub_result != 0:
|
||||
result = -1
|
||||
else:
|
||||
# 删除文件/符号链接等
|
||||
# Delete file or symbolic link etc.
|
||||
if posix.unlink(str(full_path)) != 0:
|
||||
result = -1
|
||||
_mbuddy.free(full_path)
|
||||
entry = posix.readdir(dirp)
|
||||
|
||||
posix.closedir(dirp)
|
||||
# 最后删除空目录本身
|
||||
# Delete empty directory itself
|
||||
if result == 0:
|
||||
if posix.rmdir(path) != 0:
|
||||
result = -1
|
||||
@@ -244,26 +244,26 @@ def _rmtree_posix(path: str) -> int:
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# move:移动文件/目录
|
||||
# move:Move file/directory
|
||||
# =============================================================================
|
||||
|
||||
def move(src: str, dst: str) -> int:
|
||||
"""移动/重命名文件或目录。同分区时直接 rename,跨分区时 copy+delete。
|
||||
成功返回 0,失败返回 -1。"""
|
||||
"""Move/rename files or directories. If it's the same partition, just rename;
|
||||
if it's across partitions, copy then delete. Returns 0 on success, -1 on failure."""
|
||||
if platmacro.IS_WINDOWS:
|
||||
return _move_win32(src, dst)
|
||||
return _move_posix(src, dst)
|
||||
|
||||
|
||||
def _move_win32(src: str, dst: str) -> int:
|
||||
"""Windows:MoveFileExA(同分区 rename,跨分区 copy+delete)"""
|
||||
"""Windows: MoveFileExA (rename within the same partition, copy and delete across partitions)"""
|
||||
rename_result: BOOL = w32.win32file.MoveFileExA(
|
||||
src, dst,
|
||||
w32.win32file.MOVEFILE_REPLACE_EXISTING | w32.win32file.MOVEFILE_COPY_ALLOWED
|
||||
)
|
||||
if rename_result != 0:
|
||||
return 0
|
||||
# rename 失败,尝试 copy + remove
|
||||
# rename failed, try copy or remove
|
||||
if copyfile(src, dst) != 0:
|
||||
return -1
|
||||
if w32.win32file.DeleteFileA(src) == 0:
|
||||
@@ -272,10 +272,10 @@ def _move_win32(src: str, dst: str) -> int:
|
||||
|
||||
|
||||
def _move_posix(src: str, dst: str) -> int:
|
||||
"""POSIX:rename(同分区),跨分区 copy+unlink"""
|
||||
"""POSIX: rename (within the same partition), copy and unlink across partitions"""
|
||||
if posix.rename(src, dst) == 0:
|
||||
return 0
|
||||
# rename 失败(可能跨分区),尝试 copy + unlink
|
||||
# rename failed (possibly across partitions), try copy or remove
|
||||
if copyfile(src, dst) != 0:
|
||||
return -1
|
||||
if posix.unlink(src) != 0:
|
||||
|
||||
Reference in New Issue
Block a user