bep_extensions.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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, LocalFlags:0x%x, NoPermissions:%v}",
  25. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Deleted, f.RawInvalid, f.LocalFlags, 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, LocalFlags:0x%x, NoPermissions:%v, BlockSize:%d, Blocks:%v}",
  28. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.RawInvalid, f.LocalFlags, 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, LocalFlags:0x%x, NoPermissions:%v, SymlinkTarget:%q}",
  31. f.Name, f.Type, f.Sequence, f.Version, f.Deleted, f.RawInvalid, f.LocalFlags, 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.RawInvalid || f.LocalFlags&LocalInvalidFlags != 0
  41. }
  42. func (f FileInfo) IsUnsupported() bool {
  43. return f.LocalFlags&FlagLocalUnsupported != 0
  44. }
  45. func (f FileInfo) IsIgnored() bool {
  46. return f.LocalFlags&FlagLocalIgnored != 0
  47. }
  48. func (f FileInfo) MustRescan() bool {
  49. return f.LocalFlags&FlagLocalMustRescan != 0
  50. }
  51. func (f FileInfo) IsReceiveOnlyChanged() bool {
  52. return f.LocalFlags&FlagLocalReceiveOnly != 0
  53. }
  54. func (f FileInfo) IsDirectory() bool {
  55. return f.Type == FileInfoTypeDirectory
  56. }
  57. func (f FileInfo) ShouldConflict() bool {
  58. return f.LocalFlags&LocalConflictFlags != 0
  59. }
  60. func (f FileInfo) IsSymlink() bool {
  61. switch f.Type {
  62. case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
  63. return true
  64. default:
  65. return false
  66. }
  67. }
  68. func (f FileInfo) HasPermissionBits() bool {
  69. return !f.NoPermissions
  70. }
  71. func (f FileInfo) FileSize() int64 {
  72. if f.Deleted {
  73. return 0
  74. }
  75. if f.IsDirectory() || f.IsSymlink() {
  76. return SyntheticDirectorySize
  77. }
  78. return f.Size
  79. }
  80. func (f FileInfo) BlockSize() int {
  81. if f.RawBlockSize == 0 {
  82. return MinBlockSize
  83. }
  84. return int(f.RawBlockSize)
  85. }
  86. func (f FileInfo) FileName() string {
  87. return f.Name
  88. }
  89. func (f FileInfo) FileLocalFlags() uint32 {
  90. return f.LocalFlags
  91. }
  92. func (f FileInfo) ModTime() time.Time {
  93. return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
  94. }
  95. func (f FileInfo) SequenceNo() int64 {
  96. return f.Sequence
  97. }
  98. func (f FileInfo) FileVersion() Vector {
  99. return f.Version
  100. }
  101. // WinsConflict returns true if "f" is the one to choose when it is in
  102. // conflict with "other".
  103. func (f FileInfo) WinsConflict(other FileInfo) bool {
  104. // If only one of the files is invalid, that one loses.
  105. if f.IsInvalid() != other.IsInvalid() {
  106. return !f.IsInvalid()
  107. }
  108. // If a modification is in conflict with a delete, we pick the
  109. // modification.
  110. if !f.IsDeleted() && other.IsDeleted() {
  111. return true
  112. }
  113. if f.IsDeleted() && !other.IsDeleted() {
  114. return false
  115. }
  116. // The one with the newer modification time wins.
  117. if f.ModTime().After(other.ModTime()) {
  118. return true
  119. }
  120. if f.ModTime().Before(other.ModTime()) {
  121. return false
  122. }
  123. // The modification times were equal. Use the device ID in the version
  124. // vector as tie breaker.
  125. return f.Version.Compare(other.Version) == ConcurrentGreater
  126. }
  127. func (f FileInfo) IsEmpty() bool {
  128. return f.Version.Counters == nil
  129. }
  130. func (f FileInfo) IsEquivalent(other FileInfo) bool {
  131. return f.isEquivalent(other, false, false, 0)
  132. }
  133. func (f FileInfo) IsEquivalentOptional(other FileInfo, ignorePerms bool, ignoreBlocks bool, ignoreFlags uint32) bool {
  134. return f.isEquivalent(other, ignorePerms, ignoreBlocks, ignoreFlags)
  135. }
  136. // isEquivalent checks that the two file infos represent the same actual file content,
  137. // i.e. it does purposely not check only selected (see below) struct members.
  138. // Permissions (config) and blocks (scanning) can be excluded from the comparison.
  139. // Any file info is not "equivalent", if it has different
  140. // - type
  141. // - deleted flag
  142. // - invalid flag
  143. // - permissions, unless they are ignored
  144. // A file is not "equivalent", if it has different
  145. // - modification time
  146. // - size
  147. // - blocks, unless there are no blocks to compare (scanning)
  148. // A symlink is not "equivalent", if it has different
  149. // - target
  150. // A directory does not have anything specific to check.
  151. func (f FileInfo) isEquivalent(other FileInfo, ignorePerms bool, ignoreBlocks bool, ignoreFlags uint32) bool {
  152. if f.MustRescan() || other.MustRescan() {
  153. // These are per definition not equivalent because they don't
  154. // represent a valid state, even if both happen to have the
  155. // MustRescan bit set.
  156. return false
  157. }
  158. // Mask out the ignored local flags before checking IsInvalid() below
  159. f.LocalFlags &^= ignoreFlags
  160. other.LocalFlags &^= ignoreFlags
  161. if f.Name != other.Name || f.Type != other.Type || f.Deleted != other.Deleted || f.IsInvalid() != other.IsInvalid() {
  162. return false
  163. }
  164. if !ignorePerms && !f.NoPermissions && !other.NoPermissions && !PermsEqual(f.Permissions, other.Permissions) {
  165. return false
  166. }
  167. switch f.Type {
  168. case FileInfoTypeFile:
  169. return f.Size == other.Size && f.ModTime().Equal(other.ModTime()) && (ignoreBlocks || BlocksEqual(f.Blocks, other.Blocks))
  170. case FileInfoTypeSymlink:
  171. return f.SymlinkTarget == other.SymlinkTarget
  172. case FileInfoTypeDirectory:
  173. return true
  174. }
  175. return false
  176. }
  177. func PermsEqual(a, b uint32) bool {
  178. switch runtime.GOOS {
  179. case "windows":
  180. // There is only writeable and read only, represented for user, group
  181. // and other equally. We only compare against user.
  182. return a&0600 == b&0600
  183. default:
  184. // All bits count
  185. return a&0777 == b&0777
  186. }
  187. }
  188. // BlocksEqual returns whether two slices of blocks are exactly the same hash
  189. // and index pair wise.
  190. func BlocksEqual(a, b []BlockInfo) bool {
  191. if len(b) != len(a) {
  192. return false
  193. }
  194. for i, sblk := range a {
  195. if !bytes.Equal(sblk.Hash, b[i].Hash) {
  196. return false
  197. }
  198. }
  199. return true
  200. }
  201. func (f *FileInfo) SetMustRescan(by ShortID) {
  202. f.LocalFlags = FlagLocalMustRescan
  203. f.ModifiedBy = by
  204. f.Blocks = nil
  205. f.Sequence = 0
  206. }
  207. func (f *FileInfo) SetIgnored(by ShortID) {
  208. f.LocalFlags = FlagLocalIgnored
  209. f.ModifiedBy = by
  210. f.Blocks = nil
  211. f.Sequence = 0
  212. }
  213. func (f *FileInfo) SetUnsupported(by ShortID) {
  214. f.LocalFlags = FlagLocalUnsupported
  215. f.ModifiedBy = by
  216. f.Blocks = nil
  217. f.Sequence = 0
  218. }
  219. func (b BlockInfo) String() string {
  220. return fmt.Sprintf("Block{%d/%d/%d/%x}", b.Offset, b.Size, b.WeakHash, b.Hash)
  221. }
  222. // IsEmpty returns true if the block is a full block of zeroes.
  223. func (b BlockInfo) IsEmpty() bool {
  224. if v, ok := sha256OfEmptyBlock[int(b.Size)]; ok {
  225. return bytes.Equal(b.Hash, v[:])
  226. }
  227. return false
  228. }
  229. type IndexID uint64
  230. func (i IndexID) String() string {
  231. return fmt.Sprintf("0x%16X", uint64(i))
  232. }
  233. func (i IndexID) Marshal() ([]byte, error) {
  234. bs := make([]byte, 8)
  235. binary.BigEndian.PutUint64(bs, uint64(i))
  236. return bs, nil
  237. }
  238. func (i *IndexID) Unmarshal(bs []byte) error {
  239. if len(bs) != 8 {
  240. return errors.New("incorrect IndexID length")
  241. }
  242. *i = IndexID(binary.BigEndian.Uint64(bs))
  243. return nil
  244. }
  245. func NewIndexID() IndexID {
  246. return IndexID(rand.Int64())
  247. }
  248. func (f Folder) Description() string {
  249. // used by logging stuff
  250. if f.Label == "" {
  251. return f.ID
  252. }
  253. return fmt.Sprintf("%q (%s)", f.Label, f.ID)
  254. }