trashcan.go 4.1 KB

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