entry.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }
  38. func (entry *Entry) Size() uint64 {
  39. return maxUint64(maxUint64(TotalSize(entry.Chunks), entry.FileSize), uint64(len(entry.Content)))
  40. }
  41. func (entry *Entry) Timestamp() time.Time {
  42. if entry.IsDirectory() {
  43. return entry.Crtime
  44. } else {
  45. return entry.Mtime
  46. }
  47. }
  48. func (entry *Entry) ToProtoEntry() *filer_pb.Entry {
  49. if entry == nil {
  50. return nil
  51. }
  52. return &filer_pb.Entry{
  53. Name: entry.FullPath.Name(),
  54. IsDirectory: entry.IsDirectory(),
  55. Attributes: EntryAttributeToPb(entry),
  56. Chunks: entry.Chunks,
  57. Extended: entry.Extended,
  58. HardLinkId: entry.HardLinkId,
  59. HardLinkCounter: entry.HardLinkCounter,
  60. Content: entry.Content,
  61. }
  62. }
  63. func (entry *Entry) ToProtoFullEntry() *filer_pb.FullEntry {
  64. if entry == nil {
  65. return nil
  66. }
  67. dir, _ := entry.FullPath.DirAndName()
  68. return &filer_pb.FullEntry{
  69. Dir: dir,
  70. Entry: entry.ToProtoEntry(),
  71. }
  72. }
  73. func (entry *Entry) Clone() *Entry {
  74. return &Entry{
  75. FullPath: entry.FullPath,
  76. Attr: entry.Attr,
  77. Chunks: entry.Chunks,
  78. Extended: entry.Extended,
  79. HardLinkId: entry.HardLinkId,
  80. HardLinkCounter: entry.HardLinkCounter,
  81. }
  82. }
  83. func FromPbEntry(dir string, entry *filer_pb.Entry) *Entry {
  84. return &Entry{
  85. FullPath: util.NewFullPath(dir, entry.Name),
  86. Attr: PbToEntryAttribute(entry.Attributes),
  87. Chunks: entry.Chunks,
  88. HardLinkId: HardLinkId(entry.HardLinkId),
  89. HardLinkCounter: entry.HardLinkCounter,
  90. Content: entry.Content,
  91. }
  92. }
  93. func maxUint64(x, y uint64) uint64 {
  94. if x > y {
  95. return x
  96. }
  97. return y
  98. }