trash

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const TimeFormat = time.RFC3339

TimeFormat is the XDG Trash spec time format

Variables

This section is empty.

Functions

func EnsureTrashDir

func EnsureTrashDir(trashDir string) error

EnsureTrashDir creates the trash directory structure if it doesn't exist

func FilesPath

func FilesPath(trashDir, name string) string

FilesPath returns the path where the trashed file should be stored

func GetAllTrashDirs

func GetAllTrashDirs() ([]string, error)

GetAllTrashDirs returns all trash directories that exist This includes home trash and any volume trash directories

func GetTrashDirForPath

func GetTrashDirForPath(path string) (string, error)

GetTrashDirForPath returns the appropriate trash directory for a given path

func HomeTrashDir

func HomeTrashDir() (string, error)

HomeTrashDir returns the default XDG trash directory

func SameFilesystem

func SameFilesystem(path1, path2 string) (bool, error)

SameFilesystem checks if two paths are on the same filesystem

func SetMountsFilePath added in v0.10.0

func SetMountsFilePath(path string)

SetMountsFilePath sets the path for mount points file (primarily for testing)

func TrashInfoPath

func TrashInfoPath(trashDir, name string) string

TrashInfoPath returns the path to the .trashinfo file for a given trash name

func TrashVolumeName added in v0.12.0

func TrashVolumeName(trashDir string) string

TrashVolumeName returns a user-facing name for a trash directory's volume.

func VolumeTrashDir

func VolumeTrashDir(path string) (string, error)

VolumeTrashDir returns the trash directory for a given path's volume For cross-device files, use $VOLUME/.Trash-$UID/

Types

type ItemType added in v0.6.0

type ItemType int
const (
	ItemTypeFile ItemType = iota
	ItemTypeDirectory
	ItemTypeSymlink
)

type Manager

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

Manager handles trash operations

func NewManager

func NewManager() (*Manager, error)

NewManager creates a new trash manager

func (*Manager) Delete added in v0.7.0

func (m *Manager) Delete(trashName string) error

Delete permanently removes a file from trash (no recovery possible)

func (*Manager) DeleteFromDir added in v0.7.0

func (m *Manager) DeleteFromDir(trashDir, trashName string) error

DeleteFromDir deletes from a specific trash directory

func (*Manager) Empty

func (m *Manager) Empty() error

Empty clears all items from the trash

func (*Manager) EmptyDirOlderThan

func (m *Manager) EmptyDirOlderThan(trashDir string, days int) error

func (*Manager) EmptyOlderThan

func (m *Manager) EmptyOlderThan(days int) error

EmptyOlderThan clears items older than days days from all directories

func (*Manager) FindByName

func (m *Manager) FindByName(name string) ([]TrashItem, error)

FindByName finds items in trash matching a name (supports partial match)

func (*Manager) GetLargest

func (m *Manager) GetLargest(n int) ([]TrashItem, error)

GetLargest returns the N largest items in trash

func (*Manager) GetLast

func (m *Manager) GetLast() (*TrashItem, error)

GetLast returns the most recently trashed item

func (*Manager) GetOldestAndNewest

func (m *Manager) GetOldestAndNewest() (oldest, newest time.Time, err error)

GetOldestAndNewest returns the oldest and newest deletion dates

func (*Manager) List

func (m *Manager) List() ([]TrashItem, error)

List returns all items in the trash from all directories

func (*Manager) ListFromDir

func (m *Manager) ListFromDir(trashDir string) ([]TrashItem, error)

ListFromDir returns items from a specific trash directory

func (*Manager) Move

func (m *Manager) Move(path string) error

Move moves a file or directory to the trash

func (*Manager) Restore

func (m *Manager) Restore(trashName string, overwrite bool) error

Restore restores a file from the trash by searching all directories

func (*Manager) RestoreFromDir

func (m *Manager) RestoreFromDir(trashDir, trashName string, overwrite bool) error

RestoreFromDir restores from a specific trash directory

func (*Manager) SetVerboseCallback added in v0.6.0

func (m *Manager) SetVerboseCallback(cb VerboseCallback)

func (*Manager) Status

func (m *Manager) Status() (count int, totalSize int64, err error)

Status returns statistics about the trash from all directories

func (*Manager) StatusFromDir

func (m *Manager) StatusFromDir(trashDir string) (count int, totalSize int64, err error)

StatusFromDir returns statistics from a specific trash directory

type TrashInfo

type TrashInfo struct {
	Path         string    // Original absolute path
	DeletionDate time.Time // When the file was trashed
}

TrashInfo represents the content of a .trashinfo file

func ParseTrashInfo

func ParseTrashInfo(path string) (*TrashInfo, error)

ParseTrashInfo reads and parses a .trashinfo file

func ParseTrashInfoFromRoot added in v0.4.3

func ParseTrashInfoFromRoot(root *TrashRoot, name string) (*TrashInfo, error)

ParseTrashInfoFromRoot reads and parses a .trashinfo file using TrashRoot. This is traversal-resistant - the file must be within the trash root.

