filer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. return f.store.DeleteEntry(p)
  90. }
  91. func (f *Filer) ListDirectoryEntries(p FullPath) ([]*Entry, error) {
  92. if strings.HasSuffix(string(p), "/") {
  93. p = p[0:len(p)-1]
  94. }
  95. return f.store.ListDirectoryEntries(p)
  96. }
  97. func (f *Filer) cacheGetDirectory(dirpath string) (*Entry) {
  98. if f.directoryCache == nil {
  99. return nil
  100. }
  101. item := f.directoryCache.Get(dirpath)
  102. if item == nil {
  103. return nil
  104. }
  105. return item.Value().(*Entry)
  106. }
  107. func (f *Filer) cacheSetDirectory(dirpath string, dirEntry *Entry, level int) {
  108. if f.directoryCache == nil {
  109. return
  110. }
  111. minutes := 60
  112. if level < 10 {
  113. minutes -= level * 6
  114. }
  115. f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute)
  116. }