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.

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