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.

215 lines
5.8 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/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/gabriel-vasile/mimetype"
  13. "github.com/seaweedfs/fuse"
  14. "github.com/seaweedfs/fuse/fs"
  15. "google.golang.org/grpc"
  16. )
  17. type FileHandle struct {
  18. // cache file has been written to
  19. dirtyPages *ContinuousDirtyPages
  20. contentType string
  21. dirtyMetadata bool
  22. handle uint64
  23. f *File
  24. RequestId fuse.RequestID // unique ID for request
  25. NodeId fuse.NodeID // file or directory the request is about
  26. Uid uint32 // user ID of process making request
  27. Gid uint32 // group ID of process making request
  28. }
  29. func newFileHandle(file *File, uid, gid uint32) *FileHandle {
  30. return &FileHandle{
  31. f: file,
  32. dirtyPages: newDirtyPages(file),
  33. Uid: uid,
  34. Gid: gid,
  35. }
  36. }
  37. var _ = fs.Handle(&FileHandle{})
  38. // var _ = fs.HandleReadAller(&FileHandle{})
  39. var _ = fs.HandleReader(&FileHandle{})
  40. var _ = fs.HandleFlusher(&FileHandle{})
  41. var _ = fs.HandleWriter(&FileHandle{})
  42. var _ = fs.HandleReleaser(&FileHandle{})
  43. func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
  44. glog.V(4).Infof("%s read fh %d: [%d,%d)", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(req.Size))
  45. // this value should come from the filer instead of the old f
  46. if len(fh.f.entry.Chunks) == 0 {
  47. glog.V(1).Infof("empty fh %v/%v", fh.f.dir.Path, fh.f.Name)
  48. return nil
  49. }
  50. buff := make([]byte, req.Size)
  51. if fh.f.entryViewCache == nil {
  52. fh.f.entryViewCache = filer2.NonOverlappingVisibleIntervals(fh.f.entry.Chunks)
  53. }
  54. chunkViews := filer2.ViewFromVisibleIntervals(fh.f.entryViewCache, req.Offset, req.Size)
  55. totalRead, err := filer2.ReadIntoBuffer(ctx, fh.f.wfs, fh.f.fullpath(), buff, chunkViews, req.Offset)
  56. resp.Data = buff[:totalRead]
  57. return err
  58. }
  59. // Write to the file handle
  60. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  61. // write the request to volume servers
  62. 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)))
  63. chunks, err := fh.dirtyPages.AddPage(ctx, req.Offset, req.Data)
  64. if err != nil {
  65. 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)
  66. 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)
  67. }
  68. resp.Size = len(req.Data)
  69. if req.Offset == 0 {
  70. // detect mime type
  71. var possibleExt string
  72. fh.contentType, possibleExt = mimetype.Detect(req.Data)
  73. if ext := path.Ext(fh.f.Name); ext != possibleExt {
  74. fh.contentType = mime.TypeByExtension(ext)
  75. }
  76. fh.dirtyMetadata = true
  77. }
  78. fh.f.addChunks(chunks)
  79. if len(chunks) > 0 {
  80. fh.dirtyMetadata = true
  81. }
  82. return nil
  83. }
  84. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  85. glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
  86. fh.dirtyPages.releaseResource()
  87. fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
  88. fh.f.isOpen = false
  89. return nil
  90. }
  91. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  92. // fflush works at fh level
  93. // send the data to the OS
  94. glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
  95. chunk, err := fh.dirtyPages.FlushToStorage(ctx)
  96. if err != nil {
  97. glog.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  98. return fmt.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  99. }
  100. fh.f.addChunk(chunk)
  101. if !fh.dirtyMetadata {
  102. return nil
  103. }
  104. return fh.f.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  105. if fh.f.entry.Attributes != nil {
  106. fh.f.entry.Attributes.Mime = fh.contentType
  107. fh.f.entry.Attributes.Uid = req.Uid
  108. fh.f.entry.Attributes.Gid = req.Gid
  109. fh.f.entry.Attributes.Mtime = time.Now().Unix()
  110. fh.f.entry.Attributes.Crtime = time.Now().Unix()
  111. fh.f.entry.Attributes.FileMode = uint32(0770)
  112. }
  113. request := &filer_pb.CreateEntryRequest{
  114. Directory: fh.f.dir.Path,
  115. Entry: fh.f.entry,
  116. }
  117. //glog.V(1).Infof("%s/%s set chunks: %v", fh.f.dir.Path, fh.f.Name, len(fh.f.entry.Chunks))
  118. //for i, chunk := range fh.f.entry.Chunks {
  119. // 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))
  120. //}
  121. chunks, garbages := filer2.CompactFileChunks(fh.f.entry.Chunks)
  122. fh.f.entry.Chunks = chunks
  123. // fh.f.entryViewCache = nil
  124. fh.f.wfs.deleteFileChunks(ctx, garbages)
  125. if _, err := client.CreateEntry(ctx, request); err != nil {
  126. return fmt.Errorf("update fh: %v", err)
  127. }
  128. return nil
  129. })
  130. }
  131. func deleteFileIds(ctx context.Context, grpcDialOption grpc.DialOption, client filer_pb.SeaweedFilerClient, fileIds []string) error {
  132. var vids []string
  133. for _, fileId := range fileIds {
  134. vids = append(vids, filer2.VolumeId(fileId))
  135. }
  136. lookupFunc := func(vids []string) (map[string]operation.LookupResult, error) {
  137. m := make(map[string]operation.LookupResult)
  138. glog.V(4).Infof("remove file lookup volume id locations: %v", vids)
  139. resp, err := client.LookupVolume(ctx, &filer_pb.LookupVolumeRequest{
  140. VolumeIds: vids,
  141. })
  142. if err != nil {
  143. return m, err
  144. }
  145. for _, vid := range vids {
  146. lr := operation.LookupResult{
  147. VolumeId: vid,
  148. Locations: nil,
  149. }
  150. locations := resp.LocationsMap[vid]
  151. for _, loc := range locations.Locations {
  152. lr.Locations = append(lr.Locations, operation.Location{
  153. Url: loc.Url,
  154. PublicUrl: loc.PublicUrl,
  155. })
  156. }
  157. m[vid] = lr
  158. }
  159. return m, err
  160. }
  161. _, err := operation.DeleteFilesWithLookupVolumeId(grpcDialOption, fileIds, lookupFunc)
  162. return err
  163. }