simple.go 3.0 KB

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