simple.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package versioner
  5. import (
  6. "os"
  7. "path/filepath"
  8. "sort"
  9. "strconv"
  10. "time"
  11. "github.com/syncthing/syncthing/osutil"
  12. )
  13. func init() {
  14. // Register the constructor for this type of versioner with the name "simple"
  15. Factories["simple"] = NewSimple
  16. }
  17. // The type holds our configuration
  18. type Simple struct {
  19. keep int
  20. repoPath string
  21. }
  22. // The constructor function takes a map of parameters and creates the type.
  23. func NewSimple(repoID, repoPath string, 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. repoPath: repoPath,
  31. }
  32. if debug {
  33. l.Debugf("instantiated %#v", s)
  34. }
  35. return s
  36. }
  37. // Move away the named file to a version archive. If this function returns
  38. // nil, the named file does not exist any more (has been archived).
  39. func (v Simple) Archive(filePath string) error {
  40. _, err := os.Stat(filePath)
  41. if err != nil {
  42. if os.IsNotExist(err) {
  43. if debug {
  44. l.Debugln("not archiving nonexistent file", filePath)
  45. }
  46. return nil
  47. } else {
  48. return err
  49. }
  50. }
  51. versionsDir := filepath.Join(v.repoPath, ".stversions")
  52. _, err = os.Stat(versionsDir)
  53. if err != nil {
  54. if os.IsNotExist(err) {
  55. if debug {
  56. l.Debugln("creating versions dir", versionsDir)
  57. }
  58. os.MkdirAll(versionsDir, 0755)
  59. osutil.HideFile(versionsDir)
  60. } else {
  61. return err
  62. }
  63. }
  64. if debug {
  65. l.Debugln("archiving", filePath)
  66. }
  67. file := filepath.Base(filePath)
  68. inRepoPath, err := filepath.Rel(v.repoPath, filepath.Dir(filePath))
  69. if err != nil {
  70. return err
  71. }
  72. dir := filepath.Join(versionsDir, inRepoPath)
  73. err = os.MkdirAll(dir, 0755)
  74. if err != nil && !os.IsExist(err) {
  75. return err
  76. }
  77. ver := file + "~" + time.Now().Format("20060102-150405")
  78. dst := filepath.Join(dir, ver)
  79. if debug {
  80. l.Debugln("moving to", dst)
  81. }
  82. err = osutil.Rename(filePath, dst)
  83. if err != nil {
  84. return err
  85. }
  86. versions, err := filepath.Glob(filepath.Join(dir, file+"~*"))
  87. if err != nil {
  88. l.Warnln("globbing:", err)
  89. return nil
  90. }
  91. if len(versions) > v.keep {
  92. sort.Strings(versions)
  93. for _, toRemove := range versions[:len(versions)-v.keep] {
  94. if debug {
  95. l.Debugln("cleaning out", toRemove)
  96. }
  97. err = os.Remove(toRemove)
  98. if err != nil {
  99. l.Warnln("removing old version:", err)
  100. }
  101. }
  102. }
  103. return nil
  104. }