1
0

trashcan_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. "github.com/syncthing/syncthing/lib/config"
  9. "io/ioutil"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "time"
  14. "github.com/syncthing/syncthing/lib/fs"
  15. )
  16. func TestTrashcanCleanout(t *testing.T) {
  17. // Verify that files older than the cutoff are removed, that files newer
  18. // than the cutoff are *not* removed, and that empty directories are
  19. // removed (best effort).
  20. var testcases = []struct {
  21. file string
  22. shouldRemove bool
  23. }{
  24. {"testdata/.stversions/file1", false},
  25. {"testdata/.stversions/file2", true},
  26. {"testdata/.stversions/keep1/file1", false},
  27. {"testdata/.stversions/keep1/file2", false},
  28. {"testdata/.stversions/keep2/file1", false},
  29. {"testdata/.stversions/keep2/file2", true},
  30. {"testdata/.stversions/keep3/keepsubdir/file1", false},
  31. {"testdata/.stversions/remove/file1", true},
  32. {"testdata/.stversions/remove/file2", true},
  33. {"testdata/.stversions/remove/removesubdir/file1", true},
  34. }
  35. os.RemoveAll("testdata")
  36. defer os.RemoveAll("testdata")
  37. oldTime := time.Now().Add(-8 * 24 * time.Hour)
  38. for _, tc := range testcases {
  39. os.MkdirAll(filepath.Dir(tc.file), 0777)
  40. if err := ioutil.WriteFile(tc.file, []byte("data"), 0644); err != nil {
  41. t.Fatal(err)
  42. }
  43. if tc.shouldRemove {
  44. if err := os.Chtimes(tc.file, oldTime, oldTime); err != nil {
  45. t.Fatal(err)
  46. }
  47. }
  48. }
  49. cfg := config.FolderConfiguration{
  50. FilesystemType: fs.FilesystemTypeBasic,
  51. Path: "testdata",
  52. Versioning: config.VersioningConfiguration{
  53. Params: map[string]string{
  54. "cleanoutDays": "7",
  55. },
  56. },
  57. }
  58. versioner := newTrashcan(cfg).(*trashcan)
  59. if err := versioner.cleanoutArchive(); err != nil {
  60. t.Fatal(err)
  61. }
  62. for _, tc := range testcases {
  63. _, err := os.Lstat(tc.file)
  64. if tc.shouldRemove && !os.IsNotExist(err) {
  65. t.Error(tc.file, "should have been removed")
  66. } else if !tc.shouldRemove && err != nil {
  67. t.Error(tc.file, "should not have been removed")
  68. }
  69. }
  70. if _, err := os.Lstat("testdata/.stversions/keep3"); os.IsNotExist(err) {
  71. t.Error("directory with non empty subdirs should not be removed")
  72. }
  73. if _, err := os.Lstat("testdata/.stversions/remove"); !os.IsNotExist(err) {
  74. t.Error("empty directory should have been removed")
  75. }
  76. }
  77. func TestTrashcanArchiveRestoreSwitcharoo(t *testing.T) {
  78. // This tests that trashcan versioner restoration correctly archives existing file, because trashcan versioner
  79. // files are untagged, archiving existing file to replace with a restored version technically should collide in
  80. // in names.
  81. tmpDir1, err := ioutil.TempDir("", "")
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. tmpDir2, err := ioutil.TempDir("", "")
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. cfg := config.FolderConfiguration{
  90. FilesystemType: fs.FilesystemTypeBasic,
  91. Path: tmpDir1,
  92. Versioning: config.VersioningConfiguration{
  93. Params: map[string]string{
  94. "fsType": "basic",
  95. "fsPath": tmpDir2,
  96. },
  97. },
  98. }
  99. folderFs := cfg.Filesystem()
  100. versionsFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir2)
  101. writeFile(t, folderFs, "file", "A")
  102. versioner := newTrashcan(cfg)
  103. if err := versioner.Archive("file"); err != nil {
  104. t.Fatal(err)
  105. }
  106. if _, err := folderFs.Stat("file"); !fs.IsNotExist(err) {
  107. t.Fatal(err)
  108. }
  109. // Check versions
  110. versions, err := versioner.GetVersions()
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. fileVersions := versions["file"]
  115. if len(fileVersions) != 1 {
  116. t.Fatalf("unexpected number of versions: %d != 1", len(fileVersions))
  117. }
  118. fileVersion := fileVersions[0]
  119. if !fileVersion.ModTime.Equal(fileVersion.VersionTime) {
  120. t.Error("time mismatch")
  121. }
  122. if content := readFile(t, versionsFs, "file"); content != "A" {
  123. t.Errorf("expected A got %s", content)
  124. }
  125. writeFile(t, folderFs, "file", "B")
  126. versionInfo, err := versionsFs.Stat("file")
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. if !versionInfo.ModTime().Truncate(time.Second).Equal(fileVersion.ModTime) {
  131. t.Error("time mismatch")
  132. }
  133. if err := versioner.Restore("file", fileVersion.VersionTime); err != nil {
  134. t.Fatal(err)
  135. }
  136. if content := readFile(t, folderFs, "file"); content != "A" {
  137. t.Errorf("expected A got %s", content)
  138. }
  139. if content := readFile(t, versionsFs, "file"); content != "B" {
  140. t.Errorf("expected B got %s", content)
  141. }
  142. }
  143. func readFile(t *testing.T, filesystem fs.Filesystem, name string) string {
  144. fd, err := filesystem.Open(name)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. defer fd.Close()
  149. buf, err := ioutil.ReadAll(fd)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. return string(buf)
  154. }
  155. func writeFile(t *testing.T, filesystem fs.Filesystem, name, content string) {
  156. fd, err := filesystem.OpenFile(name, fs.OptReadWrite|fs.OptCreate, 0777)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. defer fd.Close()
  161. if err := fd.Truncate(int64(len(content))); err != nil {
  162. t.Fatal(err)
  163. }
  164. if n, err := fd.Write([]byte(content)); err != nil || n != len(content) {
  165. t.Fatal(n, len(content), err)
  166. }
  167. }