infinitefs_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 fakeInfo struct {
  49. name string
  50. size int64
  51. }
  52. func (f fakeInfo) Name() string { return f.name }
  53. func (f fakeInfo) Mode() fs.FileMode { return 0755 }
  54. func (f fakeInfo) Size() int64 { return f.size }
  55. func (f fakeInfo) ModTime() time.Time { return time.Unix(1234567890, 0) }
  56. func (f fakeInfo) IsDir() bool { return strings.Contains(filepath.Base(f.name), "dir") || f.name == "." }
  57. func (f fakeInfo) IsRegular() bool { return !f.IsDir() }
  58. func (f fakeInfo) IsSymlink() bool { return false }
  59. type fakeFile struct {
  60. name string
  61. size int64
  62. readOffset int64
  63. }
  64. func (f *fakeFile) Name() string {
  65. return f.name
  66. }
  67. func (f *fakeFile) Read(bs []byte) (int, error) {
  68. remaining := f.size - f.readOffset
  69. if remaining == 0 {
  70. return 0, io.EOF
  71. }
  72. if remaining < int64(len(bs)) {
  73. f.readOffset = f.size
  74. return int(remaining), nil
  75. }
  76. f.readOffset += int64(len(bs))
  77. return len(bs), nil
  78. }
  79. func (f *fakeFile) Stat() (fs.FileInfo, error) {
  80. return fakeInfo{f.name, f.size}, nil
  81. }
  82. func (f *fakeFile) Write([]byte) (int, error) { return 0, errNotSupp }
  83. func (f *fakeFile) WriteAt([]byte, int64) (int, error) { return 0, errNotSupp }
  84. func (f *fakeFile) Close() error { return nil }
  85. func (f *fakeFile) Truncate(size int64) error { return errNotSupp }
  86. func (f *fakeFile) ReadAt([]byte, int64) (int, error) { return 0, errNotSupp }
  87. func (f *fakeFile) Seek(int64, int) (int64, error) { return 0, errNotSupp }
  88. func (f *fakeFile) Sync() error { return nil }