2020-09-01 15:21:19 +08:00
|
|
|
package filer
|
2018-05-26 14:27:06 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-03-23 15:01:34 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-05-26 14:27:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Attr struct {
|
2018-12-26 14:45:44 +08:00
|
|
|
Mtime time.Time // time of last modification
|
|
|
|
Crtime time.Time // time of creation (OS X only)
|
|
|
|
Mode os.FileMode // file mode
|
|
|
|
Uid uint32 // owner uid
|
|
|
|
Gid uint32 // group gid
|
|
|
|
Mime string // mime type
|
|
|
|
Replication string // replication
|
|
|
|
Collection string // collection name
|
|
|
|
TtlSec int32 // ttl in seconds
|
|
|
|
UserName string
|
|
|
|
GroupNames []string
|
|
|
|
SymlinkTarget string
|
2020-04-08 23:12:00 +08:00
|
|
|
Md5 []byte
|
2020-08-16 00:32:47 +08:00
|
|
|
FileSize uint64
|
2018-05-26 14:27:06 +08:00
|
|
|
}
|
|
|
|
|
2018-05-28 02:52:26 +08:00
|
|
|
func (attr Attr) IsDirectory() bool {
|
2018-05-26 14:27:06 +08:00
|
|
|
return attr.Mode&os.ModeDir > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
type Entry struct {
|
2020-03-23 15:01:34 +08:00
|
|
|
util.FullPath
|
2018-05-26 14:27:06 +08:00
|
|
|
|
|
|
|
Attr
|
2019-12-18 13:10:26 +08:00
|
|
|
Extended map[string][]byte
|
2018-05-26 14:27:06 +08:00
|
|
|
|
|
|
|
// the following is for files
|
|
|
|
Chunks []*filer_pb.FileChunk `json:"chunks,omitempty"`
|
2020-09-24 18:06:44 +08:00
|
|
|
|
|
|
|
HardLinkId HardLinkId
|
|
|
|
HardLinkCounter int32
|
2018-05-26 14:27:06 +08:00
|
|
|
}
|
|
|
|
|
2018-05-26 18:49:46 +08:00
|
|
|
func (entry *Entry) Size() uint64 {
|
2020-08-16 00:32:47 +08:00
|
|
|
return maxUint64(TotalSize(entry.Chunks), entry.FileSize)
|
2018-05-26 14:27:06 +08:00
|
|
|
}
|
|
|
|
|
2018-05-26 18:49:46 +08:00
|
|
|
func (entry *Entry) Timestamp() time.Time {
|
2018-05-26 14:27:06 +08:00
|
|
|
if entry.IsDirectory() {
|
|
|
|
return entry.Crtime
|
|
|
|
} else {
|
|
|
|
return entry.Mtime
|
|
|
|
}
|
|
|
|
}
|
2018-11-04 03:43:45 +08:00
|
|
|
|
|
|
|
func (entry *Entry) ToProtoEntry() *filer_pb.Entry {
|
|
|
|
if entry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &filer_pb.Entry{
|
2020-09-24 18:06:44 +08:00
|
|
|
Name: entry.FullPath.Name(),
|
|
|
|
IsDirectory: entry.IsDirectory(),
|
|
|
|
Attributes: EntryAttributeToPb(entry),
|
|
|
|
Chunks: entry.Chunks,
|
|
|
|
Extended: entry.Extended,
|
2020-09-25 02:11:42 +08:00
|
|
|
HardLinkId: entry.HardLinkId,
|
2020-09-24 18:06:44 +08:00
|
|
|
HardLinkCounter: entry.HardLinkCounter,
|
2018-11-04 03:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 15:44:31 +08:00
|
|
|
|
|
|
|
func (entry *Entry) ToProtoFullEntry() *filer_pb.FullEntry {
|
|
|
|
if entry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dir, _ := entry.FullPath.DirAndName()
|
|
|
|
return &filer_pb.FullEntry{
|
|
|
|
Dir: dir,
|
|
|
|
Entry: entry.ToProtoEntry(),
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 12:16:13 +08:00
|
|
|
|
2020-09-24 18:06:44 +08:00
|
|
|
func (entry *Entry) Clone() *Entry {
|
|
|
|
return &Entry{
|
|
|
|
FullPath: entry.FullPath,
|
|
|
|
Attr: entry.Attr,
|
|
|
|
Chunks: entry.Chunks,
|
|
|
|
Extended: entry.Extended,
|
|
|
|
HardLinkId: entry.HardLinkId,
|
|
|
|
HardLinkCounter: entry.HardLinkCounter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 12:16:13 +08:00
|
|
|
func FromPbEntry(dir string, entry *filer_pb.Entry) *Entry {
|
|
|
|
return &Entry{
|
2020-09-24 18:06:44 +08:00
|
|
|
FullPath: util.NewFullPath(dir, entry.Name),
|
|
|
|
Attr: PbToEntryAttribute(entry.Attributes),
|
|
|
|
Chunks: entry.Chunks,
|
|
|
|
HardLinkId: HardLinkId(entry.HardLinkId),
|
|
|
|
HardLinkCounter: entry.HardLinkCounter,
|
2020-04-22 12:16:13 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-16 00:32:47 +08:00
|
|
|
|
|
|
|
func maxUint64(x, y uint64) uint64 {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|