From 2a8de96773a730bee37d7301ff892ec0fed936f3 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Sun, 19 Feb 2023 19:00:20 +0000 Subject: [PATCH] Implemented Write --- fuse/main.go | 76 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/fuse/main.go b/fuse/main.go index 769f593..a0643bb 100644 --- a/fuse/main.go +++ b/fuse/main.go @@ -202,12 +202,12 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (* return nil, syscall.ENOENT } -type bytesFileHandle struct { +type bytesFileReadHandle struct { content []byte } -var _ = (fs.FileReader)((*bytesFileHandle)(nil)) +var _ = (fs.FileReader)((*bytesFileReadHandle)(nil)) -func (fh *bytesFileHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { +func (fh *bytesFileReadHandle) 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)) @@ -215,32 +215,78 @@ func (fh *bytesFileHandle) Read(ctx context.Context, dest []byte, off int64) (fu return fuse.ReadResultData(fh.content[off:end]), 0 } +type bytesFileWriteHandle struct { + content []byte + path string +} + +var _ = (fs.FileWriter)((*bytesFileWriteHandle)(nil)) +func (fh *bytesFileWriteHandle) Write(ctx context.Context, data []byte, off int64) (written uint32, errno syscall.Errno) { + if off != int64(len(fh.content)) { + } + fh.content = append(fh.content[:], data[:]...) + return uint32(len(data)), 0 +} + +var _ = (fs.FileFlusher)((*bytesFileWriteHandle)(nil)) +func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno) { + fp, err := myfs.Create(fh.path, uint32(len(fh.content))) + if err != nil { + return syscall.EROFS + } + nread, err := fp.Write(fh.content) + if err != nil || nread != len(fh.content) { + return syscall.EROFS + } + + return 0 +} + +var _ = (fs.NodeSetattrer)((*ITNode)(nil)) +func (bn *ITNode) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) syscall.Errno { + out.Size = 0; + out.Mtime = 0; + return 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 2: // FS file - fp, err := myfs.Open(f.path) - if err != nil { + if openFlags&syscall.O_RDWR != 0 { return nil, 0, syscall.EROFS } - buf := make([]byte, f.self.size); - nread, err := fp.Read(buf) - if err != nil || nread != int(f.self.size) { - return nil, 0, syscall.EROFS - } + if openFlags & syscall.O_WRONLY != 0 { + fh = &bytesFileWriteHandle{ + path : f.path, + content : make([]byte, 0), + } + return fh, fuse.FOPEN_DIRECT_IO, 0 + } else { + fp, err := myfs.Open(f.path) + if err != nil { + return nil, 0, syscall.EROFS + } - fh = &bytesFileHandle{ - content: buf, + buf := make([]byte, f.self.size); + nread, err := fp.Read(buf) + if err != nil || nread != int(f.self.size) { + return nil, 0, syscall.EROFS + } + + fh = &bytesFileReadHandle{ + content: buf, + } + return fh, fuse.FOPEN_DIRECT_IO, 0 } - return fh, fuse.FOPEN_DIRECT_IO, 0 case 3: // Device file // disallow writes - if fuseFlags&(syscall.O_RDWR|syscall.O_WRONLY) != 0 { + if openFlags&(syscall.O_RDWR|syscall.O_WRONLY) != 0 { return nil, 0, syscall.EROFS } @@ -251,7 +297,7 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, return nil, 0, syscall.EROFS } - fh = &bytesFileHandle{ + fh = &bytesFileReadHandle{ content: ans, } return fh, fuse.FOPEN_DIRECT_IO, 0