folder_recvenc.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/util"
  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 *util.Semaphore) service {
  26. return &receiveEncryptedFolder{newSendReceiveFolder(model, fset, ignores, cfg, ver, evLogger, ioLimiter).(*sendReceiveFolder)}
  27. }
  28. func (f *receiveEncryptedFolder) Revert() {
  29. f.doInSync(f.revert)
  30. }
  31. func (f *receiveEncryptedFolder) revert() error {
  32. l.Infof("Reverting unexpected items in folder %v (receive-encrypted)", f.Description())
  33. f.setState(FolderScanning)
  34. defer f.setState(FolderIdle)
  35. batch := db.NewFileInfoBatch(func(fs []protocol.FileInfo) error {
  36. f.updateLocalsFromScanning(fs)
  37. return nil
  38. })
  39. snap, err := f.dbSnapshot()
  40. if err != nil {
  41. return err
  42. }
  43. defer snap.Release()
  44. var iterErr error
  45. var dirs []string
  46. snap.WithHaveTruncated(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
  47. if iterErr = batch.FlushIfFull(); iterErr != nil {
  48. return false
  49. }
  50. fit := intf.(db.FileInfoTruncated)
  51. if !fit.IsReceiveOnlyChanged() || intf.IsDeleted() {
  52. return true
  53. }
  54. if fit.IsDirectory() {
  55. dirs = append(dirs, fit.Name)
  56. return true
  57. }
  58. if err := f.inWritableDir(f.mtimefs.Remove, fit.Name); err != nil && !fs.IsNotExist(err) {
  59. f.newScanError(fit.Name, fmt.Errorf("deleting unexpected item: %w", err))
  60. }
  61. fi := fit.ConvertToDeletedFileInfo(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. return true
  72. })
  73. f.revertHandleDirs(dirs, snap)
  74. if iterErr != nil {
  75. return iterErr
  76. }
  77. return batch.Flush()
  78. }
  79. func (f *receiveEncryptedFolder) revertHandleDirs(dirs []string, snap *db.Snapshot) {
  80. if len(dirs) == 0 {
  81. return
  82. }
  83. scanChan := make(chan string)
  84. go f.pullScannerRoutine(scanChan)
  85. defer close(scanChan)
  86. sort.Sort(sort.Reverse(sort.StringSlice(dirs)))
  87. for _, dir := range dirs {
  88. if err := f.deleteDirOnDisk(dir, snap, scanChan); err != nil {
  89. f.newScanError(dir, fmt.Errorf("deleting unexpected dir: %w", err))
  90. }
  91. }
  92. }