file_system.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package fileSystem
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "time"
  7. etcdErr "github.com/coreos/etcd/error"
  8. )
  9. type FileSystem struct {
  10. Root *Node
  11. EventHistory *EventHistory
  12. WatcherHub *watcherHub
  13. Index uint64
  14. Term uint64
  15. }
  16. func New() *FileSystem {
  17. return &FileSystem{
  18. Root: newDir("/", 0, 0, nil, ""),
  19. WatcherHub: newWatchHub(1000),
  20. }
  21. }
  22. func (fs *FileSystem) Get(path string, recusive bool, index uint64, term uint64) (*Event, error) {
  23. // TODO: add recursive get
  24. n, err := fs.InternalGet(path, index, term)
  25. if err != nil {
  26. return nil, err
  27. }
  28. e := newEvent(Get, path, index, term)
  29. if n.IsDir() { // node is dir
  30. e.Pairs = make([]KeyValuePair, len(n.Children))
  31. i := 0
  32. for _, subN := range n.Children {
  33. if subN.IsHidden() { // get will not list hidden node
  34. continue
  35. }
  36. if subN.IsDir() {
  37. e.Pairs[i] = KeyValuePair{
  38. Key: subN.Path,
  39. Dir: true,
  40. }
  41. } else {
  42. e.Pairs[i] = KeyValuePair{
  43. Key: subN.Path,
  44. Value: subN.Value,
  45. }
  46. }
  47. i++
  48. }
  49. } else { // node is file
  50. e.Value = n.Value
  51. }
  52. return e, nil
  53. }
  54. func (fs *FileSystem) Set(path string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
  55. path = filepath.Clean("/" + path)
  56. // update file system known index and term
  57. fs.Index, fs.Term = index, term
  58. dir, name := filepath.Split(path)
  59. // walk through the path and get the last directory node
  60. d, err := fs.walk(dir, fs.checkDir)
  61. if err != nil {
  62. return nil, err
  63. }
  64. f := newFile(name, value, fs.Index, fs.Term, d, "", expireTime)
  65. e := newEvent(Set, path, fs.Index, fs.Term)
  66. e.Value = f.Value
  67. // remove previous file if exist
  68. oldFile, err := d.GetFile(name)
  69. if err == nil {
  70. if oldFile != nil {
  71. oldFile.Remove(false)
  72. e.PrevValue = oldFile.Value
  73. }
  74. } else {
  75. return nil, err
  76. }
  77. err = d.Add(f)
  78. if err != nil {
  79. return nil, err
  80. }
  81. // Node with TTL
  82. if expireTime != Permanent {
  83. go f.Expire()
  84. e.Expiration = &f.ExpireTime
  85. e.TTL = int64(expireTime.Sub(time.Now()) / time.Second)
  86. }
  87. return e, nil
  88. }
  89. func (fs *FileSystem) TestAndSet(path string, recurisive bool, index uint64, term uint64) {
  90. }
  91. func (fs *FileSystem) TestIndexAndSet() {
  92. }
  93. func (fs *FileSystem) Delete(path string, recurisive bool, index uint64, term uint64) (*Event, error) {
  94. n, err := fs.InternalGet(path, index, term)
  95. if err != nil {
  96. return nil, err
  97. }
  98. err = n.Remove(recurisive)
  99. if err != nil {
  100. return nil, err
  101. }
  102. e := newEvent(Delete, path, index, term)
  103. if n.IsDir() {
  104. e.Dir = true
  105. } else {
  106. e.PrevValue = n.Value
  107. }
  108. return e, nil
  109. }
  110. // walk function walks all the path and apply the walkFunc on each directory
  111. func (fs *FileSystem) walk(path string, walkFunc func(prev *Node, component string) (*Node, error)) (*Node, error) {
  112. components := strings.Split(path, "/")
  113. curr := fs.Root
  114. var err error
  115. for i := 1; i < len(components); i++ {
  116. if len(components[i]) == 0 { // ignore empty string
  117. return curr, nil
  118. }
  119. curr, err = walkFunc(curr, components[i])
  120. if err != nil {
  121. return nil, err
  122. }
  123. }
  124. return curr, nil
  125. }
  126. // InternalGet function get the node of the given path.
  127. func (fs *FileSystem) InternalGet(path string, index uint64, term uint64) (*Node, error) {
  128. fmt.Println("GET: ", path)
  129. path = filepath.Clean("/" + path)
  130. // update file system known index and term
  131. fs.Index, fs.Term = index, term
  132. walkFunc := func(parent *Node, dirName string) (*Node, error) {
  133. child, ok := parent.Children[dirName]
  134. if ok {
  135. return child, nil
  136. }
  137. return nil, etcdErr.NewError(100, "get")
  138. }
  139. f, err := fs.walk(path, walkFunc)
  140. if err != nil {
  141. return nil, err
  142. }
  143. return f, nil
  144. }
  145. // checkDir function will check whether the component is a directory under parent node.
  146. // If it is a directory, this function will return the pointer to that node.
  147. // If it does not exist, this function will create a new directory and return the pointer to that node.
  148. // If it is a file, this function will return error.
  149. func (fs *FileSystem) checkDir(parent *Node, dirName string) (*Node, error) {
  150. subDir, ok := parent.Children[dirName]
  151. if ok {
  152. return subDir, nil
  153. }
  154. n := newDir(filepath.Join(parent.Path, dirName), fs.Index, fs.Term, parent, parent.ACL)
  155. parent.Children[dirName] = n
  156. return n, nil
  157. }