blocks.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 https://mozilla.org/MPL/2.0/.
  6. package scanner
  7. import (
  8. "context"
  9. "hash"
  10. "io"
  11. "github.com/chmduquesne/rollinghash/adler32"
  12. "github.com/syncthing/syncthing/lib/protocol"
  13. "github.com/syncthing/syncthing/lib/sha256"
  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(ctx context.Context, r io.Reader, blocksize int, sizehint int64, counter Counter, useWeakHashes bool) ([]protocol.BlockInfo, error) {
  21. if counter == nil {
  22. counter = &noopCounter{}
  23. }
  24. hf := sha256.New()
  25. hashLength := hf.Size()
  26. var weakHf hash.Hash32 = noopHash{}
  27. var multiHf io.Writer = hf
  28. if useWeakHashes {
  29. // Use an actual weak hash function, make the multiHf
  30. // write to both hash functions.
  31. weakHf = adler32.New()
  32. multiHf = io.MultiWriter(hf, weakHf)
  33. }
  34. var blocks []protocol.BlockInfo
  35. var hashes, thisHash []byte
  36. if sizehint >= 0 {
  37. // Allocate contiguous blocks for the BlockInfo structures and their
  38. // hashes once and for all, and stick to the specified size.
  39. r = io.LimitReader(r, sizehint)
  40. numBlocks := int(sizehint / int64(blocksize))
  41. blocks = make([]protocol.BlockInfo, 0, numBlocks)
  42. hashes = make([]byte, 0, hashLength*numBlocks)
  43. }
  44. // A 32k buffer is used for copying into the hash function.
  45. buf := make([]byte, 32<<10)
  46. var offset int64
  47. lr := io.LimitReader(r, int64(blocksize)).(*io.LimitedReader)
  48. for {
  49. select {
  50. case <-ctx.Done():
  51. return nil, ctx.Err()
  52. default:
  53. }
  54. lr.N = int64(blocksize)
  55. n, err := io.CopyBuffer(multiHf, lr, buf)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if n == 0 {
  60. break
  61. }
  62. counter.Update(n)
  63. // Carve out a hash-sized chunk of "hashes" to store the hash for this
  64. // block.
  65. hashes = hf.Sum(hashes)
  66. thisHash, hashes = hashes[:hashLength], hashes[hashLength:]
  67. b := protocol.BlockInfo{
  68. Size: int32(n),
  69. Offset: offset,
  70. Hash: thisHash,
  71. WeakHash: weakHf.Sum32(),
  72. }
  73. blocks = append(blocks, b)
  74. offset += n
  75. hf.Reset()
  76. weakHf.Reset()
  77. }
  78. if len(blocks) == 0 {
  79. // Empty file
  80. blocks = append(blocks, protocol.BlockInfo{
  81. Offset: 0,
  82. Size: 0,
  83. Hash: SHA256OfNothing,
  84. })
  85. }
  86. return blocks, nil
  87. }
  88. type noopHash struct{}
  89. func (noopHash) Sum32() uint32 { return 0 }
  90. func (noopHash) BlockSize() int { return 0 }
  91. func (noopHash) Size() int { return 0 }
  92. func (noopHash) Reset() {}
  93. func (noopHash) Sum([]byte) []byte { return nil }
  94. func (noopHash) Write([]byte) (int, error) { return 0, nil }
  95. type noopCounter struct{}
  96. func (c *noopCounter) Update(bytes int64) {}