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.

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