This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tarfs.go
293 lines (246 loc) · 6.27 KB
/
tarfs.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package tarfs
import (
"archive/tar"
"cmp"
"fmt"
"io"
"io/fs"
"os"
"path"
"slices"
"time"
"github.com/stealthrocket/timecraft/internal/sandbox"
"github.com/stealthrocket/timecraft/internal/sandbox/fspath"
)
// FileSystem is an implementation of the sandbox.FileSystem interface backed by
// a tarball.
type FileSystem struct {
data io.ReaderAt
size int64
memsize int64
filesize int64
root dir
}
// Open satisfies sandbox.FileSystem.
func (fsys *FileSystem) Open(name string, flags sandbox.OpenFlags, mode fs.FileMode) (sandbox.File, error) {
f, err := fsys.root.open(fsys)
if err != nil {
return nil, err
}
if fspath.IsRoot(name) {
return f, nil
}
defer f.Close()
return f.Open(name, flags, mode)
}
// Size returns the size of data in the file system, which is the value passed
// to Open when fsys was created. It is the full size of the underlying tarball.
func (fsys *FileSystem) Size() int64 {
return fsys.size
}
// Memsize returns the size occupied by the file system in memory.
func (fsys *FileSystem) Memsize() int64 {
return fsys.memsize
}
// Filesize returns the size occupied by the file data in the file system.
func (fsys *FileSystem) Filesize() int64 {
return fsys.filesize
}
// Open creates a file system from the tarball represented by the given section.
//
// The file system retains the io.ReaderAt as a backing storage layer, it does
// not load the content of the files present in the tarball in memory; only the
// structure of the file system is held in memory (e.g. directory paths and
// metadata about the files).
func Open(data io.ReaderAt, size int64) (*FileSystem, error) {
section := io.NewSectionReader(data, 0, size)
r := tar.NewReader(section)
modTime := time.Now()
fsys := &FileSystem{
data: data,
size: size,
root: makeDir(modTime),
}
links := make(map[string]string)
files := make(map[string]fileEntry)
files["/"] = &fsys.root
for {
header, err := r.Next()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
name := absPath(header.Name)
switch name {
case ".", "..":
continue // ignore those entries, they should never exist anyway
case "/":
template := newDir(header)
fsys.root.perm = template.perm
fsys.root.mtime = template.mtime
fsys.root.atime = template.atime
fsys.root.ctime = template.ctime
continue
}
var entry fileEntry
switch header.Typeflag {
case tar.TypeReg:
offset, _ := section.Seek(0, io.SeekCurrent)
entry = newFile(header, offset)
case tar.TypeDir:
entry = newDir(header)
case tar.TypeSymlink:
entry = newSymlink(header)
case tar.TypeLink:
if _, exists := links[name]; exists {
return nil, fmt.Errorf("%s: duplicate link entry in tar archive", name)
}
links[name] = absPath(header.Linkname)
continue
default:
entry = newPlaceholder(header)
}
if files[name] != nil {
return nil, fmt.Errorf("%s: duplicate file entry in tar archive", name)
}
files[name] = entry
if err := makePath(files, name, modTime, entry); err != nil {
return nil, err
}
}
for link, name := range links {
entry := files[name]
switch f := entry.(type) {
case *file:
f.nlink++
case *symlink:
f.nlink++
default:
return nil, fmt.Errorf("%s->%s: hard link to invalid file in tar archive", link, name)
}
if files[link] != nil {
return nil, fmt.Errorf("%s: duplicate file entry in tar archive", link)
}
if err := makePath(files, link, modTime, entry); err != nil {
return nil, err
}
}
for name, entry := range files {
if d, ok := entry.(*dir); ok {
d.parent = files[path.Dir(name)].(*dir)
slices.SortFunc(d.ents, func(a, b dirEntry) int {
return cmp.Compare(a.name, b.name)
})
}
}
for _, entry := range files {
fsys.memsize += int64(entry.memsize())
if f, ok := entry.(*file); ok {
fsys.filesize += f.size
}
}
return fsys, nil
}
// OpenFile is like Open but it takes an *os.File opened on a tarball as
// argument.
//
// The file must remain open for as long as the program needs to access the file
// system.
func OpenFile(f *os.File) (*FileSystem, error) {
s, err := f.Stat()
if err != nil {
return nil, err
}
return Open(f, s.Size())
}
type fileEntry interface {
open(fsys *FileSystem) (sandbox.File, error)
stat() sandbox.FileInfo
mode() fs.FileMode
memsize() uintptr
}
func absPath(p string) string {
return path.Join("/", p)
}
func makePath(files map[string]fileEntry, name string, modTime time.Time, file fileEntry) error {
var d *dir
dirname := path.Dir(name)
switch f := files[dirname].(type) {
case nil:
if err := makePath(files, name, modTime, file); err != nil {
return err
}
dir := makeDir(modTime)
d = &dir
files[dirname] = d
case *dir:
d = f
default:
return sandbox.EPERM
}
d.ents = append(d.ents, dirEntry{
name: path.Base(name),
file: file,
})
return nil
}
type readOnlyFile struct{}
func (readOnlyFile) Fd() uintptr {
return ^uintptr(0)
}
func (readOnlyFile) Writev([][]byte) (int, error) {
return 0, sandbox.EBADF
}
func (readOnlyFile) Pwritev([][]byte, int64) (int, error) {
return 0, sandbox.EBADF
}
func (readOnlyFile) Allocate(int64, int64) error {
return sandbox.EBADF
}
func (readOnlyFile) Truncate(int64) error {
return sandbox.EBADF
}
func (readOnlyFile) Sync() error {
return nil
}
func (readOnlyFile) Datasync() error {
return nil
}
func (readOnlyFile) Flags() (sandbox.OpenFlags, error) {
return 0, nil
}
func (readOnlyFile) SetFlags(sandbox.OpenFlags) error {
return sandbox.EINVAL
}
func (readOnlyFile) Chtimes(string, [2]sandbox.Timespec, sandbox.LookupFlags) error {
return sandbox.EPERM
}
type leafFile struct {
readOnlyFile
}
func (leafFile) Open(string, sandbox.OpenFlags, fs.FileMode) (sandbox.File, error) {
return nil, sandbox.ENOTDIR
}
func (leafFile) ReadDirent([]byte) (int, error) {
return 0, sandbox.ENOTDIR
}
func (leafFile) Mkdir(string, fs.FileMode) error {
return sandbox.ENOTDIR
}
func (leafFile) Rmdir(string) error {
return sandbox.ENOTDIR
}
func (leafFile) Rename(string, sandbox.File, string, sandbox.RenameFlags) error {
return sandbox.ENOTDIR
}
func (leafFile) Link(string, sandbox.File, string, sandbox.LookupFlags) error {
return sandbox.ENOTDIR
}
func (leafFile) Symlink(string, string) error {
return sandbox.ENOTDIR
}
func (leafFile) Unlink(string) error {
return sandbox.ENOTDIR
}