模块

std.fs

CLI 程序的托管文件读取、写入和存在性检查。

状态

当前可运行:

API返回值说明
std.fs.read(path, buf)usize从宿主路径读取字节到调用方提供的 MutSpan<u8> 缓冲区。
std.fs.write(path, bytes)usize向宿主路径写入字节并返回字节数。
std.fs.host()Fs创建宿主文件系统能力。
std.fs.open(fs, path)Maybe<owned<File>>打开文件,不可用时返回 null
std.fs.openOrRaise(fs, path)owned<File>打开文件或抛出 ![NotFound TooLarge Io]
std.fs.create(fs, path)Maybe<owned<File>>创建文件,不可用时返回 null
std.fs.createOrRaise(fs, path)owned<File>创建文件或抛出 ![NotFound TooLarge Io]
std.fs.readOrRaise(&mut file, buf)usize读入调用方存储或抛出。
std.fs.writeAll(&mut file, bytes)Bool向拥有的文件句柄写入字节。
std.fs.writeAllOrRaise(&mut file, bytes)Void写入所有字节或抛出。
std.fs.fileLen(&mut file)Maybe<usize>可用时报告文件长度。
std.fs.fileLenOrRaise(&mut file)usize报告文件长度或抛出。
std.fs.readAll(alloc, fs, path, limit)Maybe<owned<ByteBuf>>通过显式分配器和大小限制读取。
std.fs.readAllOrRaise(alloc, fs, path, limit)owned<ByteBuf>通过显式分配器和大小限制读取。
std.fs.readBytes(path, buf)Maybe<usize>读取字节到调用方存储。
std.fs.writeBytes(path, bytes)Maybe<usize>向宿主路径写入字节 span。
std.fs.exists(path)Bool检查宿主路径是否存在。
std.fs.isDir(path)Bool检查宿主路径是否为目录。
std.fs.makeDir(path)Bool创建宿主目录。
std.fs.removeDir(path)Bool删除宿主目录。
std.fs.remove(path)Bool删除宿主文件路径。
std.fs.rename(old, new)Bool重命名宿主文件路径。
std.fs.dirEntryCount(path)Maybe<usize>统计宿主目录中的条目数。
std.fs.tempName(buffer, prefix)Maybe<String>将临时路径写入调用方存储。
std.fs.atomicWrite(path, temp, bytes)Bool通过调用方提供的临时路径写入并重命名。
std.fs.close(&mut file)Void显式关闭拥有的文件句柄;剩余的拥有文件会被确定性清理。

当前限制:

  • 更丰富的权限和平台特定的文件模式。
  • 目录遍历。
  • 异步或非阻塞 I/O。

示例

pub fn main Void world World ![NotFound TooLarge Io]  let fs std.fs.host()  mut file owned<File> check std.fs.createOrRaise fs ".zero/out/example.txt"  check std.fs.writeAllOrRaise (&mut file) (std.mem.span "hello\n")  let len check std.fs.fileLenOrRaise (&mut file)  std.fs.close (&mut file)  if && (== len 6) (std.fs.exists ".zero/out/example.txt")    if std.fs.rename ".zero/out/example.txt" ".zero/out/example-renamed.txt"      if std.fs.remove ".zero/out/example-renamed.txt"        check world.out.write "fs ok\n"

设计说明

路径辅助工具是一个小型当前 API,而非隐藏的全局文件系统。

稳定的文件 API 通过能力使副作用、所有权和清理过程可见。

宿主文件系统 API 在非宿主目标上会被 TAR002 拒绝。目标无关的包应将文件系统代码放在其跨目标入口点之外。