Documentation
¶
Overview ¶
Package hardlink 找同一个文件在别的位置的硬链接,并在给定的根目录里安全地删掉它们(条目详情与 App 种子页的「彻底删除」用)。
只做文件系统的事,不读库:调用方给「已知的路径」(整理记录在库里的文件、种子的数据文件,各带预期的文件编号), Inspect 按 inode 合并成目标,Find 在根目录里找这些 inode 的其他路径,RemoveBeneath 在根目录里不跟随软链接地删掉一个路径。
Index ¶
- Constants
- Variables
- func Canon(p string) string
- func Inspect(sources []Source) (Targets, []Problem)
- func RemoveBeneath(root, path, expectFileID string, stops ...string) (int, error)
- func RootOf(roots []string, p string) string
- func Stat(p string) (key string, nlink uint64, nlinkKnown bool, err error)
- func Within(root, p string) bool
- type Link
- type Options
- type Problem
- type Result
- type Source
- type Target
- type Targets
Constants ¶
const ( // SourceRecord 是整理记录在库里的文件 SourceRecord = "record" // SourceTorrent 是种子的数据文件 SourceTorrent = "torrent" )
来源的类型(Source.Kind)。
Variables ¶
Functions ¶
func Inspect ¶
Inspect 读每个已知路径(不跟随软链接),只收普通文件;预期的文件编号对不上的(换过、改过)不收,记成问题。 同一个 inode 的路径合成一个目标。链接数读不到(Windows)时 NlinkKnown 为假。
func RemoveBeneath ¶
RemoveBeneath 在根目录 root 里删掉 path(root 里的绝对路径),前提是它还是文件编号为 expectFileID 的普通文件;删掉以后 由深到浅删掉变空的上级目录(不删 root 本身,也不删 stops 里的目录与它们的上级,例如嵌在 root 里的媒体库目录),返回删了几个目录。
从 root 的真实位置起逐级打开中间目录(不跟随软链接,任何一级是软链接或不是目录就拒绝),在最后一级的目录里核对文件编号再删: 中间目录被换成软链接时删不到 root 外面的文件或目录。空目录用父目录的句柄按名字删(AT_REMOVEDIR 只删空目录,被换成软链接时报错、停下)。 残余:同一个目录里在核对与删除之间被换了名字,POSIX 没有「按 inode 删」——换进来的也只能是这个目录里的文件。
Types ¶
type Options ¶
Options 是查找的限制:Budget 是最多找多久(默认 10 分钟),MaxFiles 是最多看几个文件(默认 200 万), Progress 在开始一个根目录时与每看 1000 个文件时调用。
type Problem ¶
Problem 是一个没进目标或查找时碰到的问题:路径与原因。
func MountProblems ¶
MountProblems 读这个进程看到的挂载(/proc/self/mountinfo),找出根目录里同一块盘的重复挂载:两个根其实是同一处目录的两个名字, 或者根里挂着同一块盘上别处的目录(bind mount)。这时同一个文件会有两个路径、按路径比较认不出来,调用方不该删其他位置的链接。 读不到挂载表时不报问题(查不了就按没有重复挂载算,和以前一样)。
func NormalizeRoots ¶
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 跟着更新。
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。