simple.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. }
  57. return err
  58. }
  59. versionsDir := filepath.Join(v.folderPath, ".stversions")
  60. _, err = os.Stat(versionsDir)
  61. if err != nil {
  62. if os.IsNotExist(err) {
  63. if debug {
  64. l.Debugln("creating versions dir", versionsDir)
  65. }
  66. os.MkdirAll(versionsDir, 0755)
  67. osutil.HideFile(versionsDir)
  68. } else {
  69. return err
  70. }
  71. }
  72. if debug {
  73. l.Debugln("archiving", filePath)
  74. }
  75. file := filepath.Base(filePath)
  76. inFolderPath, err := filepath.Rel(v.folderPath, filepath.Dir(filePath))
  77. if err != nil {
  78. return err
  79. }
  80. dir := filepath.Join(versionsDir, inFolderPath)
  81. err = os.MkdirAll(dir, 0755)
  82. if err != nil && !os.IsExist(err) {
  83. return err
  84. }
  85. ver := taggedFilename(file, fileInfo.ModTime().Format(TimeFormat))
  86. dst := filepath.Join(dir, ver)
  87. if debug {
  88. l.Debugln("moving to", dst)
  89. }
  90. err = osutil.Rename(filePath, dst)
  91. if err != nil {
  92. return err
  93. }
  94. // Glob according to the new file~timestamp.ext pattern.
  95. newVersions, err := filepath.Glob(filepath.Join(dir, taggedFilename(file, TimeGlob)))
  96. if err != nil {
  97. l.Warnln("globbing:", err)
  98. return nil
  99. }
  100. // Also according to the old file.ext~timestamp pattern.
  101. oldVersions, err := filepath.Glob(filepath.Join(dir, file+"~"+TimeGlob))
  102. if err != nil {
  103. l.Warnln("globbing:", err)
  104. return nil
  105. }
  106. // Use all the found filenames. "~" sorts after "." so all old pattern
  107. // files will be deleted before any new, which is as it should be.
  108. versions := uniqueSortedStrings(append(oldVersions, newVersions...))
  109. if len(versions) > v.keep {
  110. for _, toRemove := range versions[:len(versions)-v.keep] {
  111. if debug {
  112. l.Debugln("cleaning out", toRemove)
  113. }
  114. err = os.Remove(toRemove)
  115. if err != nil {
  116. l.Warnln("removing old version:", err)
  117. }
  118. }
  119. }
  120. return nil
  121. }