osutil_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 osutil_test
  7. import (
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. "testing"
  14. "github.com/syncthing/syncthing/lib/fs"
  15. "github.com/syncthing/syncthing/lib/osutil"
  16. )
  17. func TestIsDeleted(t *testing.T) {
  18. type tc struct {
  19. path string
  20. isDel bool
  21. }
  22. cases := []tc{
  23. {"del", true},
  24. {"del.file", false},
  25. {filepath.Join("del", "del"), true},
  26. {"file", false},
  27. {"linkToFile", false},
  28. {"linkToDel", false},
  29. {"linkToDir", false},
  30. {filepath.Join("linkToDir", "file"), true},
  31. {filepath.Join("file", "behindFile"), true},
  32. {"dir", false},
  33. {"dir.file", false},
  34. {filepath.Join("dir", "file"), false},
  35. {filepath.Join("dir", "del"), true},
  36. {filepath.Join("dir", "del", "del"), true},
  37. {filepath.Join("del", "del", "del"), true},
  38. }
  39. testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
  40. testFs.MkdirAll("dir", 0777)
  41. for _, f := range []string{"file", "del.file", "dir.file", filepath.Join("dir", "file")} {
  42. fd, err := testFs.Create(f)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. fd.Close()
  47. }
  48. if runtime.GOOS != "windows" {
  49. // Can't create unreadable dir on windows
  50. testFs.MkdirAll("inacc", 0777)
  51. if err := testFs.Chmod("inacc", 0000); err == nil {
  52. if _, err := testFs.Lstat(filepath.Join("inacc", "file")); fs.IsPermission(err) {
  53. // May fail e.g. if tests are run as root -> just skip
  54. cases = append(cases, tc{"inacc", false}, tc{filepath.Join("inacc", "file"), false})
  55. }
  56. }
  57. }
  58. for _, n := range []string{"Dir", "File", "Del"} {
  59. if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, strings.ToLower(n), "linkTo"+n); err != nil {
  60. if runtime.GOOS == "windows" {
  61. t.Skip("Symlinks aren't working")
  62. }
  63. t.Fatal(err)
  64. }
  65. }
  66. for _, c := range cases {
  67. if osutil.IsDeleted(testFs, c.path) != c.isDel {
  68. t.Errorf("IsDeleted(%v) != %v", c.path, c.isDel)
  69. }
  70. }
  71. testFs.Chmod("inacc", 0777)
  72. os.RemoveAll("testdata")
  73. }
  74. func TestRenameOrCopy(t *testing.T) {
  75. mustTempDir := func() string {
  76. t.Helper()
  77. tmpDir, err := os.MkdirTemp("", "")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. return tmpDir
  82. }
  83. sameFs := fs.NewFilesystem(fs.FilesystemTypeBasic, mustTempDir())
  84. tests := []struct {
  85. src fs.Filesystem
  86. dst fs.Filesystem
  87. file string
  88. }{
  89. {
  90. src: sameFs,
  91. dst: sameFs,
  92. file: "file",
  93. },
  94. {
  95. src: fs.NewFilesystem(fs.FilesystemTypeBasic, mustTempDir()),
  96. dst: fs.NewFilesystem(fs.FilesystemTypeBasic, mustTempDir()),
  97. file: "file",
  98. },
  99. {
  100. src: fs.NewFilesystem(fs.FilesystemTypeFake, `fake://fake/?files=1&seed=42`),
  101. dst: fs.NewFilesystem(fs.FilesystemTypeBasic, mustTempDir()),
  102. file: osutil.NativeFilename(`05/7a/4d52f284145b9fe8`),
  103. },
  104. }
  105. for _, test := range tests {
  106. content := test.src.URI()
  107. if _, err := test.src.Lstat(test.file); err != nil {
  108. if !fs.IsNotExist(err) {
  109. t.Fatal(err)
  110. }
  111. if fd, err := test.src.Create(test.file); err != nil {
  112. t.Fatal(err)
  113. } else {
  114. if _, err := fd.Write([]byte(test.src.URI())); err != nil {
  115. t.Fatal(err)
  116. }
  117. _ = fd.Close()
  118. }
  119. } else {
  120. fd, err := test.src.Open(test.file)
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. buf, err := io.ReadAll(fd)
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. _ = fd.Close()
  129. content = string(buf)
  130. }
  131. err := osutil.RenameOrCopy(fs.CopyRangeMethodStandard, test.src, test.dst, test.file, "new")
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. if fd, err := test.dst.Open("new"); err != nil {
  136. t.Fatal(err)
  137. } else {
  138. if buf, err := io.ReadAll(fd); err != nil {
  139. t.Fatal(err)
  140. } else if string(buf) != content {
  141. t.Fatalf("expected %s got %s", content, string(buf))
  142. }
  143. }
  144. }
  145. }