simple.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "strconv"
  20. "github.com/syncthing/syncthing/internal/osutil"
  21. )
  22. func init() {
  23. // Register the constructor for this type of versioner with the name "simple"
  24. Factories["simple"] = NewSimple
  25. }
  26. // The type holds our configuration
  27. type Simple struct {
  28. keep int
  29. folderPath string
  30. }
  31. // The constructor function takes a map of parameters and creates the type.
  32. func NewSimple(folderID, folderPath string, params map[string]string) Versioner {
  33. keep, err := strconv.Atoi(params["keep"])
  34. if err != nil {
  35. keep = 5 // A reasonable default
  36. }
  37. s := Simple{
  38. keep: keep,
  39. folderPath: folderPath,
  40. }
  41. if debug {
  42. l.Debugf("instantiated %#v", s)
  43. }
  44. return s
  45. }
  46. // Move away the named file to a version archive. If this function returns
  47. // nil, the named file does not exist any more (has been archived).
  48. func (v Simple) Archive(filePath string) error {
  49. fileInfo, err := os.Lstat(filePath)
  50. if err != nil {
  51. if os.IsNotExist(err) {
  52. if debug {
  53. l.Debugln("not archiving nonexistent file", filePath)
  54. }
  55. return nil
  56. } else {
  57. return err
  58. }
  59. }
  60. versionsDir := filepath.Join(v.folderPath, ".stversions")
  61. _, err = os.Stat(versionsDir)
  62. if err != nil {
  63. if os.IsNotExist(err) {
  64. if debug {
  65. l.Debugln("creating versions dir", versionsDir)
  66. }
  67. os.MkdirAll(versionsDir, 0755)
  68. osutil.HideFile(versionsDir)
  69. } else {
  70. return err
  71. }
  72. }
  73. if debug {
  74. l.Debugln("archiving", filePath)
  75. }
  76. file := filepath.Base(filePath)
  77. inFolderPath, err := filepath.Rel(v.folderPath, filepath.Dir(filePath))
  78. if err != nil {
  79. return err
  80. }
  81. dir := filepath.Join(versionsDir, inFolderPath)
  82. err = os.MkdirAll(dir, 0755)
  83. if err != nil && !os.IsExist(err) {
  84. return err
  85. }
  86. ver := taggedFilename(file, fileInfo.ModTime().Format(TimeFormat))
  87. dst := filepath.Join(dir, ver)
  88. if debug {
  89. l.Debugln("moving to", dst)
  90. }
  91. err = osutil.Rename(filePath, dst)
  92. if err != nil {
  93. return err
  94. }
  95. // Glob according to the new file~timestamp.ext pattern.
  96. newVersions, err := filepath.Glob(filepath.Join(dir, taggedFilename(file, TimeGlob)))
  97. if err != nil {
  98. l.Warnln("globbing:", err)
  99. return nil
  100. }
  101. // Also according to the old file.ext~timestamp pattern.
  102. oldVersions, err := filepath.Glob(filepath.Join(dir, file+"~"+TimeGlob))
  103. if err != nil {
  104. l.Warnln("globbing:", err)
  105. return nil
  106. }
  107. // Use all the found filenames. "~" sorts after "." so all old pattern
  108. // files will be deleted before any new, which is as it should be.
  109. versions := uniqueSortedStrings(append(oldVersions, newVersions...))
  110. if len(versions) > v.keep {
  111. for _, toRemove := range versions[:len(versions)-v.keep] {
  112. if debug {
  113. l.Debugln("cleaning out", toRemove)
  114. }
  115. err = os.Remove(toRemove)
  116. if err != nil {
  117. l.Warnln("removing old version:", err)
  118. }
  119. }
  120. }
  121. return nil
  122. }