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.

116 lines
2.4 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
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/sync/semaphore"
  8. "math"
  9. "os"
  10. "sync"
  11. )
  12. type FileHandleId uint64
  13. var IsDebug = true
  14. type FileHandle struct {
  15. fh FileHandleId
  16. counter int64
  17. entry *LockedEntry
  18. entryLock sync.Mutex
  19. inode uint64
  20. wfs *WFS
  21. // cache file has been written to
  22. dirtyMetadata bool
  23. dirtyPages *PageWriter
  24. entryViewCache []filer.VisibleInterval
  25. reader *filer.ChunkReadAt
  26. contentType string
  27. handle uint64
  28. orderedMutex *semaphore.Weighted
  29. isDeleted bool
  30. // for debugging
  31. mirrorFile *os.File
  32. }
  33. func newFileHandle(wfs *WFS, handleId FileHandleId, inode uint64, entry *filer_pb.Entry) *FileHandle {
  34. fh := &FileHandle{
  35. fh: handleId,
  36. counter: 1,
  37. inode: inode,
  38. wfs: wfs,
  39. orderedMutex: semaphore.NewWeighted(int64(math.MaxInt64)),
  40. }
  41. // dirtyPages: newContinuousDirtyPages(file, writeOnly),
  42. fh.dirtyPages = newPageWriter(fh, wfs.option.ChunkSizeLimit)
  43. if entry != nil {
  44. entry.Attributes.FileSize = filer.FileSize(entry)
  45. }
  46. fh.entry = &LockedEntry{
  47. Entry: entry,
  48. }
  49. if IsDebug {
  50. var err error
  51. fh.mirrorFile, err = os.OpenFile("/tmp/sw/"+entry.Name, os.O_RDWR|os.O_CREATE, 0600)
  52. if err != nil {
  53. println("failed to create mirror:", err.Error())
  54. }
  55. }
  56. return fh
  57. }
  58. func (fh *FileHandle) FullPath() util.FullPath {
  59. fp, _ := fh.wfs.inodeToPath.GetPath(fh.inode)
  60. return fp
  61. }
  62. func (fh *FileHandle) GetEntry() *filer_pb.Entry {
  63. return fh.entry.GetEntry()
  64. }
  65. func (fh *FileHandle) SetEntry(entry *filer_pb.Entry) {
  66. fh.entry.SetEntry(entry)
  67. }
  68. func (fh *FileHandle) UpdateEntry(fn func(entry *filer_pb.Entry)) *filer_pb.Entry {
  69. return fh.entry.UpdateEntry(fn)
  70. }
  71. func (fh *FileHandle) AddChunks(chunks []*filer_pb.FileChunk) {
  72. fh.entryLock.Lock()
  73. defer fh.entryLock.Unlock()
  74. if fh.entry == nil {
  75. return
  76. }
  77. fh.entry.AppendChunks(chunks)
  78. fh.entryViewCache = nil
  79. }
  80. func (fh *FileHandle) CloseReader() {
  81. if fh.reader != nil {
  82. _ = fh.reader.Close()
  83. fh.reader = nil
  84. }
  85. }
  86. func (fh *FileHandle) Release() {
  87. fh.entryLock.Lock()
  88. defer fh.entryLock.Unlock()
  89. glog.V(4).Infof("Release %s fh %d", fh.entry.Name, fh.handle)
  90. fh.dirtyPages.Destroy()
  91. fh.CloseReader()
  92. if IsDebug {
  93. fh.mirrorFile.Close()
  94. }
  95. }