folder_sendonly.go 3.7 KB

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