folder_sendonly.go 3.7 KB

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