entry.go 2.4 KB

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