virtualfs_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "path/filepath"
  12. "strings"
  13. "time"
  14. "github.com/syncthing/syncthing/lib/fs"
  15. "github.com/syncthing/syncthing/lib/protocol"
  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(fs.PathComponents(name)) < 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. func (infiniteFS) PlatformData(_ string, _, _ bool, _ fs.XattrFilter) (protocol.PlatformData, error) {
  49. return protocol.PlatformData{}, nil
  50. }
  51. type singleFileFS struct {
  52. fs.Filesystem
  53. name string
  54. filesize int64
  55. }
  56. func (s singleFileFS) Lstat(name string) (fs.FileInfo, error) {
  57. switch name {
  58. case ".":
  59. return fakeInfo{".", 0}, nil
  60. case s.name:
  61. return fakeInfo{s.name, s.filesize}, nil
  62. default:
  63. return nil, errors.New("no such file")
  64. }
  65. }
  66. func (s singleFileFS) Stat(name string) (fs.FileInfo, error) {
  67. switch name {
  68. case ".":
  69. return fakeInfo{".", 0}, nil
  70. case s.name:
  71. return fakeInfo{s.name, s.filesize}, nil
  72. default:
  73. return nil, errors.New("no such file")
  74. }
  75. }
  76. func (s singleFileFS) DirNames(name string) ([]string, error) {
  77. if name != "." {
  78. return nil, errors.New("no such file")
  79. }
  80. return []string{s.name}, nil
  81. }
  82. func (s singleFileFS) Open(name string) (fs.File, error) {
  83. if name != s.name {
  84. return nil, errors.New("no such file")
  85. }
  86. return &fakeFile{s.name, s.filesize, 0}, nil
  87. }
  88. func (singleFileFS) Options() []fs.Option {
  89. return nil
  90. }
  91. func (singleFileFS) PlatformData(_ string, _, _ bool, _ fs.XattrFilter) (protocol.PlatformData, error) {
  92. return protocol.PlatformData{}, nil
  93. }
  94. type fakeInfo struct {
  95. name string
  96. size int64
  97. }
  98. func (f fakeInfo) Name() string { return f.name }
  99. func (fakeInfo) Mode() fs.FileMode { return 0o755 }
  100. func (f fakeInfo) Size() int64 { return f.size }
  101. func (fakeInfo) ModTime() time.Time { return time.Unix(1234567890, 0) }
  102. func (f fakeInfo) IsDir() bool {
  103. return strings.Contains(filepath.Base(f.name), "dir") || f.name == "."
  104. }
  105. func (f fakeInfo) IsRegular() bool { return !f.IsDir() }
  106. func (fakeInfo) IsSymlink() bool { return false }
  107. func (fakeInfo) Owner() int { return 0 }
  108. func (fakeInfo) Group() int { return 0 }
  109. func (fakeInfo) Sys() interface{} { return nil }
  110. func (fakeInfo) InodeChangeTime() time.Time { return time.Time{} }
  111. type fakeFile struct {
  112. name string
  113. size int64
  114. readOffset int64
  115. }
  116. func (f *fakeFile) Name() string {
  117. return f.name
  118. }
  119. func (f *fakeFile) Read(bs []byte) (int, error) {
  120. remaining := f.size - f.readOffset
  121. if remaining == 0 {
  122. return 0, io.EOF
  123. }
  124. if remaining < int64(len(bs)) {
  125. f.readOffset = f.size
  126. return int(remaining), nil
  127. }
  128. f.readOffset += int64(len(bs))
  129. return len(bs), nil
  130. }
  131. func (f *fakeFile) Stat() (fs.FileInfo, error) {
  132. return fakeInfo{f.name, f.size}, nil
  133. }
  134. func (*fakeFile) Write([]byte) (int, error) { return 0, errNotSupp }
  135. func (*fakeFile) WriteAt([]byte, int64) (int, error) { return 0, errNotSupp }
  136. func (*fakeFile) Close() error { return nil }
  137. func (*fakeFile) Truncate(_ int64) error { return errNotSupp }
  138. func (*fakeFile) ReadAt([]byte, int64) (int, error) { return 0, errNotSupp }
  139. func (*fakeFile) Seek(int64, int) (int64, error) { return 0, errNotSupp }
  140. func (*fakeFile) Sync() error { return nil }