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.

138 lines
3.2 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
3 years ago
7 years ago
7 years ago
7 years ago
3 years ago
3 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. }
  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. newEntry.Quota = entry.Quota
  63. return newEntry
  64. }
  65. func (entry *Entry) ToProtoEntry() *filer_pb.Entry {
  66. if entry == nil {
  67. return nil
  68. }
  69. message := &filer_pb.Entry{}
  70. message.Name = entry.FullPath.Name()
  71. entry.ToExistingProtoEntry(message)
  72. return message
  73. }
  74. func (entry *Entry) ToExistingProtoEntry(message *filer_pb.Entry) {
  75. if entry == nil {
  76. return
  77. }
  78. message.IsDirectory = entry.IsDirectory()
  79. message.Attributes = EntryAttributeToPb(entry)
  80. message.Chunks = entry.Chunks
  81. message.Extended = entry.Extended
  82. message.HardLinkId = entry.HardLinkId
  83. message.HardLinkCounter = entry.HardLinkCounter
  84. message.Content = entry.Content
  85. message.RemoteEntry = entry.Remote
  86. message.Quota = entry.Quota
  87. }
  88. func FromPbEntryToExistingEntry(message *filer_pb.Entry, fsEntry *Entry) {
  89. fsEntry.Attr = PbToEntryAttribute(message.Attributes)
  90. fsEntry.Chunks = message.Chunks
  91. fsEntry.Extended = message.Extended
  92. fsEntry.HardLinkId = HardLinkId(message.HardLinkId)
  93. fsEntry.HardLinkCounter = message.HardLinkCounter
  94. fsEntry.Content = message.Content
  95. fsEntry.Remote = message.RemoteEntry
  96. fsEntry.Quota = message.Quota
  97. fsEntry.FileSize = FileSize(message)
  98. }
  99. func (entry *Entry) ToProtoFullEntry() *filer_pb.FullEntry {
  100. if entry == nil {
  101. return nil
  102. }
  103. dir, _ := entry.FullPath.DirAndName()
  104. return &filer_pb.FullEntry{
  105. Dir: dir,
  106. Entry: entry.ToProtoEntry(),
  107. }
  108. }
  109. func FromPbEntry(dir string, entry *filer_pb.Entry) *Entry {
  110. t := &Entry{}
  111. t.FullPath = util.NewFullPath(dir, entry.Name)
  112. FromPbEntryToExistingEntry(entry, t)
  113. return t
  114. }
  115. func maxUint64(x, y uint64) uint64 {
  116. if x > y {
  117. return x
  118. }
  119. return y
  120. }