simple.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. l.Debugf("instantiated %#v", s)
  31. return s
  32. }
  33. // Archive moves the named file away to a version archive. If this function
  34. // returns nil, the named file does not exist any more (has been archived).
  35. func (v Simple) Archive(filePath string) error {
  36. fileInfo, err := osutil.Lstat(filePath)
  37. if os.IsNotExist(err) {
  38. l.Debugln("not archiving nonexistent file", filePath)
  39. return nil
  40. } else if err != nil {
  41. return err
  42. }
  43. versionsDir := filepath.Join(v.folderPath, ".stversions")
  44. _, err = os.Stat(versionsDir)
  45. if err != nil {
  46. if os.IsNotExist(err) {
  47. l.Debugln("creating versions dir", versionsDir)
  48. osutil.MkdirAll(versionsDir, 0755)
  49. osutil.HideFile(versionsDir)
  50. } else {
  51. return err
  52. }
  53. }
  54. l.Debugln("archiving", filePath)
  55. file := filepath.Base(filePath)
  56. inFolderPath, err := filepath.Rel(v.folderPath, filepath.Dir(filePath))
  57. if err != nil {
  58. return err
  59. }
  60. dir := filepath.Join(versionsDir, inFolderPath)
  61. err = osutil.MkdirAll(dir, 0755)
  62. if err != nil && !os.IsExist(err) {
  63. return err
  64. }
  65. ver := taggedFilename(file, fileInfo.ModTime().Format(TimeFormat))
  66. dst := filepath.Join(dir, ver)
  67. l.Debugln("moving to", dst)
  68. err = osutil.Rename(filePath, dst)
  69. if err != nil {
  70. return err
  71. }
  72. // Glob according to the new file~timestamp.ext pattern.
  73. newVersions, err := osutil.Glob(filepath.Join(dir, taggedFilename(file, TimeGlob)))
  74. if err != nil {
  75. l.Warnln("globbing:", err)
  76. return nil
  77. }
  78. // Also according to the old file.ext~timestamp pattern.
  79. oldVersions, err := osutil.Glob(filepath.Join(dir, file+"~"+TimeGlob))
  80. if err != nil {
  81. l.Warnln("globbing:", err)
  82. return nil
  83. }
  84. // Use all the found filenames. "~" sorts after "." so all old pattern
  85. // files will be deleted before any new, which is as it should be.
  86. versions := uniqueSortedStrings(append(oldVersions, newVersions...))
  87. if len(versions) > v.keep {
  88. for _, toRemove := range versions[:len(versions)-v.keep] {
  89. l.Debugln("cleaning out", toRemove)
  90. err = os.Remove(toRemove)
  91. if err != nil {
  92. l.Warnln("removing old version:", err)
  93. }
  94. }
  95. }
  96. return nil
  97. }