/
fs.go
320 lines (263 loc) · 7.74 KB
/
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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package webresource
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"time"
)
// fullPathSplit does "/a/b/c" -> "a", "b", "c".
// Absolute paths only.
func fullPathSplit(p string) []string {
var ret []string
p1 := path.Clean("/" + p)
for {
dir, base := path.Split(p1)
p1 = strings.TrimSuffix(dir, "/")
if base == "" {
break
}
ret = append(ret, base)
}
// reverse
for i, j := 0, len(ret)-1; i < j; i, j = i+1, j-1 {
ret[i], ret[j] = ret[j], ret[i]
}
return ret
}
func NewFileSet(name string, requires ...Module) *FileSet {
return &FileSet{
name: name,
requires: requires,
root: &fileEntry{
name: "/",
buf: nil,
mode: 0755 | os.ModeDir,
modTime: time.Now(),
sys: nil,
},
}
}
// FileSet implements Module and makes it easy to add file contents.
type FileSet struct {
root *fileEntry
name string
requires []Module
}
func (fs *FileSet) Name() string { return fs.name }
func (fs *FileSet) Requires() []interface{} {
ret := make([]interface{}, 0, len(fs.requires))
for _, v := range fs.requires {
ret = append(ret, v)
}
return ret
}
func (fs *FileSet) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s", fs.name)
if len(fs.requires) == 0 {
return buf.String()
}
fmt.Fprintf(&buf, " -> (")
for _, r := range fs.requires {
fmt.Fprintf(&buf, "%s, ", r)
}
buf.Truncate(buf.Len() - 2)
fmt.Fprintf(&buf, ")")
return buf.String()
}
// traverse to find the entry for a path, nil if not found
func (fs *FileSet) findEntry(fullPath string) *fileEntry {
parts := fullPathSplit(path.Clean("/" + fullPath))
thisEntry := fs.root
for _, part := range parts {
e2 := thisEntry.children.entryWithName(part)
if e2 == nil {
return nil
}
thisEntry = e2
}
return thisEntry
}
// Mkdir creates a directory and all its parents. For directories that
// already exist this is a nop.
func (fs *FileSet) MkdirAll(fullPath string, mode os.FileMode) *FileSet {
parts := fullPathSplit(fullPath)
e := fs.root
for _, p := range parts {
sube := e.children.entryWithName(p)
if sube == nil { // create dir if not there
sube = newFileEntry(p, nil, mode|os.ModeDir, time.Now(), nil)
e.children = append(e.children, sube)
}
e = sube
}
return fs
}
// Mkdir creates a directory.
// It must not already exists but its parents must, will panic otherwise.
// Readdir() will return files and directories in the sequence they are created.
func (fs *FileSet) Mkdir(fullPath string, mode os.FileMode) *FileSet {
fullPath = path.Clean("/" + fullPath)
dir, base := path.Split(fullPath)
dire := fs.findEntry(dir)
if dire == nil {
panic(fmt.Errorf("no entry found for parent directory %q", dir))
}
if !dire.IsDir() {
panic(fmt.Errorf("%q is not a directory", dir))
}
basee := dire.children.entryWithName(base)
if basee != nil {
panic(fmt.Errorf("entry already exists for %q in directory %q", base, dir))
}
dire.children = append(dire.children,
newFileEntry(base, nil, mode|os.ModeDir, time.Now(), nil))
return fs
}
// WriteFile creates a file.
// It must not already exists but its parents must, will panic otherwise.
// The modTime argument is included because it is useful to indicate the original
// build time of the file.
// Readdir() will return files and directories in the sequence they are created.
func (fs *FileSet) WriteFile(fullPath string, mode os.FileMode, modTime time.Time, contents []byte) *FileSet {
return fs.writeFile(fullPath, mode, modTime, contents, false)
}
// WriteGzipFile works the same as WriteFile except it expects contents to be gzipped and will gunzip them when reading.
// Allows you to reduce the size of the in-memory and on-disk representation of this file.
func (fs *FileSet) WriteGzipFile(fullPath string, mode os.FileMode, modTime time.Time, contents []byte) *FileSet {
return fs.writeFile(fullPath, mode, modTime, contents, true)
}
func (fs *FileSet) writeFile(fullPath string, mode os.FileMode, modTime time.Time, contents []byte, gzipped bool) *FileSet {
fullPath = path.Clean("/" + fullPath)
dir, base := path.Split(fullPath)
dire := fs.findEntry(dir)
if dire == nil {
panic(fmt.Errorf("no entry found for parent directory %q", dir))
}
if !dire.IsDir() {
panic(fmt.Errorf("%q is not a directory", dir))
}
basee := dire.children.entryWithName(base)
if basee != nil {
panic(fmt.Errorf("entry already exists for %q in directory %q", base, dir))
}
e := newFileEntry(base, contents, mode, modTime, nil)
e.gzipped = gzipped
dire.children = append(dire.children, e)
return fs
}
// Open implements http.FileSystem.
func (fs *FileSet) Open(fullPath string) (http.File, error) {
e := fs.findEntry(fullPath)
if e == nil {
return nil, os.ErrNotExist
}
f, err := e.open()
return f, err
}
func newFileEntry(name string, b []byte, mode os.FileMode, modTime time.Time, sys interface{}) *fileEntry {
// sanity check - name should not only be the base name component, no other parth parts
d, n := path.Split(name)
if d != "" || n != name {
panic(fmt.Errorf("invalid name %q", name))
}
return &fileEntry{
name: name,
buf: bytes.NewBuffer(b),
mode: mode,
modTime: modTime,
sys: sys,
}
}
type fileEntry struct {
buf *bytes.Buffer // contents of the file
gzipped bool // true if buf contains gzipped data
name string // name component of the file/dir, will never contain a slash except for root
mode os.FileMode
modTime time.Time
sys interface{}
children fileEntryList // for directories, the child entries
}
func (fe *fileEntry) open() (*file, error) {
var br *bytes.Reader
if fe.buf != nil {
if fe.gzipped {
// we have to do the whole gunzip here because gzip readers are not seekable, so we
// cannot directly honor the http.File contract without reading in full first
gr, err := gzip.NewReader(bytes.NewReader(fe.buf.Bytes()))
if err != nil {
return nil, err
}
defer gr.Close()
b, err := ioutil.ReadAll(gr)
if err != nil {
return nil, err
}
br = bytes.NewReader(b)
} else {
br = bytes.NewReader(fe.buf.Bytes())
}
} else {
br = bytes.NewReader(nil)
}
return &file{
fileEntry: fe,
Reader: br,
children: fe.children,
}, nil
}
// make *fileEntry implement os.FileInfo so it can just return itself from Stat()
func (fe *fileEntry) Name() string { return fe.name }
func (fe *fileEntry) Size() int64 { return int64(fe.buf.Len()) }
func (fe *fileEntry) Mode() os.FileMode { return fe.mode }
func (fe *fileEntry) ModTime() time.Time { return fe.modTime }
func (fe *fileEntry) IsDir() bool { return fe.mode.IsDir() }
func (fe *fileEntry) Sys() interface{} { return fe.sys }
func (fe *fileEntry) Stat() (os.FileInfo, error) {
return fe, nil
}
type fileEntryList []*fileEntry
func (l fileEntryList) entryWithName(name string) *fileEntry {
for _, fe := range l {
if fe.name == name {
return fe
}
}
return nil
}
// file implements http.File using a fileEntry
type file struct {
*fileEntry // implements most of the stuff we need
*bytes.Reader // a reader for our specific opened instance of this fileEntry
children fileEntryList // needed by Readdir()
}
func (f *file) Close() error {
_, err := f.Reader.Seek(0, io.SeekEnd)
return err
}
func (f *file) Readdir(count int) ([]os.FileInfo, error) {
if count <= 0 { // no limit case
count = len(f.children)
}
if count > len(f.children) { // don't overflow
count = len(f.children)
}
// extract what we need and remove from list to set up for next Readdir() call
ch := f.children[:count]
f.children = f.children[count:]
// convert *file->os.FileInfo
ret := make([]os.FileInfo, 0, len(ch))
for _, c := range ch {
ret = append(ret, c)
}
if len(ret) == 0 {
return nil, io.EOF
}
return ret, nil
}