2024-09-09 20:15:30 +00:00
|
|
|
//go:build darwin || freebsd || linux
|
|
|
|
// +build darwin freebsd linux
|
|
|
|
|
|
|
|
package fuse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/anacrolix/fuse/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type treeCache struct {
|
|
|
|
nodes map[string]fs.Node
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
2024-09-09 20:37:51 +00:00
|
|
|
type forgetFn func()
|
|
|
|
|
2024-09-09 20:15:30 +00:00
|
|
|
func newTreeCache() *treeCache {
|
|
|
|
return &treeCache{
|
|
|
|
nodes: map[string]fs.Node{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 20:37:51 +00:00
|
|
|
func (t *treeCache) lookupOrCreate(name string, create func(forget forgetFn) (fs.Node, error)) (fs.Node, error) {
|
2024-09-09 20:15:30 +00:00
|
|
|
t.m.Lock()
|
|
|
|
defer t.m.Unlock()
|
|
|
|
|
|
|
|
if node, ok := t.nodes[name]; ok {
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2024-09-09 20:37:51 +00:00
|
|
|
node, err := create(func() {
|
|
|
|
t.m.Lock()
|
|
|
|
defer t.m.Unlock()
|
|
|
|
|
|
|
|
delete(t.nodes, name)
|
|
|
|
})
|
2024-09-09 20:15:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.nodes[name] = node
|
|
|
|
return node, nil
|
|
|
|
}
|