2020-03-26 05:19:19 +00:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
2020-03-26 07:08:14 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-03-26 05:19:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"github.com/seaweedfs/fuse/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FsCache struct {
|
|
|
|
root *FsNode
|
2020-04-08 19:50:34 +00:00
|
|
|
sync.RWMutex
|
2020-03-26 05:19:19 +00:00
|
|
|
}
|
|
|
|
type FsNode struct {
|
2020-03-26 07:08:14 +00:00
|
|
|
parent *FsNode
|
|
|
|
node fs.Node
|
|
|
|
name string
|
|
|
|
childrenLock sync.RWMutex
|
|
|
|
children map[string]*FsNode
|
2020-03-26 05:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newFsCache(root fs.Node) *FsCache {
|
|
|
|
return &FsCache{
|
|
|
|
root: &FsNode{
|
|
|
|
node: root,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FsCache) GetFsNode(path util.FullPath) fs.Node {
|
2020-04-08 19:50:34 +00:00
|
|
|
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
return c.doGetFsNode(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FsCache) doGetFsNode(path util.FullPath) fs.Node {
|
2020-03-26 05:19:19 +00:00
|
|
|
t := c.root
|
|
|
|
for _, p := range path.Split() {
|
|
|
|
t = t.findChild(p)
|
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t.node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FsCache) SetFsNode(path util.FullPath, node fs.Node) {
|
2020-04-08 19:50:34 +00:00
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
c.doSetFsNode(path, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FsCache) doSetFsNode(path util.FullPath, node fs.Node) {
|
2020-03-26 05:19:19 +00:00
|
|
|
t := c.root
|
|
|
|
for _, p := range path.Split() {
|
|
|
|
t = t.ensureChild(p)
|
|
|
|
}
|
|
|
|
t.node = node
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:50:34 +00:00
|
|
|
|
2020-03-26 05:19:19 +00:00
|
|
|
func (c *FsCache) EnsureFsNode(path util.FullPath, genNodeFn func() fs.Node) fs.Node {
|
2020-04-08 19:50:34 +00:00
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
t := c.doGetFsNode(path)
|
2020-03-26 05:19:19 +00:00
|
|
|
if t != nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
t = genNodeFn()
|
2020-04-08 19:50:34 +00:00
|
|
|
c.doSetFsNode(path, t)
|
2020-03-26 05:19:19 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FsCache) DeleteFsNode(path util.FullPath) {
|
2020-04-08 19:50:34 +00:00
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2020-03-26 05:19:19 +00:00
|
|
|
t := c.root
|
|
|
|
for _, p := range path.Split() {
|
|
|
|
t = t.findChild(p)
|
|
|
|
if t == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t.parent != nil {
|
2020-03-26 10:30:02 +00:00
|
|
|
t.parent.disconnectChild(t)
|
2020-03-26 05:19:19 +00:00
|
|
|
}
|
|
|
|
t.deleteSelf()
|
|
|
|
}
|
|
|
|
|
|
|
|
// oldPath and newPath are full path including the new name
|
|
|
|
func (c *FsCache) Move(oldPath util.FullPath, newPath util.FullPath) *FsNode {
|
|
|
|
|
2020-04-08 19:50:34 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2020-03-26 05:19:19 +00:00
|
|
|
// find old node
|
|
|
|
src := c.root
|
|
|
|
for _, p := range oldPath.Split() {
|
|
|
|
src = src.findChild(p)
|
|
|
|
if src == nil {
|
|
|
|
return src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if src.parent != nil {
|
2020-03-26 10:30:02 +00:00
|
|
|
src.parent.disconnectChild(src)
|
2020-03-26 05:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// find new node
|
|
|
|
target := c.root
|
|
|
|
for _, p := range newPath.Split() {
|
|
|
|
target = target.ensureChild(p)
|
|
|
|
}
|
|
|
|
parent := target.parent
|
|
|
|
src.name = target.name
|
2020-03-26 07:08:14 +00:00
|
|
|
if dir, ok := src.node.(*Dir); ok {
|
|
|
|
dir.name = target.name // target is not Dir, but a shortcut
|
|
|
|
}
|
2020-03-26 10:30:23 +00:00
|
|
|
if f, ok := src.node.(*File); ok {
|
2020-03-26 17:56:18 +00:00
|
|
|
f.Name = target.name
|
2020-03-26 10:30:23 +00:00
|
|
|
if f.entry != nil {
|
|
|
|
f.entry.Name = f.Name
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 10:30:02 +00:00
|
|
|
parent.disconnectChild(target)
|
2020-03-26 05:19:19 +00:00
|
|
|
|
|
|
|
target.deleteSelf()
|
|
|
|
|
|
|
|
src.connectToParent(parent)
|
|
|
|
|
|
|
|
return src
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *FsNode) connectToParent(parent *FsNode) {
|
|
|
|
n.parent = parent
|
|
|
|
oldNode := parent.findChild(n.name)
|
|
|
|
if oldNode != nil {
|
|
|
|
oldNode.deleteSelf()
|
|
|
|
}
|
2020-03-26 07:08:14 +00:00
|
|
|
if dir, ok := n.node.(*Dir); ok {
|
|
|
|
dir.parent = parent.node.(*Dir)
|
|
|
|
}
|
2020-03-27 07:30:55 +00:00
|
|
|
if f, ok := n.node.(*File); ok {
|
|
|
|
f.dir = parent.node.(*Dir)
|
|
|
|
}
|
2020-03-26 07:08:14 +00:00
|
|
|
n.childrenLock.Lock()
|
2020-03-26 05:19:19 +00:00
|
|
|
parent.children[n.name] = n
|
2020-03-26 07:08:14 +00:00
|
|
|
n.childrenLock.Unlock()
|
2020-03-26 05:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *FsNode) findChild(name string) *FsNode {
|
2020-03-26 07:08:14 +00:00
|
|
|
n.childrenLock.RLock()
|
|
|
|
defer n.childrenLock.RUnlock()
|
|
|
|
|
2020-03-26 05:19:19 +00:00
|
|
|
child, found := n.children[name]
|
|
|
|
if found {
|
|
|
|
return child
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *FsNode) ensureChild(name string) *FsNode {
|
2020-03-26 07:08:14 +00:00
|
|
|
n.childrenLock.Lock()
|
|
|
|
defer n.childrenLock.Unlock()
|
|
|
|
|
2020-03-26 05:19:19 +00:00
|
|
|
if n.children == nil {
|
|
|
|
n.children = make(map[string]*FsNode)
|
|
|
|
}
|
|
|
|
child, found := n.children[name]
|
|
|
|
if found {
|
|
|
|
return child
|
|
|
|
}
|
|
|
|
t := &FsNode{
|
|
|
|
parent: n,
|
|
|
|
node: nil,
|
|
|
|
name: name,
|
|
|
|
children: nil,
|
|
|
|
}
|
|
|
|
n.children[name] = t
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-03-26 10:30:02 +00:00
|
|
|
func (n *FsNode) disconnectChild(child *FsNode) {
|
2020-03-26 07:08:14 +00:00
|
|
|
n.childrenLock.Lock()
|
2020-03-26 10:30:02 +00:00
|
|
|
delete(n.children, child.name)
|
2020-03-26 07:08:14 +00:00
|
|
|
n.childrenLock.Unlock()
|
2020-03-26 10:30:02 +00:00
|
|
|
child.parent = nil
|
2020-03-26 05:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *FsNode) deleteSelf() {
|
2020-03-26 07:08:14 +00:00
|
|
|
n.childrenLock.Lock()
|
2020-03-26 05:19:19 +00:00
|
|
|
for _, child := range n.children {
|
|
|
|
child.deleteSelf()
|
|
|
|
}
|
2020-03-26 07:08:14 +00:00
|
|
|
n.children = nil
|
|
|
|
n.childrenLock.Unlock()
|
|
|
|
|
2020-03-26 05:19:19 +00:00
|
|
|
n.node = nil
|
|
|
|
n.parent = nil
|
2020-03-26 07:08:14 +00:00
|
|
|
|
2020-03-26 05:19:19 +00:00
|
|
|
}
|