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.

164 lines
3.6 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/pb/filer_pb"
  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. dirFound := false
  36. // first check local cache
  37. dirEntry := f.cacheGetDirectory(dirPath)
  38. // not found, check the store directly
  39. if dirEntry == nil {
  40. var dirFindErr error
  41. dirFound, dirEntry, dirFindErr = f.FindEntry(FullPath(dirPath))
  42. if dirFindErr != nil {
  43. return fmt.Errorf("findDirectory %s: %v", dirPath, dirFindErr)
  44. }
  45. }
  46. // no such existing directory
  47. if !dirFound {
  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 | 0660,
  56. Uid: entry.Uid,
  57. Gid: entry.Gid,
  58. },
  59. }
  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. if !hasWritePermission(lastDirectoryEntry, entry) {
  76. return fmt.Errorf("no write permission in folder %v", lastDirectoryEntry.FullPath)
  77. }
  78. if err := f.store.InsertEntry(entry); err != nil {
  79. return fmt.Errorf("insert entry %s: %v", entry.FullPath, err)
  80. }
  81. return nil
  82. }
  83. func (f *Filer) AppendFileChunk(p FullPath, chunks []*filer_pb.FileChunk) (err error) {
  84. return f.store.AppendFileChunk(p, chunks)
  85. }
  86. func (f *Filer) FindEntry(p FullPath) (found bool, entry *Entry, err error) {
  87. return f.store.FindEntry(p)
  88. }
  89. func (f *Filer) DeleteEntry(p FullPath) (fileEntry *Entry, err error) {
  90. found, entry, err := f.FindEntry(p)
  91. if err != nil || !found {
  92. return nil, err
  93. }
  94. if entry.IsDirectory() {
  95. entries, err := f.ListDirectoryEntries(p, "", false, 1)
  96. if err != nil {
  97. return nil, fmt.Errorf("list folder %s: %v", p, err)
  98. }
  99. if len(entries) > 0 {
  100. return nil, fmt.Errorf("folder %s is not empty", p)
  101. }
  102. }
  103. return f.store.DeleteEntry(p)
  104. }
  105. func (f *Filer) ListDirectoryEntries(p FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) {
  106. if strings.HasSuffix(string(p), "/") && len(p) > 1 {
  107. p = p[0:len(p)-1]
  108. }
  109. return f.store.ListDirectoryEntries(p, startFileName, inclusive, limit)
  110. }
  111. func (f *Filer) cacheGetDirectory(dirpath string) (*Entry) {
  112. if f.directoryCache == nil {
  113. return nil
  114. }
  115. item := f.directoryCache.Get(dirpath)
  116. if item == nil {
  117. return nil
  118. }
  119. return item.Value().(*Entry)
  120. }
  121. func (f *Filer) cacheSetDirectory(dirpath string, dirEntry *Entry, level int) {
  122. if f.directoryCache == nil {
  123. return
  124. }
  125. minutes := 60
  126. if level < 10 {
  127. minutes -= level * 6
  128. }
  129. f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute)
  130. }