truncated.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. "bytes"
  9. "github.com/calmh/xdr"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. )
  12. type FileInfoTruncated struct {
  13. protocol.FileInfo
  14. }
  15. func (o *FileInfoTruncated) UnmarshalXDR(bs []byte) error {
  16. var br = bytes.NewReader(bs)
  17. var xr = xdr.NewReader(br)
  18. return o.DecodeXDRFrom(xr)
  19. }
  20. func (o *FileInfoTruncated) DecodeXDRFrom(xr *xdr.Reader) error {
  21. o.Name = xr.ReadStringMax(8192)
  22. o.Flags = xr.ReadUint32()
  23. o.Modified = int64(xr.ReadUint64())
  24. (&o.Version).DecodeXDRFrom(xr)
  25. o.LocalVersion = int64(xr.ReadUint64())
  26. _BlocksSize := int(xr.ReadUint32())
  27. if _BlocksSize < 0 {
  28. return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000)
  29. }
  30. if _BlocksSize > 1000000 {
  31. return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000)
  32. }
  33. buf := make([]byte, 64)
  34. for i := 0; i < _BlocksSize; i++ {
  35. size := xr.ReadUint32()
  36. o.CachedSize += int64(size)
  37. xr.ReadBytesMaxInto(64, buf)
  38. }
  39. return xr.Error()
  40. }
  41. func BlocksToSize(num int) int64 {
  42. if num < 2 {
  43. return protocol.BlockSize / 2
  44. }
  45. return int64(num-1)*protocol.BlockSize + protocol.BlockSize/2
  46. }