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.

262 lines
5.8 KiB

7 years ago
7 years ago
7 years ago
6 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. "context"
  4. "fmt"
  5. "math"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/wdclient"
  12. "github.com/karlseguin/ccache"
  13. )
  14. var (
  15. OS_UID = uint32(os.Getuid())
  16. OS_GID = uint32(os.Getgid())
  17. )
  18. type Filer struct {
  19. store FilerStore
  20. directoryCache *ccache.Cache
  21. MasterClient *wdclient.MasterClient
  22. fileIdDeletionChan chan string
  23. }
  24. func NewFiler(masters []string) *Filer {
  25. f := &Filer{
  26. directoryCache: ccache.New(ccache.Configure().MaxSize(1000).ItemsToPrune(100)),
  27. MasterClient: wdclient.NewMasterClient(context.Background(), "filer", masters),
  28. fileIdDeletionChan: make(chan string, 4096),
  29. }
  30. go f.loopProcessingDeletion()
  31. return f
  32. }
  33. func (f *Filer) SetStore(store FilerStore) {
  34. f.store = store
  35. }
  36. func (f *Filer) DisableDirectoryCache() {
  37. f.directoryCache = nil
  38. }
  39. func (fs *Filer) GetMaster() string {
  40. return fs.MasterClient.GetMaster()
  41. }
  42. func (fs *Filer) KeepConnectedToMaster() {
  43. fs.MasterClient.KeepConnectedToMaster()
  44. }
  45. func (f *Filer) CreateEntry(entry *Entry) error {
  46. dirParts := strings.Split(string(entry.FullPath), "/")
  47. // fmt.Printf("directory parts: %+v\n", dirParts)
  48. var lastDirectoryEntry *Entry
  49. for i := 1; i < len(dirParts); i++ {
  50. dirPath := "/" + filepath.Join(dirParts[:i]...)
  51. // fmt.Printf("%d directory: %+v\n", i, dirPath)
  52. // first check local cache
  53. dirEntry := f.cacheGetDirectory(dirPath)
  54. // not found, check the store directly
  55. if dirEntry == nil {
  56. glog.V(4).Infof("find uncached directory: %s", dirPath)
  57. dirEntry, _ = f.FindEntry(FullPath(dirPath))
  58. } else {
  59. glog.V(4).Infof("found cached directory: %s", dirPath)
  60. }
  61. // no such existing directory
  62. if dirEntry == nil {
  63. // create the directory
  64. now := time.Now()
  65. dirEntry = &Entry{
  66. FullPath: FullPath(dirPath),
  67. Attr: Attr{
  68. Mtime: now,
  69. Crtime: now,
  70. Mode: os.ModeDir | 0770,
  71. Uid: entry.Uid,
  72. Gid: entry.Gid,
  73. },
  74. }
  75. glog.V(2).Infof("create directory: %s %v", dirPath, dirEntry.Mode)
  76. mkdirErr := f.store.InsertEntry(dirEntry)
  77. if mkdirErr != nil {
  78. if _, err := f.FindEntry(FullPath(dirPath)); err == ErrNotFound {
  79. return fmt.Errorf("mkdir %s: %v", dirPath, mkdirErr)
  80. }
  81. } else {
  82. f.NotifyUpdateEvent(nil, dirEntry, false)
  83. }
  84. } else if !dirEntry.IsDirectory() {
  85. return fmt.Errorf("%s is a file", dirPath)
  86. }
  87. // cache the directory entry
  88. f.cacheSetDirectory(dirPath, dirEntry, i)
  89. // remember the direct parent directory entry
  90. if i == len(dirParts)-1 {
  91. lastDirectoryEntry = dirEntry
  92. }
  93. }
  94. if lastDirectoryEntry == nil {
  95. return fmt.Errorf("parent folder not found: %v", entry.FullPath)
  96. }
  97. /*
  98. if !hasWritePermission(lastDirectoryEntry, entry) {
  99. glog.V(0).Infof("directory %s: %v, entry: uid=%d gid=%d",
  100. lastDirectoryEntry.FullPath, lastDirectoryEntry.Attr, entry.Uid, entry.Gid)
  101. return fmt.Errorf("no write permission in folder %v", lastDirectoryEntry.FullPath)
  102. }
  103. */
  104. oldEntry, _ := f.FindEntry(entry.FullPath)
  105. if oldEntry == nil {
  106. if err := f.store.InsertEntry(entry); err != nil {
  107. return fmt.Errorf("insert entry %s: %v", entry.FullPath, err)
  108. }
  109. } else {
  110. if err := f.UpdateEntry(oldEntry, entry); err != nil {
  111. return fmt.Errorf("update entry %s: %v", entry.FullPath, err)
  112. }
  113. }
  114. f.NotifyUpdateEvent(oldEntry, entry, true)
  115. f.deleteChunksIfNotNew(oldEntry, entry)
  116. return nil
  117. }
  118. func (f *Filer) UpdateEntry(oldEntry, entry *Entry) (err error) {
  119. if oldEntry != nil {
  120. if oldEntry.IsDirectory() && !entry.IsDirectory() {
  121. return fmt.Errorf("existing %s is a directory", entry.FullPath)
  122. }
  123. if !oldEntry.IsDirectory() && entry.IsDirectory() {
  124. return fmt.Errorf("existing %s is a file", entry.FullPath)
  125. }
  126. }
  127. return f.store.UpdateEntry(entry)
  128. }
  129. func (f *Filer) FindEntry(p FullPath) (entry *Entry, err error) {
  130. now := time.Now()
  131. if string(p) == "/" {
  132. return &Entry{
  133. FullPath: p,
  134. Attr: Attr{
  135. Mtime: now,
  136. Crtime: now,
  137. Mode: os.ModeDir | 0755,
  138. Uid: OS_UID,
  139. Gid: OS_GID,
  140. },
  141. }, nil
  142. }
  143. return f.store.FindEntry(p)
  144. }
  145. func (f *Filer) DeleteEntryMetaAndData(p FullPath, isRecursive bool, shouldDeleteChunks bool) (err error) {
  146. entry, err := f.FindEntry(p)
  147. if err != nil {
  148. return err
  149. }
  150. if entry.IsDirectory() {
  151. limit := int(1)
  152. if isRecursive {
  153. limit = math.MaxInt32
  154. }
  155. entries, err := f.ListDirectoryEntries(p, "", false, limit)
  156. if err != nil {
  157. return fmt.Errorf("list folder %s: %v", p, err)
  158. }
  159. if isRecursive {
  160. for _, sub := range entries {
  161. f.DeleteEntryMetaAndData(sub.FullPath, isRecursive, shouldDeleteChunks)
  162. }
  163. } else {
  164. if len(entries) > 0 {
  165. return fmt.Errorf("folder %s is not empty", p)
  166. }
  167. }
  168. f.cacheDelDirectory(string(p))
  169. }
  170. if shouldDeleteChunks {
  171. f.DeleteChunks(entry.Chunks)
  172. }
  173. if p == "/" {
  174. return nil
  175. }
  176. glog.V(3).Infof("deleting entry %v", p)
  177. f.NotifyUpdateEvent(entry, nil, shouldDeleteChunks)
  178. return f.store.DeleteEntry(p)
  179. }
  180. func (f *Filer) ListDirectoryEntries(p FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) {
  181. if strings.HasSuffix(string(p), "/") && len(p) > 1 {
  182. p = p[0 : len(p)-1]
  183. }
  184. return f.store.ListDirectoryEntries(p, startFileName, inclusive, limit)
  185. }
  186. func (f *Filer) cacheDelDirectory(dirpath string) {
  187. if f.directoryCache == nil {
  188. return
  189. }
  190. f.directoryCache.Delete(dirpath)
  191. return
  192. }
  193. func (f *Filer) cacheGetDirectory(dirpath string) *Entry {
  194. if f.directoryCache == nil {
  195. return nil
  196. }
  197. item := f.directoryCache.Get(dirpath)
  198. if item == nil {
  199. return nil
  200. }
  201. return item.Value().(*Entry)
  202. }
  203. func (f *Filer) cacheSetDirectory(dirpath string, dirEntry *Entry, level int) {
  204. if f.directoryCache == nil {
  205. return
  206. }
  207. minutes := 60
  208. if level < 10 {
  209. minutes -= level * 6
  210. }
  211. f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute)
  212. }