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.

266 lines
5.8 KiB

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