Files
wazero/internal/sysfs/dirfs.go
Crypt Keeper cb97d7a488 fs: decouples sysfs from fs.FS and consolidates guest path logic (#1058)
This decouples sysfs.FS from fs.FS by introducing a temporary type
FSHolder, which will be removed when we top-level FSConfig (shortly).

This further reduces complexity by consolidating guest path
configuration into the only type that uses it: CompositeFS.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-23 17:49:51 -10:00

90 lines
2.0 KiB
Go

package sysfs
import (
"io/fs"
"os"
"syscall"
)
func NewDirFS(dir string) FS {
return &dirFS{
dir: dir,
cleanedDir: ensureTrailingPathSeparator(dir),
}
}
func ensureTrailingPathSeparator(dir string) string {
if dir[len(dir)-1] != os.PathSeparator {
return dir + string(os.PathSeparator)
}
return dir
}
type dirFS struct {
UnimplementedFS
dir string
// cleanedDir is for easier OS-specific concatenation, as it always has
// a trailing path separator.
cleanedDir string
}
// String implements fmt.Stringer
func (d *dirFS) String() string {
return d.dir
}
// OpenFile implements FS.OpenFile
func (d *dirFS) OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error) {
f, err := os.OpenFile(d.join(name), flag, perm)
if err != nil {
return nil, unwrapPathError(err)
}
return maybeWrapFile(f), nil
}
// Mkdir implements FS.Mkdir
func (d *dirFS) Mkdir(name string, perm fs.FileMode) error {
err := os.Mkdir(d.join(name), perm)
err = unwrapPathError(err)
return adjustMkdirError(err)
}
// Rename implements FS.Rename
func (d *dirFS) Rename(from, to string) error {
if from == to {
return nil
}
return rename(d.join(from), d.join(to))
}
// Rmdir implements FS.Rmdir
func (d *dirFS) Rmdir(name string) error {
err := syscall.Rmdir(d.join(name))
return adjustRmdirError(err)
}
// Unlink implements FS.Unlink
func (d *dirFS) Unlink(name string) error {
err := syscall.Unlink(d.join(name))
return adjustUnlinkError(err)
}
// Utimes implements FS.Utimes
func (d *dirFS) Utimes(name string, atimeNsec, mtimeNsec int64) error {
return syscall.UtimesNano(d.join(name), []syscall.Timespec{
syscall.NsecToTimespec(atimeNsec),
syscall.NsecToTimespec(mtimeNsec),
})
}
func (d *dirFS) join(name string) string {
switch name {
case "", ".", "/":
// cleanedDir includes an unnecessary delimiter for the root path.
return d.cleanedDir[:len(d.cleanedDir)-1]
}
// TODO: Enforce similar to safefilepath.FromFS(name), but be careful as
// relative path inputs are allowed. e.g. dir or name == ../
return d.cleanedDir + name
}