simple.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. pattern := filepath.Join(dir, taggedFilename(file, TimeGlob))
  74. newVersions, err := osutil.Glob(pattern)
  75. if err != nil {
  76. l.Warnln("globbing:", err, "for", pattern)
  77. return nil
  78. }
  79. // Also according to the old file.ext~timestamp pattern.
  80. pattern = filepath.Join(dir, file+"~"+TimeGlob)
  81. oldVersions, err := osutil.Glob(pattern)
  82. if err != nil {
  83. l.Warnln("globbing:", err, "for", pattern)
  84. return nil
  85. }
  86. // Use all the found filenames. "~" sorts after "." so all old pattern
  87. // files will be deleted before any new, which is as it should be.
  88. versions := uniqueSortedStrings(append(oldVersions, newVersions...))
  89. if len(versions) > v.keep {
  90. for _, toRemove := range versions[:len(versions)-v.keep] {
  91. l.Debugln("cleaning out", toRemove)
  92. err = os.Remove(toRemove)
  93. if err != nil {
  94. l.Warnln("removing old version:", err)
  95. }
  96. }
  97. }
  98. return nil
  99. }