truncated.go 934 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 "github.com/syncthing/protocol"
  8. type FileInfoTruncated struct {
  9. protocol.FileInfo
  10. ActualSize int64
  11. }
  12. func ToTruncated(file protocol.FileInfo) FileInfoTruncated {
  13. t := FileInfoTruncated{
  14. FileInfo: file,
  15. ActualSize: file.Size(),
  16. }
  17. t.FileInfo.Blocks = nil
  18. return t
  19. }
  20. func (f *FileInfoTruncated) UnmarshalXDR(bs []byte) error {
  21. err := f.FileInfo.UnmarshalXDR(bs)
  22. f.ActualSize = f.FileInfo.Size()
  23. f.FileInfo.Blocks = nil
  24. return err
  25. }
  26. func (f FileInfoTruncated) Size() int64 {
  27. return f.ActualSize
  28. }
  29. func BlocksToSize(num int) int64 {
  30. if num < 2 {
  31. return protocol.BlockSize / 2
  32. }
  33. return int64(num-1)*protocol.BlockSize + protocol.BlockSize/2
  34. }