virtualfs_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2017 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 scanner
  7. import (
  8. "errors"
  9. "fmt"
  10. "io"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "time"
  15. "github.com/syncthing/syncthing/lib/fs"
  16. )
  17. type infiniteFS struct {
  18. fs.Filesystem
  19. width int // number of files and directories per level
  20. depth int // number of tree levels to simulate
  21. filesize int64 // size of each file in bytes
  22. }
  23. var errNotSupp = errors.New("not supported")
  24. func (i infiniteFS) Lstat(name string) (fs.FileInfo, error) {
  25. return fakeInfo{name, i.filesize}, nil
  26. }
  27. func (i infiniteFS) Stat(name string) (fs.FileInfo, error) {
  28. return fakeInfo{name, i.filesize}, nil
  29. }
  30. func (i infiniteFS) DirNames(name string) ([]string, error) {
  31. // Returns a list of fake files and directories. Names are such that
  32. // files appear before directories - this makes it so the scanner will
  33. // actually see a few files without having to reach the max depth.
  34. var names []string
  35. for j := 0; j < i.width; j++ {
  36. names = append(names, fmt.Sprintf("aa-file-%d", j))
  37. }
  38. if len(strings.Split(name, string(os.PathSeparator))) < i.depth {
  39. for j := 0; j < i.width; j++ {
  40. names = append(names, fmt.Sprintf("zz-dir-%d", j))
  41. }
  42. }
  43. return names, nil
  44. }
  45. func (i infiniteFS) Open(name string) (fs.File, error) {
  46. return &fakeFile{name, i.filesize, 0}, nil
  47. }
  48. type singleFileFS struct {
  49. fs.Filesystem
  50. name string
  51. filesize int64
  52. }
  53. func (s singleFileFS) Lstat(name string) (fs.FileInfo, error) {
  54. switch name {
  55. case ".":
  56. return fakeInfo{".", 0}, nil
  57. case s.name:
  58. return fakeInfo{s.name, s.filesize}, nil
  59. default:
  60. return nil, errors.New("no such file")
  61. }
  62. }
  63. func (s singleFileFS) Stat(name string) (fs.FileInfo, error) {
  64. switch name {
  65. case ".":
  66. return fakeInfo{".", 0}, nil
  67. case s.name:
  68. return fakeInfo{s.name, s.filesize}, nil
  69. default:
  70. return nil, errors.New("no such file")
  71. }
  72. }
  73. func (s singleFileFS) DirNames(name string) ([]string, error) {
  74. if name != "." {
  75. return nil, errors.New("no such file")
  76. }
  77. return []string{s.name}, nil
  78. }
  79. func (s singleFileFS) Open(name string) (fs.File, error) {
  80. if name != s.name {
  81. return nil, errors.New("no such file")
  82. }
  83. return &fakeFile{s.name, s.filesize, 0}, nil
  84. }
  85. type fakeInfo struct {
  86. name string
  87. size int64
  88. }
  89. func (f fakeInfo) Name() string { return f.name }
  90. func (f fakeInfo) Mode() fs.FileMode { return 0755 }
  91. func (f fakeInfo) Size() int64 { return f.size }
  92. func (f fakeInfo) ModTime() time.Time { return time.Unix(1234567890, 0) }
  93. func (f fakeInfo) IsDir() bool { return strings.Contains(filepath.Base(f.name), "dir") || f.name == "." }
  94. func (f fakeInfo) IsRegular() bool { return !f.IsDir() }
  95. func (f fakeInfo) IsSymlink() bool { return false }
  96. func (f fakeInfo) Owner() int { return 0 }
  97. func (f fakeInfo) Group() int { return 0 }
  98. type fakeFile struct {
  99. name string
  100. size int64
  101. readOffset int64
  102. }
  103. func (f *fakeFile) Name() string {
  104. return f.name
  105. }
  106. func (f *fakeFile) Read(bs []byte) (int, error) {
  107. remaining := f.size - f.readOffset
  108. if remaining == 0 {
  109. return 0, io.EOF
  110. }
  111. if remaining < int64(len(bs)) {
  112. f.readOffset = f.size
  113. return int(remaining), nil
  114. }
  115. f.readOffset += int64(len(bs))
  116. return len(bs), nil
  117. }
  118. func (f *fakeFile) Stat() (fs.FileInfo, error) {
  119. return fakeInfo{f.name, f.size}, nil
  120. }
  121. func (f *fakeFile) Write([]byte) (int, error) { return 0, errNotSupp }
  122. func (f *fakeFile) WriteAt([]byte, int64) (int, error) { return 0, errNotSupp }
  123. func (f *fakeFile) Close() error { return nil }
  124. func (f *fakeFile) Truncate(size int64) error { return errNotSupp }
  125. func (f *fakeFile) ReadAt([]byte, int64) (int, error) { return 0, errNotSupp }
  126. func (f *fakeFile) Seek(int64, int) (int64, error) { return 0, errNotSupp }
  127. func (f *fakeFile) Sync() error { return nil }