folder_recvenc.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/lib/config"
  11. "github.com/syncthing/syncthing/lib/db"
  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, fset *db.FileSet, ignores *ignore.Matcher, cfg config.FolderConfiguration, ver versioner.Versioner, evLogger events.Logger, ioLimiter *semaphore.Semaphore) service {
  26. f := &receiveEncryptedFolder{newSendReceiveFolder(model, fset, 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 := db.NewFileInfoBatch(func(fs []protocol.FileInfo) error {
  38. f.updateLocalsFromScanning(fs)
  39. return nil
  40. })
  41. snap, err := f.dbSnapshot()
  42. if err != nil {
  43. return err
  44. }
  45. defer snap.Release()
  46. var iterErr error
  47. var dirs []string
  48. snap.WithHaveTruncated(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
  49. if iterErr = batch.FlushIfFull(); iterErr != nil {
  50. return false
  51. }
  52. fit := intf.(db.FileInfoTruncated)
  53. if !fit.IsReceiveOnlyChanged() || intf.IsDeleted() {
  54. return true
  55. }
  56. if fit.IsDirectory() {
  57. dirs = append(dirs, fit.Name)
  58. return true
  59. }
  60. if err := f.inWritableDir(f.mtimefs.Remove, fit.Name); err != nil && !fs.IsNotExist(err) {
  61. f.newScanError(fit.Name, fmt.Errorf("deleting unexpected item: %w", err))
  62. }
  63. fi := fit.ConvertToDeletedFileInfo(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. sort.Sort(sort.Reverse(sort.StringSlice(dirs)))
  94. for _, dir := range dirs {
  95. if err := f.deleteDirOnDisk(dir, snap, scanChan); err != nil {
  96. f.newScanError(dir, fmt.Errorf("deleting unexpected dir: %w", err))
  97. }
  98. scanChan <- dir
  99. }
  100. }