entry.go 3.3 KB

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