package sysfs
import (
"io/fs"
"os"
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/sys"
)
func DirFS (dir string ) experimentalsys .FS {
return &dirFS {
dir : dir ,
cleanedDir : ensureTrailingPathSeparator (dir ),
}
}
func ensureTrailingPathSeparator(dir string ) string {
if !os .IsPathSeparator (dir [len (dir )-1 ]) {
return dir + string (os .PathSeparator )
}
return dir
}
type dirFS struct {
experimentalsys .UnimplementedFS
dir string
cleanedDir string
}
func (d *dirFS ) String () string {
return d .dir
}
func (d *dirFS ) OpenFile (path string , flag experimentalsys .Oflag , perm fs .FileMode ) (experimentalsys .File , experimentalsys .Errno ) {
return OpenOSFile (d .join (path ), flag , perm )
}
func (d *dirFS ) Lstat (path string ) (sys .Stat_t , experimentalsys .Errno ) {
return lstat (d .join (path ))
}
func (d *dirFS ) Stat (path string ) (sys .Stat_t , experimentalsys .Errno ) {
return stat (d .join (path ))
}
func (d *dirFS ) Mkdir (path string , perm fs .FileMode ) (errno experimentalsys .Errno ) {
err := os .Mkdir (d .join (path ), perm )
if errno = experimentalsys .UnwrapOSError (err ); errno == experimentalsys .ENOTDIR {
errno = experimentalsys .ENOENT
}
return
}
func (d *dirFS ) Readlink (path string ) (string , experimentalsys .Errno ) {
dst , err := os .Readlink (d .join (path ))
if err != nil {
return "" , experimentalsys .UnwrapOSError (err )
}
return platform .ToPosixPath (dst ), 0
}
func (d *dirFS ) Rmdir (path string ) experimentalsys .Errno {
return rmdir (d .join (path ))
}
func (d *dirFS ) Utimens (path string , atim , mtim int64 ) experimentalsys .Errno {
return utimens (d .join (path ), atim , mtim )
}
func (d *dirFS ) join (path string ) string {
switch path {
case "" , "." , "/" :
if d .cleanedDir == "/" {
return "/"
}
return d .cleanedDir [:len (d .cleanedDir )-1 ]
}
return d .cleanedDir + path
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .