Commit 13419d09 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Use file.Write() for returning results, if possible. Simplify

serialization code.
parent 55e582f4
...@@ -37,8 +37,9 @@ type fuseRequest struct { ...@@ -37,8 +37,9 @@ type fuseRequest struct {
status Status status Status
flatData []byte flatData []byte
// The stuff we send back to the kernel. // Header+data for what we send back to the kernel.
serialized [][]byte // May be followed by flatData.
output []byte
// Start timestamp for timing info. // Start timestamp for timing info.
startNs int64 startNs int64
...@@ -192,14 +193,21 @@ func (me *MountState) Error(err os.Error) { ...@@ -192,14 +193,21 @@ func (me *MountState) Error(err os.Error) {
} }
func (me *MountState) Write(req *fuseRequest) { func (me *MountState) Write(req *fuseRequest) {
if req.serialized == nil { if req.output == nil {
return return
} }
_, err := Writev(me.mountFile.Fd(), req.serialized) var err os.Error
if req.flatData == nil {
_, err = me.mountFile.Write(req.output)
} else {
_, err = Writev(me.mountFile.Fd(),
[][]byte{req.output, req.flatData})
}
if err != nil { if err != nil {
me.Error(os.NewError(fmt.Sprintf("writer: Writev %v failed, err: %v. Opcode: %v", me.Error(os.NewError(fmt.Sprintf("writer: Writev %v failed, err: %v. Opcode: %v",
req.serialized, err, operationName(req.inHeader.Opcode)))) req.output, err, operationName(req.inHeader.Opcode))))
} }
} }
...@@ -477,13 +485,11 @@ func (me *MountState) dispatch(req *fuseRequest) { ...@@ -477,13 +485,11 @@ func (me *MountState) dispatch(req *fuseRequest) {
} }
func serialize(req *fuseRequest, debug bool) { func serialize(req *fuseRequest, debug bool) {
out_data := make([]byte, 0) headerBytes := make([]byte, SizeOfOutHeader)
b := new(bytes.Buffer) buf := bytes.NewBuffer(headerBytes)
if req.data != nil && req.status == OK { if req.data != nil && req.status == OK {
err := binary.Write(b, binary.LittleEndian, req.data) err := binary.Write(buf, binary.LittleEndian, req.data)
if err == nil { if err != nil {
out_data = b.Bytes()
} else {
panic(fmt.Sprintf("Can't serialize out: %v, err: %v", req.data, err)) panic(fmt.Sprintf("Can't serialize out: %v, err: %v", req.data, err))
} }
} }
...@@ -491,15 +497,17 @@ func serialize(req *fuseRequest, debug bool) { ...@@ -491,15 +497,17 @@ func serialize(req *fuseRequest, debug bool) {
var hOut OutHeader var hOut OutHeader
hOut.Unique = req.inHeader.Unique hOut.Unique = req.inHeader.Unique
hOut.Status = -req.status hOut.Status = -req.status
hOut.Length = uint32(len(out_data) + SizeOfOutHeader + len(req.flatData)) hOut.Length = uint32(buf.Len() + len(req.flatData))
data := buf.Bytes()
b = new(bytes.Buffer) buf = bytes.NewBuffer(data[:0])
err := binary.Write(b, binary.LittleEndian, &hOut) err := binary.Write(buf, binary.LittleEndian, &hOut)
if err != nil { if err != nil {
panic("Can't serialize OutHeader") panic("Can't serialize OutHeader")
} }
req.serialized = [][]byte{b.Bytes(), out_data, req.flatData} req.output = data
if debug { if debug {
val := fmt.Sprintf("%v", req.data) val := fmt.Sprintf("%v", req.data)
max := 1024 max := 1024
......
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