folder_sendonly.go 3.5 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/internal/itererr"
  9. "github.com/syncthing/syncthing/lib/config"
  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, ignores *ignore.Matcher, cfg config.FolderConfiguration, _ versioner.Versioner, evLogger events.Logger, ioLimiter *semaphore.Semaphore) service {
  23. f := &sendOnlyFolder{
  24. folder: newFolder(model, ignores, cfg, evLogger, ioLimiter, nil),
  25. }
  26. f.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 := NewFileInfoBatch(func(files []protocol.FileInfo) error {
  35. f.updateLocalsFromPulling(files)
  36. return nil
  37. })
  38. for file, err := range itererr.Zip(f.db.AllNeededGlobalFiles(f.folderID, protocol.LocalDeviceID, config.PullOrderAlphabetic, 0, 0)) {
  39. if err != nil {
  40. return false, err
  41. }
  42. if err := batch.FlushIfFull(); err != nil {
  43. return false, err
  44. }
  45. if f.ignores.Match(file.FileName()).IsIgnored() {
  46. file.SetIgnored()
  47. batch.Append(file)
  48. l.Debugln(f, "Handling ignored file", file)
  49. continue
  50. }
  51. curFile, ok, err := f.db.GetDeviceFile(f.folderID, protocol.LocalDeviceID, file.FileName())
  52. if err != nil {
  53. return false, err
  54. }
  55. if !ok {
  56. if file.IsInvalid() || file.IsDeleted() {
  57. // Accept the file for accounting purposes
  58. batch.Append(file)
  59. }
  60. continue
  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. continue
  69. }
  70. batch.Append(file)
  71. l.Debugln(f, "Merging versions of identical file", file)
  72. }
  73. batch.Flush()
  74. return true, nil
  75. }
  76. func (f *sendOnlyFolder) Override() {
  77. f.doInSync(f.override)
  78. }
  79. func (f *sendOnlyFolder) override() error {
  80. f.sl.Info("Overriding global state ")
  81. f.setState(FolderScanning)
  82. defer f.setState(FolderIdle)
  83. batch := NewFileInfoBatch(func(files []protocol.FileInfo) error {
  84. f.updateLocalsFromScanning(files)
  85. return nil
  86. })
  87. for need, err := range itererr.Zip(f.db.AllNeededGlobalFiles(f.folderID, protocol.LocalDeviceID, config.PullOrderAlphabetic, 0, 0)) {
  88. if err != nil {
  89. return err
  90. }
  91. if err := batch.FlushIfFull(); err != nil {
  92. return err
  93. }
  94. have, haveOk, err := f.db.GetDeviceFile(f.folderID, protocol.LocalDeviceID, need.Name)
  95. if err != nil {
  96. return err
  97. }
  98. // Don't override files that are in a bad state (ignored,
  99. // unsupported, must rescan, ...).
  100. if haveOk && have.IsInvalid() {
  101. continue
  102. }
  103. if !haveOk || 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. }
  114. return batch.Flush()
  115. }