folder_sendonly.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, ioLimiter *byteSemaphore) service {
  24. f := &sendOnlyFolder{
  25. folder: newFolder(model, fset, ignores, cfg, evLogger, ioLimiter, nil),
  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. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  37. batchSizeBytes := 0
  38. snap := f.fset.Snapshot()
  39. defer snap.Release()
  40. snap.WithNeed(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
  41. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  42. f.updateLocalsFromPulling(batch)
  43. batch = batch[:0]
  44. batchSizeBytes = 0
  45. }
  46. if f.ignores.ShouldIgnore(intf.FileName()) {
  47. file := intf.(protocol.FileInfo)
  48. file.SetIgnored(f.shortID)
  49. batch = append(batch, file)
  50. batchSizeBytes += file.ProtoSize()
  51. l.Debugln(f, "Handling ignored file", file)
  52. return true
  53. }
  54. curFile, ok := snap.Get(protocol.LocalDeviceID, intf.FileName())
  55. if !ok {
  56. if intf.IsDeleted() {
  57. l.Debugln("Should never get a deleted file as needed when we don't have it")
  58. f.evLogger.Log(events.Failure, "got deleted file that doesn't exist locally as needed when pulling on send-only")
  59. }
  60. return true
  61. }
  62. file := intf.(protocol.FileInfo)
  63. if !file.IsEquivalentOptional(curFile, f.modTimeWindow, f.IgnorePerms, false, 0) {
  64. return true
  65. }
  66. file.Version = file.Version.Merge(curFile.Version)
  67. batch = append(batch, file)
  68. batchSizeBytes += file.ProtoSize()
  69. l.Debugln(f, "Merging versions of identical file", file)
  70. return true
  71. })
  72. if len(batch) > 0 {
  73. f.updateLocalsFromPulling(batch)
  74. }
  75. return true
  76. }
  77. func (f *sendOnlyFolder) Override() {
  78. f.doInSync(func() error { f.override(); return nil })
  79. }
  80. func (f *sendOnlyFolder) override() {
  81. l.Infof("Overriding global state on folder %v", f.Description)
  82. f.setState(FolderScanning)
  83. defer f.setState(FolderIdle)
  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 protocol.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.SetDeleted(f.shortID)
  104. } else {
  105. // We have the file, replace with our version
  106. have.Version = have.Version.Merge(need.Version).Update(f.shortID)
  107. need = have
  108. }
  109. need.Sequence = 0
  110. batch = append(batch, need)
  111. batchSizeBytes += need.ProtoSize()
  112. return true
  113. })
  114. if len(batch) > 0 {
  115. f.updateLocalsFromScanning(batch)
  116. }
  117. }