folder_sendonly.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/util"
  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 *util.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 (f *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, f.modTimeWindow, f.IgnorePerms, false, 0) {
  64. return true
  65. }
  66. batch.Append(file)
  67. l.Debugln(f, "Merging versions of identical file", file)
  68. return true
  69. })
  70. batch.Flush()
  71. return true, nil
  72. }
  73. func (f *sendOnlyFolder) Override() {
  74. f.doInSync(f.override)
  75. }
  76. func (f *sendOnlyFolder) override() error {
  77. l.Infoln("Overriding global state on folder", f.Description())
  78. f.setState(FolderScanning)
  79. defer f.setState(FolderIdle)
  80. batch := db.NewFileInfoBatch(func(files []protocol.FileInfo) error {
  81. f.updateLocalsFromScanning(files)
  82. return nil
  83. })
  84. snap, err := f.dbSnapshot()
  85. if err != nil {
  86. return err
  87. }
  88. defer snap.Release()
  89. snap.WithNeed(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
  90. need := fi.(protocol.FileInfo)
  91. _ = batch.FlushIfFull()
  92. have, ok := snap.Get(protocol.LocalDeviceID, need.Name)
  93. // Don't override files that are in a bad state (ignored,
  94. // unsupported, must rescan, ...).
  95. if ok && have.IsInvalid() {
  96. return true
  97. }
  98. if !ok || have.Name != need.Name {
  99. // We are missing the file
  100. need.SetDeleted(f.shortID)
  101. } else {
  102. // We have the file, replace with our version
  103. have.Version = have.Version.Merge(need.Version).Update(f.shortID)
  104. need = have
  105. }
  106. need.Sequence = 0
  107. batch.Append(need)
  108. return true
  109. })
  110. return batch.Flush()
  111. }