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.

172 lines
4.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "mime"
  6. "path"
  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/gabriel-vasile/mimetype"
  12. "github.com/seaweedfs/fuse"
  13. "github.com/seaweedfs/fuse/fs"
  14. )
  15. type FileHandle struct {
  16. // cache file has been written to
  17. dirtyPages *ContinuousDirtyPages
  18. contentType string
  19. dirtyMetadata bool
  20. handle uint64
  21. f *File
  22. RequestId fuse.RequestID // unique ID for request
  23. NodeId fuse.NodeID // file or directory the request is about
  24. Uid uint32 // user ID of process making request
  25. Gid uint32 // group ID of process making request
  26. }
  27. func newFileHandle(file *File, uid, gid uint32) *FileHandle {
  28. return &FileHandle{
  29. f: file,
  30. dirtyPages: newDirtyPages(file),
  31. Uid: uid,
  32. Gid: gid,
  33. }
  34. }
  35. var _ = fs.Handle(&FileHandle{})
  36. // var _ = fs.HandleReadAller(&FileHandle{})
  37. var _ = fs.HandleReader(&FileHandle{})
  38. var _ = fs.HandleFlusher(&FileHandle{})
  39. var _ = fs.HandleWriter(&FileHandle{})
  40. var _ = fs.HandleReleaser(&FileHandle{})
  41. func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
  42. glog.V(4).Infof("%s read fh %d: [%d,%d)", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(req.Size))
  43. // this value should come from the filer instead of the old f
  44. if len(fh.f.entry.Chunks) == 0 {
  45. glog.V(1).Infof("empty fh %v/%v", fh.f.dir.Path, fh.f.Name)
  46. return nil
  47. }
  48. buff := make([]byte, req.Size)
  49. if fh.f.entryViewCache == nil {
  50. fh.f.entryViewCache = filer2.NonOverlappingVisibleIntervals(fh.f.entry.Chunks)
  51. }
  52. chunkViews := filer2.ViewFromVisibleIntervals(fh.f.entryViewCache, req.Offset, req.Size)
  53. totalRead, err := filer2.ReadIntoBuffer(ctx, fh.f.wfs, fh.f.fullpath(), buff, chunkViews, req.Offset)
  54. resp.Data = buff[:totalRead]
  55. return err
  56. }
  57. // Write to the file handle
  58. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  59. // write the request to volume servers
  60. glog.V(4).Infof("%+v/%v write fh %d: [%d,%d)", fh.f.dir.Path, fh.f.Name, fh.handle, req.Offset, req.Offset+int64(len(req.Data)))
  61. chunks, err := fh.dirtyPages.AddPage(ctx, req.Offset, req.Data)
  62. if err != nil {
  63. glog.Errorf("%+v/%v write fh %d: [%d,%d): %v", fh.f.dir.Path, fh.f.Name, fh.handle, req.Offset, req.Offset+int64(len(req.Data)), err)
  64. return fmt.Errorf("write %s/%s at [%d,%d): %v", fh.f.dir.Path, fh.f.Name, req.Offset, req.Offset+int64(len(req.Data)), err)
  65. }
  66. resp.Size = len(req.Data)
  67. if req.Offset == 0 {
  68. // detect mime type
  69. var possibleExt string
  70. fh.contentType, possibleExt = mimetype.Detect(req.Data)
  71. if ext := path.Ext(fh.f.Name); ext != possibleExt {
  72. fh.contentType = mime.TypeByExtension(ext)
  73. }
  74. fh.dirtyMetadata = true
  75. }
  76. fh.f.addChunks(chunks)
  77. if len(chunks) > 0 {
  78. fh.dirtyMetadata = true
  79. }
  80. return nil
  81. }
  82. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  83. glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
  84. fh.dirtyPages.releaseResource()
  85. fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
  86. fh.f.isOpen = false
  87. return nil
  88. }
  89. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  90. // fflush works at fh level
  91. // send the data to the OS
  92. glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
  93. chunk, err := fh.dirtyPages.FlushToStorage(ctx)
  94. if err != nil {
  95. glog.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  96. return fmt.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  97. }
  98. fh.f.addChunk(chunk)
  99. if !fh.dirtyMetadata {
  100. return nil
  101. }
  102. return fh.f.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  103. if fh.f.entry.Attributes != nil {
  104. fh.f.entry.Attributes.Mime = fh.contentType
  105. fh.f.entry.Attributes.Uid = req.Uid
  106. fh.f.entry.Attributes.Gid = req.Gid
  107. fh.f.entry.Attributes.Mtime = time.Now().Unix()
  108. fh.f.entry.Attributes.Crtime = time.Now().Unix()
  109. fh.f.entry.Attributes.FileMode = uint32(0770)
  110. }
  111. request := &filer_pb.CreateEntryRequest{
  112. Directory: fh.f.dir.Path,
  113. Entry: fh.f.entry,
  114. }
  115. //glog.V(1).Infof("%s/%s set chunks: %v", fh.f.dir.Path, fh.f.Name, len(fh.f.entry.Chunks))
  116. //for i, chunk := range fh.f.entry.Chunks {
  117. // glog.V(4).Infof("%s/%s chunks %d: %v [%d,%d)", fh.f.dir.Path, fh.f.Name, i, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
  118. //}
  119. chunks, garbages := filer2.CompactFileChunks(fh.f.entry.Chunks)
  120. fh.f.entry.Chunks = chunks
  121. // fh.f.entryViewCache = nil
  122. fh.f.wfs.deleteFileChunks(ctx, garbages)
  123. if _, err := client.CreateEntry(ctx, request); err != nil {
  124. return fmt.Errorf("update fh: %v", err)
  125. }
  126. return nil
  127. })
  128. }