trashcan.go 3.9 KB

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