blocks.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. "fmt"
  10. "io"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. "github.com/syncthing/syncthing/lib/sha256"
  13. "github.com/syncthing/syncthing/lib/weakhash"
  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. type Counter interface {
  17. Update(bytes int64)
  18. }
  19. // Blocks returns the blockwise hash of the reader.
  20. func Blocks(r io.Reader, blocksize int, sizehint int64, counter Counter) ([]protocol.BlockInfo, error) {
  21. hf := sha256.New()
  22. hashLength := hf.Size()
  23. whf := weakhash.NewHash(blocksize)
  24. var blocks []protocol.BlockInfo
  25. var hashes, thisHash []byte
  26. if sizehint >= 0 {
  27. // Allocate contiguous blocks for the BlockInfo structures and their
  28. // hashes once and for all, and stick to the specified size.
  29. r = io.LimitReader(r, sizehint)
  30. numBlocks := int(sizehint / int64(blocksize))
  31. blocks = make([]protocol.BlockInfo, 0, numBlocks)
  32. hashes = make([]byte, 0, hashLength*numBlocks)
  33. }
  34. // A 32k buffer is used for copying into the hash function.
  35. buf := make([]byte, 32<<10)
  36. var offset int64
  37. for {
  38. lr := io.LimitReader(r, int64(blocksize))
  39. n, err := io.CopyBuffer(hf, io.TeeReader(lr, whf), buf)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if n == 0 {
  44. break
  45. }
  46. if counter != nil {
  47. counter.Update(n)
  48. }
  49. // Carve out a hash-sized chunk of "hashes" to store the hash for this
  50. // block.
  51. hashes = hf.Sum(hashes)
  52. thisHash, hashes = hashes[:hashLength], hashes[hashLength:]
  53. b := protocol.BlockInfo{
  54. Size: int32(n),
  55. Offset: offset,
  56. Hash: thisHash,
  57. WeakHash: whf.Sum32(),
  58. }
  59. blocks = append(blocks, b)
  60. offset += n
  61. hf.Reset()
  62. whf.Reset()
  63. }
  64. if len(blocks) == 0 {
  65. // Empty file
  66. blocks = append(blocks, protocol.BlockInfo{
  67. Offset: 0,
  68. Size: 0,
  69. Hash: SHA256OfNothing,
  70. })
  71. }
  72. return blocks, nil
  73. }
  74. // PopulateOffsets sets the Offset field on each block
  75. func PopulateOffsets(blocks []protocol.BlockInfo) {
  76. var offset int64
  77. for i := range blocks {
  78. blocks[i].Offset = offset
  79. offset += int64(blocks[i].Size)
  80. }
  81. }
  82. // BlockDiff returns lists of common and missing (to transform src into tgt)
  83. // blocks. Both block lists must have been created with the same block size.
  84. func BlockDiff(src, tgt []protocol.BlockInfo) (have, need []protocol.BlockInfo) {
  85. if len(tgt) == 0 && len(src) != 0 {
  86. return nil, nil
  87. }
  88. if len(tgt) != 0 && len(src) == 0 {
  89. // Copy the entire file
  90. return nil, tgt
  91. }
  92. for i := range tgt {
  93. if i >= len(src) || !bytes.Equal(tgt[i].Hash, src[i].Hash) {
  94. // Copy differing block
  95. need = append(need, tgt[i])
  96. } else {
  97. have = append(have, tgt[i])
  98. }
  99. }
  100. return have, need
  101. }
  102. // Verify returns nil or an error describing the mismatch between the block
  103. // list and actual reader contents
  104. func Verify(r io.Reader, blocksize int, blocks []protocol.BlockInfo) error {
  105. hf := sha256.New()
  106. // A 32k buffer is used for copying into the hash function.
  107. buf := make([]byte, 32<<10)
  108. for i, block := range blocks {
  109. lr := &io.LimitedReader{R: r, N: int64(blocksize)}
  110. _, err := io.CopyBuffer(hf, lr, buf)
  111. if err != nil {
  112. return err
  113. }
  114. hash := hf.Sum(nil)
  115. hf.Reset()
  116. if !bytes.Equal(hash, block.Hash) {
  117. return fmt.Errorf("hash mismatch %x != %x for block %d", hash, block.Hash, i)
  118. }
  119. }
  120. // We should have reached the end now
  121. bs := make([]byte, 1)
  122. n, err := r.Read(bs)
  123. if n != 0 || err != io.EOF {
  124. return fmt.Errorf("file continues past end of blocks")
  125. }
  126. return nil
  127. }
  128. func VerifyBuffer(buf []byte, block protocol.BlockInfo) ([]byte, error) {
  129. if len(buf) != int(block.Size) {
  130. return nil, fmt.Errorf("length mismatch %d != %d", len(buf), block.Size)
  131. }
  132. hf := sha256.New()
  133. _, err := hf.Write(buf)
  134. if err != nil {
  135. return nil, err
  136. }
  137. hash := hf.Sum(nil)
  138. if !bytes.Equal(hash, block.Hash) {
  139. return hash, fmt.Errorf("hash mismatch %x != %x", hash, block.Hash)
  140. }
  141. return hash, nil
  142. }
  143. // BlocksEqual returns whether two slices of blocks are exactly the same hash
  144. // and index pair wise.
  145. func BlocksEqual(src, tgt []protocol.BlockInfo) bool {
  146. if len(tgt) != len(src) {
  147. return false
  148. }
  149. for i, sblk := range src {
  150. if !bytes.Equal(sblk.Hash, tgt[i].Hash) {
  151. return false
  152. }
  153. }
  154. return true
  155. }