folder_sendonly.go 3.5 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/events"
  11. "github.com/syncthing/syncthing/lib/ignore"
  12. "github.com/syncthing/syncthing/lib/protocol"
  13. "github.com/syncthing/syncthing/lib/semaphore"
  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, evLogger events.Logger, ioLimiter *semaphore.Semaphore) service {
  23. f := &sendOnlyFolder{
  24. folder: newFolder(model, fset, ignores, cfg, evLogger, ioLimiter, nil),
  25. }
  26. f.folder.puller = f
  27. return f
  28. }
  29. func (*sendOnlyFolder) PullErrors() []FileError {
  30. return nil
  31. }
  32. // pull checks need for files that only differ by metadata (no changes on disk)
  33. func (f *sendOnlyFolder) pull() (bool, error) {
  34. batch := db.NewFileInfoBatch(func(files []protocol.FileInfo) error {
  35. f.updateLocalsFromPulling(files)
  36. return nil
  37. })
  38. snap, err := f.dbSnapshot()
  39. if err != nil {
  40. return false, err
  41. }
  42. defer snap.Release()
  43. snap.WithNeed(protocol.LocalDeviceID, func(file protocol.FileInfo) bool {
  44. batch.FlushIfFull()
  45. if f.ignores.Match(file.FileName()).IsIgnored() {
  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, file.FileName())
  52. if !ok {
  53. if file.IsInvalid() {
  54. // Global invalid file just exists for need accounting
  55. batch.Append(file)
  56. } else if file.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, protocol.FileInfoComparison{
  63. ModTimeWindow: f.modTimeWindow,
  64. IgnorePerms: f.IgnorePerms,
  65. IgnoreOwnership: !f.SyncOwnership,
  66. IgnoreXattrs: !f.SyncXattrs,
  67. }) {
  68. return true
  69. }
  70. batch.Append(file)
  71. l.Debugln(f, "Merging versions of identical file", file)
  72. return true
  73. })
  74. batch.Flush()
  75. return true, nil
  76. }
  77. func (f *sendOnlyFolder) Override() {
  78. f.doInSync(f.override)
  79. }
  80. func (f *sendOnlyFolder) override() error {
  81. l.Infoln("Overriding global state on folder", f.Description())
  82. f.setState(FolderScanning)
  83. defer f.setState(FolderIdle)
  84. batch := db.NewFileInfoBatch(func(files []protocol.FileInfo) error {
  85. f.updateLocalsFromScanning(files)
  86. return nil
  87. })
  88. snap, err := f.dbSnapshot()
  89. if err != nil {
  90. return err
  91. }
  92. defer snap.Release()
  93. snap.WithNeed(protocol.LocalDeviceID, func(need protocol.FileInfo) bool {
  94. _ = batch.FlushIfFull()
  95. have, ok := snap.Get(protocol.LocalDeviceID, need.Name)
  96. // Don't override files that are in a bad state (ignored,
  97. // unsupported, must rescan, ...).
  98. if ok && have.IsInvalid() {
  99. return true
  100. }
  101. if !ok || have.Name != need.Name {
  102. // We are missing the file
  103. need.SetDeleted(f.shortID)
  104. } else {
  105. // We have the file, replace with our version
  106. have.Version = have.Version.Merge(need.Version).Update(f.shortID)
  107. need = have
  108. }
  109. need.Sequence = 0
  110. batch.Append(need)
  111. return true
  112. })
  113. return batch.Flush()
  114. }