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.

199 lines
4.9 KiB

7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "os"
  5. "path/filepath"
  6. "sort"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/fuse"
  12. "github.com/seaweedfs/fuse/fs"
  13. )
  14. const blockSize = 512
  15. var _ = fs.Node(&File{})
  16. var _ = fs.NodeOpener(&File{})
  17. var _ = fs.NodeFsyncer(&File{})
  18. var _ = fs.NodeSetattrer(&File{})
  19. type File struct {
  20. Name string
  21. dir *Dir
  22. wfs *WFS
  23. entry *filer_pb.Entry
  24. entryViewCache []filer2.VisibleInterval
  25. isOpen bool
  26. }
  27. func (file *File) fullpath() string {
  28. return filepath.Join(file.dir.Path, file.Name)
  29. }
  30. func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error {
  31. if err := file.maybeLoadAttributes(ctx); err != nil {
  32. return err
  33. }
  34. attr.Mode = os.FileMode(file.entry.Attributes.FileMode)
  35. attr.Size = filer2.TotalSize(file.entry.Chunks)
  36. attr.Mtime = time.Unix(file.entry.Attributes.Mtime, 0)
  37. attr.Gid = file.entry.Attributes.Gid
  38. attr.Uid = file.entry.Attributes.Uid
  39. attr.Blocks = attr.Size/blockSize + 1
  40. attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit)
  41. return nil
  42. }
  43. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  44. glog.V(3).Infof("%v file open %+v", file.fullpath(), req)
  45. file.isOpen = true
  46. handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid)
  47. resp.Handle = fuse.HandleID(handle.handle)
  48. glog.V(3).Infof("%v file open handle id = %d", file.fullpath(), handle.handle)
  49. return handle, nil
  50. }
  51. func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  52. if err := file.maybeLoadAttributes(ctx); err != nil {
  53. return err
  54. }
  55. glog.V(3).Infof("%v file setattr %+v, old:%+v", file.fullpath(), req, file.entry.Attributes)
  56. if req.Valid.Size() {
  57. glog.V(3).Infof("%v file setattr set size=%v", file.fullpath(), req.Size)
  58. if req.Size == 0 {
  59. // fmt.Printf("truncate %v \n", fullPath)
  60. file.entry.Chunks = nil
  61. file.entryViewCache = nil
  62. }
  63. file.entry.Attributes.FileSize = req.Size
  64. }
  65. if req.Valid.Mode() {
  66. file.entry.Attributes.FileMode = uint32(req.Mode)
  67. }
  68. if req.Valid.Uid() {
  69. file.entry.Attributes.Uid = req.Uid
  70. }
  71. if req.Valid.Gid() {
  72. file.entry.Attributes.Gid = req.Gid
  73. }
  74. if req.Valid.Crtime() {
  75. file.entry.Attributes.Crtime = req.Crtime.Unix()
  76. }
  77. if req.Valid.Mtime() {
  78. file.entry.Attributes.Mtime = req.Mtime.Unix()
  79. }
  80. return file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  81. request := &filer_pb.UpdateEntryRequest{
  82. Directory: file.dir.Path,
  83. Entry: file.entry,
  84. }
  85. glog.V(1).Infof("set attr file entry: %v", request)
  86. _, err := client.UpdateEntry(ctx, request)
  87. if err != nil {
  88. glog.V(0).Infof("UpdateEntry file %s/%s: %v", file.dir.Path, file.Name, err)
  89. return fuse.EIO
  90. }
  91. return nil
  92. })
  93. }
  94. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  95. // fsync works at OS level
  96. // write the file chunks to the filerGrpcAddress
  97. glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
  98. return nil
  99. }
  100. func (file *File) maybeLoadAttributes(ctx context.Context) error {
  101. if file.entry == nil || !file.isOpen {
  102. item := file.wfs.listDirectoryEntriesCache.Get(file.fullpath())
  103. if item != nil && !item.Expired() {
  104. entry := item.Value().(*filer_pb.Entry)
  105. file.setEntry(entry)
  106. // glog.V(1).Infof("file attr read cached %v attributes", file.Name)
  107. } else {
  108. err := file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  109. request := &filer_pb.LookupDirectoryEntryRequest{
  110. Name: file.Name,
  111. Directory: file.dir.Path,
  112. }
  113. resp, err := client.LookupDirectoryEntry(ctx, request)
  114. if err != nil {
  115. glog.V(3).Infof("file attr read file %v: %v", request, err)
  116. return fuse.ENOENT
  117. }
  118. file.setEntry(resp.Entry)
  119. glog.V(3).Infof("file attr %v %+v: %d", file.fullpath(), file.entry.Attributes, filer2.TotalSize(file.entry.Chunks))
  120. // file.wfs.listDirectoryEntriesCache.Set(file.fullpath(), file.entry, file.wfs.option.EntryCacheTtl)
  121. return nil
  122. })
  123. if err != nil {
  124. return err
  125. }
  126. }
  127. }
  128. return nil
  129. }
  130. func (file *File) addChunk(chunk *filer_pb.FileChunk) {
  131. if chunk != nil {
  132. file.addChunks([]*filer_pb.FileChunk{chunk})
  133. }
  134. }
  135. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  136. sort.Slice(chunks, func(i, j int) bool {
  137. return chunks[i].Mtime < chunks[j].Mtime
  138. })
  139. var newVisibles []filer2.VisibleInterval
  140. for _, chunk := range chunks {
  141. newVisibles = filer2.MergeIntoVisibles(file.entryViewCache, newVisibles, chunk)
  142. t := file.entryViewCache[:0]
  143. file.entryViewCache = newVisibles
  144. newVisibles = t
  145. }
  146. file.entry.Chunks = append(file.entry.Chunks, chunks...)
  147. }
  148. func (file *File) setEntry(entry *filer_pb.Entry) {
  149. file.entry = entry
  150. file.entryViewCache = filer2.NonOverlappingVisibleIntervals(file.entry.Chunks)
  151. }