bep_extensions.go 8.2 KB

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