2014-11-08 08:21:39 +08:00
|
|
|
package sysfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-03-14 07:11:06 +08:00
|
|
|
"io/ioutil"
|
2014-11-08 08:21:39 +08:00
|
|
|
"os"
|
2017-03-14 07:11:06 +08:00
|
|
|
"strings"
|
2014-11-08 08:21:39 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
var _ File = (*MockFile)(nil)
|
|
|
|
var _ Filesystem = (*MockFilesystem)(nil)
|
|
|
|
|
|
|
|
// MockFilesystem represents a filesystem of mock files.
|
2014-11-08 08:21:39 +08:00
|
|
|
type MockFilesystem struct {
|
2017-04-27 14:41:34 +08:00
|
|
|
Seq int // Increases with each write or read.
|
|
|
|
Files map[string]*MockFile
|
|
|
|
WithReadError bool
|
|
|
|
WithWriteError bool
|
2014-11-08 08:21:39 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// A MockFile represents a mock file that contains a single string. Any write
|
2014-11-08 08:21:39 +08:00
|
|
|
// overwrites, and any read returns from the start.
|
|
|
|
type MockFile struct {
|
|
|
|
Contents string
|
|
|
|
Seq int // When this file was last written or read.
|
|
|
|
Opened bool
|
|
|
|
Closed bool
|
|
|
|
fd uintptr
|
|
|
|
|
|
|
|
fs *MockFilesystem
|
|
|
|
}
|
|
|
|
|
2017-04-27 14:41:34 +08:00
|
|
|
var (
|
|
|
|
readErr = errors.New("read error")
|
|
|
|
writeErr = errors.New("write error")
|
|
|
|
)
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Write writes string(b) to f.Contents
|
2014-11-08 08:21:39 +08:00
|
|
|
func (f *MockFile) Write(b []byte) (n int, err error) {
|
2017-04-27 14:41:34 +08:00
|
|
|
if f.fs.WithWriteError {
|
|
|
|
return 0, writeErr
|
|
|
|
}
|
2014-11-08 08:21:39 +08:00
|
|
|
return f.WriteString(string(b))
|
|
|
|
}
|
|
|
|
|
2017-01-03 02:32:04 +08:00
|
|
|
// Seek seeks to a specific offset in a file
|
2016-02-19 04:08:48 +08:00
|
|
|
func (f *MockFile) Seek(offset int64, whence int) (ret int64, err error) {
|
|
|
|
return offset, nil
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// WriteString writes s to f.Contents
|
2014-11-08 08:21:39 +08:00
|
|
|
func (f *MockFile) WriteString(s string) (ret int, err error) {
|
|
|
|
f.Contents = s
|
|
|
|
f.Seq = f.fs.next()
|
|
|
|
return len(s), nil
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Sync implements the File interface Sync function
|
2014-11-08 08:21:39 +08:00
|
|
|
func (f *MockFile) Sync() (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Read copies b bytes from f.Contents
|
2014-11-08 08:21:39 +08:00
|
|
|
func (f *MockFile) Read(b []byte) (n int, err error) {
|
2017-04-27 14:41:34 +08:00
|
|
|
if f.fs.WithReadError {
|
|
|
|
return 0, readErr
|
|
|
|
}
|
|
|
|
|
2014-11-08 08:21:39 +08:00
|
|
|
count := len(b)
|
|
|
|
if len(f.Contents) < count {
|
|
|
|
count = len(f.Contents)
|
|
|
|
}
|
|
|
|
copy(b, []byte(f.Contents)[:count])
|
|
|
|
f.Seq = f.fs.next()
|
|
|
|
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// ReadAt calls MockFile.Read
|
2014-11-08 08:21:39 +08:00
|
|
|
func (f *MockFile) ReadAt(b []byte, off int64) (n int, err error) {
|
|
|
|
return f.Read(b)
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Fd returns a random uintprt based on the time of the MockFile creation
|
2014-11-08 08:21:39 +08:00
|
|
|
func (f *MockFile) Fd() uintptr {
|
|
|
|
return f.fd
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Close implements the File interface Close function
|
2014-11-08 08:21:39 +08:00
|
|
|
func (f *MockFile) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// NewMockFilesystem returns a new MockFilesystem given a list of file paths
|
2014-11-08 08:21:39 +08:00
|
|
|
func NewMockFilesystem(files []string) *MockFilesystem {
|
|
|
|
m := &MockFilesystem{
|
|
|
|
Files: make(map[string]*MockFile),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range files {
|
|
|
|
m.Add(files[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// OpenFile opens file name from fs.Files, if the file does not exist it returns an os.PathError
|
2014-11-08 08:21:39 +08:00
|
|
|
func (fs *MockFilesystem) OpenFile(name string, flag int, perm os.FileMode) (file File, err error) {
|
|
|
|
f, ok := fs.Files[name]
|
|
|
|
if ok {
|
|
|
|
f.Opened = true
|
|
|
|
f.Closed = false
|
|
|
|
return f, nil
|
|
|
|
}
|
2014-12-31 22:12:25 +08:00
|
|
|
return (*MockFile)(nil), &os.PathError{Err: errors.New(name + ": No such file.")}
|
2014-11-08 08:21:39 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 07:11:06 +08:00
|
|
|
// Stat returns a generic FileInfo for all files in fs.Files.
|
|
|
|
// If the file does not exist it returns an os.PathError
|
|
|
|
func (fs *MockFilesystem) Stat(name string) (os.FileInfo, error) {
|
|
|
|
_, ok := fs.Files[name]
|
|
|
|
if ok {
|
|
|
|
// return file based mock FileInfo
|
|
|
|
tmpFile, err := ioutil.TempFile("", name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
|
|
|
|
return os.Stat(tmpFile.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
dirName := name + "/"
|
|
|
|
for path := range fs.Files {
|
|
|
|
if strings.HasPrefix(path, dirName) {
|
|
|
|
// return dir based mock FileInfo
|
|
|
|
tmpDir, err := ioutil.TempDir("", name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
return os.Stat(tmpDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &os.PathError{Err: errors.New(name + ": No such file.")}
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Add adds a new file to fs.Files given a name, and returns the newly created file
|
2014-11-08 08:21:39 +08:00
|
|
|
func (fs *MockFilesystem) Add(name string) *MockFile {
|
|
|
|
f := &MockFile{
|
|
|
|
Seq: -1,
|
|
|
|
fd: uintptr(time.Now().UnixNano() & 0xffff),
|
|
|
|
fs: fs,
|
|
|
|
}
|
|
|
|
fs.Files[name] = f
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *MockFilesystem) next() int {
|
|
|
|
fs.Seq++
|
|
|
|
return fs.Seq
|
|
|
|
}
|