folder_recvenc.go 3.2 KB

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