blockqueue.go 3.6 KB

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