folder_sendonly.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "context"
  9. "github.com/syncthing/syncthing/internal/itererr"
  10. "github.com/syncthing/syncthing/lib/config"
  11. "github.com/syncthing/syncthing/lib/events"
  12. "github.com/syncthing/syncthing/lib/ignore"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. "github.com/syncthing/syncthing/lib/semaphore"
  15. "github.com/syncthing/syncthing/lib/versioner"
  16. )
  17. func init() {
  18. folderFactories[config.FolderTypeSendOnly] = newSendOnlyFolder
  19. }
  20. type sendOnlyFolder struct {
  21. *folder
  22. }
  23. func newSendOnlyFolder(model *model, ignores *ignore.Matcher, cfg config.FolderConfiguration, _ versioner.Versioner, evLogger events.Logger, ioLimiter *semaphore.Semaphore) service {
  24. f := &sendOnlyFolder{
  25. folder: newFolder(model, ignores, cfg, evLogger, ioLimiter, nil),
  26. }
  27. f.puller = f
  28. return f
  29. }
  30. func (*sendOnlyFolder) PullErrors() []FileError {
  31. return nil
  32. }
  33. // pull checks need for files that only differ by metadata (no changes on disk)
  34. func (f *sendOnlyFolder) pull(ctx context.Context) (bool, error) {
  35. batch := NewFileInfoBatch(func(files []protocol.FileInfo) error {
  36. f.updateLocalsFromPulling(files)
  37. return nil
  38. })
  39. for file, err := range itererr.Zip(f.db.AllNeededGlobalFiles(f.folderID, protocol.LocalDeviceID, config.PullOrderAlphabetic, 0, 0)) {
  40. if err != nil {
  41. return false, err
  42. }
  43. if err := batch.FlushIfFull(); err != nil {
  44. return false, err
  45. }
  46. if f.ignores.Match(file.FileName()).IsIgnored() {
  47. file.SetIgnored()
  48. batch.Append(file)
  49. l.Debugln(f, "Handling ignored file", file)
  50. continue
  51. }
  52. curFile, ok, err := f.db.GetDeviceFile(f.folderID, protocol.LocalDeviceID, file.FileName())
  53. if err != nil {
  54. return false, err
  55. }
  56. if !ok {
  57. if file.IsInvalid() || file.IsDeleted() {
  58. // Accept the file for accounting purposes
  59. batch.Append(file)
  60. }
  61. continue
  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. continue
  70. }
  71. batch.Append(file)
  72. l.Debugln(f, "Merging versions of identical file", file)
  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(ctx context.Context) error {
  81. f.sl.InfoContext(ctx, "Overriding global state ")
  82. f.setState(FolderScanning)
  83. defer f.setState(FolderIdle)
  84. batch := NewFileInfoBatch(func(files []protocol.FileInfo) error {
  85. f.updateLocalsFromScanning(files)
  86. return nil
  87. })
  88. for need, err := range itererr.Zip(f.db.AllNeededGlobalFiles(f.folderID, protocol.LocalDeviceID, config.PullOrderAlphabetic, 0, 0)) {
  89. if err != nil {
  90. return err
  91. }
  92. if err := batch.FlushIfFull(); err != nil {
  93. return err
  94. }
  95. have, haveOk, err := f.db.GetDeviceFile(f.folderID, protocol.LocalDeviceID, need.Name)
  96. if err != nil {
  97. return err
  98. }
  99. // Don't override files that are in a bad state (ignored,
  100. // unsupported, must rescan, ...).
  101. if haveOk && have.IsInvalid() {
  102. continue
  103. }
  104. if !haveOk || have.Name != need.Name {
  105. // We are missing the file
  106. need.SetDeleted(f.shortID)
  107. } else {
  108. // We have the file, replace with our version
  109. have.Version = have.Version.Merge(need.Version).Update(f.shortID)
  110. need = have
  111. }
  112. need.Sequence = 0
  113. batch.Append(need)
  114. }
  115. return batch.Flush()
  116. }