forked from Elara6331/itd
Implemented Write
This commit is contained in:
parent
a8dc017a48
commit
2a8de96773
58
fuse/main.go
58
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,11 +215,56 @@ 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
|
||||
if openFlags&syscall.O_RDWR != 0 {
|
||||
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
|
||||
@ -231,16 +276,17 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
|
||||
return nil, 0, syscall.EROFS
|
||||
}
|
||||
|
||||
fh = &bytesFileHandle{
|
||||
fh = &bytesFileReadHandle{
|
||||
content: buf,
|
||||
}
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user