Implemented Write
This commit is contained in:
parent
a8dc017a48
commit
2a8de96773
76
fuse/main.go
76
fuse/main.go
@ -202,12 +202,12 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
|
|||||||
return nil, syscall.ENOENT
|
return nil, syscall.ENOENT
|
||||||
}
|
}
|
||||||
|
|
||||||
type bytesFileHandle struct {
|
type bytesFileReadHandle struct {
|
||||||
content []byte
|
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))
|
end := off + int64(len(dest))
|
||||||
if end > int64(len(fh.content)) {
|
if end > int64(len(fh.content)) {
|
||||||
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
|
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))
|
var _ = (fs.NodeOpener)((*ITNode)(nil))
|
||||||
func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
|
func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
|
||||||
switch f.kind {
|
switch f.kind {
|
||||||
case 2:
|
case 2:
|
||||||
// FS file
|
// FS file
|
||||||
fp, err := myfs.Open(f.path)
|
if openFlags&syscall.O_RDWR != 0 {
|
||||||
if err != nil {
|
|
||||||
return nil, 0, syscall.EROFS
|
return nil, 0, syscall.EROFS
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, f.self.size);
|
if openFlags & syscall.O_WRONLY != 0 {
|
||||||
nread, err := fp.Read(buf)
|
fh = &bytesFileWriteHandle{
|
||||||
if err != nil || nread != int(f.self.size) {
|
path : f.path,
|
||||||
return nil, 0, syscall.EROFS
|
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{
|
buf := make([]byte, f.self.size);
|
||||||
content: buf,
|
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:
|
case 3:
|
||||||
// Device file
|
// Device file
|
||||||
|
|
||||||
// disallow writes
|
// 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
|
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
|
return nil, 0, syscall.EROFS
|
||||||
}
|
}
|
||||||
|
|
||||||
fh = &bytesFileHandle{
|
fh = &bytesFileReadHandle{
|
||||||
content: ans,
|
content: ans,
|
||||||
}
|
}
|
||||||
return fh, fuse.FOPEN_DIRECT_IO, 0
|
return fh, fuse.FOPEN_DIRECT_IO, 0
|
||||||
|
Loading…
Reference in New Issue
Block a user