This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmanager.go
373 lines (296 loc) · 7.8 KB
/
manager.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package parcello
import (
"archive/zip"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
zipexe "github.com/daaku/go.zipexe"
"github.com/kardianos/osext"
)
var (
// ErrReadOnly is returned if the file is read-only and write operations are disabled.
ErrReadOnly = errors.New("File is read-only")
// ErrWriteOnly is returned if the file is write-only and read operations are disabled.
ErrWriteOnly = errors.New("File is write-only")
// ErrIsDirectory is returned if the file under operation is not a regular file but a directory.
ErrIsDirectory = errors.New("Is directory")
)
var (
// Manager keeps track of all resources
Manager = DefaultManager(osext.Executable)
// Make sure the ResourceManager implements the FileSystemManager interface
_ FileSystemManager = &ResourceManager{}
)
// Open opens an embedded resource for read
func Open(name string) (File, error) {
return Manager.OpenFile(name, os.O_RDONLY, 0)
}
// ManagerAt returns manager at given path
func ManagerAt(path string) FileSystemManager {
mngr, err := Manager.Dir(path)
if err != nil {
panic(err)
}
return mngr
}
// AddResource adds resource to the default resource manager
// Note that the method may panic if the resource not exists
func AddResource(resource []byte) {
if err := Manager.Add(BinaryResource(resource)); err != nil {
panic(err)
}
}
// ResourceManagerConfig represents the configuration for Resource Manager
type ResourceManagerConfig struct {
// Path to the archive
Path string
// FileSystem that stores the archive
FileSystem FileSystem
}
// ResourceManager represents a virtual in memory file system
type ResourceManager struct {
cfg *ResourceManagerConfig
rw sync.RWMutex
root *Node
// NewReader creates a new ZIP Reader
NewReader func(io.ReaderAt, int64) (*zip.Reader, error)
}
// DefaultManager creates a FileSystemManager based on whether dev mode is enabled
func DefaultManager(executable ExecutableFunc) FileSystemManager {
mode := os.Getenv("PARCELLO_DEV_ENABLED")
if mode != "" {
return Dir(getenv("PARCELLO_RESOURCE_DIR", "."))
}
path, err := executable()
if err != nil {
panic(err)
}
dir, path := filepath.Split(path)
cfg := &ResourceManagerConfig{
Path: path,
FileSystem: Dir(dir),
}
manager, err := NewResourceManager(cfg)
if err != nil {
panic(err)
}
return manager
}
// NewResourceManager creates a new manager
func NewResourceManager(cfg *ResourceManagerConfig) (*ResourceManager, error) {
manager := &ResourceManager{cfg: cfg}
file, err := cfg.FileSystem.OpenFile(cfg.Path, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return nil, err
}
resource := &Resource{
Body: file,
Size: info.Size(),
}
_ = manager.Add(resource)
return manager, nil
}
// Add adds resource to the manager
func (m *ResourceManager) Add(resource *Resource) error {
m.rw.Lock()
defer m.rw.Unlock()
if m.root == nil {
m.root = &Node{Name: "/", IsDir: true}
}
newReader := zipexe.NewReader
if m.NewReader != nil {
newReader = m.NewReader
}
reader, err := newReader(resource.Body, resource.Size)
if err != nil {
return err
}
return m.uncompress(reader)
}
func (m *ResourceManager) uncompress(reader *zip.Reader) error {
for _, header := range reader.File {
path := split(header.Name)
node := add(path, m.root)
if node == m.root || node == nil {
return fmt.Errorf("invalid path: '%s'", header.Name)
}
file, err := header.Open()
if err != nil {
return err
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
return err
}
node.IsDir = false
node.Content = &content
}
return nil
}
// Dir returns a sub-manager for given path
func (m *ResourceManager) Dir(name string) (FileSystemManager, error) {
if _, node := find(split(name), nil, m.root); node != nil {
if node.IsDir {
return &ResourceManager{root: node}, nil
}
}
return nil, os.ErrNotExist
}
// Open opens an embedded resource for read
func (m *ResourceManager) Open(name string) (ReadOnlyFile, error) {
return m.OpenFile(name, os.O_RDONLY, 0)
}
// OpenFile is the generalized open call; most users will use Open
func (m *ResourceManager) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
parent, node, err := m.open(name)
if err != nil {
return nil, err
}
if isWritable(flag) && node != nil && node.IsDir {
return nil, &os.PathError{Op: "open", Path: name, Err: ErrIsDirectory}
}
if hasFlag(os.O_CREATE, flag) {
if node != nil && !hasFlag(os.O_TRUNC, flag) {
return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrExist}
}
node = newNode(filepath.Base(name), parent)
}
if node == nil {
return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
}
return newFile(node, flag)
}
func (m *ResourceManager) open(name string) (*Node, *Node, error) {
parent, node := find(split(name), nil, m.root)
if node != m.root && parent == nil {
return nil, nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
}
return parent, node, nil
}
// Walk walks the file tree rooted at root, calling walkFn for each file or
// directory in the tree, including root.
func (m *ResourceManager) Walk(dir string, fn filepath.WalkFunc) error {
if _, node := find(split(dir), nil, m.root); node != nil {
return walk(dir, node, fn)
}
return os.ErrNotExist
}
func add(path []string, node *Node) *Node {
if !node.IsDir || node.Content != nil {
return nil
}
if len(path) == 0 {
return node
}
name := path[0]
for _, child := range node.Children {
if child.Name == name {
return add(path[1:], child)
}
}
child := &Node{
Mutex: &sync.RWMutex{},
Name: name,
IsDir: true,
ModTime: time.Now(),
}
node.Children = append(node.Children, child)
return add(path[1:], child)
}
func split(path string) []string {
parts := []string{}
for _, part := range strings.Split(path, string(os.PathSeparator)) {
if part != "" && part != "/" {
parts = append(parts, part)
}
}
return parts
}
func find(path []string, parent, node *Node) (*Node, *Node) {
if len(path) == 0 || node == nil {
return parent, node
}
for _, child := range node.Children {
if path[0] == child.Name {
if len(path) == 1 {
return node, child
}
return find(path[1:], node, child)
}
}
return parent, nil
}
func walk(path string, node *Node, fn filepath.WalkFunc) error {
if err := fn(path, &ResourceFileInfo{Node: node}, nil); err != nil {
return err
}
for _, child := range node.Children {
if err := walk(filepath.Join(path, child.Name), child, fn); err != nil {
return err
}
}
return nil
}
func newNode(name string, parent *Node) *Node {
node := &Node{
Name: name,
IsDir: false,
ModTime: time.Now(),
}
parent.Children = append(parent.Children, node)
return node
}
func newFile(node *Node, flag int) (File, error) {
if isWritable(flag) {
node.ModTime = time.Now()
}
if node.Content == nil || hasFlag(os.O_TRUNC, flag) {
buf := make([]byte, 0)
node.Content = &buf
node.Mutex = &sync.RWMutex{}
}
f := NewResourceFile(node)
if hasFlag(os.O_APPEND, flag) {
_, _ = f.Seek(0, io.SeekEnd)
}
if hasFlag(os.O_RDWR, flag) {
return f, nil
}
if hasFlag(os.O_WRONLY, flag) {
return &woFile{f}, nil
}
return &roFile{f}, nil
}
func hasFlag(flag int, flags int) bool {
return flags&flag == flag
}
func isWritable(flag int) bool {
return hasFlag(os.O_WRONLY, flag) || hasFlag(os.O_RDWR, flag) || hasFlag(os.O_APPEND, flag)
}
type roFile struct {
*ResourceFile
}
// Write is disabled and returns ErrorReadOnly
func (f *roFile) Write(p []byte) (n int, err error) {
return 0, ErrReadOnly
}
// woFile wraps the given file and disables Read(..) operation.
type woFile struct {
*ResourceFile
}
// Read is disabled and returns ErrorWroteOnly
func (f *woFile) Read(p []byte) (n int, err error) {
return 0, ErrWriteOnly
}