infinitefs_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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) DirNames(name string) ([]string, error) {
  28. // Returns a list of fake files and directories. Names are such that
  29. // files appear before directories - this makes it so the scanner will
  30. // actually see a few files without having to reach the max depth.
  31. var names []string
  32. for j := 0; j < i.width; j++ {
  33. names = append(names, fmt.Sprintf("aa-file-%d", j))
  34. }
  35. if len(strings.Split(name, string(os.PathSeparator))) < i.depth {
  36. for j := 0; j < i.width; j++ {
  37. names = append(names, fmt.Sprintf("zz-dir-%d", j))
  38. }
  39. }
  40. return names, nil
  41. }
  42. func (i infiniteFS) Open(name string) (fs.File, error) {
  43. return &fakeFile{name, i.filesize, 0}, nil
  44. }
  45. type fakeInfo struct {
  46. name string
  47. size int64
  48. }
  49. func (f fakeInfo) Name() string { return f.name }
  50. func (f fakeInfo) Mode() fs.FileMode { return 0755 }
  51. func (f fakeInfo) Size() int64 { return f.size }
  52. func (f fakeInfo) ModTime() time.Time { return time.Unix(1234567890, 0) }
  53. func (f fakeInfo) IsDir() bool { return strings.Contains(filepath.Base(f.name), "dir") || f.name == "." }
  54. func (f fakeInfo) IsRegular() bool { return !f.IsDir() }
  55. func (f fakeInfo) IsSymlink() bool { return false }
  56. type fakeFile struct {
  57. name string
  58. size int64
  59. readOffset int64
  60. }
  61. func (f *fakeFile) Name() string {
  62. return f.name
  63. }
  64. func (f *fakeFile) Read(bs []byte) (int, error) {
  65. remaining := f.size - f.readOffset
  66. if remaining == 0 {
  67. return 0, io.EOF
  68. }
  69. if remaining < int64(len(bs)) {
  70. f.readOffset = f.size
  71. return int(remaining), nil
  72. }
  73. f.readOffset += int64(len(bs))
  74. return len(bs), nil
  75. }
  76. func (f *fakeFile) Stat() (fs.FileInfo, error) {
  77. return fakeInfo{f.name, f.size}, nil
  78. }
  79. func (f *fakeFile) Write([]byte) (int, error) { return 0, errNotSupp }
  80. func (f *fakeFile) WriteAt([]byte, int64) (int, error) { return 0, errNotSupp }
  81. func (f *fakeFile) Close() error { return nil }
  82. func (f *fakeFile) Truncate(size int64) error { return errNotSupp }
  83. func (f *fakeFile) ReadAt([]byte, int64) (int, error) { return 0, errNotSupp }
  84. func (f *fakeFile) Seek(int64, int) (int64, error) { return 0, errNotSupp }
  85. func (f *fakeFile) Sync() error { return nil }