versioner.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 implements common interfaces for file versioning and a
  7. // simple default versioning scheme.
  8. package versioner
  9. import (
  10. "fmt"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/fs"
  14. )
  15. type Versioner interface {
  16. Archive(filePath string) error
  17. GetVersions() (map[string][]FileVersion, error)
  18. Restore(filePath string, versionTime time.Time) error
  19. }
  20. type FileVersion struct {
  21. VersionTime time.Time `json:"versionTime"`
  22. ModTime time.Time `json:"modTime"`
  23. Size int64 `json:"size"`
  24. }
  25. type factory func(filesystem fs.Filesystem, params map[string]string) Versioner
  26. var factories = make(map[string]factory)
  27. var ErrRestorationNotSupported = fmt.Errorf("version restoration not supported with the current versioner")
  28. const (
  29. TimeFormat = "20060102-150405"
  30. timeGlob = "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]" // glob pattern matching TimeFormat
  31. )
  32. func New(fs fs.Filesystem, cfg config.VersioningConfiguration) (Versioner, error) {
  33. fac, ok := factories[cfg.Type]
  34. if !ok {
  35. return nil, fmt.Errorf("requested versioning type %q does not exist", cfg.Type)
  36. }
  37. return fac(fs, cfg.Params), nil
  38. }