node.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package fileSystem
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "sync"
  6. "time"
  7. etcdErr "github.com/coreos/etcd/error"
  8. )
  9. var (
  10. Permanent time.Time
  11. )
  12. const (
  13. normal = iota
  14. removed
  15. )
  16. type Node struct {
  17. Path string
  18. CreateIndex uint64
  19. CreateTerm uint64
  20. Parent *Node
  21. ExpireTime time.Time
  22. ACL string
  23. Value string // for key-value pair
  24. Children map[string]*Node // for directory
  25. status int
  26. mu sync.Mutex
  27. removeChan chan bool // remove channel
  28. }
  29. func newFile(path string, value string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
  30. return &Node{
  31. Path: path,
  32. CreateIndex: createIndex,
  33. CreateTerm: createTerm,
  34. Parent: parent,
  35. ACL: ACL,
  36. removeChan: make(chan bool, 1),
  37. ExpireTime: expireTime,
  38. Value: value,
  39. }
  40. }
  41. func newDir(path string, createIndex uint64, createTerm uint64, parent *Node, ACL string) *Node {
  42. return &Node{
  43. Path: path,
  44. CreateIndex: createIndex,
  45. CreateTerm: createTerm,
  46. Parent: parent,
  47. ACL: ACL,
  48. removeChan: make(chan bool, 1),
  49. Children: make(map[string]*Node),
  50. }
  51. }
  52. // Remove function remove the node.
  53. // If the node is a directory and recursive is true, the function will recursively remove
  54. // add nodes under the receiver node.
  55. func (n *Node) Remove(recursive bool) error {
  56. n.mu.Lock()
  57. defer n.mu.Unlock()
  58. if n.status == removed {
  59. return nil
  60. }
  61. if !n.IsDir() { // key-value pair
  62. _, name := filepath.Split(n.Path)
  63. if n.Parent.Children[name] == n {
  64. delete(n.Parent.Children, name)
  65. n.removeChan <- true
  66. n.status = removed
  67. }
  68. return nil
  69. }
  70. if !recursive {
  71. return etcdErr.NewError(102, "")
  72. }
  73. for _, n := range n.Children { // delete all children
  74. n.Remove(true)
  75. }
  76. // delete self
  77. _, name := filepath.Split(n.Path)
  78. if n.Parent.Children[name] == n {
  79. delete(n.Parent.Children, name)
  80. n.removeChan <- true
  81. n.status = removed
  82. }
  83. return nil
  84. }
  85. // Get function gets the value of the node.
  86. // If the receiver node is not a key-value pair, a "Not A File" error will be returned.
  87. func (n *Node) Read() (string, error) {
  88. if n.IsDir() {
  89. return "", etcdErr.NewError(102, "")
  90. }
  91. return n.Value, nil
  92. }
  93. // Set function set the value of the node to the given value.
  94. // If the receiver node is a directory, a "Not A File" error will be returned.
  95. func (n *Node) Write(value string) error {
  96. if n.IsDir() {
  97. return etcdErr.NewError(102, "")
  98. }
  99. n.Value = value
  100. return nil
  101. }
  102. // List function return a slice of nodes under the receiver node.
  103. // If the receiver node is not a directory, a "Not A Directory" error will be returned.
  104. func (n *Node) List() ([]*Node, error) {
  105. n.mu.Lock()
  106. n.mu.Unlock()
  107. if !n.IsDir() {
  108. return nil, etcdErr.NewError(104, "")
  109. }
  110. nodes := make([]*Node, len(n.Children))
  111. i := 0
  112. for _, node := range n.Children {
  113. nodes[i] = node
  114. i++
  115. }
  116. return nodes, nil
  117. }
  118. func (n *Node) GetFile(name string) (*Node, error) {
  119. n.mu.Lock()
  120. n.mu.Unlock()
  121. if !n.IsDir() {
  122. return nil, etcdErr.NewError(104, n.Path)
  123. }
  124. f, ok := n.Children[name]
  125. if ok {
  126. if !f.IsDir() {
  127. return f, nil
  128. } else {
  129. return nil, etcdErr.NewError(102, f.Path)
  130. }
  131. }
  132. return nil, nil
  133. }
  134. // Add function adds a node to the receiver node.
  135. // If the receiver is not a directory, a "Not A Directory" error will be returned.
  136. // If there is a existing node with the same name under the directory, a "Already Exist"
  137. // error will be returned
  138. func (n *Node) Add(child *Node) error {
  139. n.mu.Lock()
  140. n.mu.Unlock()
  141. if n.status == removed {
  142. return etcdErr.NewError(100, "")
  143. }
  144. if !n.IsDir() {
  145. return etcdErr.NewError(104, "")
  146. }
  147. _, name := filepath.Split(child.Path)
  148. _, ok := n.Children[name]
  149. if ok {
  150. return etcdErr.NewError(105, "")
  151. }
  152. n.Children[name] = child
  153. return nil
  154. }
  155. // Clone function clone the node recursively and return the new node.
  156. // If the node is a directory, it will clone all the content under this directory.
  157. // If the node is a key-value pair, it will clone the pair.
  158. func (n *Node) Clone() *Node {
  159. n.mu.Lock()
  160. n.mu.Unlock()
  161. if !n.IsDir() {
  162. return newFile(n.Path, n.Value, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  163. }
  164. clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL)
  165. for key, child := range n.Children {
  166. clone.Children[key] = child.Clone()
  167. }
  168. return clone
  169. }
  170. // IsDir function checks whether the node is a directory.
  171. // If the node is a directory, the function will return true.
  172. // Otherwise the function will return false.
  173. func (n *Node) IsDir() bool {
  174. if n.Children == nil { // key-value pair
  175. return false
  176. }
  177. return true
  178. }
  179. func (n *Node) Expire() {
  180. for {
  181. duration := n.ExpireTime.Sub(time.Now())
  182. if duration <= 0 {
  183. n.Remove(true)
  184. return
  185. }
  186. select {
  187. // if timeout, delete the node
  188. case <-time.After(duration):
  189. n.Remove(true)
  190. return
  191. // if removed, return
  192. case <-n.removeChan:
  193. fmt.Println("node removed")
  194. return
  195. }
  196. }
  197. }
  198. // IsHidden function checks if the node is a hidden node. A hidden node
  199. // will begin with '_'
  200. // A hidden node will not be shown via get command under a directory
  201. // For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
  202. // will only return /foo/notHidden
  203. func (n *Node) IsHidden() bool {
  204. _, name := filepath.Split(n.Path)
  205. if name[0] == '_' { //hidden
  206. return true
  207. }
  208. return false
  209. }