2020-09-01 15:21:19 +08:00
|
|
|
package filer
|
2018-05-26 14:27:06 +08:00
|
|
|
|
|
|
|
import (
|
2019-12-18 13:10:26 +08:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-05-26 14:27:06 +08:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2022-08-18 03:05:07 +08:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-12-18 13:10:26 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
2018-05-26 14:27:06 +08:00
|
|
|
)
|
|
|
|
|
2018-05-26 18:49:46 +08:00
|
|
|
func (entry *Entry) EncodeAttributesAndChunks() ([]byte, error) {
|
2021-07-19 17:47:27 +08:00
|
|
|
message := &filer_pb.Entry{}
|
|
|
|
entry.ToExistingProtoEntry(message)
|
2018-05-26 14:27:06 +08:00
|
|
|
return proto.Marshal(message)
|
|
|
|
}
|
|
|
|
|
2018-05-28 02:52:26 +08:00
|
|
|
func (entry *Entry) DecodeAttributesAndChunks(blob []byte) error {
|
2018-05-26 14:27:06 +08:00
|
|
|
|
|
|
|
message := &filer_pb.Entry{}
|
|
|
|
|
2022-08-18 03:42:03 +08:00
|
|
|
if err := proto.Unmarshal(blob, message); err != nil {
|
2018-05-26 14:27:06 +08:00
|
|
|
return fmt.Errorf("decoding value blob for %s: %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:47:27 +08:00
|
|
|
FromPbEntryToExistingEntry(message, entry)
|
2020-09-24 18:06:44 +08:00
|
|
|
|
2018-05-26 14:27:06 +08:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-11 07:57:32 +08:00
|
|
|
|
|
|
|
func EntryAttributeToPb(entry *Entry) *filer_pb.FuseAttributes {
|
|
|
|
|
|
|
|
return &filer_pb.FuseAttributes{
|
2018-12-26 14:45:44 +08:00
|
|
|
Crtime: entry.Attr.Crtime.Unix(),
|
|
|
|
Mtime: entry.Attr.Mtime.Unix(),
|
|
|
|
FileMode: uint32(entry.Attr.Mode),
|
|
|
|
Uid: entry.Uid,
|
|
|
|
Gid: entry.Gid,
|
|
|
|
Mime: entry.Mime,
|
|
|
|
TtlSec: entry.Attr.TtlSec,
|
|
|
|
UserName: entry.Attr.UserName,
|
|
|
|
GroupName: entry.Attr.GroupNames,
|
|
|
|
SymlinkTarget: entry.Attr.SymlinkTarget,
|
2020-04-08 23:12:00 +08:00
|
|
|
Md5: entry.Attr.Md5,
|
2020-08-16 00:32:47 +08:00
|
|
|
FileSize: entry.Attr.FileSize,
|
2022-02-25 06:51:25 +08:00
|
|
|
Rdev: entry.Attr.Rdev,
|
2022-02-25 16:53:27 +08:00
|
|
|
Inode: entry.Attr.Inode,
|
2018-06-11 07:57:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
|
|
|
|
|
|
|
|
t := Attr{}
|
|
|
|
|
2020-09-24 18:06:44 +08:00
|
|
|
if attr == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2018-06-11 07:57:32 +08:00
|
|
|
t.Crtime = time.Unix(attr.Crtime, 0)
|
|
|
|
t.Mtime = time.Unix(attr.Mtime, 0)
|
|
|
|
t.Mode = os.FileMode(attr.FileMode)
|
|
|
|
t.Uid = attr.Uid
|
|
|
|
t.Gid = attr.Gid
|
|
|
|
t.Mime = attr.Mime
|
|
|
|
t.TtlSec = attr.TtlSec
|
2018-12-04 16:13:40 +08:00
|
|
|
t.UserName = attr.UserName
|
|
|
|
t.GroupNames = attr.GroupName
|
2018-12-26 14:45:44 +08:00
|
|
|
t.SymlinkTarget = attr.SymlinkTarget
|
2020-04-08 23:12:00 +08:00
|
|
|
t.Md5 = attr.Md5
|
2020-08-16 00:32:47 +08:00
|
|
|
t.FileSize = attr.FileSize
|
2022-02-25 06:51:25 +08:00
|
|
|
t.Rdev = attr.Rdev
|
2022-02-25 16:53:27 +08:00
|
|
|
t.Inode = attr.Inode
|
2018-06-11 07:57:32 +08:00
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
2018-08-13 16:20:49 +08:00
|
|
|
|
|
|
|
func EqualEntry(a, b *Entry) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a == nil && b != nil || a != nil && b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !proto.Equal(EntryAttributeToPb(a), EntryAttributeToPb(b)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(a.Chunks) != len(b.Chunks) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-12-18 13:10:26 +08:00
|
|
|
if !eq(a.Extended, b.Extended) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-08 23:12:00 +08:00
|
|
|
if !bytes.Equal(a.Md5, b.Md5) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:20:49 +08:00
|
|
|
for i := 0; i < len(a.Chunks); i++ {
|
|
|
|
if !proto.Equal(a.Chunks[i], b.Chunks[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 18:06:44 +08:00
|
|
|
|
2020-09-25 02:11:42 +08:00
|
|
|
if !bytes.Equal(a.HardLinkId, b.HardLinkId) {
|
2020-09-24 18:06:44 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if a.HardLinkCounter != b.HardLinkCounter {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-30 20:34:04 +08:00
|
|
|
if !bytes.Equal(a.Content, b.Content) {
|
|
|
|
return false
|
|
|
|
}
|
2021-07-19 17:47:27 +08:00
|
|
|
if !proto.Equal(a.Remote, b.Remote) {
|
|
|
|
return false
|
|
|
|
}
|
2022-01-21 17:42:20 +08:00
|
|
|
if a.Quota != b.Quota {
|
|
|
|
return false
|
|
|
|
}
|
2018-08-13 16:20:49 +08:00
|
|
|
return true
|
|
|
|
}
|
2019-12-18 13:10:26 +08:00
|
|
|
|
|
|
|
func eq(a, b map[string][]byte) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range a {
|
2019-12-18 13:15:28 +08:00
|
|
|
if w, ok := b[k]; !ok || !bytes.Equal(v, w) {
|
2019-12-18 13:10:26 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|