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.

101 lines
2.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/filer"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/util"
  7. "golang.org/x/exp/slices"
  8. "golang.org/x/sync/semaphore"
  9. "math"
  10. "sync"
  11. )
  12. type FileHandleId uint64
  13. type FileHandle struct {
  14. fh FileHandleId
  15. counter int64
  16. entry *LockedEntry
  17. entryLock sync.Mutex
  18. inode uint64
  19. wfs *WFS
  20. // cache file has been written to
  21. dirtyMetadata bool
  22. dirtyPages *PageWriter
  23. entryViewCache []filer.VisibleInterval
  24. reader *filer.ChunkReadAt
  25. contentType string
  26. handle uint64
  27. orderedMutex *semaphore.Weighted
  28. isDeleted bool
  29. }
  30. func newFileHandle(wfs *WFS, handleId FileHandleId, inode uint64, entry *filer_pb.Entry) *FileHandle {
  31. fh := &FileHandle{
  32. fh: handleId,
  33. counter: 1,
  34. inode: inode,
  35. wfs: wfs,
  36. orderedMutex: semaphore.NewWeighted(int64(math.MaxInt64)),
  37. }
  38. // dirtyPages: newContinuousDirtyPages(file, writeOnly),
  39. fh.dirtyPages = newPageWriter(fh, wfs.option.ChunkSizeLimit)
  40. if entry != nil {
  41. entry.Attributes.FileSize = filer.FileSize(entry)
  42. }
  43. fh.entry = &LockedEntry{
  44. Entry: entry,
  45. }
  46. return fh
  47. }
  48. func (fh *FileHandle) FullPath() util.FullPath {
  49. fp, _ := fh.wfs.inodeToPath.GetPath(fh.inode)
  50. return fp
  51. }
  52. func (fh *FileHandle) GetEntry() *filer_pb.Entry {
  53. return fh.entry.GetEntry()
  54. }
  55. func (fh *FileHandle) SetEntry(entry *filer_pb.Entry) {
  56. fh.entry.SetEntry(entry)
  57. }
  58. func (fh *FileHandle) UpdateEntry(fn func(entry *filer_pb.Entry)) *filer_pb.Entry {
  59. return fh.entry.UpdateEntry(fn)
  60. }
  61. func (fh *FileHandle) AddChunks(chunks []*filer_pb.FileChunk) {
  62. fh.entryLock.Lock()
  63. defer fh.entryLock.Unlock()
  64. if fh.entry == nil {
  65. return
  66. }
  67. fh.entry.AppendChunks(chunks)
  68. fh.entryViewCache = nil
  69. }
  70. func (fh *FileHandle) CloseReader() {
  71. if fh.reader != nil {
  72. _ = fh.reader.Close()
  73. fh.reader = nil
  74. }
  75. }
  76. func (fh *FileHandle) Release() {
  77. fh.entryLock.Lock()
  78. defer fh.entryLock.Unlock()
  79. glog.V(4).Infof("Release %s fh %d", fh.entry.Name, fh.handle)
  80. fh.dirtyPages.Destroy()
  81. fh.CloseReader()
  82. }
  83. }