folder_recvenc.go 3.3 KB

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