truncated.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package db
  16. import "github.com/syncthing/protocol"
  17. type FileInfoTruncated struct {
  18. protocol.FileInfo
  19. ActualSize int64
  20. }
  21. func ToTruncated(file protocol.FileInfo) FileInfoTruncated {
  22. t := FileInfoTruncated{
  23. FileInfo: file,
  24. ActualSize: file.Size(),
  25. }
  26. t.FileInfo.Blocks = nil
  27. return t
  28. }
  29. func (f *FileInfoTruncated) UnmarshalXDR(bs []byte) error {
  30. err := f.FileInfo.UnmarshalXDR(bs)
  31. f.ActualSize = f.FileInfo.Size()
  32. f.FileInfo.Blocks = nil
  33. return err
  34. }
  35. func (f FileInfoTruncated) Size() int64 {
  36. return f.ActualSize
  37. }
  38. func BlocksToSize(num int) int64 {
  39. if num < 2 {
  40. return protocol.BlockSize / 2
  41. }
  42. return int64(num-1)*protocol.BlockSize + protocol.BlockSize/2
  43. }