entry.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }
  25. func (attr Attr) IsDirectory() bool {
  26. return attr.Mode&os.ModeDir > 0
  27. }
  28. type Entry struct {
  29. util.FullPath
  30. Attr
  31. Extended map[string][]byte
  32. // the following is for files
  33. Chunks []*filer_pb.FileChunk `json:"chunks,omitempty"`
  34. HardLinkId HardLinkId
  35. HardLinkCounter int32
  36. Content []byte
  37. Remote *filer_pb.RemoteEntry
  38. }
  39. func (entry *Entry) Size() uint64 {
  40. return maxUint64(maxUint64(TotalSize(entry.Chunks), 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. return newEntry
  63. }
  64. func (entry *Entry) ToProtoEntry() *filer_pb.Entry {
  65. if entry == nil {
  66. return nil
  67. }
  68. message := &filer_pb.Entry{}
  69. message.Name = entry.FullPath.Name()
  70. entry.ToExistingProtoEntry(message)
  71. return message
  72. }
  73. func (entry *Entry) ToExistingProtoEntry(message *filer_pb.Entry) {
  74. if entry == nil {
  75. return
  76. }
  77. message.IsDirectory = entry.IsDirectory()
  78. message.Attributes = EntryAttributeToPb(entry)
  79. message.Chunks = entry.Chunks
  80. message.Extended = entry.Extended
  81. message.HardLinkId = entry.HardLinkId
  82. message.HardLinkCounter = entry.HardLinkCounter
  83. message.Content = entry.Content
  84. message.RemoteEntry = entry.Remote
  85. }
  86. func FromPbEntryToExistingEntry(message *filer_pb.Entry, fsEntry *Entry) {
  87. fsEntry.Attr = PbToEntryAttribute(message.Attributes)
  88. fsEntry.Chunks = message.Chunks
  89. fsEntry.Extended = message.Extended
  90. fsEntry.HardLinkId = HardLinkId(message.HardLinkId)
  91. fsEntry.HardLinkCounter = message.HardLinkCounter
  92. fsEntry.Content = message.Content
  93. fsEntry.Remote = message.RemoteEntry
  94. }
  95. func (entry *Entry) ToProtoFullEntry() *filer_pb.FullEntry {
  96. if entry == nil {
  97. return nil
  98. }
  99. dir, _ := entry.FullPath.DirAndName()
  100. return &filer_pb.FullEntry{
  101. Dir: dir,
  102. Entry: entry.ToProtoEntry(),
  103. }
  104. }
  105. func FromPbEntry(dir string, entry *filer_pb.Entry) *Entry {
  106. t := &Entry{}
  107. t.FullPath = util.NewFullPath(dir, entry.Name)
  108. FromPbEntryToExistingEntry(entry, t)
  109. return t
  110. }
  111. func maxUint64(x, y uint64) uint64 {
  112. if x > y {
  113. return x
  114. }
  115. return y
  116. }