simple.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package versioner
  16. import (
  17. "os"
  18. "path/filepath"
  19. "sort"
  20. "strconv"
  21. "github.com/syncthing/syncthing/internal/osutil"
  22. )
  23. func init() {
  24. // Register the constructor for this type of versioner with the name "simple"
  25. Factories["simple"] = NewSimple
  26. }
  27. // The type holds our configuration
  28. type Simple struct {
  29. keep int
  30. folderPath string
  31. }
  32. // The constructor function takes a map of parameters and creates the type.
  33. func NewSimple(folderID, folderPath string, params map[string]string) Versioner {
  34. keep, err := strconv.Atoi(params["keep"])
  35. if err != nil {
  36. keep = 5 // A reasonable default
  37. }
  38. s := Simple{
  39. keep: keep,
  40. folderPath: folderPath,
  41. }
  42. if debug {
  43. l.Debugf("instantiated %#v", s)
  44. }
  45. return s
  46. }
  47. // Move away the named file to a version archive. If this function returns
  48. // nil, the named file does not exist any more (has been archived).
  49. func (v Simple) Archive(filePath string) error {
  50. fileInfo, err := os.Stat(filePath)
  51. if err != nil {
  52. if os.IsNotExist(err) {
  53. if debug {
  54. l.Debugln("not archiving nonexistent file", filePath)
  55. }
  56. return nil
  57. } else {
  58. return err
  59. }
  60. }
  61. versionsDir := filepath.Join(v.folderPath, ".stversions")
  62. _, err = os.Stat(versionsDir)
  63. if err != nil {
  64. if os.IsNotExist(err) {
  65. if debug {
  66. l.Debugln("creating versions dir", versionsDir)
  67. }
  68. os.MkdirAll(versionsDir, 0755)
  69. osutil.HideFile(versionsDir)
  70. } else {
  71. return err
  72. }
  73. }
  74. if debug {
  75. l.Debugln("archiving", filePath)
  76. }
  77. file := filepath.Base(filePath)
  78. inFolderPath, err := filepath.Rel(v.folderPath, filepath.Dir(filePath))
  79. if err != nil {
  80. return err
  81. }
  82. dir := filepath.Join(versionsDir, inFolderPath)
  83. err = os.MkdirAll(dir, 0755)
  84. if err != nil && !os.IsExist(err) {
  85. return err
  86. }
  87. ver := taggedFilename(file, fileInfo.ModTime().Format(TimeFormat))
  88. dst := filepath.Join(dir, ver)
  89. if debug {
  90. l.Debugln("moving to", dst)
  91. }
  92. err = osutil.Rename(filePath, dst)
  93. if err != nil {
  94. return err
  95. }
  96. // Glob according to the new file~timestamp.ext pattern.
  97. newVersions, err := filepath.Glob(filepath.Join(dir, taggedFilename(file, TimeGlob)))
  98. if err != nil {
  99. l.Warnln("globbing:", err)
  100. return nil
  101. }
  102. // Also according to the old file.ext~timestamp pattern.
  103. oldVersions, err := filepath.Glob(filepath.Join(dir, file+"~"+TimeGlob))
  104. if err != nil {
  105. l.Warnln("globbing:", err)
  106. return nil
  107. }
  108. // Use all the found filenames. "~" sorts after "." so all old pattern
  109. // files will be deleted before any new, which is as it should be.
  110. versions := append(oldVersions, newVersions...)
  111. if len(versions) > v.keep {
  112. sort.Strings(versions)
  113. for _, toRemove := range versions[:len(versions)-v.keep] {
  114. if debug {
  115. l.Debugln("cleaning out", toRemove)
  116. }
  117. err = os.Remove(toRemove)
  118. if err != nil {
  119. l.Warnln("removing old version:", err)
  120. }
  121. }
  122. }
  123. return nil
  124. }