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. workers int
  55. outbox chan<- ScanResult
  56. inbox <-chan protocol.FileInfo
  57. counter Counter
  58. done chan<- struct{}
  59. wg sync.WaitGroup
  60. }
  61. func newParallelHasher(ctx context.Context, fs fs.Filesystem, workers int, outbox chan<- ScanResult, inbox <-chan protocol.FileInfo, counter Counter, done chan<- struct{}) {
  62. ph := &parallelHasher{
  63. fs: fs,
  64. workers: workers,
  65. outbox: outbox,
  66. inbox: inbox,
  67. counter: counter,
  68. done: done,
  69. wg: sync.NewWaitGroup(),
  70. }
  71. for i := 0; i < workers; i++ {
  72. ph.wg.Add(1)
  73. go ph.hashFiles(ctx)
  74. }
  75. go ph.closeWhenDone()
  76. }
  77. func (ph *parallelHasher) hashFiles(ctx context.Context) {
  78. defer ph.wg.Done()
  79. for {
  80. select {
  81. case f, ok := <-ph.inbox:
  82. if !ok {
  83. return
  84. }
  85. if f.IsDirectory() || f.IsDeleted() {
  86. panic("Bug. Asked to hash a directory or a deleted file.")
  87. }
  88. blocks, err := HashFile(ctx, ph.fs, f.Name, f.BlockSize(), ph.counter, true)
  89. if err != nil {
  90. l.Debugln("hash error:", f.Name, err)
  91. continue
  92. }
  93. f.Blocks = blocks
  94. f.BlocksHash = protocol.BlocksHash(blocks)
  95. // The size we saw when initially deciding to hash the file
  96. // might not have been the size it actually had when we hashed
  97. // it. Update the size from the block list.
  98. f.Size = 0
  99. for _, b := range blocks {
  100. f.Size += int64(b.Size)
  101. }
  102. select {
  103. case ph.outbox <- ScanResult{File: f}:
  104. case <-ctx.Done():
  105. return
  106. }
  107. case <-ctx.Done():
  108. return
  109. }
  110. }
  111. }
  112. func (ph *parallelHasher) closeWhenDone() {
  113. ph.wg.Wait()
  114. // In case the hasher aborted on context, wait for filesystem
  115. // walking/progress routine to finish.
  116. for range ph.inbox {
  117. }
  118. if ph.done != nil {
  119. close(ph.done)
  120. }
  121. close(ph.outbox)
  122. }