bep_extensions.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (C) 2014 The Protocol Authors.
  2. //go:generate go run ../../script/protofmt.go bep.proto
  3. //go:generate protoc -I ../../vendor/ -I ../../vendor/github.com/gogo/protobuf/protobuf -I . --gogofast_out=. bep.proto
  4. package protocol
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "errors"
  9. "fmt"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/rand"
  12. "github.com/syncthing/syncthing/lib/sha256"
  13. )
  14. const (
  15. SyntheticDirectorySize = 128
  16. )
  17. var (
  18. sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
  19. HelloMessageMagic = uint32(0x2EA7D90B)
  20. )
  21. func (m Hello) Magic() uint32 {
  22. return HelloMessageMagic
  23. }
  24. func (f FileInfo) String() string {
  25. switch f.Type {
  26. case FileInfoTypeDirectory:
  27. return fmt.Sprintf("Directory{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Deleted:%v, Invalid:%v, NoPermissions:%v}",
  28. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Deleted, f.Invalid, f.NoPermissions)
  29. case FileInfoTypeFile:
  30. return fmt.Sprintf("File{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Length:%d, Deleted:%v, Invalid:%v, NoPermissions:%v, Blocks:%v}",
  31. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.Invalid, f.NoPermissions, f.Blocks)
  32. case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
  33. return fmt.Sprintf("Symlink{Name:%q, Type:%v, Sequence:%d, Version:%v, Deleted:%v, Invalid:%v, NoPermissions:%v, SymlinkTarget:%q}",
  34. f.Name, f.Type, f.Sequence, f.Version, f.Deleted, f.Invalid, f.NoPermissions, f.SymlinkTarget)
  35. default:
  36. panic("mystery file type detected")
  37. }
  38. }
  39. func (f FileInfo) IsDeleted() bool {
  40. return f.Deleted
  41. }
  42. func (f FileInfo) IsInvalid() bool {
  43. return f.Invalid
  44. }
  45. func (f FileInfo) IsDirectory() bool {
  46. return f.Type == FileInfoTypeDirectory
  47. }
  48. func (f FileInfo) IsSymlink() bool {
  49. switch f.Type {
  50. case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
  51. return true
  52. default:
  53. return false
  54. }
  55. }
  56. func (f FileInfo) HasPermissionBits() bool {
  57. return !f.NoPermissions
  58. }
  59. func (f FileInfo) FileSize() int64 {
  60. if f.Deleted {
  61. return 0
  62. }
  63. if f.IsDirectory() || f.IsSymlink() {
  64. return SyntheticDirectorySize
  65. }
  66. return f.Size
  67. }
  68. func (f FileInfo) FileName() string {
  69. return f.Name
  70. }
  71. func (f FileInfo) ModTime() time.Time {
  72. return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
  73. }
  74. // WinsConflict returns true if "f" is the one to choose when it is in
  75. // conflict with "other".
  76. func (f FileInfo) WinsConflict(other FileInfo) bool {
  77. // If a modification is in conflict with a delete, we pick the
  78. // modification.
  79. if !f.IsDeleted() && other.IsDeleted() {
  80. return true
  81. }
  82. if f.IsDeleted() && !other.IsDeleted() {
  83. return false
  84. }
  85. // The one with the newer modification time wins.
  86. if f.ModTime().After(other.ModTime()) {
  87. return true
  88. }
  89. if f.ModTime().Before(other.ModTime()) {
  90. return false
  91. }
  92. // The modification times were equal. Use the device ID in the version
  93. // vector as tie breaker.
  94. return f.Version.Compare(other.Version) == ConcurrentGreater
  95. }
  96. func (f *FileInfo) Invalidate(invalidatedBy ShortID) {
  97. f.Invalid = true
  98. f.ModifiedBy = invalidatedBy
  99. f.Blocks = nil
  100. f.Sequence = 0
  101. }
  102. func (b BlockInfo) String() string {
  103. return fmt.Sprintf("Block{%d/%d/%d/%x}", b.Offset, b.Size, b.WeakHash, b.Hash)
  104. }
  105. // IsEmpty returns true if the block is a full block of zeroes.
  106. func (b BlockInfo) IsEmpty() bool {
  107. return b.Size == BlockSize && bytes.Equal(b.Hash, sha256OfEmptyBlock[:])
  108. }
  109. type IndexID uint64
  110. func (i IndexID) String() string {
  111. return fmt.Sprintf("0x%16X", uint64(i))
  112. }
  113. func (i IndexID) Marshal() ([]byte, error) {
  114. bs := make([]byte, 8)
  115. binary.BigEndian.PutUint64(bs, uint64(i))
  116. return bs, nil
  117. }
  118. func (i *IndexID) Unmarshal(bs []byte) error {
  119. if len(bs) != 8 {
  120. return errors.New("incorrect IndexID length")
  121. }
  122. *i = IndexID(binary.BigEndian.Uint64(bs))
  123. return nil
  124. }
  125. func NewIndexID() IndexID {
  126. return IndexID(rand.Int64())
  127. }
  128. func (f Folder) Description() string {
  129. // used by logging stuff
  130. if f.Label == "" {
  131. return f.ID
  132. }
  133. return fmt.Sprintf("%q (%s)", f.Label, f.ID)
  134. }