simple.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "path/filepath"
  9. "strconv"
  10. "github.com/syncthing/syncthing/lib/fs"
  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. fs fs.Filesystem
  21. }
  22. func NewSimple(folderID string, fs fs.Filesystem, params map[string]string) Versioner {
  23. keep, err := strconv.Atoi(params["keep"])
  24. if err != nil {
  25. keep = 5 // A reasonable default
  26. }
  27. s := Simple{
  28. keep: keep,
  29. fs: fs,
  30. }
  31. l.Debugf("instantiated %#v", s)
  32. return s
  33. }
  34. // Archive moves the named file away to a version archive. If this function
  35. // returns nil, the named file does not exist any more (has been archived).
  36. func (v Simple) Archive(filePath string) error {
  37. info, err := v.fs.Lstat(filePath)
  38. if fs.IsNotExist(err) {
  39. l.Debugln("not archiving nonexistent file", filePath)
  40. return nil
  41. } else if err != nil {
  42. return err
  43. }
  44. if info.IsSymlink() {
  45. panic("bug: attempting to version a symlink")
  46. }
  47. versionsDir := ".stversions"
  48. _, err = v.fs.Stat(versionsDir)
  49. if err != nil {
  50. if fs.IsNotExist(err) {
  51. l.Debugln("creating versions dir .stversions")
  52. v.fs.Mkdir(versionsDir, 0755)
  53. v.fs.Hide(versionsDir)
  54. } else {
  55. return err
  56. }
  57. }
  58. l.Debugln("archiving", filePath)
  59. file := filepath.Base(filePath)
  60. inFolderPath := filepath.Dir(filePath)
  61. dir := filepath.Join(versionsDir, inFolderPath)
  62. err = v.fs.MkdirAll(dir, 0755)
  63. if err != nil && !fs.IsExist(err) {
  64. return err
  65. }
  66. ver := TagFilename(file, info.ModTime().Format(TimeFormat))
  67. dst := filepath.Join(dir, ver)
  68. l.Debugln("moving to", dst)
  69. err = osutil.Rename(v.fs, filePath, dst)
  70. if err != nil {
  71. return err
  72. }
  73. // Glob according to the new file~timestamp.ext pattern.
  74. pattern := filepath.Join(dir, TagFilename(file, TimeGlob))
  75. newVersions, err := v.fs.Glob(pattern)
  76. if err != nil {
  77. l.Warnln("globbing:", err, "for", pattern)
  78. return nil
  79. }
  80. // Also according to the old file.ext~timestamp pattern.
  81. pattern = filepath.Join(dir, file+"~"+TimeGlob)
  82. oldVersions, err := v.fs.Glob(pattern)
  83. if err != nil {
  84. l.Warnln("globbing:", err, "for", pattern)
  85. return nil
  86. }
  87. // Use all the found filenames. "~" sorts after "." so all old pattern
  88. // files will be deleted before any new, which is as it should be.
  89. versions := util.UniqueStrings(append(oldVersions, newVersions...))
  90. if len(versions) > v.keep {
  91. for _, toRemove := range versions[:len(versions)-v.keep] {
  92. l.Debugln("cleaning out", toRemove)
  93. err = v.fs.Remove(toRemove)
  94. if err != nil {
  95. l.Warnln("removing old version:", err)
  96. }
  97. }
  98. }
  99. return nil
  100. }