This adds FS.Path which holds the pre-open path currently only used in WASI. It also fixes a TODO where we didn't know for sure if the FD parameter for `path_` functions must always be a pre-open. The TL;DR; is that usually it is, but it may not be (e.g. in our zig-cc example we can see any directory FD, not just pre-opens). Finally, this fixes a bug in our path resolution where we mistook paths like "foo/foo" for "foo" because we only considered basenames instead of the full path from the pre-open root. This also makes pre-open directory lookup lazy because I noticed in Trivy specifically, this is unnecessary for us to do eagerly, as they change the FS at runtime per-call. In other words, any value from init time is invalid later. Signed-off-by: Adrian Cole <adrian@tetrate.io>
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package syscallfs
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"syscall"
|
|
)
|
|
|
|
// EmptyFS is an FS that returns syscall.ENOENT for all read functions, and
|
|
// syscall.ENOSYS otherwise.
|
|
var EmptyFS FS = empty{}
|
|
|
|
type empty struct{}
|
|
|
|
// Open implements the same method as documented on fs.FS
|
|
func (empty) Open(name string) (fs.File, error) {
|
|
panic(fmt.Errorf("unexpected to call fs.FS.Open(%s)", name))
|
|
}
|
|
|
|
// Path implements FS.Path
|
|
func (empty) Path() string {
|
|
return "/"
|
|
}
|
|
|
|
// OpenFile implements FS.OpenFile
|
|
func (empty) OpenFile(path string, flag int, perm fs.FileMode) (fs.File, error) {
|
|
return nil, &fs.PathError{Op: "open", Path: path, Err: syscall.ENOENT}
|
|
}
|
|
|
|
// Mkdir implements FS.Mkdir
|
|
func (empty) Mkdir(path string, perm fs.FileMode) error {
|
|
return syscall.ENOSYS
|
|
}
|
|
|
|
// Rename implements FS.Rename
|
|
func (empty) Rename(from, to string) error {
|
|
return syscall.ENOSYS
|
|
}
|
|
|
|
// Rmdir implements FS.Rmdir
|
|
func (empty) Rmdir(path string) error {
|
|
return syscall.ENOSYS
|
|
}
|
|
|
|
// Unlink implements FS.Unlink
|
|
func (empty) Unlink(path string) error {
|
|
return syscall.ENOSYS
|
|
}
|
|
|
|
// Utimes implements FS.Utimes
|
|
func (empty) Utimes(path string, atimeNsec, mtimeNsec int64) error {
|
|
return syscall.ENOSYS
|
|
}
|