truncated.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package db
  7. import (
  8. "github.com/calmh/xdr"
  9. "github.com/syncthing/syncthing/lib/protocol"
  10. )
  11. type FileInfoTruncated struct {
  12. protocol.FileInfo
  13. }
  14. func (o *FileInfoTruncated) UnmarshalXDR(bs []byte) error {
  15. return o.UnmarshalXDRFrom(&xdr.Unmarshaller{Data: bs})
  16. }
  17. func (o *FileInfoTruncated) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
  18. o.Name = u.UnmarshalStringMax(8192)
  19. o.Flags = u.UnmarshalUint32()
  20. o.Modified = int64(u.UnmarshalUint64())
  21. (&o.Version).UnmarshalXDRFrom(u)
  22. o.LocalVersion = int64(u.UnmarshalUint64())
  23. _BlocksSize := int(u.UnmarshalUint32())
  24. if _BlocksSize < 0 {
  25. return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 10000000)
  26. } else if _BlocksSize == 0 {
  27. o.Blocks = nil
  28. } else {
  29. if _BlocksSize > 10000000 {
  30. return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 10000000)
  31. }
  32. for i := 0; i < _BlocksSize; i++ {
  33. size := int64(u.UnmarshalUint32())
  34. o.CachedSize += size
  35. u.UnmarshalBytes()
  36. }
  37. }
  38. return u.Error
  39. }
  40. func BlocksToSize(num int) int64 {
  41. if num < 2 {
  42. return protocol.BlockSize / 2
  43. }
  44. return int64(num-1)*protocol.BlockSize + protocol.BlockSize/2
  45. }