blockqueue.go 3.5 KB

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