bep_extensions.go 9.2 KB

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