-
Notifications
You must be signed in to change notification settings - Fork 2
/
bindatafs.go
180 lines (148 loc) · 4.1 KB
/
bindatafs.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Package bindatafs provides wrapper vfs.FileSystem implementation to bridge
// go-bindata-generated assets to be served by http.FileServer.
package bindatafs
import (
"bytes"
"os"
"path"
"syscall"
"golang.org/x/tools/godoc/vfs"
)
// FileSystem is a copy of vfs interface FileSystem
type FileSystem interface {
vfs.Opener
Lstat(path string) (os.FileInfo, error)
Stat(path string) (os.FileInfo, error)
ReadDir(path string) ([]os.FileInfo, error)
RootType(string) vfs.RootType
String() string
}
// New returns a FileSystem implementation of the given go-bindata generated assets
func New(name string, Asset AssetFunc, AssetDir AssetDirFunc, AssetInfo AssetInfoFunc) FileSystem {
return &binAssets{
name: name,
Asset: Asset,
AssetDir: AssetDir,
AssetInfo: AssetInfo,
}
}
// AssetFunc is the Assets() function generated by go-bindata
type AssetFunc func(name string) ([]byte, error)
// AssetDirFunc is the AssetDir() function generated by go-bindata
type AssetDirFunc func(name string) ([]string, error)
// AssetInfoFunc is the AssetInfo() function generated by go-bindata
type AssetInfoFunc func(name string) (os.FileInfo, error)
type binAssets struct {
name string
Asset AssetFunc
AssetDir AssetDirFunc
AssetInfo AssetInfoFunc
}
// Open implements vfs.Namespace
func (binAssets *binAssets) Open(pathname string) (file vfs.ReadSeekCloser, err error) {
pathname = binAssets.pathname(pathname)
// if is dir, return a dummy assetDir
if _, err = binAssets.AssetDir(pathname); err == nil {
err = &os.PathError{
Op: "Open",
Path: pathname,
Err: syscall.ENOENT,
}
return
}
// if is a file, return buffered data
var data []byte
if data, err = binAssets.Asset(pathname); err == nil {
file = &FileReader{Reader: bytes.NewReader(data)}
return
}
err = &os.PathError{
Op: "Open",
Path: pathname,
Err: syscall.ENOENT,
}
return
}
func (binAssets *binAssets) Lstat(pathname string) (fi os.FileInfo, err error) {
return binAssets.Stat(pathname)
}
func (binAssets binAssets) pathname(pathname string) string {
if len(pathname) > 0 && pathname[0] == '/' {
return pathname[1:]
}
return pathname
}
// Stat implements vfs.Namespace
func (binAssets *binAssets) Stat(pathname string) (fi os.FileInfo, err error) {
pathname = binAssets.pathname(pathname)
// if is dir, return a dummy assetDir
if _, err = binAssets.AssetDir(pathname); err == nil {
fi = &dirInfo{name: path.Base(pathname)}
return
}
// if is a file, return buffered data
if fi, err = binAssets.AssetInfo(pathname); err == nil {
fi = &fileInfo{name: path.Base(pathname), FileInfo: fi}
return
}
// return standard not found signal
err = &os.PathError{
Op: "Stat",
Path: pathname,
Err: syscall.ENOENT,
}
return
}
// ReadDir implements vfs.Namespace
func (binAssets *binAssets) ReadDir(pathname string) (fiList []os.FileInfo, err error) {
pathname = binAssets.pathname(pathname)
// if is a file, return error
if _, err = binAssets.AssetInfo(pathname); err == nil {
err = &os.PathError{
Op: "ReadDir",
Path: pathname,
Err: syscall.ENOENT,
}
return
}
// if is dir, return a dummy assetDir
var names []string
if names, err = binAssets.AssetDir(pathname); err != nil {
err = &os.PathError{
Op: "ReadDir",
Path: pathname,
Err: syscall.ENOENT,
}
return
}
// read all names entity to file info
fiList = make([]os.FileInfo, len(names))
for i, name := range names {
fiList[i], err = binAssets.Stat(path.Join(pathname, name))
}
return
}
// RootType implements vfs.Namespace
func (binAssets *binAssets) RootType(string) vfs.RootType {
return ""
}
// String implements vfs.FileSystem
func (binAssets *binAssets) String() string {
return binAssets.name
}
// FileReader implements vfs.ReadSeekCloser
type FileReader struct {
*bytes.Reader
}
// Read implements io.Reader
func (r *FileReader) Read(p []byte) (int, error) {
return r.Reader.Read(p)
}
// Seek implements io.Seeker
func (r *FileReader) Seek(offset int64, whence int) (int64, error) {
return r.Reader.Seek(offset, whence)
}
// Close implements io.Closer
func (r *FileReader) Close() error {
return nil
}