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 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. "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. versionsDir := filepath.Join(v.folderPath, ".stversions")
  45. _, err = os.Stat(versionsDir)
  46. if err != nil {
  47. if os.IsNotExist(err) {
  48. l.Debugln("creating versions dir", versionsDir)
  49. osutil.MkdirAll(versionsDir, 0755)
  50. osutil.HideFile(versionsDir)
  51. } else {
  52. return err
  53. }
  54. }
  55. l.Debugln("archiving", filePath)
  56. file := filepath.Base(filePath)
  57. inFolderPath, err := filepath.Rel(v.folderPath, filepath.Dir(filePath))
  58. if err != nil {
  59. return err
  60. }
  61. dir := filepath.Join(versionsDir, inFolderPath)
  62. err = osutil.MkdirAll(dir, 0755)
  63. if err != nil && !os.IsExist(err) {
  64. return err
  65. }
  66. ver := taggedFilename(file, fileInfo.ModTime().Format(TimeFormat))
  67. dst := filepath.Join(dir, ver)
  68. l.Debugln("moving to", dst)
  69. err = osutil.Rename(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, taggedFilename(file, TimeGlob))
  75. newVersions, err := osutil.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 := osutil.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 = os.Remove(toRemove)
  94. if err != nil {
  95. l.Warnln("removing old version:", err)
  96. }
  97. }
  98. }
  99. return nil
  100. }