maintenance

package
v0.43.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

MIT License Copyright (c) 2025 pt-tools

Package maintenance 提供 ~/.pt-tools 工作目录的共享清理服务(特性 D)。 它只在三个固定白名单子目录(logs / downloads / backups)内操作,并叠加 硬编码红线 denylist(torrents.db(+wal/shm)、secret.key、日志 base 文件), 绝不触碰工作目录之外或红线文件。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ShouldSweepStaging

func ShouldSweepStaging(db *models.TorrentDB, filePath string, siteName models.SiteGroup, retainHours, maxRetry int) bool

ShouldSweepStaging 判定一个暂存 .torrent 是否应被清理(按 site+hash 定位 DB 记录)。

这是从 internal/common.go 的 shouldSweep(Issue #450)提取出来的可复用决策,使 RSS 推送循环(internal.sweepStagingDir)与特性 D 的目录清理共享同一规则,避免规则分叉。 db 为 nil 或哈希计算/查询失败时返回 false(保守不删)。

Types

type CategoryResult

type CategoryResult struct {
	Category     CleanCategory `json:"category"`
	Deleted      []FileAction  `json:"deleted"` // DryRun 时为"将删除"项
	FreedBytes   int64         `json:"freedBytes"`
	DirUsedBytes int64         `json:"dirUsedBytes"` // 该类目录当前递归总占用(含不可删文件)
	Skipped      []string      `json:"skipped"`      // 命中红线/越界而被拒绝的路径(含原因)
	Note         string        `json:"note"`         // 例如整类被拒绝的原因
}

CategoryResult 汇总单个类别的清理结果。

type CleanCategory

type CleanCategory string

CleanCategory 标识一个可清理类别。清理只允许发生在这些类别对应的白名单根内。

const (
	// CategoryLogs 清理 ~/.pt-tools/logs 下的轮转日志备份(保留 base 文件)。
	CategoryLogs CleanCategory = "logs"
	// CategoryStaging 清理 ~/.pt-tools/downloads 下所有 tag 子目录中已无意义的 .torrent。
	CategoryStaging CleanCategory = "staging"
	// CategoryBackups 清理 ~/.pt-tools/backups 下的旧备份(保留最近 N 份)。
	CategoryBackups CleanCategory = "backups"
)

type CleanOptions

type CleanOptions struct {
	// Categories 指定要清理的类别;为空表示全部三类。
	Categories []CleanCategory
	// DryRun 为 true 时只计算将删除的文件,不实际删除。
	DryRun bool
	// KeepBackups 为 backups 类别保留的最近份数;<=0 时使用默认值 5。
	KeepBackups int
}

CleanOptions 控制一次清理行为。

type CleanResult

type CleanResult struct {
	DryRun     bool             `json:"dryRun"`
	Categories []CategoryResult `json:"categories"`
}

CleanResult 汇总一次清理的全部结果。

type Cleaner

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

Cleaner 是共享清理服务。它只在 ~/.pt-tools 下的三个固定白名单根内操作。

func NewCleaner

func NewCleaner(homeDir string, db *models.TorrentDB, zapCfg config.Zap) *Cleaner

NewCleaner 构造 Cleaner。homeDir 显式注入,便于测试用 t.TempDir() 作为 fake home; db 用于 staging 类别的 DB 判定;zapCfg 提供日志保留策略(MaxAge/MaxBackups/Directory)。

func (*Cleaner) Clean

func (c *Cleaner) Clean(_ context.Context, opts CleanOptions) (*CleanResult, error)

Clean 执行清理并返回结果。任何单文件被拒绝(红线/越界)不会中断整体清理。

type FileAction

type FileAction struct {
	Path      string `json:"path"`
	SizeBytes int64  `json:"sizeBytes"`
	Reason    string `json:"reason"`
}

FileAction 描述一个(将)被删除的文件。

Jump to

Keyboard shortcuts

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