mirror of
https://github.com/octoleo/restic.git
synced 2024-12-02 01:48:30 +00:00
51173c5003
Forget fs.Node instances once the kernel frees the corresponding nodeId. This ensures that restic does not run out of memory on large snapshots.
46 lines
700 B
Go
46 lines
700 B
Go
//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
|
|
}
|
|
|
|
type forgetFn func()
|
|
|
|
func newTreeCache() *treeCache {
|
|
return &treeCache{
|
|
nodes: map[string]fs.Node{},
|
|
}
|
|
}
|
|
|
|
func (t *treeCache) lookupOrCreate(name string, create func(forget forgetFn) (fs.Node, error)) (fs.Node, error) {
|
|
t.m.Lock()
|
|
defer t.m.Unlock()
|
|
|
|
if node, ok := t.nodes[name]; ok {
|
|
return node, nil
|
|
}
|
|
|
|
node, err := create(func() {
|
|
t.m.Lock()
|
|
defer t.m.Unlock()
|
|
|
|
delete(t.nodes, name)
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
t.nodes[name] = node
|
|
return node, nil
|
|
}
|