folder_sendonly.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(intf protocol.FileIntf) bool {
  44. batch.FlushIfFull()
  45. file := intf.(protocol.FileInfo)
  46. if f.ignores.ShouldIgnore(intf.FileName()) {
  47. file.SetIgnored()
  48. batch.Append(file)
  49. l.Debugln(f, "Handling ignored file", file)
  50. return true
  51. }
  52. curFile, ok := snap.Get(protocol.LocalDeviceID, intf.FileName())
  53. if !ok {
  54. if intf.IsInvalid() {
  55. // Global invalid file just exists for need accounting
  56. batch.Append(file)
  57. } else if intf.IsDeleted() {
  58. l.Debugln("Should never get a deleted file as needed when we don't have it")
  59. f.evLogger.Log(events.Failure, "got deleted file that doesn't exist locally as needed when pulling on send-only")
  60. }
  61. return true
  62. }
  63. if !file.IsEquivalentOptional(curFile, protocol.FileInfoComparison{
  64. ModTimeWindow: f.modTimeWindow,
  65. IgnorePerms: f.IgnorePerms,
  66. IgnoreOwnership: !f.SyncOwnership,
  67. IgnoreXattrs: !f.SyncXattrs,
  68. }) {
  69. return true
  70. }
  71. batch.Append(file)
  72. l.Debugln(f, "Merging versions of identical file", file)
  73. return true
  74. })
  75. batch.Flush()
  76. return true, nil
  77. }
  78. func (f *sendOnlyFolder) Override() {
  79. f.doInSync(f.override)
  80. }
  81. func (f *sendOnlyFolder) override() error {
  82. l.Infoln("Overriding global state on folder", f.Description())
  83. f.setState(FolderScanning)
  84. defer f.setState(FolderIdle)
  85. batch := db.NewFileInfoBatch(func(files []protocol.FileInfo) error {
  86. f.updateLocalsFromScanning(files)
  87. return nil
  88. })
  89. snap, err := f.dbSnapshot()
  90. if err != nil {
  91. return err
  92. }
  93. defer snap.Release()
  94. snap.WithNeed(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
  95. need := fi.(protocol.FileInfo)
  96. _ = batch.FlushIfFull()
  97. have, ok := snap.Get(protocol.LocalDeviceID, need.Name)
  98. // Don't override files that are in a bad state (ignored,
  99. // unsupported, must rescan, ...).
  100. if ok && have.IsInvalid() {
  101. return true
  102. }
  103. if !ok || have.Name != need.Name {
  104. // We are missing the file
  105. need.SetDeleted(f.shortID)
  106. } else {
  107. // We have the file, replace with our version
  108. have.Version = have.Version.Merge(need.Version).Update(f.shortID)
  109. need = have
  110. }
  111. need.Sequence = 0
  112. batch.Append(need)
  113. return true
  114. })
  115. return batch.Flush()
  116. }