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.

181 lines
5.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 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. if err != nil {
  56. glog.Errorf("file handle read %s: %v", fh.f.fullpath(), err)
  57. }
  58. return err
  59. }
  60. // Write to the file handle
  61. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  62. // write the request to volume servers
  63. 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)))
  64. chunks, err := fh.dirtyPages.AddPage(ctx, req.Offset, req.Data)
  65. if err != nil {
  66. 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)
  67. 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)
  68. }
  69. resp.Size = len(req.Data)
  70. if req.Offset == 0 {
  71. // detect mime type
  72. detectedMIME := mimetype.Detect(req.Data)
  73. fh.contentType = detectedMIME.String()
  74. if ext := path.Ext(fh.f.Name); ext != detectedMIME.Extension() {
  75. fh.contentType = mime.TypeByExtension(ext)
  76. }
  77. fh.dirtyMetadata = true
  78. }
  79. if len(chunks) > 0 {
  80. fh.f.addChunks(chunks)
  81. fh.dirtyMetadata = true
  82. }
  83. return nil
  84. }
  85. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  86. glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
  87. fh.dirtyPages.releaseResource()
  88. fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
  89. fh.f.isOpen = false
  90. return nil
  91. }
  92. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  93. // fflush works at fh level
  94. // send the data to the OS
  95. glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
  96. chunk, err := fh.dirtyPages.FlushToStorage(ctx)
  97. if err != nil {
  98. glog.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  99. return fmt.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  100. }
  101. fh.f.addChunk(chunk)
  102. if !fh.dirtyMetadata {
  103. return nil
  104. }
  105. return fh.f.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  106. if fh.f.entry.Attributes != nil {
  107. fh.f.entry.Attributes.Mime = fh.contentType
  108. fh.f.entry.Attributes.Uid = req.Uid
  109. fh.f.entry.Attributes.Gid = req.Gid
  110. fh.f.entry.Attributes.Mtime = time.Now().Unix()
  111. fh.f.entry.Attributes.Crtime = time.Now().Unix()
  112. fh.f.entry.Attributes.FileMode = uint32(0777 &^ fh.f.wfs.option.Umask)
  113. }
  114. request := &filer_pb.CreateEntryRequest{
  115. Directory: fh.f.dir.Path,
  116. Entry: fh.f.entry,
  117. }
  118. glog.V(3).Infof("%s/%s set chunks: %v", fh.f.dir.Path, fh.f.Name, len(fh.f.entry.Chunks))
  119. for i, chunk := range fh.f.entry.Chunks {
  120. glog.V(3).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))
  121. }
  122. chunks, garbages := filer2.CompactFileChunks(fh.f.entry.Chunks)
  123. fh.f.entry.Chunks = chunks
  124. // fh.f.entryViewCache = nil
  125. if _, err := client.CreateEntry(ctx, request); err != nil {
  126. glog.Errorf("update fh: %v", err)
  127. return fmt.Errorf("update fh: %v", err)
  128. }
  129. fh.f.wfs.deleteFileChunks(ctx, garbages)
  130. for i, chunk := range garbages {
  131. glog.V(3).Infof("garbage %s/%s chunks %d: %v [%d,%d)", fh.f.dir.Path, fh.f.Name, i, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
  132. }
  133. return nil
  134. })
  135. }