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. if oldEntry, err := f.FindEntry(entry.FullPath); err == nil {
  84. f.deleteChunks(oldEntry)
  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) UpdateEntry(entry *Entry) (err error) {
  92. return f.store.UpdateEntry(entry)
  93. }
  94. func (f *Filer) FindEntry(p FullPath) (entry *Entry, err error) {
  95. return f.store.FindEntry(p)
  96. }
  97. func (f *Filer) DeleteEntryMetaAndData(p FullPath, shouldDeleteChunks bool) (err error) {
  98. entry, err := f.FindEntry(p)
  99. if err != nil {
  100. return err
  101. }
  102. if entry.IsDirectory() {
  103. entries, err := f.ListDirectoryEntries(p, "", false, 1)
  104. if err != nil {
  105. return fmt.Errorf("list folder %s: %v", p, err)
  106. }
  107. if len(entries) > 0 {
  108. return fmt.Errorf("folder %s is not empty", p)
  109. }
  110. }
  111. if shouldDeleteChunks {
  112. f.deleteChunks(entry)
  113. }
  114. return f.store.DeleteEntry(p)
  115. }
  116. func (f *Filer) ListDirectoryEntries(p FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) {
  117. if strings.HasSuffix(string(p), "/") && len(p) > 1 {
  118. p = p[0: len(p)-1]
  119. }
  120. return f.store.ListDirectoryEntries(p, startFileName, inclusive, limit)
  121. }
  122. func (f *Filer) cacheGetDirectory(dirpath string) *Entry {
  123. if f.directoryCache == nil {
  124. return nil
  125. }
  126. item := f.directoryCache.Get(dirpath)
  127. if item == nil {
  128. return nil
  129. }
  130. return item.Value().(*Entry)
  131. }
  132. func (f *Filer) cacheSetDirectory(dirpath string, dirEntry *Entry, level int) {
  133. if f.directoryCache == nil {
  134. return
  135. }
  136. minutes := 60
  137. if level < 10 {
  138. minutes -= level * 6
  139. }
  140. f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute)
  141. }
  142. func (f *Filer) deleteChunks(entry *Entry) {
  143. if entry == nil {
  144. return
  145. }
  146. for _, chunk := range entry.Chunks {
  147. if err := operation.DeleteFile(f.GetMaster(), chunk.FileId, ""); err != nil {
  148. glog.V(0).Infof("deleting file %s: %v", chunk.FileId, err)
  149. }
  150. }
  151. }