Commit d75bba60 authored by Ivan Krasin's avatar Ivan Krasin

Introduced parseInvoke to have parseLogic in one place

parent 53596189
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io"
"os" "os"
"path" "path"
) )
...@@ -81,21 +80,21 @@ func dispatch(fs FileSystem, h *InHeader, r *bytes.Buffer, c *managerClient, err ...@@ -81,21 +80,21 @@ func dispatch(fs FileSystem, h *InHeader, r *bytes.Buffer, c *managerClient, err
fmt.Printf("Opcode: %v, NodeId: %v, h: %v\n", h.Opcode, h.NodeId, h) fmt.Printf("Opcode: %v, NodeId: %v, h: %v\n", h.Opcode, h.NodeId, h)
switch h.Opcode { switch h.Opcode {
case FUSE_INIT: case FUSE_INIT:
return initFuse(fs, h, r, c) return parseInvoke(initFuse, fs, h, r, c, new(InitIn))
case FUSE_FORGET: case FUSE_FORGET:
return nil return nil
case FUSE_GETATTR: case FUSE_GETATTR:
return getAttr(fs, h, r, c) return parseInvoke(getAttr, fs, h, r, c, new(GetAttrIn))
case FUSE_GETXATTR: case FUSE_GETXATTR:
return getXAttr(h, r, c) return parseInvoke(getXAttr, fs, h, r, c, new(GetXAttrIn))
case FUSE_OPENDIR: case FUSE_OPENDIR:
return openDir(h, r, c) return parseInvoke(openDir, fs, h, r, c, new(OpenIn))
case FUSE_READDIR: case FUSE_READDIR:
return readDir(h, r, c) return parseInvoke(readDir, fs, h, r, c, new(ReadIn))
case FUSE_LOOKUP: case FUSE_LOOKUP:
return lookup(h, r, c) return lookup(h, r, c)
case FUSE_RELEASEDIR: case FUSE_RELEASEDIR:
return releaseDir(h, r, c) return parseInvoke(releaseDir, fs, h, r, c, new(ReleaseIn))
default: default:
errors <- os.NewError(fmt.Sprintf("Unsupported OpCode: %d", h.Opcode)) errors <- os.NewError(fmt.Sprintf("Unsupported OpCode: %d", h.Opcode))
return serialize(h, ENOSYS, nil) return serialize(h, ENOSYS, nil)
...@@ -103,6 +102,26 @@ func dispatch(fs FileSystem, h *InHeader, r *bytes.Buffer, c *managerClient, err ...@@ -103,6 +102,26 @@ func dispatch(fs FileSystem, h *InHeader, r *bytes.Buffer, c *managerClient, err
return return
} }
func parse(b *bytes.Buffer, data interface{}) bool {
err := binary.Read(b, binary.LittleEndian, data)
if err == nil {
return true
}
if err == os.EOF {
return false
}
panic(fmt.Sprintf("Cannot parse %v", data))
}
type handler func(fs FileSystem, h *InHeader, ing interface{}, c *managerClient) [][]byte
func parseInvoke(f handler, fs FileSystem, h *InHeader, b *bytes.Buffer, c *managerClient, ing interface{}) [][]byte {
if parse(b, ing) {
return f(fs, h, ing, c)
}
return serialize(h, EIO, nil)
}
func serialize(h *InHeader, res Status, out interface{}) (data [][]byte) { func serialize(h *InHeader, res Status, out interface{}) (data [][]byte) {
b := new(bytes.Buffer) b := new(bytes.Buffer)
out_data := make([]byte, 0) out_data := make([]byte, 0)
...@@ -131,12 +150,8 @@ func serialize(h *InHeader, res Status, out interface{}) (data [][]byte) { ...@@ -131,12 +150,8 @@ func serialize(h *InHeader, res Status, out interface{}) (data [][]byte) {
return return
} }
func initFuse(fs FileSystem, h *InHeader, r io.Reader, c *managerClient) (data [][]byte) { func initFuse(fs FileSystem, h *InHeader, ing interface{}, c *managerClient) (data [][]byte) {
in := new(InitIn) in, _ := ing.(*InitIn)
err := binary.Read(r, binary.LittleEndian, in)
if err != nil {
return serialize(h, EIO, nil)
}
fmt.Printf("in: %v\n", in) fmt.Printf("in: %v\n", in)
if in.Major != FUSE_KERNEL_VERSION { if in.Major != FUSE_KERNEL_VERSION {
fmt.Printf("Major versions does not match. Given %d, want %d\n", in.Major, FUSE_KERNEL_VERSION) fmt.Printf("Major versions does not match. Given %d, want %d\n", in.Major, FUSE_KERNEL_VERSION)
...@@ -155,12 +170,8 @@ func initFuse(fs FileSystem, h *InHeader, r io.Reader, c *managerClient) (data [ ...@@ -155,12 +170,8 @@ func initFuse(fs FileSystem, h *InHeader, r io.Reader, c *managerClient) (data [
return serialize(h, OK, out) return serialize(h, OK, out)
} }
func getAttr(fs FileSystem, h *InHeader, r io.Reader, c *managerClient) (data [][]byte) { func getAttr(fs FileSystem, h *InHeader, ing interface{}, c *managerClient) (data [][]byte) {
in := new(GetAttrIn) in := ing.(*GetAttrIn)
err := binary.Read(r, binary.LittleEndian, in)
if err != nil {
return serialize(h, EIO, nil)
}
fmt.Printf("FUSE_GETATTR: %v, Fh: %d\n", in, in.Fh) fmt.Printf("FUSE_GETATTR: %v, Fh: %d\n", in, in.Fh)
out := new(AttrOut) out := new(AttrOut)
resp := c.getPath(in.Fh) resp := c.getPath(in.Fh)
...@@ -180,17 +191,13 @@ func getAttr(fs FileSystem, h *InHeader, r io.Reader, c *managerClient) (data [] ...@@ -180,17 +191,13 @@ func getAttr(fs FileSystem, h *InHeader, r io.Reader, c *managerClient) (data []
return serialize(h, res, out) return serialize(h, res, out)
} }
func getXAttr(h *InHeader, r io.Reader, c *managerClient) (data [][]byte) { func getXAttr(fs FileSystem, h *InHeader, ing interface{}, c *managerClient) (data [][]byte) {
out := new(GetXAttrOut) out := new(GetXAttrOut)
return serialize(h, OK, out) return serialize(h, OK, out)
} }
func openDir(h *InHeader, r io.Reader, c *managerClient) (data [][]byte) { func openDir(fs FileSystem, h *InHeader, ing interface{}, c *managerClient) (data [][]byte) {
in := new(OpenIn) in := ing.(*OpenIn)
err := binary.Read(r, binary.LittleEndian, in)
if err != nil {
return serialize(h, EIO, nil)
}
fmt.Printf("FUSE_OPENDIR: %v\n", in) fmt.Printf("FUSE_OPENDIR: %v\n", in)
resp := c.openDir(h.NodeId) resp := c.openDir(h.NodeId)
if resp.status != OK { if resp.status != OK {
...@@ -201,12 +208,8 @@ func openDir(h *InHeader, r io.Reader, c *managerClient) (data [][]byte) { ...@@ -201,12 +208,8 @@ func openDir(h *InHeader, r io.Reader, c *managerClient) (data [][]byte) {
return serialize(h, OK, out) return serialize(h, OK, out)
} }
func readDir(h *InHeader, r io.Reader, c *managerClient) (data [][]byte) { func readDir(fs FileSystem, h *InHeader, ing interface{}, c *managerClient) (data [][]byte) {
in := new(ReadIn) in := ing.(*ReadIn)
err := binary.Read(r, binary.LittleEndian, in)
if err != nil {
return serialize(h, EIO, nil)
}
fmt.Printf("FUSE_READDIR: %v\n", in) fmt.Printf("FUSE_READDIR: %v\n", in)
resp := c.getDirReader(h.NodeId, in.Fh) resp := c.getDirReader(h.NodeId, in.Fh)
if resp.status != OK { if resp.status != OK {
...@@ -234,7 +237,7 @@ func readDir(h *InHeader, r io.Reader, c *managerClient) (data [][]byte) { ...@@ -234,7 +237,7 @@ func readDir(h *InHeader, r io.Reader, c *managerClient) (data [][]byte) {
dirent.Ino = entry.nodeId dirent.Ino = entry.nodeId
dirent.NameLen = uint32(len(entry.name)) dirent.NameLen = uint32(len(entry.name))
dirent.Typ = (entry.mode & 0170000) >> 12 dirent.Typ = (entry.mode & 0170000) >> 12
err = binary.Write(buf, binary.LittleEndian, dirent) err := binary.Write(buf, binary.LittleEndian, dirent)
if err != nil { if err != nil {
panic("Serialization of Dirent failed") panic("Serialization of Dirent failed")
} }
...@@ -264,12 +267,8 @@ func lookup(h *InHeader, r *bytes.Buffer, c *managerClient) (data [][]byte) { ...@@ -264,12 +267,8 @@ func lookup(h *InHeader, r *bytes.Buffer, c *managerClient) (data [][]byte) {
return serialize(h, OK, out) return serialize(h, OK, out)
} }
func releaseDir(h *InHeader, r io.Reader, c *managerClient) (data [][]byte) { func releaseDir(fs FileSystem, h *InHeader, ing interface{}, c *managerClient) (data [][]byte) {
in := new(ReleaseIn) in := ing.(*ReleaseIn)
err := binary.Read(r, binary.LittleEndian, in)
if err != nil {
return serialize(h, EIO, nil)
}
fmt.Printf("FUSE_RELEASEDIR: %v\n", in) fmt.Printf("FUSE_RELEASEDIR: %v\n", in)
resp := c.closeDir(h.NodeId, in.Fh) resp := c.closeDir(h.NodeId, in.Fh)
if resp.status != OK { if resp.status != OK {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment