trashcan.go 3.9 KB

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