bep_extensions.go 6.2 KB

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