simple.go 2.9 KB

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