folder_sendonly.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "fmt"
  9. "github.com/syncthing/syncthing/lib/config"
  10. "github.com/syncthing/syncthing/lib/db"
  11. "github.com/syncthing/syncthing/lib/fs"
  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, cfg config.FolderConfiguration, _ versioner.Versioner, _ fs.Filesystem) service {
  22. f := &sendOnlyFolder{
  23. folder: newFolder(model, cfg),
  24. }
  25. f.folder.puller = f
  26. return f
  27. }
  28. func (f *sendOnlyFolder) String() string {
  29. return fmt.Sprintf("sendOnlyFolder/%s@%p", f.folderID, 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. f.model.fmut.RLock()
  43. folderFiles := f.model.folderFiles[f.folderID]
  44. ignores := f.model.folderIgnores[f.folderID]
  45. f.model.fmut.RUnlock()
  46. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  47. batchSizeBytes := 0
  48. folderFiles.WithNeed(protocol.LocalDeviceID, func(intf db.FileIntf) bool {
  49. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  50. f.model.updateLocalsFromPulling(f.folderID, batch)
  51. batch = batch[:0]
  52. batchSizeBytes = 0
  53. }
  54. if ignores.ShouldIgnore(intf.FileName()) {
  55. file := intf.(protocol.FileInfo)
  56. file.Invalidate(f.shortID)
  57. batch = append(batch, file)
  58. batchSizeBytes += file.ProtoSize()
  59. l.Debugln(f, "Handling ignored file", file)
  60. return true
  61. }
  62. curFile, ok := f.model.CurrentFolderFile(f.folderID, intf.FileName())
  63. if !ok {
  64. if intf.IsDeleted() {
  65. panic("Should never get a deleted file as needed when we don't have it")
  66. }
  67. return true
  68. }
  69. file := intf.(protocol.FileInfo)
  70. if !file.IsEquivalent(curFile, f.IgnorePerms, false) {
  71. return true
  72. }
  73. file.Version = file.Version.Merge(curFile.Version)
  74. batch = append(batch, file)
  75. batchSizeBytes += file.ProtoSize()
  76. l.Debugln(f, "Merging versions of identical file", file)
  77. return true
  78. })
  79. if len(batch) > 0 {
  80. f.model.updateLocalsFromPulling(f.folderID, batch)
  81. }
  82. return true
  83. }