-
Notifications
You must be signed in to change notification settings - Fork 52
/
memfs.go
60 lines (50 loc) · 1.63 KB
/
memfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package testutil
import (
"io"
"io/fs"
"os"
"path"
"testing/fstest"
"time"
)
// MemFS is copied from fstest.MapFS to support absolute paths
type MemFS map[string]*fstest.MapFile
func (m MemFS) Open(name string) (fs.File, error) {
file, ok := m[name]
if !ok {
return nil, os.ErrNotExist
}
return &openMapFile{name, mapFileInfo{path.Base(name), file}, 0}, nil
}
// An openMapFile is a regular (non-directory) fs.File open for reading.
type openMapFile struct {
path string
mapFileInfo
offset int64
}
func (f *openMapFile) Stat() (fs.FileInfo, error) { return &f.mapFileInfo, nil }
func (f *openMapFile) Close() error { return nil }
func (f *openMapFile) Read(b []byte) (int, error) {
if f.offset >= int64(len(f.f.Data)) {
return 0, io.EOF
}
if f.offset < 0 {
return 0, &fs.PathError{Op: "read", Path: f.path, Err: fs.ErrInvalid}
}
n := copy(b, f.f.Data[f.offset:])
f.offset += int64(n)
return n, nil
}
// A mapFileInfo implements fs.FileInfo and fs.DirEntry for a given map file.
type mapFileInfo struct {
name string
f *fstest.MapFile
}
func (i *mapFileInfo) Name() string { return i.name }
func (i *mapFileInfo) Size() int64 { return int64(len(i.f.Data)) }
func (i *mapFileInfo) Mode() fs.FileMode { return i.f.Mode }
func (i *mapFileInfo) Type() fs.FileMode { return i.f.Mode.Type() }
func (i *mapFileInfo) ModTime() time.Time { return i.f.ModTime }
func (i *mapFileInfo) IsDir() bool { return i.f.Mode&fs.ModeDir != 0 }
func (i *mapFileInfo) Sys() interface{} { return i.f.Sys }
func (i *mapFileInfo) Info() (fs.FileInfo, error) { return i, nil }