Use FileSystem in hellofs.

geesefs-0-30-9
Aaron Jacobs 2015-03-25 09:29:24 +11:00
parent 54d3fee527
commit a01f4b6ae5
1 changed files with 19 additions and 62 deletions

View File

@ -33,56 +33,20 @@ import (
// //
// Each file contains the string "Hello, world!". // Each file contains the string "Hello, world!".
func NewHelloFS(clock timeutil.Clock) (server fuse.Server, err error) { func NewHelloFS(clock timeutil.Clock) (server fuse.Server, err error) {
server = &helloFS{ fs := &helloFS{
Clock: clock, Clock: clock,
} }
server = fuseutil.NewFileSystemServer(fs)
return return
} }
type helloFS struct { type helloFS struct {
fuseutil.NotImplementedFileSystem
Clock timeutil.Clock Clock timeutil.Clock
} }
func (fs *helloFS) ServeOps(c *fuse.Connection) {
for {
op, err := c.ReadOp()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
switch typed := op.(type) {
case *fuseops.InitOp:
fs.init(typed)
case *fuseops.LookUpInodeOp:
fs.lookUpInode(typed)
case *fuseops.GetInodeAttributesOp:
fs.getInodeAttributes(typed)
case *fuseops.OpenDirOp:
fs.openDir(typed)
case *fuseops.ReadDirOp:
fs.readDir(typed)
case *fuseops.OpenFileOp:
fs.openFile(typed)
case *fuseops.ReadFileOp:
fs.readFile(typed)
default:
typed.Respond(fuse.ENOSYS)
}
}
}
const ( const (
rootInode fuseops.InodeID = fuseops.RootInodeID + iota rootInode fuseops.InodeID = fuseops.RootInodeID + iota
helloInode helloInode
@ -183,14 +147,11 @@ func (fs *helloFS) patchAttributes(
attr.Crtime = now attr.Crtime = now
} }
func (fs *helloFS) init(op *fuseops.InitOp) { func (fs *helloFS) Init(op *fuseops.InitOp) (err error) {
op.Respond(nil) return
} }
func (fs *helloFS) lookUpInode(op *fuseops.LookUpInodeOp) { func (fs *helloFS) LookUpInode(op *fuseops.LookUpInodeOp) (err error) {
var err error
defer func() { op.Respond(err) }()
// Find the info for the parent. // Find the info for the parent.
parentInfo, ok := gInodeInfo[op.Parent] parentInfo, ok := gInodeInfo[op.Parent]
if !ok { if !ok {
@ -214,10 +175,8 @@ func (fs *helloFS) lookUpInode(op *fuseops.LookUpInodeOp) {
return return
} }
func (fs *helloFS) getInodeAttributes(op *fuseops.GetInodeAttributesOp) { func (fs *helloFS) GetInodeAttributes(
var err error op *fuseops.GetInodeAttributesOp) (err error) {
defer func() { op.Respond(err) }()
// Find the info for this inode. // Find the info for this inode.
info, ok := gInodeInfo[op.Inode] info, ok := gInodeInfo[op.Inode]
if !ok { if !ok {
@ -234,15 +193,14 @@ func (fs *helloFS) getInodeAttributes(op *fuseops.GetInodeAttributesOp) {
return return
} }
func (fs *helloFS) openDir(op *fuseops.OpenDirOp) { func (fs *helloFS) OpenDir(
op *fuseops.OpenDirOp) (err error) {
// Allow opening any directory. // Allow opening any directory.
op.Respond(nil) return
} }
func (fs *helloFS) readDir(op *fuseops.ReadDirOp) { func (fs *helloFS) ReadDir(
var err error op *fuseops.ReadDirOp) (err error) {
defer func() { op.Respond(err) }()
// Find the info for this inode. // Find the info for this inode.
info, ok := gInodeInfo[op.Inode] info, ok := gInodeInfo[op.Inode]
if !ok { if !ok {
@ -277,15 +235,14 @@ func (fs *helloFS) readDir(op *fuseops.ReadDirOp) {
return return
} }
func (fs *helloFS) openFile(op *fuseops.OpenFileOp) { func (fs *helloFS) OpenFile(
op *fuseops.OpenFileOp) (err error) {
// Allow opening any file. // Allow opening any file.
op.Respond(nil) return
} }
func (fs *helloFS) readFile(op *fuseops.ReadFileOp) { func (fs *helloFS) ReadFile(
var err error op *fuseops.ReadFileOp) (err error) {
defer func() { op.Respond(err) }()
// Let io.ReaderAt deal with the semantics. // Let io.ReaderAt deal with the semantics.
reader := strings.NewReader("Hello, world!") reader := strings.NewReader("Hello, world!")