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
/
dirfs_unix.go
393 lines (343 loc) · 9.08 KB
/
dirfs_unix.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package sandbox
import (
"fmt"
"io/fs"
"runtime"
"sync"
"github.com/stealthrocket/timecraft/internal/sandbox/fspath"
"golang.org/x/sys/unix"
)
type dirFS struct {
root string
}
func (fsys *dirFS) Open(name string, flags OpenFlags, mode fs.FileMode) (File, error) {
f, err := fsys.openRoot()
if err != nil {
return nil, err
}
if fspath.IsRoot(name) {
return f, nil
}
defer f.Close()
return f.Open(name, flags, mode)
}
func (fsys *dirFS) openRoot() (File, error) {
dirfd, err := openat(unix.AT_FDCWD, fsys.root, O_DIRECTORY.sysFlags(), 0)
if err != nil {
return nil, err
}
return newFile(nil, dirfd), nil
}
func newFile(dir *dirFile, fd int) *dirFile {
f := &dirFile{dir: dir, fd: fd}
if dir != nil {
dir.ref()
}
f.ref()
return f
}
type dirFile struct {
dir *dirFile
mutex sync.Mutex
refc int32
closed bool
fd int
}
func (f *dirFile) acquire() (fd int) {
f.mutex.Lock()
if f.closed {
fd = -1
} else {
fd = f.fd
f.refc++
}
f.mutex.Unlock()
return fd
}
func (f *dirFile) release(fd int) {
if fd >= 0 {
f.unref()
}
}
func (f *dirFile) ref() {
f.mutex.Lock()
f.refc++
f.mutex.Unlock()
}
func (f *dirFile) unref() {
f.mutex.Lock()
if f.refc--; f.refc == 0 {
f.close()
}
f.mutex.Unlock()
}
func (f *dirFile) close() {
fd := f.fd
f.fd = -1
closeTraceError(fd)
if f.dir != nil {
f.dir.unref()
}
}
func (f *dirFile) String() string {
return fmt.Sprintf("&sandbox.dirFile{fd:%d}", f.fd)
}
func (f *dirFile) Fd() uintptr {
return uintptr(f.fd)
}
func (f *dirFile) Close() error {
f.mutex.Lock()
if !f.closed {
f.closed = true
if f.refc--; f.refc == 0 {
f.close()
}
}
f.mutex.Unlock()
return nil
}
func (f *dirFile) openSelf() (File, error) {
fd, err := dup(f.fd)
if err != nil {
return nil, err
}
return newFile(f.dir, fd), nil
}
func (f *dirFile) openParent() (File, error) {
if f.dir != nil { // not already at the root?
f = f.dir
}
return f.openSelf()
}
func (f *dirFile) openRoot() (File, error) {
for f.dir != nil { // walk up to root
f = f.dir
}
return f.openSelf()
}
func (f *dirFile) openFile(name string, flags OpenFlags, mode fs.FileMode) (File, error) {
fd, err := openat(f.fd, name, (flags | O_NOFOLLOW).sysFlags(), uint32(mode.Perm()))
if err != nil {
return nil, err
}
return newFile(f, fd), nil
}
func (f *dirFile) open(name string, flags OpenFlags, mode fs.FileMode) (File, error) {
switch name {
case ".":
return f.openSelf()
case "..":
return f.openParent()
default:
return f.openFile(name, flags, mode)
}
}
func (f *dirFile) Open(name string, flags OpenFlags, mode fs.FileMode) (File, error) {
return withFD2(f, func(int) (File, error) {
if fspath.IsRoot(name) {
return f.openRoot()
}
if fspath.HasTrailingSlash(name) {
flags |= O_DIRECTORY
}
return ResolvePath(f, name, flags.LookupFlags(), func(d *dirFile, name string) (File, error) {
return d.open(name, flags, mode)
})
})
}
func (f *dirFile) Readv(iovs [][]byte) (int, error) {
return withFD2(f, func(fd int) (int, error) { return readv(fd, iovs) })
}
func (f *dirFile) Writev(iovs [][]byte) (int, error) {
return withFD2(f, func(fd int) (int, error) { return writev(fd, iovs) })
}
func (f *dirFile) Preadv(iovs [][]byte, offset int64) (int, error) {
return withFD2(f, func(fd int) (int, error) { return preadv(fd, iovs, offset) })
}
func (f *dirFile) Pwritev(iovs [][]byte, offset int64) (int, error) {
return withFD2(f, func(fd int) (int, error) { return pwritev(fd, iovs, offset) })
}
func (f *dirFile) CopyFileRange(srcOffset int64, dst File, dstOffset int64, length int) (int, error) {
if runtime.GOOS == "linux" {
return withFD2(f, func(srcfd int) (int, error) {
return copyFileRange(srcfd, srcOffset, int(dst.Fd()), dstOffset, length)
})
}
return CopyFileRange(f, srcOffset, dst, dstOffset, length)
}
func (f *dirFile) Seek(offset int64, whence int) (int64, error) {
return withFD2(f, func(fd int) (int64, error) { return lseek(fd, offset, whence) })
}
func (f *dirFile) Allocate(offset, length int64) error {
return withFD1(f, func(fd int) error { return fallocate(fd, offset, length) })
}
func (f *dirFile) Truncate(size int64) error {
return withFD1(f, func(fd int) error { return ftruncate(fd, size) })
}
func (f *dirFile) Sync() error {
return withFD1(f, func(fd int) error { return fsync(fd) })
}
func (f *dirFile) Datasync() error {
return withFD1(f, func(fd int) error { return fdatasync(fd) })
}
func (f *dirFile) Flags() (OpenFlags, error) {
return withFD2(f, func(fd int) (OpenFlags, error) {
flags, err := unix.FcntlInt(uintptr(fd), unix.F_GETFL, 0)
return makeOpenFlags(flags), err
})
}
func (f *dirFile) SetFlags(flags OpenFlags) error {
return withFD1(f, func(fd int) error {
_, err := unix.FcntlInt(uintptr(fd), unix.F_SETFL, flags.sysFlags())
return err
})
}
func (f *dirFile) ReadDirent(buf []byte) (int, error) {
return withFD2(f, func(fd int) (int, error) {
return ignoreEINTR2(func() (int, error) {
return unix.ReadDirent(fd, buf)
})
})
}
func (f *dirFile) Stat(name string, flags LookupFlags) (FileInfo, error) {
return resolvePath2(f, name, flags, func(fd int, name string) (FileInfo, error) {
var stat unix.Stat_t
var err error
if name == "" {
err = fstat(fd, &stat)
} else {
err = fstatat(fd, name, &stat, AT_SYMLINK_NOFOLLOW.sysFlags())
}
if err != nil {
return FileInfo{}, err
}
if (stat.Mode & unix.S_IFMT) == unix.S_IFLNK {
if (flags & AT_SYMLINK_NOFOLLOW) == 0 {
return FileInfo{}, ELOOP
}
}
info := FileInfo{
Dev: uint64(stat.Dev),
Ino: uint64(stat.Ino),
Nlink: uint64(stat.Nlink),
Mode: fs.FileMode(stat.Mode & 0777), // perm
Uid: uint32(stat.Uid),
Gid: uint32(stat.Gid),
Size: int64(stat.Size),
Atime: stat.Atim,
Mtime: stat.Mtim,
Ctime: stat.Ctim,
}
switch stat.Mode & unix.S_IFMT {
case unix.S_IFREG:
case unix.S_IFBLK:
info.Mode |= fs.ModeDevice
case unix.S_IFCHR:
info.Mode |= fs.ModeDevice | fs.ModeCharDevice
case unix.S_IFDIR:
info.Mode |= fs.ModeDir
case unix.S_IFIFO:
info.Mode |= fs.ModeNamedPipe
case unix.S_IFLNK:
info.Mode |= fs.ModeSymlink
case unix.S_IFSOCK:
info.Mode |= fs.ModeSocket
default:
info.Mode |= fs.ModeIrregular
}
return info, nil
})
}
func (f *dirFile) Readlink(name string, buf []byte) (int, error) {
return resolvePath2(f, name, AT_SYMLINK_NOFOLLOW, func(fd int, name string) (int, error) {
if name == "" {
return freadlink(fd, buf)
} else {
return readlinkat(fd, name, buf)
}
})
}
func (f *dirFile) Chtimes(name string, times [2]Timespec, flags LookupFlags) error {
return resolvePath1(f, name, flags, func(fd int, name string) error {
if name == "" {
return futimens(fd, ×)
} else {
return utimensat(fd, name, ×, AT_SYMLINK_NOFOLLOW.sysFlags())
}
})
}
func (f *dirFile) Mkdir(name string, mode fs.FileMode) error {
return resolvePath1(f, name, AT_SYMLINK_NOFOLLOW, func(fd int, name string) error {
return mkdirat(fd, name, uint32(mode.Perm()))
})
}
func (f *dirFile) Rmdir(name string) error {
return resolvePath1(f, name, AT_SYMLINK_NOFOLLOW, func(fd int, name string) error {
return unlinkat(fd, name, unix.AT_REMOVEDIR)
})
}
func (f1 *dirFile) Rename(oldName string, newDir File, newName string, flags RenameFlags) error {
f2, ok := newDir.(*dirFile)
if !ok {
return EXDEV
}
return resolvePath1(f1, oldName, AT_SYMLINK_NOFOLLOW, func(fd1 int, name1 string) error {
return resolvePath1(f2, newName, AT_SYMLINK_NOFOLLOW, func(fd2 int, name2 string) error {
return renameat(fd1, name1, fd2, name2, flags.sysFlags())
})
})
}
func (f1 *dirFile) Link(oldName string, newDir File, newName string, flags LookupFlags) error {
f2, ok := newDir.(*dirFile)
if !ok {
return EXDEV
}
return resolvePath1(f1, oldName, flags, func(fd1 int, name1 string) error {
return resolvePath1(f2, newName, flags, func(fd2 int, name2 string) error {
return linkat(fd1, name1, fd2, name2, 0)
})
})
}
func (f *dirFile) Symlink(oldName string, newName string) error {
return resolvePath1(f, newName, AT_SYMLINK_NOFOLLOW, func(fd int, name string) error {
return symlinkat(oldName, fd, name)
})
}
func (f *dirFile) Unlink(name string) error {
return resolvePath1(f, name, AT_SYMLINK_NOFOLLOW, func(fd int, name string) error {
return unlinkat(fd, name, 0)
})
}
func resolvePath1(f *dirFile, name string, flags LookupFlags, do func(int, string) error) error {
return withFD1(f, func(int) error {
_, err := ResolvePath(f, name, flags, func(d *dirFile, name string) (_ struct{}, err error) {
err = do(d.fd, name)
return
})
return err
})
}
func resolvePath2[R any](f *dirFile, name string, flags LookupFlags, do func(int, string) (R, error)) (R, error) {
return withFD2(f, func(int) (R, error) {
return ResolvePath(f, name, flags, func(d *dirFile, name string) (R, error) {
return do(d.fd, name)
})
})
}
func withFD1(f *dirFile, do func(int) error) error {
fd := f.acquire()
if fd < 0 {
return EBADF
}
defer f.release(fd)
return do(fd)
}
func withFD2[R any](f *dirFile, do func(int) (R, error)) (R, error) {
fd := f.acquire()
if fd < 0 {
var zero R
return zero, EBADF
}
defer f.release(fd)
return do(fd)
}