goncdu/node/node.go
2025-02-10 16:26:13 +08:00

105 lines
1.8 KiB
Go

package node
import (
"os"
)
// PathNodeType 表示路径节点类型
type PathNodeType int
const (
DIR PathNodeType = iota
FILE
UNKNOWN
// Stat
NOSCAN
)
// PathNode 表示目录树节点
type PathNode struct {
path string
children map[string]*PathNode
parent *PathNode
size int64
count int
node_type PathNodeType
}
func NewPathNode(path string) *PathNode {
return &PathNode{
path: path,
children: make(map[string]*PathNode),
parent: nil,
size: 0,
count: 0,
node_type: NOSCAN,
}
}
func (pn *PathNode) GetPath() string {
return pn.path
}
func (pn *PathNode) GetSize() int64 {
return pn.size
}
func (pn *PathNode) GetSizeByFormat() string {
return FormatSize(pn.size)
}
func (pn *PathNode) GetCount() int {
return pn.count
}
func (pn *PathNode) GetType() PathNodeType {
return pn.node_type
}
func (pn *PathNode) GetChildren() []*PathNode {
children := make([]*PathNode, 0, len(pn.children))
for _, child := range pn.children {
children = append(children, child)
}
return children
}
func (pn *PathNode) GetChild(path string) *PathNode {
return pn.children[path]
}
func (pn *PathNode) VaildPath() bool {
stat, err := os.Lstat(pn.path)
if err != nil {
return false
}
if stat.IsDir() {
return true
}
return false
}
// linkChild 添加子节点
func (pn *PathNode) linkChild(child *PathNode) {
child.parent = pn
pn.children[child.path] = child
}
func (pn *PathNode) flushNode(diff_size int64, diff_count int) {
pn.size += diff_size
pn.count += diff_count
if pn.parent == nil {
return
}
pn.parent.flushNode(diff_size, diff_count)
}
func (pn *PathNode) ClearWithoutFlush() {
for key, child := range pn.children {
child.ClearWithoutFlush()
child.parent = nil
delete(pn.children, key)
}
}