folder_recvenc.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2018 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. "fmt"
  9. "slices"
  10. "strings"
  11. "github.com/syncthing/syncthing/lib/config"
  12. "github.com/syncthing/syncthing/lib/db"
  13. "github.com/syncthing/syncthing/lib/events"
  14. "github.com/syncthing/syncthing/lib/fs"
  15. "github.com/syncthing/syncthing/lib/ignore"
  16. "github.com/syncthing/syncthing/lib/protocol"
  17. "github.com/syncthing/syncthing/lib/semaphore"
  18. "github.com/syncthing/syncthing/lib/versioner"
  19. )
  20. func init() {
  21. folderFactories[config.FolderTypeReceiveEncrypted] = newReceiveEncryptedFolder
  22. }
  23. type receiveEncryptedFolder struct {
  24. *sendReceiveFolder
  25. }
  26. func newReceiveEncryptedFolder(model *model, fset *db.FileSet, ignores *ignore.Matcher, cfg config.FolderConfiguration, ver versioner.Versioner, evLogger events.Logger, ioLimiter *semaphore.Semaphore) service {
  27. f := &receiveEncryptedFolder{newSendReceiveFolder(model, fset, ignores, cfg, ver, evLogger, ioLimiter).(*sendReceiveFolder)}
  28. f.localFlags = protocol.FlagLocalReceiveOnly // gets propagated to the scanner, and set on locally changed files
  29. return f
  30. }
  31. func (f *receiveEncryptedFolder) Revert() {
  32. f.doInSync(f.revert)
  33. }
  34. func (f *receiveEncryptedFolder) revert() error {
  35. l.Infof("Reverting unexpected items in folder %v (receive-encrypted)", f.Description())
  36. f.setState(FolderScanning)
  37. defer f.setState(FolderIdle)
  38. batch := db.NewFileInfoBatch(func(fs []protocol.FileInfo) error {
  39. f.updateLocalsFromScanning(fs)
  40. return nil
  41. })
  42. snap, err := f.dbSnapshot()
  43. if err != nil {
  44. return err
  45. }
  46. defer snap.Release()
  47. var iterErr error
  48. var dirs []string
  49. snap.WithHaveTruncated(protocol.LocalDeviceID, func(fi protocol.FileInfo) bool {
  50. if iterErr = batch.FlushIfFull(); iterErr != nil {
  51. return false
  52. }
  53. if !fi.IsReceiveOnlyChanged() || fi.IsDeleted() {
  54. return true
  55. }
  56. if fi.IsDirectory() {
  57. dirs = append(dirs, fi.Name)
  58. return true
  59. }
  60. if err := f.inWritableDir(f.mtimefs.Remove, fi.Name); err != nil && !fs.IsNotExist(err) {
  61. f.newScanError(fi.Name, fmt.Errorf("deleting unexpected item: %w", err))
  62. }
  63. fi.SetDeleted(f.shortID)
  64. // Set version to zero, such that we pull the global version in case
  65. // this is a valid filename that was erroneously changed locally.
  66. // Should already be zero from scanning, but lets be safe.
  67. fi.Version = protocol.Vector{}
  68. // Purposely not removing FlagLocalReceiveOnly as the deleted
  69. // item should still not be sent in index updates. However being
  70. // deleted, it will not show up as an unexpected file in the UI
  71. // anymore.
  72. batch.Append(fi)
  73. return true
  74. })
  75. f.revertHandleDirs(dirs, snap)
  76. if iterErr != nil {
  77. return iterErr
  78. }
  79. if err := batch.Flush(); err != nil {
  80. return err
  81. }
  82. // We might need to pull items if the local changes were on valid, global files.
  83. f.SchedulePull()
  84. return nil
  85. }
  86. func (f *receiveEncryptedFolder) revertHandleDirs(dirs []string, snap *db.Snapshot) {
  87. if len(dirs) == 0 {
  88. return
  89. }
  90. scanChan := make(chan string)
  91. go f.pullScannerRoutine(scanChan)
  92. defer close(scanChan)
  93. slices.SortFunc(dirs, func(a, b string) int {
  94. return strings.Compare(b, a)
  95. })
  96. for _, dir := range dirs {
  97. if err := f.deleteDirOnDisk(dir, snap, scanChan); err != nil {
  98. f.newScanError(dir, fmt.Errorf("deleting unexpected dir: %w", err))
  99. }
  100. scanChan <- dir
  101. }
  102. }