hardlink

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 14 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 hardlink 找同一个文件在别的位置的硬链接,并在给定的根目录里安全地删掉它们(条目详情与 App 种子页的「彻底删除」用)。

只做文件系统的事,不读库:调用方给「已知的路径」(整理记录在库里的文件、种子的数据文件,各带预期的文件编号), Inspect 按 inode 合并成目标,Find 在根目录里找这些 inode 的其他路径,RemoveBeneath 在根目录里不跟随软链接地删掉一个路径。

Index

Constants

View Source
const (
	// SourceRecord 是整理记录在库里的文件
	SourceRecord = "record"
	// SourceTorrent 是种子的数据文件
	SourceTorrent = "torrent"
)

来源的类型(Source.Kind)。

Variables

View Source
var (
	// ErrUnsupported 表示这个系统上做不了(Windows 读不到链接数,也不提供不跟随软链接的删除)。
	ErrUnsupported = errors.New("这个系统上不支持查找与删除其他硬链接")
	// ErrChanged 表示路径上的文件已经不是预期的那个(换成了别的、改过),没有删。
	ErrChanged = errors.New("已经不是原来的文件")
	// ErrOutside 表示路径不在根目录里,或者路径里有软链接、不是目录的一段,没有删。
	ErrOutside = errors.New("不在允许的目录里")
)

Functions

func Canon

func Canon(p string) string

Canon 是路径的规范写法(上级目录解开软链接,见 Inspect 的 Known):拿别处来的路径和查找结果比较前先换成它。

func Inspect

func Inspect(sources []Source) (Targets, []Problem)

Inspect 读每个已知路径(不跟随软链接),只收普通文件;预期的文件编号对不上的(换过、改过)不收,记成问题。 同一个 inode 的路径合成一个目标。链接数读不到(Windows)时 NlinkKnown 为假。

func RemoveBeneath

func RemoveBeneath(root, path, expectFileID string, stops ...string) (int, error)

RemoveBeneath 在根目录 root 里删掉 path(root 里的绝对路径),前提是它还是文件编号为 expectFileID 的普通文件;删掉以后 由深到浅删掉变空的上级目录(不删 root 本身,也不删 stops 里的目录与它们的上级,例如嵌在 root 里的媒体库目录),返回删了几个目录。

从 root 的真实位置起逐级打开中间目录(不跟随软链接,任何一级是软链接或不是目录就拒绝),在最后一级的目录里核对文件编号再删: 中间目录被换成软链接时删不到 root 外面的文件或目录。空目录用父目录的句柄按名字删(AT_REMOVEDIR 只删空目录,被换成软链接时报错、停下)。 残余:同一个目录里在核对与删除之间被换了名字,POSIX 没有「按 inode 删」——换进来的也只能是这个目录里的文件。

func RootOf

func RootOf(roots []string, p string) string

RootOf 是包含 p 的根目录(最长的那个),没有时为空串。p 先规范化(见 canon)。

func Stat

func Stat(p string) (key string, nlink uint64, nlinkKnown bool, err error)

Stat 现读一个路径上的文件(不跟随软链接、只认普通文件):key 同 Target.Key(同一个 inode 相同), nlink 是链接数(nlinkKnown 为假时读不到)。

func Within

func Within(root, p string) bool

Within 报告 p 是不是 root 本身或在 root 里面(两者都先用 Canon 规范化)。

Types

type Link struct {
	Path string `json:"path"`
	Root string `json:"root"`
}

Link 是找到的一个其他位置的硬链接:路径与它所在的根目录(删的时候从这个根往下走)。

type Options

type Options struct {
	Budget   time.Duration
	MaxFiles int
	Progress func(scanned int, root string)
}

Options 是查找的限制:Budget 是最多找多久(默认 10 分钟),MaxFiles 是最多看几个文件(默认 200 万), Progress 在开始一个根目录时与每看 1000 个文件时调用。

type Problem

type Problem struct {
	Path   string `json:"path"`
	Reason string `json:"reason"`
}

Problem 是一个没进目标或查找时碰到的问题:路径与原因。

func MountProblems

func MountProblems(roots []string) []Problem

MountProblems 读这个进程看到的挂载(/proc/self/mountinfo),找出根目录里同一块盘的重复挂载:两个根其实是同一处目录的两个名字, 或者根里挂着同一块盘上别处的目录(bind mount)。这时同一个文件会有两个路径、按路径比较认不出来,调用方不该删其他位置的链接。 读不到挂载表时不报问题(查不了就按没有重复挂载算,和以前一样)。

func NormalizeRoots

func NormalizeRoots(roots []string) ([]string, []Problem)

NormalizeRoots 整理根目录:只留绝对路径、Clean、解开软链接(解不开的记成问题),去重,去掉被别的根包含的。

type Result

type Result struct {
	Scanned     int       `json:"scanned"`
	Incomplete  bool      `json:"incomplete"`
	Unsupported bool      `json:"unsupported"`
	Problems    []Problem `json:"problems"`
}

Result 是一次查找:看了几个文件、有没有没找完(到了时限或上限)、这个系统查不了(Unsupported)与碰到的问题。 找到的路径写进各个目标的 Extra,Missing 跟着更新。

func Find

func Find(ctx context.Context, roots []string, targets Targets, opt Options) Result

Find 在 roots(先用 NormalizeRoots 整理)里找 targets 里还差链接的文件的其他路径:不跟随软链接、不进别的设备上的目录, 先比大小再比 inode;每个目标凑齐链接数就不再找它,全部凑齐时提前结束。到了时限、上限或 ctx 取消时 Incomplete 为真。 找到的写进目标的 Extra(带所在的根),最后按链接数更新 Missing。

type Source

type Source struct {
	Kind         string `json:"kind"`
	RecordID     uint   `json:"record_id,omitempty"`
	DownloaderID uint   `json:"downloader_id,omitempty"`
	Hash         string `json:"hash,omitempty"`
	Path         string `json:"path"`
	ExpectFileID string `json:"-"`
}

Source 是一个已知的路径:从哪来(整理记录或种子)、本地路径与预期的文件编号(transfer.FileID 的格式)。 ExpectFileID 为空时认路径上现在的文件(种子的数据就是种子现在的文件)。

type Target

type Target struct {
	Key        string   `json:"key"`
	FileID     string   `json:"file_id"`
	Size       int64    `json:"size"`
	Nlink      uint64   `json:"nlink,omitempty"`
	NlinkKnown bool     `json:"nlink_known"`
	Sources    []Source `json:"sources"`
	Known      []string `json:"known"`
	Extra      []Link   `json:"extra"`
	// Kept 是找到的、但不删的链接(调用方从 Extra 挪过来的,例如别的整理记录在库里的文件):不算其他位置的链接,删完它还在
	Kept    []string `json:"kept"`
	Missing *int     `json:"missing"`
	// contains filtered or unexported fields
}

Target 是一个要查找的文件(同一个 inode 合成一个):Known 是已知的路径,Extra 是找到的其他路径, Missing 是还差几个链接(在根目录以外、找不到的位置),NlinkKnown 为假(Windows)时为 nil。

type Targets

type Targets map[string]*Target

Targets 是按 inode(Target.Key)合并的目标。

func (Targets) Sorted

func (ts Targets) Sorted() []*Target

Sorted 是按 Key 排好序的目标(输出、算指纹用)。

Jump to

Keyboard shortcuts

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