diff --git a/lib/db/truncated.go b/lib/db/truncated.go index c15e529b2..d61f17458 100644 --- a/lib/db/truncated.go +++ b/lib/db/truncated.go @@ -6,22 +6,44 @@ package db -import "github.com/syncthing/syncthing/lib/protocol" +import ( + "bytes" + + "github.com/calmh/xdr" + "github.com/syncthing/syncthing/lib/protocol" +) type FileInfoTruncated struct { protocol.FileInfo - ActualSize int64 } -func (f *FileInfoTruncated) UnmarshalXDR(bs []byte) error { - err := f.FileInfo.UnmarshalXDR(bs) - f.ActualSize = f.FileInfo.Size() - f.FileInfo.Blocks = nil - return err +func (o *FileInfoTruncated) UnmarshalXDR(bs []byte) error { + var br = bytes.NewReader(bs) + var xr = xdr.NewReader(br) + return o.DecodeXDRFrom(xr) } -func (f FileInfoTruncated) Size() int64 { - return f.ActualSize +func (o *FileInfoTruncated) DecodeXDRFrom(xr *xdr.Reader) error { + o.Name = xr.ReadStringMax(8192) + o.Flags = xr.ReadUint32() + o.Modified = int64(xr.ReadUint64()) + (&o.Version).DecodeXDRFrom(xr) + o.LocalVersion = int64(xr.ReadUint64()) + _BlocksSize := int(xr.ReadUint32()) + if _BlocksSize < 0 { + return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000) + } + if _BlocksSize > 1000000 { + return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000) + } + + buf := make([]byte, 64) + for i := 0; i < _BlocksSize; i++ { + size := xr.ReadUint32() + o.CachedSize += int64(size) + xr.ReadBytesMaxInto(64, buf) + } + return xr.Error() } func BlocksToSize(num int) int64 { diff --git a/lib/protocol/message.go b/lib/protocol/message.go index 93353478e..f3791e3f5 100644 --- a/lib/protocol/message.go +++ b/lib/protocol/message.go @@ -33,9 +33,13 @@ func (f FileInfo) Size() (bytes int64) { if f.IsDeleted() || f.IsDirectory() { return 128 } + if f.CachedSize > 0 { + return f.CachedSize + } for _, b := range f.Blocks { bytes += int64(b.Size) } + f.CachedSize = bytes return }