bep_extensions.go 7.2 KB

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