trashcan.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2015 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 versioner
  7. import (
  8. "context"
  9. "fmt"
  10. "strconv"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/fs"
  14. )
  15. func init() {
  16. // Register the constructor for this type of versioner
  17. factories["trashcan"] = newTrashcan
  18. }
  19. type trashcan struct {
  20. folderFs fs.Filesystem
  21. versionsFs fs.Filesystem
  22. cleanoutDays int
  23. copyRangeMethod fs.CopyRangeMethod
  24. }
  25. func newTrashcan(cfg config.FolderConfiguration) Versioner {
  26. cleanoutDays, _ := strconv.Atoi(cfg.Versioning.Params["cleanoutDays"])
  27. // On error we default to 0, "do not clean out the trash can"
  28. s := &trashcan{
  29. folderFs: cfg.Filesystem(nil),
  30. versionsFs: versionerFsFromFolderCfg(cfg),
  31. cleanoutDays: cleanoutDays,
  32. copyRangeMethod: cfg.CopyRangeMethod,
  33. }
  34. l.Debugf("instantiated %#v", s)
  35. return s
  36. }
  37. // Archive moves the named file away to a version archive. If this function
  38. // returns nil, the named file does not exist any more (has been archived).
  39. func (t *trashcan) Archive(filePath string) error {
  40. return archiveFile(t.copyRangeMethod, t.folderFs, t.versionsFs, filePath, func(name, tag string) string {
  41. return name
  42. })
  43. }
  44. func (t *trashcan) String() string {
  45. return fmt.Sprintf("trashcan@%p", t)
  46. }
  47. func (t *trashcan) Clean(ctx context.Context) error {
  48. if t.cleanoutDays <= 0 {
  49. return nil
  50. }
  51. if _, err := t.versionsFs.Lstat("."); fs.IsNotExist(err) {
  52. return nil
  53. }
  54. cutoff := time.Now().Add(time.Duration(-24*t.cleanoutDays) * time.Hour)
  55. dirTracker := make(emptyDirTracker)
  56. walkFn := func(path string, info fs.FileInfo, err error) error {
  57. if err != nil {
  58. return err
  59. }
  60. select {
  61. case <-ctx.Done():
  62. return ctx.Err()
  63. default:
  64. }
  65. if info.IsDir() && !info.IsSymlink() {
  66. dirTracker.addDir(path)
  67. return nil
  68. }
  69. if info.ModTime().Before(cutoff) {
  70. // The file is too old; remove it.
  71. err = t.versionsFs.Remove(path)
  72. } else {
  73. // Keep this file, and remember it so we don't unnecessarily try
  74. // to remove this directory.
  75. dirTracker.addFile(path)
  76. }
  77. return err
  78. }
  79. if err := t.versionsFs.Walk(".", walkFn); err != nil {
  80. return err
  81. }
  82. dirTracker.deleteEmptyDirs(t.versionsFs)
  83. return nil
  84. }
  85. func (t *trashcan) GetVersions() (map[string][]FileVersion, error) {
  86. return retrieveVersions(t.versionsFs)
  87. }
  88. func (t *trashcan) Restore(filepath string, versionTime time.Time) error {
  89. // If we have an untagged file A and want to restore it on top of existing file A, we can't first archive the
  90. // existing A as we'd overwrite the old A version, therefore when we archive existing file, we archive it with a
  91. // tag but when the restoration is finished, we rename it (untag it). This is only important if when restoring A,
  92. // there already exists a file at the same location
  93. // If we restore a deleted file, there won't be a conflict and archiving won't happen thus there won't be anything
  94. // in the archive to rename afterwards. Log whether the file exists prior to restoring.
  95. _, dstPathErr := t.folderFs.Lstat(filepath)
  96. taggedName := ""
  97. tagger := func(name, tag string) string {
  98. // We also abuse the fact that tagger gets called twice, once for tagging the restoration version, which
  99. // should just return the plain name, and second time by archive which archives existing file in the folder.
  100. // We can't use TagFilename here, as restoreFile would discover that as a valid version and restore that instead.
  101. if taggedName != "" {
  102. return taggedName
  103. }
  104. taggedName = fs.TempName(name)
  105. return name
  106. }
  107. if err := restoreFile(t.copyRangeMethod, t.versionsFs, t.folderFs, filepath, versionTime, tagger); taggedName == "" {
  108. return err
  109. }
  110. // If a deleted file was restored, even though the RenameOrCopy method is robust, check if the file exists and
  111. // skip the renaming function if this is the case.
  112. if fs.IsNotExist(dstPathErr) {
  113. if _, err := t.folderFs.Lstat(filepath); err != nil {
  114. return err
  115. }
  116. return nil
  117. }
  118. return t.versionsFs.Rename(taggedName, filepath)
  119. }