simple.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "time"
  11. "github.com/syncthing/syncthing/lib/fs"
  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. folderFs fs.Filesystem
  21. versionsFs fs.Filesystem
  22. }
  23. func NewSimple(folderID string, folderFs fs.Filesystem, params map[string]string) Versioner {
  24. keep, err := strconv.Atoi(params["keep"])
  25. if err != nil {
  26. keep = 5 // A reasonable default
  27. }
  28. s := Simple{
  29. keep: keep,
  30. folderFs: folderFs,
  31. versionsFs: fsFromParams(folderFs, params),
  32. }
  33. l.Debugf("instantiated %#v", s)
  34. return s
  35. }
  36. // Archive moves the named file away to a version archive. If this function
  37. // returns nil, the named file does not exist any more (has been archived).
  38. func (v Simple) Archive(filePath string) error {
  39. err := archiveFile(v.folderFs, v.versionsFs, filePath, TagFilename)
  40. if err != nil {
  41. return err
  42. }
  43. file := filepath.Base(filePath)
  44. dir := filepath.Dir(filePath)
  45. // Glob according to the new file~timestamp.ext pattern.
  46. pattern := filepath.Join(dir, TagFilename(file, TimeGlob))
  47. newVersions, err := v.versionsFs.Glob(pattern)
  48. if err != nil {
  49. l.Warnln("globbing:", err, "for", pattern)
  50. return nil
  51. }
  52. // Also according to the old file.ext~timestamp pattern.
  53. pattern = filepath.Join(dir, file+"~"+TimeGlob)
  54. oldVersions, err := v.versionsFs.Glob(pattern)
  55. if err != nil {
  56. l.Warnln("globbing:", err, "for", pattern)
  57. return nil
  58. }
  59. // Use all the found filenames. "~" sorts after "." so all old pattern
  60. // files will be deleted before any new, which is as it should be.
  61. versions := util.UniqueStrings(append(oldVersions, newVersions...))
  62. if len(versions) > v.keep {
  63. for _, toRemove := range versions[:len(versions)-v.keep] {
  64. l.Debugln("cleaning out", toRemove)
  65. err = v.versionsFs.Remove(toRemove)
  66. if err != nil {
  67. l.Warnln("removing old version:", err)
  68. }
  69. }
  70. }
  71. return nil
  72. }
  73. func (v Simple) GetVersions() (map[string][]FileVersion, error) {
  74. return retrieveVersions(v.versionsFs)
  75. }
  76. func (v Simple) Restore(filepath string, versionTime time.Time) error {
  77. return restoreFile(v.versionsFs, v.folderFs, filepath, versionTime, TagFilename)
  78. }