trashcan.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (C) 2015 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. "fmt"
  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
  17. factories["trashcan"] = newTrashcan
  18. }
  19. type trashcan struct {
  20. folderFs fs.Filesystem
  21. versionsFs fs.Filesystem
  22. cleanoutDays int
  23. copyRangeMethod fs.CopyRangeMethod
  24. }
  25. func newTrashcan(cfg config.FolderConfiguration) Versioner {
  26. cleanoutDays, _ := strconv.Atoi(cfg.Versioning.Params["cleanoutDays"])
  27. // On error we default to 0, "do not clean out the trash can"
  28. s := &trashcan{
  29. folderFs: cfg.Filesystem(),
  30. versionsFs: versionerFsFromFolderCfg(cfg),
  31. cleanoutDays: cleanoutDays,
  32. copyRangeMethod: cfg.CopyRangeMethod,
  33. }
  34. l.Debugf("instantiated %#v", s)
  35. return s
  36. }
  37. // Archive moves the named file away to a version archive. If this function
  38. // returns nil, the named file does not exist any more (has been archived).
  39. func (t *trashcan) Archive(filePath string) error {
  40. return archiveFile(t.copyRangeMethod, t.folderFs, t.versionsFs, filePath, func(name, tag string) string {
  41. return name
  42. })
  43. }
  44. func (t *trashcan) String() string {
  45. return fmt.Sprintf("trashcan@%p", t)
  46. }
  47. func (t *trashcan) Clean(ctx context.Context) error {
  48. return cleanByDay(ctx, t.versionsFs, t.cleanoutDays)
  49. }
  50. func (t *trashcan) GetVersions() (map[string][]FileVersion, error) {
  51. return retrieveVersions(t.versionsFs)
  52. }
  53. func (t *trashcan) Restore(filepath string, versionTime time.Time) error {
  54. // If we have an untagged file A and want to restore it on top of existing file A, we can't first archive the
  55. // existing A as we'd overwrite the old A version, therefore when we archive existing file, we archive it with a
  56. // tag but when the restoration is finished, we rename it (untag it). This is only important if when restoring A,
  57. // there already exists a file at the same location
  58. taggedName := ""
  59. tagger := func(name, tag string) string {
  60. // We also abuse the fact that tagger gets called twice, once for tagging the restoration version, which
  61. // should just return the plain name, and second time by archive which archives existing file in the folder.
  62. // We can't use TagFilename here, as restoreFile would discover that as a valid version and restore that instead.
  63. if taggedName != "" {
  64. return taggedName
  65. }
  66. taggedName = fs.TempName(name)
  67. return name
  68. }
  69. err := restoreFile(t.copyRangeMethod, t.versionsFs, t.folderFs, filepath, versionTime, tagger)
  70. if taggedName == "" {
  71. return err
  72. }
  73. return t.versionsFs.Rename(taggedName, filepath)
  74. }