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.

117 lines
2.7 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. }
  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) ToProtoEntry() *filer_pb.Entry {
  50. if entry == nil {
  51. return nil
  52. }
  53. message := &filer_pb.Entry{}
  54. message.Name = entry.FullPath.Name()
  55. entry.ToExistingProtoEntry(message)
  56. return message
  57. }
  58. func (entry *Entry) ToExistingProtoEntry(message *filer_pb.Entry) {
  59. if entry == nil {
  60. return
  61. }
  62. message.IsDirectory = entry.IsDirectory()
  63. message.Attributes = EntryAttributeToPb(entry)
  64. message.Chunks = entry.Chunks
  65. message.Extended = entry.Extended
  66. message.HardLinkId = entry.HardLinkId
  67. message.HardLinkCounter = entry.HardLinkCounter
  68. message.Content = entry.Content
  69. message.RemoteEntry = entry.Remote
  70. }
  71. func FromPbEntryToExistingEntry(message *filer_pb.Entry, fsEntry *Entry) {
  72. fsEntry.Attr = PbToEntryAttribute(message.Attributes)
  73. fsEntry.Chunks = message.Chunks
  74. fsEntry.Extended = message.Extended
  75. fsEntry.HardLinkId = HardLinkId(message.HardLinkId)
  76. fsEntry.HardLinkCounter = message.HardLinkCounter
  77. fsEntry.Content = message.Content
  78. fsEntry.Remote = message.RemoteEntry
  79. }
  80. func (entry *Entry) ToProtoFullEntry() *filer_pb.FullEntry {
  81. if entry == nil {
  82. return nil
  83. }
  84. dir, _ := entry.FullPath.DirAndName()
  85. return &filer_pb.FullEntry{
  86. Dir: dir,
  87. Entry: entry.ToProtoEntry(),
  88. }
  89. }
  90. func FromPbEntry(dir string, entry *filer_pb.Entry) *Entry {
  91. t := &Entry{}
  92. t.FullPath = util.NewFullPath(dir, entry.Name)
  93. FromPbEntryToExistingEntry(entry, t)
  94. return t
  95. }
  96. func maxUint64(x, y uint64) uint64 {
  97. if x > y {
  98. return x
  99. }
  100. return y
  101. }