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.

163 lines
3.5 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
  1. package filer2
  2. import (
  3. "fmt"
  4. "github.com/karlseguin/ccache"
  5. "strings"
  6. "path/filepath"
  7. "time"
  8. "os"
  9. )
  10. type Filer struct {
  11. master string
  12. store FilerStore
  13. directoryCache *ccache.Cache
  14. }
  15. func NewFiler(master string) *Filer {
  16. return &Filer{
  17. master: master,
  18. directoryCache: ccache.New(ccache.Configure().MaxSize(1000).ItemsToPrune(100)),
  19. }
  20. }
  21. func (f *Filer) SetStore(store FilerStore) () {
  22. f.store = store
  23. }
  24. func (f *Filer) DisableDirectoryCache() () {
  25. f.directoryCache = nil
  26. }
  27. func (f *Filer) CreateEntry(entry *Entry) (error) {
  28. dirParts := strings.Split(string(entry.FullPath), "/")
  29. // fmt.Printf("directory parts: %+v\n", dirParts)
  30. var lastDirectoryEntry *Entry
  31. for i := 1; i < len(dirParts); i++ {
  32. dirPath := "/" + filepath.Join(dirParts[:i]...)
  33. // fmt.Printf("%d directory: %+v\n", i, dirPath)
  34. dirFound := false
  35. // first check local cache
  36. dirEntry := f.cacheGetDirectory(dirPath)
  37. // not found, check the store directly
  38. if dirEntry == nil {
  39. var dirFindErr error
  40. dirFound, dirEntry, dirFindErr = f.FindEntry(FullPath(dirPath))
  41. if dirFindErr != nil {
  42. return fmt.Errorf("findDirectory %s: %v", dirPath, dirFindErr)
  43. }
  44. }
  45. // no such existing directory
  46. if !dirFound {
  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 | 0660,
  55. Uid: entry.Uid,
  56. Gid: entry.Gid,
  57. },
  58. }
  59. mkdirErr := f.store.InsertEntry(dirEntry)
  60. if mkdirErr != nil {
  61. return fmt.Errorf("mkdir %s: %v", dirPath, mkdirErr)
  62. }
  63. }
  64. // cache the directory entry
  65. f.cacheSetDirectory(dirPath, dirEntry, i)
  66. // remember the direct parent directory entry
  67. if i == len(dirParts)-1 {
  68. lastDirectoryEntry = dirEntry
  69. }
  70. }
  71. if lastDirectoryEntry == nil {
  72. return fmt.Errorf("parent folder not found: %v", entry.FullPath)
  73. }
  74. if !hasWritePermission(lastDirectoryEntry, entry) {
  75. return fmt.Errorf("no write permission in folder %v", lastDirectoryEntry.FullPath)
  76. }
  77. if err := f.store.InsertEntry(entry); err != nil {
  78. return fmt.Errorf("insert entry %s: %v", entry.FullPath, err)
  79. }
  80. return nil
  81. }
  82. func (f *Filer) AppendFileChunk(p FullPath, c FileChunk) (err error) {
  83. return f.store.AppendFileChunk(p, c)
  84. }
  85. func (f *Filer) FindEntry(p FullPath) (found bool, entry *Entry, err error) {
  86. return f.store.FindEntry(p)
  87. }
  88. func (f *Filer) DeleteEntry(p FullPath) (fileEntry *Entry, err error) {
  89. found, entry, err := f.FindEntry(p)
  90. if err != nil || !found {
  91. return nil, err
  92. }
  93. if entry.IsDirectory() {
  94. entries, err := f.ListDirectoryEntries(p, "", false, 1)
  95. if err != nil {
  96. return nil, fmt.Errorf("list folder %s: %v", p, err)
  97. }
  98. if len(entries) > 0 {
  99. return nil, fmt.Errorf("folder %s is not empty", p)
  100. }
  101. }
  102. return f.store.DeleteEntry(p)
  103. }
  104. func (f *Filer) ListDirectoryEntries(p FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) {
  105. if strings.HasSuffix(string(p), "/") && len(p) > 1 {
  106. p = p[0:len(p)-1]
  107. }
  108. return f.store.ListDirectoryEntries(p, startFileName, inclusive, limit)
  109. }
  110. func (f *Filer) cacheGetDirectory(dirpath string) (*Entry) {
  111. if f.directoryCache == nil {
  112. return nil
  113. }
  114. item := f.directoryCache.Get(dirpath)
  115. if item == nil {
  116. return nil
  117. }
  118. return item.Value().(*Entry)
  119. }
  120. func (f *Filer) cacheSetDirectory(dirpath string, dirEntry *Entry, level int) {
  121. if f.directoryCache == nil {
  122. return
  123. }
  124. minutes := 60
  125. if level < 10 {
  126. minutes -= level * 6
  127. }
  128. f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute)
  129. }