-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
209 lines (183 loc) · 5.58 KB
/
util.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
// 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 (
"bytes"
"compress/gzip"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package methods for proxy to OS calls.
//______________________________________________________________________________
// Open method calls `os.Open` if fs == nil otherwise VFS.
//
// NOTE: Use VFS instance directly `aah.AppVFS().*`. This is created to prevent
// repetition code in consumimg libraries of aah.
func Open(fs *VFS, name string) (File, error) {
if fs == nil {
return os.Open(name)
}
return fs.Open(name)
}
// Lstat method calls `os.Lstat` if fs == nil otherwise VFS.
//
// NOTE: Use VFS instance directly `aah.AppVFS().*`. This is created to prevent
// repetition code in consumimg libraries of aah.
func Lstat(fs *VFS, name string) (os.FileInfo, error) {
if fs == nil {
return os.Lstat(name)
}
return fs.Lstat(name)
}
// Stat method calls `os.Stat` if fs == nil otherwise VFS.
//
// NOTE: Use VFS instance directly `aah.AppVFS().*`. This is created to prevent
// repetition code in consumimg libraries of aah.
func Stat(fs *VFS, name string) (os.FileInfo, error) {
if fs == nil {
return os.Stat(name)
}
return fs.Stat(name)
}
// ReadFile method calls `ioutil.ReadFile` if fs == nil otherwise VFS.
//
// NOTE: Use VFS instance directly `aah.AppVFS().*`. This is created to prevent
// repetition code in consumimg libraries of aah.
func ReadFile(fs *VFS, filename string) ([]byte, error) {
if fs == nil {
return ioutil.ReadFile(filename)
}
return fs.ReadFile(filename)
}
// ReadDir method calls `ioutil.ReadDir` if fs == nil otherwise VFS.
//
// NOTE: Use VFS instance directly `aah.AppVFS().*`. This is created to prevent
// repetition code in consumimg libraries of aah.
func ReadDir(fs *VFS, dirname string) ([]os.FileInfo, error) {
if fs == nil {
return ioutil.ReadDir(dirname)
}
return fs.ReadDir(dirname)
}
// Glob method calls `filepath.Glob` if fs == nil otherwise VFS.
//
// NOTE: Use VFS instance directly `aah.AppVFS().*`. This is created to prevent
// repetition code in consumimg libraries of aah.
func Glob(fs *VFS, pattern string) ([]string, error) {
if fs == nil {
return filepath.Glob(pattern)
}
return fs.Glob(pattern)
}
// IsExists method is helper to find existence.
//
// NOTE: Use VFS instance directly `aah.AppVFS().*`. This is created to prevent
// repetition code in consumimg libraries of aah.
func IsExists(fs *VFS, name string) bool {
var err error
if fs == nil {
_, err = os.Lstat(name)
} else {
_, err = fs.Lstat(name)
}
return err == nil
}
// Walk method calls `filepath.Walk` if fs == nil otherwise VFS.
//
// NOTE: Use VFS instance directly `aah.AppVFS().*`. This is created to prevent
// repetition code in consumimg libraries of aah.
func Walk(fs *VFS, root string, walkFn filepath.WalkFunc) error {
if fs == nil {
return filepath.Walk(root, walkFn)
}
return fs.Walk(root, walkFn)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package unexported methods
//______________________________________________________________________________
func newNode(name string, fi os.FileInfo) *node {
return &node{
NodeInfo: newNodeInfo(name, fi),
childInfos: make([]os.FileInfo, 0),
childs: make(map[string]*node),
}
}
func newNodeInfo(name string, fi os.FileInfo) *NodeInfo {
return &NodeInfo{
Path: name,
Dir: fi.IsDir(),
DataSize: fi.Size(),
Time: fi.ModTime(),
}
}
func newFile(n *node) *file {
f := &file{node: n}
if !f.IsDir() {
// transparent reading for caller regardless of data bytes.
f.rs = bytes.NewReader(f.node.data)
if f.IsGzip() {
r, _ := gzip.NewReader(f.rs)
f.rs = &gzipData{n: n, r: r}
}
}
return f
}
// readDirNames reads the directory named by dirname and returns
// a sorted list of directory entries.
func readDirNames(fs FileSystem, dirname string) ([]string, error) {
f, err := fs.Open(dirname)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
names, err := f.Readdirnames(-1)
if err != nil {
return nil, err
}
sort.Strings(names)
return names, nil
}
// walk recursively descends path.
func walk(fs FileSystem, fpath string, info os.FileInfo, walkFn filepath.WalkFunc) error {
err := walkFn(fpath, info, nil)
if err != nil {
if info.IsDir() && err == filepath.SkipDir {
return nil
}
return err
}
if !info.IsDir() {
return nil
}
names, err := readDirNames(fs, fpath)
if err != nil {
return walkFn(fpath, info, err)
}
for _, name := range names {
filename := path.Join(fpath, name)
fi, err := fs.Lstat(filename)
if err != nil {
if err = walkFn(filename, fi, err); err != nil && err != filepath.SkipDir {
return err
}
} else {
err = walk(fs, filename, fi, walkFn)
if err != nil {
if !fi.IsDir() || err != filepath.SkipDir {
return err
}
}
}
}
return nil
}
// byName implements sort.Interface
type byName []os.FileInfo
func (f byName) Len() int { return len(f) }
func (f byName) Less(i, j int) bool { return f[i].Name() < f[j].Name() }
func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] }