seaweedfs/weed/filer/entry_codec.go

141 lines
2.8 KiB
Go
Raw Permalink Normal View History

2020-09-01 07:21:19 +00:00
package filer
2018-05-26 06:27:06 +00:00
import (
2019-12-18 05:10:26 +00:00
"bytes"
"fmt"
2018-05-26 06:27:06 +00:00
"os"
"time"
2019-02-19 02:03:16 +00:00
"github.com/golang/protobuf/proto"
2019-12-18 05:10:26 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2018-05-26 06:27:06 +00:00
)
func (entry *Entry) EncodeAttributesAndChunks() ([]byte, error) {
2018-05-26 06:27:06 +00:00
message := &filer_pb.Entry{
2020-09-24 10:06:44 +00:00
Attributes: EntryAttributeToPb(entry),
Chunks: entry.Chunks,
Extended: entry.Extended,
2020-09-24 18:11:42 +00:00
HardLinkId: entry.HardLinkId,
2020-09-24 10:06:44 +00:00
HardLinkCounter: entry.HardLinkCounter,
2018-05-26 06:27:06 +00:00
}
return proto.Marshal(message)
}
2018-05-27 18:52:26 +00:00
func (entry *Entry) DecodeAttributesAndChunks(blob []byte) error {
2018-05-26 06:27:06 +00:00
message := &filer_pb.Entry{}
if err := proto.UnmarshalMerge(blob, message); err != nil {
return fmt.Errorf("decoding value blob for %s: %v", entry.FullPath, err)
}
entry.Attr = PbToEntryAttribute(message.Attributes)
2018-05-26 06:27:06 +00:00
2019-12-18 05:10:26 +00:00
entry.Extended = message.Extended
2018-05-26 06:27:06 +00:00
entry.Chunks = message.Chunks
2020-09-24 18:11:42 +00:00
entry.HardLinkId = message.HardLinkId
2020-09-24 10:06:44 +00:00
entry.HardLinkCounter = message.HardLinkCounter
2018-05-26 06:27:06 +00:00
return nil
}
func EntryAttributeToPb(entry *Entry) *filer_pb.FuseAttributes {
return &filer_pb.FuseAttributes{
2018-12-26 06:45:44 +00: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,
Collection: entry.Attr.Collection,
Replication: entry.Attr.Replication,
TtlSec: entry.Attr.TtlSec,
UserName: entry.Attr.UserName,
GroupName: entry.Attr.GroupNames,
SymlinkTarget: entry.Attr.SymlinkTarget,
Md5: entry.Attr.Md5,
FileSize: entry.Attr.FileSize,
}
}
func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
t := Attr{}
2020-09-24 10:06:44 +00:00
if attr == nil {
return t
}
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.Collection = attr.Collection
t.Replication = attr.Replication
t.TtlSec = attr.TtlSec
2018-12-04 08:13:40 +00:00
t.UserName = attr.UserName
t.GroupNames = attr.GroupName
2018-12-26 06:45:44 +00:00
t.SymlinkTarget = attr.SymlinkTarget
t.Md5 = attr.Md5
t.FileSize = attr.FileSize
return t
}
2018-08-13 08:20:49 +00: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 05:10:26 +00:00
if !eq(a.Extended, b.Extended) {
return false
}
if !bytes.Equal(a.Md5, b.Md5) {
return false
}
2018-08-13 08:20:49 +00:00
for i := 0; i < len(a.Chunks); i++ {
if !proto.Equal(a.Chunks[i], b.Chunks[i]) {
return false
}
}
2020-09-24 10:06:44 +00:00
2020-09-24 18:11:42 +00:00
if !bytes.Equal(a.HardLinkId, b.HardLinkId) {
2020-09-24 10:06:44 +00:00
return false
}
if a.HardLinkCounter != b.HardLinkCounter {
return false
}
2018-08-13 08:20:49 +00:00
return true
}
2019-12-18 05:10:26 +00:00
func eq(a, b map[string][]byte) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
2019-12-18 05:15:28 +00:00
if w, ok := b[k]; !ok || !bytes.Equal(v, w) {
2019-12-18 05:10:26 +00:00
return false
}
}
return true
}