-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmount.go
More file actions
233 lines (194 loc) · 5.91 KB
/
Copy pathmount.go
File metadata and controls
233 lines (194 loc) · 5.91 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
226
227
228
229
230
231
232
233
// 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"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strings"
)
var _ FileSystem = (*Mount)(nil)
// Mount struct represents mount of single physical directory into virtual directory.
//
// Mount implements `vfs.FileSystem`, its a combination of package `os` and `ioutil`
// focused on Read-Only operations.
type Mount struct {
Vroot string
Proot string
tree *node
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Mount's FileSystem interface
//______________________________________________________________________________
// Open method behaviour is same as `os.Open`.
func (m Mount) Open(name string) (File, error) {
f, err := m.open(name)
if os.IsNotExist(err) {
return m.openPhysical(name)
}
return f, err
}
// Lstat method behaviour is same as `os.Lstat`.
func (m Mount) Lstat(name string) (os.FileInfo, error) {
f, err := m.open(name)
if os.IsNotExist(err) {
return os.Lstat(m.toPhysicalPath(name))
}
return f, err
}
// Stat method behaviour is same as `os.Stat`
func (m Mount) Stat(name string) (os.FileInfo, error) {
f, err := m.open(name)
if os.IsNotExist(err) {
return os.Stat(m.toPhysicalPath(name))
}
return f, err
}
// ReadFile method behaviour is same as `ioutil.ReadFile`.
func (m Mount) ReadFile(name string) ([]byte, error) {
f, err := m.Open(name)
if os.IsNotExist(err) {
f, err = m.openPhysical(name)
}
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
if fi.IsDir() {
return nil, &os.PathError{Op: "read", Path: name, Err: errors.New("is a directory")}
}
return ioutil.ReadAll(f)
}
// ReadDir method behaviour is same as `ioutil.ReadDir`.
func (m Mount) ReadDir(dirname string) ([]os.FileInfo, error) {
f, err := m.open(dirname)
if os.IsNotExist(err) {
return ioutil.ReadDir(m.toPhysicalPath(dirname))
}
if !f.IsDir() {
return nil, &os.PathError{Op: "read", Path: dirname, Err: errors.New("is a file")}
}
list := append([]os.FileInfo{}, f.node.childInfos...)
sort.Sort(byName(list))
return list, nil
}
// Glob method somewhat similar to `filepath.Glob`, since aah vfs does pattern
// match only on `filepath.Base` value.
func (m Mount) Glob(pattern string) ([]string, error) {
var matches []string
f, err := m.open(path.Dir(pattern))
if os.IsNotExist(err) {
flist, err := filepath.Glob(m.toPhysicalPath(pattern))
if err != nil {
return nil, err
}
for _, p := range flist {
matches = append(matches, m.toVirtualPath(p))
}
return matches, nil
}
base := path.Base(pattern)
for _, c := range f.childs {
match, err := filepath.Match(base, c.Name())
if err != nil {
return nil, err
}
if match {
matches = append(matches, c.Path)
}
}
return matches, nil
}
// IsExists method is helper to find existence.
func (m Mount) IsExists(name string) bool {
_, err := m.Lstat(name)
return err == nil
}
// String method Stringer interface.
func (m Mount) String() string {
return fmt.Sprintf("mount(%s => %s)", m.Vroot, m.Proot)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Mount adding file and directory
//______________________________________________________________________________
// Name method returns mounted path.
func (m *Mount) Name() string {
return m.Vroot
}
// AddDir method is to add directory node into VFS from mounted source directory.
func (m *Mount) AddDir(fi os.FileInfo) error {
return m.addNode(fi, nil)
}
// AddFile method is to add file node into VFS from mounted source directory.
func (m *Mount) AddFile(fi os.FileInfo, data []byte) error {
return m.addNode(fi, data)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Mount unexported methods
//______________________________________________________________________________
func (m Mount) cleanDir(p string) string {
dp := strings.TrimPrefix(p, m.Vroot)
return path.Dir(dp)
}
func (m Mount) open(name string) (*file, error) {
if m.isTreeEmpty() {
return nil, os.ErrNotExist
}
if m.Vroot == name { // extact match, root dir
return newFile(m.tree), nil
}
return m.tree.find(strings.TrimPrefix(name, m.Vroot))
}
func (m Mount) openPhysical(name string) (File, error) {
pname := m.toPhysicalPath(name)
if _, err := os.Lstat(pname); os.IsNotExist(err) {
return nil, err
}
return os.Open(pname)
}
func (m Mount) toPhysicalPath(name string) string {
if strings.HasPrefix(name, m.Proot) {
return name
}
return filepath.Clean(filepath.FromSlash(
filepath.Join(m.Proot, strings.TrimPrefix(name, m.Vroot))))
}
func (m *Mount) toVirtualPath(name string) string {
if strings.HasPrefix(name, m.Vroot) {
return name
}
return path.Clean(filepath.ToSlash(
filepath.Join(m.Vroot, strings.TrimPrefix(name, m.Proot))))
}
func (m *Mount) addNode(fi os.FileInfo, data []byte) error {
mountPath := fi.(*NodeInfo).Path
t, err := m.tree.findNode(m.cleanDir(mountPath))
switch {
case err != nil:
return err
case t == nil:
return nil
}
n := newNode(mountPath, fi)
if data != nil {
n.data = data
}
t.addChild(n)
return nil
}
func (m *Mount) match(name string) bool {
return m.Vroot == name ||
strings.HasPrefix(name, m.tree.Path+"/") ||
strings.HasPrefix(name, m.Proot)
}
func (m *Mount) isTreeEmpty() bool {
return m.tree == nil || len(m.tree.childs) == 0
}