folder_sendonly.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/protocol"
  12. "github.com/syncthing/syncthing/lib/versioner"
  13. )
  14. func init() {
  15. folderFactories[config.FolderTypeSendOnly] = newSendOnlyFolder
  16. }
  17. type sendOnlyFolder struct {
  18. folder
  19. }
  20. func newSendOnlyFolder(model *Model, cfg config.FolderConfiguration, _ versioner.Versioner, _ fs.Filesystem) service {
  21. f := &sendOnlyFolder{
  22. folder: newFolder(model, cfg),
  23. }
  24. f.folder.puller = f
  25. return f
  26. }
  27. func (f *sendOnlyFolder) PullErrors() []FileError {
  28. return nil
  29. }
  30. // pull checks need for files that only differ by metadata (no changes on disk)
  31. func (f *sendOnlyFolder) pull() bool {
  32. select {
  33. case <-f.initialScanFinished:
  34. default:
  35. // Once the initial scan finished, a pull will be scheduled
  36. return false
  37. }
  38. f.model.fmut.RLock()
  39. folderFiles := f.model.folderFiles[f.folderID]
  40. ignores := f.model.folderIgnores[f.folderID]
  41. f.model.fmut.RUnlock()
  42. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  43. batchSizeBytes := 0
  44. folderFiles.WithNeed(protocol.LocalDeviceID, func(intf db.FileIntf) bool {
  45. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  46. f.model.updateLocalsFromPulling(f.folderID, batch)
  47. batch = batch[:0]
  48. batchSizeBytes = 0
  49. }
  50. if 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.model.CurrentFolderFile(f.folderID, intf.FileName())
  59. if !ok {
  60. if intf.IsDeleted() {
  61. panic("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.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.model.updateLocalsFromPulling(f.folderID, batch)
  77. }
  78. return true
  79. }
  80. func (f *sendOnlyFolder) Override(fs *db.FileSet, updateFn func([]protocol.FileInfo)) {
  81. f.setState(FolderScanning)
  82. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  83. batchSizeBytes := 0
  84. fs.WithNeed(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
  85. need := fi.(protocol.FileInfo)
  86. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  87. updateFn(batch)
  88. batch = batch[:0]
  89. batchSizeBytes = 0
  90. }
  91. have, ok := fs.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. updateFn(batch)
  115. }
  116. f.setState(FolderIdle)
  117. }