56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package nar
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
)
|
|
|
|
type DirFS string
|
|
|
|
func (dir DirFS) Open(name string) (fs.File, error) {
|
|
if !fs.ValidPath(name) {
|
|
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
|
|
}
|
|
f, err := os.Open(dir.Join(name))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
func (dir DirFS) Stat(name string) (fs.FileInfo, error) {
|
|
if !fs.ValidPath(name) {
|
|
return nil, &fs.PathError{Op: "stat", Path: name, Err: fs.ErrInvalid}
|
|
}
|
|
f, err := os.Stat(dir.Join(name))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
func (dir DirFS) Lstat(name string) (fs.FileInfo, error) {
|
|
if !fs.ValidPath(name) {
|
|
return nil, &fs.PathError{Op: "lstat", Path: name, Err: fs.ErrInvalid}
|
|
}
|
|
f, err := os.Lstat(dir.Join(name))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
func (dir DirFS) Readlink(name string) (string, error) {
|
|
if !fs.ValidPath(name) {
|
|
return "", &fs.PathError{Op: "readlink", Path: name, Err: fs.ErrInvalid}
|
|
}
|
|
f, err := os.Readlink(dir.Join(name))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
func (dir DirFS) Join(name string) string {
|
|
return string(dir) + string(os.PathSeparator) + name
|
|
}
|