You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
2.5 KiB

  1. package filesys
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/util"
  4. "github.com/seaweedfs/fuse/fs"
  5. )
  6. type FsCache struct {
  7. root *FsNode
  8. }
  9. type FsNode struct {
  10. parent *FsNode
  11. node fs.Node
  12. name string
  13. children map[string]*FsNode
  14. }
  15. func newFsCache(root fs.Node) *FsCache {
  16. return &FsCache{
  17. root: &FsNode{
  18. node: root,
  19. },
  20. }
  21. }
  22. func (c *FsCache) GetFsNode(path util.FullPath) fs.Node {
  23. t := c.root
  24. for _, p := range path.Split() {
  25. t = t.findChild(p)
  26. if t == nil {
  27. return nil
  28. }
  29. }
  30. return t.node
  31. }
  32. func (c *FsCache) SetFsNode(path util.FullPath, node fs.Node) {
  33. t := c.root
  34. for _, p := range path.Split() {
  35. t = t.ensureChild(p)
  36. }
  37. t.node = node
  38. }
  39. func (c *FsCache) EnsureFsNode(path util.FullPath, genNodeFn func() fs.Node) fs.Node {
  40. t := c.GetFsNode(path)
  41. if t != nil {
  42. return t
  43. }
  44. t = genNodeFn()
  45. c.SetFsNode(path, t)
  46. return t
  47. }
  48. func (c *FsCache) DeleteFsNode(path util.FullPath) {
  49. t := c.root
  50. for _, p := range path.Split() {
  51. t = t.findChild(p)
  52. if t == nil {
  53. return
  54. }
  55. }
  56. if t.parent != nil {
  57. t.parent.deleteChild(t.name)
  58. }
  59. t.deleteSelf()
  60. }
  61. // oldPath and newPath are full path including the new name
  62. func (c *FsCache) Move(oldPath util.FullPath, newPath util.FullPath) *FsNode {
  63. // find old node
  64. src := c.root
  65. for _, p := range oldPath.Split() {
  66. src = src.findChild(p)
  67. if src == nil {
  68. return src
  69. }
  70. }
  71. if src.parent != nil {
  72. src.parent.deleteChild(src.name)
  73. }
  74. src.parent = nil
  75. // find new node
  76. target := c.root
  77. for _, p := range newPath.Split() {
  78. target = target.ensureChild(p)
  79. }
  80. parent := target.parent
  81. src.name = target.name
  82. parent.deleteChild(target.name)
  83. target.deleteSelf()
  84. src.connectToParent(parent)
  85. return src
  86. }
  87. func (n *FsNode) connectToParent(parent *FsNode) {
  88. n.parent = parent
  89. oldNode := parent.findChild(n.name)
  90. if oldNode != nil {
  91. oldNode.deleteSelf()
  92. }
  93. parent.children[n.name] = n
  94. }
  95. func (n *FsNode) findChild(name string) *FsNode {
  96. child, found := n.children[name]
  97. if found {
  98. return child
  99. }
  100. return nil
  101. }
  102. func (n *FsNode) ensureChild(name string) *FsNode {
  103. if n.children == nil {
  104. n.children = make(map[string]*FsNode)
  105. }
  106. child, found := n.children[name]
  107. if found {
  108. return child
  109. }
  110. t := &FsNode{
  111. parent: n,
  112. node: nil,
  113. name: name,
  114. children: nil,
  115. }
  116. n.children[name] = t
  117. return t
  118. }
  119. func (n *FsNode) deleteChild(name string) {
  120. delete(n.children, name)
  121. }
  122. func (n *FsNode) deleteSelf() {
  123. for _, child := range n.children {
  124. child.deleteSelf()
  125. }
  126. n.node = nil
  127. n.parent = nil
  128. n.children = nil
  129. }