Documentation
¶
Overview ¶
Package atomicfile 提供协议无关的原子落盘原语。
写入先在目标同目录创建临时文件并同步,再按平台替换目标;替换失败时由 Result 说明目标与源文件的状态。
包的组成:AtomicWrite 流式写文件,ReplaceFile 与 ReplaceFileWithBackup 执行平台替换,SyncParentDirectory 同步目录项,MustPreserveReplacementSource 判断错误是否要求保留源文件。
Index ¶
- func AtomicWrite(ctx context.Context, path string, src io.Reader) (written int64, resultErr error)
- func MustPreserveReplacementSource(err error) bool
- func ReplaceFile(sourcePath, targetPath string) error
- func ReplaceFileWithBackup(sourcePath, targetPath, _ string) error
- func SyncParentDirectory(path string) error
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AtomicWrite ¶
AtomicWrite 将 src 流式写入 path,并以同目录临时文件加 fsync/rename 完成原子提交。
func MustPreserveReplacementSource ¶
MustPreserveReplacementSource 报告 wrapped/joined error 是否要求调用方保留 source。
func ReplaceFile ¶
ReplaceFile 用源文件替换目标路径;调用方应确保源文件已完整写入并关闭。
func ReplaceFileWithBackup ¶
ReplaceFileWithBackup 与 Windows 的 API 形状一致;非 Windows 的 rename 已原子替换目标, 不会产生旧文件备份。
func SyncParentDirectory ¶
SyncParentDirectory 在替换提交后同步目标目录项。Windows 上由平台实现返回 可兑现的保证;调用方仍需处理返回的真实错误。
Types ¶
type Result ¶
Result 描述 private-file 替换在返回错误时留下的文件状态。 Committed 表示 target 已经换成 source;PreserveSource 表示替换状态未决, source 仍是恢复材料,调用方不得将其当作普通失败临时文件删除。
func ReplacePrivateFile ¶
ReplacePrivateFile 使用本平台的 private-file 替换协议。