folder_recvenc.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/internal/itererr"
  12. "github.com/syncthing/syncthing/lib/config"
  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, ignores *ignore.Matcher, cfg config.FolderConfiguration, ver versioner.Versioner, evLogger events.Logger, ioLimiter *semaphore.Semaphore) service {
  27. f := &receiveEncryptedFolder{newSendReceiveFolder(model, 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. f.sl.Info("Reverting unexpected items")
  36. f.setState(FolderScanning)
  37. defer f.setState(FolderIdle)
  38. batch := NewFileInfoBatch(func(fs []protocol.FileInfo) error {
  39. f.updateLocalsFromScanning(fs)
  40. return nil
  41. })
  42. var dirs []string
  43. for fi, err := range itererr.Zip(f.db.AllLocalFiles(f.folderID, protocol.LocalDeviceID)) {
  44. if err != nil {
  45. return err
  46. }
  47. if err := batch.FlushIfFull(); err != nil {
  48. return err
  49. }
  50. if !fi.IsReceiveOnlyChanged() || fi.IsDeleted() {
  51. continue
  52. }
  53. if fi.IsDirectory() {
  54. dirs = append(dirs, fi.Name)
  55. continue
  56. }
  57. if err := f.inWritableDir(f.mtimefs.Remove, fi.Name); err != nil && !fs.IsNotExist(err) {
  58. f.newScanError(fi.Name, fmt.Errorf("deleting unexpected item: %w", err))
  59. }
  60. fi.SetDeleted(f.shortID)
  61. // Set version to zero, such that we pull the global version in case
  62. // this is a valid filename that was erroneously changed locally.
  63. // Should already be zero from scanning, but lets be safe.
  64. fi.Version = protocol.Vector{}
  65. // Purposely not removing FlagLocalReceiveOnly as the deleted
  66. // item should still not be sent in index updates. However being
  67. // deleted, it will not show up as an unexpected file in the UI
  68. // anymore.
  69. batch.Append(fi)
  70. }
  71. f.revertHandleDirs(dirs)
  72. if err := batch.Flush(); err != nil {
  73. return err
  74. }
  75. // We might need to pull items if the local changes were on valid, global files.
  76. f.SchedulePull()
  77. return nil
  78. }
  79. func (f *receiveEncryptedFolder) revertHandleDirs(dirs []string) {
  80. if len(dirs) == 0 {
  81. return
  82. }
  83. scanChan := make(chan string)
  84. go f.pullScannerRoutine(scanChan)
  85. defer close(scanChan)
  86. slices.SortFunc(dirs, func(a, b string) int {
  87. return strings.Compare(b, a)
  88. })
  89. for _, dir := range dirs {
  90. if err := f.deleteDirOnDisk(dir, scanChan); err != nil {
  91. f.newScanError(dir, fmt.Errorf("deleting unexpected dir: %w", err))
  92. }
  93. scanChan <- dir
  94. }
  95. }