trashcan.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 http://mozilla.org/MPL/2.0/.
  6. package versioner
  7. import (
  8. "fmt"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  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. folderPath string
  21. cleanoutDays int
  22. stop chan struct{}
  23. }
  24. func NewTrashcan(folderID, folderPath string, 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. folderPath: folderPath,
  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. _, err := osutil.Lstat(filePath)
  39. if os.IsNotExist(err) {
  40. l.Debugln("not archiving nonexistent file", filePath)
  41. return nil
  42. } else if err != nil {
  43. return err
  44. }
  45. versionsDir := filepath.Join(t.folderPath, ".stversions")
  46. if _, err := os.Stat(versionsDir); err != nil {
  47. if !os.IsNotExist(err) {
  48. return err
  49. }
  50. l.Debugln("creating versions dir", versionsDir)
  51. if err := osutil.MkdirAll(versionsDir, 0777); err != nil {
  52. return err
  53. }
  54. osutil.HideFile(versionsDir)
  55. }
  56. l.Debugln("archiving", filePath)
  57. relativePath, err := filepath.Rel(t.folderPath, filePath)
  58. if err != nil {
  59. return err
  60. }
  61. archivedPath := filepath.Join(versionsDir, relativePath)
  62. if err := osutil.MkdirAll(filepath.Dir(archivedPath), 0777); err != nil && !os.IsExist(err) {
  63. return err
  64. }
  65. l.Debugln("moving to", archivedPath)
  66. if err := osutil.Rename(filePath, archivedPath); err != nil {
  67. return err
  68. }
  69. // Set the mtime to the time the file was deleted. This is used by the
  70. // cleanout routine. If this fails things won't work optimally but there's
  71. // not much we can do about it so we ignore the error.
  72. os.Chtimes(archivedPath, time.Now(), time.Now())
  73. return nil
  74. }
  75. func (t *Trashcan) Serve() {
  76. l.Debugln(t, "starting")
  77. defer l.Debugln(t, "stopping")
  78. // Do the first cleanup one minute after startup.
  79. timer := time.NewTimer(time.Minute)
  80. defer timer.Stop()
  81. for {
  82. select {
  83. case <-t.stop:
  84. return
  85. case <-timer.C:
  86. if t.cleanoutDays > 0 {
  87. if err := t.cleanoutArchive(); err != nil {
  88. l.Infoln("Cleaning trashcan:", err)
  89. }
  90. }
  91. // Cleanups once a day should be enough.
  92. timer.Reset(24 * time.Hour)
  93. }
  94. }
  95. }
  96. func (t *Trashcan) Stop() {
  97. close(t.stop)
  98. }
  99. func (t *Trashcan) String() string {
  100. return fmt.Sprintf("trashcan@%p", t)
  101. }
  102. func (t *Trashcan) cleanoutArchive() error {
  103. versionsDir := filepath.Join(t.folderPath, ".stversions")
  104. if _, err := osutil.Lstat(versionsDir); os.IsNotExist(err) {
  105. return nil
  106. }
  107. cutoff := time.Now().Add(time.Duration(-24*t.cleanoutDays) * time.Hour)
  108. currentDir := ""
  109. filesInDir := 0
  110. walkFn := func(path string, info os.FileInfo, err error) error {
  111. if err != nil {
  112. return err
  113. }
  114. if info.IsDir() {
  115. // We have entered a new directory. Lets check if the previous
  116. // directory was empty and try to remove it. We ignore failure for
  117. // the time being.
  118. if currentDir != "" && filesInDir == 0 {
  119. osutil.Remove(currentDir)
  120. }
  121. currentDir = path
  122. filesInDir = 0
  123. return nil
  124. }
  125. if info.ModTime().Before(cutoff) {
  126. // The file is too old; remove it.
  127. osutil.Remove(path)
  128. } else {
  129. // Keep this file, and remember it so we don't unnecessarily try
  130. // to remove this directory.
  131. filesInDir++
  132. }
  133. return nil
  134. }
  135. if err := filepath.Walk(versionsDir, walkFn); err != nil {
  136. return err
  137. }
  138. // The last directory seen by the walkFn may not have been removed as it
  139. // should be.
  140. if currentDir != "" && filesInDir == 0 {
  141. osutil.Remove(currentDir)
  142. }
  143. return nil
  144. }