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.

255 lines
5.9 KiB

7 years ago
7 years ago
7 years ago
5 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. var _ = fs.NodeGetxattrer(&File{})
  20. var _ = fs.NodeSetxattrer(&File{})
  21. var _ = fs.NodeRemovexattrer(&File{})
  22. var _ = fs.NodeListxattrer(&File{})
  23. type File struct {
  24. Name string
  25. dir *Dir
  26. wfs *WFS
  27. entry *filer_pb.Entry
  28. entryViewCache []filer2.VisibleInterval
  29. isOpen bool
  30. }
  31. func (file *File) fullpath() string {
  32. return filepath.Join(file.dir.Path, file.Name)
  33. }
  34. func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error {
  35. glog.V(4).Infof("file Attr %s", file.fullpath())
  36. if err := file.maybeLoadEntry(ctx); err != nil {
  37. return err
  38. }
  39. attr.Mode = os.FileMode(file.entry.Attributes.FileMode)
  40. attr.Size = filer2.TotalSize(file.entry.Chunks)
  41. attr.Mtime = time.Unix(file.entry.Attributes.Mtime, 0)
  42. attr.Gid = file.entry.Attributes.Gid
  43. attr.Uid = file.entry.Attributes.Uid
  44. attr.Blocks = attr.Size/blockSize + 1
  45. attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit)
  46. return nil
  47. }
  48. func (file *File) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  49. glog.V(4).Infof("file Getxattr %s", file.fullpath())
  50. if err := file.maybeLoadEntry(ctx); err != nil {
  51. return err
  52. }
  53. return getxattr(file.entry, req, resp)
  54. }
  55. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  56. glog.V(4).Infof("file %v open %+v", file.fullpath(), req)
  57. file.isOpen = true
  58. handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid)
  59. resp.Handle = fuse.HandleID(handle.handle)
  60. glog.V(3).Infof("%v file open handle id = %d", file.fullpath(), handle.handle)
  61. return handle, nil
  62. }
  63. func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  64. if err := file.maybeLoadEntry(ctx); err != nil {
  65. return err
  66. }
  67. glog.V(3).Infof("%v file setattr %+v, old:%+v", file.fullpath(), req, file.entry.Attributes)
  68. if req.Valid.Size() {
  69. glog.V(3).Infof("%v file setattr set size=%v", file.fullpath(), req.Size)
  70. if req.Size == 0 {
  71. // fmt.Printf("truncate %v \n", fullPath)
  72. file.entry.Chunks = nil
  73. file.entryViewCache = nil
  74. }
  75. file.entry.Attributes.FileSize = req.Size
  76. }
  77. if req.Valid.Mode() {
  78. file.entry.Attributes.FileMode = uint32(req.Mode)
  79. }
  80. if req.Valid.Uid() {
  81. file.entry.Attributes.Uid = req.Uid
  82. }
  83. if req.Valid.Gid() {
  84. file.entry.Attributes.Gid = req.Gid
  85. }
  86. if req.Valid.Crtime() {
  87. file.entry.Attributes.Crtime = req.Crtime.Unix()
  88. }
  89. if req.Valid.Mtime() {
  90. file.entry.Attributes.Mtime = req.Mtime.Unix()
  91. }
  92. if file.isOpen {
  93. return nil
  94. }
  95. file.wfs.listDirectoryEntriesCache.Delete(file.fullpath())
  96. return file.saveEntry(ctx)
  97. }
  98. func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  99. glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name)
  100. if err := file.maybeLoadEntry(ctx); err != nil {
  101. return err
  102. }
  103. if err := setxattr(file.entry, req); err != nil {
  104. return err
  105. }
  106. file.wfs.listDirectoryEntriesCache.Delete(file.fullpath())
  107. return file.saveEntry(ctx)
  108. }
  109. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  110. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  111. if err := file.maybeLoadEntry(ctx); err != nil {
  112. return err
  113. }
  114. if err := removexattr(file.entry, req); err != nil {
  115. return err
  116. }
  117. file.wfs.listDirectoryEntriesCache.Delete(file.fullpath())
  118. return file.saveEntry(ctx)
  119. }
  120. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  121. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  122. if err := file.maybeLoadEntry(ctx); err != nil {
  123. return err
  124. }
  125. if err := listxattr(file.entry, req, resp); err != nil {
  126. return err
  127. }
  128. return nil
  129. }
  130. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  131. // fsync works at OS level
  132. // write the file chunks to the filerGrpcAddress
  133. glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
  134. return nil
  135. }
  136. func (file *File) maybeLoadEntry(ctx context.Context) error {
  137. if file.entry == nil || !file.isOpen {
  138. entry, err := file.wfs.maybeLoadEntry(ctx, file.dir.Path, file.Name)
  139. if err != nil {
  140. return err
  141. }
  142. if entry != nil {
  143. file.setEntry(entry)
  144. }
  145. }
  146. return nil
  147. }
  148. func (file *File) addChunk(chunk *filer_pb.FileChunk) {
  149. if chunk != nil {
  150. file.addChunks([]*filer_pb.FileChunk{chunk})
  151. }
  152. }
  153. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  154. sort.Slice(chunks, func(i, j int) bool {
  155. return chunks[i].Mtime < chunks[j].Mtime
  156. })
  157. var newVisibles []filer2.VisibleInterval
  158. for _, chunk := range chunks {
  159. newVisibles = filer2.MergeIntoVisibles(file.entryViewCache, newVisibles, chunk)
  160. t := file.entryViewCache[:0]
  161. file.entryViewCache = newVisibles
  162. newVisibles = t
  163. }
  164. glog.V(3).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks))
  165. file.entry.Chunks = append(file.entry.Chunks, chunks...)
  166. }
  167. func (file *File) setEntry(entry *filer_pb.Entry) {
  168. file.entry = entry
  169. file.entryViewCache = filer2.NonOverlappingVisibleIntervals(file.entry.Chunks)
  170. }
  171. func (file *File) saveEntry(ctx context.Context) error {
  172. return file.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  173. request := &filer_pb.UpdateEntryRequest{
  174. Directory: file.dir.Path,
  175. Entry: file.entry,
  176. }
  177. glog.V(1).Infof("save file entry: %v", request)
  178. _, err := client.UpdateEntry(ctx, request)
  179. if err != nil {
  180. glog.V(0).Infof("UpdateEntry file %s/%s: %v", file.dir.Path, file.Name, err)
  181. return fuse.EIO
  182. }
  183. return nil
  184. })
  185. }