From 5008cdd4f408ab0e8007f2c847a339ece9f08b80 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Sun, 19 Feb 2023 12:23:44 +0000 Subject: [PATCH] Define reader --- fuse/main.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/fuse/main.go b/fuse/main.go index c4580cb..b6c762f 100644 --- a/fuse/main.go +++ b/fuse/main.go @@ -106,6 +106,41 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (* return nil, syscall.ENOENT } +type bytesFileHandle struct { + content []byte +} +var _ = (fs.FileReader)((*bytesFileHandle)(nil)) + +func (fh *bytesFileHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { + end := off + int64(len(dest)) + if end > int64(len(fh.content)) { + end = int64(len(fh.content)) + } + return fuse.ReadResultData(fh.content[off:end]), 0 +} + +var _ = (fs.NodeOpener)((*ITNode)(nil)) +func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) { + switch f.kind { + case 3: + // Device file + + // disallow writes + if fuseFlags&(syscall.O_RDWR|syscall.O_WRONLY) != 0 { + return nil, 0, syscall.EROFS + } + + for _, value := range properties { + if value.Ino == f.Ino { + fh = &bytesFileHandle{ + content: []byte(value.name), + } + return fh, fuse.FOPEN_DIRECT_IO, 0 + } + } + } + return nil, 0, syscall.EROFS +} func main() { // This is where we'll mount the FS