bep_extensions.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "runtime"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/rand"
  13. "github.com/syncthing/syncthing/lib/sha256"
  14. )
  15. const (
  16. SyntheticDirectorySize = 128
  17. )
  18. var (
  19. sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
  20. HelloMessageMagic = uint32(0x2EA7D90B)
  21. )
  22. func (m Hello) Magic() uint32 {
  23. return HelloMessageMagic
  24. }
  25. func (f FileInfo) String() string {
  26. switch f.Type {
  27. case FileInfoTypeDirectory:
  28. return fmt.Sprintf("Directory{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Deleted:%v, Invalid:%v, NoPermissions:%v}",
  29. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Deleted, f.Invalid, f.NoPermissions)
  30. case FileInfoTypeFile:
  31. 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}",
  32. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.Invalid, f.NoPermissions, f.Blocks)
  33. case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
  34. return fmt.Sprintf("Symlink{Name:%q, Type:%v, Sequence:%d, Version:%v, Deleted:%v, Invalid:%v, NoPermissions:%v, SymlinkTarget:%q}",
  35. f.Name, f.Type, f.Sequence, f.Version, f.Deleted, f.Invalid, f.NoPermissions, f.SymlinkTarget)
  36. default:
  37. panic("mystery file type detected")
  38. }
  39. }
  40. func (f FileInfo) IsDeleted() bool {
  41. return f.Deleted
  42. }
  43. func (f FileInfo) IsInvalid() bool {
  44. return f.Invalid
  45. }
  46. func (f FileInfo) IsDirectory() bool {
  47. return f.Type == FileInfoTypeDirectory
  48. }
  49. func (f FileInfo) IsSymlink() bool {
  50. switch f.Type {
  51. case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
  52. return true
  53. default:
  54. return false
  55. }
  56. }
  57. func (f FileInfo) HasPermissionBits() bool {
  58. return !f.NoPermissions
  59. }
  60. func (f FileInfo) FileSize() int64 {
  61. if f.Deleted {
  62. return 0
  63. }
  64. if f.IsDirectory() || f.IsSymlink() {
  65. return SyntheticDirectorySize
  66. }
  67. return f.Size
  68. }
  69. func (f FileInfo) FileName() string {
  70. return f.Name
  71. }
  72. func (f FileInfo) ModTime() time.Time {
  73. return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
  74. }
  75. func (f FileInfo) SequenceNo() int64 {
  76. return f.Sequence
  77. }
  78. // WinsConflict returns true if "f" is the one to choose when it is in
  79. // conflict with "other".
  80. func (f FileInfo) WinsConflict(other FileInfo) bool {
  81. // If only one of the files is invalid, that one loses
  82. if f.IsInvalid() != other.IsInvalid() {
  83. return !f.IsInvalid()
  84. }
  85. // If a modification is in conflict with a delete, we pick the
  86. // modification.
  87. if !f.IsDeleted() && other.IsDeleted() {
  88. return true
  89. }
  90. if f.IsDeleted() && !other.IsDeleted() {
  91. return false
  92. }
  93. // The one with the newer modification time wins.
  94. if f.ModTime().After(other.ModTime()) {
  95. return true
  96. }
  97. if f.ModTime().Before(other.ModTime()) {
  98. return false
  99. }
  100. // The modification times were equal. Use the device ID in the version
  101. // vector as tie breaker.
  102. return f.Version.Compare(other.Version) == ConcurrentGreater
  103. }
  104. func (f FileInfo) IsEmpty() bool {
  105. return f.Version.Counters == nil
  106. }
  107. // IsEquivalent checks that the two file infos represent the same actual file content,
  108. // i.e. it does purposely not check only selected (see below) struct members.
  109. // Permissions (config) and blocks (scanning) can be excluded from the comparison.
  110. // Any file info is not "equivalent", if it has different
  111. // - type
  112. // - deleted flag
  113. // - invalid flag
  114. // - permissions, unless they are ignored
  115. // A file is not "equivalent", if it has different
  116. // - modification time
  117. // - size
  118. // - blocks, unless there are no blocks to compare (scanning)
  119. // A symlink is not "equivalent", if it has different
  120. // - target
  121. // A directory does not have anything specific to check.
  122. func (f FileInfo) IsEquivalent(other FileInfo, ignorePerms bool, ignoreBlocks bool) bool {
  123. if f.Name != other.Name || f.Type != other.Type || f.Deleted != other.Deleted || f.Invalid != other.Invalid {
  124. return false
  125. }
  126. if !ignorePerms && !f.NoPermissions && !other.NoPermissions && !PermsEqual(f.Permissions, other.Permissions) {
  127. return false
  128. }
  129. switch f.Type {
  130. case FileInfoTypeFile:
  131. return f.Size == other.Size && f.ModTime().Equal(other.ModTime()) && (ignoreBlocks || BlocksEqual(f.Blocks, other.Blocks))
  132. case FileInfoTypeSymlink:
  133. return f.SymlinkTarget == other.SymlinkTarget
  134. case FileInfoTypeDirectory:
  135. return true
  136. }
  137. return false
  138. }
  139. func PermsEqual(a, b uint32) bool {
  140. switch runtime.GOOS {
  141. case "windows":
  142. // There is only writeable and read only, represented for user, group
  143. // and other equally. We only compare against user.
  144. return a&0600 == b&0600
  145. default:
  146. // All bits count
  147. return a&0777 == b&0777
  148. }
  149. }
  150. // BlocksEqual returns whether two slices of blocks are exactly the same hash
  151. // and index pair wise.
  152. func BlocksEqual(a, b []BlockInfo) bool {
  153. if len(b) != len(a) {
  154. return false
  155. }
  156. for i, sblk := range a {
  157. if !bytes.Equal(sblk.Hash, b[i].Hash) {
  158. return false
  159. }
  160. }
  161. return true
  162. }
  163. func (f *FileInfo) Invalidate(invalidatedBy ShortID) {
  164. f.Invalid = true
  165. f.ModifiedBy = invalidatedBy
  166. f.Blocks = nil
  167. f.Sequence = 0
  168. }
  169. func (b BlockInfo) String() string {
  170. return fmt.Sprintf("Block{%d/%d/%d/%x}", b.Offset, b.Size, b.WeakHash, b.Hash)
  171. }
  172. // IsEmpty returns true if the block is a full block of zeroes.
  173. func (b BlockInfo) IsEmpty() bool {
  174. return b.Size == BlockSize && bytes.Equal(b.Hash, sha256OfEmptyBlock[:])
  175. }
  176. type IndexID uint64
  177. func (i IndexID) String() string {
  178. return fmt.Sprintf("0x%16X", uint64(i))
  179. }
  180. func (i IndexID) Marshal() ([]byte, error) {
  181. bs := make([]byte, 8)
  182. binary.BigEndian.PutUint64(bs, uint64(i))
  183. return bs, nil
  184. }
  185. func (i *IndexID) Unmarshal(bs []byte) error {
  186. if len(bs) != 8 {
  187. return errors.New("incorrect IndexID length")
  188. }
  189. *i = IndexID(binary.BigEndian.Uint64(bs))
  190. return nil
  191. }
  192. func NewIndexID() IndexID {
  193. return IndexID(rand.Int64())
  194. }
  195. func (f Folder) Description() string {
  196. // used by logging stuff
  197. if f.Label == "" {
  198. return f.ID
  199. }
  200. return fmt.Sprintf("%q (%s)", f.Label, f.ID)
  201. }