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
5 years ago
7 years ago
4 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/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. Quota int64
  39. }
  40. func (entry *Entry) Size() uint64 {
  41. return maxUint64(maxUint64(TotalSize(entry.Chunks), 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.Chunks
  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. }
  89. func FromPbEntryToExistingEntry(message *filer_pb.Entry, fsEntry *Entry) {
  90. fsEntry.Attr = PbToEntryAttribute(message.Attributes)
  91. fsEntry.Chunks = message.Chunks
  92. fsEntry.Extended = message.Extended
  93. fsEntry.HardLinkId = HardLinkId(message.HardLinkId)
  94. fsEntry.HardLinkCounter = message.HardLinkCounter
  95. fsEntry.Content = message.Content
  96. fsEntry.Remote = message.RemoteEntry
  97. fsEntry.Quota = message.Quota
  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. }