lib9p

Go 9P library.
Log | Files | Refs | LICENSE

export_fs.go (782B)


      1 package lib9p
      2 
      3 import (
      4 	"errors"
      5 	"io/fs"
      6 )
      7 
      8 // An ExportFS is a wrapper of FS to export it as an fs.FS.
      9 type ExportFS struct {
     10 	FS
     11 }
     12 
     13 func (fsys ExportFS) Open(name string) (fs.File, error) {
     14 	f, err := fsys.OpenFile(name, O_RDONLY)
     15 	return ExportFile{f}, err
     16 }
     17 
     18 // An ExportFile is a wrapper of File to export it as an fs.File.
     19 type ExportFile struct {
     20 	f File
     21 }
     22 
     23 func (f ExportFile) Stat() (fs.FileInfo, error) {
     24 	info, err := f.f.Stat()
     25 	return fs.FileInfo(info), err
     26 }
     27 
     28 func (f ExportFile) Close() error {
     29 	return f.f.Close()
     30 }
     31 
     32 func (f ExportFile) Read(p []byte) (n int, err error) {
     33 	return f.f.Read(p)
     34 }
     35 
     36 func (f ExportFile) ReadDir(n int) ([]fs.DirEntry, error) {
     37 	d, ok := f.f.(ReadDirFile)
     38 	if !ok {
     39 		return nil, errors.New("not a directory")
     40 	}
     41 	return d.ReadDir(n)
     42 }