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.

112 lines
2.5 KiB

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