entry.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package filer
  2. import (
  3. "os"
  4. "time"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/util"
  7. )
  8. type Attr struct {
  9. Mtime time.Time // time of last modification
  10. Crtime time.Time // time of creation (OS X only)
  11. Mode os.FileMode // file mode
  12. Uid uint32 // owner uid
  13. Gid uint32 // group gid
  14. Mime string // mime type
  15. TtlSec int32 // ttl in seconds
  16. UserName string
  17. GroupNames []string
  18. SymlinkTarget string
  19. Md5 []byte
  20. FileSize uint64
  21. Rdev uint32
  22. Inode uint64
  23. }
  24. func (attr Attr) IsDirectory() bool {
  25. return attr.Mode&os.ModeDir > 0
  26. }
  27. type Entry struct {
  28. util.FullPath
  29. Attr
  30. Extended map[string][]byte
  31. // the following is for files
  32. Chunks []*filer_pb.FileChunk `json:"chunks,omitempty"`
  33. HardLinkId HardLinkId
  34. HardLinkCounter int32
  35. Content []byte
  36. Remote *filer_pb.RemoteEntry
  37. Quota int64
  38. }
  39. func (entry *Entry) Size() uint64 {
  40. return maxUint64(maxUint64(TotalSize(entry.GetChunks()), entry.FileSize), uint64(len(entry.Content)))
  41. }
  42. func (entry *Entry) Timestamp() time.Time {
  43. if entry.IsDirectory() {
  44. return entry.Crtime
  45. } else {
  46. return entry.Mtime
  47. }
  48. }
  49. func (entry *Entry) ShallowClone() *Entry {
  50. if entry == nil {
  51. return nil
  52. }
  53. newEntry := &Entry{}
  54. newEntry.FullPath = entry.FullPath
  55. newEntry.Attr = entry.Attr
  56. newEntry.Chunks = entry.Chunks
  57. newEntry.Extended = entry.Extended
  58. newEntry.HardLinkId = entry.HardLinkId
  59. newEntry.HardLinkCounter = entry.HardLinkCounter
  60. newEntry.Content = entry.Content
  61. newEntry.Remote = entry.Remote
  62. newEntry.Quota = entry.Quota
  63. return newEntry
  64. }
  65. func (entry *Entry) ToProtoEntry() *filer_pb.Entry {
  66. if entry == nil {
  67. return nil
  68. }
  69. message := &filer_pb.Entry{}
  70. message.Name = entry.FullPath.Name()
  71. entry.ToExistingProtoEntry(message)
  72. return message
  73. }
  74. func (entry *Entry) ToExistingProtoEntry(message *filer_pb.Entry) {
  75. if entry == nil {
  76. return
  77. }
  78. message.IsDirectory = entry.IsDirectory()
  79. message.Attributes = EntryAttributeToPb(entry)
  80. message.Chunks = entry.GetChunks()
  81. message.Extended = entry.Extended
  82. message.HardLinkId = entry.HardLinkId
  83. message.HardLinkCounter = entry.HardLinkCounter
  84. message.Content = entry.Content
  85. message.RemoteEntry = entry.Remote
  86. message.Quota = entry.Quota
  87. }
  88. func FromPbEntryToExistingEntry(message *filer_pb.Entry, fsEntry *Entry) {
  89. fsEntry.Attr = PbToEntryAttribute(message.Attributes)
  90. fsEntry.Chunks = message.Chunks
  91. fsEntry.Extended = message.Extended
  92. fsEntry.HardLinkId = HardLinkId(message.HardLinkId)
  93. fsEntry.HardLinkCounter = message.HardLinkCounter
  94. fsEntry.Content = message.Content
  95. fsEntry.Remote = message.RemoteEntry
  96. fsEntry.Quota = message.Quota
  97. fsEntry.FileSize = FileSize(message)
  98. }
  99. func (entry *Entry) ToProtoFullEntry() *filer_pb.FullEntry {
  100. if entry == nil {
  101. return nil
  102. }
  103. dir, _ := entry.FullPath.DirAndName()
  104. return &filer_pb.FullEntry{
  105. Dir: dir,
  106. Entry: entry.ToProtoEntry(),
  107. }
  108. }
  109. func (entry *Entry) GetChunks() []*filer_pb.FileChunk {
  110. return entry.Chunks
  111. }
  112. func FromPbEntry(dir string, entry *filer_pb.Entry) *Entry {
  113. t := &Entry{}
  114. t.FullPath = util.NewFullPath(dir, entry.Name)
  115. FromPbEntryToExistingEntry(entry, t)
  116. return t
  117. }
  118. func maxUint64(x, y uint64) uint64 {
  119. if x > y {
  120. return x
  121. }
  122. return y
  123. }