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.

205 lines
5.0 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
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. if file.isOpen {
  81. return nil
  82. }
  83. return file.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  84. request := &filer_pb.UpdateEntryRequest{
  85. Directory: file.dir.Path,
  86. Entry: file.entry,
  87. }
  88. glog.V(1).Infof("set attr file entry: %v", request)
  89. _, err := client.UpdateEntry(ctx, request)
  90. if err != nil {
  91. glog.V(0).Infof("UpdateEntry file %s/%s: %v", file.dir.Path, file.Name, err)
  92. return fuse.EIO
  93. }
  94. return nil
  95. })
  96. }
  97. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  98. // fsync works at OS level
  99. // write the file chunks to the filerGrpcAddress
  100. glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
  101. return nil
  102. }
  103. func (file *File) maybeLoadAttributes(ctx context.Context) error {
  104. if file.entry == nil || !file.isOpen {
  105. item := file.wfs.listDirectoryEntriesCache.Get(file.fullpath())
  106. if item != nil && !item.Expired() {
  107. entry := item.Value().(*filer_pb.Entry)
  108. file.setEntry(entry)
  109. // glog.V(1).Infof("file attr read cached %v attributes", file.Name)
  110. } else {
  111. err := file.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  112. request := &filer_pb.LookupDirectoryEntryRequest{
  113. Name: file.Name,
  114. Directory: file.dir.Path,
  115. }
  116. resp, err := client.LookupDirectoryEntry(ctx, request)
  117. if err != nil {
  118. glog.V(3).Infof("file attr read file %v: %v", request, err)
  119. return fuse.ENOENT
  120. }
  121. file.setEntry(resp.Entry)
  122. glog.V(3).Infof("file attr %v %+v: %d", file.fullpath(), file.entry.Attributes, filer2.TotalSize(file.entry.Chunks))
  123. // file.wfs.listDirectoryEntriesCache.Set(file.fullpath(), file.entry, file.wfs.option.EntryCacheTtl)
  124. return nil
  125. })
  126. if err != nil {
  127. return err
  128. }
  129. }
  130. }
  131. return nil
  132. }
  133. func (file *File) addChunk(chunk *filer_pb.FileChunk) {
  134. if chunk != nil {
  135. file.addChunks([]*filer_pb.FileChunk{chunk})
  136. }
  137. }
  138. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  139. sort.Slice(chunks, func(i, j int) bool {
  140. return chunks[i].Mtime < chunks[j].Mtime
  141. })
  142. var newVisibles []filer2.VisibleInterval
  143. for _, chunk := range chunks {
  144. newVisibles = filer2.MergeIntoVisibles(file.entryViewCache, newVisibles, chunk)
  145. t := file.entryViewCache[:0]
  146. file.entryViewCache = newVisibles
  147. newVisibles = t
  148. }
  149. glog.V(3).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks))
  150. file.entry.Chunks = append(file.entry.Chunks, chunks...)
  151. }
  152. func (file *File) setEntry(entry *filer_pb.Entry) {
  153. file.entry = entry
  154. file.entryViewCache = filer2.NonOverlappingVisibleIntervals(file.entry.Chunks)
  155. }