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