blockqueue.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "errors"
  10. "github.com/syncthing/syncthing/lib/fs"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. "github.com/syncthing/syncthing/lib/sync"
  13. )
  14. // HashFile hashes the files and returns a list of blocks representing the file.
  15. func HashFile(ctx context.Context, fs fs.Filesystem, path string, blockSize int, counter Counter, useWeakHashes bool) ([]protocol.BlockInfo, error) {
  16. fd, err := fs.Open(path)
  17. if err != nil {
  18. l.Debugln("open:", err)
  19. return nil, err
  20. }
  21. defer fd.Close()
  22. // Get the size and modtime of the file before we start hashing it.
  23. fi, err := fd.Stat()
  24. if err != nil {
  25. l.Debugln("stat before:", err)
  26. return nil, err
  27. }
  28. size := fi.Size()
  29. modTime := fi.ModTime()
  30. // Hash the file. This may take a while for large files.
  31. blocks, err := Blocks(ctx, fd, blockSize, size, counter, useWeakHashes)
  32. if err != nil {
  33. l.Debugln("blocks:", err)
  34. return nil, err
  35. }
  36. // Recheck the size and modtime again. If they differ, the file changed
  37. // while we were reading it and our hash results are invalid.
  38. fi, err = fd.Stat()
  39. if err != nil {
  40. l.Debugln("stat after:", err)
  41. return nil, err
  42. }
  43. if size != fi.Size() || !modTime.Equal(fi.ModTime()) {
  44. return nil, errors.New("file changed during hashing")
  45. }
  46. return blocks, nil
  47. }
  48. // The parallel hasher reads FileInfo structures from the inbox, hashes the
  49. // file to populate the Blocks element and sends it to the outbox. A number of
  50. // workers are used in parallel. The outbox will become closed when the inbox
  51. // is closed and all items handled.
  52. type parallelHasher struct {
  53. fs fs.Filesystem
  54. blockSize int
  55. workers int
  56. outbox chan<- protocol.FileInfo
  57. inbox <-chan protocol.FileInfo
  58. counter Counter
  59. done chan<- struct{}
  60. useWeakHashes bool
  61. wg sync.WaitGroup
  62. }
  63. func newParallelHasher(ctx context.Context, fs fs.Filesystem, blockSize, workers int, outbox chan<- protocol.FileInfo, inbox <-chan protocol.FileInfo, counter Counter, done chan<- struct{}, useWeakHashes bool) {
  64. ph := &parallelHasher{
  65. fs: fs,
  66. blockSize: blockSize,
  67. workers: workers,
  68. outbox: outbox,
  69. inbox: inbox,
  70. counter: counter,
  71. done: done,
  72. useWeakHashes: useWeakHashes,
  73. wg: sync.NewWaitGroup(),
  74. }
  75. for i := 0; i < workers; i++ {
  76. ph.wg.Add(1)
  77. go ph.hashFiles(ctx)
  78. }
  79. go ph.closeWhenDone()
  80. }
  81. func (ph *parallelHasher) hashFiles(ctx context.Context) {
  82. defer ph.wg.Done()
  83. for {
  84. select {
  85. case f, ok := <-ph.inbox:
  86. if !ok {
  87. return
  88. }
  89. if f.IsDirectory() || f.IsDeleted() {
  90. panic("Bug. Asked to hash a directory or a deleted file.")
  91. }
  92. blocks, err := HashFile(ctx, ph.fs, f.Name, ph.blockSize, ph.counter, ph.useWeakHashes)
  93. if err != nil {
  94. l.Debugln("hash error:", f.Name, err)
  95. continue
  96. }
  97. f.Blocks = blocks
  98. // The size we saw when initially deciding to hash the file
  99. // might not have been the size it actually had when we hashed
  100. // it. Update the size from the block list.
  101. f.Size = 0
  102. for _, b := range blocks {
  103. f.Size += int64(b.Size)
  104. }
  105. select {
  106. case ph.outbox <- f:
  107. case <-ctx.Done():
  108. return
  109. }
  110. case <-ctx.Done():
  111. return
  112. }
  113. }
  114. }
  115. func (ph *parallelHasher) closeWhenDone() {
  116. ph.wg.Wait()
  117. if ph.done != nil {
  118. close(ph.done)
  119. }
  120. close(ph.outbox)
  121. }