You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.4 KiB

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