filehandle.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "os"
  8. "sync"
  9. )
  10. type FileHandleId uint64
  11. var IsDebugFileReadWrite = false
  12. type FileHandle struct {
  13. fh FileHandleId
  14. counter int64
  15. entry *LockedEntry
  16. entryLock sync.RWMutex
  17. entryChunkGroup *filer.ChunkGroup
  18. inode uint64
  19. wfs *WFS
  20. // cache file has been written to
  21. dirtyMetadata bool
  22. dirtyPages *PageWriter
  23. reader *filer.ChunkReadAt
  24. contentType string
  25. sync.RWMutex
  26. isDeleted bool
  27. // for debugging
  28. mirrorFile *os.File
  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. }
  37. // dirtyPages: newContinuousDirtyPages(file, writeOnly),
  38. fh.dirtyPages = newPageWriter(fh, wfs.option.ChunkSizeLimit)
  39. fh.entry = &LockedEntry{
  40. Entry: entry,
  41. }
  42. if entry != nil {
  43. fh.SetEntry(entry)
  44. }
  45. if IsDebugFileReadWrite {
  46. var err error
  47. fh.mirrorFile, err = os.OpenFile("/tmp/sw/"+entry.Name, os.O_RDWR|os.O_CREATE, 0600)
  48. if err != nil {
  49. println("failed to create mirror:", err.Error())
  50. }
  51. }
  52. return fh
  53. }
  54. func (fh *FileHandle) FullPath() util.FullPath {
  55. fp, _ := fh.wfs.inodeToPath.GetPath(fh.inode)
  56. return fp
  57. }
  58. func (fh *FileHandle) GetEntry() *filer_pb.Entry {
  59. return fh.entry.GetEntry()
  60. }
  61. func (fh *FileHandle) SetEntry(entry *filer_pb.Entry) {
  62. if entry != nil {
  63. fileSize := filer.FileSize(entry)
  64. entry.Attributes.FileSize = fileSize
  65. var resolveManifestErr error
  66. fh.entryChunkGroup, resolveManifestErr = filer.NewChunkGroup(fh.wfs.LookupFn(), fh.wfs.chunkCache, entry.Chunks)
  67. if resolveManifestErr != nil {
  68. glog.Warningf("failed to resolve manifest chunks in %+v", entry)
  69. }
  70. } else {
  71. glog.Fatalf("setting file handle entry to nil")
  72. }
  73. fh.entry.SetEntry(entry)
  74. }
  75. func (fh *FileHandle) UpdateEntry(fn func(entry *filer_pb.Entry)) *filer_pb.Entry {
  76. return fh.entry.UpdateEntry(fn)
  77. }
  78. func (fh *FileHandle) AddChunks(chunks []*filer_pb.FileChunk) {
  79. fh.entryLock.Lock()
  80. defer fh.entryLock.Unlock()
  81. if fh.entry == nil {
  82. return
  83. }
  84. fh.entry.AppendChunks(chunks)
  85. }
  86. func (fh *FileHandle) ReleaseHandle() {
  87. fh.Lock()
  88. defer fh.Unlock()
  89. fh.entryLock.Lock()
  90. defer fh.entryLock.Unlock()
  91. fh.dirtyPages.Destroy()
  92. if IsDebugFileReadWrite {
  93. fh.mirrorFile.Close()
  94. }
  95. }
  96. func lessThan(a, b *filer_pb.FileChunk) bool {
  97. if a.ModifiedTsNs == b.ModifiedTsNs {
  98. return a.Fid.FileKey < b.Fid.FileKey
  99. }
  100. return a.ModifiedTsNs < b.ModifiedTsNs
  101. }