bep_extensions.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2014 The Protocol Authors.
  2. //go:generate go run ../../script/protofmt.go bep.proto
  3. //go:generate protoc --proto_path=../../../../../:../../../../gogo/protobuf/protobuf:. --gogofast_out=. bep.proto
  4. package protocol
  5. import (
  6. "bytes"
  7. "crypto/sha256"
  8. "fmt"
  9. )
  10. var (
  11. sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
  12. HelloMessageMagic = uint32(0x2EA7D90B)
  13. )
  14. func (m Hello) Magic() uint32 {
  15. return HelloMessageMagic
  16. }
  17. func (f FileInfo) String() string {
  18. return fmt.Sprintf("File{Name:%q, Permissions:0%o, Modified:%d, Version:%v, Length:%d, Deleted:%v, Invalid:%v, NoPermissions:%v, Blocks:%v}",
  19. f.Name, f.Permissions, f.Modified, f.Version, f.Size, f.Deleted, f.Invalid, f.NoPermissions, f.Blocks)
  20. }
  21. func (f FileInfo) IsDeleted() bool {
  22. return f.Deleted
  23. }
  24. func (f FileInfo) IsInvalid() bool {
  25. return f.Invalid
  26. }
  27. func (f FileInfo) IsDirectory() bool {
  28. return f.Type == FileInfoTypeDirectory
  29. }
  30. func (f FileInfo) IsSymlink() bool {
  31. switch f.Type {
  32. case FileInfoTypeSymlinkDirectory, FileInfoTypeSymlinkFile, FileInfoTypeSymlinkUnknown:
  33. return true
  34. default:
  35. return false
  36. }
  37. }
  38. func (f FileInfo) HasPermissionBits() bool {
  39. return !f.NoPermissions
  40. }
  41. func (f FileInfo) FileSize() int64 {
  42. if f.IsDirectory() || f.IsDeleted() {
  43. return 128
  44. }
  45. return f.Size
  46. }
  47. func (f FileInfo) FileName() string {
  48. return f.Name
  49. }
  50. // WinsConflict returns true if "f" is the one to choose when it is in
  51. // conflict with "other".
  52. func (f FileInfo) WinsConflict(other FileInfo) bool {
  53. // If a modification is in conflict with a delete, we pick the
  54. // modification.
  55. if !f.IsDeleted() && other.IsDeleted() {
  56. return true
  57. }
  58. if f.IsDeleted() && !other.IsDeleted() {
  59. return false
  60. }
  61. // The one with the newer modification time wins.
  62. if f.Modified > other.Modified {
  63. return true
  64. }
  65. if f.Modified < other.Modified {
  66. return false
  67. }
  68. // The modification times were equal. Use the device ID in the version
  69. // vector as tie breaker.
  70. return f.Version.Compare(other.Version) == ConcurrentGreater
  71. }
  72. func (b BlockInfo) String() string {
  73. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  74. }
  75. // IsEmpty returns true if the block is a full block of zeroes.
  76. func (b BlockInfo) IsEmpty() bool {
  77. return b.Size == BlockSize && bytes.Equal(b.Hash, sha256OfEmptyBlock[:])
  78. }