entry.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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) ToProtoEntry() *filer_pb.Entry {
  50. if entry == nil {
  51. return nil
  52. }
  53. message := &filer_pb.Entry{}
  54. message.Name = entry.FullPath.Name()
  55. entry.ToExistingProtoEntry(message)
  56. return message
  57. }
  58. func (entry *Entry) ToExistingProtoEntry(message *filer_pb.Entry) {
  59. if entry == nil {
  60. return
  61. }
  62. message.IsDirectory = entry.IsDirectory()
  63. message.Attributes = EntryAttributeToPb(entry)
  64. message.Chunks = entry.Chunks
  65. message.Extended = entry.Extended
  66. message.HardLinkId = entry.HardLinkId
  67. message.HardLinkCounter = entry.HardLinkCounter
  68. message.Content = entry.Content
  69. message.RemoteEntry = entry.Remote
  70. }
  71. func FromPbEntryToExistingEntry(message *filer_pb.Entry, fsEntry *Entry) {
  72. fsEntry.Attr = PbToEntryAttribute(message.Attributes)
  73. fsEntry.Chunks = message.Chunks
  74. fsEntry.Extended = message.Extended
  75. fsEntry.HardLinkId = HardLinkId(message.HardLinkId)
  76. fsEntry.HardLinkCounter = message.HardLinkCounter
  77. fsEntry.Content = message.Content
  78. fsEntry.Remote = message.RemoteEntry
  79. }
  80. func (entry *Entry) ToProtoFullEntry() *filer_pb.FullEntry {
  81. if entry == nil {
  82. return nil
  83. }
  84. dir, _ := entry.FullPath.DirAndName()
  85. return &filer_pb.FullEntry{
  86. Dir: dir,
  87. Entry: entry.ToProtoEntry(),
  88. }
  89. }
  90. func FromPbEntry(dir string, entry *filer_pb.Entry) *Entry {
  91. t := &Entry{}
  92. t.FullPath = util.NewFullPath(dir, entry.Name)
  93. FromPbEntryToExistingEntry(entry, t)
  94. return t
  95. }
  96. func maxUint64(x, y uint64) uint64 {
  97. if x > y {
  98. return x
  99. }
  100. return y
  101. }