filehandle.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.Mutex
  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. handle uint64
  26. sync.Mutex
  27. isDeleted bool
  28. // for debugging
  29. mirrorFile *os.File
  30. }
  31. func newFileHandle(wfs *WFS, handleId FileHandleId, inode uint64, entry *filer_pb.Entry) *FileHandle {
  32. fh := &FileHandle{
  33. fh: handleId,
  34. counter: 1,
  35. inode: inode,
  36. wfs: wfs,
  37. }
  38. // dirtyPages: newContinuousDirtyPages(file, writeOnly),
  39. fh.dirtyPages = newPageWriter(fh, wfs.option.ChunkSizeLimit)
  40. fh.entry = &LockedEntry{
  41. Entry: entry,
  42. }
  43. if entry != nil {
  44. fh.SetEntry(entry)
  45. }
  46. if IsDebugFileReadWrite {
  47. var err error
  48. fh.mirrorFile, err = os.OpenFile("/tmp/sw/"+entry.Name, os.O_RDWR|os.O_CREATE, 0600)
  49. if err != nil {
  50. println("failed to create mirror:", err.Error())
  51. }
  52. }
  53. return fh
  54. }
  55. func (fh *FileHandle) FullPath() util.FullPath {
  56. fp, _ := fh.wfs.inodeToPath.GetPath(fh.inode)
  57. return fp
  58. }
  59. func (fh *FileHandle) GetEntry() *filer_pb.Entry {
  60. return fh.entry.GetEntry()
  61. }
  62. func (fh *FileHandle) SetEntry(entry *filer_pb.Entry) {
  63. if entry != nil {
  64. fileSize := filer.FileSize(entry)
  65. entry.Attributes.FileSize = fileSize
  66. var resolveManifestErr error
  67. fh.entryChunkGroup, resolveManifestErr = filer.NewChunkGroup(fh.wfs.LookupFn(), fh.wfs.chunkCache, entry.Chunks)
  68. if resolveManifestErr != nil {
  69. glog.Warningf("failed to resolve manifest chunks in %+v", entry)
  70. }
  71. } else {
  72. glog.Fatalf("setting file handle entry to nil")
  73. }
  74. fh.entry.SetEntry(entry)
  75. }
  76. func (fh *FileHandle) UpdateEntry(fn func(entry *filer_pb.Entry)) *filer_pb.Entry {
  77. return fh.entry.UpdateEntry(fn)
  78. }
  79. func (fh *FileHandle) AddChunks(chunks []*filer_pb.FileChunk) {
  80. fh.entryLock.Lock()
  81. defer fh.entryLock.Unlock()
  82. if fh.entry == nil {
  83. return
  84. }
  85. fh.entry.AppendChunks(chunks)
  86. }
  87. func (fh *FileHandle) ReleaseHandle() {
  88. fh.entryLock.Lock()
  89. defer fh.entryLock.Unlock()
  90. fh.dirtyPages.Destroy()
  91. if IsDebugFileReadWrite {
  92. fh.mirrorFile.Close()
  93. }
  94. }
  95. func lessThan(a, b *filer_pb.FileChunk) bool {
  96. if a.ModifiedTsNs == b.ModifiedTsNs {
  97. return a.Fid.FileKey < b.Fid.FileKey
  98. }
  99. return a.ModifiedTsNs < b.ModifiedTsNs
  100. }