2020-09-01 07:21:19 +00:00
|
|
|
package filer
|
2018-05-26 06:27:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-05-26 06:27:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Attr struct {
|
2018-12-26 06:45:44 +00: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
|
|
|
|
TtlSec int32 // ttl in seconds
|
|
|
|
UserName string
|
|
|
|
GroupNames []string
|
|
|
|
SymlinkTarget string
|
2020-04-08 15:12:00 +00:00
|
|
|
Md5 []byte
|
2020-08-15 16:32:47 +00:00
|
|
|
FileSize uint64
|
2022-02-24 22:51:25 +00:00
|
|
|
Rdev uint32
|
2022-02-25 08:53:27 +00:00
|
|
|
Inode uint64
|
2018-05-26 06:27:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 18:52:26 +00:00
|
|
|
func (attr Attr) IsDirectory() bool {
|
2018-05-26 06:27:06 +00:00
|
|
|
return attr.Mode&os.ModeDir > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
type Entry struct {
|
2020-03-23 07:01:34 +00:00
|
|
|
util.FullPath
|
2018-05-26 06:27:06 +00:00
|
|
|
|
|
|
|
Attr
|
2019-12-18 05:10:26 +00:00
|
|
|
Extended map[string][]byte
|
2018-05-26 06:27:06 +00:00
|
|
|
|
|
|
|
// the following is for files
|
|
|
|
Chunks []*filer_pb.FileChunk `json:"chunks,omitempty"`
|
2020-09-24 10:06:44 +00:00
|
|
|
|
|
|
|
HardLinkId HardLinkId
|
|
|
|
HardLinkCounter int32
|
2020-11-30 12:34:04 +00:00
|
|
|
Content []byte
|
2021-07-27 05:53:44 +00:00
|
|
|
Remote *filer_pb.RemoteEntry
|
2022-01-21 09:42:20 +00:00
|
|
|
Quota int64
|
2018-05-26 06:27:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 10:49:46 +00:00
|
|
|
func (entry *Entry) Size() uint64 {
|
2020-12-10 08:15:22 +00:00
|
|
|
return maxUint64(maxUint64(TotalSize(entry.Chunks), entry.FileSize), uint64(len(entry.Content)))
|
2018-05-26 06:27:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 10:49:46 +00:00
|
|
|
func (entry *Entry) Timestamp() time.Time {
|
2018-05-26 06:27:06 +00:00
|
|
|
if entry.IsDirectory() {
|
|
|
|
return entry.Crtime
|
|
|
|
} else {
|
|
|
|
return entry.Mtime
|
|
|
|
}
|
|
|
|
}
|
2018-11-03 19:43:45 +00:00
|
|
|
|
2021-08-09 21:35:18 +00:00
|
|
|
func (entry *Entry) ShallowClone() *Entry {
|
|
|
|
if entry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
newEntry := &Entry{}
|
|
|
|
newEntry.FullPath = entry.FullPath
|
|
|
|
newEntry.Attr = entry.Attr
|
|
|
|
newEntry.Chunks = entry.Chunks
|
|
|
|
newEntry.Extended = entry.Extended
|
|
|
|
newEntry.HardLinkId = entry.HardLinkId
|
|
|
|
newEntry.HardLinkCounter = entry.HardLinkCounter
|
|
|
|
newEntry.Content = entry.Content
|
|
|
|
newEntry.Remote = entry.Remote
|
2022-01-21 09:42:20 +00:00
|
|
|
newEntry.Quota = entry.Quota
|
2021-08-09 21:35:18 +00:00
|
|
|
|
|
|
|
return newEntry
|
|
|
|
}
|
|
|
|
|
2018-11-03 19:43:45 +00:00
|
|
|
func (entry *Entry) ToProtoEntry() *filer_pb.Entry {
|
|
|
|
if entry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-19 09:47:27 +00:00
|
|
|
message := &filer_pb.Entry{}
|
|
|
|
message.Name = entry.FullPath.Name()
|
|
|
|
entry.ToExistingProtoEntry(message)
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) ToExistingProtoEntry(message *filer_pb.Entry) {
|
|
|
|
if entry == nil {
|
|
|
|
return
|
2018-11-03 19:43:45 +00:00
|
|
|
}
|
2021-07-19 09:47:27 +00:00
|
|
|
message.IsDirectory = entry.IsDirectory()
|
|
|
|
message.Attributes = EntryAttributeToPb(entry)
|
|
|
|
message.Chunks = entry.Chunks
|
|
|
|
message.Extended = entry.Extended
|
|
|
|
message.HardLinkId = entry.HardLinkId
|
|
|
|
message.HardLinkCounter = entry.HardLinkCounter
|
|
|
|
message.Content = entry.Content
|
2021-07-27 05:53:44 +00:00
|
|
|
message.RemoteEntry = entry.Remote
|
2022-01-21 09:42:20 +00:00
|
|
|
message.Quota = entry.Quota
|
2021-07-19 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FromPbEntryToExistingEntry(message *filer_pb.Entry, fsEntry *Entry) {
|
|
|
|
fsEntry.Attr = PbToEntryAttribute(message.Attributes)
|
|
|
|
fsEntry.Chunks = message.Chunks
|
|
|
|
fsEntry.Extended = message.Extended
|
|
|
|
fsEntry.HardLinkId = HardLinkId(message.HardLinkId)
|
|
|
|
fsEntry.HardLinkCounter = message.HardLinkCounter
|
|
|
|
fsEntry.Content = message.Content
|
2021-07-27 05:53:44 +00:00
|
|
|
fsEntry.Remote = message.RemoteEntry
|
2022-01-21 09:42:20 +00:00
|
|
|
fsEntry.Quota = message.Quota
|
2018-11-03 19:43:45 +00:00
|
|
|
}
|
2019-04-16 07:44:31 +00: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 04:16:13 +00:00
|
|
|
|
|
|
|
func FromPbEntry(dir string, entry *filer_pb.Entry) *Entry {
|
2021-07-19 09:47:27 +00:00
|
|
|
t := &Entry{}
|
|
|
|
t.FullPath = util.NewFullPath(dir, entry.Name)
|
|
|
|
FromPbEntryToExistingEntry(entry, t)
|
|
|
|
return t
|
2020-04-22 04:16:13 +00:00
|
|
|
}
|
2020-08-15 16:32:47 +00:00
|
|
|
|
|
|
|
func maxUint64(x, y uint64) uint64 {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|