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 db
  18. import (
  19. "fmt"
  20. "github.com/syncthing/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 int64
  28. LocalVersion int64
  29. NumBlocks int32
  30. }
  31. func ToTruncated(file protocol.FileInfo) FileInfoTruncated {
  32. return FileInfoTruncated{
  33. Name: file.Name,
  34. Flags: file.Flags,
  35. Modified: file.Modified,
  36. Version: file.Version,
  37. LocalVersion: file.LocalVersion,
  38. NumBlocks: int32(len(file.Blocks)),
  39. }
  40. }
  41. func (f FileInfoTruncated) String() string {
  42. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%d, Size:%d, NumBlocks:%d}",
  43. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.NumBlocks)
  44. }
  45. // Returns a statistical guess on the size, not the exact figure
  46. func (f FileInfoTruncated) Size() int64 {
  47. if f.IsDeleted() || f.IsDirectory() {
  48. return 128
  49. }
  50. return BlocksToSize(int(f.NumBlocks))
  51. }
  52. func (f FileInfoTruncated) IsDeleted() bool {
  53. return f.Flags&protocol.FlagDeleted != 0
  54. }
  55. func (f FileInfoTruncated) IsInvalid() bool {
  56. return f.Flags&protocol.FlagInvalid != 0
  57. }
  58. func (f FileInfoTruncated) IsDirectory() bool {
  59. return f.Flags&protocol.FlagDirectory != 0
  60. }
  61. func (f FileInfoTruncated) IsSymlink() bool {
  62. return f.Flags&protocol.FlagSymlink != 0
  63. }
  64. func (f FileInfoTruncated) HasPermissionBits() bool {
  65. return f.Flags&protocol.FlagNoPermBits == 0
  66. }
  67. func BlocksToSize(num int) int64 {
  68. if num < 2 {
  69. return protocol.BlockSize / 2
  70. }
  71. return int64(num-1)*protocol.BlockSize + protocol.BlockSize/2
  72. }