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.

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