diff options
author | Jakob Unterwurzacher | 2020-08-09 22:11:46 +0200 |
---|---|---|
committer | Jakob Unterwurzacher | 2020-08-09 22:11:46 +0200 |
commit | 6d4f1a6888cafd218cb97bd11de6a7553d9bc8f1 (patch) | |
tree | 7b48b6e97c4c73a4e0e9dce3456768526c46da65 /internal/fusefrontend_reverse/virtualconf.go | |
parent | 527609266386890fe1657c225c643ca448bcd71d (diff) |
v2api/reverse: implement Read
Diffstat (limited to 'internal/fusefrontend_reverse/virtualconf.go')
-rw-r--r-- | internal/fusefrontend_reverse/virtualconf.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/fusefrontend_reverse/virtualconf.go b/internal/fusefrontend_reverse/virtualconf.go new file mode 100644 index 0000000..8620f6d --- /dev/null +++ b/internal/fusefrontend_reverse/virtualconf.go @@ -0,0 +1,55 @@ +package fusefrontend_reverse + +import ( + "context" + "sync" + "syscall" + + "github.com/hanwen/go-fuse/v2/fs" + "github.com/hanwen/go-fuse/v2/fuse" +) + +var _ = (fs.NodeOpener)((*VirtualConfNode)(nil)) + +type VirtualConfNode struct { + fs.Inode + + path string +} + +func (n *VirtualConfNode) Open(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) { + fd, err := syscall.Open(n.path, syscall.O_RDONLY, 0) + if err != nil { + errno = fs.ToErrno(err) + return + } + fh = &VirtualConfFile{fd: fd} + return +} + +// Check that we have implemented the fs.File* interfaces +var _ = (fs.FileReader)((*VirtualConfFile)(nil)) +var _ = (fs.FileReleaser)((*VirtualConfFile)(nil)) + +type VirtualConfFile struct { + mu sync.Mutex + fd int +} + +func (f *VirtualConfFile) Read(ctx context.Context, buf []byte, off int64) (res fuse.ReadResult, errno syscall.Errno) { + f.mu.Lock() + defer f.mu.Unlock() + res = fuse.ReadResultFd(uintptr(f.fd), off, len(buf)) + return +} + +func (f *VirtualConfFile) Release(ctx context.Context) syscall.Errno { + f.mu.Lock() + defer f.mu.Unlock() + if f.fd != -1 { + err := syscall.Close(f.fd) + f.fd = -1 + return fs.ToErrno(err) + } + return syscall.EBADF +} |