blocks.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. "github.com/syncthing/syncthing/lib/protocol"
  13. )
  14. 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}
  15. type Counter interface {
  16. Update(bytes int64)
  17. }
  18. // Blocks returns the blockwise hash of the reader.
  19. func Blocks(r io.Reader, blocksize int, sizehint int64, counter Counter) ([]protocol.BlockInfo, error) {
  20. hf := sha256.New()
  21. hashLength := hf.Size()
  22. var blocks []protocol.BlockInfo
  23. var hashes, thisHash []byte
  24. if sizehint > 0 {
  25. // Allocate contiguous blocks for the BlockInfo structures and their
  26. // hashes once and for all.
  27. numBlocks := int(sizehint / int64(blocksize))
  28. blocks = make([]protocol.BlockInfo, 0, numBlocks)
  29. hashes = make([]byte, 0, hashLength*numBlocks)
  30. }
  31. // A 32k buffer is used for copying into the hash function.
  32. buf := make([]byte, 32<<10)
  33. var offset int64
  34. for {
  35. lr := io.LimitReader(r, int64(blocksize))
  36. n, err := copyBuffer(hf, lr, buf)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if n == 0 {
  41. break
  42. }
  43. if counter != nil {
  44. counter.Update(int64(n))
  45. }
  46. // Carve out a hash-sized chunk of "hashes" to store the hash for this
  47. // block.
  48. hashes = hf.Sum(hashes)
  49. thisHash, hashes = hashes[:hashLength], hashes[hashLength:]
  50. b := protocol.BlockInfo{
  51. Size: int32(n),
  52. Offset: offset,
  53. Hash: thisHash,
  54. }
  55. blocks = append(blocks, b)
  56. offset += int64(n)
  57. hf.Reset()
  58. }
  59. if len(blocks) == 0 {
  60. // Empty file
  61. blocks = append(blocks, protocol.BlockInfo{
  62. Offset: 0,
  63. Size: 0,
  64. Hash: SHA256OfNothing,
  65. })
  66. }
  67. return blocks, nil
  68. }
  69. // PopulateOffsets sets the Offset field on each block
  70. func PopulateOffsets(blocks []protocol.BlockInfo) {
  71. var offset int64
  72. for i := range blocks {
  73. blocks[i].Offset = offset
  74. offset += int64(blocks[i].Size)
  75. }
  76. }
  77. // BlockDiff returns lists of common and missing (to transform src into tgt)
  78. // blocks. Both block lists must have been created with the same block size.
  79. func BlockDiff(src, tgt []protocol.BlockInfo) (have, need []protocol.BlockInfo) {
  80. if len(tgt) == 0 && len(src) != 0 {
  81. return nil, nil
  82. }
  83. if len(tgt) != 0 && len(src) == 0 {
  84. // Copy the entire file
  85. return nil, tgt
  86. }
  87. for i := range tgt {
  88. if i >= len(src) || !bytes.Equal(tgt[i].Hash, src[i].Hash) {
  89. // Copy differing block
  90. need = append(need, tgt[i])
  91. } else {
  92. have = append(have, tgt[i])
  93. }
  94. }
  95. return have, need
  96. }
  97. // Verify returns nil or an error describing the mismatch between the block
  98. // list and actual reader contents
  99. func Verify(r io.Reader, blocksize int, blocks []protocol.BlockInfo) error {
  100. hf := sha256.New()
  101. for i, block := range blocks {
  102. lr := &io.LimitedReader{R: r, N: int64(blocksize)}
  103. _, err := io.Copy(hf, lr)
  104. if err != nil {
  105. return err
  106. }
  107. hash := hf.Sum(nil)
  108. hf.Reset()
  109. if !bytes.Equal(hash, block.Hash) {
  110. return fmt.Errorf("hash mismatch %x != %x for block %d", hash, block.Hash, i)
  111. }
  112. }
  113. // We should have reached the end now
  114. bs := make([]byte, 1)
  115. n, err := r.Read(bs)
  116. if n != 0 || err != io.EOF {
  117. return fmt.Errorf("file continues past end of blocks")
  118. }
  119. return nil
  120. }
  121. func VerifyBuffer(buf []byte, block protocol.BlockInfo) ([]byte, error) {
  122. if len(buf) != int(block.Size) {
  123. return nil, fmt.Errorf("length mismatch %d != %d", len(buf), block.Size)
  124. }
  125. hf := sha256.New()
  126. _, err := hf.Write(buf)
  127. if err != nil {
  128. return nil, err
  129. }
  130. hash := hf.Sum(nil)
  131. if !bytes.Equal(hash, block.Hash) {
  132. return hash, fmt.Errorf("hash mismatch %x != %x", hash, block.Hash)
  133. }
  134. return hash, nil
  135. }
  136. // BlocksEqual returns whether two slices of blocks are exactly the same hash
  137. // and index pair wise.
  138. func BlocksEqual(src, tgt []protocol.BlockInfo) bool {
  139. if len(tgt) != len(src) {
  140. return false
  141. }
  142. for i, sblk := range src {
  143. if !bytes.Equal(sblk.Hash, tgt[i].Hash) {
  144. return false
  145. }
  146. }
  147. return true
  148. }
  149. // This is a copy & paste of io.copyBuffer from the Go 1.5 standard library,
  150. // as we want this but also want to build with Go 1.3+.
  151. // copyBuffer is the actual implementation of Copy and CopyBuffer.
  152. // if buf is nil, one is allocated.
  153. func copyBuffer(dst io.Writer, src io.Reader, buf []byte) (written int64, err error) {
  154. // If the reader has a WriteTo method, use it to do the copy.
  155. // Avoids an allocation and a copy.
  156. if wt, ok := src.(io.WriterTo); ok {
  157. return wt.WriteTo(dst)
  158. }
  159. // Similarly, if the writer has a ReadFrom method, use it to do the copy.
  160. if rt, ok := dst.(io.ReaderFrom); ok {
  161. return rt.ReadFrom(src)
  162. }
  163. if buf == nil {
  164. buf = make([]byte, 32*1024)
  165. }
  166. for {
  167. nr, er := src.Read(buf)
  168. if nr > 0 {
  169. nw, ew := dst.Write(buf[0:nr])
  170. if nw > 0 {
  171. written += int64(nw)
  172. }
  173. if ew != nil {
  174. err = ew
  175. break
  176. }
  177. if nr != nw {
  178. err = io.ErrShortWrite
  179. break
  180. }
  181. }
  182. if er == io.EOF {
  183. break
  184. }
  185. if er != nil {
  186. err = er
  187. break
  188. }
  189. }
  190. return written, err
  191. }