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.

171 lines
4.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package filer2
  2. import (
  3. "fmt"
  4. "github.com/karlseguin/ccache"
  5. "strings"
  6. "path/filepath"
  7. "time"
  8. "os"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. )
  12. type Filer struct {
  13. master string
  14. store FilerStore
  15. directoryCache *ccache.Cache
  16. }
  17. func NewFiler(master string) *Filer {
  18. return &Filer{
  19. master: master,
  20. directoryCache: ccache.New(ccache.Configure().MaxSize(1000).ItemsToPrune(100)),
  21. }
  22. }
  23. func (f *Filer) SetStore(store FilerStore) () {
  24. f.store = store
  25. }
  26. func (f *Filer) DisableDirectoryCache() () {
  27. f.directoryCache = nil
  28. }
  29. func (f *Filer) CreateEntry(entry *Entry) (error) {
  30. dirParts := strings.Split(string(entry.FullPath), "/")
  31. // fmt.Printf("directory parts: %+v\n", dirParts)
  32. var lastDirectoryEntry *Entry
  33. for i := 1; i < len(dirParts); i++ {
  34. dirPath := "/" + filepath.Join(dirParts[:i]...)
  35. // fmt.Printf("%d directory: %+v\n", i, dirPath)
  36. // first check local cache
  37. dirEntry := f.cacheGetDirectory(dirPath)
  38. // not found, check the store directly
  39. if dirEntry == nil {
  40. glog.V(4).Infof("find uncached directory: %s", dirPath)
  41. var dirFindErr error
  42. _, dirEntry, dirFindErr = f.FindEntry(FullPath(dirPath))
  43. if dirFindErr != nil {
  44. return fmt.Errorf("findDirectory %s: %v", dirPath, dirFindErr)
  45. }
  46. } else {
  47. glog.V(4).Infof("found cached directory: %s", dirPath)
  48. }
  49. // no such existing directory
  50. if dirEntry == nil {
  51. // create the directory
  52. now := time.Now()
  53. dirEntry = &Entry{
  54. FullPath: FullPath(dirPath),
  55. Attr: Attr{
  56. Mtime: now,
  57. Crtime: now,
  58. Mode: os.ModeDir | 0660,
  59. Uid: entry.Uid,
  60. Gid: entry.Gid,
  61. },
  62. }
  63. glog.V(2).Infof("create directory: %s", dirPath)
  64. mkdirErr := f.store.InsertEntry(dirEntry)
  65. if mkdirErr != nil {
  66. return fmt.Errorf("mkdir %s: %v", dirPath, mkdirErr)
  67. }
  68. }
  69. // cache the directory entry
  70. f.cacheSetDirectory(dirPath, dirEntry, i)
  71. // remember the direct parent directory entry
  72. if i == len(dirParts)-1 {
  73. lastDirectoryEntry = dirEntry
  74. }
  75. }
  76. if lastDirectoryEntry == nil {
  77. return fmt.Errorf("parent folder not found: %v", entry.FullPath)
  78. }
  79. /*
  80. if !hasWritePermission(lastDirectoryEntry, entry) {
  81. glog.V(0).Infof("directory %s: %v, entry: uid=%d gid=%d",
  82. lastDirectoryEntry.FullPath, lastDirectoryEntry.Attr, entry.Uid, entry.Gid)
  83. return fmt.Errorf("no write permission in folder %v", lastDirectoryEntry.FullPath)
  84. }
  85. */
  86. if err := f.store.InsertEntry(entry); err != nil {
  87. return fmt.Errorf("insert entry %s: %v", entry.FullPath, err)
  88. }
  89. return nil
  90. }
  91. func (f *Filer) AppendFileChunk(p FullPath, chunks []*filer_pb.FileChunk) (err error) {
  92. return f.store.AppendFileChunk(p, chunks)
  93. }
  94. func (f *Filer) FindEntry(p FullPath) (found bool, entry *Entry, err error) {
  95. return f.store.FindEntry(p)
  96. }
  97. func (f *Filer) DeleteEntry(p FullPath) (fileEntry *Entry, err error) {
  98. found, entry, err := f.FindEntry(p)
  99. if err != nil || !found {
  100. return nil, err
  101. }
  102. if entry.IsDirectory() {
  103. entries, err := f.ListDirectoryEntries(p, "", false, 1)
  104. if err != nil {
  105. return nil, fmt.Errorf("list folder %s: %v", p, err)
  106. }
  107. if len(entries) > 0 {
  108. return nil, fmt.Errorf("folder %s is not empty", p)
  109. }
  110. }
  111. return f.store.DeleteEntry(p)
  112. }
  113. func (f *Filer) ListDirectoryEntries(p FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) {
  114. if strings.HasSuffix(string(p), "/") && len(p) > 1 {
  115. p = p[0:len(p)-1]
  116. }
  117. return f.store.ListDirectoryEntries(p, startFileName, inclusive, limit)
  118. }
  119. func (f *Filer) cacheGetDirectory(dirpath string) (*Entry) {
  120. if f.directoryCache == nil {
  121. return nil
  122. }
  123. item := f.directoryCache.Get(dirpath)
  124. if item == nil {
  125. return nil
  126. }
  127. return item.Value().(*Entry)
  128. }
  129. func (f *Filer) cacheSetDirectory(dirpath string, dirEntry *Entry, level int) {
  130. if f.directoryCache == nil {
  131. return
  132. }
  133. minutes := 60
  134. if level < 10 {
  135. minutes -= level * 6
  136. }
  137. f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute)
  138. }