folder_sendonly.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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)
  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. f.fset.WithNeed(protocol.LocalDeviceID, func(intf db.FileIntf) bool {
  45. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  46. f.updateLocalsFromPulling(batch)
  47. batch = batch[:0]
  48. batchSizeBytes = 0
  49. }
  50. if f.ignores.ShouldIgnore(intf.FileName()) {
  51. file := intf.(protocol.FileInfo)
  52. file.SetIgnored(f.shortID)
  53. batch = append(batch, file)
  54. batchSizeBytes += file.ProtoSize()
  55. l.Debugln(f, "Handling ignored file", file)
  56. return true
  57. }
  58. curFile, ok := f.fset.Get(protocol.LocalDeviceID, intf.FileName())
  59. if !ok {
  60. if intf.IsDeleted() {
  61. l.Debugln("Should never get a deleted file as needed when we don't have it")
  62. }
  63. return true
  64. }
  65. file := intf.(protocol.FileInfo)
  66. if !file.IsEquivalentOptional(curFile, f.ModTimeWindow(), f.IgnorePerms, false, 0) {
  67. return true
  68. }
  69. file.Version = file.Version.Merge(curFile.Version)
  70. batch = append(batch, file)
  71. batchSizeBytes += file.ProtoSize()
  72. l.Debugln(f, "Merging versions of identical file", file)
  73. return true
  74. })
  75. if len(batch) > 0 {
  76. f.updateLocalsFromPulling(batch)
  77. }
  78. return true
  79. }
  80. func (f *sendOnlyFolder) Override() {
  81. f.setState(FolderScanning)
  82. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  83. batchSizeBytes := 0
  84. f.fset.WithNeed(protocol.LocalDeviceID, func(fi db.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 := f.fset.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.Deleted = true
  100. need.Blocks = nil
  101. need.Version = need.Version.Update(f.shortID)
  102. need.Size = 0
  103. } else {
  104. // We have the file, replace with our version
  105. have.Version = have.Version.Merge(need.Version).Update(f.shortID)
  106. need = have
  107. }
  108. need.Sequence = 0
  109. batch = append(batch, need)
  110. batchSizeBytes += need.ProtoSize()
  111. return true
  112. })
  113. if len(batch) > 0 {
  114. f.updateLocalsFromScanning(batch)
  115. }
  116. f.setState(FolderIdle)
  117. }