folder_sendonly.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/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, _ fs.Filesystem) service {
  22. f := &sendOnlyFolder{
  23. folder: newFolder(model, fset, ignores, cfg),
  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. select {
  34. case <-f.initialScanFinished:
  35. default:
  36. // Once the initial scan finished, a pull will be scheduled
  37. return false
  38. }
  39. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  40. batchSizeBytes := 0
  41. f.fset.WithNeed(protocol.LocalDeviceID, func(intf db.FileIntf) bool {
  42. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  43. f.updateLocalsFromPulling(batch)
  44. batch = batch[:0]
  45. batchSizeBytes = 0
  46. }
  47. if f.ignores.ShouldIgnore(intf.FileName()) {
  48. file := intf.(protocol.FileInfo)
  49. file.SetIgnored(f.shortID)
  50. batch = append(batch, file)
  51. batchSizeBytes += file.ProtoSize()
  52. l.Debugln(f, "Handling ignored file", file)
  53. return true
  54. }
  55. curFile, ok := f.fset.Get(protocol.LocalDeviceID, intf.FileName())
  56. if !ok {
  57. if intf.IsDeleted() {
  58. panic("Should never get a deleted file as needed when we don't have it")
  59. }
  60. return true
  61. }
  62. file := intf.(protocol.FileInfo)
  63. if !file.IsEquivalentOptional(curFile, 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.setState(FolderScanning)
  79. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  80. batchSizeBytes := 0
  81. f.fset.WithNeed(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
  82. need := fi.(protocol.FileInfo)
  83. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  84. f.updateLocalsFromScanning(batch)
  85. batch = batch[:0]
  86. batchSizeBytes = 0
  87. }
  88. have, ok := f.fset.Get(protocol.LocalDeviceID, need.Name)
  89. // Don't override files that are in a bad state (ignored,
  90. // unsupported, must rescan, ...).
  91. if ok && have.IsInvalid() {
  92. return true
  93. }
  94. if !ok || have.Name != need.Name {
  95. // We are missing the file
  96. need.Deleted = true
  97. need.Blocks = nil
  98. need.Version = need.Version.Update(f.shortID)
  99. need.Size = 0
  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. f.setState(FolderIdle)
  114. }