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.

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