-
Notifications
You must be signed in to change notification settings - Fork 2
/
union.go
269 lines (244 loc) · 6.47 KB
/
union.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
package wpk
import (
"io"
"io/fs"
"path"
"strconv"
"strings"
)
// Void is empty structure to release of the set of keys.
type Void = struct{}
// UnionDir is helper to get access to union directories,
// that contains files from all packages with same dir if it present.
type UnionDir struct {
TagsetRaw
*Union
}
// fs.ReadDirFile interface implementation.
func (f *UnionDir) Stat() (fs.FileInfo, error) {
return f, nil
}
// fs.ReadDirFile interface implementation.
func (f *UnionDir) Read([]byte) (int, error) {
return 0, io.EOF
}
// fs.ReadDirFile interface implementation.
func (f *UnionDir) Close() error {
return nil
}
// fs.ReadDirFile interface implementation.
func (f *UnionDir) ReadDir(n int) ([]fs.DirEntry, error) {
var dir = f.Path()
if len(f.List) > 0 {
if dir = f.List[0].TrimPath(dir); dir == "" {
return nil, ErrOtherSubdir
}
}
return f.ReadDirN(dir, n)
}
// Union glues list of packages into single filesystem.
type Union struct {
List []*Package
}
// Close call Close-function for all included into the union packages.
// io.Closer implementation.
func (u *Union) Close() (err error) {
for _, pkg := range u.List {
if err1 := pkg.Tagger.Close(); err1 != nil {
err = err1
}
}
return
}
// AllKeys returns list of all accessible files in union of packages.
// If union have more than one file with the same name, only first
// entry will be included to result.
func (u *Union) AllKeys() (res []string) {
var found = map[string]Void{}
for _, pkg := range u.List {
pkg.Enum(func(fkey string, ts TagsetRaw) bool {
if _, ok := found[fkey]; !ok {
res = append(res, fkey)
found[fkey] = Void{}
}
return true
})
}
return
}
// Sub clones object and gives access to pointed subdirectory.
// fs.SubFS implementation.
func (u *Union) Sub(dir string) (fs.FS, error) {
var u1 Union
for _, pkg := range u.List {
if sub1, err1 := pkg.Sub(dir); err1 == nil {
u1.List = append(u1.List, sub1.(*Package))
}
}
if len(u1.List) == 0 {
return nil, &fs.PathError{Op: "sub", Path: dir, Err: fs.ErrNotExist}
}
return &u1, nil
}
// Stat returns a fs.FileInfo describing the file.
// If union have more than one file with the same name, info of the first will be returned.
// fs.StatFS implementation.
func (u *Union) Stat(fpath string) (fs.FileInfo, error) {
var ts TagsetRaw
var is bool
for _, pkg := range u.List {
if ts, is = pkg.GetTagset(fpath); is {
return ts, nil
}
}
return nil, &fs.PathError{Op: "stat", Path: fpath, Err: fs.ErrNotExist}
}
// Glob returns the names of all files in union matching pattern or nil
// if there is no matching file.
func (u *Union) Glob(pattern string) (res []string, err error) {
pattern = ToSlash(pattern)
if _, err = path.Match(pattern, ""); err != nil {
return
}
var found = map[string]Void{}
for _, pkg := range u.List {
pkg.Enum(func(fkey string, ts TagsetRaw) bool {
if _, ok := found[fkey]; !ok {
if matched, _ := path.Match(pattern, fkey); matched {
res = append(res, fkey)
}
found[fkey] = Void{}
}
return true
})
}
return
}
// ReadFile returns slice with nested into union of packages file content.
// If union have more than one file with the same name, first will be returned.
// fs.ReadFileFS implementation.
func (u *Union) ReadFile(fpath string) ([]byte, error) {
for _, pkg := range u.List {
if ts, is := pkg.GetTagset(fpath); is {
var f, err = pkg.Tagger.OpenTagset(ts)
if err != nil {
return nil, err
}
defer f.Close()
var size = ts.Size()
var buf = make([]byte, size)
_, err = f.Read(buf)
return buf, err
}
}
return nil, &fs.PathError{Op: "readfile", Path: fpath, Err: fs.ErrNotExist}
}
// ReadDir reads the named directory
// and returns a list of directory entries sorted by filename.
func (u *Union) ReadDirN(dir string, n int) (list []fs.DirEntry, err error) {
dir = ToSlash(dir)
var found = map[string]fs.DirEntry{}
var ni = n
for _, pkg := range u.List {
var fulldir = pkg.FullPath(dir)
var prefix string
if fulldir != "." {
prefix = fulldir + "/" // set terminated slash
}
pkg.tsm.Range(func(fkey string, ts TagsetRaw) bool {
if strings.HasPrefix(fkey, prefix) {
var suffix = fkey[len(prefix):]
var sp = strings.IndexByte(suffix, '/')
if sp < 0 { // file detected
found[suffix] = ts
ni--
} else { // dir detected
var subdir = JoinPath(prefix, suffix[:sp])
if _, ok := found[subdir]; !ok {
var dts = TagsetRaw{}.
Put(TIDpath, StrTag(subdir))
var f = &PackDirFile{
TagsetRaw: dts,
ftt: pkg.FTT,
}
found[subdir] = f
ni--
}
}
}
return ni != 0
})
if ni == 0 {
break
}
}
list = make([]fs.DirEntry, len(found))
var i int
for _, de := range found {
list[i] = de
i++
}
if ni > 0 {
err = io.EOF
}
return
}
// ReadDir reads the named directory
// and returns a list of directory entries sorted by filename.
// fs.ReadDirFS interface implementation.
func (u *Union) ReadDir(dir string) ([]fs.DirEntry, error) {
return u.ReadDirN(dir, -1)
}
// Open implements access to nested into union of packages file or directory by keyname.
// If union have more than one file with the same name, first will be returned.
// fs.FS implementation.
func (u *Union) Open(dir string) (fs.File, error) {
dir = ToSlash(dir)
if len(u.List) == 0 {
return nil, &fs.PathError{Op: "open", Path: dir, Err: fs.ErrNotExist}
}
if len(u.List) > 0 {
if fulldir := u.List[0].FullPath(dir); strings.HasPrefix(fulldir, PackName+"/") {
var idx, err = strconv.ParseUint(dir[len(PackName)+1:], 10, 32)
if err != nil {
return nil, &fs.PathError{Op: "open", Path: dir, Err: err}
}
if idx >= uint64(len(u.List)) {
return nil, &fs.PathError{Op: "open", Path: dir, Err: fs.ErrNotExist}
}
return u.List[idx].Open(PackName)
}
}
// try to get the file
for _, pkg := range u.List {
if ts, is := pkg.GetTagset(dir); is {
return pkg.Tagger.OpenTagset(ts)
}
}
// try to get the folder
var prefix string
if dir != "." && dir != "" {
prefix = dir + "/" // set terminated slash
}
for _, pkg := range u.List {
var f *UnionDir
pkg.Enum(func(fkey string, ts TagsetRaw) bool {
if strings.HasPrefix(fkey, prefix) {
var dts = TagsetRaw{}.
Put(TIDpath, StrTag(pkg.FullPath(dir)))
f = &UnionDir{
TagsetRaw: dts,
Union: u,
}
return false
}
return true
})
if f != nil {
return f, nil
}
}
// on case if not found
return nil, &fs.PathError{Op: "open", Path: dir, Err: fs.ErrNotExist}
}
// The End.