atomicfile

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package atomicfile 提供协议无关的原子落盘原语。

写入先在目标同目录创建临时文件并同步,再按平台替换目标;替换失败时由 Result 说明目标与源文件的状态。

包的组成:AtomicWrite 流式写文件,ReplaceFile 与 ReplaceFileWithBackup 执行平台替换,SyncParentDirectory 同步目录项,MustPreserveReplacementSource 判断错误是否要求保留源文件。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicWrite

func AtomicWrite(ctx context.Context, path string, src io.Reader) (written int64, resultErr error)

AtomicWrite 将 src 流式写入 path,并以同目录临时文件加 fsync/rename 完成原子提交。

func MustPreserveReplacementSource

func MustPreserveReplacementSource(err error) bool

MustPreserveReplacementSource 报告 wrapped/joined error 是否要求调用方保留 source。

func ReplaceFile

func ReplaceFile(sourcePath, targetPath string) error

ReplaceFile 用源文件替换目标路径;调用方应确保源文件已完整写入并关闭。

func ReplaceFileWithBackup

func ReplaceFileWithBackup(sourcePath, targetPath, _ string) error

ReplaceFileWithBackup 与 Windows 的 API 形状一致;非 Windows 的 rename 已原子替换目标, 不会产生旧文件备份。

func SyncParentDirectory

func SyncParentDirectory(path string) error

SyncParentDirectory 在替换提交后同步目标目录项。Windows 上由平台实现返回 可兑现的保证;调用方仍需处理返回的真实错误。

Types

type Result

type Result struct {
	Committed      bool
	PreserveSource bool
}

Result 描述 private-file 替换在返回错误时留下的文件状态。 Committed 表示 target 已经换成 source;PreserveSource 表示替换状态未决, source 仍是恢复材料,调用方不得将其当作普通失败临时文件删除。

func ReplacePrivateFile

func ReplacePrivateFile(sourcePath, targetPath string) (Result, error)

ReplacePrivateFile 使用本平台的 private-file 替换协议。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL