truncated.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. //go:generate -command genxdr go run ../../Godeps/_workspace/src/github.com/calmh/xdr/cmd/genxdr/main.go
  16. //go:generate genxdr -o truncated_xdr.go truncated.go
  17. package files
  18. import (
  19. "fmt"
  20. "github.com/syncthing/syncthing/internal/protocol"
  21. )
  22. // Used for unmarshalling a FileInfo structure but skipping the block list.
  23. type FileInfoTruncated struct {
  24. Name string // max:8192
  25. Flags uint32
  26. Modified int64
  27. Version uint64
  28. LocalVersion uint64
  29. NumBlocks uint32
  30. }
  31. func (f FileInfoTruncated) String() string {
  32. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%d, Size:%d, NumBlocks:%d}",
  33. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.NumBlocks)
  34. }
  35. // Returns a statistical guess on the size, not the exact figure
  36. func (f FileInfoTruncated) Size() int64 {
  37. if f.IsDeleted() || f.IsDirectory() {
  38. return 128
  39. }
  40. return BlocksToSize(f.NumBlocks)
  41. }
  42. func (f FileInfoTruncated) IsDeleted() bool {
  43. return f.Flags&protocol.FlagDeleted != 0
  44. }
  45. func (f FileInfoTruncated) IsInvalid() bool {
  46. return f.Flags&protocol.FlagInvalid != 0
  47. }
  48. func (f FileInfoTruncated) IsDirectory() bool {
  49. return f.Flags&protocol.FlagDirectory != 0
  50. }
  51. func (f FileInfoTruncated) IsSymlink() bool {
  52. return f.Flags&protocol.FlagSymlink != 0
  53. }
  54. func (f FileInfoTruncated) HasPermissionBits() bool {
  55. return f.Flags&protocol.FlagNoPermBits == 0
  56. }
  57. func Truncate(f protocol.FileInfo) FileInfoTruncated {
  58. return FileInfoTruncated{
  59. Name: f.Name,
  60. Flags: f.Flags,
  61. Modified: f.Modified,
  62. Version: f.Version,
  63. LocalVersion: f.LocalVersion,
  64. NumBlocks: uint32(len(f.Blocks)),
  65. }
  66. }
  67. func BlocksToSize(num uint32) int64 {
  68. if num < 2 {
  69. return protocol.BlockSize / 2
  70. }
  71. return int64(num-1)*protocol.BlockSize + protocol.BlockSize/2
  72. }