simple.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "context"
  9. "sort"
  10. "strconv"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/fs"
  14. )
  15. func init() {
  16. // Register the constructor for this type of versioner with the name "simple"
  17. factories["simple"] = newSimple
  18. }
  19. type simple struct {
  20. keep int
  21. cleanoutDays int
  22. folderFs fs.Filesystem
  23. versionsFs fs.Filesystem
  24. copyRangeMethod fs.CopyRangeMethod
  25. }
  26. func newSimple(cfg config.FolderConfiguration) Versioner {
  27. var keep, err = strconv.Atoi(cfg.Versioning.Params["keep"])
  28. cleanoutDays, _ := strconv.Atoi(cfg.Versioning.Params["cleanoutDays"])
  29. // On error we default to 0, "do not clean out the versioned items"
  30. if err != nil {
  31. keep = 5 // A reasonable default
  32. }
  33. s := simple{
  34. keep: keep,
  35. cleanoutDays: cleanoutDays,
  36. folderFs: cfg.Filesystem(nil),
  37. versionsFs: versionerFsFromFolderCfg(cfg),
  38. copyRangeMethod: cfg.CopyRangeMethod,
  39. }
  40. l.Debugf("instantiated %#v", s)
  41. return s
  42. }
  43. // Archive moves the named file away to a version archive. If this function
  44. // returns nil, the named file does not exist any more (has been archived).
  45. func (v simple) Archive(filePath string) error {
  46. err := archiveFile(v.copyRangeMethod, v.folderFs, v.versionsFs, filePath, TagFilename)
  47. if err != nil {
  48. return err
  49. }
  50. cleanVersions(v.versionsFs, findAllVersions(v.versionsFs, filePath), v.toRemove)
  51. return nil
  52. }
  53. func (v simple) GetVersions() (map[string][]FileVersion, error) {
  54. return retrieveVersions(v.versionsFs)
  55. }
  56. func (v simple) Restore(filepath string, versionTime time.Time) error {
  57. return restoreFile(v.copyRangeMethod, v.versionsFs, v.folderFs, filepath, versionTime, TagFilename)
  58. }
  59. func (v simple) Clean(ctx context.Context) error {
  60. return clean(ctx, v.versionsFs, v.toRemove)
  61. }
  62. func (v simple) toRemove(versions []string, now time.Time) []string {
  63. var remove []string
  64. // The list of versions may or may not be properly sorted.
  65. sort.Strings(versions)
  66. // If the amount of elements exceeds the limit: the oldest elements are to be removed.
  67. if len(versions) > v.keep {
  68. remove = versions[:len(versions)-v.keep]
  69. versions = versions[len(versions)-v.keep:]
  70. }
  71. // If cleanoutDays is not a positive value then don't remove based on age.
  72. if v.cleanoutDays <= 0 {
  73. return remove
  74. }
  75. maxAge := time.Duration(v.cleanoutDays) * 24 * time.Hour
  76. // For the rest of the versions, elements which are too old are to be removed
  77. for _, version := range versions {
  78. versionTime, err := time.ParseInLocation(TimeFormat, extractTag(version), time.Local)
  79. if err != nil {
  80. l.Debugf("Versioner: file name %q is invalid: %v", version, err)
  81. continue
  82. }
  83. if now.Sub(versionTime) > maxAge {
  84. remove = append(remove, version)
  85. }
  86. }
  87. return remove
  88. }