versioner.go 1.4 KB

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