bep_extensions.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "time"
  11. "github.com/syncthing/syncthing/lib/rand"
  12. "github.com/syncthing/syncthing/lib/sha256"
  13. )
  14. const (
  15. SyntheticDirectorySize = 128
  16. )
  17. var (
  18. sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
  19. HelloMessageMagic = uint32(0x2EA7D90B)
  20. )
  21. func (m Hello) Magic() uint32 {
  22. return HelloMessageMagic
  23. }
  24. func (f FileInfo) String() string {
  25. switch f.Type {
  26. case FileInfoTypeDirectory:
  27. return fmt.Sprintf("Directory{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Deleted:%v, Invalid:%v, NoPermissions:%v}",
  28. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Deleted, f.Invalid, f.NoPermissions)
  29. case FileInfoTypeFile:
  30. return fmt.Sprintf("File{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Length:%d, Deleted:%v, Invalid:%v, NoPermissions:%v, Blocks:%v}",
  31. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.Invalid, f.NoPermissions, f.Blocks)
  32. case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
  33. return fmt.Sprintf("Symlink{Name:%q, Type:%v, Sequence:%d, Version:%v, Deleted:%v, Invalid:%v, NoPermissions:%v, SymlinkTarget:%q}",
  34. f.Name, f.Type, f.Sequence, f.Version, f.Deleted, f.Invalid, f.NoPermissions, f.SymlinkTarget)
  35. default:
  36. panic("mystery file type detected")
  37. }
  38. }
  39. func (f FileInfo) IsDeleted() bool {
  40. return f.Deleted
  41. }
  42. func (f FileInfo) IsInvalid() bool {
  43. return f.Invalid
  44. }
  45. func (f FileInfo) IsDirectory() bool {
  46. return f.Type == FileInfoTypeDirectory
  47. }
  48. func (f FileInfo) IsSymlink() bool {
  49. switch f.Type {
  50. case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
  51. return true
  52. default:
  53. return false
  54. }
  55. }
  56. func (f FileInfo) HasPermissionBits() bool {
  57. return !f.NoPermissions
  58. }
  59. func (f FileInfo) FileSize() int64 {
  60. if f.Deleted {
  61. return 0
  62. }
  63. if f.IsDirectory() || f.IsSymlink() {
  64. return SyntheticDirectorySize
  65. }
  66. return f.Size
  67. }
  68. func (f FileInfo) FileName() string {
  69. return f.Name
  70. }
  71. func (f FileInfo) ModTime() time.Time {
  72. return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
  73. }
  74. func (f FileInfo) SequenceNo() int64 {
  75. return f.Sequence
  76. }
  77. // WinsConflict returns true if "f" is the one to choose when it is in
  78. // conflict with "other".
  79. func (f FileInfo) WinsConflict(other FileInfo) bool {
  80. // If a modification is in conflict with a delete, we pick the
  81. // modification.
  82. if !f.IsDeleted() && other.IsDeleted() {
  83. return true
  84. }
  85. if f.IsDeleted() && !other.IsDeleted() {
  86. return false
  87. }
  88. // The one with the newer modification time wins.
  89. if f.ModTime().After(other.ModTime()) {
  90. return true
  91. }
  92. if f.ModTime().Before(other.ModTime()) {
  93. return false
  94. }
  95. // The modification times were equal. Use the device ID in the version
  96. // vector as tie breaker.
  97. return f.Version.Compare(other.Version) == ConcurrentGreater
  98. }
  99. func (f *FileInfo) Invalidate(invalidatedBy ShortID) {
  100. f.Invalid = true
  101. f.ModifiedBy = invalidatedBy
  102. f.Blocks = nil
  103. f.Sequence = 0
  104. }
  105. func (b BlockInfo) String() string {
  106. return fmt.Sprintf("Block{%d/%d/%d/%x}", b.Offset, b.Size, b.WeakHash, b.Hash)
  107. }
  108. // IsEmpty returns true if the block is a full block of zeroes.
  109. func (b BlockInfo) IsEmpty() bool {
  110. return b.Size == BlockSize && bytes.Equal(b.Hash, sha256OfEmptyBlock[:])
  111. }
  112. type IndexID uint64
  113. func (i IndexID) String() string {
  114. return fmt.Sprintf("0x%16X", uint64(i))
  115. }
  116. func (i IndexID) Marshal() ([]byte, error) {
  117. bs := make([]byte, 8)
  118. binary.BigEndian.PutUint64(bs, uint64(i))
  119. return bs, nil
  120. }
  121. func (i *IndexID) Unmarshal(bs []byte) error {
  122. if len(bs) != 8 {
  123. return errors.New("incorrect IndexID length")
  124. }
  125. *i = IndexID(binary.BigEndian.Uint64(bs))
  126. return nil
  127. }
  128. func NewIndexID() IndexID {
  129. return IndexID(rand.Int64())
  130. }
  131. func (f Folder) Description() string {
  132. // used by logging stuff
  133. if f.Label == "" {
  134. return f.ID
  135. }
  136. return fmt.Sprintf("%q (%s)", f.Label, f.ID)
  137. }