fcp

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	B32K = createPool(s32K)
	B1M  = createPool(s1M)
	B4M  = createPool(s4M)
)

Functions

func Backup

func Backup(ctx context.Context, source, target string, options BackupOptions) (err error)

func DirSeq

func DirSeq(ctx context.Context, dir string, excludes []string) iter.Seq2[os.DirEntry, error]

DirSeq 迭代读取文件夹中所有文件信息(非递归)

func FileCopy

func FileCopy(ctx context.Context, sourcePath, targetPath string, options FileCopyOptions) (err error)

FileCopy 原子性复制或移动文件

  • sourcePath: 源文件路径
  • targetPath: 目标文件路径
  • options: 显式传入的配置结构体

备注:

  • 采用了原子写入逻辑:先写入 .*.tmp 文件再 Rename,防止目标文件损坏。
  • 支持跨文件系统移动:当重命名报 syscall.EXDEV 错误时,自动降级为“复制+删除”。
  • 自动创建路径:目标目录不存在时会自动创建。

func FileEqual

func FileEqual(srcPath, dstPath string, modTime bool) (equal bool, err error)

文件比对

func IsExcluded

func IsExcluded(name string, dir bool, exprs []string) bool

IsExcluded 是否排除

  • name 文件名,仅含名称,有带`/`的 = false
  • dir 表示文件名是文件夹
  • exprs 匹配规则

备注:

  • 规则为 path.Glob 表达式,额外可包含前缀`!`和后缀`/`
  • `!`表示匹配结果取反(取反规则),只用来推翻上个匹配成功结果,不表示仅包含
  • `/`表示只匹配文件夹(文件夹规则)
  • 文件名级别的规则匹配
  • 逐行匹配
  • 忽略格式错误的规则
  • 取反规则在有效正向规则之前的都忽略。
  • 正向匹配成功后,忽略后续正向规则,直到找到取反规则,如果取反匹配成功,忽略后续取反规则,直到找到正向匹配,以此类推。
  • 如果不是文件夹,忽略所有文件夹规则,文件夹参与非文件夹规则匹配
  • 如果最终结果文件夹匹配成功,整个文件夹含子文件(夹)都被排除

func OpenFor

func OpenFor(filePath string, mode int, perm os.FileMode, process func(*os.File) error) (err error)

打开文件,并在操作完成后自动关闭

Types

type BackupOptions

type BackupOptions struct {
	RemoveSource bool // 是否删除源文件
	Overwrite    bool // 是否覆盖目标文件
	KeepTime     bool // 保持时间
	KeepOwen     bool // 保持所有者

	MaxParallel int  // 最大并行数
	ErrContinue bool // 出错继续

	// 排除
	//  - 文件名级别的Glob匹配
	//  - 跳过格式错误的Glob表达式
	//  - 增加前缀!表示匹配结果取反
	//  - 增加末尾`/`表示只匹配文件夹
	//  - 逐行匹配
	//  - 取反匹配只用来推翻上一个匹配成功结果,不表示仅包含,第一行的取反匹配忽略。
	//  - 直到成功和没有取反匹配,有取反匹配直到失败。
	//  - 如果文件夹匹配成功,整个文件夹含子文件(夹)都被排除
	Excludes []string

	ErrReport      func(source, target, err string)
	ProgressReport func(progress BackupProgress)
}

type BackupProgress

type BackupProgress struct {
	TotalSize   int64
	TotalCount  int64
	CopiedSize  int64
	CoppedCount int64
}

type BackupState

type BackupState struct {
	// contains filtered or unexported fields
}

func (BackupState) Progress

func (s BackupState) Progress() BackupProgress

func (*BackupState) ProgressReport

func (s *BackupState) ProgressReport()

type FileCopyOptions

type FileCopyOptions struct {
	RemoveSource  bool // 是否删除源文件
	Overwrite     bool // 是否覆盖目标文件
	KeepTime      bool // 保持修改时间
	KeepOwen      bool // 保持所有者
	AutoCreateDir bool // 自动创建文件所在的文件夹(权限默认0755,有需要同步权限时不要设置为true,由上游处理文件夹)

	Report func(delta, copied, total int64) // 已复制增量和总量
}

FileCopyOptions 复制文件的选项

Jump to

Keyboard shortcuts

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