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.

189 lines
4.1 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
  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. 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. dirEntry, _ = f.FindEntry(FullPath(dirPath))
  42. } else {
  43. glog.V(4).Infof("found cached directory: %s", dirPath)
  44. }
  45. // no such existing directory
  46. if dirEntry == nil {
  47. // create the directory
  48. now := time.Now()
  49. dirEntry = &Entry{
  50. FullPath: FullPath(dirPath),
  51. Attr: Attr{
  52. Mtime: now,
  53. Crtime: now,
  54. Mode: os.ModeDir | 0770,
  55. Uid: entry.Uid,
  56. Gid: entry.Gid,
  57. },
  58. }
  59. glog.V(2).Infof("create directory: %s %v", dirPath, dirEntry.Mode)
  60. mkdirErr := f.store.InsertEntry(dirEntry)
  61. if mkdirErr != nil {
  62. return fmt.Errorf("mkdir %s: %v", dirPath, mkdirErr)
  63. }
  64. }
  65. // cache the directory entry
  66. f.cacheSetDirectory(dirPath, dirEntry, i)
  67. // remember the direct parent directory entry
  68. if i == len(dirParts)-1 {
  69. lastDirectoryEntry = dirEntry
  70. }
  71. }
  72. if lastDirectoryEntry == nil {
  73. return fmt.Errorf("parent folder not found: %v", entry.FullPath)
  74. }
  75. /*
  76. if !hasWritePermission(lastDirectoryEntry, entry) {
  77. glog.V(0).Infof("directory %s: %v, entry: uid=%d gid=%d",
  78. lastDirectoryEntry.FullPath, lastDirectoryEntry.Attr, entry.Uid, entry.Gid)
  79. return fmt.Errorf("no write permission in folder %v", lastDirectoryEntry.FullPath)
  80. }
  81. */
  82. if oldEntry, err := f.FindEntry(entry.FullPath); err == nil {
  83. f.deleteChunks(oldEntry)
  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) (entry *Entry, err error) {
  94. return f.store.FindEntry(p)
  95. }
  96. func (f *Filer) DeleteEntryMetaAndData(p FullPath) (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. f.deleteChunks(entry)
  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. }
  139. func (f *Filer) deleteChunks(entry *Entry) {
  140. if f.master == "" {
  141. return
  142. }
  143. if entry == nil {
  144. return
  145. }
  146. for _, chunk := range entry.Chunks {
  147. if err := operation.DeleteFile(f.master, chunk.FileId, ""); err != nil {
  148. glog.V(0).Infof("deleting file %s: %v", chunk.FileId, err)
  149. }
  150. }
  151. }