simple.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 https://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. "github.com/syncthing/syncthing/lib/util"
  13. )
  14. func init() {
  15. // Register the constructor for this type of versioner with the name "simple"
  16. Factories["simple"] = NewSimple
  17. }
  18. type Simple struct {
  19. keep int
  20. folderPath string
  21. }
  22. func NewSimple(folderID, folderPath string, params map[string]string) Versioner {
  23. cleanSymlinks(folderPath)
  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. l.Debugf("instantiated %#v", s)
  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. l.Debugln("not archiving nonexistent file", filePath)
  41. return nil
  42. } else if err != nil {
  43. return err
  44. }
  45. if fileInfo.Mode()&os.ModeSymlink != 0 {
  46. panic("bug: attempting to version a symlink")
  47. }
  48. versionsDir := filepath.Join(v.folderPath, ".stversions")
  49. _, err = os.Stat(versionsDir)
  50. if err != nil {
  51. if os.IsNotExist(err) {
  52. l.Debugln("creating versions dir", versionsDir)
  53. osutil.MkdirAll(versionsDir, 0755)
  54. osutil.HideFile(versionsDir)
  55. } else {
  56. return err
  57. }
  58. }
  59. l.Debugln("archiving", filePath)
  60. file := filepath.Base(filePath)
  61. inFolderPath, err := filepath.Rel(v.folderPath, filepath.Dir(filePath))
  62. if err != nil {
  63. return err
  64. }
  65. dir := filepath.Join(versionsDir, inFolderPath)
  66. err = osutil.MkdirAll(dir, 0755)
  67. if err != nil && !os.IsExist(err) {
  68. return err
  69. }
  70. ver := taggedFilename(file, fileInfo.ModTime().Format(TimeFormat))
  71. dst := filepath.Join(dir, ver)
  72. l.Debugln("moving to", dst)
  73. err = osutil.Rename(filePath, dst)
  74. if err != nil {
  75. return err
  76. }
  77. // Glob according to the new file~timestamp.ext pattern.
  78. pattern := filepath.Join(dir, taggedFilename(file, TimeGlob))
  79. newVersions, err := osutil.Glob(pattern)
  80. if err != nil {
  81. l.Warnln("globbing:", err, "for", pattern)
  82. return nil
  83. }
  84. // Also according to the old file.ext~timestamp pattern.
  85. pattern = filepath.Join(dir, file+"~"+TimeGlob)
  86. oldVersions, err := osutil.Glob(pattern)
  87. if err != nil {
  88. l.Warnln("globbing:", err, "for", pattern)
  89. return nil
  90. }
  91. // Use all the found filenames. "~" sorts after "." so all old pattern
  92. // files will be deleted before any new, which is as it should be.
  93. versions := util.UniqueStrings(append(oldVersions, newVersions...))
  94. if len(versions) > v.keep {
  95. for _, toRemove := range versions[:len(versions)-v.keep] {
  96. l.Debugln("cleaning out", toRemove)
  97. err = os.Remove(toRemove)
  98. if err != nil {
  99. l.Warnln("removing old version:", err)
  100. }
  101. }
  102. }
  103. return nil
  104. }