github.com/gaukas/wazerofs@v0.1.0/memfs/file.go (about) 1 package memfs 2 3 import ( 4 "errors" 5 "io" 6 "strings" 7 8 wasys "github.com/tetratelabs/wazero/sys" 9 10 "github.com/tetratelabs/wazero/experimental/sys" 11 12 "github.com/blang/vfs" 13 "github.com/blang/vfs/memfs" 14 ) 15 16 type memoryFSFile struct { 17 fs *memfs.MemFS 18 fl vfs.File 19 path string 20 21 sys.UnimplementedFile 22 } 23 24 func (f *memoryFSFile) Stat() (wasys.Stat_t, sys.Errno) { 25 return stat(f.fs, f.path) 26 } 27 28 func (f *memoryFSFile) Close() sys.Errno { 29 err := f.fl.Close() 30 if err != nil { 31 // this will never happen 32 return sys.EIO 33 } 34 return 0 35 } 36 37 func (f *memoryFSFile) IsDir() (bool, sys.Errno) { 38 return false, 0 39 } 40 41 func (f *memoryFSFile) Read(buf []byte) (n int, errno sys.Errno) { 42 n, err := f.fl.Read(buf) 43 if err != nil { 44 if errors.Is(err, io.EOF) { 45 return n, 0 46 } 47 // this will never happen 48 return 0, sys.EBADF 49 } 50 return 51 } 52 53 func (f *memoryFSFile) Seek(offset int64, whence int) (newOffset int64, errno sys.Errno) { 54 r, err := f.fl.Seek(offset, whence) 55 if err != nil { 56 57 if strings.Contains(err.Error(), "invalid whence") { 58 return 0, sys.EINVAL 59 } 60 if strings.Contains(err.Error(), "negative position") { 61 return 0, sys.EINVAL 62 } 63 if strings.Contains(err.Error(), "too far") { 64 // it should be POSIX EFBIG but wazero maps that to EIO 65 return 0, sys.EIO 66 } 67 // can never happen 68 return 0, sys.EINVAL 69 } 70 return r, 0 71 } 72 73 func (f *memoryFSFile) Write(buf []byte) (n int, errno sys.Errno) { 74 n, err := f.fl.Write(buf) 75 if err != nil { 76 // it should be POSIX EFBIG but wazero maps that to EIO 77 return 0, sys.EIO 78 } 79 return 80 }