239 lines
8.7 KiB
Plaintext
239 lines
8.7 KiB
Plaintext
from stdint import *
|
|
import fat32 as fs
|
|
import fat32_types as types
|
|
import fat32_part as part
|
|
import fat32_mkfs as mkfs_mod
|
|
import string
|
|
import t, c
|
|
|
|
|
|
def print_fresult(res: types.FRESULT, msg: t.CConst | str) -> int:
|
|
if res == types.FRESULT.FR_OK:
|
|
print(f"[成功] {msg}")
|
|
return 0
|
|
else:
|
|
print(f"[失败] {msg}: {res}")
|
|
return -1
|
|
|
|
|
|
def print_fresult_void(res: types.FRESULT, msg: t.CConst | str):
|
|
print_fresult(res, msg)
|
|
|
|
|
|
def example_main() -> int:
|
|
print("=== FAT32 文件系统完整示例 ===")
|
|
print()
|
|
|
|
# 1. 扫描可用驱动器
|
|
print("--- 1. 扫描可用驱动器 ---")
|
|
res: types.FRESULT = fs.scan_drives()
|
|
print_fresult_void(res, "扫描驱动器")
|
|
|
|
drive_count: int = fs.get_drive_count()
|
|
print(f"找到 {drive_count} 个驱动器")
|
|
i: int
|
|
for i in range(drive_count):
|
|
letter: t.CChar = fs.get_drive_letter(i)
|
|
part_count: int = fs.get_partition_count(letter)
|
|
print(f" 驱动器 {i}: {letter}")
|
|
for j in range(part_count):
|
|
is_fat32: int = fs.is_partition_fat32(letter, j)
|
|
print(f" 分区 {j}: FAT32={is_fat32}")
|
|
print()
|
|
|
|
# 2. 挂载文件系统
|
|
print("--- 2. 挂载文件系统 ---")
|
|
res = fs.mount()
|
|
if print_fresult(res, "挂载文件系统") != 0:
|
|
print("提示: 可能需要先格式化磁盘")
|
|
print()
|
|
|
|
# 3. 文件系统信息
|
|
print("--- 3. 文件系统信息 ---")
|
|
mounted: int = fs.is_mounted()
|
|
print(f"已挂载: {mounted}")
|
|
if mounted:
|
|
total_clusters: uint32 = fs.get_total()
|
|
free_clusters: uint32 = fs.get_free()
|
|
cluster_size: uint32 = fs.get_cluster_size()
|
|
total_bytes: uint64 = uint64(total_clusters) * uint64(cluster_size)
|
|
free_bytes: uint64 = uint64(free_clusters) * uint64(cluster_size)
|
|
print(f"总簇数: {total_clusters}")
|
|
print(f"空闲簇数: {free_clusters}")
|
|
print(f"簇大小: {cluster_size} 字节")
|
|
print(f"总大小: {total_bytes} 字节")
|
|
print(f"空闲大小: {free_bytes} 字节")
|
|
print()
|
|
|
|
# 4. 元数据操作
|
|
print("--- 4. 元数据操作 ---")
|
|
fs.set_metadata(0x12345678)
|
|
metadata: uint32 = fs.get_metadata()
|
|
print(f"设置的元数据: 0x{metadata:08X}")
|
|
print()
|
|
|
|
# 5. 获取 FAT 时间
|
|
print("--- 5. 获取 FAT 时间 ---")
|
|
fattime: uint32 = fs.get_fattime()
|
|
print(f"当前 FAT 时间: 0x{fattime:08X}")
|
|
print()
|
|
|
|
# 6. 目录操作
|
|
if fs.is_mounted():
|
|
print("--- 6. 目录操作 ---")
|
|
|
|
# 创建目录
|
|
res = fs.mkdir("/test_dir_1")
|
|
print_fresult_void(res, "创建目录 /test_dir_1")
|
|
|
|
res = fs.mkdir("/test_dir_1/subdir")
|
|
print_fresult_void(res, "创建子目录 /test_dir_1/subdir")
|
|
|
|
res = fs.mkdir("/test_dir_2")
|
|
print_fresult_void(res, "创建目录 /test_dir_2")
|
|
print()
|
|
|
|
# 7. 文件操作
|
|
print("--- 7. 文件操作 ---")
|
|
|
|
# 创建并写入文件
|
|
fp1: types.fileobj | t.CPtr = fs.open("/test_dir_1/file1.txt", types.FA_CREATE_ALWAYS | types.FA_WRITE)
|
|
if fp1 is not None:
|
|
test_data1: t.CConst | str = "Hello, World!"
|
|
written: uint32 = fs.write(fp1, test_data1, 13)
|
|
print(f"写入文件 /test_dir_1/file1.txt: {written} 字节")
|
|
fs.close(fp1)
|
|
|
|
# 打开追加
|
|
fp2: types.fileobj | t.CPtr = fs.open("/test_dir_1/file1.txt", types.FA_OPEN_ALWAYS | types.FA_WRITE | types.FA_OPEN_APPEND)
|
|
if fp2 is not None:
|
|
test_data2: t.CConst | str = " Append data!"
|
|
written2: uint32 = fs.write(fp2, test_data2, 13)
|
|
print(f"追加写入: {written2} 字节")
|
|
fs.close(fp2)
|
|
|
|
# 读取文件
|
|
fp3: types.fileobj | t.CPtr = fs.open("/test_dir_1/file1.txt", types.FA_OPEN_EXISTING | types.FA_READ)
|
|
if fp3 is not None:
|
|
buf: t.CArray[uint8, 256]
|
|
read_bytes: uint32 = fs.read(fp3, c.Addr(buf[0]), 256)
|
|
print(f"读取字节数: {read_bytes}")
|
|
print(f"文件内容: {t.CChar(c.Addr(buf[0]), t.CPtr)}")
|
|
|
|
# seek 和 tell
|
|
fs.seek(fp3, 5)
|
|
pos: uint32 = fs.tell(fp3)
|
|
print(f"seek 到位置 5, 当前位置: {pos}")
|
|
|
|
read_bytes2: uint32 = fs.read(fp3, c.Addr(buf[0]), 256)
|
|
print(f"从位置 5 读取: {t.CChar(c.Addr(buf[0]), t.CPtr)}")
|
|
|
|
# 获取文件大小
|
|
fsize: uint32 = fs.size(fp3)
|
|
print(f"文件大小: {fsize} 字节")
|
|
|
|
fs.close(fp3)
|
|
|
|
# 创建另一个文件
|
|
fp4: types.fileobj | t.CPtr = fs.open("/test_dir_1/large_file.txt", types.FA_CREATE_ALWAYS | types.FA_WRITE)
|
|
if fp4 is not None:
|
|
large_data: t.CArray[uint8, 1024]
|
|
k: int
|
|
for k in range(1024):
|
|
large_data[k] = uint8(ord('A') + (k % 26))
|
|
written3: uint32 = fs.write(fp4, c.Addr(large_data[0]), 1024)
|
|
print(f"创建大文件, 写入: {written3} 字节")
|
|
fs.close(fp4)
|
|
print()
|
|
|
|
# 8. 文件状态和属性
|
|
print("--- 8. 文件状态和属性 ---")
|
|
|
|
info: types.fileinfo
|
|
res = fs.stat("/test_dir_1/file1.txt", c.Addr(info))
|
|
print_fresult_void(res, "获取文件信息")
|
|
if res == types.FRESULT.FR_OK:
|
|
print(f" 文件名: {info.fname}")
|
|
print(f" 大小: {info.file_size} 字节")
|
|
print(f" 属性: 0x{info.attr:02X}")
|
|
print(f" 创建时间: 0x{info.ctime:04X}")
|
|
print(f" 创建日期: 0x{info.cdate:04X}")
|
|
print(f" 修改时间: 0x{info.mtime:04X}")
|
|
print(f" 修改日期: 0x{info.mdate:04X}")
|
|
print(f" 访问日期: 0x{info.adate:04X}")
|
|
|
|
# 修改属性
|
|
res = fs.chmod("/test_dir_1/file1.txt", types.AM_RDO, types.AM_RDO)
|
|
print_fresult_void(res, "设置文件为只读")
|
|
print()
|
|
|
|
# 9. 重命名和移动
|
|
print("--- 9. 重命名和移动 ---")
|
|
|
|
res = fs.rename("/test_dir_1/file1.txt", "/test_dir_1/renamed.txt")
|
|
print_fresult_void(res, "重命名文件")
|
|
|
|
res = fs.move("/test_dir_1/renamed.txt", "/test_dir_2/moved.txt")
|
|
print_fresult_void(res, "移动文件")
|
|
|
|
res = fs.rename("/test_dir_2", "/test_dir_renamed")
|
|
print_fresult_void(res, "重命名目录")
|
|
print()
|
|
|
|
# 10. 列出目录
|
|
print("--- 10. 列出目录 ---")
|
|
|
|
dp: types.dirobj
|
|
res = fs.opendir("/", c.Addr(dp))
|
|
if res == types.FRESULT.FR_OK:
|
|
print("根目录内容:")
|
|
dir_info: types.fileinfo
|
|
while True:
|
|
res = fs.readdir(c.Addr(dp), c.Addr(dir_info))
|
|
if res != types.FRESULT.FR_OK:
|
|
break
|
|
attr_str: t.CArray[t.CChar, 8] = [0]
|
|
if dir_info.attr & types.AM_DIR:
|
|
string.strcat(attr_str, "D")
|
|
else:
|
|
string.strcat(attr_str, "F")
|
|
if dir_info.attr & types.AM_RDO:
|
|
string.strcat(attr_str, "R")
|
|
if dir_info.attr & types.AM_HID:
|
|
string.strcat(attr_str, "H")
|
|
if dir_info.attr & types.AM_SYS:
|
|
string.strcat(attr_str, "S")
|
|
if dir_info.attr & types.AM_ARC:
|
|
string.strcat(attr_str, "A")
|
|
print(f" [{attr_str}] {dir_info.fname} ({dir_info.file_size} 字节)")
|
|
fs.closedir(c.Addr(dp))
|
|
print()
|
|
|
|
# 11. 删除文件和目录
|
|
print("--- 11. 删除文件和目录 ---")
|
|
|
|
res = fs.remove("/test_dir_renamed/moved.txt")
|
|
print_fresult_void(res, "删除文件")
|
|
|
|
res = fs.remove("/test_dir_1/large_file.txt")
|
|
print_fresult_void(res, "删除大文件")
|
|
|
|
res = fs.rmdir("/test_dir_1/subdir")
|
|
print_fresult_void(res, "删除子目录")
|
|
|
|
res = fs.rmdir("/test_dir_1")
|
|
print_fresult_void(res, "删除目录 /test_dir_1")
|
|
|
|
res = fs.rmdir("/test_dir_renamed")
|
|
print_fresult_void(res, "删除目录 /test_dir_renamed")
|
|
print()
|
|
|
|
# 12. 卸载文件系统
|
|
print("--- 12. 卸载文件系统 ---")
|
|
res = fs.unmount()
|
|
print_fresult_void(res, "卸载文件系统")
|
|
print()
|
|
|
|
print("=== 示例完成 ===")
|
|
return 0
|