folder_sendonly.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 model
  7. import (
  8. "github.com/syncthing/syncthing/lib/config"
  9. "github.com/syncthing/syncthing/lib/db"
  10. "github.com/syncthing/syncthing/lib/events"
  11. "github.com/syncthing/syncthing/lib/fs"
  12. "github.com/syncthing/syncthing/lib/ignore"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. "github.com/syncthing/syncthing/lib/util"
  15. "github.com/syncthing/syncthing/lib/versioner"
  16. )
  17. func init() {
  18. folderFactories[config.FolderTypeSendOnly] = newSendOnlyFolder
  19. }
  20. type sendOnlyFolder struct {
  21. folder
  22. }
  23. func newSendOnlyFolder(model *model, fset *db.FileSet, ignores *ignore.Matcher, cfg config.FolderConfiguration, _ versioner.Versioner, _ fs.Filesystem, evLogger events.Logger) service {
  24. f := &sendOnlyFolder{
  25. folder: newFolder(model, fset, ignores, cfg, evLogger),
  26. }
  27. f.folder.puller = f
  28. f.folder.Service = util.AsService(f.serve, f.String())
  29. return f
  30. }
  31. func (f *sendOnlyFolder) PullErrors() []FileError {
  32. return nil
  33. }
  34. // pull checks need for files that only differ by metadata (no changes on disk)
  35. func (f *sendOnlyFolder) pull() bool {
  36. select {
  37. case <-f.initialScanFinished:
  38. default:
  39. // Once the initial scan finished, a pull will be scheduled
  40. return false
  41. }
  42. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  43. batchSizeBytes := 0
  44. snap := f.fset.Snapshot()
  45. defer snap.Release()
  46. snap.WithNeed(protocol.LocalDeviceID, func(intf db.FileIntf) bool {
  47. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  48. f.updateLocalsFromPulling(batch)
  49. batch = batch[:0]
  50. batchSizeBytes = 0
  51. }
  52. if f.ignores.ShouldIgnore(intf.FileName()) {
  53. file := intf.(protocol.FileInfo)
  54. file.SetIgnored(f.shortID)
  55. batch = append(batch, file)
  56. batchSizeBytes += file.ProtoSize()
  57. l.Debugln(f, "Handling ignored file", file)
  58. return true
  59. }
  60. curFile, ok := snap.Get(protocol.LocalDeviceID, intf.FileName())
  61. if !ok {
  62. if intf.IsDeleted() {
  63. l.Debugln("Should never get a deleted file as needed when we don't have it")
  64. }
  65. return true
  66. }
  67. file := intf.(protocol.FileInfo)
  68. if !file.IsEquivalentOptional(curFile, f.ModTimeWindow(), f.IgnorePerms, false, 0) {
  69. return true
  70. }
  71. file.Version = file.Version.Merge(curFile.Version)
  72. batch = append(batch, file)
  73. batchSizeBytes += file.ProtoSize()
  74. l.Debugln(f, "Merging versions of identical file", file)
  75. return true
  76. })
  77. if len(batch) > 0 {
  78. f.updateLocalsFromPulling(batch)
  79. }
  80. return true
  81. }
  82. func (f *sendOnlyFolder) Override() {
  83. f.setState(FolderScanning)
  84. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  85. batchSizeBytes := 0
  86. snap := f.fset.Snapshot()
  87. defer snap.Release()
  88. snap.WithNeed(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
  89. need := fi.(protocol.FileInfo)
  90. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  91. f.updateLocalsFromScanning(batch)
  92. batch = batch[:0]
  93. batchSizeBytes = 0
  94. }
  95. have, ok := snap.Get(protocol.LocalDeviceID, need.Name)
  96. // Don't override files that are in a bad state (ignored,
  97. // unsupported, must rescan, ...).
  98. if ok && have.IsInvalid() {
  99. return true
  100. }
  101. if !ok || have.Name != need.Name {
  102. // We are missing the file
  103. need.Deleted = true
  104. need.Blocks = nil
  105. need.Version = need.Version.Update(f.shortID)
  106. need.Size = 0
  107. } else {
  108. // We have the file, replace with our version
  109. have.Version = have.Version.Merge(need.Version).Update(f.shortID)
  110. need = have
  111. }
  112. need.Sequence = 0
  113. batch = append(batch, need)
  114. batchSizeBytes += need.ProtoSize()
  115. return true
  116. })
  117. if len(batch) > 0 {
  118. f.updateLocalsFromScanning(batch)
  119. }
  120. f.setState(FolderIdle)
  121. }