simple.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. keep, err := strconv.Atoi(params["keep"])
  24. if err != nil {
  25. keep = 5 // A reasonable default
  26. }
  27. s := Simple{
  28. keep: keep,
  29. folderPath: folderPath,
  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. fileInfo, err := osutil.Lstat(filePath)
  38. if os.IsNotExist(err) {
  39. l.Debugln("not archiving nonexistent file", filePath)
  40. return nil
  41. } else if err != nil {
  42. return err
  43. }
  44. if fileInfo.Mode()&os.ModeSymlink != 0 {
  45. panic("bug: attempting to version a symlink")
  46. }
  47. versionsDir := filepath.Join(v.folderPath, ".stversions")
  48. _, err = os.Stat(versionsDir)
  49. if err != nil {
  50. if os.IsNotExist(err) {
  51. l.Debugln("creating versions dir", versionsDir)
  52. osutil.MkdirAll(versionsDir, 0755)
  53. osutil.HideFile(versionsDir)
  54. } else {
  55. return err
  56. }
  57. }
  58. l.Debugln("archiving", filePath)
  59. file := filepath.Base(filePath)
  60. inFolderPath, err := filepath.Rel(v.folderPath, filepath.Dir(filePath))
  61. if err != nil {
  62. return err
  63. }
  64. dir := filepath.Join(versionsDir, inFolderPath)
  65. err = osutil.MkdirAll(dir, 0755)
  66. if err != nil && !os.IsExist(err) {
  67. return err
  68. }
  69. ver := taggedFilename(file, fileInfo.ModTime().Format(TimeFormat))
  70. dst := filepath.Join(dir, ver)
  71. l.Debugln("moving to", dst)
  72. err = osutil.Rename(filePath, dst)
  73. if err != nil {
  74. return err
  75. }
  76. // Glob according to the new file~timestamp.ext pattern.
  77. pattern := filepath.Join(dir, taggedFilename(file, TimeGlob))
  78. newVersions, err := osutil.Glob(pattern)
  79. if err != nil {
  80. l.Warnln("globbing:", err, "for", pattern)
  81. return nil
  82. }
  83. // Also according to the old file.ext~timestamp pattern.
  84. pattern = filepath.Join(dir, file+"~"+TimeGlob)
  85. oldVersions, err := osutil.Glob(pattern)
  86. if err != nil {
  87. l.Warnln("globbing:", err, "for", pattern)
  88. return nil
  89. }
  90. // Use all the found filenames. "~" sorts after "." so all old pattern
  91. // files will be deleted before any new, which is as it should be.
  92. versions := util.UniqueStrings(append(oldVersions, newVersions...))
  93. if len(versions) > v.keep {
  94. for _, toRemove := range versions[:len(versions)-v.keep] {
  95. l.Debugln("cleaning out", toRemove)
  96. err = os.Remove(toRemove)
  97. if err != nil {
  98. l.Warnln("removing old version:", err)
  99. }
  100. }
  101. }
  102. return nil
  103. }