blocks.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package scanner
  7. import (
  8. "bytes"
  9. "crypto/sha256"
  10. "fmt"
  11. "io"
  12. "sync/atomic"
  13. "github.com/syncthing/protocol"
  14. )
  15. var SHA256OfNothing = []uint8{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}
  16. // Blocks returns the blockwise hash of the reader.
  17. func Blocks(r io.Reader, blocksize int, sizehint int64, counter *int64) ([]protocol.BlockInfo, error) {
  18. var blocks []protocol.BlockInfo
  19. if sizehint > 0 {
  20. blocks = make([]protocol.BlockInfo, 0, int(sizehint/int64(blocksize)))
  21. }
  22. var offset int64
  23. hf := sha256.New()
  24. for {
  25. lr := &io.LimitedReader{R: r, N: int64(blocksize)}
  26. n, err := io.Copy(hf, lr)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if n == 0 {
  31. break
  32. }
  33. if counter != nil {
  34. atomic.AddInt64(counter, int64(n))
  35. }
  36. b := protocol.BlockInfo{
  37. Size: int32(n),
  38. Offset: offset,
  39. Hash: hf.Sum(nil),
  40. }
  41. blocks = append(blocks, b)
  42. offset += int64(n)
  43. hf.Reset()
  44. }
  45. if len(blocks) == 0 {
  46. // Empty file
  47. blocks = append(blocks, protocol.BlockInfo{
  48. Offset: 0,
  49. Size: 0,
  50. Hash: SHA256OfNothing,
  51. })
  52. }
  53. return blocks, nil
  54. }
  55. // PopulateOffsets sets the Offset field on each block
  56. func PopulateOffsets(blocks []protocol.BlockInfo) {
  57. var offset int64
  58. for i := range blocks {
  59. blocks[i].Offset = offset
  60. offset += int64(blocks[i].Size)
  61. }
  62. }
  63. // BlockDiff returns lists of common and missing (to transform src into tgt)
  64. // blocks. Both block lists must have been created with the same block size.
  65. func BlockDiff(src, tgt []protocol.BlockInfo) (have, need []protocol.BlockInfo) {
  66. if len(tgt) == 0 && len(src) != 0 {
  67. return nil, nil
  68. }
  69. if len(tgt) != 0 && len(src) == 0 {
  70. // Copy the entire file
  71. return nil, tgt
  72. }
  73. for i := range tgt {
  74. if i >= len(src) || bytes.Compare(tgt[i].Hash, src[i].Hash) != 0 {
  75. // Copy differing block
  76. need = append(need, tgt[i])
  77. } else {
  78. have = append(have, tgt[i])
  79. }
  80. }
  81. return have, need
  82. }
  83. // Verify returns nil or an error describing the mismatch between the block
  84. // list and actual reader contents
  85. func Verify(r io.Reader, blocksize int, blocks []protocol.BlockInfo) error {
  86. hf := sha256.New()
  87. for i, block := range blocks {
  88. lr := &io.LimitedReader{R: r, N: int64(blocksize)}
  89. _, err := io.Copy(hf, lr)
  90. if err != nil {
  91. return err
  92. }
  93. hash := hf.Sum(nil)
  94. hf.Reset()
  95. if bytes.Compare(hash, block.Hash) != 0 {
  96. return fmt.Errorf("hash mismatch %x != %x for block %d", hash, block.Hash, i)
  97. }
  98. }
  99. // We should have reached the end now
  100. bs := make([]byte, 1)
  101. n, err := r.Read(bs)
  102. if n != 0 || err != io.EOF {
  103. return fmt.Errorf("file continues past end of blocks")
  104. }
  105. return nil
  106. }
  107. func VerifyBuffer(buf []byte, block protocol.BlockInfo) ([]byte, error) {
  108. if len(buf) != int(block.Size) {
  109. return nil, fmt.Errorf("length mismatch %d != %d", len(buf), block.Size)
  110. }
  111. hf := sha256.New()
  112. _, err := hf.Write(buf)
  113. if err != nil {
  114. return nil, err
  115. }
  116. hash := hf.Sum(nil)
  117. if !bytes.Equal(hash, block.Hash) {
  118. return hash, fmt.Errorf("hash mismatch %x != %x", hash, block.Hash)
  119. }
  120. return hash, nil
  121. }
  122. // BlocksEqual returns whether two slices of blocks are exactly the same hash
  123. // and index pair wise.
  124. func BlocksEqual(src, tgt []protocol.BlockInfo) bool {
  125. if len(tgt) != len(src) {
  126. return false
  127. }
  128. for i, sblk := range src {
  129. if !bytes.Equal(sblk.Hash, tgt[i].Hash) {
  130. return false
  131. }
  132. }
  133. return true
  134. }