trashcan.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. "path/filepath"
  10. "strconv"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/fs"
  13. "github.com/syncthing/syncthing/lib/osutil"
  14. )
  15. func init() {
  16. // Register the constructor for this type of versioner
  17. Factories["trashcan"] = NewTrashcan
  18. }
  19. type Trashcan struct {
  20. fs fs.Filesystem
  21. cleanoutDays int
  22. stop chan struct{}
  23. }
  24. func NewTrashcan(folderID string, fs fs.Filesystem, params map[string]string) Versioner {
  25. cleanoutDays, _ := strconv.Atoi(params["cleanoutDays"])
  26. // On error we default to 0, "do not clean out the trash can"
  27. s := &Trashcan{
  28. fs: fs,
  29. cleanoutDays: cleanoutDays,
  30. stop: make(chan struct{}),
  31. }
  32. l.Debugf("instantiated %#v", s)
  33. return s
  34. }
  35. // Archive moves the named file away to a version archive. If this function
  36. // returns nil, the named file does not exist any more (has been archived).
  37. func (t *Trashcan) Archive(filePath string) error {
  38. info, err := t.fs.Lstat(filePath)
  39. if fs.IsNotExist(err) {
  40. l.Debugln("not archiving nonexistent file", filePath)
  41. return nil
  42. } else if err != nil {
  43. return err
  44. }
  45. if info.IsSymlink() {
  46. panic("bug: attempting to version a symlink")
  47. }
  48. versionsDir := ".stversions"
  49. if _, err := t.fs.Stat(versionsDir); err != nil {
  50. if !fs.IsNotExist(err) {
  51. return err
  52. }
  53. l.Debugln("creating versions dir", versionsDir)
  54. if err := t.fs.MkdirAll(versionsDir, 0777); err != nil {
  55. return err
  56. }
  57. t.fs.Hide(versionsDir)
  58. }
  59. l.Debugln("archiving", filePath)
  60. archivedPath := filepath.Join(versionsDir, filePath)
  61. if err := t.fs.MkdirAll(filepath.Dir(archivedPath), 0777); err != nil && !fs.IsExist(err) {
  62. return err
  63. }
  64. l.Debugln("moving to", archivedPath)
  65. if err := osutil.Rename(t.fs, filePath, archivedPath); err != nil {
  66. return err
  67. }
  68. // Set the mtime to the time the file was deleted. This is used by the
  69. // cleanout routine. If this fails things won't work optimally but there's
  70. // not much we can do about it so we ignore the error.
  71. t.fs.Chtimes(archivedPath, time.Now(), time.Now())
  72. return nil
  73. }
  74. func (t *Trashcan) Serve() {
  75. l.Debugln(t, "starting")
  76. defer l.Debugln(t, "stopping")
  77. // Do the first cleanup one minute after startup.
  78. timer := time.NewTimer(time.Minute)
  79. defer timer.Stop()
  80. for {
  81. select {
  82. case <-t.stop:
  83. return
  84. case <-timer.C:
  85. if t.cleanoutDays > 0 {
  86. if err := t.cleanoutArchive(); err != nil {
  87. l.Infoln("Cleaning trashcan:", err)
  88. }
  89. }
  90. // Cleanups once a day should be enough.
  91. timer.Reset(24 * time.Hour)
  92. }
  93. }
  94. }
  95. func (t *Trashcan) Stop() {
  96. close(t.stop)
  97. }
  98. func (t *Trashcan) String() string {
  99. return fmt.Sprintf("trashcan@%p", t)
  100. }
  101. func (t *Trashcan) cleanoutArchive() error {
  102. versionsDir := ".stversions"
  103. if _, err := t.fs.Lstat(versionsDir); fs.IsNotExist(err) {
  104. return nil
  105. }
  106. cutoff := time.Now().Add(time.Duration(-24*t.cleanoutDays) * time.Hour)
  107. dirTracker := make(emptyDirTracker)
  108. walkFn := func(path string, info fs.FileInfo, err error) error {
  109. if err != nil {
  110. return err
  111. }
  112. if info.IsDir() && !info.IsSymlink() {
  113. dirTracker.addDir(path)
  114. return nil
  115. }
  116. if info.ModTime().Before(cutoff) {
  117. // The file is too old; remove it.
  118. t.fs.Remove(path)
  119. } else {
  120. // Keep this file, and remember it so we don't unnecessarily try
  121. // to remove this directory.
  122. dirTracker.addFile(path)
  123. }
  124. return nil
  125. }
  126. if err := t.fs.Walk(versionsDir, walkFn); err != nil {
  127. return err
  128. }
  129. dirTracker.deleteEmptyDirs(t.fs)
  130. return nil
  131. }