package main import ( "fmt" "io/fs" "os" "path/filepath" "strings" ) type Entry interface { fmt.Stringer stringRecursive(depth, maxDepth int, b *strings.Builder) StringRecursive(depth int) string Name() string Path() string Size() uint64 SizeModified() uint64 FileCount() uint64 FileCountModified() uint64 Entries() []Entry Entry(path string) Entry SetRemovalMark(mark bool) RemovalMark() bool RemovalStats(stats *RemovalStats) modify(size uint64, fileCount uint64) IsDir() bool Parent() Entry Scan(ch chan<- string, mounts map[string]struct{}) } func NewEntryFromDirEntry(parentEntry Entry, dirEntry fs.DirEntry) Entry { if dirEntry.IsDir() { return &DirectoryEntry{ path: filepath.Join(parentEntry.Path(), dirEntry.Name()), parent: parentEntry, } } else { size := uint64(0) noPerm := false if fi, err := dirEntry.Info(); err == nil { size = uint64(fi.Size()) } else { noPerm = true } return &FileEntry{ path: filepath.Join(parentEntry.Path(), dirEntry.Name()), size: &size, noPerm: noPerm, parent: parentEntry, } } } func NewEntry(path string) (Entry, error) { path = filepath.Clean(path) fi, err := os.Lstat(path) if err != nil { return nil, err } if fi.IsDir() { return &DirectoryEntry{path: path}, nil } else { return &FileEntry{path: path}, nil } }