-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
fs.go
193 lines (149 loc) · 3.32 KB
/
fs.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
package tarfs
import (
"archive/tar"
"bytes"
"io"
"io/fs"
"path"
"strings"
)
const (
blockSize = 512 // Size of each block in a tar stream
)
type tarfs struct {
entries map[string]fs.DirEntry
}
var _ fs.FS = &tarfs{}
// New creates a new tar fs.FS from r.
// If r implements io.ReaderAt:
// - files content are not stored in memory
// - r must stay opened while using the fs.FS
func New(r io.Reader) (fs.FS, error) {
tfs := &tarfs{make(map[string]fs.DirEntry)}
tfs.entries["."] = newDirEntry(fs.FileInfoToDirEntry(fakeDirFileInfo(".")))
ra, isReaderAt := r.(readReaderAt)
if !isReaderAt {
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
ra = bytes.NewReader(buf)
}
var cr readCounterIface
if rs, isReadSeeker := ra.(io.ReadSeeker); isReadSeeker {
cr = &readSeekCounter{ReadSeeker: rs}
} else {
cr = &readCounter{Reader: ra}
}
tr := tar.NewReader(cr)
for {
h, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if h.Typeflag == tar.TypeXGlobalHeader {
continue
}
name := path.Clean(h.Name)
if name == "." {
continue
}
de := fs.FileInfoToDirEntry(h.FileInfo())
if h.FileInfo().IsDir() {
tfs.append(name, newDirEntry(de))
} else {
tfs.append(name, ®Entry{de, name, ra, cr.Count() - blockSize})
}
}
return tfs, nil
}
func (tfs *tarfs) append(name string, e fs.DirEntry) {
tfs.entries[name] = e
dir := path.Dir(name)
if parent, ok := tfs.entries[dir]; ok {
parent := parent.(*dirEntry)
parent.append(e)
return
}
parent := newDirEntry(fs.FileInfoToDirEntry(fakeDirFileInfo(path.Base(dir))))
tfs.append(dir, parent)
parent.append(e)
}
func (tfs *tarfs) Open(name string) (fs.File, error) {
const op = "open"
e, err := tfs.get(op, name)
if err != nil {
return nil, err
}
return e.open()
}
var _ fs.ReadDirFS = &tarfs{}
func (tfs *tarfs) ReadDir(name string) ([]fs.DirEntry, error) {
e, err := tfs.get("readdir", name)
if err != nil {
return nil, err
}
return e.readdir(name)
}
var _ fs.ReadFileFS = &tarfs{}
func (tfs *tarfs) ReadFile(name string) ([]byte, error) {
e, err := tfs.get("readfile", name)
if err != nil {
return nil, err
}
return e.readfile(name)
}
var _ fs.StatFS = &tarfs{}
func (tfs *tarfs) Stat(name string) (fs.FileInfo, error) {
e, err := tfs.get("stat", name)
if err != nil {
return nil, err
}
return e.Info()
}
var _ fs.GlobFS = &tarfs{}
func (tfs *tarfs) Glob(pattern string) (matches []string, _ error) {
for name := range tfs.entries {
match, err := path.Match(pattern, name)
if err != nil {
return nil, err
}
if match {
matches = append(matches, name)
}
}
return
}
var _ fs.SubFS = &tarfs{}
func (tfs *tarfs) Sub(dir string) (fs.FS, error) {
const op = "sub"
if dir == "." {
return tfs, nil
}
e, err := tfs.get(op, dir)
if err != nil {
return nil, err
}
subfs := &tarfs{make(map[string]fs.DirEntry)}
subfs.entries["."] = e
prefix := dir + "/"
for name, file := range tfs.entries {
if strings.HasPrefix(name, prefix) {
subfs.entries[strings.TrimPrefix(name, prefix)] = file
}
}
return subfs, nil
}
func (tfs *tarfs) get(op, path string) (entry, error) {
if !fs.ValidPath(path) {
return nil, newErr(op, path, fs.ErrInvalid)
}
e, ok := tfs.entries[path]
if !ok {
return nil, newErrNotExist(op, path)
}
return e.(entry), nil
}