func (*TrashInfo) Write

func (ti *TrashInfo) Write(path string) error

Write writes the TrashInfo to a .trashinfo file

func (*TrashInfo) WriteExclusive

func (ti *TrashInfo) WriteExclusive(path string) error

WriteExclusive writes the TrashInfo to a .trashinfo file atomically using O_EXCL Returns os.ErrExist if the file already exists

type TrashItem

type TrashItem struct {
	Name         string      // Name in trash
	OriginalPath string      // Original absolute path
	DeletionDate time.Time   // When it was trashed
	Size         int64       // Size in bytes
	Mode         os.FileMode // File mode/type
	IsDir        bool        // Is directory
	TrashDir     string      // Trash directory containing this item
	FileCount    int         // Number of files (for directories, includes nested files)
}

TrashItem represents a file in the trash

func (TrashItem) TypeString added in v0.11.0

func (t TrashItem) TypeString() string

TypeString returns a human-readable string of the item type

func (TrashItem) VolumeName added in v0.12.0

func (t TrashItem) VolumeName() string

VolumeName returns a user-facing name for the volume containing the item.

type TrashRoot added in v0.4.3

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

TrashRoot provides os.Root-based access to trash directory. All operations are traversal-resistant - symlinks and ".." cannot escape the root.

func OpenTrashRoot added in v0.4.3

func OpenTrashRoot(trashDir string) (*TrashRoot, error)

OpenTrashRoot opens a trash directory for os.Root-based operations. The trashDir must be an absolute path to the trash directory root (e.g., ~/.local/share/Trash or /volume/.Trash-$UID).

func (*TrashRoot) Close added in v0.4.3

func (tr *TrashRoot) Close() error

Close closes the trash root.

func (*TrashRoot) CountFiles added in v0.11.0

func (tr *TrashRoot) CountFiles(name string) (int, error)

CountFiles returns the number of files in a trashed item. If it's a directory, it recursively counts all files inside.

func (*TrashRoot) CreateFiles added in v0.4.3

func (tr *TrashRoot) CreateFiles(name string) (*os.File, error)

CreateFiles creates a new file in files/ directory.

func (*TrashRoot) CreateInfo added in v0.4.3

func (tr *TrashRoot) CreateInfo(name string) (*os.File, error)

CreateInfo creates a new trashinfo file in info/ directory.

func (*TrashRoot) FilesPath added in v0.4.3

func (tr *TrashRoot) FilesPath(name string) string

FilesPath returns the relative path for a file in the files/ directory.

func (*TrashRoot) InfoPath added in v0.4.3

func (tr *TrashRoot) InfoPath(name string) string

InfoPath returns the relative path for a trashinfo file in the info/ directory.

func (*TrashRoot) LstatFiles added in v0.4.3

func (tr *TrashRoot) LstatFiles(name string) (os.FileInfo, error)

LstatFiles returns FileInfo for a file in files/ directory. Uses os.Root.Lstat - traversal-resistant, does not follow symlinks.

func (*TrashRoot) LstatInfo added in v0.4.3

func (tr *TrashRoot) LstatInfo(name string) (os.FileInfo, error)

LstatInfo returns FileInfo for a trashinfo file in info/ directory.

func (*TrashRoot) MkdirFiles added in v0.4.3

func (tr *TrashRoot) MkdirFiles() error

MkdirFiles creates files/ directory.

func (*TrashRoot) MkdirInfo added in v0.4.3

func (tr *TrashRoot) MkdirInfo() error

MkdirInfo creates info/ directory.

func (*TrashRoot) OpenFiles added in v0.4.3

func (tr *TrashRoot) OpenFiles(name string) (*os.File, error)

OpenFiles opens a file in files/ directory for reading.

func (*TrashRoot) OpenInfo added in v0.4.3

func (tr *TrashRoot) OpenInfo(name string) (*os.File, error)

OpenInfo opens a trashinfo file in info/ directory for reading.

func (*TrashRoot) ReadDirFiles added in v0.4.3

func (tr *TrashRoot) ReadDirFiles() ([]os.DirEntry, error)

ReadDirFiles reads the files/ directory.

func (*TrashRoot) RemoveAllFiles added in v0.4.3

func (tr *TrashRoot) RemoveAllFiles(name string) error

RemoveAllFiles recursively removes a file or directory from files/. Unlike os.RemoveAll, this uses os.Root operations which are traversal-resistant - symlinks cannot escape the root.

func (*TrashRoot) RemoveFiles added in v0.4.3

func (tr *TrashRoot) RemoveFiles(name string) error

RemoveFiles removes a file from files/ directory.

func (*TrashRoot) RemoveInfo added in v0.4.3

func (tr *TrashRoot) RemoveInfo(name string) error

RemoveInfo removes a trashinfo file from info/ directory.

type VerboseCallback added in v0.6.0

type VerboseCallback func(path string, itemType ItemType)

Jump to

Keyboard shortcuts

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