-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfs.go
More file actions
225 lines (194 loc) · 6.1 KB
/
Copy pathfs.go
File metadata and controls
225 lines (194 loc) · 6.1 KB
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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/vfs source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package vfs
import (
"errors"
"os"
"path"
"path/filepath"
"time"
)
var _ FileSystem = (*VFS)(nil)
// VFS errors
var (
ErrMountExists = errors.New("vfs: mount already exists")
ErrMountNotExists = errors.New("vfs: mount does not exist")
ErrNotAbsolutPath = errors.New("vfs: not a absolute path")
)
// VFS represents Virtual FileSystem (VFS), it operates in-memory.
// if file/directory doesn't exists on in-memory then it tries physical filesystem.
//
// VFS implements `vfs.FileSystem`, its a combination of package `os` and `ioutil`
// focused on Read-Only operations.
//
// Single point of access for all mounted virtual directories in aah application.
type VFS struct {
embeddedMode bool
mounts map[string]*Mount
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// VFS FileSystem interface methods
//______________________________________________________________________________
// Open method behaviour is same as `os.Open`.
func (v *VFS) Open(name string) (File, error) {
m, err := v.FindMount(name)
if err != nil {
return nil, err
}
return m.Open(m.toVirtualPath(name))
}
// Lstat method behaviour is same as `os.Lstat`.
func (v *VFS) Lstat(name string) (os.FileInfo, error) {
m, err := v.FindMount(name)
if err != nil {
return nil, err
}
return m.Lstat(m.toVirtualPath(name))
}
// Stat method behaviour is same as `os.Stat`
func (v *VFS) Stat(name string) (os.FileInfo, error) {
m, err := v.FindMount(name)
if err != nil {
return nil, err
}
return m.Stat(m.toVirtualPath(name))
}
// ReadFile method behaviour is same as `ioutil.ReadFile`.
func (v *VFS) ReadFile(filename string) ([]byte, error) {
m, err := v.FindMount(filename)
if err != nil {
return nil, err
}
return m.ReadFile(m.toVirtualPath(filename))
}
// ReadDir method behaviour is same as `ioutil.ReadDir`.
func (v *VFS) ReadDir(dirname string) ([]os.FileInfo, error) {
m, err := v.FindMount(dirname)
if err != nil {
return nil, err
}
return m.ReadDir(m.toVirtualPath(dirname))
}
// Glob method somewhat similar to `filepath.Glob`, since aah vfs does pattern
// match only on `filepath.Base` value.
func (v *VFS) Glob(pattern string) ([]string, error) {
m, err := v.FindMount(pattern)
if err != nil {
return nil, err
}
return m.Glob(m.toVirtualPath(pattern))
}
// IsExists method is helper to find existence.
func (v *VFS) IsExists(name string) bool {
_, err := v.Lstat(name)
return err == nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// VFS methods
//______________________________________________________________________________
// IsEmbeddedMode method returns true if its a single binary otherwise false.
func (v *VFS) IsEmbeddedMode() bool {
return v.embeddedMode
}
// SetEmbeddedMode method set the VFS into Embedded Mode. It means single binary.
func (v *VFS) SetEmbeddedMode() {
v.embeddedMode = true
}
// Walk method behaviour is same as `filepath.Walk`.
func (v *VFS) Walk(root string, walkFn filepath.WalkFunc) error {
m, err := v.FindMount(root)
if err != nil {
return err
}
if m.isTreeEmpty() {
// virtual is empty, move on with physical filesystem
// Proot := filepath.Join(m.Proot, strings.TrimPrefix(root, m.Vroot))
return filepath.Walk(m.toPhysicalPath(root),
func(fpath string, fi os.FileInfo, err error) error {
return walkFn(m.toVirtualPath(fpath), fi, err)
})
}
info, err := m.Lstat(root)
if err == nil {
err = walk(m, root, info, walkFn)
} else {
err = walkFn(root, nil, err)
}
if err == filepath.SkipDir {
return nil
}
return err
}
// Dirs method returns directories path recursively for given root path.
func (v *VFS) Dirs(root string) ([]string, error) {
var dirs []string
err := v.Walk(root, func(fpath string, fi os.FileInfo, err error) error {
if fi.IsDir() {
dirs = append(dirs, fpath)
}
return nil
})
return dirs, err
}
// Files method returns directories path recursively for given root path.
func (v *VFS) Files(root string) ([]string, error) {
var files []string
err := v.Walk(root, func(fpath string, fi os.FileInfo, err error) error {
if !fi.IsDir() {
files = append(files, fpath)
}
return nil
})
return files, err
}
// FindMount method finds the mounted virtual directory by mount path.
// if found then returns `Mount` instance otherwise nil and error.
//
// Mount implements `vfs.FileSystem`, its a combination of package `os` and `ioutil`
// focused on Read-Only operations.
func (v *VFS) FindMount(name string) (*Mount, error) {
name = path.Clean(name)
for _, m := range v.mounts {
if m.match(name) {
return m, nil
}
}
return nil, &os.PathError{Op: "read", Path: name, Err: ErrMountNotExists}
}
// AddMount method used to mount physical directory as a virtual mounted directory.
//
// Basically aah scans and application source files and builds each file from
// mounted source directory into binary for single binary build.
func (v *VFS) AddMount(mountPath, physicalPath string) error {
pp := filepath.Clean(physicalPath)
if !filepath.IsAbs(physicalPath) {
return ErrNotAbsolutPath
}
if !v.embeddedMode {
fi, err := os.Lstat(pp)
if err != nil {
return err
}
if !fi.IsDir() {
return &os.PathError{Op: "addmount", Path: pp, Err: errors.New("is a file")}
}
}
mp := filepath.ToSlash(path.Clean(mountPath))
if mp == "" {
mp = path.Base(pp)
}
mp = path.Clean("/" + mp)
if v.mounts == nil {
v.mounts = make(map[string]*Mount)
}
if _, found := v.mounts[mp]; found {
return &os.PathError{Op: "addmount", Path: mp, Err: ErrMountExists}
}
v.mounts[mp] = &Mount{
Vroot: mp,
Proot: pp,
tree: newNode(mp, &NodeInfo{Dir: true, Time: time.Now().UTC()}),
}
return nil
}