folder_sendonly.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/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, evLogger events.Logger, ioLimiter *byteSemaphore) service {
  22. f := &sendOnlyFolder{
  23. folder: newFolder(model, fset, ignores, cfg, evLogger, ioLimiter, nil),
  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, error) {
  33. batch := newFileInfoBatch(func(files []protocol.FileInfo) error {
  34. f.updateLocalsFromPulling(files)
  35. return nil
  36. })
  37. snap, err := f.dbSnapshot()
  38. if err != nil {
  39. return false, err
  40. }
  41. defer snap.Release()
  42. snap.WithNeed(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
  43. batch.flushIfFull()
  44. file := intf.(protocol.FileInfo)
  45. if f.ignores.ShouldIgnore(intf.FileName()) {
  46. file.SetIgnored()
  47. batch.append(file)
  48. l.Debugln(f, "Handling ignored file", file)
  49. return true
  50. }
  51. curFile, ok := snap.Get(protocol.LocalDeviceID, intf.FileName())
  52. if !ok {
  53. if intf.IsInvalid() {
  54. // Global invalid file just exists for need accounting
  55. batch.append(file)
  56. } else if intf.IsDeleted() {
  57. l.Debugln("Should never get a deleted file as needed when we don't have it")
  58. f.evLogger.Log(events.Failure, "got deleted file that doesn't exist locally as needed when pulling on send-only")
  59. }
  60. return true
  61. }
  62. if !file.IsEquivalentOptional(curFile, f.modTimeWindow, f.IgnorePerms, false, 0) {
  63. return true
  64. }
  65. batch.append(file)
  66. l.Debugln(f, "Merging versions of identical file", file)
  67. return true
  68. })
  69. batch.flush()
  70. return true, nil
  71. }
  72. func (f *sendOnlyFolder) Override() {
  73. f.doInSync(f.override)
  74. }
  75. func (f *sendOnlyFolder) override() error {
  76. l.Infoln("Overriding global state on folder", f.Description())
  77. f.setState(FolderScanning)
  78. defer f.setState(FolderIdle)
  79. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  80. batchSizeBytes := 0
  81. snap, err := f.dbSnapshot()
  82. if err != nil {
  83. return err
  84. }
  85. defer snap.Release()
  86. snap.WithNeed(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
  87. need := fi.(protocol.FileInfo)
  88. if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
  89. f.updateLocalsFromScanning(batch)
  90. batch = batch[:0]
  91. batchSizeBytes = 0
  92. }
  93. have, ok := snap.Get(protocol.LocalDeviceID, need.Name)
  94. // Don't override files that are in a bad state (ignored,
  95. // unsupported, must rescan, ...).
  96. if ok && have.IsInvalid() {
  97. return true
  98. }
  99. if !ok || have.Name != need.Name {
  100. // We are missing the file
  101. need.SetDeleted(f.shortID)
  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. return nil
  116. }