-
Notifications
You must be signed in to change notification settings - Fork 1
/
go-fs.go
88 lines (69 loc) · 1.77 KB
/
go-fs.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gofat
import (
"errors"
"io"
"io/fs"
)
type GoDirEntry struct {
fs.FileInfo
}
func (g GoDirEntry) Type() fs.FileMode {
return g.FileInfo.Mode().Type()
}
func (g GoDirEntry) Info() (fs.FileInfo, error) {
// TODO: This may return an error satisfying errors.Is(err, ErrNotExist) if the file does not exist anymore.
return g.FileInfo, nil
}
type GoFile struct {
*File
}
func (g GoFile) Stat() (fs.FileInfo, error) {
return g.File.Stat()
}
func (g GoFile) Read(bytes []byte) (int, error) {
return g.File.Read(bytes)
}
func (g GoFile) Close() error {
return g.File.Close()
}
func (g GoFile) ReadDir(n int) ([]fs.DirEntry, error) {
entries, err := g.File.Readdir(n)
goEntries := make([]fs.DirEntry, len(entries))
for i, e := range entries {
goEntries[i] = GoDirEntry{e}
}
return goEntries, err
}
// GoFs just wraps the afero FAT implementation to be compatible with fs.FS.
type GoFs struct {
Fs
}
// NewGoFS opens a FAT filesystem from the given reader as fs.FS compatible filesystem.
func NewGoFS(reader io.ReadSeeker) (*GoFs, error) {
fs, err := New(reader)
if err != nil {
return nil, err
}
return &GoFs{*fs}, nil
}
// NewGoFSSkipChecks opens a FAT filesystem from the given reader as fs.FS compatible filesystem just like NewGoFs but
// it skips some filesystem validations which may allow you to open not perfectly standard FAT filesystems.
// Use with caution!
func NewGoFSSkipChecks(reader io.ReadSeeker) (*GoFs, error) {
fs, err := NewSkipChecks(reader)
if err != nil {
return nil, err
}
return &GoFs{*fs}, nil
}
func (g GoFs) Open(name string) (fs.File, error) {
file, err := g.Fs.Open(name)
if err != nil {
return nil, err
}
f, ok := file.(*File)
if !ok {
return nil, errors.New("invalid File implementation")
}
return GoFile{f}, nil
}