trashcan.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "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. cleanSymlinks(folderPath)
  26. cleanoutDays, _ := strconv.Atoi(params["cleanoutDays"])
  27. // On error we default to 0, "do not clean out the trash can"
  28. s := &Trashcan{
  29. folderPath: folderPath,
  30. cleanoutDays: cleanoutDays,
  31. stop: make(chan struct{}),
  32. }
  33. l.Debugf("instantiated %#v", s)
  34. return s
  35. }
  36. // Archive moves the named file away to a version archive. If this function
  37. // returns nil, the named file does not exist any more (has been archived).
  38. func (t *Trashcan) Archive(filePath string) error {
  39. info, err := osutil.Lstat(filePath)
  40. if os.IsNotExist(err) {
  41. l.Debugln("not archiving nonexistent file", filePath)
  42. return nil
  43. } else if err != nil {
  44. return err
  45. }
  46. if info.Mode()&os.ModeSymlink != 0 {
  47. panic("bug: attempting to version a symlink")
  48. }
  49. versionsDir := filepath.Join(t.folderPath, ".stversions")
  50. if _, err := os.Stat(versionsDir); err != nil {
  51. if !os.IsNotExist(err) {
  52. return err
  53. }
  54. l.Debugln("creating versions dir", versionsDir)
  55. if err := osutil.MkdirAll(versionsDir, 0777); err != nil {
  56. return err
  57. }
  58. osutil.HideFile(versionsDir)
  59. }
  60. l.Debugln("archiving", filePath)
  61. relativePath, err := filepath.Rel(t.folderPath, filePath)
  62. if err != nil {
  63. return err
  64. }
  65. archivedPath := filepath.Join(versionsDir, relativePath)
  66. if err := osutil.MkdirAll(filepath.Dir(archivedPath), 0777); err != nil && !os.IsExist(err) {
  67. return err
  68. }
  69. l.Debugln("moving to", archivedPath)
  70. if err := osutil.Rename(filePath, archivedPath); err != nil {
  71. return err
  72. }
  73. // Set the mtime to the time the file was deleted. This is used by the
  74. // cleanout routine. If this fails things won't work optimally but there's
  75. // not much we can do about it so we ignore the error.
  76. os.Chtimes(archivedPath, time.Now(), time.Now())
  77. return nil
  78. }
  79. func (t *Trashcan) Serve() {
  80. l.Debugln(t, "starting")
  81. defer l.Debugln(t, "stopping")
  82. // Do the first cleanup one minute after startup.
  83. timer := time.NewTimer(time.Minute)
  84. defer timer.Stop()
  85. for {
  86. select {
  87. case <-t.stop:
  88. return
  89. case <-timer.C:
  90. if t.cleanoutDays > 0 {
  91. if err := t.cleanoutArchive(); err != nil {
  92. l.Infoln("Cleaning trashcan:", err)
  93. }
  94. }
  95. // Cleanups once a day should be enough.
  96. timer.Reset(24 * time.Hour)
  97. }
  98. }
  99. }
  100. func (t *Trashcan) Stop() {
  101. close(t.stop)
  102. }
  103. func (t *Trashcan) String() string {
  104. return fmt.Sprintf("trashcan@%p", t)
  105. }
  106. func (t *Trashcan) cleanoutArchive() error {
  107. versionsDir := filepath.Join(t.folderPath, ".stversions")
  108. if _, err := osutil.Lstat(versionsDir); os.IsNotExist(err) {
  109. return nil
  110. }
  111. cutoff := time.Now().Add(time.Duration(-24*t.cleanoutDays) * time.Hour)
  112. currentDir := ""
  113. filesInDir := 0
  114. walkFn := func(path string, info os.FileInfo, err error) error {
  115. if err != nil {
  116. return err
  117. }
  118. if info.IsDir() {
  119. // We have entered a new directory. Lets check if the previous
  120. // directory was empty and try to remove it. We ignore failure for
  121. // the time being.
  122. if currentDir != "" && filesInDir == 0 {
  123. os.Remove(currentDir)
  124. }
  125. currentDir = path
  126. filesInDir = 0
  127. return nil
  128. }
  129. if info.ModTime().Before(cutoff) {
  130. // The file is too old; remove it.
  131. os.Remove(path)
  132. } else {
  133. // Keep this file, and remember it so we don't unnecessarily try
  134. // to remove this directory.
  135. filesInDir++
  136. }
  137. return nil
  138. }
  139. if err := filepath.Walk(versionsDir, walkFn); err != nil {
  140. return err
  141. }
  142. // The last directory seen by the walkFn may not have been removed as it
  143. // should be.
  144. if currentDir != "" && filesInDir == 0 {
  145. os.Remove(currentDir)
  146. }
  147. return nil
  148. }