2017-04-26 00:15:23 +00:00
|
|
|
// Copyright (C) 2017 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type infiniteFS struct {
|
2017-08-19 14:36:56 +00:00
|
|
|
fs.Filesystem
|
2017-04-26 00:15:23 +00:00
|
|
|
width int // number of files and directories per level
|
|
|
|
depth int // number of tree levels to simulate
|
|
|
|
filesize int64 // size of each file in bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
var errNotSupp = errors.New("not supported")
|
|
|
|
|
|
|
|
func (i infiniteFS) Lstat(name string) (fs.FileInfo, error) {
|
|
|
|
return fakeInfo{name, i.filesize}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-24 00:05:47 +00:00
|
|
|
func (i infiniteFS) Stat(name string) (fs.FileInfo, error) {
|
|
|
|
return fakeInfo{name, i.filesize}, nil
|
|
|
|
}
|
|
|
|
|
2017-04-26 00:15:23 +00:00
|
|
|
func (i infiniteFS) DirNames(name string) ([]string, error) {
|
|
|
|
// Returns a list of fake files and directories. Names are such that
|
|
|
|
// files appear before directories - this makes it so the scanner will
|
|
|
|
// actually see a few files without having to reach the max depth.
|
|
|
|
var names []string
|
|
|
|
for j := 0; j < i.width; j++ {
|
|
|
|
names = append(names, fmt.Sprintf("aa-file-%d", j))
|
|
|
|
}
|
|
|
|
if len(strings.Split(name, string(os.PathSeparator))) < i.depth {
|
|
|
|
for j := 0; j < i.width; j++ {
|
|
|
|
names = append(names, fmt.Sprintf("zz-dir-%d", j))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i infiniteFS) Open(name string) (fs.File, error) {
|
|
|
|
return &fakeFile{name, i.filesize, 0}, nil
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:08:50 +00:00
|
|
|
type singleFileFS struct {
|
|
|
|
fs.Filesystem
|
|
|
|
name string
|
|
|
|
filesize int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s singleFileFS) Lstat(name string) (fs.FileInfo, error) {
|
|
|
|
switch name {
|
|
|
|
case ".":
|
|
|
|
return fakeInfo{".", 0}, nil
|
|
|
|
case s.name:
|
|
|
|
return fakeInfo{s.name, s.filesize}, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("no such file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s singleFileFS) Stat(name string) (fs.FileInfo, error) {
|
|
|
|
switch name {
|
|
|
|
case ".":
|
|
|
|
return fakeInfo{".", 0}, nil
|
|
|
|
case s.name:
|
|
|
|
return fakeInfo{s.name, s.filesize}, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("no such file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s singleFileFS) DirNames(name string) ([]string, error) {
|
|
|
|
if name != "." {
|
|
|
|
return nil, errors.New("no such file")
|
|
|
|
}
|
|
|
|
return []string{s.name}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s singleFileFS) Open(name string) (fs.File, error) {
|
|
|
|
if name != s.name {
|
|
|
|
return nil, errors.New("no such file")
|
|
|
|
}
|
|
|
|
return &fakeFile{s.name, s.filesize, 0}, nil
|
|
|
|
}
|
|
|
|
|
2017-04-26 00:15:23 +00:00
|
|
|
type fakeInfo struct {
|
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fakeInfo) Name() string { return f.name }
|
|
|
|
func (f fakeInfo) Mode() fs.FileMode { return 0755 }
|
|
|
|
func (f fakeInfo) Size() int64 { return f.size }
|
|
|
|
func (f fakeInfo) ModTime() time.Time { return time.Unix(1234567890, 0) }
|
2020-04-07 07:31:29 +00:00
|
|
|
func (f fakeInfo) IsDir() bool {
|
|
|
|
return strings.Contains(filepath.Base(f.name), "dir") || f.name == "."
|
|
|
|
}
|
|
|
|
func (f fakeInfo) IsRegular() bool { return !f.IsDir() }
|
|
|
|
func (f fakeInfo) IsSymlink() bool { return false }
|
|
|
|
func (f fakeInfo) Owner() int { return 0 }
|
|
|
|
func (f fakeInfo) Group() int { return 0 }
|
2017-04-26 00:15:23 +00:00
|
|
|
|
|
|
|
type fakeFile struct {
|
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
readOffset int64
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
func (f *fakeFile) Name() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2017-04-26 00:15:23 +00:00
|
|
|
func (f *fakeFile) Read(bs []byte) (int, error) {
|
|
|
|
remaining := f.size - f.readOffset
|
|
|
|
if remaining == 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
if remaining < int64(len(bs)) {
|
|
|
|
f.readOffset = f.size
|
|
|
|
return int(remaining), nil
|
|
|
|
}
|
|
|
|
f.readOffset += int64(len(bs))
|
|
|
|
return len(bs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeFile) Stat() (fs.FileInfo, error) {
|
|
|
|
return fakeInfo{f.name, f.size}, nil
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
func (f *fakeFile) Write([]byte) (int, error) { return 0, errNotSupp }
|
|
|
|
func (f *fakeFile) WriteAt([]byte, int64) (int, error) { return 0, errNotSupp }
|
|
|
|
func (f *fakeFile) Close() error { return nil }
|
|
|
|
func (f *fakeFile) Truncate(size int64) error { return errNotSupp }
|
|
|
|
func (f *fakeFile) ReadAt([]byte, int64) (int, error) { return 0, errNotSupp }
|
|
|
|
func (f *fakeFile) Seek(int64, int) (int64, error) { return 0, errNotSupp }
|
|
|
|
func (f *fakeFile) Sync() error { return nil }
|