diskspace/entry.go
2022-06-28 13:11:16 +02:00

73 lines
1.3 KiB
Go

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
Entries() []Entry
Entry(path string) Entry
SetRemovalMark(mark bool)
RemovalMark() bool
RemovalStats(stats *RemovalStats)
modifySize(modifier 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
}
}