2023-10-16 09:18:40 +00:00
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OsFS struct {
|
|
|
|
hostDir string
|
|
|
|
}
|
|
|
|
|
2023-12-25 22:11:03 +00:00
|
|
|
// Unlink implements Filesystem.
|
|
|
|
func (fs *OsFS) Unlink(filename string) error {
|
|
|
|
return fs.Unlink(filename)
|
|
|
|
}
|
|
|
|
|
2023-12-21 23:15:39 +00:00
|
|
|
// Stat implements Filesystem.
|
|
|
|
func (fs *OsFS) Stat(filename string) (fs.FileInfo, error) {
|
|
|
|
if path.Clean(filename) == Separator {
|
|
|
|
return newDirInfo(Separator), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Stat(path.Join(fs.hostDir, filename))
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:18:40 +00:00
|
|
|
// Open implements Filesystem.
|
|
|
|
func (fs *OsFS) Open(filename string) (File, error) {
|
|
|
|
if path.Clean(filename) == Separator {
|
2023-12-21 23:15:39 +00:00
|
|
|
return NewDir(filename), nil
|
2023-10-16 09:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
osfile, err := os.Open(path.Join(fs.hostDir, filename))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewOsFile(osfile), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDir implements Filesystem.
|
2023-12-21 23:15:39 +00:00
|
|
|
func (o *OsFS) ReadDir(dir string) ([]fs.DirEntry, error) {
|
2023-10-16 09:18:40 +00:00
|
|
|
dir = path.Join(o.hostDir, dir)
|
2023-12-21 23:15:39 +00:00
|
|
|
return os.ReadDir(dir)
|
2023-10-16 09:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewOsFs(osDir string) *OsFS {
|
|
|
|
return &OsFS{
|
|
|
|
hostDir: osDir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Filesystem = &OsFS{}
|
|
|
|
|
|
|
|
type OsFile struct {
|
|
|
|
f *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOsFile(f *os.File) *OsFile {
|
|
|
|
return &OsFile{f: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ File = &OsFile{}
|
|
|
|
|
2023-12-21 23:15:39 +00:00
|
|
|
// Info implements File.
|
|
|
|
func (f *OsFile) Info() (fs.FileInfo, error) {
|
|
|
|
return f.f.Stat()
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:18:40 +00:00
|
|
|
// Close implements File.
|
|
|
|
func (f *OsFile) Close() error {
|
|
|
|
return f.f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements File.
|
|
|
|
func (f *OsFile) Read(p []byte) (n int, err error) {
|
|
|
|
return f.f.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAt implements File.
|
|
|
|
func (f *OsFile) ReadAt(p []byte, off int64) (n int, err error) {
|
|
|
|
return f.f.ReadAt(p, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *OsFile) Stat() (fs.FileInfo, error) {
|
|
|
|
return f.f.Stat()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements File.
|
|
|
|
func (f *OsFile) Size() int64 {
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return stat.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDir implements File.
|
|
|
|
func (f *OsFile) IsDir() bool {
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return stat.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
type LazyOsFile struct {
|
|
|
|
m sync.Mutex
|
|
|
|
path string
|
|
|
|
file *os.File
|
2023-12-21 23:15:39 +00:00
|
|
|
|
|
|
|
// cached field
|
|
|
|
info fs.FileInfo
|
2023-10-16 09:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLazyOsFile(path string) *LazyOsFile {
|
|
|
|
return &LazyOsFile{path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ File = &OsFile{}
|
|
|
|
|
|
|
|
func (f *LazyOsFile) open() error {
|
|
|
|
f.m.Lock()
|
|
|
|
defer f.m.Unlock()
|
|
|
|
|
|
|
|
if f.file != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
osFile, err := os.Open(f.path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.file = osFile
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements File.
|
|
|
|
func (f *LazyOsFile) Close() error {
|
2023-12-21 23:15:39 +00:00
|
|
|
if f.file == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-16 09:18:40 +00:00
|
|
|
return f.file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements File.
|
|
|
|
func (f *LazyOsFile) Read(p []byte) (n int, err error) {
|
2023-12-21 23:15:39 +00:00
|
|
|
err = f.open()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-10-16 09:18:40 +00:00
|
|
|
return f.file.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAt implements File.
|
|
|
|
func (f *LazyOsFile) ReadAt(p []byte, off int64) (n int, err error) {
|
2023-12-21 23:15:39 +00:00
|
|
|
err = f.open()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-10-16 09:18:40 +00:00
|
|
|
return f.file.ReadAt(p, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *LazyOsFile) Stat() (fs.FileInfo, error) {
|
2023-12-21 23:15:39 +00:00
|
|
|
f.m.Lock()
|
|
|
|
if f.info == nil {
|
|
|
|
if f.file == nil {
|
|
|
|
info, err := os.Stat(f.path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f.info = info
|
|
|
|
} else {
|
|
|
|
info, err := f.file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f.info = info
|
|
|
|
}
|
2023-10-16 09:18:40 +00:00
|
|
|
}
|
2023-12-21 23:15:39 +00:00
|
|
|
f.m.Unlock()
|
|
|
|
return f.info, nil
|
2023-10-16 09:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements File.
|
|
|
|
func (f *LazyOsFile) Size() int64 {
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return stat.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDir implements File.
|
|
|
|
func (f *LazyOsFile) IsDir() bool {
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return stat.IsDir()
|
|
|
|
}
|